Skip to content

Commit 8ddd1be

Browse files
committed
Automatic adaption to 64bit architectures in guest code
1 parent fc4f633 commit 8ddd1be

File tree

10 files changed

+466
-308
lines changed

10 files changed

+466
-308
lines changed

Cargo.lock

Lines changed: 58 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ prettyplease = "0.2.20"
3333
syn = { version = "2.0.89", features = ["printing"] }
3434
futures = "0.3.31"
3535

36-
wasmparser = "0.225.0"
37-
wasm-encoder = "0.225.0"
38-
wasm-metadata = "0.225.0"
39-
wit-parser = "0.225.0"
40-
wit-component = "0.225.0"
36+
wasmparser = { git = "https://github.com/bytecodealliance/wasm-tools.git" }
37+
wasm-encoder = { git = "https://github.com/bytecodealliance/wasm-tools.git" }
38+
wasm-metadata = { git = "https://github.com/bytecodealliance/wasm-tools.git" }
39+
wit-parser = { git = "https://github.com/bytecodealliance/wasm-tools.git" }
40+
wit-component = { git = "https://github.com/bytecodealliance/wasm-tools.git" }
4141

4242
wit-bindgen-core = { path = 'crates/core', version = '0.39.0' }
4343
wit-bindgen-c = { path = 'crates/c', version = '0.39.0' }

