diff python/ir/instruction.py @ 177:460db5669efa

Added clean pass for IR
author Windel Bouwman
date Mon, 22 Apr 2013 23:54:54 +0200
parents 5fd02aa38b42
children de3a68f677a5
line wrap: on
line diff
--- a/python/ir/instruction.py	Sat Apr 20 12:00:51 2013 +0200
+++ b/python/ir/instruction.py	Mon Apr 22 23:54:54 2013 +0200
@@ -43,9 +43,13 @@
    def setParent(self, p):
       self.parent = p
    Parent = property(getParent, setParent)
+
+class Terminator(Instruction):
    @property
    def Targets(self):
       return self.getTargets()
+   def changeTarget(self, tfrom, tto):
+      pass
 
 # Function calling:
 class Call(Instruction):
@@ -69,7 +73,7 @@
       args = ','.join([str(arg) for arg in self.arguments])
       return pfx + '{0}({1})'.format(self.callee.name, args)
 
-class Return(Instruction):
+class Return(Terminator):
    def __init__(self, value=None):
       super().__init__()
       self.value = value
@@ -143,7 +147,7 @@
       return '[{0}] = {1}'.format(self.location, self.value)
 
 # Branching:
-class Branch(Instruction):
+class Branch(Terminator):
    def __init__(self, target):
       super().__init__()
       assert type(target) is BasicBlock
@@ -152,8 +156,11 @@
       return 'BRANCH {0}'.format(self.target)
    def getTargets(self):
       return [self.target]
+   def changeTarget(self, tfrom, tto):
+      if tfrom is self.target:
+         self.target = tto
 
-class ConditionalBranch(Instruction):
+class ConditionalBranch(Terminator):
    def __init__(self, a, cond, b, lab1, lab2):
       super().__init__()
       self.a = a
@@ -171,6 +178,11 @@
       return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
    def getTargets(self):
       return [self.lab1, self.lab2]
+   def changeTarget(self, tfrom, tto):
+      if tfrom is self.lab1:
+         self.lab1 = tto
+      if tfrom is self.lab2:
+         self.lab2 = tto
 
 class PhiNode(Instruction):
    def __init__(self):