Clean code un peu
This commit is contained in:
parent
056118fe7b
commit
23851b8389
5 changed files with 120 additions and 84 deletions
|
@ -59,7 +59,7 @@ Param : Type tID { printf("Prametre : %s\n", $2); };
|
|||
SuiteParams : tCOMA Param SuiteParams ;
|
||||
SuiteParams : ;
|
||||
|
||||
Body : tOBRACKET {profondeur++;} Instructions tCBRACKET {print(); reset_pronf(); profondeur--;} ;
|
||||
Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();} ;
|
||||
|
||||
|
||||
Instructions : Instruction Instructions ;
|
||||
|
@ -76,28 +76,28 @@ 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]++;
|
||||
decrement_temp_var();}
|
||||
instructions_ligne_to_patch[get_prof()][nbs_instructions_to_patch[get_prof()]] = current;
|
||||
nbs_instructions_to_patch[get_prof()]++;
|
||||
pop();}
|
||||
Else {printf("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);
|
||||
for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
|
||||
patch(instructions_ligne_to_patch[get_prof()][i],current);
|
||||
}
|
||||
nbs_instructions_to_patch[profondeur] = 0;
|
||||
nbs_instructions_to_patch[get_prof()] = 0;
|
||||
};
|
||||
Else : {int current = get_current_index();
|
||||
for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
|
||||
patch(instructions_ligne_to_patch[profondeur][i],current);
|
||||
for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
|
||||
patch(instructions_ligne_to_patch[get_prof()][i],current);
|
||||
}
|
||||
nbs_instructions_to_patch[profondeur] = 0;};
|
||||
nbs_instructions_to_patch[get_prof()] = 0;};
|
||||
While : tWHILE tOBRACE E tCBRACE {
|
||||
add_operation(JMF,$3,0,0);
|
||||
$1 = get_current_index() - 1;
|
||||
decrement_temp_var();}
|
||||
pop();}
|
||||
|
||||
Body { printf("While reconnu\n");
|
||||
int current = get_current_index();
|
||||
|
@ -105,39 +105,39 @@ patch($1,current + 1);
|
|||
add_operation(JMP,$1,0,0);};
|
||||
|
||||
|
||||
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();};
|
||||
Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0); pop();} ;
|
||||
Aff : DebutAffPointeur tEQ E tPV {add_operation(WR,$1,$3,0); pop(); pop();};
|
||||
|
||||
DebutAff : tID {struct symbole_t * symbole = get_variable($1); symbole->initialized = 1; $$=symbole->adresse; printf("%s prend une valeur\n", $1);};
|
||||
|
||||
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();};
|
||||
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();};
|
||||
|
||||
|
||||
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 : 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 : tNB { int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
|
||||
E : tNBEXP { printf("Nombre exp\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
|
||||
E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
|
||||
E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
|
||||
E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
|
||||
E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
|
||||
E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); 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(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 : tSUB E { printf("Moins\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,0,0); add_operation(SOU, $2,$2,addr); $$ = $2; pop();};
|
||||
E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; pop();};
|
||||
E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; pop();};
|
||||
E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; pop();};
|
||||
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 : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
|
||||
E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; pop();} ;
|
||||
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;};
|
||||
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
|
||||
|
@ -150,12 +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 {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;} ;
|
||||
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);} ;
|
||||
FinDecl : tPV { printf("Fin Decl\n");};
|
||||
FinDecl : tCOMA SuiteDecl FinDecl ;
|
||||
|
||||
SuiteDeclConst : tID tEQ E {decrement_temp_var(); int addr = push($1,1, type_courant);};
|
||||
SuiteDeclConst : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
|
||||
FinDeclConst : tPV;
|
||||
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.*
|
||||
|
||||
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
|
||||
|
|
|
@ -13,9 +13,11 @@ struct operation_t {
|
|||
int arg3;
|
||||
};
|
||||
|
||||
extern struct operation_t tab_op[MAXTAILLE];
|
||||
|
||||
//Ajoute une opération dans la table (à la fin)
|
||||
void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3);
|
||||
void create_asm();
|
||||
//Renvoi le prochain slot disponible
|
||||
int get_current_index();
|
||||
//Permet de patcher les Jump (pas de Van Halen)
|
||||
void patch(int index, int arg);
|
||||
//Ecrit la table des intructions dans un fichier ASM
|
||||
void create_asm();
|
||||
|
|
|
@ -37,6 +37,7 @@ int temp_addr = MAXADDR;
|
|||
int taille_types[] = {-1, 4};
|
||||
int profondeur = 0;
|
||||
int last_temp_var_size;
|
||||
const struct type_t integer = {INT, 0, 1};
|
||||
|
||||
struct element_t {
|
||||
struct symbole_t symbole;
|
||||
|
@ -90,7 +91,7 @@ int push(char * nom, int isInit, struct type_t type) {
|
|||
pile->first = aux;
|
||||
pile->taille++;
|
||||
int addr_var = last_addr;
|
||||
last_addr += taille_types[type.base];
|
||||
last_addr += (type.nb_blocs)*taille_types[type.base];
|
||||
return addr_var;
|
||||
}
|
||||
|
||||
|
@ -108,30 +109,6 @@ 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;
|
||||
struct element_t * aux = pile->first;
|
||||
int i;
|
||||
for (i=0; i < pile->taille; i++) {
|
||||
if (!strcmp(nom, aux->symbole.nom)) {
|
||||
if (aux->symbole.initialized) {
|
||||
retour = 1;
|
||||
} else {
|
||||
retour = 2;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
aux = aux->suivant;
|
||||
}
|
||||
}
|
||||
return retour;
|
||||
}
|
||||
|
||||
struct symbole_t * get_variable(char * nom){
|
||||
struct symbole_t * retour = NULL;
|
||||
struct element_t * aux = pile->first;
|
||||
|
@ -166,20 +143,18 @@ int get_last_addr(){
|
|||
return last_addr;
|
||||
}
|
||||
|
||||
|
||||
int allocate_mem_temp_var(enum base_type_t type){
|
||||
struct type_t type_bis = {type,0};
|
||||
int addr = push("",1,type_bis);
|
||||
return addr;
|
||||
void inc_prof() {
|
||||
profondeur++;
|
||||
}
|
||||
|
||||
void reset_pronf(){
|
||||
int get_prof() {
|
||||
return profondeur;
|
||||
}
|
||||
|
||||
void reset_prof(){
|
||||
printf("Profondeur dans reset : %d\n", profondeur);
|
||||
while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){
|
||||
pop();
|
||||
}
|
||||
}
|
||||
|
||||
void decrement_temp_var(){
|
||||
pop();
|
||||
profondeur--;
|
||||
}
|
||||
|
|
|
@ -28,37 +28,96 @@ Opérations possible :
|
|||
#include <stdint.h>
|
||||
|
||||
|
||||
enum base_type_t {UNKNOWN, INT};
|
||||
extern int taille_types[];
|
||||
extern int profondeur;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
/**** Gestion des types possibles ****/
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
|
||||
//Enumération des types de base reconnus
|
||||
enum base_type_t {UNKNOWN, INT};
|
||||
//Table enregistrant la taille de ces types de bases (indexée sur les types)
|
||||
extern int taille_types[];
|
||||
//Structure gérant les types
|
||||
struct type_t {
|
||||
//Champs enregsitrant le type de base
|
||||
enum base_type_t base;
|
||||
//Si la variable est un pointeur, on enregitre son niveau (nombre de *)
|
||||
int pointeur_level;
|
||||
//Si il s'agit d'un tableau, on enregistre sa taille (nombre de cases)
|
||||
int nb_blocs;
|
||||
};
|
||||
//Constante pour les entiers
|
||||
extern const struct type_t integer;
|
||||
|
||||
|
||||
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
/***** Gestion de la profondeur *****/
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
//Incrémente la profondeur
|
||||
void inc_prof();
|
||||
//Récupère la profondeur
|
||||
int get_prof();
|
||||
//Détruit toutes les variables de la profondeur actuelle puis décrémente la profondeur
|
||||
void reset_prof();
|
||||
|
||||
|
||||
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
/*Table des symboles : fonctionnement */
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
|
||||
//Structure représentant un symbole
|
||||
struct symbole_t {
|
||||
//Son Nom
|
||||
char nom[30];
|
||||
//Son Adresse
|
||||
uintptr_t adresse;
|
||||
//Son type
|
||||
struct type_t type;
|
||||
//Est il initialisé
|
||||
int initialized;
|
||||
//Sa profondeur de création
|
||||
int profondeur;
|
||||
};
|
||||
|
||||
//Fonction d'affichage d'un symbole
|
||||
void print_symbole(struct symbole_t symbole);
|
||||
|
||||
|
||||
//Fonction d'initialisation de la table, à appeler une fois
|
||||
void init(void);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
/** Table des symboles : primitives **/
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
|
||||
//Ajout d'un symbole a la table
|
||||
int push(char * nom, int isInit, struct type_t type);
|
||||
//Destruction et récupération du premier élément de la table
|
||||
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();
|
||||
//Retourne la dernière adresse disponible
|
||||
int get_last_addr();
|
||||
//Renvoi un pointeur vers le symbole correspondant au nom de variable
|
||||
struct symbole_t * get_variable(char * nom);
|
||||
int allocate_mem_temp_var(enum base_type_t type);
|
||||
void reset_temp_vars();
|
||||
void reset_pronf();
|
||||
void decrement_temp_var();
|
||||
//Affiche la table des symboles
|
||||
void print();
|
||||
|
||||
|
|
Loading…
Reference in a new issue