Skip to content

Commit fea3fbe

Browse files
chore: reverse stack op closures
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent a4e7001 commit fea3fbe

6 files changed

Lines changed: 83 additions & 130 deletions

File tree

‎Cargo.lock‎

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

‎Cargo.toml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ default-members=[".", "crates/tinywasm", "crates/types", "crates/parser"]
44
resolver="3"
55

66
[workspace.dependencies]
7-
tinywasm-parser={version="0.9.0", path="crates/parser", default-features=false}
8-
tinywasm-types={version="0.9.0", path="crates/types", default-features=false}
9-
tinywasm-cli={version="0.9.0", path="crates/cli", default-features=false}
10-
tinywasm={version="0.9.0", path="crates/tinywasm", default-features=false}
7+
tinywasm-parser={version="0.10.0", path="crates/parser", default-features=false}
8+
tinywasm-types={version="0.10.0", path="crates/types", default-features=false}
9+
tinywasm-cli={version="0.10.0", path="crates/cli", default-features=false}
10+
tinywasm={version="0.10.0", path="crates/tinywasm", default-features=false}
1111

1212
criterion={version="0.8", default-features=false, features=["cargo_bench_support", "rayon"]}
1313
eyre="0.6"
@@ -23,7 +23,7 @@ wast="254"
2323
wat="1.254"
2424

2525
[workspace.package]
26-
version="0.9.1"
26+
version="0.10.0"
2727
rust-version="1.95"
2828
edition="2024"
2929
license="MIT OR Apache-2.0"

‎crates/tinywasm/src/interpreter/executor.rs‎

Lines changed: 64 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
6262
}
6363
}
6464

