示例#1
0
def test_compiled():
    class MyCode:
        pass

    def get_name(mycode):
        raise NotImplementedError

    rvmprof.register_code_object_class(MyCode, get_name)

    @rvmprof.vmprof_execute_code("mycode", lambda code, level: code)
    def mainloop(code, level):
        if level > 0:
            mainloop(code, level - 1)
        else:
            p, length = traceback.traceback(20)
            traceback.walk_traceback(MyCode, my_callback, 42, p, length)
            lltype.free(p, flavor='raw')

    def my_callback(code, loc, arg):
        print code, loc, arg
        return 0

    def f(argv):
        code1 = MyCode()
        rvmprof.register_code(code1, "foo")
        mainloop(code1, 2)
        return 0

    t = Translation(f, None, gc="boehm")
    t.compile_c()
    stdout = t.driver.cbuilder.cmdexec('')
    r = re.compile("[<]MyCode object at 0x([0-9a-f]+)[>] 0 42\n")
    got = r.findall(stdout)
    assert got == [got[0]] * 3
示例#2
0
 def test_enter_leave_portal_frame(self):
     from rpython.translator.interactive import Translation
     def g():
         enter_portal_frame(1)
         leave_portal_frame()
     t = Translation(g, [])
     t.compile_c() # does not crash
示例#3
0
def test_compiled():
    class MyCode:
        pass

    def get_name(mycode):
        raise NotImplementedError

    rvmprof.register_code_object_class(MyCode, get_name)

    @rvmprof.vmprof_execute_code("mycode", lambda code, level: code)
    def mainloop(code, level):
        if level > 0:
            mainloop(code, level - 1)
        else:
            p, length = traceback.traceback(20)
            traceback.walk_traceback(MyCode, my_callback, 42, p, length)
            lltype.free(p, flavor="raw")

    def my_callback(code, loc, arg):
        print code, loc, arg
        return 0

    def f(argv):
        code1 = MyCode()
        rvmprof.register_code(code1, "foo")
        mainloop(code1, 2)
        return 0

    t = Translation(f, None, gc="boehm")
    t.compile_c()
    stdout = t.driver.cbuilder.cmdexec("")
    r = re.compile("[<]MyCode object at 0x([0-9a-f]+)[>] 0 42\n")
    got = r.findall(stdout)
    assert got == [got[0]] * 3
示例#4
0
文件: test_jit.py 项目: sota/pypy-old
 def test_enter_leave_portal_frame(self):
     from rpython.translator.interactive import Translation
     def g():
         enter_portal_frame(1)
         leave_portal_frame()
     t = Translation(g, [])
     t.compile_c() # does not crash
示例#5
0
文件: test_genc.py 项目: charred/pypy
def test_name():
    def f():
        return 3

    f.c_name = 'pypy_xyz_f'

    t = Translation(f, [], backend="c")
    t.annotate()
    t.compile_c()
    if py.test.config.option.view:
        t.view()
    assert hasattr(ctypes.CDLL(str(t.driver.c_entryp)), 'pypy_xyz_f')
示例#6
0
def test_jitted():
    from rpython.jit.backend import detect_cpu
    if not detect_cpu.autodetect().startswith('x86'):
        py.test.skip("HAS_CODEMAP is only in the x86 jit backend for now")

    class MyCode:
        pass

    def get_name(mycode):
        raise NotImplementedError

    rvmprof.register_code_object_class(MyCode, get_name)

    jitdriver = jit.JitDriver(
        greens=['code'],
        reds='auto',
        is_recursive=True,
        get_unique_id=lambda code: rvmprof.get_unique_id(code))

    @rvmprof.vmprof_execute_code("mycode", lambda code, level, total_i: code)
    def mainloop(code, level, total_i):
        i = 20
        while i > 0:
            jitdriver.jit_merge_point(code=code)
            i -= 1
            if level > 0:
                mainloop(code, level - 1, total_i + i)
        if level == 0 and total_i == 0:
            p, length = traceback.traceback(20)
            traceback.walk_traceback(MyCode, my_callback, 42, p, length)
            lltype.free(p, flavor='raw')

    def my_callback(code, loc, arg):
        print code, loc, arg
        return 0

    def f(argv):
        jit.set_param(jitdriver, "inlining", 0)
        code1 = MyCode()
        rvmprof.register_code(code1, "foo")
        mainloop(code1, 2, 0)
        return 0

    t = Translation(f, None, gc="boehm")
    t.rtype()
    t.driver.pyjitpl_lltype()
    t.compile_c()
    stdout = t.driver.cbuilder.cmdexec('')
    r = re.compile("[<]MyCode object at 0x([0-9a-f]+)[>] (\d) 42\n")
    got = r.findall(stdout)
    addr = got[0][0]
    assert got == [(addr, '1'), (addr, '1'), (addr, '0')]
