예제 #1
0
    def _reduce_argmax_argmin_impl(op_name):
        reduce_driver = jit.JitDriver(
            greens=['shapelen', 'sig'],
            reds=['result', 'idx', 'frame', 'self', 'cur_best', 'dtype'],
            get_printable_location=signature.new_printable_location(op_name),
            name='numpy_' + op_name,
        )

        def loop(self):
            sig = self.find_sig()
            frame = sig.create_frame(self)
            cur_best = sig.eval(frame, self)
            shapelen = len(self.shape)
            frame.next(shapelen)
            dtype = self.find_dtype()
            result = 0
            idx = 1
            while not frame.done():
                reduce_driver.jit_merge_point(sig=sig,
                                              shapelen=shapelen,
                                              self=self,
                                              dtype=dtype,
                                              frame=frame,
                                              result=result,
                                              idx=idx,
                                              cur_best=cur_best)
                new_best = getattr(dtype.itemtype,
                                   op_name)(cur_best, sig.eval(frame, self))
                if dtype.itemtype.ne(new_best, cur_best):
                    result = idx
                    cur_best = new_best
                frame.next(shapelen)
                idx += 1
            return result

        def impl(self, space):
            if self.size == 0:
                raise OperationError(
                    space.w_ValueError,
                    space.wrap("Can't call %s on zero-size arrays" % op_name))
            return space.wrap(loop(self))

        return func_with_new_name(impl, "reduce_arg%s_impl" % op_name)
예제 #2
0
    def _create_unpack_into():
        jitdriver = jit.JitDriver(greens=['pycode'],
                                  reds=['self', 'frame', 'results'],
                                  name='unpack_into')

        def unpack_into(self, results):
            """This is a hack for performance: runs the generator and collects
            all produced items in a list."""
            # XXX copied and simplified version of send_ex()
            space = self.space
            if self.running:
                raise OperationError(space.w_ValueError,
                                     space.wrap('generator already executing'))
            frame = self.frame
            if frame is None:  # already finished
                return
            self.running = True
            try:
                pycode = self.pycode
                while True:
                    jitdriver.jit_merge_point(self=self,
                                              frame=frame,
                                              results=results,
                                              pycode=pycode)
                    try:
                        w_result = frame.execute_frame(space.w_None)
                    except OperationError, e:
                        if not e.match(space, space.w_StopIteration):
                            raise
                        break
                    # if the frame is now marked as finished, it was RETURNed from
                    if frame.frame_finished_execution:
                        break
                    results.append(w_result)  # YIELDed
            finally:
                frame.f_backref = jit.vref_None
                self.running = False
                self.frame = None

        return unpack_into
예제 #3
0
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.gateway import interp2app, NoneNotWrapped
from pypy.interpreter.typedef import TypeDef, GetSetProperty
from pypy.module.micronumpy import interp_ufuncs, interp_dtype, signature
from pypy.module.micronumpy.strides import calculate_slice_strides
from pypy.rlib import jit
from pypy.rpython.lltypesystem import lltype, rffi
from pypy.tool.sourcetools import func_with_new_name
from pypy.rlib.rstring import StringBuilder
from pypy.module.micronumpy.interp_iter import ArrayIterator, OneDimIterator,\
     SkipLastAxisIterator

