Skip to content

Commit

Permalink
Merge pull request #114 from AluVM/simplify
Browse files Browse the repository at this point in the history
Remove needless traits and structs
  • Loading branch information
dr-orlovsky authored Apr 18, 2024
2 parents 7103495 + 3e4b095 commit 22ec737
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 201 deletions.
31 changes: 29 additions & 2 deletions src/isa/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ pub trait InstructionSet: Bytecode + core::fmt::Display + core::fmt::Debug {
fn dst_regs(&self) -> BTreeSet<Reg>;

/// Returns computational complexity of the instruction
#[inline]
fn complexity(&self) -> u64 { 1 }
fn complexity(&self) -> u64;

/// Executes given instruction taking all registers as input and output.
///
Expand Down Expand Up @@ -172,6 +171,26 @@ where
}
}

fn complexity(&self) -> u64 {
match self {
Instr::ControlFlow(instr) => instr.complexity(),
Instr::Put(instr) => instr.complexity(),
Instr::Move(instr) => instr.complexity(),
Instr::Cmp(instr) => instr.complexity(),
Instr::Arithmetic(instr) => instr.complexity(),
Instr::Bitwise(instr) => instr.complexity(),
Instr::Bytes(instr) => instr.complexity(),
Instr::Digest(instr) => instr.complexity(),
#[cfg(feature = "secp256k1")]
Instr::Secp256k1(instr) => instr.complexity(),
#[cfg(feature = "curve25519")]
Instr::Curve25519(instr) => instr.complexity(),
Instr::ExtensionCodes(instr) => instr.complexity(),
Instr::ReservedInstruction(instr) => instr.complexity(),
Instr::Nop => 1,
}
}

#[inline]
fn exec(&self, regs: &mut CoreRegs, site: LibSite, ctx: &Self::Context<'_>) -> ExecStep {
match self {
Expand Down Expand Up @@ -412,6 +431,8 @@ impl InstructionSet for MoveOp {
}
}

fn complexity(&self) -> u64 { 1 }

fn exec(&self, regs: &mut CoreRegs, _: LibSite, _: &()) -> ExecStep {
match self {
MoveOp::MovA(reg, idx1, idx2) => {
Expand Down Expand Up @@ -546,6 +567,8 @@ impl InstructionSet for CmpOp {
}
}

fn complexity(&self) -> u64 { 1 }

fn exec(&self, regs: &mut CoreRegs, _: LibSite, _: &()) -> ExecStep {
match self {
CmpOp::GtA(sign_flag, reg, idx1, idx2) => {
Expand Down Expand Up @@ -887,6 +910,8 @@ impl InstructionSet for BitwiseOp {
}
}

fn complexity(&self) -> u64 { 1 }

fn exec(&self, regs: &mut CoreRegs, _site: LibSite, _: &()) -> ExecStep {
fn shl(original: &[u8], shift: usize, n_bytes: usize) -> [u8; 1024] {
let mut ret = [0u8; 1024];
Expand Down Expand Up @@ -1639,6 +1664,8 @@ impl InstructionSet for ReservedOp {

fn dst_regs(&self) -> BTreeSet<Reg> { bset![] }

fn complexity(&self) -> u64 { u64::MAX }

fn exec(&self, regs: &mut CoreRegs, site: LibSite, ctx: &()) -> ExecStep {
ControlFlowOp::Fail.exec(regs, site, ctx)
}
Expand Down
7 changes: 3 additions & 4 deletions src/isa/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
///
/// ```
/// # use aluvm::aluasm;
/// # use aluvm::{Prog, Vm};
/// # use aluvm::library::Lib;
/// # use aluvm::Vm;
/// # use aluvm::library::{Lib, LibSite};
/// # use aluvm::isa::Instr;
///
/// let code = aluasm! {
Expand All @@ -46,9 +46,8 @@
/// };
///
/// let lib = Lib::assemble(&code).unwrap();
/// let program = Prog::<Instr>::new(lib);
/// let mut vm = Vm::<Instr>::new();
/// match vm.run(&program, &()) {
/// match vm.exec(LibSite::default(), |_| Some(&lib), &()) {
/// true => println!("success"),
/// false => println!("failure"),
/// }
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ pub mod data;
#[macro_use]
pub mod isa;
pub mod library;
mod program;
pub mod reg;
#[cfg(feature = "stl")]
pub mod stl;
Expand All @@ -167,7 +166,6 @@ pub use isa::Isa;
pub use library::LibArmorError;
#[doc(hidden)]
pub use paste::paste;
pub use program::{Prog, ProgError, Program};
pub use vm::Vm;

/// Struct types library name.
Expand Down
177 changes: 0 additions & 177 deletions src/program.rs

This file was deleted.

22 changes: 6 additions & 16 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ use alloc::boxed::Box;
use core::marker::PhantomData;

use crate::isa::{Instr, InstructionSet, ReservedOp};
use crate::library::LibSite;
use crate::library::{Lib, LibId, LibSite};
use crate::reg::CoreRegs;
use crate::Program;

/// Alu virtual machine providing single-core execution environment
#[derive(Debug, Default)]
Expand All @@ -57,24 +56,15 @@ where
/// # Returns
///
/// Value of the `st0` register at the end of the program execution.
pub fn run(&mut self, program: &impl Program<Isa = Isa>, context: &Isa::Context<'_>) -> bool {
self.call(program, program.entrypoint(), context)
}

/// Executes the program starting from the provided entry point.
///
/// # Returns
///
/// Value of the `st0` register at the end of the program execution.
pub fn call(
pub fn exec<'prog>(
&mut self,
program: &impl Program<Isa = Isa>,
method: LibSite,
entry_point: LibSite,
lib_resolver: impl Fn(LibId) -> Option<&'prog Lib>,
context: &Isa::Context<'_>,
) -> bool {
let mut call = Some(method);
let mut call = Some(entry_point);
while let Some(ref mut site) = call {
if let Some(lib) = program.lib(site.lib) {
if let Some(lib) = lib_resolver(site.lib) {
call = lib.exec::<Isa>(site.pos, &mut self.registers, context);
} else if let Some(pos) = site.pos.checked_add(1) {
site.pos = pos;
Expand Down

0 comments on commit 22ec737

Please sign in to comment.