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
use il::*;
use std::collections::BTreeMap;
use std::fmt;
use RC;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Program {
functions: BTreeMap<usize, RC<Function>>,
next_index: usize
}
impl Program {
pub fn new() -> Program {
Program {
functions: BTreeMap::new(),
next_index: 0
}
}
pub fn function_by_address(&self, address: u64) -> Option<&Function> {
for function in &self.functions {
if function.1.address() == address {
return Some(function.1);
}
}
None
}
pub fn functions(&self) -> Vec<&Function> {
let mut v = Vec::new();
for f in &self.functions {
let f: &Function = &f.1;
v.push(f);
}
v
}
pub fn functions_map(&self) -> BTreeMap<usize, &Function> {
self.functions
.iter()
.map(|(index, function)| (*index, function.as_ref()))
.collect::<BTreeMap<usize, &Function>>()
}
pub fn function(&self, index: usize) -> Option<&Function> {
match self.functions.get(&index) {
Some(f) => Some(f),
None => None
}
}
pub fn add_function(&mut self, mut function: Function) {
function.set_index(Some(self.next_index));
self.functions.insert(self.next_index, RC::new(function));
self.next_index += 1;
}
pub fn function_by_name(&self, name: &str) -> Option<&Function> {
self.functions
.iter()
.find(|(_, function)| function.name() == name)
.map(|(_, function)| function.as_ref())
}
}
impl fmt::Display for Program {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for function in &self.functions {
writeln!(f, "{}@{:08X}", function.1.name(), function.0)?
}
Ok(())
}
}