Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Dec 10, 2022
1 parent dc11878 commit f99be88
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 128 deletions.
4 changes: 2 additions & 2 deletions crates/analyzer/src/traversal/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1698,13 +1698,13 @@ fn expr_call_method(
vec!["Hint: rename one of the methods to disambiguate".into()],
);
let return_type = first.signature(context.db()).return_type.clone()?;
return Ok((
Ok((
ExpressionAttributes::new(return_type),
CallType::ValueMethod {
typ: obj_type,
sig: *first,
},
));
))
}
}
}
Expand Down
34 changes: 27 additions & 7 deletions crates/codegen/src/cgu.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,63 @@
//! This module contains definitions for Codegen Units (CGUs) which are used to
//! emit sonatina/yul ir.

use std::hash::{Hash, Hasher};
use std::{
hash::{Hash, Hasher},
rc::Rc,
};

use fe_analyzer::namespace::items::{ContractId, ModuleId};
use fe_common::impl_intern_key;
use fe_mir::ir::{FunctionBody, FunctionSigId};
use indexmap::IndexSet;
use indexmap::IndexMap;

use crate::db::CodegenDb;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodegenUnit {
pub module: ModuleId,

/// All contracts in the CGU.
pub contracts: Vec<ContractId>,

/// All functions in the CGU.
pub functions: Vec<CguFunction>,
pub functions: IndexMap<FunctionSigId, CguFunction>,
}

impl CodegenUnit {
pub fn new(module: ModuleId) -> Self {
Self {
module,
contracts: Vec::new(),
functions: Vec::new(),
functions: IndexMap::default(),
}
}
}

impl Hash for CodegenUnit {
fn hash<H: Hasher>(&self, state: &mut H) {
self.module.hash(state);
self.contracts.hash(state);
self.functions.iter().for_each(|(sig, func)| {
sig.hash(state);
func.hash(state);
});
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CodegenUnitId(pub u32);
impl_intern_key!(CodegenUnitId);

impl CodegenUnitId {
pub fn data(self, db: &dyn CodegenDb) -> Rc<CodegenUnit> {
db.lookup_codegen_intern_cgu(self)
}
}

#[derive(Debug, Clone)]
pub struct CguFunction {
pub sig: FunctionSigId,
pub body: FunctionBody,
pub callees: IndexSet<FunctionSigId>,
}

impl PartialEq for CguFunction {
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/src/db/queries/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn abi_function(db: &dyn CodegenDb, function: FunctionSigId) -> AbiFunction

// The "stateMutability" field is derived from the presence & mutability of
// `self` and `ctx` params in the analyzer fn sig.
let analyzer_sig = sig.analyzer_func_id.signature(db.upcast());
let analyzer_sig = sig.analyzer_id.signature(db.upcast());
let self_param = match analyzer_sig.self_decl {
None => SelfParam::None,
Some(SelfDecl { mut_: None, .. }) => SelfParam::Imm,
Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/src/db/queries/cgu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn generate_cgu(db: &dyn CodegenDb, ingot: IngotId) -> Rc<FxHashMap<ModuleId
.entry(module)
.or_insert_with(|| CodegenUnit::new(module))
.functions
.push(cgu_func_id);
.insert(sig, cgu_func_id);
}

Rc::new(
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<'db> CguFunctionBuilder<'db> {
}
}
}
let cgu_func = CguFunction { sig, body, callees };
let cgu_func = CguFunction { sig, body };
(cgu_func, mono_funcs)
}

Expand Down
10 changes: 2 additions & 8 deletions crates/codegen/src/db/queries/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::rc::Rc;
use fe_analyzer::{
display::Displayable,
namespace::{
items::{FunctionId, Item},
items::Item,
types::{Type, TypeId},
},
};
use fe_mir::ir::{FunctionBody, FunctionSigId, FunctionSignature};
use fe_mir::ir::{FunctionSigId, FunctionSignature};
use salsa::InternKey;
use smol_str::SmolStr;

Expand All @@ -19,12 +19,6 @@ pub fn legalized_signature(db: &dyn CodegenDb, function: FunctionSigId) -> Rc<Fu
sig.into()
}

pub fn legalized_body(db: &dyn CodegenDb, func: FunctionId) -> Rc<FunctionBody> {
let mut body = (*db.mir_lowered_func_body(func)).clone();
legalize::legalize_func_body(db, &mut body);
body.into()
}

pub fn symbol_name(db: &dyn CodegenDb, function: FunctionSigId) -> Rc<String> {
let module = function.data(db.upcast()).module_id;
let module_name = module.name(db.upcast());
Expand Down
18 changes: 10 additions & 8 deletions crates/codegen/src/yul/isel/cgu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use crate::{
yul::runtime::{DefaultRuntimeProvider, RuntimeProvider},
};

use super::lower_function;
use super::function::lower_function;

pub(super) fn compile_cgu(db: &dyn CodegenDb, cgu: CodegenUnit) -> CompiledCgu {
let mut runtime_func_deps = IndexMap::default();
let mut functions = IndexMap::with_capacity(cgu.functions.len());
let mut runtime_funcs = IndexMap::default();
let mut constants = IndexSet::default();

for func in cgu.functions {
for func in cgu.functions.into_values() {
let mut ctx = CguLowerContext::default();
let sig = func.sig;
let yul_func = lower_function(db, &mut ctx, func);
Expand All @@ -26,8 +26,10 @@ pub(super) fn compile_cgu(db: &dyn CodegenDb, cgu: CodegenUnit) -> CompiledCgu {
.or_insert_with(Vec::new)
.push(runtime_func.name.identifier.clone());
runtime_funcs.insert(runtime_func.name.identifier.clone(), runtime_func);
constants.extend(ctx.string_constants);
}

constants.extend(ctx.string_constants.into_iter());

functions.insert(sig, yul_func);
}

Expand All @@ -40,11 +42,11 @@ pub(super) fn compile_cgu(db: &dyn CodegenDb, cgu: CodegenUnit) -> CompiledCgu {
}

#[derive(Default)]
pub(super) struct CompiledCgu {
functions: IndexMap<FunctionSigId, yul::FunctionDefinition>,
constants: IndexSet<yul::Data>,
runtime_funcs: IndexMap<String, yul::FunctionDefinition>,
runtime_func_deps: IndexMap<FunctionSigId, Vec<String>>,
pub struct CompiledCgu {
pub functions: IndexMap<FunctionSigId, yul::FunctionDefinition>,
pub constants: IndexSet<yul::Data>,
pub runtime_funcs: IndexMap<String, yul::FunctionDefinition>,
pub runtime_func_deps: IndexMap<FunctionSigId, Vec<String>>,
}

pub(super) struct CguLowerContext {
Expand Down
Loading

0 comments on commit f99be88

Please sign in to comment.