tnj_air/instructions/value/
block.rs1use 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 BasicBlock(u32)
12}
13
14index_struct! {
15 BlockParam(u32)
17}
18
19#[derive(Debug, Clone, Eq, PartialEq, Hash)]
21pub struct BlockParamData {
22 pub name: Option<SmolStr>,
24 pub ty: Type,
26}
27
28impl BlockParamData {
29 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#[derive(Default, Clone)]
46pub struct BasicBlockData {
47 pub(crate) name: String,
49 pub(crate) params: Vec<BlockParam>,
51 pub(crate) insts: Vec<Inst>,
53 pub(crate) facts: HashMap<Value, Expr>,
56 pub(crate) assertions: Vec<Expr>,
58}
59
60impl BasicBlockData {
61 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 #[inline]
74 pub fn get_fact(&self, value: Value) -> Option<&Expr> {
75 self.facts.get(&value)
76 }
77
78 #[inline]
80 pub fn assertions(&self) -> &[Expr] {
81 &self.assertions
82 }
83
84 #[inline]
86 pub fn facts(&self) -> impl Iterator<Item = (&Value, &Expr)> + '_ {
87 self.facts.iter()
88 }
89
90 #[inline]
92 pub fn insts(&self) -> &[Inst] {
93 &self.insts
94 }
95
96 #[inline]
98 pub fn name(&self) -> &str {
99 &self.name
100 }
101}