tnj_air/instructions/value/
block.rs

1use crate::instructions::{Inst, Value};
2use arch::reg::Register;
3use macros::index_struct;
4use smol_str::SmolStr;
5use std::collections::HashMap;
6use sym::Expr;
7use types::Type;
8
9index_struct! {
10    /// References a basic block in the bb pool
11    BasicBlock(u32)
12}
13
14index_struct! {
15    /// References a block param in a basic block
16    BlockParam(u32)
17}
18
19/// A block parameter.
20#[derive(Debug, Clone, Eq, PartialEq, Hash)]
21pub struct BlockParamData {
22    /// The name of the block param
23    pub name: Option<SmolStr>,
24    /// The type of the block param
25    pub ty: Type,
26}
27
28impl BlockParamData {
29    /// Creates a new block param from a register
30    pub fn from_reg(reg: Register) -> Self {
31        Self {
32            name: Some(reg.name),
33            ty: reg.ty,
34        }
35    }
36}
37
38impl From<Type> for BlockParamData {
39    fn from(ty: Type) -> Self {
40        Self { name: None, ty }
41    }
42}
43
44/// The basic block
45#[derive(Default, Clone)]
46pub struct BasicBlockData {
47    /// The name of the block
48    pub(crate) name: String,
49    /// Block params
50    pub(crate) params: Vec<BlockParam>,
51    /// The ordered list of instructions
52    pub(crate) insts: Vec<Inst>,
53    /// Facts attached to the beginning of a block. Keys may be registers (in non-ssa form), block
54    /// params or instructions. They associate values with abstract values.
55    pub(crate) facts: HashMap<Value, Expr>,
56    /// The assertions attached to the beginning of a block.
57    pub(crate) assertions: Vec<Expr>,
58}
59
60impl BasicBlockData {
61    /// Create a new block with a given name
62    pub fn new<S: Into<String>>(name: S, params: Vec<BlockParam>) -> Self {
63        Self {
64            name: name.into(),
65            params,
66            insts: Vec::new(),
67            facts: HashMap::new(),
68            assertions: Vec::new(),
69        }
70    }
71
72    /// Look up a fact for a given value.
73    #[inline]
74    pub fn get_fact(&self, value: Value) -> Option<&Expr> {
75        self.facts.get(&value)
76    }
77
78    /// Get all assertions
79    #[inline]
80    pub fn assertions(&self) -> &[Expr] {
81        &self.assertions
82    }
83
84    /// Get all facts
85    #[inline]
86    pub fn facts(&self) -> impl Iterator<Item = (&Value, &Expr)> + '_ {
87        self.facts.iter()
88    }
89
90    /// Get the instructions of this block
91    #[inline]
92    pub fn insts(&self) -> &[Inst] {
93        &self.insts
94    }
95
96    /// Get the name of the block
97    #[inline]
98    pub fn name(&self) -> &str {
99        &self.name
100    }
101}