comparison 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
comparison
equal deleted inserted replaced
176:5fd02aa38b42 177:460db5669efa
41 def getParent(self): 41 def getParent(self):
42 return self.parent 42 return self.parent
43 def setParent(self, p): 43 def setParent(self, p):
44 self.parent = p 44 self.parent = p
45 Parent = property(getParent, setParent) 45 Parent = property(getParent, setParent)
46
47 class Terminator(Instruction):
46 @property 48 @property
47 def Targets(self): 49 def Targets(self):
48 return self.getTargets() 50 return self.getTargets()
51 def changeTarget(self, tfrom, tto):
52 pass
49 53
50 # Function calling: 54 # Function calling:
51 class Call(Instruction): 55 class Call(Instruction):
52 def __init__(self, callee, arguments, result=None): 56 def __init__(self, callee, arguments, result=None):
53 super().__init__() 57 super().__init__()
67 else: 71 else:
68 pfx = '' 72 pfx = ''
69 args = ','.join([str(arg) for arg in self.arguments]) 73 args = ','.join([str(arg) for arg in self.arguments])
70 return pfx + '{0}({1})'.format(self.callee.name, args) 74 return pfx + '{0}({1})'.format(self.callee.name, args)
71 75
72 class Return(Instruction): 76 class Return(Terminator):
73 def __init__(self, value=None): 77 def __init__(self, value=None):
74 super().__init__() 78 super().__init__()
75 self.value = value 79 self.value = value
76 if value: 80 if value:
77 self.addUse(value) 81 self.addUse(value)
141 self.addUse(location) 145 self.addUse(location)
142 def __repr__(self): 146 def __repr__(self):
143 return '[{0}] = {1}'.format(self.location, self.value) 147 return '[{0}] = {1}'.format(self.location, self.value)
144 148
145 # Branching: 149 # Branching:
146 class Branch(Instruction): 150 class Branch(Terminator):
147 def __init__(self, target): 151 def __init__(self, target):
148 super().__init__() 152 super().__init__()
149 assert type(target) is BasicBlock 153 assert type(target) is BasicBlock
150 self.target = target 154 self.target = target
151 def __repr__(self): 155 def __repr__(self):
152 return 'BRANCH {0}'.format(self.target) 156 return 'BRANCH {0}'.format(self.target)
153 def getTargets(self): 157 def getTargets(self):
154 return [self.target] 158 return [self.target]
159 def changeTarget(self, tfrom, tto):
160 if tfrom is self.target:
161 self.target = tto
155 162
156 class ConditionalBranch(Instruction): 163 class ConditionalBranch(Terminator):
157 def __init__(self, a, cond, b, lab1, lab2): 164 def __init__(self, a, cond, b, lab1, lab2):
158 super().__init__() 165 super().__init__()
159 self.a = a 166 self.a = a
160 assert type(a) is Value 167 assert type(a) is Value
161 self.cond = cond 168 self.cond = cond
169 self.lab2 = lab2 176 self.lab2 = lab2
170 def __repr__(self): 177 def __repr__(self):
171 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2) 178 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
172 def getTargets(self): 179 def getTargets(self):
173 return [self.lab1, self.lab2] 180 return [self.lab1, self.lab2]
181 def changeTarget(self, tfrom, tto):
182 if tfrom is self.lab1:
183 self.lab1 = tto
184 if tfrom is self.lab2:
185 self.lab2 = tto
174 186
175 class PhiNode(Instruction): 187 class PhiNode(Instruction):
176 def __init__(self): 188 def __init__(self):
177 super().__init__() 189 super().__init__()
178 self.incBB = [] 190 self.incBB = []