Ajout pointeurs

This commit is contained in:
Tali Elies 2021-04-06 18:14:37 +02:00
parent ae49ecc714
commit fb7127065f
8 changed files with 89 additions and 75 deletions

View file

@ -1,20 +1,7 @@
int main(){ int main(){
int b = 4 * 5; int * b = 4 * 5;
int a = 1; int ** c,d;
while (b){ int ** y = &b;
b = 2; * c = 1;
if (a){ &b = 2;
int u = 8;
}
else{
if (1){
int j = 7;
}
else if (5){
int g = 89;
}
int y = 9;
}
}
int p = 85;
} }

View file

@ -28,6 +28,7 @@ yyerror (char const *s)
"&&" { return tAND; } "&&" { return tAND; }
"||" { return tOR; } "||" { return tOR; }
"else" { return tELSE;} "else" { return tELSE;}
"&" { return tADDR;}
[0-9]+ { yylval.nombre = atoi(yytext); return tNB; } [0-9]+ { yylval.nombre = atoi(yytext); return tNB; }

View file

@ -10,7 +10,7 @@
#include "../Tables/Instructions/tab_instruc.h" #include "../Tables/Instructions/tab_instruc.h"
#define TAILLE 1024 #define TAILLE 1024
enum type_t type_courant; struct type_t type_courant;
int instructions_ligne_to_patch[10][20]; int instructions_ligne_to_patch[10][20];
int nbs_instructions_to_patch[10]; int nbs_instructions_to_patch[10];
@ -31,6 +31,7 @@ int nbs_instructions_to_patch[10];
%token<nombre> tIF tWHILE tELSE %token<nombre> tIF tWHILE tELSE
%token tLT tGT tEQCOND %token tLT tGT tEQCOND
%token tAND tOR %token tAND tOR
%token tADDR
%left tAND tOR %left tAND tOR
%left tNOT %left tNOT
@ -39,7 +40,7 @@ int nbs_instructions_to_patch[10];
%left tADD tSUB %left tADD tSUB
%left tMUL tDIV %left tMUL tDIV
%type<nombre> E Invocation %type<nombre> E Invocation DebutAff
@ -53,11 +54,11 @@ Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
Params : { printf("Sans Params\n"); } ; Params : { printf("Sans Params\n"); } ;
Params : Param SuiteParams ; Params : Param SuiteParams ;
Param : DeclType tID { printf("Prametre : %s\n", $2); }; Param : Type tID { printf("Prametre : %s\n", $2); };
SuiteParams : tCOMA Param SuiteParams ; SuiteParams : tCOMA Param SuiteParams ;
SuiteParams : ; SuiteParams : ;
Body : tOBRACKET Instructions tCBRACKET { } ; Body : tOBRACKET {profondeur++;} Instructions tCBRACKET {print(); reset_pronf(); profondeur--;} ;
Instructions : Instruction Instructions ; Instructions : Instruction Instructions ;
@ -69,18 +70,8 @@ Instruction : If {reset_temp_vars();};
Instruction : While {reset_temp_vars();}; Instruction : While {reset_temp_vars();};
//On considère que la première ligne du code en ASM est la ligne 0 //On considère que la première ligne du code en ASM est la ligne 0
If : tIF tOBRACE E tCBRACE { profondeur++; If : tIF tOBRACE E tCBRACE {
add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;} add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
Body {int current = get_current_index();
patch($1,current + 1);
add_operation(JMP,0,0,0);
instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
nbs_instructions_to_patch[profondeur]++;}
Else { printf("If reconnu\n"); reset_pronf(); profondeur--;};
//On considère que la première ligne du code en ASM est la ligne 0
ElseIf : tIF tOBRACE E tCBRACE {add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
Body {int current = get_current_index(); Body {int current = get_current_index();
patch($1,current + 1); patch($1,current + 1);
add_operation(JMP,0,0,0); add_operation(JMP,0,0,0);
@ -89,9 +80,7 @@ Body {int current = get_current_index();
Else { printf("If reconnu\n");}; Else { printf("If reconnu\n");};
Else : tELSE If { printf("Else if reconnu\n"); };
Else : tELSE ElseIf { printf("Else if reconnu\n"); };
Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index(); Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index();
for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){ for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
patch(instructions_ligne_to_patch[profondeur][i],current); patch(instructions_ligne_to_patch[profondeur][i],current);
@ -103,44 +92,52 @@ for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
patch(instructions_ligne_to_patch[profondeur][i],current); patch(instructions_ligne_to_patch[profondeur][i],current);
} }
nbs_instructions_to_patch[profondeur] = 0;}; nbs_instructions_to_patch[profondeur] = 0;};
While : tWHILE tOBRACE E tCBRACE {profondeur++; While : tWHILE tOBRACE E tCBRACE {
add_operation(JMF,$3,0,0); add_operation(JMF,$3,0,0);
$1 = get_current_index() - 1;} $1 = get_current_index() - 1;}
Body { printf("While reconnu\n"); Body { printf("While reconnu\n");
int current = get_current_index(); int current = get_current_index();
patch($1,current + 1); patch($1,current + 1);
add_operation(JMP,$1,0,0); add_operation(JMP,$1,0,0);};
reset_pronf(); profondeur--;};
Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1); struct symbole_t * symbole = get_variable($1); symbole->initialized = 1; add_operation(COP, symbole->adresse, $3,0);} ; //besoin de get_address Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0);} ; //besoin de get_address
DebutAff : tID {struct symbole_t * symbole = get_variable($1); symbole->initialized = 1; $$=symbole->adresse; printf("%s prend une valeur\n", $1);};
DebutAff : tMUL E { add_operation(GET, $2, $2, 0); $$=$2;};
DebutAff : tADDR tID { int addr = allocate_mem_temp_var(INT); struct symbole_t * symbole = get_variable($2); add_operation(AFC,addr, symbole->adresse,0); $$=addr;};
E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;}; E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;}; E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
E : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); int addr = allocate_mem_temp_var(symbole->type); add_operation(COP, addr,symbole->adresse,0); $$=addr;}; E : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
E : E tMUL E { printf("Mul\n"); int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr,$1,$3); $$ = addr;}; E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
E : E tDIV E { printf("Div\n"); int addr = allocate_mem_temp_var(INT); add_operation(DIV, addr,$1,$3); $$ = addr;}; E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; decrement_temp_var();};
E : E tSUB E { printf("Sub\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, addr,$1,$3); $$ = addr;}; E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; decrement_temp_var();};
E : E tADD E { printf("Add\n"); int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr,$1,$3); $$ = addr;}; E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; decrement_temp_var();};
E : Invocation { printf("Invoc\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;}; E : Invocation { printf("Invoc\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;}; E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
E : tSUB E { printf("Moins\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, 0,addr,0); $$ = addr;}; E : tSUB E { printf("Moins\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,0,0); add_operation(SOU, $2,$2,addr); $$ = $2; decrement_temp_var();};
E : E tEQCOND E { printf("==\n"); int addr = allocate_mem_temp_var(INT); add_operation(EQU, addr,$1,$3); $$ = addr;}; E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; decrement_temp_var();};
E : E tGT E { printf(">\n"); int addr = allocate_mem_temp_var(INT); add_operation(SUP, addr,$1,$3); $$ = addr;}; E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; decrement_temp_var();};
E : E tLT E { printf("<\n"); int addr = allocate_mem_temp_var(INT); add_operation(INF, addr,$1,$3); $$ = addr;}; E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; decrement_temp_var();};
E : tNOT E { printf("!\n"); }; E : tNOT E { printf("!\n"); };
E : E tAND E {int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr, $1, $3); $$=addr;}; E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
E : E tOR E {int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr, $1, $3); $$=addr;} ; E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; decrement_temp_var();} ;
E : tMUL E { add_operation(GET, $2, $2, 0); $$=$2;};
E : tADDR tID { int addr = allocate_mem_temp_var(INT); struct symbole_t * symbole = get_variable($2); add_operation(AFC,addr, symbole->adresse,0); $$=addr;};
//Créer un champ isConst dans la table des symboles //Créer un champ isConst dans la table des symboles
DeclType : tINT {type_courant = INT; printf("Type int\n");} ; Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; printf("Type int\n");} ;
Type : Type tMUL {type_courant.pointeur_level++; printf("Type int *\n");};
//SuiteType : tMUL SuiteType {type_courant.pointeur_level++; printf(" * en plus\n");} ;
//SuiteType : ;
Decl : DeclType SuiteDecl FinDecl ; Decl : Type SuiteDecl FinDecl ;
Decl : tCONST DeclType SuiteDeclConst FinDeclConst; Decl : tCONST Type SuiteDeclConst FinDeclConst;
SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");}; SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);}; SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};

