comparison python/transform.py @ 174:3eb06f5fb987

Added memory alloc for locals
author Windel Bouwman
date Fri, 19 Apr 2013 19:22:52 +0200
parents c1d2b6b9f9a7
children a51b3c956386
comparison
equal deleted inserted replaced
173:c1d2b6b9f9a7 174:3eb06f5fb987
18 """ Override this virtual method """ 18 """ Override this virtual method """
19 raise NotImplementedError() 19 raise NotImplementedError()
20 20
21 class InstructionPass(BasicBlockPass): 21 class InstructionPass(BasicBlockPass):
22 def onBasicBlock(self, bb): 22 def onBasicBlock(self, bb):
23 for ins in bb.Instructions: 23 for ins in iter(bb.Instructions):
24 self.onInstruction(ins) 24 self.onInstruction(ins)
25 def onInstruction(self, ins): 25 def onInstruction(self, ins):
26 """ Override this virtual method """ 26 """ Override this virtual method """
27 raise NotImplementedError() 27 raise NotImplementedError()
28 28
48 elif op == '-': 48 elif op == '-':
49 i2 = ImmLoad(i.result, a.constval - b.constval) 49 i2 = ImmLoad(i.result, a.constval - b.constval)
50 print(i2) 50 print(i2)
51 i.Parent.replaceInstruction(i, i2) 51 i.Parent.replaceInstruction(i, i2)
52 52
53 class DeadCodeDeleter(InstructionPass): 53 class DeadCodeDeleter(BasicBlockPass):
54 def onInstruction(self, ins): 54 def onBasicBlock(self, bb):
55 print(ins, ins.Users) 55 def instructionUsed(ins):
56 if not ins.defs in ins.live_out: 56 if len(ins.defs) == 0:
57 ins.Parent.removeInstruction(ins) 57 # In case this instruction does not define any variables, assume it is usefull.
58 return True
59 for d in ins.defs:
60 if d.IsUsed:
61 return True
62 return False
63 bb.Instructions = list(filter(instructionUsed, bb.Instructions))
58 64