Exemplo n.º 1
0
def interpret(code_obj=None, args=[], self_obj=None, frame=None):

    if frame is None:
        assert code_obj is not None
        frame = Frame(code_obj, args, self_obj or code_obj)

    while True:
        jitdriver.jit_merge_point(bc=frame.bc,
                                  ip=frame.ip,
                                  sp=frame.sp,
                                  base_code=frame.base_code,
                                  frame=frame,
                                  is_continuation=frame._is_continuation)
        inst = frame.get_inst()
        #print code.BYTECODES[inst], frame.consts, frame.ip

        if inst == code.LOAD_CONST:
            arg = frame.get_inst()
            #print "load const", arg
            frame.push_const(arg)
            continue

        if inst == code.INVOKE:
            debug_ip = frame.ip
            argc = frame.get_inst()
            fn = frame.nth(argc - 1)

            #assert isinstance(fn, code.BaseCode), "Expected callable, got " + str(fn)

            args = frame.pop_n(argc - 1)
            frame.pop()
            try:
                frame.push(fn.invoke(args))
                continue
            except WrappedException as ex:
                dp = frame.debug_points.get(debug_ip - 1, None)
                if dp:
                    ex._ex._trace.append(dp)
                raise

            continue

        # if inst == code.TAIL_CALL:
        #     argc = frame.get_inst()
        #     fn = frame.nth(argc - 1)
        #
        #     assert isinstance(fn, code.BaseCode)
        #
        #     args = frame.pop_n(argc - 1)
        #     frame.pop()
        #
        #     return code.TailCall(fn, args)

        if inst == code.ARG:
            arg = frame.get_inst()
            frame.push_arg(arg)

            continue

        if inst == code.RETURN:
            val = frame.pop()
            frame.finished = True
            return val

        if inst == code.COND_BR:
            v = frame.pop()
            loc = frame.get_inst()
            if v is not nil and v is not false:
                continue
            frame.jump_rel(loc)
            continue

        if inst == code.JMP:
            ip = frame.get_inst()
            frame.jump_rel(ip)
            continue

        if inst == code.EQ:
            a = frame.pop()
            b = frame.pop()
            frame.push(numbers.eq(a, b))
            continue

        if inst == code.MAKE_CLOSURE:
            argc = frame.get_inst()

            lst = [None] * argc

            for idx in range(argc - 1, -1, -1):
                lst[idx] = frame.pop()

            cobj = frame.pop()
            closure = code.Closure(cobj, lst)
            frame.push(closure)

            continue

        if inst == code.CLOSED_OVER:
            assert isinstance(frame.code_obj, code.Closure)
            idx = frame.get_inst()
            frame.push_closed_over(idx)
            continue

        if inst == code.SET_VAR:
            val = frame.pop()
            var = frame.pop()

            affirm(isinstance(var, code.Var),
                   u"Can't set the value of a non-var")
            var.set_root(val)
            frame.push(var)
            continue

        if inst == code.POP:
            frame.pop()
            continue

        if inst == code.DEREF_VAR:
            debug_ip = frame.ip
            var = frame.pop()
            if not isinstance(var, code.Var):
                tp = var.type()
                assert isinstance(tp, Type)
                affirm(False, u"Can't deref " + tp._name)
            try:
                frame.push(var.deref())
                continue
            except WrappedException as ex:
                dp = frame.debug_points.get(debug_ip - 1, None)
                if dp:
                    ex._ex._trace.append(dp)
                raise

        if inst == code.PUSH_SELF:
            frame.push(frame.self_obj)
            continue

        if inst == code.DUP_NTH:
            n = frame.nth(frame.get_inst())
            frame.push(n)
            continue

        if inst == code.POP_UP_N:
            val = frame.pop()
            num = frame.get_inst()
            frame.pop_n(num)
            frame.push(val)
            continue

        if inst == code.LOOP_RECUR:
            argc = frame.get_inst()
            stack_depth = frame.get_inst()
            ip = frame.get_inst()

            args = frame.pop_n(argc)
            frame.pop_n(stack_depth)
            frame.pop_n(argc)
            frame.push_n(args, argc)
            frame.ip = ip

            jitdriver.can_enter_jit(bc=frame.bc,
                                    ip=frame.ip,
                                    sp=frame.sp,
                                    base_code=frame.base_code,
                                    frame=frame,
                                    is_continuation=frame._is_continuation)
            continue

        if inst == code.MAKE_MULTI_ARITY:
            frame.push(make_multi_arity(frame, frame.get_inst()))

            continue

        if inst == code.MAKE_VARIADIC:
            code_object = frame.pop()
            required_arity = frame.get_inst()
            frame.push(code.VariadicCode(code_object, required_arity))

            continue

        if inst == code.YIELD:
            if not frame.is_continuation():
                frame.set_continuation()
                #frame = jit.hint(frame, force_virtualizable=True)
                k = ShallowContinuation(frame, frame.pop())
                return k
            else:
                return frame.pop()

        if inst == code.PUSH_NS:
            from pixie.vm.compiler import NS_VAR
            frame.push(NS_VAR.deref())
            continue

        affirm(False, u"NO DISPATCH FOR: " + unicode(code.BYTECODES[inst]))
        raise Exception()
