Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: factor out common ic-pc code #7396

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 29 additions & 38 deletions crates/evm/core/src/ic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,7 @@ pub struct PcIcMap {
impl PcIcMap {
/// Creates a new `PcIcMap` for the given code.
pub fn new(spec: SpecId, code: &[u8]) -> Self {
let opcode_infos = spec_opcode_gas(spec);
let mut map = FxHashMap::default();

let mut i = 0;
let mut cumulative_push_size = 0;
while i < code.len() {
let op = code[i];
map.insert(i, i - cumulative_push_size);
if opcode_infos[op as usize].is_push() {
// Skip the push bytes.
//
// For more context on the math, see: https://github.com/bluealloy/revm/blob/007b8807b5ad7705d3cacce4d92b89d880a83301/crates/revm/src/interpreter/contract.rs#L114-L115
i += (op - opcode::PUSH1 + 1) as usize;
cumulative_push_size += (op - opcode::PUSH1 + 1) as usize;
}
i += 1;
}

Self { inner: map }
Self { inner: make_map::<true>(spec, code) }
}

/// Returns the instruction counter for the given program counter.
Expand All @@ -51,29 +33,38 @@ pub struct IcPcMap {
impl IcPcMap {
/// Creates a new `IcPcMap` for the given code.
pub fn new(spec: SpecId, code: &[u8]) -> Self {
let opcode_infos = spec_opcode_gas(spec);
let mut map = FxHashMap::default();

let mut i = 0;
let mut cumulative_push_size = 0;
while i < code.len() {
let op = code[i];
map.insert(i - cumulative_push_size, i);
if opcode_infos[op as usize].is_push() {
// Skip the push bytes.
//
// For more context on the math, see: https://github.com/bluealloy/revm/blob/007b8807b5ad7705d3cacce4d92b89d880a83301/crates/revm/src/interpreter/contract.rs#L114-L115
i += (op - opcode::PUSH1 + 1) as usize;
cumulative_push_size += (op - opcode::PUSH1 + 1) as usize;
}
i += 1;
}

Self { inner: map }
Self { inner: make_map::<false>(spec, code) }
}

/// Returns the program counter for the given instruction counter.
pub fn get(&self, ic: usize) -> Option<usize> {
self.inner.get(&ic).copied()
}
}

fn make_map<const PC_FIRST: bool>(spec: SpecId, code: &[u8]) -> FxHashMap<usize, usize> {
let opcode_infos = spec_opcode_gas(spec);
let mut map = FxHashMap::default();

let mut pc = 0;
let mut cumulative_push_size = 0;
while pc < code.len() {
let ic = pc - cumulative_push_size;
if PC_FIRST {
map.insert(pc, ic);
} else {
map.insert(ic, pc);
}

let op = code[pc];
if opcode_infos[op as usize].is_push() {
// Skip the push bytes.
let push_size = (op - opcode::PUSH0) as usize;
pc += push_size;
cumulative_push_size += push_size;
}

pc += 1;
}
map
}
Loading