use crate::abi::{Abi, common_call_stub, mangle_macos_name};
use lamina_platform::TargetOperatingSystem;
pub struct X86ABI {
target_os: TargetOperatingSystem,
}
impl X86ABI {
pub fn new(target_os: TargetOperatingSystem) -> Self {
Self { target_os }
}
pub fn mangle_function_name(&self, name: &str) -> String {
match self.target_os {
TargetOperatingSystem::MacOS => mangle_macos_name(name),
TargetOperatingSystem::Windows => {
if name == "main" {
"main".to_string()
} else {
name.to_string()
}
}
_ => name.to_string(),
}
}
pub fn get_main_global(&self) -> &'static str {
match self.target_os {
TargetOperatingSystem::Windows => ".globl main",
_ => ".globl main",
}
}
pub fn arg_registers(&self) -> &'static [&'static str] {
match self.target_os {
TargetOperatingSystem::Windows => &["rcx", "rdx", "r8", "r9"],
_ => &["rdi", "rsi", "rdx", "rcx", "r8", "r9"],
}
}
pub const ARG_REGISTERS: &'static [&'static str] = &["rdi", "rsi", "rdx", "rcx", "r8", "r9"];
pub const CALLER_SAVED_REGISTERS: &'static [&'static str] =
&["rax", "rcx", "rdx", "rsi", "rdi", "r8", "r9", "r10", "r11"];
pub const CALLEE_SAVED_REGISTERS: &'static [&'static str] =
&["rbx", "rbp", "r12", "r13", "r14", "r15"];
}
impl Abi for X86ABI {
fn target_os(&self) -> TargetOperatingSystem {
self.target_os
}
fn mangle_function_name(&self, name: &str) -> String {
X86ABI::mangle_function_name(self, name)
}
fn call_stub(&self, name: &str) -> Option<String> {
common_call_stub(name, self.target_os)
}
}