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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use crate::il::*;
use crate::{graph, Error};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt;
#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Serialize, Default)]
pub struct ControlFlowGraph {
graph: graph::Graph<Block, Edge>,
next_index: usize,
next_temp_index: u64,
entry: Option<usize>,
exit: Option<usize>,
ssa_form: bool,
}
impl ControlFlowGraph {
pub fn new() -> ControlFlowGraph {
ControlFlowGraph {
graph: graph::Graph::new(),
next_index: 0,
next_temp_index: 0,
entry: None,
exit: None,
ssa_form: false,
}
}
pub fn graph(&self) -> &graph::Graph<Block, Edge> {
&self.graph
}
pub fn set_entry(&mut self, entry: usize) -> Result<(), Error> {
if self.graph.has_vertex(entry) {
self.entry = Some(entry);
return Ok(());
}
Err("Index does not exist for set_entry".into())
}
pub fn set_exit(&mut self, exit: usize) -> Result<(), Error> {
if self.graph.has_vertex(exit) {
self.exit = Some(exit);
return Ok(());
}
Err("Index does not exist for set_exit".into())
}
pub fn entry(&self) -> Option<usize> {
self.entry
}
pub fn exit(&self) -> Option<usize> {
self.exit
}
pub fn block(&self, index: usize) -> Result<&Block, Error> {
self.graph.vertex(index)
}
pub fn block_mut(&mut self, index: usize) -> Result<&mut Block, Error> {
self.graph.vertex_mut(index)
}
pub fn blocks(&self) -> Vec<&Block> {
self.graph.vertices()
}
pub fn blocks_mut(&mut self) -> Vec<&mut Block> {
self.graph.vertices_mut()
}
pub fn edge(&self, head: usize, tail: usize) -> Result<&Edge, Error> {
self.graph.edge(head, tail)
}
pub fn edge_mut(&mut self, head: usize, tail: usize) -> Result<&mut Edge, Error> {
self.graph.edge_mut(head, tail)
}
pub fn edges(&self) -> Vec<&Edge> {
self.graph.edges()
}
pub fn edges_mut(&mut self) -> Vec<&mut Edge> {
self.graph.edges_mut()
}
pub fn edges_in(&self, index: usize) -> Result<Vec<&Edge>, Error> {
self.graph.edges_in(index)
}
pub fn edges_out(&self, index: usize) -> Result<Vec<&Edge>, Error> {
self.graph.edges_out(index)
}
pub fn predecessor_indices(&self, index: usize) -> Result<Vec<usize>, Error> {
self.graph.predecessor_indices(index)
}
pub fn successor_indices(&self, index: usize) -> Result<Vec<usize>, Error> {
self.graph.successor_indices(index)
}
pub fn set_address(&mut self, address: Option<u64>) {
for block in self.blocks_mut() {
for instruction in block.instructions_mut() {
instruction.set_address(address);
}
}
}
pub fn entry_block(&self) -> Option<Result<&Block, Error>> {
if self.entry.is_none() {
None
} else {
Some(self.block(self.entry.unwrap()))
}
}
pub fn temp(&mut self, bits: usize) -> Scalar {
let next_index = self.next_temp_index;
self.next_temp_index = next_index + 1;
Scalar::new(format!("temp_{}", next_index), bits)
}
pub fn new_block(&mut self) -> Result<&mut Block, Error> {
let next_index = self.next_index;
self.next_index += 1;
let block = Block::new(next_index);
self.graph.insert_vertex(block)?;
Ok(self.graph.vertex_mut(next_index).unwrap())
}
pub fn unconditional_edge(&mut self, head: usize, tail: usize) -> Result<(), Error> {
let edge = Edge::new(head, tail, None);
self.graph.insert_edge(edge)
}
pub fn conditional_edge(
&mut self,
head: usize,
tail: usize,
condition: Expression,
) -> Result<(), Error> {
let edge = Edge::new(head, tail, Some(condition));
self.graph.insert_edge(edge)
}
pub fn merge(&mut self) -> Result<(), Error> {
use std::collections::HashSet;
loop {
let mut blocks_being_merged: HashSet<usize> = HashSet::new();
let mut merges: Vec<(usize, usize)> = Vec::new();
for block in self.blocks() {
if blocks_being_merged.contains(&block.index()) {
continue;
}
let successors = self.graph.edges_out(block.index()).unwrap();
if successors.len() != 1 {
continue;
}
if successors.first().unwrap().condition().is_some() {
continue;
}
let successor: usize = match successors.first() {
Some(successor) => successor.tail(),
None => return Err(Error::ControlFlowGraphSuccessorNotFound),
};
if self
.entry()
.map(|entry| entry == successor)
.unwrap_or(false)
{
continue;
}
if blocks_being_merged.contains(&successor) {
continue;
}
let predecessors = self.graph.edges_in(successor).unwrap();
if predecessors.len() != 1 {
continue;
}
blocks_being_merged.insert(block.index());
blocks_being_merged.insert(successor);
merges.push((block.index(), successor));
}
if merges.is_empty() {
break;
}
for (merge_index, successor_index) in merges {
let successor_block = self.graph.vertex(successor_index)?.clone();
self.graph.vertex_mut(merge_index)?.append(&successor_block);
let mut new_edges = Vec::new();
for edge in self.graph.edges_out(successor_index).unwrap() {
let head = merge_index;
let tail = edge.tail();
let condition = edge.condition();
let edge = Edge::new(head, tail, condition.cloned());
new_edges.push(edge);
}
for edge in new_edges {
self.graph.insert_edge(edge)?;
}
self.graph.remove_vertex(successor_index)?;
}
}
Ok(())
}
pub fn append(&mut self, other: &ControlFlowGraph) -> Result<(), Error> {
let is_empty = self.graph.num_vertices() == 0;
if !is_empty && (self.entry().is_none() || self.exit().is_none()) {
return Err("entry/exit not set for dest ControlFlowGraph::append".into());
}
if other.entry().is_none() || other.exit().is_none() {
return Err("entry/exit not set for src ControlFlowGraph::append".into());
}
let mut block_map: BTreeMap<usize, usize> = BTreeMap::new();
for block in other.graph().vertices() {
let new_block = block.clone_new_index(self.next_index);
block_map.insert(block.index(), self.next_index);
self.next_index += 1;
self.graph.insert_vertex(new_block)?;
}
for edge in other.graph().edges() {
let new_head: usize = block_map[&edge.head()];
let new_tail: usize = block_map[&edge.tail()];
let new_edge = Edge::new(new_head, new_tail, edge.condition().cloned());
self.graph.insert_edge(new_edge)?;
}
if is_empty {
self.entry = Some(block_map[&other.entry().unwrap()]);
} else {
let transition_edge = Edge::new(
self.exit.unwrap(),
block_map[&(other.entry().unwrap())],
None,
);
self.graph.insert_edge(transition_edge)?;
}
self.exit = Some(block_map[&other.exit().unwrap()]);
Ok(())
}
pub fn insert(&mut self, other: &ControlFlowGraph) -> Result<(usize, usize), Error> {
if other.entry().is_none() || other.exit().is_none() {
return Err(Error::ControlFlowGraphEntryExitNotFound);
}
self.entry = None;
self.exit = None;
let mut entry_index = None;
let mut exit_index = None;
let mut block_map: BTreeMap<usize, usize> = BTreeMap::new();
for block in other.graph().vertices() {
let new_block = block.clone_new_index(self.next_index);
block_map.insert(block.index(), self.next_index);
if block.index() == other.entry().unwrap() {
entry_index = Some(self.next_index);
}
if block.index() == other.exit().unwrap() {
exit_index = Some(self.next_index);
}
self.next_index += 1;
self.graph.insert_vertex(new_block)?;
}
for edge in other.graph().edges() {
let new_head: usize = block_map[&edge.head()];
let new_tail: usize = block_map[&edge.tail()];
let new_edge = Edge::new(new_head, new_tail, edge.condition().cloned());
self.graph.insert_edge(new_edge)?;
}
if entry_index.is_none() || exit_index.is_none() {
return Err(Error::ControlFlowGraphEntryExitNotFound);
}
Ok((entry_index.unwrap(), exit_index.unwrap()))
}
}
impl fmt::Display for ControlFlowGraph {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for block in self.blocks() {
writeln!(f, "{}", block)?;
}
for edge in self.edges() {
writeln!(f, "edge {}", edge)?;
}
Ok(())
}
}