| 1 | """StdObjSpace custom opcode implementations""" |
| 2 | |
| 3 | import operator |
| 4 | |
| 5 | from rpython.rlib.rarithmetic import ovfcheck |
| 6 | from rpython.tool.sourcetools import func_renamer |
| 7 | |
| 8 | from pypy.interpreter.pyframe import PyFrame |
| 9 | from pypy.interpreter.error import oefmt |
| 10 | from pypy.objspace.std.intobject import W_IntObject |
| 11 | from pypy.objspace.std.listobject import W_ListObject |
| 12 | |
| 13 | |
| 14 | class BaseFrame(PyFrame): |
| 15 | """These opcodes are always overridden.""" |
| 16 | |
| 17 | def LIST_APPEND(self, oparg, next_instr): |
| 18 | w = self.popvalue() |
| 19 | v = self.peekvalue(oparg - 1) |
| 20 | if type(v) is W_ListObject: |
| 21 | v.append(w) |
| 22 | else: |
| 23 | raise AssertionError |
| 24 | |
| 25 | |
| 26 | def _intshortcut(spaceopname): |
| 27 | if spaceopname.startswith('inplace_'): |
| 28 | opname = spaceopname[len('inplace_'):] |
| 29 | funcprefix = 'int_' |
| 30 | else: |
| 31 | opname = spaceopname |
| 32 | funcprefix = 'int_BINARY_' |
| 33 | op = getattr(operator, opname) |
| 34 | int_op = getattr(W_IntObject, 'descr_' + opname) |
| 35 | |
| 36 | @func_renamer(funcprefix + spaceopname.upper()) |
| 37 | def opimpl(self, oparg, next_instr): |
| 38 | space = self.space |
| 39 | space_op = getattr(space, spaceopname) |
| 40 | |
| 41 | w_2 = self.popvalue() |
| 42 | w_1 = self.popvalue() |
| 43 | if type(w_1) is W_IntObject and type(w_2) is W_IntObject: |
| 44 | try: |
| 45 | z = ovfcheck(op(w_1.intval, w_2.intval)) |
| 46 | except OverflowError: |
| 47 | w_result = int_op(w_1, space, w_2) |
| 48 | else: |
| 49 | w_result = space.newint(z) |
| 50 | else: |
| 51 | w_result = space_op(w_1, w_2) |
| 52 | self.pushvalue(w_result) |
| 53 | |
| 54 | return opimpl |
| 55 | |
| 56 | |
| 57 | int_BINARY_ADD = _intshortcut('add') |
| 58 | int_INPLACE_ADD = _intshortcut('inplace_add') |
| 59 | int_BINARY_SUBTRACT = _intshortcut('sub') |
| 60 | int_INPLACE_SUBTRACT = _intshortcut('inplace_sub') |
| 61 | |
| 62 | |
| 63 | def list_BINARY_SUBSCR(self, oparg, next_instr): |
| 64 | space = self.space |
| 65 | w_2 = self.popvalue() |
| 66 | w_1 = self.popvalue() |
| 67 | if type(w_1) is W_ListObject and type(w_2) is W_IntObject: |
| 68 | try: |
| 69 | w_result = w_1.getitem(w_2.intval) |
| 70 | except IndexError: |
| 71 | raise oefmt(space.w_IndexError, "list index out of range") |
| 72 | else: |
| 73 | w_result = space.getitem(w_1, w_2) |
| 74 | self.pushvalue(w_result) |
| 75 | |
| 76 | |
| 77 | def build_frame(space): |
| 78 | """Consider the objspace config and return a patched frame object.""" |
| 79 | class StdObjSpaceFrame(BaseFrame): |
| 80 | pass |
| 81 | if space.config.objspace.std.intshortcut: |
| 82 | StdObjSpaceFrame.BINARY_ADD = int_BINARY_ADD |
| 83 | StdObjSpaceFrame.INPLACE_ADD = int_INPLACE_ADD |
| 84 | StdObjSpaceFrame.BINARY_SUBTRACT = int_BINARY_SUBTRACT |
| 85 | StdObjSpaceFrame.INPLACE_SUBTRACT = int_INPLACE_SUBTRACT |
| 86 | if space.config.objspace.std.optimized_list_getitem: |
| 87 | StdObjSpaceFrame.BINARY_SUBSCR = list_BINARY_SUBSCR |
| 88 | from pypy.objspace.std.callmethod import LOAD_METHOD, CALL_METHOD, CALL_METHOD_KW |
| 89 | StdObjSpaceFrame.LOAD_METHOD = LOAD_METHOD |
| 90 | StdObjSpaceFrame.CALL_METHOD = CALL_METHOD |
| 91 | StdObjSpaceFrame.CALL_METHOD_KW = CALL_METHOD_KW |
| 92 | return StdObjSpaceFrame |