Skip to content

Commit

Permalink
implement unary instruction execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbepop committed Aug 22, 2023
1 parent 3ec6b2e commit 795883d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 20 deletions.
41 changes: 21 additions & 20 deletions crates/wasmi/src/engine/regmach/executor/instrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod memory;
mod select;
mod store;
mod table;
mod unary;

macro_rules! forward_call {
($expr:expr) => {{
Expand Down Expand Up @@ -637,12 +638,12 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> {
Instr::F64Gt(_) => todo!(),
Instr::F32Ge(_) => todo!(),
Instr::F64Ge(_) => todo!(),
Instr::I32Clz(_) => todo!(),
Instr::I64Clz(_) => todo!(),
Instr::I32Ctz(_) => todo!(),
Instr::I64Ctz(_) => todo!(),
Instr::I32Popcnt(_) => todo!(),
Instr::I64Popcnt(_) => todo!(),
Instr::I32Clz(instr) => self.execute_i32_clz(instr),
Instr::I64Clz(instr) => self.execute_i64_clz(instr),
Instr::I32Ctz(instr) => self.execute_i32_ctz(instr),
Instr::I64Ctz(instr) => self.execute_i64_ctz(instr),
Instr::I32Popcnt(instr) => self.execute_i32_popcnt(instr),
Instr::I64Popcnt(instr) => self.execute_i64_popcnt(instr),
Instr::I32Add(_) => todo!(),
Instr::I64Add(_) => todo!(),
Instr::I32AddImm16(_) => todo!(),
Expand Down Expand Up @@ -723,20 +724,20 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> {
Instr::I64RotrImm(_) => todo!(),
Instr::I32RotrImm16Rev(_) => todo!(),
Instr::I64RotrImm16Rev(_) => todo!(),
Instr::F32Abs(_) => todo!(),
Instr::F64Abs(_) => todo!(),
Instr::F32Neg(_) => todo!(),
Instr::F64Neg(_) => todo!(),
Instr::F32Ceil(_) => todo!(),
Instr::F64Ceil(_) => todo!(),
Instr::F32Floor(_) => todo!(),
Instr::F64Floor(_) => todo!(),
Instr::F32Trunc(_) => todo!(),
Instr::F64Trunc(_) => todo!(),
Instr::F32Nearest(_) => todo!(),
Instr::F64Nearest(_) => todo!(),
Instr::F32Sqrt(_) => todo!(),
Instr::F64Sqrt(_) => todo!(),
Instr::F32Abs(instr) => self.execute_f32_abs(instr),
Instr::F64Abs(instr) => self.execute_f64_abs(instr),
Instr::F32Neg(instr) => self.execute_f32_neg(instr),
Instr::F64Neg(instr) => self.execute_f64_neg(instr),
Instr::F32Ceil(instr) => self.execute_f32_ceil(instr),
Instr::F64Ceil(instr) => self.execute_f64_ceil(instr),
Instr::F32Floor(instr) => self.execute_f32_floor(instr),
Instr::F64Floor(instr) => self.execute_f64_floor(instr),
Instr::F32Trunc(instr) => self.execute_f32_trunc(instr),
Instr::F64Trunc(instr) => self.execute_f64_trunc(instr),
Instr::F32Nearest(instr) => self.execute_f32_nearest(instr),
Instr::F64Nearest(instr) => self.execute_f64_nearest(instr),
Instr::F32Sqrt(instr) => self.execute_f32_sqrt(instr),
Instr::F64Sqrt(instr) => self.execute_f64_sqrt(instr),
Instr::F32Add(_) => todo!(),
Instr::F64Add(_) => todo!(),
Instr::F32Sub(_) => todo!(),
Expand Down
52 changes: 52 additions & 0 deletions crates/wasmi/src/engine/regmach/executor/instrs/unary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::Executor;
use crate::{core::UntypedValue, engine::bytecode2::UnaryInstr};

#[cfg(doc)]
use crate::engine::bytecode2::Instruction;

macro_rules! impl_unary_impls {
( $( (Instruction::$var_name:ident, $fn_name:ident, $op:expr) ),* $(,)? ) => {
$(
#[doc = concat!("Executes an [`Instruction::", stringify!($var_name), "`].")]
#[inline(always)]
pub fn $fn_name(&mut self, instr: UnaryInstr) {
self.execute_unary(instr, $op)
}
)*
};
}

impl<'ctx, 'engine> Executor<'ctx, 'engine> {
/// Executes a generic unary [`Instruction`].
fn execute_unary(&mut self, instr: UnaryInstr, op: fn(UntypedValue) -> UntypedValue) {
let value = self.get_register(instr.input);
self.set_register(instr.result, op(value));
self.next_instr();
}

impl_unary_impls! {
(Instruction::I32Clz, execute_i32_clz, UntypedValue::i32_clz),
(Instruction::I32Ctz, execute_i32_ctz, UntypedValue::i32_ctz),
(Instruction::I32Popcnt, execute_i32_popcnt, UntypedValue::i32_popcnt),

(Instruction::I64Clz, execute_i64_clz, UntypedValue::i64_clz),
(Instruction::I64Ctz, execute_i64_ctz, UntypedValue::i64_ctz),
(Instruction::I64Popcnt, execute_i64_popcnt, UntypedValue::i64_popcnt),

(Instruction::F32Abs, execute_f32_abs, UntypedValue::f32_abs),
(Instruction::F32Neg, execute_f32_neg, UntypedValue::f32_neg),
(Instruction::F32Ceil, execute_f32_ceil, UntypedValue::f32_ceil),
(Instruction::F32Floor, execute_f32_floor, UntypedValue::f32_floor),
(Instruction::F32Trunc, execute_f32_trunc, UntypedValue::f32_trunc),
(Instruction::F32Nearest, execute_f32_nearest, UntypedValue::f32_nearest),
(Instruction::F32Sqrt, execute_f32_sqrt, UntypedValue::f32_sqrt),

(Instruction::F64Abs, execute_f64_abs, UntypedValue::f64_abs),
(Instruction::F64Neg, execute_f64_neg, UntypedValue::f64_neg),
(Instruction::F64Ceil, execute_f64_ceil, UntypedValue::f64_ceil),
(Instruction::F64Floor, execute_f64_floor, UntypedValue::f64_floor),
(Instruction::F64Trunc, execute_f64_trunc, UntypedValue::f64_trunc),
(Instruction::F64Nearest, execute_f64_nearest, UntypedValue::f64_nearest),
(Instruction::F64Sqrt, execute_f64_sqrt, UntypedValue::f64_sqrt),
}
}

0 comments on commit 795883d

Please sign in to comment.