示例#7
0
def test_name():
    def f():
        return 3

    f.c_name = 'pypy_xyz_f'
    f.exported_symbol = True

    t = Translation(f, [], backend="c")
    t.annotate()
    t.compile_c()
    if py.test.config.option.view:
        t.view()
    assert hasattr(ctypes.CDLL(str(t.driver.c_entryp)), 'pypy_xyz_f')
示例#8
0
def test_jitted():
    from rpython.jit.backend import detect_cpu

    if not detect_cpu.autodetect().startswith("x86"):
        py.test.skip("HAS_CODEMAP is only in the x86 jit backend for now")

    class MyCode:
        pass

    def get_name(mycode):
        raise NotImplementedError

    rvmprof.register_code_object_class(MyCode, get_name)

    jitdriver = jit.JitDriver(
        greens=["code"], reds="auto", is_recursive=True, get_unique_id=lambda code: rvmprof.get_unique_id(code)
    )

    @rvmprof.vmprof_execute_code("mycode", lambda code, level, total_i: code)
    def mainloop(code, level, total_i):
        i = 20
        while i > 0:
            jitdriver.jit_merge_point(code=code)
            i -= 1
            if level > 0:
                mainloop(code, level - 1, total_i + i)
        if level == 0 and total_i == 0:
            p, length = traceback.traceback(20)
            traceback.walk_traceback(MyCode, my_callback, 42, p, length)
            lltype.free(p, flavor="raw")

    def my_callback(code, loc, arg):
        print code, loc, arg
        return 0

    def f(argv):
        jit.set_param(jitdriver, "inlining", 0)
        code1 = MyCode()
        rvmprof.register_code(code1, "foo")
        mainloop(code1, 2, 0)
        return 0

    t = Translation(f, None, gc="boehm")
    t.rtype()
    t.driver.pyjitpl_lltype()
    t.compile_c()
    stdout = t.driver.cbuilder.cmdexec("")
    r = re.compile("[<]MyCode object at 0x([0-9a-f]+)[>] (\d) 42\n")
    got = r.findall(stdout)
    addr = got[0][0]
    assert got == [(addr, "1"), (addr, "1"), (addr, "0")]
示例#9
0
def test_entrypoints():
    def f():
        return 3

    key = "test_entrypoints42"
    @entrypoint(key, [int], "foobar")
    def g(x):
        return x + 42

    t = Translation(f, [], backend="c", secondaryentrypoints="test_entrypoints42")
    t.annotate()
    t.compile_c()
    if py.test.config.option.view:
        t.view()
    assert hasattr(ctypes.CDLL(str(t.driver.c_entryp)), 'foobar')
示例#10
0
def test_exportstruct():
    from rpython.translator.tool.cbuild import ExternalCompilationInfo
    from rpython.rlib.exports import export_struct
    def f():
        return 42
    FOO = Struct("FOO", ("field1", Signed))
    foo = malloc(FOO, flavor="raw")
    foo.field1 = 43
    export_struct("BarStruct", foo._obj)
    t = Translation(f, [], backend="c")
    t.annotate()
    t.compile_c()
    if py.test.config.option.view:
        t.view()
    assert hasattr(ctypes.CDLL(str(t.driver.c_entryp)), 'BarStruct')
    free(foo, flavor="raw")
示例#11
0
文件: util.py 项目: qinsoon/zebu
def fncptr_from_rpy_func(rpy_fnc,
                         llargtypes,
                         llrestype,
                         mode=ctypes.RTLD_GLOBAL,
                         **kwargs):
    # NOTE: requires mu-client-pypy
    from rpython.rtyper.lltypesystem import rffi
    from rpython.translator.interactive import Translation
    from rpython.config.translationoption import set_opt_level

    preload_libmu()
    emit_dir = os.environ.get('MU_EMIT_DIR', str(bin_dir))
    kwargs.setdefault('backend', 'mu')
    kwargs.setdefault('impl', 'zebu')
    kwargs.setdefault('codegen', 'api')
    kwargs.setdefault('testjit', True)
    kwargs.setdefault('vmargs', "--aot-emit-dir=" + emit_dir)
    kwargs.setdefault('suplibdir', str(bin_dir))
    kwargs.setdefault('no_ovf', True)

    t = Translation(rpy_fnc, llargtypes, **kwargs)
    set_opt_level(t.config, '3')
    if kwargs['backend'] == 'mu':
        db, bdlgen, fnc_name = t.compile_mu()
        emit_dir = py.path.local(emit_dir)
        libpath = emit_dir.join('lib%(fnc_name)s' % locals() + libext)
        bdlgen.mu.compile_to_sharedlib(libpath.strpath, [])
        extras = (db, bdlgen)
    else:
        libpath = t.compile_c()
        fnc_name = 'pypy_g_' + rpy_fnc.__name__
        extras = None
    return rffi_fncptr_from_lib(libpath, fnc_name, llargtypes, llrestype,
                                mode), extras
