use std::collections::{BTreeMap, BTreeSet};
use crate::instruction::Instruction;
use super::graph::Cfg;
mod blocks;
mod edges;
mod leaders;
mod offsets;
mod targets;
mod terminator;
pub struct CfgBuilder<'a> {
instructions: &'a [Instruction],
offset_to_index: BTreeMap<usize, usize>,
leaders: BTreeSet<usize>,
}
impl<'a> CfgBuilder<'a> {
#[must_use]
pub fn new(instructions: &'a [Instruction]) -> Self {
let mut offset_to_index = BTreeMap::new();
for (i, instr) in instructions.iter().enumerate() {
offset_to_index.insert(instr.offset, i);
}
Self {
instructions,
offset_to_index,
leaders: BTreeSet::new(),
}
}
#[must_use]
pub fn build(mut self) -> Cfg {
if self.instructions.is_empty() {
return Cfg::new();
}
self.find_leaders();
let blocks = self.create_blocks();
self.build_cfg(blocks)
}
}