From 60a503a7915b81f217507b6037bfadba9c93ad53 Mon Sep 17 00:00:00 2001 From: David Mak Date: Tue, 3 Oct 2023 18:02:45 +0800 Subject: [PATCH] core: Allocate more stack variables at the beginning of function All allocas for temporary objects are now placed in the beginning of the function. Allocas for on-temporary objects are not modified because these variables may appear in a loop and thus must be uniquely allocated by different allocas. --- nac3core/src/codegen/expr.rs | 5 +++-- nac3core/src/toplevel/builtins.rs | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 734f6e76..d66dec08 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -120,7 +120,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { let vals = ls.iter().map(|v| self.gen_symbol_val(generator, v, ty)).collect_vec(); let fields = vals.iter().map(|v| v.get_type()).collect_vec(); let ty = self.ctx.struct_type(&fields, false); - let ptr = self.builder.build_alloca(ty, "tuple"); + let ptr = gen_var(self, ty.into(), Some("tuple")).unwrap(); let zero = self.ctx.i32_type().const_zero(); unsafe { for (i, val) in vals.into_iter().enumerate() { @@ -144,7 +144,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { _ => unreachable!("must be option type"), }; let val = self.gen_symbol_val(generator, v, ty); - let ptr = self.builder.build_alloca(val.get_type(), "default_opt_some"); + let ptr = generator.gen_var_alloc(self, val.get_type(), Some("default_opt_some")).unwrap(); self.builder.build_store(ptr, val); ptr.into() } @@ -664,6 +664,7 @@ pub fn gen_call<'ctx, 'a, G: CodeGenerator>( let key; let param_vals; let is_extern; + let symbol = { // make sure this lock guard is dropped at the end of this scope... let def = definition.read(); diff --git a/nac3core/src/toplevel/builtins.rs b/nac3core/src/toplevel/builtins.rs index 5d07ac34..1168a3cd 100644 --- a/nac3core/src/toplevel/builtins.rs +++ b/nac3core/src/toplevel/builtins.rs @@ -710,7 +710,7 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo { }); let start = start.unwrap_or_else(|| int32.const_zero().into()); let ty = int32.array_type(3); - let ptr = ctx.builder.build_alloca(ty, "range"); + let ptr = generator.gen_var_alloc(ctx, ty.into(), Some("range")).unwrap(); unsafe { let a = ctx.builder.build_in_bounds_gep(ptr, &[zero, zero], "start"); let b = ctx.builder.build_in_bounds_gep( @@ -1236,7 +1236,7 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo { |ctx, _, fun, args, generator| { let arg_ty = fun.0.args[0].ty; let arg_val = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?; - let alloca = ctx.builder.build_alloca(arg_val.get_type(), "alloca_some"); + let alloca = generator.gen_var_alloc(ctx, arg_val.get_type(), Some("alloca_some")).unwrap(); ctx.builder.build_store(alloca, arg_val); Ok(Some(alloca.into())) },