-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
182 lines (167 loc) · 5.21 KB
/
lib.rs
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
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
fmt::Display,
};
use bbb::{form_blocks, Block, BlockHelpers};
use bril_rs::{Argument, EffectOps, Function, Instruction};
use petgraph::prelude::DiGraphMap;
#[derive(Debug, Clone)]
pub struct CFG {
pub blocks: Vec<Block>,
pub graph: DiGraphMap<CFGNode, Edge>,
pub args: Vec<Argument>,
}
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum CFGNode {
Block(usize),
Return,
}
#[derive(Debug, Clone)]
pub enum Edge {
Always,
Bool(bool),
}
pub trait DataFlowHelpers {
fn get_defs(&self) -> HashMap<String, HashSet<CFGNode>>;
fn get_uses(&self) -> HashMap<String, HashSet<CFGNode>>;
}
impl DataFlowHelpers for CFG {
fn get_defs(&self) -> HashMap<String, HashSet<CFGNode>> {
let mut result = HashMap::new();
self.blocks.iter().enumerate().for_each(|(index, block)| {
block.get_defs().into_iter().for_each(|def| {
result
.entry(def)
.or_insert_with(|| HashSet::new())
.insert(CFGNode::Block(index));
})
});
result
}
fn get_uses(&self) -> HashMap<String, HashSet<CFGNode>> {
let mut result = HashMap::new();
self.blocks.iter().enumerate().for_each(|(index, block)| {
block.get_uses().into_iter().for_each(|r#use| {
result
.entry(r#use)
.or_insert_with(|| HashSet::new())
.insert(CFGNode::Block(index));
})
});
result
}
}
impl CFG {
pub fn get_block(&self, node: CFGNode) -> Option<&Block> {
match node {
CFGNode::Block(i) => self.blocks.get(i),
CFGNode::Return => None,
}
}
pub fn get_block_mut(&mut self, node: CFGNode) -> Option<&mut Block> {
match node {
CFGNode::Block(i) => self.blocks.get_mut(i),
CFGNode::Return => None,
}
}
pub fn split_blocks_mut(blocks: &mut Vec<Block>) -> HashMap<CFGNode, RefCell<&mut Block>> {
blocks
.iter_mut()
.enumerate()
.map(|(i, b)| (CFGNode::Block(i), RefCell::new(b)))
.collect()
}
}
impl Display for CFG {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for block in &self.blocks {
writeln!(f, "[Block: {}]", block.label)?;
for instr in &block.instrs {
writeln!(f, " {}", instr)?;
}
writeln!(f, "")?;
}
Ok(())
}
}
fn generate_label_index(blocks: &Vec<Block>) -> HashMap<String, usize> {
blocks
.iter()
.enumerate()
.map(|(i, block)| (block.label.to_owned(), i))
.collect()
}
pub fn generate_cfg(function: &Function) -> CFG {
let mut blocks = form_blocks(function);
let label_index = generate_label_index(&blocks);
let mut graph = DiGraphMap::new();
let n = blocks.len();
for i in 0..n {
let (left, right) = blocks.split_at_mut(i + 1);
let block = &mut left[i];
let node = CFGNode::Block(i);
graph.add_node(node);
match block.instrs.last() {
Some(Instruction::Effect {
op: EffectOps::Return,
..
}) => {
graph.add_edge(node, CFGNode::Return, Edge::Always);
}
Some(Instruction::Effect {
op: EffectOps::Jump,
labels,
..
}) => {
graph.add_edge(
node,
CFGNode::Block(*label_index.get(&labels[0]).unwrap()),
Edge::Always,
);
}
Some(Instruction::Effect {
op: EffectOps::Branch,
labels,
..
}) => {
graph.add_edge(
node,
CFGNode::Block(*label_index.get(&labels[0]).unwrap()),
Edge::Bool(true),
);
graph.add_edge(
node,
CFGNode::Block(*label_index.get(&labels[1]).unwrap()),
Edge::Bool(false),
);
}
_ => {
if let Some(next) = right.get(0) {
graph.add_edge(node, CFGNode::Block(i + 1), Edge::Always);
block.instrs.push(Instruction::Effect {
op: EffectOps::Jump,
labels: vec![next.label.to_owned()],
args: Vec::new(),
funcs: Vec::new(),
pos: None,
});
} else {
graph.add_edge(node, CFGNode::Return, Edge::Always);
block.instrs.push(Instruction::Effect {
op: EffectOps::Return,
args: Vec::new(),
funcs: Vec::new(),
labels: Vec::new(),
pos: None,
});
}
}
};
}
CFG {
blocks,
graph,
args: function.args.clone(),
}
}