Skip to content

Commit

Permalink
Merge pull request #254 from rust-ethereum/sp-async2
Browse files Browse the repository at this point in the history
Several refactor work (attempt 2)
  • Loading branch information
sorpaas authored Dec 6, 2023
2 parents d2a21ce + 92504d6 commit 713c737
Show file tree
Hide file tree
Showing 16 changed files with 704 additions and 665 deletions.
483 changes: 0 additions & 483 deletions interpreter/src/call_create.rs

This file was deleted.

26 changes: 19 additions & 7 deletions interpreter/src/etable.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
use crate::{
call_create::CallCreateTrap, eval::*, ExitResult, GasState, Machine, Opcode, RuntimeBackend,
eval::*, trap::CallCreateTrap, ExitResult, GasState, Machine, Opcode, RuntimeBackend,
RuntimeEnvironment, RuntimeState, TrapConstruct,
};
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

pub trait EtableSet<S, H, Tr> {
pub trait EtableSet {
type State;
type Handle;
type Trap;

fn eval(
&self,
machine: &mut Machine<S>,
handle: &mut H,
machine: &mut Machine<Self::State>,
handle: &mut Self::Handle,
opcode: Opcode,
position: usize,
) -> Control<Tr>;
) -> Control<Self::Trap>;
}

impl<S, H, Tr, F> EtableSet<S, H, Tr> for Etable<S, H, Tr, F>
impl<S, H, Tr, F> EtableSet for Etable<S, H, Tr, F>
where
F: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
{
type State = S;
type Handle = H;
type Trap = Tr;

fn eval(
&self,
machine: &mut Machine<S>,
Expand All @@ -30,11 +38,15 @@ where
}
}

