Les tableaux c'est ENFIN bon normalement

This commit is contained in:
Elies Tali 2021-04-13 12:19:58 +02:00
parent f196725be3
commit 2001f717a6
7 changed files with 21 additions and 16 deletions

View file

@ -1,6 +1,5 @@
int main(){ int main(){
int b[3]; int b[3];
int a;
a = b[1];
int * c = &b[2]; int * c = &b[2];
int * f = &b[1];
} }

View file

@ -113,7 +113,7 @@ DebutAff : tID {struct symbole_t * symbole = get_variable($1); symbole->initial
DebutAffPointeur : tMUL SuiteAffPointeur {add_operation(READ, $2, $2, 0); $$=$2;}; DebutAffPointeur : tMUL SuiteAffPointeur {add_operation(READ, $2, $2, 0); $$=$2;};
DebutAffPointeur : SuiteAffPointeur {$$=$1;}; DebutAffPointeur : SuiteAffPointeur {$$=$1;};
SuiteAffPointeur : tMUL tID {struct symbole_t * symbole = get_variable($2); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(COP, addr,symbole->adresse,0); $$=addr;}; SuiteAffPointeur : tMUL tID {struct symbole_t * symbole = get_variable($2); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
SuiteAffPointeur : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(AFC, addr,symbole->adresse,0); int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); $$=$3; pop(); pop();}; SuiteAffPointeur : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); int addr = push("0_TEMPORARY", 1, symbole->type); if (symbole->type.pointeur_level > 0){add_operation(COP, addr,symbole->adresse,0);} else{add_operation(AFCA, addr,symbole->adresse,0);} int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); $$=$3; pop(); pop();};
E : tNB { int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;}; E : tNB { int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
@ -134,16 +134,16 @@ E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; pop();} ;
E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;}; E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;};
E : tADDR EBis {add_operation(COPA,$2, $2,0); $$=$2;}; E : tADDR EBis {add_operation(COPA,$2, $2,0); $$=$2;};
E : tADDR ETer {add_operation(COPA,$2, $2,0); $$=$2;}; E : tADDR ETer {add_operation(COPA,$2, $2,0); $$=$2;};
E : tID 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 : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); if (symbole->type.isTab){add_operation(AFCA, addr,symbole->adresse,0); } else{add_operation(COP, addr,symbole->adresse,0);} $$=addr;};
E : 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();}; E : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); if(type.pointeur_level > 0) {add_operation(COP, addr,symbole->adresse,0);} else{add_operation(AFCA, addr,symbole->adresse,0);} int addr2 = push("0_TEMPORARY", 1, integer); 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; pop(); pop();};
EBis : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); if(type.pointeur_level > 0) {add_operation(COP, addr,symbole->adresse,0);} else{add_operation(AFCA, addr,symbole->adresse,0);} int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); $$=$3; pop(); pop();};
ETer : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); add_operation(AFCA, addr,symbole->adresse,0); $$=addr;};
EBeforeAddr : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(AFC, addr,symbole->adresse,0); int addr2 = push("0_TEMPORARY", 1, integer); 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; pop(); pop();};
EBeforeAddr : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(COP, 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
Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; type_courant.nb_blocs = 1; printf("Type int\n");} ; Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; type_courant.isTab = 0; type_courant.nb_blocs = 1; printf("Type int\n");} ;
Type : Type tMUL {type_courant.pointeur_level++; 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 : tMUL SuiteType {type_courant.pointeur_level++; printf(" * en plus\n");} ;
//SuiteType : ; //SuiteType : ;
@ -153,7 +153,7 @@ 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 {pop(); int addr = push($1,1, type_courant);}; SuiteDecl : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
SuiteDecl : tID tOCROCH tNB tCCROCH {type_courant.pointeur_level++; type_courant.nb_blocs = $3; push($1, 0, type_courant);} ; SuiteDecl : tID tOCROCH tNB tCCROCH {type_courant.isTab = 1; type_courant.nb_blocs = $3; push($1, 0, type_courant);} ;
FinDecl : tPV { printf("Fin Decl\n");}; FinDecl : tPV { printf("Fin Decl\n");};
FinDecl : tCOMA SuiteDecl FinDecl ; FinDecl : tCOMA SuiteDecl FinDecl ;

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 -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 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

@ -36,6 +36,9 @@ char * get_asm_line_from_op(struct operation_t op){
case (AFC): case (AFC):
sprintf(buffer,"AFC %d %d\n",op.arg1, op.arg2); sprintf(buffer,"AFC %d %d\n",op.arg1, op.arg2);
break; break;
case (AFCA):
sprintf(buffer,"AFCA %d %d\n",op.arg1, op.arg2);
break;
case (COPA): case (COPA):
sprintf(buffer,"COPA %d %d\n",op.arg1, op.arg2); sprintf(buffer,"COPA %d %d\n",op.arg1, op.arg2);
break; break;

View file

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

View file

@ -67,9 +67,9 @@ char * type_to_string(struct type_t type) {
void print_symbole(struct symbole_t symbole) { void print_symbole(struct symbole_t symbole) {
char * type = type_to_string(symbole.type); 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, symbole.profondeur); printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d, isTab : %d}\n", symbole.nom, symbole.adresse, type, symbole.profondeur,symbole.type.isTab);
} else { } else {
printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", symbole.nom, symbole.adresse, type,symbole.profondeur); printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d, isTab : %d}\n", symbole.nom, symbole.adresse, type,symbole.profondeur,symbole.type.isTab);
} }
free(type); free(type);
} }
@ -130,9 +130,9 @@ void print() {
int i; int i;
for (i=0; i < pile->taille; i++) { for (i=0; i < pile->taille; i++) {
if (aux->symbole.initialized) { if (aux->symbole.initialized) {
printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur); printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d, isTab : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur, aux->symbole.type.isTab);
} else { } else {
printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur); printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d, isTab : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur, aux->symbole.type.isTab);
} }
aux = aux->suivant; aux = aux->suivant;
} }

View file

@ -51,8 +51,11 @@ struct type_t {
enum base_type_t base; enum base_type_t base;
//Si la variable est un pointeur, on enregitre son niveau (nombre de *) //Si la variable est un pointeur, on enregitre son niveau (nombre de *)
int pointeur_level; int pointeur_level;
//Si il s'agit d'un tableau, on enregistre sa taille (nombre de cases) //Si il s'agit d'un tableau, on enregistre sa taille (nombre de cases) (même si c'est une variable on met quand même un nb_blocs à)
int nb_blocs; int nb_blocs;
//Si c'est un tableau, cette valeur est à 1
int isTab;
}; };
//Constante pour les entiers //Constante pour les entiers
extern const struct type_t integer; extern const struct type_t integer;