Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(moonbit): use internal abi #1243

Merged
merged 5 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions crates/moonbit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@ through the `wit-bindgen-cli` crate.
anyhow = { workspace = true }
wit-bindgen-core = { workspace = true }
heck = { workspace = true }
clap = { workspace = true, optional = true }

[dev-dependencies]
test-helpers = { path = '../test-helpers' }
clap = { workspace = true, optional = true }
150 changes: 62 additions & 88 deletions crates/moonbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,12 @@ pub extern "wasm" fn f32_to_i32(value : Float) -> Int =
pub extern "wasm" fn f32_to_i64(value : Float) -> Int64 =
#|(func (param f32) (result i64) local.get 0 f32.convert_i64_s)

extern "wasm" fn malloc_inline(size : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 call $moonbit.malloc)

pub fn malloc(size : Int) -> Int {
let words = size / 4 + 1
let address = malloc_inline(8 + words * 4)
store32(address, 1)
store32(address + 4, (words << 8) | 246)
address + 8
}
// set pseudo header; allocate extra bytes for string
pub extern "wasm" fn malloc(size : Int) -> Int =
#|(func (param i32) (result i32) (local i32)
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1 i32.const 8 i32.add)

pub extern "wasm" fn free(position : Int) =
#|(func (param i32) local.get 0 i32.const 8 i32.sub call $moonbit.decref)
Expand All @@ -99,30 +95,20 @@ extern "wasm" fn copy(dest : Int, src : Int, len : Int) =
pub extern "wasm" fn str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)

extern "wasm" fn ptr2str_ffi(ptr : Int) -> String =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)

pub fn ptr2str(ptr : Int, len : Int) -> String {
let words = len * 2 / 4 + 1
let address = ptr - 8
store32(address + 4, (words << 8) | 243)
store8(address + words * 4 + 7, 3 - len * 2 % 4)
ptr2str_ffi(ptr)
}
pub extern "wasm" fn ptr2str(ptr : Int, len : Int) -> String =
#|(func (param i32) (param i32) (result i32) (local i32)
#| local.get 0 i32.const 8 i32.sub local.tee 2
#| local.get 1 call $moonbit.init_array16
#| local.get 2)

pub extern "wasm" fn bytes2ptr(bytes : FixedArray[Byte]) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)

extern "wasm" fn ptr2bytes_ffi(ptr : Int) -> FixedArray[Byte] =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)

pub fn ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] {
let words = len / 4 + 1
let address = ptr - 8
store32(address + 4, (words << 8) | 246)
store8(address + words * 4 + 7, 3 - len % 4)
ptr2bytes_ffi(ptr)
}
pub extern "wasm" fn ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
#|(func (param i32) (param i32) (result i32) (local i32)
#| local.get 0 i32.const 8 i32.sub local.tee 2
#| local.get 1 call $moonbit.init_array8
#| local.get 2)

pub extern "wasm" fn uint_array2ptr(array : FixedArray[UInt]) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
Expand All @@ -142,56 +128,41 @@ pub extern "wasm" fn float_array2ptr(array : FixedArray[Float]) -> Int =
pub extern "wasm" fn double_array2ptr(array : FixedArray[Double]) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)

extern "wasm" fn ptr2uint_array_ffi(ptr : Int) -> FixedArray[UInt] =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)

pub fn ptr2uint_array(ptr : Int, len : Int) -> FixedArray[UInt] {
set_header_ffi(ptr - 4, len)
ptr2uint_array_ffi(ptr)
}

extern "wasm" fn ptr2int_array_ffi(ptr : Int) -> FixedArray[Int] =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)

pub fn ptr2int_array(ptr : Int, len : Int) -> FixedArray[Int] {
set_header_ffi(ptr - 4, len)
ptr2int_array_ffi(ptr)
}

extern "wasm" fn ptr2float_array_ffi(ptr : Int) -> FixedArray[Float] =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)

pub fn ptr2float_array(ptr : Int, len : Int) -> FixedArray[Float] {
set_header_ffi(ptr - 4, len)
ptr2float_array_ffi(ptr)
}
extern "wasm" fn ptr2uint64_array_ffi(ptr : Int) -> FixedArray[UInt64] =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)

pub fn ptr2uint64_array(ptr : Int, len : Int) -> FixedArray[UInt64] {
set_header_ffi(ptr - 4, len)
ptr2uint64_array_ffi(ptr)
}

extern "wasm" fn ptr2int64_array_ffi(ptr : Int) -> FixedArray[Int64] =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)

pub fn ptr2int64_array(ptr : Int, len : Int) -> FixedArray[Int64] {
set_header_ffi(ptr - 4, len)
ptr2int64_array_ffi(ptr)
}

extern "wasm" fn ptr2double_array_ffi(ptr : Int) -> FixedArray[Double] =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)

pub fn ptr2double_array(ptr : Int, len : Int) -> FixedArray[Double] {
set_header_ffi(ptr - 4, len)
ptr2double_array_ffi(ptr)
}

fn set_header_ffi(offset : Int, len : Int) -> Unit {
store32(offset, len << 8 | 241)
}
pub extern "wasm" fn ptr2uint_array(ptr : Int, len : Int) -> FixedArray[UInt] =
#|(func (param i32) (param i32) (result i32) (local i32)
#| local.get 0 i32.const 8 i32.sub local.tee 2
#| local.get 1 call $moonbit.init_array32
#| local.get 2)

