To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

alive_entry.rs 10.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#![allow(dead_code)]

use utils::LinkedHashMap;
use utils::vec_utils;
use ast::ir::*;
use ast::ptr::*;
use std::fmt;

type EntryID = usize;

11
#[derive(Clone)]
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
pub struct AliveEntries {
    index: EntryID,

    inner: LinkedHashMap<EntryID, RegisterEntry>
}

impl fmt::Display for AliveEntries {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{} entries", self.inner.len()).unwrap();
        writeln!(f, "| {:20} | {:20} | {:20} |", "ssa", "registers", "stack slots").unwrap();
        for entry in self.inner.values() {
            writeln!(f, "{}", entry).unwrap()
        }

        Ok(())
    }
}

impl AliveEntries {
    pub fn new() -> AliveEntries {
        AliveEntries {
            index: 0,
            inner: LinkedHashMap::new()
        }
    }

    fn new_index(&mut self) -> EntryID {
        let ret = self.index;
        self.index += 1;

        ret
    }

qinsoon's avatar
qinsoon committed
45
    pub fn has_entries_for_temp(&self, temp: MuID) -> bool {
46
47
        for entry in self.inner.values() {
            if entry.match_temp(temp) {
qinsoon's avatar
qinsoon committed
48
                return true;
49
50
51
            }
        }

qinsoon's avatar
qinsoon committed
52
        false
53
    }
qinsoon's avatar
qinsoon committed
54
55
56
57
58
59
60
61
62
63
64
    pub fn find_entries_for_temp(&self, temp: MuID) -> Vec<&RegisterEntry> {
        let mut ret = vec![];
        for entry in self.inner.values() {
            if entry.match_temp(temp) {
                ret.push(entry);
            }
        }
        ret
    }
    pub fn find_entries_for_temp_mut(&mut self, temp: MuID) -> Vec<&mut RegisterEntry> {
        let mut ret = vec![];
65
66
        for entry in self.inner.values_mut() {
            if entry.match_temp(temp) {
qinsoon's avatar
qinsoon committed
67
                ret.push(entry);
68
69
            }
        }
qinsoon's avatar
qinsoon committed
70
        ret
71
72
    }

qinsoon's avatar
qinsoon committed
73
    pub fn has_entries_for_reg(&self, reg: MuID) -> bool {
74
75
        for entry in self.inner.values() {
            if entry.match_reg(reg) {
qinsoon's avatar
qinsoon committed
76
                return true;
77
78
            }
        }
qinsoon's avatar
qinsoon committed
79
        false
80
    }
qinsoon's avatar
qinsoon committed
81
82
83
84
85
86
87
88
89
90
91
    pub fn find_entries_for_reg(&self, reg: MuID) -> Vec<&RegisterEntry> {
        let mut ret = vec![];
        for entry in self.inner.values() {
            if entry.match_reg(reg) {
                ret.push(entry);
            }
        }
        ret
    }
    pub fn find_entries_for_reg_mut(&mut self, reg: MuID) -> Vec<&mut RegisterEntry> {
        let mut ret = vec![];
92
93
        for entry in self.inner.values_mut() {
            if entry.match_reg(reg) {
qinsoon's avatar
qinsoon committed
94
                ret.push(entry)
95
96
            }
        }
qinsoon's avatar
qinsoon committed
97
98
        ret
    }
99

qinsoon's avatar
qinsoon committed
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
    pub fn has_entries_for_mem(&self, mem: P<Value>) -> bool {
        for entry in self.inner.values() {
            if entry.match_stack_loc(mem.clone()) {
                return true;
            }
        }
        false
    }
    pub fn find_entries_for_mem(&self, mem: P<Value>) -> Vec<&RegisterEntry> {
        let mut ret = vec![];
        for entry in self.inner.values() {
            if entry.match_stack_loc(mem.clone()) {
                ret.push(entry)
            }
        }
        ret
    }
    pub fn find_entries_for_mem_mut(&mut self, mem: P<Value>) -> Vec<&mut RegisterEntry> {
        let mut ret = vec![];
        for entry in self.inner.values_mut() {
            if entry.match_stack_loc(mem.clone()) {
                ret.push(entry)
            }
        }
        ret
125
126
    }

qinsoon's avatar
qinsoon committed
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
    pub fn new_alive_reg(&mut self, reg: MuID) {
        debug!("adding alive reg: {}", reg);

        let id = self.new_index();
        let entry = RegisterEntry {
            temp : None,
            real : vec![reg],
            stack: vec![]
        };

        self.inner.insert(id, entry);
    }

