diff --git a/examples/ptr.reid b/examples/ptr.reid index ef9f616..f4be760 100755 --- a/examples/ptr.reid +++ b/examples/ptr.reid @@ -1,9 +1,7 @@ // Arithmetic, function calls and imports! -import std::allocate; - fn main() -> u8 { - let mut ptr = allocate(4); + let mut ptr = u8::alloca(4); ptr[0] = 5; diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index 25a88a9..2f315cd 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -587,9 +587,15 @@ impl FunctionHolder { let param_ptr = param_types.as_mut_ptr(); let param_len = param_types.len(); + let name = if self.data.flags.is_main { + c"main" + } else { + &into_cstring(&self.data.name) + }; + let fn_type = LLVMFunctionType(ret_type, param_ptr, param_len as u32, 0); - let function_ref = LLVMAddFunction(module_ref, into_cstring(&self.data.name).as_ptr(), fn_type); + let function_ref = LLVMAddFunction(module_ref, name.as_ptr(), fn_type); if self.data.flags.inline { let attribute = LLVMCreateEnumAttribute(context.context_ref, LLVMEnumAttribute::AlwaysInline as u32, 0); diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 4b1caf8..6b86dd4 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -55,6 +55,7 @@ impl ast::Module { }) .collect(), kind: mir::FunctionDefinitionKind::Extern(false), + source: Some(module_id), }; functions.push(def); } @@ -176,6 +177,7 @@ impl ast::FunctionDefinition { .unwrap_or(mir::TypeKind::Void), parameters: params, kind: mir::FunctionDefinitionKind::Local(block.into_mir(module_id), (range).as_meta(module_id)), + source: Some(module_id), } } } diff --git a/reid/src/codegen/intrinsics.rs b/reid/src/codegen/intrinsics.rs index f054a76..b430664 100644 --- a/reid/src/codegen/intrinsics.rs +++ b/reid/src/codegen/intrinsics.rs @@ -49,6 +49,7 @@ pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option Some(FunctionDefinition { name: "alloca".to_owned(), @@ -62,6 +63,7 @@ pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option Some(FunctionDefinition { name: "null".to_owned(), @@ -71,6 +73,7 @@ pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option None, } diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index 7204afc..583a371 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -68,6 +68,7 @@ impl mir::Context { #[derive(Clone)] struct ModuleCodegen<'ctx> { + name: String, module: Module<'ctx>, } @@ -186,9 +187,30 @@ impl mir::Module { .collect(); let is_main = self.is_main && function.name == "main"; + + let is_true_extern = match function.kind { + FunctionDefinitionKind::Extern(i) => !i, + _ => false, + }; + let module_prefix = if is_true_extern { + String::new() + } else if let Some(module) = function.source { + if module == self.module_id { + format!("reid.{}.", self.name) + } else { + format!("reid.{}.", modules.get(&module).unwrap().name) + } + } else { + format!("reid.intrinsic.") + }; + let linkage_name = format!( + "{}{}", + module_prefix, + function.linkage_name.clone().unwrap_or(function.name.clone()) + ); let func = match &function.kind { mir::FunctionDefinitionKind::Local(_, _) => Some(module.function( - &function.linkage_name.clone().unwrap_or(function.name.clone()), + &linkage_name, function.return_type.get_type(&type_values), param_types, FunctionFlags { @@ -199,7 +221,7 @@ impl mir::Module { }, )), mir::FunctionDefinitionKind::Extern(imported) => Some(module.function( - &function.linkage_name.clone().unwrap_or(function.name.clone()), + &linkage_name, function.return_type.get_type(&type_values), param_types, FunctionFlags { @@ -226,9 +248,18 @@ impl mir::Module { .collect(); let is_main = self.is_main && function.name == "main"; + let module_name = if let Some(module) = function.source { + if module == self.module_id { + format!("reid.{}", self.name) + } else { + format!("reid.{}", modules.get(&module).unwrap().name) + } + } else { + format!("reid.intrinsic") + }; let func = match &function.kind { mir::FunctionDefinitionKind::Local(_, _) => Some(module.function( - &format!("{}::{}", ty, function.name), + &format!("{}.{}.{}", module_name, ty, function.name), function.return_type.get_type(&type_values), param_types, FunctionFlags { @@ -475,7 +506,10 @@ impl mir::Module { } } - Ok(ModuleCodegen { module }) + Ok(ModuleCodegen { + name: self.name.clone(), + module, + }) } } diff --git a/reid/src/mir/linker.rs b/reid/src/mir/linker.rs index 2bfce10..8016110 100644 --- a/reid/src/mir/linker.rs +++ b/reid/src/mir/linker.rs @@ -186,6 +186,7 @@ impl<'map> Pass for LinkerPass<'map> { .borrow_mut(); let import_name = unsafe { path.get_unchecked(1) }; + let import_id = imported.module_id; let mut imported_types = Vec::new(); @@ -234,6 +235,7 @@ impl<'map> Pass for LinkerPass<'map> { return_type, parameters: param_tys, kind: super::FunctionDefinitionKind::Extern(true), + source: Some(imported.module_id), }); } else if let Some(ty) = imported.typedefs.iter_mut().find(|f| f.name == *import_name) { let external_key = CustomTypeKey(ty.name.clone(), ty.source_module); @@ -319,12 +321,13 @@ impl<'map> Pass for LinkerPass<'map> { ty.clone(), FunctionDefinition { name: func_name.clone(), - linkage_name: Some(format!("{}::{}", ty, func_name)), + linkage_name: Some(format!("{}.{}", ty, func_name)), is_pub: false, is_imported: false, return_type, parameters: param_tys, kind: super::FunctionDefinitionKind::Extern(true), + source: Some(import_id), }, )); } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 198acf7..7867a15 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -298,6 +298,7 @@ pub struct FunctionDefinition { pub return_type: TypeKind, pub parameters: Vec, pub kind: FunctionDefinitionKind, + pub source: Option, } #[derive(Debug, Clone, PartialEq, PartialOrd)]