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
use std::ops::Range;
use std::usize;
use petgraph::graph::NodeIndex;
use {Uuid, Area, Function};
use mnemonic::MnemonicIndex;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BasicBlock {
pub uuid: Uuid,
pub mnemonics: Range<MnemonicIndex>,
pub node: NodeIndex,
pub area: Area,
pub statements: Range<usize>,
}
impl BasicBlock {
pub fn area<'a>(&'a self) -> &'a Area { &self.area }
}
#[derive(Clone,Copy,Debug,PartialOrd,Ord,PartialEq,Eq)]
pub struct BasicBlockIndex {
index: usize
}
impl BasicBlockIndex {
pub fn new(i: usize) -> BasicBlockIndex { BasicBlockIndex{ index: i } }
pub fn index(&self) -> usize { self.index }
}
#[derive(Clone)]
pub struct BasicBlockIterator<'a> {
function: &'a Function,
index: usize,
max: usize,
}
impl<'a> BasicBlockIterator<'a> {
pub fn new(function: &'a Function,index: usize, max: usize) -> BasicBlockIterator<'a> {
BasicBlockIterator{
function: function,
index: index,
max: max,
}
}
}
impl<'a> Iterator for BasicBlockIterator<'a> {
type Item = (BasicBlockIndex,&'a BasicBlock);
fn next(&mut self) -> Option<(BasicBlockIndex,&'a BasicBlock)> {
if self.index < self.max {
let idx = BasicBlockIndex::new(self.index);
let bb = self.function.basic_block(idx);
self.index += 1;
Some((idx,bb))
} else {
None
}
}
}
impl<'a> ExactSizeIterator for BasicBlockIterator<'a> {
fn len(&self) -> usize {
self.max - self.index
}
}
impl<'a> DoubleEndedIterator for BasicBlockIterator<'a> {
fn next_back(&mut self) -> Option<(BasicBlockIndex,&'a BasicBlock)> {
if self.index < self.max {
let idx = BasicBlockIndex::new(self.max - 1);
let bb = self.function.basic_block(idx);
self.max -= 1;
Some((idx,bb))
} else {
None
}
}
}