    pub fn new_alive_mem(&mut self, mem: P<Value>) {
        debug!("adding alive mem: {}", mem);

        let id = self.new_index();
        let entry = RegisterEntry {
            temp : None,
            real : vec![],
            stack: vec![mem]
        };

        self.inner.insert(id, entry);
    }

    pub fn add_temp_in_reg(&mut self, temp: MuID, reg: MuID) {
        debug!("adding alive temp in reg: {} in {}", temp, reg);

qinsoon's avatar
qinsoon committed
157
        let entry_exists = self.has_entries_for_temp(temp);
158
159

        if entry_exists {
qinsoon's avatar
qinsoon committed
160
161
162
163
            let mut entries = self.find_entries_for_temp_mut(temp);
            for entry in entries {
                entry.add_real_reg(reg);
            }
164
165
166
167
168
169
170
171
172
173
174
175
        } else {
            let id = self.new_index();
            let entry = RegisterEntry {
                temp: Some(temp),
                real: vec![reg],
                stack: vec![]
            };

            self.inner.insert(id, entry);
        }
    }

qinsoon's avatar
qinsoon committed
176
    pub fn add_temp_in_mem(&mut self, temp: MuID, mem: P<Value>) {
qinsoon's avatar
qinsoon committed
177
        debug!("adding alive temp in mem: {} in {}", temp, mem);
qinsoon's avatar
qinsoon committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

        let entry_exists = self.has_entries_for_temp(temp);

        if entry_exists {
            let mut entries = self.find_entries_for_temp_mut(temp);
            for entry in entries {
                entry.add_stack_loc(mem.clone());
            }
        } else {
            let id = self.new_index();
            let entry = RegisterEntry {
                temp: Some(temp),
                real: vec![],
                stack: vec![mem]
            };

            self.inner.insert(id, entry);
        }
    }

198
199
200
201
    pub fn remove_reg(&mut self, reg: MuID) {
        debug!("removing alive reg: {}", reg);
        let mut indices = vec![];

202
        for (index, entry) in self.inner.iter_mut() {
203
            if entry.match_reg(reg) {
204
205
206
207
208
                entry.remove_real(reg);

                if entry.is_empty() {
                    indices.push(*index);
                }
209
210
211
212
213
214
215
216
            }
        }

        for index in indices {
            self.inner.remove(&index);
        }
    }

217
218
    pub fn remove_temp(&mut self, temp: MuID) {
        debug!("removing alive temp: {}", temp);
219

220
        let mut ret = vec![];
221

222
223
224
        for (index, entry) in self.inner.iter() {
            if entry.match_temp(temp) {
                ret.push(*index);
225
            }
226
        }
227

228
229
230
231
232
233
234
        if ret.len() == 0 {
            return;
        } else if ret.len() == 1 {
            self.inner.remove(&ret[0]);
        } else {
            panic!("Temp{} has more than one entry in AliveEntries");
        }
235
    }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

    pub fn intersect(&mut self, another: &Self) -> bool {
        let mut entries_to_delete : Vec<EntryID> = vec![];

        let mut changed = false;

        for (index, entry) in self.inner.iter_mut() {
            if entry.has_temp() {
                let temp = entry.get_temp().unwrap();

                // find entry with the same temp in the other set, and do intersect
                for another_entry in another.find_entries_for_temp(temp) {
                    if entry.intersect(another_entry) {
                        changed = true;
                    }
                }
            } else {
                // find entry without a temp in the other set and do intersect
                for another_entry in another.inner.values() {
                    if !another_entry.has_temp() {
                        if entry.intersect(another_entry) {
                            changed = true;
                        }
                    }
                }
            }
        }

        changed
    }
qinsoon's avatar
qinsoon committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

