Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(codegen): introduce module wasm #205

Merged
merged 6 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/install-conta/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ runs:
key: ${{ runner.os }}-conta

- name: Install Conta
shell: bash
run: cargo install conta
15 changes: 15 additions & 0 deletions .github/workflows/deps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Deps

on:
pull_request:
branches: [main]

jobs:
review:
name: Review
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Dependency Review
uses: actions/dependency-review-action@v3
10 changes: 0 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,3 @@ jobs:
run: cargo fmt --check
- name: Clippy
run: cargo clippy --all -- -D warnings

dep:
name: Review
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Dependency Review
uses: actions/dependency-review-action@v3
8 changes: 3 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ paste = "1.0.14"
postcard = { version = "1.0.8", default-features = false }
proc-macro2 = "1.0.71"
quote = "1.0.33"
revm = "3.5.0"
revm = { version = "3.5.0", default-features = false }
semver = "1.0.20"
serde = { version = "1.0.193", default-features = false }
serde_json = "1.0.108"
Expand Down
27 changes: 0 additions & 27 deletions codegen/src/code/func.rs

This file was deleted.

15 changes: 12 additions & 3 deletions codegen/src/code/mod.rs → codegen/src/codegen/code.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//! Table for the code section.

pub use func::ExtFunc;
use indexmap::IndexMap;

mod func;

/// Code section for EVM.
#[derive(Clone, Default, Debug)]
pub struct Code {
/// The offset of the code section
offset: usize,
/// Function table.
funcs: IndexMap<ExtFunc, usize>,
Expand Down Expand Up @@ -67,3 +65,14 @@ impl Code {
code
}
}

/// 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>,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Contract constructor.

use crate::{Buffer, CodeGen, Function, JumpTable, MacroAssembler, Result, ToLSBytes};
use crate::{
wasm::{self, ToLSBytes},
Buffer, Function, JumpTable, MacroAssembler, Result,
};

