Skip to content

Commit e1f76e2

Browse files
authored
Simplify wasmi_ir codegen build scripts (#1831)
* use new scheme to define unary ops * apply rustfmt * use new scheme to define binary ops * remove Ty::S{32,64} variants * use IdentPrefix in more places * remove Ty::S* SIMD variants * dedupe Display code * add and use From<Ty> for FieldTy * use new scheme to define cmp+branch ops * move Ty and FieldTy into their own module * add MemoryOperand enum and use for load ops * add OffsetOperand and use it in load ops * no longer use S prefix * remove remaining uses of S prefixed identifiers * use new scheme to define load ops This also necessitated new identifiers for some load operators. Generally, load operator identifiers should be more uniform now. * merge LoadLane into LoadOp variant * move unnecessary LoadKind params * use {Memory,Offset}Operand in StoreOp * use Self in match arms * use new scheme to define store ops This also necessitated some renamings but all in all the identifiers of store ops are more uniform now. * refactor LoadKind type * remove unused Ty variants * refactor StoreKind type * remove unused Ty variants
1 parent 12c9fd2 commit e1f76e2

19 files changed

Lines changed: 1545 additions & 2600 deletions

File tree

crates/ir/build/display/constructors.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::build::{
1515
TernaryOp,
1616
UnaryOp,
1717
V128ExtractLaneOp,
18-
V128LoadLaneOp,
1918
V128ReplaceLaneOp,
2019
},
2120
};
@@ -112,7 +111,6 @@ impl_display_constructor! {
112111
TableSetOp,
113112
V128ReplaceLaneOp,
114113
V128ExtractLaneOp,
115-
V128LoadLaneOp,
116114
}
117115

118116
impl<const N: usize> Display for DisplayConstructor<&'_ GenericOp<N>> {

crates/ir/build/display/decode.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ use crate::build::{
44
Indent,
55
Suffix,
66
ident::DisplayIdent,
7-
utils::{DisplayConcat, DisplaySequence, IntoDisplayMaybe as _},
7+
utils::{DisplayConcat, DisplayMaybe, DisplaySequence, IntoDisplayMaybe as _},
88
},
99
ident::{CamelCase, Ident, SnakeCase},
1010
op::{
1111
BinaryOp,
1212
CmpBranchOp,
1313
GenericOp,
1414
LaneWidth,
15+
LoadKind,
1516
LoadOp,
17+
MemoryOperand,
18+
OffsetOperand,
1619
OperandKind,
1720
SelectOp,
1821
StoreOp,
@@ -21,9 +24,9 @@ use crate::build::{
2124
TernaryOp,
2225
UnaryOp,
2326
V128ExtractLaneOp,
24-
V128LoadLaneOp,
2527
V128ReplaceLaneOp,
2628
},
29+
ty::FieldTy,
2730
};
2831
use core::fmt::{self, Display};
2932

@@ -112,14 +115,30 @@ impl Display for DisplayDecode<&'_ LoadOp> {
112115
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113116
let op = self.value;
114117
let camel_ident = DisplayIdent::camel(op);
115-
let mem0_offset16 = (op.mem0 && op.offset16)
116-
.then_some("Mem0Offset16")
117-
.display_maybe();
118+
let mem0_suffix = match op.mem {
119+
MemoryOperand::Immediate => "",
120+
MemoryOperand::Mem0 => "Mem0",
121+
};
122+
let offset16_suffix = match op.offset {
123+
OffsetOperand::Offset => "",
124+
OffsetOperand::Offset16 => "Offset16",
125+
};
126+
let (lane_suffix, lane_param) = match op.kind {
127+
LoadKind::Lane { width } => {
128+
let lane_param = DisplayConcat(('<', FieldTy::from(width), '>'));
129+
(
130+
Some(CamelCase(Ident::Lane)),
131+
Some(lane_param).display_maybe(),
132+
)
133+
}
134+
_ => (None, DisplayMaybe::None),
135+
};
136+
let lane_suffix = lane_suffix.display_maybe();
118137
let result_suffix = CamelCase(Suffix(OperandKind::Slot));
119138
let ptr_suffix = SnakeCase(Suffix(op.ptr));
120139
writeln!(
121140
f,
122-
"pub type {camel_ident} = LoadOp{mem0_offset16}_{result_suffix}{ptr_suffix};"
141+
"pub type {camel_ident} = Load{lane_suffix}Op{mem0_suffix}{offset16_suffix}_{result_suffix}{ptr_suffix}{lane_param};"
123142
)
124143
}
125144
}
@@ -132,9 +151,14 @@ impl Display for DisplayDecode<&'_ StoreOp> {
132151
.laneidx_field()
133152
.map(|_| CamelCase(Ident::Lane))
134153
.display_maybe();
135-
let mem0_offset16 = (op.mem0 && op.offset16)
136-
.then_some("Mem0Offset16")
137-
.display_maybe();
154+
let mem0_suffix = match op.mem {
155+
MemoryOperand::Immediate => "",
156+
MemoryOperand::Mem0 => "Mem0",
157+
};
158+
let offset16_suffix = match op.offset {
159+
OffsetOperand::Offset => "",
160+
OffsetOperand::Offset16 => "Offset16",
161+
};
138162
let ptr_suffix = CamelCase(Suffix(op.ptr));
139163
let value_ty = op.value_field().ty;
140164
let laneidx_ty = op
@@ -144,7 +168,7 @@ impl Display for DisplayDecode<&'_ StoreOp> {
144168
.display_maybe();
145169
writeln!(
146170
f,
147-
"pub type {camel_ident} = Store{lane_ident}Op{mem0_offset16}_{ptr_suffix}<{value_ty}{laneidx_ty}>;"
171+
"pub type {camel_ident} = Store{lane_ident}Op{mem0_suffix}{offset16_suffix}_{ptr_suffix}<{value_ty}{laneidx_ty}>;"
148172
)
149173
}
150174
}
@@ -238,20 +262,3 @@ impl Display for DisplayDecode<&'_ V128ExtractLaneOp> {
238262
)
239263
}
240264
}
241-
242-
impl Display for DisplayDecode<&'_ V128LoadLaneOp> {
243-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
244-
let op = self.value;
245-
let camel_ident = DisplayIdent::camel(op);
246-
let result_suffix = CamelCase(Suffix(OperandKind::Slot));
247-
let mem0_offset16 = (op.mem0 && op.offset16)
248-
.then_some("Mem0Offset16")
249-
.display_maybe();
250-
let ptr_suffix = SnakeCase(Suffix(op.ptr));
251-
let laneidx = op.width.to_laneidx();
252-
writeln!(
253-
f,
254-
"pub type {camel_ident} = V128LoadLaneOp{mem0_offset16}_{result_suffix}{ptr_suffix}<{laneidx}>;"
255-
)
256-
}
257-
}

crates/ir/build/display/encode.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::build::{
1515
TernaryOp,
1616
UnaryOp,
1717
V128ExtractLaneOp,
18-
V128LoadLaneOp,
1918
V128ReplaceLaneOp,
2019
},
2120
};
@@ -114,7 +113,6 @@ impl_display_encode! {
114113
TableSetOp,
115114
V128ReplaceLaneOp,
116115
V128ExtractLaneOp,
117-
V128LoadLaneOp,
118116
}
119117

120118
impl<const N: usize> Display for DisplayEncode<&'_ GenericOp<N>> {

0 commit comments

Comments
 (0)