From 3b7a6cc50528f4a7c0cef15a4def7e43c715d0b4 Mon Sep 17 00:00:00 2001 From: tali Date: Sun, 11 Apr 2021 15:34:55 +0200 Subject: [PATCH] Ajout tableaux et pointeurs avec AFCA --- Fichiers_Tests/progC | 15 ++++++--- Lex_Yacc/al.lex | 2 ++ Lex_Yacc/as.y | 53 +++++++++++++++++++------------ Makefile | 2 +- Tables/Instructions/tab_instruc.c | 11 ++++--- Tables/Instructions/tab_instruc.h | 2 +- Tables/Symboles/table_symboles.c | 22 +++++++------ Tables/Symboles/table_symboles.h | 6 ++-- 8 files changed, 68 insertions(+), 45 deletions(-) diff --git a/Fichiers_Tests/progC b/Fichiers_Tests/progC index a0ec383..82cdf9f 100644 --- a/Fichiers_Tests/progC +++ b/Fichiers_Tests/progC @@ -1,7 +1,12 @@ int main(){ - int * b = 4 * 5; - int ** c,d; - int ** y = &b; - * c = 1; - &b = 2; + int b[3]; + if (1){ + int a = b[1]; + } + else if (2){ + int * c = &b[2]; + } + else{ + b[0] = 80; + } } diff --git a/Lex_Yacc/al.lex b/Lex_Yacc/al.lex index 8f5086b..f541dbf 100644 --- a/Lex_Yacc/al.lex +++ b/Lex_Yacc/al.lex @@ -29,6 +29,8 @@ yyerror (char const *s) "||" { return tOR; } "else" { return tELSE;} "&" { return tADDR;} +"[" { return tOCROCH;} +"]" { return tCCROCH;} [0-9]+ { yylval.nombre = atoi(yytext); return tNB; } diff --git a/Lex_Yacc/as.y b/Lex_Yacc/as.y index 4d81492..77ce8c6 100644 --- a/Lex_Yacc/as.y +++ b/Lex_Yacc/as.y @@ -20,6 +20,7 @@ int nbs_instructions_to_patch[10]; %token tMAIN %token tOBRACKET tCBRACKET %token tOBRACE tCBRACE +%token tOCROCH tCCROCH %token tINT %token tCONST %token tPV tCOMA @@ -33,14 +34,14 @@ int nbs_instructions_to_patch[10]; %token tAND tOR %token tADDR -%left tAND tOR -%left tNOT %left tLT tGT %left tEQCOND +%left tAND tOR +%left tNOT %left tADD tSUB %left tMUL tDIV -%type E Invocation DebutAff +%type E Invocation DebutAff SuiteAffPointeur DebutAffPointeur EBeforeAddr @@ -63,11 +64,11 @@ Body : tOBRACKET {profondeur++;} Instructions tCBRACKET {print(); reset_pronf(); Instructions : Instruction Instructions ; Instructions : ; -Instruction : Aff {reset_temp_vars();}; -Instruction : Decl {reset_temp_vars();}; -Instruction : Invocation tPV{reset_temp_vars();}; -Instruction : If {reset_temp_vars();}; -Instruction : While {reset_temp_vars();}; +Instruction : Aff {}; +Instruction : Decl {}; +Instruction : Invocation tPV{}; +Instruction : If {}; +Instruction : While {}; //On considère que la première ligne du code en ASM est la ligne 0 If : tIF tOBRACE E tCBRACE { @@ -76,8 +77,9 @@ 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");}; + nbs_instructions_to_patch[profondeur]++; + decrement_temp_var();} +Else {printf("If reconnu\n");}; Else : tELSE If { printf("Else if reconnu\n"); }; @@ -94,7 +96,8 @@ for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){ nbs_instructions_to_patch[profondeur] = 0;}; While : tWHILE tOBRACE E tCBRACE { add_operation(JMF,$3,0,0); -$1 = get_current_index() - 1;} +$1 = get_current_index() - 1; +decrement_temp_var();} Body { printf("While reconnu\n"); int current = get_current_index(); @@ -102,16 +105,19 @@ patch($1,current + 1); add_operation(JMP,$1,0,0);}; -Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0);} ; //besoin de get_address +Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0); decrement_temp_var();} ; +Aff : DebutAffPointeur tEQ E tPV {add_operation(WR,$1,$3,0); decrement_temp_var(); decrement_temp_var();}; 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;}; + +DebutAffPointeur : tMUL SuiteAffPointeur {add_operation(READ, $2, $2, 0); $$=$2;}; +DebutAffPointeur : SuiteAffPointeur {$$=$1;}; +SuiteAffPointeur : tMUL tID {struct symbole_t * symbole = get_variable($2); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(COP, addr,symbole->adresse,0); $$=addr;}; +SuiteAffPointeur : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(AFC, addr,symbole->adresse,0); int addr2 = allocate_mem_temp_var(INT); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); $$=$3; decrement_temp_var(); decrement_temp_var();}; + 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 : 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"); add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();}; E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; decrement_temp_var();}; E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; decrement_temp_var();}; @@ -125,13 +131,17 @@ E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; decrement_tem E : tNOT E { printf("!\n"); }; E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();}; 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;}; +E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;}; +E : tADDR EBeforeAddr {add_operation(AFCA,$2, $2,0); $$=$2;}; +E : EBeforeAddr {$$=$1;}; +EBeforeAddr : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(AFC, addr,symbole->adresse,0); int addr2 = allocate_mem_temp_var(INT); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); add_operation(READ,$3,$3,0); $$=$3; decrement_temp_var(); decrement_temp_var();}; +EBeforeAddr : 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;}; + //Créer un champ isConst dans la table des symboles -Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; printf("Type int\n");} ; +Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; type_courant.nb_blocs = 1; 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 : ; @@ -140,11 +150,12 @@ Decl : Type SuiteDecl FinDecl ; Decl : tCONST Type SuiteDeclConst FinDeclConst; 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 {decrement_temp_var(); int addr = push($1,1, type_courant);}; +SuiteDecl : tID tOCROCH tNB tCCROCH {type_courant.pointeur_level++; decl_tab($1,type_courant,$3); type_courant.nb_blocs = 1;} ; FinDecl : tPV { printf("Fin Decl\n");}; FinDecl : tCOMA SuiteDecl FinDecl ; -SuiteDeclConst : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);}; +SuiteDeclConst : tID tEQ E {decrement_temp_var(); int addr = push($1,1, type_courant);}; FinDeclConst : tPV; FinDeclConst : tCOMA SuiteDeclConst FinDeclConst; diff --git a/Makefile b/Makefile index f90b52a..375cbfa 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ clean_Lex_Yacc: @rm -f Lex_Yacc/as.output Lex_Yacc/as.tab.* Lex_Yacc/lex.yy.* 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 -ly -o rondoudou_gcc + 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 build_Symboles: clean_Symboles gcc -c Tables/Symboles/table_symboles.c -o Tables/Symboles/table_symboles.o diff --git a/Tables/Instructions/tab_instruc.c b/Tables/Instructions/tab_instruc.c index 8a0768a..7811f2e 100644 --- a/Tables/Instructions/tab_instruc.c +++ b/Tables/Instructions/tab_instruc.c @@ -36,6 +36,9 @@ char * get_asm_line_from_op(struct operation_t op){ case (AFC): sprintf(buffer,"AFC %d %d\n",op.arg1, op.arg2); break; + case (AFCA): + sprintf(buffer,"AFCA %d %d\n",op.arg1, op.arg2); + break; case (JMP): sprintf(buffer,"JMP %d\n",op.arg1); break; @@ -54,11 +57,11 @@ char * get_asm_line_from_op(struct operation_t op){ case (PRI): sprintf(buffer,"PRI %d\n",op.arg1); break; - case (GET): - sprintf(buffer,"GET %d %d\n",op.arg1, op.arg2); + case (READ): + sprintf(buffer,"READ %d %d\n",op.arg1, op.arg2); break; - case (MOV): - sprintf(buffer,"MOV %d %d\n",op.arg1, op.arg2); + case (WR): + sprintf(buffer,"WR %d %d\n",op.arg1, op.arg2); break; } return buffer; diff --git a/Tables/Instructions/tab_instruc.h b/Tables/Instructions/tab_instruc.h index fe09134..32d1f06 100644 --- a/Tables/Instructions/tab_instruc.h +++ b/Tables/Instructions/tab_instruc.h @@ -4,7 +4,7 @@ #include -enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,JMP,JMF,INF,SUP,EQU,PRI,GET,MOV}; +enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,AFCA,JMP,JMF,INF,SUP,EQU,PRI,READ,WR}; struct operation_t { enum opcode_t opcode; diff --git a/Tables/Symboles/table_symboles.c b/Tables/Symboles/table_symboles.c index 0f106b8..b705a76 100644 --- a/Tables/Symboles/table_symboles.c +++ b/Tables/Symboles/table_symboles.c @@ -79,6 +79,8 @@ void init (void) { pile->taille = 0; } + + int push(char * nom, int isInit, struct type_t type) { struct element_t * aux = malloc(sizeof(struct element_t)); struct symbole_t symbole = {"", last_addr, type, isInit,profondeur}; @@ -105,6 +107,11 @@ struct symbole_t pop() { } return retour; } + +struct symbole_t decl_tab(char * name, struct type_t type, int nb_blocs){ + push(name,0,type); + last_addr += (nb_blocs-1)*taille_types[type.base]; +} char status(char * nom) { char retour = 0; @@ -161,16 +168,11 @@ int get_last_addr(){ int allocate_mem_temp_var(enum base_type_t type){ - last_temp_var_size = taille_types[type]; - temp_addr -= last_temp_var_size; - return temp_addr; + struct type_t type_bis = {type,0}; + int addr = push("",1,type_bis); + return addr; } -void reset_temp_vars(){ - temp_addr = MAXADDR; -} - - void reset_pronf(){ printf("Profondeur dans reset : %d\n", profondeur); while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){ @@ -178,6 +180,6 @@ void reset_pronf(){ } } -void decrement_temp_var(struct type_t type){ - temp_addr += last_temp_var_size; +void decrement_temp_var(){ + pop(); } diff --git a/Tables/Symboles/table_symboles.h b/Tables/Symboles/table_symboles.h index b95a3d9..fc798c5 100644 --- a/Tables/Symboles/table_symboles.h +++ b/Tables/Symboles/table_symboles.h @@ -33,8 +33,9 @@ extern int taille_types[]; extern int profondeur; struct type_t { - enum base_type_t base; + enum base_type_t base; int pointeur_level; + int nb_blocs; }; struct symbole_t { @@ -45,14 +46,13 @@ struct symbole_t { int profondeur; }; - - void print_symbole(struct symbole_t symbole); void init(void); int push(char * nom, int isInit, struct type_t type); struct symbole_t pop(); +struct symbole_t decl_tab(char * name, struct type_t type, int nb_blocs); // renvoi 0 si nom n'existe pas, 2 si nom existe sans etre initialisée, 1 sinon char status(char * nom); void print();