use crate::abi::Abi;
use lamina_mir::MirType;
use lamina_platform::TargetOperatingSystem;
pub struct WasmABI {
target_os: TargetOperatingSystem,
}
impl WasmABI {
pub fn new(target_os: TargetOperatingSystem) -> Self {
Self { target_os }
}
pub fn mangle_function_name(&self, name: &str) -> String {
name.to_string()
}
pub fn get_print_import(&self) -> &'static str {
"(import \"console\" \"log\" (func $log (param i64)))\n (import \"console\" \"logu\" (func $logu (param i64)))\n (import \"console\" \"logf\" (func $logf (param f64)))"
}
pub fn get_runtime_imports(&self) -> &'static [&'static str] {
&[
"(import \"env\" \"writebyte\" (func $writebyte (param i64) (result i64)))",
"(import \"env\" \"readbyte\" (func $readbyte (result i64)))",
"(import \"env\" \"writeptr\" (func $writeptr (param i64 i64) (result i64)))",
"(import \"env\" \"malloc\" (func $malloc (param i64) (result i64)))",
"(import \"env\" \"free\" (func $free (param i64)))",
]
}
pub fn get_wasm_type(&self, ty: &MirType) -> &'static str {
let _ = ty;
"i64"
}
pub fn generate_global_decl(&self, index: usize) -> String {
format!(" (global $vreg{index} (mut i64) (i64.const 0))")
}
pub fn generate_local_decl(&self, index: usize) -> String {
format!(" (local $l{index} i64)")
}
}
impl Abi for WasmABI {
fn target_os(&self) -> TargetOperatingSystem {
self.target_os
}
fn mangle_function_name(&self, name: &str) -> String {
WasmABI::mangle_function_name(self, name)
}
}