Skip to content

Commit

Permalink
refactor(codegen): nest code generators in module codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Dec 23, 2023
1 parent df58092 commit 95b6990
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 46 deletions.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Contract constructor.
use crate::{Buffer, CodeGen, Function, JumpTable, MacroAssembler, Result, ToLSBytes};
use crate::{wasm, Buffer, Function, JumpTable, MacroAssembler, Result, ToLSBytes};

/// Contract constructor.
///
Expand All @@ -25,10 +25,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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Code generator for EVM dispatcher.
use crate::{
code::ExtFunc, Data, Error, Exports, Function, Functions, Imports, JumpTable, MacroAssembler,
Result, ToLSBytes,
codegen::code::ExtFunc, wasm, Data, Error, Exports, Functions, Imports, JumpTable,
MacroAssembler, Result, ToLSBytes,
};
use wasmparser::{FuncType, Operator};
use zabi::Abi;
Expand Down Expand Up @@ -71,7 +71,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,7 +182,7 @@ 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)
Expand Down
6 changes: 3 additions & 3 deletions codegen/src/codegen.rs → codegen/src/codegen/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
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.
Expand All @@ -32,7 +32,7 @@ pub struct CodeGen {
pub(crate) is_main: bool,
}

impl CodeGen {
impl Function {
/// Create a new code generator.
pub fn new(env: FuncType, dataset: Data, imports: Imports, is_main: bool) -> Result<Self> {
let mut params_count = 0;
Expand Down Expand Up @@ -109,7 +109,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 func;

pub use self::{
code::{Code, ExtFunc},
constructor::Constructor,
dispatcher::Dispatcher,
func::Function,
};
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
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
12 changes: 3 additions & 9 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@

pub use crate::{
asm::Assembler,
code::Code,
codegen::CodeGen,
constructor::Constructor,
codegen::{Code, Constructor, Dispatcher, Function},
control::{ControlStack, ControlStackFrame, ControlStackFrameType},
dispatcher::Dispatcher,
jump::JumpTable,
local::{LocalSlot, Locals},
masm::MacroAssembler,
result::{Error, Result},
wasm::{Data, Exports, Function, Functions, HostFunc, Imports, ToLSBytes, Type},
wasm::{Data, Exports, Functions, HostFunc, Imports, ToLSBytes, Type},
};
use smallvec::SmallVec;

mod asm;
mod backtrace;
mod code;
mod codegen;
mod constructor;
mod control;
mod dispatcher;
mod jump;
mod local;
mod masm;
mod result;
mod validator;
mod visitor;
mod wasm;
pub mod wasm;

/// Maximum size of a evm bytecode in bytes.
pub const BUFFER_LIMIT: usize = 0x6000;
Expand Down
8 changes: 3 additions & 5 deletions codegen/src/validator.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! Pre-visitor for parsing WASM.
use crate::Result;
use crate::{Function, Result};
use wasmparser::{Operator, VisitOperator};

use crate::CodeGen;

/// A pre-visitor that validates the WASM and then visits it.
pub struct ValidateThenVisit<'a, T>(pub T, pub &'a mut CodeGen);
pub struct ValidateThenVisit<'a, T>(pub T, pub &'a mut Function);

macro_rules! validate_then_visit {
($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident)*) => {
Expand Down Expand Up @@ -40,7 +38,7 @@ trait ReachableState {
fn is_reachable(&self) -> bool;
}

impl ReachableState for CodeGen {
impl ReachableState for Function {
fn is_reachable(&self) -> bool {
true
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/visitor/call.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! call instructions
use crate::{wasm::HostFunc, CodeGen, Error, Result};
use crate::{wasm::HostFunc, Error, Function, Result};

impl CodeGen {
impl Function {
/// The call indirect instruction calls a function indirectly
/// through an operand indexing into a table.
pub fn _call_indirect(
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/visitor/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
use crate::{
control::{ControlStackFrame, ControlStackFrameType},
CodeGen, Result,
Function, Result,
};
use wasmparser::{BlockType, BrTable};

impl CodeGen {
impl Function {
/// The beginning of an if construct with an implicit block.
pub fn _if(&mut self, blockty: BlockType) -> Result<()> {
// Emit iszero to check the condition.
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/visitor/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Case handlers
use crate::{CodeGen, ControlStackFrame, ControlStackFrameType, Result};
use crate::{ControlStackFrame, ControlStackFrameType, Function, Result};

impl CodeGen {
impl Function {
/// Handle the end of the function.
pub(crate) fn handle_return(&mut self) -> Result<()> {
let results = self.env.results();
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/visitor/local.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Local instructions
use crate::{CodeGen, Error, Result};
use crate::{Error, Function, Result};

impl CodeGen {
impl Function {
/// This instruction gets the value of a variable.
pub fn _local_get(&mut self, local_index: u32) -> Result<()> {
let local_index = local_index as usize;
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/visitor/log.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! System instructions
use crate::{masm::MemoryInfo, CodeGen, Error, Result, ToLSBytes};
use crate::{masm::MemoryInfo, Error, Function, Result, ToLSBytes};

impl CodeGen {
impl Function {
/// Parse log data from the bytecode.
fn log_data(&mut self) -> Result<(i32, i32)> {
let buffer: Vec<u8> = self.masm.buffer().into();
Expand Down
6 changes: 3 additions & 3 deletions codegen/src/visitor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! This module is the central place for machine code emission.
//!
//! It defines an implementation of wasmparser's Visitor trait for
//! `CodeGen`; which defines a visitor per op-code, which validates
//! `Function`; which defines a visitor per op-code, which validates
//! and dispatches to the corresponding machine code emitter.
use crate::{CodeGen, Result};
use crate::{Function, Result};
use paste::paste;
use tracing::trace;
use wasmparser::{for_each_operator, BlockType, BrTable, Ieee32, Ieee64, MemArg, VisitOperator};
Expand Down Expand Up @@ -189,7 +189,7 @@ macro_rules! map_wasm_operators {
};
}

impl<'a> VisitOperator<'a> for CodeGen {
impl<'a> VisitOperator<'a> for Function {
type Output = Result<()>;

for_each_operator!(impl_visit_operator);
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{parser::Parser, Config, Error, Result};
use zabi::Abi;
use zingen::{
Buffer, CodeGen, Constructor, Data, Dispatcher, Function, Imports, JumpTable, BUFFER_LIMIT,
wasm, Buffer, Constructor, Data, Dispatcher, Function, Imports, JumpTable, BUFFER_LIMIT,
};

/// Zink Compiler
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Compiler {
&mut self,
dataset: Data,
imports: Imports,
mut func: Function<'_>,
mut func: wasm::Function<'_>,
) -> Result<()> {
let func_index = func.index();
let sig = func.sig()?;
Expand All @@ -94,7 +94,7 @@ impl Compiler {
func_index - (imports.len() as u32) == 0
};

let mut codegen = CodeGen::new(sig, dataset, imports, is_main)?;
let mut codegen = Function::new(sig, dataset, imports, is_main)?;
let mut locals_reader = func.body.get_locals_reader()?;
let mut ops_reader = func.body.get_operators_reader()?;

Expand All @@ -106,7 +106,7 @@ impl Compiler {
}

/// Emit buffer to the inner buffer.
fn emit_buffer(&mut self, func_index: u32, codegen: CodeGen) -> Result<()> {
fn emit_buffer(&mut self, func_index: u32, codegen: Function) -> Result<()> {
let buffer = codegen.finish(&mut self.table, self.buffer.len() as u16)?;
self.table
.call_offset(func_index, self.buffer.len() as u16)?;
Expand All @@ -125,7 +125,7 @@ impl Compiler {
}

/// Returns bytecode.
fn bytecode(&self, constructor: Option<Function<'_>>) -> Result<Buffer> {
fn bytecode(&self, constructor: Option<wasm::Function<'_>>) -> Result<Buffer> {
Constructor::new(constructor, self.buffer.clone())?
.finish()
.map_err(Into::into)
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wasmparser::{
Data, DataKind, Export, ExternalKind, Import, Operator, Payload, SectionLimited, TypeRef,
ValidPayload, Validator,
};
use zingen::{Data as DataSet, Exports, Function, Functions, HostFunc, Imports};
use zingen::{wasm, Data as DataSet, Exports, Functions, HostFunc, Imports};

/// WASM module parser
#[derive(Default)]
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'p> Parser<'p> {
}

/// Returns constructor if some.
pub fn remove_constructor(&mut self) -> Option<Function<'p>> {
pub fn remove_constructor(&mut self) -> Option<wasm::Function<'p>> {
self.funcs.remove_constructor(&self.exports)
}
}
Expand Down

0 comments on commit 95b6990

Please sign in to comment.