use std::fmt;
use crate::decompiler::cfg::BlockId;
use super::SsaVariable;
#[derive(Debug, Clone, PartialEq)]
pub struct PhiNode {
pub target: SsaVariable,
pub operands: std::collections::BTreeMap<BlockId, SsaVariable>,
}
impl PhiNode {
#[must_use]
pub const fn new(target: SsaVariable) -> Self {
Self {
target,
operands: std::collections::BTreeMap::new(),
}
}
pub fn add_operand(&mut self, predecessor: BlockId, var: SsaVariable) {
self.operands.insert(predecessor, var);
}
#[must_use]
pub fn operand_count(&self) -> usize {
self.operands.len()
}
}
impl fmt::Display for PhiNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} = φ(", self.target)?;
let mut first = true;
for (block, var) in &self.operands {
if !first {
write!(f, ", ")?;
}
write!(f, "{:?}: {}", block, var)?;
first = false;
}
write!(f, ")")
}
}