Browse Source

Clean code un peu

Faure Paul 3 years ago
parent
commit
23851b8389
5 changed files with 118 additions and 82 deletions
  1. 33
    33
      Lex_Yacc/as.y
  2. 1
    1
      Makefile
  3. 5
    3
      Tables/Instructions/tab_instruc.h
  4. 9
    34
      Tables/Symboles/table_symboles.c
  5. 70
    11
      Tables/Symboles/table_symboles.h

+ 33
- 33
Lex_Yacc/as.y View File

@@ -59,7 +59,7 @@ Param : Type tID { printf("Prametre : %s\n", $2); };
59 59
 SuiteParams : tCOMA Param SuiteParams ;
60 60
 SuiteParams : ;
61 61
 
62
-Body : tOBRACKET {profondeur++;} Instructions tCBRACKET {print(); reset_pronf(); profondeur--;} ;
62
+Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();} ;
63 63
 
64 64
 
65 65
 Instructions : Instruction Instructions ;
@@ -76,28 +76,28 @@ add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
76 76
 Body  {int current = get_current_index();
77 77
 		patch($1,current + 1);
78 78
 		add_operation(JMP,0,0,0);
79
-		instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
80
-		nbs_instructions_to_patch[profondeur]++;
81
-		decrement_temp_var();}
79
+		instructions_ligne_to_patch[get_prof()][nbs_instructions_to_patch[get_prof()]] = current;
80
+		nbs_instructions_to_patch[get_prof()]++;
81
+		pop();}
82 82
 Else {printf("If reconnu\n");};
83 83
 
84 84
 
85 85
 Else : tELSE If { printf("Else if reconnu\n"); };
86 86
 Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index(); 
87
-for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
88
-	patch(instructions_ligne_to_patch[profondeur][i],current);
87
+for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
88
+	patch(instructions_ligne_to_patch[get_prof()][i],current);
89 89
 }
90
-nbs_instructions_to_patch[profondeur] = 0;
90
+nbs_instructions_to_patch[get_prof()] = 0;
91 91
 };
92 92
 Else : {int current = get_current_index(); 
93
-for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
94
-	patch(instructions_ligne_to_patch[profondeur][i],current);
93
+for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
94
+	patch(instructions_ligne_to_patch[get_prof()][i],current);
95 95
 }
96
-nbs_instructions_to_patch[profondeur] = 0;};
96
+nbs_instructions_to_patch[get_prof()] = 0;};
97 97
 While : tWHILE tOBRACE E tCBRACE {
98 98
 add_operation(JMF,$3,0,0); 
99 99
 $1 = get_current_index() - 1;
100
-decrement_temp_var();}
100
+pop();}
101 101
 
102 102
 Body { printf("While reconnu\n");
103 103
 int current = get_current_index();
@@ -105,39 +105,39 @@ patch($1,current + 1);
105 105
 add_operation(JMP,$1,0,0);};
106 106
 
107 107
 
108
-Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0); decrement_temp_var();} ; 
109
-Aff : DebutAffPointeur tEQ E tPV {add_operation(WR,$1,$3,0); decrement_temp_var(); decrement_temp_var();};
108
+Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0); pop();} ; 
109
+Aff : DebutAffPointeur tEQ E tPV {add_operation(WR,$1,$3,0); pop(); pop();};
110 110
 
111 111
 DebutAff : tID {struct symbole_t * symbole  = get_variable($1); symbole->initialized = 1; $$=symbole->adresse; printf("%s prend une valeur\n", $1);};
112 112
 
113 113
 DebutAffPointeur : tMUL SuiteAffPointeur {add_operation(READ, $2, $2, 0); $$=$2;};
114 114
 DebutAffPointeur : SuiteAffPointeur {$$=$1;};
115
-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;};
116
-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();};
115
+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;};
116
+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();};
117 117
 
118 118
 
119
-E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
120
-E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
121
-E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
122
-E : E tDIV E { printf("Div\n");  add_operation(DIV, $1,$1,$3); $$ = $1; decrement_temp_var();};
123
-E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; decrement_temp_var();};
124
-E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; decrement_temp_var();};
125
-E : Invocation { printf("Invoc\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
119
+E : tNB { int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
120
+E : tNBEXP { printf("Nombre exp\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
121
+E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
122
+E : E tDIV E { printf("Div\n");  add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
123
+E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
124
+E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
125
+E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
126 126
 E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
127
-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();};
128
-E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; decrement_temp_var();};
129
-E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; decrement_temp_var();};
130
-E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; decrement_temp_var();};
127
+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();};
128
+E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; pop();};
129
+E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; pop();};
130
+E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; pop();};
131 131
 E : tNOT E { printf("!\n"); };
