205
|
1 from . import Value, BasicBlock, Function, Variable
|
171
|
2
|
|
3 class NameGenerator:
|
|
4 def __init__(self, prefix):
|
|
5 self.prefix = prefix
|
|
6 def NumGen():
|
|
7 a = 0
|
|
8 while True:
|
|
9 yield a
|
|
10 a = a + 1
|
|
11 self.nums = NumGen()
|
174
|
12 def gen(self, prefix=None):
|
|
13 if not prefix:
|
|
14 prefix = self.prefix
|
|
15 return '{0}{1}'.format(prefix, self.nums.__next__())
|
171
|
16
|
|
17 class ValueGenerator(NameGenerator):
|
|
18 def __init__(self):
|
|
19 super().__init__('t')
|
174
|
20 def gen(self, prefix=None):
|
|
21 v = Value(super().gen(prefix))
|
171
|
22 return v
|
|
23
|
|
24 class BBGenerator(NameGenerator):
|
|
25 def __init__(self):
|
|
26 super().__init__('lab')
|
|
27 def gen(self):
|
|
28 v = BasicBlock(super().gen())
|
|
29 return v
|
|
30
|
|
31 class Builder:
|
|
32 def __init__(self):
|
|
33 self.newTmp = ValueGenerator().gen
|
|
34 self.newBBint = BBGenerator().gen
|
|
35 self.bb = None
|
|
36 self.m = None
|
172
|
37 self.fn = None
|
171
|
38
|
|
39 # Helpers:
|
|
40 def newBB(self):
|
|
41 bb = self.newBBint()
|
172
|
42 self.fn.addBB(bb)
|
171
|
43 return bb
|
205
|
44
|
171
|
45 def setModule(self, m):
|
|
46 self.m = m
|
205
|
47
|
204
|
48 def newFunction(self, name):
|
172
|
49 f = Function(name)
|
|
50 self.m.addFunc(f)
|
|
51 return f
|
205
|
52
|
|
53 def newVariable(self, name):
|
|
54 v = Variable(name)
|
|
55 self.m.addVariable(v)
|
|
56 return v
|
|
57
|
204
|
58 def setFunction(self, f):
|
172
|
59 self.fn = f
|
205
|
60
|
171
|
61 def setBB(self, bb):
|
|
62 self.bb = bb
|
205
|
63
|
171
|
64 def addIns(self, i):
|
186
|
65 if not self.bb:
|
|
66 raise Exception('No basic block')
|
171
|
67 self.bb.addIns(i)
|
|
68
|