-
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.
feat(codegen): dispatching functions with function dispatcher (#153)
* feat(compiler): introduce flag dispatcher for compiler * feat(zabi): serde ABI * chore(zabi): not using default features of postcard * refactor(codegen): register compiled bytecode in backtrace * feat(codegen): introduce offset in jump table * feat(codegen): move return handlers to macro assembler * refactor(codegen): introduce external function in code section * feat(codegen): embed functions in codegen * feat(codegen): clean instructions of the dispatcher * feat(codegen): load calldata for dispatcher
- Loading branch information
Showing
42 changed files
with
623 additions
and
622 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//! Abi results | ||
/// ABI error | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
/// Failed to encode or decode with postcard. | ||
#[error(transparent)] | ||
Postcard(#[from] postcard::Error), | ||
/// Failed to decode from hex. | ||
#[error(transparent)] | ||
Hex(#[from] hex::FromHexError), | ||
} | ||
|
||
/// ABI result | ||
pub type Result<T> = std::result::Result<T, Error>; |
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 |
---|---|---|
|
@@ -92,6 +92,7 @@ macro_rules! offset { | |
|
||
offset! { | ||
(usize, 8), | ||
(u64, 8), | ||
(i64, 8), | ||
(i32, 4), | ||
(u32, 4), | ||
|
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,32 +1,36 @@ | ||
//! Backtrace support for the code generation. | ||
use std::collections::BTreeMap; | ||
|
||
/// Backtrace implementation for the code generation. | ||
/// | ||
/// TODO: full implementation #21 | ||
#[derive(Default)] | ||
/// TODO: full implementation (#21) | ||
#[derive(Debug, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//! External Function for the code section. | ||
use opcodes::ShangHai as OpCode; | ||
|
||
trait OpCodesToBytes { | ||
fn to_bytes(self) -> Vec<u8>; | ||
} | ||
|
||
impl OpCodesToBytes for &[OpCode] { | ||
fn to_bytes(self) -> Vec<u8> { | ||
[&[OpCode::JUMPDEST], self] | ||
.concat() | ||
.iter() | ||
.map(|op| (*op).into()) | ||
.collect() | ||
} | ||
} | ||
|
||
/// External function in code section. | ||
#[derive(PartialEq, Eq, Debug, Clone, Hash)] | ||
pub struct ExtFunc { | ||
/// Stack input. | ||
pub stack_out: u8, | ||
/// Stack output. | ||
pub stack_in: u8, | ||
/// The bytecode of the external function. | ||
pub bytecode: Vec<u8>, | ||
} | ||
|
||
impl ExtFunc { | ||
/// Function select. | ||
pub fn select() -> Self { | ||
Self { | ||
stack_in: 2, | ||
stack_out: 1, | ||
bytecode: [ | ||
OpCode::POP, | ||
OpCode::PUSH1, | ||
OpCode::Data(0x06), | ||
OpCode::ADD, | ||
OpCode::JUMP, | ||
] | ||
.to_bytes(), | ||
} | ||
} | ||
} |
Oops, something went wrong.