View file

@ -17,7 +17,7 @@ clean_Lex_Yacc:
@rm -f Lex_Yacc/as.output Lex_Yacc/as.tab.* Lex_Yacc/lex.yy.* @rm -f Lex_Yacc/as.output Lex_Yacc/as.tab.* Lex_Yacc/lex.yy.*
build : clean build_Symboles build_Instructions build_Lex_Yacc build : clean build_Symboles build_Instructions build_Lex_Yacc
gcc Lex_Yacc/as.tab.o Lex_Yacc/lex.yy.o Tables/Instructions/tab_instruc.o Tables/Symboles/table_symboles.o -ll -o rondoudou_gcc gcc Lex_Yacc/as.tab.o Lex_Yacc/lex.yy.o Tables/Instructions/tab_instruc.o Tables/Symboles/table_symboles.o -ly -o rondoudou_gcc
build_Symboles: clean_Symboles build_Symboles: clean_Symboles
gcc -c Tables/Symboles/table_symboles.c -o Tables/Symboles/table_symboles.o gcc -c Tables/Symboles/table_symboles.c -o Tables/Symboles/table_symboles.o

View file

@ -54,6 +54,12 @@ char * get_asm_line_from_op(struct operation_t op){
case (PRI): case (PRI):
sprintf(buffer,"PRI %d\n",op.arg1); sprintf(buffer,"PRI %d\n",op.arg1);
break; break;
case (GET):
sprintf(buffer,"GET %d %d\n",op.arg1, op.arg2);
break;
case (MOV):
sprintf(buffer,"MOV %d %d\n",op.arg1, op.arg2);
break;
} }
return buffer; return buffer;
} }

