git @ Cat's Eye Technologies SixtyPical / 30e8390
First cut at serialization of fallthru-optimized routines. Chris Pressey 6 years ago
3 changed file(s) with 33 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
5858 analyzer.analyze_program(program)
5959
6060 if options.optimize_fallthru:
61 from sixtypical.fallthru import FallthruAnalyzer
62
6163 def dump(label, data):
6264 import json
6365 if not options.dump_fallthru_info:
6769 sys.stdout.write(json.dumps(data, indent=4, sort_keys=True))
6870 sys.stdout.write("\n")
6971
70 from sixtypical.fallthru import FallthruAnalyzer
71
7272 fa = FallthruAnalyzer(debug=options.debug)
7373 fa.analyze_program(program)
7474 dump(None, fa.fall_in_map)
8181 fa.break_cycle()
8282 dump('after breaking cycle', fa.fall_in_map)
8383 fa.find_cycles()
84
85 routines_list = fa.serialize()
86 #dump('serialization', routines_list)
8487
8588 if options.analyze_only:
8689 return
00 # encoding: UTF-8
1
2 from copy import copy
13
24 from sixtypical.model import RoutineType
35
4951 self.fall_in_map = new_fall_in_map
5052
5153 def serialize(self):
52 raise NotImplementedError
54 self.fall_out_map = {}
55 for key, values in self.fall_in_map.iteritems():
56 for value in values:
57 assert value not in self.fall_out_map
58 self.fall_out_map[value] = key
59
60 routine_list = []
61 fall_out_map = copy(self.fall_out_map)
62 while fall_out_map:
63 key = fall_out_map.keys()[0]
64 # ...
65 # 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.
66 # 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."
67 #
68 del fall_out_map[key]
69
70 return routine_list
3333
3434 But they do permit cycles.
3535
36 So, we first break those cycles. We will be left with out() sets which
37 are disjoint trees, i.e. if r1 ∈ in(r2), then r1 ∉ in(r3) for all r3 ≠ r2.
36 So, we first break those cycles. (Is there a "best" way to do this?
37 Perhaps. But for now, we just break them arbitrarily; pick a r1 that
38 has a cycle and remove it from in(r2) for all r2. This also means
39 that, now, out(r1) = ∅. Then check if there are still cycles, and keep
40 picking one and breaking it until there are no cycles remaining.)
41
42 We will be left with out() sets which are disjoint trees, i.e.
43 if r1 ∈ in(r2), then r1 ∉ in(r3) for all r3 ≠ r2. Also,
44 out(r1) = ∅ → for all r2, r1 ∉ in(r2).
3845
3946 We then follow an algorithm something like this. Treat R as a mutable
4047 set and start with an empty list L. Then,