65-
#[allow(clippy::redundant_closure_call)] // we wrap these in closures to get better perf traces
6665
#[inline(always)]
6766
fn exec(&mut self) -> Result<Option<()>, Trap> {
6867
macro_rules! stack_op {
@@ -73,81 +72,56 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
7372
stack_op!(binary $ty => $ty, |$lhs, $rhs| $expr)
7473
};
7574
(binary try $ty:ty, |$lhs:ident, $rhs:ident| $expr:expr) => {{
76-
(|| -> Result<(), Trap> {
77-
let $rhs = <$ty>::stack_pop(&mut self.store.value_stack);
78-
let $lhs = <$ty>::stack_pop(&mut self.store.value_stack);
79-
<$ty>::stack_push(&mut self.store.value_stack, $expr?)?;
80-
Ok(())
81-
})()?;
75+
let $rhs = <$ty>::stack_pop(&mut self.store.value_stack);
76+
let $lhs = <$ty>::stack_pop(&mut self.store.value_stack);
77+
<$ty>::stack_push(&mut self.store.value_stack, $expr?)?;
8278
}};
8379
(unary $from:ty => $to:ty, |$v:ident| $expr:expr) => {{
84-
(|| -> Result<(), Trap> {
85-
let $v = <$from>::stack_pop(&mut self.store.value_stack);
86-
<$to>::stack_push(&mut self.store.value_stack, $expr)?;
87-
Ok(())
88-
})()?;
80+
let $v = <$from>::stack_pop(&mut self.store.value_stack);
81+
<$to>::stack_push(&mut self.store.value_stack, $expr)?;
8982
}};
9083
(binary $from:ty => $to:ty, |$lhs:ident, $rhs:ident| $expr:expr) => {{
91-
(|| -> Result<(), Trap> {
92-
let $rhs = <$from>::stack_pop(&mut self.store.value_stack);
93-
let $lhs = <$from>::stack_pop(&mut self.store.value_stack);
94-
<$to>::stack_push(&mut self.store.value_stack, $expr)?;
95-
Ok(())
96-
})()?;
84+
let $rhs = <$from>::stack_pop(&mut self.store.value_stack);
85+
let $lhs = <$from>::stack_pop(&mut self.store.value_stack);
86+
<$to>::stack_push(&mut self.store.value_stack, $expr)?;
9787
}};
9888
(binary_into2 $from:ty => $to:ty, |$lhs:ident, $rhs:ident| $expr:expr) => {{
99-
(|| -> Result<(), Trap> {
100-
let $rhs = <$from>::stack_pop(&mut self.store.value_stack);
101-
let $lhs = <$from>::stack_pop(&mut self.store.value_stack);
102-
let out = $expr;
103-
<$to>::stack_push(&mut self.store.value_stack, out.0)?;
104-
<$to>::stack_push(&mut self.store.value_stack, out.1)?;
105-
Ok(())
106-
})()?;
89+
let $rhs = <$from>::stack_pop(&mut self.store.value_stack);
90+
let $lhs = <$from>::stack_pop(&mut self.store.value_stack);
91+
let out = $expr;
92+
<$to>::stack_push(&mut self.store.value_stack, out.0)?;
93+
<$to>::stack_push(&mut self.store.value_stack, out.1)?;
10794
}};
10895
(binary $lhs_ty:ty, $rhs_ty:ty, |$lhs:ident, $rhs:ident| $expr:expr) => {
10996
stack_op!(binary $lhs_ty, $rhs_ty => $rhs_ty, |$lhs, $rhs| $expr)
11097
};
11198
(binary $lhs_ty:ty, $rhs_ty:ty => $res:ty, |$lhs:ident, $rhs:ident| $expr:expr) => {{
112-
(|| -> Result<(), Trap> {
113-
let $rhs = <$rhs_ty>::stack_pop(&mut self.store.value_stack);
114-
let $lhs = <$lhs_ty>::stack_pop(&mut self.store.value_stack);
115-
<$res>::stack_push(&mut self.store.value_stack, $expr)?;
116-
Ok(())
117-
})()?;
99+
let $rhs = <$rhs_ty>::stack_pop(&mut self.store.value_stack);
100+
let $lhs = <$lhs_ty>::stack_pop(&mut self.store.value_stack);
101+
<$res>::stack_push(&mut self.store.value_stack, $expr)?;
118102
}};
119103
(ternary $ty:ty, |$a:ident, $b:ident, $c:ident| $expr:expr) => {{
120-
(|| -> Result<(), Trap> {
121-
let $c = <$ty>::stack_pop(&mut self.store.value_stack);
122-
let $b = <$ty>::stack_pop(&mut self.store.value_stack);
123-
let $a = <$ty>::stack_pop(&mut self.store.value_stack);
124-
<$ty>::stack_push(&mut self.store.value_stack, $expr)?;
125-
Ok(())
126-
})()?;
104+
let $c = <$ty>::stack_pop(&mut self.store.value_stack);
105+
let $b = <$ty>::stack_pop(&mut self.store.value_stack);
106+
let $a = <$ty>::stack_pop(&mut self.store.value_stack);
107+
<$ty>::stack_push(&mut self.store.value_stack, $expr)?;
127108
}};
128109
(quaternary_into2 $from:ty => $to:ty, |$a:ident, $b:ident, $c:ident, $d:ident| $expr:expr) => {{
129-
(|| -> Result<(), Trap> {
130-
let $d = <$from>::stack_pop(&mut self.store.value_stack);
131-
let $c = <$from>::stack_pop(&mut self.store.value_stack);
132-
let $b = <$from>::stack_pop(&mut self.store.value_stack);
133-
let $a = <$from>::stack_pop(&mut self.store.value_stack);
134-
let out = $expr;
135-
<$to>::stack_push(&mut self.store.value_stack, out.0)?;
136-
<$to>::stack_push(&mut self.store.value_stack, out.1)?;
137-
Ok(())
138-
})()?;
110+
let $d = <$from>::stack_pop(&mut self.store.value_stack);
111+
let $c = <$from>::stack_pop(&mut self.store.value_stack);
112+
let $b = <$from>::stack_pop(&mut self.store.value_stack);
113+
let $a = <$from>::stack_pop(&mut self.store.value_stack);
114+
let out = $expr;
115+
<$to>::stack_push(&mut self.store.value_stack, out.0)?;
116+
<$to>::stack_push(&mut self.store.value_stack, out.1)?;
139117
}};
140118
(local_set_pop $ty:ty, $local_index:expr) => {{
141-
(|| {
142-
let val = <$ty>::stack_pop(&mut self.store.value_stack);
143-
<$ty>::local_set(&mut self.store.value_stack, &self.cf, *$local_index, val);
144-
})();
119+
let val = <$ty>::stack_pop(&mut self.store.value_stack);
120+
<$ty>::local_set(&mut self.store.value_stack, &self.cf, *$local_index, val);
145121
}};
146122
(local_tee $ty:ty, $local_index:expr) => {{
147-
(|| {
148-
let val = <$ty>::stack_peek(&self.store.value_stack);
149-
<$ty>::local_set(&mut self.store.value_stack, &self.cf, *$local_index, val);
150-
})();
123+
let val = <$ty>::stack_peek(&self.store.value_stack);
124+
<$ty>::local_set(&mut self.store.value_stack, &self.cf, *$local_index, val);
151125
}};
152126
}
153127

@@ -194,62 +168,46 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
194168

