Skip to content

Commit 0e191cc

Browse files
authored
Automatic adaption to 64bit architectures in guest code (#1163)
* Automatic adaption to 64bit architectures in guest code * prefer absolute path for core crate * fix rust codegen after merge
1 parent d73c073 commit 0e191cc

File tree

8 files changed

+386
-269
lines changed

8 files changed

+386
-269
lines changed

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);
@@ -1779,12 +1781,14 @@ impl InterfaceGenerator<'_> {
17791781
..
17801782
} = f;
17811783

1782-
if import_return_pointer_area_size > 0 {
1784+
if !import_return_pointer_area_size.is_empty() {
17831785
self.src.c_adapters(&format!(
17841786
"\
1785-
__attribute__((__aligned__({import_return_pointer_area_align})))
1786-
uint8_t ret_area[{import_return_pointer_area_size}];
1787+
__attribute__((__aligned__({})))
1788+
uint8_t ret_area[{}];
17871789
",
1790+
import_return_pointer_area_align.format(POINTER_SIZE_EXPRESSION),
1791+
import_return_pointer_area_size.format(POINTER_SIZE_EXPRESSION),
17881792
));
17891793
}
17901794

@@ -2121,8 +2125,8 @@ struct FunctionBindgen<'a, 'b> {
21212125
params: Vec<String>,
21222126
wasm_return: Option<String>,
21232127
ret_store_cnt: usize,
2124-
import_return_pointer_area_size: usize,
2125-
import_return_pointer_area_align: usize,
2128+
import_return_pointer_area_size: ArchitectureSize,
2129+
import_return_pointer_area_align: Alignment,
21262130

21272131
/// Borrows observed during lifting an export, that will need to be dropped when the guest
21282132
/// function exits.
@@ -2150,8 +2154,8 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
21502154
params: Vec::new(),
21512155
wasm_return: None,
21522156
ret_store_cnt: 0,
2153-
import_return_pointer_area_size: 0,
2154-
import_return_pointer_area_align: 0,
2157+
import_return_pointer_area_size: Default::default(),
2158+
import_return_pointer_area_align: Default::default(),
21552159
borrow_decls: Default::default(),
21562160
borrows: Vec::new(),
21572161
}
@@ -2164,23 +2168,40 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
21642168
self.src.push_str(";\n");
21652169
}
21662170

2167-
fn load(&mut self, ty: &str, offset: i32, operands: &[String], results: &mut Vec<String>) {
2168-
results.push(format!("*(({}*) ({} + {}))", ty, operands[0], offset));
2171+
fn load(
2172+
&mut self,
2173+
ty: &str,
2174+
offset: ArchitectureSize,
2175+
operands: &[String],
2176+
results: &mut Vec<String>,
2177+
) {
2178+
results.push(format!(
2179+
"*(({}*) ({} + {}))",
2180+
ty,
2181+
operands[0],
2182+
offset.format(POINTER_SIZE_EXPRESSION)
2183+
));
21692184
}
21702185

2171-
fn load_ext(&mut self, ty: &str, offset: i32, operands: &[String], results: &mut Vec<String>) {
2186+
fn load_ext(
2187+
&mut self,
2188+
ty: &str,
2189+
offset: ArchitectureSize,
2190+
operands: &[String],
2191+
results: &mut Vec<String>,
2192+
) {
21722193
self.load(ty, offset, operands, results);
21732194
let result = results.pop().unwrap();
21742195
results.push(format!("(int32_t) {}", result));
21752196
}
21762197

2177-
fn store(&mut self, ty: &str, offset: i32, operands: &[String]) {
2198+
fn store(&mut self, ty: &str, offset: ArchitectureSize, operands: &[String]) {
21782199
uwriteln!(
21792200
self.src,
21802201
"*(({}*)({} + {})) = {};",
21812202
ty,
21822203
operands[1],
2183-
offset,
2204+
offset.format(POINTER_SIZE_EXPRESSION),
21842205
operands[0]
21852206
);
21862207
}
@@ -2230,7 +2251,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
22302251
self.blocks.push((src.into(), mem::take(operands)));
22312252
}
22322253

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

22362257
// Use a stack-based return area for imports, because exports need
@@ -3034,8 +3055,12 @@ impl Bindgen for FunctionBindgen<'_, '_> {
30343055
uwriteln!(self.src, "uint8_t *{ptr} = {};", operands[0]);
30353056
let i = self.locals.tmp("i");
30363057
uwriteln!(self.src, "for (size_t {i} = 0; {i} < {len}; {i}++) {{");
3037-
let size = self.gen.gen.sizes.size(element).size_wasm32();
3038-
uwriteln!(self.src, "uint8_t *base = {ptr} + {i} * {size};");
3058+
let size = self.gen.gen.sizes.size(element);
3059+
uwriteln!(
3060+
self.src,
3061+
"uint8_t *base = {ptr} + {i} * {};",
3062+
size.format(POINTER_SIZE_EXPRESSION)
3063+
);
30393064
uwriteln!(self.src, "(void) base;");
30403065
uwrite!(self.src, "{body}");
30413066
uwriteln!(self.src, "}}");
@@ -3272,3 +3297,5 @@ pub fn to_c_ident(name: &str) -> String {
32723297
s => s.to_snake_case(),
32733298
}
32743299
}
3300+
3301+
const POINTER_SIZE_EXPRESSION: &str = "sizeof(void*)";

0 commit comments

Comments
 (0)