annotate python/registerallocator.py @ 220:3f6c30a5d234

Major change in expression parsing to enable pointers and structs
author Windel Bouwman
date Sat, 06 Jul 2013 21:32:20 +0200
parents a51b3c956386
children 5f8c04a8d26b
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 allVals = []
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
19 # construct interference:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
20 for i in ir.Instructions:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
21 for v in i.live_in:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
22 allVals.append(v)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
23 for v2 in i.live_in:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
24 if v != v2:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
25 v.interferes.add(v2)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
26 # assign random registers:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
27 regs = set(regs)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
28 for v in allVals:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
29 takenregs = set([iv.reg for iv in v.interferes])
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
30 r2 = list(regs.difference(takenregs))
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
31 # Pick next available:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
32 v.reg = r2[0]
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
33