From e7fca6778627b5270a1a95229a1982eb246a5e5c Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 12 Apr 2024 18:59:18 +0800 Subject: [PATCH] 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. --- nac3core/src/codegen/stmt.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index fd5d51a0..6ca73cd0 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -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;