-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(codegen): register compiled bytecode in backtrace
- Loading branch information
Showing
3 changed files
with
22 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
//! Backtrace support for the code generation. | ||
use std::collections::BTreeMap; | ||
|
||
/// Backtrace implementation for the code generation. | ||
/// | ||
/// TODO: full implementation #21 | ||
/// TODO: full implementation (#21) | ||
#[derive(Default)] | ||
pub struct Backtrace { | ||
/// The length of each operand. | ||
len: Vec<usize>, | ||
/// Compiled instructions. | ||
/// | ||
/// TODO: Transform this into Opcodes. (#21) | ||
instrs: BTreeMap<usize, Vec<u8>>, | ||
} | ||
|
||
impl Backtrace { | ||
/// Pushes a new operand to the backtrace. | ||
pub fn push(&mut self, len: usize) { | ||
self.len.push(len); | ||
/// Pushes a new instruction set to the backtrace. | ||
pub fn push(&mut self, bytes: impl AsRef<[u8]>) { | ||
self.instrs.insert(self.instrs.len(), bytes.as_ref().into()); | ||
} | ||
|
||
/// Pops the last operand from the backtrace. | ||
pub fn pop(&mut self) -> usize { | ||
self.len.pop().unwrap_or_default() | ||
/// Pops the last instruction from the backtrace. | ||
pub fn pop(&mut self) -> Vec<u8> { | ||
self.instrs.pop_last().unwrap_or_default().1 | ||
} | ||
|
||
pub fn popn(&mut self, n: usize) -> usize { | ||
let mut r: Vec<usize> = Default::default(); | ||
/// Pop the last `n` operands from the backtrace. | ||
pub fn popn(&mut self, n: usize) -> Vec<Vec<u8>> { | ||
let mut r: Vec<Vec<u8>> = Default::default(); | ||
|
||
while r.len() < n { | ||
r.push(self.pop()) | ||
} | ||
|
||
r.into_iter().sum() | ||
r | ||
} | ||
} |
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