Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create dedecated bytecode_offset crate
- Loading branch information
Showing
6 changed files
with
95 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/// For tracking bytecode offsets in jumps | ||
#[derive(Clone, Copy, PartialEq, Debug)] | ||
#[must_use] | ||
pub struct BytecodeOffset { | ||
pub offset: usize, | ||
} | ||
|
||
impl BytecodeOffset { | ||
fn new(offset: usize) -> Self { | ||
Self { offset } | ||
} | ||
|
||
/// diff_from is useful for finding the offset between two bytecodes. This is usually | ||
/// used for jumps and jump_targets. | ||
/// | ||
/// For a forward jump, self will be a larger number, and start will be smaller | ||
/// the output will be a positive number. for a backward jump, the reverse | ||
/// will be true, and the number will be negative. So it is important to use this api | ||
/// consistently in both cases. | ||
/// | ||
/// Examples: | ||
/// let offset_diff: BytecodeOffsetDiff = forward_jump_target_offset.diff_from(jump) | ||
/// let offset_diff: BytecodeOffsetDiff = backward_jump_target_offset.diff_from(jump) | ||
pub fn diff_from(self, start: BytecodeOffset) -> BytecodeOffsetDiff { | ||
BytecodeOffsetDiff::new(self, start) | ||
} | ||
} | ||
|
||
impl From<BytecodeOffset> for usize { | ||
fn from(offset: BytecodeOffset) -> usize { | ||
offset.offset | ||
} | ||
} | ||
|
||
impl From<usize> for BytecodeOffset { | ||
fn from(offset: usize) -> BytecodeOffset { | ||
BytecodeOffset::new(offset) | ||
} | ||
} | ||
|
||
pub struct BytecodeOffsetDiff { | ||
diff: i32, | ||
} | ||
|
||
impl BytecodeOffsetDiff { | ||
fn new(end: BytecodeOffset, start: BytecodeOffset) -> Self { | ||
let diff = (end.offset as i128 - start.offset as i128) as i32; | ||
Self { diff } | ||
} | ||
|
||
pub fn uninitialized() -> Self { | ||
Self { diff: 0i32 } | ||
} | ||
} | ||
|
||
impl From<BytecodeOffsetDiff> for i32 { | ||
fn from(offset: BytecodeOffsetDiff) -> i32 { | ||
offset.diff | ||
} | ||
} |
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