Skip to content

Commit a15acab

Browse files
chore: cleanup
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 303abc7 commit a15acab

File tree

5 files changed

+70
-78
lines changed

5 files changed

+70
-78
lines changed

crates/parser/src/module.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ impl ModuleReader {
210210
.map(|(func_idx, ((instructions, data, locals), ty_idx))| {
211211
let ty = self.func_types.get(ty_idx as usize).expect("No func type for func, this is a bug").clone();
212212
let params = ValueCountsSmall::from(&ty.params);
213+
let locals = ValueCountsSmall {
214+
c32: u16::try_from(locals.c32).unwrap_or_else(|_| unreachable!("local count exceeds u16")),
215+
c64: u16::try_from(locals.c64).unwrap_or_else(|_| unreachable!("local count exceeds u16")),
216+
c128: u16::try_from(locals.c128).unwrap_or_else(|_| unreachable!("local count exceeds u16")),
217+
cref: u16::try_from(locals.cref).unwrap_or_else(|_| unreachable!("local count exceeds u16")),
218+
};
213219
let self_func_addr = imported_func_count + func_idx as u32;
214220
let mut instructions = instructions.to_vec();
215221
Self::apply_instruction_rewrites(&mut instructions, self_func_addr);

crates/tinywasm/src/func.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ impl FuncHandle {
6666
// Reset stack, push args, allocate locals, create entry frame.
6767
store.stack.clear();
6868
store.stack.values.extend_from_wasmvalues(params)?;
69-
let (locals_base, _stack_base, stack_offset) =
70-
store.stack.values.enter_locals(wasm_func.params, wasm_func.locals)?;
69+
let locals_base = store.stack.values.enter_locals(&wasm_func.params, &wasm_func.locals)?;
70+
let stack_offset = wasm_func.locals;
7171
let callframe = CallFrame::new(self.addr, func_inst.owner, locals_base, stack_offset);
7272

7373
// Execute until completion and then collect result values from the stack.
@@ -100,8 +100,8 @@ impl FuncHandle {
100100
Function::Wasm(wasm_func) => {
101101
store.stack.clear();
102102
store.stack.values.extend_from_wasmvalues(params)?;
103-
let (locals_base, _stack_base, stack_offset) =
104-
store.stack.values.enter_locals(wasm_func.params, wasm_func.locals)?;
103+
let locals_base = store.stack.values.enter_locals(&wasm_func.params, &wasm_func.locals)?;
104+
let stack_offset = wasm_func.locals;
105105
let callframe = CallFrame::new(self.addr, func_inst_owner, locals_base, stack_offset);
106106

107107
Ok(FuncExecution {

crates/tinywasm/src/interpreter/executor.rs

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
4949

5050
#[inline(always)]
5151
fn exec<const ITERATIONS: usize>(&mut self) -> Result<Option<()>> {
52-
for _ in 0..ITERATIONS {
53-
use tinywasm_types::Instruction::*;
54-
55-
macro_rules! stack_op {
52+
macro_rules! stack_op {
5653
(simd_unary $method:ident) => { stack_op!(unary Value128, |v| v.$method()) };
5754
(simd_binary $method:ident) => { stack_op!(binary Value128, |a, b| a.$method(b)) };
5855
(unary $ty:ty, |$v:ident| $expr:expr) => { self.store.stack.values.unary::<$ty>(|$v| Ok($expr))? };
@@ -73,6 +70,9 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
7370
}};
7471
}
7572

73+
for _ in 0..ITERATIONS {
74+
use tinywasm_types::Instruction::*;
75+
7676
let next = match self.func.instructions.0.get(self.cf.instr_ptr as usize) {
7777
Some(instr) => instr,
7878
None => unreachable!(
@@ -164,9 +164,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
164164
let mem = self.store.state.get_mem_mut(self.module.resolve_mem_addr(m.mem_addr()));
165165
let addr = u64::from(self.store.stack.values.local_get::<u32>(&self.cf, *addr_local));
166166
let value = self.store.stack.values.local_get::<u32>(&self.cf, *value_local).to_mem_bytes();
167-
if let Err(e) = mem.store((m.offset() + addr) as usize, value.len(), &value) {
168-
return Err(e);
169-
}
167+
mem.store((m.offset() + addr) as usize, value.len(), &value)?;
170168
}
171169
I32LoadLocalTee(m, addr_local, dst_local) => {
172170
let mem = self.store.state.get_mem(self.module.resolve_mem_addr(m.mem_addr()));
@@ -677,24 +675,15 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
677675
self.store.stack.values.truncate_keep_counts(self.cf.locals_base, wasm_func.params);
678676
}
679677

680-
let (locals_base, _stack_base, stack_offset) =
681-
match self.store.stack.values.enter_locals(wasm_func.params, wasm_func.locals) {
682-
Ok(v) => v,
683-
Err(Error::Trap(Trap::ValueStackOverflow)) if !IS_RETURN_CALL => {
684-
return Err(Trap::CallStackOverflow.into());
685-
}
686-
Err(err) => return Err(err),
687-
};
688-
689-
let new_call_frame = CallFrame::new(func_addr, owner, locals_base, stack_offset);
678+
let res = self.store.stack.values.enter_locals(&wasm_func.params, &wasm_func.locals);
679+
let locals_base = res.map_err(|err| if IS_RETURN_CALL { err } else { Error::Trap(Trap::CallStackOverflow) })?;
680+
let new_call_frame = CallFrame::new(func_addr, owner, locals_base, wasm_func.locals);
690681

691-
if IS_RETURN_CALL {
692-
self.cf = new_call_frame;
693-
} else {
682+
if !IS_RETURN_CALL {
694683
self.cf.incr_instr_ptr(); // skip the call instruction
695684
self.store.stack.call_stack.push(self.cf)?;
696-
self.cf = new_call_frame;
697685
}
686+
self.cf = new_call_frame;
698687

699688
if self.cf.module_addr != self.module.idx {
700689
self.module = self.store.get_module_instance_raw(self.cf.module_addr).clone();
@@ -730,24 +719,15 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
730719
self.store.stack.values.truncate_keep_counts(self.cf.locals_base, params);
731720
}
732721

733-
let (locals_base, _stack_base, stack_offset) = match self.store.stack.values.enter_locals(params, locals) {
734-
Ok(v) => v,
735-
Err(Error::Trap(Trap::ValueStackOverflow)) if !IS_RETURN_CALL => {
736-
return Err(Trap::CallStackOverflow.into());
737-
}
738-
Err(err) => return Err(err),
739-
};
740-
741-
let new_call_frame = CallFrame::new(self.cf.func_addr, self.cf.module_addr, locals_base, stack_offset);
722+
let res = self.store.stack.values.enter_locals(&params, &locals);
723+
let locals_base = res.map_err(|err| if IS_RETURN_CALL { err } else { Error::Trap(Trap::CallStackOverflow) })?;
724+
let new_call_frame = CallFrame::new(self.cf.func_addr, self.cf.module_addr, locals_base, locals);
742725

743-
if IS_RETURN_CALL {
744-
self.cf = new_call_frame;
745-
} else {
726+
if !IS_RETURN_CALL {
746727
self.cf.incr_instr_ptr();
747728
self.store.stack.call_stack.push(self.cf)?;
748-
self.cf = new_call_frame;
749729
}
750-
730+
self.cf = new_call_frame;
751731
Ok(())
752732
}
753733

@@ -987,9 +967,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
987967
false => self.store.stack.values.pop::<i32>() as u32 as u64,
988968
};
989969

990-
if let Err(e) = mem.store((offset + addr) as usize, val.len(), &val) {
991-
return Err(e);
992-
}
970+
mem.store((offset + addr) as usize, val.len(), &val)?;
993971

994972
Ok(())
995973
}
@@ -1009,9 +987,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
1009987
false => u64::from(self.store.stack.values.pop::<i32>() as u32),
1010988
};
1011989

1012-
if let Err(e) = mem.store((offset + addr) as usize, val.len(), &val) {
1013-
return Err(e);
1014-
}
990+
mem.store((offset + addr) as usize, val.len(), &val)?;
1015991

1016992
Ok(())
1017993
}

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

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloc::boxed::Box;
22
use alloc::vec::Vec;
3-
use tinywasm_types::{ExternRef, FuncRef, LocalAddr, ValType, ValueCounts, ValueCountsSmall, WasmValue};
3+
use tinywasm_types::{ExternRef, FuncRef, LocalAddr, ValType, ValueCountsSmall, WasmValue};
44

55
use crate::{Result, Trap, engine::Config, interpreter::*, unlikely};
66

@@ -84,20 +84,22 @@ impl<T: Copy + Default> Stack<T> {
8484

8585
pub(crate) fn truncate_keep(&mut self, n: usize, end_keep: usize) {
8686
debug_assert!(n <= self.len);
87-
if n >= self.len {
87+
let len = self.len;
88+
if n >= len {
8889
return;
8990
}
9091

91-
let keep = (self.len - n).min(end_keep);
92-
if keep != 0 {
93-
let src = self.len - keep;
94-
self.data.copy_within(src..self.len, n);
92+
if end_keep == 0 {
93+
self.len = n;
94+
return;
9595
}
9696

97+
let keep = (len - n).min(end_keep);
98+
self.data.copy_within((len - keep)..len, n);
9799
self.len = n + keep;
98100
}
99101

100-
pub(crate) fn enter_locals(&mut self, param_count: usize, local_count: usize) -> Result<(u32, u32)> {
102+
pub(crate) fn enter_locals(&mut self, param_count: usize, local_count: usize) -> Result<u32> {
101103
debug_assert!(param_count <= local_count);
102104
let start = self.len - param_count;
103105
let end = start + local_count;
@@ -107,10 +109,11 @@ impl<T: Copy + Default> Stack<T> {
107109
}
108110

109111
let init_start = start + param_count;
110-
self.data[init_start..end].fill(T::default());
112+
if init_start != end {
113+
self.data[init_start..end].fill(T::default());
114+
}
111115
self.len = end;
112-
113-
Ok((start as u32, end as u32))
116+
Ok(start as u32)
114117
}
115118

116119
pub(crate) fn select_many(&mut self, count: usize, condition: bool) {
@@ -246,33 +249,40 @@ impl ValueStack {
246249
val_types.into_iter().map(|val_type| self.pop_wasmvalue(*val_type))
247250
}
248251

249-
pub(crate) fn enter_locals(
250-
&mut self,
251-
params: ValueCountsSmall,
252-
locals: ValueCounts,
253-
) -> Result<(StackBase, StackBase, ValueCountsSmall)> {
254-
let stack_offset = ValueCountsSmall {
255-
c32: u16::try_from(locals.c32).unwrap_or_else(|_| unreachable!("local count exceeds u16")),
256-
c64: u16::try_from(locals.c64).unwrap_or_else(|_| unreachable!("local count exceeds u16")),
257-
c128: u16::try_from(locals.c128).unwrap_or_else(|_| unreachable!("local count exceeds u16")),
258-
cref: u16::try_from(locals.cref).unwrap_or_else(|_| unreachable!("local count exceeds u16")),
252+
pub(crate) fn enter_locals(&mut self, params: &ValueCountsSmall, locals: &ValueCountsSmall) -> Result<StackBase> {
253+
let locals_base32 = if params.c32 == 0 && locals.c32 == 0 {
254+
self.stack_32.len as u32
255+
} else {
256+
self.stack_32.enter_locals(params.c32 as usize, locals.c32 as usize)?
257+
};
258+
let locals_base64 = if params.c64 == 0 && locals.c64 == 0 {
259+
self.stack_64.len as u32
260+
} else {
261+
self.stack_64.enter_locals(params.c64 as usize, locals.c64 as usize)?
262+
};
263+
let locals_base128 = if params.c128 == 0 && locals.c128 == 0 {
264+
self.stack_128.len as u32
265+
} else {
266+
self.stack_128.enter_locals(params.c128 as usize, locals.c128 as usize)?
267+
};
268+
let locals_baseref = if params.cref == 0 && locals.cref == 0 {
269+
self.stack_ref.len as u32
270+
} else {
271+
self.stack_ref.enter_locals(params.cref as usize, locals.cref as usize)?
259272
};
260273

261-
let (locals_base32, stack_base32) = self.stack_32.enter_locals(params.c32 as usize, locals.c32 as usize)?;
262-
let (locals_base64, stack_base64) = self.stack_64.enter_locals(params.c64 as usize, locals.c64 as usize)?;
263-
let (locals_base128, stack_base128) =
264-
self.stack_128.enter_locals(params.c128 as usize, locals.c128 as usize)?;
265-
let (locals_baseref, stack_baseref) =
266-
self.stack_ref.enter_locals(params.cref as usize, locals.cref as usize)?;
267-
268-
Ok((
269-
StackBase { s32: locals_base32, s64: locals_base64, s128: locals_base128, sref: locals_baseref },
270-
StackBase { s32: stack_base32, s64: stack_base64, s128: stack_base128, sref: stack_baseref },
271-
stack_offset,
272-
))
274+
Ok(StackBase { s32: locals_base32, s64: locals_base64, s128: locals_base128, sref: locals_baseref })
273275
}
274276

275277
pub(crate) fn truncate_keep_counts(&mut self, base: StackBase, keep: ValueCountsSmall) {
278+
if keep.c32 == 0 && keep.c64 == 0 && keep.c128 == 0 && keep.cref == 0 {
279+
self.stack_32.len = base.s32 as usize;
280+
self.stack_64.len = base.s64 as usize;
281+
self.stack_128.len = base.s128 as usize;
282+
self.stack_ref.len = base.sref as usize;
283+
return;
284+
}
285+
276286
self.stack_32.truncate_keep(base.s32 as usize, keep.c32 as usize);
277287
self.stack_64.truncate_keep(base.s64 as usize, keep.c64 as usize);
278288
self.stack_128.truncate_keep(base.s128 as usize, keep.c128 as usize);

crates/types/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl<'a, T: IntoIterator<Item = &'a ValType>> From<T> for ValueCountsSmall {
262262
pub struct WasmFunction {
263263
pub instructions: ArcSlice<Instruction>,
264264
pub data: WasmFunctionData,
265-
pub locals: ValueCounts,
265+
pub locals: ValueCountsSmall,
266266
pub params: ValueCountsSmall,
267267
pub ty: FuncType,
268268
}

0 commit comments

Comments
 (0)