aboutsummaryrefslogtreecommitdiff
path: root/src/tpc.y
diff options
context:
space:
mode:
Diffstat (limited to 'src/tpc.y')
-rw-r--r--src/tpc.y183
1 files changed, 126 insertions, 57 deletions
diff --git a/src/tpc.y b/src/tpc.y
index 340ce5c..db59e43 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -14,8 +14,7 @@
14 * - Evaluation paresseuse 14 * - Evaluation paresseuse
15 * - Gestion des tableaux 15 * - Gestion des tableaux
16 * - Tableau des fonctions 16 * - Tableau des fonctions
17 * 17 *
18 * - remettre car conflit pour l'instant->
19 */ 18 */
20 19
21#include <stdio.h> 20#include <stdio.h>
@@ -107,12 +106,16 @@ DeclVars:
107 | 106 |
108 ; 107 ;
109Declarateurs: 108Declarateurs:
110 Declarateurs ',' Declarateur {if(status == GLOBAL) glo_addVar($<ident>3, $<type>0); 109 Declarateurs ',' Declarateur {
111 else loc_addVar($<ident>3, $<type>0); 110 if(status == GLOBAL) glo_addVar($<ident>3, $<type>0);
112 printf("push 0\n");} 111 else loc_addVar($<ident>3, $<type>0);
113 | Declarateur {if(status == GLOBAL) glo_addVar($<ident>1, $<type>0); 112 printf("push 0\n");
114 else loc_addVar($<ident>1, $<type>0); 113 }
115 printf("push 0\n");} 114 | Declarateur {
115 if(status == GLOBAL) glo_addVar($<ident>1, $<type>0);
116 else loc_addVar($<ident>1, $<type>0);
117 printf("push 0\n");
118 }
116 ; 119 ;
117Declarateur: 120Declarateur:
118 IDENT 121 IDENT
@@ -134,12 +137,16 @@ Parametres:
134 | ListTypVar 137 | ListTypVar
135 ; 138 ;
136ListTypVar: 139ListTypVar:
137 ListTypVar ',' TYPE IDENT {if(status == GLOBAL) glo_addVar($<ident>4, $<type>3); 140 ListTypVar ',' TYPE IDENT {
138 else loc_addVar($<ident>4, $<type>3); 141 if(status == GLOBAL) glo_addVar($<ident>4, $<type>3);
139 printf("push 0\n");} 142 else loc_addVar($<ident>4, $<type>3);
140 | TYPE IDENT {if(status == GLOBAL) glo_addVar($<ident>2, $<type>1); 143 printf("push 0\n");
141 else loc_addVar($<ident>2, $<type>1); 144 }
142 printf("push 0\n");} 145 | TYPE IDENT {
146 if(status == GLOBAL) glo_addVar($<ident>2, $<type>1);
147 else loc_addVar($<ident>2, $<type>1);
148 printf("push 0\n");
149 }
143 ; 150 ;
144Corps: 151Corps:
145 '{' DeclConsts DeclVars SuiteInstr '}' 152 '{' DeclConsts DeclVars SuiteInstr '}'
@@ -152,74 +159,116 @@ Instr:
152 | ';' 159 | ';'
153 | RETURN Exp ';' 160 | RETURN Exp ';'
154 | RETURN ';' 161 | RETURN ';'
155 | READE '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3); 162 | READE '(' IDENT ')' ';' {
156 else loc_lookup($<ident>3);} 163 if(status == GLOBAL) glo_lookup($<ident>3);
157 | READC '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3); 164 else loc_lookup($<ident>3);
158 else loc_lookup($<ident>3);} 165 }
166 | READC '(' IDENT ')' ';' {
167 if(status == GLOBAL) glo_lookup($<ident>3);
168 else loc_lookup($<ident>3);
169 }
159 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");} 170 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");}
160 | IF '(' Exp IfHandling')' Instr IfEndHandling 171 | IF '(' Exp IfHandling')' Instr {printf(".end_if%d:\n;ENDIF\n\n",$<num>4);}
161 | IF '(' Exp IfHandling')' Instr IfEndHandling ELSE Instr IfElseEndHandling 172 | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling
162 | WHILE '(' Exp ')' Instr 173 | WHILE '(' Exp ')' Instr
163 | '{' SuiteInstr '}' 174 | '{' SuiteInstr '}'
164 ; 175 ;
165IfHandling: {printf("pop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);}; 176IfHandling: {printf("\n;BEGINIF\npop rax\ncmp rax,0\njz .end_if%d\n",$<num>$ = num_if++);};
166IfEndHandling: {printf(".end_if%d\n",$<num>-2);}; 177IfEndHandling: {printf("jmp .end_ifelse%d\n.end_if%d:\n",$<num>-3,$<num>-3);};
167IfElseEndHandling: {printf(".end_if%d\n",$<num>-4);}; 178IfElseEndHandling: {printf(".end_ifelse%d:\n;ENDIF\n\n",$<num>-5);};
168Exp: 179Exp:
169 LValue '=' Exp {if(status == GLOBAL){ 180 LValue '=' Exp {
181 if(status == GLOBAL){
170 $$ = glo_lookup($<ident>1); 182 $$ = glo_lookup($<ident>1);
171 printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1); 183 printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);
172 } 184 }
173 else { 185 else {
174 $$ = loc_lookup($<ident>1); 186 $$ = loc_lookup($<ident>1);
175 printf("pop QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1); 187 printf("pop QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);
176 } 188 }
177 } 189 }
178 | EB 190 | EB
179 ; 191 ;
180EB: 192EB:
181 EB {check_expected_type($1,INT); printf("pop rax\n");} 193 EB OR TB {
182 OR TB {check_expected_type($4,INT);} 194 check_expected_type($1,INT);check_expected_type($3,INT);
195 printf(";a OR c\npop rax\n");
196 printf("cmp rax,1\nje .true%d\npop rcx\ncmp rcx,1\njz .true%d\npush 0\njmp .false%d\n",num_label,num_label,num_label);
197 printf(".true%d:\npush 1\n.false%d:",num_label,num_label);
198 num_label++;
199 }
183 | TB 200 | TB
184 ; 201 ;
185TB: 202TB:
186 TB AND FB {check_expected_type($1,INT);check_expected_type($3,INT);} 203 TB AND FB {
204 check_expected_type($1,INT);check_expected_type($3,INT);
205 printf(";a AND c\npop rax\n");
206 printf("cmp rax,0\njz .false%d\npop rcx\ncmp rcx,0\njz .false%d\npush 1\njmp .true%d\n",num_label,num_label,num_label);
207 printf(".false%d:\npush 0\n.true%d:",num_label,num_label);
208 num_label++;
209 }
187 | FB 210 | FB
188 ; 211 ;
189FB: 212FB:
190 FB EQ M {check_expected_type($1,INT);check_expected_type($3,INT);} 213 FB EQ M {
214 check_expected_type($1,INT);check_expected_type($3,INT);
215 printf(";a EQ c\npop rax\npop rcx\ncmp rax,rcx\n");
216 if(!strcmp($2,"==")){
217 printf("je .true%d\n",num_label);
218 }else{
219 printf("jne .true%d\n",num_label);
220 }
221 printf("push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:",num_label,num_label,num_label);
222 num_label++;
223 }
191 | M 224 | M
192 ; 225 ;
193M: 226M:
194 M ORDER E {check_expected_type($1,INT);check_expected_type($3,INT);} 227 M ORDER E {
228 check_expected_type($1,INT);check_expected_type($3,INT);
229 printf(";a ORDER c\npop rcx\npop rax\ncmp rax,rcx\n");
230 if(!strcmp($2,"<")){
231 printf("jl .true%d\n",num_label);
232 }else if(!strcmp($2,"<=")){
233 printf("jle .true%d\n",num_label);
234 }else if(!strcmp($2,">")){
235 printf("jg .true%d\n",num_label);
236 }else{
237 printf("jge .true%d\n",num_label);
238 }
239 printf("push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:",num_label,num_label,num_label);
240 num_label++;
241 }
195 | E 242 | E
196 ; 243 ;
197E: 244E:
198 E ADDSUB T {check_expected_type($1,INT);check_expected_type($3,INT); 245 E ADDSUB T {
199 if($2 == '+'){ 246 check_expected_type($1,INT);check_expected_type($3,INT);
200 printf(";E + T\npop rcx\npop rax\nadd rax,rcx\npush rax\n"); 247 if($2 == '+'){
201 } 248 printf(";E + T\npop rcx\npop rax\nadd rax,rcx\npush rax\n");
202 else{ 249 }
203 printf(";E - T\npop rcx\npop rax\nsub rax,rcx\npush rax\n"); 250 else{
204 } 251 printf(";E - T\npop rcx\npop rax\nsub rax,rcx\npush rax\n");
252 }
205 } 253 }
206 | T 254 | T
207 ; 255 ;
208T: 256T:
209 T DIVSTAR F {check_expected_type($1,INT);check_expected_type($3,INT); 257 T DIVSTAR F {
210 if($2 == '*'){ 258 check_expected_type($1,INT);check_expected_type($3,INT);
211 printf(";E * T\npop rax\npop rcx\nimul rax,rcx\npush rax\n"); 259 if($2 == '*'){
212 } 260 printf(";E * T\npop rax\npop rcx\nimul rax,rcx\npush rax\n");
213 else{
214 printf(";E / T\npop rax\npop rcx\nxor rdx,rdx\nidiv rcx\n");
215 if ($2 == '/'){
216 printf("push rax\n");
217 } 261 }
218 else{ 262 else{
219 printf("push rdx\n"); 263 printf(";E / T\npop rax\npop rcx\nxor rdx,rdx\nidiv rcx\n");
264 if ($2 == '/'){
265 printf("push rax\n");
266 }
267 else{
268 printf("push rdx\n");
269 }
220 } 270 }
221 } 271 }
222 }
223 | F 272 | F
224 ; 273 ;
225F: 274F:
@@ -233,17 +282,37 @@ F:
233 } 282 }
234 | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} 283 | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");}
235 | '(' Exp ')' {$$ = $2;} 284 | '(' Exp ')' {$$ = $2;}
236 | LValue {if(status == GLOBAL) {$$ = glo_lookup($<ident>1);printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);} 285 | LValue {
237 else {$$ = loc_lookup($<ident>1); printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);}} 286 if(status == GLOBAL) {
287 $$ = glo_lookup($<ident>1);
288 printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);
289 }
290 else {
291 $$ = loc_lookup($<ident>1);
292 printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);
293 }
294 }
238 | NUM {$$ = INT; if(status == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique 295 | NUM {$$ = INT; if(status == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique
239 | CARACTERE {$$ = CHAR; if(status == LOCAL) printf("push %d\n",$1);} 296