示例#12
0
文件: test_genc.py 项目: charred/pypy
def test_exportstruct():
    from rpython.translator.tool.cbuild import ExternalCompilationInfo
    from rpython.rlib.exports import export_struct
    def f():
        return 42
    FOO = Struct("FOO", ("field1", Signed))
    foo = malloc(FOO, flavor="raw")
    foo.field1 = 43
    # maybe export_struct should add the struct name to eci automatically?
    # https://bugs.pypy.org/issue1361
    foo._obj._compilation_info = ExternalCompilationInfo(export_symbols=['BarStruct'])
    export_struct("BarStruct", foo._obj)
    t = Translation(f, [], backend="c")
    t.annotate()
    t.compile_c()
    if py.test.config.option.view:
        t.view()
    assert hasattr(ctypes.CDLL(str(t.driver.c_entryp)), 'BarStruct')
    free(foo, flavor="raw")
示例#13
0
def test_tagged_boehm():
    t = Translation(entry_point, gc='boehm', taggedpointers=True)
    try:
        exename = str(t.compile_c())
    finally:
        if option.view:
            t.view()
    g = os.popen(exename, 'r')
    data = g.read()
    g.close()
    assert data.rstrip().endswith('ALL OK')
示例#14
0
def test_tagged_boehm():
    t = Translation(entry_point, gc='boehm', taggedpointers=True)
    try:
        exename = str(t.compile_c())
    finally:
        if option.view:
            t.view()
    g = os.popen(exename, 'r')
    data = g.read()
    g.close()
    assert data.rstrip().endswith('ALL OK')
示例#15
0
def test_parser():
    def f(x):
        if x:
            s = "a(X, Y, Z)."
        else:
            s = "f(a, X, _, _, X, f(X, 2.455))."
        term = parsing.parse_file(s)
        assert isinstance(term, parsing.Nonterminal)
        return term.symbol
    assert f(True) == "file"
    assert f(True) == "file"
    t = Translation(f)
    t.annotate([bool])
    t.rtype()
    t.backendopt()
    func = t.compile_c()
    assert func(True) == "file"
    assert func(False) == "file"
示例#16
0
def test_engine():
    e = get_engine("""
        g(a, a).
        g(a, b).
        g(b, c).
        f(X, Z) :- g(X, Y), g(Y, Z).
    """)
    t1 = parse_query_term("f(a, c).")
    t2 = parse_query_term("f(X, c).")
    def run():
        e.run(t1)
        e.run(t2)
        v0 = e.heap.getvar(0)
        if isinstance(v0, Atom):
            return v0.name()        return "no!"
    assert run() == "a"
    t = Translation(run)
    t.annotate()
    t.rtype()
    func = t.compile_c()
    assert func() == "a"
示例#17
0
def compile(self,
            entry_point,
            backendopt=True,
            withsmallfuncsets=None,
            shared=False,
            thread=False):
    t = Translation(entry_point, None, gc="boehm")
    self.t = t
    t.set_backend_extra_options(c_debug_defines=True)
    t.config.translation.reverse_debugger = True
    t.config.translation.lldebug0 = True
    t.config.translation.shared = shared
    t.config.translation.thread = thread
    if withsmallfuncsets is not None:
        t.config.translation.withsmallfuncsets = withsmallfuncsets
    if not backendopt:
        t.disable(["backendopt_lltype"])
    t.annotate()
    t.rtype()
    if t.backendopt:
        t.backendopt()
    self.exename = t.compile_c()
    self.rdbname = os.path.join(os.path.dirname(str(self.exename)), 'log.rdb')
