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
use crate::file_format::{Bytecode, CodeOffset};
use std::collections::{BTreeMap, BTreeSet};
type Map<K, V> = BTreeMap<K, V>;
type Set<V> = BTreeSet<V>;
pub type BlockId = CodeOffset;
pub trait ControlFlowGraph {
fn block_start(&self, block_id: BlockId) -> CodeOffset;
fn block_end(&self, block_id: BlockId) -> CodeOffset;
fn successors(&self, block_id: BlockId) -> &Vec<BlockId>;
fn instr_indexes(&self, block_id: BlockId) -> Box<dyn Iterator<Item = CodeOffset>>;
fn blocks(&self) -> Vec<BlockId>;
fn num_blocks(&self) -> u16;
fn entry_block_id(&self) -> BlockId;
}
struct BasicBlock {
entry: CodeOffset,
exit: CodeOffset,
successors: Vec<BlockId>,
}
pub struct VMControlFlowGraph {
blocks: Map<BlockId, BasicBlock>,
}
impl BasicBlock {
pub fn display(&self) {
println!("+=======================+");
println!("| Enter: {} |", self.entry);
println!("+-----------------------+");
println!("==> Children: {:?}", self.successors);
println!("+-----------------------+");
println!("| Exit: {} |", self.exit);
println!("+=======================+");
}
}
const ENTRY_BLOCK_ID: BlockId = 0;
impl VMControlFlowGraph {
pub fn new(code: &[Bytecode]) -> Self {
let mut block_ids = Set::new();
block_ids.insert(ENTRY_BLOCK_ID);
for pc in 0..code.len() {
VMControlFlowGraph::record_block_ids(pc as CodeOffset, code, &mut block_ids);
}
let mut cfg = VMControlFlowGraph { blocks: Map::new() };
let mut entry = 0;
for pc in 0..code.len() {
let co_pc: CodeOffset = pc as CodeOffset;
if VMControlFlowGraph::is_end_of_block(co_pc, code, &block_ids) {
let successors = Bytecode::get_successors(co_pc, code);
let bb = BasicBlock {
entry,
exit: co_pc,
successors,
};
cfg.blocks.insert(entry, bb);
entry = co_pc + 1;
}
}
assert_eq!(entry, code.len() as CodeOffset);
cfg
}
pub fn display(&self) {
for block in self.blocks.values() {
block.display();
}
}
fn is_end_of_block(pc: CodeOffset, code: &[Bytecode], block_ids: &Set<BlockId>) -> bool {
pc + 1 == (code.len() as CodeOffset) || block_ids.contains(&(pc + 1))
}
fn record_block_ids(pc: CodeOffset, code: &[Bytecode], block_ids: &mut Set<BlockId>) {
let bytecode = &code[pc as usize];
if let Some(offset) = bytecode.offset() {
block_ids.insert(*offset);
}
if bytecode.is_branch() && pc + 1 < (code.len() as CodeOffset) {
block_ids.insert(pc + 1);
}
}
fn traverse_by(&self, block_id: BlockId) -> Vec<BlockId> {
let mut ret = Vec::new();
let mut index = 0;
let mut seen = Set::new();
ret.push(block_id);
seen.insert(&block_id);
while index < ret.len() {
let block_id = ret[index];
index += 1;
let successors = self.successors(block_id);
for block_id in successors.iter() {
if !seen.contains(&block_id) {
ret.push(*block_id);
seen.insert(block_id);
}
}
}
ret
}
pub fn reachable_from(&self, block_id: BlockId) -> Vec<BlockId> {
self.traverse_by(block_id)
}
}
impl ControlFlowGraph for VMControlFlowGraph {
fn block_start(&self, block_id: BlockId) -> CodeOffset {
self.blocks[&block_id].entry
}
fn block_end(&self, block_id: BlockId) -> CodeOffset {
self.blocks[&block_id].exit
}
fn successors(&self, block_id: BlockId) -> &Vec<BlockId> {
&self.blocks[&block_id].successors
}
fn blocks(&self) -> Vec<BlockId> {
self.blocks.keys().cloned().collect()
}
fn instr_indexes(&self, block_id: BlockId) -> Box<dyn Iterator<Item = CodeOffset>> {
Box::new(self.block_start(block_id)..=self.block_end(block_id))
}
fn num_blocks(&self) -> u16 {
self.blocks.len() as u16
}
fn entry_block_id(&self) -> BlockId {
ENTRY_BLOCK_ID
}
}