codea: not inkl. optimierung
[uebersetzerbau-ss10.git] / codea / code.bfe
1 %{
2 #define BFEHAX
3
4 #define KID_REG(A) bnode->kids[A]->reg
5 #define KID_VAL(A) bnode->kids[A]->val
6 #define BN_REG bnode->reg
7 #define BN_VAL bnode->val
8
9 /* falls ein parameter auf der "leseseite" ist, soll das statt ein weiteres
10  * register verwendet werden */
11 #define KIDREG2PARM(A) if(bnode->kids[A]->param_index > -1) { bnode->kids[A]->reg = param_reg(bnode->kids[A]->param_index); }
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <assert.h>
16 #include "tree.h"
17 #include "chelper.h"
18
19 void gen_e_eno(struct treenode *bnode, char *instr)
20 {
21         KIDREG2PARM(1);
22         printf("\t%s %%%s, %%%s\n", instr, KID_REG(1), KID_REG(0));
23 }
24
25 void gen_e_imm(struct treenode *bnode, char *instr)
26 {
27         /* man kann sich ein move der konstante bei der multiplikation ersparen */
28         if(strcmp(instr, "imulq") == 0) {
29                 printf("\timulq $%li, %%%s, %%%s\n", KID_VAL(1), KID_REG(0), BN_REG);
30         } else {
31                 printf("\t%s $%li, %%%s\n", instr, KID_VAL(1), KID_REG(0));
32                 move(KID_REG(0), BN_REG);
33         }
34 }
35
36 void gen_imm_eno(struct treenode *bnode, char *instr)
37 {
38         KIDREG2PARM(1);
39         /* man kann sich ein move der konstante bei der multiplikation ersparen */
40         if(strcmp(instr, "imulq") == 0) {
41                 printf("\timulq $%li, %%%s, %%%s\n", KID_VAL(0), KID_REG(1), BN_REG);
42         } else {
43                 moveimm(KID_VAL(0), BN_REG);
44                 printf("\t%s %%%s, %%%s\n", instr, KID_REG(1), BN_REG);
45         }
46 }
47
48 void gen_eqless(struct treenode *bnode, char *op, short e0, short e1)
49 {
50         printf("\t//gen_eqless_%i%i\n", e0, e1);
51         if(e0) KIDREG2PARM(0);
52         if(e1) KIDREG2PARM(1);
53
54         if(e0 && e1) {
55                 printf("\tcmp %%%s, %%%s\n", KID_REG(1), KID_REG(0));
56         } else if(e0 && !e1) {
57                 printf("\tcmp $%li, %%%s\n", KID_VAL(1), KID_REG(0));
58         } else if(!e0 && e1) {
59                 if(strcmp("e", op) == 0) {
60                         printf("\tcmp $%li, %%%s\n", KID_VAL(0), KID_REG(1));
61                 } else {
62                         moveimm(KID_VAL(0), BN_REG);
63                         printf("\tcmp %%%s, %%%s\n", KID_REG(1), BN_REG);
64                 }
65         }
66         printf("\tset%s %%%s\n", op, reg_64to8l(BN_REG));
67         printf("\tand $1, %%%s\n", BN_REG);
68 }
69
70 %}
71
72 %start begin
73 %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
74
75 %%
76
77 begin: ret # 0 # printf("\n");
78 ret: O_RET(expr) # 2 # move(BN_REG, "rax"); func_footer();
79
80 expr: O_ID # 1 # if(bnode->param_index > -1) move(param_reg(bnode->param_index), BN_REG);
81 expr: imm # 1 # moveimm(BN_VAL, BN_REG);
82
83 expr: O_SUB(expr,exprno) # 1 # gen_e_eno(bnode, "subq");
84 expr: O_SUB(expr,imm)    # 2 # gen_e_imm(bnode, "subq");
85 expr: O_SUB(imm,exprno)  # 2 # gen_imm_eno(bnode, "subq");
86
87 expr: O_ADD(expr,exprno) # 1 # gen_e_eno(bnode, "addq");
88 expr: O_ADD(imm,expr)    # 2 # gen_e_imm(bnode, "addq");
89
90 expr: O_MUL(expr,exprno) # 1 # gen_e_eno(bnode, "imulq");
91 expr: O_MUL(expr,imm)    # 1 # gen_e_imm(bnode, "imulq");
92 expr: O_MUL(imm,exprno)  # 1 # gen_imm_eno(bnode, "imulq");
93
94 expr: O_OR(expr,exprno) # 1 # gen_e_eno(bnode, "orq");
95 expr: O_OR(expr,imm)    # 2 # gen_e_imm(bnode, "orq");
96
97 expr: O_LESS(expr,expr) # 3 # gen_eqless(bnode, "l", 1, 1);
98 expr: O_LESS(expr,imm)  # 3 # gen_eqless(bnode, "l", 1, 0);
99 expr: O_LESS(imm,expr)  # 3 # gen_eqless(bnode, "l", 0, 1);
100
101 expr: O_EQ(exprno,exprno) # 3 # gen_eqless(bnode, "e", 1, 1);
102 expr: O_EQ(exprno,imm)    # 3 # gen_eqless(bnode, "e", 1, 0);
103 expr: O_EQ(imm,exprno)    # 3 # gen_eqless(bnode, "e", 0, 1);
104 expr: O_EQ(nexpr,O_NULL)  # 0 #
105 expr: O_EQ(exprno,O_NULL) # 3 # gen_eqless(bnode, "e", 1, 0);
106
107 expr: O_FIELD(exprno) # 1 # KIDREG2PARM(0); printf("\tmovq %li(%%%s), %%%s\n", bnode->soffset * 8, KID_REG(0), BN_REG);
108
109
110 exprno: O_ID # 0 # /* brauchen wir nicht 'zwischenlagern', weil nur gelesen wird */
111 exprno: expr
112
113 nexpr: O_EQ(expr,O_NULL) # 0 #
114
115
116 imm: O_ADD(imm,imm)  # 0 # BN_VAL = KID_VAL(0) + KID_VAL(1);
117 imm: O_SUB(imm,imm)  # 0 # BN_VAL = KID_VAL(0) - KID_VAL(1);
118 imm: O_MUL(imm,imm)  # 0 # BN_VAL = KID_VAL(0) * KID_VAL(1);
119 imm: O_LESS(imm,imm) # 0 # BN_VAL = KID_VAL(0) < KID_VAL(1) ? 1 : 0;
120 imm: O_EQ(imm,imm)   # 0 # BN_VAL = KID_VAL(0) = KID_VAL(1) ? 1 : 0;
121 imm: O_NUM # 0 #
122
123 %%
124
125 /* vim: filetype=c
126  */