From: Michael D. Lowis Date: Wed, 1 Dec 2021 21:38:14 +0000 (-0500) Subject: attempted to add in function blocks but return statements are not working at the... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=676bc8cbd945048d19ddde869eb4987cd8f14bb4;p=proto%2Fobnc.git attempted to add in function blocks but return statements are not working at the moment --- diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index bddaf0f..ad68702 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -270,7 +270,7 @@ SsaBlock* ssa_block(Parser* p); void ssa_block_add(SsaBlock* blk, SsaNode* stmt); SsaNode* ssa_if(Parser* p, SsaNode* cond, SsaBlock* br1, SsaBlock* br2); -SsaNode* ssa_return(SsaNode* expr); +SsaNode* ssa_return(Parser* p, SsaNode* expr); 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 158a21e..b476629 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -438,11 +438,6 @@ static SsaBlock* statement_seq(Parser* p) p->curr_join = p->curr_join->next; expect(p, END); } -// else if (accept(p, END)) -// { -// /* this is an empty block?? */ -// break; -// } else /* assignments/expressions */ { SsaNode* expr = expression(p); @@ -574,6 +569,32 @@ void proc_decl(Parser* p) var_decl(p); } + expect(p, BEGIN); + SsaBlock* block = ssa_block(p); + p->curr_join = ssa_block(p); + block->links[1] = p->curr_join; + + if (!matches(p, RETURN) && !matches(p, END)) + { + block->links[0] = statement_seq(p); + } + else + { + block->links[0] = ssa_block(p); + p->curr_block = block->links[0]; + } + + if (proctype->base != &VoidType) + { + expect(p, RETURN); + SsaNode* retval = expression(p); + check_type(p, proctype->base, retval); + expect(p, ';'); + ssa_return(p, retval); + } + expect(p, END); + ssa_print_graph(p, block); + // /* parse the body of the procedure */ // expect(p, BEGIN); // proc->value = ast_block(); diff --git a/cerise/src/ssa.c b/cerise/src/ssa.c index 8c335dd..45b74f3 100644 --- a/cerise/src/ssa.c +++ b/cerise/src/ssa.c @@ -194,7 +194,15 @@ SsaNode* ssa_if(Parser* p, SsaNode* cond, SsaBlock* br1, SsaBlock* br2) return node; } -SsaNode* ssa_return(SsaNode* expr); +SsaNode* ssa_return(Parser* p, SsaNode* expr) +{ + load(p, expr); + SsaNode* node = ssa_node(RETURN, MODE_CONTROL); + node->type = expr->type; + node = load(p, node); + node->dest = expr->dest; + 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 1b08561..92d535f 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -54,11 +54,11 @@ var # return z1; #end -#procedure Bar*(a : Int) : Int -#begin -# return a; -#end -# +procedure Bar*(a : Int) : Int +begin + return a; +end + #procedure Baz*(a : Int) #begin # if (1 > 2) then @@ -70,8 +70,8 @@ var begin b = 42; -# b = -b; -# c = b + 1; + b = -b; + c = b + 1; if c == b then c = 42;