Skip to content

Commit bb71307

Browse files
authored
Update wasmi_ir2::BranchOffset type (#1661)
update BranchOffset type
1 parent 142e6f9 commit bb71307

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

crates/ir2/src/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Encode for BranchOffset {
8787
where
8888
E: Encoder,
8989
{
90-
let pos = self.to_i32().encode(encoder)?;
90+
let pos = i32::from(*self).encode(encoder)?;
9191
encoder.branch_offset(pos, *self)?;
9292
Ok(pos)
9393
}

crates/ir2/src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use core::fmt;
55
pub enum Error {
66
/// Encountered when trying to create a [`Slot`](crate::Slot) from an out of bounds integer.
77
StackSlotOutOfBounds,
8-
/// Encountered when trying to create a [`BranchOffset`](crate::BranchOffset) from an out of bounds integer.
9-
BranchOffsetOutOfBounds,
108
/// Encountered when trying to create a [`BlockFuel`](crate::BlockFuel) from an out of bounds integer.
119
BlockFuelOutOfBounds,
1210
/// Encountered when trying to create a [`Memory`] from an out of bounds integer.
@@ -19,7 +17,6 @@ impl fmt::Display for Error {
1917
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2018
let s = match self {
2119
Self::StackSlotOutOfBounds => "stack slot out of bounds",
22-
Self::BranchOffsetOutOfBounds => "branch offset out of bounds",
2320
Self::BlockFuelOutOfBounds => "block fuel out of bounds",
2421
Self::MemoryIndexOutOfBounds => "memory index out of bounds",
2522
};

crates/ir2/src/primitive.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,35 +106,31 @@ impl From<i32> for BranchOffset {
106106
}
107107
}
108108

109+
impl From<BranchOffset> for i32 {
110+
fn from(offset: BranchOffset) -> Self {
111+
offset.0
112+
}
113+
}
114+
109115
impl BranchOffset {
110-
/// Creates an uninitialized [`BranchOffset`].
111-
pub fn uninit() -> Self {
112-
Self(0)
116+
/// Returns `true` if [`Self`] is an offset for a backward branch.
117+
pub fn is_backwards(&self) -> bool {
118+
self.0.is_negative()
113119
}
114120

115-
/// Creates an initialized [`BranchOffset`] from `src` to `dst`.
116-
///
117-
/// # Errors
118-
///
119-
/// If the resulting [`BranchOffset`] is out of bounds.
120-
pub fn from_src_to_dst(src: u32, dst: u32) -> Result<Self, Error> {
121-
let src = i64::from(src);
122-
let dst = i64::from(dst);
123-
let Some(offset) = dst.checked_sub(src) else {
124-
// Note: This never needs to be called on backwards branches since they are immediated resolved.
125-
unreachable!(
126-
"offset for forward branches must have `src` be smaller than or equal to `dst`"
127-
);
128-
};
129-
let Ok(offset) = i32::try_from(offset) else {
130-
return Err(Error::BranchOffsetOutOfBounds);
131-
};
132-
Ok(Self(offset))
121+
/// Returns `true` if [`Self`] is an offset for a forward branch.
122+
pub fn is_forwards(&self) -> bool {
123+
self.0.is_positive()
133124
}
134125

135126
/// Returns `true` if the [`BranchOffset`] has been initialized.
136127
pub fn is_init(self) -> bool {
137-
self.to_i32() != 0
128+
self.0 != 0
129+
}
130+
131+
/// Creates an uninitialized [`BranchOffset`].
132+
pub fn uninit() -> Self {
133+
Self(0)
138134
}
139135

140136
/// Initializes the [`BranchOffset`] with a proper value.
@@ -148,11 +144,6 @@ impl BranchOffset {
148144
assert!(!self.is_init());
149145
*self = valid_offset;
150146
}
151-
152-
/// Returns the `i32` representation of the [`BranchOffset`].
153-
pub fn to_i32(self) -> i32 {
154-
self.0
155-
}
156147
}
157148

158149
/// The accumulated fuel to execute a block via [`Op::ConsumeFuel`].

0 commit comments

Comments
 (0)