1pub mod hir;
2pub mod ty;
3pub mod arch;
4pub mod index_counter;
5pub mod source_info;
6pub mod mir;
7pub mod tir;
8mod internal_types;
9
10pub use internal_types::*;
11
12use index_vec::{IndexVec, index_vec, define_index_type};
13use display_adapter::display_adapter;
14
15use hir::{HirCode, Item, GenericCtx};
16use mir::{MirCode, Instr, InstrId, VOID_INSTR};
17use source_info::SourceRange;
18use ty::Type;
19
20define_index_type!(pub struct OpId = u32;);
21define_index_type!(pub struct BlockId = u32;);
22
23#[derive(Clone, Debug)]
24pub enum Op {
25 HirItem { item: Item, has_semicolon: bool },
26 MirInstr(Instr, InstrId, Type),
27}
28
29impl Op {
30 #[inline]
31 pub fn as_mir_instr(&self) -> Option<&Instr> {
32 match self {
33 Op::MirInstr(instr, _, _) => Some(instr),
34 _ => None,
35 }
36 }
37
38 #[inline]
39 pub fn as_mir_instr_mut(&mut self) -> Option<&mut Instr> {
40 match self {
41 Op::MirInstr(instr, _, _) => Some(instr),
42 _ => None,
43 }
44 }
45
46 #[inline]
47 pub fn get_mir_instr_id(&self) -> Option<InstrId> {
48 match self {
49 &Op::MirInstr(_, id, _) => Some(id),
50 _ => None,
51 }
52 }
53
54 #[inline]
55 pub fn get_mir_instr_type(&self) -> Option<&Type> {
56 match self {
57 Op::MirInstr(_, _, ty) => Some(ty),
58 _ => None,
59 }
60 }
61
62 pub fn as_hir_item(&self) -> Option<Item> {
63 match self {
64 &Op::HirItem { item, .. } => Some(item),
65 _ => None,
66 }
67 }
68
69 pub fn has_semicolon(&self) -> bool {
70 match self {
71 &Op::HirItem { has_semicolon, .. } => has_semicolon,
72 _ => false,
73 }
74 }
75}
76
77#[derive(Default)]
78pub struct Block {
79 pub ops: Vec<OpId>,
80}
81pub struct Code {
82 pub blocks: IndexVec<BlockId, Block>,
83 pub ops: IndexVec<OpId, Op>,
84 pub hir: HirCode,
85 pub mir: MirCode,
86}
87
88impl Default for Code {
89 fn default() -> Self {
90 let mut val = Code {
91 blocks: IndexVec::default(),
92 ops: index_vec![Op::MirInstr(Instr::Void, InstrId::new(0), Type::Void)],
93 hir: HirCode::default(),
94 mir: MirCode::default(),
95 };
96 val.mir.source_ranges.insert(VOID_INSTR, SourceRange::default());
97 val.mir.instr_names.insert(VOID_INSTR, "void".to_string());
98 val.hir.generic_ctxs.push(GenericCtx::Blank);
99 val
100 }
101}
102
103impl Code {
104 #[display_adapter]
105 pub fn display_block(&self, block: BlockId, w: &mut Formatter) {
106 let block = &self.blocks[block];
107 for &id in &block.ops {
108 write!(w, " %op{}", id.index())?;
109 match self.ops[id] {
110 Op::HirItem { item, .. } => {
111 match item {
112 Item::Expr(expr) => {
113 write!(w, "(%expr{}) = hir.", expr.index())?;
114 let expr = &self.hir.exprs[expr];
115 writeln!(w, "{:?}", expr)?;
116 },
117 Item::Decl(decl) => {
118 write!(w, "(%decl{}) = hir.", decl.index())?;
119 let decl = &self.hir.decls[decl];
120 writeln!(w, "{:?}", decl)?;
121 }
122 }
123 },
124 Op::MirInstr(ref instr, _, _) => {
125 writeln!(w, " = mir.{:?}", instr)?;
126 },
127 }
128 }
129 Ok(())
130 }
131}