Skip to content

Commit d986137

Browse files
committed
Implement InstSerializer for yul codegen
1 parent af50f71 commit d986137

29 files changed

Lines changed: 841 additions & 122 deletions

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bar.dot

Whitespace-only changes.

crates/codegen/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ authors = ["The Fe Developers <snakecharmers@ethereum.org>"]
55
edition = "2021"
66

77
[dependencies]
8-
fe-mir = { path = "../mir", version = "^0.14.0-alpha" }
8+
fe-mir = { path = "../mir", version = "^0.14.0-alpha" }
9+
fe-common = { path = "../common", version = "^0.14.0-alpha" }
10+
fe-new_abi = { path = "../new_abi", version = "^0.14.0-alpha" }
11+
salsa = "0.16.1"
12+
fxhash = "0.2.1"
13+
smol_str = "0.1.21"
14+
yultsur = { git = "https://github.com/g-r-a-n-t/yultsur", rev = "ae85470" }

crates/codegen/src/db.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::rc::Rc;
2+
3+
use fe_common::db::{Upcast, UpcastMut};
4+
use fe_mir::{
5+
db::MirDb,
6+
ir::{FunctionBody, FunctionId, FunctionSignature, TypeId},
7+
};
8+
use fe_new_abi::{function::AbiFunction, types::AbiType};
9+
10+
mod queries;
11+
12+
#[salsa::query_group(CodegenDbStorage)]
13+
pub trait CodegenDb: MirDb + Upcast<dyn MirDb> + UpcastMut<dyn MirDb> {
14+
#[salsa::invoke(queries::function::legalized_signature)]
15+
fn codegen_legalized_signature(&self, function_id: FunctionId) -> Rc<FunctionSignature>;
16+
#[salsa::invoke(queries::function::legalized_body)]
17+
fn codegen_legalized_body(&self, function_id: FunctionId) -> Rc<FunctionBody>;
18+
19+
#[salsa::invoke(queries::abi::abi_type)]
20+
fn codegen_abi_type(&self, ty: TypeId) -> AbiType;
21+
#[salsa::invoke(queries::abi::abi_function)]
22+
fn codegen_abi_function(&self, function_id: FunctionId) -> Rc<AbiFunction>;
23+
}

crates/codegen/src/db/queries.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod abi;
2+
pub mod function;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use std::rc::Rc;
2+
3+
use fe_mir::ir::{self, FunctionId, TypeId};
4+
use fe_new_abi::{
5+
function::{AbiFunction, AbiFunctionType},
6+
types::{AbiTupleField, AbiType},
7+
};
8+
9+
use crate::db::CodegenDb;
10+
11+
pub fn abi_function(db: &dyn CodegenDb, function: FunctionId) -> Rc<AbiFunction> {
12+
// We use a legalized signature.
13+
let sig = db.codegen_legalized_signature(function);
14+
15+
let name = function.name(db.upcast());
16+
let args = sig
17+
.params
18+
.iter()
19+
.map(|param| (param.name.to_string(), db.codegen_abi_type(param.ty)))
20+
.collect();
21+
let ret_ty = sig.return_type.map(|ty| db.codegen_abi_type(ty));
22+
23+
AbiFunction::new(AbiFunctionType::Function, name.to_string(), args, ret_ty).into()
24+
}
25+
26+
pub fn abi_type(db: &dyn CodegenDb, ty: TypeId) -> AbiType {
27+
if ty.is_zero_sized(db.upcast()) {
28+
unreachable!("zero-sized type must be removed in legalization");
29+
}
30+
31+
let ty = ty.data(db.upcast());
32+
33+
match ty.as_ref() {
34+
ir::Type::I8 => AbiType::Int(8),
35+
ir::Type::I16 => AbiType::Int(16),
36+
ir::Type::I32 => AbiType::Int(32),
37+
ir::Type::I64 => AbiType::Int(64),
38+
ir::Type::I128 => AbiType::Int(128),
39+
ir::Type::I256 => AbiType::Int(256),
40+
ir::Type::U8 => AbiType::UInt(8),
41+
ir::Type::U16 => AbiType::UInt(16),
42+
ir::Type::U32 => AbiType::UInt(32),
43+
ir::Type::U64 => AbiType::UInt(64),
44+
ir::Type::U128 => AbiType::UInt(128),
45+
ir::Type::U256 => AbiType::UInt(256),
46+
ir::Type::Bool => AbiType::Bool,
47+
ir::Type::Address => AbiType::Address,
48+
ir::Type::Unit => unreachable!("zero-sized type must be removed in legalization"),
49+
ir::Type::Array(def) => {
50+
let elem_ty = db.codegen_abi_type(def.elem_ty);
51+
let len = def.len;
52+
AbiType::Array {
53+
elem_ty: elem_ty.into(),
54+
len,
55+
}
56+
}
57+
ir::Type::Tuple(def) => {
58+
let fields = def
59+
.items
60+
.iter()
61+
.enumerate()
62+
.map(|(i, item)| {
63+
let field_ty = db.codegen_abi_type(*item);
64+
AbiTupleField::new(format!("{}", i), field_ty)
65+
})
66+
.collect();
67+
68+
AbiType::Tuple(fields)
69+
}
70+
ir::Type::Struct(def) => {
71+
let fields = def
72+
.fields
73+
.iter()
74+
.map(|(name, ty)| {
75+
let ty = db.codegen_abi_type(*ty);
76+
AbiTupleField::new(name.to_string(), ty)
77+
})
78+
.collect();
79+
80+
AbiType::Tuple(fields)
81+
}
82+
ir::Type::Event(_) | ir::Type::Contract(_) => unreachable!(),
83+
ir::Type::Map(_) => todo!("map type can't be used in parameter or return type"),
84+
}
85+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::rc::Rc;
2+
3+
use fe_mir::ir::{FunctionBody, FunctionId, FunctionSignature};
4+
5+
use crate::{db::CodegenDb, yul::legalize};
6+
7+
pub fn legalized_signature(db: &dyn CodegenDb, function: FunctionId) -> Rc<FunctionSignature> {
8+
let mut sig = function.signature(db.upcast()).as_ref().clone();
9+
legalize::legalize_func_signature(db, &mut sig);
10+
sig.into()
11+
}
12+
13+
pub fn legalized_body(db: &dyn CodegenDb, function: FunctionId) -> Rc<FunctionBody> {
14+
let mut body = function.body(db.upcast()).as_ref().clone();
15+
legalize::legalize_func_body(db, &mut body);
16+
body.into()
17+
}

crates/codegen/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub mod db;
12
pub mod yul;

0 commit comments

Comments
 (0)