From 128163389fedc8bc6029a0efd522ba286eff0c9a Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Sat, 29 May 2010 23:13:51 +0200 Subject: [PATCH] codeb: minimale 'IF expr THEN statseq END' implementierung --- codeb/code.bfe | 5 ++++- codeb/parser.y | 12 +++++++++++- codeb/tree.h | 4 ++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/codeb/code.bfe b/codeb/code.bfe index 61fc793..3847d7a 100644 --- a/codeb/code.bfe +++ b/codeb/code.bfe @@ -175,12 +175,13 @@ void gen_subspecial(struct treenode *bnode, short e) %} %start begin -%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 +%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 %% begin: ret # 0 # printf("\n"); begin: assign # 0 # +begin: ifstat # 0 # assign: O_ASSIGN(O_ID, expr) # 1 # KIDREG2PARM(0); printf("\tmovq %%%s, %%%s\n", BN_REG, KID_REG(0)); assign: O_ASSIGN(O_ID, O_ID) # 1 # KIDREG2PARM(0); KIDREG2PARM(1); printf("\tmovq %%%s, %%%s\n", KID_REG(1), KID_REG(0)); @@ -191,6 +192,8 @@ ret: O_RET(retexpr) # 2 # printf("\t//o_ret(expr)\n"); move(BN_REG, "rax"); func retexpr: O_ID # 1 # printf("\t//retexpr\n"); if(bnode->param_index > -1) move(param_reg(bnode->param_index), BN_REG); retexpr: expr +ifstat: O_IF(expr) # 0 # + expr: O_ID # 0 # expr: imm # 1 # moveimm(BN_VAL, BN_REG); diff --git a/codeb/parser.y b/codeb/parser.y index 0141755..cda1e7b 100644 --- a/codeb/parser.y +++ b/codeb/parser.y @@ -172,8 +172,17 @@ Statement: statinout() xxputsin(@Expr.s@,) xxputsin(@Statseq.s@,) - @i @Statement.node@ = TREENULL; + + @i @Statement.node@ = new_node(O_IF, @Expr.node@, TREENULL); @i @Statement.vars@ = 0; + + @reg @Statement.node@->reg = @Expr.node@->reg = next_reg((char *)NULL, @Expr.gparamges@); + @gen { + write_tree(@Statement.node@, 0); burm_label(@Statement.node@); burm_reduce(@Statement.node@, 1); + /* TODO: kann ich mir das test wirklich wegan and davor sparen? */ + printf("\ttest %s1, %%rax\n\tjz if_end\n", "$"); + } + @gen @revorder(1) printf("if_end:\n"); @} | IF Expr THEN Statseq ELSE Statseq END @@ -199,6 +208,7 @@ Statement: @{ statinout() xxputsin(@Expr.s@,) + @i @Statement.vars@ = 0; @i @Statement.node@ = new_node(O_RET, @Expr.node@, TREENULL); @reg @Statement.node@->reg = @Expr.node@->reg = next_reg((char *)NULL, @Expr.gparamges@); diff --git a/codeb/tree.h b/codeb/tree.h index 8975cc1..a4cfd60 100644 --- a/codeb/tree.h +++ b/codeb/tree.h @@ -13,14 +13,14 @@ enum { O_RET=1, O_NULL, O_SUB, O_MUL, O_OR=5, O_LESS, O_EQ, O_ID, O_ADD, O_NUM=10, O_FIELD, O_MTWO, O_MFOUR, O_MEIGHT, - O_MONE=15, O_ASSIGN + O_MONE=15, O_ASSIGN, O_IF }; static char *o_names[] = { "", "O_RET", "O_NULL", "O_SUB", "O_MUL", "O_OR", "O_LESS", "O_EQ", "O_ID", "O_ADD", "O_NUM", "O_FIELD", "O_MTWO", "O_MFOUR", "O_MEIGHT", - "O_MONE", "O_ASSIGN" + "O_MONE", "O_ASSIGN", "O_IF" }; struct treenode { -- 2.25.1