Skip to content

Commit 23af6c2

Browse files
committed
add for_each_op macro to wasmi_ir2
1 parent 8e53cc4 commit 23af6c2

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

crates/ir2/build/display/op.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::build::{
1212
Field,
1313
GenericOp,
1414
LoadOp,
15+
Op,
1516
StoreOp,
1617
TableGetOp,
1718
TableSetOp,
@@ -67,6 +68,26 @@ where
6768
}
6869
}
6970

71+
pub struct DisplayForEachOpBody<T> {
72+
pub value: T,
73+
pub indent: Indent,
74+
}
75+
76+
impl<T> DisplayForEachOpBody<T> {
77+
pub fn new(value: T, indent: Indent) -> Self {
78+
Self { value, indent }
79+
}
80+
}
81+
82+
impl Display for DisplayForEachOpBody<&'_ Op> {
83+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84+
let indent = self.indent.inc();
85+
let camel_ident = DisplayIdent::camel(self.value);
86+
let snake_ident = DisplayIdent::snake(self.value);
87+
write!(f, "{indent}{snake_ident} => {camel_ident}")
88+
}
89+
}
90+
7091
impl Display for DisplayOp<&'_ Isa> {
7192
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7293
let indent = self.indent;
@@ -77,6 +98,13 @@ impl Display for DisplayOp<&'_ Isa> {
7798
.iter()
7899
.map(|op| DisplayOp::new(op, indent.inc())),
79100
);
101+
let for_each_op_body = DisplaySequence::new(
102+
",\n",
103+
self.value
104+
.ops
105+
.iter()
106+
.map(|op| DisplayForEachOpBody::new(op, indent.inc())),
107+
);
80108
write!(
81109
f,
82110
"\
@@ -92,6 +120,33 @@ impl Display for DisplayOp<&'_ Isa> {
92120
{indent}pub enum Op {{\n\
93121
{variants}\n\
94122
{indent}}}\n\
123+
\n\
124+
{indent}/// Expands `mac` using the snake-case and camel-case identifiers of all operators.
125+
{indent}/// \n\
126+
{indent}/// # Note\n\
127+
{indent}/// \n\
128+
{indent}/// Simd related operators are only included if the `simd` crate feature is enabled.\n\
129+
{indent}/// \n\
130+
{indent}/// # Example\n\
131+
{indent}/// \n\
132+
{indent}/// The expanded code format fed to the `mac` macro is as follows:\n\
133+
{indent}/// \n\
134+
{indent}/// ```\n\
135+
{indent}/// i32_add_sss => I32Add_Sss,\n\
136+
{indent}/// i32_add_ssi => I32Add_Ssi,\n\
137+
{indent}/// i32_sub_sss => I32Sub_Sss,\n\
138+
{indent}/// i32_sub_ssi => I32Sub_Ssi,\n\
139+
{indent}/// i32_sub_sis => I32Sub_Sis,\n\
140+
{indent}/// i32_mul_sss => I32Mul_Sss,\n\
141+
{indent}/// i32_mul_ssi => I32Mul_Ssi,\n\
142+
{indent}/// etc ..\n\
143+
{indent}/// ```\n\
144+
{indent}#[macro_export]\n\
145+
{indent}macro_rules! for_each_op {{\n\
146+
{indent} ($mac:ident) => {{\n\
147+
{for_each_op_body},\n\
148+
{indent} }};\n\
149+
{indent}}}\n\
95150
"
96151
)
97152
}

crates/ir2/build/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ pub fn generate_code(config: &Config) -> Result<(), Error> {
7575

7676
fn generate_op_rs(config: &Config, isa: &Isa, contents: &mut String) -> Result<(), Error> {
7777
let expected_size = match config.simd {
78-
true => 235_000,
79-
false => 150_000,
78+
true => 270_000,
79+
false => 170_000,
8080
};
8181
write_to_buffer(contents, expected_size, |buffer| {
8282
write!(

0 commit comments

Comments
 (0)