Find the longest chain of routines in R and remove it.
Chris Pressey
7 years ago
9 | 9 | if sub not in s: |
10 | 10 | s.add(sub) |
11 | 11 | make_transitive_closure(d, sub, s) |
12 | ||
13 | ||
14 | def find_chains(d, key, pred): | |
15 | chains = [] | |
16 | for sub in d.get(key, []): | |
17 | if pred(sub): | |
18 | subchains = find_chains(d, sub, pred) | |
19 | for subchain in subchains: | |
20 | chains.append([key] + subchain) | |
21 | chains.append([key]) | |
22 | return chains | |
12 | 23 | |
13 | 24 | |
14 | 25 | class FallthruAnalyzer(object): |
62 | 73 | self.fall_out_map[routine.name] = None |
63 | 74 | |
64 | 75 | routine_list = [] |
65 | fall_out_map = copy(self.fall_out_map) | |
66 | while fall_out_map: | |
67 | key = fall_out_map.keys()[0] | |
76 | pending_routines = copy(self.fall_out_map) | |
77 | while pending_routines: | |
78 | # Pick a routine that is still pending to be serialized. | |
79 | key = pending_routines.keys()[0] | |
80 | ||
68 | 81 | in_set = self.fall_in_map.get(key, []) |
69 | # Find the longest chain of routines r1,r2,...rn in R where out(r1) = {r2}, out(r2} = {r3}, ... out(rn-1) = {rn}, and rn = r. | |
70 | # TODO implement this | |
71 | routines = [key] | |
72 | 82 | |
73 | # Remove (r1,r2,...,rn) from R and append them to L in that order. Mark (r1,r2,...rn-1) as "will have their final goto removed." | |
83 | # Find the longest chain of routines r1,r2,...rn in R | |
84 | # where out(r1) = {r2}, out(r2} = {r3}, ... out(rn-1) = {rn}, and rn = r. | |
85 | chains = find_chains(self.fall_in_map, key, lambda k: k in pending_routines) | |
86 | chains.sort(key=len, reverse=True) | |
87 | routines = chains[0] | |
88 | ||
89 | # Remove (r1,r2,...,rn) from R and append them to L in that order. | |
90 | # Mark (r1,r2,...rn-1) as "will have their final goto removed." | |
74 | 91 | for r in routines: |
75 | del fall_out_map[r] | |
92 | del pending_routines[r] | |
76 | 93 | if r == routines[-1]: |
77 | 94 | routine_list.append(['retain', r]) |
78 | 95 | else: |