impl<S, H, Tr, F1, F2> EtableSet<S, H, Tr> for (Etable<S, H, Tr, F1>, Etable<S, H, Tr, F2>)
impl<S, H, Tr, F1, F2> EtableSet for (Etable<S, H, Tr, F1>, Etable<S, H, Tr, F2>)
where
F1: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
F2: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
{
type State = S;
type Handle = H;
type Trap = Tr;

fn eval(
&self,
machine: &mut Machine<S>,
Expand Down
2 changes: 1 addition & 1 deletion interpreter/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod misc;
mod system;

use crate::{
call_create::CallCreateTrap, Control, ExitException, ExitSucceed, GasState, Machine, Opcode,
trap::CallCreateTrap, Control, ExitException, ExitSucceed, GasState, Machine, Opcode,
RuntimeBackend, RuntimeEnvironment, RuntimeState, TrapConstruct,
};
use core::ops::{BitAnd, BitOr, BitXor};
Expand Down
48 changes: 25 additions & 23 deletions interpreter/src/interpreter/etable.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
use crate::interpreter::{Interpreter, RunInterpreter, StepInterpreter};
use crate::{
Capture, Control, EtableSet, ExitError, ExitException, ExitFatal, ExitResult, ExitSucceed,
Interpreter, Machine, Opcode, Stack, StepInterpreter, Valids,
Machine, Opcode, Stack, Valids,
};
use alloc::vec::Vec;
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

pub struct EtableInterpreter<'etable, S, H, Tr, ES> {
pub struct EtableInterpreter<'etable, S, ES> {
valids: Valids,
position: usize,
machine: Machine<S>,
etable: &'etable ES,
_marker: PhantomData<(H, Tr)>,
}

impl<'etable, S, H, Tr, ES> Deref for EtableInterpreter<'etable, S, H, Tr, ES> {
impl<'etable, S, ES> Deref for EtableInterpreter<'etable, S, ES> {
type Target = Machine<S>;

fn deref(&self) -> &Machine<S> {
&self.machine
}
}

impl<'etable, S, H, Tr, ES> DerefMut for EtableInterpreter<'etable, S, H, Tr, ES> {
impl<'etable, S, ES> DerefMut for EtableInterpreter<'etable, S, ES> {
fn deref_mut(&mut self) -> &mut Machine<S> {
&mut self.machine
}
}

impl<'etable, S, H, Tr, ES> EtableInterpreter<'etable, S, H, Tr, ES>
impl<'etable, S, ES> EtableInterpreter<'etable, S, ES>
where
ES: EtableSet<S, H, Tr>,
ES: EtableSet<State = S>,
{
/// Return a reference of the program counter.
pub const fn position(&self) -> usize {
Expand All @@ -45,7 +44,6 @@ where
valids,
position: 0,
etable,
_marker: PhantomData,
}
}

Expand Down Expand Up @@ -86,10 +84,9 @@ where
}
}

impl<'etable, S, H, Tr, ES> Interpreter<S, H, Tr> for EtableInterpreter<'etable, S, H, Tr, ES>
where
ES: EtableSet<S, H, Tr>,
{
impl<'etable, S, ES> Interpreter for EtableInterpreter<'etable, S, ES> {
type State = S;

fn machine(&self) -> &Machine<S> {
&self.machine
}
Expand All @@ -102,6 +99,19 @@ where
(self.machine.state, self.machine.retval)
}

fn advance(&mut self) {
if self.position == self.code.len() {
return;
}

self.position += 1;
}
}

impl<'etable, S, H, Tr, ES> RunInterpreter<H, Tr> for EtableInterpreter<'etable, S, ES>
where
ES: EtableSet<State = S, Handle = H, Trap = Tr>,
{
fn run(&mut self, handle: &mut H) -> Capture<ExitResult, Tr> {
loop {
match self.step(handle) {
Expand All @@ -110,19 +120,11 @@ where
}
}
}

fn advance(&mut self) {
if self.position == self.code.len() {
return;
}

self.position += 1;
}
}

impl<'etable, S, H, Tr, ES> StepInterpreter<S, H, Tr> for EtableInterpreter<'etable, S, H, Tr, ES>
impl<'etable, S, H, Tr, ES> StepInterpreter<H, Tr> for EtableInterpreter<'etable, S, ES>
where
ES: EtableSet<S, H, Tr>,
ES: EtableSet<State = S, Handle = H, Trap = Tr>,
{
#[inline]
fn step(&mut self, handle: &mut H) -> Result<(), Capture<ExitResult, Tr>> {
Expand Down
17 changes: 11 additions & 6 deletions interpreter/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ pub use self::etable::EtableInterpreter;
use crate::{Capture, ExitResult, Machine};
use alloc::vec::Vec;

pub trait Interpreter<S, H, Tr> {
fn machine(&self) -> &Machine<S>;
fn machine_mut(&mut self) -> &mut Machine<S>;
pub trait Interpreter {
type State;

fn deconstruct(self) -> (S, Vec<u8>);
fn run(&mut self, handle: &mut H) -> Capture<ExitResult, Tr>;
fn machine(&self) -> &Machine<Self::State>;
fn machine_mut(&mut self) -> &mut Machine<Self::State>;

fn deconstruct(self) -> (Self::State, Vec<u8>);
fn advance(&mut self);
}

pub trait StepInterpreter<S, H, Tr>: Interpreter<S, H, Tr> {
pub trait RunInterpreter<H, Tr>: Interpreter {
fn run(&mut self, handle: &mut H) -> Capture<ExitResult, Tr>;
}

pub trait StepInterpreter<H, Tr>: Interpreter {
fn step(&mut self, handle: &mut H) -> Result<(), Capture<ExitResult, Tr>>;
}
6 changes: 2 additions & 4 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@

extern crate alloc;

pub mod call_create;
mod error;
mod etable;
pub mod eval;
mod interpreter;
pub mod interpreter;
mod memory;
mod opcode;
mod runtime;
mod stack;
mod trap;
pub mod trap;
pub mod utils;
mod valids;

pub use crate::error::{Capture, ExitError, ExitException, ExitFatal, ExitResult, ExitSucceed};
pub use crate::etable::{Control, Efn, Etable, EtableSet};
pub use crate::interpreter::{EtableInterpreter, Interpreter, StepInterpreter};
pub use crate::memory::Memory;
pub use crate::opcode::Opcode;
pub use crate::runtime::{
Expand Down
Loading

0 comments on commit 713c737

Please sign in to comment.