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 b = 4 * 5;
int a = 1;
while (b){
b = 2;
if (a){
int u = 8;
}
else{
if (1){
int j = 7;
}
else if (5){
int g = 89;
}
int y = 9;
}
}
int p = 85;
int * b = 4 * 5;
int ** c,d;
int ** y = &b;
* c = 1;
&b = 2;
}

View file

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

View file

@ -10,7 +10,7 @@
#include "../Tables/Instructions/tab_instruc.h"
#define TAILLE 1024
enum type_t type_courant;
struct type_t type_courant;
int instructions_ligne_to_patch[10][20];
int nbs_instructions_to_patch[10];
@ -31,6 +31,7 @@ int nbs_instructions_to_patch[10];
%token<nombre> tIF tWHILE tELSE
%token tLT tGT tEQCOND
%token tAND tOR
%token tADDR
%left tAND tOR
%left tNOT
@ -39,7 +40,7 @@ int nbs_instructions_to_patch[10];
%left tADD tSUB
%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 : Param SuiteParams ;
Param : DeclType tID { printf("Prametre : %s\n", $2); };
Param : Type tID { printf("Prametre : %s\n", $2); };
SuiteParams : tCOMA Param SuiteParams ;
SuiteParams : ;
Body : tOBRACKET Instructions tCBRACKET { } ;
Body : tOBRACKET {profondeur++;} Instructions tCBRACKET {print(); reset_pronf(); profondeur--;} ;
Instructions : Instruction Instructions ;
@ -69,18 +70,8 @@ Instruction : If {reset_temp_vars();};
Instruction : While {reset_temp_vars();};
//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;}
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();
patch($1,current + 1);
add_operation(JMP,0,0,0);
@ -89,9 +80,7 @@ Body {int current = get_current_index();
Else { printf("If reconnu\n");};
Else : tELSE ElseIf { printf("Else if reconnu\n"); };
Else : tELSE If { printf("Else if reconnu\n"); };
Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index();
for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
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);
}
nbs_instructions_to_patch[profondeur] = 0;};
While : tWHILE tOBRACE E tCBRACE {profondeur++;
While : tWHILE tOBRACE E tCBRACE {
add_operation(JMF,$3,0,0);
$1 = get_current_index() - 1;}
Body { printf("While reconnu\n");
int current = get_current_index();
patch($1,current + 1);
add_operation(JMP,$1,0,0);
reset_pronf(); profondeur--;};
add_operation(JMP,$1,0,0);};
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 : 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 : E tMUL E { printf("Mul\n"); int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr,$1,$3); $$ = addr;};
E : E tDIV E { printf("Div\n"); int addr = allocate_mem_temp_var(INT); add_operation(DIV, addr,$1,$3); $$ = addr;};
E : E tSUB E { printf("Sub\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, addr,$1,$3); $$ = addr;};
E : E tADD E { printf("Add\n"); int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr,$1,$3); $$ = 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();};
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 : 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 : E tEQCOND E { printf("==\n"); int addr = allocate_mem_temp_var(INT); add_operation(EQU, addr,$1,$3); $$ = addr;};
E : E tGT E { printf(">\n"); int addr = allocate_mem_temp_var(INT); add_operation(SUP, addr,$1,$3); $$ = addr;};
E : E tLT E { printf("<\n"); int addr = allocate_mem_temp_var(INT); add_operation(INF, addr,$1,$3); $$ = 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"); add_operation(EQU,$1,$1,$3); $$ = $1; decrement_temp_var();};
E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; decrement_temp_var();};
E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; decrement_temp_var();};
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 tOR E {int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr, $1, $3); $$=addr;} ;
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;};
//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 : tCONST DeclType SuiteDeclConst FinDeclConst;
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);};

View file

@ -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 -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
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):
sprintf(buffer,"PRI %d\n",op.arg1);
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;
}

View file

@ -4,7 +4,7 @@
#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 {
enum opcode_t opcode;

View file

@ -36,6 +36,7 @@ int last_addr = 0;
int temp_addr = MAXADDR;
int taille_types[] = {-1, 4};
int profondeur = 0;
int last_temp_var_size;
struct element_t {
struct symbole_t symbole;
@ -48,20 +49,28 @@ struct pile_t {
};
struct pile_t * pile;
char * type_to_string(enum type_t type) {
if (type == INT) {
return "int";
} else {
return "unknown";
char * type_to_string(struct type_t type) {
char * star = "*";
char * resultat = malloc(sizeof(char)*20);
for (int i = 0; i< type.pointeur_level; i++){
strcat(resultat,star);
}
if (type.base == INT) {
strcat(resultat,"int");
} else {;
strcat(resultat,"unknown");
}
return resultat;
}
void print_symbole(struct symbole_t symbole) {
char * type = type_to_string(symbole.type);
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 {
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) {
@ -70,7 +79,7 @@ void init (void) {
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 symbole_t symbole = {"", last_addr, type, isInit,profondeur};
strcpy(symbole.nom,nom);
@ -79,7 +88,7 @@ int push(char * nom, int isInit, enum type_t type) {
pile->first = aux;
pile->taille++;
int addr_var = last_addr;
last_addr += taille_types[type];
last_addr += taille_types[type.base];
return addr_var;
}
@ -92,7 +101,7 @@ struct symbole_t pop() {
retour = aux->symbole;
free(aux);
pile->taille--;
last_addr -= taille_types[retour.type];
last_addr -= taille_types[retour.type.base];
}
return retour;
}
@ -151,8 +160,9 @@ int get_last_addr(){
}
int allocate_mem_temp_var(enum type_t type){
temp_addr -= taille_types[type];
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;
}
@ -162,8 +172,12 @@ void reset_temp_vars(){
void reset_pronf(){
while (pile->first->symbole.profondeur == profondeur){
printf("Profondeur dans reset : %d\n", profondeur);
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>
enum type_t {UNKNOWN, INT};
enum base_type_t {UNKNOWN, INT};
extern int taille_types[];
extern int profondeur;
struct type_t {
enum base_type_t base;
int pointeur_level;
};
struct symbole_t {
char nom[30];
uintptr_t adresse;
enum type_t type;
struct type_t type;
int initialized;
int profondeur;
};
void print_symbole(struct symbole_t symbole);
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();
// renvoi 0 si nom n'existe pas, 2 si nom existe sans etre initialisée, 1 sinon
char status(char * nom);
void print();
int get_last_addr();
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_pronf();
void decrement_temp_var();