diff --git a/reid-llvm-lib/src/builder.rs b/reid-llvm-lib/src/builder.rs index ed6d2f3..3be565b 100644 --- a/reid-llvm-lib/src/builder.rs +++ b/reid-llvm-lib/src/builder.rs @@ -28,10 +28,10 @@ pub struct BlockValue(pub(crate) FunctionValue, pub(crate) usize); pub struct InstructionValue(pub(crate) BlockValue, pub(crate) usize); #[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)] -pub struct ConstantValue(pub(crate) usize); +pub struct ConstantValue(pub(crate) ModuleValue, pub(crate) usize); -#[derive(Clone, Hash, Copy, PartialEq, Eq)] -pub struct GlobalValue(pub(crate) usize); +#[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)] +pub struct GlobalValue(pub(crate) ModuleValue, pub(crate) usize); #[derive(Clone)] pub struct ModuleHolder { @@ -194,7 +194,7 @@ impl Builder { unsafe { let mut modules = self.modules.borrow_mut(); let module = modules.get_unchecked_mut(module.0); - let value = ConstantValue(module.constants.len()); + let value = ConstantValue(module.value, module.constants.len()); module.constants.push(ConstantValueHolder { value, kind }); value } @@ -209,7 +209,7 @@ impl Builder { unsafe { let mut modules = self.modules.borrow_mut(); let module = modules.get_unchecked_mut(module.0); - let value = GlobalValue(module.globals.len()); + let value = GlobalValue(module.value, module.globals.len()); module.globals.push(GlobalValueHolder { value, name, @@ -383,6 +383,24 @@ impl Builder { self.modules.clone() } + pub(crate) fn get_global_initializer(&self, value: GlobalValue) -> ConstantValue { + unsafe { + let modules = self.modules.borrow(); + let module = modules.get_unchecked(value.0.0); + let global = module.globals.get_unchecked(value.1); + global.initializer + } + } + + pub(crate) fn get_const_kind(&self, value: ConstantValue) -> ConstValueKind { + unsafe { + let modules = self.modules.borrow(); + let module = modules.get_unchecked(value.0.0); + let constant = module.constants.get_unchecked(value.1); + constant.kind.clone() + } + } + pub fn check_instruction(&self, instruction: &InstructionValue) -> CompileResult<()> { unsafe { match self.instr_data(&instruction).kind { @@ -617,6 +635,7 @@ impl Builder { Err(ErrorKind::Null) } } + Instr::GetGlobal(_) => Ok(()), } } } @@ -768,6 +787,11 @@ impl InstructionValue { ShiftRightLogical(lhs, _) => lhs.get_type(builder), ShiftRightArithmetic(lhs, _) => lhs.get_type(builder), ShiftLeft(lhs, _) => lhs.get_type(builder), + GetGlobal(global_value) => { + let constant = builder.get_global_initializer(*global_value); + let kind = builder.get_const_kind(constant); + Ok(kind.get_type()) + } } } } diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index f3ccbf2..c0052d0 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -1052,6 +1052,7 @@ impl InstructionHolder { let rhs_val = module.values.get(&rhs).unwrap().value_ref; LLVMBuildShl(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) } + GetGlobal(global_value) => module.globals.get(global_value).unwrap().clone(), } }; if let Some(record) = &self.record { diff --git a/reid-llvm-lib/src/fmt.rs b/reid-llvm-lib/src/fmt.rs index 4bb356f..0c9d585 100644 --- a/reid-llvm-lib/src/fmt.rs +++ b/reid-llvm-lib/src/fmt.rs @@ -392,6 +392,7 @@ impl Debug for Instr { Instr::ShiftRightLogical(lhs, rhs) => fmt_binop(f, lhs, &">>l", rhs), Instr::ShiftRightArithmetic(lhs, rhs) => fmt_binop(f, lhs, &">>a", rhs), Instr::ShiftLeft(lhs, rhs) => fmt_binop(f, lhs, &"<<", rhs), + Instr::GetGlobal(global_value) => write!(f, "global {:?}", global_value), } } } diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index 665feec..a0a7b8d 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -276,6 +276,7 @@ impl Instr { Instr::ShiftRightLogical(..) => "lshr", Instr::ShiftRightArithmetic(..) => "ashr", Instr::ShiftLeft(..) => "shl", + Instr::GetGlobal(global_value) => todo!(), } } } @@ -373,6 +374,7 @@ pub enum CmpPredicate { pub enum Instr { Param(usize), Constant(ConstValueKind), + GetGlobal(GlobalValue), /// Add two integers Add(InstructionValue, InstructionValue),