Пример #1
0
def test_specialize_value():
    assert specialize_value(lltype.Char, 0x41) == '\x41'
    if longlong.supports_longlong:
        import sys
        value = longlong.r_float_storage(sys.maxint*17)
        assert specialize_value(lltype.SignedLongLong, value) == sys.maxint*17
    sfval = r_singlefloat(42.5)
    ival = longlong.singlefloat2int(sfval)
    assert specialize_value(rffi.FLOAT, ival) == sfval
Пример #2
0
def test_specialize_value():
    assert specialize_value(lltype.Char, 0x41) == '\x41'
    if longlong.supports_longlong:
        import sys
        value = longlong.r_float_storage(sys.maxint*17)
        assert specialize_value(lltype.SignedLongLong, value) == sys.maxint*17
    sfval = r_singlefloat(42.5)
    ival = longlong.singlefloat2int(sfval)
    assert specialize_value(rffi.FLOAT, ival) == sfval
Пример #3
0
 def ll_portal_runner(*args):
     start = True
     while 1:
         try:
             # maybe enter from the function's start.  Note that the
             # 'start' variable is constant-folded away because it's
             # the first statement in the loop.
             if start:
                 maybe_compile_and_run(
                     state.increment_function_threshold, *args)
             #
             # then run the normal portal function, i.e. the
             # interpreter's main loop.  It might enter the jit
             # via maybe_enter_jit(), which typically ends with
             # handle_fail() being called, which raises on the
             # following exceptions --- catched here, because we
             # want to interrupt the whole interpreter loop.
             return support.maybe_on_top_of_llinterp(rtyper,
                                               portal_ptr)(*args)
         except jitexc.ContinueRunningNormally, e:
             args = ()
             for ARGTYPE, attrname, count in portalfunc_ARGS:
                 x = getattr(e, attrname)[count]
                 x = specialize_value(ARGTYPE, x)
                 args = args + (x,)
             start = False
             continue
         except jitexc.DoneWithThisFrameVoid:
             assert result_kind == 'void'
             return
Пример #4
0
 def ll_portal_runner(*args):
     start = True
     while 1:
         try:
             # maybe enter from the function's start.  Note that the
             # 'start' variable is constant-folded away because it's
             # the first statement in the loop.
             if start:
                 maybe_compile_and_run(
                     state.increment_function_threshold, *args)
             #
             # then run the normal portal function, i.e. the
             # interpreter's main loop.  It might enter the jit
             # via maybe_enter_jit(), which typically ends with
             # handle_fail() being called, which raises on the
             # following exceptions --- catched here, because we
             # want to interrupt the whole interpreter loop.
             return support.maybe_on_top_of_llinterp(
                 rtyper, portal_ptr)(*args)
         except jitexc.ContinueRunningNormally, e:
             args = ()
             for ARGTYPE, attrname, count in portalfunc_ARGS:
                 x = getattr(e, attrname)[count]
                 x = specialize_value(ARGTYPE, x)
                 args = args + (x, )
             start = False
             continue
         except jitexc.DoneWithThisFrameVoid:
             assert result_kind == 'void'
             return
Пример #5
0
 def handle_jitexception(e):
     # XXX the bulk of this function is mostly a copy-paste from above
     try:
         raise e
     except jitexc.ContinueRunningNormally, e:
         args = ()
         for ARGTYPE, attrname, count in portalfunc_ARGS:
             x = getattr(e, attrname)[count]
             x = specialize_value(ARGTYPE, x)
             args = args + (x,)
         result = ll_portal_runner(*args)
         if result_kind != 'void':
             result = unspecialize_value(result)
         return result
Пример #6
0
 def handle_jitexception(e):
     # XXX the bulk of this function is mostly a copy-paste from above
     try:
         raise e
     except jitexc.ContinueRunningNormally, e:
         args = ()
         for ARGTYPE, attrname, count in portalfunc_ARGS:
             x = getattr(e, attrname)[count]
             x = specialize_value(ARGTYPE, x)
             args = args + (x, )
         result = ll_portal_runner(*args)
         if result_kind != 'void':
             result = unspecialize_value(result)
         return result
