lamina_codegen/wasm/
abi.rs1use crate::abi::Abi;
7use lamina_mir::MirType;
8use lamina_platform::TargetOperatingSystem;
9
10pub struct WasmABI {
12 target_os: TargetOperatingSystem,
13}
14
15impl WasmABI {
16 pub fn new(target_os: TargetOperatingSystem) -> Self {
18 Self { target_os }
19 }
20
21 pub fn mangle_function_name(&self, name: &str) -> String {
25 name.to_string()
26 }
27
28 pub fn get_runtime_imports(&self) -> &'static [(&'static str, &'static str)] {
32 &[
33 (
34 "$writebyte",
35 "(import \"env\" \"writebyte\" (func $writebyte (param i64) (result i64)))",
36 ),
37 (
38 "$readbyte",
39 "(import \"env\" \"readbyte\" (func $readbyte (result i64)))",
40 ),
41 (
42 "$malloc",
43 "(import \"env\" \"malloc\" (func $malloc (param i64) (result i64)))",
44 ),
45 (
46 "$free",
47 "(import \"env\" \"free\" (func $free (param i64)))",
48 ),
49 (
50 "$write",
51 "(import \"env\" \"write\" (func $write (param i64 i64) (result i64)))",
52 ),
53 (
54 "$read",
55 "(import \"env\" \"read\" (func $read (param i64 i64) (result i64)))",
56 ),
57 (
58 "$memcpy",
59 "(import \"env\" \"memcpy\" (func $memcpy (param i64 i64 i64)))",
60 ),
61 (
62 "$memmove",
63 "(import \"env\" \"memmove\" (func $memmove (param i64 i64 i64)))",
64 ),
65 (
66 "$memset",
67 "(import \"env\" \"memset\" (func $memset (param i64 i64 i64)))",
68 ),
69 ]
70 }
71
72 pub fn get_wasm_type(&self, ty: &MirType) -> &'static str {
74 let _ = ty;
75 "i64"
76 }
77
78 pub fn generate_local_decl(&self, index: usize) -> String {
80 format!(" (local $l{index} i64)")
81 }
82}
83
84impl Abi for WasmABI {
85 fn target_os(&self) -> TargetOperatingSystem {
86 self.target_os
87 }
88
89 fn mangle_function_name(&self, name: &str) -> String {
90 WasmABI::mangle_function_name(self, name)
91 }
92}