]> git.mdlowis.com Git - proto/obnc.git/commitdiff
implemented if statements in code generator
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 5 Jul 2022 03:03:19 +0000 (23:03 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 5 Jul 2022 03:03:19 +0000 (23:03 -0400)
cerise/backend/ssa/codegen.c
cerise/inc/cerise.h
cerise/src/grammar.c
cerise/src/ssa.c
cerise/tests/Module.m

index 325f97a183f37ba427a4013370505f087094b614..031561f0ea0fce49fbf6e6f03f548350ca899565 100644 (file)
@@ -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:
index ffb86274b5d2e393562460c526189d9b12537647..41354c6bd632dc533a8fdc4e1c840d2237975cc7 100644 (file)
@@ -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);
index bb9e35453718a40c960fb879664d8d929de7d68e..e646d3085710fd9138c3541d4f3ee14476744cd5 100644 (file)
@@ -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
index cae0fea79d11979388321c96d51c6f70943da73d..c2d4434577687aad4ebbe998085f510aef717030 100644 (file)
@@ -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);
 
index 108842602a53cb6436039f09897874fc5864b893..f466d8953c81560bf95fbc7fb0813bd2020ba26a 100644 (file)
@@ -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;