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
use crate::ir::*;
use std::{
fmt::Display,
ops::{Deref, DerefMut},
};
#[derive(Default)]
pub struct Verifier {
errors: VerifierErrors,
unit: Option<String>,
flags: UnitFlags,
}
impl Verifier {
pub fn new() -> Self {
Default::default()
}
pub fn verify_module(&mut self, module: &Module) {
for unit in module.units() {
match &module[unit] {
ModUnitData::Function(x) => self.verify_function(x),
ModUnitData::Process(x) => self.verify_process(x),
ModUnitData::Entity(x) => self.verify_entity(x),
ModUnitData::Declare { .. } => (),
}
}
}
pub fn verify_function(&mut self, func: &Function) {
self.unit = Some(format!("func {}", func.name));
self.flags = UnitFlags::FUNCTION;
self.verify_function_layout(&func.layout, &func.dfg);
self.unit = None;
}
pub fn verify_process(&mut self, prok: &Process) {
self.unit = Some(format!("proc {}", prok.name));
self.flags = UnitFlags::PROCESS;
self.verify_function_layout(&prok.layout, &prok.dfg);
self.unit = None;
}
pub fn verify_entity(&mut self, ent: &Entity) {
self.unit = Some(format!("entity {}", ent.name));
self.flags = UnitFlags::ENTITY;
self.verify_inst_layout(&ent.layout, &ent.dfg);
self.unit = None;
}
pub fn verify_function_layout(&mut self, layout: &FunctionLayout, dfg: &DataFlowGraph) {
if layout.first_block().is_none() {
self.errors.push(VerifierError {
unit: self.unit.clone(),
object: None,
message: format!("layout has no entry block"),
});
}
for bb in layout.blocks() {
if layout.first_inst(bb).is_none() {
self.errors.push(VerifierError {
unit: self.unit.clone(),
object: Some(bb.into()),
message: format!("block is empty"),
})
}
for inst in layout.insts(bb) {
if dfg[inst].opcode().is_terminator() && Some(inst) != layout.last_inst(bb) {
self.errors.push(VerifierError {
unit: self.unit.clone(),
object: Some(inst.into()),
message: format!(
"terminator instruction `{}` must be at the end of block {}",
inst.dump(dfg),
bb
),
});
}
if Some(inst) == layout.last_inst(bb) && !dfg[inst].opcode().is_terminator() {
self.errors.push(VerifierError {
unit: self.unit.clone(),
object: Some(bb.into()),
message: format!(
"last instruction `{}` must be a terminator",
inst.dump(dfg)
),
})
}
self.verify_inst(inst, dfg);
}
}
}
pub fn verify_inst_layout(&mut self, layout: &InstLayout, dfg: &DataFlowGraph) {
for inst in layout.insts() {
self.verify_inst(inst, dfg);
}
}
pub fn verify_inst(&mut self, inst: Inst, dfg: &DataFlowGraph) {
if !dfg[inst].opcode().valid_in().contains(self.flags) {
self.errors.push(VerifierError {
unit: self.unit.clone(),
object: Some(inst.into()),
message: format!("{} may not appear in this unit", dfg[inst].opcode()),
});
}
}
pub fn finish(self) -> Result<(), VerifierErrors> {
if self.errors.is_empty() {
Ok(())
} else {
Err(self.errors)
}
}
pub fn finish_panic(self) {
match self.finish() {
Ok(()) => (),
Err(errs) => panic!("Verification failed:\n{}", errs),
}
}
}
#[derive(Debug)]
pub struct VerifierError {
pub unit: Option<String>,
pub object: Option<AnyObject>,
pub message: String,
}
impl Display for VerifierError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(ref unit) = self.unit {
write!(f, "{}: ", unit)?;
}
if let Some(object) = self.object {
write!(f, "{}: ", object)?;
}
write!(f, "{}", self.message)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct VerifierErrors(pub Vec<VerifierError>);
impl Deref for VerifierErrors {
type Target = Vec<VerifierError>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for VerifierErrors {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Display for VerifierErrors {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
for err in self.iter() {
writeln!(f, "- {}", err)?;
}
Ok(())
}
}