| 1 |
"""Implements the core parts of flow graph creation. |
| 2 |
""" |
| 3 |
from __future__ import print_function |
| 4 |
import sys |
| 5 |
import collections |
| 6 |
import types |
| 7 |
import __builtin__ |
| 8 |
|
| 9 |
from rpython.tool.error import source_lines |
| 10 |
from rpython.rlib import rstackovf |
| 11 |
from rpython.flowspace.argument import CallSpec |
| 12 |
from rpython.flowspace.model import (Constant, Variable, Block, Link, |
| 13 |
c_last_exception, const, FSException) |
| 14 |
from rpython.flowspace.framestate import FrameState |
| 15 |
from rpython.flowspace.specialcase import (rpython_print_item, |
| 16 |
rpython_print_newline, rpython_print_end) |
| 17 |
from rpython.flowspace.operation import op |
| 18 |
from rpython.flowspace.bytecode import BytecodeCorruption |
| 19 |
|
| 20 |
w_None = const(None) |
| 21 |
|
| 22 |
class FlowingError(Exception): |
| 23 |
""" Signals invalid RPython in the function being analysed""" |
| 24 |
ctx = None |
| 25 |
|
| 26 |
def __str__(self): |
| 27 |
msg = ["\n"] |
| 28 |
msg += map(str, self.args) |
| 29 |
msg += [""] |
| 30 |
msg += source_lines(self.ctx.graph, None, offset=self.ctx.last_offset) |
| 31 |
return "\n".join(msg) |
| 32 |
|
| 33 |
|
| 34 |
class StopFlowing(Exception): |
| 35 |
pass |
| 36 |
|
| 37 |
|
| 38 |
class SpamBlock(Block): |
| 39 |
def __init__(self, framestate): |
| 40 |
Block.__init__(self, framestate.getvariables()) |
| 41 |
self.framestate = framestate |
| 42 |
self.dead = False |
| 43 |
|
| 44 |
def make_recorder(self): |
| 45 |
return BlockRecorder(self) |
| 46 |
|
| 47 |
class EggBlock(Block): |
| 48 |
def __init__(self, inputargs, prevblock, booloutcome): |
| 49 |
Block.__init__(self, inputargs) |
| 50 |
self.prevblock = prevblock |
| 51 |
self.booloutcome = booloutcome |
| 52 |
|
| 53 |
@property |
| 54 |
def ancestor(self): |
| 55 |
parent = self.prevblock |
| 56 |
while isinstance(parent, EggBlock): |
| 57 |
parent = parent.prevblock |
| 58 |
return parent |
| 59 |
|
| 60 |
@property |
| 61 |
def dead(self): |
| 62 |
return self.ancestor.dead |
| 63 |
|
| 64 |
@property |
| 65 |
def framestate(self): |
| 66 |
return self.ancestor.framestate |
| 67 |
|
| 68 |
def make_recorder(self): |
| 69 |
recorder = BlockRecorder(self) |
| 70 |
curr = self |
| 71 |
while isinstance(curr, EggBlock): |
| 72 |
prev = curr.prevblock |
| 73 |
recorder = Replayer(prev, curr.booloutcome, recorder) |
| 74 |
curr = prev |
| 75 |
return recorder |
| 76 |
|
| 77 |
def extravars(self, last_exception=None, last_exc_value=None): |
| 78 |
self.last_exception = last_exception |
| 79 |
|
| 80 |
def fixeggblocks(graph): |
| 81 |
for block in graph.iterblocks(): |
| 82 |
if isinstance(block, SpamBlock): |
| 83 |
del block.framestate |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
class Recorder(object): |
| 88 |
def append(self, operation): |
| 89 |
raise NotImplementedError |
| 90 |
|
| 91 |
def guessbool(self, ctx, w_condition): |
| 92 |
raise AssertionError("cannot guessbool(%s)" % (w_condition,)) |
| 93 |
|
| 94 |
|
| 95 |
class BlockRecorder(Recorder): |
| 96 |
|
| 97 |
|
| 98 |
def __init__(self, block): |
| 99 |
self.crnt_block = block |
| 100 |
|
| 101 |
|
| 102 |
self.final_state = None |
| 103 |
|
| 104 |
def append(self, operation): |
| 105 |
self.crnt_block.operations.append(operation) |
| 106 |
|
| 107 |
def guessbool(self, ctx, w_condition): |
| 108 |
block = self.crnt_block |
| 109 |
links = [] |
| 110 |
for case in [False, True]: |
| 111 |
egg = EggBlock([], block, case) |
| 112 |
ctx.pendingblocks.append(egg) |
| 113 |
link = Link([], egg, case) |
| 114 |
links.append(link) |
| 115 |
|
| 116 |
block.exitswitch = w_condition |
| 117 |
block.closeblock(*links) |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
raise StopFlowing |
| 123 |
|
| 124 |
def guessexception(self, ctx, *cases): |
| 125 |
block = self.crnt_block |
| 126 |
links = [] |
| 127 |
for case in [None] + list(cases): |
| 128 |
if case is not None: |
| 129 |
if case is Exception: |
| 130 |
last_exc = Variable('last_exception') |
| 131 |
else: |
| 132 |
last_exc = Constant(case) |
| 133 |
last_exc_value = Variable('last_exc_value') |
| 134 |
vars = [last_exc, last_exc_value] |
| 135 |
vars2 = [Variable(), Variable()] |
| 136 |
else: |
| 137 |
vars = [] |
| 138 |
vars2 = [] |
| 139 |
egg = EggBlock(vars2, block, case) |
| 140 |
ctx.pendingblocks.append(egg) |
| 141 |
link = Link(vars, egg, case) |
| 142 |
if case is not None: |
| 143 |
link.extravars(last_exception=last_exc, last_exc_value=last_exc_value) |
| 144 |
egg.extravars(last_exception=last_exc) |
| 145 |
links.append(link) |
| 146 |
|
| 147 |
block.exitswitch = c_last_exception |
| 148 |
block.closeblock(*links) |
| 149 |
raise StopFlowing |
| 150 |
|
| 151 |
|
| 152 |
class Replayer(Recorder): |
| 153 |
|
| 154 |
def __init__(self, block, booloutcome, nextreplayer): |
| 155 |
self.crnt_block = block |
| 156 |
self.listtoreplay = block.operations |
| 157 |
self.booloutcome = booloutcome |
| 158 |
self.nextreplayer = nextreplayer |
| 159 |
self.index = 0 |
| 160 |
|
| 161 |
def append(self, operation): |
| 162 |
operation.result = self.listtoreplay[self.index].result |
| 163 |
assert operation == self.listtoreplay[self.index], ( |
| 164 |
'\n'.join(["Not generating the same operation sequence:"] + |
| 165 |
[str(s) for s in self.listtoreplay[:self.index]] + |
| 166 |
[" ---> | while repeating we see here"] + |
| 167 |
[" | %s" % operation] + |
| 168 |
[str(s) for s in self.listtoreplay[self.index:]])) |
| 169 |
self.index += 1 |
| 170 |
|
| 171 |
def guessbool(self, ctx, w_condition): |
| 172 |
assert self.index == len(self.listtoreplay) |
| 173 |
ctx.recorder = self.nextreplayer |
| 174 |
return self.booloutcome |
| 175 |
|
| 176 |
def guessexception(self, ctx, *classes): |
| 177 |
assert self.index == len(self.listtoreplay) |
| 178 |
ctx.recorder = self.nextreplayer |
| 179 |
outcome = self.booloutcome |
| 180 |
if outcome is not None: |
| 181 |
egg = self.nextreplayer.crnt_block |
| 182 |
w_exc_cls, w_exc_value = egg.inputargs[-2:] |
| 183 |
if isinstance(egg.last_exception, Constant): |
| 184 |
w_exc_cls = egg.last_exception |
| 185 |
assert not isinstance(w_exc_cls.value, list) |
| 186 |
raise RaiseImplicit(FSException(w_exc_cls, w_exc_value)) |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
_unary_ops = [ |
| 191 |
('UNARY_POSITIVE', op.pos), |
| 192 |
('UNARY_NEGATIVE', op.neg), |
| 193 |
('UNARY_CONVERT', op.repr), |
| 194 |
('UNARY_INVERT', op.invert), |
| 195 |
] |
| 196 |
|
| 197 |
def unaryoperation(OPCODE, operation): |
| 198 |
def UNARY_OP(self, *ignored): |
| 199 |
w_1 = self.popvalue() |
| 200 |
w_result = operation(w_1).eval(self) |
| 201 |
self.pushvalue(w_result) |
| 202 |
UNARY_OP.__name__ = OPCODE |
| 203 |
return UNARY_OP |
| 204 |
|
| 205 |
_binary_ops = [ |
| 206 |
('BINARY_MULTIPLY', op.mul), |
| 207 |
('BINARY_TRUE_DIVIDE', op.truediv), |
| 208 |
('BINARY_FLOOR_DIVIDE', op.floordiv), |
| 209 |
('BINARY_DIVIDE', op.div), |
| 210 |
('BINARY_MODULO', op.mod), |
| 211 |
('BINARY_ADD', op.add), |
| 212 |
('BINARY_SUBTRACT', op.sub), |
| 213 |
('BINARY_SUBSCR', op.getitem), |
| 214 |
('BINARY_LSHIFT', op.lshift), |
| 215 |
('BINARY_RSHIFT', op.rshift), |
| 216 |
('BINARY_AND', op.and_), |
| 217 |
('BINARY_XOR', op.xor), |
| 218 |
('BINARY_OR', op.or_), |
| 219 |
('INPLACE_MULTIPLY', op.inplace_mul), |
| 220 |
('INPLACE_TRUE_DIVIDE', op.inplace_truediv), |
| 221 |
('INPLACE_FLOOR_DIVIDE', op.inplace_floordiv), |
| 222 |
('INPLACE_DIVIDE', op.inplace_div), |
| 223 |
('INPLACE_MODULO', op.inplace_mod), |
| 224 |
('INPLACE_ADD', op.inplace_add), |
| 225 |
('INPLACE_SUBTRACT', op.inplace_sub), |
| 226 |
('INPLACE_LSHIFT', op.inplace_lshift), |
| 227 |
('INPLACE_RSHIFT', op.inplace_rshift), |
| 228 |
('INPLACE_AND', op.inplace_and), |
| 229 |
('INPLACE_XOR', op.inplace_xor), |
| 230 |
('INPLACE_OR', op.inplace_or), |
| 231 |
] |
| 232 |
|
| 233 |
def binaryoperation(OPCODE, operation): |
| 234 |
"""NOT_RPYTHON""" |
| 235 |
def BINARY_OP(self, _): |
| 236 |
w_2 = self.popvalue() |
| 237 |
w_1 = self.popvalue() |
| 238 |
w_result = operation(w_1, w_2).eval(self) |
| 239 |
self.pushvalue(w_result) |
| 240 |
BINARY_OP.__name__ = OPCODE |
| 241 |
return BINARY_OP |
| 242 |
|
| 243 |
_unsupported_ops = [ |
| 244 |
('BINARY_POWER', "a ** b"), |
| 245 |
('BUILD_CLASS', 'defining classes inside functions'), |
| 246 |
('EXEC_STMT', 'exec statement'), |
| 247 |
('STOP_CODE', '???'), |
| 248 |
('STORE_NAME', 'modifying globals'), |
| 249 |
('INPLACE_POWER', 'a **= b'), |
| 250 |
('LOAD_LOCALS', 'locals()'), |
| 251 |
('IMPORT_STAR', 'import *'), |
| 252 |
('MISSING_OPCODE', '???'), |
| 253 |
('DELETE_GLOBAL', 'modifying globals'), |
| 254 |
('DELETE_NAME', 'modifying globals'), |
| 255 |
('DELETE_ATTR', 'deleting attributes'), |
| 256 |
] |
| 257 |
|
| 258 |
def unsupportedoperation(OPCODE, msg): |
| 259 |
def UNSUPPORTED(self, *ignored): |
| 260 |
raise FlowingError("%s is not RPython" % (msg,)) |
| 261 |
UNSUPPORTED.__name__ = OPCODE |
| 262 |
return UNSUPPORTED |
| 263 |
|
| 264 |
compare_method = [ |
| 265 |
"cmp_lt", |
| 266 |
"cmp_le", |
| 267 |
"cmp_eq", |
| 268 |
"cmp_ne", |
| 269 |
"cmp_gt", |
| 270 |
"cmp_ge", |
| 271 |
"cmp_in", |
| 272 |
"cmp_not_in", |
| 273 |
"cmp_is", |
| 274 |
"cmp_is_not", |
| 275 |
"cmp_exc_match", |
| 276 |
] |
| 277 |
|
| 278 |
|
| 279 |
class FlowContext(object): |
| 280 |
def __init__(self, graph, code): |
| 281 |
self.graph = graph |
| 282 |
func = graph.func |
| 283 |
self.pycode = code |
| 284 |
self.w_globals = Constant(func.__globals__) |
| 285 |
self.blockstack = [] |
| 286 |
|
| 287 |
self.init_closure(func.__closure__) |
| 288 |
self.f_lineno = code.co_firstlineno |
| 289 |
self.last_offset = 0 |
| 290 |
|
| 291 |
self.init_locals_stack(code) |
| 292 |
|
| 293 |
self.joinpoints = {} |
| 294 |
|
| 295 |
def init_closure(self, closure): |
| 296 |
if closure is None: |
| 297 |
self.closure = [] |
| 298 |
else: |
| 299 |
self.closure = list(closure) |
| 300 |
assert len(self.closure) == len(self.pycode.co_freevars) |
| 301 |
|
| 302 |
def init_locals_stack(self, code): |
| 303 |
""" |
| 304 |
Initialize the locals and the stack. |
| 305 |
|
| 306 |
The locals are ordered according to self.pycode.signature. |
| 307 |
""" |
| 308 |
self.nlocals = code.co_nlocals |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
self.locals_w = [None] * code.co_nlocals |
| 315 |
self.stack = [] |
| 316 |
|
| 317 |
@property |
| 318 |
def stackdepth(self): |
| 319 |
return len(self.stack) |
| 320 |
|
| 321 |
def pushvalue(self, w_object): |
| 322 |
self.stack.append(w_object) |
| 323 |
|
| 324 |
def popvalue(self): |
| 325 |
return self.stack.pop() |
| 326 |
|
| 327 |
def peekvalue(self, index_from_top=0): |
| 328 |
|
| 329 |
index = ~index_from_top |
| 330 |
return self.stack[index] |
| 331 |
|
| 332 |
def settopvalue(self, w_object, index_from_top=0): |
| 333 |
index = ~index_from_top |
| 334 |
self.stack[index] = w_object |
| 335 |
|
| 336 |
def popvalues(self, n): |
| 337 |
if n == 0: |
| 338 |
return [] |
| 339 |
values_w = self.stack[-n:] |
| 340 |
del self.stack[-n:] |
| 341 |
return values_w |
| 342 |
|
| 343 |
def dropvaluesuntil(self, finaldepth): |
| 344 |
del self.stack[finaldepth:] |
| 345 |
|
| 346 |
def getstate(self, next_offset): |
| 347 |
return FrameState(self.locals_w, self.stack[:], |
| 348 |
self.last_exception, self.blockstack[:], next_offset) |
| 349 |
|
| 350 |
def setstate(self, state): |
| 351 |
""" Reset the context to the given frame state. """ |
| 352 |
self.locals_w = state.locals_w[:] |
| 353 |
self.stack = state.stack[:] |
| 354 |
self.last_exception = state.last_exception |
| 355 |
self.blockstack = state.blocklist[:] |
| 356 |
self._normalize_raise_signals() |
| 357 |
|
| 358 |
def _normalize_raise_signals(self): |
| 359 |
st = self.stack |
| 360 |
for i in range(len(st)): |
| 361 |
if isinstance(st[i], RaiseImplicit): |
| 362 |
st[i] = Raise(st[i].w_exc) |
| 363 |
|
| 364 |
def guessbool(self, w_condition): |
| 365 |
if isinstance(w_condition, Constant): |
| 366 |
return w_condition.value |
| 367 |
return self.recorder.guessbool(self, w_condition) |
| 368 |
|
| 369 |
def maybe_merge(self): |
| 370 |
recorder = self.recorder |
| 371 |
if getattr(recorder, 'final_state', None) is not None: |
| 372 |
self.mergeblock(recorder.crnt_block, recorder.final_state) |
| 373 |
raise StopFlowing |
| 374 |
|
| 375 |
def record(self, spaceop): |
| 376 |
spaceop.offset = self.last_offset |
| 377 |
self.recorder.append(spaceop) |
| 378 |
|
| 379 |
def do_op(self, op): |
| 380 |
self.maybe_merge() |
| 381 |
self.record(op) |
| 382 |
self.guessexception(op.canraise) |
| 383 |
return op.result |
| 384 |
|
| 385 |
def guessexception(self, exceptions): |
| 386 |
""" |
| 387 |
Catch possible exceptions implicitly. |
| 388 |
""" |
| 389 |
if not exceptions: |
| 390 |
return |
| 391 |
|
| 392 |
if self.has_exc_handler(): |
| 393 |
self.recorder.guessexception(self, *exceptions) |
| 394 |
|
| 395 |
def has_exc_handler(self): |
| 396 |
return any(isinstance(block, (ExceptBlock, FinallyBlock)) |
| 397 |
for block in self.blockstack) |
| 398 |
|
| 399 |
def build_flow(self): |
| 400 |
graph = self.graph |
| 401 |
self.pendingblocks = collections.deque([graph.startblock]) |
| 402 |
while self.pendingblocks: |
| 403 |
block = self.pendingblocks.popleft() |
| 404 |
if not block.dead: |
| 405 |
self.record_block(block) |
| 406 |
|
| 407 |
def record_block(self, block): |
| 408 |
self.setstate(block.framestate) |
| 409 |
next_offset = block.framestate.next_offset |
| 410 |
self.recorder = block.make_recorder() |
| 411 |
try: |
| 412 |
while True: |
| 413 |
next_offset = self.handle_bytecode(next_offset) |
| 414 |
self.recorder.final_state = self.getstate(next_offset) |
| 415 |
except StopFlowing: |
| 416 |
pass |
| 417 |
except FlowingError as exc: |
| 418 |
if exc.ctx is None: |
| 419 |
exc.ctx = self |
| 420 |
raise |
| 421 |
|
| 422 |
self.recorder = None |
| 423 |
|
| 424 |
def mergeblock(self, currentblock, currentstate): |
| 425 |
next_offset = currentstate.next_offset |
| 426 |
|
| 427 |
|
| 428 |
candidates = self.joinpoints.setdefault(next_offset, []) |
| 429 |
for block in candidates: |
| 430 |
newstate = block.framestate.union(currentstate) |
| 431 |
if newstate is not None: |
| 432 |
break |
| 433 |
else: |
| 434 |
newblock = self.make_next_block(currentblock, currentstate) |
| 435 |
candidates.insert(0, newblock) |
| 436 |
return |
| 437 |
|
| 438 |
if newstate.matches(block.framestate): |
| 439 |
outputargs = currentstate.getoutputargs(newstate) |
| 440 |
currentblock.closeblock(Link(outputargs, block)) |
| 441 |
return |
| 442 |
|
| 443 |
newblock = SpamBlock(newstate) |
| 444 |
varnames = self.pycode.co_varnames |
| 445 |
for name, w_value in zip(varnames, newstate.locals_w): |
| 446 |
if isinstance(w_value, Variable): |
| 447 |
w_value.rename(name) |
| 448 |
|
| 449 |
outputargs = currentstate.getoutputargs(newstate) |
| 450 |
link = Link(outputargs, newblock) |
| 451 |
currentblock.closeblock(link) |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
block.dead = True |
| 456 |
block.operations = () |
| 457 |
block.exitswitch = None |
| 458 |
outputargs = block.framestate.getoutputargs(newstate) |
| 459 |
block.recloseblock(Link(outputargs, newblock)) |
| 460 |
candidates.remove(block) |
| 461 |
|
| 462 |
candidates.insert(0, newblock) |
| 463 |
self.pendingblocks.append(newblock) |
| 464 |
|
| 465 |
def make_next_block(self, block, state): |
| 466 |
newstate = state.copy() |
| 467 |
newblock = SpamBlock(newstate) |
| 468 |
|
| 469 |
outputargs = state.getoutputargs(newstate) |
| 470 |
link = Link(outputargs, newblock) |
| 471 |
block.closeblock(link) |
| 472 |
self.pendingblocks.append(newblock) |
| 473 |
return newblock |
| 474 |
|
| 475 |
|
| 476 |
def replace_in_stack(self, oldvalue, newvalue): |
| 477 |
w_new = Constant(newvalue) |
| 478 |
stack_items_w = self.stack |
| 479 |
for i in range(self.stackdepth - 1, - 1, -1): |
| 480 |
w_v = stack_items_w[i] |
| 481 |
if isinstance(w_v, Constant): |
| 482 |
if w_v.value is oldvalue: |
| 483 |
|
| 484 |
|
| 485 |
stack_items_w[i] = w_new |
| 486 |
break |
| 487 |
|
| 488 |
def handle_bytecode(self, next_offset): |
| 489 |
self.last_offset = next_offset |
| 490 |
next_offset, methodname, oparg = self.pycode.read(next_offset) |
| 491 |
try: |
| 492 |
offset = getattr(self, methodname)(oparg) |
| 493 |
return offset if offset is not None else next_offset |
| 494 |
except FlowSignal as signal: |
| 495 |
return self.unroll(signal) |
| 496 |
|
| 497 |
def unroll(self, signal): |
| 498 |
while self.blockstack: |
| 499 |
block = self.blockstack.pop() |
| 500 |
if isinstance(signal, block.handles): |
| 501 |
return block.handle(self, signal) |
| 502 |
block.cleanupstack(self) |
| 503 |
return signal.nomoreblocks(self) |
| 504 |
|
| 505 |
def getlocalvarname(self, index): |
| 506 |
return self.pycode.co_varnames[index] |
| 507 |
|
| 508 |
def getconstant_w(self, index): |
| 509 |
return const(self.pycode.consts[index]) |
| 510 |
|
| 511 |
def getname_u(self, index): |
| 512 |
return self.pycode.names[index] |
| 513 |
|
| 514 |
def getname_w(self, index): |
| 515 |
return Constant(self.pycode.names[index]) |
| 516 |
|
| 517 |
def appcall(self, func, *args_w): |
| 518 |
"""Call an app-level RPython function directly""" |
| 519 |
w_func = const(func) |
| 520 |
return self.do_op(op.simple_call(w_func, *args_w)) |
| 521 |
|
| 522 |
def BAD_OPCODE(self, _): |
| 523 |
raise FlowingError("This operation is not RPython") |
| 524 |
|
| 525 |
def BREAK_LOOP(self, oparg): |
| 526 |
raise Break |
| 527 |
|
| 528 |
def CONTINUE_LOOP(self, startofloop): |
| 529 |
raise Continue(startofloop) |
| 530 |
|
| 531 |
def not_(self, w_obj): |
| 532 |
w_bool = op.bool(w_obj).eval(self) |
| 533 |
return const(not self.guessbool(w_bool)) |
| 534 |
|
| 535 |
def UNARY_NOT(self, _): |
| 536 |
w_obj = self.popvalue() |
| 537 |
self.pushvalue(self.not_(w_obj)) |
| 538 |
|
| 539 |
def cmp_lt(self, w_1, w_2): |
| 540 |
return op.lt(w_1, w_2).eval(self) |
| 541 |
|
| 542 |
def cmp_le(self, w_1, w_2): |
| 543 |
return op.le(w_1, w_2).eval(self) |
| 544 |
|
| 545 |
def cmp_eq(self, w_1, w_2): |
| 546 |
return op.eq(w_1, w_2).eval(self) |
| 547 |
|
| 548 |
def cmp_ne(self, w_1, w_2): |
| 549 |
return op.ne(w_1, w_2).eval(self) |
| 550 |
|
| 551 |
def cmp_gt(self, w_1, w_2): |
| 552 |
return op.gt(w_1, w_2).eval(self) |
| 553 |
|
| 554 |
def cmp_ge(self, w_1, w_2): |
| 555 |
return op.ge(w_1, w_2).eval(self) |
| 556 |
|
| 557 |
def cmp_in(self, w_1, w_2): |
| 558 |
return op.contains(w_2, w_1).eval(self) |
| 559 |
|
| 560 |
def cmp_not_in(self, w_1, w_2): |
| 561 |
return self.not_(self.cmp_in(w_1, w_2)) |
| 562 |
|
| 563 |
def cmp_is(self, w_1, w_2): |
| 564 |
return op.is_(w_1, w_2).eval(self) |
| 565 |
|
| 566 |
def cmp_is_not(self, w_1, w_2): |
| 567 |
return self.not_(op.is_(w_1, w_2).eval(self)) |
| 568 |
|
| 569 |
def exception_match(self, w_exc_type, w_check_class): |
| 570 |
"""Checks if the given exception type matches 'w_check_class'.""" |
| 571 |
if not isinstance(w_check_class, Constant): |
| 572 |
raise FlowingError("Non-constant except guard.") |
| 573 |
check_class = w_check_class.value |
| 574 |
if not isinstance(check_class, tuple): |
| 575 |
|
| 576 |
if issubclass(check_class, (NotImplementedError, AssertionError)): |
| 577 |
raise FlowingError( |
| 578 |
"Catching NotImplementedError, AssertionError, or a " |
| 579 |
"subclass is not valid in RPython (%r)" % (check_class,)) |
| 580 |
return self.guessbool(op.issubtype(w_exc_type, w_check_class).eval(self)) |
| 581 |
|
| 582 |
if check_class == rstackovf.StackOverflow: |
| 583 |
w_real_class = const(rstackovf._StackOverflow) |
| 584 |
return self.guessbool(op.issubtype(w_exc_type, w_real_class).eval(self)) |
| 585 |
|
| 586 |
for klass in w_check_class.value: |
| 587 |
if self.exception_match(w_exc_type, const(klass)): |
| 588 |
return True |
| 589 |
return False |
| 590 |
|
| 591 |
def cmp_exc_match(self, w_1, w_2): |
| 592 |
return const(self.exception_match(w_1, w_2)) |
| 593 |
|
| 594 |
def COMPARE_OP(self, testnum): |
| 595 |
w_2 = self.popvalue() |
| 596 |
w_1 = self.popvalue() |
| 597 |
w_result = getattr(self, compare_method[testnum])(w_1, w_2) |
| 598 |
self.pushvalue(w_result) |
| 599 |
|
| 600 |
def exc_from_raise(self, w_arg1, w_arg2): |
| 601 |
""" |
| 602 |
Create a wrapped exception from the arguments of a raise statement. |
| 603 |
|
| 604 |
Returns an FSException object whose w_value is an instance of w_type. |
| 605 |
""" |
| 606 |
from rpython.rlib.debug import ll_assert_not_none |
| 607 |
|
| 608 |
check_not_none = False |
| 609 |
w_is_type = op.isinstance(w_arg1, const(type)).eval(self) |
| 610 |
if self.guessbool(w_is_type): |
| 611 |
|
| 612 |
if self.guessbool(op.is_(w_arg2, w_None).eval(self)): |
| 613 |
|
| 614 |
w_value = op.simple_call(w_arg1).eval(self) |
| 615 |
else: |
| 616 |
w_valuetype = op.type(w_arg2).eval(self) |
| 617 |
if self.guessbool(op.issubtype(w_valuetype, w_arg1).eval(self)): |
| 618 |
|
| 619 |
w_value = w_arg2 |
| 620 |
check_not_none = True |
| 621 |
else: |
| 622 |
|
| 623 |
w_value = op.simple_call(w_arg1, w_arg2).eval(self) |
| 624 |
else: |
| 625 |
|
| 626 |
if not self.guessbool(op.is_(w_arg2, const(None)).eval(self)): |
| 627 |
exc = TypeError("instance exception may not have a " |
| 628 |
"separate value") |
| 629 |
raise Raise(const(exc)) |
| 630 |
w_value = w_arg1 |
| 631 |
check_not_none = True |
| 632 |
if check_not_none: |
| 633 |
w_value = op.simple_call(const(ll_assert_not_none), |
| 634 |
w_value).eval(self) |
| 635 |
w_type = op.type(w_value).eval(self) |
| 636 |
return FSException(w_type, w_value) |
| 637 |
|
| 638 |
def RAISE_VARARGS(self, nbargs): |
| 639 |
if nbargs == 0: |
| 640 |
if self.last_exception is not None: |
| 641 |
w_exc = self.last_exception |
| 642 |
else: |
| 643 |
w_exc = const(TypeError( |
| 644 |
"raise: no active exception to re-raise")) |
| 645 |
raise Raise(w_exc) |
| 646 |
|
| 647 |
if nbargs >= 3: |
| 648 |
self.popvalue() |
| 649 |
if nbargs >= 2: |
| 650 |
w_value = self.popvalue() |
| 651 |
w_type = self.popvalue() |
| 652 |
operror = self.exc_from_raise(w_type, w_value) |
| 653 |
else: |
| 654 |
w_type = self.popvalue() |
| 655 |
operror = self.exc_from_raise(w_type, w_None) |
| 656 |
raise Raise(operror) |
| 657 |
|
| 658 |
def import_name(self, name, glob=None, loc=None, frm=None, level=-1): |
| 659 |
try: |
| 660 |
mod = __import__(name, glob, loc, frm, level) |
| 661 |
except ImportError as e: |
| 662 |
raise Raise(const(e)) |
| 663 |
return const(mod) |
| 664 |
|
| 665 |
def IMPORT_NAME(self, nameindex): |
| 666 |
modulename = self.getname_u(nameindex) |
| 667 |
glob = self.w_globals.value |
| 668 |
fromlist = self.popvalue().value |
| 669 |
level = self.popvalue().value |
| 670 |
w_obj = self.import_name(modulename, glob, None, fromlist, level) |
| 671 |
self.pushvalue(w_obj) |
| 672 |
|
| 673 |
def import_from(self, w_module, w_name): |
| 674 |
assert isinstance(w_module, Constant) |
| 675 |
assert isinstance(w_name, Constant) |
| 676 |
try: |
| 677 |
return op.getattr(w_module, w_name).eval(self) |
| 678 |
except FlowingError: |
| 679 |
exc = ImportError("cannot import name '%s'" % w_name.value) |
| 680 |
raise Raise(const(exc)) |
| 681 |
|
| 682 |
def IMPORT_FROM(self, nameindex): |
| 683 |
w_name = self.getname_w(nameindex) |
| 684 |
w_module = self.peekvalue() |
| 685 |
self.pushvalue(self.import_from(w_module, w_name)) |
| 686 |
|
| 687 |
def RETURN_VALUE(self, oparg): |
| 688 |
w_returnvalue = self.popvalue() |
| 689 |
raise Return(w_returnvalue) |
| 690 |
|
| 691 |
def END_FINALLY(self, oparg): |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
w_top = self.popvalue() |
| 702 |
if w_top == w_None: |
| 703 |
|
| 704 |
return |
| 705 |
elif isinstance(w_top, FlowSignal): |
| 706 |
|
| 707 |
raise w_top |
| 708 |
else: |
| 709 |
|
| 710 |
self.popvalue() |
| 711 |
signal = self.popvalue() |
| 712 |
raise signal |
| 713 |
|
| 714 |
def POP_BLOCK(self, oparg): |
| 715 |
block = self.blockstack.pop() |
| 716 |
block.cleanupstack(self) |
| 717 |
|
| 718 |
def JUMP_ABSOLUTE(self, jumpto): |
| 719 |
return jumpto |
| 720 |
|
| 721 |
def YIELD_VALUE(self, _): |
| 722 |
assert self.pycode.is_generator |
| 723 |
w_result = self.popvalue() |
| 724 |
op.yield_(w_result).eval(self) |
| 725 |
|
| 726 |
|
| 727 |
self.pushvalue(None) |
| 728 |
|
| 729 |
PRINT_EXPR = BAD_OPCODE |
| 730 |
PRINT_ITEM_TO = BAD_OPCODE |
| 731 |
PRINT_NEWLINE_TO = BAD_OPCODE |
| 732 |
|
| 733 |
def PRINT_ITEM(self, oparg): |
| 734 |
w_item = self.popvalue() |
| 735 |
w_s = op.str(w_item).eval(self) |
| 736 |
self.appcall(rpython_print_item, w_s) |
| 737 |
|
| 738 |
def PRINT_NEWLINE(self, oparg): |
| 739 |
self.appcall(rpython_print_newline) |
| 740 |
|
| 741 |
def JUMP_FORWARD(self, target): |
| 742 |
return target |
| 743 |
|
| 744 |
def JUMP_IF_FALSE(self, target): |
| 745 |
|
| 746 |
w_cond = self.peekvalue() |
| 747 |
if not self.guessbool(op.bool(w_cond).eval(self)): |
| 748 |
return target |
| 749 |
|
| 750 |
def JUMP_IF_TRUE(self, target): |
| 751 |
|
| 752 |
w_cond = self.peekvalue() |
| 753 |
if self.guessbool(op.bool(w_cond).eval(self)): |
| 754 |
return target |
| 755 |
|
| 756 |
def POP_JUMP_IF_FALSE(self, target): |
| 757 |
w_value = self.popvalue() |
| 758 |
if not self.guessbool(op.bool(w_value).eval(self)): |
| 759 |
return target |
| 760 |
|
| 761 |
def POP_JUMP_IF_TRUE(self, target): |
| 762 |
w_value = self.popvalue() |
| 763 |
if self.guessbool(op.bool(w_value).eval(self)): |
| 764 |
return target |
| 765 |
|
| 766 |
def JUMP_IF_FALSE_OR_POP(self, target): |
| 767 |
w_value = self.peekvalue() |
| 768 |
if not self.guessbool(op.bool(w_value).eval(self)): |
| 769 |
return target |
| 770 |
self.popvalue() |
| 771 |
|
| 772 |
def JUMP_IF_TRUE_OR_POP(self, target): |
| 773 |
w_value = self.peekvalue() |
| 774 |
if self.guessbool(op.bool(w_value).eval(self)): |
| 775 |
return target |
| 776 |
return target |
| 777 |
self.popvalue() |
| 778 |
|
| 779 |
def JUMP_IF_NOT_DEBUG(self, target): |
| 780 |
pass |
| 781 |
|
| 782 |
def GET_ITER(self, oparg): |
| 783 |
w_iterable = self.popvalue() |
| 784 |
w_iterator = op.iter(w_iterable).eval(self) |
| 785 |
self.pushvalue(w_iterator) |
| 786 |
|
| 787 |
def FOR_ITER(self, target): |
| 788 |
w_iterator = self.peekvalue() |
| 789 |
self.blockstack.append(IterBlock(self, target)) |
| 790 |
w_nextitem = op.next(w_iterator).eval(self) |
| 791 |
self.blockstack.pop() |
| 792 |
self.pushvalue(w_nextitem) |
| 793 |
|
| 794 |
def SETUP_LOOP(self, target): |
| 795 |
block = LoopBlock(self, target) |
| 796 |
self.blockstack.append(block) |
| 797 |
|
| 798 |
def SETUP_EXCEPT(self, target): |
| 799 |
block = ExceptBlock(self, target) |
| 800 |
self.blockstack.append(block) |
| 801 |
|
| 802 |
def SETUP_FINALLY(self, target): |
| 803 |
block = FinallyBlock(self, target) |
| 804 |
self.blockstack.append(block) |
| 805 |
|
| 806 |
def SETUP_WITH(self, target): |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
w_manager = self.peekvalue() |
| 811 |
w_exit = op.getattr(w_manager, const("__exit__")).eval(self) |
| 812 |
self.settopvalue(w_exit) |
| 813 |
w_enter = op.getattr(w_manager, const('__enter__')).eval(self) |
| 814 |
w_result = op.simple_call(w_enter).eval(self) |
| 815 |
block = WithBlock(self, target) |
| 816 |
self.blockstack.append(block) |
| 817 |
self.pushvalue(w_result) |
| 818 |
|
| 819 |
def WITH_CLEANUP(self, oparg): |
| 820 |
|
| 821 |
|
| 822 |
unroller = self.popvalue() |
| 823 |
w_exitfunc = self.popvalue() |
| 824 |
self.pushvalue(unroller) |
| 825 |
|
| 826 |
if isinstance(unroller, Raise): |
| 827 |
w_exc = unroller.w_exc |
| 828 |
|
| 829 |
|
| 830 |
op.simple_call(w_exitfunc, w_exc.w_value, w_exc.w_value, w_None |
| 831 |
).eval(self) |
| 832 |
else: |
| 833 |
op.simple_call(w_exitfunc, w_None, w_None, w_None).eval(self) |
| 834 |
|
| 835 |
def LOAD_FAST(self, varindex): |
| 836 |
w_value = self.locals_w[varindex] |
| 837 |
if w_value is None: |
| 838 |
raise FlowingError("Local variable referenced before assignment") |
| 839 |
self.pushvalue(w_value) |
| 840 |
|
| 841 |
def LOAD_CONST(self, constindex): |
| 842 |
w_const = self.getconstant_w(constindex) |
| 843 |
self.pushvalue(w_const) |
| 844 |
|
| 845 |
def find_global(self, w_globals, varname): |
| 846 |
try: |
| 847 |
value = w_globals.value[varname] |
| 848 |
except KeyError: |
| 849 |
|
| 850 |
try: |
| 851 |
value = getattr(__builtin__, varname) |
| 852 |
except AttributeError: |
| 853 |
raise FlowingError("global name '%s' is not defined" % varname) |
| 854 |
return const(value) |
| 855 |
|
| 856 |
def LOAD_GLOBAL(self, nameindex): |
| 857 |
w_result = self.find_global(self.w_globals, self.getname_u(nameindex)) |
| 858 |
self.pushvalue(w_result) |
| 859 |
LOAD_NAME = LOAD_GLOBAL |
| 860 |
|
| 861 |
def LOAD_ATTR(self, nameindex): |
| 862 |
"obj.attributename" |
| 863 |
w_obj = self.popvalue() |
| 864 |
w_attributename = self.getname_w(nameindex) |
| 865 |
w_value = op.getattr(w_obj, w_attributename).eval(self) |
| 866 |
self.pushvalue(w_value) |
| 867 |
LOOKUP_METHOD = LOAD_ATTR |
| 868 |
|
| 869 |
def LOAD_DEREF(self, varindex): |
| 870 |
cell = self.closure[varindex] |
| 871 |
try: |
| 872 |
content = cell.cell_contents |
| 873 |
except ValueError: |
| 874 |
name = self.pycode.co_freevars[varindex] |
| 875 |
raise FlowingError("Undefined closure variable '%s'" % name) |
| 876 |
self.pushvalue(const(content)) |
| 877 |
|
| 878 |
def STORE_FAST(self, varindex): |
| 879 |
w_newvalue = self.popvalue() |
| 880 |
assert w_newvalue is not None |
| 881 |
self.locals_w = self.locals_w[:] |
| 882 |
self.locals_w[varindex] = w_newvalue |
| 883 |
if isinstance(w_newvalue, Variable): |
| 884 |
w_newvalue.rename(self.getlocalvarname(varindex)) |
| 885 |
|
| 886 |
def STORE_GLOBAL(self, nameindex): |
| 887 |
varname = self.getname_u(nameindex) |
| 888 |
raise FlowingError( |
| 889 |
"Attempting to modify global variable %r." % (varname)) |
| 890 |
|
| 891 |
def POP_TOP(self, oparg): |
| 892 |
self.popvalue() |
| 893 |
|
| 894 |
def ROT_TWO(self, oparg): |
| 895 |
w_1 = self.popvalue() |
| 896 |
w_2 = self.popvalue() |
| 897 |
self.pushvalue(w_1) |
| 898 |
self.pushvalue(w_2) |
| 899 |
|
| 900 |
def ROT_THREE(self, oparg): |
| 901 |
w_1 = self.popvalue() |
| 902 |
w_2 = self.popvalue() |
| 903 |
w_3 = self.popvalue() |
| 904 |
self.pushvalue(w_1) |
| 905 |
self.pushvalue(w_3) |
| 906 |
self.pushvalue(w_2) |
| 907 |
|
| 908 |
def ROT_FOUR(self, oparg): |
| 909 |
w_1 = self.popvalue() |
| 910 |
w_2 = self.popvalue() |
| 911 |
w_3 = self.popvalue() |
| 912 |
w_4 = self.popvalue() |
| 913 |
self.pushvalue(w_1) |
| 914 |
self.pushvalue(w_4) |
| 915 |
self.pushvalue(w_3) |
| 916 |
self.pushvalue(w_2) |
| 917 |
|
| 918 |
def DUP_TOP(self, oparg): |
| 919 |
w_1 = self.peekvalue() |
| 920 |
self.pushvalue(w_1) |
| 921 |
|
| 922 |
def DUP_TOPX(self, itemcount): |
| 923 |
delta = itemcount - 1 |
| 924 |
while True: |
| 925 |
itemcount -= 1 |
| 926 |
if itemcount < 0: |
| 927 |
break |
| 928 |
w_value = self.peekvalue(delta) |
| 929 |
self.pushvalue(w_value) |
| 930 |
|
| 931 |
for OPCODE, op in _unary_ops: |
| 932 |
locals()[OPCODE] = unaryoperation(OPCODE, op) |
| 933 |
|
| 934 |
for OPCODE, op in _binary_ops: |
| 935 |
locals()[OPCODE] = binaryoperation(OPCODE, op) |
| 936 |
|
| 937 |
for OPCODE, op in _unsupported_ops: |
| 938 |
locals()[OPCODE] = unsupportedoperation(OPCODE, op) |
| 939 |
|
| 940 |
def BUILD_LIST_FROM_ARG(self, _): |
| 941 |
|
| 942 |
|
| 943 |
last_val = self.popvalue() |
| 944 |
self.pushvalue(op.newlist().eval(self)) |
| 945 |
self.pushvalue(last_val) |
| 946 |
|
| 947 |
def call_function(self, oparg, w_star=None, w_starstar=None): |
| 948 |
if w_starstar is not None: |
| 949 |
raise FlowingError("Dict-unpacking is not RPython") |
| 950 |
n_arguments = oparg & 0xff |
| 951 |
n_keywords = (oparg >> 8) & 0xff |
| 952 |
keywords = {} |
| 953 |
for _ in range(n_keywords): |
| 954 |
w_value = self.popvalue() |
| 955 |
w_key = self.popvalue() |
| 956 |
key = w_key.value |
| 957 |
keywords[key] = w_value |
| 958 |
arguments = self.popvalues(n_arguments) |
| 959 |
w_function = self.popvalue() |
| 960 |
try: |
| 961 |
|
| 962 |
if w_function.value == print: |
| 963 |
return self.handle_print_function(arguments, keywords, w_star) |
| 964 |
except (AttributeError, TypeError): |
| 965 |
pass |
| 966 |
|
| 967 |
args = CallSpec(arguments, keywords, w_star) |
| 968 |
if args.keywords or isinstance(args.w_stararg, Variable): |
| 969 |
shape, args_w = args.flatten() |
| 970 |
hlop = op.call_args(w_function, Constant(shape), *args_w) |
| 971 |
else: |
| 972 |
hlop = op.simple_call(w_function, *args.as_list()) |
| 973 |
self.pushvalue(hlop.eval(self)) |
| 974 |
|
| 975 |
def handle_print_function(self, arguments, keywords, w_star): |
| 976 |
if w_star: |
| 977 |
raise FlowingError( |
| 978 |
"print function with *args is not RPython" |
| 979 |
) |
| 980 |
|
| 981 |
bad_kwargs = [k for k in keywords if k != "end"] |
| 982 |
if bad_kwargs: |
| 983 |
raise FlowingError( |
| 984 |
"print function with {}= is not RPython" |
| 985 |
.format(bad_kwargs[0]) |
| 986 |
) |
| 987 |
|
| 988 |
for w_arg in arguments: |
| 989 |
w_s = op.str(w_arg).eval(self) |
| 990 |
self.appcall(rpython_print_item, w_s) |
| 991 |
|
| 992 |
if "end" in keywords: |
| 993 |
self.appcall(rpython_print_end, keywords["end"]) |
| 994 |
else: |
| 995 |
self.appcall(rpython_print_newline) |
| 996 |
self.pushvalue(Constant(None)) |
| 997 |
|
| 998 |
def CALL_FUNCTION(self, oparg): |
| 999 |
self.call_function(oparg) |
| 1000 |
CALL_METHOD = CALL_FUNCTION |
| 1001 |
|
| 1002 |
def CALL_FUNCTION_VAR(self, oparg): |
| 1003 |
w_varargs = self.popvalue() |
| 1004 |
self.call_function(oparg, w_varargs) |
| 1005 |
|
| 1006 |
def CALL_FUNCTION_KW(self, oparg): |
| 1007 |
w_varkw = self.popvalue() |
| 1008 |
self.call_function(oparg, None, w_varkw) |
| 1009 |
|
| 1010 |
def CALL_FUNCTION_VAR_KW(self, oparg): |
| 1011 |
w_varkw = self.popvalue() |
| 1012 |
w_varargs = self.popvalue() |
| 1013 |
self.call_function(oparg, w_varargs, w_varkw) |
| 1014 |
|
| 1015 |
def newfunction(self, w_code, defaults_w): |
| 1016 |
if not all(isinstance(value, Constant) for value in defaults_w): |
| 1017 |
raise FlowingError("Dynamically created function must" |
| 1018 |
" have constant default values.") |
| 1019 |
code = w_code.value |
| 1020 |
globals = self.w_globals.value |
| 1021 |
defaults = tuple([default.value for default in defaults_w]) |
| 1022 |
fn = types.FunctionType(code, globals, code.co_name, defaults) |
| 1023 |
return Constant(fn) |
| 1024 |
|
| 1025 |
def MAKE_FUNCTION(self, numdefaults): |
| 1026 |
w_codeobj = self.popvalue() |
| 1027 |
defaults = self.popvalues(numdefaults) |
| 1028 |
fn = self.newfunction(w_codeobj, defaults) |
| 1029 |
self.pushvalue(fn) |
| 1030 |
|
| 1031 |
def STORE_ATTR(self, nameindex): |
| 1032 |
"obj.attributename = newvalue" |
| 1033 |
w_attributename = self.getname_w(nameindex) |
| 1034 |
w_obj = self.popvalue() |
| 1035 |
w_newvalue = self.popvalue() |
| 1036 |
op.setattr(w_obj, w_attributename, w_newvalue).eval(self) |
| 1037 |
|
| 1038 |
def unpack_sequence(self, w_iterable, expected_length): |
| 1039 |
w_len = op.len(w_iterable).eval(self) |
| 1040 |
w_correct = op.eq(w_len, const(expected_length)).eval(self) |
| 1041 |
if not self.guessbool(op.bool(w_correct).eval(self)): |
| 1042 |
w_exc = self.exc_from_raise(const(ValueError), const(None)) |
| 1043 |
raise Raise(w_exc) |
| 1044 |
return [op.getitem(w_iterable, const(i)).eval(self) |
| 1045 |
for i in range(expected_length)] |
| 1046 |
|
| 1047 |
def UNPACK_SEQUENCE(self, itemcount): |
| 1048 |
w_iterable = self.popvalue() |
| 1049 |
items = self.unpack_sequence(w_iterable, itemcount) |
| 1050 |
for w_item in reversed(items): |
| 1051 |
self.pushvalue(w_item) |
| 1052 |
|
| 1053 |
def slice(self, w_start, w_end): |
| 1054 |
w_obj = self.popvalue() |
| 1055 |
w_result = op.getslice(w_obj, w_start, w_end).eval(self) |
| 1056 |
self.pushvalue(w_result) |
| 1057 |
|
| 1058 |
def SLICE_0(self, oparg): |
| 1059 |
self.slice(w_None, w_None) |
| 1060 |
|
| 1061 |
def SLICE_1(self, oparg): |
| 1062 |
w_start = self.popvalue() |
| 1063 |
self.slice(w_start, w_None) |
| 1064 |
|
| 1065 |
def SLICE_2(self, oparg): |
| 1066 |
w_end = self.popvalue() |
| 1067 |
self.slice(w_None, w_end) |
| 1068 |
|
| 1069 |
def SLICE_3(self, oparg): |
| 1070 |
w_end = self.popvalue() |
| 1071 |
w_start = self.popvalue() |
| 1072 |
self.slice(w_start, w_end) |
| 1073 |
|
| 1074 |
def storeslice(self, w_start, w_end): |
| 1075 |
w_obj = self.popvalue() |
| 1076 |
w_newvalue = self.popvalue() |
| 1077 |
op.setslice(w_obj, w_start, w_end, w_newvalue).eval(self) |
| 1078 |
|
| 1079 |
def STORE_SLICE_0(self, oparg): |
| 1080 |
self.storeslice(w_None, w_None) |
| 1081 |
|
| 1082 |
def STORE_SLICE_1(self, oparg): |
| 1083 |
w_start = self.popvalue() |
| 1084 |
self.storeslice(w_start, w_None) |
| 1085 |
|
| 1086 |
def STORE_SLICE_2(self, oparg): |
| 1087 |
w_end = self.popvalue() |
| 1088 |
self.storeslice(w_None, w_end) |
| 1089 |
|
| 1090 |
def STORE_SLICE_3(self, oparg): |
| 1091 |
w_end = self.popvalue() |
| 1092 |
w_start = self.popvalue() |
| 1093 |
self.storeslice(w_start, w_end) |
| 1094 |
|
| 1095 |
def deleteslice(self, w_start, w_end): |
| 1096 |
w_obj = self.popvalue() |
| 1097 |
op.delslice(w_obj, w_start, w_end).eval(self) |
| 1098 |
|
| 1099 |
def DELETE_SLICE_0(self, oparg): |
| 1100 |
self.deleteslice(w_None, w_None) |
| 1101 |
|
| 1102 |
def DELETE_SLICE_1(self, oparg): |
| 1103 |
w_start = self.popvalue() |
| 1104 |
self.deleteslice(w_start, w_None) |
| 1105 |
|
| 1106 |
def DELETE_SLICE_2(self, oparg): |
| 1107 |
w_end = self.popvalue() |
| 1108 |
self.deleteslice(w_None, w_end) |
| 1109 |
|
| 1110 |
def DELETE_SLICE_3(self, oparg): |
| 1111 |
w_end = self.popvalue() |
| 1112 |
w_start = self.popvalue() |
| 1113 |
self.deleteslice(w_start, w_end) |
| 1114 |
|
| 1115 |
def LIST_APPEND(self, oparg): |
| 1116 |
w_value = self.popvalue() |
| 1117 |
if sys.version_info < (2, 7): |
| 1118 |
w_list = self.popvalue() |
| 1119 |
else: |
| 1120 |
w_list = self.peekvalue(oparg - 1) |
| 1121 |
w_append_meth = op.getattr(w_list, const('append')).eval(self) |
| 1122 |
op.simple_call(w_append_meth, w_value).eval(self) |
| 1123 |
|
| 1124 |
def DELETE_FAST(self, varindex): |
| 1125 |
if self.locals_w[varindex] is None: |
| 1126 |
varname = self.getlocalvarname(varindex) |
| 1127 |
message = "local variable '%s' referenced before assignment" |
| 1128 |
raise UnboundLocalError(message, varname) |
| 1129 |
self.locals_w = self.locals_w[:] |
| 1130 |
self.locals_w[varindex] = None |
| 1131 |
|
| 1132 |
def STORE_MAP(self, oparg): |
| 1133 |
w_key = self.popvalue() |
| 1134 |
w_value = self.popvalue() |
| 1135 |
w_dict = self.peekvalue() |
| 1136 |
op.setitem(w_dict, w_key, w_value).eval(self) |
| 1137 |
|
| 1138 |
def STORE_SUBSCR(self, oparg): |
| 1139 |
"obj[subscr] = newvalue" |
| 1140 |
w_subscr = self.popvalue() |
| 1141 |
w_obj = self.popvalue() |
| 1142 |
w_newvalue = self.popvalue() |
| 1143 |
op.setitem(w_obj, w_subscr, w_newvalue).eval(self) |
| 1144 |
|
| 1145 |
def BUILD_SLICE(self, numargs): |
| 1146 |
if numargs == 3: |
| 1147 |
w_step = self.popvalue() |
| 1148 |
elif numargs == 2: |
| 1149 |
w_step = w_None |
| 1150 |
else: |
| 1151 |
raise BytecodeCorruption |
| 1152 |
w_end = self.popvalue() |
| 1153 |
w_start = self.popvalue() |
| 1154 |
w_slice = op.newslice(w_start, w_end, w_step).eval(self) |
| 1155 |
self.pushvalue(w_slice) |
| 1156 |
|
| 1157 |
def DELETE_SUBSCR(self, oparg): |
| 1158 |
"del obj[subscr]" |
| 1159 |
w_subscr = self.popvalue() |
| 1160 |
w_obj = self.popvalue() |
| 1161 |
op.delitem(w_obj, w_subscr).eval(self) |
| 1162 |
|
| 1163 |
def BUILD_TUPLE(self, itemcount): |
| 1164 |
items = self.popvalues(itemcount) |
| 1165 |
w_tuple = op.newtuple(*items).eval(self) |
| 1166 |
self.pushvalue(w_tuple) |
| 1167 |
|
| 1168 |
def BUILD_LIST(self, itemcount): |
| 1169 |
items = self.popvalues(itemcount) |
| 1170 |
w_list = op.newlist(*items).eval(self) |
| 1171 |
self.pushvalue(w_list) |
| 1172 |
|
| 1173 |
def BUILD_MAP(self, itemcount): |
| 1174 |
w_dict = op.newdict().eval(self) |
| 1175 |
self.pushvalue(w_dict) |
| 1176 |
|
| 1177 |
def NOP(self, *args): |
| 1178 |
pass |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
def BUILD_SET(self, oparg): |
| 1185 |
raise NotImplementedError("BUILD_SET") |
| 1186 |
|
| 1187 |
def SET_ADD(self, oparg): |
| 1188 |
raise NotImplementedError("SET_ADD") |
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
def MAP_ADD(self, oparg): |
| 1193 |
raise NotImplementedError("MAP_ADD") |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
STORE_DEREF = BAD_OPCODE |
| 1198 |
LOAD_CLOSURE = BAD_OPCODE |
| 1199 |
MAKE_CLOSURE = BAD_OPCODE |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
class FlowSignal(Exception): |
| 1204 |
"""Abstract base class for translator-level objects that instruct the |
| 1205 |
interpreter to change the control flow and the block stack. |
| 1206 |
|
| 1207 |
The concrete subclasses correspond to the various values WHY_XXX |
| 1208 |
values of the why_code enumeration in ceval.c: |
| 1209 |
|
| 1210 |
WHY_NOT, OK, not this one :-) |
| 1211 |
WHY_EXCEPTION, Raise |
| 1212 |
WHY_RERAISE, implemented differently, see Reraise |
| 1213 |
WHY_RETURN, Return |
| 1214 |
WHY_BREAK, Break |
| 1215 |
WHY_CONTINUE, Continue |
| 1216 |
WHY_YIELD not needed |
| 1217 |
""" |
| 1218 |
def nomoreblocks(self, ctx): |
| 1219 |
raise BytecodeCorruption("misplaced bytecode - should not return") |
| 1220 |
|
| 1221 |
def __eq__(self, other): |
| 1222 |
return type(other) is type(self) and other.args == self.args |
| 1223 |
|
| 1224 |
|
| 1225 |
class Return(FlowSignal): |
| 1226 |
"""Signals a 'return' statement. |
| 1227 |
Argument is the wrapped object to return. |
| 1228 |
""" |
| 1229 |
def __init__(self, w_value): |
| 1230 |
self.w_value = w_value |
| 1231 |
|
| 1232 |
def nomoreblocks(self, ctx): |
| 1233 |
w_result = self.w_value |
| 1234 |
link = Link([w_result], ctx.graph.returnblock) |
| 1235 |
ctx.recorder.crnt_block.closeblock(link) |
| 1236 |
raise StopFlowing |
| 1237 |
|
| 1238 |
@property |
| 1239 |
def args(self): |
| 1240 |
return [self.w_value] |
| 1241 |
|
| 1242 |
@staticmethod |
| 1243 |
def rebuild(w_value): |
| 1244 |
return Return(w_value) |
| 1245 |
|
| 1246 |
class Raise(FlowSignal): |
| 1247 |
"""Signals an application-level exception |
| 1248 |
(i.e. an OperationException).""" |
| 1249 |
|
| 1250 |
def __init__(self, w_exc): |
| 1251 |
self.w_exc = w_exc |
| 1252 |
|
| 1253 |
def nomoreblocks(self, ctx): |
| 1254 |
w_exc = self.w_exc |
| 1255 |
if w_exc.w_type == const(ImportError): |
| 1256 |
msg = 'ImportError is raised in RPython: %s' % ( |
| 1257 |
getattr(w_exc.w_value, 'value', '<not a constant message>'),) |
| 1258 |
raise ImportError(msg) |
| 1259 |
link = Link([w_exc.w_type, w_exc.w_value], ctx.graph.exceptblock) |
| 1260 |
ctx.recorder.crnt_block.closeblock(link) |
| 1261 |
raise StopFlowing |
| 1262 |
|
| 1263 |
@property |
| 1264 |
def args(self): |
| 1265 |
return [self.w_exc.w_type, self.w_exc.w_value] |
| 1266 |
|
| 1267 |
@classmethod |
| 1268 |
def rebuild(cls, w_type, w_value): |
| 1269 |
return cls(FSException(w_type, w_value)) |
| 1270 |
|
| 1271 |
class RaiseImplicit(Raise): |
| 1272 |
"""Signals an exception raised implicitly""" |
| 1273 |
def nomoreblocks(self, ctx): |
| 1274 |
w_exc = self.w_exc |
| 1275 |
if isinstance(w_exc.w_type, Constant): |
| 1276 |
exc_cls = w_exc.w_type.value |
| 1277 |
else: |
| 1278 |
exc_cls = Exception |
| 1279 |
msg = "implicit %s shouldn't occur" % exc_cls.__name__ |
| 1280 |
w_type = Constant(AssertionError) |
| 1281 |
w_value = Constant(AssertionError(msg)) |
| 1282 |
link = Link([w_type, w_value], ctx.graph.exceptblock) |
| 1283 |
ctx.recorder.crnt_block.closeblock(link) |
| 1284 |
raise StopFlowing |
| 1285 |
|
| 1286 |
|
| 1287 |
class Break(FlowSignal): |
| 1288 |
"""Signals a 'break' statement.""" |
| 1289 |
|
| 1290 |
@property |
| 1291 |
def args(self): |
| 1292 |
return [] |
| 1293 |
|
| 1294 |
@staticmethod |
| 1295 |
def rebuild(): |
| 1296 |
return Break.singleton |
| 1297 |
|
| 1298 |
Break.singleton = Break() |
| 1299 |
|
| 1300 |
class Continue(FlowSignal): |
| 1301 |
"""Signals a 'continue' statement. |
| 1302 |
Argument is the bytecode position of the beginning of the loop.""" |
| 1303 |
|
| 1304 |
def __init__(self, jump_to): |
| 1305 |
self.jump_to = jump_to |
| 1306 |
|
| 1307 |
@property |
| 1308 |
def args(self): |
| 1309 |
return [const(self.jump_to)] |
| 1310 |
|
| 1311 |
@staticmethod |
| 1312 |
def rebuild(w_jump_to): |
| 1313 |
return Continue(w_jump_to.value) |
| 1314 |
|
| 1315 |
|
| 1316 |
class FrameBlock(object): |
| 1317 |
"""Abstract base class for frame blocks from the blockstack, |
| 1318 |
used by the SETUP_XXX and POP_BLOCK opcodes.""" |
| 1319 |
|
| 1320 |
def __init__(self, ctx, handlerposition): |
| 1321 |
self.handlerposition = handlerposition |
| 1322 |
self.stackdepth = ctx.stackdepth |
| 1323 |
|
| 1324 |
def __eq__(self, other): |
| 1325 |
return (self.__class__ is other.__class__ and |
| 1326 |
self.handlerposition == other.handlerposition and |
| 1327 |
self.stackdepth == other.stackdepth) |
| 1328 |
|
| 1329 |
def __ne__(self, other): |
| 1330 |
return not (self == other) |
| 1331 |
|
| 1332 |
def __hash__(self): |
| 1333 |
return hash((self.handlerposition, self.stackdepth)) |
| 1334 |
|
| 1335 |
def cleanupstack(self, ctx): |
| 1336 |
ctx.dropvaluesuntil(self.stackdepth) |
| 1337 |
|
| 1338 |
def handle(self, ctx, unroller): |
| 1339 |
raise NotImplementedError |
| 1340 |
|
| 1341 |
class LoopBlock(FrameBlock): |
| 1342 |
"""A loop block. Stores the end-of-loop pointer in case of 'break'.""" |
| 1343 |
|
| 1344 |
handles = (Break, Continue) |
| 1345 |
|
| 1346 |
def handle(self, ctx, unroller): |
| 1347 |
if isinstance(unroller, Continue): |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
ctx.blockstack.append(self) |
| 1352 |
return unroller.jump_to |
| 1353 |
else: |
| 1354 |
|
| 1355 |
self.cleanupstack(ctx) |
| 1356 |
return self.handlerposition |
| 1357 |
|
| 1358 |
class ExceptBlock(FrameBlock): |
| 1359 |
"""An try:except: block. Stores the position of the exception handler.""" |
| 1360 |
|
| 1361 |
handles = Raise |
| 1362 |
|
| 1363 |
def handle(self, ctx, unroller): |
| 1364 |
|
| 1365 |
|
| 1366 |
self.cleanupstack(ctx) |
| 1367 |
assert isinstance(unroller, Raise) |
| 1368 |
w_exc = unroller.w_exc |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
ctx.pushvalue(unroller) |
| 1373 |
ctx.pushvalue(w_exc.w_value) |
| 1374 |
ctx.pushvalue(w_exc.w_type) |
| 1375 |
ctx.last_exception = w_exc |
| 1376 |
return self.handlerposition |
| 1377 |
|
| 1378 |
class IterBlock(ExceptBlock): |
| 1379 |
"""A pseudo-block to catch the StopIteration inside FOR_ITER""" |
| 1380 |
def handle(self, ctx, unroller): |
| 1381 |
w_exc = unroller.w_exc |
| 1382 |
if ctx.exception_match(w_exc.w_type, const(StopIteration)): |
| 1383 |
ctx.popvalue() |
| 1384 |
return self.handlerposition |
| 1385 |
else: |
| 1386 |
return ctx.unroll(unroller) |
| 1387 |
|
| 1388 |
class FinallyBlock(FrameBlock): |
| 1389 |
"""A try:finally: block. Stores the position of the exception handler.""" |
| 1390 |
|
| 1391 |
handles = FlowSignal |
| 1392 |
|
| 1393 |
def handle(self, ctx, unroller): |
| 1394 |
|
| 1395 |
|
| 1396 |
self.cleanupstack(ctx) |
| 1397 |
ctx.pushvalue(unroller) |
| 1398 |
return self.handlerposition |
| 1399 |
|
| 1400 |
|
| 1401 |
class WithBlock(FinallyBlock): |
| 1402 |
|
| 1403 |
def handle(self, ctx, unroller): |
| 1404 |
return FinallyBlock.handle(self, ctx, unroller) |