View file

@ -4,7 +4,7 @@
#include <stdio.h> #include <stdio.h>
enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,JMP,JMF,INF,SUP,EQU,PRI}; enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,JMP,JMF,INF,SUP,EQU,PRI,GET,MOV};
struct operation_t { struct operation_t {
enum opcode_t opcode; enum opcode_t opcode;

View file

@ -36,6 +36,7 @@ int last_addr = 0;
int temp_addr = MAXADDR; int temp_addr = MAXADDR;
int taille_types[] = {-1, 4}; int taille_types[] = {-1, 4};
int profondeur = 0; int profondeur = 0;
int last_temp_var_size;
struct element_t { struct element_t {
struct symbole_t symbole; struct symbole_t symbole;
@ -48,20 +49,28 @@ struct pile_t {
}; };
struct pile_t * pile; struct pile_t * pile;
char * type_to_string(enum type_t type) { char * type_to_string(struct type_t type) {
if (type == INT) { char * star = "*";
return "int"; char * resultat = malloc(sizeof(char)*20);
} else { for (int i = 0; i< type.pointeur_level; i++){
return "unknown"; strcat(resultat,star);
}
if (type.base == INT) {
strcat(resultat,"int");
} else {;
strcat(resultat,"unknown");
} }
return resultat;
} }
void print_symbole(struct symbole_t symbole) { void print_symbole(struct symbole_t symbole) {
char * type = type_to_string(symbole.type);
if (symbole.initialized) { if (symbole.initialized) {
printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d}\n", symbole.nom, symbole.adresse, type_to_string(symbole.type), symbole.profondeur); printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d}\n", symbole.nom, symbole.adresse, type, symbole.profondeur);
} else { } else {
printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", symbole.nom, symbole.adresse, type_to_string(symbole.type),symbole.profondeur); printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", symbole.nom, symbole.adresse, type,symbole.profondeur);
} }
free(type);
} }
void init (void) { void init (void) {
@ -70,7 +79,7 @@ void init (void) {
pile->taille = 0; pile->taille = 0;
} }
int push(char * nom, int isInit, enum type_t type) { int push(char * nom, int isInit, struct type_t type) {
struct element_t * aux = malloc(sizeof(struct element_t)); struct element_t * aux = malloc(sizeof(struct element_t));
struct symbole_t symbole = {"", last_addr, type, isInit,profondeur}; struct symbole_t symbole = {"", last_addr, type, isInit,profondeur};
strcpy(symbole.nom,nom); strcpy(symbole.nom,nom);
@ -79,7 +88,7 @@ int push(char * nom, int isInit, enum type_t type) {
pile->first = aux; pile->first = aux;
pile->taille++; pile->taille++;
int addr_var = last_addr; int addr_var = last_addr;
last_addr += taille_types[type]; last_addr += taille_types[type.base];
return addr_var; return addr_var;
} }
@ -92,7 +101,7 @@ struct symbole_t pop() {
retour = aux->symbole; retour = aux->symbole;
free(aux); free(aux);
pile->taille--; pile->taille--;
last_addr -= taille_types[retour.type]; last_addr -= taille_types[retour.type.base];
} }
return retour; return retour;
} }
@ -151,8 +160,9 @@ int get_last_addr(){
} }
int allocate_mem_temp_var(enum type_t type){ int allocate_mem_temp_var(enum base_type_t type){
temp_addr -= taille_types[type]; last_temp_var_size = taille_types[type];
temp_addr -= last_temp_var_size;
return temp_addr; return temp_addr;
} }
@ -162,8 +172,12 @@ void reset_temp_vars(){
void reset_pronf(){ void reset_pronf(){
while (pile->first->symbole.profondeur == profondeur){ printf("Profondeur dans reset : %d\n", profondeur);
pop(); while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){
} pop();
}
} }
void decrement_temp_var(struct type_t type){
temp_addr += last_temp_var_size;
}

