annotate python/ir/builder.py @ 252:c4370696ccc7

added optimize function
author Windel Bouwman
date Tue, 30 Jul 2013 17:57:46 +0200
parents e41e4109addd
children 5ec7580976d9
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:
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
32 def __init__(self):
171
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
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
38 self.loc = None
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
39
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
40 # Helpers:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
41 def newBB(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
42 bb = self.newBBint()
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
43 self.fn.addBB(bb)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
44 return bb
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
45
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
46 def setModule(self, m):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
47 self.m = m
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
48
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
49 def newFunction(self, name):
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
50 f = Function(name)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
51 self.m.addFunc(f)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
52 return f
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
53
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
54 def newVariable(self, name):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
55 v = Variable(name)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
56 self.m.addVariable(v)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
57 return v
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
58
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
59 def setFunction(self, f):
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
60 self.fn = f
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
61
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
62 def setBB(self, bb):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
63 self.bb = bb
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
64
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
65 def setLoc(self, l):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
66 self.loc = l
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
67
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
68 def addIns(self, i):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
69 i.debugLoc = self.loc
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
70 if not self.bb:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 174
diff changeset
71 raise Exception('No basic block')
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
72 self.bb.addInstruction(i)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
73