示例#18
0
def compile(fn,
            argtypes,
            view=False,
            gcpolicy="none",
            backendopt=True,
            annotatorpolicy=None,
            thread=False,
            **kwds):
    argtypes_unroll = unrolling_iterable(enumerate(argtypes))

    for argtype in argtypes:
        if argtype not in [
                int, float, str, bool, r_ulonglong, r_longlong, r_uint
        ]:
            raise Exception("Unsupported argtype, %r" % (argtype, ))

    def entry_point(argv):
        args = ()
        for i, argtype in argtypes_unroll:
            a = argv[i + 1]
            if argtype is int:
                args += (int(a), )
            elif argtype is r_uint:
                args += (r_uint(int(a)), )
            elif argtype is r_longlong:
                args += (parse_longlong(a), )
            elif argtype is r_ulonglong:
                args += (parse_ulonglong(a), )
            elif argtype is bool:
                if a == 'True':
                    args += (True, )
                else:
                    assert a == 'False'
                    args += (False, )
            elif argtype is float:
                if a == 'inf':
                    args += (INFINITY, )
                elif a == '-inf':
                    args += (-INFINITY, )
                elif a == 'nan':
                    args += (NAN, )
                else:
                    args += (float(a), )
            else:
                if a.startswith('/'):  # escaped string
                    if len(a) == 1:
                        a = ''
                    else:
                        l = a[1:].split(',')
                        a = ''.join([chr(int(x)) for x in l])
                args += (a, )
        res = fn(*args)
        print "THE RESULT IS:", llrepr_out(res), ";"
        return 0

    t = Translation(entry_point,
                    None,
                    gc=gcpolicy,
                    backend="c",
                    policy=annotatorpolicy,
                    thread=thread,
                    **kwds)
    if not backendopt:
        t.disable(["backendopt_lltype"])
    t.driver.config.translation.countmallocs = True
    t.annotate()
    try:
        if py.test.config.option.view:
            t.view()
    except AttributeError:
        pass
    t.rtype()
    if backendopt:
        t.backendopt()
    try:
        if py.test.config.option.view:
            t.view()
    except AttributeError:
        pass
    t.compile_c()
    ll_res = graphof(t.context, fn).getreturnvar().concretetype

    def output(stdout):
        for line in stdout.splitlines(False):
            if len(repr(line)) == len(line) + 2:  # no escaped char
                print line
            else:
                print 'REPR:', repr(line)

    def f(*args, **kwds):
        expected_extra_mallocs = kwds.pop('expected_extra_mallocs', 0)
        expected_exception_name = kwds.pop('expected_exception_name', None)
        assert not kwds
        assert len(args) == len(argtypes)
        for arg, argtype in zip(args, argtypes):
            assert isinstance(arg, argtype)

        stdout = t.driver.cbuilder.cmdexec(
            " ".join([llrepr_in(arg) for arg in args]),
            expect_crash=(expected_exception_name is not None))
        #
        if expected_exception_name is not None:
            stdout, stderr = stdout
            print '--- stdout ---'
            output(stdout)
            print '--- stderr ---'
            output(stderr)
            print '--------------'
            stderr, prevline, lastline, empty = stderr.rsplit('\n', 3)
            assert empty == ''
            expected = 'Fatal RPython error: ' + expected_exception_name
            assert lastline == expected or prevline == expected
            return None

        output(stdout)
        stdout, lastline, empty = stdout.rsplit('\n', 2)
        assert empty == ''
        assert lastline.startswith('MALLOC COUNTERS: ')
        mallocs, frees = map(int, lastline.split()[2:])
        assert stdout.endswith(' ;')
        pos = stdout.rindex('THE RESULT IS: ')
        res = stdout[pos + len('THE RESULT IS: '):-2]
        #
        if isinstance(expected_extra_mallocs, int):
            assert mallocs - frees == expected_extra_mallocs
        else:
            assert mallocs - frees in expected_extra_mallocs
        #
        if ll_res in [
                lltype.Signed, lltype.Unsigned, lltype.SignedLongLong,
                lltype.UnsignedLongLong
        ]:
            return int(res)
        elif ll_res == lltype.Bool:
            return bool(int(res))
        elif ll_res == lltype.Char:
            assert len(res) == 1
            return res
        elif ll_res == lltype.Float:
            return float(res)
        elif ll_res == lltype.Ptr(STR):
            return res
        elif ll_res == lltype.Void:
            return None
        raise NotImplementedError("parsing %s" % (ll_res, ))

    class CompilationResult(object):
        def __repr__(self):
            return 'CompilationResult(%s)' % (fn.__name__, )

        def __call__(self, *args, **kwds):
            return f(*args, **kwds)

    cr = CompilationResult()
    cr.t = t
    cr.builder = t.driver.cbuilder
    return cr
