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.
This commit is contained in:
David Mak 2023-10-03 18:02:45 +08:00
parent 0c49b30a90
commit 60a503a791
2 changed files with 5 additions and 4 deletions

View File

@ -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 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 fields = vals.iter().map(|v| v.get_type()).collect_vec();
let ty = self.ctx.struct_type(&fields, false); 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(); let zero = self.ctx.i32_type().const_zero();
unsafe { unsafe {
for (i, val) in vals.into_iter().enumerate() { for (i, val) in vals.into_iter().enumerate() {
@ -144,7 +144,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
_ => unreachable!("must be option type"), _ => unreachable!("must be option type"),
}; };
let val = self.gen_symbol_val(generator, v, ty); 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); self.builder.build_store(ptr, val);
ptr.into() ptr.into()
} }
@ -664,6 +664,7 @@ pub fn gen_call<'ctx, 'a, G: CodeGenerator>(
let key; let key;
let param_vals; let param_vals;
let is_extern; let is_extern;
let symbol = { let symbol = {
// make sure this lock guard is dropped at the end of this scope... // make sure this lock guard is dropped at the end of this scope...
let def = definition.read(); let def = definition.read();

View File

@ -710,7 +710,7 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo {
}); });
let start = start.unwrap_or_else(|| int32.const_zero().into()); let start = start.unwrap_or_else(|| int32.const_zero().into());
let ty = int32.array_type(3); 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 { unsafe {
let a = ctx.builder.build_in_bounds_gep(ptr, &[zero, zero], "start"); let a = ctx.builder.build_in_bounds_gep(ptr, &[zero, zero], "start");
let b = ctx.builder.build_in_bounds_gep( 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| { |ctx, _, fun, args, generator| {
let arg_ty = fun.0.args[0].ty; let arg_ty = fun.0.args[0].ty;
let arg_val = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_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); ctx.builder.build_store(alloca, arg_val);
Ok(Some(alloca.into())) Ok(Some(alloca.into()))
}, },