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:
|
249
|
32 def __init__(self):
|
171
|
33 self.newTmp = ValueGenerator().gen
|
|
34 self.newBBint = BBGenerator().gen
|
|
35 self.bb = None
|
|
36 self.m = None
|
172
|
37 self.fn = None
|
249
|
38 self.loc = None
|
171
|
39
|
249
|
40 # Helpers:
|
|
41 def newBB(self):
|
171
|
42 bb = self.newBBint()
|
172
|
43 self.fn.addBB(bb)
|
171
|
44 return bb
|
205
|
45
|
249
|
46 def setModule(self, m):
|
171
|
47 self.m = m
|
205
|
48
|
249
|
49 def newFunction(self, name):
|
172
|
50 f = Function(name)
|
|
51 self.m.addFunc(f)
|
|
52 return f
|
205
|
53
|
249
|
54 def newVariable(self, name):
|
205
|
55 v = Variable(name)
|
|
56 self.m.addVariable(v)
|
|
57 return v
|
|
58
|
249
|
59 def setFunction(self, f):
|
172
|
60 self.fn = f
|
205
|
61
|
249
|
62 def setBB(self, bb):
|
171
|
63 self.bb = bb
|
205
|
64
|
249
|
65 def setLoc(self, l):
|
|
66 self.loc = l
|
|
67
|
|
68 def addIns(self, i):
|
|
69 i.debugLoc = self.loc
|
|
70 if not self.bb:
|
186
|
71 raise Exception('No basic block')
|
249
|
72 self.bb.addInstruction(i)
|
171
|
73
|