示例#19
0
def compile(fn, argtypes, view=False, gcpolicy="none", backendopt=True,
            annotatorpolicy=None, thread=False, **kwds):
    argtypes_unroll = unrolling_iterable(enumerate(argtypes))

    for argtype in argtypes:
        if argtype not in [int, float, str, bool, r_ulonglong, r_longlong,
                           r_uint]:
            raise Exception("Unsupported argtype, %r" % (argtype,))

    def entry_point(argv):
        args = ()
        for i, argtype in argtypes_unroll:
            a = argv[i + 1]
            if argtype is int:
                args += (int(a),)
            elif argtype is r_uint:
                args += (r_uint(int(a)),)
            elif argtype is r_longlong:
                args += (parse_longlong(a),)
            elif argtype is r_ulonglong:
                args += (parse_ulonglong(a),)
            elif argtype is bool:
                if a == 'True':
                    args += (True,)
                else:
                    assert a == 'False'
                    args += (False,)
            elif argtype is float:
                if a == 'inf':
                    args += (INFINITY,)
                elif a == '-inf':
                    args += (-INFINITY,)
                elif a == 'nan':
                    args += (NAN,)
                else:
                    args += (float(a),)
            else:
                if a.startswith('/'):     # escaped string
                    if len(a) == 1:
                        a = ''
                    else:
                        l = a[1:].split(',')
                        a = ''.join([chr(int(x)) for x in l])
                args += (a,)
        res = fn(*args)
        print "THE RESULT IS:", llrepr_out(res), ";"
        return 0

    t = Translation(entry_point, None, gc=gcpolicy, backend="c",
                    policy=annotatorpolicy, thread=thread, **kwds)
    if not backendopt:
        t.disable(["backendopt_lltype"])
    t.driver.config.translation.countmallocs = True
    t.annotate()
    try:
        if py.test.config.option.view:
            t.view()
    except AttributeError:
        pass
    t.rtype()
    if backendopt:
        t.backendopt()
    try:
        if py.test.config.option.view:
            t.view()
    except AttributeError:
        pass
    t.compile_c()
    ll_res = graphof(t.context, fn).getreturnvar().concretetype

    def output(stdout):
        for line in stdout.splitlines(False):
            if len(repr(line)) == len(line) + 2:   # no escaped char
                print line
            else:
                print 'REPR:', repr(line)

    def f(*args, **kwds):
        expected_extra_mallocs = kwds.pop('expected_extra_mallocs', 0)
        expected_exception_name = kwds.pop('expected_exception_name', None)
        assert not kwds
        assert len(args) == len(argtypes)
        for arg, argtype in zip(args, argtypes):
            assert isinstance(arg, argtype)

        stdout = t.driver.cbuilder.cmdexec(
            " ".join([llrepr_in(arg) for arg in args]),
            expect_crash=(expected_exception_name is not None))
        #
        if expected_exception_name is not None:
            stdout, stderr = stdout
            print '--- stdout ---'
            output(stdout)
            print '--- stderr ---'
            output(stderr)
            print '--------------'
            stderr, prevline, lastline, empty = stderr.rsplit('\n', 3)
            assert empty == ''
            expected = 'Fatal RPython error: ' + expected_exception_name
            assert lastline == expected or prevline == expected
            return None

        output(stdout)
        stdout, lastline, empty = stdout.rsplit('\n', 2)
        assert empty == ''
        assert lastline.startswith('MALLOC COUNTERS: ')
        mallocs, frees = map(int, lastline.split()[2:])
        assert stdout.endswith(' ;')
        pos = stdout.rindex('THE RESULT IS: ')
        res = stdout[pos + len('THE RESULT IS: '):-2]
        #
        if isinstance(expected_extra_mallocs, int):
            assert mallocs - frees == expected_extra_mallocs
        else:
            assert mallocs - frees in expected_extra_mallocs
        #
        if ll_res in [lltype.Signed, lltype.Unsigned, lltype.SignedLongLong,
                      lltype.UnsignedLongLong]:
            return int(res)
        elif ll_res == lltype.Bool:
            return bool(int(res))
        elif ll_res == lltype.Char:
            assert len(res) == 1
            return res
        elif ll_res == lltype.Float:
            return float(res)
        elif ll_res == lltype.Ptr(STR):
            return res
        elif ll_res == lltype.Void:
            return None
        raise NotImplementedError("parsing %s" % (ll_res,))

    class CompilationResult(object):
        def __repr__(self):
            return 'CompilationResult(%s)' % (fn.__name__,)
        def __call__(self, *args, **kwds):
            return f(*args, **kwds)

    cr = CompilationResult()
    cr.t = t
    cr.builder = t.driver.cbuilder
    return cr