Mercurial > lcfOS
comparison python/ir/builder.py @ 171:3eb9b9e2958d
Improved IR code
author | Windel Bouwman |
---|---|
date | Wed, 03 Apr 2013 22:20:20 +0200 |
parents | |
children | 5a7d37d615ee |
comparison
equal
deleted
inserted
replaced
170:4348da5ca307 | 171:3eb9b9e2958d |
---|---|
1 from . import Value, BasicBlock | |
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 | |
35 | |
36 # Helpers: | |
37 def newBB(self): | |
38 bb = self.newBBint() | |
39 self.m.addBB(bb) | |
40 return bb | |
41 def setModule(self, m): | |
42 self.m = m | |
43 def setBB(self, bb): | |
44 self.bb = bb | |
45 def addIns(self, i): | |
46 self.bb.addIns(i) | |
47 |