Skip to content

Commit 1dc86cd

Browse files
chore: simplify LinearMemory trait
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 8bc916e commit 1dc86cd

7 files changed

Lines changed: 258 additions & 507 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- Removed the parser's `optimize_remove_nop` option and accessors.
1919
- Changed public `Instruction` variants and the `.twasm` format. Existing archives must be regenerated.
2020
- `TableType` limits now use `u64`. Use `TableType::new` or `TableType::new64` instead of struct literals.
21+
- `LinearMemory` trait now uses a single `usize` address for all memory operations
2122

2223
## [0.9.1] - 2026-06-29
2324

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

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use alloc::rc::Rc;
99
use alloc::vec::Vec;
1010

1111
use alloc::sync::Arc;
12-
use interpreter::stack::CallFrame;
12+
use interpreter::stack::{CallFrame, ValueStack};
1313
use tinywasm_types::*;
1414

1515
use super::ExecState;
@@ -31,6 +31,19 @@ pub(crate) struct Executor<'store, const BUDGETED: bool> {
3131
}
3232

3333
impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
34+
#[inline(always)]
35+
fn pop_memory_addr<const N: usize>(
36+
stack: &mut ValueStack,
37+
mem: &MemoryInstance,
38+
offset: u64,
39+
) -> Result<usize, Trap> {
40+
if mem.is_64bit() {
41+
mem.effective_addr_64::<N>(<i64>::stack_pop(stack) as u64, offset)
42+
} else {
43+
mem.effective_addr_32::<N>(<i32>::stack_pop(stack) as u32, offset)
44+
}
45+
}
46+
3447
pub(crate) fn new(store: &'store mut Store, cf: CallFrame, call_stack_base: u32) -> Self {
3548
let wasm_func = store.state.get_wasm_func(cf.func_addr);
3649
let module = store.get_module_instance_internal(wasm_func.owner);
@@ -240,18 +253,10 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
240253
}};
241254
}
242255

243-
let Some(next) = self.func.instructions.get(self.cf.instr_ptr) else {
244-
unreachable!(
245-
"Instruction pointer out of bounds: {} ({} instructions)",
246-
self.cf.instr_ptr,
247-
self.func.instructions.len()
248-
)
249-
};
250-
251256
use tinywasm_types::Instruction::*;
252257
#[rustfmt::skip]
253-
match next {
254-
Unreachable => return Err(Trap::Unreachable),
258+
match &self.func.instructions[self.cf.instr_ptr] {
259+
Unreachable => { cold_path(); return Err(Trap::Unreachable) },
255260
Drop32 => { _ = Value32::stack_pop(&mut self.store.value_stack)},
256261
Drop64 => { _ = Value64::stack_pop(&mut self.store.value_stack)},
257262
Drop128 => { _ = Value128::stack_pop(&mut self.store.value_stack)},
@@ -755,10 +760,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
755760
I64x2ExtendHighI32x4S => stack_op!(unary Value128, |a| a.i64x2_extend_high_i32x4_s()),
756761
I64x2ExtendHighI32x4U => stack_op!(unary Value128, |a| a.i64x2_extend_high_i32x4_u()),
757762
I8x16Popcnt => stack_op!(unary Value128, |v| v.i8x16_popcnt()),
758-
I8x16Shuffle(idx) => {
759-
let mask = self.func.data.v128_const(*idx);
760-
stack_op!(binary Value128, |a, b| Value128::i8x16_shuffle(a, b, Value128(mask)))
761-
},
763+
I8x16Shuffle(idx) => stack_op!(binary Value128, |a, b| Value128::i8x16_shuffle(a, b, Value128(self.func.data.v128_const(*idx)))),
762764
I16x8Q15MulrSatS => stack_op!(binary Value128, |a, b| a.i16x8_q15mulr_sat_s(b)),
763765
I32x4DotI16x8S => stack_op!(binary Value128, |a, b| a.i32x4_dot_i16x8_s(b)),
764766
I8x16RelaxedLaneselect => stack_op!(ternary Value128, |a, b, c| Value128::i8x16_relaxed_laneselect(a, b, c)),
@@ -822,7 +824,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
822824
I32x4RelaxedTruncF64x2UZero => stack_op!(unary Value128, |v| v.i32x4_relaxed_trunc_f64x2_u_zero()),
823825
};
824826

825-
self.cf.incr_instr_ptr();
827+
self.cf.instr_ptr += 1;
826828

827829
Ok(None)
828830
}
@@ -985,7 +987,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
985987
};
986988

987989
self.store.value_stack.extend_from_wasmvalues(&res)?;
988-
self.cf.incr_instr_ptr();
990+
self.cf.instr_ptr += 1;
989991
Ok(())
990992
}
991993

