-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: create new types for pc-ic maps
- Loading branch information
Showing
8 changed files
with
88 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,78 @@ | ||
use revm::{ | ||
interpreter::{opcode, opcode::spec_opcode_gas}, | ||
primitives::SpecId, | ||
primitives::{HashMap, SpecId}, | ||
}; | ||
use std::collections::BTreeMap; | ||
|
||
/// A map of program counters to instruction counters. | ||
pub type PCICMap = BTreeMap<usize, usize>; | ||
|
||
/// Builds a mapping from program counters to instruction counters. | ||
pub fn build_pc_ic_map(spec: SpecId, code: &[u8]) -> PCICMap { | ||
let opcode_infos = spec_opcode_gas(spec); | ||
let mut pc_ic_map: PCICMap = BTreeMap::new(); | ||
|
||
let mut i = 0; | ||
let mut cumulative_push_size = 0; | ||
while i < code.len() { | ||
let op = code[i]; | ||
pc_ic_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; | ||
|
||
/// Maps from program counter to instruction counter. | ||
/// | ||
/// Inverse of [`IcPcMap`]. | ||
pub struct PcIcMap { | ||
pub inner: HashMap<usize, usize>, | ||
} | ||
|
||
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 = HashMap::new(); | ||
|
||
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; | ||
} | ||
i += 1; | ||
|
||
Self { inner: map } | ||
} | ||
|
||
pc_ic_map | ||
/// Returns the instruction counter for the given program counter. | ||
pub fn get(&self, pc: usize) -> Option<usize> { | ||
self.inner.get(&pc).copied() | ||
} | ||
} | ||
|
||
/// A map of instruction counters to program counters. | ||
pub type ICPCMap = BTreeMap<usize, usize>; | ||
|
||
/// Builds a mapping from instruction counters to program counters. | ||
pub fn build_ic_pc_map(spec: SpecId, code: &[u8]) -> ICPCMap { | ||
let opcode_infos = spec_opcode_gas(spec); | ||
let mut ic_pc_map: ICPCMap = ICPCMap::new(); | ||
|
||
let mut i = 0; | ||
let mut cumulative_push_size = 0; | ||
while i < code.len() { | ||
let op = code[i]; | ||
ic_pc_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; | ||
/// Map from instruction counter to program counter. | ||
/// | ||
/// Inverse of [`PcIcMap`]. | ||
pub struct IcPcMap { | ||
pub inner: HashMap<usize, usize>, | ||
} | ||
|
||
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 = HashMap::new(); | ||
|
||
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; | ||
} | ||
i += 1; | ||
|
||
Self { inner: map } | ||
} | ||
|
||
ic_pc_map | ||
/// Returns the program counter for the given instruction counter. | ||
pub fn get(&self, ic: usize) -> Option<usize> { | ||
self.inner.get(&ic).copied() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters