-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.bfe
More file actions
executable file
·91 lines (74 loc) · 7.53 KB
/
code.bfe
File metadata and controls
executable file
·91 lines (74 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
%{
/* vim: filetype=c
*/
#define CODE
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "tree.h"
#include "code_gen.h"
/* TODO (-a)+b */
/* TODO func f(a,a) return a; end; shall do the same as func f(b,a) return a; end; */
%}
%start stat
%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_NotEqual=16 OP_Nop=17 OP_MinusNot=18 OP_Assign=19 OP_NopEmpty=20 OP_If=21 OP_Stats=22 OP_CallNoParam=23 OP_Arg=24
%%
stat: ret # 0 #
stat: assign # 0 #
stat: OP_NopEmpty # 0 #
stat: expr # 0 #
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); }
assign: OP_Assign(OP_Field(expr,OP_ID), expr) # 1 # printf("\tmovq %%%s, %li(%%%s)\n", bnode->kids[1]->reg, bnode->kids[0]->value, bnode->kids[0]->reg);
ret: OP_Return(expr) # 1 # move(bnode->reg, "rax"); ret();
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); }
expr: imm # 1 # printf("\tmovq $%li, %%%s\n", bnode->value, bnode->reg);
expr: call # 0 #
expr: OP_Nop(expr) # 0 #
expr: OP_Negation(expr) # 1 # printf("\tnegq %%%s\n", bnode->reg);
expr: OP_Not(expr) # 1 # printf("\tnotq %%%s\n", bnode->reg);
expr: OP_MinusNot(expr) # 1 # printf("\tnotq %%%s\n", bnode->reg); printf("\tnegq %%%s\n", bnode->reg);
expr: OP_NotEqual(expr,expr) # 1 # printf("\tcmpq %%%s, %%%s\n", bnode->kids[1]->reg, bnode->kids[0]->reg); printf("\tsetne %%al\n"); printf("\tmovzbq %%al, %%%s\n", bnode->reg); printf("\tnegq %%%s\n", bnode->reg);
expr: OP_NotEqual(imm,expr) # 1 # printf("\tcmpq $%li, %%%s\n", bnode->kids[0]->value, bnode->kids[1]->reg); printf("\tsetne %%al\n"); printf("\tmovzbq %%al, %%%s\n", bnode->reg); printf("\tnegq %%%s\n", bnode->reg);
expr: OP_NotEqual(expr,imm) # 1 # printf("\tcmpq $%li, %%%s\n", bnode->kids[1]->value, bnode->kids[0]->reg); printf("\tsetne %%al\n"); printf("\tmovzbq %%al, %%%s\n", bnode->reg); printf("\tnegq %%%s\n", bnode->reg);
expr: OP_Greater(expr,expr) # 1 # printf("\tcmpq %%%s, %%%s\n", bnode->kids[1]->reg, bnode->kids[0]->reg); printf("\tsetg %%al\n"); printf("\tmovzbq %%al, %%%s\n", bnode->reg); printf("\tnegq %%%s\n", bnode->reg);
expr: OP_Greater(imm,expr) # 1 # printf("\tcmpq $%li, %%%s\n", bnode->kids[0]->value, bnode->kids[1]->reg); printf("\tsetl %%al\n"); printf("\tmovzbq %%al, %%%s\n", bnode->reg); printf("\tnegq %%%s\n", bnode->reg);
expr: OP_Greater(expr,imm) # 1 # printf("\tcmpq $%li, %%%s\n", bnode->kids[1]->value, bnode->kids[0]->reg); printf("\tsetg %%al\n"); printf("\tmovzbq %%al, %%%s\n", bnode->reg); printf("\tnegq %%%s\n", bnode->reg);
expr: OP_Disjunction(expr,expr) # 1 # printf("\torq %%%s, %%%s\n", bnode->kids[0]->reg, bnode->kids[1]->reg);
expr: OP_Disjunction(imm,expr) # 1 # printf("\torq $%li, %%%s\n", bnode->kids[0]->value, bnode->kids[1]->reg); move(bnode->kids[1]->reg, bnode->reg);
expr: OP_Disjunction(expr,imm) # 1 # if(bnode->kids[0]->op=OP_ID) { move(bnode->kids[0]->reg, bnode->reg); printf("\torq $%li, %%%s\n", bnode->kids[1]->value, bnode->reg); } else { printf("\torq $%li, %%%s\n", bnode->kids[1]->value, bnode->kids[0]->reg); }
expr: OP_Addition(expr,expr) # 1 # printf("\taddq %%%s, %%%s\n", bnode->kids[1]->reg, bnode->kids[0]->reg);
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);
expr: OP_Addition(expr,imm) # 1 # if(bnode->kids[0]->op=OP_ID) { move(bnode->kids[0]->reg, bnode->reg); printf("\taddq $%li, %%%s\n", bnode->kids[1]->value, bnode->reg); } else { printf("\tadd $%li, %%%s\n", bnode->kids[1]->value, bnode->kids[0]->reg); }
expr: OP_Multiplication(expr,expr) # 1 # printf("\timulq %%%s, %%%s\n", bnode->kids[1]->reg, bnode->kids[0]->reg);
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);
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("\tadd $%li, %%%s\n", bnode->kids[1]->value, bnode->kids[0]->reg); }
expr: OP_Field(expr,OP_ID) # 2 # printf("\tmovq %li(%%%s), %%%s\n", bnode->value, bnode->kids[0]->reg, bnode->reg);
expr: OP_Field(imm,OP_ID) # 1 # printf("\tmovq %li, %%%s\n", bnode->kids[0]->value+bnode->value, bnode->reg);
call: OP_Call(exprs) # 0 # prepare_call(bnode->name, bnode->reg); /* reg_return=bnode->reg; */ do_call(bnode->name, bnode->reg);
call: OP_CallNoParam # 0 # prepare_call(bnode->name, bnode->reg); /* reg_return=bnode->reg; */ /* prepare_call(bnode->name); */ do_call(bnode->name, bnode->reg);
exprs: OP_Arg(expr) # 0 # /* reg_return=bnode->reg; function_name=bnode->name; */ /* prepare_call(bnode->name); */
exprs: expr # 0 #
exprs: OP_Exprs(exprs,expr) # 0 #
zero: OP_Negation(zero) # 0 #
zero: OP_Zero # 0 #
zero: OP_Multiplication(zexpr,zero) # 0 #
zero: OP_Multiplication(zero,zexpr) # 0 #
zexpr: zero # 0 #
zexpr: imm # 0 #
zexpr: OP_Negation(zexpr) # 0 #
zexpr: OP_Addition(zexpr,zexpr) # 0 #
zexpr: OP_Multiplication(zexpr,zexpr) # 0 #
zexpr: OP_Field(zexpr,OP_ID) # 0 #
zexpr: OP_ID # 0 #
imm: zero # 0 #
imm: OP_Not(imm) # 0 # bnode->value = ~ bnode->kids[0]->value;
imm: OP_Negation(imm) # 0 # bnode->value = - bnode->kids[0]->value;
imm: OP_Addition(imm,imm) # 0 # bnode->value = bnode->kids[0]->value + bnode->kids[1]->value;
imm: OP_Multiplication(imm,imm) # 0 # bnode->value = bnode->kids[0]->value * bnode->kids[1]->value;
imm: OP_Disjunction(imm,imm) # 0 # bnode->value = bnode->kids[0]->value | bnode->kids[1]->value;
imm: OP_Greater(imm,imm) # 0 # bnode->value = (bnode->kids[0]->value > bnode->kids[1]->value) ? -1 : 0;
imm: OP_NotEqual(imm,imm) # 0 # bnode->value = (bnode->kids[0]->value != bnode->kids[1]->value) ? -1 : 0;
imm: OP_Number # 0 #
imm: OP_Zero # 0 #
imm: OP_One # 0 #
%%