@@ -1140,10 +1142,11 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
11401142
addr_local: u8,
11411143
value_local: u8,
11421144
) -> Result<(), Trap> {
1143-
let addr = u64::from(u32::local_get(&self.store.value_stack, &self.cf, u16::from(addr_local)));
1144-
let value = T::local_get(&self.store.value_stack, &self.cf, u16::from(value_local)).to_mem_bytes();
1145+
let base = u32::local_get(&self.store.value_stack, &self.cf, u16::from(addr_local));
1146+
let value = T::local_get(&self.store.value_stack, &self.cf, u16::from(value_local));
11451147
let mem = self.store.state.get_mem_mut(self.module.resolve_mem_addr(memarg.mem_addr()));
1146-
mem.store(addr, memarg.offset(), value)?;
1148+
let addr = mem.effective_addr_32::<N>(base, memarg.offset())?;
1149+
value.store_at(&mut *mem.inner, addr)?;
11471150
Ok(())
11481151
}
11491152

@@ -1161,7 +1164,8 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
11611164
let addr = i32::stack_pop(&mut self.store.value_stack);
11621165
let fma = acc + lhs * rhs;
11631166
let mem = self.store.state.get_mem_mut(self.module.resolve_mem_addr(m.mem_addr()));
1164-
mem.store(addr as u32 as u64, m.offset(), fma.to_mem_bytes())?;
1167+
let addr = mem.effective_addr_32::<N>(addr as u32, m.offset())?;
1168+
fma.store_at(&mut *mem.inner, addr)?;
11651169
Ok(())
11661170
}
11671171

@@ -1183,9 +1187,15 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
11831187
addr_local: u8,
11841188
) -> Result<T, Trap> {
11851189
let mem = self.store.state.get_mem(self.module.resolve_mem_addr(memarg.mem_addr()));
1186-
let addr = u64::from(u32::local_get(&self.store.value_stack, &self.cf, u16::from(addr_local)));
1187-
let bytes = mem.load(addr, memarg.offset())?;
1188-
Ok(T::from_mem_bytes(bytes))
1190+
let base = u32::local_get(&self.store.value_stack, &self.cf, u16::from(addr_local));
1191+
let addr = mem.effective_addr_32::<N>(base, memarg.offset())?;
1192+
match T::load_at(&*mem.inner, addr) {
1193+
Ok(res) => Ok(res),
1194+
Err(err) => {
1195+
cold_path();
1196+
Err(err)
1197+
}
1198+
}
11891199
}
11901200

