Namespace all functions, except those that are explicitly extern

This commit is contained in:
Sofia 2025-07-28 21:13:53 +03:00
parent 5026013df3
commit 13be3e9c02
5 changed files with 39 additions and 20 deletions

View File

@ -8,23 +8,16 @@ fn main() {
let module = context.module("test", true); 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 mut m_entry = main.block("entry");
let fibonacci = module.function( let fibonacci = module.function("fibonacci", None, Type::I32, vec![Type::I32], FunctionFlags::default());
"fibonacci",
Type::I32,
vec![Type::I32],
FunctionFlags::default(),
);
let arg = m_entry.build_named("const", Constant(I32(5))).unwrap(); let arg = m_entry.build_named("const", Constant(I32(5))).unwrap();
let fibonacci_call = m_entry let fibonacci_call = m_entry
.build_named("const", FunctionCall(fibonacci.value(), vec![arg])) .build_named("const", FunctionCall(fibonacci.value(), vec![arg]))
.unwrap(); .unwrap();
m_entry m_entry.terminate(TerminatorKind::Ret(fibonacci_call)).unwrap();
.terminate(TerminatorKind::Ret(fibonacci_call))
.unwrap();
let mut f_entry = fibonacci.block("entry"); let mut f_entry = fibonacci.block("entry");

View File

@ -590,7 +590,7 @@ impl FunctionHolder {
let name = if self.data.flags.is_main { let name = if self.data.flags.is_main {
c"main" c"main"
} else { } 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); 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 { let metadata = if let Some(debug) = debug {
if let Some(scope_value) = &self.debug_info { if let Some(scope_value) = &self.debug_info {
let scope_data = debug.info.get_scope_data(scope_value).unwrap(); let scope_data = debug.info.get_scope_data(scope_value).unwrap();
dbg!(&debug.info.get_scope());
let mangled_length_ptr = &mut 0; let mangled_length_ptr = &mut 0;
let mangled_name = LLVMGetValueName2(function_ref, mangled_length_ptr); let mangled_name = LLVMGetValueName2(function_ref, mangled_length_ptr);

View File

@ -66,7 +66,14 @@ pub struct Module<'ctx> {
} }
impl<'ctx> Module<'ctx> { impl<'ctx> Module<'ctx> {
pub fn function(&self, name: &str, ret: Type, params: Vec<Type>, flags: FunctionFlags) -> Function<'ctx> { pub fn function(
&self,
name: &str,
linkage: Option<String>,
ret: Type,
params: Vec<Type>,
flags: FunctionFlags,
) -> Function<'ctx> {
unsafe { unsafe {
Function { Function {
phantom: PhantomData, phantom: PhantomData,
@ -75,6 +82,7 @@ impl<'ctx> Module<'ctx> {
&self.value, &self.value,
FunctionData { FunctionData {
name: name.to_owned(), name: name.to_owned(),
linkage_name: linkage,
ret, ret,
params, params,
flags, flags,
@ -126,6 +134,7 @@ impl<'ctx> Drop for Module<'ctx> {
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, Hash)]
pub struct FunctionData { pub struct FunctionData {
name: String, name: String,
linkage_name: Option<String>,
ret: Type, ret: Type,
params: Vec<Type>, params: Vec<Type>,
flags: FunctionFlags, flags: FunctionFlags,

View File

@ -374,7 +374,7 @@ pub struct IntrinsicMalloc(TypeKind);
impl IntrinsicFunction for IntrinsicMalloc { impl IntrinsicFunction for IntrinsicMalloc {
fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result<StackValue, ErrorKind> { fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result<StackValue, ErrorKind> {
let amount = params.get(0).unwrap(); 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 let instr = scope
.block .block
.build(Instr::FunctionCall(function, vec![amount.instr()])) .build(Instr::FunctionCall(function, vec![amount.instr()]))

View File

@ -192,9 +192,7 @@ impl mir::Module {
FunctionDefinitionKind::Extern(i) => !i, FunctionDefinitionKind::Extern(i) => !i,
_ => false, _ => false,
}; };
let module_prefix = if is_true_extern { let module_prefix = if let Some(module) = function.source {
String::new()
} else if let Some(module) = function.source {
if module == self.module_id { if module == self.module_id {
format!("reid.{}.", self.name) format!("reid.{}.", self.name)
} else { } else {
@ -203,14 +201,17 @@ impl mir::Module {
} else { } else {
format!("reid.intrinsic.") 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, module_prefix,
function.linkage_name.clone().unwrap_or(function.name.clone()) function.linkage_name.clone().unwrap_or(function.name.clone())
); );
let func = match &function.kind { let func = match &function.kind {
mir::FunctionDefinitionKind::Local(_, _) => Some(module.function( mir::FunctionDefinitionKind::Local(_, _) => Some(module.function(
&linkage_name, &full_name,
None,
function.return_type.get_type(&type_values), function.return_type.get_type(&type_values),
param_types, param_types,
FunctionFlags { FunctionFlags {
@ -221,7 +222,20 @@ impl mir::Module {
}, },
)), )),
mir::FunctionDefinitionKind::Extern(imported) => Some(module.function( 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), function.return_type.get_type(&type_values),
param_types, param_types,
FunctionFlags { FunctionFlags {
@ -260,6 +274,7 @@ impl mir::Module {
let func = match &function.kind { let func = match &function.kind {
mir::FunctionDefinitionKind::Local(_, _) => Some(module.function( mir::FunctionDefinitionKind::Local(_, _) => Some(module.function(
&format!("{}.{}.{}", module_name, ty, function.name), &format!("{}.{}.{}", module_name, ty, function.name),
None,
function.return_type.get_type(&type_values), function.return_type.get_type(&type_values),
param_types, param_types,
FunctionFlags { FunctionFlags {
@ -271,6 +286,7 @@ impl mir::Module {
)), )),
mir::FunctionDefinitionKind::Extern(imported) => Some(module.function( mir::FunctionDefinitionKind::Extern(imported) => Some(module.function(
&function.linkage_name.clone().unwrap_or(function.name.clone()), &function.linkage_name.clone().unwrap_or(function.name.clone()),
None,
function.return_type.get_type(&type_values), function.return_type.get_type(&type_values),
param_types, param_types,
FunctionFlags { FunctionFlags {
@ -308,6 +324,7 @@ impl mir::Module {
FunctionDefinitionKind::Local(..) => { FunctionDefinitionKind::Local(..) => {
let ir_function = module.function( let ir_function = module.function(
&binop_fn_name, &binop_fn_name,
None,
binop.return_type.get_type(&type_values), binop.return_type.get_type(&type_values),
vec![binop.lhs.ty.get_type(&type_values), binop.rhs.ty.get_type(&type_values)], vec![binop.lhs.ty.get_type(&type_values), binop.rhs.ty.get_type(&type_values)],
FunctionFlags { FunctionFlags {
@ -373,6 +390,7 @@ impl mir::Module {
} }
FunctionDefinitionKind::Extern(imported) => ScopeFunctionKind::UserGenerated(module.function( FunctionDefinitionKind::Extern(imported) => ScopeFunctionKind::UserGenerated(module.function(
&binop_fn_name, &binop_fn_name,
None,
binop.return_type.get_type(&type_values), binop.return_type.get_type(&type_values),
vec![binop.lhs.ty.get_type(&type_values), binop.rhs.ty.get_type(&type_values)], vec![binop.lhs.ty.get_type(&type_values), binop.rhs.ty.get_type(&type_values)],
FunctionFlags { FunctionFlags {