core/stmt: Do not generate jumps if bb is already terminated

Future-proofs gen_*_callback functions in case other codegen functions
will delegate to it in the future.
This commit is contained in:
David Mak 2024-04-12 18:59:18 +08:00
parent 52c731c312
commit e7fca67786
1 changed files with 15 additions and 11 deletions

View File

@ -509,28 +509,32 @@ pub fn gen_for_callback<'ctx, 'a, G, I, InitFn, CondFn, BodyFn, UpdateFn>(
ctx.builder.build_unconditional_branch(init_bb).unwrap();
let loop_var = {
ctx.builder.position_at_end(init_bb);
let result = init(generator, ctx)?;
ctx.builder.position_at_end(init_bb);
let loop_var = init(generator, ctx)?;
if !ctx.is_terminated() {
ctx.builder.build_unconditional_branch(cond_bb).unwrap();
result
};
}
ctx.builder.position_at_end(cond_bb);
let cond = cond(generator, ctx, loop_var.clone())?;
assert_eq!(cond.get_type().get_bit_width(), ctx.ctx.bool_type().get_bit_width());
ctx.builder
.build_conditional_branch(cond, body_bb, cont_bb)
.unwrap();
if !ctx.is_terminated() {
ctx.builder
.build_conditional_branch(cond, body_bb, cont_bb)
.unwrap();
}
ctx.builder.position_at_end(body_bb);
body(generator, ctx, loop_var.clone())?;
ctx.builder.build_unconditional_branch(update_bb).unwrap();
if !ctx.is_terminated() {
ctx.builder.build_unconditional_branch(update_bb).unwrap();
}
ctx.builder.position_at_end(update_bb);
update(generator, ctx, loop_var)?;
ctx.builder.build_unconditional_branch(cond_bb).unwrap();
if !ctx.is_terminated() {
ctx.builder.build_unconditional_branch(cond_bb).unwrap();
}
ctx.builder.position_at_end(cont_bb);
ctx.loop_target = loop_bb;