11911201
fn exec_load_local_tee<T: InternalValue + MemValue<N>, const N: usize>(
@@ -1375,20 +1385,11 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
13751385
lane: u8,
13761386
) -> Result<(), Trap> {
13771387
let mem = self.store.state.get_mem(self.module.resolve_mem_addr(mem_addr));
1378-
let base = match mem.is_64bit() {
1379-
true => <i64>::stack_pop(&mut self.store.value_stack) as u64,
1380-
false => <i32>::stack_pop(&mut self.store.value_stack) as u32 as u64,
1381-
};
1382-
let val = match mem.load::<LOAD_SIZE>(base, offset) {
1383-
Ok(val) => val,
1384-
Err(e) => {
1385-
cold_path();
1386-
return Err(e);
1387-
}
1388-
};
1388+
let addr = Self::pop_memory_addr::<LOAD_SIZE>(&mut self.store.value_stack, mem, offset)?;
1389+
let val = LOAD::load_at(&*mem.inner, addr)?;
13891390
let offset = lane as usize * LOAD_SIZE;
13901391
let mut imm = <Value128>::stack_pop(&mut self.store.value_stack).to_mem_bytes();
1391-
imm[offset..offset + LOAD_SIZE].copy_from_slice(&val);
1392+
imm[offset..offset + LOAD_SIZE].copy_from_slice(&val.to_mem_bytes());
13921393
self.store.value_stack.push(Value128(imm))?;
13931394
Ok(())
13941395
}
@@ -1401,12 +1402,9 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
14011402
cast: impl Fn(LOAD) -> TARGET,
14021403
) -> Result<(), Trap> {
14031404
let mem = self.store.state.get_mem(self.module.resolve_mem_addr(mem_addr));
1404-
let base = match mem.is_64bit() {
1405-
true => <i64>::stack_pop(&mut self.store.value_stack) as u64,
1406-
false => <i32>::stack_pop(&mut self.store.value_stack) as u32 as u64,
1407-
};
1405+
let addr = Self::pop_memory_addr::<LOAD_SIZE>(&mut self.store.value_stack, mem, offset)?;
14081406

1409-
match LOAD::load(&*mem.inner, base, offset) {
1407+
match LOAD::load_at(&*mem.inner, addr) {
14101408
Ok(val) => {
14111409
self.store.value_stack.push(cast(val))?;
14121410
Ok(())
@@ -1426,15 +1424,13 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
14261424
) -> Result<(), Trap> {
14271425
let bytes = <Value128>::stack_pop(&mut self.store.value_stack).to_mem_bytes();
14281426
let lane_offset = lane as usize * N;
1429-
let mut val = [0u8; N];
1430-
val.copy_from_slice(&bytes[lane_offset..lane_offset + N]);
1427+
let mut val_bytes = [0u8; N];
1428+
val_bytes.copy_from_slice(&bytes[lane_offset..lane_offset + N]);
1429+
let val = U::from_mem_bytes(val_bytes);
14311430
let mem_addr = self.module.resolve_mem_addr(mem_addr);
14321431
let mem = self.store.state.get_mem_mut(mem_addr);
1433-
let addr = match mem.is_64bit() {
1434-
true => <i64>::stack_pop(&mut self.store.value_stack) as u64,
1435-
false => <i32>::stack_pop(&mut self.store.value_stack) as u32 as u64,
1436-
};
1437-
match mem.store(addr, offset, val) {
1432+
let addr = Self::pop_memory_addr::<N>(&mut self.store.value_stack, mem, offset)?;
1433+
match val.store_at(&mut *mem.inner, addr) {
14381434
Ok(()) => Ok(()),
14391435
Err(e) => {
14401436
cold_path();
@@ -1450,15 +1446,12 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
14501446
cast: impl Fn(T) -> U,
14511447
) -> Result<(), Trap> {
14521448
let val = <T>::stack_pop(&mut self.store.value_stack);
1453-
let val = cast(val).to_mem_bytes();
1449+
let val = cast(val);
14541450

14551451
let mem_addr = self.module.resolve_mem_addr(mem_addr);
14561452
let mem = self.store.state.get_mem_mut(mem_addr);
1457-
let addr = match mem.is_64bit() {
1458-
true => <i64>::stack_pop(&mut self.store.value_stack) as u64,
1459-
false => <i32>::stack_pop(&mut self.store.value_stack) as u32 as u64,
1460-
};
1461-
match mem.store(addr, offset, val) {
1453+
let addr = Self::pop_memory_addr::<N>(&mut self.store.value_stack, mem, offset)?;
1454+
match val.store_at(&mut *mem.inner, addr) {
14621455
Ok(()) => Ok(()),
14631456
Err(e) => {
14641457
cold_path();

‎crates/tinywasm/src/store/memory/instance.rs‎

Lines changed: 38 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,53 @@ use core::hint::cold_path;
99
/// A WebAssembly Memory Instance
1010
///
1111
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#memory-instances>
12-
#[cfg_attr(feature = "debug", derive(Debug))]
1312
pub(crate) struct MemoryInstance {
1413
pub(crate) kind: MemoryType,
1514
pub(crate) inner: MemoryStorage,
1615
pub(crate) page_count: usize,
1716
}
1817

18+
#[cfg(feature = "debug")]
19+
impl core::fmt::Debug for MemoryInstance {
20+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21+
f.debug_struct("MemoryInstance").field("kind", &self.kind).field("page_count", &self.page_count).finish()
22+
}
23+
}
24+
1925
impl MemoryInstance {
2026
const COPY_CHUNK_SIZE: usize = 4 * 1024;
2127

28+
#[inline(always)]
29+
pub(crate) fn effective_addr_32<const N: usize>(&self, base: u32, offset: u64) -> Result<usize, Trap> {
30+
#[cfg(target_pointer_width = "64")]
31+
{
32+
debug_assert!(u32::try_from(offset).is_ok(), "validated memory32 offsets fit in u32");
33+
Ok(base as usize + offset as usize)
34+
}
35+
36+
#[cfg(not(target_pointer_width = "64"))]
37+
{
38+
match usize::try_from(u64::from(base) + offset) {
39+
Ok(addr) => Ok(addr),
40+
Err(_) => {
41+
cold_path();
42+
Err(memory_oob(base as usize, N, self.inner.len()))
43+
}
44+
}
45+
}
46+
}
47+
48+
#[inline(always)]
49+
pub(crate) fn effective_addr_64<const N: usize>(&self, base: u64, offset: u64) -> Result<usize, Trap> {
50+
match base.checked_add(offset).and_then(|addr| usize::try_from(addr).ok()) {
51+
Some(addr) => Ok(addr),
52+
None => {
53+
cold_path();
54+
Err(memory_oob(base as usize, N, self.inner.len()))
55+
}
56+
}
57+
}
58+
2259
pub(crate) fn new(kind: MemoryType, backend: &MemoryBackend) -> Result<Self> {
2360
assert!(kind.page_count_initial() <= kind.page_count_max());
2461

@@ -62,132 +99,6 @@ impl MemoryInstance {
6299
matches!(self.kind.arch(), MemoryArch::I64)
63100
}
64101

65-
#[inline(always)]
66-
pub(crate) fn load<const SIZE: usize>(&self, base: u64, offset: u64) -> Result<[u8; SIZE], Trap> {
67-
// the compiler doesn't optimize .as_slice().try_into() for some reason, so we have to manually copy the bytes into an array
68-
// heavy usage of cold_path() seems to help a lot from looking at profile data
69-
match SIZE {
70-
1 => {
71-
let res = match self.inner.read_8(base, offset) {
72-
Ok(bytes) => bytes,
73-
Err(e) => {
74-
cold_path();
75-
return Err(e);
76-
}
77-
};
78-
let mut bytes = [0; SIZE];
79-
bytes[0] = res;
80-
Ok(bytes)
81-
}
82-
2 => {
83-
let res = match self.inner.read_16(base, offset) {
84-
Ok(bytes) => bytes,
85-
Err(e) => {
86-
cold_path();
87-
return Err(e);
88-
}
89-
};
90-
let mut bytes = [0; SIZE];
91-
bytes[0] = res[0];
92-
bytes[1] = res[1];
93-
Ok(bytes)
94-
}
95-
4 => {
96-
let mut bytes = [0; SIZE];
97-
let res = match self.inner.read_32(base, offset) {
98-
Ok(bytes) => bytes,
99-
Err(e) => {
100-
cold_path();
101-
return Err(e);
102-
}
103-
};
104-
bytes[0] = res[0];
105-
bytes[1] = res[1];
106-
bytes[2] = res[2];
107-
bytes[3] = res[3];
108-
Ok(bytes)
109-
}
110-
8 => {
111-
let mut bytes = [0; SIZE];
112-
let res = match self.inner.read_64(base, offset) {
113-
Ok(bytes) => bytes,
114-
Err(e) => {
115-
cold_path();
116-
return Err(e);
117-
}
118-
};
119-
bytes[0] = res[0];
120-
bytes[1] = res[1];
121-
bytes[2] = res[2];
122-
bytes[3] = res[3];
123-
bytes[4] = res[4];
124-
bytes[5] = res[5];
125-
bytes[6] = res[6];
126-
bytes[7] = res[7];
127-
Ok(bytes)
128-
}
129-
16 => {
130-
let mut bytes = [0; SIZE];
131-
let res = match self.inner.read_128(base, offset) {
132-
Ok(bytes) => bytes,
133-
Err(e) => {
134-
cold_path();
135-
return Err(e);
136-
}
137-
};
138-
bytes[0] = res[0];
139-
bytes[1] = res[1];
140-
bytes[2] = res[2];
141-
bytes[3] = res[3];
142-
bytes[4] = res[4];
143-
bytes[5] = res[5];
144-
bytes[6] = res[6];
145-
bytes[7] = res[7];
146-
bytes[8] = res[8];
147-
bytes[9] = res[9];
148-
bytes[10] = res[10];
149-
bytes[11] = res[11];
150-
bytes[12] = res[12];
151-
bytes[13] = res[13];
152-
bytes[14] = res[14];
153-
bytes[15] = res[15];
154-
Ok(bytes)
155-
}
156-
_ => unreachable!("unsupported fixed-size read width {SIZE}"),
157-
}
158-
}
159-
160-
#[inline(always)]
161-
pub(crate) fn store<const SIZE: usize>(&mut self, base: u64, offset: u64, bytes: [u8; SIZE]) -> Result<(), Trap> {
162-
// the compiler doesn't optimize .as_slice().try_into() for some reason, so we have to manually copy the bytes into an array
163-
// heavy usage of cold_path() seems to help a lot from looking at profile data
164-
let res = match SIZE {
165-
1 => self.inner.write_8(base, offset, bytes[0]),
166-
2 => self.inner.write_16(base, offset, [bytes[0], bytes[1]]),
167-
4 => self.inner.write_32(base, offset, [bytes[0], bytes[1], bytes[2], bytes[3]]),
168-
8 => self.inner.write_64(
169-
base,
170-
offset,
171-
[bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]],
172-
),
173-
16 => self.inner.write_128(
174-
base,
175-
offset,
176-
[
177-
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8], bytes[9],
178-
bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15],
179-
],
180-
),
181-
_ => unreachable!("unsupported fixed-size write width {SIZE}"),
182-
};
183-
184-
if let Err(e) = res {
185-
cold_path();
186-
return Err(e);
187-
}
188-
Ok(())
189-
}
190-
191102
pub(crate) fn copy_from_memory(
192103
&mut self,
193104
dst: usize,

0 commit comments

Comments
 (0)