    pub fn preserve_list(&mut self, list: &Vec<MuID>) {
        let mut indices_to_delete : Vec<EntryID> = vec![];

        for (index, entry) in self.inner.iter() {
            if !entry.has_temp() {
                if list.iter().any(|x| entry.match_reg(*x)) {

                } else {
                    indices_to_delete.push(*index);
                }
            } else {
                let temp = entry.get_temp().unwrap();
                if !vec_utils::find_value(list, temp).is_some() {
                    indices_to_delete.push(*index);
                }
            }
        }

        // always preserve entries with spill location
        let mut indices_to_really_delete : Vec<EntryID> = vec![];
        for index in indices_to_delete {
            let entry = self.inner.get(&index).unwrap();

            if !entry.has_stack_slots() {
                indices_to_really_delete.push(index);
            }
        }

        // delete
        for index in indices_to_really_delete {
            self.inner.remove(&index);
        }
    }
300
301
}

302
#[derive(Clone)]
303
304
305
306
307
308
309
pub struct RegisterEntry {
    temp  : Option<MuID>,
    real  : Vec<MuID>,
    stack : Vec<P<Value>>
}

impl RegisterEntry {
310
311
312
313
    pub fn is_empty(&self) -> bool {
        !self.has_real() && !self.has_stack_slots()
    }

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    pub fn has_temp(&self) -> bool {
        self.temp.is_some()
    }
    pub fn has_real(&self) -> bool {
        !self.real.is_empty()
    }
    pub fn has_stack_slots(&self) -> bool {
        !self.stack.is_empty()
    }

    pub fn set_temp(&mut self, temp: MuID) {
        self.temp = Some(temp);
    }
    pub fn get_temp(&self) -> Option<MuID> {
        self.temp.clone()
    }

qinsoon's avatar
qinsoon committed
331
332
333
334
335
336
337
338
339
340
341
342
    pub fn remove_real(&mut self, reg: MuID) {
        if let Some(index) = vec_utils::find_value(&self.real, reg) {
            self.real.remove(index);
        }
    }

    pub fn remove_stack_loc(&mut self, mem: P<Value>) {
        if let Some(index) = vec_utils::find_value(&self.stack, mem) {
            self.stack.remove(index);
        }
    }

343
344
345
346
347
348
349
350
351
352
353
354
    pub fn match_temp(&self, temp: MuID) -> bool {
        if self.temp.is_some() && self.temp.unwrap() == temp {
            true
        } else {
            false
        }
    }

    pub fn match_reg(&self, reg: MuID) -> bool {
        vec_utils::find_value(&self.real, reg).is_some()
    }

qinsoon's avatar
qinsoon committed
355
356
357
358
    pub fn match_stack_loc(&self, mem: P<Value>) -> bool {
        vec_utils::find_value(&self.stack, mem).is_some()
    }

359
360
361
362
363
    pub fn add_real_reg(&mut self, reg: MuID) {
        if vec_utils::find_value(&mut self.real, reg).is_none() {
            self.real.push(reg);
        }
    }
qinsoon's avatar
qinsoon committed
364
365
366
367
368
369

    pub fn add_stack_loc(&mut self, mem: P<Value>) {
        if vec_utils::find_value(&mut self.stack, mem.clone()).is_none() {
            self.stack.push(mem)
        }
    }
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391

    // two entries can intersect only when they have the same temp, or they do not have temps
    pub fn intersect(&mut self, another: &Self) -> bool {
        assert!(
            (!self.has_temp() && !another.has_temp()
            || (self.has_temp() && another.has_temp() && self.get_temp().unwrap() == another.get_temp().unwrap()))
        );

        let mut changed = false;

        // intersect real registers
        if vec_utils::intersect(&mut self.real, &another.real) {
            changed = true;
        }

        // intersect memory
        if vec_utils::intersect(&mut self.stack, &another.stack) {
            changed = true;
        }

        changed
    }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
}

impl fmt::Display for RegisterEntry {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let temp = match self.temp {
            Some(id) => format!("{}", id),
            None     => "_".to_string()
        };

        let real = format!("{:?}", self.real);
        let stack = format!("{:?}", self.stack);

        write!(f, "| {:20} | {:20} | {:20} |", temp, real, stack)
    }
}