Add GetGlobal "instruction"

This commit is contained in:
Sofia 2025-07-29 00:29:04 +03:00
parent 140d963d9b
commit ebe7fc8d75
4 changed files with 33 additions and 5 deletions

View File

@ -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())
}
}
}
}

View File

@ -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 {

View File

@ -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),
}
}
}

View File

@ -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),