annotate python/ir/builder.py @ 235:ff40407c0240

Fix ALabel to Label
author Windel Bouwman
date Mon, 15 Jul 2013 17:20:37 +0200
parents d77cb5962cc5
children ef683881c64e
rev   line source
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
1 from . import Value, BasicBlock, Function, Variable
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
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
44
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
45 def setModule(self, m):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
46 self.m = m
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
47
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
48 def newFunction(self, name):
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
49 f = Function(name)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
50 self.m.addFunc(f)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
51 return f
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
52
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
53 def newVariable(self, name):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
54 v = Variable(name)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
55 self.m.addVariable(v)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
56 return v
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
57
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 186
diff changeset
58 def setFunction(self, f):
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
59 self.fn = f
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
60
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
61 def setBB(self, bb):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
62 self.bb = bb
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
63
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
64 def addIns(self, i):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 174
diff changeset
65 if not self.bb:
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 174
diff changeset
66 raise Exception('No basic block')
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
67 self.bb.addIns(i)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
68