Skip to content

Commit dcd74ec

Browse files
committed
use a member enum to select the abi width
1 parent 1c68f86 commit dcd74ec

1 file changed

Lines changed: 17 additions & 71 deletions

File tree

crates/wit-parser/src/sizealign.rs

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

3-
pub type SizeAlign = SizeAlignSelect;
4-
5-
// Data type with construction selected variant
6-
pub enum SizeAlignSelect {
7-
Wasm32(SizeAlignAbi<false>),
8-
Wasm64(SizeAlignAbi<true>),
3+
#[derive(Default)]
4+
pub enum AddressSize {
5+
#[default]
6+
Wasm32,
7+
Wasm64,
98
}
109

11-
impl std::default::Default for SizeAlignSelect {
12-
fn default() -> Self {
13-
Self::Wasm32(Default::default())
14-
}
10+
#[derive(Default)]
11+
pub struct SizeAlign {
12+
map: Vec<(usize, usize)>,
13+
wasm_type: AddressSize,
1514
}
1615

17-
impl SizeAlignSelect {
18-
pub fn fill(&mut self, resolve: &Resolve) {
19-
match self {
20-
SizeAlignSelect::Wasm32(sna) => sna.fill(resolve),
21-
SizeAlignSelect::Wasm64(sna) => sna.fill(resolve),
22-
}
23-
}
24-
25-
pub fn size(&self, ty: &Type) -> usize {
26-
match self {
27-
SizeAlignSelect::Wasm32(sna) => sna.size(ty),
28-
SizeAlignSelect::Wasm64(sna) => sna.size(ty),
29-
}
30-
}
31-
32-
pub fn align(&self, ty: &Type) -> usize {
33-
match self {
34-
SizeAlignSelect::Wasm32(sna) => sna.align(ty),
35-
SizeAlignSelect::Wasm64(sna) => sna.align(ty),
36-
}
37-
}
38-
39-
pub fn record<'a>(&self, types: impl Iterator<Item = &'a Type>) -> (usize, usize) {
40-
match self {
41-
SizeAlignSelect::Wasm32(sna) => sna.record(types),
42-
SizeAlignSelect::Wasm64(sna) => sna.record(types),
43-
}
44-
}
45-
46-
pub fn params<'a>(&self, types: impl IntoIterator<Item = &'a Type>) -> (usize, usize) {
47-
self.record(types.into_iter())
48-
}
49-
50-
pub fn field_offsets<'a>(
51-
&self,
52-
types: impl IntoIterator<Item = &'a Type>,
53-
) -> Vec<(usize, &'a Type)> {
54-
match self {
55-
SizeAlignSelect::Wasm32(sna) => sna.field_offsets(types),
56-
SizeAlignSelect::Wasm64(sna) => sna.field_offsets(types),
16+
impl SizeAlign {
17+
pub fn new(wasm_type: AddressSize) -> Self {
18+
Self {
19+
map: Vec::new(),
20+
wasm_type,
5721
}
5822
}
5923

60-
pub fn payload_offset<'a>(
61-
&self,
62-
tag: Int,
63-
cases: impl IntoIterator<Item = Option<&'a Type>>,
64-
) -> usize {
65-
match self {
66-
SizeAlignSelect::Wasm32(sna) => sna.payload_offset(tag, cases),
67-
SizeAlignSelect::Wasm64(sna) => sna.payload_offset(tag, cases),
68-
}
69-
}
70-
}
71-
72-
#[derive(Default)]
73-
pub struct SizeAlignAbi<const wasm64: bool> {
74-
map: Vec<(usize, usize)>,
75-
}
76-
77-
impl<const wasm64: bool> SizeAlignAbi<wasm64> {
7824
pub fn fill(&mut self, resolve: &Resolve) {
7925
self.map = Vec::new();
8026
for (_, ty) in resolve.types.iter() {
@@ -87,7 +33,7 @@ impl<const wasm64: bool> SizeAlignAbi<wasm64> {
8733
match &ty.kind {
8834
TypeDefKind::Type(t) => (self.size(t), self.align(t)),
8935
TypeDefKind::List(_) => {
90-
if wasm64 {
36+
if matches!(self.wasm_type, AddressSize::Wasm64) {
9137
(16, 8)
9238
} else {
9339
(8, 4)
@@ -124,7 +70,7 @@ impl<const wasm64: bool> SizeAlignAbi<wasm64> {
12470
Type::U32 | Type::S32 | Type::Float32 | Type::Char => 4,
12571
Type::U64 | Type::S64 | Type::Float64 => 8,
12672
Type::String => {
127-
if wasm64 {
73+
if matches!(self.wasm_type, AddressSize::Wasm64) {
12874
16
12975
} else {
13076
8
@@ -141,7 +87,7 @@ impl<const wasm64: bool> SizeAlignAbi<wasm64> {
14187
Type::U32 | Type::S32 | Type::Float32 | Type::Char => 4,
14288
Type::U64 | Type::S64 | Type::Float64 => 8,
14389
Type::String => {
144-
if wasm64 {
90+
if matches!(self.wasm_type, AddressSize::Wasm64) {
14591
8
14692
} else {
14793
4

0 commit comments

Comments
 (0)