195169
macro_rules! binop {
196170
(local_local $vt:ty, $width:tt, $op:ident, $a:ident, $b:ident) => {{
197-
(|| -> Result<(), Trap> {
198-
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$a);
199-
let rhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$b);
200-
self.store.value_stack.push(exec_binop!($width, *$op, lhs, rhs))?;
201-
Ok(())
202-
})()?;
171+
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$a);
172+
let rhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$b);
173+
self.store.value_stack.push(exec_binop!($width, *$op, lhs, rhs))?;
203174
}};
204175
(local_local_set $vt:ty, $width:tt, $op:ident, $a:ident, $b:ident, $dst:ident) => {{
205-
(|| {
206-
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$a);
207-
let rhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$b);
208-
let value = exec_binop!($width, *$op, lhs, rhs);
209-
<$vt>::local_set(&mut self.store.value_stack, &self.cf, *$dst, value);
210-
})();
176+
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$a);
177+
let rhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$b);
178+
let value = exec_binop!($width, *$op, lhs, rhs);
179+
<$vt>::local_set(&mut self.store.value_stack, &self.cf, *$dst, value);
211180
}};
212181
(local_local_tee $vt:ty, $width:tt, $op:ident, $a:ident, $b:ident, $dst:ident) => {{
213-
(|| -> Result<(), Trap> {
214-
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$a);
215-
let rhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$b);
216-
let value = exec_binop!($width, *$op, lhs, rhs);
217-
<$vt>::local_set(&mut self.store.value_stack, &self.cf, *$dst, value);
218-
self.store.value_stack.push(value)?;
219-
Ok(())
220-
})()?;
182+
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$a);
183+
let rhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$b);
184+
let value = exec_binop!($width, *$op, lhs, rhs);
185+
<$vt>::local_set(&mut self.store.value_stack, &self.cf, *$dst, value);
186+
self.store.value_stack.push(value)?;
221187
}};
222188
(local_const $vt:ty, $width:tt, $op:ident, $local:ident, $rhs:expr) => {{
223-
(|| -> Result<(), Trap> {
224-
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$local);
225-
self.store.value_stack.push(exec_binop!($width, *$op, lhs, $rhs))?;
226-
Ok(())
227-
})()?;
189+
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$local);
190+
self.store.value_stack.push(exec_binop!($width, *$op, lhs, $rhs))?;
228191
}};
229192
(local_const_set $vt:ty, $width:tt, $op:ident, $local:ident, $rhs:expr, $dst:ident) => {{
230-
(|| {
231-
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$local);
232-
let value = exec_binop!($width, *$op, lhs, $rhs);
233-
<$vt>::local_set(&mut self.store.value_stack, &self.cf, *$dst, value);
234-
})();
193+
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$local);
194+
let value = exec_binop!($width, *$op, lhs, $rhs);
195+
<$vt>::local_set(&mut self.store.value_stack, &self.cf, *$dst, value);
235196
}};
236197
(local_const_tee $vt:ty, $width:tt, $op:ident, $local:ident, $rhs:expr, $dst:ident) => {{
237-
(|| -> Result<(), Trap> {
238-
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$local);
239-
let value = exec_binop!($width, *$op, lhs, $rhs);
240-
<$vt>::local_set(&mut self.store.value_stack, &self.cf, *$dst, value);
241-
self.store.value_stack.push(value)?;
242-
Ok(())
243-
})()?;
198+
let lhs = <$vt>::local_get(&self.store.value_stack, &self.cf, *$local);
199+
let value = exec_binop!($width, *$op, lhs, $rhs);
200+
<$vt>::local_set(&mut self.store.value_stack, &self.cf, *$dst, value);
201+
self.store.value_stack.push(value)?;
244202
}};
245-
(stack_global $vt:ty, $as_fn:ident, $width:tt, $op:ident, $global:ident) => {{
246-
(|| -> Result<(), Trap> {
247-
let global_val =
248-
self.store.state.get_global_val(self.module.resolve_global_addr(*$global)).$as_fn().unwrap();
249-
let stack_val = <$vt>::stack_pop(&mut self.store.value_stack);
250-
self.store.value_stack.push(exec_binop!($width, *$op, stack_val, global_val))?;
251-
Ok(())
252-
})()?;
203+
(stack_global $vt:ident, $width:tt, $op:ident, $global:ident) => {{
204+
let TinyWasmValue::$vt(global_val) =
205+
self.store.state.get_global_val(self.module.resolve_global_addr(*$global))
206+
else {
207+
unreachable!("expected global to be Value32")
208+
};
209+
let stack_val = <$vt>::stack_pop(&mut self.store.value_stack);
210+
self.store.value_stack.push(exec_binop!($width, *$op, stack_val, global_val))?;
253211
}};
254212
}
255213

@@ -329,8 +287,8 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
329287
BinOpLocalConstTee32(op, local_index, c, dst) => binop!(local_const_tee Value32, 32, op, local_index, *c as u32, dst),
330288
BinOpLocalConstTee64(op, local_index, c, dst) => binop!(local_const_tee Value64, 64, op, local_index, *c as u64, dst),
331289
BinOpLocalConstTee128(op, local_index, c, dst) => binop!(local_const_tee Value128, 128, op, local_index, Value128(self.func.data.v128_const(*c)), dst),
332-
BinOpStackGlobal32(op, global_index) => binop!(stack_global Value32, as_32, 32, op, global_index),
333-
BinOpStackGlobal64(op, global_index) => binop!(stack_global Value64, as_64, 64, op, global_index),
290+
BinOpStackGlobal32(op, global_index) => binop!(stack_global Value32, 32, op, global_index),
291+
BinOpStackGlobal64(op, global_index) => binop!(stack_global Value64, 64, op, global_index),
334292
SetLocalConst32(local_index, c) => i32::local_set(&mut self.store.value_stack, &self.cf, *local_index, *c),
335293
SetLocalConst64(local_index, c) => i64::local_set(&mut self.store.value_stack, &self.cf, *local_index, *c),
336294
SetLocalConst128(local_index, c) => Value128::local_set(&mut self.store.value_stack, &self.cf, *local_index, Value128(self.func.data.v128_const(*c))),
@@ -1381,16 +1339,8 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
13811339
let offset = i32::stack_pop(&mut self.store.value_stack);
13821340
let dst = i32::stack_pop(&mut self.store.value_stack);
13831341

1384-
let data_addr = self.module.resolve_data_addr(data_index) as usize;
1385-
let Some(data) = self.store.state.data.get(data_addr) else {
1386-
unreachable!("data segment not found, should have been validated by the parser")
1387-
};
1388-
1389-
let mem_addr = self.module.resolve_mem_addr(mem_index) as usize;
1390-
let Some(mem) = self.store.state.memories.get_mut(mem_addr) else {
1391-
unreachable!("memory not found, should have been validated by the parser")
1392-
};
1393-
1342+
let data = &self.store.state.data[self.module.resolve_data_addr(data_index) as usize];
1343+
let mem = &mut self.store.state.memories[self.module.resolve_mem_addr(mem_index) as usize];
13941344
let data_len = data.data.as_ref().map_or(0, |d| d.len());
13951345
if ((size + offset) as usize > data_len) || ((dst + size) as usize > mem.inner.len()) {
13961346
cold_path();

‎crates/tinywasm/src/interpreter/stack/value_stack.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,17 @@ impl<T: Copy + Default> Stack<T> {
5858

5959
#[inline(always)]
6060
pub(crate) fn get(&self, index: usize) -> &T {
61-
self.data.get(index).unwrap_or_else(|| unreachable!("Stack index out of bounds, this is a bug"))
61+
&self.data[index]
6262
}
6363

6464
#[inline(always)]
6565
pub(crate) fn set(&mut self, index: usize, value: T) {
66-
*self.data.get_mut(index).unwrap_or_else(|| unreachable!("Stack index out of bounds, this is a bug")) = value;
66+
self.data[index] = value;
6767
}
6868

6969
#[inline(always)]
7070
pub(crate) fn copy(&mut self, from: usize, to: usize) {
71-
let val = self.data.get(from).unwrap_or_else(|| unreachable!("Stack index out of bounds, this is a bug"));
72-
*self.data.get_mut(to).unwrap_or_else(|| unreachable!("Stack index out of bounds, this is a bug")) = *val;
71+
self.data[to] = self.data[from];
7372
}
7473

7574
#[inline(always)]

‎crates/tinywasm/src/interpreter/values.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ macro_rules! impl_internalvalue {
178178
#[inline(always)]
179179
fn stack_push(stack: &mut ValueStack, value: Self) -> Result<(), crate::Trap> {
180180
let $to_stack_v = value;
181-
stack.$stack.push($to_stack)
181+
if let Err(e) = stack.$stack.push($to_stack) {
182+
core::hint::cold_path();
183+
return Err(e);
184+
}
185+
Ok(())
182186
}
183187

184188
#[inline(always)]

‎crates/tinywasm/src/store/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static STORE_ID: AtomicUsize = AtomicUsize::new(0);
4141
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#store>
4242
pub struct Store {
4343
id: usize,
44-
module_instances: Vec<ModuleInstance>,
44+
pub(crate) module_instances: Vec<ModuleInstance>,
4545

4646
pub(crate) engine: Engine,
4747
pub(crate) execution_fuel: u32,

0 commit comments

Comments
 (0)