15 | 15 |
self.debug = debug
|
16 | 16 |
|
17 | 17 |
def analyze_program(self, program):
|
18 | |
fallthru_map = {}
|
|
18 |
fall_in_map = {}
|
19 | 19 |
for routine in program.routines:
|
20 | 20 |
encountered_gotos = list(routine.encountered_gotos)
|
21 | 21 |
if len(encountered_gotos) == 1 and isinstance(encountered_gotos[0].type, RoutineType):
|
22 | |
fallthru_map.setdefault(encountered_gotos[0].name, set()).add(routine.name)
|
23 | |
self.fallthru_map = dict([(k, sorted(v)) for k, v in fallthru_map.iteritems()])
|
24 | |
return self.fallthru_map
|
|
22 |
fall_in_map.setdefault(encountered_gotos[0].name, set()).add(routine.name)
|
|
23 |
self.fall_in_map = dict([(k, sorted(v)) for k, v in fall_in_map.iteritems()])
|
|
24 |
return self.fall_in_map
|
25 | 25 |
|
26 | 26 |
def find_cycles(self):
|
27 | 27 |
self.ancestor_map = {}
|
28 | |
for key in self.fallthru_map:
|
|
28 |
for key in self.fall_in_map:
|
29 | 29 |
ancestors = set()
|
30 | |
make_transitive_closure(self.fallthru_map, key, ancestors)
|
|
30 |
make_transitive_closure(self.fall_in_map, key, ancestors)
|
31 | 31 |
self.ancestor_map[key] = sorted(ancestors)
|
32 | 32 |
|
33 | 33 |
self.cycles_found = set()
|
|
41 | 41 |
cycle_to_break = sorted(self.cycles_found)[0]
|
42 | 42 |
cycles_to_break = set([cycle_to_break])
|
43 | 43 |
|
44 | |
new_fallthru_map = {}
|
45 | |
for key in self.fallthru_map:
|
46 | |
values = set(self.fallthru_map[key]) - cycles_to_break
|
|
44 |
new_fall_in_map = {}
|
|
45 |
for key in self.fall_in_map:
|
|
46 |
values = set(self.fall_in_map[key]) - cycles_to_break
|
47 | 47 |
if values:
|
48 | |
new_fallthru_map[key] = sorted(values)
|
49 | |
self.fallthru_map = new_fallthru_map
|
|
48 |
new_fall_in_map[key] = sorted(values)
|
|
49 |
self.fall_in_map = new_fall_in_map
|
50 | 50 |
|
51 | 51 |
def serialize(self):
|
52 | 52 |
raise NotImplementedError
|