diff options
Diffstat (limited to 'src/tpc.y')
-rw-r--r-- | src/tpc.y | 116 |
1 files changed, 59 insertions, 57 deletions
@@ -15,14 +15,14 @@ int yylex(); | |||
15 | void yyerror(char *); | 15 | void yyerror(char *); |
16 | static Scope scope = GLOBAL; | 16 | static Scope scope = GLOBAL; |
17 | static Type return_type = VOID_T; | 17 | static Type return_type = VOID_T; |
18 | static int bss_done = 0; | 18 | static bool bss_done = false; |
19 | static int num_label = 0; | 19 | static int num_label = 0; |
20 | static int num_if = 0; | 20 | static int num_if = 0; |
21 | static int num_while = 0; | 21 | static int num_while = 0; |
22 | static int nb_param[255]; | 22 | static int nb_param[255]; |
23 | static int num_scope = -1; | 23 | static int num_scope = -1; |
24 | static int offset = 0; | 24 | static int offset = 0; |
25 | static int is_array = 0; | 25 | static bool is_array = false; |
26 | static char fname[64]; | 26 | static char fname[64]; |
27 | %} | 27 | %} |
28 | 28 | ||
@@ -76,50 +76,49 @@ DeclVars: | |||
76 | | | 76 | | |
77 | ; | 77 | ; |
78 | Declarateurs: | 78 | Declarateurs: |
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 | ; |
94 | Declarateur: | 84 | Declarateur: |
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 | ; |
98 | DeclFoncts: | 88 | DeclFoncts: |
99 | DeclFoncts DeclFonct | 89 | DeclFoncts DeclFonct |
100 | | DeclFonct | 90 | | DeclFonct |
101 | ; | 91 | ; |
102 | DeclFonct: | 92 | DeclFonct: |
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 | ; |
106 | EnTeteFonct: | 97 | EnTeteFonct: |
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 | ||
111 | PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; | 106 | PrologueCont: { scope = LOCAL; gen_prologue_continue(&bss_done); }; |
112 | 107 | ||
113 | Parametres: | 108 | Parametres: |
114 | VOID {$$ = 0;} | 109 | VOID { $$ = 0; } |
115 | | ListTypVar { $<num>$ = $<num>1;} | 110 | | ListTypVar { $<num>$ = $<num>1; } |
116 | ; | 111 | ; |
117 | ListTypVar: | 112 | ListTypVar: |
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 | ; |
121 | Corps: | 116 | Corps: |
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 | ; |
124 | SuiteInstr: | 123 | SuiteInstr: |
125 | SuiteInstr Instr | 124 | SuiteInstr Instr |
@@ -128,67 +127,70 @@ SuiteInstr: | |||
128 | Instr: | 127 | Instr: |
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 | ; |
141 | IfHandling: { gen_if_start($<num>$ = num_if++); }; | 143 | IfHandling: { gen_if_start($<num>$ = num_if++); }; |
142 | IfEndHandling: { gen_if_end($<num>-3); }; | 144 | IfEndHandling: { gen_if_end($<num>-3); }; |
143 | IfElseEndHandling: { gen_ifelse_end($<num>-5); }; | 145 | IfElseEndHandling: { gen_ifelse_end($<num>-5); }; |
144 | Exp: | 146 | Exp: |
145 | LValue '=' Exp { $$ = gen_assign($<ident>1, scope); } | 147 | LValue '=' Exp { $$ = gen_assign($<ident>1, scope); } |
146 | | EB | 148 | | EB |
147 | ; | 149 | ; |
148 | EB: | 150 | EB: |
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 | ; |
152 | TB: | 154 | TB: |
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 | ; |
156 | FB: | 158 | FB: |
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 | ; |
160 | M: | 162 | M: |
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 | ; |
164 | E: | 166 | E: |
165 | E ADDSUB T { gen_addsub($2, $1, $3); } | 167 | E ADDSUB T { gen_addsub($2, $1, $3); } |
166 | | T | 168 | | T |
167 | ; | 169 | ; |
168 | T: | 170 | T: |
169 | T DIVSTAR F { gen_divstar($2, $1, $3); } | 171 | T DIVSTAR F { gen_divstar($2, $1, $3); } |
170 | | F | 172 | | F |
171 | ; | 173 | ; |
172 | F: | 174 | F: |
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 | ; |
181 | LValue: | 183 | LValue: |
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 | ; |
185 | Arguments: | 187 | Arguments: |
186 | ListExp | 188 | ListExp |
187 | | {$<num>$ = 0;} | 189 | | {$<num>$ = 0;} |
188 | ; | 190 | ; |
189 | ListExp: | 191 | ListExp: |
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 | ||