From 4335a8cfbb8823219128f6547146d938e12ccba3 Mon Sep 17 00:00:00 2001 From: Zhou Fang <33002388+zhou-w-fang@users.noreply.github.com> Date: Sun, 17 Nov 2024 21:08:15 -0800 Subject: [PATCH 1/6] feat: design skip_to() --- crates/interpreter/src/parser.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/interpreter/src/parser.rs b/crates/interpreter/src/parser.rs index 730f11d1..1dbf0f80 100644 --- a/crates/interpreter/src/parser.rs +++ b/crates/interpreter/src/parser.rs @@ -64,6 +64,10 @@ impl<'m, M: Mode> Parser<'m, M> { Ok(result) } + pub fn parse_bytes_reversely(&mut self, delta: i32) -> MResult<&'m [u8], M> { + unimplemented!() + } + pub fn split_at(&mut self, len: usize) -> MResult, M> { Ok(Self::internal_new(self.parse_bytes(len)?)) } @@ -564,6 +568,15 @@ impl<'m, M: Mode> Parser<'m, M> { } Ok(()) } + + pub fn skip_to(&mut self, delta: i32) -> MResult<(), M> { + if delta >= 0 { + self.parse_bytes(delta as usize)?; + } else { + self.parse_bytes_reversely(delta)?; + } + Ok(()) + } } pub trait ParseElem<'m, M: Mode> { From b1e636b1995ea2003c4886ac59d6d0ed6e0c3288 Mon Sep 17 00:00:00 2001 From: Zhou Fang <33002388+zhou-w-fang@users.noreply.github.com> Date: Sat, 30 Nov 2024 17:22:51 -0800 Subject: [PATCH 2/6] feat!: apply delta_ip and delta_stp in exec.rs --- crates/interpreter/src/parser.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/crates/interpreter/src/parser.rs b/crates/interpreter/src/parser.rs index 1dbf0f80..730f11d1 100644 --- a/crates/interpreter/src/parser.rs +++ b/crates/interpreter/src/parser.rs @@ -64,10 +64,6 @@ impl<'m, M: Mode> Parser<'m, M> { Ok(result) } - pub fn parse_bytes_reversely(&mut self, delta: i32) -> MResult<&'m [u8], M> { - unimplemented!() - } - pub fn split_at(&mut self, len: usize) -> MResult, M> { Ok(Self::internal_new(self.parse_bytes(len)?)) } @@ -568,15 +564,6 @@ impl<'m, M: Mode> Parser<'m, M> { } Ok(()) } - - pub fn skip_to(&mut self, delta: i32) -> MResult<(), M> { - if delta >= 0 { - self.parse_bytes(delta as usize)?; - } else { - self.parse_bytes_reversely(delta)?; - } - Ok(()) - } } pub trait ParseElem<'m, M: Mode> { From 03a5bfa677da74ab302b14810574d68f96f994d6 Mon Sep 17 00:00:00 2001 From: Zhou Fang <33002388+zhou-w-fang@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:11:13 -0800 Subject: [PATCH 3/6] refactor: apply->restore side table for loop --- crates/interpreter/src/exec.rs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/crates/interpreter/src/exec.rs b/crates/interpreter/src/exec.rs index 84238bc6..349d8c2d 100644 --- a/crates/interpreter/src/exec.rs +++ b/crates/interpreter/src/exec.rs @@ -766,13 +766,7 @@ impl<'m> Thread<'m> { Unreachable => return Err(trap()), Nop => (), Block(b) => self.push_label(self.blocktype(inst, &b), LabelKind::Block), - Loop(b) => { - let side_table = self.frame().side_table; - self.push_label( - self.blocktype(inst, &b), - LabelKind::Loop(LoopState { parser_data: saved, side_table }), - ) - } + Loop(b) => self.push_label(self.blocktype(inst, &b), LabelKind::Loop(saved)), If(b) => match self.pop_value().unwrap_i32() { 0 => { self.take_jump(0); @@ -1064,9 +1058,9 @@ impl<'m> Thread<'m> { self.values().drain(values_len - values_cnt .. values_len - arity); self.label().values_cnt += arity; match kind { - LabelKind::Loop(state) => unsafe { - self.frame().side_table = state.side_table; - self.parser.restore(state.parser_data) + LabelKind::Loop(parser_data) => unsafe { + self.take_jump(offset); + self.parser.restore(parser_data) }, LabelKind::Block | LabelKind::If => self.take_jump(offset), } @@ -1454,12 +1448,6 @@ struct Label<'m> { values_cnt: usize, } -#[derive(Debug)] -struct LoopState<'m> { - parser_data: &'m [u8], - side_table: &'m [SideTableEntry], -} - #[derive(Debug)] enum LabelKind<'m> { // TODO: If and Block can be merged and then we just have Option> which is @@ -1467,7 +1455,7 @@ enum LabelKind<'m> { Block, // TODO: Could be just NonNull since we can reuse the end of current parser since it // never changes. - Loop(LoopState<'m>), + Loop(&'m [u8]), If, } From d1f028b0e632b35baf83a1cc6512622056efd4bb Mon Sep 17 00:00:00 2001 From: Zhou Fang <33002388+zhou-w-fang@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:32:13 -0800 Subject: [PATCH 4/6] fix!: delta_ip-=2 for loop --- crates/interpreter/src/exec.rs | 30 ++++++++++++------------------ crates/interpreter/src/valid.rs | 20 +++++++++++++------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/crates/interpreter/src/exec.rs b/crates/interpreter/src/exec.rs index 349d8c2d..6845da86 100644 --- a/crates/interpreter/src/exec.rs +++ b/crates/interpreter/src/exec.rs @@ -15,6 +15,7 @@ // TODO: Some toctou could be used instead of panic. use alloc::vec; use alloc::vec::Vec; +use core::fmt::Debug; use crate::error::*; use crate::module::*; @@ -759,14 +760,13 @@ impl<'m> Thread<'m> { fn step(mut self, store: &mut Store<'m>) -> Result, Error> { use Instr::*; - let saved = self.parser.save(); let inst_id = self.frame().inst_id; let inst = &mut store.insts[inst_id]; match self.parser.parse_instr().into_ok() { Unreachable => return Err(trap()), Nop => (), Block(b) => self.push_label(self.blocktype(inst, &b), LabelKind::Block), - Loop(b) => self.push_label(self.blocktype(inst, &b), LabelKind::Loop(saved)), + Loop(b) => self.push_label(self.blocktype(inst, &b), LabelKind::Loop), If(b) => match self.pop_value().unwrap_i32() { 0 => { self.take_jump(0); @@ -982,11 +982,11 @@ impl<'m> Thread<'m> { self.frames.last_mut().unwrap() } - fn labels(&mut self) -> &mut Vec> { + fn labels(&mut self) -> &mut Vec