Skip to content

Commit

Permalink
refactor(codegen): register compiled bytecode in backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Oct 22, 2023
1 parent 5929a4f commit 3034943
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
28 changes: 16 additions & 12 deletions codegen/src/backtrace.rs
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
}
}
2 changes: 1 addition & 1 deletion codegen/src/visitor/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ impl CodeGen {
let buffer: Vec<u8> = self.masm.buffer().into();

// Pop offset and size from the bytecode.
let data_len = self.backtrace.popn(2);
let data_len = self.backtrace.popn(2).concat().len();
let data = &buffer[(buffer.len() - data_len)..];
*self.masm.buffer_mut() = buffer[..(buffer.len() - data_len)].into();

Expand Down
9 changes: 5 additions & 4 deletions codegen/src/visitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ macro_rules! map_wasm_operators {
let before = self.masm.buffer().len();
self.masm.[< _ $evm >]()?;

let after = self.masm.buffer().len();
self.backtrace.push(after - before);
let instr = self.masm.buffer()[before..].to_vec();
self.backtrace.push(instr);

Ok(())
}
Expand Down Expand Up @@ -100,8 +100,9 @@ macro_rules! map_wasm_operators {

let before = self.masm.buffer().len();
self.$($field.)*[< _ $evm >]($($arg),*)?;
let after = self.masm.buffer().len();
self.backtrace.push(after - before);

let instr = self.masm.buffer()[before..].to_vec();
self.backtrace.push(instr);
Ok(())
}
}
Expand Down

0 comments on commit 3034943

Please sign in to comment.