|
@@ -3,15 +3,16 @@
|
3
|
3
|
char id[30];
|
4
|
4
|
}
|
5
|
5
|
%{
|
|
6
|
+#include "../Tables/Fonctions/tab_fonctions.h"
|
6
|
7
|
#include "../Tables/Symboles/table_symboles.h"
|
7
|
8
|
#include <stdio.h>
|
8
|
9
|
#include <string.h>
|
9
|
10
|
#include <stdlib.h>
|
10
|
11
|
#include "../Tables/Instructions/tab_instruc.h"
|
11
|
|
-#include "../Tables/Fonctions/tab_fonctions.h"
|
12
|
12
|
#define TAILLE 1024
|
13
|
13
|
|
14
|
14
|
struct type_t type_courant;
|
|
15
|
+struct type_t return_type_fonc;
|
15
|
16
|
|
16
|
17
|
int instructions_ligne_to_patch[10][20];
|
17
|
18
|
int nbs_instructions_to_patch[10];
|
|
@@ -42,7 +43,7 @@ int nbs_instructions_to_patch[10];
|
42
|
43
|
%left tADD tSUB
|
43
|
44
|
%left tMUL tDIV
|
44
|
45
|
|
45
|
|
-%type<nombre> E DebutAff SuiteAffPointeur DebutAffPointeur EBis ETer
|
|
46
|
+%type<nombre> E DebutAff SuiteAffPointeur DebutAffPointeur EBis Invocation Args ArgSuite Arg SuiteParams Params
|
46
|
47
|
|
47
|
48
|
|
48
|
49
|
|
|
@@ -52,20 +53,24 @@ int nbs_instructions_to_patch[10];
|
52
|
53
|
|
53
|
54
|
%%
|
54
|
55
|
|
55
|
|
-Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
|
56
|
56
|
|
57
|
|
-Params : { printf("Sans Params\n"); } ;
|
58
|
|
-Params : Param SuiteParams ;
|
59
|
|
-Param : Type tID { printf("Parametre : %s\n", $2); };
|
60
|
|
-SuiteParams : tCOMA Param SuiteParams ;
|
61
|
|
-SuiteParams : ;
|
62
|
57
|
|
|
58
|
+C : Fonction Fonctions;
|
|
59
|
+
|
|
60
|
+Fonctions : Fonction Fonctions;
|
|
61
|
+Fonctions : ;
|
|
62
|
+
|
|
63
|
+Main : tINT {printf("Déclaration du main\n");} tMAIN tOBRACE Args tCBRACE Body { print(); create_asm();} ;
|
|
64
|
+
|
|
65
|
+Fonction : Type tID {return_type_fonc = type_courant; printf("Déclaration de la fonction %s\n", $2);} tOBRACE {inc_prof();} Args {decrement_prof(); push_fonction($2,return_type_fonc,get_current_index(), $6);} tCBRACE Body { print_fonctions();} ;
|
|
66
|
+Fonction : Main {print_fonctions();};
|
63
|
67
|
|
64
|
|
-Args : Arg ArgSuite;
|
65
|
|
-Args : ;
|
66
|
|
-Arg : Type tID {int addr = push($2,1, type_courant);};
|
67
|
|
-ArgSuite : tCOMA Arg ArgSuite {} ;
|
68
|
|
-ArgSuite : ;
|
|
68
|
+Args : Arg ArgSuite {$$ = $1 + $2; printf("Les arguments de la fonctions vont avoir une taille dans la pile de : %d\n",$$);};
|
|
69
|
+Args : {$$ = 0;};
|
|
70
|
+Arg : Type tID {int addr = push($2,1, type_courant); if (type_courant.pointeur_level > 0){$$ = taille_types[INT];} else{$$ = taille_types[type_courant.base];}};
|
|
71
|
+Arg : Type tID tOCROCH tCCROCH {int addr = push($2,1, type_courant); $$ = taille_types[INT];};
|
|
72
|
+ArgSuite : tCOMA Arg ArgSuite {$$ = $2 + $3;} ;
|
|
73
|
+ArgSuite : {$$ = 0;};
|
69
|
74
|
|
70
|
75
|
|
71
|
76
|
Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();} ;
|
|
@@ -75,10 +80,24 @@ Instructions : Instruction Instructions ;
|
75
|
80
|
Instructions : ;
|
76
|
81
|
Instruction : Aff {};
|
77
|
82
|
Instruction : Decl {};
|
78
|
|
-//Instruction : Invocation tPV{};
|
|
83
|
+Instruction : Invocation tPV{};
|
79
|
84
|
Instruction : If {};
|
80
|
85
|
Instruction : While {};
|
81
|
86
|
|
|
87
|
+
|
|
88
|
+Invocation : tID tOBRACE Params tCBRACE {multiple_pop($3); struct fonction_t fonc = get_fonction($1); add_operation(CALL,fonc.first_instruction_line, fonc.taille_args,0);};
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+//Pour les tableaux quand on les passe en arguments, il faudra faire gaffe à bien les passer comme un pointeur vers un tableau et pas juste un tableau car sinon in ne pourra pas accéder au tableau de base depuis la fonction appellée. Il faut pour cela peut être rajouter un nouveau champ (isArg) dans la table des symboles (ou autre idée?)
|
|
92
|
+
|
|
93
|
+Params : {$$ = 0; printf("Sans Params\n"); } ;
|
|
94
|
+Params : Param SuiteParams {$$ = $2 + 1;};
|
|
95
|
+Param : E {printf("Parametre : %d\n", $1);};
|
|
96
|
+SuiteParams : tCOMA Param SuiteParams {$$ = $3 + 1;};
|
|
97
|
+SuiteParams : {$$ = 0;};
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
82
|
101
|
//On considère que la première ligne du code en ASM est la ligne 0
|
83
|
102
|
If : tIF tOBRACE E tCBRACE {
|
84
|
103
|
add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
|
|
@@ -131,7 +150,7 @@ E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
|
131
|
150
|
E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
|
132
|
151
|
E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
|
133
|
152
|
E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
|
134
|
|
-//E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
|
|
153
|
+E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
|
135
|
154
|
E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
|
136
|
155
|
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();};
|
137
|
156
|
E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; pop();};
|
|
@@ -141,21 +160,32 @@ E : tNOT E { printf("!\n"); };
|
141
|
160
|
E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
|
142
|
161
|
E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; pop();} ;
|
143
|
162
|
E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;};
|
144
|
|
-E : tADDR EBis {add_operation(COPA,$2, $2,0); $$=$2;};
|
145
|
|
-E : tADDR ETer {add_operation(COPA,$2, $2,0); $$=$2;};
|
146
|
163
|
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;};
|
147
|
|
-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();};
|
|
164
|
+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);
|
|
165
|
+ add_operation(ADD,$3,addr,$3); add_operation(READ,$3,$3,0); $$=$3; pop(); pop();};
|
|
166
|
+E : tADDR EBis {add_operation(COPA,$2, $2,0); $$=$2;};
|
148
|
167
|
|
149
|
|
-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();};
|
150
|
|
-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;};
|
|
168
|
+EBis : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1);
|
|
169
|
+struct type_t type = symbole->type; type.nb_blocs = 1;
|
|
170
|
+int addr = push("0_TEMPORARY", 1, type);
|
|
171
|
+if(type.pointeur_level > 0) {
|
|
172
|
+add_operation(COP, addr,symbole->adresse,0);
|
|
173
|
+}
|
|
174
|
+ else{
|
|
175
|
+add_operation(AFCA, addr,symbole->adresse,0);
|
|
176
|
+}
|
|
177
|
+int addr2 = push("0_TEMPORARY", 1, integer);
|
|
178
|
+add_operation(AFC, addr2, taille_types[symbole->type.base],0);
|
|
179
|
+add_operation(MUL,$3,addr2,$3);
|
|
180
|
+add_operation(ADD,$3,addr,$3); $$=$3;
|
|
181
|
+pop(); pop();};
|
|
182
|
+EBis : 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;};
|
151
|
183
|
|
152
|
184
|
|
153
|
185
|
|
154
|
186
|
//Créer un champ isConst dans la table des symboles
|
155
|
187
|
Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; type_courant.isTab = 0; type_courant.nb_blocs = 1; printf("Type int\n");} ;
|
156
|
188
|
Type : Type tMUL {type_courant.pointeur_level++; printf("Type int *\n");};
|
157
|
|
-//SuiteType : tMUL SuiteType {type_courant.pointeur_level++; printf(" * en plus\n");} ;
|
158
|
|
-//SuiteType : ;
|
159
|
189
|
|
160
|
190
|
Decl : Type SuiteDecl FinDecl ;
|
161
|
191
|
Decl : tCONST Type SuiteDeclConst FinDeclConst;
|
|
@@ -169,10 +199,6 @@ FinDecl : tCOMA SuiteDecl FinDecl ;
|
169
|
199
|
SuiteDeclConst : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
|
170
|
200
|
FinDeclConst : tPV;
|
171
|
201
|
FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
|
172
|
|
-
|
173
|
|
-Fonction : Type tID {push_fonction($2,type_courant,get_current_index());} tOBRACE {} Args {} tCBRACE Body { printf("Déclaration de la fonction %s\n", $1); } ;)
|
174
|
|
-
|
175
|
|
-
|
176
|
202
|
%%
|
177
|
203
|
void main(void) {
|
178
|
204
|
init();
|