Mangle function names, except for user defined externs

This commit is contained in:
Sofia 2025-07-28 20:25:36 +03:00
parent e14efa2ea7
commit beaba4e7de
7 changed files with 56 additions and 9 deletions

View File

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

View File

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

View File

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

View File

@ -49,6 +49,7 @@ pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option<FunctionDef
return_type: TypeKind::U64,
parameters: Vec::new(),
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSizeOf(ty.clone()))),
source: None,
}),
"alloca" => Some(FunctionDefinition {
name: "alloca".to_owned(),
@ -62,6 +63,7 @@ pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option<FunctionDef
meta: Default::default(),
}],
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicAlloca(ty.clone()))),
source: None,
}),
"null" => Some(FunctionDefinition {
name: "null".to_owned(),
@ -71,6 +73,7 @@ pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option<FunctionDef
return_type: TypeKind::UserPtr(Box::new(ty.clone())),
parameters: Vec::new(),
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicNullPtr(ty.clone()))),
source: None,
}),
_ => None,
}

View File

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

View File

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

View File

@ -298,6 +298,7 @@ pub struct FunctionDefinition {
pub return_type: TypeKind,
pub parameters: Vec<FunctionParam>,
pub kind: FunctionDefinitionKind,
pub source: Option<SourceModuleId>,
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]