Skip to content

Commit

Permalink
wip sidetable stuff in validation/code.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanja Zaeske committed Aug 20, 2024
1 parent dee74c1 commit 829cc44
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/core/sidetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ pub type Sidetable = Vec<SidetableEntry>;
/// - else
pub struct SidetableEntry {
/// Δpc: the amount to adjust the instruction pointer by if the branch is taken
delta_pc: isize,
pub delta_pc: isize,

/// Δstp: the amount to adjust the side-table index by if the branch is taken
delta_stp: isize,
pub delta_stp: isize,

/// valcnt: the number of values that will be copied if the branch is taken
///
/// Branches may additionally consume operands themselves, which they push back on the operand
/// stack after unwinding.
valcnt: usize,
pub valcnt: usize,

/// popcnt: the number of values that will be popped if the branch is taken
///
/// Taking a branch unwinds the operand stack down to the height where the targeted structured
/// control instruction was entered.
popcnt: usize,
pub popcnt: usize,
}
41 changes: 38 additions & 3 deletions src/validation/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::core::reader::types::global::Global;
use crate::core::reader::types::memarg::MemArg;
use crate::core::reader::types::{FuncType, NumType, ResultType, ValType};
use crate::core::reader::{WasmReadable, WasmReader};
use crate::core::sidetable::Sidetable;
use crate::core::sidetable::{self, Sidetable, SidetableEntry};
use crate::{Error, Result};

pub fn validate_code_section(
Expand Down Expand Up @@ -100,7 +100,8 @@ fn read_instructions(
// block: [] -> [t*2]
BLOCK | LOOP | IF => {
let block_ty = if wasm.peek_u8()? as i8 == 0x40 {
/* empty block type */
// empty block type
debug_assert_eq!(wasm.read_u8()?, 0x40);
FuncType {
params: ResultType {
valtypes: Vec::new(),
Expand All @@ -109,7 +110,8 @@ fn read_instructions(
valtypes: Vec::new(),
},
}
} else if let Ok(val_ty) = ValType::read(wasm) {
} else if let Ok(val_ty) = wasm.handle_transaction(ValType::read) {
// consumes no parameters, return type is a single primitive value
FuncType {
params: ResultType {
valtypes: Vec::new(),
Expand All @@ -119,6 +121,7 @@ fn read_instructions(
},
}
} else {
// type matches one of the types from the FuncType table
let maybe_ty_idx: usize = wasm
.read_var_i64()?
.try_into()
Expand All @@ -129,11 +132,43 @@ fn read_instructions(
.ok_or_else(|| Error::InvalidFuncTypeIdx)?
.clone()
};

// check the tail of the stack matches the parameter types of the block
for (block_param_ty, value_ty) in block_ty
.params
.valtypes
.iter()
.rev()
.zip(value_stack.iter().rev())
{
if block_param_ty != value_ty {
return Err(Error::InvalidValueStackType(Some(*value_ty)));
}
}

// TODO insert marker in stack, that this is where the new blocks region on the stack starts
}
ELSE => {
// TODO sidetable entry!!!
// TODO check how many values to pop from the value stack until the innermost sourrounding IF

sidetable.push(SidetableEntry {
delta_pc: todo!("is this the first byte after the end of the block? */,"),
delta_stp: todo!(),
valcnt: todo!("the block_ty.params.valtypes.len() of this block ty"),
popcnt: 0,
})
}
// end
END => {
return Ok(());
}
BR_IF => {
// TODO sidetable entry!!!
}
BR_TABLE => {
// TODO sidetable entry!!!
}
// local.get: [] -> [t]
LOCAL_GET => {
let local_idx = wasm.read_var_u32()? as LocalIdx;
Expand Down

0 comments on commit 829cc44

Please sign in to comment.