Skip to content

Automatic adaption to 64bit architectures in guest code #1163

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

Merged
merged 3 commits into from
Mar 11, 2025
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
67 changes: 47 additions & 20 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ struct C {
opts: Opts,
h_includes: Vec<String>,
c_includes: Vec<String>,
return_pointer_area_size: usize,
return_pointer_area_align: usize,
return_pointer_area_size: ArchitectureSize,
return_pointer_area_align: Alignment,
names: Ns,
needs_string: bool,
needs_union_int32_float: bool,
Expand Down Expand Up @@ -463,16 +463,18 @@ impl WorldGenerator for C {
// Declare a statically-allocated return area, if needed. We only do
// this for export bindings, because import bindings allocate their
// return-area on the stack.
if self.return_pointer_area_size > 0 {
if !self.return_pointer_area_size.is_empty() {
// Automatic indentation avoided due to `extern "C" {` declaration
uwrite!(
c_str,
"
__attribute__((__aligned__({})))
static uint8_t RET_AREA[{}];
",
self.return_pointer_area_align,
self.return_pointer_area_size,
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 @@ -1779,12 +1781,14 @@ impl InterfaceGenerator<'_> {
..
} = f;

if import_return_pointer_area_size > 0 {
if !import_return_pointer_area_size.is_empty() {
self.src.c_adapters(&format!(
"\
__attribute__((__aligned__({import_return_pointer_area_align})))
uint8_t ret_area[{import_return_pointer_area_size}];
__attribute__((__aligned__({})))
uint8_t ret_area[{}];
",
import_return_pointer_area_align.format(POINTER_SIZE_EXPRESSION),
import_return_pointer_area_size.format(POINTER_SIZE_EXPRESSION),
));
}

Expand Down Expand Up @@ -2121,8 +2125,8 @@ struct FunctionBindgen<'a, 'b> {
params: Vec<String>,
wasm_return: Option<String>,
ret_store_cnt: usize,
import_return_pointer_area_size: usize,
import_return_pointer_area_align: usize,
import_return_pointer_area_size: ArchitectureSize,
import_return_pointer_area_align: Alignment,

/// Borrows observed during lifting an export, that will need to be dropped when the guest
/// function exits.
Expand Down Expand Up @@ -2150,8 +2154,8 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
params: Vec::new(),
wasm_return: None,
ret_store_cnt: 0,
import_return_pointer_area_size: 0,
import_return_pointer_area_align: 0,
import_return_pointer_area_size: Default::default(),
import_return_pointer_area_align: Default::default(),
borrow_decls: Default::default(),
borrows: Vec::new(),
}
Expand All @@ -2164,23 +2168,40 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
self.src.push_str(";\n");
}

fn load(&mut self, ty: &str, offset: i32, operands: &[String], results: &mut Vec<String>) {
results.push(format!("*(({}*) ({} + {}))", ty, operands[0], offset));
fn load(
&mut self,
ty: &str,
offset: ArchitectureSize,
operands: &[String],
results: &mut Vec<String>,
) {
results.push(format!(
"*(({}*) ({} + {}))",
ty,
operands[0],
offset.format(POINTER_SIZE_EXPRESSION)
));
}

fn load_ext(&mut self, ty: &str, offset: i32, operands: &[String], results: &mut Vec<String>) {
fn load_ext(
&mut self,
ty: &str,
offset: ArchitectureSize,
operands: &[String],
results: &mut Vec<String>,
) {
self.load(ty, offset, operands, results);
let result = results.pop().unwrap();
results.push(format!("(int32_t) {}", result));
}

fn store(&mut self, ty: &str, offset: i32, operands: &[String]) {
fn store(&mut self, ty: &str, offset: ArchitectureSize, operands: &[String]) {
uwriteln!(
self.src,
"*(({}*)({} + {})) = {};",
ty,
operands[1],
offset,
offset.format(POINTER_SIZE_EXPRESSION),
operands[0]
);
}
Expand Down Expand Up @@ -2230,7 +2251,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.blocks.push((src.into(), mem::take(operands)));
}

fn return_pointer(&mut self, size: usize, align: usize) -> String {
fn return_pointer(&mut self, size: ArchitectureSize, align: Alignment) -> String {
let ptr = self.locals.tmp("ptr");

// Use a stack-based return area for imports, because exports need
Expand Down Expand Up @@ -3034,8 +3055,12 @@ 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).size_wasm32();
uwriteln!(self.src, "uint8_t *base = {ptr} + {i} * {size};");
let size = self.gen.gen.sizes.size(element);
uwriteln!(
self.src,
"uint8_t *base = {ptr} + {i} * {};",
size.format(POINTER_SIZE_EXPRESSION)
);
uwriteln!(self.src, "(void) base;");
uwrite!(self.src, "{body}");
uwriteln!(self.src, "}}");
Expand Down Expand Up @@ -3272,3 +3297,5 @@ pub fn to_c_ident(name: &str) -> String {
s => s.to_snake_case(),
}
}

const POINTER_SIZE_EXPRESSION: &str = "sizeof(void*)";
Loading
Loading