132
-E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
133
-E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; decrement_temp_var();} ;
132
+E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
133
+E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; pop();} ;
134 134
 E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;};
135 135
 E : tADDR EBeforeAddr {add_operation(AFCA,$2, $2,0); $$=$2;};
136 136
 E : EBeforeAddr {$$=$1;};
137 137
 
138 138
 
139
-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();};
140
-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;};
139
+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();};
140
+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;};
141 141
 
142 142
 
143 143
 //Créer un champ isConst dans la table des symboles
@@ -150,12 +150,12 @@ Decl : Type SuiteDecl FinDecl ;
150 150
 Decl : tCONST Type SuiteDeclConst FinDeclConst;
151 151
 
152 152
 SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
153
-SuiteDecl : tID tEQ E {decrement_temp_var(); int addr = push($1,1, type_courant);};
154
-SuiteDecl : tID tOCROCH tNB tCCROCH {type_courant.pointeur_level++; decl_tab($1,type_courant,$3); type_courant.nb_blocs = 1;} ;
153
+SuiteDecl : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
154
+SuiteDecl : tID tOCROCH tNB tCCROCH {type_courant.pointeur_level++; type_courant.nb_blocs = $3; push($1, 0, type_courant);} ;
155 155
 FinDecl : tPV { printf("Fin Decl\n");};
156 156
 FinDecl : tCOMA SuiteDecl FinDecl ;
157 157
 
158
-SuiteDeclConst : tID tEQ E {decrement_temp_var(); int addr = push($1,1, type_courant);};
158
+SuiteDeclConst : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
159 159
 FinDeclConst : tPV;
160 160
 FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
161 161
 

+ 1
- 1
Makefile View File

@@ -17,7 +17,7 @@ clean_Lex_Yacc:
17 17
 	@rm -f Lex_Yacc/as.output Lex_Yacc/as.tab.* Lex_Yacc/lex.yy.*
18 18
 
19 19
 build : clean build_Symboles build_Instructions build_Lex_Yacc
20
-	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
20
+	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
21 21
 
22 22
 build_Symboles: clean_Symboles
23 23
 	gcc -c Tables/Symboles/table_symboles.c -o Tables/Symboles/table_symboles.o

+ 5
- 3
Tables/Instructions/tab_instruc.h View File

@@ -13,9 +13,11 @@ struct operation_t {
13 13
 	int arg3;
14 14
 };
15 15
 
16
-extern struct operation_t tab_op[MAXTAILLE];
17
-
16
+//Ajoute une opération dans la table (à la fin)
18 17
 void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3);
19
-void create_asm();
18
+//Renvoi le prochain slot disponible
20 19
 int get_current_index();
20
+//Permet de patcher les Jump (pas de Van Halen)
21 21
 void patch(int index, int arg);
22
+//Ecrit la table des intructions dans un fichier ASM
23
+void create_asm();

+ 9
- 34
Tables/Symboles/table_symboles.c View File

@@ -37,6 +37,7 @@ int temp_addr = MAXADDR;
37 37
 int taille_types[] = {-1, 4};
38 38
 int profondeur = 0;
39 39
 int last_temp_var_size;
40
+const struct type_t integer = {INT, 0, 1};
40 41
 
