Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasmi_v1: improve instruction scheduler #376

Merged
merged 11 commits into from
Jul 13, 2022
6 changes: 1 addition & 5 deletions wasmi_v1/src/engine/bytecode/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
//! The instruction architecture of the `wasmi` interpreter.

mod utils;
mod visitor;

#[cfg(test)]
mod tests;

pub use self::{
utils::{BrTable, DropKeep, FuncIdx, GlobalIdx, LocalIdx, Offset, SignatureIdx, Target},
visitor::VisitInstruction,
};
pub use self::utils::{DropKeep, FuncIdx, GlobalIdx, LocalIdx, Offset, SignatureIdx, Target};
use wasmi_core::UntypedValue;

/// The internal `wasmi` bytecode that is stored for Wasm functions.
Expand Down
50 changes: 1 addition & 49 deletions wasmi_v1/src/engine/bytecode/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{super::super::engine::InstructionIdx, Instruction};
use core::cmp;
use super::super::super::engine::InstructionIdx;

/// Defines how many stack values are going to be dropped and kept after branching.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -191,50 +190,3 @@ impl Offset {
self.0
}
}

/// A reference to a `wasmi` bytecode `br_table`.
#[derive(Debug)]
pub struct BrTable<'a> {
/// The branches of the `wasmi` bytecode `br_table` including the default target.
///
/// # Note
///
/// All elements of this slice are of variant [`Instruction::Br`] or [`Instruction::Return`].
branches: &'a [Instruction],
}

impl<'a> BrTable<'a> {
/// Creates a new reference to a `wasmi` bytecode `br_table`.
///
/// # Note
///
/// The `targets` slice must contain the default target at its end.
///
/// # Panics (Debug Mode)
///
/// If the `targets` slice does not represent a `wasmi` bytecode `br_table`.
pub fn new(branches: &'a [Instruction]) -> Self {
assert!(
!branches.is_empty(),
"the targets slice must not be empty since the \
default target must be included at least",
);
debug_assert!(
branches
.iter()
.all(|inst| matches!(inst, Instruction::Br(_) | Instruction::Return(_))),
"the branches slice contains non `br` or `return` instructions: {:?}",
branches,
);
Self { branches }
}

/// Returns the branch at the given `index` if any or the default target.
pub fn branch_or_default(&self, index: usize) -> &Instruction {
// The index of the default target which is the last target of the slice.
let max_index = self.branches.len() - 1;
// A normalized index will always yield a target without panicking.
let normalized_index = cmp::min(index, max_index);
&self.branches[normalized_index]
}
}
185 changes: 0 additions & 185 deletions wasmi_v1/src/engine/bytecode/visitor.rs

This file was deleted.

18 changes: 14 additions & 4 deletions wasmi_v1/src/engine/call_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ pub struct FunctionFrame {
/// calling functions using the default table and avoids one indirection
/// to look-up the table in the `Instance`.
default_table: Option<Table>,
/// The current value of the instruction pointer.
/// The current value of the program counter.
///
/// # Note
///
/// The instruction pointer always points to the instruction
/// The program counter always points to the instruction
/// that is going to executed next.
pub inst_ptr: usize,
pc: usize,
}

impl FunctionFrame {
Expand All @@ -89,6 +89,16 @@ impl FunctionFrame {
}
}

/// Returns the program counter.
pub(super) fn pc(&self) -> usize {
self.pc
}

/// Updates the program counter.
pub(super) fn update_pc(&mut self, new_pc: usize) {
self.pc = new_pc;
}

/// Creates a new [`FunctionFrame`] from the given Wasm function entity.
pub(super) fn new_wasm(func: Func, wasm_func: &WasmFuncEntity) -> Self {
let instance = wasm_func.instance();
Expand All @@ -100,7 +110,7 @@ impl FunctionFrame {
instance,
default_memory: None,
default_table: None,
inst_ptr: 0,
pc: 0,
}
}

Expand Down
Loading