Пример #7
0
 def handle_jitexception(e):
     # XXX there are too many exceptions all around...
     while True:
         if isinstance(e, EnterJitAssembler):
             try:
                 return e.execute()
             except jitexc.JitException as e:
                 continue
         #
         if isinstance(e, jitexc.ContinueRunningNormally):
             args = ()
             for ARGTYPE, attrname, count in portalfunc_ARGS:
                 x = getattr(e, attrname)[count]
                 x = specialize_value(ARGTYPE, x)
                 args = args + (x, )
             try:
                 result = support.maybe_on_top_of_llinterp(
                     rtyper, portal_ptr)(*args)
             except jitexc.JitException as e:
                 continue
             if result_kind != 'void':
                 result = unspecialize_value(result)
             return result
         #
         if result_kind == 'void':
             if isinstance(e, jitexc.DoneWithThisFrameVoid):
                 return None
         if result_kind == 'int':
             if isinstance(e, jitexc.DoneWithThisFrameInt):
                 return e.result
         if result_kind == 'ref':
             if isinstance(e, jitexc.DoneWithThisFrameRef):
                 return e.result
         if result_kind == 'float':
             if isinstance(e, jitexc.DoneWithThisFrameFloat):
                 return e.result
         #
         if isinstance(e, jitexc.ExitFrameWithExceptionRef):
             value = ts.cast_to_baseclass(e.value)
             if not we_are_translated():
                 raise LLException(ts.get_typeptr(value), value)
             else:
                 value = cast_base_ptr_to_instance(Exception, value)
                 assert value is not None
                 raise value
         #
         raise AssertionError("all cases should have been handled")
Пример #8
0
 def handle_jitexception(e):
     # XXX there are too many exceptions all around...
     while True:
         if isinstance(e, EnterJitAssembler):
             try:
                 return e.execute()
             except jitexc.JitException as e:
                 continue
         #
         if isinstance(e, jitexc.ContinueRunningNormally):
             args = ()
             for ARGTYPE, attrname, count in portalfunc_ARGS:
                 x = getattr(e, attrname)[count]
                 x = specialize_value(ARGTYPE, x)
                 args = args + (x,)
             try:
                 result = support.maybe_on_top_of_llinterp(rtyper,
                                                     portal_ptr)(*args)
             except jitexc.JitException as e:
                 continue
             if result_kind != 'void':
                 result = unspecialize_value(result)
             return result
         #
         if result_kind == 'void':
             if isinstance(e, jitexc.DoneWithThisFrameVoid):
                 return None
         if result_kind == 'int':
             if isinstance(e, jitexc.DoneWithThisFrameInt):
                 return e.result
         if result_kind == 'ref':
             if isinstance(e, jitexc.DoneWithThisFrameRef):
                 return e.result
         if result_kind == 'float':
             if isinstance(e, jitexc.DoneWithThisFrameFloat):
                 return e.result
         #
         if isinstance(e, jitexc.ExitFrameWithExceptionRef):
             value = ts.cast_to_baseclass(e.value)
             if not we_are_translated():
                 raise LLException(ts.get_typeptr(value), value)
             else:
                 value = cast_base_ptr_to_instance(Exception, value)
                 assert value is not None
                 raise value
         #
         raise AssertionError("all cases should have been handled")
Пример #9
0
 def ll_portal_runner(*args):
     try:
         # maybe enter from the function's start.
         maybe_compile_and_run(state.increment_function_threshold,
                               *args)
         #
         # then run the normal portal function, i.e. the
         # interpreter's main loop.  It might enter the jit
         # via maybe_enter_jit(), which typically ends with
         # handle_fail() being called, which raises on the
         # following exceptions --- catched here, because we
         # want to interrupt the whole interpreter loop.
         return support.maybe_on_top_of_llinterp(rtyper,
                                                 portal_ptr)(*args)
     except jitexc.JitException as e:
         result = handle_jitexception(e)
         if result_kind != 'void':
             result = specialize_value(RESULT, result)
         return result
Пример #10
0
 def ll_portal_runner(*args):
     try:
         # maybe enter from the function's start.
         maybe_compile_and_run(
             state.increment_function_threshold, *args)
         #
         # then run the normal portal function, i.e. the
         # interpreter's main loop.  It might enter the jit
         # via maybe_enter_jit(), which typically ends with
         # handle_fail() being called, which raises on the
         # following exceptions --- catched here, because we
         # want to interrupt the whole interpreter loop.
         return support.maybe_on_top_of_llinterp(rtyper,
                                           portal_ptr)(*args)
     except jitexc.JitException as e:
         result = handle_jitexception(e)
         if result_kind != 'void':
             result = specialize_value(RESULT, result)
         return result
