Skip to main content

lamina_codegen/riscv/
abi.rs

1use crate::abi::{Abi, common_call_stub, mangle_macos_name};
2use lamina_platform::TargetOperatingSystem;
3
4/// RISC-V ABI utilities
5pub struct RiscVAbi {
6    target_os: TargetOperatingSystem,
7}
8
9impl RiscVAbi {
10    pub fn new(target_os: TargetOperatingSystem) -> Self {
11        Self { target_os }
12    }
13
14    /// Get the appropriate function name with platform-specific prefix
15    pub fn mangle_function_name(&self, name: &str) -> String {
16        match self.target_os {
17            TargetOperatingSystem::MacOS => mangle_macos_name(name),
18            _ => name.to_string(),
19        }
20    }
21
22    /// Get the appropriate global declaration for main
23    pub fn get_main_global(&self) -> &'static str {
24        ".globl main"
25    }
26
27    /// Get the data section directive
28    pub fn get_data_section(&self) -> &'static str {
29        ".data"
30    }
31
32    /// Get the text section directive
33    pub fn get_text_section(&self) -> &'static str {
34        ".text"
35    }
36
37    /// RISC-V calling convention argument registers (first 8 arguments)
38    pub const ARG_REGISTERS: &'static [&'static str] =
39        &["a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7"];
40
41    /// Map well-known intrinsic/runtime names to platform symbol stubs
42    pub fn call_stub(&self, name: &str) -> Option<String> {
43        common_call_stub(name, self.target_os)
44    }
45}
46
47impl Abi for RiscVAbi {
48    fn target_os(&self) -> TargetOperatingSystem {
49        self.target_os
50    }
51
52    fn mangle_function_name(&self, name: &str) -> String {
53        RiscVAbi::mangle_function_name(self, name)
54    }
55
56    fn call_stub(&self, name: &str) -> Option<String> {
57        RiscVAbi::call_stub(self, name)
58    }
59}