Skip to content

Commit

Permalink
feat(zinkc): embed ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Dec 16, 2023
1 parent 387999b commit b622922
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod selector;
use core::ops::{Deref, DerefMut};

/// Function ABI.
#[derive(Debug, Clone)]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Abi(sol_abi::Abi);

Expand Down
2 changes: 1 addition & 1 deletion codegen/src/code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use indexmap::IndexMap;
mod func;

/// Code section for EVM.
#[derive(Default, Debug)]
#[derive(Clone, Default, Debug)]
pub struct Code {
offset: usize,
/// Function table.
Expand Down
13 changes: 11 additions & 2 deletions codegen/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub struct Dispatcher<'d> {
pub data: DataSet,
/// Jump table
pub table: JumpTable,
/// ABI for the current function
///
/// TODO: refactor this. (#192)
pub abi: Vec<Abi>,
}

impl<'d> Dispatcher<'d> {
Expand All @@ -33,6 +37,7 @@ impl<'d> Dispatcher<'d> {
imports: Default::default(),
data: Default::default(),
table: Default::default(),
abi: Default::default(),
}
}

Expand Down Expand Up @@ -179,6 +184,10 @@ impl<'d> Dispatcher<'d> {
/// Emit selector to buffer.
fn emit_selector(&mut self, selector: &Function<'_>, last: bool) -> Result<()> {
let abi = self.load_abi(selector)?;

// TODO: refactor this. (#192)
self.abi.push(abi.clone());

let selector_bytes = abi.selector();

tracing::trace!(
Expand Down Expand Up @@ -236,7 +245,7 @@ impl<'d> Dispatcher<'d> {
}

/// Emit compiled code to the given buffer.
pub fn finish(mut self, selectors: Functions<'_>, table: &mut JumpTable) -> Result<Vec<u8>> {
pub fn finish(&mut self, selectors: Functions<'_>, table: &mut JumpTable) -> Result<Vec<u8>> {
if selectors.is_empty() {
return Err(Error::SelectorNotFound);
}
Expand All @@ -252,7 +261,7 @@ impl<'d> Dispatcher<'d> {
len -= 1;
}

table.merge(self.table, 0)?;
table.merge(self.table.clone(), 0)?;
Ok(self.asm.buffer().into())
}
}
2 changes: 1 addition & 1 deletion codegen/src/jump/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::BTreeMap;

/// Jump table implementation.
///
#[derive(Default, Debug)]
#[derive(Clone, Default, Debug)]
pub struct JumpTable {
/// Jump table.
pub(crate) jump: BTreeMap<u16, Jump>,
Expand Down
1 change: 1 addition & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ anyhow.workspace = true
thiserror.workspace = true
tracing.workspace = true
wasmparser.workspace = true
zabi.workspace = true
zingen.workspace = true

[dev-dependencies]
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! Zink compiler
use crate::{parser::Parser, Config, Error, Result};
use zabi::Abi;
use zingen::{
Buffer, CodeGen, Constructor, DataSet, Dispatcher, Function, Imports, JumpTable, BUFFER_LIMIT,
};

/// Zink Compiler
#[derive(Default)]
pub struct Compiler {
abi: Vec<Abi>,
buffer: Buffer,
table: JumpTable,
config: Config,
Expand Down Expand Up @@ -71,6 +73,7 @@ impl Compiler {
return Err(Error::BufferOverflow(self.buffer.len()));
}

self.abi = dispatcher.abi;
Ok(())
}

Expand Down
24 changes: 22 additions & 2 deletions evm/abi/src/abi.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Solidity ABI abstraction.
use crate::Input;
use core::{convert::Infallible, str::FromStr};

/// Solidity ABI abstraction.
#[derive(Debug, Clone)]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Abi {
/// ABI name.
Expand Down Expand Up @@ -36,15 +38,33 @@ impl From<&syn::Signature> for Abi {
}

/// Solidity ABI type.
#[derive(Debug, Clone)]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Type {
/// Constructor ABI.
Constructor,
/// Function ABI.
#[default]
Function,
}

impl From<&str> for Type {
fn from(s: &str) -> Self {
match s {
"constructor" => Type::Constructor,
_ => Type::Function,
}
}
}

impl FromStr for Type {
type Err = Infallible;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(s))
}
}

impl AsRef<str> for Type {
fn as_ref(&self) -> &str {
match self {
Expand Down
15 changes: 13 additions & 2 deletions evm/abi/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Input of solidity ABI.
use core::{convert::Infallible, str::FromStr};

/// Input of solidity ABI.
#[derive(Debug, Clone)]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Input {
/// Name of the input.
Expand All @@ -11,7 +13,7 @@ pub struct Input {
}

/// The canonical type of the parameter.
#[derive(Debug, Clone)]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Param {
/// A 32-bit integer.
Expand All @@ -23,6 +25,7 @@ pub enum Param {
/// A 64-bit unsigned integer.
UInt64,
/// An unknown type.
#[default]
Unknown,
}

Expand All @@ -38,6 +41,14 @@ impl From<&str> for Param {
}
}

impl FromStr for Param {
type Err = Infallible;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(s))
}
}

impl AsRef<str> for Param {
fn as_ref(&self) -> &str {
match self {
Expand Down

0 comments on commit b622922

Please sign in to comment.