aboutsummaryrefslogtreecommitdiff
path: root/src/tpc.y
diff options
context:
space:
mode:
Diffstat (limited to 'src/tpc.y')
-rw-r--r--src/tpc.y116
1 files changed, 59 insertions, 57 deletions
diff --git a/src/tpc.y b/src/tpc.y
index 6e733d0..a21100d 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -15,14 +15,14 @@ int yylex();
15void yyerror(char *); 15void yyerror(char *);
16static Scope scope = GLOBAL; 16static Scope scope = GLOBAL;
17static Type return_type = VOID_T; 17static Type return_type = VOID_T;
18static int bss_done = 0; 18static bool bss_done = false;
19static int num_label = 0; 19static int num_label = 0;
20static int num_if = 0; 20static int num_if = 0;
21static int num_while = 0; 21static int num_while = 0;
22static int nb_param[255]; 22static int nb_param[255];
23static int num_scope = -1; 23static int num_scope = -1;
24static int offset = 0; 24static int offset = 0;
25static int is_array = 0; 25static bool is_array = false;
26static char fname[64]; 26static char fname[64];
27%} 27%}
28 28
@@ -76,50 +76,49 @@ DeclVars:
76| 76|
77; 77;
78Declarateurs: 78Declarateurs:
79 Declarateurs ',' Declarateur { 79 Declarateurs ',' Declarateur { if (is_array) gen_tab_declaration($<ident>3, scope, offset);
80 if(!is_array){ 80 else gen_declaration($<ident>3, $<type>0, scope); }
81 gen_declaration($<ident>3, $<type>0, scope); 81| Declarateur { if (is_array) gen_tab_declaration($<ident>1, scope, offset);
82 }else{ 82 else gen_declaration($<ident>1, $<type>0, scope); }
83 gen_tab_declaration($<ident>3, scope, offset);
84 }
85 }
86| Declarateur {
87 if(!is_array){
88 gen_declaration($<ident>1, $<type>0, scope);
89 }else{
90 gen_tab_declaration($<ident>1, scope, offset);
91 }
92 }
93; 83;
94Declarateur: 84Declarateur:
95 IDENT {strcpy($$,$1);is_array = 0;} 85 IDENT { strcpy($$, $1); is_array = false; }
96| IDENT '[' NUM ']' {offset = $<num>3;strcpy($$,$1);is_array=1;} 86| IDENT '[' NUM ']' { offset = $<num>3; strcpy($$, $1); is_array = true; }
97; 87;
98DeclFoncts: 88DeclFoncts:
99 DeclFoncts DeclFonct 89 DeclFoncts DeclFonct
100| DeclFonct 90| DeclFonct
101; 91;
102DeclFonct: 92DeclFonct:
103 EnTeteFonct { scope = LOCAL; } 93 EnTeteFonct { scope = LOCAL; }
104 Corps { gen_function_end_declaration(fname,return_type,nb_param[num_scope]); scope = GLOBAL; return_type = VOID_T; num_scope--; loc_clean_table(); } 94 Corps { gen_function_end_declaration(fname, return_type, nb_param[num_scope]);
95 scope = GLOBAL; return_type = VOID_T; num_scope--; loc_clean_table(); }
105; 96;
106EnTeteFonct: 97EnTeteFonct:
107 TYPE IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, $<type>1);} '(' Parametres ')' { nb_param[++num_scope] = $<num>6 ; scope = GLOBAL;} 98 TYPE IDENT PrologueCont { strcpy(fname, $<ident>2);
108| VOID IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, VOID_T);} '(' Parametres ')' { nb_param[++num_scope] = $<num>6 ; scope = GLOBAL; } 99 return_type = gen_function_declaration($<ident>2, $<type>1); }
100 '(' Parametres ')' { nb_param[++num_scope] = $<num>6; scope = GLOBAL;}
101| VOID IDENT PrologueCont { strcpy(fname, $<ident>2);
102 return_type = gen_function_declaration($<ident>2, VOID_T); }
103 '(' Parametres ')' { nb_param[++num_scope] = $<num>6; scope = GLOBAL; }
109; 104;
110 105
111PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; 106PrologueCont: { scope = LOCAL; gen_prologue_continue(&bss_done); };
112 107
113Parametres: 108Parametres:
114 VOID {$$ = 0;} 109 VOID { $$ = 0; }
115| ListTypVar { $<num>$ = $<num>1;} 110| ListTypVar { $<num>$ = $<num>1; }
116; 111;
117ListTypVar: 112ListTypVar:
118 ListTypVar ',' TYPE IDENT { gen_declaration($<ident>4, $<type>3, scope); $<num>$ = $<num>1+1; } 113 ListTypVar ',' TYPE IDENT { gen_declaration($<ident>4, $<type>3, scope); $<num>$ = $<num>1+1; }
119| TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; } 114| TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; }
120; 115;
121Corps: 116Corps:
122 '{' {int i; for(i=1;i<=nb_param[num_scope];i++){fprintf(output, "mov r8, [rbp + %d]\nmov [rbp - %d], r8\n", (nb_param[num_scope]-i+2)*8, i*8);} } DeclConsts DeclVars SuiteInstr '}' 117 '{' { int i;
118 for(i=1;i<=nb_param[num_scope];i++){
119 fprintf(output, "mov r8, [rbp + %d]\nmov [rbp - %d], r8\n", (nb_param[num_scope]-i+2)*8, i*8);
120 } }
121 DeclConsts DeclVars SuiteInstr '}'
123; 122;
124SuiteInstr: 123SuiteInstr:
125 SuiteInstr Instr 124 SuiteInstr Instr
@@ -128,67 +127,70 @@ SuiteInstr:
128Instr: 127Instr:
129 Exp ';' 128 Exp ';'
130| ';' 129| ';'
131| RETURN Exp ';' { gen_function_return(return_type, $<type>2); } 130| RETURN Exp ';' { gen_function_return(return_type, $<type>2); }
132| RETURN ';' { gen_function_return(return_type, VOID_T);} 131| RETURN ';' { gen_function_return(return_type, VOID_T); }
133| READE '(' IDENT ')' ';' { gen_reade($<ident>3, scope); } 132| READE '(' IDENT ')' ';' { gen_reade($<ident>3, scope); }
134| READC '(' IDENT ')' ';' { gen_readc($<ident>3, scope); } 133| READC '(' IDENT ')' ';' { gen_readc($<ident>3, scope); }
135| PRINT '(' Exp ')' ';' { gen_print($<type>3);} 134| PRINT '(' Exp ')' ';' { gen_print($<type>3);}
136| IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); } 135| IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); }
137| IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling 136| IF '(' Exp IfHandling')' Instr
138| WHILE {fprintf(output,".upwhile%d:\n",num_while);}'(' Exp {fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n",num_while);}')' Instr {fprintf(output,"jmp .upwhile%d\n.downwhile%d:\n",num_while,num_while);num_while++;} 137 ELSE IfEndHandling Instr IfElseEndHandling
138| WHILE { fprintf(output,".upwhile%d:\n", num_while); }
139 '(' Exp { fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n", num_while); }
140 ')' Instr { fprintf(output,"jmp .upwhile%d\n.downwhile%d:\n", num_while, num_while); num_while++; }
139| '{' SuiteInstr '}' 141| '{' SuiteInstr '}'
140; 142;
141IfHandling: { gen_if_start($<num>$ = num_if++); }; 143IfHandling: { gen_if_start($<num>$ = num_if++); };
142IfEndHandling: { gen_if_end($<num>-3); }; 144IfEndHandling: { gen_if_end($<num>-3); };
143IfElseEndHandling: { gen_ifelse_end($<num>-5); }; 145IfElseEndHandling: { gen_ifelse_end($<num>-5); };
144Exp: 146Exp:
145 LValue '=' Exp { $$ = gen_assign($<ident>1, scope); } 147 LValue '=' Exp { $$ = gen_assign($<ident>1, scope); }
146| EB 148| EB
147; 149;
148EB: 150EB:
149 EB OR TB { gen_or($1, $3, num_label++); } 151 EB OR TB { gen_or($1, $3, num_label++); }
150| TB 152| TB
151; 153;
152TB: 154TB:
153 TB AND FB { gen_and($1, $3, num_label++); } 155 TB AND FB { gen_and($1, $3, num_label++); }
154| FB 156| FB
155; 157;
156FB: 158FB:
157 FB EQ M { gen_eq($2, $1, $3, num_label++); } 159 FB EQ M { gen_eq($2, $1, $3, num_label++); }
158| M 160| M
159; 161;
160M: 162M:
161 M ORDER E { gen_order($2, $1, $3, num_label++); } 163 M ORDER E { gen_order($2, $1, $3, num_label++); }
162| E 164| E
163; 165;
164E: 166E:
165 E ADDSUB T { gen_addsub($2, $1, $3); } 167 E ADDSUB T { gen_addsub($2, $1, $3); }
166| T 168| T
167; 169;
168T: 170T:
169 T DIVSTAR F { gen_divstar($2, $1, $3); } 171 T DIVSTAR F { gen_divstar($2, $1, $3); }
170| F 172| F
171 ; 173 ;
172F: 174F:
173 ADDSUB F { $$ = gen_signed_expr($1, $2); } 175 ADDSUB F { $$ = gen_signed_expr($1, $2); }
174| '!' F { $$ = gen_negate_expr($2); } 176| '!' F { $$ = gen_negate_expr($2); }
175| '(' Exp ')' { $$ = $2; } 177| '(' Exp ')' { $$ = $2; }
176| LValue { $$ = gen_value($<ident>1, scope); } 178| LValue { $$ = gen_value($<ident>1, scope); }
177| NUM { $$ = gen_num($1, scope); } 179| NUM { $$ = gen_num($1, scope); }
178| CARACTERE { $$ = gen_char($1, scope); } 180| CARACTERE { $$ = gen_char($1, scope); }
179| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } 181| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1, $<num>3); }
180; 182;
181LValue: 183LValue:
182 IDENT { gen_check($<ident>1, scope); strcpy($$, $1);} 184 IDENT { gen_check($<ident>1, scope); strcpy($$, $1);}
183| IDENT '[' Exp ']' { gen_check($<ident>1, scope); strcpy($$, $1);} 185| IDENT '[' Exp ']' { gen_check($<ident>1, scope); strcpy($$, $1);}
184; 186;
185Arguments: 187Arguments:
186 ListExp 188 ListExp
187| {$<num>$ = 0;} 189| {$<num>$ = 0;}
188; 190;
189ListExp: 191ListExp:
190 ListExp ',' Exp {$<num>$ = $<num>1 + 1;} 192 ListExp ',' Exp { $<num>$ = $<num>1 + 1; }
191| Exp {$<num>$ = 1;} 193| Exp { $<num>$ = 1; }
192; 194;
193%% 195%%
194 196