1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::{
ir::{
Block, ControlFlowGraph, DataFlowGraph, FunctionInsertPos, FunctionLayout, Inst, InstData,
InstLayout, Signature, Unit, UnitBuilder, UnitKind, UnitName, Value,
},
ty::Type,
verifier::Verifier,
};
#[derive(Serialize, Deserialize)]
pub struct Process {
pub name: UnitName,
pub sig: Signature,
pub dfg: DataFlowGraph,
pub cfg: ControlFlowGraph,
pub layout: FunctionLayout,
}
impl Process {
pub fn new(name: UnitName, sig: Signature) -> Self {
assert!(!sig.has_return_type());
let mut prok = Self {
name,
sig,
dfg: DataFlowGraph::new(),
cfg: ControlFlowGraph::new(),
layout: FunctionLayout::new(),
};
prok.dfg.make_args_for_signature(&prok.sig);
prok
}
}
impl Unit for Process {
fn kind(&self) -> UnitKind {
UnitKind::Process
}
fn get_process(&self) -> Option<&Process> {
Some(self)
}
fn get_process_mut(&mut self) -> Option<&mut Process> {
Some(self)
}
fn dfg(&self) -> &DataFlowGraph {
&self.dfg
}
fn dfg_mut(&mut self) -> &mut DataFlowGraph {
&mut self.dfg
}
fn try_cfg(&self) -> Option<&ControlFlowGraph> {
Some(&self.cfg)
}
fn try_cfg_mut(&mut self) -> Option<&mut ControlFlowGraph> {
Some(&mut self.cfg)
}
fn sig(&self) -> &Signature {
&self.sig
}
fn sig_mut(&mut self) -> &mut Signature {
&mut self.sig
}
fn name(&self) -> &UnitName {
&self.name
}
fn name_mut(&mut self) -> &mut UnitName {
&mut self.name
}
fn func_layout(&self) -> &FunctionLayout {
&self.layout
}
fn func_layout_mut(&mut self) -> &mut FunctionLayout {
&mut self.layout
}
fn inst_layout(&self) -> &InstLayout {
panic!("inst_layout() called on process");
}
fn inst_layout_mut(&mut self) -> &mut InstLayout {
panic!("inst_layout_mut() called on process");
}
fn dump_fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "proc {} {} {{\n", self.name, self.sig.dump(&self.dfg))?;
for bb in self.layout.blocks() {
write!(f, "{}:\n", bb.dump(&self.cfg))?;
for inst in self.layout.insts(bb) {
write!(f, " {}\n", inst.dump(&self.dfg, Some(&self.cfg)))?;
}
}
write!(f, "}}")?;
Ok(())
}
fn verify(&self) {
let mut verifier = Verifier::new();
verifier.verify_process(self);
match verifier.finish() {
Ok(()) => (),
Err(errs) => {
eprintln!("");
eprintln!("Verified process:");
eprintln!("{}", self.dump());
eprintln!("");
eprintln!("Verification errors:");
eprintln!("{}", errs);
panic!("verification failed");
}
}
}
}
pub struct ProcessBuilder<'u> {
pub prok: &'u mut Process,
pos: FunctionInsertPos,
}
impl<'u> ProcessBuilder<'u> {
pub fn new(prok: &'u mut Process) -> Self {
Self {
prok,
pos: FunctionInsertPos::None,
}
}
}
impl UnitBuilder for ProcessBuilder<'_> {
type Unit = Process;
fn unit(&self) -> &Process {
self.prok
}
fn unit_mut(&mut self) -> &mut Process {
self.prok
}
fn build_inst(&mut self, data: InstData, ty: Type) -> Inst {
let inst = self.prok.dfg.add_inst(data, ty);
self.pos.add_inst(inst, &mut self.prok.layout);
inst
}
fn remove_inst(&mut self, inst: Inst) {
self.prok.dfg.remove_inst(inst);
self.pos.remove_inst(inst, &self.prok.layout);
self.prok.layout.remove_inst(inst);
}
fn block(&mut self) -> Block {
let bb = self.prok.cfg.add_block();
self.prok.layout.append_block(bb);
bb
}
fn remove_block(&mut self, bb: Block) {
let insts: Vec<_> = self.prok.layout.insts(bb).collect();
self.prok.layout.remove_block(bb);
self.prok.cfg_mut().remove_block(bb);
for inst in insts {
if self.prok.dfg().has_result(inst) {
let value = self.prok.dfg().inst_result(inst);
self.prok.dfg_mut().replace_use(value, Value::invalid());
}
self.prok.dfg_mut().remove_inst(inst);
}
}
fn insert_at_end(&mut self) {
panic!("insert_at_end() called on process")
}
fn insert_at_beginning(&mut self) {
panic!("insert_at_beginning() called on process")
}
fn append_to(&mut self, bb: Block) {
self.pos = FunctionInsertPos::Append(bb);
}
fn prepend_to(&mut self, bb: Block) {
self.pos = FunctionInsertPos::Prepend(bb);
}
fn insert_after(&mut self, inst: Inst) {
self.pos = FunctionInsertPos::After(inst);
}
fn insert_before(&mut self, inst: Inst) {
self.pos = FunctionInsertPos::Before(inst);
}
}