diff options
Diffstat (limited to 'src/tpc.y')
-rw-r--r-- | src/tpc.y | 225 |
1 files changed, 93 insertions, 132 deletions
@@ -50,167 +50,128 @@ static int num_if = 0; | |||
50 | 50 | ||
51 | %% | 51 | %% |
52 | Prog: { gen_prologue(); } | 52 | Prog: { gen_prologue(); } |
53 | DeclConsts DeclVars DeclFoncts { gen_const_declaration(); } | 53 | DeclConsts DeclVars DeclFoncts { gen_const_declaration(); } |
54 | ; | 54 | ; |
55 | DeclConsts: | 55 | DeclConsts: |
56 | DeclConsts CONST ListConst ';' | 56 | DeclConsts CONST ListConst ';' |
57 | | | 57 | | |
58 | ; | 58 | ; |
59 | ListConst: | 59 | ListConst: |
60 | ListConst ',' IDENT '=' Litteral | 60 | ListConst ',' IDENT '=' Litteral |
61 | | IDENT '=' Litteral | 61 | | IDENT '=' Litteral |
62 | ; | 62 | ; |
63 | Litteral: | 63 | Litteral: |
64 | NombreSigne | 64 | NombreSigne |
65 | | CARACTERE | 65 | | CARACTERE |
66 | ; | 66 | ; |
67 | NombreSigne: | 67 | NombreSigne: |
68 | NUM | 68 | NUM |
69 | | ADDSUB NUM | 69 | | ADDSUB NUM |
70 | ; | 70 | ; |
71 | DeclVars: | 71 | DeclVars: |
72 | DeclVars TYPE Declarateurs ';' | 72 | DeclVars TYPE Declarateurs ';' |
73 | | | 73 | | |
74 | ; | 74 | ; |
75 | Declarateurs: | 75 | Declarateurs: |
76 | Declarateurs ',' Declarateur { gen_declaration($<ident>3, $<type>0, scope); } | 76 | Declarateurs ',' Declarateur { gen_declaration($<ident>3, $<type>0, scope); } |
77 | | Declarateur { gen_declaration($<ident>1, $<type>0, scope); } | 77 | | Declarateur { gen_declaration($<ident>1, $<type>0, scope); } |
78 | ; | 78 | ; |
79 | Declarateur: | 79 | Declarateur: |
80 | IDENT | 80 | IDENT |
81 | | IDENT '[' NUM ']' | 81 | | IDENT '[' NUM ']' |
82 | ; | 82 | ; |
83 | DeclFoncts: | 83 | DeclFoncts: |
84 | DeclFoncts DeclFonct | 84 | DeclFoncts DeclFonct |
85 | | DeclFonct | 85 | | DeclFonct |
86 | ; | 86 | ; |
87 | DeclFonct: | 87 | DeclFonct: |
88 | EnTeteFonct { scope = LOCAL; } | 88 | EnTeteFonct { scope = LOCAL; } |
89 | Corps { scope = GLOBAL; } | 89 | Corps { scope = GLOBAL; } |
90 | ; | 90 | ; |
91 | EnTeteFonct: | 91 | EnTeteFonct: |
92 | TYPE IDENT '(' Parametres ')' | 92 | TYPE IDENT '(' Parametres ')' |
93 | | VOID IDENT '(' Parametres ')' | 93 | | VOID IDENT '(' Parametres ')' |
94 | ; | 94 | ; |
95 | Parametres: | 95 | Parametres: |
96 | VOID | 96 | VOID |
97 | | ListTypVar | 97 | | ListTypVar |
98 | ; | 98 | ; |
99 | ListTypVar: | 99 | ListTypVar: |
100 | ListTypVar ',' TYPE IDENT { gen_declaration($<ident>4, $<type>3, scope); } | 100 | ListTypVar ',' TYPE IDENT { gen_declaration($<ident>4, $<type>3, scope); } |
101 | | TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); } | 101 | | TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); } |
102 | ; | 102 | ; |
103 | Corps: | 103 | Corps: |
104 | '{' DeclConsts DeclVars SuiteInstr '}' | 104 | '{' DeclConsts DeclVars SuiteInstr '}' |
105 | ; | 105 | ; |
106 | SuiteInstr: | 106 | SuiteInstr: |
107 | SuiteInstr Instr | 107 | SuiteInstr Instr |
108 | | | 108 | | |
109 | ; | 109 | ; |
110 | Instr: | 110 | Instr: |
111 | Exp ';' | 111 | Exp ';' |
112 | | ';' | 112 | | ';' |
113 | | RETURN Exp ';' | 113 | | RETURN Exp ';' |
114 | | RETURN ';' | 114 | | RETURN ';' |
115 | | READE '(' IDENT ')' ';' { gen_read($<ident>3, scope); } | 115 | | READE '(' IDENT ')' ';' { gen_read($<ident>3, scope); } |
116 | | READC '(' IDENT ')' ';' { gen_read($<ident>3, scope); } | 116 | | READC '(' IDENT ')' ';' { gen_read($<ident>3, scope); } |
117 | | PRINT '(' Exp ')' ';' { gen_print();} | 117 | | PRINT '(' Exp ')' ';' { gen_print();} |
118 | | IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); } | 118 | | IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); } |
119 | | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling | 119 | | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling |
120 | | WHILE '(' Exp ')' Instr | 120 | | WHILE '(' Exp ')' Instr |
121 | | '{' SuiteInstr '}' | 121 | | '{' SuiteInstr '}' |
122 | ; | 122 | ; |
123 | IfHandling: { gen_if_start($<num>$ = num_if++); }; | 123 | IfHandling: { gen_if_start($<num>$ = num_if++); }; |
124 | IfEndHandling: { gen_if_end($<num>-3); }; | 124 | IfEndHandling: { gen_if_end($<num>-3); }; |
125 | IfElseEndHandling: { gen_ifelse_end($<num>-5); }; | 125 | IfElseEndHandling: { gen_ifelse_end($<num>-5); }; |
126 | Exp: | 126 | Exp: |
127 | LValue '=' Exp { /* TODO: extract this into func */ | 127 | LValue '=' Exp { $$ = gen_assign($<ident>1, scope); } |
128 | if(scope == GLOBAL){ | 128 | | EB |
129 | $$ = glo_lookup($<ident>1); | 129 | ; |
130 | printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1); | ||
131 | } | ||
132 | else { | ||
133 | $$ = loc_lookup($<ident>1); | ||
134 | printf("pop QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1); | ||
135 | } | ||
136 | } | ||
137 | | EB | ||
138 | ; | ||
139 | EB: | 130 | EB: |
140 | EB OR TB { gen_or($1, $3, num_label++); } | 131 | EB OR TB { gen_or($1, $3, num_label++); } |
141 | | TB | 132 | | TB |
142 | ; | 133 | ; |
143 | TB: | 134 | TB: |
144 | TB AND FB { gen_and($1, $3, num_label++); } | 135 | TB AND FB { gen_and($1, $3, num_label++); } |
145 | | FB | 136 | | FB |
146 | ; | 137 | ; |
147 | FB: | 138 | FB: |
148 | FB EQ M { gen_eq($2, $1, $3, num_label++); } | 139 | FB EQ M { gen_eq($2, $1, $3, num_label++); } |
149 | | M | 140 | | M |
150 | ; | 141 | ; |
151 | M: | 142 | M: |
152 | M ORDER E { gen_order($2, $1, $3, num_label++); } | 143 | M ORDER E { gen_order($2, $1, $3, num_label++); } |
153 | | E | 144 | | E |
154 | ; | 145 | ; |
155 | E: | 146 | E: |
156 | E ADDSUB T { gen_addsub($2, $1, $3); } | 147 | E ADDSUB T { gen_addsub($2, $1, $3); } |
157 | | T | 148 | | T |
158 | ; | 149 | ; |
159 | T: | 150 | T: |
160 | T DIVSTAR F { gen_divstar($2, $1, $3); } | 151 | T DIVSTAR F { gen_divstar($2, $1, $3); } |
161 | | F | 152 | | F |
162 | ; | 153 | ; |
163 | F: | 154 | F: |
164 | ADDSUB F {$$ = $2;//on fait remonter le type | 155 | ADDSUB F { $$ = gen_signed_expr($1, $2); } |
165 | if($1 == '+') { | 156 | | '!' F { $$ = gen_negate_expr($2); } |
166 | printf(";+F\n"); | 157 | | '(' Exp ')' { $$ = $2; } |
167 | } | 158 | | LValue { $$ = gen_value($<ident>1, scope); } |
168 | else{ | 159 | | NUM { $$ = gen_num($1, scope); } |
169 | printf(";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n"); | 160 | | CARACTERE { $$ = gen_char($1, scope); } |
170 | } | 161 | | IDENT '(' Arguments ')' { $$ = INT; } // tableau d'entiers uniquement |
171 | } | 162 | ; |
172 | | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} | ||
173 | | '(' Exp ')' {$$ = $2;} | ||
174 | | LValue { | ||
175 | if(scope == GLOBAL) { | ||
176 | $$ = glo_lookup($<ident>1); | ||
177 | printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1); | ||
178 | } | ||
179 | else { | ||
180 | $$ = loc_lookup($<ident>1); | ||
181 | printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1); | ||
182 | } | ||
183 | } | ||
184 | | NUM {$$ = INT; if(scope == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique | ||
185 | | CARACTERE {$$ = CHAR; if(scope == LOCAL) printf("push %d\n",$1);} | ||
186 | | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement | ||
187 | ; | ||
188 | LValue: | 163 | LValue: |
189 | IDENT { | 164 | IDENT { gen_read($<ident>1, scope); } |
190 | if(scope == GLOBAL) { | 165 | | IDENT '[' Exp ']' { gen_read($<ident>1, scope); } |
191 | glo_lookup($<ident>1); | 166 | ; |
192 | } | ||
193 | else { | ||
194 | loc_lookup($<ident>1); | ||
195 | } | ||
196 | } | ||
197 | | IDENT '[' Exp ']' { | ||
198 | if(scope == GLOBAL) { | ||
199 | glo_lookup($<ident>1); | ||
200 | } | ||
201 | else { | ||
202 | loc_lookup($<ident>1); | ||
203 | } | ||
204 | } | ||
205 | ; | ||
206 | Arguments: | 167 | Arguments: |
207 | ListExp | 168 | ListExp |
208 | | | 169 | | |
209 | ; | 170 | ; |
210 | ListExp: | 171 | ListExp: |
211 | ListExp ',' Exp | 172 | ListExp ',' Exp |
212 | | Exp | 173 | | Exp |
213 | ; | 174 | ; |
214 | %% | 175 | %% |
215 | 176 | ||
216 | void yyerror(char *msg){ | 177 | void yyerror(char *msg){ |