Skip to content

Commit

Permalink
[WIP] Implement codegen for yul
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Mar 10, 2022
1 parent fcab730 commit aaf633f
Show file tree
Hide file tree
Showing 14 changed files with 509 additions and 41 deletions.
1 change: 1 addition & 0 deletions crates/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["The Fe Developers <[email protected]>"]
edition = "2021"

[dependencies]
fe-analyzer = { path = "../analyzer", 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" }
Expand Down
51 changes: 49 additions & 2 deletions crates/codegen/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::rc::Rc;

use fe_common::db::{Upcast, UpcastMut};
use fe_analyzer::{db::AnalyzerDbStorage, AnalyzerDb};
use fe_common::db::{SourceDb, SourceDbStorage, Upcast, UpcastMut};
use fe_mir::{
db::MirDb,
db::{MirDb, MirDbStorage},
ir::{FunctionBody, FunctionId, FunctionSignature, TypeId},
};
use fe_new_abi::{function::AbiFunction, types::AbiType};
Expand All @@ -15,9 +16,55 @@ pub trait CodegenDb: MirDb + Upcast<dyn MirDb> + UpcastMut<dyn MirDb> {
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::function::lower_function)]
fn codegen_lower_function(&self, function_id: FunctionId) -> Rc<String>;

#[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>;
}

// TODO: Move this to driver.
#[salsa::database(SourceDbStorage, AnalyzerDbStorage, MirDbStorage, CodegenDbStorage)]
#[derive(Default)]
pub struct NewDb {
storage: salsa::Storage<NewDb>,
}
impl salsa::Database for NewDb {}

impl Upcast<dyn MirDb> for NewDb {
fn upcast(&self) -> &(dyn MirDb + 'static) {
&*self
}
}

impl UpcastMut<dyn MirDb> for NewDb {
fn upcast_mut(&mut self) -> &mut (dyn MirDb + 'static) {
&mut *self
}
}

impl Upcast<dyn SourceDb> for NewDb {
fn upcast(&self) -> &(dyn SourceDb + 'static) {
&*self
}
}

impl UpcastMut<dyn SourceDb> for NewDb {
fn upcast_mut(&mut self) -> &mut (dyn SourceDb + 'static) {
&mut *self
}
}

impl Upcast<dyn AnalyzerDb> for NewDb {
fn upcast(&self) -> &(dyn AnalyzerDb + 'static) {
&*self
}
}

impl UpcastMut<dyn AnalyzerDb> for NewDb {
fn upcast_mut(&mut self) -> &mut (dyn AnalyzerDb + 'static) {
&mut *self
}
}
9 changes: 8 additions & 1 deletion crates/codegen/src/db/queries/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::rc::Rc;

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

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

pub fn legalized_signature(db: &dyn CodegenDb, function: FunctionId) -> Rc<FunctionSignature> {
let mut sig = function.signature(db.upcast()).as_ref().clone();
Expand All @@ -15,3 +18,7 @@ pub fn legalized_body(db: &dyn CodegenDb, function: FunctionId) -> Rc<FunctionBo
legalize::legalize_func_body(db, &mut body);
body.into()
}

pub fn lower_function(db: &dyn CodegenDb, function: FunctionId) -> Rc<String> {
isel::lower_function(db, function).into()
}
Loading

0 comments on commit aaf633f

Please sign in to comment.