annotate python/registerallocator.py @ 173:c1d2b6b9f9a7

Rework into passes
author Windel Bouwman
date Fri, 19 Apr 2013 12:42:21 +0200
parents
children a51b3c956386
rev   line source
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
1
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
2 class RegisterAllocator:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
3 def live_analyze(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
4 # Determine liveness:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
5 for i in self.Instructions:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
6 i.live_in = set()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
7 i.live_out = set()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
8 for z in range(50):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
9 # TODO iterate until converge
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
10 for i in self.Instructions:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
11 lo_mk = i.live_out.difference(i.defs)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
12 i.live_in = i.uses.union(lo_mk)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
13 lo = set()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
14 for s in i.succ:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
15 lo = lo.union(s.live_in)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
16 i.live_out = lo
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
17 def registerAllocate(self, ir, regs):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
18 print(regs)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
19 allVals = []
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
20 # construct interference:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
21 for i in ir.Instructions:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
22 for v in i.live_in:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
23 allVals.append(v)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
24 for v2 in i.live_in:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
25 if v != v2:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
26 v.interferes.add(v2)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
27 # assign random registers:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
28 print(allVals)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
29 regs = set(regs)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
30 for v in allVals:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
31 takenregs = set([iv.reg for iv in v.interferes])
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
32 r2 = list(regs.difference(takenregs))
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
33 # Pick next available:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
34 v.reg = r2[0]
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
35