codea: mehr generisch
[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 #define KIDREG2PARM(A) if(bnode->kids[A]->param_index > -1) { bnode->kids[A]->reg = param_reg(bnode->kids[A]->param_index); }
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <assert.h>
13 #include "tree.h"
14 #include "chelper.h"
15
16 void gen_e_eno(struct treenode *bnode, char *instr)
17 {
18         KIDREG2PARM(1);
19         printf("\t%s %%%s, %%%s\n", instr, KID_REG(1), KID_REG(0));
20 }
21
22 void gen_e_imm(struct treenode *bnode, char *instr)
23 {
24         printf("\t%s $%li, %%%s\n", instr, KID_VAL(1), KID_REG(0));
25         move(KID_REG(0), BN_REG);
26 }
27
28 void gen_imm_eno(struct treenode *bnode, char *instr)
29 {
30         moveimm(KID_VAL(0), BN_REG);
31         KIDREG2PARM(1);
32         printf("\tsubq %%%s, %%%s\n", KID_REG(1), BN_REG);
33 }
34
35 %}
36
37 %start begin
38 %term O_RET=1 O_NOT=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
39
40 %%
41
42 begin: ret # 0 # printf("\n");
43 ret: O_RET(expr) # 2 # move(BN_REG, "rax"); func_footer();
44
45 expr: O_ID # 1 # if(bnode->param_index > -1) move(param_reg(bnode->param_index), BN_REG);
46 expr: imm # 1 # moveimm(BN_VAL, BN_REG);
47
48 expr: O_SUB(expr,exprno) # 1 # gen_e_eno(bnode, "subq");
49 expr: O_SUB(expr,imm) # 2 # gen_e_imm(bnode, "subq");
50 expr: O_SUB(imm,exprno) # 3 # gen_imm_eno(bnode, "subq");
51
52 expr: O_ADD(expr,exprno) # 1 # gen_e_eno(bnode, "addq");
53 expr: O_ADD(imm,expr) # 2 # gen_e_imm(bnode, "addq");
54
55 expr: O_MUL(expr,exprno) # 1 # gen_e_eno(bnode, "imulq");
56 expr: O_MUL(expr,imm) # 2 # gen_e_imm(bnode, "imulq");
57
58 exprno: O_ID # 0 # /* brauchen wir nicht 'zwischenlagern', weil nur gelesen wird */
59 exprno: expr
60
61
62 imm: O_ADD(imm,imm) # 0 # BN_VAL = KID_VAL(0)+KID_VAL(1);
63 imm: O_SUB(imm,imm) # 0 # BN_VAL = KID_VAL(0)-KID_VAL(1);
64 imm: O_MUL(imm,imm) # 0 # BN_VAL = KID_VAL(0)*KID_VAL(1);
65 imm: O_NUM # 0 #
66
67 %%
68
69 /* vim: filetype=c
70  */