crates/c/src/lib.rs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ struct C {
1818
opts: Opts,
1919
h_includes: Vec<String>,
2020
c_includes: Vec<String>,
21-
return_pointer_area_size: usize,
22-
return_pointer_area_align: usize,
21+
return_pointer_area_size: ArchitectureSize,
22+
return_pointer_area_align: Alignment,
2323
names: Ns,
2424
needs_string: bool,
2525
needs_union_int32_float: bool,
@@ -463,16 +463,18 @@ impl WorldGenerator for C {
463463
// Declare a statically-allocated return area, if needed. We only do
464464
// this for export bindings, because import bindings allocate their
465465
// return-area on the stack.
466-
if self.return_pointer_area_size > 0 {
466+
if !self.return_pointer_area_size.is_empty() {
467467
// Automatic indentation avoided due to `extern "C" {` declaration
468468
uwrite!(
469469
c_str,
470470
"
471471
__attribute__((__aligned__({})))
472472
static uint8_t RET_AREA[{}];
473473
",
474-
self.return_pointer_area_align,
475-
self.return_pointer_area_size,
474+
self.return_pointer_area_align
475+
.format(POINTER_SIZE_EXPRESSION),
476+
self.return_pointer_area_size
477+
.format(POINTER_SIZE_EXPRESSION),
476478
);
477479
}
478480
c_str.push_str(&self.src.c_adapters);
@@ -1788,12 +1790,14 @@ impl InterfaceGenerator<'_> {
17881790
..
17891791
} = f;
17901792

1791-
if import_return_pointer_area_size > 0 {
1793+
if !import_return_pointer_area_size.is_empty() {
17921794
self.src.c_adapters(&format!(
17931795
"\
1794-
__attribute__((__aligned__({import_return_pointer_area_align})))
1795-
uint8_t ret_area[{import_return_pointer_area_size}];
1796+
__attribute__((__aligned__({})))
1797+
uint8_t ret_area[{}];
17961798
",
1799+
import_return_pointer_area_align.format(POINTER_SIZE_EXPRESSION),
1800+
import_return_pointer_area_size.format(POINTER_SIZE_EXPRESSION),
17971801
));
17981802
}
17991803

@@ -2136,8 +2140,8 @@ struct FunctionBindgen<'a, 'b> {
21362140
params: Vec<String>,
21372141
wasm_return: Option<String>,
21382142
ret_store_cnt: usize,
2139-
import_return_pointer_area_size: usize,
2140-
import_return_pointer_area_align: usize,
2143+
import_return_pointer_area_size: ArchitectureSize,
2144+
import_return_pointer_area_align: Alignment,
21412145

21422146
/// Borrows observed during lifting an export, that will need to be dropped when the guest
21432147
/// function exits.
@@ -2165,8 +2169,8 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
21652169
params: Vec::new(),
21662170
wasm_return: None,
21672171
ret_store_cnt: 0,
2168-
import_return_pointer_area_size: 0,
2169-
import_return_pointer_area_align: 0,
2172+
import_return_pointer_area_size: Default::default(),
2173+
import_return_pointer_area_align: Default::default(),
21702174
borrow_decls: Default::default(),
21712175
borrows: Vec::new(),
21722176
}
@@ -2179,23 +2183,40 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
21792183
self.src.push_str(";\n");
21802184
}
21812185

2182-
fn load(&mut self, ty: &str, offset: i32, operands: &[String], results: &mut Vec<String>) {
2183-
results.push(format!("*(({}*) ({} + {}))", ty, operands[0], offset));
2186+
fn load(
2187+
&mut self,
2188+
ty: &str,
2189+
offset: ArchitectureSize,
2190+
operands: &[String],
2191+
results: &mut Vec<String>,
2192+
) {
2193+
results.push(format!(
2194+
"*(({}*) ({} + {}))",
2195+
ty,
2196+
operands[0],
2197+
offset.format(POINTER_SIZE_EXPRESSION)
2198+
));
21842199
}
21852200

2186-
fn load_ext(&mut self, ty: &str, offset: i32, operands: &[String], results: &mut Vec<String>) {
2201+
fn load_ext(
2202+
&mut self,
2203+
ty: &str,
2204+
offset: ArchitectureSize,
2205+
operands: &[String],
2206+
results: &mut Vec<String>,
2207+
) {
21872208
self.load(ty, offset, operands, results);
21882209
let result = results.pop().unwrap();
21892210
results.push(format!("(int32_t) {}", result));
21902211
}
21912212

2192-
fn store(&mut self, ty: &str, offset: i32, operands: &[String]) {
2213+
fn store(&mut self, ty: &str, offset: ArchitectureSize, operands: &[String]) {
21932214
uwriteln!(
21942215
self.src,
21952216
"*(({}*)({} + {})) = {};",
21962217
ty,
21972218
operands[1],
2198-
offset,
2219+
offset.format(POINTER_SIZE_EXPRESSION),
21992220
operands[0]
22002221
);
22012222
}
@@ -2245,7 +2266,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
22452266
self.blocks.push((src.into(), mem::take(operands)));
22462267
}
22472268

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

22512272
// Use a stack-based return area for imports, because exports need
@@ -3049,8 +3070,12 @@ impl Bindgen for FunctionBindgen<'_, '_> {
30493070
uwriteln!(self.src, "uint8_t *{ptr} = {};", operands[0]);
30503071
let i = self.locals.tmp("i");
30513072
uwriteln!(self.src, "for (size_t {i} = 0; {i} < {len}; {i}++) {{");
3052-
let size = self.gen.gen.sizes.size(element).size_wasm32();
3053-
uwriteln!(self.src, "uint8_t *base = {ptr} + {i} * {size};");
3073+
let size = self.gen.gen.sizes.size(element);
3074+
uwriteln!(
3075+
self.src,
3076+
"uint8_t *base = {ptr} + {i} * {};",
3077+
size.format(POINTER_SIZE_EXPRESSION)
3078+
);
30543079
uwriteln!(self.src, "(void) base;");
30553080
uwrite!(self.src, "{body}");
30563081
uwriteln!(self.src, "}}");
@@ -3288,3 +3313,5 @@ pub fn to_c_ident(name: &str) -> String {
32883313
s => s.to_snake_case(),
32893314
}
32903315
}
3316+
3317+
const POINTER_SIZE_EXPRESSION: &str = "sizeof(void*)";

0 commit comments

Comments
 (0)