Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 8eec592

Browse files
chore: add more arithmetic instructions
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent aa2cbdc commit 8eec592

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

crates/tinywasm/src/runtime/executor/macros.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,29 @@ macro_rules! lts_instr {
3838
}};
3939
}
4040

41+
/// Multiply the top two values on the stack
42+
macro_rules! mul_instr {
43+
($ty:ty, $stack:ident) => {{
44+
let [a, b] = $stack.values.pop_n_const::<2>()?;
45+
let a: $ty = a.into();
46+
let b: $ty = b.into();
47+
$stack.values.push((a * b).into());
48+
}};
49+
}
50+
51+
/// Compare the top two values on the stack for equality
52+
macro_rules! eq_instr {
53+
($ty:ty, $stack:ident) => {{
54+
let [a, b] = $stack.values.pop_n_const::<2>()?;
55+
let a: $ty = a.into();
56+
let b: $ty = b.into();
57+
$stack.values.push(((a == b) as i32).into());
58+
}};
59+
}
60+
4161
pub(super) use add_instr;
4262
pub(super) use div_instr;
63+
pub(super) use eq_instr;
4364
pub(super) use lts_instr;
65+
pub(super) use mul_instr;
4466
pub(super) use sub_instr;

crates/tinywasm/src/runtime/executor/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn exec_one(
156156
stack.values.extend(res.iter().copied());
157157
}
158158
_ => {
159-
panic!("Attempted to end a block that is not the top block");
159+
panic!("end: unimplemented block type end: {:?}", block.ty);
160160
}
161161
}
162162
}
@@ -200,6 +200,26 @@ fn exec_one(
200200
F32Div => div_instr!(f32, stack),
201201
F64Div => div_instr!(f64, stack),
202202

203+
I32Mul => mul_instr!(i32, stack),
204+
I64Mul => mul_instr!(i64, stack),
205+
F32Mul => mul_instr!(f32, stack),
206+
F64Mul => mul_instr!(f64, stack),
207+
208+
I32Eq => eq_instr!(i32, stack),
209+
I64Eq => eq_instr!(i64, stack),
210+
F32Eq => eq_instr!(f32, stack),
211+
F64Eq => eq_instr!(f64, stack),
212+
213+
I32Eqz => {
214+
let val: i32 = stack.values.pop().ok_or(Error::StackUnderflow)?.into();
215+
stack.values.push(((val == 0) as i32).into());
216+
}
217+
218+
I64Eqz => {
219+
let val: i64 = stack.values.pop().ok_or(Error::StackUnderflow)?.into();
220+
stack.values.push(((val == 0) as i32).into());
221+
}
222+
203223
i => todo!("{:?}", i),
204224
};
205225

0 commit comments

Comments
 (0)