Exemplo n.º 2
0
def interpret(code_obj=None, args=[], self_obj = None, frame=None):

    if frame is None:
        assert code_obj is not None
        frame = Frame(code_obj, args, self_obj or code_obj)



    while True:
        jitdriver.jit_merge_point(bc=frame.bc,
                                  ip=frame.ip,
                                  sp=frame.sp,
                                  base_code=frame.base_code,
                                  frame=frame,
                                  is_continuation=frame._is_continuation)
        inst = frame.get_inst()

        if inst == code.LOAD_CONST:
            arg = frame.get_inst()
            frame.push_const(arg)
            continue

        if inst == code.INVOKE:
            debug_ip = frame.ip
            argc = frame.get_inst()
            fn = frame.nth(argc - 1)

            #assert isinstance(fn, code.BaseCode), "Expected callable, got " + str(fn)



            args = frame.pop_n(argc - 1)
            frame.pop()
            try:
                frame.push(fn.invoke(args))
                continue
            except WrappedException as ex:
                dp = frame.debug_points.get(debug_ip - 1, None)
                if dp:
                    ex._ex._trace.append(dp)
                raise

            continue

        # if inst == code.TAIL_CALL:
        #     argc = frame.get_inst()
        #     fn = frame.nth(argc - 1)
        #
        #     assert isinstance(fn, code.BaseCode)
        #
        #     args = frame.pop_n(argc - 1)
        #     frame.pop()
        #
        #     return code.TailCall(fn, args)

        if inst == code.ARG:
            arg = frame.get_inst()
            frame.push_arg(arg)

            continue

        if inst == code.RETURN:
            val = frame.pop()
            frame.finished = True
            return val

        if inst == code.COND_BR:
            v = frame.pop()
            loc = frame.get_inst()
            if v is not nil and v is not false:
                continue
            frame.jump_rel(loc)
            continue

        if inst == code.JMP:
            ip = frame.get_inst()
            frame.jump_rel(ip)
            continue

        if inst == code.EQ:
            a = frame.pop()
            b = frame.pop()
            frame.push(numbers.eq(a, b))
            continue

        if inst == code.MAKE_CLOSURE:
            argc = frame.get_inst()

            lst = [None] * argc

            for idx in range(argc - 1, -1, -1):
                lst[idx] = frame.pop()

            cobj = frame.pop()
            closure = code.Closure(cobj, lst)
            frame.push(closure)

            continue

        if inst == code.CLOSED_OVER:
            assert isinstance(frame.code_obj, code.Closure)
            idx = frame.get_inst()
            frame.push_closed_over(idx)
            continue

        if inst == code.SET_VAR:
            val = frame.pop()
            var = frame.pop()

            affirm(isinstance(var, code.Var), u"Can't set the value of a non-var")
            var.set_root(val)
            frame.push(var)
            continue

        if inst == code.POP:
            frame.pop()
            continue

        if inst == code.DEREF_VAR:
            debug_ip = frame.ip
            var = frame.pop()
            if not isinstance(var, code.Var):
                affirm(False, u"Can't deref " + var.type()._name)
            try:
                frame.push(var.deref())
                continue
            except WrappedException as ex:
                dp = frame.debug_points.get(debug_ip - 1, None)
                if dp:
                    ex._ex._trace.append(dp)
                raise


        if inst == code.RECUR:
            argc = frame.get_inst()
            args = frame.pop_n(argc)

            frame = Frame(frame.code_obj, args, frame.self_obj)

            jitdriver.can_enter_jit(bc=frame.bc,
                                  ip=frame.ip,
                                  sp=frame.sp,
                                  base_code=frame.base_code,
                                  frame=frame,
                                  is_continuation=frame._is_continuation)
            continue

        if inst == code.PUSH_SELF:
            frame.push(frame.self_obj)
            continue

        if inst == code.DUP_NTH:
            n = frame.nth(frame.get_inst())
            frame.push(n)
            continue

        if inst == code.POP_UP_N:
            val = frame.pop()
            num = frame.get_inst()
            frame.pop_n(num)
            frame.push(val)
            continue

        if inst == code.LOOP_RECUR:
            argc = frame.get_inst()
            stack_depth = frame.get_inst()
            ip = frame.get_inst()

            args = frame.pop_n(argc)
            frame.pop_n(stack_depth)
            frame.pop_n(argc)
            frame.push_n(args, argc)
            frame.ip = ip


            jitdriver.can_enter_jit(bc=frame.bc,
                                  ip=frame.ip,
                                  sp=frame.sp,
                                  base_code=frame.base_code,
                                  frame=frame,
                                  is_continuation=frame._is_continuation)
            continue

        if inst == code.MAKE_MULTI_ARITY:
            frame.push(make_multi_arity(frame, frame.get_inst()))

            continue

        if inst == code.MAKE_VARIADIC:
            code_object = frame.pop()
            required_arity = frame.get_inst()
            frame.push(code.VariadicCode(code_object, required_arity))

            continue

        if inst == code.YIELD:
            if not frame.is_continuation():
                frame.set_continuation()
                #frame = jit.hint(frame, force_virtualizable=True)
                k = ShallowContinuation(frame, frame.pop())
                return k
            else:
                return frame.pop()

        if inst == code.PUSH_NS:
            from pixie.vm.compiler import NS_VAR
            frame.push(NS_VAR.deref())
            continue


        affirm(False, u"NO DISPATCH FOR: " + unicode(code.BYTECODES[inst]))
        raise Exception()
