From 4db871c244dff936ac3554b851d8069c695d6f36 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 9 Aug 2021 16:37:28 +0800 Subject: [PATCH] put alloca in init block --- nac3core/src/codegen/stmt.rs | 15 +++++++-------- nac3core/src/top_level.rs | 2 ++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index b66825cd..f2d7dd33 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -1,17 +1,16 @@ -use std::convert::TryInto; - use crate::{top_level::CodeGenContext, typecheck::typedef::Type}; -use inkwell::{ - types::BasicTypeEnum, - values::{BasicValueEnum, PointerValue}, -}; +use inkwell::values::{BasicValueEnum, PointerValue}; use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind}; impl<'ctx> CodeGenContext<'ctx> { fn gen_var(&mut self, ty: Type) -> PointerValue<'ctx> { - // should we build the alloca in an initial block? + // put the alloca in init block + let current = self.builder.get_insert_block().unwrap(); + self.builder.position_at_end(self.init_bb); let ty = self.get_llvm_type(ty); - self.builder.build_alloca(ty, "tmp") + let ptr = self.builder.build_alloca(ty, "tmp"); + self.builder.position_at_end(current); + ptr } fn parse_pattern(&mut self, pattern: &Expr>) -> PointerValue<'ctx> { diff --git a/nac3core/src/top_level.rs b/nac3core/src/top_level.rs index cd4af4d8..d5a3276c 100644 --- a/nac3core/src/top_level.rs +++ b/nac3core/src/top_level.rs @@ -68,6 +68,8 @@ pub struct CodeGenContext<'ctx> { pub var_assignment: HashMap>, pub type_cache: HashMap>, pub primitives: PrimitiveStore, + // stores the alloca for variables + pub init_bb: BasicBlock<'ctx>, // where continue and break should go to respectively // the first one is the test_bb, and the second one is bb after the loop pub loop_bb: Option<(BasicBlock<'ctx>, BasicBlock<'ctx>)>,