Skip to content

Commit 52fe118

Browse files
committed
add translation for i{32,64} unary operators
1 parent 8250bab commit 52fe118

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,40 @@ impl FuncTranslator {
697697
cmp_instr.try_into_cmp_branch_instr(offset, &mut self.layout)
698698
}
699699

700+
/// Translates a unary Wasm instruction to Wasmi bytecode.
701+
fn translate_unary<T, R>(
702+
&mut self,
703+
make_instr: fn(result: Reg, input: Reg) -> Instruction,
704+
consteval: fn(input: T) -> R,
705+
) -> Result<(), Error>
706+
where
707+
T: From<TypedVal>,
708+
R: Into<TypedVal> + Typed,
709+
{
710+
bail_unreachable!(self);
711+
let input = self.stack.pop();
712+
if let Operand::Immediate(input) = input {
713+
self.stack
714+
.push_immediate(consteval(input.val().into()).into())?;
715+
return Ok(());
716+
}
717+
let consume_fuel_instr = self.stack.consume_fuel_instr();
718+
let input = self.layout.operand_to_reg(input)?;
719+
let iidx = self.instrs.next_instr();
720+
let result = self
721+
.layout
722+
.temp_to_reg(self.stack.push_temp(<R as Typed>::TY, Some(iidx))?)?;
723+
assert_eq!(
724+
self.instrs.push_instr(
725+
make_instr(result, input),
726+
consume_fuel_instr,
727+
FuelCostsProvider::base
728+
)?,
729+
iidx
730+
);
731+
Ok(())
732+
}
733+
700734
/// Translates a commutative binary Wasm operator to Wasmi bytecode.
701735
fn translate_binary_commutative<T, R>(
702736
&mut self,

crates/wasmi/src/engine/translator/func2/visit.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -588,15 +588,15 @@ impl<'a> VisitOperator<'a> for FuncTranslator {
588588
}
589589

590590
fn visit_i32_clz(&mut self) -> Self::Output {
591-
todo!()
591+
self.translate_unary::<i32, i32>(Instruction::i32_clz, wasm::i32_clz)
592592
}
593593

594594
fn visit_i32_ctz(&mut self) -> Self::Output {
595-
todo!()
595+
self.translate_unary::<i32, i32>(Instruction::i32_ctz, wasm::i32_ctz)
596596
}
597597

598598
fn visit_i32_popcnt(&mut self) -> Self::Output {
599-
todo!()
599+
self.translate_unary::<i32, i32>(Instruction::i32_popcnt, wasm::i32_popcnt)
600600
}
601601

602602
fn visit_i32_add(&mut self) -> Self::Output {
@@ -680,15 +680,15 @@ impl<'a> VisitOperator<'a> for FuncTranslator {
680680
}
681681

682682
fn visit_i64_clz(&mut self) -> Self::Output {
683-
todo!()
683+
self.translate_unary::<i64, i64>(Instruction::i64_clz, wasm::i64_clz)
684684
}
685685

686686
fn visit_i64_ctz(&mut self) -> Self::Output {
687-
todo!()
687+
self.translate_unary::<i64, i64>(Instruction::i64_ctz, wasm::i64_ctz)
688688
}
689689

690690
fn visit_i64_popcnt(&mut self) -> Self::Output {
691-
todo!()
691+
self.translate_unary::<i64, i64>(Instruction::i64_popcnt, wasm::i64_popcnt)
692692
}
693693

694694
fn visit_i64_add(&mut self) -> Self::Output {

0 commit comments

Comments
 (0)