From 50a875ad210677fbe7f2a22911e291b0ec1cff17 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 28 Jul 2025 23:32:47 +0300 Subject: [PATCH] Add compilation of global values --- examples/macro_easy.reid | 5 +---- reid-llvm-lib/src/compile.rs | 42 +++++++++++++++++++++++++++++++----- reid-llvm-lib/src/lib.rs | 4 ++-- reid/src/codegen/mod.rs | 3 +++ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/examples/macro_easy.reid b/examples/macro_easy.reid index 6b63281..666e7e1 100644 --- a/examples/macro_easy.reid +++ b/examples/macro_easy.reid @@ -1,7 +1,4 @@ -import std::print; -import std::String; - fn main() -> u32 { // let message = String::from(include_bytes!("./macro_easy_file.txt")); return test_macro!(); -} \ No newline at end of file +} diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index 6fa3073..60fc909 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -27,7 +27,7 @@ use llvm_sys::{ use crate::{ CustomTypeKind, - builder::{TypeHolder, TypeValue}, + builder::{ConstantValue, GlobalValue, TypeHolder, TypeValue}, debug_information::*, util::{ErrorMessageHolder, MemoryBufferHolder, from_cstring, into_cstring}, }; @@ -193,6 +193,8 @@ pub struct LLVMModule<'a> { blocks: HashMap, values: HashMap, types: HashMap, + constants: HashMap, + globals: HashMap, debug: Option>, } @@ -237,6 +239,8 @@ impl ModuleHolder { // Compile the contents let mut types = HashMap::new(); + let mut constants = HashMap::new(); + let mut globals = HashMap::new(); let mut metadata = HashMap::new(); let mut scopes = HashMap::new(); let mut locations = HashMap::new(); @@ -320,6 +324,27 @@ impl ModuleHolder { 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(); for function in &self.functions { let func = function.compile_signature(context, module_ref, &types, &mut debug); @@ -358,6 +383,8 @@ impl ModuleHolder { types, blocks: HashMap::new(), values: HashMap::new(), + constants, + globals, debug, }; @@ -732,7 +759,7 @@ impl InstructionHolder { use super::Instr::*; match &self.data.kind { 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) => { let lhs_val = module.values.get(&lhs).unwrap().value_ref; let rhs_val = module.values.get(&rhs).unwrap().value_ref; @@ -1145,9 +1172,14 @@ impl CmpPredicate { } impl ConstValueKind { - fn as_llvm(&self, module: &LLVMModule) -> LLVMValueRef { + fn as_llvm( + &self, + context: LLVMContextRef, + builder: LLVMBuilderRef, + types: &HashMap, + ) -> LLVMValueRef { unsafe { - let t = self.get_type().as_llvm(module.context_ref, &module.types); + let t = self.get_type().as_llvm(context, &types); match self { ConstValueKind::Bool(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::U128(val) => LLVMConstInt(t, *val as u64, 1), 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::F32B(val) => LLVMConstReal(t, *val as f64), diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index c4c28b0..a926b2a 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -129,8 +129,8 @@ impl<'ctx> Module<'ctx> { unsafe { self.builder.build_constant(self.value, constant) } } - pub fn add_global(&self, name: String, constant: ConstantValue) -> GlobalValue { - unsafe { self.builder.add_global(self.value, name, constant) } + pub fn add_global>(&self, name: T, constant: ConstantValue) -> GlobalValue { + unsafe { self.builder.add_global(self.value, name.into(), constant) } } } diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index 6c1b5e7..a923050 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -105,6 +105,9 @@ impl mir::Module { let mut module = context.module(&self.name, self.is_main); 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 { module.create_debug_info(DebugFileData { name: path.file_name().unwrap().to_str().unwrap().to_owned(),