diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index d66dec088..89db94d38 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -459,9 +459,14 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { params: [Option>; 3], loc: Location, ) { - let ty = self.get_llvm_type(generator, self.primitives.exception).into_pointer_type(); - let zelf_ty: BasicTypeEnum = ty.get_element_type().into_struct_type().into(); - let zelf = generator.gen_var_alloc(self, zelf_ty, Some("exn")).unwrap(); + let zelf = if let Some(exception_val) = self.exception_val { + exception_val + } else { + let ty = self.get_llvm_type(generator, self.primitives.exception).into_pointer_type(); + let zelf_ty: BasicTypeEnum = ty.get_element_type().into_struct_type().into(); + let zelf = generator.gen_var_alloc(self, zelf_ty, Some("exn")).unwrap(); + self.exception_val.insert(zelf).to_owned() + }; let int32 = self.ctx.i32_type(); let zero = int32.const_zero(); unsafe { diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 28a4d60fd..da5cee895 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -146,6 +146,7 @@ pub struct CodeGenContext<'ctx, 'a> { pub const_strings: HashMap>, // stores the alloca for variables pub init_bb: BasicBlock<'ctx>, + pub exception_val: Option>, /// The header and exit basic blocks of a loop in this context. See /// https://llvm.org/docs/LoopTerminology.html for explanation of these terminology. pub loop_target: Option<(BasicBlock<'ctx>, BasicBlock<'ctx>)>, @@ -802,6 +803,7 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte type_cache, primitives, init_bb, + exception_val: Default::default(), builder, module, unifier,