From 13be3e9c02fa8af8b15c2fba62ab73f378f3f901 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 28 Jul 2025 21:13:53 +0300 Subject: [PATCH] Namespace all functions, except those that are explicitly extern --- reid-llvm-lib/examples/libtest.rs | 13 +++---------- reid-llvm-lib/src/compile.rs | 3 +-- reid-llvm-lib/src/lib.rs | 11 ++++++++++- reid/src/codegen/intrinsics.rs | 2 +- reid/src/codegen/mod.rs | 30 ++++++++++++++++++++++++------ 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/reid-llvm-lib/examples/libtest.rs b/reid-llvm-lib/examples/libtest.rs index 32e4584..5caf56a 100644 --- a/reid-llvm-lib/examples/libtest.rs +++ b/reid-llvm-lib/examples/libtest.rs @@ -8,23 +8,16 @@ fn main() { let module = context.module("test", true); - let main = module.function("main", Type::I32, Vec::new(), FunctionFlags::default()); + let main = module.function("main", None, Type::I32, Vec::new(), FunctionFlags::default()); let mut m_entry = main.block("entry"); - let fibonacci = module.function( - "fibonacci", - Type::I32, - vec![Type::I32], - FunctionFlags::default(), - ); + let fibonacci = module.function("fibonacci", None, Type::I32, vec![Type::I32], FunctionFlags::default()); let arg = m_entry.build_named("const", Constant(I32(5))).unwrap(); let fibonacci_call = m_entry .build_named("const", FunctionCall(fibonacci.value(), vec![arg])) .unwrap(); - m_entry - .terminate(TerminatorKind::Ret(fibonacci_call)) - .unwrap(); + m_entry.terminate(TerminatorKind::Ret(fibonacci_call)).unwrap(); let mut f_entry = fibonacci.block("entry"); diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index 2f315cd..a9c0c64 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -590,7 +590,7 @@ impl FunctionHolder { let name = if self.data.flags.is_main { c"main" } else { - &into_cstring(&self.data.name) + &into_cstring(&self.data.linkage_name.clone().unwrap_or(self.data.name.clone())) }; let fn_type = LLVMFunctionType(ret_type, param_ptr, param_len as u32, 0); @@ -605,7 +605,6 @@ impl FunctionHolder { let metadata = if let Some(debug) = debug { if let Some(scope_value) = &self.debug_info { let scope_data = debug.info.get_scope_data(scope_value).unwrap(); - dbg!(&debug.info.get_scope()); let mangled_length_ptr = &mut 0; let mangled_name = LLVMGetValueName2(function_ref, mangled_length_ptr); diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index 4efd4cd..3fb5d35 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -66,7 +66,14 @@ pub struct Module<'ctx> { } impl<'ctx> Module<'ctx> { - pub fn function(&self, name: &str, ret: Type, params: Vec, flags: FunctionFlags) -> Function<'ctx> { + pub fn function( + &self, + name: &str, + linkage: Option, + ret: Type, + params: Vec, + flags: FunctionFlags, + ) -> Function<'ctx> { unsafe { Function { phantom: PhantomData, @@ -75,6 +82,7 @@ impl<'ctx> Module<'ctx> { &self.value, FunctionData { name: name.to_owned(), + linkage_name: linkage, ret, params, flags, @@ -126,6 +134,7 @@ impl<'ctx> Drop for Module<'ctx> { #[derive(Debug, Clone, Hash)] pub struct FunctionData { name: String, + linkage_name: Option, ret: Type, params: Vec, flags: FunctionFlags, diff --git a/reid/src/codegen/intrinsics.rs b/reid/src/codegen/intrinsics.rs index 6cb2cab..e639df4 100644 --- a/reid/src/codegen/intrinsics.rs +++ b/reid/src/codegen/intrinsics.rs @@ -374,7 +374,7 @@ pub struct IntrinsicMalloc(TypeKind); impl IntrinsicFunction for IntrinsicMalloc { fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result { let amount = params.get(0).unwrap(); - let function = scope.block.find_function(&"malloc".to_owned()).unwrap(); + let function = scope.block.find_function(&MALLOC_IDENT.to_owned()).unwrap(); let instr = scope .block .build(Instr::FunctionCall(function, vec![amount.instr()])) diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index 583a371..eb77b57 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -192,9 +192,7 @@ impl mir::Module { FunctionDefinitionKind::Extern(i) => !i, _ => false, }; - let module_prefix = if is_true_extern { - String::new() - } else if let Some(module) = function.source { + let module_prefix = if let Some(module) = function.source { if module == self.module_id { format!("reid.{}.", self.name) } else { @@ -203,14 +201,17 @@ impl mir::Module { } else { format!("reid.intrinsic.") }; - let linkage_name = format!( + let linkage_name = function.linkage_name.clone().unwrap_or(function.name.clone()); + let full_name = format!( "{}{}", module_prefix, function.linkage_name.clone().unwrap_or(function.name.clone()) ); + let func = match &function.kind { mir::FunctionDefinitionKind::Local(_, _) => Some(module.function( - &linkage_name, + &full_name, + None, function.return_type.get_type(&type_values), param_types, FunctionFlags { @@ -221,7 +222,20 @@ impl mir::Module { }, )), mir::FunctionDefinitionKind::Extern(imported) => Some(module.function( - &linkage_name, + if function.source == None { + &function.name + } else { + &full_name + }, + if function.source == None { + Some(function.linkage_name.clone().unwrap()) + } else { + if !*imported { + Some(linkage_name.clone()) + } else { + None + } + }, function.return_type.get_type(&type_values), param_types, FunctionFlags { @@ -260,6 +274,7 @@ impl mir::Module { let func = match &function.kind { mir::FunctionDefinitionKind::Local(_, _) => Some(module.function( &format!("{}.{}.{}", module_name, ty, function.name), + None, function.return_type.get_type(&type_values), param_types, FunctionFlags { @@ -271,6 +286,7 @@ impl mir::Module { )), mir::FunctionDefinitionKind::Extern(imported) => Some(module.function( &function.linkage_name.clone().unwrap_or(function.name.clone()), + None, function.return_type.get_type(&type_values), param_types, FunctionFlags { @@ -308,6 +324,7 @@ impl mir::Module { FunctionDefinitionKind::Local(..) => { let ir_function = module.function( &binop_fn_name, + None, binop.return_type.get_type(&type_values), vec![binop.lhs.ty.get_type(&type_values), binop.rhs.ty.get_type(&type_values)], FunctionFlags { @@ -373,6 +390,7 @@ impl mir::Module { } FunctionDefinitionKind::Extern(imported) => ScopeFunctionKind::UserGenerated(module.function( &binop_fn_name, + None, binop.return_type.get_type(&type_values), vec![binop.lhs.ty.get_type(&type_values), binop.rhs.ty.get_type(&type_values)], FunctionFlags {