Skip to content

Commit

Permalink
refactor main loop
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Nov 4, 2023
1 parent 7780414 commit 256e639
Show file tree
Hide file tree
Showing 24 changed files with 511 additions and 519 deletions.
22 changes: 11 additions & 11 deletions crates/interpreter/src/instructions/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,67 @@ use crate::{
Host, InstructionResult, Interpreter,
};

pub fn wrapped_add<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn wrapped_add<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1.wrapping_add(*op2);
}

pub fn wrapping_mul<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn wrapping_mul<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
*op2 = op1.wrapping_mul(*op2);
}

pub fn wrapping_sub<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn wrapping_sub<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1.wrapping_sub(*op2);
}

pub fn div<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn div<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
if *op2 != U256::ZERO {
*op2 = op1.wrapping_div(*op2);
}
}

pub fn sdiv<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn sdiv<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
*op2 = i256_div(op1, *op2);
}

pub fn rem<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn rem<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
if *op2 != U256::ZERO {
*op2 = op1.wrapping_rem(*op2);
}
}

pub fn smod<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn smod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
if *op2 != U256::ZERO {
*op2 = i256_mod(op1, *op2)
}
}

pub fn addmod<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn addmod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::MID);
pop_top!(interpreter, op1, op2, op3);
*op3 = op1.add_mod(op2, *op3)
}

pub fn mulmod<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn mulmod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::MID);
pop_top!(interpreter, op1, op2, op3);
*op3 = op1.mul_mod(op2, *op3)
}

pub fn exp<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn exp<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
pop_top!(interpreter, op1, op2);
gas_or_fail!(interpreter, gas::exp_cost::<SPEC>(*op2));
*op2 = op1.pow(*op2);
Expand All @@ -86,7 +86,7 @@ pub fn exp<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H
/// `y | !mask` where `|` is the bitwise `OR` and `!` is bitwise negation. Similarly, if
/// `b == 0` then the yellow paper says the output should start with all zeros, then end with
/// bits from `b`; this is equal to `y & mask` where `&` is bitwise `AND`.
pub fn signextend<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn signextend<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
if op1 < U256::from(32) {
Expand Down
28 changes: 14 additions & 14 deletions crates/interpreter/src/instructions/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,67 @@ use crate::{
};
use core::cmp::Ordering;

pub fn lt<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn lt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(op1 < *op2);
}

pub fn gt<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn gt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(op1 > *op2);
}

pub fn slt<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn slt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(i256_cmp(&op1, op2) == Ordering::Less);
}

pub fn sgt<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn sgt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(i256_cmp(&op1, op2) == Ordering::Greater);
}

pub fn eq<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn eq<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(op1 == *op2);
}

pub fn iszero<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn iszero<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1);
*op1 = U256::from(*op1 == U256::ZERO);
}

pub fn bitand<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn bitand<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1 & *op2;
}

pub fn bitor<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn bitor<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1 | *op2;
}

pub fn bitxor<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn bitxor<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1 ^ *op2;
}

pub fn not<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn not<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1);
*op1 = !*op1;
}

pub fn byte<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn byte<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);

Expand All @@ -80,23 +80,23 @@ pub fn byte<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
}

/// EIP-145: Bitwise shifting instructions in EVM
pub fn shl<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn shl<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
check!(interpreter, CONSTANTINOPLE);
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 <<= as_usize_saturated!(op1);
}

/// EIP-145: Bitwise shifting instructions in EVM
pub fn shr<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn shr<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
check!(interpreter, CONSTANTINOPLE);
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 >>= as_usize_saturated!(op1);
}

/// EIP-145: Bitwise shifting instructions in EVM
pub fn sar<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn sar<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
check!(interpreter, CONSTANTINOPLE);
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
Expand Down
28 changes: 11 additions & 17 deletions crates/interpreter/src/instructions/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Host, InstructionResult, Interpreter, InterpreterResult,
};

pub fn jump<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn jump<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::MID);
pop!(interpreter, dest);
let dest = as_usize_or_fail!(interpreter, dest, InstructionResult::InvalidJump);
Expand All @@ -20,7 +20,7 @@ pub fn jump<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
}
}

pub fn jumpi<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn jumpi<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::HIGH);
pop!(interpreter, dest, value);
if value != U256::ZERO {
Expand All @@ -36,18 +36,18 @@ pub fn jumpi<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
}
}

pub fn jumpdest<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn jumpdest<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::JUMPDEST);
}

pub fn pc<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn pc<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::BASE);
// - 1 because we have already advanced the instruction pointer in `Interpreter::step`
push!(interpreter, U256::from(interpreter.program_counter() - 1));
}

#[inline(always)]
fn return_inner(interpreter: &mut Interpreter<'_>, instruction_result: InstructionResult) {
fn return_inner(interpreter: &mut Interpreter, instruction_result: InstructionResult) {
// zero gas cost
// gas!(interpreter, gas::ZERO);
pop!(interpreter, offset, len);
Expand All @@ -58,13 +58,7 @@ fn return_inner(interpreter: &mut Interpreter<'_>, instruction_result: Instructi
let offset = as_usize_or_fail!(interpreter, offset);
shared_memory_resize!(interpreter, offset, len);

output = interpreter
.shared_memory
.as_mut()
.unwrap()
.slice(offset, len)
.to_vec()
.into()
output = interpreter.shared_memory.slice(offset, len).to_vec().into()
}
interpreter.instruction_result = instruction_result;
interpreter.next_action = Some(crate::InterpreterAction::Return {
Expand All @@ -76,27 +70,27 @@ fn return_inner(interpreter: &mut Interpreter<'_>, instruction_result: Instructi
});
}

pub fn ret<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn ret<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
return_inner(interpreter, InstructionResult::Return);
}

/// EIP-140: REVERT instruction
pub fn revert<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn revert<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
check!(interpreter, BYZANTIUM);
return_inner(interpreter, InstructionResult::Revert);
}

pub fn stop<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn stop<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
interpreter.return_data_buffer = Bytes::default();
interpreter.instruction_result = InstructionResult::Stop;
}

pub fn invalid<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn invalid<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
interpreter.return_data_buffer = Bytes::default();
interpreter.instruction_result = InstructionResult::InvalidFEOpcode;
}

pub fn not_found<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pub fn not_found<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
interpreter.return_data_buffer = Bytes::default();
interpreter.instruction_result = InstructionResult::OpcodeNotFound;
}
Loading

0 comments on commit 256e639

Please sign in to comment.