| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import types |
| 7 |
import py |
| 8 |
|
| 9 |
from rpython.tool.uid import uid, Hashable |
| 10 |
from rpython.tool.sourcetools import PY_IDENTIFIER, nice_repr_for_func |
| 11 |
|
| 12 |
|
| 13 |
class FunctionGraph(object): |
| 14 |
def __init__(self, name, startblock, return_var=None): |
| 15 |
self.name = name |
| 16 |
self.startblock = startblock |
| 17 |
|
| 18 |
self.returnblock = Block([return_var or Variable()]) |
| 19 |
self.returnblock.operations = () |
| 20 |
self.returnblock.exits = () |
| 21 |
|
| 22 |
self.exceptblock = Block([Variable('etype'), |
| 23 |
Variable('evalue')]) |
| 24 |
self.exceptblock.operations = () |
| 25 |
self.exceptblock.exits = () |
| 26 |
self.tag = None |
| 27 |
|
| 28 |
def getargs(self): |
| 29 |
return self.startblock.inputargs |
| 30 |
|
| 31 |
def getreturnvar(self): |
| 32 |
return self.returnblock.inputargs[0] |
| 33 |
|
| 34 |
@property |
| 35 |
def source(self): |
| 36 |
if hasattr(self, "_source"): |
| 37 |
return self._source |
| 38 |
from rpython.tool.sourcetools import getsource |
| 39 |
self.func |
| 40 |
src = getsource(self.func) |
| 41 |
if src is None: |
| 42 |
raise AttributeError('source not found') |
| 43 |
return src |
| 44 |
|
| 45 |
@source.setter |
| 46 |
def source(self, value): |
| 47 |
self._source = value |
| 48 |
|
| 49 |
@property |
| 50 |
def startline(self): |
| 51 |
return self.func.__code__.co_firstlineno |
| 52 |
|
| 53 |
@property |
| 54 |
def filename(self): |
| 55 |
return self.func.__code__.co_filename |
| 56 |
|
| 57 |
def __str__(self): |
| 58 |
if hasattr(self, 'func'): |
| 59 |
return nice_repr_for_func(self.func, self.name) |
| 60 |
else: |
| 61 |
return self.name |
| 62 |
|
| 63 |
def __repr__(self): |
| 64 |
return '<FunctionGraph of %s at 0x%x>' % (self, uid(self)) |
| 65 |
|
| 66 |
def iterblocks(self): |
| 67 |
block = self.startblock |
| 68 |
yield block |
| 69 |
seen = {block: True} |
| 70 |
stack = list(block.exits[::-1]) |
| 71 |
while stack: |
| 72 |
block = stack.pop().target |
| 73 |
if block not in seen: |
| 74 |
yield block |
| 75 |
seen[block] = True |
| 76 |
stack += block.exits[::-1] |
| 77 |
|
| 78 |
def iterlinks(self): |
| 79 |
block = self.startblock |
| 80 |
seen = {block: True} |
| 81 |
stack = list(block.exits[::-1]) |
| 82 |
while stack: |
| 83 |
link = stack.pop() |
| 84 |
yield link |
| 85 |
block = link.target |
| 86 |
if block not in seen: |
| 87 |
seen[block] = True |
| 88 |
stack += block.exits[::-1] |
| 89 |
|
| 90 |
def iterblockops(self): |
| 91 |
for block in self.iterblocks(): |
| 92 |
for op in block.operations: |
| 93 |
yield block, op |
| 94 |
|
| 95 |
def show(self, t=None): |
| 96 |
from rpython.translator.tool.graphpage import FlowGraphPage |
| 97 |
FlowGraphPage(t, [self]).display() |
| 98 |
|
| 99 |
def showbg(self, t=None): |
| 100 |
import os |
| 101 |
self.show(t) |
| 102 |
if os.fork() == 0: |
| 103 |
self.show(t) |
| 104 |
os._exit(0) |
| 105 |
|
| 106 |
view = show |
| 107 |
|
| 108 |
|
| 109 |
class Link(object): |
| 110 |
|
| 111 |
__slots__ = """args target exitcase llexitcase prevblock |
| 112 |
last_exception last_exc_value""".split() |
| 113 |
|
| 114 |
def __init__(self, args, target, exitcase=None): |
| 115 |
if target is not None: |
| 116 |
assert len(args) == len(target.inputargs), "output args mismatch" |
| 117 |
self.args = list(args) |
| 118 |
self.target = target |
| 119 |
self.exitcase = exitcase |
| 120 |
self.prevblock = None |
| 121 |
|
| 122 |
|
| 123 |
self.last_exception = None |
| 124 |
self.last_exc_value = None |
| 125 |
|
| 126 |
|
| 127 |
def extravars(self, last_exception=None, last_exc_value=None): |
| 128 |
self.last_exception = last_exception |
| 129 |
self.last_exc_value = last_exc_value |
| 130 |
|
| 131 |
def getextravars(self): |
| 132 |
"Return the extra vars created by this Link." |
| 133 |
result = [] |
| 134 |
if isinstance(self.last_exception, Variable): |
| 135 |
result.append(self.last_exception) |
| 136 |
if isinstance(self.last_exc_value, Variable): |
| 137 |
result.append(self.last_exc_value) |
| 138 |
return result |
| 139 |
|
| 140 |
def copy(self, rename=lambda x: x): |
| 141 |
newargs = [rename(a) for a in self.args] |
| 142 |
newlink = Link(newargs, self.target, self.exitcase) |
| 143 |
newlink.prevblock = self.prevblock |
| 144 |
newlink.last_exception = rename(self.last_exception) |
| 145 |
newlink.last_exc_value = rename(self.last_exc_value) |
| 146 |
if hasattr(self, 'llexitcase'): |
| 147 |
newlink.llexitcase = self.llexitcase |
| 148 |
return newlink |
| 149 |
|
| 150 |
def replace(self, mapping): |
| 151 |
def rename(v): |
| 152 |
if v is not None: |
| 153 |
return v.replace(mapping) |
| 154 |
return self.copy(rename) |
| 155 |
|
| 156 |
def settarget(self, targetblock): |
| 157 |
assert len(self.args) == len(targetblock.inputargs), ( |
| 158 |
"output args mismatch") |
| 159 |
self.target = targetblock |
| 160 |
|
| 161 |
def __repr__(self): |
| 162 |
return "link from %s to %s" % (str(self.prevblock), str(self.target)) |
| 163 |
|
| 164 |
def show(self): |
| 165 |
from rpython.translator.tool.graphpage import try_show |
| 166 |
return try_show(self) |
| 167 |
|
| 168 |
view = show |
| 169 |
|
| 170 |
|
| 171 |
class Block(object): |
| 172 |
__slots__ = """inputargs operations exitswitch |
| 173 |
exits blockcolor generation""".split() |
| 174 |
|
| 175 |
def __init__(self, inputargs): |
| 176 |
self.inputargs = list(inputargs) |
| 177 |
self.operations = [] |
| 178 |
self.exitswitch = None |
| 179 |
|
| 180 |
self.exits = [] |
| 181 |
|
| 182 |
def is_final_block(self): |
| 183 |
return self.operations == () |
| 184 |
|
| 185 |
def at(self): |
| 186 |
if self.operations and self.operations[0].offset >= 0: |
| 187 |
return "@%d" % self.operations[0].offset |
| 188 |
else: |
| 189 |
return "" |
| 190 |
|
| 191 |
def __str__(self): |
| 192 |
if self.operations: |
| 193 |
txt = "block@%d" % self.operations[0].offset |
| 194 |
else: |
| 195 |
if (not self.exits) and len(self.inputargs) == 1: |
| 196 |
txt = "return block" |
| 197 |
elif (not self.exits) and len(self.inputargs) == 2: |
| 198 |
txt = "raise block" |
| 199 |
else: |
| 200 |
txt = "codeless block" |
| 201 |
if len(self.inputargs) > 0: |
| 202 |
if len(self.inputargs) > 1: |
| 203 |
txt += '[%s...]' % (self.inputargs[0],) |
| 204 |
else: |
| 205 |
txt += '[%s]' % (self.inputargs[0],) |
| 206 |
return txt |
| 207 |
|
| 208 |
def __repr__(self): |
| 209 |
txt = "%s with %d exits" % (str(self), len(self.exits)) |
| 210 |
if self.exitswitch: |
| 211 |
txt = "%s(%s)" % (txt, self.exitswitch) |
| 212 |
return txt |
| 213 |
|
| 214 |
@property |
| 215 |
def canraise(self): |
| 216 |
return self.exitswitch is c_last_exception |
| 217 |
|
| 218 |
@property |
| 219 |
def raising_op(self): |
| 220 |
if self.canraise: |
| 221 |
return self.operations[-1] |
| 222 |
|
| 223 |
def getvariables(self): |
| 224 |
"Return all variables mentioned in this Block." |
| 225 |
result = self.inputargs[:] |
| 226 |
for op in self.operations: |
| 227 |
result += op.args |
| 228 |
result.append(op.result) |
| 229 |
return uniqueitems([w for w in result if isinstance(w, Variable)]) |
| 230 |
|
| 231 |
def getconstants(self): |
| 232 |
"Return all constants mentioned in this Block." |
| 233 |
result = self.inputargs[:] |
| 234 |
for op in self.operations: |
| 235 |
result += op.args |
| 236 |
return uniqueitems([w for w in result if isinstance(w, Constant)]) |
| 237 |
|
| 238 |
def renamevariables(self, mapping): |
| 239 |
self.inputargs = [a.replace(mapping) for a in self.inputargs] |
| 240 |
self.operations = [op.replace(mapping) for op in self.operations] |
| 241 |
if self.exitswitch is not None: |
| 242 |
self.exitswitch = self.exitswitch.replace(mapping) |
| 243 |
for link in self.exits: |
| 244 |
link.args = [a.replace(mapping) for a in link.args] |
| 245 |
|
| 246 |
def closeblock(self, *exits): |
| 247 |
assert self.exits == [], "block already closed" |
| 248 |
self.recloseblock(*exits) |
| 249 |
|
| 250 |
def recloseblock(self, *exits): |
| 251 |
for exit in exits: |
| 252 |
exit.prevblock = self |
| 253 |
self.exits = exits |
| 254 |
|
| 255 |
def show(self): |
| 256 |
from rpython.translator.tool.graphpage import try_show |
| 257 |
return try_show(self) |
| 258 |
|
| 259 |
def _slowly_get_graph(self): |
| 260 |
import gc |
| 261 |
pending = [self] |
| 262 |
seen = {self: True, None: True} |
| 263 |
for x in pending: |
| 264 |
for y in gc.get_referrers(x): |
| 265 |
if isinstance(y, FunctionGraph): |
| 266 |
return y |
| 267 |
elif isinstance(y, Link): |
| 268 |
block = y.prevblock |
| 269 |
if block not in seen: |
| 270 |
pending.append(block) |
| 271 |
seen[block] = True |
| 272 |
elif isinstance(y, dict): |
| 273 |
pending.append(y) |
| 274 |
return pending |
| 275 |
|
| 276 |
view = show |
| 277 |
|
| 278 |
|
| 279 |
class Variable(object): |
| 280 |
__slots__ = ["_name", "_nr", "annotation", "concretetype"] |
| 281 |
|
| 282 |
dummyname = 'v' |
| 283 |
namesdict = {dummyname: (dummyname, 0)} |
| 284 |
|
| 285 |
@property |
| 286 |
def name(self): |
| 287 |
_name = self._name |
| 288 |
_nr = self._nr |
| 289 |
if _nr == -1: |
| 290 |
|
| 291 |
nd = self.namesdict |
| 292 |
_nr = self._nr = nd[_name][1] |
| 293 |
nd[_name] = (_name, _nr + 1) |
| 294 |
return "%s%d" % (_name, _nr) |
| 295 |
|
| 296 |
@property |
| 297 |
def renamed(self): |
| 298 |
return self._name is not self.dummyname |
| 299 |
|
| 300 |
def __init__(self, name=None): |
| 301 |
self._name = self.dummyname |
| 302 |
self._nr = -1 |
| 303 |
self.annotation = None |
| 304 |
|
| 305 |
if name is not None: |
| 306 |
self.rename(name) |
| 307 |
|
| 308 |
def __repr__(self): |
| 309 |
return self.name |
| 310 |
|
| 311 |
def rename(self, name): |
| 312 |
if self._name is not self.dummyname: |
| 313 |
return |
| 314 |
if type(name) is not str: |
| 315 |
|
| 316 |
name = name._name |
| 317 |
if name is self.dummyname: |
| 318 |
return |
| 319 |
else: |
| 320 |
|
| 321 |
name = name.translate(PY_IDENTIFIER) + '_' |
| 322 |
if name[0] <= '9': |
| 323 |
name = '_' + name |
| 324 |
name = self.namesdict.setdefault(name, (name, 0))[0] |
| 325 |
self._name = name |
| 326 |
self._nr = -1 |
| 327 |
|
| 328 |
def set_name_from(self, v): |
| 329 |
|
| 330 |
v.name |
| 331 |
self._name = v._name |
| 332 |
self._nr = v._nr |
| 333 |
|
| 334 |
def set_name(self, name, nr): |
| 335 |
|
| 336 |
self._name = intern(name) |
| 337 |
self._nr = nr |
| 338 |
|
| 339 |
def foldable(self): |
| 340 |
return False |
| 341 |
|
| 342 |
def copy(self): |
| 343 |
"""Make a copy of the Variable, preserving annotations and concretetype.""" |
| 344 |
newvar = Variable(self) |
| 345 |
newvar.annotation = self.annotation |
| 346 |
if hasattr(self, 'concretetype'): |
| 347 |
newvar.concretetype = self.concretetype |
| 348 |
return newvar |
| 349 |
|
| 350 |
def replace(self, mapping): |
| 351 |
return mapping.get(self, self) |
| 352 |
|
| 353 |
|
| 354 |
class Constant(Hashable): |
| 355 |
__slots__ = ["concretetype"] |
| 356 |
|
| 357 |
def __init__(self, value, concretetype=None): |
| 358 |
Hashable.__init__(self, value) |
| 359 |
if concretetype is not None: |
| 360 |
self.concretetype = concretetype |
| 361 |
|
| 362 |
def foldable(self): |
| 363 |
to_check = self.value |
| 364 |
if hasattr(to_check, 'im_self'): |
| 365 |
to_check = to_check.im_self |
| 366 |
if isinstance(to_check, (type, types.ClassType, types.ModuleType)): |
| 367 |
|
| 368 |
return True |
| 369 |
if (hasattr(to_check, '__class__') and |
| 370 |
to_check.__class__.__module__ == '__builtin__'): |
| 371 |
|
| 372 |
return True |
| 373 |
|
| 374 |
if hasattr(to_check, '_freeze_'): |
| 375 |
assert to_check._freeze_() is True |
| 376 |
return True |
| 377 |
else: |
| 378 |
|
| 379 |
return False |
| 380 |
|
| 381 |
def replace(self, mapping): |
| 382 |
return self |
| 383 |
|
| 384 |
|
| 385 |
class FSException(object): |
| 386 |
def __init__(self, w_type, w_value): |
| 387 |
assert w_type is not None |
| 388 |
self.w_type = w_type |
| 389 |
self.w_value = w_value |
| 390 |
|
| 391 |
def __str__(self): |
| 392 |
return '[%s: %s]' % (self.w_type, self.w_value) |
| 393 |
|
| 394 |
class ConstException(Constant, FSException): |
| 395 |
def foldable(self): |
| 396 |
return True |
| 397 |
|
| 398 |
@property |
| 399 |
def w_type(self): |
| 400 |
return Constant(type(self.value)) |
| 401 |
|
| 402 |
@property |
| 403 |
def w_value(self): |
| 404 |
return Constant(self.value) |
| 405 |
|
| 406 |
|
| 407 |
class UnwrapException(Exception): |
| 408 |
"""Attempted to unwrap a Variable.""" |
| 409 |
|
| 410 |
class WrapException(Exception): |
| 411 |
"""Attempted wrapping of a type that cannot sanely appear in flow graph or |
| 412 |
during its construction""" |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
if hasattr(complex.real.__get__, 'im_self'): |
| 417 |
type_with_bad_introspection = None |
| 418 |
else: |
| 419 |
type_with_bad_introspection = type(complex.real.__get__) |
| 420 |
|
| 421 |
def const(obj): |
| 422 |
if hasattr(obj, "_flowspace_rewrite_directly_as_"): |
| 423 |
obj = obj._flowspace_rewrite_directly_as_ |
| 424 |
if isinstance(obj, (Variable, Constant)): |
| 425 |
raise TypeError("already wrapped: " + repr(obj)) |
| 426 |
|
| 427 |
|
| 428 |
if type(obj) is type_with_bad_introspection: |
| 429 |
raise WrapException |
| 430 |
elif isinstance(obj, Exception): |
| 431 |
return ConstException(obj) |
| 432 |
return Constant(obj) |
| 433 |
|
| 434 |
class SpaceOperation(object): |
| 435 |
|
| 436 |
def __init__(self, opname, args, result, offset=-1): |
| 437 |
self.opname = intern(opname) |
| 438 |
self.args = list(args) |
| 439 |
self.result = result |
| 440 |
self.offset = offset |
| 441 |
|
| 442 |
def __eq__(self, other): |
| 443 |
return (self.__class__ is other.__class__ and |
| 444 |
self.opname == other.opname and |
| 445 |
self.args == other.args and |
| 446 |
self.result == other.result) |
| 447 |
|
| 448 |
def __ne__(self, other): |
| 449 |
return not (self == other) |
| 450 |
|
| 451 |
def __hash__(self): |
| 452 |
return hash((self.opname, tuple(self.args), self.result)) |
| 453 |
|
| 454 |
def __repr__(self): |
| 455 |
return "%r = %s(%s)" % (self.result, self.opname, |
| 456 |
", ".join(map(repr, self.args))) |
| 457 |
|
| 458 |
def replace(self, mapping): |
| 459 |
newargs = [arg.replace(mapping) for arg in self.args] |
| 460 |
newresult = self.result.replace(mapping) |
| 461 |
return type(self)(self.opname, newargs, newresult, self.offset) |
| 462 |
|
| 463 |
class Atom(object): |
| 464 |
def __init__(self, name): |
| 465 |
self.__name__ = name |
| 466 |
def __repr__(self): |
| 467 |
return self.__name__ |
| 468 |
|
| 469 |
last_exception = Atom('last_exception') |
| 470 |
c_last_exception = Constant(last_exception) |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
def uniqueitems(lst): |
| 477 |
"Returns a list with duplicate elements removed." |
| 478 |
result = [] |
| 479 |
seen = {} |
| 480 |
for item in lst: |
| 481 |
if item not in seen: |
| 482 |
result.append(item) |
| 483 |
seen[item] = True |
| 484 |
return result |
| 485 |
|
| 486 |
|
| 487 |
def flattenobj(*args): |
| 488 |
for arg in args: |
| 489 |
try: |
| 490 |
for atom in flattenobj(*arg): |
| 491 |
yield atom |
| 492 |
except: |
| 493 |
yield arg |
| 494 |
|
| 495 |
def mkentrymap(funcgraph): |
| 496 |
"Returns a dict mapping Blocks to lists of Links." |
| 497 |
startlink = Link(funcgraph.getargs(), funcgraph.startblock) |
| 498 |
result = {funcgraph.startblock: [startlink]} |
| 499 |
for link in funcgraph.iterlinks(): |
| 500 |
lst = result.setdefault(link.target, []) |
| 501 |
lst.append(link) |
| 502 |
return result |
| 503 |
|
| 504 |
def copygraph(graph, shallow=False, varmap={}, shallowvars=False): |
| 505 |
"Make a copy of a flow graph." |
| 506 |
blockmap = {} |
| 507 |
varmap = varmap.copy() |
| 508 |
shallowvars = shallowvars or shallow |
| 509 |
|
| 510 |
def copyvar(v): |
| 511 |
if shallowvars: |
| 512 |
return v |
| 513 |
try: |
| 514 |
return varmap[v] |
| 515 |
except KeyError: |
| 516 |
if isinstance(v, Variable): |
| 517 |
v2 = varmap[v] = Variable(v) |
| 518 |
if hasattr(v, 'concretetype'): |
| 519 |
v2.concretetype = v.concretetype |
| 520 |
return v2 |
| 521 |
else: |
| 522 |
return v |
| 523 |
|
| 524 |
def copyblock(block): |
| 525 |
newblock = Block([copyvar(v) for v in block.inputargs]) |
| 526 |
if block.operations == (): |
| 527 |
newblock.operations = () |
| 528 |
else: |
| 529 |
def copyoplist(oplist): |
| 530 |
if shallow: |
| 531 |
return oplist[:] |
| 532 |
result = [] |
| 533 |
for op in oplist: |
| 534 |
copyop = SpaceOperation(op.opname, |
| 535 |
[copyvar(v) for v in op.args], |
| 536 |
copyvar(op.result), op.offset) |
| 537 |
|
| 538 |
result.append(copyop) |
| 539 |
return result |
| 540 |
newblock.operations = copyoplist(block.operations) |
| 541 |
newblock.exitswitch = copyvar(block.exitswitch) |
| 542 |
return newblock |
| 543 |
|
| 544 |
for block in graph.iterblocks(): |
| 545 |
blockmap[block] = copyblock(block) |
| 546 |
|
| 547 |
if graph.returnblock not in blockmap: |
| 548 |
blockmap[graph.returnblock] = copyblock(graph.returnblock) |
| 549 |
if graph.exceptblock not in blockmap: |
| 550 |
blockmap[graph.exceptblock] = copyblock(graph.exceptblock) |
| 551 |
|
| 552 |
for block, newblock in blockmap.items(): |
| 553 |
newlinks = [] |
| 554 |
for link in block.exits: |
| 555 |
newlink = link.copy(copyvar) |
| 556 |
newlink.target = blockmap[link.target] |
| 557 |
newlinks.append(newlink) |
| 558 |
newblock.closeblock(*newlinks) |
| 559 |
|
| 560 |
newstartblock = blockmap[graph.startblock] |
| 561 |
newgraph = FunctionGraph(graph.name, newstartblock) |
| 562 |
newgraph.returnblock = blockmap[graph.returnblock] |
| 563 |
newgraph.exceptblock = blockmap[graph.exceptblock] |
| 564 |
for key, value in graph.__dict__.items(): |
| 565 |
newgraph.__dict__.setdefault(key, value) |
| 566 |
return newgraph |
| 567 |
|
| 568 |
def checkgraph(graph): |
| 569 |
"Check the consistency of a flow graph." |
| 570 |
if not __debug__: |
| 571 |
return |
| 572 |
try: |
| 573 |
from rpython.rlib.rarithmetic import (is_valid_int, r_longlong, |
| 574 |
r_ulonglong, r_uint) |
| 575 |
vars_previous_blocks = {} |
| 576 |
|
| 577 |
exitblocks = {graph.returnblock: 1, |
| 578 |
graph.exceptblock: 2} |
| 579 |
|
| 580 |
for block, nbargs in exitblocks.items(): |
| 581 |
assert len(block.inputargs) == nbargs |
| 582 |
assert block.operations == () |
| 583 |
assert block.exits == () |
| 584 |
|
| 585 |
def definevar(v, only_in_link=None): |
| 586 |
assert isinstance(v, Variable) |
| 587 |
assert v not in vars, "duplicate variable %r" % (v,) |
| 588 |
assert v not in vars_previous_blocks, ( |
| 589 |
"variable %r used in more than one block" % (v,)) |
| 590 |
vars[v] = only_in_link |
| 591 |
|
| 592 |
def usevar(v, in_link=None): |
| 593 |
assert v in vars |
| 594 |
if in_link is not None: |
| 595 |
assert vars[v] is None or vars[v] is in_link |
| 596 |
|
| 597 |
|
| 598 |
for block in graph.iterblocks(): |
| 599 |
assert type(block.exits) is tuple, ( |
| 600 |
"block.exits is a %s (closeblock() or recloseblock() missing?)" |
| 601 |
% (type(block.exits).__name__,)) |
| 602 |
if not block.exits: |
| 603 |
assert block in exitblocks |
| 604 |
vars = {} |
| 605 |
|
| 606 |
for v in block.inputargs: |
| 607 |
definevar(v) |
| 608 |
|
| 609 |
for op in block.operations: |
| 610 |
for v in op.args: |
| 611 |
assert isinstance(v, (Constant, Variable)) |
| 612 |
if isinstance(v, Variable): |
| 613 |
usevar(v) |
| 614 |
else: |
| 615 |
assert v.value is not last_exception |
| 616 |
|
| 617 |
if op.opname == 'direct_call': |
| 618 |
assert isinstance(op.args[0], Constant) |
| 619 |
elif op.opname == 'indirect_call': |
| 620 |
assert isinstance(op.args[0], Variable) |
| 621 |
definevar(op.result) |
| 622 |
|
| 623 |
exc_links = {} |
| 624 |
if block.exitswitch is None: |
| 625 |
assert len(block.exits) <= 1 |
| 626 |
if block.exits: |
| 627 |
assert block.exits[0].exitcase is None |
| 628 |
elif block.canraise: |
| 629 |
assert len(block.operations) >= 1 |
| 630 |
|
| 631 |
|
| 632 |
assert block.raising_op.opname not in ("keepalive", |
| 633 |
"cast_pointer", |
| 634 |
"same_as") |
| 635 |
assert len(block.exits) >= 2 |
| 636 |
assert block.exits[0].exitcase is None |
| 637 |
for link in block.exits[1:]: |
| 638 |
assert issubclass(link.exitcase, py.builtin.BaseException) |
| 639 |
exc_links[link] = True |
| 640 |
else: |
| 641 |
assert isinstance(block.exitswitch, Variable) |
| 642 |
assert block.exitswitch in vars |
| 643 |
if (len(block.exits) == 2 and block.exits[0].exitcase is False |
| 644 |
and block.exits[1].exitcase is True): |
| 645 |
|
| 646 |
pass |
| 647 |
else: |
| 648 |
|
| 649 |
|
| 650 |
assert len(block.exits) >= 1 |
| 651 |
cases = [link.exitcase for link in block.exits] |
| 652 |
has_default = cases[-1] == 'default' |
| 653 |
for n in cases[:len(cases)-has_default]: |
| 654 |
if is_valid_int(n): |
| 655 |
continue |
| 656 |
if isinstance(n, (r_longlong, r_ulonglong, r_uint)): |
| 657 |
continue |
| 658 |
if isinstance(n, (str, unicode)) and len(n) == 1: |
| 659 |
continue |
| 660 |
assert n != 'default', ( |
| 661 |
"'default' branch of a switch is not the last exit" |
| 662 |
) |
| 663 |
assert n is not None, ( |
| 664 |
"exitswitch Variable followed by a None exitcase") |
| 665 |
raise AssertionError( |
| 666 |
"switch on a non-primitive value %r" % (n,)) |
| 667 |
|
| 668 |
allexitcases = {} |
| 669 |
for link in block.exits: |
| 670 |
assert len(link.args) == len(link.target.inputargs) |
| 671 |
assert link.prevblock is block |
| 672 |
exc_link = link in exc_links |
| 673 |
if exc_link: |
| 674 |
for v in [link.last_exception, link.last_exc_value]: |
| 675 |
assert isinstance(v, (Variable, Constant)) |
| 676 |
if isinstance(v, Variable): |
| 677 |
definevar(v, only_in_link=link) |
| 678 |
else: |
| 679 |
assert link.last_exception is None |
| 680 |
assert link.last_exc_value is None |
| 681 |
for v in link.args: |
| 682 |
assert isinstance(v, (Constant, Variable)) |
| 683 |
if isinstance(v, Variable): |
| 684 |
usevar(v, in_link=link) |
| 685 |
if exc_link: |
| 686 |
assert v != block.operations[-1].result |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
allexitcases[link.exitcase] = True |
| 692 |
assert len(allexitcases) == len(block.exits) |
| 693 |
vars_previous_blocks.update(vars) |
| 694 |
|
| 695 |
except AssertionError as e: |
| 696 |
|
| 697 |
|
| 698 |
if block and not hasattr(e, '__annotator_block'): |
| 699 |
setattr(e, '__annotator_block', block) |
| 700 |
raise |
| 701 |
|
| 702 |
def summary(graph): |
| 703 |
|
| 704 |
insns = {} |
| 705 |
for block in graph.iterblocks(): |
| 706 |
for op in block.operations: |
| 707 |
if op.opname != 'same_as': |
| 708 |
insns[op.opname] = insns.get(op.opname, 0) + 1 |
| 709 |
return insns |
| 710 |
|
| 711 |
def safe_iterblocks(graph): |
| 712 |
|
| 713 |
|
| 714 |
block = getattr(graph, 'startblock', None) |
| 715 |
yield block |
| 716 |
seen = {block: True} |
| 717 |
stack = list(getattr(block, 'exits', [])[::-1]) |
| 718 |
while stack: |
| 719 |
block = stack.pop().target |
| 720 |
if block not in seen: |
| 721 |
yield block |
| 722 |
seen[block] = True |
| 723 |
stack += getattr(block, 'exits', [])[::-1] |
| 724 |
|
| 725 |
def safe_iterlinks(graph): |
| 726 |
|
| 727 |
|
| 728 |
block = getattr(graph, 'startblock', None) |
| 729 |
seen = {block: True} |
| 730 |
stack = list(getattr(block, 'exits', [])[::-1]) |
| 731 |
while stack: |
| 732 |
link = stack.pop() |
| 733 |
yield link |
| 734 |
block = getattr(link, 'target', None) |
| 735 |
if block not in seen: |
| 736 |
seen[block] = True |
| 737 |
stack += getattr(block, 'exits', [])[::-1] |