Ajout tableaux et pointeurs avec AFCA
This commit is contained in:
parent
fb7127065f
commit
3b7a6cc505
8 changed files with 68 additions and 45 deletions
|
@ -1,7 +1,12 @@
|
||||||
int main(){
|
int main(){
|
||||||
int * b = 4 * 5;
|
int b[3];
|
||||||
int ** c,d;
|
if (1){
|
||||||
int ** y = &b;
|
int a = b[1];
|
||||||
* c = 1;
|
}
|
||||||
&b = 2;
|
else if (2){
|
||||||
|
int * c = &b[2];
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
b[0] = 80;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,8 @@ yyerror (char const *s)
|
||||||
"||" { return tOR; }
|
"||" { return tOR; }
|
||||||
"else" { return tELSE;}
|
"else" { return tELSE;}
|
||||||
"&" { return tADDR;}
|
"&" { return tADDR;}
|
||||||
|
"[" { return tOCROCH;}
|
||||||
|
"]" { return tCCROCH;}
|
||||||
|
|
||||||
|
|
||||||
[0-9]+ { yylval.nombre = atoi(yytext); return tNB; }
|
[0-9]+ { yylval.nombre = atoi(yytext); return tNB; }
|
||||||
|
|
|
@ -20,6 +20,7 @@ int nbs_instructions_to_patch[10];
|
||||||
%token tMAIN
|
%token tMAIN
|
||||||
%token tOBRACKET tCBRACKET
|
%token tOBRACKET tCBRACKET
|
||||||
%token tOBRACE tCBRACE
|
%token tOBRACE tCBRACE
|
||||||
|
%token tOCROCH tCCROCH
|
||||||
%token tINT
|
%token tINT
|
||||||
%token tCONST
|
%token tCONST
|
||||||
%token tPV tCOMA
|
%token tPV tCOMA
|
||||||
|
@ -33,14 +34,14 @@ int nbs_instructions_to_patch[10];
|
||||||
%token tAND tOR
|
%token tAND tOR
|
||||||
%token tADDR
|
%token tADDR
|
||||||
|
|
||||||
%left tAND tOR
|
|
||||||
%left tNOT
|
|
||||||
%left tLT tGT
|
%left tLT tGT
|
||||||
%left tEQCOND
|
%left tEQCOND
|
||||||
|
%left tAND tOR
|
||||||
|
%left tNOT
|
||||||
%left tADD tSUB
|
%left tADD tSUB
|
||||||
%left tMUL tDIV
|
%left tMUL tDIV
|
||||||
|
|
||||||
%type<nombre> E Invocation DebutAff
|
%type<nombre> E Invocation DebutAff SuiteAffPointeur DebutAffPointeur EBeforeAddr
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,11 +64,11 @@ Body : tOBRACKET {profondeur++;} Instructions tCBRACKET {print(); reset_pronf();
|
||||||
|
|
||||||
Instructions : Instruction Instructions ;
|
Instructions : Instruction Instructions ;
|
||||||
Instructions : ;
|
Instructions : ;
|
||||||
Instruction : Aff {reset_temp_vars();};
|
Instruction : Aff {};
|
||||||
Instruction : Decl {reset_temp_vars();};
|
Instruction : Decl {};
|
||||||
Instruction : Invocation tPV{reset_temp_vars();};
|
Instruction : Invocation tPV{};
|
||||||
Instruction : If {reset_temp_vars();};
|
Instruction : If {};
|
||||||
Instruction : While {reset_temp_vars();};
|
Instruction : While {};
|
||||||
|
|
||||||
//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 {
|
If : tIF tOBRACE E tCBRACE {
|
||||||
|
@ -76,8 +77,9 @@ 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);
|
||||||
instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
|
instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
|
||||||
nbs_instructions_to_patch[profondeur]++;}
|
nbs_instructions_to_patch[profondeur]++;
|
||||||
Else { printf("If reconnu\n");};
|
decrement_temp_var();}
|
||||||
|
Else {printf("If reconnu\n");};
|
||||||
|
|
||||||
|
|
||||||
Else : tELSE If { printf("Else 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;};
|
nbs_instructions_to_patch[profondeur] = 0;};
|
||||||
While : tWHILE tOBRACE E tCBRACE {
|
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;
|
||||||
|
decrement_temp_var();}
|
||||||
|
|
||||||
Body { printf("While reconnu\n");
|
Body { printf("While reconnu\n");
|
||||||
int current = get_current_index();
|
int current = get_current_index();
|
||||||
|
@ -102,16 +105,19 @@ patch($1,current + 1);
|
||||||
add_operation(JMP,$1,0,0);};
|
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 : 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 : 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.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 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 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();};
|
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 : tNOT E { printf("!\n"); };
|
||||||
E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
|
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 : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; decrement_temp_var();} ;
|
||||||
E : tMUL E { add_operation(GET, $2, $2, 0); $$=$2;};
|
E : tMUL E { add_operation(READ, $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 : 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
|
//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");};
|
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 : ;
|
||||||
|
@ -140,11 +150,12 @@ Decl : Type SuiteDecl FinDecl ;
|
||||||
Decl : tCONST Type 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 {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 : tPV { printf("Fin Decl\n");};
|
||||||
FinDecl : tCOMA SuiteDecl FinDecl ;
|
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 : tPV;
|
||||||
FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
|
FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
|
||||||
|
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -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
|
||||||
|
|
|
@ -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 (JMP):
|
case (JMP):
|
||||||
sprintf(buffer,"JMP %d\n",op.arg1);
|
sprintf(buffer,"JMP %d\n",op.arg1);
|
||||||
break;
|
break;
|
||||||
|
@ -54,11 +57,11 @@ 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):
|
case (READ):
|
||||||
sprintf(buffer,"GET %d %d\n",op.arg1, op.arg2);
|
sprintf(buffer,"READ %d %d\n",op.arg1, op.arg2);
|
||||||
break;
|
break;
|
||||||
case (MOV):
|
case (WR):
|
||||||
sprintf(buffer,"MOV %d %d\n",op.arg1, op.arg2);
|
sprintf(buffer,"WR %d %d\n",op.arg1, op.arg2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|
|
@ -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,GET,MOV};
|
enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,AFCA,JMP,JMF,INF,SUP,EQU,PRI,READ,WR};
|
||||||
|
|
||||||
struct operation_t {
|
struct operation_t {
|
||||||
enum opcode_t opcode;
|
enum opcode_t opcode;
|
||||||
|
|
|
@ -79,6 +79,8 @@ void init (void) {
|
||||||
pile->taille = 0;
|
pile->taille = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int push(char * nom, int isInit, struct 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};
|
||||||
|
@ -105,6 +107,11 @@ struct symbole_t pop() {
|
||||||
}
|
}
|
||||||
return retour;
|
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 status(char * nom) {
|
||||||
char retour = 0;
|
char retour = 0;
|
||||||
|
@ -161,16 +168,11 @@ int get_last_addr(){
|
||||||
|
|
||||||
|
|
||||||
int allocate_mem_temp_var(enum base_type_t type){
|
int allocate_mem_temp_var(enum base_type_t type){
|
||||||
last_temp_var_size = taille_types[type];
|
struct type_t type_bis = {type,0};
|
||||||
temp_addr -= last_temp_var_size;
|
int addr = push("",1,type_bis);
|
||||||
return temp_addr;
|
return addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset_temp_vars(){
|
|
||||||
temp_addr = MAXADDR;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void reset_pronf(){
|
void reset_pronf(){
|
||||||
printf("Profondeur dans reset : %d\n", profondeur);
|
printf("Profondeur dans reset : %d\n", profondeur);
|
||||||
while (pile->first != NULL && pile->first->symbole.profondeur == 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){
|
void decrement_temp_var(){
|
||||||
temp_addr += last_temp_var_size;
|
pop();
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,9 @@ extern int taille_types[];
|
||||||
extern int profondeur;
|
extern int profondeur;
|
||||||
|
|
||||||
struct type_t {
|
struct type_t {
|
||||||
enum base_type_t base;
|
enum base_type_t base;
|
||||||
int pointeur_level;
|
int pointeur_level;
|
||||||
|
int nb_blocs;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct symbole_t {
|
struct symbole_t {
|
||||||
|
@ -45,14 +46,13 @@ struct symbole_t {
|
||||||
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, struct type_t type);
|
int push(char * nom, int isInit, struct type_t type);
|
||||||
struct symbole_t pop();
|
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
|
// 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();
|
||||||
|
|
Loading…
Reference in a new issue