View file

@ -27,29 +27,38 @@ Opérations possible :
#include <stdint.h> #include <stdint.h>
enum type_t {UNKNOWN, INT};
enum base_type_t {UNKNOWN, INT};
extern int taille_types[]; extern int taille_types[];
extern int profondeur; extern int profondeur;
struct type_t {
enum base_type_t base;
int pointeur_level;
};
struct symbole_t { struct symbole_t {
char nom[30]; char nom[30];
uintptr_t adresse; uintptr_t adresse;
enum type_t type; struct type_t type;
int initialized; int initialized;
int profondeur; int profondeur;
}; };
void print_symbole(struct symbole_t symbole); void print_symbole(struct symbole_t symbole);
void init(void); void init(void);
int push(char * nom, int isInit, enum type_t type); int push(char * nom, int isInit, struct type_t type);
struct symbole_t pop(); struct symbole_t pop();
// renvoi 0 si nom n'existe pas, 2 si nom existe sans etre initialisée, 1 sinon // renvoi 0 si nom n'existe pas, 2 si nom existe sans etre initialisée, 1 sinon
char status(char * nom); char status(char * nom);
void print(); void print();
int get_last_addr(); int get_last_addr();
struct symbole_t * get_variable(char * nom); struct symbole_t * get_variable(char * nom);
int allocate_mem_temp_var(enum type_t type); int allocate_mem_temp_var(enum base_type_t type);
void reset_temp_vars(); void reset_temp_vars();
void reset_pronf(); void reset_pronf();
void decrement_temp_var();