Skip to content

Commit

Permalink
feat: control flow improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
  • Loading branch information
explodingcamera committed Dec 7, 2023
1 parent b0292c7 commit c9a757f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
9 changes: 7 additions & 2 deletions crates/tinywasm/src/runtime/executer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ impl<const CHECK_TYPES: bool> Runtime<CHECK_TYPES> {
let val = stack.values.pop().ok_or(Error::StackUnderflow)?;
cf.set_local(*local_index as usize, val);
}
I32Const(val) => {
stack.values.push((*val).into());
LocalTee(local_index) => {
debug!("local.tee: {:?}", local_index);
let val = stack.values.pop().ok_or(Error::StackUnderflow)?;
cf.set_local(*local_index as usize, val);
stack.values.push(val);
}
I32Const(val) => stack.values.push((*val).into()),
I64Const(val) => stack.values.push((*val).into()),
I64Add => add_instr!(i64, stack),
I32Add => add_instr!(i32, stack),
F32Add => add_instr!(f32, stack),
Expand Down
2 changes: 1 addition & 1 deletion crates/tinywasm/src/runtime/stack/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Blocks {

#[derive(Debug)]
pub(crate) struct BlockFrame {
// position of the instruction pointer when the block was entered
// where to resume execution when the block is broken
pub instr_ptr: usize,
// position of the stack pointer when the block was entered
pub stack_ptr: usize,
Expand Down
15 changes: 11 additions & 4 deletions crates/tinywasm/src/runtime/stack/call_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{runtime::RawWasmValue, Error, Result};
use alloc::{boxed::Box, vec::Vec};
use tinywasm_types::{ValType, WasmValue};

use super::blocks::Blocks;
use super::{blocks::Blocks, BlockFrameType};

// minimum call stack size
pub const CALL_STACK_SIZE: usize = 1024;
Expand Down Expand Up @@ -70,9 +70,16 @@ impl CallFrame {
self.instr_ptr = block.instr_ptr;
value_stack.trim(block.stack_ptr);

// -2 because the block we're breaking to is still on the stack
// TODO: this might be wrong
self.blocks.trim(block_index as usize - 2);
// Adjusting how to trim the blocks stack based on the block type
let trim_index = match block.ty {
// if we are breaking to a loop, we want to jump back to the start of the loop
BlockFrameType::Loop => block_index as usize - 2,
// if we are breaking to any other block, we want to jump to the end of the block
// TODO: check if this is correct
BlockFrameType::If | BlockFrameType::Else | BlockFrameType::Block => block_index as usize - 1,
};

self.blocks.trim(trim_index);
Ok(())
}

Expand Down

0 comments on commit c9a757f

Please sign in to comment.