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
use cretonne_codegen::ir::entities::AnyEntity;
use cretonne_codegen::ir::{Ebb, FuncRef, GlobalValue, Heap, JumpTable, SigRef, StackSlot, Value};
use error::{Location, ParseResult};
use lexer::split_entity_name;
use std::collections::HashMap;
#[derive(Debug, Default)]
pub struct SourceMap {
locations: HashMap<AnyEntity, Location>,
}
impl SourceMap {
pub fn contains_value(&self, v: Value) -> bool {
self.locations.contains_key(&v.into())
}
pub fn contains_ebb(&self, ebb: Ebb) -> bool {
self.locations.contains_key(&ebb.into())
}
pub fn contains_ss(&self, ss: StackSlot) -> bool {
self.locations.contains_key(&ss.into())
}
pub fn contains_gv(&self, gv: GlobalValue) -> bool {
self.locations.contains_key(&gv.into())
}
pub fn contains_heap(&self, heap: Heap) -> bool {
self.locations.contains_key(&heap.into())
}
pub fn contains_sig(&self, sig: SigRef) -> bool {
self.locations.contains_key(&sig.into())
}
pub fn contains_fn(&self, fn_: FuncRef) -> bool {
self.locations.contains_key(&fn_.into())
}
pub fn contains_jt(&self, jt: JumpTable) -> bool {
self.locations.contains_key(&jt.into())
}
pub fn lookup_str(&self, name: &str) -> Option<AnyEntity> {
split_entity_name(name).and_then(|(ent, num)| match ent {
"v" => Value::with_number(num).and_then(|v| {
if !self.contains_value(v) {
None
} else {
Some(v.into())
}
}),
"ebb" => Ebb::with_number(num).and_then(|ebb| {
if !self.contains_ebb(ebb) {
None
} else {
Some(ebb.into())
}
}),
"ss" => StackSlot::with_number(num).and_then(|ss| {
if !self.contains_ss(ss) {
None
} else {
Some(ss.into())
}
}),
"gv" => GlobalValue::with_number(num).and_then(|gv| {
if !self.contains_gv(gv) {
None
} else {
Some(gv.into())
}
}),
"heap" => Heap::with_number(num).and_then(|heap| {
if !self.contains_heap(heap) {
None
} else {
Some(heap.into())
}
}),
"sig" => SigRef::with_number(num).and_then(|sig| {
if !self.contains_sig(sig) {
None
} else {
Some(sig.into())
}
}),
"fn" => FuncRef::with_number(num).and_then(|fn_| {
if !self.contains_fn(fn_) {
None
} else {
Some(fn_.into())
}
}),
"jt" => JumpTable::with_number(num).and_then(|jt| {
if !self.contains_jt(jt) {
None
} else {
Some(jt.into())
}
}),
_ => None,
})
}
pub fn location(&self, entity: AnyEntity) -> Option<Location> {
self.locations.get(&entity).cloned()
}
}
impl SourceMap {
pub fn new() -> Self {
Self {
locations: HashMap::new(),
}
}
pub fn def_value(&mut self, entity: Value, loc: Location) -> ParseResult<()> {
self.def_entity(entity.into(), loc)
}
pub fn def_ebb(&mut self, entity: Ebb, loc: Location) -> ParseResult<()> {
self.def_entity(entity.into(), loc)
}
pub fn def_ss(&mut self, entity: StackSlot, loc: Location) -> ParseResult<()> {
self.def_entity(entity.into(), loc)
}
pub fn def_gv(&mut self, entity: GlobalValue, loc: Location) -> ParseResult<()> {
self.def_entity(entity.into(), loc)
}
pub fn def_heap(&mut self, entity: Heap, loc: Location) -> ParseResult<()> {
self.def_entity(entity.into(), loc)
}
pub fn def_sig(&mut self, entity: SigRef, loc: Location) -> ParseResult<()> {
self.def_entity(entity.into(), loc)
}
pub fn def_fn(&mut self, entity: FuncRef, loc: Location) -> ParseResult<()> {
self.def_entity(entity.into(), loc)
}
pub fn def_jt(&mut self, entity: JumpTable, loc: Location) -> ParseResult<()> {
self.def_entity(entity.into(), loc)
}
pub fn def_entity(&mut self, entity: AnyEntity, loc: Location) -> ParseResult<()> {
if self.locations.insert(entity, loc).is_some() {
err!(loc, "duplicate entity: {}", entity)
} else {
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use parse_test;
#[test]
fn details() {
let tf = parse_test(
"function %detail() {
ss10 = incoming_arg 13
jt10 = jump_table ebb0
ebb0(v4: i32, v7: i32):
v10 = iadd v4, v7
}",
).unwrap();
let map = &tf.functions[0].1.map;
assert_eq!(map.lookup_str("v0"), None);
assert_eq!(map.lookup_str("ss1"), None);
assert_eq!(map.lookup_str("ss10").unwrap().to_string(), "ss10");
assert_eq!(map.lookup_str("jt10").unwrap().to_string(), "jt10");
assert_eq!(map.lookup_str("ebb0").unwrap().to_string(), "ebb0");
assert_eq!(map.lookup_str("v4").unwrap().to_string(), "v4");
assert_eq!(map.lookup_str("v7").unwrap().to_string(), "v7");
assert_eq!(map.lookup_str("v10").unwrap().to_string(), "v10");
}
}