Skip to content

Commit 847fdac

Browse files
authored
Refactor Instr and move definition to translator::utils submodule (#1541)
* replace {into,from}_u32 methods with From impls * refactor Instr::{from,into}_usize * update Instr docs * move Instr to translator utils
1 parent 352d564 commit 847fdac

File tree

6 files changed

+70
-60
lines changed

6 files changed

+70
-60
lines changed

crates/wasmi/src/engine/translator/func/control_frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::LabelRef;
22
use crate::{
3-
engine::{translator::Instr, BlockType, TranslationError},
3+
engine::{translator::utils::Instr, BlockType, TranslationError},
44
ir::{BoundedRegSpan, RegSpan},
55
Engine,
66
Error,

crates/wasmi/src/engine/translator/func/instr_encoder.rs

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ use crate::{
1818
ValueStack,
1919
},
2020
relink_result::RelinkResult as _,
21-
utils::{BumpFuelConsumption as _, FuelInfo, IsInstructionParameter as _, WasmInteger},
21+
utils::{
22+
BumpFuelConsumption as _,
23+
FuelInfo,
24+
Instr,
25+
IsInstructionParameter as _,
26+
WasmInteger,
27+
},
2228
visit_register::VisitInputRegisters as _,
2329
},
2430
ir::{
@@ -38,53 +44,6 @@ use crate::{
3844
use alloc::vec::{Drain, Vec};
3945
use core::mem;
4046

41-
/// A reference to an instruction of the partially
42-
/// constructed function body of the [`InstrEncoder`].
43-
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
44-
pub struct Instr(u32);
45-
46-
impl Instr {
47-
/// Creates an [`Instr`] from the given `usize` value.
48-
///
49-
/// # Note
50-
///
51-
/// This intentionally is an API intended for test purposes only.
52-
///
53-
/// # Panics
54-
///
55-
/// If the `value` exceeds limitations for [`Instr`].
56-
pub fn from_usize(value: usize) -> Self {
57-
let value = value.try_into().unwrap_or_else(|error| {
58-
panic!("invalid index {value} for instruction reference: {error}")
59-
});
60-
Self(value)
61-
}
62-
63-
/// Returns an `usize` representation of the instruction index.
64-
pub fn into_usize(self) -> usize {
65-
self.0 as usize
66-
}
67-
68-
/// Creates an [`Instr`] form the given `u32` value.
69-
pub fn from_u32(value: u32) -> Self {
70-
Self(value)
71-
}
72-
73-
/// Returns an `u32` representation of the instruction index.
74-
pub fn into_u32(self) -> u32 {
75-
self.0
76-
}
77-
78-
/// Returns the absolute distance between `self` and `other`.
79-
///
80-
/// - Returns `0` if `self == other`.
81-
/// - Returns `1` if `self` is adjacent to `other` in the sequence of instructions.
82-
/// - etc..
83-
pub fn distance(self, other: Self) -> u32 {
84-
self.0.abs_diff(other.0)
85-
}
86-
}
87-
8847
/// Encodes Wasmi bytecode instructions to an [`Instruction`] stream.
8948
#[derive(Debug, Default)]
9049
pub struct InstrEncoder {
@@ -150,10 +109,9 @@ impl InstrSequence {
150109
/// If there are too many instructions in the instruction sequence.
151110
fn push_before(&mut self, instr: Instr, instruction: Instruction) -> Result<Instr, Error> {
152111
self.instrs.insert(instr.into_usize(), instruction);
153-
let shifted_instr = instr
154-
.into_u32()
112+
let shifted_instr = u32::from(instr)
155113
.checked_add(1)
156-
.map(Instr::from_u32)
114+
.map(Instr::from)
157115
.unwrap_or_else(|| panic!("pushed to many instructions to a single function"));
158116
Ok(shifted_instr)
159117
}

crates/wasmi/src/engine/translator/func/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod simd;
1515
pub use self::{
1616
control_frame::ControlFrame,
1717
control_stack::ControlStack,
18-
instr_encoder::{Instr, InstrEncoder},
18+
instr_encoder::InstrEncoder,
1919
stack::TypedProvider,
2020
};
2121
use self::{
@@ -38,7 +38,7 @@ use crate::{
3838
code_map::CompiledFuncEntity,
3939
translator::{
4040
labels::{LabelRef, LabelRegistry},
41-
utils::{FuelInfo, WasmFloat, WasmInteger, Wrap},
41+
utils::{FuelInfo, Instr, WasmFloat, WasmInteger, Wrap},
4242
WasmTranslator,
4343
},
4444
BlockType,
@@ -224,7 +224,7 @@ impl WasmTranslator<'_> for FuncTranslator {
224224
// the compiled function.
225225
// Note: The function enclosing block fuel instruction is always
226226
// the instruction at the 0th index if fuel metering is enabled.
227-
let fuel_instr = Instr::from_u32(0);
227+
let fuel_instr = Instr::from(0_u32);
228228
let fuel_info = FuelInfo::some(fuel_costs.clone(), fuel_instr);
229229
self.instr_encoder
230230
.bump_fuel_consumption(&fuel_info, |costs| {

crates/wasmi/src/engine/translator/labels.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{engine::translator::Instr, ir::BranchOffset, Error};
1+
use crate::{engine::translator::utils::Instr, ir::BranchOffset, Error};
22
use alloc::vec::Vec;
33
use core::{
44
fmt::{self, Display},
@@ -147,7 +147,7 @@ impl LabelRegistry {
147147
) -> Result<BranchOffset, Error> {
148148
let offset = match *self.get_label(label) {
149149
Label::Pinned(target) => {
150-
BranchOffset::from_src_to_dst(user.into_u32(), target.into_u32())?
150+
BranchOffset::from_src_to_dst(u32::from(user), u32::from(target))?
151151
}
152152
Label::Unpinned => {
153153
self.users.push(LabelUser::new(label, user));
@@ -206,7 +206,7 @@ impl Iterator for ResolvedUserIter<'_> {
206206
.resolve_label(next.label)
207207
.unwrap_or_else(|err| panic!("failed to resolve user: {err}"));
208208
let offset =
209-
BranchOffset::from_src_to_dst(src.into_u32(), dst.into_u32()).map_err(Into::into);
209+
BranchOffset::from_src_to_dst(u32::from(src), u32::from(dst)).map_err(Into::into);
210210
Some((src, offset))
211211
}
212212
}

crates/wasmi/src/engine/translator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::Engine;
1818
pub use self::{
1919
driver::FuncTranslationDriver,
2020
error::TranslationError,
21-
func::{FuncTranslator, FuncTranslatorAllocations, Instr},
21+
func::{FuncTranslator, FuncTranslatorAllocations},
2222
};
2323
use super::code_map::CompiledFuncEntity;
2424
use crate::{

crates/wasmi/src/engine/translator/utils.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::Instr;
21
use crate::{
32
core::{FuelCostsProvider, Typed, TypedVal, ValType},
43
ir::{Const16, Instruction, Sign},
@@ -215,3 +214,56 @@ impl IsInstructionParameter for Instruction {
215214
)
216215
}
217216
}
217+
218+
/// A reference to an encoded [`Instruction`].
219+
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
220+
pub struct Instr(u32);
221+
222+
impl From<u32> for Instr {
223+
fn from(index: u32) -> Self {
224+
Self(index)
225+
}
226+
}
227+
228+
impl From<Instr> for u32 {
229+
fn from(instr: Instr) -> Self {
230+
instr.0
231+
}
232+
}
233+
234+
impl Instr {
235+
/// Creates an [`Instr`] from the given `usize` value.
236+
///
237+
/// # Note
238+
///
239+
/// This intentionally is an API intended for test purposes only.
240+
///
241+
/// # Panics
242+
///
243+
/// If the `value` exceeds limitations for [`Instr`].
244+
pub fn from_usize(value: usize) -> Self {
245+
let Ok(index) = u32::try_from(value) else {
246+
panic!("out of bounds index {value} for `Instr`")
247+
};
248+
Self(index)
249+
}
250+
251+
/// Returns an `usize` representation of the instruction index.
252+
pub fn into_usize(self) -> usize {
253+
match usize::try_from(self.0) {
254+
Ok(index) => index,
255+
Err(error) => {
256+
panic!("out of bound index {} for `Instr`: {error}", self.0)
257+
}
258+
}
259+
}
260+
261+
/// Returns the absolute distance between `self` and `other`.
262+
///
263+
/// - Returns `0` if `self == other`.
264+
/// - Returns `1` if `self` is adjacent to `other` in the sequence of instructions.
265+
/// - etc..
266+
pub fn distance(self, other: Self) -> u32 {
267+
self.0.abs_diff(other.0)
268+
}
269+
}

0 commit comments

Comments
 (0)