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

Commit d20e788

Browse files
feat: improve func type resolution, int ops
Signed-off-by: Henry <[email protected]>
1 parent 4ee0bac commit d20e788

File tree

17 files changed

+1583
-937
lines changed

17 files changed

+1583
-937
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/parser/src/conversion.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ pub fn process_operators<'a>(
407407
..
408408
} => Instruction::CallIndirect(type_index, table_index),
409409
Drop => Instruction::Drop,
410-
Select => Instruction::Select,
410+
Select => Instruction::Select(None),
411+
TypedSelect { ty } => Instruction::Select(Some(convert_valtype(&ty))),
411412
LocalGet { local_index } => Instruction::LocalGet(local_index),
412413
LocalSet { local_index } => Instruction::LocalSet(local_index),
413414
LocalTee { local_index } => Instruction::LocalTee(local_index),
@@ -547,6 +548,8 @@ pub fn process_operators<'a>(
547548
I32TruncF64U => Instruction::I32TruncF64U,
548549
I64ExtendI32S => Instruction::I64ExtendI32S,
549550
I64ExtendI32U => Instruction::I64ExtendI32U,
551+
I32Extend8S => Instruction::I32Extend8S,
552+
I32Extend16S => Instruction::I32Extend16S,
550553
I64TruncF32S => Instruction::I64TruncF32S,
551554
I64TruncF32U => Instruction::I64TruncF32U,
552555
I64TruncF64S => Instruction::I64TruncF64S,

crates/tinywasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ eyre={version="0.6"}
2525
serde_json={version="1.0"}
2626
serde={version="1.0", features=["derive"]}
2727
plotters={version="0.3"}
28+
pretty_env_logger="0.5"
2829

2930
[features]
3031
default=["std", "parser", "logging"]

crates/tinywasm/src/instance.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,16 @@ impl ModuleInstance {
6565
}
6666

6767
let export = self.0.exports.get(name, ExternalKind::Func)?;
68+
log::debug!("get_func: export: {:?}", export);
69+
70+
log::debug!("{:?}", self.0.func_addrs);
6871
let func_addr = self.0.func_addrs[export.index as usize];
72+
log::debug!("get_func: func index: {}", export.index);
6973
let func = store.get_func(func_addr as usize)?;
74+
log::debug!("get_func: func_addr: {}, func: {:?}", func_addr, func);
7075
let ty = self.0.types[func.ty_addr() as usize].clone();
76+
log::debug!("get_func: ty: {:?}", ty);
77+
log::debug!("types: {:?}", self.0.types);
7178

