arm: first working example
[uebersetzerbau-ss10.git] / gesamt_arm / 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         KIDREG2PARM(0);
42         KIDREG2PARM(1);
43         printf("\t%s %s,%s,%s\n", instr, BN_REG, KID_REG(0), KID_REG(1));
44 }
45
46 void gen_id_eno(struct treenode *bnode)
47 {
48         printf("\t#gen_id_eno\n");
49         KIDKIDREG2PARM(1,0);
50         KIDKIDREG2PARM(1,1);
51         KIDREG2PARM(0);
52
53         printf("\tsub %s,%s,%s\n", BN_REG, KID_REG(0), KIDKID_REG(1,1));
54         printf("\tsub %s,%s,%s\n", BN_REG, BN_REG, KIDKID_REG(1,0));
55 }
56
57 void gen_e_imm(struct treenode *bnode, char *instr)
58 {
59         printf("\t#gen_e_imm(%s)\n", instr);
60         KIDREG2PARM(0);
61         KIDREG2ID(1);
62         /* man kann sich ein move der konstante bei der multiplikation ersparen */
63         if(strcmp(instr, "mullw") == 0) {
64                 if(KID_VAL(1) == 1 && strcmp(KID_REG(0), BN_REG) == 0) {
65                         printf("\t#multiplikation mit 1 wegoptimiert\n");
66                 } else {
67                         if(KID_VAL(1) > (65536)-1 || KID_VAL(1) < -65536) {
68                                 moveimm(KID_VAL(1), next_reg(BN_REG,0));
69                                 printf("\tmullw %s,%s,%s\n", BN_REG, KID_REG(0), next_reg(BN_REG,0));
70                         } else {
71                                 printf("\tmulli %s,%s,%d\n", BN_REG, KID_REG(0), KID_VAL(1));
72                         }
73                 }
74         } else {
75                 if(strcmp(instr, "sub") == 0 && KID_VAL(1) == 0) {
76                         printf("\t#subtraktion mit 0 wegoptimiert\n");
77                         move(KID_REG(0), BN_REG);
78                 } else {
79                         if(KID_VAL(1) > (65536)-1 || KID_VAL(1) < -65536) {
80                                 moveimm(KID_VAL(1), next_reg(BN_REG,0));
81                                 printf("\t%s %s,%s,%s\n", instr, BN_REG, KID_REG(0), next_reg(BN_REG,0));
82                         } else {
83                                 printf("\t%si %s,%s,%d\n", instr, BN_REG, KID_REG(0), KID_VAL(1));
84                         }
85                 }
86         }
87 }
88
89 void gen_imm_eno(struct treenode *bnode, char *instr)
90 {
91         printf("\t#gen_imm_eno(%s)\n", instr);
92         KIDREG2ID(0);
93         KIDREG2PARM(1);
94         /* man kann sich ein move der konstante bei der multiplikation ersparen */
95         if(strcmp(instr, "mullw") == 0) {
96                 if(KID_VAL(0) == 1 && strcmp(KID_REG(1), BN_REG) == 0) {
97                         printf("\t#multiplikation mit 1 wegoptimiert\n");
98                 } else {
99                         if(KID_VAL(0) > (65536)-1 || KID_VAL(0) < -65536) {
100                                 moveimm(KID_VAL(0), next_reg(BN_REG,0));
101                                 printf("\tmullw %s,%s,%s\n", BN_REG, KID_REG(1), next_reg(BN_REG,0));
102                         } else {
103                                 printf("\tmulli %s,%s,%d\n", BN_REG, KID_REG(1), KID_VAL(0));
104                         }
105                 }
106         } else { /* addq */
107                 /* TODO: imm check einbauen */
108                 printf("\taddi %s,%s,%d\n", BN_REG, KID_REG(1), KID_VAL(0));
109         }
110 }
111
112 void gen_eqless(struct treenode *bnode, char *op, short e0, short e1, short deep)
113 {
114         printf("\t#gen_eqless_%i%i @ %i (op: %s)\n", e0, e1, deep, op);
115 #if 0
116         if(e0) { KIDREG2PARM(0); } else { KIDREG2ID(0); }
117         if(e1) { KIDREG2PARM(1); } else { KIDREG2ID(1); }
118
119         if(e0 && e1) {
120                 if(deep) {
121                         KIDKIDREG2PARM(1,0);
122                         printf("\tcmp %d(%%%s), %%%s\n", bnode->kids[1]->soffset *8, KIDKID_REG(1,0), KID_REG(0));
123                 } else {
124                         printf("\tcmp %%%s, %%%s\n", KID_REG(1), KID_REG(0));
125                 }
126         } else if(e0 && !e1) {
127                 if (deep == 0) {
128                         printf("\tcmp $%d, %%%s\n", KID_VAL(1), KID_REG(0));
129                 } else if (deep == 1) {
130                         KIDKIDREG2PARM(0,0);
131                         printf("\tcmp $%d, %%%s\n", KID_VAL(1), KIDKID_REG(0,0));
132                 } else if (deep == 2) {
133                         KIDKIDKIDREG2PARM(0,0,0);
134                         printf("\tcmp $%d, %%%s\n", KID_VAL(1), KIDKIDKID_REG(0,0,0));
135                 }
136         } else if(!e0 && e1) {
137                         printf("\tcmp $%d, %%%s\n", KID_VAL(0), KID_REG(1));
138         }
139         printf("\tset%s %%%s\n", op, reg_64to8l(BN_REG));
140         printf("\tand $1, %%%s\n", BN_REG);
141 #else
142         if(e0) { KIDREG2PARM(0); } else { moveimm(KID_VAL(0), BN_REG); }
143         if(e1) { KIDREG2PARM(1); }
144         if(strcmp(op,"e")==0 && bnode->kids[1]->op == O_NULL) {
145                 /* not */
146                 printf("\tcntlzw %s,%s\n", KID_REG(0), KID_REG(0));
147                 printf("\tsrwi %s,%s,5\n", BN_REG, KID_REG(0));
148         } else {
149                 if(!e1) {
150                         moveimm(KID_VAL(1), KID_REG(1));
151                 }
152                 if(strcmp(op, "e")==0) {
153                         /* eq */
154                         printf("\txor %s,%s,%s\n", BN_REG, KID_REG(0), KID_REG(1));
155                         printf("\tcntlzw %s,%s\n", BN_REG, BN_REG);
156                         printf("\tsrwi %s,%s,5\n", BN_REG, BN_REG);
157                 } else if(strcmp(op, "l")==0 || strcmp(op, "g")==0) {
158                         /* less */
159                         printf("\tcmpw 7,%s,%s\n", KID_REG(1), KID_REG(0));
160                         printf("\tmfcr %s\n", BN_REG);
161                         /* EQ, GT, LT */
162                         /* um (32-29)=3 nach rechts shiften und das LSB anschauen */
163                         printf("\trlwinm %s,%s,%i,31,31\n", BN_REG, BN_REG, strcmp(op,"l")==0 ? 30 : 30);
164                 }
165         }
166         /* vergleich mit null und in CR0 speichern */
167         printf("\tcmpwi %s,0\n", BN_REG);
168 #endif
169 }
170
171 void gen_subspecial(struct treenode *bnode, short e)
172 {
173         /* tritt z.b. bei snafu_05.0 auf */
174         printf("\t#gen_subspecial(%i)\n", e);
175         KIDREG2ID(0);
176         KIDKIDREG2PARM(1,0);
177
178         /* TODO: Loong@ codea_snafu_03.0 */
179
180         if(e) {
181                 if(KIDKID_VAL(1,0) != 0) {
182                         printf("\tsubi %s,%s,%d\n", BN_REG, BN_REG, KIDKID_VAL(1,0));
183                 }
184         } else {
185                 printf("\tsub %s,%s,%s\n", BN_REG, BN_REG, KIDKID_REG(1,0));
186         }
187         if(e) KIDKIDREG2PARM(1,1);
188         printf("\tadd %s,%s,%s\n", BN_REG, BN_REG, KIDKID_REG(1,1));
189 }
190
191 void assign_var(struct treenode *bnode)
192 {
193         printf("\t#assign_var\n");
194         KIDREG2PARM(1);
195         if (strcmp(bnode->kids[0]->kids[0]->name, bnode->kids[1]->name) != 0) {
196                 KIDKIDREG2PARM(0,0);
197                 printf("\tmr %s,%s\n", KID_REG(1), KIDKID_REG(0,0));
198         } /*else:  x := x - 1 geht in einem befehl */
199         printf("\tsubi %s,%s,%d\n", KID_REG(1), KID_REG(1), KIDKID_VAL(0,1));
200 }
201
202 /* ... dirty */
203 static short sc[8] = {0};
204 void make_call(struct treenode *bnode)
205 {
206         int j;
207         printf("\t#params pushen\n");
208         for(j = 0; j < bnode->soffset; j++) {
209                 if(sc[j] == 1) {
210                         printf("\tlwz 20,%d(1)\n", j*4);
211                         printf("\tstw %s,%d(1)\n", param_reg(j), j*4);
212                         printf("\tmr %s,20\n", param_reg(j));
213                 } else if (sc[j] == 0) {
214                         printf("\tstw %s,%d(1)\n", param_reg(j), j*4);
215                 }
216         }
217         printf("\t#vars pushen\n");
218         for(j = bnode->soffset; j < bnode->soffset + bnode->vars; j++) {
219                 printf("\tstw %s,%d(1)\n", param_reg(j), j*4);
220         }
221
222         /* TODO: schoener machen... */
223         if(strcmp(BN_REG, "14")!=0) {
224                 printf("\t#tmp register pushen\n");
225                 printf("\tstw 14,52(1)\n");
226                 if(strcmp(BN_REG, "15")!=0) {
227                         printf("\tstw 15,56(1)\n");
228                         if(strcmp(BN_REG, "16")!=0) {
229                                 printf("\tstw 16,60(1)\n");
230                         }
231                 }
232         }
233
234         printf("\tbl %s\n", bnode->name);
235         move("r0", BN_REG);
236
237         if(strcmp(BN_REG, "14")!=0) {
238                 printf("\t#tmp register poppen\n");
239                 if(strcmp(BN_REG, "15")!=0) {
240                         if(strcmp(BN_REG, "16")!=0) {
241                                 printf("\tlwz 16,60(1)\n");
242                         }
243                         printf("\tlwz 15,56(1)\n");
244                 }
245                 printf("\tlwz 14,52(1)\n");
246         }
247
248         printf("\t#vars poppen\n");
249         for(j = bnode->soffset + bnode->vars - 1; j > bnode->soffset - 1; j--) {
250                 printf("\tlwz %s,%d(1)\n", param_reg(j), j*4);
251         }
252
253         printf("\t#params poppen\n");
254         for(j = bnode->soffset - 1; j >= 0; j--) {
255                 if(sc[j] == 0)
256                         printf("\tlwz %s,%d(1)\n", param_reg(j), j*4);
257         }
258         for(j = 0; j < bnode->soffset; j++) {
259                 if(sc[j] > 0)
260                         printf("\tlwz %s,%d(1)\n", param_reg(j), j*4);
261         }
262
263         /* clear stack control array */
264         for(j = 0; j < sizeof sc / sizeof sc[0]; j++)
265                 sc[j] = 0;
266 }
267
268 void prep_arg(struct treenode *bnode, int moveit)
269 {
270         printf("\t#args-nr-> %i (%%%s) [moveit= %i]\n", bnode->soffset, param_reg(bnode->soffset), moveit);
271         sc[bnode->soffset] = 1;
272         if(moveit) { /* expr */
273                 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)) {
274                         if(bnode->kids[1]->name != (char *) NULL && strcmp(bnode->kids[1]->name,"this")!=0) {
275                                 KIDREG2PARM(1);
276                                 printf("\tstw %s,%d(1)\n", KID_REG(1),bnode->soffset*4);
277                         } else {
278                                 printf("\tstw %s,%d(1)\n", param_reg(bnode->soffset), bnode->soffset*4);
279                                 sc[bnode->soffset] = 2;
280                         }
281                 } else {
282                         printf("\tstw %s,%d(1)\n", BN_REG, bnode->soffset*4);
283                 }
284         } else { /* just O_ID */
285                 KIDREG2PARM(0);
286                 printf("\tstw %s,%d(1)\n", KID_REG(0), bnode->soffset*4);
287         }
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); moveimm(KID_VAL(0), KID_REG(1));
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(O_BOOL(imm)) # 1 # printf("\tcmpwi %s,0\n", BN_REG);
315 ifstat: O_IF(expr) # 2 # /* iburg beschummeln :/ */ printf("\tcmpwi %s,0\n", BN_REG);
316 ifstat: O_IF(O_BOOL(expr)) # 1 # /* dann braucht man kein test */
317
318
319 ret: O_RET(retexpr) # 2 # printf("\t@ o_ret(expr)\n"); move(BN_REG, "r0");
320 ret: O_EXPR(expr) # 0 #
321
322 retexpr: O_ID # 1 # printf("\t/*retexpr*/\n"); if(bnode->param_index > -1) move(param_reg(bnode->param_index), BN_REG);
323 retexpr: expr
324
325
326 expr: O_ID # 0 #
327 expr: imm # 1 # moveimm(BN_VAL, BN_REG);
328 expr: O_BOOL(expr) # 0 #
329
330 expr: O_CALL(expr) # 0 # make_call(bnode);
331 expr: O_ARG(expr,expr) # 1 # prep_arg(bnode, 1);
332 expr: O_ARG(O_ID,expr) # 1 # prep_arg(bnode, 0);
333 expr: O_NOTHING # 0 #
334
335 expr: O_SUB(expr,expr)          # 2 # gen_e_eno(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, "add");
345 expr: O_ADD(expr,imm)    # 1 # gen_e_imm(bnode, "add");
346 expr: O_ADD(imm,expr)    # 1 # gen_imm_eno(bnode, "add");
347
348
349 expr: O_MUL(expr,expr)   # 1 # gen_e_eno(bnode, "mullw");
350 expr: O_MUL(expr,imm)    # 1 # gen_e_imm(bnode, "mullw");
351 expr: O_MUL(imm,expr)    # 1 # gen_imm_eno(bnode, "mullw");
352
353
354 expr: O_OR(expr,expr)          # 1 # gen_e_eno(bnode, "or");
355 expr: O_OR(expr,imm)           # 2 # gen_e_imm(bnode, "or");
356
357
358 expr: O_LESS(expr,expr)          # 3 # gen_eqless(bnode, "l", 1, 1, 0);
359 expr: O_LESS(expr,imm)           # 3 # gen_eqless(bnode, "l", 1, 0, 0);
360 expr: O_LESS(imm,expr)           # 3 # gen_eqless(bnode, "g", 0, 1, 0);
361
362
363 expr: O_EQ(expr,expr)          # 3 # gen_eqless(bnode, "e", 1, 1, 0);
364 expr: O_EQ(expr,imm)           # 3 # gen_eqless(bnode, "e", 1, 0, 0);
365 expr: O_EQ(imm,expr)           # 3 # gen_eqless(bnode, "e", 0, 1, 0);
366 expr: O_EQ(expr,O_NULL)        # 3 # gen_eqless(bnode, "e", 1, 0, 0);
367
368 expr: O_EQ(O_EQ(expr,O_NULL),O_NULL)  # 3 # gen_eqless(bnode, "ne", 1, 0, 1);
369 expr: O_EQ(O_EQ(O_EQ(expr,O_NULL),O_NULL),O_NULL) # 3 # gen_eqless(bnode, "e", 1, 0, 2);
370
371
372 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));
373 expr: O_FIELD(imm)  # 1 # printf("\t/* field(imm)*/\n"); moveimm(KID_VAL(0), BN_REG); printf("\tlwz %s,%d(%s)\n", BN_REG, bnode->soffset * 4, BN_REG);
374
375
376 imm: O_ADD(imm,imm)  # 0 # BN_VAL = KID_VAL(0) + KID_VAL(1);
377 imm: O_SUB(imm,imm)  # 0 # BN_VAL = KID_VAL(0) - KID_VAL(1);
378 imm: O_MUL(imm,imm)  # 0 # BN_VAL = KID_VAL(0) * KID_VAL(1);
379 imm: O_LESS(imm,imm) # 0 # BN_VAL = KID_VAL(0) < KID_VAL(1) ? 1 : 0;
380 imm: O_EQ(imm,imm)   # 0 # BN_VAL = KID_VAL(0) == KID_VAL(1) ? 1 : 0;
381 imm: O_OR(imm,imm)   # 0 # BN_VAL = KID_VAL(0) | KID_VAL(1);
382 imm: O_NUM # 0 #
383 imm: O_MONE # 0 #
384 imm: O_MTWO # 0 #
385 imm: O_MFOUR # 0 #
386 imm: O_MEIGHT # 0 #
387 imm: O_NULL # 0 #
388
389 %%
390
391 /* vim: filetype=c
392  */