Skip to content

Commit 031fc61

Browse files
authored
size calculation for wasm32 and wasm64 (#1446)
* size calculation for wasm32 and wasm64 * use a member enum to select the abi width
1 parent 07a8652 commit 031fc61

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

crates/wit-parser/src/sizealign.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
use crate::{FlagsRepr, Int, Resolve, Type, TypeDef, TypeDefKind};
22

3+
#[derive(Default)]
4+
pub enum AddressSize {
5+
#[default]
6+
Wasm32,
7+
Wasm64,
8+
}
9+
310
#[derive(Default)]
411
pub struct SizeAlign {
512
map: Vec<(usize, usize)>,
13+
wasm_type: AddressSize,
614
}
715

816
impl SizeAlign {
17+
pub fn new(wasm_type: AddressSize) -> Self {
18+
Self {
19+
map: Vec::new(),
20+
wasm_type,
21+
}
22+
}
23+
924
pub fn fill(&mut self, resolve: &Resolve) {
1025
self.map = Vec::new();
1126
for (_, ty) in resolve.types.iter() {
@@ -17,7 +32,13 @@ impl SizeAlign {
1732
fn calculate(&self, ty: &TypeDef) -> (usize, usize) {
1833
match &ty.kind {
1934
TypeDefKind::Type(t) => (self.size(t), self.align(t)),
20-
TypeDefKind::List(_) => (8, 4),
35+
TypeDefKind::List(_) => {
36+
if matches!(self.wasm_type, AddressSize::Wasm64) {
37+
(16, 8)
38+
} else {
39+
(8, 4)
40+
}
41+
}
2142
TypeDefKind::Record(r) => self.record(r.fields.iter().map(|f| &f.ty)),
2243
TypeDefKind::Tuple(t) => self.record(t.types.iter()),
2344
TypeDefKind::Flags(f) => match f.repr() {
@@ -47,7 +68,14 @@ impl SizeAlign {
4768
Type::Bool | Type::U8 | Type::S8 => 1,
4869
Type::U16 | Type::S16 => 2,
4970
Type::U32 | Type::S32 | Type::Float32 | Type::Char => 4,
50-
Type::U64 | Type::S64 | Type::Float64 | Type::String => 8,
71+
Type::U64 | Type::S64 | Type::Float64 => 8,
72+
Type::String => {
73+
if matches!(self.wasm_type, AddressSize::Wasm64) {
74+
16
75+
} else {
76+
8
77+
}
78+
}
5179
Type::Id(id) => self.map[id.index()].0,
5280
}
5381
}
@@ -56,8 +84,15 @@ impl SizeAlign {
5684
match ty {
5785
Type::Bool | Type::U8 | Type::S8 => 1,
5886
Type::U16 | Type::S16 => 2,
59-
Type::U32 | Type::S32 | Type::Float32 | Type::Char | Type::String => 4,
87+
Type::U32 | Type::S32 | Type::Float32 | Type::Char => 4,
6088
Type::U64 | Type::S64 | Type::Float64 => 8,
89+
Type::String => {
90+
if matches!(self.wasm_type, AddressSize::Wasm64) {
91+
8
92+
} else {
93+
4
94+
}
95+
}
6196
Type::Id(id) => self.map[id.index()].1,
6297
}
6398
}

0 commit comments

Comments
 (0)