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

Add support for params passed indirectly #208

Merged
merged 2 commits into from
Apr 28, 2022
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
21 changes: 8 additions & 13 deletions crates/gen-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,18 +656,6 @@ impl Generator for C {
let variant = Self::abi_variant(dir);
self.sizes.fill(iface);
self.in_import = variant == AbiVariant::GuestImport;

for func in iface.functions.iter() {
let sig = iface.wasm_signature(variant, func);
if sig.retptr {
self.return_pointer_area_size = self
.return_pointer_area_size
.max(self.sizes.size(&func.result));
self.return_pointer_area_align = self
.return_pointer_area_align
.max(self.sizes.align(&func.result));
}
}
}

fn type_record(
Expand Down Expand Up @@ -1262,7 +1250,9 @@ impl Bindgen for FunctionBindgen<'_> {
self.blocks.push((src.into(), mem::take(operands)));
}

fn return_pointer(&mut self, _iface: &Interface, _ty: &Type) -> String {
fn return_pointer(&mut self, size: usize, align: usize) -> String {
self.gen.return_pointer_area_size = self.gen.return_pointer_area_size.max(size);
self.gen.return_pointer_area_align = self.gen.return_pointer_area_align.max(align);
let ptr = self.locals.tmp("ptr");
self.src
.push_str(&format!("int32_t {} = (int32_t) &RET_AREA;\n", ptr));
Expand Down Expand Up @@ -1784,6 +1774,11 @@ impl Bindgen for FunctionBindgen<'_> {
self.load_ext("int16_t", *offset, operands, results)
}

Instruction::Free { .. } => {
self.src
.push_str(&format!("free((void*) ({}));\n", operands[0]));
}

i => unimplemented!("{:?}", i),
}
}
Expand Down
17 changes: 16 additions & 1 deletion crates/gen-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ impl Bindgen for FunctionBindgen<'_> {
self.blocks.push((src.into(), mem::take(operands)));
}

fn return_pointer(&mut self, _iface: &Interface, _ty: &Type) -> String {
fn return_pointer(&mut self, _size: usize, _align: usize) -> String {
unimplemented!()
}

Expand Down Expand Up @@ -1944,6 +1944,21 @@ impl Bindgen for FunctionBindgen<'_> {
Instruction::I32Store8 { offset } => self.store("setInt8", *offset, operands),
Instruction::I32Store16 { offset } => self.store("setInt16", *offset, operands),

Instruction::Malloc {
realloc,
size,
align,
} => {
self.needs_realloc = Some(realloc.to_string());
let tmp = self.tmp();
let ptr = format!("ptr{}", tmp);
self.src.js(&format!(
"const {} = realloc(0, 0, {}, {});\n",
ptr, align, size
));
results.push(ptr);
}

i => unimplemented!("{:?}", i),
}
}
Expand Down
28 changes: 15 additions & 13 deletions crates/gen-rust-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,6 @@ impl Generator for RustWasm {
}

self.sizes.fill(iface);

for func in iface.functions.iter() {
let sig = iface.wasm_signature(variant, func);
if sig.retptr {
self.return_pointer_area_size = self
.return_pointer_area_size
.max(self.sizes.size(&func.result));
self.return_pointer_area_align = self
.return_pointer_area_align
.max(self.sizes.align(&func.result));
}
}
}

fn type_record(
Expand Down Expand Up @@ -791,7 +779,9 @@ impl Bindgen for FunctionBindgen<'_> {
}
}

