revm_bytecode/legacy/
analyzed.rs

1use super::JumpTable;
2use bitvec::{bitvec, order::Lsb0};
3use primitives::Bytes;
4use std::sync::Arc;
5
6// Legacy analyzed
7#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct LegacyAnalyzedBytecode {
10    /// Bytecode with 32 zero bytes padding
11    bytecode: Bytes,
12    /// Original bytes length
13    original_len: usize,
14    /// Jump table
15    jump_table: JumpTable,
16}
17
18impl Default for LegacyAnalyzedBytecode {
19    #[inline]
20    fn default() -> Self {
21        Self {
22            bytecode: Bytes::from_static(&[0]),
23            original_len: 0,
24            jump_table: JumpTable(Arc::new(bitvec![u8, Lsb0; 0])),
25        }
26    }
27}
28
29impl LegacyAnalyzedBytecode {
30    /// Creates new analyzed bytecode.
31    pub fn new(bytecode: Bytes, original_len: usize, jump_table: JumpTable) -> Self {
32        Self {
33            bytecode,
34            original_len,
35            jump_table,
36        }
37    }
38
39    /// Returns a reference to the bytecode.
40    ///
41    /// The bytecode is padded with 32 zero bytes.
42    pub fn bytecode(&self) -> &Bytes {
43        &self.bytecode
44    }
45
46    /// Returns original bytes length.
47    pub fn original_len(&self) -> usize {
48        self.original_len
49    }
50
51    /// Returns original bytes without padding.
52    pub fn original_bytes(&self) -> Bytes {
53        self.bytecode.slice(..self.original_len)
54    }
55
56    /// Returns original bytes without padding.
57    pub fn original_byte_slice(&self) -> &[u8] {
58        &self.bytecode[..self.original_len]
59    }
60
61    /// Returns [JumpTable] of analyzed bytes.
62    pub fn jump_table(&self) -> &JumpTable {
63        &self.jump_table
64    }
65}