Skip to content

Commit

Permalink
isa: fix instruction complexities
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Mar 28, 2024
1 parent a2efc3a commit e689fe7
Showing 1 changed file with 29 additions and 2 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 @@ -173,6 +172,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(),

Check warning on line 184 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L175-L184

Added lines #L175 - L184 were not covered by tests
#[cfg(feature = "secp256k1")]
Instr::Secp256k1(instr) => instr.complexity(),

Check warning on line 186 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L186

Added line #L186 was not covered by tests
#[cfg(feature = "curve25519")]
Instr::Curve25519(instr) => instr.complexity(),
Instr::ExtensionCodes(instr) => instr.complexity(),
Instr::ReservedInstruction(instr) => instr.complexity(),
Instr::Nop => 1,

Check warning on line 191 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L188-L191

Added lines #L188 - L191 were not covered by tests
}
}

Check warning on line 193 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L193

Added line #L193 was not covered by tests

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

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

Check warning on line 435 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L435

Added line #L435 was not covered by tests

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

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

Check warning on line 571 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L571

Added line #L571 was not covered by tests

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

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

Check warning on line 914 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L914

Added line #L914 was not covered by tests

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 @@ -1652,6 +1677,8 @@ impl InstructionSet for ReservedOp {

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

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

Check warning on line 1680 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L1680

Added line #L1680 was not covered by tests

fn exec(&self, regs: &mut CoreRegs, site: LibSite, ctx: &()) -> ExecStep {
ControlFlowOp::Fail.exec(regs, site, ctx)
}
Expand Down

0 comments on commit e689fe7

Please sign in to comment.