7279
Ok(FuncHandle {
7380
addr: export.index,

crates/tinywasm/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ impl Module {
5959
// imports: Option<()>,
6060
) -> Result<ModuleInstance> {
6161
let idx = store.next_module_instance_idx();
62-
6362
let func_addrs = store.add_funcs(self.data.funcs.into(), idx);
63+
6464
let instance = ModuleInstance::new(
6565
self.data.func_types,
6666
self.data.start_func,

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ macro_rules! comp_zero {
5151
}
5252

5353
/// Apply an arithmetic operation to two values on the stack
54-
macro_rules! arithmetic {
54+
macro_rules! arithmetic_op {
5555
($op:tt, $ty:ty, $stack:ident) => {{
5656
let [a, b] = $stack.values.pop_n_const::<2>()?;
5757
let a: $ty = a.into();
@@ -60,19 +60,36 @@ macro_rules! arithmetic {
6060
}};
6161
}
6262

63+
macro_rules! arithmetic_method {
64+
($op:ident, $ty:ty, $stack:ident) => {{
65+
let [a, b] = $stack.values.pop_n_const::<2>()?;
66+
let a: $ty = a.into();
67+
let b: $ty = b.into();
68+
let result = a.$op(b);
69+
$stack.values.push(result.into());
70+
}};
71+
}
72+
6373
/// Apply an arithmetic operation to two values on the stack
64-
macro_rules! checked_arithmetic {
74+
macro_rules! checked_arithmetic_method {
6575
($op:ident, $ty:ty, $stack:ident, $trap:expr) => {{
6676
let [a, b] = $stack.values.pop_n_const::<2>()?;
6777
let a: $ty = a.into();
6878
let b: $ty = b.into();
6979
let result = a.$op(b).ok_or_else(|| Error::Trap($trap))?;
80+
debug!(
81+
"checked_arithmetic_method: {}, a: {}, b: {}, res: {}",
82+
stringify!($op),
83+
a,
84+
b,
85+
result
86+
);
7087
$stack.values.push(result.into());
7188
}};
7289
}
7390

7491
/// Apply an arithmetic operation to two values on the stack (cast to ty2 before operation)
75-
macro_rules! checked_arithmetic_cast {
92+
macro_rules! checked_arithmetic_method_cast {
7693
($op:ident, $ty:ty, $ty2:ty, $stack:ident, $trap:expr) => {{
7794
let [a, b] = $stack.values.pop_n_const::<2>()?;
7895
let a: $ty = a.into();
@@ -87,9 +104,10 @@ macro_rules! checked_arithmetic_cast {
87104
}};
88105
}
89106

90-
pub(super) use arithmetic;
91-
pub(super) use checked_arithmetic;
92-
pub(super) use checked_arithmetic_cast;
107+
pub(super) use arithmetic_method;
108+
pub(super) use arithmetic_op;
109+
pub(super) use checked_arithmetic_method;
110+
pub(super) use checked_arithmetic_method_cast;
93111
pub(super) use comp;
94112
pub(super) use comp_cast;
95113
pub(super) use comp_zero;

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ fn exec_one(
8585
Nop => { /* do nothing */ }
8686
Unreachable => return Ok(ExecResult::Trap(crate::Trap::Unreachable)), // we don't need to include the call frame here because it's already on the stack
8787
Drop => stack.values.pop().map(|_| ())?,
88-
Select => {
88+
Select(t) => {
89+
if t.is_some() {
90+
unimplemented!("select with type");
91+
}
92+
8993
let cond: i32 = stack.values.pop()?.into();
9094
let val2 = stack.values.pop()?;
9195

@@ -283,34 +287,34 @@ fn exec_one(
283287
F32Gt => comp!(>, f32, stack),
284288
F64Gt => comp!(>, f64, stack),
285289

286-
I64Add => arithmetic!(+, i64, stack),
287-
I32Add => arithmetic!(+, i32, stack),
288-
F32Add => arithmetic!(+, f32, stack),
289-
F64Add => arithmetic!(+, f64, stack),
290+
I64Add => arithmetic_method!(wrapping_add, i64, stack),
291+
I32Add => arithmetic_method!(wrapping_add, i32, stack),
292+
F32Add => arithmetic_op!(+, f32, stack),
293+
F64Add => arithmetic_op!(+, f64, stack),
290294

291-
I32Sub => arithmetic!(-, i32, stack),
292-
I64Sub => arithmetic!(-, i64, stack),
293-
F32Sub => arithmetic!(-, f32, stack),
294-
F64Sub => arithmetic!(-, f64, stack),
295+
I32Sub => arithmetic_method!(wrapping_sub, i32, stack),
296+
I64Sub => arithmetic_method!(wrapping_sub, i64, stack),
297+
F32Sub => arithmetic_op!(-, f32, stack),
298+
F64Sub => arithmetic_op!(-, f64, stack),
295299

296-
F32Div => arithmetic!(/, f32, stack),
297-
F64Div => arithmetic!(/, f64, stack),
300+
F32Div => arithmetic_op!(/, f32, stack),
301+
F64Div => arithmetic_op!(/, f64, stack),
298302

299-
I32Mul => arithmetic!(*, i32, stack),
300-
I64Mul => arithmetic!(*, i64, stack),
301-
F32Mul => arithmetic!(*, f32, stack),
302-
F64Mul => arithmetic!(*, f64, stack),
303+
I32Mul => arithmetic_method!(wrapping_mul, i32, stack),
304+
I64Mul => arithmetic_method!(wrapping_mul, i64, stack),
305+
F32Mul => arithmetic_op!(*, f32, stack),
306+
F64Mul => arithmetic_op!(*, f64, stack),
303307

304308
// these can trap
305-
I32DivS => checked_arithmetic!(checked_div, i32, stack, crate::Trap::DivisionByZero),
306-
I64DivS => checked_arithmetic!(checked_div, i64, stack, crate::Trap::DivisionByZero),
307-
I32DivU => checked_arithmetic_cast!(checked_div, i32, u32, stack, crate::Trap::DivisionByZero),
308-
I64DivU => checked_arithmetic_cast!(checked_div, i64, u64, stack, crate::Trap::DivisionByZero),
309-
310-
I32RemS => checked_arithmetic!(checked_rem, i32, stack, crate::Trap::DivisionByZero),
311-
I64RemS => checked_arithmetic!(checked_rem, i64, stack, crate::Trap::DivisionByZero),
312-
I32RemU => checked_arithmetic_cast!(checked_rem, i32, u32, stack, crate::Trap::DivisionByZero),
313-
I64RemU => checked_arithmetic_cast!(checked_rem, i64, u64, stack, crate::Trap::DivisionByZero),
309+
I32DivS => checked_arithmetic_method!(checked_div, i32, stack, crate::Trap::DivisionByZero),
310+
I64DivS => checked_arithmetic_method!(checked_div, i64, stack, crate::Trap::DivisionByZero),
311+
I32DivU => checked_arithmetic_method_cast!(checked_div, i32, u32, stack, crate::Trap::DivisionByZero),
312+
I64DivU => checked_arithmetic_method_cast!(checked_div, i64, u64, stack, crate::Trap::DivisionByZero),
313+
314+
I32RemS => checked_arithmetic_method!(checked_rem, i32, stack, crate::Trap::DivisionByZero),
315+
I64RemS => checked_arithmetic_method!(checked_rem, i64, stack, crate::Trap::DivisionByZero),
316+
I32RemU => checked_arithmetic_method_cast!(checked_rem, i32, u32, stack, crate::Trap::DivisionByZero),
317+
I64RemU => checked_arithmetic_method_cast!(checked_rem, i64, u64, stack, crate::Trap::DivisionByZero),
314318

315319
F32ConvertI32S => conv_1!(i32, f32, stack),
316320
F32ConvertI64S => conv_1!(i64, f32, stack),

crates/tinywasm/src/store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ impl Store {
126126

127127
pub(crate) fn add_funcs(&mut self, funcs: Vec<Function>, idx: ModuleInstanceAddr) -> Vec<FuncAddr> {
128128
let mut func_addrs = Vec::with_capacity(funcs.len());
129-
for func in funcs.into_iter() {
129+
for (i, func) in funcs.into_iter().enumerate() {
130130
self.data.funcs.push(Rc::new(FunctionInstance {
131131
func,
132132
_module_instance: idx,
133133
}));
134-
func_addrs.push(idx as FuncAddr);
134+
func_addrs.push(i as FuncAddr);
135135
}
136136
func_addrs
137137
}

0 commit comments

Comments
 (0)