/// Contract constructor.
///
Expand All @@ -25,10 +28,10 @@ pub struct Constructor {

impl Constructor {
/// Create a new constructor.
pub fn new(constructor: Option<Function<'_>>, runtime_bytecode: Buffer) -> Result<Self> {
pub fn new(constructor: Option<wasm::Function<'_>>, runtime_bytecode: Buffer) -> Result<Self> {
let mut init_code = Buffer::new();
if let Some(constructor) = constructor {
let codegen = CodeGen::new(
let codegen = Function::new(
constructor.sig()?,
Default::default(),
Default::default(),
Expand Down
19 changes: 10 additions & 9 deletions codegen/src/dispatcher.rs → codegen/src/codegen/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Code generator for EVM dispatcher.

use crate::{
code::ExtFunc, DataSet, Error, Exports, Function, Functions, Imports, JumpTable,
MacroAssembler, Result, ToLSBytes,
codegen::code::ExtFunc,
wasm::{self, Data, Exports, Functions, Imports, ToLSBytes},
Error, JumpTable, MacroAssembler, Result,
};
use wasmparser::{FuncType, Operator};
use zabi::Abi;
Expand All @@ -18,12 +19,12 @@ pub struct Dispatcher<'d> {
/// Module imports
pub imports: Imports,
/// Module data
pub data: DataSet,
pub data: Data,
/// Jump table
pub table: JumpTable,
/// ABI for the current function
///
/// TODO: refactor this. (#192)
/// TODO: refactor this. (#206)
pub abi: Vec<Abi>,
}

Expand Down Expand Up @@ -54,15 +55,15 @@ impl<'d> Dispatcher<'d> {
}

/// Set data for the dispatcher.
pub fn data(&mut self, data: DataSet) -> &mut Self {
pub fn data(&mut self, data: Data) -> &mut Self {
self.data = data;
self
}

/// Query exported function from selector.
fn query_func(&self, name: &str) -> Result<u32> {
for (index, export) in self.exports.iter() {
if export.name == name {
if export == name {
return Ok(*index);
}
}
Expand All @@ -71,7 +72,7 @@ impl<'d> Dispatcher<'d> {
}

/// Load function ABI.
fn load_abi(&mut self, selector: &Function<'_>) -> Result<Abi> {
fn load_abi(&mut self, selector: &wasm::Function<'_>) -> Result<Abi> {
let mut reader = selector.body.get_operators_reader()?;

let Operator::I32Const { value: offset } = reader.read()? else {
Expand Down Expand Up @@ -182,10 +183,10 @@ impl<'d> Dispatcher<'d> {
}

/// Emit selector to buffer.
fn emit_selector(&mut self, selector: &Function<'_>, last: bool) -> Result<()> {
fn emit_selector(&mut self, selector: &wasm::Function<'_>, last: bool) -> Result<()> {
let abi = self.load_abi(selector)?;

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

let selector_bytes = abi.selector();
Expand Down
13 changes: 7 additions & 6 deletions codegen/src/codegen.rs → codegen/src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ use crate::{
local::{LocalSlot, LocalSlotType, Locals},
masm::MacroAssembler,
validator::ValidateThenVisit,
Buffer, DataSet, Error, Imports, Result,
wasm::{Data, Imports},
Buffer, Error, Result,
};
use wasmparser::{FuncType, FuncValidator, LocalsReader, OperatorsReader, ValidatorResources};

/// The code generation abstraction.
pub struct CodeGen {
pub struct Function {
/// The backtrace.
pub(crate) backtrace: Backtrace,
/// Control stack frames.
pub(crate) control: ControlStack,
/// Control stack frames.
pub(crate) dataset: DataSet,
pub(crate) dataset: Data,
/// The function environment.
pub(crate) env: FuncType,
/// The defined locals for a function.
Expand All @@ -32,9 +33,9 @@ pub struct CodeGen {
pub(crate) is_main: bool,
}

impl CodeGen {
impl Function {
/// Create a new code generator.
pub fn new(env: FuncType, dataset: DataSet, imports: Imports, is_main: bool) -> Result<Self> {
pub fn new(env: FuncType, dataset: Data, imports: Imports, is_main: bool) -> Result<Self> {
let mut params_count = 0;
if !is_main {
params_count = env.params().len() as u8;
Expand Down Expand Up @@ -109,7 +110,7 @@ impl CodeGen {
Ok(())
}

/// Emit function operators
/// Emit function operators.
pub fn emit_operators(
&mut self,
ops: &mut OperatorsReader<'_>,
Expand Down
18 changes: 18 additions & 0 deletions codegen/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Code generators
//!
//! - CONSTRUCTOR
//! - DISPATCHER
//! - FUNCTION
//! - CODE

mod code;
mod constructor;
mod dispatcher;
mod function;

pub use self::{
code::{Code, ExtFunc},
constructor::Constructor,
dispatcher::Dispatcher,
function::Function,
};
61 changes: 0 additions & 61 deletions codegen/src/export.rs

This file was deleted.

2 changes: 1 addition & 1 deletion codegen/src/jump/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Jump table implementation.

use crate::code::ExtFunc;
use crate::codegen::ExtFunc;
pub use table::JumpTable;

mod pc;
Expand Down
3 changes: 2 additions & 1 deletion codegen/src/jump/relocate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use crate::{
jump::{relocate, JumpTable},
Buffer, Error, Result, ToLSBytes, BUFFER_LIMIT,
wasm::ToLSBytes,
Buffer, Error, Result, BUFFER_LIMIT,
};
use opcodes::ShangHai as OpCode;

Expand Down
2 changes: 1 addition & 1 deletion codegen/src/jump/table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Jump Table

use crate::{code::ExtFunc, jump::Jump, Code, Error, Result};
use crate::{codegen::ExtFunc, jump::Jump, Code, Error, Result};
use std::collections::BTreeMap;

/// Jump table implementation.
Expand Down
Loading