Skip to content

Commit

Permalink
chore(ssa refactor): Implement function calls (#1235)
Browse files Browse the repository at this point in the history
* Implement ssa-gen for if

* Satisfy the clippy gods

* Fix printing bug

* Print constants directly

* Impl for loops

* Implement immutable and mutable variables

* Implement function calls
  • Loading branch information
jfecher authored Apr 27, 2023
1 parent 62dcc5c commit 64cf49d
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 287 deletions.
8 changes: 5 additions & 3 deletions crates/noirc_evaluator/src/ssa_refactor/ir/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ impl ControlFlowGraph {

#[cfg(test)]
mod tests {
use crate::ssa_refactor::ir::{instruction::TerminatorInstruction, types::Type};
use crate::ssa_refactor::ir::{instruction::TerminatorInstruction, map::Id, types::Type};

use super::{super::function::Function, ControlFlowGraph};

#[test]
fn empty() {
let mut func = Function::new("func".into());
let func_id = Id::test_new(0);
let mut func = Function::new("func".into(), func_id);
let block_id = func.entry_block();
func.dfg[block_id].set_terminator(TerminatorInstruction::Return { return_values: vec![] });

Expand All @@ -139,7 +140,8 @@ mod tests {
// block2():
// return ()
// }
let mut func = Function::new("func".into());
let func_id = Id::test_new(0);
let mut func = Function::new("func".into(), func_id);
let block0_id = func.entry_block();
let cond = func.dfg.add_block_parameter(block0_id, Type::unsigned(1));
let block1_id = func.dfg.make_block();
Expand Down
18 changes: 14 additions & 4 deletions crates/noirc_evaluator/src/ssa_refactor/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ pub(crate) struct Function {
source_locations: SecondaryMap<Instruction, Location>,

/// The first basic block in the function
pub(super) entry_block: BasicBlockId,
entry_block: BasicBlockId,

/// Name of the function for debugging only
pub(super) name: String,
name: String,

id: FunctionId,

pub(crate) dfg: DataFlowGraph,
}
Expand All @@ -30,10 +32,18 @@ impl Function {
/// Creates a new function with an automatically inserted entry block.
///
/// Note that any parameters to the function must be manually added later.
pub(crate) fn new(name: String) -> Self {
pub(crate) fn new(name: String, id: FunctionId) -> Self {
let mut dfg = DataFlowGraph::default();
let entry_block = dfg.make_block();
Self { name, source_locations: SecondaryMap::new(), entry_block, dfg }
Self { name, source_locations: SecondaryMap::new(), id, entry_block, dfg }
}

pub(crate) fn name(&self) -> &str {
&self.name
}

pub(crate) fn id(&self) -> FunctionId {
self.id
}

pub(crate) fn entry_block(&self) -> BasicBlockId {
Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_evaluator/src/ssa_refactor/ir/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use super::{
};

pub(crate) fn display_function(function: &Function, f: &mut Formatter) -> Result {
writeln!(f, "fn {} {{", function.name)?;
display_block_with_successors(function, function.entry_block, &mut HashSet::new(), f)?;
writeln!(f, "fn {} {} {{", function.name(), function.id())?;
display_block_with_successors(function, function.entry_block(), &mut HashSet::new(), f)?;
write!(f, "}}")
}

Expand Down

This file was deleted.

Loading

0 comments on commit 64cf49d

Please sign in to comment.