-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
InstSerializer
for yul codegen
- Loading branch information
Showing
29 changed files
with
841 additions
and
122 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.
Empty file.
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 |
---|---|---|
|
@@ -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" } |
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,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>; | ||
} |
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,2 @@ | ||
pub mod abi; | ||
pub mod function; |
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,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"), | ||
} | ||
} |
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,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() | ||
} |
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 +1,2 @@ | ||
pub mod db; | ||
pub mod yul; |
Oops, something went wrong.