From 357970a793b837b2553cf44e5c9ee3d92104b1d2 Mon Sep 17 00:00:00 2001 From: David Mak Date: Wed, 15 Jan 2025 15:19:18 +0800 Subject: [PATCH] [core] codegen/stmt: Add build_{break,continue}_branch functions --- nac3core/src/codegen/expr.rs | 4 +--- nac3core/src/codegen/stmt.rs | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 8f52e929..30b8dcd3 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -2122,9 +2122,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>( ctx.ctx.bool_type().const_zero(), ) .unwrap(); - ctx.builder - .build_unconditional_branch(hooks.exit_bb) - .unwrap(); + hooks.build_break_branch(&ctx.builder); Ok(()) }, diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index 85a894ac..7b99bc26 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -1,6 +1,7 @@ use inkwell::{ attributes::{Attribute, AttributeLoc}, basic_block::BasicBlock, + builder::Builder, types::{BasicType, BasicTypeEnum}, values::{BasicValue, BasicValueEnum, FunctionValue, IntValue, PointerValue}, IntPredicate, @@ -662,11 +663,25 @@ pub fn gen_for( #[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)] pub struct BreakContinueHooks<'ctx> { /// The [exit block][`BasicBlock`] to branch to when `break`-ing out of a loop. - pub exit_bb: BasicBlock<'ctx>, + exit_bb: BasicBlock<'ctx>, /// The [latch basic block][`BasicBlock`] to branch to for `continue`-ing to the next iteration /// of the loop. - pub latch_bb: BasicBlock<'ctx>, + latch_bb: BasicBlock<'ctx>, +} + +impl<'ctx> BreakContinueHooks<'ctx> { + /// Creates a [`br` instruction][Builder::build_unconditional_branch] to the exit + /// [`BasicBlock`], as if by calling `break`. + pub fn build_break_branch(&self, builder: &Builder<'ctx>) { + builder.build_unconditional_branch(self.exit_bb).unwrap(); + } + + /// Creates a [`br` instruction][Builder::build_unconditional_branch] to the latch + /// [`BasicBlock`], as if by calling `continue`. + pub fn build_continue_branch(&self, builder: &Builder<'ctx>) { + builder.build_unconditional_branch(self.latch_bb).unwrap(); + } } /// Generates a C-style `for` construct using lambdas, similar to the following C code: