| 1 | from pypy.interpreter.error import OperationError, oefmt |
| 2 | from pypy.interpreter.gateway import WrappedDefault, unwrap_spec |
| 3 | from pypy.tool import stdlib_opcode |
| 4 | from pypy.interpreter.astcompiler.assemble import (_opcode_stack_effect, |
| 5 | _opcode_stack_effect_jump) |
| 6 | |
| 7 | |
| 8 | @unwrap_spec(opcode=int) |
| 9 | def stack_effect(space, opcode, w_oparg=None, w_jump=None): |
| 10 | "Compute the stack effect of the opcode." |
| 11 | if opcode == stdlib_opcode.EXTENDED_ARG: |
| 12 | if space.is_none(w_oparg): |
| 13 | raise oefmt(space.w_ValueError, |
| 14 | "stack_effect: opcode requires oparg but oparg was not specified") |
| 15 | return space.newint(0) |
| 16 | if opcode >= stdlib_opcode.HAVE_ARGUMENT: |
| 17 | if space.is_none(w_oparg): |
| 18 | raise oefmt(space.w_ValueError, |
| 19 | "stack_effect: opcode requires oparg but oparg was not specified") |
| 20 | oparg = space.int_w(w_oparg) |
| 21 | else: |
| 22 | if not space.is_none(w_oparg): |
| 23 | raise oefmt(space.w_ValueError, |
| 24 | "stack_effect: opcode does not permit oparg but oparg was specified") |
| 25 | oparg = -1 |
| 26 | try: |
| 27 | withoutjump = _opcode_stack_effect(opcode, oparg) |
| 28 | except KeyError: |
| 29 | raise oefmt(space.w_ValueError, |
| 30 | "invalid opcode or oparg") |
| 31 | hasjump = opcode in stdlib_opcode.hasjrel or opcode in stdlib_opcode.hasjabs |
| 32 | if hasjump: |
| 33 | withjump = _opcode_stack_effect_jump(opcode) |
| 34 | if space.is_none(w_jump): |
| 35 | return space.newint(max(withoutjump, withjump)) |
| 36 | elif space.is_true(w_jump): |
| 37 | return space.newint(withjump) |
| 38 | else: |
| 39 | return space.newint(withoutjump) |
| 40 | return space.newint(withoutjump) |