Skip to content

Commit

Permalink
Add runtime dependency graph
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Dec 10, 2022
1 parent 773add4 commit a239f2a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ salsa = "0.16.1"
num-bigint = "0.4.3"
fxhash = "0.2.1"
indexmap = "1.6.2"
petgraph = "0.6.0"
smol_str = "0.1.21"
yultsur = { git = "https://github.com/g-r-a-n-t/yultsur", rev = "ae85470" }
yultsur = { git = "https://github.com/g-r-a-n-t/yultsur", rev = "ae85470" }
42 changes: 34 additions & 8 deletions crates/codegen/src/yul/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@ use std::fmt::Write;
use fe_abi::types::AbiType;
use fe_analyzer::namespace::items::ContractId;
use fe_mir::ir::{types::ArrayDef, FunctionSigId, TypeId, TypeKind};
use fxhash::FxHashMap;
use indexmap::IndexMap;
use yultsur::*;

use num_bigint::BigInt;
use petgraph::graph::{DiGraph, NodeIndex};
use yultsur::*;

use crate::{db::CodegenDb, yul::slot_size::SLOT_SIZE};

use super::slot_size::yul_primitive_type;

#[derive(Debug, Default)]
pub struct RuntimeFuncDepGraph {
graph: DiGraph<String, ()>,
node_map: FxHashMap<String, NodeIndex>,
}

pub trait RuntimeProvider {
fn collect_definitions(&self) -> Vec<yul::FunctionDefinition>;
fn collect(self) -> (Vec<yul::FunctionDefinition>, RuntimeFuncDepGraph);

fn alloc(&mut self, db: &dyn CodegenDb, size: yul::Expression) -> yul::Expression;

Expand Down Expand Up @@ -226,6 +233,9 @@ pub enum AbiSrcLocation {
#[derive(Debug, Default)]
pub struct DefaultRuntimeProvider {
functions: IndexMap<String, RuntimeFunction>,
dep_graph: RuntimeFuncDepGraph,
cur_node: Option<NodeIndex>,
node_map: FxHashMap<String, NodeIndex>,
}

impl DefaultRuntimeProvider {
Expand All @@ -238,23 +248,39 @@ impl DefaultRuntimeProvider {
where
F: FnOnce(&mut Self) -> RuntimeFunction,
{
let node_idx = *self
.node_map
.entry(name.to_string())
.or_insert_with(|| self.dep_graph.graph.add_node(name.to_string()));
if let Some(cur_node) = self.cur_node {
self.dep_graph.graph.update_edge(cur_node, node_idx, ());
}

if let Some(func) = self.functions.get(name) {
func.call(args)
} else {
let pred_node = self.cur_node;
self.cur_node = Some(node_idx);

let func = func_builder(self);
let result = func.call(args);

self.functions.insert(name.to_string(), func);
self.cur_node = pred_node;
result
}
}
}

impl RuntimeProvider for DefaultRuntimeProvider {
fn collect_definitions(&self) -> Vec<yul::FunctionDefinition> {
self.functions
.values()
.map(RuntimeFunction::definition)
.collect()
fn collect(self) -> (Vec<yul::FunctionDefinition>, RuntimeFuncDepGraph) {
(
self.functions
.values()
.map(RuntimeFunction::definition)
.collect(),
self.dep_graph,
)
}

fn alloc(&mut self, _db: &dyn CodegenDb, bytes: yul::Expression) -> yul::Expression {
Expand Down

0 comments on commit a239f2a

Please sign in to comment.