-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathvalidate.rs
241 lines (214 loc) · 8.15 KB
/
validate.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
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
use std::{cmp::Ordering, collections::BTreeSet};
use crate::ir::{BitOffset, BlockItemInner, IR};
#[derive(Debug, Clone)]
pub struct Options {
pub allow_register_overlap: bool,
pub allow_field_overlap: bool,
pub allow_enum_dup_value: bool,
pub allow_unused_enums: bool,
pub allow_unused_fieldsets: bool,
}
pub fn validate(ir: &IR, options: Options) -> Vec<String> {
let mut errs = Vec::new();
let mut used_fieldsets = BTreeSet::new();
let mut used_enums = BTreeSet::new();
for (bname, b) in &ir.blocks {
if let Some(n) = &b.extends {
if !ir.blocks.contains_key(n) {
errs.push(format!(
"block {}: extends block {} does not exist",
bname, n
))
}
}
for bi in &b.items {
match &bi.inner {
BlockItemInner::Block(i) => {
if !ir.blocks.contains_key(&i.block) {
errs.push(format!(
"block {} item {}: block {} does not exist",
bname, bi.name, i.block
))
}
}
BlockItemInner::Register(i) => {
if let Some(fs) = &i.fieldset {
used_fieldsets.insert(fs.clone());
if !ir.fieldsets.contains_key(fs) {
errs.push(format!(
"block {} item {}: fieldset {} does not exist",
bname, bi.name, fs
))
}
}
}
}
}
if !options.allow_register_overlap {
for (i1, i2) in Pairs::new(b.items.iter()) {
if i1.byte_offset == i2.byte_offset {
errs.push(format!(
"block {}: registers overlap: {} {}",
bname, i1.name, i2.name
));
}
}
}
}
for (fsname, fs) in &ir.fieldsets {
if let Some(n) = &fs.extends {
used_fieldsets.insert(n.clone());
if !ir.fieldsets.contains_key(n) {
errs.push(format!(
"fieldset {}: extends fieldset {} does not exist",
fsname, n
))
}
}
}
for (fsname, fs) in &ir.fieldsets {
if !options.allow_unused_fieldsets && !used_fieldsets.contains(fsname) {
errs.push(format!("fieldset {} is unused", fsname));
}
'FIELD: for f in &fs.fields {
if let Some(ename) = &f.enumm {
used_enums.insert(ename.clone());
let Some(e) = ir.enums.get(ename) else {
errs.push(format!(
"fieldset {} field {}: enum {} does not exist",
fsname, f.name, ename
));
continue;
};
// do extra check when bit_offset is in "range mode"
if let BitOffset::Cursed(ranges) = &f.bit_offset {
let mut last_max_index = 0;
let mut ranges_size = 0;
for (index, range) in ranges.iter().enumerate() {
// every "range" shouldn't be empty (aka start > end)
if range.is_empty() {
errs.push(format!(
"fieldset {} field {}: end value of bit_offset is bigger than start value",
fsname, f.name,
));
continue 'FIELD;
}
// "range"s of same field shouldn't overlap
if index > 0 {
match range.start().cmp(&last_max_index) {
Ordering::Less => {
errs.push(format!(
"fieldset {} field {}: bit_offset is overlapped with itself",
fsname, f.name,
));
continue 'FIELD;
}
Ordering::Equal => {
errs.push(format!(
"fieldset {} field {}: bit_offset has continuous part, should be merged",
fsname, f.name,
));
continue 'FIELD;
}
Ordering::Greater => last_max_index = *range.end(),
}
}
ranges_size += range.end() - range.start() + 1;
}
// bit size from "ranges" should be the same as field bit_size
if ranges_size != f.bit_size {
errs.push(format!(
"fieldset {} field {}: size of bit_offset ranges is mismatch with field bit_size",
fsname, f.name,
));
continue;
}
}
if f.bit_size != e.bit_size {
errs.push(format!(
"fieldset {} field {}: bit_size {} does not match enum {} bit_size {}",
fsname, f.name, f.bit_size, ename, e.bit_size
));
}
}
}
if !options.allow_field_overlap {
for (i1, i2) in Pairs::new(fs.fields.iter()) {
// expand every BitOffset to a Vec<RangeInclusive>,
// and compare at that level
'COMPARE: for i1_range in i1.bit_offset.clone().into_ranges(i1.bit_size) {
for i2_range in i2.bit_offset.clone().into_ranges(i2.bit_size) {
if i2_range.end() > i1_range.start() && i1_range.end() > i2_range.start() {
errs.push(format!(
"fieldset {}: fields overlap: {} {}",
fsname, i1.name, i2.name
));
break 'COMPARE;
}
}
}
}
}
}
for (ename, e) in &ir.enums {
if !options.allow_unused_enums && !used_enums.contains(ename) {
errs.push(format!("enum {} is unused", ename));
}
let maxval = 1 << e.bit_size;
for v in &e.variants {
if v.value >= maxval {
errs.push(format!(
"enum {} variant {}: value {} is not less than than max 1<<{} = {}",
ename, v.name, v.value, e.bit_size, maxval,
));
}
}
if !options.allow_enum_dup_value {
for (i1, i2) in Pairs::new(e.variants.iter()) {
if i1.value == i2.value {
errs.push(format!(
"enum {}: variants with same value: {} {}",
ename, i1.name, i2.name
));
}
}
}
}
errs
}
// ==============
struct Pairs<U: Iterator + Clone> {
head: Option<U::Item>,
tail: U,
next: U,
}
impl<U: Iterator + Clone> Pairs<U> {
fn new(mut iter: U) -> Self {
let head = iter.next();
Pairs {
head,
tail: iter.clone(),
next: iter,
}
}
}
impl<U: Iterator + Clone> Iterator for Pairs<U>
where
U::Item: Clone,
{
type Item = (U::Item, U::Item);
fn next(&mut self) -> Option<Self::Item> {
let a = self.head.as_ref()?.clone();
if let Some(b) = self.tail.next() {
return Some((a, b));
}
match self.next.next() {
Some(new_head) => {
self.head = Some(new_head);
self.tail = self.next.clone();
self.next()
}
None => None,
}
}
}