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()
|
|
12 def gen(self):
|
|
13 return '{0}{1}'.format(self.prefix, self.nums.__next__())
|
|
14
|
|
15 class ValueGenerator(NameGenerator):
|
|
16 def __init__(self):
|
|
17 super().__init__('t')
|
|
18 def gen(self):
|
|
19 v = Value(super().gen())
|
|
20 return v
|
|
21
|
|
22 class BBGenerator(NameGenerator):
|
|
23 def __init__(self):
|
|
24 super().__init__('lab')
|
|
25 def gen(self):
|
|
26 v = BasicBlock(super().gen())
|
|
27 return v
|
|
28
|
|
29 class Builder:
|
|
30 def __init__(self):
|
|
31 self.newTmp = ValueGenerator().gen
|
|
32 self.newBBint = BBGenerator().gen
|
|
33 self.bb = None
|
|
34 self.m = None
|
172
|
35 self.fn = None
|
171
|
36
|
|
37 # Helpers:
|
|
38 def newBB(self):
|
|
39 bb = self.newBBint()
|
172
|
40 self.fn.addBB(bb)
|
171
|
41 return bb
|
|
42 def setModule(self, m):
|
|
43 self.m = m
|
172
|
44 def newFunc(self, name):
|
|
45 f = Function(name)
|
|
46 self.m.addFunc(f)
|
|
47 return f
|
|
48 def setFunc(self, f):
|
|
49 self.fn = f
|
171
|
50 def setBB(self, bb):
|
|
51 self.bb = bb
|
|
52 def addIns(self, i):
|
|
53 self.bb.addIns(i)
|
|
54
|