From fc24a0d47d3f509c136697b1dc55c7cdbfc2c3b9 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Thu, 18 Jul 2024 23:24:37 +0200 Subject: [PATCH] revised symmetric code generation --- crates/core/src/abi.rs | 25 ++++++---- crates/cpp/src/lib.rs | 9 ++++ .../component_a/the_world.cpp | 48 +++++++++---------- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/crates/core/src/abi.rs b/crates/core/src/abi.rs index bb789ab21..74d53a0e7 100644 --- a/crates/core/src/abi.rs +++ b/crates/core/src/abi.rs @@ -845,15 +845,18 @@ 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 - && !matches!(self.lift_lower, LiftLower::Symmetric) + let retptr = if sig.retptr + && (matches!(self.lift_lower, LiftLower::Symmetric) + || self.variant == AbiVariant::GuestImport) { let (size, align) = self.bindgen.sizes().params(func.results.iter_types()); let ptr = self.bindgen.return_pointer(size, align); self.return_pointer = Some(ptr.clone()); - self.stack.push(ptr); - } + self.stack.push(ptr.clone()); + Some(ptr) + } else { + None + }; // Now that all the wasm args are prepared we can call the // actual wasm function. @@ -865,8 +868,8 @@ impl<'a, B: Bindgen> Generator<'a, B> { }); if matches!(self.lift_lower, LiftLower::Symmetric) && sig.retptr { - let ptr = self.stack.pop().unwrap(); - self.read_results_from_memory(&func.results, ptr.clone(), 0); + //let ptr = self.stack.pop().unwrap(); + self.read_results_from_memory(&func.results, retptr.clone().unwrap(), 0); if guest_export_needs_post_return(self.resolve, func) { let post_sig = WasmSignature { params: vec![WasmType::Pointer], @@ -875,7 +878,7 @@ impl<'a, B: Bindgen> Generator<'a, B> { retptr: false, }; // TODO: can we get this name from somewhere? - self.stack.push(ptr); + self.stack.push(retptr.unwrap()); self.emit(&Instruction::CallWasm { name: &func.name, sig: &post_sig, @@ -996,7 +999,9 @@ impl<'a, B: Bindgen> Generator<'a, B> { let (size, align) = self.bindgen.sizes().params(func.results.iter_types()); let ptr = self.bindgen.return_pointer(size, align); self.write_params_to_memory(func.results.iter_types(), ptr.clone(), 0); - self.stack.push(ptr); + if !matches!(self.lift_lower, LiftLower::Symmetric) { + self.stack.push(ptr); + } } } } @@ -2047,7 +2052,7 @@ pub fn wasm_signature_symmetric( if results.len() > MAX_FLAT_RESULTS { retptr = true; results.truncate(0); - results.push(WasmType::Pointer); + params.push(WasmType::Pointer); } WasmSignature { diff --git a/crates/cpp/src/lib.rs b/crates/cpp/src/lib.rs index 7147f1a55..cea6fff29 100644 --- a/crates/cpp/src/lib.rs +++ b/crates/cpp/src/lib.rs @@ -1744,6 +1744,15 @@ impl CppInterfaceGenerator<'_> { ); params.push(name); } + if sig.retptr && self.gen.opts.symmetric { + let name = "retptr"; + uwrite!( + self.gen.c_src.src, + "{} {name}", + self.gen.opts.wasm_type(WasmType::Pointer) + ); + params.push(name.into()); + } self.gen.c_src.src.push_str(") {\n"); let mut f = FunctionBindgen::new(self, params.clone()); diff --git a/crates/cpp/tests/meshless_strings/component_a/the_world.cpp b/crates/cpp/tests/meshless_strings/component_a/the_world.cpp index 8e96d24e6..a37d87a89 100644 --- a/crates/cpp/tests/meshless_strings/component_a/the_world.cpp +++ b/crates/cpp/tests/meshless_strings/component_a/the_world.cpp @@ -47,16 +47,16 @@ void comp_a::foo::foo::strings::A(std::string_view x) { fooX3AfooX2FstringsX00a(ptr0, len0); } wit::string comp_a::foo::foo::strings::B() { - size_t ret_area[2]; - uint8_t *ret = (uint8_t *)(&ret_area); - fooX3AfooX2FstringsX00b(ret); - auto len0 = *((size_t *)(ret + 8)); + uint64_t ret_area[2]; + uint8_t *ptr0 = (uint8_t *)(&ret_area); + fooX3AfooX2FstringsX00b(ptr0); + auto len1 = *((size_t *)(ptr0 + 8)); - auto string0 = wit::string::from_view( - std::string_view((char const *)(*((uint8_t **)(ret + 0))), len0)); + auto string1 = wit::string::from_view( + std::string_view((char const *)(*((uint8_t **)(ptr0 + 0))), len1)); - cabi_post_fooX3AfooX2FstringsX00b(ret); - return string0; + cabi_post_fooX3AfooX2FstringsX00b(ptr0); + return string1; } wit::string comp_a::foo::foo::strings::C(std::string_view a, std::string_view b) { @@ -66,16 +66,16 @@ wit::string comp_a::foo::foo::strings::C(std::string_view a, auto const &vec1 = b; auto ptr1 = (uint8_t *)(vec1.data()); auto len1 = (size_t)(vec1.size()); - size_t ret_area[2]; - uint8_t *ret = (uint8_t *)(&ret_area); - fooX3AfooX2FstringsX00c(ptr0, len0, ptr1, len1, ret); - auto len2 = *((size_t *)(ret + 8)); + uint64_t ret_area[2]; + uint8_t *ptr2 = (uint8_t *)(&ret_area); + fooX3AfooX2FstringsX00c(ptr0, len0, ptr1, len1, ptr2); + auto len3 = *((size_t *)(ptr2 + 8)); - auto string2 = wit::string::from_view( - std::string_view((char const *)(*((uint8_t **)(ret + 0))), len2)); + auto string3 = wit::string::from_view( + std::string_view((char const *)(*((uint8_t **)(ptr2 + 0))), len3)); - cabi_post_fooX3AfooX2FstringsX00c(ret); - return string2; + cabi_post_fooX3AfooX2FstringsX00c(ptr2); + return string3; } extern "C" __attribute__((__export_name__("foo:foo/strings#a"))) void a_fooX3AfooX2FstringsX00a(uint8_t *arg0, size_t arg1) { @@ -100,14 +100,14 @@ a_fooX3AfooX2FstringsX00b(uint8_t *ptr1) { extern "C" __attribute__((__weak__, __export_name__("cabi_post_fooX3AfooX2FstringsX00b"))) void - a_cabi_post_fooX3AfooX2FstringsX00b(uint8_t *arg0) { - if ((*((size_t *)(arg0 + 8))) > 0) { - wit::string::drop_raw((void *)(*((uint8_t **)(arg0 + 0)))); + a_cabi_post_fooX3AfooX2FstringsX00b(uint8_t *retptr) { + if ((*((size_t *)(retptr + 8))) > 0) { + wit::string::drop_raw((void *)(*((uint8_t **)(retptr + 0)))); } } extern "C" __attribute__((__export_name__("foo:foo/strings#c"))) void -a_fooX3AfooX2FstringsX00c(uint8_t *arg0, size_t arg1, uint8_t *arg2, - size_t arg3, uint8_t* ptr3) { +a_fooX3AfooX2FstringsX00c(uint8_t *arg0, size_t arg1, uint8_t *arg2, size_t arg3, + uint8_t *ptr3) { auto len0 = arg1; auto string0 = @@ -131,9 +131,9 @@ a_fooX3AfooX2FstringsX00c(uint8_t *arg0, size_t arg1, uint8_t *arg2, extern "C" __attribute__((__weak__, __export_name__("cabi_post_fooX3AfooX2FstringsX00c"))) void - a_cabi_post_fooX3AfooX2FstringsX00c(uint8_t *arg0) { - if ((*((size_t *)(arg0 + 8))) > 0) { - wit::string::drop_raw((void *)(*((uint8_t **)(arg0 + 0)))); + a_cabi_post_fooX3AfooX2FstringsX00c(uint8_t *retptr) { + if ((*((size_t *)(retptr + 8))) > 0) { + wit::string::drop_raw((void *)(*((uint8_t **)(retptr + 0)))); } }