aboutsummaryrefslogtreecommitdiff
path: root/src/tpc.y
diff options
context:
space:
mode:
authorpacien2018-05-07 02:24:04 +0200
committerpacien2018-05-07 02:24:04 +0200
commit91468b81ccad51eb3f8c0fbc90270a893ff040a9 (patch)
tree9f2477e5326b7388d0268465952b7525e32861a4 /src/tpc.y
parent15390a3b612562a8f1c995e968cd3b1943375ab9 (diff)
downloadtpc-compiler-91468b81ccad51eb3f8c0fbc90270a893ff040a9.tar.gz
Extract generator functions (partial commit)
Diffstat (limited to 'src/tpc.y')
-rw-r--r--src/tpc.y108
1 files changed, 33 insertions, 75 deletions
diff --git a/src/tpc.y b/src/tpc.y
index b0d3633..14f3488 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -13,13 +13,12 @@
13 13
14#include <stdio.h> 14#include <stdio.h>
15#include "symbol_table.h" 15#include "symbol_table.h"
16#include "generator.h"
16 17
17extern int lineno; 18extern int lineno;
18int yylex(); 19int yylex();
19void yyerror(char *); 20void yyerror(char *);
20#define GLOBAL 0 21static Scope scope = GLOBAL;
21#define LOCAL 1
22static int status = GLOBAL;
23static int num_label = 0; 22static int num_label = 0;
24static int num_if = 0; 23static int num_if = 0;
25%} 24%}
@@ -50,100 +49,59 @@ static int num_if = 0;
50%precedence ELSE 49%precedence ELSE
51 50
52%% 51%%
53Prog:{printf("extern printf\n"); 52Prog: { prologue(); }
54 printf("section .data\n"); 53 DeclConsts DeclVars DeclFoncts { const_declaration(); }
55 printf("format_int db \"%%d\",10,0\n\n");
56 printf("section .bss\nsection .text\n\nglobal _start\n");
57 printf("print: ;print needs an argument in rax\n");
58 printf("push rbp\n");
59 printf("mov rbp, rsp\n");
60 printf("push rsi\n");
61 printf("mov rsi, rax\n");
62 printf("mov rdi, format_int\n");
63 printf("mov rax, 0\n");
64 printf("call printf WRT ..plt\n");
65 printf("pop rsi\n");
66 printf("pop rbp\n");
67 printf("ret\n");
68 printf("\n_start:\n");
69 printf("push rbp\nmov rbp, rsp\n\n");
70 }
71 DeclConsts DeclVars DeclFoncts
72 {
73 printf("mov rax,60 \n");
74 printf("mov rdi,0 \n");
75 printf("syscall \n");
76 printf(";global table\n");
77 glo_display_table();
78 printf(";local table\n");
79 loc_display_table();
80 }
81 ; 54 ;
82DeclConsts: 55DeclConsts:
83 DeclConsts CONST ListConst ';' 56 DeclConsts CONST ListConst ';'
84 | 57 |
85 ; 58 ;
86ListConst: 59ListConst:
87 ListConst ',' IDENT '=' Litteral 60 ListConst ',' IDENT '=' Litteral
88 | IDENT '=' Litteral 61 | IDENT '=' Litteral
89 ; 62 ;
90Litteral: 63Litteral:
91 NombreSigne 64 NombreSigne
92 | CARACTERE 65 | CARACTERE
93 ; 66 ;
94NombreSigne: 67NombreSigne:
95 NUM 68 NUM
96 | ADDSUB NUM 69 | ADDSUB NUM
97 ; 70 ;
98DeclVars: 71DeclVars:
99 DeclVars TYPE Declarateurs ';' 72 DeclVars TYPE Declarateurs ';'
100 | 73 |
101 ; 74 ;
102Declarateurs: 75Declarateurs:
103 Declarateurs ',' Declarateur { 76 Declarateurs ',' Declarateur { declaration($<ident>3, $<type>0, scope); }
104 if(status == GLOBAL) glo_addVar($<ident>3, $<type>0); 77 | Declarateur { declaration($<ident>1, $<type>0, scope); }
105 else loc_addVar($<ident>3, $<type>0);
106 printf("push 0\n");
107 }
108 | Declarateur {
109 if(status == GLOBAL) glo_addVar($<ident>1, $<type>0);
110 else loc_addVar($<ident>1, $<type>0);
111 printf("push 0\n");
112 }
113 ; 78 ;
114Declarateur: 79Declarateur:
115 IDENT 80 IDENT
116 | IDENT '[' NUM ']' 81 | IDENT '[' NUM ']'
117 ; 82 ;
118DeclFoncts: 83DeclFoncts:
119 DeclFoncts DeclFonct 84 DeclFoncts DeclFonct
120 | DeclFonct 85 | DeclFonct
121 ; 86 ;
122DeclFonct: 87DeclFonct:
123 EnTeteFonct {status = LOCAL;} Corps {status = GLOBAL;} 88 EnTeteFonct { scope = LOCAL; }
89 Corps { scope = GLOBAL; }
124 ; 90 ;
125EnTeteFonct: 91EnTeteFonct:
126 TYPE IDENT '(' Parametres ')' 92 TYPE IDENT '(' Parametres ')'
127 | VOID IDENT '(' Parametres ')' 93 | VOID IDENT '(' Parametres ')'
128 ; 94 ;
129Parametres: 95Parametres:
130 VOID 96 VOID
131 | ListTypVar 97 | ListTypVar
132 ; 98 ;
133ListTypVar: 99ListTypVar:
134 ListTypVar ',' TYPE IDENT { 100 ListTypVar ',' TYPE IDENT { declaration($<ident>4, $<type>3, scope); }
135 if(status == GLOBAL) glo_addVar($<ident>4, $<type>3); 101 | TYPE IDENT { declaration($<ident>2, $<type>1, scope); }
136 else loc_addVar($<ident>4, $<type>3);
137 printf("push 0\n");
138 }
139 | TYPE IDENT {
140 if(status == GLOBAL) glo_addVar($<ident>2, $<type>1);
141 else loc_addVar($<ident>2, $<type>1);
142 printf("push 0\n");
143 }
144 ; 102 ;
145Corps: 103Corps:
146 '{' DeclConsts DeclVars SuiteInstr '}' 104 '{' DeclConsts DeclVars SuiteInstr '}'
147 ; 105 ;
148SuiteInstr: 106SuiteInstr:
149 SuiteInstr Instr 107 SuiteInstr Instr
@@ -154,11 +112,11 @@ Instr:
154 | RETURN Exp ';' 112 | RETURN Exp ';'
155 | RETURN ';' 113 | RETURN ';'
156 | READE '(' IDENT ')' ';' { 114 | READE '(' IDENT ')' ';' {
157 if(status == GLOBAL) glo_lookup($<ident>3); 115 if(scope == GLOBAL) glo_lookup($<ident>3);
158 else loc_lookup($<ident>3); 116 else loc_lookup($<ident>3);
159 } 117 }
160 | READC '(' IDENT ')' ';' { 118 | READC '(' IDENT ')' ';' {
161 if(status == GLOBAL) glo_lookup($<ident>3); 119 if(scope == GLOBAL) glo_lookup($<ident>3);
162 else loc_lookup($<ident>3); 120 else loc_lookup($<ident>3);
163 } 121 }
164 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");} 122 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");}
@@ -172,7 +130,7 @@ IfEndHandling: {printf("jmp .end_ifelse%d\n.end_if%d:\n",$<nu
172IfElseEndHandling: {printf(".end_ifelse%d:\n;ENDIF\n\n",$<num>-5);}; 130IfElseEndHandling: {printf(".end_ifelse%d:\n;ENDIF\n\n",$<num>-5);};
173Exp: 131Exp:
174 LValue '=' Exp { 132 LValue '=' Exp {
175 if(status == GLOBAL){ 133 if(scope == GLOBAL){
176 $$ = glo_lookup($<ident>1); 134 $$ = glo_lookup($<ident>1);
177 printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1); 135 printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);
178 } 136 }
@@ -277,7 +235,7 @@ F:
277 | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} 235 | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");}
278 | '(' Exp ')' {$$ = $2;} 236 | '(' Exp ')' {$$ = $2;}
279 | LValue { 237 | LValue {
280 if(status == GLOBAL) { 238 if(scope == GLOBAL) {
281 $$ = glo_lookup($<ident>1); 239 $$ = glo_lookup($<ident>1);
282 printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1); 240 printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);
283 } 241 }
@@ -286,13 +244,13 @@ F:
286 printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1); 244 printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);
287 } 245 }
288 } 246 }
289 | NUM {$$ = INT; if(status == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique 247 | NUM {$$ = INT; if(scope == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique
290 | CARACTERE {$$ = CHAR; if(status == LOCAL) printf("push %d\n",$1);} 248 | CARACTERE {$$ = CHAR; if(scope == LOCAL) printf("push %d\n",$1);}
291 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement 249 | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement
292 ; 250 ;
293LValue: 251LValue:
294 IDENT { 252 IDENT {
295 if(status == GLOBAL) { 253 if(scope == GLOBAL) {
296 glo_lookup($<ident>1); 254 glo_lookup($<ident>1);
297 } 255 }
298 else { 256 else {
@@ -300,7 +258,7 @@ LValue:
300 } 258 }
301 } 259 }
302 | IDENT '[' Exp ']' { 260 | IDENT '[' Exp ']' {
303 if(status == GLOBAL) { 261 if(scope == GLOBAL) {
304 glo_lookup($<ident>1); 262 glo_lookup($<ident>1);
305 } 263 }
306 else { 264 else {