Skip to content

Commit 5082b07

Browse files
authored
wasmi_ir2: generate Op::result_ref getter method (#1663)
* change codegen of Op::result_mut * add Op::result_ref method * bump expected filesizes * simplify result_{mut,ref} code generator * rename result_mut.rs file into just result.rs * fix indentation of Op::code method * mention utility methods in docs of Op type * add proper newlines for docs
1 parent fb952ab commit 5082b07

File tree

6 files changed

+99
-156
lines changed

6 files changed

+99
-156
lines changed

crates/ir2/build/display/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod encode;
44
mod ident;
55
mod op;
66
mod op_code;
7-
mod result_mut;
7+
mod result;
88
mod utils;
99

1010
pub use self::{
@@ -13,7 +13,7 @@ pub use self::{
1313
encode::DisplayEncode,
1414
op::DisplayOp,
1515
op_code::DisplayOpCode,
16-
result_mut::DisplayResultMut,
16+
result::DisplayResultMut,
1717
utils::Indent,
1818
};
1919
use crate::build::{display::ident::DisplayIdent, op::Op};
@@ -36,6 +36,5 @@ macro_rules! impl_trait_for_op {
3636
apply_macro_for_ops!(impl_trait_for_op, DisplayOp);
3737
apply_macro_for_ops!(impl_trait_for_op, DisplayIdent);
3838
apply_macro_for_ops!(impl_trait_for_op, DisplayConstructor);
39-
apply_macro_for_ops!(impl_trait_for_op, DisplayResultMut);
4039
apply_macro_for_ops!(impl_trait_for_op, DisplayEncode);
4140
apply_macro_for_ops!(impl_trait_for_op, DisplayDecode);

crates/ir2/build/display/op.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ impl Display for DisplayOp<&'_ Isa> {
8181
f,
8282
"\
8383
{indent}/// A Wasmi bytecode operator or instruction.\n\
84+
{indent}///\n\
85+
{indent}/// The [`Op`] type features a small utility API:\n\
86+
{indent}///\n\
87+
{indent}/// - [`Op::result_ref`]\n\
88+
{indent}/// - [`Op::result_mut`]\n\
89+
{indent}/// - [`Op::code`]\n\
8490
{indent}#[allow(non_camel_case_types)]\n\
8591
{indent}#[derive(Debug)]\n\
8692
{indent}pub enum Op {{\n\

crates/ir2/build/display/op_code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Display for DisplayOpCode<&'_ Isa> {
6565
{indent}}}\n\
6666
\n\
6767
{indent}impl Op {{\n\
68-
{indent} /// Returns the [`OpCode`] associated to `self`.
68+
{indent} /// Returns the [`OpCode`] associated to `self`.\n\
6969
{indent} pub fn code(&self) -> OpCode {{\n\
7070
{indent} match self {{\n\
7171
{match_arms_code}\n\

crates/ir2/build/display/result.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use crate::build::{
2+
display::{ident::DisplayIdent, utils::DisplaySequence, Indent},
3+
isa::Isa,
4+
op::Op,
5+
};
6+
use core::fmt::{self, Display};
7+
8+
pub struct DisplayResultMut<T> {
9+
pub value: T,
10+
pub indent: Indent,
11+
}
12+
13+
impl<T> DisplayResultMut<T> {
14+
pub fn new(value: T, indent: Indent) -> Self {
15+
Self { value, indent }
16+
}
17+
}
18+
19+
impl Display for DisplayResultMut<&'_ Isa> {
20+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21+
let indent = self.indent;
22+
let variants = DisplaySequence::new(
23+
"\n",
24+
self.value
25+
.ops
26+
.iter()
27+
.filter(|op| op.has_result())
28+
.map(|op| DisplayResultMut::new(op, indent.inc_by(3))),
29+
);
30+
write!(
31+
f,
32+
"\
33+
{indent}impl Op {{\n\
34+
{indent} /// Returns a shared reference to the result [`Slot`] of `self` if any.\n\
35+
{indent} pub fn result_ref(&self) -> Option<&Slot> {{\n\
36+
{indent} let res = match self {{\n\
37+
{variants} => result,\n\
38+
{indent} _ => return None,\n\
39+
{indent} }};\n\
40+
{indent} Some(res)\n\
41+
{indent} }}\n\
42+
\n\
43+
{indent} /// Returns an exclusive reference to the result [`Slot`] of `self` if any.\n\
44+
{indent} pub fn result_mut(&mut self) -> Option<&mut Slot> {{\n\
45+
{indent} let res = match self {{\n\
46+
{variants} => result,\n\
47+
{indent} _ => return None,\n\
48+
{indent} }};\n\
49+
{indent} Some(res)\n\
50+
{indent} }}\n\
51+
{indent}}}\n\
52+
"
53+
)
54+
}
55+
}
56+
57+
impl Display for DisplayResultMut<&'_ Op> {
58+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59+
let indent = self.indent;
60+
let ident = DisplayIdent::camel(self.value);
61+
write!(f, "{indent}| Self::{ident} {{ result, .. }}")
62+
}
63+
}
64+
65+
impl Op {
66+
/// Returns `true` if `self` has a result field.
67+
pub fn has_result(&self) -> bool {
68+
match self {
69+
Op::Unary(_) => true,
70+
Op::Binary(_) => true,
71+
Op::Ternary(_) => true,
72+
Op::CmpBranch(_) => false,
73+
Op::CmpSelect(_) => true,
74+
Op::Load(_) => true,
75+
Op::Store(_) => false,
76+
Op::TableGet(_) => true,
77+
Op::TableSet(_) => false,
78+
Op::Generic0(op) => op.has_result(),
79+
Op::Generic1(op) => op.has_result(),
80+
Op::Generic2(op) => op.has_result(),
81+
Op::Generic3(op) => op.has_result(),
82+
Op::Generic4(op) => op.has_result(),
83+
Op::Generic5(op) => op.has_result(),
84+
Op::V128ReplaceLane(_) => true,
85+
Op::V128LoadLane(_) => true,
86+
}
87+
}
88+
}

crates/ir2/build/display/result_mut.rs

Lines changed: 0 additions & 150 deletions
This file was deleted.

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 => 210_000,
79-
false => 135_000,
78+
true => 235_000,
79+
false => 150_000,
8080
};
8181
write_to_buffer(contents, expected_size, |buffer| {
8282
write!(

0 commit comments

Comments
 (0)