annotate python/ir/builder.py @ 172:5a7d37d615ee

Added function to IR
author Windel Bouwman
date Thu, 04 Apr 2013 17:58:37 +0200
parents 3eb9b9e2958d
children 3eb06f5fb987
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()
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
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
35 self.fn = None
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
36
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
37 # Helpers:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
38 def newBB(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
39 bb = self.newBBint()
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
40 self.fn.addBB(bb)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
41 return bb
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
42 def setModule(self, m):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
43 self.m = m
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
44 def newFunc(self, name):
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
45 f = Function(name)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
46 self.m.addFunc(f)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
47 return f
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
48 def setFunc(self, f):
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
49 self.fn = f
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
50 def setBB(self, bb):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
51 self.bb = bb
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
52 def addIns(self, i):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
53 self.bb.addIns(i)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
54