Skip to content

Commit

Permalink
fix Rust and C codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Jul 21, 2024
1 parent 1833302 commit 68bb99c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
24 changes: 17 additions & 7 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,10 @@ impl WorldGenerator for C {
__attribute__((__aligned__({})))
static uint8_t RET_AREA[{}];
",
self.return_pointer_area_align.align_wasm32(),
self.return_pointer_area_size.size_wasm32(),
self.return_pointer_area_align
.format(POINTER_SIZE_EXPRESSION),
self.return_pointer_area_size
.format(POINTER_SIZE_EXPRESSION),
);
}
c_str.push_str(&self.src.c_adapters);
Expand Down Expand Up @@ -1773,8 +1775,8 @@ impl InterfaceGenerator<'_> {
__attribute__((__aligned__({})))
uint8_t ret_area[{}];
",
import_return_pointer_area_align.align_wasm32(),
import_return_pointer_area_size.size_wasm32(),
import_return_pointer_area_align.format(POINTER_SIZE_EXPRESSION),
import_return_pointer_area_size.format(POINTER_SIZE_EXPRESSION),
));
}

Expand Down Expand Up @@ -2174,7 +2176,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
"*(({}*) ({} + {}))",
ty,
operands[0],
offset.format("sizeof(void*)")
offset.format(POINTER_SIZE_EXPRESSION)
));
}

Expand All @@ -2196,7 +2198,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
"*(({}*)({} + {})) = {};",
ty,
operands[1],
offset.format("sizeof(void*)"),
offset.format(POINTER_SIZE_EXPRESSION),
operands[0]
);
}
Expand Down Expand Up @@ -3054,14 +3056,20 @@ impl Bindgen for FunctionBindgen<'_, '_> {
uwriteln!(
self.src,
"uint8_t *base = {ptr} + {i} * {};",
size.format("sizeof(void*)")
size.format(POINTER_SIZE_EXPRESSION)
);
uwriteln!(self.src, "(void) base;");
uwrite!(self.src, "{body}");
uwriteln!(self.src, "}}");
uwriteln!(self.src, "free({ptr});");
uwriteln!(self.src, "}}");
}
Instruction::Flush { amt } => {
for i in 0..*amt {
// no easy way to create a temporary?
results.push(operands[i].clone());
}
}

i => unimplemented!("{:?}", i),
}
Expand Down Expand Up @@ -3289,3 +3297,5 @@ pub fn to_c_ident(name: &str) -> String {
s => s.to_snake_case(),
}
}

pub const POINTER_SIZE_EXPRESSION: &str = "sizeof(void*)";
2 changes: 1 addition & 1 deletion crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(super) struct FunctionBindgen<'a, 'b> {
pub handle_decls: Vec<String>,
}

pub const POINTER_SIZE_EXPRESSION: &str = "std::sizeof(*const u8)";
pub const POINTER_SIZE_EXPRESSION: &str = "core::mem::size_of::<*const u8>()";

impl<'a, 'b> FunctionBindgen<'a, 'b> {
pub(super) fn new(
Expand Down
2 changes: 1 addition & 1 deletion crates/rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ macro_rules! {macro_name} {{
"struct _RetArea([::core::mem::MaybeUninit::<u8>; {size}]);
static mut _RET_AREA: _RetArea = _RetArea([::core::mem::MaybeUninit::uninit(); {size}]);
",
size = self.return_pointer_area_size.format("std::sizeof(usize)"),
size = self.return_pointer_area_size.format(POINTER_SIZE_EXPRESSION),
);
}

Expand Down

0 comments on commit 68bb99c

Please sign in to comment.