Mercurial > lcfOS
comparison python/transform.py @ 176:5fd02aa38b42
Added while loop code generation
author | Windel Bouwman |
---|---|
date | Sat, 20 Apr 2013 12:00:51 +0200 |
parents | a51b3c956386 |
children | 460db5669efa |
comparison
equal
deleted
inserted
replaced
175:a51b3c956386 | 176:5fd02aa38b42 |
---|---|
2 # Standard passes: | 2 # Standard passes: |
3 | 3 |
4 class FunctionPass: | 4 class FunctionPass: |
5 def run(self, ir): | 5 def run(self, ir): |
6 """ Main entry point for the pass """ | 6 """ Main entry point for the pass """ |
7 self.prepare() | |
7 for f in ir.Functions: | 8 for f in ir.Functions: |
8 self.onFunction(f) | 9 self.onFunction(f) |
9 def onFunction(self, f): | 10 def onFunction(self, f): |
10 """ Override this virtual method """ | 11 """ Override this virtual method """ |
11 raise NotImplementedError() | 12 raise NotImplementedError() |
13 def prepare(self): | |
14 pass | |
12 | 15 |
13 class BasicBlockPass(FunctionPass): | 16 class BasicBlockPass(FunctionPass): |
14 def onFunction(self, f): | 17 def onFunction(self, f): |
15 for bb in f.BasicBlocks: | 18 for bb in f.BasicBlocks: |
16 self.onBasicBlock(bb) | 19 self.onBasicBlock(bb) |
26 """ Override this virtual method """ | 29 """ Override this virtual method """ |
27 raise NotImplementedError() | 30 raise NotImplementedError() |
28 | 31 |
29 # Usefull transforms: | 32 # Usefull transforms: |
30 class ConstantFolder(InstructionPass): | 33 class ConstantFolder(InstructionPass): |
34 def prepare(self): | |
35 self.constMap = {} | |
31 def onInstruction(self, i): | 36 def onInstruction(self, i): |
32 if type(i) is ImmLoad: | 37 if type(i) is ImmLoad: |
33 i.target.constval = i.value | 38 self.constMap[i.target] = i.value |
34 elif type(i) is BinaryOperator: | 39 elif type(i) is BinaryOperator: |
35 a = i.value1 | 40 if i.value1 in self.constMap and i.value2 in self.constMap: |
36 b = i.value2 | |
37 if hasattr(a, 'constval') and hasattr(b,'constval'): | |
38 op = i.operation | 41 op = i.operation |
42 va = self.constMap[i.value1] | |
43 vb = self.constMap[i.value2] | |
39 if op == '+': | 44 if op == '+': |
40 i2 = ImmLoad(i.result, a.constval + b.constval) | 45 vr = va + vb |
41 i.Parent.replaceInstruction(i, i2) | |
42 elif op == '*': | 46 elif op == '*': |
43 i2 = ImmLoad(i.result, a.constval * b.constval) | 47 vr = va * vb |
44 i.Parent.replaceInstruction(i, i2) | |
45 elif op == '-': | 48 elif op == '-': |
46 i2 = ImmLoad(i.result, a.constval - b.constval) | 49 vr = va - vb |
47 i.Parent.replaceInstruction(i, i2) | 50 else: |
51 vr = None | |
52 return | |
53 self.constMap[i.result] = vr | |
54 i2 = ImmLoad(i.result, vr) | |
55 i.Parent.replaceInstruction(i, i2) | |
48 | 56 |
49 class DeadCodeDeleter(BasicBlockPass): | 57 class DeadCodeDeleter(BasicBlockPass): |
50 def onBasicBlock(self, bb): | 58 def onBasicBlock(self, bb): |
51 def instructionUsed(ins): | 59 def instructionUsed(ins): |
52 if len(ins.defs) == 0: | 60 if len(ins.defs) == 0: |
69 otherUse = True | 77 otherUse = True |
70 return True | 78 return True |
71 | 79 |
72 class Mem2RegPromotor(FunctionPass): | 80 class Mem2RegPromotor(FunctionPass): |
73 def onFunction(self, f): | 81 def onFunction(self, f): |
82 # TODO | |
74 print(f) | 83 print(f) |
75 | 84 |