172
|
1 from . import Value, BasicBlock, Function
|
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
|
|
44 def setModule(self, m):
|
|
45 self.m = m
|
172
|
46 def newFunc(self, name):
|
|
47 f = Function(name)
|
|
48 self.m.addFunc(f)
|
|
49 return f
|
|
50 def setFunc(self, f):
|
|
51 self.fn = f
|
171
|
52 def setBB(self, bb):
|
|
53 self.bb = bb
|
|
54 def addIns(self, i):
|
186
|
55 if not self.bb:
|
|
56 raise Exception('No basic block')
|
171
|
57 self.bb.addIns(i)
|
|
58
|