Пример #11
0
    def rewrite_jit_merge_point(self, jd, policy):
        #
        # Mutate the original portal graph from this:
        #
        #       def original_portal(..):
        #           stuff
        #           while 1:
        #               jit_merge_point(*args)
        #               more stuff
        #
        # to that:
        #
        #       def original_portal(..):
        #           stuff
        #           return portal_runner(*args)
        #
        #       def portal_runner(*args):
        #           while 1:
        #               try:
        #                   return portal(*args)
        #               except ContinueRunningNormally, e:
        #                   *args = *e.new_args
        #               except DoneWithThisFrame, e:
        #                   return e.return
        #               except ExitFrameWithException, e:
        #                   raise Exception, e.value
        #
        #       def portal(*args):
        #           while 1:
        #               more stuff
        #
        origportalgraph = jd._jit_merge_point_in
        portalgraph = jd.portal_graph
        PORTALFUNC = jd._PORTAL_FUNCTYPE

        # ____________________________________________________________
        # Prepare the portal_runner() helper
        #
        from rpython.jit.metainterp.warmstate import specialize_value
        from rpython.jit.metainterp.warmstate import unspecialize_value
        portal_ptr = self.cpu.ts.functionptr(PORTALFUNC, 'portal',
                                         graph=portalgraph)
        jd._portal_ptr = portal_ptr
        #
        portalfunc_ARGS = []
        nums = {}
        for i, ARG in enumerate(PORTALFUNC.ARGS):
            kind = history.getkind(ARG)
            assert kind != 'void'
            if i < len(jd.jitdriver.greens):
                color = 'green'
            else:
                color = 'red'
            attrname = '%s_%s' % (color, kind)
            count = nums.get(attrname, 0)
            nums[attrname] = count + 1
            portalfunc_ARGS.append((ARG, attrname, count))
        portalfunc_ARGS = unrolling_iterable(portalfunc_ARGS)
        #
        rtyper = self.translator.rtyper
        RESULT = PORTALFUNC.RESULT
        result_kind = history.getkind(RESULT)
        ts = self.cpu.ts
        state = jd.warmstate
        maybe_compile_and_run = jd._maybe_compile_and_run_fn

        def ll_portal_runner(*args):
            start = True
            while 1:
                try:
                    # maybe enter from the function's start.  Note that the
                    # 'start' variable is constant-folded away because it's
                    # the first statement in the loop.
                    if start:
                        maybe_compile_and_run(
                            state.increment_function_threshold, *args)
                    #
                    # then run the normal portal function, i.e. the
                    # interpreter's main loop.  It might enter the jit
                    # via maybe_enter_jit(), which typically ends with
                    # handle_fail() being called, which raises on the
                    # following exceptions --- catched here, because we
                    # want to interrupt the whole interpreter loop.
                    return support.maybe_on_top_of_llinterp(rtyper,
                                                      portal_ptr)(*args)
                except jitexc.ContinueRunningNormally, e:
                    args = ()
                    for ARGTYPE, attrname, count in portalfunc_ARGS:
                        x = getattr(e, attrname)[count]
                        x = specialize_value(ARGTYPE, x)
                        args = args + (x,)
                    start = False
                    continue
                except jitexc.DoneWithThisFrameVoid:
                    assert result_kind == 'void'
                    return
                except jitexc.DoneWithThisFrameInt, e:
                    assert result_kind == 'int'
                    return specialize_value(RESULT, e.result)
Пример #12
0
                    args = ()
                    for ARGTYPE, attrname, count in portalfunc_ARGS:
                        x = getattr(e, attrname)[count]
                        x = specialize_value(ARGTYPE, x)
                        args = args + (x,)
                    start = False
                    continue
                except jitexc.DoneWithThisFrameVoid:
                    assert result_kind == 'void'
                    return
                except jitexc.DoneWithThisFrameInt, e:
                    assert result_kind == 'int'
                    return specialize_value(RESULT, e.result)
                except jitexc.DoneWithThisFrameRef, e:
                    assert result_kind == 'ref'
                    return specialize_value(RESULT, e.result)
                except jitexc.DoneWithThisFrameFloat, e:
                    assert result_kind == 'float'
                    return specialize_value(RESULT, e.result)
                except jitexc.ExitFrameWithExceptionRef, e:
                    value = ts.cast_to_baseclass(e.value)
                    if not we_are_translated():
                        raise LLException(ts.get_typeptr(value), value)
                    else:
                        value = cast_base_ptr_to_instance(Exception, value)
                        raise Exception, value

        def handle_jitexception(e):
            # XXX the bulk of this function is mostly a copy-paste from above
            try:
                raise e
