annotate python/ir/builder.py @ 171:3eb9b9e2958d

Improved IR code
author Windel Bouwman
date Wed, 03 Apr 2013 22:20:20 +0200
parents
children 5a7d37d615ee
rev   line source
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
1 from . import Value, BasicBlock
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
2
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
3 class NameGenerator:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
4 def __init__(self, prefix):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
5 self.prefix = prefix
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
6 def NumGen():
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
7 a = 0
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
8 while True:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
9 yield a
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
10 a = a + 1
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
11 self.nums = NumGen()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
12 def gen(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
13 return '{0}{1}'.format(self.prefix, self.nums.__next__())
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
14
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
15 class ValueGenerator(NameGenerator):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
16 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
17 super().__init__('t')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
18 def gen(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
19 v = Value(super().gen())
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
20 return v
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
21
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
22 class BBGenerator(NameGenerator):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
23 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
24 super().__init__('lab')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
25 def gen(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
26 v = BasicBlock(super().gen())
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
27 return v
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
28
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
29 class Builder:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
30 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
31 self.newTmp = ValueGenerator().gen
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
32 self.newBBint = BBGenerator().gen
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
33 self.bb = None
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
34 self.m = None
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
35
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
36 # Helpers:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
37 def newBB(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
38 bb = self.newBBint()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
39 self.m.addBB(bb)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
40 return bb
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
41 def setModule(self, m):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
42 self.m = m
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
43 def setBB(self, bb):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
44 self.bb = bb
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
45 def addIns(self, i):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
46 self.bb.addIns(i)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
47