comparison python/ir/instruction.py @ 261:444b9df2ed99

try to split up code generation
author Windel Bouwman
date Fri, 09 Aug 2013 09:05:13 +0200
parents ac603eb66b63
children ed14e077124c
comparison
equal deleted inserted replaced
260:b2f94b4951f1 261:444b9df2ed99
29 return all(use.Block is bb for use in self.used_by) 29 return all(use.Block is bb for use in self.used_by)
30 30
31 def lastUse(self, ins): 31 def lastUse(self, ins):
32 assert ins in self.used_by 32 assert ins in self.used_by
33 return all(not ins.precedes(ub) for ub in self.used_by) 33 return all(not ins.precedes(ub) for ub in self.used_by)
34
35 def replaceby(self, v2):
36 for use_ins in self.used_by:
37 use_ins.replaceValue(self, v2.value)
38 assert not self.Used
34 39
35 class Variable(Value): 40 class Variable(Value):
36 pass 41 pass
37 42
38 43
131 #assert self.Position > v.Setter.Position 136 #assert self.Position > v.Setter.Position
132 137
133 138
134 139
135 140
136 class Terminator(Instruction):
137 @property
138 def Targets(self):
139 return self.getTargets()
140
141 def changeTarget(self, tfrom, tto):
142 pass
143 141
144 142
145 # Function calling: 143 # Function calling:
146 class Call(Instruction): 144 class Call(Instruction):
147 def __init__(self, callee, arguments, result=None): 145 def __init__(self, callee, arguments, result=None):
154 self.addUse(arg) 152 self.addUse(arg)
155 self.result = result 153 self.result = result
156 if result: 154 if result:
157 assert type(result) is Value 155 assert type(result) is Value
158 self.addDef(result) 156 self.addDef(result)
157
159 def __repr__(self): 158 def __repr__(self):
160 if self.result: 159 if self.result:
161 pfx = '{0} = '.format(self.result) 160 pfx = '{0} = '.format(self.result)
162 else: 161 else:
163 pfx = '' 162 pfx = ''
164 args = ','.join([str(arg) for arg in self.arguments]) 163 args = ','.join([str(arg) for arg in self.arguments])
165 return pfx + '{0}({1})'.format(self.callee.name, args) 164 return pfx + '{0}({1})'.format(self.callee.name, args)
166 165
167 class Return(Terminator):
168 def __init__(self, value=None):
169 super().__init__()
170 self.value = value
171 if value:
172 self.addUse(value)
173 def __repr__(self):
174 if self.value:
175 return 'ret {0}'.format(self.value)
176 else:
177 return 'ret'
178 def getTargets(self):
179 return []
180 166
181 class Alloc(Instruction): 167 class Alloc(Instruction):
182 """ Allocates space on the stack """ 168 """ Allocates space on the stack """
183 def __init__(self, value): 169 def __init__(self, value):
184 super().__init__() 170 super().__init__()
185 assert isinstance(value, Value) 171 assert isinstance(value, Value)
186 self.value = value 172 self.value = value
187 self.addDef(value) 173 self.addDef(value)
188 def __repr__(self): 174
189 return '{0} = alloc'.format(self.value) 175 def __repr__(self):
176 return '{0} = alloc'.format(self.value)
177
190 178
191 class ImmLoad(Instruction): 179 class ImmLoad(Instruction):
192 def __init__(self, target, value): 180 def __init__(self, target, value):
193 super().__init__() 181 super().__init__()
194 assert type(target) is Value 182 assert type(target) is Value
195 self.target = target 183 self.target = target
196 self.value = value 184 self.value = value
197 self.addDef(target) 185 self.addDef(target)
198 def __repr__(self): 186
199 return '{} = {}'.format(self.target, self.value) 187 def __repr__(self):
188 return '{} = {}'.format(self.target, self.value)
200 189
201 # Data operations 190 # Data operations
202 class BinaryOperator(Instruction): 191 class BinaryOperator(Instruction):
203 def __init__(self, result, operation, value1, value2): 192 def __init__(self, result, operation, value1, value2):
204 super().__init__() 193 super().__init__()
205 #print('operation is in binops:', operation in BinOps) 194 assert type(value1) is Value, str(value1) + str(type(value1))
206 # Check types of the two operands: 195 assert type(value2) is Value, value2
207 assert type(value1) is Value, str(value1) + str(type(value1)) 196 self.result = result
208 assert type(value2) is Value, value2 197 self.addDef(result)
209 self.result = result 198 self.value1 = value1
210 self.addDef(result) 199 self.value2 = value2
211 self.value1 = value1 200 self.addUse(value1)
212 self.value2 = value2 201 self.addUse(value2)
213 self.addUse(value1) 202 self.operation = operation
214 self.addUse(value2)
215 self.operation = operation
216 203
217 def __repr__(self): 204 def __repr__(self):
218 a, b = self.value1, self.value2 205 a, b = self.value1, self.value2
219 return '{} = {} {} {}'.format(self.result, a, self.operation, b) 206 return '{} = {} {} {}'.format(self.result, a, self.operation, b)
220 207
228 else: 215 else:
229 raise Exception() 216 raise Exception()
230 self.removeUse(old) 217 self.removeUse(old)
231 self.addUse(new) 218 self.addUse(new)
232 219
220
233 # Memory functions: 221 # Memory functions:
234 class Load(Instruction): 222 class Load(Instruction):
235 def __init__(self, location, value): 223 def __init__(self, location, value):
236 super().__init__() 224 super().__init__()
237 assert type(value) is Value 225 assert type(value) is Value
241 self.location = location 229 self.location = location
242 self.addUse(self.location) 230 self.addUse(self.location)
243 231
244 def __repr__(self): 232 def __repr__(self):
245 return '{} = [{}]'.format(self.value, self.location) 233 return '{} = [{}]'.format(self.value, self.location)
234
246 235
247 class Store(Instruction): 236 class Store(Instruction):
248 def __init__(self, location, value): 237 def __init__(self, location, value):
249 super().__init__() 238 super().__init__()
250 assert type(value) is Value, value 239 assert type(value) is Value, value
265 else: 254 else:
266 raise Exception() 255 raise Exception()
267 self.removeUse(old) 256 self.removeUse(old)
268 self.addUse(new) 257 self.addUse(new)
269 258
259
270 # Branching: 260 # Branching:
261 class Terminator(Instruction):
262 @property
263 def Targets(self):
264 return self.getTargets()
265
266 def changeTarget(self, tfrom, tto):
267 pass
268
269
271 class Branch(Terminator): 270 class Branch(Terminator):
272 def __init__(self, target): 271 def __init__(self, target):
273 super().__init__() 272 super().__init__()
274 assert type(target) is BasicBlock 273 assert type(target) is BasicBlock
275 self.target = target 274 self.target = target
275
276 def __repr__(self): 276 def __repr__(self):
277 return 'BRANCH {0}'.format(self.target) 277 return 'BRANCH {0}'.format(self.target)
278
278 def getTargets(self): 279 def getTargets(self):
279 return [self.target] 280 return [self.target]
281
280 def changeTarget(self, tfrom, tto): 282 def changeTarget(self, tfrom, tto):
281 assert tfrom is self.target 283 assert tfrom is self.target
282 self.target = tto 284 self.target = tto
285
283 286
284 class ConditionalBranch(Terminator): 287 class ConditionalBranch(Terminator):
285 def __init__(self, a, cond, b, lab1, lab2): 288 def __init__(self, a, cond, b, lab1, lab2):
286 super().__init__() 289 super().__init__()
287 self.a = a 290 self.a = a
294 assert type(b) is Value 297 assert type(b) is Value
295 assert type(lab1) is BasicBlock 298 assert type(lab1) is BasicBlock
296 self.lab1 = lab1 299 self.lab1 = lab1
297 assert type(lab2) is BasicBlock 300 assert type(lab2) is BasicBlock
298 self.lab2 = lab2 301 self.lab2 = lab2
302
299 def __repr__(self): 303 def __repr__(self):
300 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2) 304 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
301 def getTargets(self): 305 def getTargets(self):
302 return [self.lab1, self.lab2] 306 return [self.lab1, self.lab2]
307
303 def changeTarget(self, tfrom, tto): 308 def changeTarget(self, tfrom, tto):
304 assert tfrom is self.lab1 or tfrom is self.lab2 309 assert tfrom is self.lab1 or tfrom is self.lab2
305 if tfrom is self.lab1: 310 if tfrom is self.lab1:
306 self.lab1 = tto 311 self.lab1 = tto
307 elif tfrom is self.lab2: 312 elif tfrom is self.lab2:
308 self.lab2 = tto 313 self.lab2 = tto
309 314
315
316 class Return(Terminator):
317 def __init__(self, value=None):
318 super().__init__()
319 self.value = value
320 if value:
321 self.addUse(value)
322
323 def __repr__(self):
324 if self.value:
325 return 'ret {0}'.format(self.value)
326 else:
327 return 'ret'
328
329 def getTargets(self):
330 return []
331
332
310 class PhiNode(Instruction): 333 class PhiNode(Instruction):
311 def __init__(self): 334 def __init__(self):
312 super().__init__() 335 super().__init__()
313 self.incBB = [] 336 self.incBB = []
314 337