arm64: codea/abgabe_aa.0
[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("\tmul %s, %s, %s\n", BN_REG, KID_REG(0), next_reg(BN_REG,0));
70                         } else {
71                                 printf("\tmul %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%s %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, "mul") == 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("\tmul %s, %s, %s\n", BN_REG, KID_REG(1), next_reg(BN_REG,0));
102                         } else {
103                                 printf("\tmul %s, %s, #%d\n", BN_REG, KID_REG(1), KID_VAL(0));
104                         }
105                 }
106         } else { /* addq */
107                 /* TODO: imm check einbauen */
108                 printf("\tadd %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(e0) { KIDREG2PARM(0); } else { moveimm(KID_VAL(0), BN_REG); }
116         if(e1) { KIDREG2PARM(1); }
117         if(strcmp(op,"e")==0 && bnode->kids[1]->op == O_NULL) {
118                 /* not */
119                 printf("\tcmp %s, #0\n", KID_REG(0));
120                 printf("\tmoveq %s, #1\n", BN_REG);
121                 printf("\tmovne %s, #0\n", BN_REG);
122         } else {
123                 if(!e1) {
124                         moveimm(KID_VAL(1), KID_REG(1));
125                 }
126                 if(strcmp(op, "e")==0) {
127                         /* eq */
128                         printf("\tcmp %s,%s\n", KID_REG(0), KID_REG(1));
129                         printf("\tmoveq %s, #1\n", BN_REG);
130                         printf("\tmovne %s, #0\n", BN_REG);
131                 } else if(strcmp(op, "l")==0 || strcmp(op, "g")==0) {
132                         /* less */
133                         printf("\tcmp %s, %s\n", KID_REG(1), KID_REG(0));
134                         printf("\tmovle %s, #0\n", BN_REG);
135                         printf("\tmovgt %s, #1\n", BN_REG);
136                 }
137         }
138 }
139
140 void gen_subspecial(struct treenode *bnode, short e)
141 {
142         /* tritt z.b. bei snafu_05.0 auf */
143         printf("\t@gen_subspecial(%i)\n", e);
144         KIDREG2ID(0);
145         KIDKIDREG2PARM(1,0);
146
147         /* TODO: Loong@ codea_snafu_03.0 */
148
149         if(e) {
150                 if(KIDKID_VAL(1,0) != 0) {
151                         printf("\tsubi %s,%s,%d\n", BN_REG, BN_REG, KIDKID_VAL(1,0));
152                 }
153         } else {
154                 printf("\tsub %s,%s,%s\n", BN_REG, BN_REG, KIDKID_REG(1,0));
155         }
156         if(e) KIDKIDREG2PARM(1,1);
157         printf("\tadd %s,%s,%s\n", BN_REG, BN_REG, KIDKID_REG(1,1));
158 }
159
160 void assign_var(struct treenode *bnode)
161 {
162         printf("\t@assign_var\n");
163         KIDREG2PARM(1);
164         if (strcmp(bnode->kids[0]->kids[0]->name, bnode->kids[1]->name) != 0) {
165                 KIDKIDREG2PARM(0,0);
166                 printf("\tmr %s,%s\n", KID_REG(1), KIDKID_REG(0,0));
167         } /*else:  x := x - 1 geht in einem befehl */
168         printf("\tsubi %s,%s,%d\n", KID_REG(1), KID_REG(1), KIDKID_VAL(0,1));
169 }
170
171 /* ... dirty */
172 static short sc[8] = {0};
173 void make_call(struct treenode *bnode)
174 {
175         int j;
176         printf("\t@params pushen\n");
177         for(j = 0; j < bnode->soffset; j++) {
178                 if(sc[j] == 1) {
179                         printf("\tlwz 20,%d(1)\n", j*4);
180                         printf("\tstw %s,%d(1)\n", param_reg(j), j*4);
181                         printf("\tmr %s,20\n", param_reg(j));
182                 } else if (sc[j] == 0) {
183                         printf("\tstw %s,%d(1)\n", param_reg(j), j*4);
184                 }
185         }
186         printf("\t@vars pushen\n");
187         for(j = bnode->soffset; j < bnode->soffset + bnode->vars; j++) {
188                 printf("\tstw %s,%d(1)\n", param_reg(j), j*4);
189         }
190
191         /* TODO: schoener machen... */
192         if(strcmp(BN_REG, "14")!=0) {
193                 printf("\t@tmp register pushen\n");
194                 printf("\tstw 14,52(1)\n");
195                 if(strcmp(BN_REG, "15")!=0) {
196                         printf("\tstw 15,56(1)\n");
197                         if(strcmp(BN_REG, "16")!=0) {
198                                 printf("\tstw 16,60(1)\n");
199                         }
200                 }
201         }
202
203         printf("\tbl %s\n", bnode->name);
204         move("r0", BN_REG);
205
206         if(strcmp(BN_REG, "14")!=0) {
207                 printf("\t@tmp register poppen\n");
208                 if(strcmp(BN_REG, "15")!=0) {
209                         if(strcmp(BN_REG, "16")!=0) {
210                                 printf("\tlwz 16,60(1)\n");
211                         }
212                         printf("\tlwz 15,56(1)\n");
213                 }
214                 printf("\tlwz 14,52(1)\n");
215         }
216
217         printf("\t@vars poppen\n");
218         for(j = bnode->soffset + bnode->vars - 1; j > bnode->soffset - 1; j--) {
219                 printf("\tlwz %s,%d(1)\n", param_reg(j), j*4);
220         }
221
222         printf("\t@params poppen\n");
223         for(j = bnode->soffset - 1; j >= 0; j--) {
224                 if(sc[j] == 0)
225                         printf("\tlwz %s,%d(1)\n", param_reg(j), j*4);
226         }
227         for(j = 0; j < bnode->soffset; j++) {
228                 if(sc[j] > 0)
229                         printf("\tlwz %s,%d(1)\n", param_reg(j), j*4);
230         }
231
232         /* clear stack control array */
233         for(j = 0; j < sizeof sc / sizeof sc[0]; j++)
234                 sc[j] = 0;
235 }
236
237 void prep_arg(struct treenode *bnode, int moveit)
238 {
239         printf("\t@args-nr-> %i (%%%s) [moveit= %i]\n", bnode->soffset, param_reg(bnode->soffset), moveit);
240         sc[bnode->soffset] = 1;
241         if(moveit) { /* expr */
242                 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)) {
243                         if(bnode->kids[1]->name != (char *) NULL && strcmp(bnode->kids[1]->name,"this")!=0) {
244                                 KIDREG2PARM(1);
245                                 printf("\tstw %s,%d(1)\n", KID_REG(1),bnode->soffset*4);
246                         } else {
247                                 printf("\tstw %s,%d(1)\n", param_reg(bnode->soffset), bnode->soffset*4);
248                                 sc[bnode->soffset] = 2;
249                         }
250                 } else {
251                         printf("\tstw %s,%d(1)\n", BN_REG, bnode->soffset*4);
252                 }
253         } else { /* just O_ID */
254                 KIDREG2PARM(0);
255                 printf("\tstw %s,%d(1)\n", KID_REG(0), bnode->soffset*4);
256         }
257 }
258
259 void field_lolbfefail(struct treenode *bnode)
260 {
261         printf("\t@ field(expr)\n");
262         KIDREG2PARM(0);
263         printf("\tldr %s, [%s, #%d]\n", BN_REG, KID_REG(0), bnode->soffset * 4);
264 }
265
266 void field_lolbfefail_imm(struct treenode *bnode)
267 {
268         printf("\t@ field(imm)\n");
269         moveimm(KID_VAL(0), BN_REG);
270         printf("\tlwz %s, [%s, #%d]\n", BN_REG, BN_REG, bnode->soffset * 4);
271 }
272
273 %}
274
275 %start begin
276 %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
277
278 %%
279
280 begin: ret # 0 #
281 begin: assign # 0 #
282 begin: ifstat # 0 #
283 begin: args # 0 #
284
285
286 assign: O_ASSIGN(expr, O_ID) # 1 # KIDREG2PARM(1); printf("\tmr %s,%s\n", KID_REG(1), BN_REG);
287 assign: O_ASSIGN(imm, O_ID) # 1 # KIDREG2PARM(1); moveimm(KID_VAL(0), KID_REG(1));
288 assign: O_ASSIGN(O_ID, O_ID) # 1 # KIDREG2PARM(1); KIDREG2PARM(0); printf("\tmr %s,%s\n", KID_REG(1), KID_REG(0));
289
290 assign: O_ASSIGN(O_SUB(O_ID,O_NUM), O_ID) # 1 # assign_var(bnode);
291
292 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));
293 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));
294
295
296 ifstat: O_IF(O_ID) # 1 # /* fuer faelle wie "if bla then" noetig */ KIDREG2PARM(0); printf("\tcmpwi %s,0\n", KID_REG(0));
297 ifstat: O_IF(O_BOOL(imm)) # 1 # printf("\tcmpwi %s,0\n", BN_REG);
298 ifstat: O_IF(expr) # 2 # /* iburg beschummeln :/ */ printf("\tcmpwi %s,0\n", BN_REG);
299 ifstat: O_IF(O_BOOL(expr)) # 1 # /* dann braucht man kein test */
300
301
302 ret: O_RET(retexpr) # 2 # printf("\t@ o_ret(expr)\n"); move(BN_REG, "r0");
303 ret: O_EXPR(expr) # 0 #
304
305 retexpr: O_ID # 1 # printf("\t@*retexpr*/\n"); if(bnode->param_index > -1) move(param_reg(bnode->param_index), BN_REG);
306 retexpr: expr
307
308
309 expr: O_ID # 0 #
310 expr: imm # 1 # moveimm(BN_VAL, BN_REG);
311 expr: O_BOOL(expr) # 0 #
312
313 expr: O_CALL(expr) # 0 # make_call(bnode);
314 expr: O_ARG(expr,expr) # 1 # prep_arg(bnode, 1);
315 expr: O_ARG(O_ID,expr) # 1 # prep_arg(bnode, 0);
316 expr: O_NOTHING # 0 #
317
318 expr: O_SUB(expr,expr)          # 2 # gen_e_eno(bnode, "sub");
319 expr: O_SUB(expr,imm)           # 1 # gen_e_imm(bnode, "sub");
320
321 expr: O_SUB(expr,O_SUB(O_ID,expr)) # 2 # gen_subspecial(bnode, 0);
322 expr: O_SUB(expr,O_SUB(imm,expr))  # 2 # gen_subspecial(bnode, 1);
323
324 expr: O_SUB(expr, O_ADD(O_ID,expr))    # 1 # gen_id_eno(bnode);
325
326
327 expr: O_ADD(expr,expr)   # 1 # gen_e_eno(bnode, "add");
328 expr: O_ADD(expr,imm)    # 1 # gen_e_imm(bnode, "add");
329 expr: O_ADD(imm,expr)    # 1 # gen_imm_eno(bnode, "add");
330
331
332 expr: O_MUL(expr,expr)   # 1 # gen_e_eno(bnode, "mul");
333 expr: O_MUL(expr,imm)    # 1 # gen_e_imm(bnode, "mul");
334 expr: O_MUL(imm,expr)    # 1 # gen_imm_eno(bnode, "mul");
335
336
337 expr: O_OR(expr,expr)          # 1 # gen_e_eno(bnode, "orr");
338 expr: O_OR(expr,imm)           # 2 # gen_e_imm(bnode, "orr");
339
340
341 expr: O_LESS(expr,expr)          # 3 # gen_eqless(bnode, "l", 1, 1, 0);
342 expr: O_LESS(expr,imm)           # 3 # gen_eqless(bnode, "l", 1, 0, 0);
343 expr: O_LESS(imm,expr)           # 3 # gen_eqless(bnode, "g", 0, 1, 0);
344
345
346 expr: O_EQ(expr,expr)          # 3 # gen_eqless(bnode, "e", 1, 1, 0);
347 expr: O_EQ(expr,imm)           # 3 # gen_eqless(bnode, "e", 1, 0, 0);
348 expr: O_EQ(imm,expr)           # 3 # gen_eqless(bnode, "e", 0, 1, 0);
349 expr: O_EQ(expr,O_NULL)        # 3 # gen_eqless(bnode, "e", 1, 0, 0);
350
351 expr: O_EQ(O_EQ(expr,O_NULL),O_NULL)  # 3 # gen_eqless(bnode, "ne", 1, 0, 1);
352 expr: O_EQ(O_EQ(O_EQ(expr,O_NULL),O_NULL),O_NULL) # 3 # gen_eqless(bnode, "e", 1, 0, 2);
353
354
355 expr: O_FIELD(expr) # 1 # field_lolbfefail(bnode);
356 expr: O_FIELD(imm)  # 1 # field_lolbfefail_imm(bnode);
357
358
359 imm: O_ADD(imm,imm)  # 0 # BN_VAL = KID_VAL(0) + KID_VAL(1);
360 imm: O_SUB(imm,imm)  # 0 # BN_VAL = KID_VAL(0) - KID_VAL(1);
361 imm: O_MUL(imm,imm)  # 0 # BN_VAL = KID_VAL(0) * KID_VAL(1);
362 imm: O_LESS(imm,imm) # 0 # BN_VAL = KID_VAL(0) < KID_VAL(1) ? 1 : 0;
363 imm: O_EQ(imm,imm)   # 0 # BN_VAL = KID_VAL(0) == KID_VAL(1) ? 1 : 0;
364 imm: O_OR(imm,imm)   # 0 # BN_VAL = KID_VAL(0) | KID_VAL(1);
365 imm: O_NUM # 0 #
366 imm: O_MONE # 0 #
367 imm: O_MTWO # 0 #
368 imm: O_MFOUR # 0 #
369 imm: O_MEIGHT # 0 #
370 imm: O_NULL # 0 #
371
372 %%
373
374 /* vim: filetype=c
375  */