fn return_pointer(&mut self, _iface: &Interface, _ty: &Type) -> String {
fn return_pointer(&mut self, size: usize, align: usize) -> String {
self.gen.return_pointer_area_size = self.gen.return_pointer_area_size.max(size);
self.gen.return_pointer_area_align = self.gen.return_pointer_area_align.max(align);
let tmp = self.tmp();
self.push_str(&format!(
"let ptr{} = RET_AREA.0.as_mut_ptr() as i32;\n",
Expand Down Expand Up @@ -1422,6 +1412,18 @@ impl Bindgen for FunctionBindgen<'_> {
operands[1], offset, operands[0]
));
}

Instruction::Malloc { .. } => unimplemented!(),
Instruction::Free {
free: _,
size,
align,
} => {
self.push_str(&format!(
"wit_bindgen_rust::rt::canonical_abi_free({} as *mut u8, {}, {});\n",
operands[0], size, align
));
}
}
}
}
61 changes: 34 additions & 27 deletions crates/gen-spidermonkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ lazy_static! {
params: vec![],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -57,6 +58,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -65,6 +67,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32, WasmType::I32, WasmType::I32],
results: vec![WasmType::I32],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -73,6 +76,7 @@ lazy_static! {
params: vec![],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -81,6 +85,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32],
results: vec![WasmType::I32],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -89,6 +94,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32, WasmType::I32, WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -97,6 +103,7 @@ lazy_static! {
params: vec![WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -105,6 +112,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -113,6 +121,7 @@ lazy_static! {
params: vec![WasmType::I32],
results: vec![WasmType::I32],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -121,14 +130,16 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
}
indirect_params: false,
}
),
(
"SMW_clear_operands",
WasmSignature {
params: vec![],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -137,6 +148,7 @@ lazy_static! {
params: vec![WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -145,6 +157,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32, WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -153,6 +166,7 @@ lazy_static! {
params: vec![WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -161,6 +175,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -169,6 +184,7 @@ lazy_static! {
params: vec![WasmType::I32],
results: vec![WasmType::I32],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -177,6 +193,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -185,6 +202,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -193,6 +211,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -201,6 +220,7 @@ lazy_static! {
params: vec![WasmType::I32],
results: vec![WasmType::I32],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -209,6 +229,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -217,6 +238,7 @@ lazy_static! {
params: vec![WasmType::I32, WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -225,6 +247,7 @@ lazy_static! {
params: vec![WasmType::I32],
results: vec![],
retptr: false,
indirect_params: false,
},
),
(
Expand All @@ -233,6 +256,7 @@ lazy_static! {
params: vec![WasmType::I32],
results: vec![WasmType::I32],
retptr: false,
indirect_params: false,
},
),
];
Expand Down Expand Up @@ -585,6 +609,7 @@ impl<'a> SpiderMonkeyWasm<'a> {
params: vec![],
results: vec![],
retptr: false,
indirect_params: false,
});
funcs.function(ty_index);

Expand Down Expand Up @@ -908,30 +933,6 @@ impl Generator for SpiderMonkeyWasm<'_> {
Some(u32::try_from(imports.iter().map(|i| i.functions.len()).sum::<usize>()).unwrap());
self.num_export_functions =
Some(u32::try_from(exports.iter().map(|i| i.functions.len()).sum::<usize>()).unwrap());

// Figure out what the maximum return pointer area we will need is.
for (iface, variant) in imports
.iter()
.zip(std::iter::repeat(AbiVariant::GuestImport))
.chain(
exports
.iter()
.zip(std::iter::repeat(AbiVariant::GuestExport)),
)
{
self.sizes.fill(iface);
for func in iface.functions.iter() {
let sig = iface.wasm_signature(variant, func);
if sig.retptr {
self.return_pointer_area_size = self
.return_pointer_area_size
.max(self.sizes.size(&func.result));
self.return_pointer_area_align = self
.return_pointer_area_align
.max(self.sizes.align(&func.result));
}
}
}
}

fn preprocess_one(&mut self, iface: &Interface, _dir: Direction) {
Expand Down Expand Up @@ -1101,6 +1102,7 @@ impl Generator for SpiderMonkeyWasm<'_> {
WasmType::I32,
],
retptr: false,
indirect_params: false,
});
for f in &self.import_glue_fns {
funcs.function(js_native_type_index);
Expand Down Expand Up @@ -2118,10 +2120,15 @@ impl abi::Bindgen for Bindgen<'_, '_> {

abi::Instruction::ReturnAsyncExport { .. } => todo!(),
abi::Instruction::ReturnAsyncImport { .. } => todo!(),

abi::Instruction::Malloc { .. } => todo!(),
abi::Instruction::Free { .. } => todo!(),
}
}

fn return_pointer(&mut self, _iface: &Interface, _ty: &Type) -> Self::Operand {
fn return_pointer(&mut self, size: usize, align: usize) -> Self::Operand {
self.gen.return_pointer_area_size = self.gen.return_pointer_area_size.max(size);
self.gen.return_pointer_area_align = self.gen.return_pointer_area_align.max(align);
let local = self.new_local(wasm_encoder::ValType::I32);

// []
Expand All @@ -2142,7 +2149,7 @@ impl abi::Bindgen for Bindgen<'_, '_> {
}

fn sizes(&self) -> &SizeAlign {
todo!()
&self.gen.sizes
}

fn is_list_canonical(&self, _iface: &Interface, _ty: &Type) -> bool {
Expand Down
Loading