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

Commit 6c2c6b2

Browse files
chore: prepare resumable execution
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 9193672 commit 6c2c6b2

File tree

4 files changed

+495
-565
lines changed

4 files changed

+495
-565
lines changed

.vscode/settings.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
},
55
"rust-analyzer.linkedProjects": [
66
"./Cargo.toml",
7-
"./examples/rust/Cargo.toml"
87
]
98
}

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

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010
// This is a bit hard to see from the spec, but it's vaild to use breaks to return
1111
// from a function, so we need to check if the label stack is empty
1212
macro_rules! break_to {
13-
($cf:ident, $stack:ident, $module:ident, $store:ident, $break_to_relative:ident) => {{
14-
if $cf.break_to(*$break_to_relative, &mut $stack.values, &mut $stack.blocks).is_none() {
15-
if $stack.call_stack.is_empty() {
16-
return Ok(());
13+
($break_to_relative:expr, $self:expr) => {{
14+
if $self.cf.break_to($break_to_relative, &mut $self.stack.values, &mut $self.stack.blocks).is_none() {
15+
if $self.stack.call_stack.is_empty() {
16+
return Ok(ExecResult::Return);
1717
}
1818

19-
call!($cf, $stack, $module, $store)
19+
return $self.process_call();
2020
}
2121
}};
2222
}
2323

2424
/// Load a value from memory
2525
macro_rules! mem_load {
26-
($type:ty, $arg:expr, $stack:ident, $store:ident, $module:ident) => {{
27-
mem_load!($type, $type, $arg, $stack, $store, $module)
26+
($type:ty, $arg:expr, $self:expr) => {{
27+
mem_load!($type, $type, $arg, $self)
2828
}};
2929

30-
($load_type:ty, $target_type:ty, $arg:expr, $stack:ident, $store:ident, $module:ident) => {{
30+
($load_type:ty, $target_type:ty, $arg:expr, $self:expr) => {{
3131
#[inline(always)]
3232
fn mem_load_inner(
3333
store: &Store,
@@ -56,17 +56,17 @@ macro_rules! mem_load {
5656
}
5757

5858
let (mem_addr, offset) = $arg;
59-
mem_load_inner($store, &$module, $stack, *mem_addr, *offset)?;
59+
mem_load_inner($self.store, &$self.module, $self.stack, *mem_addr, *offset)?;
6060
}};
6161
}
6262

6363
/// Store a value to memory
6464
macro_rules! mem_store {
65-
($type:ty, $arg:expr, $stack:ident, $store:ident, $module:ident) => {{
66-
mem_store!($type, $type, $arg, $stack, $store, $module)
65+
($type:ty, $arg:expr, $self:expr) => {{
66+
mem_store!($type, $type, $arg, $self)
6767
}};
6868

69-
($store_type:ty, $target_type:ty, $arg:expr, $stack:ident, $store:ident, $module:ident) => {{
69+
($store_type:ty, $target_type:ty, $arg:expr, $self:expr) => {{
7070
#[inline(always)]
7171
fn mem_store_inner(
7272
store: &Store,
@@ -83,8 +83,7 @@ macro_rules! mem_store {
8383
Ok(())
8484
}
8585

86-
let (mem_addr, offset) = $arg;
87-
mem_store_inner($store, &$module, $stack, *mem_addr, *offset)?;
86+
mem_store_inner($self.store, &$self.module, $self.stack, *$arg.0, *$arg.1)?;
8887
}};
8988
}
9089

@@ -110,21 +109,21 @@ macro_rules! float_min_max {
110109

111110
/// Convert a value on the stack
112111
macro_rules! conv {
113-
($from:ty, $to:ty, $stack:ident) => {
114-
$stack.values.replace_top(|v| (<$from>::from(v) as $to).into())?
112+
($from:ty, $to:ty, $self:expr) => {
113+
$self.stack.values.replace_top(|v| (<$from>::from(v) as $to).into())?
115114
};
116115
}
117116

118117
/// Convert a value on the stack with error checking
119118
macro_rules! checked_conv_float {
120119
// Direct conversion with error checking (two types)
121-
($from:tt, $to:tt, $stack:ident) => {
122-
checked_conv_float!($from, $to, $to, $stack)
120+
($from:tt, $to:tt, $self:expr) => {
121+
checked_conv_float!($from, $to, $to, $self)
123122
};
124123
// Conversion with an intermediate unsigned type and error checking (three types)
125-
($from:tt, $intermediate:tt, $to:tt, $stack:ident) => {{
124+
($from:tt, $intermediate:tt, $to:tt, $self:expr) => {{
126125
let (min, max) = float_min_max!($from, $intermediate);
127-
let a: $from = $stack.values.pop()?.into();
126+
let a: $from = $self.stack.values.pop()?.into();
128127

129128
if unlikely(a.is_nan()) {
130129
return Err(Error::Trap(crate::Trap::InvalidConversionToInt));
@@ -134,59 +133,59 @@ macro_rules! checked_conv_float {
134133
return Err(Error::Trap(crate::Trap::IntegerOverflow));
135134
}
136135

137-
$stack.values.push((a as $intermediate as $to).into());
136+
$self.stack.values.push((a as $intermediate as $to).into());
138137
}};
139138
}
140139

141140
/// Compare two values on the stack
142141
macro_rules! comp {
143-
($op:tt, $to:ty, $stack:ident) => {
144-
$stack.values.calculate(|a, b| {
142+
($op:tt, $to:ty, $self:ident) => {
143+
$self.stack.values.calculate(|a, b| {
145144
((<$to>::from(a) $op <$to>::from(b)) as i32).into()
146145
})?
147146
};
148147
}
149148

150149
/// Compare a value on the stack to zero
151150
macro_rules! comp_zero {
152-
($op:tt, $ty:ty, $stack:ident) => {
153-
$stack.values.replace_top(|v| {
151+
($op:tt, $ty:ty, $self:expr) => {
152+
$self.stack.values.replace_top(|v| {
154153
((<$ty>::from(v) $op 0) as i32).into()
155154
})?
156155
};
157156
}
158157

159158
/// Apply an arithmetic method to two values on the stack
160159
macro_rules! arithmetic {
161-
($op:ident, $to:ty, $stack:ident) => {
162-
$stack.values.calculate(|a, b| {
160+
($op:ident, $to:ty, $self:expr) => {
161+
$self.stack.values.calculate(|a, b| {
163162
(<$to>::from(a).$op(<$to>::from(b)) as $to).into()
164163
})?
165164
};
166165

167166
// also allow operators such as +, -
168-
($op:tt, $ty:ty, $stack:ident) => {
169-
$stack.values.calculate(|a, b| {
167+
($op:tt, $ty:ty, $self:expr) => {
168+
$self.stack.values.calculate(|a, b| {
170169
((<$ty>::from(a) $op <$ty>::from(b)) as $ty).into()
171170
})?
172171
};
173172
}
174173

175174
/// Apply an arithmetic method to a single value on the stack
176175
macro_rules! arithmetic_single {
177-
($op:ident, $ty:ty, $stack:ident) => {
178-
arithmetic_single!($op, $ty, $ty, $stack)
176+
($op:ident, $ty:ty, $self:expr) => {
177+
arithmetic_single!($op, $ty, $ty, $self)
179178
};
180179

181-
($op:ident, $from:ty, $to:ty, $stack:ident) => {
182-
$stack.values.replace_top(|v| (<$from>::from(v).$op() as $to).into())?
180+
($op:ident, $from:ty, $to:ty, $self:expr) => {
181+
$self.stack.values.replace_top(|v| (<$from>::from(v).$op() as $to).into())?
183182
};
184183
}
185184

186185
/// Apply an arithmetic operation to two values on the stack with error checking
187186
macro_rules! checked_int_arithmetic {
188-
($op:ident, $to:ty, $stack:ident) => {
189-
$stack.values.calculate_trap(|a, b| {
187+
($op:ident, $to:ty, $self:expr) => {
188+
$self.stack.values.calculate_trap(|a, b| {
190189
let a: $to = a.into();
191190
let b: $to = b.into();
192191

@@ -200,27 +199,10 @@ macro_rules! checked_int_arithmetic {
200199
};
201200
}
202201

203-
macro_rules! call {
204-
($cf:expr, $stack:expr, $module:expr, $store:expr) => {{
205-
let old = $cf.block_ptr;
206-
$cf = $stack.call_stack.pop()?;
207-
208-
if old > $cf.block_ptr {
209-
$stack.blocks.truncate(old);
210-
}
211-
212-
if $cf.module_addr != $module.id() {
213-
$module.swap_with($cf.module_addr, $store);
214-
}
215-
216-
continue;
217-
}};
218-
}
219-
220202
macro_rules! skip {
221203
($code:expr) => {
222204
match $code {
223-
Ok(_) => continue,
205+
Ok(_) => return Ok(ExecResult::Continue),
224206
Err(e) => return Err(e),
225207
}
226208
};
@@ -229,7 +211,6 @@ macro_rules! skip {
229211
pub(super) use arithmetic;
230212
pub(super) use arithmetic_single;
231213
pub(super) use break_to;
232-
pub(super) use call;
233214
pub(super) use checked_conv_float;
234215
pub(super) use checked_int_arithmetic;
235216
pub(super) use comp;

0 commit comments

Comments
 (0)