Exemplo n.º 3
0
def interpret(code_obj, args=[]):
    frame = Frame(code_obj, args)
    while True:
        jitdriver.jit_merge_point(bc=frame.bc,
                                  ip=frame.ip,
                                  sp=frame.sp,
                                  base_code=frame.base_code,
                                  frame=frame)
        inst = frame.get_inst()

        #print code.BYTECODES[inst]

        if inst == code.LOAD_CONST:
            arg = frame.get_inst()
            frame.push_const(arg)
            continue

        if inst == code.INVOKE:
            argc = frame.get_inst()
            fn = frame.nth(argc - 1)

            assert isinstance(fn, code.BaseCode), "Expected callable, got " + str(fn)

            args = frame.pop_n(argc - 1)
            frame.pop()

            frame.push(fn.invoke(args))

            continue

        # if inst == code.TAIL_CALL:
        #     argc = frame.get_inst()
        #     fn = frame.nth(argc - 1)
        #
        #     assert isinstance(fn, code.BaseCode)
        #
        #     args = frame.pop_n(argc - 1)
        #     frame.pop()
        #
        #     return code.TailCall(fn, args)

        if inst == code.ARG:
            arg = frame.get_inst()
            frame.push_arg(arg)

            continue

        if inst == code.RETURN:
            val = frame.pop()

            return val

        if inst == code.COND_BR:
            v = frame.pop()
            loc = frame.get_inst()
            if v is not nil and v is not false:
                continue
            frame.jump_rel(loc)
            continue

        if inst == code.JMP:
            ip = frame.get_inst()
            frame.jump_rel(ip)
            continue

        if inst == code.EQ:
            a = frame.pop()
            b = frame.pop()
            frame.push(numbers.eq(a, b))
            continue

        if inst == code.MAKE_CLOSURE:
            argc = frame.get_inst()

            lst = [None] * argc

            for idx in range(argc - 1, -1, -1):
                lst[idx] = frame.pop()

            cobj = frame.pop()
            closure = code.Closure(cobj, lst)
            frame.push(closure)

            continue

        if inst == code.CLOSED_OVER:
            assert isinstance(frame.code_obj, code.Closure)
            idx = frame.get_inst()
            frame.push_closed_over(idx)
            continue

        if inst == code.SET_VAR:
            val = frame.pop()
            var = frame.pop()

            affirm(isinstance(var, code.Var), u"Can't set the value of a non-var")
            var.set_root(val)
            frame.push(var)
            continue

        if inst == code.POP:
            frame.pop()
            continue

        if inst == code.DEREF_VAR:
            var = frame.pop()
            if not isinstance(var, code.Var):
                affirm(False, u"Can't deref " + var.type()._name)
            frame.push(var.deref())
            continue

        if inst == code.RECUR:
            argc = frame.get_inst()
            args = frame.pop_n(argc)

            frame = Frame(frame.code_obj, args)

            jitdriver.can_enter_jit(bc=frame.bc,
                                  ip=frame.ip,
                                  sp=frame.sp,
                                  base_code=frame.base_code,
                                  frame=frame)
            continue

        if inst == code.PUSH_SELF:
            frame.push(frame.code_obj)
            continue

        if inst == code.DUP_NTH:
            n = frame.nth(frame.get_inst())
            frame.push(n)
            continue

        if inst == code.POP_UP_N:
            val = frame.pop()
            num = frame.get_inst()
            frame.pop_n(num)
            frame.push(val)
            continue

        if inst == code.LOOP_RECUR:
            argc = frame.get_inst()
            stack_depth = frame.get_inst()
            ip = frame.get_inst()

            args = frame.pop_n(argc)
            frame.pop_n(stack_depth)
            frame.pop_n(argc)
            frame.push_n(args, argc)
            frame.ip = ip


            jitdriver.can_enter_jit(bc=frame.bc,
                                  ip=frame.ip,
                                  sp=frame.sp,
                                  base_code=frame.base_code,
                                  frame=frame)
            continue

        if inst == code.MAKE_MULTI_ARITY:
            frame.push(make_multi_arity(frame, frame.get_inst()))

            continue

        if inst == code.MAKE_VARIADIC:
            code_object = frame.pop()
            required_arity = frame.get_inst()
            frame.push(code.VariadicCode(code_object, required_arity))

            continue


        print "NO DISPATCH FOR: " + code.BYTECODES[inst]
        raise Exception()