41 42
 struct element_t {
42 43
 	struct symbole_t symbole;
@@ -90,7 +91,7 @@ int push(char * nom, int isInit, struct type_t type) {
90 91
 	pile->first = aux;
91 92
 	pile->taille++;
92 93
 	int addr_var = last_addr;
93
-	last_addr += taille_types[type.base]; 
94
+	last_addr += (type.nb_blocs)*taille_types[type.base]; 
94 95
 	return addr_var;
95 96
 }
96 97
 
@@ -108,30 +109,6 @@ struct symbole_t pop() {
108 109
 	return retour;
109 110
 }
110 111
 
111
-struct symbole_t decl_tab(char * name, struct type_t type, int nb_blocs){
112
-	push(name,0,type);
113
-	last_addr += (nb_blocs-1)*taille_types[type.base];
114
-}
115
-		
116
-char status(char * nom) {
117
-	char retour = 0;
118
-	struct element_t * aux = pile->first;
119
-	int i;
120
-	for (i=0; i < pile->taille; i++) {
121
-		if (!strcmp(nom, aux->symbole.nom)) {
122
-			if (aux->symbole.initialized) {
123
-				retour = 1;
124
-			} else {
125
-				retour = 2;
126
-			}
127
-			break;
128
-		} else {
129
-			aux = aux->suivant;
130
-		}
131
-	}
132
-	return retour;
133
-}
134
-
135 112
 struct symbole_t * get_variable(char * nom){
136 113
 	struct symbole_t * retour = NULL;
137 114
 	struct element_t * aux = pile->first;
@@ -166,20 +143,18 @@ int get_last_addr(){
166 143
 	return last_addr;
167 144
 }
168 145
 
146
+void inc_prof() {
147
+    profondeur++;
148
+}
169 149
 
170
-int allocate_mem_temp_var(enum base_type_t type){
171
-	struct type_t type_bis = {type,0};
172
-	int addr = push("",1,type_bis);
173
-	return addr;
150
+int get_prof() {
151
+    return profondeur;
174 152
 }
175 153
 
176
-void reset_pronf(){
154
+void reset_prof(){
177 155
     printf("Profondeur dans reset : %d\n", profondeur);
178 156
     while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){
179 157
 	    pop();
180 158
     }
181
-}
182
-
183
-void decrement_temp_var(){
184
-   pop();
159
+    profondeur--;
185 160
 }

+ 70
- 11
Tables/Symboles/table_symboles.h View File

@@ -28,37 +28,96 @@ Opérations possible :
28 28
 #include <stdint.h>
29 29
 
30 30
 
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+/**************************************/
39
+/**************************************/
40
+/**** Gestion des types possibles  ****/
41
+/**************************************/
42
+/**************************************/
43
+
44
+//Enumération des types de base reconnus 
31 45
 enum base_type_t {UNKNOWN, INT};
46
+//Table enregistrant la taille de ces types de bases (indexée sur les types)
32 47
 extern int taille_types[];
33
-extern int profondeur;
34
-
48
+//Structure gérant les types 
35 49
 struct type_t {
50
+    //Champs enregsitrant le type de base
36 51
     enum base_type_t base;
52
+    //Si la variable est un pointeur, on enregitre son niveau (nombre de *)
37 53
     int pointeur_level;
54
+    //Si il s'agit d'un tableau, on enregistre sa taille (nombre de cases)
38 55
 	int nb_blocs;
39 56
 };
57
+//Constante pour les entiers
58
+extern const struct type_t integer;
59
+
60
+
61
+
62
+/**************************************/
63
+/**************************************/
64
+/*****  Gestion de la profondeur  *****/
65
+/**************************************/
66
+/**************************************/
67
+//Incrémente la profondeur
68
+void inc_prof();
69
+//Récupère la profondeur
70
+int get_prof();
71
+//Détruit toutes les variables de la profondeur actuelle puis décrémente la profondeur
72
+void reset_prof();
73
+
40 74
 
75
+
76
+/**************************************/
77
+/**************************************/
78
+/*Table des symboles : fonctionnement */
79
+/**************************************/
80
+/**************************************/
81
+
82
+//Structure représentant un symbole
41 83
 struct symbole_t {
84
+    //Son Nom
42 85
 	char nom[30];
86
+    //Son Adresse
43 87
 	uintptr_t adresse;
88
+    //Son type
44 89
 	struct type_t type;
90
+    //Est il initialisé
45 91
 	int initialized;
92
+    //Sa profondeur de création
46 93
     int profondeur;
47 94
 };
48 95
 
96
+//Fonction d'affichage d'un symbole
49 97
 void print_symbole(struct symbole_t symbole);
50 98
 
51
-
99
+//Fonction d'initialisation de la table, à appeler une fois
52 100
 void init(void);
101
+
102
+
103
+
104
+
105
+
106
+
107
+/**************************************/
108
+/**************************************/
109
+/** Table des symboles : primitives  **/
110
+/**************************************/
111
+/**************************************/
112
+
113
+//Ajout d'un symbole a la table
53 114
 int push(char * nom, int isInit, struct type_t type);
115
+//Destruction et récupération du premier élément de la table
54 116
 struct symbole_t pop();
55
-struct symbole_t decl_tab(char * name, struct type_t type, int nb_blocs);
56
-// renvoi 0 si nom n'existe pas, 2 si nom existe sans etre initialisée, 1 sinon
57
-char status(char * nom);
58
-void print();
117
+//Retourne la dernière adresse disponible
59 118
 int get_last_addr();
119
+//Renvoi un pointeur vers le symbole correspondant au nom de variable
60 120
 struct symbole_t * get_variable(char * nom);
61
-int allocate_mem_temp_var(enum base_type_t type);
62
-void reset_temp_vars();
63
-void reset_pronf();
64
-void decrement_temp_var();
121
+//Affiche la table des symboles
122
+void print();
123
+

Loading…
Cancel
Save