🐛 fix label issue in if/loop statements

This commit is contained in:
ValKmjolnir 2024-03-25 21:51:08 +08:00
parent 62cddc4767
commit 73ec2e8f7e
3 changed files with 25 additions and 2 deletions

View File

@ -8,6 +8,8 @@ out.ll:
test: $(COLGMCC)
@ $(COLGMCC) test/assign.colgm
@ lli out.ll
@ $(COLGMCC) test/branch.colgm
@ lli out.ll
@ $(COLGMCC) test/func.colgm
@ lli out.ll
@ $(COLGMCC) test/hello.colgm

View File

@ -889,8 +889,9 @@ bool ir_gen::visit_ret_stmt(ret_stmt* node) {
}
bool ir_gen::visit_while_stmt(while_stmt* node) {
auto while_begin_label = ircode_block->stmt_size();
auto while_begin_label = ircode_block->stmt_size()+1;
// condition
ircode_block->add_stmt(new sir_br(ircode_block->stmt_size()+1));
ircode_block->add_stmt(new sir_label(ircode_block->stmt_size()));
node->get_condition()->accept(this);
@ -931,8 +932,9 @@ bool ir_gen::visit_if_stmt(if_stmt* node) {
ircode_block->add_stmt(new sir_label(ircode_block->stmt_size()));
node->get_block()->accept(this);
if (br_cond) {
br_cond->set_false_label(ircode_block->stmt_size());
br_cond->set_false_label(ircode_block->stmt_size()+1);
}
ircode_block->add_stmt(new sir_br(ircode_block->stmt_size()+1));
ircode_block->add_stmt(new sir_label(ircode_block->stmt_size()));
ircode_block->add_nop();
return true;

View File

@ -0,0 +1,19 @@
func puts(v: i8*) -> i32;
func main() -> i64 {
if (1==1) {
puts("1==1");
}
if (1!=2) {
puts("1!=2");
}
puts(" -> branch.colgm: if statement");
var a: i64 = 0;
while(a<10) {
puts("wow!");
a += 1;
}
puts(" -> branch.colgm: loop statement");
return 0;
}