From 2b9ff82afbd6cbbf44284a2a674edb98baa51105 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sat, 25 Nov 2023 09:19:28 +0100 Subject: [PATCH] remove superseeded branch+cmp instructions --- .../src/engine/regmach/bytecode/construct.rs | 28 +++++------ .../wasmi/src/engine/regmach/bytecode/mod.rs | 45 ------------------ .../src/engine/regmach/bytecode/utils.rs | 4 +- .../src/engine/regmach/executor/instrs.rs | 12 ----- .../engine/regmach/executor/instrs/branch.rs | 36 --------------- .../src/engine/regmach/tests/op/block.rs | 6 +-- .../src/engine/regmach/tests/op/br_if.rs | 16 +++---- .../src/engine/regmach/tests/op/cmp_br.rs | 8 ++-- .../wasmi/src/engine/regmach/tests/op/if_.rs | 34 +++++++------- .../regmach/translator/instr_encoder.rs | 46 +++++++++---------- .../regmach/translator/relink_result.rs | 4 -- .../regmach/translator/visit_register.rs | 4 -- 12 files changed, 72 insertions(+), 171 deletions(-) diff --git a/crates/wasmi/src/engine/regmach/bytecode/construct.rs b/crates/wasmi/src/engine/regmach/bytecode/construct.rs index 03b0479e2c..230fefaa6c 100644 --- a/crates/wasmi/src/engine/regmach/bytecode/construct.rs +++ b/crates/wasmi/src/engine/regmach/bytecode/construct.rs @@ -224,8 +224,8 @@ macro_rules! constructor_for_branch_binop_imm { impl Instruction { $( #[doc = concat!("Creates a new [`Instruction::", stringify!($op_code), "`].")] - pub fn $name(lhs: Register, rhs: Const16<$ty>, offset: BranchOffset16) -> Self { - Self::$op_code(BranchBinOpInstrImm16::new(lhs, rhs, offset)) + pub fn $name(lhs: Register, rhs: impl Into>, offset: BranchOffset16) -> Self { + Self::$op_code(BranchBinOpInstrImm16::new(lhs, rhs.into(), offset)) } )* } @@ -412,24 +412,24 @@ impl Instruction { Self::Branch { offset } } - /// Creates a new [`Instruction::BranchI32Eqz`] for the given `condition` and `offset`. - pub fn branch_i32_eqz(condition: Register, offset: BranchOffset) -> Self { - Self::BranchI32Eqz { condition, offset } + /// Convenience constructor to create a new [`Instruction::BranchI32Eqz`] with a zero immediate value. + pub fn branch_i32_eqz(condition: Register, offset: BranchOffset16) -> Self { + Self::branch_i32_eq_imm(condition, 0_i16, offset) } - /// Creates a new [`Instruction::BranchI32Nez`] for the given `condition` and `offset`. - pub fn branch_i32_nez(condition: Register, offset: BranchOffset) -> Self { - Self::BranchI32Nez { condition, offset } + /// Convenience constructor to create a new [`Instruction::BranchI32Nez`] with a zero immediate value. + pub fn branch_i32_nez(condition: Register, offset: BranchOffset16) -> Self { + Self::branch_i32_ne_imm(condition, 0_i16, offset) } - /// Creates a new [`Instruction::BranchI64Eqz`] for the given `condition` and `offset`. - pub fn branch_i64_eqz(condition: Register, offset: BranchOffset) -> Self { - Self::BranchI64Eqz { condition, offset } + /// Convenience constructor to create a new [`Instruction::BranchI64Eqz`] with a zero immediate value. + pub fn branch_i64_eqz(condition: Register, offset: BranchOffset16) -> Self { + Self::branch_i64_eq_imm(condition, 0_i16, offset) } - /// Creates a new [`Instruction::BranchI64Nez`] for the given `condition` and `offset`. - pub fn branch_i64_nez(condition: Register, offset: BranchOffset) -> Self { - Self::BranchI64Nez { condition, offset } + /// Convenience constructor to create a new [`Instruction::BranchI64Nez`] with a zero immediate value. + pub fn branch_i64_nez(condition: Register, offset: BranchOffset16) -> Self { + Self::branch_i64_ne_imm(condition, 0_i16, offset) } /// Creates a new [`Instruction::BranchTable`] for the given `index` and `len_targets`. diff --git a/crates/wasmi/src/engine/regmach/bytecode/mod.rs b/crates/wasmi/src/engine/regmach/bytecode/mod.rs index 11f4fc7ec8..cf9a414760 100644 --- a/crates/wasmi/src/engine/regmach/bytecode/mod.rs +++ b/crates/wasmi/src/engine/regmach/bytecode/mod.rs @@ -367,51 +367,6 @@ pub enum Instruction { /// The branching offset for the instruction pointer. offset: BranchOffset, }, - /// A conditional branch instruction. - /// - /// # Note - /// - /// - The branch is taken if `condition` evaluates to zero. - /// - Partially translated from negated Wasm `br_if` instructions. - BranchI32Eqz { - /// The register holding the condition to evaluate against zero. - condition: Register, - /// The branching offset for the instruction pointer. - offset: BranchOffset, - }, - /// A Wasm `br_if` instruction. - /// - /// # Note - /// - /// The branch is taken if `condition` evaluates to zero. - BranchI32Nez { - /// The register holding the condition to evaluate against zero. - condition: Register, - /// The branching offset for the instruction pointer. - offset: BranchOffset, - }, - /// A fused Wasm `i64.eqz` + `if` instruction. - /// - /// # Note - /// - /// - The branch is taken if `condition` evaluates to zero. - BranchI64Eqz { - /// The register holding the condition to evaluate against zero. - condition: Register, - /// The branching offset for the instruction pointer. - offset: BranchOffset, - }, - /// A fused Wasm `i64.eqz` + `br_if` instruction. - /// - /// # Note - /// - /// The branch is taken if `condition` evaluates to zero. - BranchI64Nez { - /// The register holding the condition to evaluate against zero. - condition: Register, - /// The branching offset for the instruction pointer. - offset: BranchOffset, - }, /// A fused [`Instruction::I32And`] and [`Instruction::BranchI32Nez`] instruction. BranchI32And(BranchBinOpInstr), diff --git a/crates/wasmi/src/engine/regmach/bytecode/utils.rs b/crates/wasmi/src/engine/regmach/bytecode/utils.rs index 1dfd5095ad..faac2be3bf 100644 --- a/crates/wasmi/src/engine/regmach/bytecode/utils.rs +++ b/crates/wasmi/src/engine/regmach/bytecode/utils.rs @@ -496,7 +496,9 @@ impl TryFrom for BranchOffset16 { fn try_from(offset: BranchOffset) -> Result { let Ok(offset16) = i16::try_from(offset.to_i32()) else { - return Err(TranslationError::new(TranslationErrorInner::BranchOffsetOutOfBounds)) + return Err(TranslationError::new( + TranslationErrorInner::BranchOffsetOutOfBounds, + )); }; Ok(Self(offset16)) } diff --git a/crates/wasmi/src/engine/regmach/executor/instrs.rs b/crates/wasmi/src/engine/regmach/executor/instrs.rs index ec40a71e7a..b851abee36 100644 --- a/crates/wasmi/src/engine/regmach/executor/instrs.rs +++ b/crates/wasmi/src/engine/regmach/executor/instrs.rs @@ -245,18 +245,6 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> { forward_return!(self.execute_return_nez_many(condition, values)) } Instr::Branch { offset } => self.execute_branch(offset), - Instr::BranchI32Eqz { condition, offset } => { - self.execute_branch_i32_eqz(condition, offset) - } - Instr::BranchI32Nez { condition, offset } => { - self.execute_branch_i32_nez(condition, offset) - } - Instr::BranchI64Eqz { condition, offset } => { - self.execute_branch_i64_eqz(condition, offset) - } - Instr::BranchI64Nez { condition, offset } => { - self.execute_branch_i64_nez(condition, offset) - } Instr::BranchTable { index, len_targets } => { self.execute_branch_table(index, len_targets) } diff --git a/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs b/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs index 3b4721e269..a5b5e1a167 100644 --- a/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs +++ b/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs @@ -15,42 +15,6 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> { self.branch_to(offset) } - #[inline(always)] - pub fn execute_branch_i32_nez(&mut self, condition: Register, offset: BranchOffset) { - let value: i32 = self.get_register_as(condition); - if value != 0 { - return self.branch_to(offset); - } - self.next_instr(); - } - - #[inline(always)] - pub fn execute_branch_i32_eqz(&mut self, condition: Register, offset: BranchOffset) { - let value: i32 = self.get_register_as(condition); - if value == 0 { - return self.branch_to(offset); - } - self.next_instr(); - } - - #[inline(always)] - pub fn execute_branch_i64_nez(&mut self, condition: Register, offset: BranchOffset) { - let value: i64 = self.get_register_as(condition); - if value != 0 { - return self.branch_to(offset); - } - self.next_instr(); - } - - #[inline(always)] - pub fn execute_branch_i64_eqz(&mut self, condition: Register, offset: BranchOffset) { - let value: i64 = self.get_register_as(condition); - if value == 0 { - return self.branch_to(offset); - } - self.next_instr(); - } - #[inline(always)] pub fn execute_branch_table(&mut self, index: Register, len_targets: Const32) { let index: u32 = self.get_register_as(index); diff --git a/crates/wasmi/src/engine/regmach/tests/op/block.rs b/crates/wasmi/src/engine/regmach/tests/op/block.rs index 5b89a53d39..ad31e450ed 100644 --- a/crates/wasmi/src/engine/regmach/tests/op/block.rs +++ b/crates/wasmi/src/engine/regmach/tests/op/block.rs @@ -2,7 +2,7 @@ use super::*; use crate::engine::{ bytecode::BranchOffset, regmach::{ - bytecode::RegisterSpan, + bytecode::{BranchOffset16, RegisterSpan}, tests::{display_wasm::DisplayValueType, wasm_type::WasmType}, }, }; @@ -346,7 +346,7 @@ fn branch_if_block_0() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(1)), + Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset16::from(1)), Instruction::Return, ]) .run() @@ -369,7 +369,7 @@ fn branch_if_block_1() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(3)), + Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset16::from(3)), Instruction::copy(Register::from_i16(2), Register::from_i16(0)), Instruction::branch(BranchOffset::from(2)), Instruction::copy(Register::from_i16(2), Register::from_i16(0)), diff --git a/crates/wasmi/src/engine/regmach/tests/op/br_if.rs b/crates/wasmi/src/engine/regmach/tests/op/br_if.rs index e4259c8c08..87f2637889 100644 --- a/crates/wasmi/src/engine/regmach/tests/op/br_if.rs +++ b/crates/wasmi/src/engine/regmach/tests/op/br_if.rs @@ -2,7 +2,7 @@ use super::*; use crate::engine::{ bytecode::BranchOffset, regmach::{ - bytecode::RegisterSpan, + bytecode::{BranchOffset16, RegisterSpan}, tests::{display_wasm::DisplayValueType, driver::ExpectedFunc, wasm_type::WasmType}, }, }; @@ -844,7 +844,7 @@ fn branch_if_results_0() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(1)), + Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset16::from(1)), Instruction::Return, ]) .run() @@ -867,7 +867,7 @@ fn branch_if_results_1() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(3)), + Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset16::from(3)), Instruction::copy(Register::from_i16(2), Register::from_i16(0)), Instruction::branch(BranchOffset::from(2)), Instruction::copy(Register::from_i16(2), Register::from_i16(0)), @@ -902,7 +902,7 @@ fn branch_if_results_1_avoid_copy() { .expect_func_instrs([ Instruction::i32_clz(Register::from_i16(2), Register::from_i16(0)), Instruction::i32_ctz(Register::from_i16(3), Register::from_i16(1)), - Instruction::branch_i32_nez(Register::from_i16(3), BranchOffset::from(1)), + Instruction::branch_i32_nez(Register::from_i16(3), BranchOffset16::from(1)), Instruction::return_reg(Register::from_i16(2)), ]) .run() @@ -927,7 +927,7 @@ fn branch_if_results_2() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset::from(3)), + Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset16::from(3)), Instruction::copy2(RegisterSpan::new(Register::from_i16(3)), 0, 1), Instruction::branch(BranchOffset::from(2)), Instruction::copy2(RegisterSpan::new(Register::from_i16(3)), 0, 1), @@ -968,7 +968,7 @@ fn branch_if_results_2_avoid_copy() { .expect_func_instrs([ Instruction::i32_clz(Register::from_i16(3), Register::from_i16(0)), Instruction::i32_ctz(Register::from_i16(4), Register::from_i16(1)), - Instruction::branch_i32_nez(Register::from_i16(2), BranchOffset::from(1)), + Instruction::branch_i32_nez(Register::from_i16(2), BranchOffset16::from(1)), Instruction::i32_add( Register::from_i16(3), Register::from_i16(3), @@ -1002,7 +1002,7 @@ fn branch_if_results_4_mixed_1() { TranslationTest::new(wasm) .expect_func( ExpectedFunc::new([ - Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset::from(4)), + Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset16::from(4)), Instruction::copy_many_non_overlapping( RegisterSpan::new(Register::from_i16(3)), -1, @@ -1045,7 +1045,7 @@ fn branch_if_results_4_mixed_2() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset::from(4)), + Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset16::from(4)), Instruction::copy_many_non_overlapping(RegisterSpan::new(Register::from_i16(3)), 0, 0), Instruction::register2(1, 1), Instruction::branch(BranchOffset::from(3)), diff --git a/crates/wasmi/src/engine/regmach/tests/op/cmp_br.rs b/crates/wasmi/src/engine/regmach/tests/op/cmp_br.rs index 6a628bc9c7..c42ea2e25e 100644 --- a/crates/wasmi/src/engine/regmach/tests/op/cmp_br.rs +++ b/crates/wasmi/src/engine/regmach/tests/op/cmp_br.rs @@ -150,7 +150,7 @@ fn loop_backward_imm() { #[test] #[cfg_attr(miri, ignore)] fn loop_backward_imm_eqz() { - fn test_for(op: &str, expect_instr: fn(Register, BranchOffset) -> Instruction) { + fn test_for(op: &str, expect_instr: fn(Register, BranchOffset16) -> Instruction) { let wasm = wat2wasm(&format!( r" (module @@ -166,7 +166,7 @@ fn loop_backward_imm_eqz() { )); TranslationTest::new(wasm) .expect_func_instrs([ - expect_instr(Register::from_i16(0), BranchOffset::from(0_i32)), + expect_instr(Register::from_i16(0), BranchOffset16::from(0_i16)), Instruction::Return, ]) .run() @@ -539,7 +539,7 @@ fn block_i64_eqz_fuse() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i64_eqz(Register::from_i16(0), BranchOffset::from(1)), + Instruction::branch_i64_eqz(Register::from_i16(0), BranchOffset16::from(1)), Instruction::Return, ]) .run() @@ -561,7 +561,7 @@ fn if_i64_eqz_fuse() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i64_nez(Register::from_i16(0), BranchOffset::from(1)), + Instruction::branch_i64_nez(Register::from_i16(0), BranchOffset16::from(1)), Instruction::Return, ]) .run() diff --git a/crates/wasmi/src/engine/regmach/tests/op/if_.rs b/crates/wasmi/src/engine/regmach/tests/op/if_.rs index 3f496ad747..cfb79aca9d 100644 --- a/crates/wasmi/src/engine/regmach/tests/op/if_.rs +++ b/crates/wasmi/src/engine/regmach/tests/op/if_.rs @@ -1,7 +1,7 @@ use super::*; use crate::engine::{ bytecode::{BranchOffset, GlobalIdx}, - regmach::bytecode::RegisterSpan, + regmach::bytecode::{BranchOffset16, RegisterSpan}, CompiledFunc, }; use wasmi_core::{TrapCode, UntypedValue}; @@ -21,7 +21,7 @@ fn simple_if_then() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(1)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(1)), Instruction::Return, ]) .run() @@ -46,8 +46,8 @@ fn simple_if_then_nested() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)), - Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(1)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(2)), + Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset16::from(1)), Instruction::Return, ]) .run() @@ -72,7 +72,7 @@ fn if_then_global_set() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(2)), Instruction::global_set(GlobalIdx::from(0), Register::from_i16(1)), Instruction::return_imm32(AnyConst32::from(10_i32)), ]) @@ -102,7 +102,7 @@ fn if_then_return() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(3)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(3)), Instruction::i32_add( Register::from_i16(3), Register::from_i16(1), @@ -135,7 +135,7 @@ fn if_then_else_return() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(2)), Instruction::return_imm32(AnyConst32::from(10_i32)), Instruction::return_imm32(AnyConst32::from(20_i32)), ]) @@ -163,7 +163,7 @@ fn if_then_br_else() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(2)), Instruction::branch(BranchOffset::from(2)), Instruction::return_imm32(AnyConst32::from(10_i32)), Instruction::return_imm32(AnyConst32::from(20_i32)), @@ -192,7 +192,7 @@ fn if_then_else_br() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(2)), Instruction::return_imm32(AnyConst32::from(10_i32)), Instruction::branch(BranchOffset::from(1)), Instruction::return_imm32(AnyConst32::from(20_i32)), @@ -218,7 +218,7 @@ fn simple_if_then_else() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(2)), Instruction::branch(BranchOffset::from(1)), Instruction::Return, ]) @@ -251,11 +251,11 @@ fn simple_if_then_else_nested() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(4)), - Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(2)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(4)), + Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset16::from(2)), Instruction::branch(BranchOffset::from(1)), Instruction::branch(BranchOffset::from(3)), - Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(2)), + Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset16::from(2)), Instruction::branch(BranchOffset::from(1)), Instruction::Return, ]) @@ -280,7 +280,7 @@ fn if_then_else_with_params() { ); TranslationTest::new(wasm) .expect_func_instrs([ - Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(3)), + Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset16::from(3)), Instruction::i32_add( Register::from_i16(3), Register::from_i16(1), @@ -445,7 +445,7 @@ fn const_condition_br_if_then() { test_for( false, [ - Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(2)), + Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset16::from(2)), Instruction::Trap(TrapCode::UnreachableCodeReached), Instruction::return_imm32(AnyConst32::from(1_i32)), ], @@ -484,7 +484,7 @@ fn const_condition_br_if_else() { test_for( true, [ - Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(2)), + Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset16::from(2)), Instruction::Trap(TrapCode::UnreachableCodeReached), Instruction::return_imm32(AnyConst32::from(1_i32)), ], @@ -569,7 +569,7 @@ fn test_if_without_else_has_result() { RegisterSpan::new(Register::from_i16(0)), CompiledFunc::from_u32(0), ), - Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(3)), + Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset16::from(3)), Instruction::copy_i64imm32(Register::from_i16(0), -1), Instruction::branch(BranchOffset::from(1)), Instruction::return_reg(Register::from_i16(0)), diff --git a/crates/wasmi/src/engine/regmach/translator/instr_encoder.rs b/crates/wasmi/src/engine/regmach/translator/instr_encoder.rs index 7d07d5f319..fe04047fc6 100644 --- a/crates/wasmi/src/engine/regmach/translator/instr_encoder.rs +++ b/crates/wasmi/src/engine/regmach/translator/instr_encoder.rs @@ -842,7 +842,9 @@ impl InstrEncoder { condition: Register, label: LabelRef, ) -> Result<(), TranslationError> { - let offset = this.try_resolve_label(label)?; + let offset = this + .try_resolve_label(label) + .and_then(BranchOffset16::try_from)?; this.push_instr(Instruction::branch_i32_eqz(condition, offset))?; Ok(()) } @@ -904,8 +906,8 @@ impl InstrEncoder { match stack.get_register_space(instr.result) { RegisterSpace::Local => None, _ => { - let offset32 = self.try_resolve_label_for(label, last_instr)?; - Some(Instruction::branch_i32_nez(instr.reg_in, offset32)) + let offset16 = self.try_resolve_label_for(label, last_instr).and_then(BranchOffset16::try_from)?; + Some(Instruction::branch_i32_nez(instr.reg_in, offset16)) } } } @@ -913,8 +915,8 @@ impl InstrEncoder { match stack.get_register_space(instr.result) { RegisterSpace::Local => None, _ => { - let offset32 = self.try_resolve_label_for(label, last_instr)?; - Some(Instruction::branch_i64_nez(instr.reg_in, offset32)) + let offset16 = self.try_resolve_label_for(label, last_instr).and_then(BranchOffset16::try_from)?; + Some(Instruction::branch_i64_nez(instr.reg_in, offset16)) } } } @@ -922,8 +924,8 @@ impl InstrEncoder { match stack.get_register_space(instr.result) { RegisterSpace::Local => None, _ => { - let offset32 = self.try_resolve_label_for(label, last_instr)?; - Some(Instruction::branch_i32_eqz(instr.reg_in, offset32)) + let offset16 = self.try_resolve_label_for(label, last_instr).and_then(BranchOffset16::try_from)?; + Some(Instruction::branch_i32_eqz(instr.reg_in, offset16)) } } } @@ -931,8 +933,8 @@ impl InstrEncoder { match stack.get_register_space(instr.result) { RegisterSpace::Local => None, _ => { - let offset32 = self.try_resolve_label_for(label, last_instr)?; - Some(Instruction::branch_i64_eqz(instr.reg_in, offset32)) + let offset16 = self.try_resolve_label_for(label, last_instr).and_then(BranchOffset16::try_from)?; + Some(Instruction::branch_i64_eqz(instr.reg_in, offset16)) } } } @@ -1018,7 +1020,9 @@ impl InstrEncoder { condition: Register, label: LabelRef, ) -> Result<(), TranslationError> { - let offset = this.try_resolve_label(label)?; + let offset = this + .try_resolve_label(label) + .and_then(BranchOffset16::try_from)?; this.push_instr(Instruction::branch_i32_nez(condition, offset))?; Ok(()) } @@ -1080,8 +1084,8 @@ impl InstrEncoder { match stack.get_register_space(instr.result) { RegisterSpace::Local => None, _ => { - let offset32 = self.try_resolve_label_for(label, last_instr)?; - Some(Instruction::branch_i32_eqz(instr.reg_in, offset32)) + let offset16 = self.try_resolve_label_for(label, last_instr).and_then(BranchOffset16::try_from)?; + Some(Instruction::branch_i32_eqz(instr.reg_in, offset16)) } } } @@ -1089,8 +1093,8 @@ impl InstrEncoder { match stack.get_register_space(instr.result) { RegisterSpace::Local => None, _ => { - let offset32 = self.try_resolve_label_for(label, last_instr)?; - Some(Instruction::branch_i64_eqz(instr.reg_in, offset32)) + let offset16 = self.try_resolve_label_for(label, last_instr).and_then(BranchOffset16::try_from)?; + Some(Instruction::branch_i64_eqz(instr.reg_in, offset16)) } } } @@ -1098,8 +1102,8 @@ impl InstrEncoder { match stack.get_register_space(instr.result) { RegisterSpace::Local => None, _ => { - let offset32 = self.try_resolve_label_for(label, last_instr)?; - Some(Instruction::branch_i32_nez(instr.reg_in, offset32)) + let offset16 = self.try_resolve_label_for(label, last_instr).and_then(BranchOffset16::try_from)?; + Some(Instruction::branch_i32_nez(instr.reg_in, offset16)) } } } @@ -1107,8 +1111,8 @@ impl InstrEncoder { match stack.get_register_space(instr.result) { RegisterSpace::Local => None, _ => { - let offset32 = self.try_resolve_label_for(label, last_instr)?; - Some(Instruction::branch_i64_nez(instr.reg_in, offset32)) + let offset16 = self.try_resolve_label_for(label, last_instr).and_then(BranchOffset16::try_from)?; + Some(Instruction::branch_i64_nez(instr.reg_in, offset16)) } } } @@ -1197,11 +1201,7 @@ impl Instruction { new_offset: BranchOffset, ) -> Result<(), TranslationError> { match self { - Instruction::Branch { offset } - | Instruction::BranchI32Eqz { offset, .. } - | Instruction::BranchI32Nez { offset, .. } - | Instruction::BranchI64Eqz { offset, .. } - | Instruction::BranchI64Nez { offset, .. } => { + Instruction::Branch { offset } => { offset.init(new_offset); Ok(()) } diff --git a/crates/wasmi/src/engine/regmach/translator/relink_result.rs b/crates/wasmi/src/engine/regmach/translator/relink_result.rs index aad1686255..77fd8e7c3b 100644 --- a/crates/wasmi/src/engine/regmach/translator/relink_result.rs +++ b/crates/wasmi/src/engine/regmach/translator/relink_result.rs @@ -75,10 +75,6 @@ impl Instruction { | I::BranchI32OrEqzImm(_) | I::BranchI32XorEqz(_) | I::BranchI32XorEqzImm(_) - | I::BranchI32Eqz { .. } - | I::BranchI32Nez { .. } - | I::BranchI64Eqz { .. } - | I::BranchI64Nez { .. } | I::BranchTable { .. } | I::BranchI32Eq(_) | I::BranchI32EqImm(_) diff --git a/crates/wasmi/src/engine/regmach/translator/visit_register.rs b/crates/wasmi/src/engine/regmach/translator/visit_register.rs index eb4836bac7..fab832522b 100644 --- a/crates/wasmi/src/engine/regmach/translator/visit_register.rs +++ b/crates/wasmi/src/engine/regmach/translator/visit_register.rs @@ -78,10 +78,6 @@ impl VisitInputRegisters for Instruction { values.visit_input_registers(f); } Instruction::Branch { .. } => {}, - Instruction::BranchI32Eqz { condition, .. } | - Instruction::BranchI32Nez { condition, .. } | - Instruction::BranchI64Eqz { condition, .. } | - Instruction::BranchI64Nez { condition, .. } => f(condition), Instruction::BranchTable { index, .. } => f(index), Instruction::BranchI32And(instr) => instr.visit_input_registers(f),