Skip to content

Commit

Permalink
Implement InstSerializer for yul codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Mar 10, 2022
1 parent 1a7009c commit fcab730
Show file tree
Hide file tree
Showing 29 changed files with 841 additions and 122 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

Empty file added crates/bar.dot
Empty file.
8 changes: 7 additions & 1 deletion crates/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ authors = ["The Fe Developers <[email protected]>"]
edition = "2021"

[dependencies]
fe-mir = { path = "../mir", version = "^0.14.0-alpha" }
fe-mir = { path = "../mir", version = "^0.14.0-alpha" }
fe-common = { path = "../common", version = "^0.14.0-alpha" }
fe-new_abi = { path = "../new_abi", version = "^0.14.0-alpha" }
salsa = "0.16.1"
fxhash = "0.2.1"
smol_str = "0.1.21"
yultsur = { git = "https://github.com/g-r-a-n-t/yultsur", rev = "ae85470" }
23 changes: 23 additions & 0 deletions crates/codegen/src/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::rc::Rc;

use fe_common::db::{Upcast, UpcastMut};
use fe_mir::{
db::MirDb,
ir::{FunctionBody, FunctionId, FunctionSignature, TypeId},
};
use fe_new_abi::{function::AbiFunction, types::AbiType};

mod queries;

#[salsa::query_group(CodegenDbStorage)]
pub trait CodegenDb: MirDb + Upcast<dyn MirDb> + UpcastMut<dyn MirDb> {
#[salsa::invoke(queries::function::legalized_signature)]
fn codegen_legalized_signature(&self, function_id: FunctionId) -> Rc<FunctionSignature>;
#[salsa::invoke(queries::function::legalized_body)]
fn codegen_legalized_body(&self, function_id: FunctionId) -> Rc<FunctionBody>;

#[salsa::invoke(queries::abi::abi_type)]
fn codegen_abi_type(&self, ty: TypeId) -> AbiType;
#[salsa::invoke(queries::abi::abi_function)]
fn codegen_abi_function(&self, function_id: FunctionId) -> Rc<AbiFunction>;
}
2 changes: 2 additions & 0 deletions crates/codegen/src/db/queries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod abi;
pub mod function;
85 changes: 85 additions & 0 deletions crates/codegen/src/db/queries/abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::rc::Rc;

use fe_mir::ir::{self, FunctionId, TypeId};
use fe_new_abi::{
function::{AbiFunction, AbiFunctionType},
types::{AbiTupleField, AbiType},
};

use crate::db::CodegenDb;

pub fn abi_function(db: &dyn CodegenDb, function: FunctionId) -> Rc<AbiFunction> {
// We use a legalized signature.
let sig = db.codegen_legalized_signature(function);

let name = function.name(db.upcast());
let args = sig
.params
.iter()
.map(|param| (param.name.to_string(), db.codegen_abi_type(param.ty)))
.collect();
let ret_ty = sig.return_type.map(|ty| db.codegen_abi_type(ty));

AbiFunction::new(AbiFunctionType::Function, name.to_string(), args, ret_ty).into()
}

pub fn abi_type(db: &dyn CodegenDb, ty: TypeId) -> AbiType {
if ty.is_zero_sized(db.upcast()) {
unreachable!("zero-sized type must be removed in legalization");
}

let ty = ty.data(db.upcast());

match ty.as_ref() {
ir::Type::I8 => AbiType::Int(8),
ir::Type::I16 => AbiType::Int(16),
ir::Type::I32 => AbiType::Int(32),
ir::Type::I64 => AbiType::Int(64),
ir::Type::I128 => AbiType::Int(128),
ir::Type::I256 => AbiType::Int(256),
ir::Type::U8 => AbiType::UInt(8),
ir::Type::U16 => AbiType::UInt(16),
ir::Type::U32 => AbiType::UInt(32),
ir::Type::U64 => AbiType::UInt(64),
ir::Type::U128 => AbiType::UInt(128),
ir::Type::U256 => AbiType::UInt(256),
ir::Type::Bool => AbiType::Bool,
ir::Type::Address => AbiType::Address,
ir::Type::Unit => unreachable!("zero-sized type must be removed in legalization"),
ir::Type::Array(def) => {
let elem_ty = db.codegen_abi_type(def.elem_ty);
let len = def.len;
AbiType::Array {
elem_ty: elem_ty.into(),
len,
}
}
ir::Type::Tuple(def) => {
let fields = def
.items
.iter()
.enumerate()
.map(|(i, item)| {
let field_ty = db.codegen_abi_type(*item);
AbiTupleField::new(format!("{}", i), field_ty)
})
.collect();

AbiType::Tuple(fields)
}
ir::Type::Struct(def) => {
let fields = def
.fields
.iter()
.map(|(name, ty)| {
let ty = db.codegen_abi_type(*ty);
AbiTupleField::new(name.to_string(), ty)
})
.collect();

AbiType::Tuple(fields)
}
ir::Type::Event(_) | ir::Type::Contract(_) => unreachable!(),
ir::Type::Map(_) => todo!("map type can't be used in parameter or return type"),
}
}
17 changes: 17 additions & 0 deletions crates/codegen/src/db/queries/function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::rc::Rc;

use fe_mir::ir::{FunctionBody, FunctionId, FunctionSignature};

use crate::{db::CodegenDb, yul::legalize};

pub fn legalized_signature(db: &dyn CodegenDb, function: FunctionId) -> Rc<FunctionSignature> {
let mut sig = function.signature(db.upcast()).as_ref().clone();
legalize::legalize_func_signature(db, &mut sig);
sig.into()
}

pub fn legalized_body(db: &dyn CodegenDb, function: FunctionId) -> Rc<FunctionBody> {
let mut body = function.body(db.upcast()).as_ref().clone();
legalize::legalize_func_body(db, &mut body);
body.into()
}
1 change: 1 addition & 0 deletions crates/codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod db;
pub mod yul;
Loading

0 comments on commit fcab730

Please sign in to comment.