From: Michael D. Lowis Date: Tue, 5 Jul 2022 03:03:19 +0000 (-0400) Subject: implemented if statements in code generator X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=124377e3f85de16e8fbafaa5c96b8953e5f95353;p=proto%2Fobnc.git implemented if statements in code generator --- diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c index 325f97a..031561f 100644 --- a/cerise/backend/ssa/codegen.c +++ b/cerise/backend/ssa/codegen.c @@ -311,6 +311,19 @@ void print_op(Parser* p, SsaNode* expr) } puts(""); } + else if (expr->code == IF) + { + printf(" br %%Bool "); + print_ident(p, &(expr->dest)); + printf(", label %%L%ld, label %%L%ld\n", + expr->left.block->id, + expr->right.block->id + ); + } + else if (expr->code == BRANCH) + { + printf(" br label %%L%ld\n", expr->left.block->id); + } break; case MODE_UNOP: diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index ffb8627..41354c6 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -58,6 +58,8 @@ typedef enum { CALL, LOAD, STORE, + + BRANCH, ERROR = -2, END_FILE = -1 } TokType; @@ -287,6 +289,7 @@ void ssa_block_add(SsaBlock* blk, SsaNode* stmt); SsaNode* ssa_if(Parser* p, SsaNode* cond, SsaBlock* br1, SsaBlock* br2); SsaNode* ssa_return(Parser* p, SsaNode* expr); +SsaNode* ssa_branch(Parser* p, SsaBlock* br); SsaNode* ssa_call(SsaNode* func); void ssa_call_add(SsaBlock* call, SsaNode* arg); diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index bb9e354..e646d30 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -414,15 +414,16 @@ static SsaBlock* statement_seq(Parser* p) check_bool(p, cond); expect(p, THEN); SsaNode* if_node = ssa_if(p, cond, NULL, NULL); - SsaBlock* block = p->curr_block; block->links[0] = statement_seq(p); + ssa_branch(p, p->curr_join); block->links[0]->links[0] = p->curr_join; ssa_reset_vars(p); if (accept(p, ELSE)) { block->links[1] = statement_seq(p); + ssa_branch(p, p->curr_join); block->links[1]->links[0] = p->curr_join; } else diff --git a/cerise/src/ssa.c b/cerise/src/ssa.c index cae0fea..c2d4434 100644 --- a/cerise/src/ssa.c +++ b/cerise/src/ssa.c @@ -202,7 +202,10 @@ SsaNode* ssa_store(Parser* p, SsaNode* dest, SsaNode* value) node->left.var = value->dest; } - node->dest.symver = phi_add(p, node->dest); + if (!symbol_getbyid(p, node->dest.symid)->global) + { + node->dest.symver = phi_add(p, node->dest); + } ssa_block_add(p->curr_block, node); node->loaded = 1; return node; @@ -285,6 +288,16 @@ SsaNode* ssa_return(Parser* p, SsaNode* expr) return node; } +SsaNode* ssa_branch(Parser* p, SsaBlock* br) +{ + SsaNode* node = ssa_node(BRANCH, MODE_CTRL); + node->left.block = br; + ssa_block_add(p->curr_block, node); + node->loaded = 1; + return node; +} + + SsaNode* ssa_call(SsaNode* func); void ssa_call_add(SsaBlock* call, SsaNode* arg); diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 1088426..f466d89 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -81,7 +81,6 @@ begin vBool = vInt >= vInt; end - procedure TestRealArithOps() begin vReal = vReal + 1.0; @@ -132,6 +131,19 @@ begin vBool = vReal >= vReal; end +procedure TestIfStatements() +begin + if vInt == 1 then + vInt = 42; + end + + if vInt == 1 then + vInt = 42; + else + vInt = 24; + end +end + #import # Foo # Bar3 = Bar2 @@ -204,100 +216,6 @@ end ##end # begin -# # Integer Arithmetic ops -# c = b + 1; -# c = 1 + b; -# c = b + b; -# -# c = b - 1; -# c = 1 - b; -# c = b - b; -# -# c = b * 1; -# c = 1 * b; -# c = b * b; -# -# c = b / 1; -# c = 1 / b; -# c = b / b; -# -# c = b % 1; -# c = 1 % b; -# c = b % b; -# -# # Integer Comparison ops -# a = b == 1; -# a = 1 == b; -# a = b == b; -# -# a = b != 1; -# a = 1 != b; -# a = b != b; -# -# a = b < 1; -# a = 1 < b; -# a = b < b; -# -# a = b > 1; -# a = 1 > b; -# a = b > b; -# -# a = b <= 1; -# a = 1 <= b; -# a = b <= b; -# -# a = b >= 1; -# a = 1 >= b; -# a = b >= b; -# -# # Real Arithmetic ops -# e = d + 1.0; -# e = 1.0 + d; -# e = d + d; -# -# e = d - 1.0; -# e = 1.0 - d; -# e = d - d; -# -# e = d * 1.0; -# e = 1.0 * d; -# e = d * d; -# -# e = d / 1.0; -# e = 1.0 / d; -# e = d / d; -# -# e = d % 1.0; -# e = 1.0 % d; -# e = d % d; -# -# # Real Comparison ops -# a = d == 1.0; -# a = 1.0 == d; -# a = d == d; -# -# a = d != 1.0; -# a = 1.0 != d; -# a = d != d; -# -# a = d < 1.0; -# a = 1.0 < d; -# a = d < d; -# -# a = d > 1.0; -# a = 1.0 > d; -# a = d > d; -# -# a = d <= 1.0; -# a = 1.0 <= d; -# a = d <= d; -# -# a = d >= 1.0; -# a = 1.0 >= d; -# a = d >= d; -# -# -# ## b = 1; ## if c == b then ## b = b - 1;