diff --git a/nac3core/src/codegen/generator.rs b/nac3core/src/codegen/generator.rs index 22d0eac..f82c0f7 100644 --- a/nac3core/src/codegen/generator.rs +++ b/nac3core/src/codegen/generator.rs @@ -83,24 +83,49 @@ pub trait CodeGenerator { /// Allocate memory for a variable and return a pointer pointing to it. /// The default implementation places the allocations at the start of the function. + fn gen_var_alloc_named<'ctx, 'a>( + &mut self, + ctx: &mut CodeGenContext<'ctx, 'a>, + ty: BasicTypeEnum<'ctx>, + name: &str, + ) -> Result, String> { + gen_var(ctx, ty, Some(name)) + } + + /// Similar to [gen_var_alloc_named], but provides a default name for the created pointer + /// variable. fn gen_var_alloc<'ctx, 'a>( &mut self, ctx: &mut CodeGenContext<'ctx, 'a>, ty: BasicTypeEnum<'ctx>, ) -> Result, String> { - gen_var(ctx, ty) + gen_var(ctx, ty, None) } /// Return a pointer pointing to the target of the expression. + fn gen_store_target_named<'ctx, 'a>( + &mut self, + ctx: &mut CodeGenContext<'ctx, 'a>, + pattern: &Expr>, + name: &str, + ) -> Result, String> + where + Self: Sized, + { + gen_store_target(self, ctx, pattern, Some(name)) + } + + /// Similar to [gen_store_target_named], but provides a default name for the created pointer + /// variable. fn gen_store_target<'ctx, 'a>( &mut self, ctx: &mut CodeGenContext<'ctx, 'a>, pattern: &Expr>, ) -> Result, String> - where - Self: Sized, + where + Self: Sized, { - gen_store_target(self, ctx, pattern) + gen_store_target(self, ctx, pattern, None) } /// Generate code for an assignment expression. diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index aa2e317..a2f7c02 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -24,12 +24,13 @@ use std::convert::TryFrom; pub fn gen_var<'ctx, 'a>( ctx: &mut CodeGenContext<'ctx, 'a>, ty: BasicTypeEnum<'ctx>, + name: Option<&str>, ) -> Result, String> { // put the alloca in init block let current = ctx.builder.get_insert_block().unwrap(); // position before the last branching instruction... ctx.builder.position_before(&ctx.init_bb.get_last_instruction().unwrap()); - let ptr = ctx.builder.build_alloca(ty, "tmp"); + let ptr = ctx.builder.build_alloca(ty, name.unwrap_or("tmp")); ctx.builder.position_at_end(current); Ok(ptr) } @@ -38,6 +39,7 @@ pub fn gen_store_target<'ctx, 'a, G: CodeGenerator>( generator: &mut G, ctx: &mut CodeGenContext<'ctx, 'a>, pattern: &Expr>, + name: Option<&str>, ) -> Result, String> { // very similar to gen_expr, but we don't do an extra load at the end // and we flatten nested tuples @@ -45,7 +47,7 @@ pub fn gen_store_target<'ctx, 'a, G: CodeGenerator>( ExprKind::Name { id, .. } => match ctx.var_assignment.get(id) { None => { let ptr_ty = ctx.get_llvm_type(generator, pattern.custom.unwrap()); - let ptr = generator.gen_var_alloc(ctx, ptr_ty)?; + let ptr = generator.gen_var_alloc_named(ctx, ptr_ty, name.unwrap_or("tmp"))?; ctx.var_assignment.insert(*id, (ptr, None, 0)); ptr } @@ -74,7 +76,7 @@ pub fn gen_store_target<'ctx, 'a, G: CodeGenerator>( ctx.ctx.i32_type().const_zero(), ctx.ctx.i32_type().const_int(index as u64, false), ], - "attr", + name.unwrap_or("attr"), ) } } @@ -135,7 +137,7 @@ pub fn gen_store_target<'ctx, 'a, G: CodeGenerator>( let arr_ptr = ctx .build_gep_and_load(v, &[i32_type.const_zero(), i32_type.const_zero()]) .into_pointer_value(); - ctx.builder.build_gep(arr_ptr, &[index], "loadarrgep") + ctx.builder.build_gep(arr_ptr, &[index], name.unwrap_or("loadarrgep")) } } _ => unreachable!(),