annotate python/ir/builder.py @ 186:46d62dadd61b

Improved testsuite
author Windel Bouwman
date Sat, 25 May 2013 14:26:25 +0200
parents 3eb06f5fb987
children de3a68f677a5
rev   line source
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
1 from . import Value, BasicBlock, Function
171
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()
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
12 def gen(self, prefix=None):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
13 if not prefix:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
14 prefix = self.prefix
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
15 return '{0}{1}'.format(prefix, self.nums.__next__())
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
16
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
17 class ValueGenerator(NameGenerator):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
18 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
19 super().__init__('t')
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
20 def gen(self, prefix=None):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 172
diff changeset
21 v = Value(super().gen(prefix))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
22 return v
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
23
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
24 class BBGenerator(NameGenerator):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
25 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
26 super().__init__('lab')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
27 def gen(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
28 v = BasicBlock(super().gen())
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
29 return v
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
30
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
31 class Builder:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
32 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
33 self.newTmp = ValueGenerator().gen
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
34 self.newBBint = BBGenerator().gen
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
35 self.bb = None
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
36 self.m = None
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
37 self.fn = None
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
38
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
39 # Helpers:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
40 def newBB(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
41 bb = self.newBBint()
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
42 self.fn.addBB(bb)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
43 return bb
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
44 def setModule(self, m):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
45 self.m = m
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
46 def newFunc(self, name):
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
47 f = Function(name)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
48 self.m.addFunc(f)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
49 return f
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
50 def setFunc(self, f):
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
51 self.fn = f
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
52 def setBB(self, bb):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
53 self.bb = bb
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
54 def addIns(self, i):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 174
diff changeset
55 if not self.bb:
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 174
diff changeset
56 raise Exception('No basic block')
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
57 self.bb.addIns(i)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
58