Skip to content

Commit

Permalink
feat: add support for more instructions
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 10, 2023
1 parent 33ad024 commit 5a193cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
38 changes: 28 additions & 10 deletions crates/tinywasm/src/runtime/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ impl DefaultRuntime {
ExecResult::Ok => {
cf.instr_ptr += 1;
}

// trap the program
ExecResult::Trap(trap) => {
cf.instr_ptr += 1;
// push the call frame back onto the stack so that it can be resumed
// if the trap can be handled
stack.call_stack.push(cf);
return Err(Error::Trap(trap));
}
}
}

Expand All @@ -50,6 +59,7 @@ enum ExecResult {
Ok,
Return,
Call,
Trap(crate::Trap),
}

#[inline]
Expand All @@ -63,9 +73,21 @@ fn exec_one(
) -> Result<ExecResult> {
use tinywasm_types::Instruction::*;
match instr {
Nop => {} // do nothing
Unreachable => return Err(Error::Trap(crate::Trap::Unreachable)),

Nop => { /* do nothing */ }
Unreachable => return Ok(ExecResult::Trap(crate::Trap::Unreachable)), // we don't need to include the call frame here because it's already on the stack
Drop => {
stack.values.pop().ok_or(Error::StackUnderflow)?;
}
Select => {
let cond: i32 = stack.values.pop().ok_or(Error::StackUnderflow)?.into();
let val2 = stack.values.pop().ok_or(Error::StackUnderflow)?;

// if cond != 0, we already have the right value on the stack
if cond == 0 {
let _ = stack.values.pop().ok_or(Error::StackUnderflow)?;
stack.values.push(val2);
}
}
Return => {
debug!("return");
}
Expand Down Expand Up @@ -117,7 +139,6 @@ fn exec_one(

todo!()
}

Br(v) => cf.break_to(*v, &mut stack.values)?,
BrIf(v) => {
let val: i32 = stack.values.pop().ok_or(Error::StackUnderflow)?.into();
Expand Down Expand Up @@ -164,19 +185,16 @@ fn exec_one(
LocalGet(local_index) => {
debug!("local.get: {:?}", local_index);
let val = cf.get_local(*local_index as usize);
debug!("local: {:#?}", val);
stack.values.push(val);
}
LocalSet(local_index) => {
debug!("local.set: {:?}", local_index);
let val = stack.values.pop().ok_or(Error::StackUnderflow)?;
cf.set_local(*local_index as usize, val);
}
// Equivalent to local.set, local.get
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);
let val = stack.values.last().ok_or(Error::StackUnderflow)?;
cf.set_local(*local_index as usize, *val);
}
I32Const(val) => stack.values.push((*val).into()),
I64Const(val) => stack.values.push((*val).into()),
Expand Down
5 changes: 5 additions & 0 deletions crates/tinywasm/src/runtime/stack/value_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ impl ValueStack {
self.stack.push(value);
}

#[inline]
pub(crate) fn last(&self) -> Option<&RawWasmValue> {
self.stack.last()
}

#[inline]
pub(crate) fn pop(&mut self) -> Option<RawWasmValue> {
self.top -= 1;
Expand Down

0 comments on commit 5a193cf

Please sign in to comment.