Skip to content

Commit

Permalink
feat(interpreter): add more helper methods to memory (#794)
Browse files Browse the repository at this point in the history
* feat(interpreter): add more helper methods to memory

* chore: reduce usage of `#[inline(always)]`

* docs
  • Loading branch information
DaniPopes authored Oct 16, 2023
1 parent c593653 commit 2f78eb9
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 126 deletions.
1 change: 0 additions & 1 deletion bins/revme/src/statetest/merkle_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ struct TrieAccount {
}

impl TrieAccount {
#[inline(always)]
fn new(acc: &PlainAccount) -> Self {
Self {
nonce: acc.info.nonce,
Expand Down
16 changes: 8 additions & 8 deletions crates/interpreter/src/instructions/i256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const MIN_NEGATIVE_VALUE: U256 = U256::from_limbs([

const FLIPH_BITMASK_U64: u64 = 0x7FFFFFFFFFFFFFFF;

#[inline(always)]
#[inline]
pub fn i256_sign(val: &U256) -> Sign {
if val.bit(U256::BITS - 1) {
Sign::Minus
Expand All @@ -30,7 +30,7 @@ pub fn i256_sign(val: &U256) -> Sign {
}
}

#[inline(always)]
#[inline]
pub fn i256_sign_compl(val: &mut U256) -> Sign {
let sign = i256_sign(val);
if sign == Sign::Minus {
Expand All @@ -39,25 +39,25 @@ pub fn i256_sign_compl(val: &mut U256) -> Sign {
sign
}

#[inline(always)]
#[inline]
fn u256_remove_sign(val: &mut U256) {
// SAFETY: U256 does not have any padding bytes
unsafe {
val.as_limbs_mut()[3] &= FLIPH_BITMASK_U64;
}
}

#[inline(always)]
#[inline]
pub fn two_compl_mut(op: &mut U256) {
*op = two_compl(*op);
}

#[inline(always)]
#[inline]
pub fn two_compl(op: U256) -> U256 {
op.wrapping_neg()
}

#[inline(always)]
#[inline]
pub fn i256_cmp(first: &U256, second: &U256) -> Ordering {
let first_sign = i256_sign(first);
let second_sign = i256_sign(second);
Expand All @@ -69,7 +69,7 @@ pub fn i256_cmp(first: &U256, second: &U256) -> Ordering {
}
}

#[inline(always)]
#[inline]
pub fn i256_div(mut first: U256, mut second: U256) -> U256 {
let second_sign = i256_sign_compl(&mut second);
if second_sign == Sign::Zero {
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn i256_div(mut first: U256, mut second: U256) -> U256 {
}
}

#[inline(always)]
#[inline]
pub fn i256_mod(mut first: U256, mut second: U256) -> U256 {
let first_sign = i256_sign_compl(&mut first);
if first_sign == Sign::Zero {
Expand Down
11 changes: 1 addition & 10 deletions crates/interpreter/src/instructions/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@ pub fn mload<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pop!(interpreter, index);
let index = as_usize_or_fail!(interpreter, index);
shared_memory_resize!(interpreter, index, 32);
push!(
interpreter,
U256::from_be_bytes::<32>(
interpreter
.shared_memory
.slice(index, 32)
.try_into()
.unwrap()
)
);
push!(interpreter, interpreter.shared_memory.get_u256(index));
}

pub fn mstore<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
Expand Down
3 changes: 2 additions & 1 deletion crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ impl<'a> Interpreter<'a> {
/// Returns the current program counter.
#[inline]
pub fn program_counter(&self) -> usize {
// Safety: this is just subtraction of pointers, it is safe to do.
// SAFETY: `instruction_pointer` should be at an offset from the start of the bytecode.
// In practice this is always true unless a caller modifies the `instruction_pointer` field manually.
unsafe {
self.instruction_pointer
.offset_from(self.contract.bytecode.as_ptr()) as usize
Expand Down
Loading

0 comments on commit 2f78eb9

Please sign in to comment.