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    /// Get the format string for printing integers
38    pub fn get_print_format(&self) -> &'static str {
39        match self.target_os {
40            TargetOperatingSystem::MacOS => "__mir_fmt_int: .asciz \"%lld\\n\"",
41            _ => ".L_mir_fmt_int: .string \"%lld\\n\"",
42        }
43    }
44
45    /// RISC-V calling convention argument registers (first 8 arguments)
46    pub const ARG_REGISTERS: &'static [&'static str] =
47        &["a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7"];
48
49    /// Map well-known intrinsic/runtime names to platform symbol stubs
50    pub fn call_stub(&self, name: &str) -> Option<String> {
51        common_call_stub(name, self.target_os)
52    }
53}
54
55impl Abi for RiscVAbi {
56    fn target_os(&self) -> TargetOperatingSystem {
57        self.target_os
58    }
59
60    fn mangle_function_name(&self, name: &str) -> String {
61        RiscVAbi::mangle_function_name(self, name)
62    }
63
64    fn call_stub(&self, name: &str) -> Option<String> {
65        RiscVAbi::call_stub(self, name)
66    }
67}