aboutsummaryrefslogtreecommitdiff
path: root/src/tpc.y
diff options
context:
space:
mode:
Diffstat (limited to 'src/tpc.y')
-rw-r--r--src/tpc.y132
1 files changed, 59 insertions, 73 deletions
diff --git a/src/tpc.y b/src/tpc.y
index 74d31c6..a21100d 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -2,11 +2,6 @@
2/* 2/*
3 * UPEM / Compilation / Projet 3 * UPEM / Compilation / Projet
4 * Pacien TRAN-GIRARD, Adam NAILI 4 * Pacien TRAN-GIRARD, Adam NAILI
5 *
6 * TODO :
7 * ------
8 * - arrays
9 *
10 */ 5 */
11 6
12int nb_globals = 0; 7int nb_globals = 0;
@@ -20,14 +15,14 @@ int yylex();
20void yyerror(char *); 15void yyerror(char *);
21static Scope scope = GLOBAL; 16static Scope scope = GLOBAL;
22static Type return_type = VOID_T; 17static Type return_type = VOID_T;
23static int bss_done = 0; 18static bool bss_done = false;
24static int num_label = 0; 19static int num_label = 0;
25static int num_if = 0; 20static int num_if = 0;
26static int num_while = 0; 21static int num_while = 0;
27static int nb_param[255]; 22static int nb_param[255];
28static int num_scope = -1; 23static int num_scope = -1;
29static int offset = 0; 24static int offset = 0;
30static int is_array = 0; 25static bool is_array = false;
31static char fname[64]; 26static char fname[64];
32%} 27%}
33 28
@@ -81,50 +76,49 @@ DeclVars:
81| 76|
82; 77;
83Declarateurs: 78Declarateurs:
84 Declarateurs ',' Declarateur { 79 Declarateurs ',' Declarateur { if (is_array) gen_tab_declaration($<ident>3, scope, offset);
85 if(!is_array){ 80 else gen_declaration($<ident>3, $<type>0, scope); }
86 gen_declaration($<ident>3, $<type>0, scope); 81| Declarateur { if (is_array) gen_tab_declaration($<ident>1, scope, offset);
87 }else{ 82 else gen_declaration($<ident>1, $<type>0, scope); }
88 gen_tab_declaration($<ident>3, scope, offset);
89 }
90 }
91| Declarateur {
92 if(!is_array){
93 gen_declaration($<ident>1, $<type>0, scope);
94 }else{
95 gen_tab_declaration($<ident>1, scope, offset);
96 }
97 }
98; 83;
99Declarateur: 84Declarateur:
100 IDENT {strcpy($$,$1);is_array = 0;} 85 IDENT { strcpy($$, $1); is_array = false; }
101| IDENT '[' NUM ']' {offset = $<num>3;strcpy($$,$1);is_array=1;} 86| IDENT '[' NUM ']' { offset = $<num>3; strcpy($$, $1); is_array = true; }
102; 87;
103DeclFoncts: 88DeclFoncts:
104 DeclFoncts DeclFonct 89 DeclFoncts DeclFonct
105| DeclFonct 90| DeclFonct
106; 91;
107DeclFonct: 92DeclFonct:
108 EnTeteFonct { scope = LOCAL; } 93 EnTeteFonct { scope = LOCAL; }
109 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(); }
110; 96;
111EnTeteFonct: 97EnTeteFonct:
112 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);
113| 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; }
114; 104;
115 105
116PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; 106PrologueCont: { scope = LOCAL; gen_prologue_continue(&bss_done); };
117 107
118Parametres: 108Parametres:
119 VOID {$$ = 0;} 109 VOID { $$ = 0; }
120| ListTypVar { $<num>$ = $<num>1;} 110| ListTypVar { $<num>$ = $<num>1; }
121; 111;
122ListTypVar: 112ListTypVar:
123 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; }
124| TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; } 114| TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; }
125; 115;
126Corps: 116Corps:
127 '{' {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 '}'
128; 122;
129SuiteInstr: 123SuiteInstr:
130 SuiteInstr Instr 124 SuiteInstr Instr
@@ -133,78 +127,70 @@ SuiteInstr:
133Instr: 127Instr:
134 Exp ';' 128 Exp ';'
135| ';' 129| ';'
136| RETURN Exp ';' { gen_function_return(return_type, $<type>2); } 130| RETURN Exp ';' { gen_function_return(return_type, $<type>2); }
137| RETURN ';' { gen_function_return(return_type, VOID_T);} 131| RETURN ';' { gen_function_return(return_type, VOID_T); }
138| READE '(' IDENT ')' ';' { gen_reade($<ident>3, scope); } 132| READE '(' IDENT ')' ';' { gen_reade($<ident>3, scope); }
139| READC '(' IDENT ')' ';' { gen_readc($<ident>3, scope); } 133| READC '(' IDENT ')' ';' { gen_readc($<ident>3, scope); }
140| PRINT '(' Exp ')' ';' { gen_print($<type>3);} 134| PRINT '(' Exp ')' ';' { gen_print($<type>3);}
141| IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); } 135| IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); }
142| IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling 136| IF '(' Exp IfHandling')' Instr
143| 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++; }
144| '{' SuiteInstr '}' 141| '{' SuiteInstr '}'
145; 142;
146IfHandling: { gen_if_start($<num>$ = num_if++); }; 143IfHandling: { gen_if_start($<num>$ = num_if++); };
147IfEndHandling: { gen_if_end($<num>-3); }; 144IfEndHandling: { gen_if_end($<num>-3); };
148IfElseEndHandling: { gen_ifelse_end($<num>-5); }; 145IfElseEndHandling: { gen_ifelse_end($<num>-5); };
149Exp: 146Exp:
150 LValue '=' Exp { 147 LValue '=' Exp { $$ = gen_assign($<ident>1, scope); }
151 if(loc_lookup($<ident>1) != TAB){
152 $$ = gen_assign($<ident>1, scope);
153 }else{
154 $$ = gen_assign_tab($<ident>1,scope);
155 }
156 }
157| EB 148| EB
158; 149;
159EB: 150EB:
160 EB OR TB { gen_or($1, $3, num_label++); } 151 EB OR TB { gen_or($1, $3, num_label++); }
161| TB 152| TB
162; 153;
163TB: 154TB:
164 TB AND FB { gen_and($1, $3, num_label++); } 155 TB AND FB { gen_and($1, $3, num_label++); }
165| FB 156| FB
166; 157;
167FB: 158FB:
168 FB EQ M { gen_eq($2, $1, $3, num_label++); } 159 FB EQ M { gen_eq($2, $1, $3, num_label++); }
169| M 160| M
170; 161;
171M: 162M:
172 M ORDER E { gen_order($2, $1, $3, num_label++); } 163 M ORDER E { gen_order($2, $1, $3, num_label++); }
173| E 164| E
174; 165;
175E: 166E:
176 E ADDSUB T { gen_addsub($2, $1, $3); } 167 E ADDSUB T { gen_addsub($2, $1, $3); }
177| T 168| T
178; 169;
179T: 170T:
180 T DIVSTAR F { gen_divstar($2, $1, $3); } 171 T DIVSTAR F { gen_divstar($2, $1, $3); }
181| F 172| F
182 ; 173 ;
183F: 174F:
184 ADDSUB F { $$ = gen_signed_expr($1, $2); } 175 ADDSUB F { $$ = gen_signed_expr($1, $2); }
185| '!' F { $$ = gen_negate_expr($2); } 176| '!' F { $$ = gen_negate_expr($2); }
186| '(' Exp ')' { $$ = $2; } 177| '(' Exp ')' { $$ = $2; }
187| LValue { if(loc_lookup($<ident>1) != TAB){ 178| LValue { $$ = gen_value($<ident>1, scope); }
188 $$ = gen_value($<ident>1, scope); 179| NUM { $$ = gen_num($1, scope); }
189 }else{ 180| CARACTERE { $$ = gen_char($1, scope); }
190 $$ = gen_value_tab($<ident>1,scope); 181| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1, $<num>3); }
191 }
192 }
193| NUM { $$ = gen_num($1, scope); }
194| CARACTERE { $$ = gen_char($1, scope); }
195| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); }
196; 182;
197LValue: 183LValue:
198 IDENT { gen_check($<ident>1, scope); strcpy($$, $1);} 184 IDENT { gen_check($<ident>1, scope); strcpy($$, $1);}
199| IDENT '[' Exp ']' { gen_check($<ident>1, scope); strcpy($$, $1);} 185| IDENT '[' Exp ']' { gen_check($<ident>1, scope); strcpy($$, $1);}
200; 186;
201Arguments: 187Arguments:
202 ListExp 188 ListExp
203| {$<num>$ = 0;} 189| {$<num>$ = 0;}
204; 190;
205ListExp: 191ListExp:
206 ListExp ',' Exp {$<num>$ = $<num>1 + 1;} 192 ListExp ',' Exp { $<num>$ = $<num>1 + 1; }
207| Exp {$<num>$ = 1;} 193| Exp { $<num>$ = 1; }
208; 194;
209%% 195%%
210 196