ppc: alle codeb testfaelle klappen nun auch
[uebersetzerbau-ss10.git] / gesamt_ppc / code.bfe
1 %{
2 #define BFEHAX
3
4 /* macros zum registerzugriff bei kinder */
5 #define KID_REG(A) bnode->kids[A]->reg
6 #define KIDKID_REG(A,B) bnode->kids[A]->kids[B]->reg
7 #define KIDKIDKID_REG(A,B,C) bnode->kids[A]->kids[B]->kids[C]->reg
8
9 /* macros zum wertezugriff bei kindern */
10 #define KID_VAL(A) bnode->kids[A]->val
11 #define KIDKID_VAL(A,B) bnode->kids[A]->kids[B]->val
12 #define KIDKIDKID_VAL(A,B,C) bnode->kids[A]->kids[B]->kids[C]->val
13
14 #define KID_PARM(A) bnode->kids[A]->param_index
15 #define KIDKID_PARM(A,B) bnode->kids[A]->kids[B]->param_index
16 #define KIDKIDKID_PARM(A,B,C) bnode->kids[A]->kids[B]->kids[C]->param_index
17
18 /* macros zum zugriff des aktuellen knotens */
19 #define BN_REG bnode->reg
20 #define BN_VAL bnode->val
21
22 /* wenn sich ein parameter auf der "leseseite" (also links bei at&t syntax)
23  * befindet, dann soll dieses register verwendet werden */
24 #define KIDREG2PARM(A) if(bnode->kids[A]->param_index > -1) { bnode->kids[A]->reg = param_reg(bnode->kids[A]->param_index); }
25 #define KIDKIDREG2PARM(A,B) if(bnode->kids[A]->kids[B]->param_index > -1) { bnode->kids[A]->kids[B]->reg = param_reg(bnode->kids[A]->kids[B]->param_index); }
26 #define KIDKIDKIDREG2PARM(A,B,C) if(bnode->kids[A]->kids[B]->kids[C]->param_index > -1) { bnode->kids[A]->kids[B]->kids[C]->reg = param_reg(bnode->kids[A]->kids[B]->kids[C]->param_index); }
27
28 /* wenn sich ein parameter auf der "schreibeseite" befindet (also rechts bei
29  * at&t syntax), dann muss es vorher in ein temporaeres register gemovt werden */
30 #define KIDREG2ID(A) if(bnode->kids[A]->op == O_ID && bnode->kids[A]->param_index > -1) move(param_reg(bnode->kids[A]->param_index), bnode->kids[A]->reg);
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <assert.h>
35 #include "tree.h"
36 #include "chelper.h"
37
38 void gen_e_eno(struct treenode *bnode, char *instr)
39 {
40         printf("\t#gen_e_eno(%s)\n", instr);
41 #if 0
42         KIDREG2ID(0);
43         KIDREG2PARM(1);
44         printf("\t%s %s, %s\n", instr, KID_REG(1), KID_REG(0));
45 #else
46         KIDREG2PARM(0);
47         KIDREG2PARM(1);
48         printf("\t%s %s,%s,%s\n", instr, BN_REG, KID_REG(0), KID_REG(1));
49 #endif
50 }
51
52 void gen_id_eno(struct treenode *bnode)
53 {
54         printf("\t#gen_id_eno\n");
55         KIDKIDREG2PARM(1,0);
56         KIDKIDREG2PARM(1,1);
57         KIDREG2PARM(0);
58         move(KID_REG(0), BN_REG);
59         printf("\tsub %%%s, %%%s\n", KIDKID_REG(1,1), BN_REG);
60         printf("\tsub %%%s, %%%s\n", KIDKID_REG(1,0), BN_REG);
61 }
62
63 void gen_e_field(struct treenode *bnode, char *instr)
64 {
65         printf("\t#gen_e_field(%s)\n", instr);
66         KIDREG2ID(0);
67         KIDKIDREG2PARM(1,0);
68         printf("\t%s %d(%%%s), %%%s\n", instr, bnode->kids[1]->soffset * 8, KIDKID_REG(1,0), KID_REG(0));
69 }
70
71 void gen_field_imm(struct treenode *bnode)
72 {
73         printf("\t#gen_field_imm\n");
74         KIDKIDREG2PARM(0,0);
75         KIDREG2ID(1);
76         printf("\tmullw $%d, %d(%%%s), %%%s\n", KID_VAL(1), bnode->kids[0]->soffset * 8, KIDKID_REG(0, 0), BN_REG);
77 }
78
79 void gen_e_imm(struct treenode *bnode, char *instr)
80 {
81         printf("\t#gen_e_imm(%s)\n", instr);
82         KIDREG2PARM(0);
83         KIDREG2ID(1);
84         /* man kann sich ein move der konstante bei der multiplikation ersparen */
85         if(strcmp(instr, "mullw") == 0) {
86                 if(KID_VAL(1) == 1 && strcmp(KID_REG(0), BN_REG) == 0) {
87                         printf("\t#multiplikation mit 1 wegoptimiert\n");
88                 } else {
89                         printf("\tmulli %s,%s,%d\n", BN_REG, KID_REG(0), KID_VAL(1));
90                 }
91         } else {
92                 if(strcmp(instr, "sub") == 0 && KID_VAL(1) == 0) {
93                         printf("\t#subtraktion mit 0 wegoptimiert\n");
94                         move(KID_REG(0), BN_REG);
95                 } else {
96 #if 0
97                         move(KID_REG(0), BN_REG);
98                         printf("\t%s $%d, %%%s\n", instr, KID_VAL(1), BN_REG);
99 #else
100                         printf("\t%si %s,%s,%d\n", instr, BN_REG, KID_REG(0), KID_VAL(1));
101 #endif
102                 }
103         }
104 }
105
106 void gen_imm_field(struct treenode *bnode)
107 {
108         printf("\t#gen_imm_field\n");
109         KIDREG2ID(0);
110         KIDKIDREG2PARM(1, 0);
111
112         moveimm(KID_VAL(0), BN_REG);
113         printf("\tsubf %d(%%%s), %%%s\n", bnode->kids[1]->soffset * 8, KIDKID_REG(1, 0), BN_REG);
114 }
115
116 void gen_imm_eno(struct treenode *bnode, char *instr)
117 {
118         printf("\t#gen_imm_eno(%s)\n", instr);
119         KIDREG2ID(0);
120         KIDREG2PARM(1);
121         /* man kann sich ein move der konstante bei der multiplikation ersparen */
122         if(strcmp(instr, "mullw") == 0) {
123                 if(KID_VAL(0) == 1 && strcmp(KID_REG(1), BN_REG) == 0) {
124                         printf("\t#multiplikation mit 1 wegoptimiert\n");
125                 } else {
126                         printf("\tmulli %s,%s,%d\n", BN_REG, KID_REG(1), KID_VAL(0));
127                 }
128         } else { /* addq */
129                 printf("\taddq $%d, %%%s\n", KID_VAL(0), BN_REG);
130         }
131 }
132
133 void gen_eqless(struct treenode *bnode, char *op, short e0, short e1, short deep)
134 {
135         printf("\t#gen_eqless_%i%i @ %i (op: %s)\n", e0, e1, deep, op);
136 #if 0
137         if(e0) { KIDREG2PARM(0); } else { KIDREG2ID(0); }
138         if(e1) { KIDREG2PARM(1); } else { KIDREG2ID(1); }
139
140         if(e0 && e1) {
141                 if(deep) {
142                         KIDKIDREG2PARM(1,0);
143                         printf("\tcmp %d(%%%s), %%%s\n", bnode->kids[1]->soffset *8, KIDKID_REG(1,0), KID_REG(0));
144                 } else {
145                         printf("\tcmp %%%s, %%%s\n", KID_REG(1), KID_REG(0));
146                 }
147         } else if(e0 && !e1) {
148                 if (deep == 0) {
149                         printf("\tcmp $%d, %%%s\n", KID_VAL(1), KID_REG(0));
150                 } else if (deep == 1) {
151                         KIDKIDREG2PARM(0,0);
152                         printf("\tcmp $%d, %%%s\n", KID_VAL(1), KIDKID_REG(0,0));
153                 } else if (deep == 2) {
154                         KIDKIDKIDREG2PARM(0,0,0);
155                         printf("\tcmp $%d, %%%s\n", KID_VAL(1), KIDKIDKID_REG(0,0,0));
156                 }
157         } else if(!e0 && e1) {
158                         printf("\tcmp $%d, %%%s\n", KID_VAL(0), KID_REG(1));
159         }
160         printf("\tset%s %%%s\n", op, reg_64to8l(BN_REG));
161         printf("\tand $1, %%%s\n", BN_REG);
162 #else
163         if(e0) { KIDREG2PARM(0); } else { moveimm(KID_VAL(0), BN_REG); }
164         if(e1) { KIDREG2PARM(1); } else { if(KID_VAL(1) != 0) moveimm(KID_VAL(1), KID_REG(1)); }
165         if(strcmp(op,"e")==0 && KID_VAL(1) == 0 && KID_VAL(0) == 0) {
166                 /* not */
167                 printf("\tcntlzw %s,%s\n", KID_REG(0), KID_REG(0));
168                 printf("\tsrwi %s,%s,5\n", BN_REG, KID_REG(0));
169         } else if(strcmp(op, "e")==0) {
170                 /* eq */
171                 printf("\txor %s,%s,%s\n", BN_REG, KID_REG(0), KID_REG(1));
172                 printf("\tcntlzw %s,%s\n", BN_REG, BN_REG);
173                 printf("\tsrwi %s,%s,5\n", BN_REG, BN_REG);
174         } else if(strcmp(op, "l")==0 || strcmp(op, "g")==0) {
175                 /* less */
176                 printf("\tcmpw 7,%s,%s\n", KID_REG(1), KID_REG(0));
177                 printf("\tmfcr %s\n", BN_REG);
178                 /* EQ, GT, LT */
179                 /* um (32-29)=3 nach rechts shiften und das LSB anschauen */
180                 printf("\trlwinm %s,%s,%i,31,31\n", BN_REG, BN_REG, strcmp(op,"l")==0 ? 30 : 30);
181         }
182         /* vergleich mit null und in CR0 speichern */
183         printf("\tcmpwi %s,0\n", BN_REG);
184 #endif
185 }
186
187 void gen_subspecial(struct treenode *bnode, short e)
188 {
189         /* tritt z.b. bei snafu_05.0 auf */
190         printf("\t#gen_subspecial(%i)\n", e);
191         KIDREG2ID(0);
192         KIDKIDREG2PARM(1,0);
193
194         if(e) {
195                 if(KIDKID_VAL(1,0) != 0) {
196                         printf("\tsubf $%d, %%%s\n", KIDKID_VAL(1,0), BN_REG);
197                 }
198         } else {
199                 printf("\tsubf %%%s, %%%s\n", KIDKID_REG(1,0), BN_REG);
200         }
201         if(e) KIDKIDREG2PARM(1,1);
202         printf("\tadd %s,%s,%s\n", BN_REG, BN_REG, KIDKID_REG(1,1));
203 }
204
205 void assign_var(struct treenode *bnode)
206 {
207         printf("\t#assign_var\n");
208         KIDREG2PARM(1);
209         if (strcmp(bnode->kids[0]->kids[0]->name, bnode->kids[1]->name) != 0) {
210                 KIDKIDREG2PARM(0,0);
211                 printf("\tmov %%%s, %%%s\n", KIDKID_REG(0,0), KID_REG(1));
212         } /*else:  x := x - 1 geht in einem befehl */
213         printf("\tsubi %s,%s,%d\n", KID_REG(1), KID_REG(1), KIDKID_VAL(0,1));
214 }
215
216 void make_call(struct treenode *bnode)
217 {
218         int j;
219         printf("\t#params pushen\n");
220         for(j = 0; j < bnode->soffset; j++) {
221                 printf("\txchg %%%s, %d(%%rsp)\n", param_reg(j), j*8);
222         }
223         printf("\t#vars pushen\n");
224         for(j = VARBEGIN; j > VARBEGIN - bnode->vars; j--) {
225                 printf("\tpushq %%%s\n", param_reg(j));
226         }
227
228         /* TODO: schoener machen... */
229         if(strcmp(BN_REG, "rax")!=0) {
230                 printf("\t#tmp register pushen\n");
231                 printf("\tpushq %%rax\n");
232                 if(strcmp(BN_REG, "r10")!=0) {
233                         printf("\tpushq %%r10\n");
234                 }
235         }
236         printf("\tcall %s\n", bnode->name);
237         if(strcmp(BN_REG, "rax")!=0) {
238                 move("rax", BN_REG);
239                 if(strcmp(BN_REG, "r10")!=0) {
240                         printf("\tpopq %%r10\n");
241                 }
242                 printf("\tpopq %%rax\n");
243         }
244
245         /* vars poppen */
246         for(j = VARBEGIN+1 - bnode->vars; j < VARBEGIN+1; j++) {
247                 printf("\tpopq %%%s\n", param_reg(j));
248         }
249
250         /* params poppen */
251         for(j = 0; j < bnode->soffset; j++) {
252                 printf("\tpopq %%%s\n", param_reg(j));
253         }
254 }
255
256 void prep_arg(struct treenode *bnode, int moveit)
257 {
258         printf("\t#args-nr-> %i (%%%s) [moveit= %i]\n", bnode->soffset, param_reg(bnode->soffset), moveit);
259         if(moveit) { /* expr */
260                 if((BN_REG == (char *) NULL) || (bnode->kids[1] != TREENULL && bnode->kids[1]->op == O_ID && bnode->kids[1]->kids[0] == TREENULL && bnode->kids[1]->kids[1] == TREENULL)) {
261                         if(bnode->kids[1]->name != (char *) NULL && strcmp(bnode->kids[1]->name,"this")!=0) {
262                                 KIDREG2PARM(1);
263                                 printf("\tpushq %%%s\n", KID_REG(1));
264                         } else {
265                                 printf("\tpushq %%%s\n", param_reg(bnode->soffset));
266                         }
267                 } else {
268                         printf("\tpushq %%%s\n", BN_REG);
269                 }
270         } else { /* just O_ID */
271                 KIDREG2PARM(0);
272                 printf("\tpushq %%%s\n", KID_REG(0));
273         }
274 }
275
276 void gen_sub_field(struct treenode *bnode)
277 {
278         /* siehe intelli_03.0 @ gesamt */
279         printf("\t#gen_sub_field\n");
280         KIDKIDREG2PARM(1,0);
281         if(!(strcmp(bnode->kids[0]->kids[0]->kids[0]->name, bnode->kids[1]->kids[0]->name) == 0 &&
282                         bnode->kids[0]->kids[0]->soffset == bnode->kids[1]->soffset)) {
283                 KIDKIDKIDREG2PARM(0,0,0);
284                 printf("\tmov %s,%d(%s)\n", BN_REG, bnode->kids[0]->kids[0]->soffset * 8, KIDKIDKID_REG(0,0,0));
285                 printf("\tmov %s,%d(%s)\n", BN_REG, bnode->kids[1]->soffset * 8, KIDKID_REG(1,0));
286         }
287         printf("\tsubi $%d, %d(%%%s)\n", bnode->kids[0]->kids[1]->val, bnode->kids[1]->soffset * 8, KIDKID_REG(1,0));
288 }
289
290 %}
291
292 %start begin
293 %term O_RET=1 O_NULL=2 O_SUB=3 O_MUL=4 O_OR=5 O_LESS=6 O_EQ=7 O_ID=8 O_ADD=9 O_NUM=10 O_FIELD=11 O_MTWO=12 O_MFOUR=13 O_MEIGHT=14 O_MONE=15 O_ASSIGN=16 O_IF=17 O_BOOL=18 O_CALL=19 O_ARG=20 O_NOTHING=21 O_EXPR=22
294
295 %%
296
297 begin: ret # 0 #
298 begin: assign # 0 #
299 begin: ifstat # 0 #
300 begin: args # 0 #
301
302
303 assign: O_ASSIGN(expr, O_ID) # 1 # KIDREG2PARM(1); printf("\tmr %s,%s\n", KID_REG(1), BN_REG);
304 assign: O_ASSIGN(imm, O_ID) # 1 # KIDREG2PARM(1); printf("\tli %s,%d\n", KID_REG(1), KID_VAL(0));
305 assign: O_ASSIGN(O_ID, O_ID) # 1 # KIDREG2PARM(1); KIDREG2PARM(0); printf("\tmr %s,%s\n", KID_REG(1), KID_REG(0));
306
307 assign: O_ASSIGN(O_SUB(O_ID,O_NUM), O_ID) # 1 # assign_var(bnode);
308
309 assign: O_ASSIGN(expr, O_FIELD(expr)) # 1 # KIDKIDREG2PARM(1,0); printf("\tstw %s,%d(%s)\n", BN_REG, bnode->kids[1]->soffset * 4, KIDKID_REG(1,0));
310 assign: O_ASSIGN(O_ID, O_FIELD(expr)) # 1 # KIDREG2PARM(0); KIDKIDREG2PARM(1,0); printf("\tstw %s,%d(%s)\n", KID_REG(0), bnode->kids[1]->soffset * 4, KIDKID_REG(1,0));
311
312
313 ifstat: O_IF(O_ID) # 1 # /* fuer faelle wie "if bla then" noetig */ KIDREG2PARM(0); printf("\tcmpwi %s,0\n", KID_REG(0));
314 ifstat: O_IF(expr) # 2 # /* iburg beschummeln :/ */ printf("\ttest $-1, %%rax\n");
315 ifstat: O_IF(O_BOOL(expr)) # 1 # /* dann braucht man kein test */
316
317
318 ret: O_RET(retexpr) # 2 # printf("\t/*o_ret(expr)*/\n"); move(BN_REG, "3");
319 ret: O_EXPR(expr) # 0 #
320
321 retexpr: O_ID # 1 # printf("\t/*retexpr*/\n"); if(bnode->param_index > -1) move(param_reg(bnode->param_index), BN_REG);
322 retexpr: expr
323
324
325 expr: O_ID # 0 #
326 expr: imm # 1 # moveimm(BN_VAL, BN_REG);
327 expr: O_BOOL(expr) # 0 #
328
329 expr: O_CALL(expr) # 0 # make_call(bnode);
330 expr: O_ARG(expr,expr) # 1 # prep_arg(bnode, 1);
331 expr: O_ARG(O_ID,expr) # 1 # prep_arg(bnode, 0);
332 expr: O_NOTHING # 0 #
333
334 expr: O_SUB(expr,expr)          # 2 # gen_e_eno(bnode, "sub");
335 expr: O_SUB(expr,O_FIELD(expr)) # 2 # gen_e_field(bnode, "sub");
336 expr: O_SUB(expr,imm)           # 1 # gen_e_imm(bnode, "sub");
337
338 expr: O_SUB(expr,O_SUB(O_ID,expr)) # 2 # gen_subspecial(bnode, 0);
339 expr: O_SUB(expr,O_SUB(imm,expr))  # 2 # gen_subspecial(bnode, 1);
340
341 expr: O_SUB(expr, O_ADD(O_ID,expr))    # 1 # gen_id_eno(bnode);
342
343
344 expr: O_ADD(expr,expr)   # 1 # gen_e_eno(bnode, "addq");
345 expr: O_ADD(expr,imm)    # 2 # gen_e_imm(bnode, "addq");
346 expr: O_ADD(imm,expr)    # 1 # gen_imm_eno(bnode, "addq");
347
348 expr: O_ADD(expr,O_FIELD(expr)) # 2 # gen_e_field(bnode, "addq");
349
350
351 expr: O_MUL(expr,expr)   # 1 # gen_e_eno(bnode, "mullw");
352 expr: O_MUL(expr,imm)    # 1 # gen_e_imm(bnode, "mullw");
353 expr: O_MUL(imm,expr)    # 1 # gen_imm_eno(bnode, "mullw");
354
355 expr: O_MUL(expr,O_FIELD(expr)) # 1 # gen_e_field(bnode, "mullw");
356
357 expr: O_OR(expr,expr)          # 1 # gen_e_eno(bnode, "or");
358 expr: O_OR(expr,imm)           # 2 # gen_e_imm(bnode, "or");
359 expr: O_OR(expr,O_FIELD(expr)) # 2 # gen_e_field(bnode, "or");
360
361
362 expr: O_LESS(expr,expr)          # 3 # gen_eqless(bnode, "l", 1, 1, 0);
363 expr: O_LESS(expr,O_FIELD(expr)) # 3 # gen_eqless(bnode, "l", 1, 1, 1);
364 expr: O_LESS(expr,imm)           # 3 # gen_eqless(bnode, "l", 1, 0, 0);
365 expr: O_LESS(imm,expr)           # 3 # gen_eqless(bnode, "g", 0, 1, 0);
366
367
368 expr: O_EQ(expr,expr)          # 3 # gen_eqless(bnode, "e", 1, 1, 0);
369 expr: O_EQ(expr,O_FIELD(expr)) # 3 # gen_eqless(bnode, "e", 1, 1, 1);
370 expr: O_EQ(expr,imm)           # 3 # gen_eqless(bnode, "e", 1, 0, 0);
371 expr: O_EQ(imm,expr)           # 3 # gen_eqless(bnode, "e", 0, 1, 0);
372 expr: O_EQ(expr,O_NULL)        # 3 # gen_eqless(bnode, "e", 1, 0, 0);
373
374 expr: O_EQ(O_EQ(expr,O_NULL),O_NULL)  # 3 # gen_eqless(bnode, "ne", 1, 0, 1);
375 expr: O_EQ(O_EQ(O_EQ(expr,O_NULL),O_NULL),O_NULL) # 3 # gen_eqless(bnode, "e", 1, 0, 2);
376
377
378 expr: O_FIELD(expr) # 1 # printf("\t/* field(expr)*/\n"); KIDREG2PARM(0); printf("\tlwz %s, %d(%s)\n", BN_REG, bnode->soffset * 4, KID_REG(0));
379 expr: O_FIELD(imm)  # 1 # printf("\t/* field(imm)*/\n"); printf("\tmovq %d, %%%s\n", KID_VAL(0) + (bnode->soffset * 8), BN_REG);
380
381
382 imm: O_ADD(imm,imm)  # 0 # BN_VAL = KID_VAL(0) + KID_VAL(1);
383 imm: O_SUB(imm,imm)  # 0 # BN_VAL = KID_VAL(0) - KID_VAL(1);
384 imm: O_MUL(imm,imm)  # 0 # BN_VAL = KID_VAL(0) * KID_VAL(1);
385 imm: O_LESS(imm,imm) # 0 # BN_VAL = KID_VAL(0) < KID_VAL(1) ? 1 : 0;
386 imm: O_EQ(imm,imm)   # 0 # BN_VAL = KID_VAL(0) == KID_VAL(1) ? 1 : 0;
387 imm: O_OR(imm,imm)   # 0 # BN_VAL = KID_VAL(0) | KID_VAL(1);
388 imm: O_NUM # 0 #
389 imm: O_MONE # 0 #
390 imm: O_MTWO # 0 #
391 imm: O_MFOUR # 0 #
392 imm: O_MEIGHT # 0 #
393 imm: O_NULL # 0 #
394
395 %%
396
397 /* vim: filetype=c
398  */