numpy_driver = jit.JitDriver(
    greens=['shapelen', 'sig'],
    virtualizables=['frame'],
    reds=['result_size', 'frame', 'ri', 'self', 'result'],
    get_printable_location=signature.new_printable_location('numpy'),
    name='numpy',
)
all_driver = jit.JitDriver(
    greens=['shapelen', 'sig'],
    virtualizables=['frame'],
    reds=['frame', 'self', 'dtype'],
    get_printable_location=signature.new_printable_location('all'),
    name='numpy_all',
)
any_driver = jit.JitDriver(
    greens=['shapelen', 'sig'],
    virtualizables=['frame'],
    reds=['frame', 'self', 'dtype'],
    get_printable_location=signature.new_printable_location('any'),
예제 #4
0
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.gateway import interp2app
from pypy.interpreter.typedef import TypeDef, GetSetProperty, interp_attrproperty
from pypy.module.micronumpy import interp_boxes, interp_dtype
from pypy.module.micronumpy.signature import ReduceSignature,\
     find_sig, new_printable_location, AxisReduceSignature, ScalarSignature
from pypy.rlib import jit
from pypy.rlib.rarithmetic import LONG_BIT
from pypy.tool.sourcetools import func_with_new_name

reduce_driver = jit.JitDriver(
    greens=['shapelen', "sig"],
    virtualizables=["frame"],
    reds=["frame", "self", "dtype", "value", "obj"],
    get_printable_location=new_printable_location('reduce'),
    name='numpy_reduce',
)

axisreduce_driver = jit.JitDriver(
    greens=['shapelen', 'sig'],
    virtualizables=['frame'],
    reds=['self', 'arr', 'identity', 'frame'],
    name='numpy_axisreduce',
    get_printable_location=new_printable_location('axisreduce'),
)


class W_Ufunc(Wrappable):
    _attrs_ = ["name", "promote_to_float", "promote_bools", "identity"]
    _immutable_fields_ = ["promote_to_float", "promote_bools", "name"]
예제 #5
0
def test_stack_alignment():
    # we can detect gcc 4.5.0 to test those as well
    if sys.platform != 'darwin':
        py.test.skip("tests darwin only stack alignment requirements")

    externs = [
        """
extern void check0();
extern void check1(int);
extern void check2(int, int);
extern void check3(int, int, int);
"""
    ]
    c_source = r"""
#include <stdio.h>
    
void check0() {
    void *ip = __builtin_return_address(0);
    void *fp = __builtin_frame_address(0);
    printf("0 %p %p %u\n", ip, fp, (unsigned)fp % 16);
}

void check1(int a) {
    void *ip = __builtin_return_address(0);
    void *fp = __builtin_frame_address(0);
    printf("1 %p %p %u\n", ip, fp, (unsigned)fp % 16);
}

void check2(int a, int b) {
    void *ip = __builtin_return_address(0);
    void *fp = __builtin_frame_address(0);
    printf("2 %p %p %u\n", ip, fp, (unsigned)fp % 16);
}

void check3(int a, int b, int c) {
    void *ip = __builtin_return_address(0);
    void *fp = __builtin_frame_address(0);
    printf("3 %p %p %u\n", ip, fp, (unsigned)fp % 16);
}
"""

    eci = rffi.ExternalCompilationInfo(
        separate_module_sources=[c_source],
        post_include_bits=externs,
        # not ideal, would like to apply this only to the checkX
        # functions
        compile_extra=["-fno-omit-frame-pointer"])

    check0 = rffi.llexternal('check0', [],
                             lltype.Void,
                             compilation_info=eci,
                             _nowrapper=True)
    check1 = rffi.llexternal('check1', [lltype.Signed],
                             lltype.Void,
                             compilation_info=eci,
                             _nowrapper=True)
    check2 = rffi.llexternal('check2', [lltype.Signed, lltype.Signed],
                             lltype.Void,
                             compilation_info=eci,
                             _nowrapper=True)

    check3 = rffi.llexternal('check3',
                             [lltype.Signed, lltype.Signed, lltype.Signed],
                             lltype.Void,
                             compilation_info=eci,
                             _nowrapper=True)

    myjitdriver = jit.JitDriver(greens=[], reds=['n'])

    def entrypoint(argv):
        jit.set_param(myjitdriver, 'threshold', 2)
        jit.set_param(myjitdriver, 'trace_eagerness', 0)
        n = 16
        while n > 0:
            myjitdriver.can_enter_jit(n=n)
            myjitdriver.jit_merge_point(n=n)
            n -= 1
            check0()
            check1(0)
            check2(0, 1)
            check3(0, 1, 2)
        return 0

    output = compile_and_run(entrypoint, 'boehm', jit=True)
    for line in output.splitlines():
        print line
        # ret ip + bp == 8
        assert int(line.split()[-1]) == 8
예제 #6
0
class Interpreter(object):
    jitdriver = jit.JitDriver(
        greens=["pc", "bytecode", "block_bytecode"],
        reds=["self", "frame"],
        virtualizables=["frame"],
        get_printable_location=get_printable_location,
    )

    def get_block_bytecode(self, block):
        return block.bytecode if block is not None else None

    def interpret(self, space, frame, bytecode):
        pc = 0
        try:
            while True:
                self.jitdriver.jit_merge_point(
                    self=self, bytecode=bytecode, frame=frame, pc=pc,
                    block_bytecode=self.get_block_bytecode(frame.block),
                )
                try:
                    pc = self.handle_bytecode(space, pc, frame, bytecode)
                except RubyError as e:
                    pc = self.handle_ruby_error(space, pc, frame, bytecode, e)
                except RaiseReturn as e:
                    pc = self.handle_raise_return(space, pc, frame, bytecode, e)
        except RaiseReturn as e:
            if e.parent_interp is self:
                return e.w_value
            raise
        except Return as e:
            return e.w_value

    def handle_bytecode(self, space, pc, frame, bytecode):
        instr = ord(bytecode.code[pc])
        pc += 1
        if we_are_translated():
            for i, name in consts.UNROLLING_BYTECODES:
                if i == instr:
                    pc = self.run_instr(space, name, consts.BYTECODE_NUM_ARGS[i], bytecode, frame, pc)
                    break
            else:
                raise NotImplementedError
        else:
            pc = self.run_instr(space, consts.BYTECODE_NAMES[instr], consts.BYTECODE_NUM_ARGS[instr], bytecode, frame, pc)
        return pc

    @specialize.arg(2, 3)
    def run_instr(self, space, name, num_args, bytecode, frame, pc):
        args = ()
        # Do not change these from * 256 to << 8, lshift has defined overflow
        # semantics which cause it to not propogate the nonnegative-ness.
        if num_args >= 1:
            v = ord(bytecode.code[pc]) | (ord(bytecode.code[pc + 1]) * 256)
            check_nonneg(v)
            args += (v,)
            pc += 2
        if num_args >= 2:
            v = ord(bytecode.code[pc]) | (ord(bytecode.code[pc + 1]) * 256)
            check_nonneg(v)
            args += (v,)
            pc += 2
        if num_args >= 3:
            raise NotImplementedError

        method = getattr(self, name)
        res = method(space, bytecode, frame, pc, *args)
        if res is not None:
            pc = res
        return pc

    def handle_ruby_error(self, space, pc, frame, bytecode, e):
        e.w_value.last_instructions.append(pc)
        block = frame.unrollstack(ApplicationException.kind)
        if block is None:
            raise e
        unroller = ApplicationException(e)
        return block.handle(space, frame, unroller)

    def handle_raise_return(self, space, pc, frame, bytecode, e):
        block = frame.unrollstack(RaiseReturnValue.kind)
        if block is None:
            raise e
        unroller = RaiseReturnValue(e.parent_interp, e.w_value)
        return block.handle(space, frame, unroller)

    def jump(self, space, bytecode, frame, cur_pc, target_pc):
        if target_pc < cur_pc:
            self.jitdriver.can_enter_jit(
                self=self, bytecode=bytecode, frame=frame, pc=target_pc,
                block_bytecode=self.get_block_bytecode(frame.block),
            )
        return target_pc

    def LOAD_SELF(self, space, bytecode, frame, pc):
        w_self = frame.w_self
        jit.promote(space.getclass(w_self))
        frame.push(w_self)

    def LOAD_SCOPE(self, space, bytecode, frame, pc):
        frame.push(jit.promote(frame.w_scope))

    def LOAD_CODE(self, space, bytecode, frame, pc):
        frame.push(bytecode)

    def LOAD_CONST(self, space, bytecode, frame, pc, idx):
        frame.push(bytecode.consts_w[idx])

    def LOAD_LOCAL(self, space, bytecode, frame, pc, idx):
        frame.push(frame.locals_w[idx] or space.w_nil)

    def STORE_LOCAL(self, space, bytecode, frame, pc, idx):
        frame.locals_w[idx] = frame.peek()

    def LOAD_DEREF(self, space, bytecode, frame, pc, idx):
        frame.push(frame.cells[idx].get())

    def STORE_DEREF(self, space, bytecode, frame, pc, idx):
        frame.cells[idx].set(frame.peek())

    def LOAD_CLOSURE(self, space, bytecode, frame, pc, idx):
        frame.push(frame.cells[idx])

    def LOAD_CONSTANT(self, space, bytecode, frame, pc, idx):
        w_scope = frame.pop()
        w_name = bytecode.consts_w[idx]
        name = space.symbol_w(w_name)
        w_obj = space.find_const(w_scope, name)
        frame.push(w_obj)

    def STORE_CONSTANT(self, space, bytecode, frame, pc, idx):
        w_name = bytecode.consts_w[idx]
        name = space.symbol_w(w_name)
        w_value = frame.pop()
        w_scope = frame.pop()
        space.set_const(w_scope, name, w_value)
        frame.push(w_value)

    def DEFINED_CONSTANT(self, space, bytecode, frame, pc, idx):
        w_name = bytecode.consts_w[idx]
        w_scope = frame.pop()
        if space.is_true(space.send(w_scope, space.newsymbol("const_defined?"), [w_name])):
            frame.push(space.newstr_fromstr("constant"))
        else:
            frame.push(space.w_nil)

    def LOAD_INSTANCE_VAR(self, space, bytecode, frame, pc, idx):
        w_name = bytecode.consts_w[idx]
        w_obj = frame.pop()
        w_res = space.find_instance_var(w_obj, space.symbol_w(w_name))
        frame.push(w_res)

    def STORE_INSTANCE_VAR(self, space, bytecode, frame, pc, idx):
        w_name = bytecode.consts_w[idx]
        w_value = frame.pop()
        w_obj = frame.pop()
        space.set_instance_var(w_obj, space.symbol_w(w_name), w_value)
        frame.push(w_value)

    def DEFINED_INSTANCE_VAR(self, space, bytecode, frame, pc, idx):
        w_name = bytecode.consts_w[idx]
        w_obj = frame.pop()
        if space.is_true(space.send(w_obj, space.newsymbol("instance_variable_defined?"), [w_name])):
            frame.push(space.newstr_fromstr("instance-variable"))
        else:
            frame.push(space.w_nil)

    def LOAD_CLASS_VAR(self, space, bytecode, frame, pc, idx):
        name = space.symbol_w(bytecode.consts_w[idx])
        w_module = frame.pop()
        assert isinstance(w_module, W_ModuleObject)
        w_value = space.find_class_var(w_module, name)
        if w_value is None:
            raise space.error(space.w_NameError,
                "uninitialized class variable %s in %s" % (name, w_module.name)
            )
        frame.push(w_value)

    def STORE_CLASS_VAR(self, space, bytecode, frame, pc, idx):
        name = space.symbol_w(bytecode.consts_w[idx])
        w_value = frame.pop()
        w_module = frame.pop()
        assert isinstance(w_module, W_ModuleObject)
        space.set_class_var(w_module, name, w_value)
        frame.push(w_value)

    def LOAD_GLOBAL(self, space, bytecode, frame, pc, idx):
        name = space.symbol_w(bytecode.consts_w[idx])
        w_value = space.globals.get(name) or space.w_nil
        frame.push(w_value)

    def STORE_GLOBAL(self, space, bytecode, frame, pc, idx):
        name = space.symbol_w(bytecode.consts_w[idx])
        w_value = frame.peek()
        space.globals.set(name, w_value)

    @jit.unroll_safe
    def BUILD_ARRAY(self, space, bytecode, frame, pc, n_items):
        items_w = frame.popitemsreverse(n_items)
        frame.push(space.newarray(items_w))

    @jit.unroll_safe
    def BUILD_STRING(self, space, bytecode, frame, pc, n_items):
        items_w = frame.popitemsreverse(n_items)
        total_length = 0
        for w_item in items_w:
            assert isinstance(w_item, W_StringObject)
            total_length += w_item.length()

        storage = newlist_hint(total_length)
        for w_item in items_w:
            assert isinstance(w_item, W_StringObject)
            w_item.strategy.extend_into(w_item.str_storage, storage)
        frame.push(space.newstr_fromchars(storage))

    def BUILD_HASH(self, space, bytecode, frame, pc):
        frame.push(space.newhash())

    def BUILD_RANGE(self, space, bytecode, frame, pc):
        w_end = frame.pop()
        w_start = frame.pop()
        w_range = space.newrange(w_start, w_end, False)
        frame.push(w_range)

    def BUILD_RANGE_EXCLUSIVE(self, space, bytecode, frame, pc):
        w_end = frame.pop()
        w_start = frame.pop()
        w_range = space.newrange(w_start, w_end, True)
        frame.push(w_range)

    def BUILD_FUNCTION(self, space, bytecode, frame, pc):
        w_code = frame.pop()
        w_name = frame.pop()
        w_func = space.newfunction(w_name, w_code)
        frame.push(w_func)

    @jit.unroll_safe
    def BUILD_BLOCK(self, space, bytecode, frame, pc, n_cells):
        cells = [frame.pop() for _ in range(n_cells)]
        w_code = frame.pop()
        assert isinstance(w_code, W_CodeObject)
        block = W_BlockObject(
            w_code, frame.w_self, frame.w_scope, cells, frame.block, self
        )
        frame.push(block)

    def BUILD_CLASS(self, space, bytecode, frame, pc):
        superclass = frame.pop()
        w_name = frame.pop()
        w_scope = frame.pop()

        name = space.symbol_w(w_name)
        w_cls = w_scope.find_local_const(self, name)
        if w_cls is None:
            if superclass is space.w_nil:
                superclass = space.w_object
            assert isinstance(superclass, W_ClassObject)
            w_cls = space.newclass(name, superclass)
            space.set_const(w_scope, name, w_cls)
            space.set_lexical_scope(w_cls, w_scope)

        frame.push(w_cls)

    def BUILD_MODULE(self, space, bytecode, frame, pc):
        w_bytecode = frame.pop()
        w_name = frame.pop()
        w_scope = frame.pop()

        name = space.symbol_w(w_name)
        w_mod = w_scope.find_const(space, name)
        if w_mod is None:
            w_mod = space.newmodule(name)
            space.set_const(w_scope, name, w_mod)
            space.set_lexical_scope(w_mod, w_scope)

        assert isinstance(w_bytecode, W_CodeObject)
        sub_frame = space.create_frame(w_bytecode, w_mod, w_mod)
        with space.getexecutioncontext().visit_frame(sub_frame):
            space.execute_frame(sub_frame, w_bytecode)

        frame.push(space.w_nil)

    def BUILD_REGEXP(self, space, bytecode, frame, pc):
        w_string = frame.pop()
        frame.push(space.newregexp(space.str_w(w_string)))

    def COPY_STRING(self, space, bytecode, frame, pc):
        w_s = frame.pop()
        assert isinstance(w_s, W_StringObject)
        frame.push(w_s.copy(space))

    def COERCE_ARRAY(self, space, bytecode, frame, pc):
        w_obj = frame.pop()
        if w_obj is space.w_nil:
            frame.push(space.newarray([]))
        elif isinstance(w_obj, W_ArrayObject):
            frame.push(w_obj)
        else:
            if space.respond_to(w_obj, space.newsymbol("to_a")):
                w_obj = space.send(w_obj, space.newsymbol("to_a"))
            elif space.respond_to(w_obj, space.newsymbol("to_ary")):
                w_obj = space.send(w_obj, space.newsymbol("to_ary"))
            if not isinstance(w_obj, W_ArrayObject):
                w_obj = space.newarray([w_obj])
            frame.push(w_obj)

    def COERCE_BLOCK(self, space, bytecode, frame, pc):
        w_block = frame.pop()
        if w_block is space.w_nil:
            frame.push(w_block)
        elif isinstance(w_block, W_ProcObject):
            frame.push(w_block.block)
        elif space.respond_to(w_block, space.newsymbol("to_proc")):
            # Proc implements to_proc, too, but MRI doesn't call it
            w_res = space.convert_type(w_block, space.w_proc, "to_proc")
            assert isinstance(w_res, W_ProcObject)
            frame.push(w_res.block)
        else:
            raise space.error(space.w_TypeError, "wrong argument type")

    @jit.unroll_safe
    def UNPACK_SEQUENCE(self, space, bytecode, frame, pc, n_items):
        w_obj = frame.pop()
        items_w = space.listview(w_obj)
        for i in xrange(n_items - 1, -1, -1):
            try:
                w_obj = items_w[i]
            except IndexError:
                w_obj = space.w_nil
            frame.push(w_obj)

    @jit.unroll_safe
    def UNPACK_SEQUENCE_SPLAT(self, space, bytecode, frame, pc, n_targets, n_pre):
        w_obj = frame.pop()
        items_w = space.listview(w_obj)
        n_items = len(items_w)
        n_post = n_targets - n_pre - 1
        n_splat = max(n_items - n_pre - n_post, 0)
        for i in xrange(n_items, n_pre + n_splat + n_post, 1):
            items_w.append(space.w_nil)

        for i in xrange(n_pre + n_splat + n_post - 1, n_pre + n_splat - 1, -1):
            frame.push(items_w[i])
        splat_array = [items_w[i] for i in xrange(n_pre, n_pre + n_splat, 1)]
        frame.push(space.newarray(splat_array))
        for i in xrange(n_pre - 1, -1, -1):
            frame.push(items_w[i])

    def DEFINE_FUNCTION(self, space, bytecode, frame, pc):
        w_func = frame.pop()
        w_name = frame.pop()
        w_scope = frame.pop()
        assert isinstance(w_func, W_FunctionObject)
        w_scope.define_method(space, space.symbol_w(w_name), w_func)
        frame.push(space.w_nil)

    def ATTACH_FUNCTION(self, space, bytecode, frame, pc):
        w_func = frame.pop()
        w_name = frame.pop()
        w_obj = frame.pop()
        assert isinstance(w_func, W_FunctionObject)
        w_obj.attach_method(space, space.symbol_w(w_name), w_func)
        frame.push(space.w_nil)

    def EVALUATE_CLASS(self, space, bytecode, frame, pc):
        w_bytecode = frame.pop()
        w_cls = frame.pop()
        assert isinstance(w_bytecode, W_CodeObject)
        sub_frame = space.create_frame(w_bytecode, w_cls, w_cls)
        with space.getexecutioncontext().visit_frame(sub_frame):
            space.execute_frame(sub_frame, w_bytecode)

        frame.push(space.w_nil)

    @jit.unroll_safe
    def SEND(self, space, bytecode, frame, pc, meth_idx, num_args):
        args_w = frame.popitemsreverse(num_args)
        w_receiver = frame.pop()
        w_res = space.send(w_receiver, bytecode.consts_w[meth_idx], args_w)
        frame.push(w_res)

    @jit.unroll_safe
    def SEND_BLOCK(self, space, bytecode, frame, pc, meth_idx, num_args):
        w_block = frame.pop()
        args_w = frame.popitemsreverse(num_args - 1)
        w_receiver = frame.pop()
        if w_block is space.w_nil:
            w_block = None
        else:
            assert isinstance(w_block, W_BlockObject)
        w_res = space.send(w_receiver, bytecode.consts_w[meth_idx], args_w, block=w_block)
        frame.push(w_res)

    def SEND_SPLAT(self, space, bytecode, frame, pc, meth_idx):
        args_w = space.listview(frame.pop())
        w_receiver = frame.pop()
        w_res = space.send(w_receiver, bytecode.consts_w[meth_idx], args_w)
        frame.push(w_res)

    def SEND_BLOCK_SPLAT(self, space, bytecode, frame, pc, meth_idx):
        w_block = frame.pop()
        args_w = space.listview(frame.pop())
        w_receiver = frame.pop()
        assert isinstance(w_block, W_BlockObject)
        w_res = space.send(w_receiver, bytecode.consts_w[meth_idx], args_w, block=w_block)
        frame.push(w_res)

    def DEFINED_METHOD(self, space, bytecode, frame, pc, meth_idx):
        w_obj = frame.pop()
        if space.respond_to(w_obj, bytecode.consts_w[meth_idx]):
            frame.push(space.newstr_fromstr("method"))
        else:
            frame.push(space.w_nil)

    def SEND_SUPER(self, space, bytecode, frame, pc, meth_idx, num_args):
        args_w = frame.popitemsreverse(num_args)
        w_receiver = frame.pop()
        w_res = space.send_super(frame.w_scope, w_receiver, bytecode.consts_w[meth_idx], args_w)
        frame.push(w_res)

    def SEND_SUPER_SPLAT(self, space, bytecode, frame, pc, meth_idx):
        args_w = space.listview(frame.pop())
        w_receiver = frame.pop()
        w_res = space.send_super(frame.w_scope, w_receiver, bytecode.consts_w[meth_idx], args_w)
        frame.push(w_res)

    def SETUP_LOOP(self, space, bytecode, frame, pc, target_pc):
        frame.lastblock = LoopBlock(target_pc, frame.lastblock, frame.stackpos)

    def SETUP_EXCEPT(self, space, bytecode, frame, pc, target_pc):
        frame.lastblock = ExceptBlock(target_pc, frame.lastblock, frame.stackpos)

    def SETUP_FINALLY(self, space, bytecode, frame, pc, target_pc):
        frame.lastblock = FinallyBlock(target_pc, frame.lastblock, frame.stackpos)

    def END_FINALLY(self, space, bytecode, frame, pc):
        frame.pop()
        unroller = frame.pop()
        if isinstance(unroller, SuspendedUnroller):
            block = frame.unrollstack(unroller.kind)
            if block is None:
                unroller.nomoreblocks()
            else:
                return block.handle(space, frame, unroller)
        return pc

    def COMPARE_EXC(self, space, bytecode, frame, pc):
        w_expected = frame.pop()
        w_actual = frame.peek()
        frame.push(space.newbool(w_expected is space.getclass(w_actual)))

    def POP_BLOCK(self, space, bytecode, frame, pc):
        block = frame.popblock()
        block.cleanup(space, frame)

    def JUMP(self, space, bytecode, frame, pc, target_pc):
        return self.jump(space, bytecode, frame, pc, target_pc)

    def JUMP_IF_TRUE(self, space, bytecode, frame, pc, target_pc):
        if space.is_true(frame.pop()):
            return self.jump(space, bytecode, frame, pc, target_pc)
        else:
            return pc

    def JUMP_IF_FALSE(self, space, bytecode, frame, pc, target_pc):
        if space.is_true(frame.pop()):
            return pc
        else:
            return self.jump(space, bytecode, frame, pc, target_pc)

    def DISCARD_TOP(self, space, bytecode, frame, pc):
        frame.pop()

    def DUP_TOP(self, space, bytecode, frame, pc):
        frame.push(frame.peek())

    def DUP_TWO(self, space, bytecode, frame, pc):
        w_1 = frame.pop()
        w_2 = frame.pop()
        frame.push(w_2)
        frame.push(w_1)
        frame.push(w_2)
        frame.push(w_1)

    def ROT_TWO(self, space, bytecode, frame, pc):
        w_1 = frame.pop()
        w_2 = frame.pop()
        frame.push(w_1)
        frame.push(w_2)

    def ROT_THREE(self, space, bytecode, frame, pc):
        w_1 = frame.pop()
        w_2 = frame.pop()
        w_3 = frame.pop()
        frame.push(w_1)
        frame.push(w_3)
        frame.push(w_2)

    def RETURN(self, space, bytecode, frame, pc):
        w_returnvalue = frame.pop()
        block = frame.unrollstack(ReturnValue.kind)
        if block is None:
            raise Return(w_returnvalue)
        unroller = ReturnValue(w_returnvalue)
        return block.handle(space, frame, unroller)

    def RAISE_RETURN(self, space, bytecode, frame, pc):
        w_returnvalue = frame.pop()
        block = frame.unrollstack(RaiseReturnValue.kind)
        if block is None:
            raise RaiseReturn(frame.parent_interp, w_returnvalue)
        unroller = RaiseReturnValue(frame.parent_interp, w_returnvalue)
        return block.handle(space, frame, unroller)

    @jit.unroll_safe
    def YIELD(self, space, bytecode, frame, pc, n_args):
        args_w = [None] * n_args
        for i in xrange(n_args - 1, -1, -1):
            args_w[i] = frame.pop()
        w_res = space.invoke_block(frame.block, args_w)
        frame.push(w_res)

    def CONTINUE_LOOP(self, space, bytecode, frame, pc, target_pc):
        frame.pop()
        return frame.unrollstack_and_jump(space, ContinueLoop(target_pc))

    def BREAK_LOOP(self, space, bytecode, frame, pc):
        w_obj = frame.pop()
        return frame.unrollstack_and_jump(space, BreakLoop(w_obj))

    def UNREACHABLE(self, space, bytecode, frame, pc):
        raise Exception
예제 #7
0
파일: dot.py 프로젝트: nipengadmaster/pypy
from pypy.module.micronumpy.strides import calculate_dot_strides
from pypy.interpreter.error import OperationError
from pypy.module.micronumpy.interp_iter import ViewIterator
from pypy.rlib import jit

def dot_printable_location(shapelen):
    return 'numpy dot [%d]' % shapelen

dot_driver = jit.JitDriver(
    greens=['shapelen'],
    reds=['lefti', 'righti', 'outi', 'result', 'right', 'dtype',
          'left'],
    get_printable_location=dot_printable_location,
    name='dot',
)

def match_dot_shapes(space, left, right):
    my_critical_dim_size = left.shape[-1]
    right_critical_dim_size = right.shape[0]
    right_critical_dim = 0
    out_shape = []
    if len(right.shape) > 1:
        right_critical_dim = len(right.shape) - 2
        right_critical_dim_size = right.shape[right_critical_dim]
        assert right_critical_dim >= 0
        out_shape += left.shape[:-1] + \
                     right.shape[0:right_critical_dim] + \
                     right.shape[right_critical_dim + 1:]
    elif len(right.shape) > 0:
        #dot does not reduce for scalars
        out_shape += left.shape[:-1]