use lamina_platform::TargetOperatingSystem;
pub trait Abi {
fn target_os(&self) -> TargetOperatingSystem;
fn mangle_function_name(&self, name: &str) -> String;
fn call_stub(&self, name: &str) -> Option<String> {
let _ = name;
None
}
}
pub fn mangle_macos_name(name: &str) -> String {
if name == "main" {
"_main".to_string()
} else {
format!("_{}", name)
}
}
pub fn get_printf_symbol(target_os: TargetOperatingSystem) -> &'static str {
match target_os {
TargetOperatingSystem::MacOS => "_printf",
_ => "printf",
}
}
pub fn get_malloc_symbol(target_os: TargetOperatingSystem) -> &'static str {
match target_os {
TargetOperatingSystem::MacOS => "_malloc",
_ => "malloc",
}
}
pub fn get_free_symbol(target_os: TargetOperatingSystem) -> &'static str {
match target_os {
TargetOperatingSystem::MacOS => "_free",
_ => "free",
}
}
pub fn common_call_stub(name: &str, target_os: TargetOperatingSystem) -> Option<String> {
match name {
"print" => Some(get_printf_symbol(target_os).to_string()),
"malloc" => Some(get_malloc_symbol(target_os).to_string()),
"dealloc" => Some(get_free_symbol(target_os).to_string()),
_ => None,
}
}