Add compilation of global values
This commit is contained in:
parent
30257e1a2b
commit
50a875ad21
@ -1,7 +1,4 @@
|
|||||||
import std::print;
|
|
||||||
import std::String;
|
|
||||||
|
|
||||||
fn main() -> u32 {
|
fn main() -> u32 {
|
||||||
// let message = String::from(include_bytes!("./macro_easy_file.txt"));
|
// let message = String::from(include_bytes!("./macro_easy_file.txt"));
|
||||||
return test_macro!();
|
return test_macro!();
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ use llvm_sys::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
CustomTypeKind,
|
CustomTypeKind,
|
||||||
builder::{TypeHolder, TypeValue},
|
builder::{ConstantValue, GlobalValue, TypeHolder, TypeValue},
|
||||||
debug_information::*,
|
debug_information::*,
|
||||||
util::{ErrorMessageHolder, MemoryBufferHolder, from_cstring, into_cstring},
|
util::{ErrorMessageHolder, MemoryBufferHolder, from_cstring, into_cstring},
|
||||||
};
|
};
|
||||||
@ -193,6 +193,8 @@ pub struct LLVMModule<'a> {
|
|||||||
blocks: HashMap<BlockValue, LLVMBasicBlockRef>,
|
blocks: HashMap<BlockValue, LLVMBasicBlockRef>,
|
||||||
values: HashMap<InstructionValue, LLVMValue>,
|
values: HashMap<InstructionValue, LLVMValue>,
|
||||||
types: HashMap<TypeValue, LLVMTypeRef>,
|
types: HashMap<TypeValue, LLVMTypeRef>,
|
||||||
|
constants: HashMap<ConstantValue, LLVMValue>,
|
||||||
|
globals: HashMap<GlobalValue, LLVMValueRef>,
|
||||||
debug: Option<LLVMDebugInformation<'a>>,
|
debug: Option<LLVMDebugInformation<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,6 +239,8 @@ impl ModuleHolder {
|
|||||||
// Compile the contents
|
// Compile the contents
|
||||||
|
|
||||||
let mut types = HashMap::new();
|
let mut types = HashMap::new();
|
||||||
|
let mut constants = HashMap::new();
|
||||||
|
let mut globals = HashMap::new();
|
||||||
let mut metadata = HashMap::new();
|
let mut metadata = HashMap::new();
|
||||||
let mut scopes = HashMap::new();
|
let mut scopes = HashMap::new();
|
||||||
let mut locations = HashMap::new();
|
let mut locations = HashMap::new();
|
||||||
@ -320,6 +324,27 @@ impl ModuleHolder {
|
|||||||
types.insert(ty.value, ty.compile_type(context, &types));
|
types.insert(ty.value, ty.compile_type(context, &types));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for constant in &self.constants {
|
||||||
|
constants.insert(
|
||||||
|
constant.value,
|
||||||
|
LLVMValue {
|
||||||
|
ty: constant.kind.get_type(),
|
||||||
|
value_ref: constant.kind.as_llvm(context.context_ref, context.builder_ref, &types),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for global in &self.globals {
|
||||||
|
let initializer = constants.get(&global.initializer).expect("No initializer?");
|
||||||
|
let global_value = LLVMAddGlobal(
|
||||||
|
module_ref,
|
||||||
|
initializer.ty.as_llvm(context.context_ref, &types),
|
||||||
|
into_cstring(global.name.clone()).as_ptr(),
|
||||||
|
);
|
||||||
|
LLVMSetInitializer(global_value, initializer.value_ref);
|
||||||
|
globals.insert(global.value, global_value);
|
||||||
|
}
|
||||||
|
|
||||||
let mut functions = HashMap::new();
|
let mut functions = HashMap::new();
|
||||||
for function in &self.functions {
|
for function in &self.functions {
|
||||||
let func = function.compile_signature(context, module_ref, &types, &mut debug);
|
let func = function.compile_signature(context, module_ref, &types, &mut debug);
|
||||||
@ -358,6 +383,8 @@ impl ModuleHolder {
|
|||||||
types,
|
types,
|
||||||
blocks: HashMap::new(),
|
blocks: HashMap::new(),
|
||||||
values: HashMap::new(),
|
values: HashMap::new(),
|
||||||
|
constants,
|
||||||
|
globals,
|
||||||
debug,
|
debug,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -732,7 +759,7 @@ impl InstructionHolder {
|
|||||||
use super::Instr::*;
|
use super::Instr::*;
|
||||||
match &self.data.kind {
|
match &self.data.kind {
|
||||||
Param(nth) => LLVMGetParam(function.value_ref, *nth as u32),
|
Param(nth) => LLVMGetParam(function.value_ref, *nth as u32),
|
||||||
Constant(val) => val.as_llvm(module),
|
Constant(val) => val.as_llvm(module.context_ref, module.builder_ref, &module.types),
|
||||||
Add(lhs, rhs) => {
|
Add(lhs, rhs) => {
|
||||||
let lhs_val = module.values.get(&lhs).unwrap().value_ref;
|
let lhs_val = module.values.get(&lhs).unwrap().value_ref;
|
||||||
let rhs_val = module.values.get(&rhs).unwrap().value_ref;
|
let rhs_val = module.values.get(&rhs).unwrap().value_ref;
|
||||||
@ -1145,9 +1172,14 @@ impl CmpPredicate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ConstValueKind {
|
impl ConstValueKind {
|
||||||
fn as_llvm(&self, module: &LLVMModule) -> LLVMValueRef {
|
fn as_llvm(
|
||||||
|
&self,
|
||||||
|
context: LLVMContextRef,
|
||||||
|
builder: LLVMBuilderRef,
|
||||||
|
types: &HashMap<TypeValue, LLVMTypeRef>,
|
||||||
|
) -> LLVMValueRef {
|
||||||
unsafe {
|
unsafe {
|
||||||
let t = self.get_type().as_llvm(module.context_ref, &module.types);
|
let t = self.get_type().as_llvm(context, &types);
|
||||||
match self {
|
match self {
|
||||||
ConstValueKind::Bool(val) => LLVMConstInt(t, *val as u64, 1),
|
ConstValueKind::Bool(val) => LLVMConstInt(t, *val as u64, 1),
|
||||||
ConstValueKind::I8(val) => LLVMConstInt(t, *val as u64, 1),
|
ConstValueKind::I8(val) => LLVMConstInt(t, *val as u64, 1),
|
||||||
@ -1161,7 +1193,7 @@ impl ConstValueKind {
|
|||||||
ConstValueKind::U64(val) => LLVMConstInt(t, *val as u64, 1),
|
ConstValueKind::U64(val) => LLVMConstInt(t, *val as u64, 1),
|
||||||
ConstValueKind::U128(val) => LLVMConstInt(t, *val as u64, 1),
|
ConstValueKind::U128(val) => LLVMConstInt(t, *val as u64, 1),
|
||||||
ConstValueKind::Str(val) => {
|
ConstValueKind::Str(val) => {
|
||||||
LLVMBuildGlobalString(module.builder_ref, into_cstring(val).as_ptr(), c"string".as_ptr())
|
LLVMBuildGlobalString(builder, into_cstring(val).as_ptr(), c"string".as_ptr())
|
||||||
}
|
}
|
||||||
ConstValueKind::F16(val) => LLVMConstReal(t, *val as f64),
|
ConstValueKind::F16(val) => LLVMConstReal(t, *val as f64),
|
||||||
ConstValueKind::F32B(val) => LLVMConstReal(t, *val as f64),
|
ConstValueKind::F32B(val) => LLVMConstReal(t, *val as f64),
|
||||||
|
@ -129,8 +129,8 @@ impl<'ctx> Module<'ctx> {
|
|||||||
unsafe { self.builder.build_constant(self.value, constant) }
|
unsafe { self.builder.build_constant(self.value, constant) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_global(&self, name: String, constant: ConstantValue) -> GlobalValue {
|
pub fn add_global<T: Into<String>>(&self, name: T, constant: ConstantValue) -> GlobalValue {
|
||||||
unsafe { self.builder.add_global(self.value, name, constant) }
|
unsafe { self.builder.add_global(self.value, name.into(), constant) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,6 +105,9 @@ impl mir::Module {
|
|||||||
let mut module = context.module(&self.name, self.is_main);
|
let mut module = context.module(&self.name, self.is_main);
|
||||||
let tokens = &self.tokens;
|
let tokens = &self.tokens;
|
||||||
|
|
||||||
|
let const_value = module.add_constant(ConstValueKind::I128(132));
|
||||||
|
module.add_global("some_global", const_value);
|
||||||
|
|
||||||
let (debug, compile_unit) = if let Some(path) = &self.path {
|
let (debug, compile_unit) = if let Some(path) = &self.path {
|
||||||
module.create_debug_info(DebugFileData {
|
module.create_debug_info(DebugFileData {
|
||||||
name: path.file_name().unwrap().to_str().unwrap().to_owned(),
|
name: path.file_name().unwrap().to_str().unwrap().to_owned(),
|
||||||
|
Loading…
Reference in New Issue
Block a user