pub extern "wasm" fn ptr2int_array(ptr : Int, len : Int) -> FixedArray[Int] =
#|(func (param i32) (param i32) (result i32) (local i32)
#| local.get 0 i32.const 8 i32.sub local.tee 2
#| local.get 1 call $moonbit.init_array32
#| local.get 2)

pub extern "wasm" fn ptr2float_array(ptr : Int, len : Int) -> FixedArray[Float] =
#|(func (param i32) (param i32) (result i32) (local i32)
#| local.get 0 i32.const 8 i32.sub local.tee 2
#| local.get 1 call $moonbit.init_array32
#| local.get 2)

pub extern "wasm" fn ptr2uint64_array(ptr : Int, len : Int) -> FixedArray[UInt64] =
#|(func (param i32) (param i32) (result i32) (local i32)
#| local.get 0 i32.const 8 i32.sub local.tee 2
#| local.get 1 call $moonbit.init_array64
#| local.get 2)

pub extern "wasm" fn ptr2int64_array(ptr : Int, len : Int) -> FixedArray[Int64] =
#|(func (param i32) (param i32) (result i32) (local i32)
#| local.get 0 i32.const 8 i32.sub local.tee 2
#| local.get 1 call $moonbit.init_array64
#| local.get 2)

pub extern "wasm" fn ptr2double_array(ptr : Int, len : Int) -> FixedArray[Double] =
#|(func (param i32) (param i32) (result i32) (local i32)
#| local.get 0 i32.const 8 i32.sub local.tee 2
#| local.get 1 call $moonbit.init_array64
#| local.get 2)

pub fn cabi_realloc(
src_offset : Int,
Expand Down Expand Up @@ -593,7 +564,10 @@ impl WorldGenerator for MoonBit {
wit_bindgen_core::generated_preamble(&mut body, version);
body.push_str(FFI);
files.push(&format!("{FFI_DIR}/top.mbt"), indent(&body).as_bytes());
files.push(&format!("{FFI_DIR}/moon.pkg.json"), "{}".as_bytes());
files.push(
&format!("{FFI_DIR}/moon.pkg.json"),
"{ \"warn-list\": \"-44\", \"supported-targets\": [\"wasm\"] }".as_bytes(),
);

// Export project files
if !self.opts.ignore_stub && !self.opts.ignore_module_file {
Expand Down Expand Up @@ -2252,7 +2226,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
"{}str2ptr({op})",
self.gen.qualify_package(FFI_DIR)
));
results.push(format!("{op}.charcode_length()"));
results.push(format!("{op}.length()"));
if realloc.is_none() {
self.cleanup.push(Cleanup::Object(op.clone()));
}
Expand Down Expand Up @@ -2780,18 +2754,18 @@ impl Bindgen for FunctionBindgen<'_, '_> {
fn perform_cast(op: &str, cast: &Bitcast) -> String {
match cast {
Bitcast::I32ToF32 => {
format!("({op}).to_float()")
format!("({op}).reinterpret_as_float()")
}
Bitcast::I64ToF32 => format!("Int64::to_int({op}).to_float()"),
Bitcast::I64ToF32 => format!("({op}).to_int().reinterpret_as_float()"),
Bitcast::F32ToI32 => {
format!("@ffi.f32_to_i32({op})")
format!("({op}).reinterpret_as_int()")
}
Bitcast::F32ToI64 => format!("@ffi.f32_to_i64({op})"),
Bitcast::F32ToI64 => format!("({op}).reinterpret_as_int().to_int64()"),
Bitcast::I64ToF64 => {
format!("Int64::to_double({op})")
format!("({op}).reinterpret_as_double()")
}
Bitcast::F64ToI64 => {
format!("Double::to_int64({op})")
format!("({op}).reinterpret_as_int64()")
}
Bitcast::LToI64 | Bitcast::PToP64 | Bitcast::I32ToI64 => format!("Int::to_int64({op})"),
Bitcast::I64ToL | Bitcast::P64ToP | Bitcast::I64ToI32 => format!("Int64::to_int({op})"),
Expand Down
61 changes: 0 additions & 61 deletions crates/moonbit/tests/codegen.rs

This file was deleted.

5 changes: 5 additions & 0 deletions crates/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod c;
mod config;
mod csharp;
mod custom;
mod moonbit;
mod runner;
mod rust;
mod wat;
Expand Down Expand Up @@ -193,6 +194,7 @@ enum Language {
Cpp,
Wat,
Csharp,
MoonBit,
Custom(custom::Language),
}

Expand Down Expand Up @@ -410,6 +412,7 @@ impl Runner<'_> {
"cpp" => Language::Cpp,
"wat" => Language::Wat,
"cs" => Language::Csharp,
"mbt" => Language::MoonBit,
other => Language::Custom(custom::Language::lookup(self, other)?),
};

Expand Down Expand Up @@ -1198,6 +1201,7 @@ impl Language {
Language::Cpp,
Language::Wat,
Language::Csharp,
Language::MoonBit,
];

fn obj(&self) -> &dyn LanguageMethods {
Expand All @@ -1207,6 +1211,7 @@ impl Language {
Language::Cpp => &c::Cpp,
Language::Wat => &wat::Wat,
Language::Csharp => &csharp::Csharp,
Language::MoonBit => &moonbit::MoonBit,
Language::Custom(custom) => custom,
}
}
Expand Down
Loading