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
use il::*;
use std::fmt;
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct Block {
index: usize,
next_instruction_index: usize,
next_temp_index: usize,
instructions: Vec<Instruction>,
}
impl Block {
pub(crate) fn new(index: usize) -> Block {
Block {
index: index,
next_instruction_index: 0,
next_temp_index: 0,
instructions: Vec::new()
}
}
fn new_instruction_index(&mut self) -> usize {
let instruction_index = self.next_instruction_index;
self.next_instruction_index = instruction_index + 1;
instruction_index
}
fn push(&mut self, instruction: Instruction) {
self.instructions.push(instruction);
}
pub fn address(&self) -> Option<u64> {
self.instructions
.first()
.and_then(|instruction| instruction.address())
}
pub fn append(&mut self, other: &Block) {
other.instructions().into_iter().for_each(|instruction| {
let index = self.new_instruction_index();
self.instructions.push(instruction.clone_new_index(index));
})
}
pub fn index(&self) -> usize {
self.index
}
pub fn instructions(&self) -> &Vec<Instruction> {
&self.instructions
}
pub fn instructions_mut(&mut self) -> &mut Vec<Instruction> {
&mut self.instructions
}
pub fn is_empty(&self) -> bool {
self.instructions.is_empty()
}
pub fn instruction(&self, index: usize) -> Option<&Instruction> {
self.instructions
.iter()
.find(|instruction| instruction.index() == index)
}
pub fn instruction_mut<>(&mut self, index: usize) -> Option<&mut Instruction> {
self.instructions
.iter_mut()
.find(|instruction| instruction.index() == index)
}
pub fn remove_instruction(&mut self, index: usize) -> Result<()> {
self.instructions
.iter()
.position(|instruction| instruction.index() == index)
.map(|index| { self.instructions.remove(index); })
.ok_or(format!("No instruction with index {} found", index).into())
}
pub(crate) fn clone_new_index(&self, index: usize) -> Block {
let mut clone = self.clone();
clone.index = index;
clone
}
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_{}.{}", self.index, next_index), bits)
}
pub fn assign(&mut self, dst: Scalar, src: Expression) {
let index = self.new_instruction_index();
self.push(Instruction::assign(index, dst, src));
}
pub fn store(&mut self, address: Expression, src: Expression) {
let index = self.new_instruction_index();
self.push(Instruction::store(index, address, src))
}
pub fn load(&mut self, dst: Scalar, address: Expression) {
let index = self.new_instruction_index();
self.push(Instruction::load(index, dst, address));
}
pub fn branch(&mut self, dst: Expression) {
let index = self.new_instruction_index();
self.push(Instruction::branch(index, dst));
}
pub fn intrinsic(&mut self, intrinsic: Intrinsic) {
let index = self.new_instruction_index();
self.push(Instruction::intrinsic(index, intrinsic));
}
pub fn nop(&mut self) {
let index = self.new_instruction_index();
self.push(Instruction::nop(index));
}
}
impl graph::Vertex for Block {
fn index (&self) -> usize { self.index }
fn dot_label(&self) -> String { format!("{}", self) }
}
impl fmt::Display for Block {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(writeln!(f, "[ Block: 0x{:X} ]", self.index));
for instruction in self.instructions() {
try!(writeln!(f, "{}", instruction));
}
Ok(())
}
}