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

Automatic adaption to 64bit architectures in guest code #1163

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
80 changes: 58 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ prettyplease = "0.2.20"
syn = { version = "2.0.89", features = ["printing"] }
futures = "0.3.31"

wasmparser = "0.225.0"
wasm-encoder = "0.225.0"
wasm-metadata = "0.225.0"
wit-parser = "0.225.0"
wit-component = "0.225.0"
wasmparser = { git = "https://github.com/bytecodealliance/wasm-tools.git" }
wasm-encoder = { git = "https://github.com/bytecodealliance/wasm-tools.git" }
wasm-metadata = { git = "https://github.com/bytecodealliance/wasm-tools.git" }
wit-parser = { git = "https://github.com/bytecodealliance/wasm-tools.git" }
wit-component = { git = "https://github.com/bytecodealliance/wasm-tools.git" }

wit-bindgen-core = { path = 'crates/core', version = '0.39.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.39.0' }
Expand Down
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 @@ -1788,12 +1790,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 @@ -2136,8 +2140,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 @@ -2165,8 +2169,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 @@ -2179,23 +2183,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 @@ -2245,7 +2266,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 @@ -3049,8 +3070,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 @@ -3288,3 +3313,5 @@ pub fn to_c_ident(name: &str) -> String {
s => s.to_snake_case(),
}
}

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