From 8eec592bba5279ec9213bfa1de816dc046748c0f Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Sun, 10 Dec 2023 01:05:01 +0100 Subject: chore: add more arithmetic instructions Signed-off-by: Henry Gressmann --- crates/tinywasm/src/runtime/executor/macros.rs | 22 ++++++++++++++++++++++ crates/tinywasm/src/runtime/executor/mod.rs | 22 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/crates/tinywasm/src/runtime/executor/macros.rs b/crates/tinywasm/src/runtime/executor/macros.rs index 154d7f7..7a999a2 100644 --- a/crates/tinywasm/src/runtime/executor/macros.rs +++ b/crates/tinywasm/src/runtime/executor/macros.rs @@ -38,7 +38,29 @@ macro_rules! lts_instr { }}; } +/// Multiply the top two values on the stack +macro_rules! mul_instr { + ($ty:ty, $stack:ident) => {{ + let [a, b] = $stack.values.pop_n_const::<2>()?; + let a: $ty = a.into(); + let b: $ty = b.into(); + $stack.values.push((a * b).into()); + }}; +} + +/// Compare the top two values on the stack for equality +macro_rules! eq_instr { + ($ty:ty, $stack:ident) => {{ + let [a, b] = $stack.values.pop_n_const::<2>()?; + let a: $ty = a.into(); + let b: $ty = b.into(); + $stack.values.push(((a == b) as i32).into()); + }}; +} + pub(super) use add_instr; pub(super) use div_instr; +pub(super) use eq_instr; pub(super) use lts_instr; +pub(super) use mul_instr; pub(super) use sub_instr; diff --git a/crates/tinywasm/src/runtime/executor/mod.rs b/crates/tinywasm/src/runtime/executor/mod.rs index b71ae11..95b5391 100644 --- a/crates/tinywasm/src/runtime/executor/mod.rs +++ b/crates/tinywasm/src/runtime/executor/mod.rs @@ -156,7 +156,7 @@ fn exec_one( stack.values.extend(res.iter().copied()); } _ => { - panic!("Attempted to end a block that is not the top block"); + panic!("end: unimplemented block type end: {:?}", block.ty); } } } @@ -200,6 +200,26 @@ fn exec_one( F32Div => div_instr!(f32, stack), F64Div => div_instr!(f64, stack), + I32Mul => mul_instr!(i32, stack), + I64Mul => mul_instr!(i64, stack), + F32Mul => mul_instr!(f32, stack), + F64Mul => mul_instr!(f64, stack), + + I32Eq => eq_instr!(i32, stack), + I64Eq => eq_instr!(i64, stack), + F32Eq => eq_instr!(f32, stack), + F64Eq => eq_instr!(f64, stack), + + I32Eqz => { + let val: i32 = stack.values.pop().ok_or(Error::StackUnderflow)?.into(); + stack.values.push(((val == 0) as i32).into()); + } + + I64Eqz => { + let val: i64 = stack.values.pop().ok_or(Error::StackUnderflow)?.into(); + stack.values.push(((val == 0) as i32).into()); + } + i => todo!("{:?}", i), }; -- cgit v1.3.1