diff options
Diffstat (limited to 'src/tpc.y')
-rw-r--r-- | src/tpc.y | 132 |
1 files changed, 59 insertions, 73 deletions
@@ -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 | ||
12 | int nb_globals = 0; | 7 | int nb_globals = 0; |
@@ -20,14 +15,14 @@ int yylex(); | |||
20 | void yyerror(char *); | 15 | void yyerror(char *); |
21 | static Scope scope = GLOBAL; | 16 | static Scope scope = GLOBAL; |
22 | static Type return_type = VOID_T; | 17 | static Type return_type = VOID_T; |
23 | static int bss_done = 0; | 18 | static bool bss_done = false; |
24 | static int num_label = 0; | 19 | static int num_label = 0; |
25 | static int num_if = 0; | 20 | static int num_if = 0; |
26 | static int num_while = 0; | 21 | static int num_while = 0; |
27 | static int nb_param[255]; | 22 | static int nb_param[255]; |
28 | static int num_scope = -1; | 23 | static int num_scope = -1; |
29 | static int offset = 0; | 24 | static int offset = 0; |
30 | static int is_array = 0; | 25 | static bool is_array = false; |
31 | static char fname[64]; | 26 | static char fname[64]; |
32 | %} | 27 | %} |
33 | 28 | ||
@@ -81,50 +76,49 @@ DeclVars: | |||
81 | | | 76 | | |
82 | ; | 77 | ; |
83 | Declarateurs: | 78 | Declarateurs: |
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 | ; |
99 | Declarateur: | 84 | Declarateur: |
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 | ; |
103 | DeclFoncts: | 88 | DeclFoncts: |
104 | DeclFoncts DeclFonct | 89 | DeclFoncts DeclFonct |
105 | | DeclFonct | 90 | | DeclFonct |
106 | ; | 91 | ; |
107 | DeclFonct: | 92 | DeclFonct: |
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 | ; |
111 | EnTeteFonct: | 97 | EnTeteFonct: |
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 | ||
116 | PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; | 106 | PrologueCont: { scope = LOCAL; gen_prologue_continue(&bss_done); }; |
117 | 107 | ||
118 | Parametres: | 108 | Parametres: |
119 | VOID {$$ = 0;} | 109 | VOID { $$ = 0; } |
120 | | ListTypVar { $<num>$ = $<num>1;} | 110 | | ListTypVar { $<num>$ = $<num>1; } |
121 | ; | 111 | ; |
122 | ListTypVar: | 112 | ListTypVar: |
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 | ; |
126 | Corps: | 116 | Corps: |
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 | ; |
129 | SuiteInstr: | 123 | SuiteInstr: |
130 | SuiteInstr Instr | 124 | SuiteInstr Instr |
@@ -133,78 +127,70 @@ SuiteInstr: | |||
133 | Instr: | 127 | Instr: |
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 | ; |
146 | IfHandling: { gen_if_start($<num>$ = num_if++); }; | 143 | IfHandling: { gen_if_start($<num>$ = num_if++); }; |
147 | IfEndHandling: { gen_if_end($<num>-3); }; | 144 | IfEndHandling: { gen_if_end($<num>-3); }; |
148 | IfElseEndHandling: { gen_ifelse_end($<num>-5); }; | 145 | IfElseEndHandling: { gen_ifelse_end($<num>-5); }; |
149 | Exp: | 146 | Exp: |
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 | ; |
159 | EB: | 150 | EB: |
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 | ; |
163 | TB: | 154 | TB: |
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 | ; |
167 | FB: | 158 | FB: |
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 | ; |
171 | M: | 162 | M: |
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 | ; |
175 | E: | 166 | E: |
176 | E ADDSUB T { gen_addsub($2, $1, $3); } | 167 | E ADDSUB T { gen_addsub($2, $1, $3); } |
177 | | T | 168 | | T |
178 | ; | 169 | ; |
179 | T: | 170 | T: |
180 | T DIVSTAR F { gen_divstar($2, $1, $3); } | 171 | T DIVSTAR F { gen_divstar($2, $1, $3); } |
181 | | F | 172 | | F |
182 | ; | 173 | ; |
183 | F: | 174 | F: |
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 | ; |
197 | LValue: | 183 | LValue: |
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 | ; |
201 | Arguments: | 187 | Arguments: |
202 | ListExp | 188 | ListExp |
203 | | {$<num>$ = 0;} | 189 | | {$<num>$ = 0;} |
204 | ; | 190 | ; |
205 | ListExp: | 191 | ListExp: |
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 | ||