doc: paulchen beispiele {code{a,b},gesamt} entpackt (jedes mal entpacken nervt langsa...
[uebersetzerbau-ss10.git] / aus_sammelwut / paulchen / ublu / ss08 / abgabe / gesamt / code.bfe
1 %{
2 /* vim: filetype=c
3  */
4 #define CODE
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <assert.h>
9
10 #include "tree.h"
11 #include "code_gen.h"
12
13 /* TODO (-a)+b */
14 /* TODO imul -> imulq, add -> addq */
15 /* TODO bugs - hopefully fixed:
16  * func f(a,b,c,d) return ((a+1)+(b+1))+((c+1)+(d+1)); end;
17  * func f(a,b,c,d) var b:=a; var c:=b; var d:=c; return a+b+c+d; end;
18  * mul, add etc. with direct memory access
19  */
20 /* TODO immediate values in assignments */
21 /* TODO assignment with immediate value/variable on RHS */
22 /* TODO immediate values in boolean expressions */
23 /* TODO optimize boolean expressions */
24 /* FIXME if_then label not inserted */
25 %}
26
27 %start stat
28 %term OP_Not=1 OP_Negation=2 OP_Addition=3 OP_Multiplication=4 OP_Disjunction=5 OP_Greater=6 OP_Equal=7 OP_ID=8 OP_Number=9 OP_Field=10 OP_Return=11 OP_Zero=12 OP_One=13 OP_Exprs=14 OP_Call=15 OP_Assign=16 OP_If=17 OP_Stats=18 OP_Empty=19 OP_Ifstats=20 OP_While=21 OP_CallNoParam=22 OP_Arg=23
29
30 %%
31
32 stat:   ret                                     # 0 #
33 stat:   assign                                  # 0 #
34 stat:   bexpr                                   # 0 #
35 stat:   expr                                    # 0 #
36
37 assign: OP_Assign(OP_ID, expr)                  # 1 # if(bnode->kids[0]->param_index!=-1 && !call) { printf("\tmovq %%%s, %%%s /* x */\n", bnode->reg, get_param_reg(bnode->kids[0]->param_index)); } else if(bnode->kids[0]->param_index!=-1 && call) { printf("\tmovq %%%s, %i(%%rsp) /* y */\n", bnode->reg, 8*(variables-bnode->kids[0]->param_index)); } else { printf("\tmovq %%%s, %i(%%rsp) /* z */\n", bnode->reg, bnode->kids[0]->value); }
38 assign: OP_Assign(OP_Field(expr,OP_ID), expr)   # 1 # printf("\tmovq %%%s, %li(%%%s)\n", bnode->kids[1]->reg, 8*bnode->kids[0]->value, bnode->kids[0]->reg); 
39
40 ret:    OP_Return(expr)                         # 1 # move(bnode->reg, "rax"); ret(); 
41
42 expr:   OP_ID                                   # 1 # if(bnode->param_index!=-1 && !call) { move(get_param_reg(bnode->param_index), bnode->reg); } else if(bnode->param_index!=-1 && call) { printf("\tmov %i(%%rsp), %%%s\n", 8*(variables-bnode->param_index), bnode->reg); } else { printf("\tmovq %i(%%rsp), %%%s\n", bnode->value, bnode->reg); }
43 expr:   imm                                     # 1 # printf("\tmovq $%li, %%%s\n", bnode->value, bnode->reg);
44 expr:   call                                    # 0 #
45 expr:   OP_Negation(expr)                       # 1 # printf("\tnegq %%%s\n", bnode->reg);
46 expr:   OP_Addition(expr,expr)                  # 1 # printf("\taddq %%%s, %%%s\n", bnode->kids[1]->reg, bnode->kids[0]->reg);
47 expr:   OP_Addition(imm,expr)                   # 1 # printf("\taddq $%li, %%%s\n", bnode->kids[0]->value, bnode->kids[1]->reg); move(bnode->kids[1]->reg, bnode->reg);
48 expr:   OP_Addition(expr,imm)                   # 1 # if(bnode->kids[0]->op==OP_ID) { move(bnode->kids[0]->reg, bnode->reg); printf("\taddq $%li, %%%s /* x2 */\n", bnode->kids[1]->value, bnode->reg); } else { printf("\taddq $%li, %%%s /* y2 */\n", bnode->kids[1]->value, bnode->kids[0]->reg); }
49 expr:   OP_Multiplication(expr,expr)            # 1 # printf("\timulq %%%s, %%%s\n", bnode->kids[1]->reg, bnode->kids[0]->reg);
50 expr:   OP_Multiplication(imm,expr)             # 1 # printf("\timulq $%li, %%%s\n", bnode->kids[0]->value, bnode->kids[1]->reg); move(bnode->kids[1]->reg, bnode->reg);
51 expr:   OP_Multiplication(expr,imm)             # 1 # if(bnode->kids[0]->op==OP_ID) { move(bnode->kids[0]->reg, bnode->reg); printf("\timulq $%li, %%%s\n", bnode->kids[1]->value, bnode->reg); } else { printf("\timulq $%li, %%%s\n", bnode->kids[1]->value, bnode->kids[0]->reg); }
52 expr:   OP_Field(expr,OP_ID)                    # 2 # printf("\tmovq %li(%%%s), %%%s\n", 8*bnode->value, bnode->kids[0]->reg, bnode->reg);
53 expr:   OP_Field(imm,OP_ID)                     # 1 # printf("\tmovq %li, %%%s\n", bnode->kids[0]->value+bnode->value*8, bnode->reg);
54
55 call:   OP_Call(exprs)                          # 0 # prepare_call(bnode->name, bnode->reg); /* reg_return=bnode->reg; */ do_call(bnode->name, bnode->reg);
56 call:   OP_CallNoParam                          # 0 # # prepare_call(bnode->name, bnode->reg); /* reg_return=bnode->reg; */ /* prepare_call(bnode->name); */ do_call(bnode->name, bnode->reg);
57
58 exprs:  OP_Arg(expr)                            # 0 # /* reg_return=bnode->reg; function_name=bnode->name; */ /* prepare_call(bnode->name); */
59 exprs:  OP_Exprs(exprs,exprs)                   # 0 #
60
61 zero:   OP_Negation(zero)                       # 0 #
62 zero:   OP_Zero                                 # 0 #
63 zero:   OP_Multiplication(zexpr,zero)           # 0 #
64 zero:   OP_Multiplication(zero,zexpr)           # 0 #
65
66 zexpr:  zero                                    # 0 #
67 zexpr:  imm                                     # 0 #
68 zexpr:  OP_Negation(zexpr)                      # 0 #
69 zexpr:  OP_Addition(zexpr,zexpr)                # 0 #
70 zexpr:  OP_Multiplication(zexpr,zexpr)          # 0 #
71 zexpr:  OP_Field(zexpr,OP_ID)                   # 0 #
72 zexpr:  OP_ID                                   # 0 #
73
74 imm:    zero                                    # 0 #
75 imm:    OP_Negation(imm)                        # 0 # bnode->value=-bnode->kids[0]->value;
76 imm:    OP_Addition(imm,imm)                    # 0 # bnode->value=bnode->kids[0]->value+bnode->kids[1]->value;
77 imm:    OP_Multiplication(imm,imm)              # 0 # bnode->value=bnode->kids[0]->value*bnode->kids[1]->value;
78 imm:    OP_Number                               # 0 #
79 imm:    OP_Zero                                 # 0 #
80 imm:    OP_One                                  # 0 #
81
82 bexpr:  OP_Disjunction(bexpr,bexpr)             # 0 #
83 bexpr:  OP_Not(bexpr)                           # 0 #
84 bexpr:  OP_Greater(expr,expr)                   # 0 #
85 bexpr:  OP_Equal(expr,expr)                     # 0 #
86
87 %%
88