core: Allocate exceptions at the beginning of function

Only one instance of exception is necessary, as exceptions will always
be initialized before being thrown.
This commit is contained in:
David Mak 2023-10-06 11:47:28 +08:00
parent 60a503a791
commit a2fce49b26
2 changed files with 10 additions and 3 deletions

View File

@ -459,9 +459,14 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
params: [Option<IntValue<'ctx>>; 3], params: [Option<IntValue<'ctx>>; 3],
loc: Location, loc: Location,
) { ) {
let ty = self.get_llvm_type(generator, self.primitives.exception).into_pointer_type(); let zelf = if let Some(exception_val) = self.exception_val {
let zelf_ty: BasicTypeEnum = ty.get_element_type().into_struct_type().into(); exception_val
let zelf = generator.gen_var_alloc(self, zelf_ty, Some("exn")).unwrap(); } 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 int32 = self.ctx.i32_type();
let zero = int32.const_zero(); let zero = int32.const_zero();
unsafe { unsafe {

View File

@ -146,6 +146,7 @@ pub struct CodeGenContext<'ctx, 'a> {
pub const_strings: HashMap<String, BasicValueEnum<'ctx>>, pub const_strings: HashMap<String, BasicValueEnum<'ctx>>,
// stores the alloca for variables // stores the alloca for variables
pub init_bb: BasicBlock<'ctx>, pub init_bb: BasicBlock<'ctx>,
pub exception_val: Option<PointerValue<'ctx>>,
/// The header and exit basic blocks of a loop in this context. See /// The header and exit basic blocks of a loop in this context. See
/// https://llvm.org/docs/LoopTerminology.html for explanation of these terminology. /// https://llvm.org/docs/LoopTerminology.html for explanation of these terminology.
pub loop_target: Option<(BasicBlock<'ctx>, BasicBlock<'ctx>)>, 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, type_cache,
primitives, primitives,
init_bb, init_bb,
exception_val: Default::default(),
builder, builder,
module, module,
unifier, unifier,