def build_adi(function, types): t = Translation(function, types) t.rtype() if option.view: t.view() adi = AbstractDataFlowInterpreter(t.context) graph = graphof(t.context, function) adi.schedule_function(graph) adi.complete() return t.context, adi, graph
def build_adi(function, types): t = Translation(function, types) t.rtype() if option.view: t.view() adi = AbstractDataFlowInterpreter(t.context) graph = graphof(t.context, function) adi.schedule_function(graph) adi.complete() return t.context, adi, graph
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')
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')
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')
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')
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')
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")
def translate_to_graph(function, arguments): """ Translate a function to basic blocks and visualize these blocks (see requirements.txt for necessary pip packages); use this mechanism in conjunction with interpret_from_graph :param function: the RPython function to interpret :param arguments: a list of the arguments passed to 'function' :return: the translator RTyper and the basic block graph for the function passed """ assert callable(function) assert isinstance(arguments, list) translation = Translation(function, arguments) translation.annotate() translation.rtype() translation.backendopt() translation.view() return translation.driver.translator.rtyper, translation.driver.translator.graphs[0]
def test_computed_int_symbolic(): too_early = True def compute_fn(): assert not too_early return 7 k = ComputedIntSymbolic(compute_fn) def f(ignored): return k*6 t = Translation(f) t.rtype() if option.view: t.view() too_early = False fn = compile(f, [int]) res = fn(0) assert res == 42
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")
def test_computed_int_symbolic(): too_early = True def compute_fn(): assert not too_early return 7 k = ComputedIntSymbolic(compute_fn) def f(ignored): return k * 6 t = Translation(f) t.rtype() if option.view: t.view() too_early = False fn = compile(f, [int]) res = fn(0) assert res == 42
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
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