Skip to main content

lamina_codegen/aarch64/
abi.rs

1//! AArch64 ABI utilities for symbol naming and calling conventions.
2
3use crate::abi::{Abi, common_call_stub, mangle_macos_name};
4use lamina_platform::TargetOperatingSystem;
5
6/// Platform-specific ABI utilities for AArch64 code generation.
7pub struct AArch64ABI {
8    target_os: TargetOperatingSystem,
9}
10
11impl AArch64ABI {
12    /// Creates a new ABI instance for the specified target OS.
13    pub fn new(target_os: TargetOperatingSystem) -> Self {
14        Self { target_os }
15    }
16
17    /// Returns the mangled function name with platform-specific prefix.
18    pub fn mangle_function_name(&self, name: &str) -> String {
19        match self.target_os {
20            TargetOperatingSystem::MacOS => mangle_macos_name(name),
21            _ => name.to_string(),
22        }
23    }
24
25    /// Returns the global declaration directive for a function.
26    pub fn get_global_directive(&self, func_name: &str) -> Option<String> {
27        match self.target_os {
28            TargetOperatingSystem::MacOS => Some(format!(".globl _{}", func_name)),
29            _ => Some(format!(".globl {}", func_name)),
30        }
31    }
32
33    /// Maps well-known intrinsic/runtime names to platform symbol stubs.
34    pub fn call_stub(&self, name: &str) -> Option<String> {
35        common_call_stub(name, self.target_os)
36    }
37
38    /// AArch64 AAPCS64 calling convention argument registers (first 8 arguments).
39    pub const ARG_REGISTERS: &'static [&'static str] =
40        &["x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7"];
41
42    /// Caller-saved registers that must be preserved by the caller if live across function calls.
43    pub const CALLER_SAVED_REGISTERS: &'static [&'static str] = &[
44        "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13",
45        "x14", "x15", "x16", "x17",
46    ];
47
48    /// Callee-saved registers that are preserved by called functions.
49    pub const CALLEE_SAVED_REGISTERS: &'static [&'static str] = &[
50        "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "x29", "x30",
51    ];
52
53    /// Returns the target operating system for this ABI instance.
54    pub fn target_os(&self) -> TargetOperatingSystem {
55        self.target_os
56    }
57}
58
59impl Abi for AArch64ABI {
60    fn target_os(&self) -> TargetOperatingSystem {
61        self.target_os
62    }
63
64    fn mangle_function_name(&self, name: &str) -> String {
65        AArch64ABI::mangle_function_name(self, name)
66    }
67
68    fn call_stub(&self, name: &str) -> Option<String> {
69        AArch64ABI::call_stub(self, name)
70    }
71}