From: Michael D. Lowis Date: Tue, 30 Nov 2021 22:10:27 +0000 (-0500) Subject: reporting if statements correctly now X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=34107ae5147fa3f556036bc3e76d88dba2d4e87a;p=proto%2Fobnc.git reporting if statements correctly now --- diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 221ed7a..158a21e 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -413,6 +413,7 @@ static SsaBlock* statement_seq(Parser* p) SsaNode* cond = expression(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); @@ -423,13 +424,15 @@ static SsaBlock* statement_seq(Parser* p) { block->links[1] = statement_seq(p); block->links[1]->links[0] = p->curr_join; - /* reset vars to backup values */ } else { block->links[1] = p->curr_join; } + if_node->left.block = block->links[0]; + if_node->right.block = block->links[1]; + /* pop the join node */ p->curr_block = p->curr_join; p->curr_join = p->curr_join->next; diff --git a/cerise/src/ssa.c b/cerise/src/ssa.c index c99bd50..8c335dd 100644 --- a/cerise/src/ssa.c +++ b/cerise/src/ssa.c @@ -184,9 +184,14 @@ void ssa_block_add(SsaBlock* blk, SsaNode* node) SsaNode* ssa_if(Parser* p, SsaNode* cond, SsaBlock* br1, SsaBlock* br2) { - (void)br1, (void)br2; cond = load(p, cond); - return NULL; + SsaNode* node = ssa_node(IF, MODE_CONTROL); + node->type = &VoidType; + node->left.block = br1; + node->right.block = br2; + node = load(p, node); + node->dest = cond->dest; + return node; } SsaNode* ssa_return(SsaNode* expr); @@ -406,8 +411,11 @@ static void print_ident(Parser* p, SsaVar* var) static void print_dest(Parser* p, SsaNode* node) { - print_ident(p, &(node->dest)); - printf(" = "); + if (node->mode != MODE_CONTROL) + { + print_ident(p, &(node->dest)); + printf(" = "); + } } static void print_opcode(SsaNode* node) @@ -427,19 +435,19 @@ static void print_opcode(SsaNode* node) } else if (op == EQ) { - printf("=="); + printf(" == "); } else if (op == NEQ) { - printf("!="); + printf(" != "); } else if (op == LTEQ) { - printf("<="); + printf(" <= "); } else if (op == GTEQ) { - printf(">="); + printf(" >= "); } else { @@ -468,6 +476,12 @@ void ssa_print(Parser* p, SsaNode* expr) printf("(Real)%f", ssa_asreal(expr)); break; + case IF: + printf("if "); + print_ident(p, &(expr->dest)); + printf(" then block%lu else block%lu", expr->left.block->id, expr->right.block->id); + break; + default: if (expr->mode == MODE_VAR) { @@ -560,7 +574,7 @@ void ssa_print_graph(Parser* p, SsaBlock* block) { /* print the phis */ - for (SsaPhi* phi = block->phis; phi; phi = phi->next) + for (SsaPhi* phi = curr->phis; phi; phi = phi->next) { Symbol* s = symbol_getbyid(p, phi->symid); s->version++;