Mercurial > lcfOS
comparison python/transform.py @ 255:7416c923a02a
Added more logging
author | Windel Bouwman |
---|---|
date | Sun, 04 Aug 2013 15:10:10 +0200 |
parents | 74c6a20302d5 |
children | 444b9df2ed99 |
comparison
equal
deleted
inserted
replaced
254:bd26dc13f270 | 255:7416c923a02a |
---|---|
5 import logging | 5 import logging |
6 from ir import * | 6 from ir import * |
7 # Standard passes: | 7 # Standard passes: |
8 | 8 |
9 class FunctionPass: | 9 class FunctionPass: |
10 def __init__(self): | |
11 self.logger = logging.getLogger('optimize') | |
12 | |
10 def run(self, ir): | 13 def run(self, ir): |
11 """ Main entry point for the pass """ | 14 """ Main entry point for the pass """ |
12 logging.info('Running pass {}'.format(type(self))) | 15 self.logger.info('Running pass {}'.format(type(self))) |
16 ir.check() | |
13 self.prepare() | 17 self.prepare() |
14 for f in ir.Functions: | 18 for f in ir.Functions: |
15 self.onFunction(f) | 19 self.onFunction(f) |
20 ir.check() | |
16 | 21 |
17 def onFunction(self, f): | 22 def onFunction(self, f): |
18 """ Override this virtual method """ | 23 """ Override this virtual method """ |
19 raise NotImplementedError() | 24 raise NotImplementedError() |
20 | 25 |
40 def onInstruction(self, ins): | 45 def onInstruction(self, ins): |
41 """ Override this virtual method """ | 46 """ Override this virtual method """ |
42 raise NotImplementedError() | 47 raise NotImplementedError() |
43 | 48 |
44 # Usefull transforms: | 49 # Usefull transforms: |
45 class ConstantFolder(InstructionPass): | 50 class ConstantFolder(BasicBlockPass): |
46 def prepare(self): | 51 def onBasicBlock(self, bb): |
47 self.constMap = {} | 52 constMap = {} |
48 | 53 ins = [i for i in bb.Instructions if type(i) in [ImmLoad, BinaryOperator]] |
49 def onInstruction(self, i): | 54 for i in ins: |
50 if type(i) is ImmLoad: | 55 if type(i) is ImmLoad: |
51 self.constMap[i.target] = i.value | 56 constMap[i.target] = i.value |
52 elif type(i) is BinaryOperator: | 57 elif type(i) is BinaryOperator: |
53 if i.value1 in self.constMap and i.value2 in self.constMap and i.operation in ['+', '-', '*', '<<']: | 58 if i.value1 in constMap and i.value2 in constMap and i.operation in ['+', '-', '*', '<<']: |
54 op = i.operation | 59 op = i.operation |
55 va = self.constMap[i.value1] | 60 va = constMap[i.value1] |
56 vb = self.constMap[i.value2] | 61 vb = constMap[i.value2] |
57 if op == '+': | 62 if op == '+': |
58 vr = va + vb | 63 vr = va + vb |
59 elif op == '*': | 64 elif op == '*': |
60 vr = va * vb | 65 vr = va * vb |
61 elif op == '-': | 66 elif op == '-': |
62 vr = va - vb | 67 vr = va - vb |
63 elif op == '<<': | 68 elif op == '<<': |
64 vr = va << vb | 69 vr = va << vb |
65 else: | 70 else: |
66 vr = None | 71 raise NotImplementedError() |
67 return | 72 constMap[i.result] = vr |
68 self.constMap[i.result] = vr | 73 i.removeDef(i.result) |
69 i.removeDef(i.result) | 74 i2 = ImmLoad(i.result, vr) |
70 i2 = ImmLoad(i.result, vr) | 75 logging.debug('Replacing {} with {}'.format(i, i2)) |
71 logging.debug('Replacing {}'.format(i)) | 76 i.Parent.replaceInstruction(i, i2) |
72 i.Parent.replaceInstruction(i, i2) | |
73 | 77 |
74 | 78 |
75 class DeadCodeDeleter(BasicBlockPass): | 79 class DeadCodeDeleter(BasicBlockPass): |
76 def onBasicBlock(self, bb): | 80 def onBasicBlock(self, bb): |
77 def instructionUsed(ins): | 81 def instructionUsed(ins): |