Skip to content

Commit

Permalink
revised symmetric code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Jul 18, 2024
1 parent 5887d50 commit fc24a0d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
25 changes: 15 additions & 10 deletions crates/core/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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],
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions crates/cpp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
48 changes: 24 additions & 24 deletions crates/cpp/tests/meshless_strings/component_a/the_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 =
Expand All @@ -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))));
}
}

Expand Down

0 comments on commit fc24a0d

Please sign in to comment.