Пример #13
0
    def rewrite_jit_merge_point(self, jd, policy):
        #
        # Mutate the original portal graph from this:
        #
        #       def original_portal(..):
        #           stuff
        #           while 1:
        #               jit_merge_point(*args)
        #               more stuff
        #
        # to that:
        #
        #       def original_portal(..):
        #           stuff
        #           return portal_runner(*args)
        #
        #       def portal_runner(*args):
        #           while 1:
        #               try:
        #                   return portal(*args)
        #               except ContinueRunningNormally, e:
        #                   *args = *e.new_args
        #               except DoneWithThisFrame, e:
        #                   return e.return
        #               except ExitFrameWithException, e:
        #                   raise Exception, e.value
        #
        #       def portal(*args):
        #           while 1:
        #               more stuff
        #
        origportalgraph = jd._jit_merge_point_in
        portalgraph = jd.portal_graph
        PORTALFUNC = jd._PORTAL_FUNCTYPE

        # ____________________________________________________________
        # Prepare the portal_runner() helper
        #
        from rpython.jit.metainterp.warmstate import specialize_value
        from rpython.jit.metainterp.warmstate import unspecialize_value
        portal_ptr = self.cpu.ts.functionptr(PORTALFUNC,
                                             'portal',
                                             graph=portalgraph)
        jd._portal_ptr = portal_ptr
        #
        portalfunc_ARGS = []
        nums = {}
        for i, ARG in enumerate(PORTALFUNC.ARGS):
            kind = history.getkind(ARG)
            assert kind != 'void'
            if i < len(jd.jitdriver.greens):
                color = 'green'
            else:
                color = 'red'
            attrname = '%s_%s' % (color, kind)
            count = nums.get(attrname, 0)
            nums[attrname] = count + 1
            portalfunc_ARGS.append((ARG, attrname, count))
        portalfunc_ARGS = unrolling_iterable(portalfunc_ARGS)
        #
        rtyper = self.translator.rtyper
        RESULT = PORTALFUNC.RESULT
        result_kind = history.getkind(RESULT)
        ts = self.cpu.ts
        state = jd.warmstate
        maybe_compile_and_run = jd._maybe_compile_and_run_fn

        def ll_portal_runner(*args):
            start = True
            while 1:
                try:
                    # maybe enter from the function's start.  Note that the
                    # 'start' variable is constant-folded away because it's
                    # the first statement in the loop.
                    if start:
                        maybe_compile_and_run(
                            state.increment_function_threshold, *args)
                    #
                    # then run the normal portal function, i.e. the
                    # interpreter's main loop.  It might enter the jit
                    # via maybe_enter_jit(), which typically ends with
                    # handle_fail() being called, which raises on the
                    # following exceptions --- catched here, because we
                    # want to interrupt the whole interpreter loop.
                    return support.maybe_on_top_of_llinterp(
                        rtyper, portal_ptr)(*args)
                except jitexc.ContinueRunningNormally, e:
                    args = ()
                    for ARGTYPE, attrname, count in portalfunc_ARGS:
                        x = getattr(e, attrname)[count]
                        x = specialize_value(ARGTYPE, x)
                        args = args + (x, )
                    start = False
                    continue
                except jitexc.DoneWithThisFrameVoid:
                    assert result_kind == 'void'
                    return
                except jitexc.DoneWithThisFrameInt, e:
                    assert result_kind == 'int'
                    return specialize_value(RESULT, e.result)
Пример #14
0
                    args = ()
                    for ARGTYPE, attrname, count in portalfunc_ARGS:
                        x = getattr(e, attrname)[count]
                        x = specialize_value(ARGTYPE, x)
                        args = args + (x, )
                    start = False
                    continue
                except jitexc.DoneWithThisFrameVoid:
                    assert result_kind == 'void'
                    return
                except jitexc.DoneWithThisFrameInt, e:
                    assert result_kind == 'int'
                    return specialize_value(RESULT, e.result)
                except jitexc.DoneWithThisFrameRef, e:
                    assert result_kind == 'ref'
                    return specialize_value(RESULT, e.result)
                except jitexc.DoneWithThisFrameFloat, e:
                    assert result_kind == 'float'
                    return specialize_value(RESULT, e.result)
                except jitexc.ExitFrameWithExceptionRef, e:
                    value = ts.cast_to_baseclass(e.value)
                    if not we_are_translated():
                        raise LLException(ts.get_typeptr(value), value)
                    else:
                        value = cast_base_ptr_to_instance(Exception, value)
                        raise Exception, value

        def handle_jitexception(e):
            # XXX the bulk of this function is mostly a copy-paste from above
            try:
                raise e