-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
266 additions
and
144 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,78 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::{ | ||
repr::{identifier::FunctionIdx, ir::Function}, | ||
util::symbol_map::interner_symbol_map::InternerSymbolMap, | ||
repr::{ | ||
ast::typed as ast, | ||
identifier::FunctionIdx, | ||
ir::{BasicBlockIdx, Function, Triple, TripleRef}, | ||
ty::Ty, | ||
}, | ||
util::symbol_map::interner_symbol_map::Symbol, | ||
}; | ||
|
||
#[derive(Default, Clone, Debug)] | ||
pub struct IRCtx { | ||
/// Map of function symbol to the basic block entry point | ||
pub functions: HashMap<FunctionIdx, Function>, | ||
pub symbol_map: InternerSymbolMap, | ||
pub trait IRCtx { | ||
type FunctionBuilder: FunctionBuilder; | ||
|
||
/// Register an IR function implementation against it's identifier. | ||
fn register_function(&mut self, idx: FunctionIdx, function: Function); | ||
|
||
/// Prepare and return a new builder for the provided AST function representation. | ||
fn new_builder(&self, function: &ast::Function) -> Self::FunctionBuilder { | ||
Self::FunctionBuilder::new(function) | ||
} | ||
|
||
// TODO: Probably get rid of this | ||
fn all_functions(&self) -> Vec<(FunctionIdx, Function)>; | ||
} | ||
|
||
#[cfg(test)] | ||
mockall::mock! { | ||
pub IRCtx {} | ||
|
||
impl IRCtx for IRCtx { | ||
type FunctionBuilder = MockFunctionBuilder; | ||
|
||
fn register_function(&mut self, idx: FunctionIdx, function: Function); | ||
fn new_builder(&self, function: &ast::Function) -> MockFunctionBuilder; | ||
fn all_functions(&self) -> Vec<(FunctionIdx, Function)>; | ||
} | ||
} | ||
|
||
impl IRCtx { | ||
pub fn new(symbol_map: InternerSymbolMap) -> Self { | ||
Self { | ||
symbol_map, | ||
..Default::default() | ||
} | ||
/// A stateful representation of a function that is being constructed. A function consists of basic | ||
/// blocks, and the builder is always 'located' at a basic block. | ||
pub trait FunctionBuilder { | ||
/// Initialise a new builder with the provided function, positioned at the entry point. | ||
fn new(function: &ast::Function) -> Self; | ||
|
||
/// Register the provided symbol with the given type into the function scope. | ||
fn register_scoped(&mut self, symbol: Symbol, ty: Ty); | ||
|
||
/// Add a triple to the current basic block. | ||
fn add_triple(&mut self, triple: Triple) -> TripleRef; | ||
|
||
/// Get the current basic block. | ||
fn current_bb(&self) -> BasicBlockIdx; | ||
|
||
/// Go to a specific basic block. | ||
fn goto_bb(&mut self, bb: BasicBlockIdx); | ||
|
||
/// Create a new basic block, switch to it, and return its identifier. | ||
fn push_bb(&mut self) -> BasicBlockIdx; | ||
|
||
/// Consume the builder, and register the built function against the context. | ||
fn build<Ctx: IRCtx<FunctionBuilder = Self>>(self, ctx: &mut Ctx); | ||
} | ||
|
||
#[cfg(test)] | ||
mockall::mock! { | ||
pub FunctionBuilder {} | ||
|
||
impl FunctionBuilder for FunctionBuilder { | ||
fn new(function: &ast::Function) -> Self; | ||
fn register_scoped(&mut self, symbol: Symbol, ty: Ty); | ||
fn add_triple(&mut self, triple: Triple) -> TripleRef; | ||
fn current_bb(&self) -> BasicBlockIdx; | ||
fn goto_bb(&mut self, bb: BasicBlockIdx) ; | ||
fn push_bb(&mut self) -> BasicBlockIdx; | ||
#[mockall::concretize] | ||
fn build<Ctx: IRCtx<FunctionBuilder = MockFunctionBuilder>>(self, ctx: &mut Ctx); | ||
} | ||
} |
Oops, something went wrong.