mod block_sort;
use crate::{Block, BlockData, IRType, Value, Variable};
use std::{
collections::{BTreeMap, HashMap},
fmt::Display,
};
#[derive(Default, Clone)]
pub struct Function {
pub(crate) blocks: BTreeMap<Block, BlockData>,
pub(crate) entry_block: Option<Block>,
pub(crate) values: HashMap<Value, IRType>,
pub(crate) variables: HashMap<Variable, IRType>,
}
impl Function {
pub fn get_blocks(&self) -> Vec<Block> {
self.blocks.keys().copied().collect()
}
pub fn get_block(&self, block: &Block) -> Option<&BlockData> {
self.blocks.get(block)
}
pub fn get_block_mut(&mut self, block: &Block) -> Option<&mut BlockData> {
self.blocks.get_mut(block)
}
pub fn entry_block(&self) -> Option<Block> {
self.entry_block
}
pub(crate) fn is_val_type(&self, val: Value, ty: IRType) -> bool {
self.get_val_type(val) == ty
}
pub fn get_val_type(&self, val: Value) -> IRType {
self.values[&val]
}
pub fn get_vars(&self) -> Vec<Variable> {
self.variables.keys().copied().collect()
}
pub fn get_vals(&self) -> Vec<Value> {
self.values.keys().copied().collect()
}
pub fn get_var_types(&self) -> &HashMap<Variable, IRType> {
&self.variables
}
pub fn get_val_types(&self) -> &HashMap<Value, IRType> {
&self.values
}
pub fn get_var_type(&self, var: Variable) -> IRType {
self.variables[&var]
}
}
impl Display for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (block, data) in self.blocks.iter() {
write!(f, "{}{}", block, data)?;
}
Ok(())
}
}