From ff239685441ec39295b60a776a238786e6efdcfb Mon Sep 17 00:00:00 2001 From: David Mak Date: Wed, 6 Sep 2023 11:00:02 +0800 Subject: [PATCH] core: Add name parameter to gen_{var_alloc,store_target} This allows variables in the IR to be assigned a custom name as opposed to names with a default prefix. --- nac3artiq/src/codegen.rs | 8 ++++---- nac3core/src/codegen/expr.rs | 6 +++--- nac3core/src/codegen/generator.rs | 6 ++++-- nac3core/src/codegen/stmt.rs | 22 ++++++++++++---------- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/nac3artiq/src/codegen.rs b/nac3artiq/src/codegen.rs index afee7e56..94563bab 100644 --- a/nac3artiq/src/codegen.rs +++ b/nac3artiq/src/codegen.rs @@ -84,7 +84,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { .try_as_basic_value() .left() .unwrap(); - let end_store = self.gen_store_target(ctx, &end)?; + let end_store = self.gen_store_target(ctx, &end, None)?; ctx.builder.build_store(end_store, max); } if let Some(start) = self.start.clone() { @@ -140,7 +140,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { node: ExprKind::Name { id: start, ctx: name_ctx.clone() }, custom: Some(ctx.primitives.int64), }; - let start = self.gen_store_target(ctx, &start_expr)?; + let start = self.gen_store_target(ctx, &start_expr, None)?; ctx.builder.build_store(start, now); Ok(Some(start_expr)) as Result<_, String> }, @@ -153,7 +153,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { node: ExprKind::Name { id: end, ctx: name_ctx.clone() }, custom: Some(ctx.primitives.int64), }; - let end = self.gen_store_target(ctx, &end_expr)?; + let end = self.gen_store_target(ctx, &end_expr, None)?; ctx.builder.build_store(end, now); self.end = Some(end_expr); self.name_counter += 1; @@ -204,7 +204,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> { .try_as_basic_value() .left() .unwrap(); - let outer_end = self.gen_store_target(ctx, old_end)?; + let outer_end = self.gen_store_target(ctx, old_end, None)?; ctx.builder.build_store(outer_end, max); } self.start = old_start; diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index f6f01887..908c4cce 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -810,7 +810,7 @@ pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>( let zero_size_t = size_t.const_zero(); let zero_32 = int32.const_zero(); - let index = generator.gen_var_alloc(ctx, size_t.into())?; + let index = generator.gen_var_alloc(ctx, size_t.into(), None)?; ctx.builder.build_store(index, zero_size_t); let elem_ty = ctx.get_llvm_type(generator, elt.custom.unwrap()); @@ -853,7 +853,7 @@ pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>( list_content = ctx.build_gep_and_load(list, &[zero_size_t, zero_32]).into_pointer_value(); - let i = generator.gen_store_target(ctx, target)?; + let i = generator.gen_store_target(ctx, target, None)?; ctx.builder.build_store(i, ctx.builder.build_int_sub(start, step, "start_init")); ctx.builder.build_unconditional_branch(test_bb); ctx.builder.position_at_end(test_bb); @@ -888,7 +888,7 @@ pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>( list = allocate_list(generator, ctx, elem_ty, length); list_content = ctx.build_gep_and_load(list, &[zero_size_t, zero_32]).into_pointer_value(); - let counter = generator.gen_var_alloc(ctx, size_t.into())?; + let counter = generator.gen_var_alloc(ctx, size_t.into(), None)?; // counter = -1 ctx.builder.build_store(counter, size_t.const_int(u64::max_value(), true)); ctx.builder.build_unconditional_branch(test_bb); diff --git a/nac3core/src/codegen/generator.rs b/nac3core/src/codegen/generator.rs index 22d0eac2..33542b6d 100644 --- a/nac3core/src/codegen/generator.rs +++ b/nac3core/src/codegen/generator.rs @@ -87,8 +87,9 @@ pub trait CodeGenerator { &mut self, ctx: &mut CodeGenContext<'ctx, 'a>, ty: BasicTypeEnum<'ctx>, + name: Option<&str>, ) -> Result, String> { - gen_var(ctx, ty) + gen_var(ctx, ty, name) } /// Return a pointer pointing to the target of the expression. @@ -96,11 +97,12 @@ pub trait CodeGenerator { &mut self, ctx: &mut CodeGenContext<'ctx, 'a>, pattern: &Expr>, + name: Option<&str>, ) -> Result, String> where Self: Sized, { - gen_store_target(self, ctx, pattern) + gen_store_target(self, ctx, pattern, name) } /// Generate code for an assignment expression. diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index c34f885a..947cb241 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(ctx, ptr_ty, name)?; 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!(), @@ -191,7 +193,7 @@ pub fn gen_assign<'ctx, 'a, G: CodeGenerator>( } } _ => { - let ptr = generator.gen_store_target(ctx, target)?; + let ptr = generator.gen_store_target(ctx, target, None)?; if let ExprKind::Name { id, .. } = &target.node { let (_, static_value, counter) = ctx.var_assignment.get_mut(id).unwrap(); *counter += 1; @@ -237,8 +239,8 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>( if ctx.unifier.unioned(iter.custom.unwrap(), ctx.primitives.range) { // setup let iter_val = iter_val.into_pointer_value(); - let i = generator.gen_var_alloc(ctx, int32.into())?; - let user_i = generator.gen_store_target(ctx, target)?; + let i = generator.gen_var_alloc(ctx, int32.into(), None)?; + let user_i = generator.gen_store_target(ctx, target, None)?; let (start, end, step) = destructure_range(ctx, iter_val); ctx.builder.build_store(i, ctx.builder.build_int_sub(start, step, "start_init")); ctx.builder.build_unconditional_branch(test_bb); @@ -270,7 +272,7 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>( ); ctx.builder.position_at_end(body_bb); } else { - let counter = generator.gen_var_alloc(ctx, size_t.into())?; + let counter = generator.gen_var_alloc(ctx, size_t.into(), None)?; // counter = -1 ctx.builder.build_store(counter, size_t.const_int(u64::max_value(), true)); let len = ctx @@ -655,7 +657,7 @@ pub fn gen_try<'ctx, 'a, G: CodeGenerator>( let mut final_data = None; let has_cleanup = !finalbody.is_empty(); if has_cleanup { - let final_state = generator.gen_var_alloc(ctx, ptr_type.into())?; + let final_state = generator.gen_var_alloc(ctx, ptr_type.into(), None)?; final_data = Some((final_state, Vec::new(), Vec::new())); if let Some((continue_target, break_target)) = ctx.loop_target { let break_proxy = ctx.ctx.append_basic_block(current_fun, "try.break"); @@ -821,7 +823,7 @@ pub fn gen_try<'ctx, 'a, G: CodeGenerator>( ctx.builder.position_at_end(handler_bb); if let Some(name) = name { let exn_ty = ctx.get_llvm_type(generator, type_.as_ref().unwrap().custom.unwrap()); - let exn_store = generator.gen_var_alloc(ctx, exn_ty)?; + let exn_store = generator.gen_var_alloc(ctx, exn_ty, None)?; ctx.var_assignment.insert(*name, (exn_store, None, 0)); ctx.builder.build_store(exn_store, exn.as_basic_value()); }