Skip to content

Commit

Permalink
Merge pull request #25 from dhil/wasmfx-merge
Browse files Browse the repository at this point in the history
Merge with upstream
  • Loading branch information
dhil authored Aug 23, 2024
2 parents c86b26e + 57629e1 commit 3640c4c
Show file tree
Hide file tree
Showing 22 changed files with 367 additions and 381 deletions.
559 changes: 273 additions & 286 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ indexmap = "2.0.0"
prettyplease = "0.2.20"
syn = { version = "2.0", features = ["printing"] }

wasmparser = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.215.0" }
wasm-metadata = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.215.0" }
wasm-encoder = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.215.0" }
wit-parser = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.215.0" }
wit-component = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.215.0" }
wasmparser = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.216.0" }
wasm-metadata = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.216.0" }
wasm-encoder = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.216.0" }
wit-parser = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.216.0" }
wit-component = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.216.0" }

wit-bindgen-core = { path = 'crates/core', version = '0.30.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.30.0' }
Expand Down Expand Up @@ -86,8 +86,8 @@ moonbit = ['dep:wit-bindgen-moonbit']

[dev-dependencies]
heck = { workspace = true }
wasmtime = { version = "22.0.0", features = ['component-model'] }
wasmtime-wasi = { version = "22.0.0" }
wasmtime = { version = "24.0.0", features = ['component-model'] }
wasmtime-wasi = { version = "24.0.0" }
test-artifacts = { path = 'crates/test-rust-wasm/artifacts' }
wit-parser = { workspace = true }
wasmparser = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3028,7 +3028,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
uwriteln!(self.src, "uint8_t *{ptr} = {};", operands[0]);
let i = self.locals.tmp("i");
uwriteln!(self.src, "for (size_t {i} = 0; {i} < {len}; {i}++) {{");
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
uwriteln!(self.src, "uint8_t *base = {ptr} + {i} * {size};");
uwriteln!(self.src, "(void) base;");
uwrite!(self.src, "{body}");
Expand Down
68 changes: 46 additions & 22 deletions crates/core/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,31 +800,33 @@ impl<'a, B: Bindgen> Generator<'a, B> {
// ... otherwise if parameters are indirect space is
// allocated from them and each argument is lowered
// individually into memory.
let (size, align) = self
let info = self
.bindgen
.sizes()
.record(func.params.iter().map(|t| &t.1));
let ptr = match self.variant {
// When a wasm module calls an import it will provide
// space that isn't explicitly deallocated.
AbiVariant::GuestImport => self.bindgen.return_pointer(size, align),
AbiVariant::GuestImport => self
.bindgen
.return_pointer(info.size.size_wasm32(), info.align.align_wasm32()),
// When calling a wasm module from the outside, though,
// malloc needs to be called.
AbiVariant::GuestExport => {
self.emit(&Instruction::Malloc {
realloc: "cabi_realloc",
size,
align,
size: info.size.size_wasm32(),
align: info.align.align_wasm32(),
});
self.stack.pop().unwrap()
}
};
let mut offset = 0usize;
for (nth, (_, ty)) in func.params.iter().enumerate() {
self.emit(&Instruction::GetArg { nth });
offset = align_to(offset, self.bindgen.sizes().align(ty));
offset = align_to(offset, self.bindgen.sizes().align(ty).align_wasm32());
self.write_to_memory(ty, ptr.clone(), offset as i32);
offset += self.bindgen.sizes().size(ty);
offset += self.bindgen.sizes().size(ty).size_wasm32();
}

self.stack.push(ptr);
Expand All @@ -833,8 +835,10 @@ impl<'a, B: Bindgen> Generator<'a, B> {
// If necessary we may need to prepare a return pointer for
// this ABI.
if self.variant == AbiVariant::GuestImport && sig.retptr {
let (size, align) = self.bindgen.sizes().params(func.results.iter_types());
let ptr = self.bindgen.return_pointer(size, align);
let info = self.bindgen.sizes().params(func.results.iter_types());
let ptr = self
.bindgen
.return_pointer(info.size.size_wasm32(), info.align.align_wasm32());
self.return_pointer = Some(ptr.clone());
self.stack.push(ptr);
}
Expand Down Expand Up @@ -904,9 +908,9 @@ impl<'a, B: Bindgen> Generator<'a, B> {
self.emit(&Instruction::GetArg { nth: 0 });
let ptr = self.stack.pop().unwrap();
for (_, ty) in func.params.iter() {
offset = align_to(offset, self.bindgen.sizes().align(ty));
offset = align_to(offset, self.bindgen.sizes().align(ty).align_wasm32());
self.read_from_memory(ty, ptr.clone(), offset as i32);
offset += self.bindgen.sizes().size(ty);
offset += self.bindgen.sizes().size(ty).size_wasm32();
}
}

Expand All @@ -917,12 +921,15 @@ impl<'a, B: Bindgen> Generator<'a, B> {
// it's been read by the guest we need to deallocate it.
if let AbiVariant::GuestExport = self.variant {
if sig.indirect_params {
let (size, align) = self
let info = self
.bindgen
.sizes()
.record(func.params.iter().map(|t| &t.1));
self.emit(&Instruction::GetArg { nth: 0 });
self.emit(&Instruction::GuestDeallocate { size, align });
self.emit(&Instruction::GuestDeallocate {
size: info.size.size_wasm32(),
align: info.align.align_wasm32(),
});
}
}

Expand Down Expand Up @@ -959,9 +966,10 @@ impl<'a, B: Bindgen> Generator<'a, B> {
// (statically) and then write the result into that
// memory, returning the pointer at the end.
AbiVariant::GuestExport => {
let (size, align) =
self.bindgen.sizes().params(func.results.iter_types());
let ptr = self.bindgen.return_pointer(size, align);
let info = self.bindgen.sizes().params(func.results.iter_types());
let ptr = self
.bindgen
.return_pointer(info.size.size_wasm32(), info.align.align_wasm32());
self.write_params_to_memory(func.results.iter_types(), ptr.clone(), 0);
self.stack.push(ptr);
}
Expand Down Expand Up @@ -998,6 +1006,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
.sizes()
.field_offsets(func.results.iter_types())
{
let offset = offset.size_wasm32();
let offset = i32::try_from(offset).unwrap();
self.deallocate(ty, addr.clone(), offset);
}
Expand Down Expand Up @@ -1535,8 +1544,12 @@ impl<'a, B: Bindgen> Generator<'a, B> {
tag: Int,
cases: impl IntoIterator<Item = Option<&'b Type>> + Clone,
) {
let payload_offset =
offset + (self.bindgen.sizes().payload_offset(tag, cases.clone()) as i32);
let payload_offset = offset
+ (self
.bindgen
.sizes()
.payload_offset(tag, cases.clone())
.size_wasm32() as i32);
for (i, ty) in cases.into_iter().enumerate() {
self.push_block();
self.emit(&Instruction::VariantPayloadName);
Expand Down Expand Up @@ -1581,6 +1594,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
.zip(fields)
{
self.stack.push(op);
let field_offset = field_offset.size_wasm32();
self.write_to_memory(ty, addr.clone(), offset + (field_offset as i32));
}
}
Expand Down Expand Up @@ -1715,8 +1729,12 @@ impl<'a, B: Bindgen> Generator<'a, B> {
) {
self.stack.push(addr.clone());
self.load_intrepr(offset, tag);
let payload_offset =
offset + (self.bindgen.sizes().payload_offset(tag, cases.clone()) as i32);
let payload_offset = offset
+ (self
.bindgen
.sizes()
.payload_offset(tag, cases.clone())
.size_wasm32() as i32);
for ty in cases {
self.push_block();
if let Some(ty) = ty {
Expand All @@ -1743,7 +1761,8 @@ impl<'a, B: Bindgen> Generator<'a, B> {
offset: i32,
) {
for (field_offset, ty) in self.bindgen.sizes().field_offsets(tys).iter() {
self.read_from_memory(ty, addr.clone(), offset + (*field_offset as i32));
let field_offset = field_offset.size_wasm32();
self.read_from_memory(ty, addr.clone(), offset + (field_offset as i32));
}
}

Expand Down Expand Up @@ -1882,8 +1901,12 @@ impl<'a, B: Bindgen> Generator<'a, B> {
) {
self.stack.push(addr.clone());
self.load_intrepr(offset, tag);
let payload_offset =
offset + (self.bindgen.sizes().payload_offset(tag, cases.clone()) as i32);
let payload_offset = offset
+ (self
.bindgen
.sizes()
.payload_offset(tag, cases.clone())
.size_wasm32() as i32);
for ty in cases {
self.push_block();
if let Some(ty) = ty {
Expand All @@ -1895,6 +1918,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {

fn deallocate_fields(&mut self, tys: &[Type], addr: B::Operand, offset: i32) {
for (field_offset, ty) in self.bindgen.sizes().field_offsets(tys) {
let field_offset = field_offset.size_wasm32();
self.deallocate(ty, addr.clone(), offset + (field_offset as i32));
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let address = self.locals.tmp("address");
let buffer = self.locals.tmp("buffer");
let gc_handle = self.locals.tmp("gcHandle");
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
uwrite!(
self.src,
"
Expand Down Expand Up @@ -2597,7 +2597,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
assert!(block_results.is_empty());

let list = &operands[0];
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let ty = self.gen.type_name_with_qualifier(element, true);
let index = self.locals.tmp("index");

Expand Down Expand Up @@ -2641,7 +2641,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let length = &operands[1];
let array = self.locals.tmp("array");
let ty = self.gen.type_name_with_qualifier(element, true);
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let index = self.locals.tmp("index");

let result = match &block_results[..] {
Expand Down
19 changes: 9 additions & 10 deletions crates/moonbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ pub extern "wasm" fn ptr2float_array(ptr : Int) -> FixedArray[Float] =
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.const 241 i32.store8 local.get 0 i32.const 8 i32.sub)
pub extern "wasm" fn ptr2uint64_array(ptr : Int) -> FixedArray[UInt64] =
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.load i32.const 2 i32.lsr i32.const 241 i32.lor local.get 0 i32.const 4 i32.sub i32.store local.get 0 i32.const 8 i32.sub)
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.load i32.const 1 i32.shr_u i32.const 241 i32.or local.get 0 i32.const 4 i32.sub i32.store local.get 0 i32.const 8 i32.sub)
pub extern "wasm" fn ptr2int64_array(ptr : Int) -> FixedArray[Int64] =
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.load i32.const 2 i32.lsr i32.const 241 i32.lor local.get 0 i32.const 4 i32.sub i32.store local.get 0 i32.const 8 i32.sub)
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.load i32.const 1 i32.shr_u i32.const 241 i32.or local.get 0 i32.const 4 i32.sub i32.store local.get 0 i32.const 8 i32.sub)
pub extern "wasm" fn ptr2double_array(ptr : Int) -> FixedArray[Double] =
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.load i32.const 2 i32.lsr i32.const 241 i32.lor local.get 0 i32.const 4 i32.sub i32.store local.get 0 i32.const 8 i32.sub)
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.load i32.const 1 i32.shr_u i32.const 241 i32.or local.get 0 i32.const 4 i32.sub i32.store local.get 0 i32.const 8 i32.sub)
pub trait Any {}
pub struct Cleanup {
Expand Down Expand Up @@ -1629,9 +1629,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::I32FromS8 => {
results.push(format!("{ffi_qualifier}extend8({})", operands[0]))
}
Instruction::S8FromI32 => results.push(format!("({}.land(0xFF))", operands[0])),

Instruction::S16FromI32 => results.push(format!("({}.land(0xFFFF))", operands[0])),
Instruction::S8FromI32 => results.push(format!("({} - 0x100)", operands[0])),
Instruction::S16FromI32 => results.push(format!("({} - 0x10000)", operands[0])),
Instruction::I32FromS16 => {
results.push(format!("{ffi_qualifier}extend16({})", operands[0]))
}
Expand Down Expand Up @@ -2027,8 +2026,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
assert!(block_results.is_empty());

let op = &operands[0];
let size = self.gen.gen.sizes.size(element);
let align = self.gen.gen.sizes.align(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let align = self.gen.gen.sizes.align(element).align_wasm32();
let address = self.locals.tmp("address");
let ty = self.gen.type_name(element, true);
let index = self.locals.tmp("index");
Expand Down Expand Up @@ -2068,7 +2067,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let length = &operands[1];
let array = self.locals.tmp("array");
let ty = self.gen.type_name(element, true);
let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
// let align = self.gen.gen.sizes.align(element);
let index = self.locals.tmp("index");

Expand Down Expand Up @@ -2365,7 +2364,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let address = &operands[0];
let length = &operands[1];

let size = self.gen.gen.sizes.size(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
// let align = self.gen.gen.sizes.align(element);

if !body.trim().is_empty() {
Expand Down
12 changes: 6 additions & 6 deletions crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
operand0 = operands[0]
));
self.push_str(&format!("let {len} = {vec}.len();\n"));
let size = self.gen.sizes.size(element);
let align = self.gen.sizes.align(element);
let size = self.gen.sizes.size(element).size_wasm32();
let align = self.gen.sizes.align(element).align_wasm32();
self.push_str(&format!(
"let {layout} = {alloc}::Layout::from_size_align_unchecked({vec}.len() * {size}, {align});\n",
));
Expand Down Expand Up @@ -746,8 +746,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::ListLift { element, .. } => {
let body = self.blocks.pop().unwrap();
let tmp = self.tmp();
let size = self.gen.sizes.size(element);
let align = self.gen.sizes.align(element);
let size = self.gen.sizes.size(element).size_wasm32();
let align = self.gen.sizes.align(element).align_wasm32();
let len = format!("len{tmp}");
let base = format!("base{tmp}");
let result = format!("result{tmp}");
Expand Down Expand Up @@ -1038,8 +1038,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::GuestDeallocateList { element } => {
let body = self.blocks.pop().unwrap();
let tmp = self.tmp();
let size = self.gen.sizes.size(element);
let align = self.gen.sizes.align(element);
let size = self.gen.sizes.size(element).size_wasm32();
let align = self.gen.sizes.align(element).align_wasm32();
let len = format!("len{tmp}");
let base = format!("base{tmp}");
self.push_str(&format!(
Expand Down
12 changes: 6 additions & 6 deletions crates/teavm-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,8 +1667,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
assert!(block_results.is_empty());

let op = &operands[0];
let size = self.gen.gen.sizes.size(element);
let align = self.gen.gen.sizes.align(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let align = self.gen.gen.sizes.align(element).align_wasm32();
let address = self.locals.tmp("address");
let ty = self.gen.type_name(element);
let index = self.locals.tmp("index");
Expand Down Expand Up @@ -1708,8 +1708,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let length = &operands[1];
let array = self.locals.tmp("array");
let ty = self.gen.type_name(element);
let size = self.gen.gen.sizes.size(element);
let align = self.gen.gen.sizes.align(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let align = self.gen.gen.sizes.align(element).align_wasm32();
let index = self.locals.tmp("index");

let result = match &block_results[..] {
Expand Down Expand Up @@ -2006,8 +2006,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let address = &operands[0];
let length = &operands[1];

let size = self.gen.gen.sizes.size(element);
let align = self.gen.gen.sizes.align(element);
let size = self.gen.gen.sizes.size(element).size_wasm32();
let align = self.gen.gen.sizes.align(element).align_wasm32();

if !body.trim().is_empty() {
let index = self.locals.tmp("index");
Expand Down
2 changes: 1 addition & 1 deletion src/bin/wit-bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn main() -> Result<()> {
Some(path) => path.join(name),
None => name.into(),
};
println!("Generating {:?}", dst);
eprintln!("Generating {:?}", dst);

if opt.check {
let prev = std::fs::read(&dst).with_context(|| format!("failed to read {:?}", dst))?;
Expand Down
Loading

0 comments on commit 3640c4c

Please sign in to comment.