def test_dont_remove_with__del__(self): import os delcalls = [0] class A(object): nextid = 0 def __init__(self): self.id = self.nextid self.nextid += 1 def __del__(self): delcalls[0] += 1 os.write(1, "__del__\n") def f(x=int): a = A() i = 0 while i < x: a = A() os.write(1, str(delcalls[0]) + "\n") i += 1 return 1 t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) backend_optimizations(t) op = graph.startblock.exits[0].target.exits[1].target.operations[0] assert op.opname == "malloc"
def check(self, fn, signature, args, expected_result, must_be_removed=True): remover = self.MallocRemover() t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper(type_system=self.type_system).specialize() graph = graphof(t, fn) if option.view: t.view() # to detect missing keepalives and broken intermediate graphs, # we do the loop ourselves instead of calling remove_simple_mallocs() while True: progress = remover.remove_mallocs_once(graph) simplify.transform_dead_op_vars_in_blocks(list(graph.iterblocks())) if progress and option.view: t.view() if expected_result is not Ellipsis: interp = LLInterpreter(t.rtyper) res = interp.eval_graph(graph, args) assert res == expected_result if not progress: break if must_be_removed: self.check_malloc_removed(graph) return graph
def _build_gen(func, annotation, graph=None, backendopt=True, exctrans=False, annotatorpolicy=None, nowrap=False): try: func = func.im_func except AttributeError: pass t = TranslationContext() if graph is not None: graph.func = func ann = t.buildannotator(policy=annotatorpolicy) inputcells = [ann.typeannotation(a) for a in annotation] ann.build_graph_types(graph, inputcells) t.graphs.insert(0, graph) else: ann = t.buildannotator(policy=annotatorpolicy) ann.build_types(func, annotation) if getoption('view'): t.view() t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() return _build_gen_from_graph(main_graph, t, exctrans, nowrap)
def test_dont_remove_with__del__(self): import os delcalls = [0] class A(object): nextid = 0 def __init__(self): self.id = self.nextid self.nextid += 1 def __del__(self): delcalls[0] += 1 os.write(1, "__del__\n") def f(x=int): a = A() i = 0 while i < x: a = A() os.write(1, str(delcalls[0]) + "\n") i += 1 return 1 t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) backend_optimizations(t) op = graph.startblock.exits[0].target.exits[1].target.operations[0] assert op.opname == "malloc"
def getcompiled(self, func, argstypelist=[], annotatorpolicy=None): from pypy.config.pypyoption import get_pypy_config config = get_pypy_config(translating=True) config.translation.gc = self.gcpolicy config.translation.thread = self.use_threads if self.stacklessgc: config.translation.gcrootfinder = "stackless" config.translation.simplifying = True t = TranslationContext(config=config) self.t = t a = t.buildannotator(policy=annotatorpolicy) a.build_types(func, argstypelist) t.buildrtyper().specialize() t.checkgraphs() def compile(): cbuilder = CExtModuleBuilder(t, func, config=config) c_source_filename = cbuilder.generate_source( defines=cbuilder.DEBUG_DEFINES) if conftest.option.view: t.view() cbuilder.compile() self._cleanups.append( cbuilder.cleanup) # schedule cleanup after test return cbuilder.get_entry_point(isolated=True) return compile()
def test_remove_same_as_nonconst(): from pypy.rlib.nonconst import NonConstant from pypy.rpython.lltypesystem.lloperation import llop from pypy.rpython.lltypesystem import lltype def f(): if NonConstant(False): x = llop.same_as(lltype.Signed, 666) return 42 t = TranslationContext() t.buildannotator().build_types(f, []) t.buildrtyper().specialize() f_graph = graphof(t, f) #simple_inline_function(t, nothing, f_graph) # here, the graph looks like v21=same_as(True); exitswitch: v21 remove_same_as(f_graph) t.checkgraphs() # only one path should be left for block in f_graph.iterblocks(): assert len(block.exits) <= 1 for block in t.annotator.annotated: assert None not in block.operations interp = LLInterpreter(t.rtyper) result = interp.eval_graph(f_graph, []) assert result == 42
def translate(self, func, sig): t = TranslationContext() t.buildannotator().build_types(func, sig) t.buildrtyper(type_system=self.type_system).specialize() if option.view: t.view() return t, RaiseAnalyzer(t)
def test_remove_same_as(): def nothing(x): return x def f(): nothing(False) if nothing(True): return 42 else: return 666 t = TranslationContext() t.buildannotator().build_types(f, []) t.buildrtyper().specialize() # now we make the 'if True' appear f_graph = graphof(t, f) simple_inline_function(t, nothing, f_graph) # here, the graph looks like v21=same_as(True); exitswitch: v21 remove_same_as(f_graph) t.checkgraphs() # only one path should be left for block in f_graph.iterblocks(): assert len(block.exits) <= 1 interp = LLInterpreter(t.rtyper) result = interp.eval_graph(f_graph, []) assert result == 42
def compile_func(fn, inputtypes, t=None, gcpolicy="ref"): from pypy.config.pypyoption import get_pypy_config config = get_pypy_config(translating=True) config.translation.gc = gcpolicy config.translation.countmallocs = True if t is None: t = TranslationContext(config=config) if inputtypes is not None: t.buildannotator().build_types(fn, inputtypes) t.buildrtyper().specialize() builder = genc.CExtModuleBuilder(t, fn, config=config) builder.generate_source() builder.compile() if conftest.option.view: t.view() compiled_fn = builder.get_entry_point() malloc_counters = builder.get_malloc_counters() def checking_fn(*args, **kwds): try: return compiled_fn(*args, **kwds) finally: mallocs, frees = malloc_counters() assert mallocs == frees return checking_fn
def translates(self, func=None, argtypes=None, **kwds): config = make_config(None, **kwds) if func is not None: if argtypes is None: nb_args = func.func_code.co_argcount argtypes = [W_Root] * nb_args # t = TranslationContext(config=config) self.t = t # for debugging ann = t.buildannotator() ann.policy.allow_someobjects = False if func is not None: ann.build_types(func, argtypes, complete_now=False) # # annotate all _seen_extras, knowing that annotating some may # grow the list done = 0 while done < len(self._seen_extras): #print self._seen_extras ann.build_types(self._seen_extras[done], [], complete_now=False) done += 1 ann.complete() #t.viewcg() t.buildrtyper().specialize() t.checkgraphs()
def translate(self, func, sig): t = TranslationContext() t.buildannotator().build_types(func, sig) t.buildrtyper().specialize() if option.view: t.view() return t, WriteAnalyzer(t)
def getcompiled(func, view=conftest.option.view, use_boehm=False): from pypy.translator.translator import TranslationContext from pypy.translator.backendopt.all import backend_optimizations from pypy.translator.c import gc from pypy.translator.c.genc import CExtModuleBuilder global t # allow us to view later t = TranslationContext() t.buildannotator().build_types(func, get_annotation(func)) t.buildrtyper().specialize() t.checkgraphs() gcpolicy = None if use_boehm: gcpolicy = gc.BoehmGcPolicy cbuilder = CExtModuleBuilder(t, func, t.config, gcpolicy=gcpolicy) cbuilder.generate_source() cbuilder.compile() backend_optimizations(t) if view: t.viewcg() return getattr(cbuilder.import_module(), func.__name__)
def compile(f, gc, enable_opts='', **kwds): from pypy.annotation.listdef import s_list_of_strings from pypy.translator.translator import TranslationContext from pypy.jit.metainterp.warmspot import apply_jit from pypy.translator.c import genc # t = TranslationContext() t.config.translation.gc = gc if gc != 'boehm': t.config.translation.gcremovetypeptr = True for name, value in kwds.items(): setattr(t.config.translation, name, value) ann = t.buildannotator(policy=annpolicy.StrictAnnotatorPolicy()) ann.build_types(f, [s_list_of_strings], main_entry_point=True) t.buildrtyper().specialize() if kwds['jit']: patch = get_functions_to_patch() old_value = {} try: for (obj, attr), value in patch.items(): old_value[obj, attr] = getattr(obj, attr) setattr(obj, attr, value) # apply_jit(t, enable_opts=enable_opts) # finally: for (obj, attr), oldvalue in old_value.items(): setattr(obj, attr, oldvalue) cbuilder = genc.CStandaloneBuilder(t, f, t.config) cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES) cbuilder.compile() return cbuilder
def __init__(self, function, annotations, stackless=False, view=False, html=None, is_interactive=False, root = None, run_browser = True, policy = None): if not use_browsertest and not _CLI_is_on_path(): py.test.skip('Javascript CLI (js) not found') self.html = html self.is_interactive = is_interactive t = TranslationContext() if policy is None: from pypy.annotation.policy import AnnotatorPolicy policy = AnnotatorPolicy() policy.allow_someobjects = False ann = t.buildannotator(policy=policy) ann.build_types(function, annotations) if view or option.view: t.view() t.buildrtyper(type_system="ootype").specialize() if view or option.view: t.view() #self.js = JS(t, [function, callback_function], stackless) self.js = JS(t, function, stackless) self.js.write_source() if root is None and use_tg: from pypy.translator.js.demo.jsdemo.controllers import Root self.root = Root else: self.root = root self.run_browser = run_browser self.function_calls = []
def wrap_stackless_function(self, fn): def entry_point(argv): os.write(1, str(fn())+"\n") return 0 from pypy.config.pypyoption import get_pypy_config config = get_pypy_config(translating=True) config.translation.gc = self.gcpolicy config.translation.stackless = True if self.stacklessgc: config.translation.gcrootfinder = "stackless" t = TranslationContext(config=config) self.t = t t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() if self.backendopt: backend_optimizations(t) from pypy.translator.transform import insert_ll_stackcheck insert_ll_stackcheck(t) cbuilder = CStandaloneBuilder(t, entry_point, config=config) cbuilder.stackless = True cbuilder.generate_source() cbuilder.compile() res = cbuilder.cmdexec('') return int(res.strip())
def check(self, fn, signature, args, expected_result, must_be_removed=True, inline=None): remover = self.MallocRemover() t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper(type_system=self.type_system).specialize() graph = graphof(t, fn) if inline is not None: from pypy.translator.backendopt.inline import auto_inline_graphs auto_inline_graphs(t, t.graphs, inline) if option.view: t.view() # to detect missing keepalives and broken intermediate graphs, # we do the loop ourselves instead of calling remove_simple_mallocs() while True: progress = remover.remove_mallocs_once(graph) simplify.transform_dead_op_vars_in_blocks(list(graph.iterblocks())) if progress and option.view: t.view() if expected_result is not Ellipsis: interp = LLInterpreter(t.rtyper) res = interp.eval_graph(graph, args) assert res == expected_result if not progress: break if must_be_removed: self.check_malloc_removed(graph) return graph
def check(self, fn, signature, args, expected_result, expected_mallocs=0, expected_calls=0): t = TranslationContext() self.translator = t t.buildannotator().build_types(fn, signature) t.buildrtyper(type_system=self.type_system).specialize() graph = graphof(t, fn) if option.view: t.view() self.original_graph_count = len(t.graphs) # to detect broken intermediate graphs, # we do the loop ourselves instead of calling remove_simple_mallocs() maxiter = 100 mallocv = MallocVirtualizer(t.graphs, t.rtyper, verbose=True) while True: progress = mallocv.remove_mallocs_once() #simplify.transform_dead_op_vars_in_blocks(list(graph.iterblocks())) if progress and option.view: t.view() t.checkgraphs() if expected_result is not DONT_CHECK_RESULT: interp = LLInterpreter(t.rtyper) if not isinstance(expected_result, CHECK_RAISES): res = interp.eval_graph(graph, args) assert res == expected_result else: excinfo = py.test.raises(LLException, interp.eval_graph, graph, args) assert expected_result.excname in str(excinfo.value) if not progress: break maxiter -= 1 assert maxiter > 0, "infinite loop?" self.check_malloc_removed(graph, expected_mallocs, expected_calls) return graph
def translate(func, argtypes, backend_optimize=True): t = TranslationContext() t.buildannotator().build_types(func, argtypes) t.buildrtyper().specialize() if backend_optimize: backend_optimizations(t) return graphof(t, func), t
def makegraph(func, argtypes): t = TranslationContext() t.buildannotator().build_types(func, [int]) t.buildrtyper().specialize() bk = t.annotator.bookkeeper graph = bk.getdesc(func).getuniquegraph() return t, graph
def test_lookup_graphs_abstract(): from pypy.translator.translator import TranslationContext, graphof class A: pass class B(A): def foo(self): pass class C(A): def foo(self): pass def fn(flag): obj = flag and B() or C() obj.foo() return obj t = TranslationContext() t.buildannotator().build_types(fn, [int]) t.buildrtyper(type_system='ootype').specialize() graph = graphof(t, fn) TYPE_A = graph.getreturnvar().concretetype TYPE_B = TYPE_A._subclasses[0] TYPE_C = TYPE_A._subclasses[1] assert len(TYPE_A._lookup_graphs('ofoo')) == 2 assert len(TYPE_B._lookup_graphs('ofoo')) == 1 assert len(TYPE_C._lookup_graphs('ofoo')) == 1
def _build_gen(func, annotation, graph=None, backendopt=True): try: func = func.im_func except AttributeError: pass t = TranslationContext() if graph is not None: graph.func = func ann = t.buildannotator() inputcells = [ann.typeannotation(a) for a in annotation] ann.build_graph_types(graph, inputcells) t.graphs.insert(0, graph) else: ann = t.buildannotator() ann.build_types(func, annotation) if getoption('view'): t.view() t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() if getoption('wd'): tmpdir = py.path.local('.') else: tmpdir = udir return GenCli(tmpdir, t, TestEntryPoint(main_graph, True))
def getcompiled(self, func, argstypelist = [], annotatorpolicy=None): from pypy.config.pypyoption import get_pypy_config config = get_pypy_config(translating=True) config.translation.gc = self.gcpolicy config.translation.thread = self.use_threads if self.stacklessgc: config.translation.gcrootfinder = "stackless" config.translation.simplifying = True t = TranslationContext(config=config) self.t = t a = t.buildannotator(policy=annotatorpolicy) a.build_types(func, argstypelist) t.buildrtyper().specialize() t.checkgraphs() def compile(): cbuilder = CExtModuleBuilder(t, func, config=config) c_source_filename = cbuilder.generate_source( defines = cbuilder.DEBUG_DEFINES) if conftest.option.view: t.view() cbuilder.compile() self._cleanups.append(cbuilder.cleanup) # schedule cleanup after test return cbuilder.get_entry_point(isolated=True) return compile()
def test_counters(self): from pypy.rpython.lltypesystem import lltype from pypy.rpython.lltypesystem.lloperation import llop def entry_point(argv): llop.instrument_count(lltype.Void, 'test', 2) llop.instrument_count(lltype.Void, 'test', 1) llop.instrument_count(lltype.Void, 'test', 1) llop.instrument_count(lltype.Void, 'test', 2) llop.instrument_count(lltype.Void, 'test', 1) return 0 t = TranslationContext(self.config) t.config.translation.instrument = True t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() cbuilder = CStandaloneBuilder(t, entry_point, config=t.config) # xxx cbuilder.generate_source() cbuilder.compile() counters_fname = udir.join("_counters_") os.putenv('_INSTRUMENT_COUNTERS', str(counters_fname)) try: data = cbuilder.cmdexec() finally: os.unsetenv('_INSTRUMENT_COUNTERS') f = counters_fname.open('rb') counters_data = f.read() f.close() import struct counters = struct.unpack("LLL", counters_data) assert counters == (0,3,2)
def get_graph(fn, signature): t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper().specialize() graph = graphof(t, fn) if conftest.option.view: t.view() return graph, t
def compile(self, entry_point): t = TranslationContext(self.config) t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper(type_system='ootype').specialize() cbuilder = CliStandaloneBuilder(t, entry_point, t.config) cbuilder.compile() return t, cbuilder
def rtype(fn, signature): t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper().specialize() graph = graphof(t, fn) if option.view: t.view() return t, graph
def rtype(func, inputtypes, specialize=True): t = TranslationContext() t.buildannotator().build_types(func, inputtypes) if specialize: t.buildrtyper().specialize() if conftest.option.view: t.view() return t
def translate(func, argtypes, backendopt=False): t = TranslationContext() t.buildannotator().build_types(func, argtypes) t.buildrtyper(type_system="ootype").specialize() if backendopt: backend_optimizations(t, merge_if_blocks=True) return t
def get_graph(fn, signature): t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper().specialize() graph = graphof(t, fn) if conftest.option.view: t.view() return graph, t
def rtype(func, inputtypes, specialize=True): t = TranslationContext() t.buildannotator().build_types(func, inputtypes) if specialize: t.buildrtyper().specialize() if conftest.option.view: t.view() return t
def rtype(fn, signature): t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper().specialize() graph = graphof(t, fn) if option.view: t.view() return t, graph
def test_half_exceptiontransformed_graphs(): from pypy.translator import exceptiontransform def f1(x): if x < 0: raise ValueError return 754 def g1(x): try: return f1(x) except ValueError: return 5 def f2(x): if x < 0: raise ValueError return 21 def g2(x): try: return f2(x) except ValueError: return 6 f3 = lltype.functionptr(lltype.FuncType([lltype.Signed], lltype.Signed), 'f3', _callable=f1) def g3(x): try: return f3(x) except ValueError: return 7 def f(flag, x): if flag == 1: return g1(x) elif flag == 2: return g2(x) else: return g3(x) t = TranslationContext() t.buildannotator().build_types(f, [int, int]) t.buildrtyper().specialize() etrafo = exceptiontransform.ExceptionTransformer(t) etrafo.create_exception_handling(graphof(t, f1)) etrafo.create_exception_handling(graphof(t, g2)) etrafo.create_exception_handling(graphof(t, g3)) graph = graphof(t, f) interp = LLInterpreter(t.rtyper) res = interp.eval_graph(graph, [1, -64]) assert res == 5 res = interp.eval_graph(graph, [2, -897]) assert res == 6 res = interp.eval_graph(graph, [3, -9831]) assert res == 7
def translate(func, argtypes, backend_optimize=True): t = TranslationContext() t.buildannotator().build_types(func, argtypes) t.buildrtyper().specialize() if backend_optimize: backend_optimizations(t) if conftest.option.view: t.view() return graphof(t, func), t
def build_adi(function, types): t = TranslationContext() t.buildannotator().build_types(function, types) t.buildrtyper().specialize() adi = AbstractDataFlowInterpreter(t) graph = graphof(t, function) adi.schedule_function(graph) adi.complete() return t, adi, graph
def test_caching_dynamic_deallocator(): S = lltype.GcStruct("S", ('x', lltype.Signed), rtti=True) S1 = lltype.GcStruct("S1", ('s', S), ('y', lltype.Signed), rtti=True) T = lltype.GcStruct("T", ('x', lltype.Signed), rtti=True) def f_S(s): s.x = 1 def f_S1(s1): s1.s.x = 1 s1.y = 2 def f_T(s): s.x = 1 def type_info_S(p): return lltype.getRuntimeTypeInfo(S) def type_info_T(p): return lltype.getRuntimeTypeInfo(T) qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], lltype.Ptr( lltype.RuntimeTypeInfo)), "type_info_S", _callable=type_info_S) dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], lltype.Void), "destructor_funcptr", _callable=f_S) pinf = lltype.attachRuntimeTypeInfo(S, qp, destrptr=dp) dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], lltype.Void), "destructor_funcptr", _callable=f_S1) pinf = lltype.attachRuntimeTypeInfo(S1, qp, destrptr=dp) qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(T)], lltype.Ptr( lltype.RuntimeTypeInfo)), "type_info_S", _callable=type_info_T) dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(T)], lltype.Void), "destructor_funcptr", _callable=f_T) pinf = lltype.attachRuntimeTypeInfo(T, qp, destrptr=dp) def f(): pass t = TranslationContext() t.buildannotator().build_types(f, []) t.buildrtyper().specialize() transformer = RefcountingGCTransformer(t) p_S = transformer.dynamic_deallocation_funcptr_for_type(S) p_S1 = transformer.dynamic_deallocation_funcptr_for_type(S1) p_T = transformer.dynamic_deallocation_funcptr_for_type(T) assert p_S is not p_T assert p_S is p_S1
def test_half_exceptiontransformed_graphs(): from pypy.translator import exceptiontransform def f1(x): if x < 0: raise ValueError return 754 def g1(x): try: return f1(x) except ValueError: return 5 def f2(x): if x < 0: raise ValueError return 21 def g2(x): try: return f2(x) except ValueError: return 6 f3 = lltype.functionptr(lltype.FuncType([lltype.Signed], lltype.Signed), "f3", _callable=f1) def g3(x): try: return f3(x) except ValueError: return 7 def f(flag, x): if flag == 1: return g1(x) elif flag == 2: return g2(x) else: return g3(x) t = TranslationContext() t.buildannotator().build_types(f, [int, int]) t.buildrtyper().specialize() etrafo = exceptiontransform.ExceptionTransformer(t) etrafo.create_exception_handling(graphof(t, f1)) etrafo.create_exception_handling(graphof(t, g2)) etrafo.create_exception_handling(graphof(t, g3)) graph = graphof(t, f) interp = LLInterpreter(t.rtyper) res = interp.eval_graph(graph, [1, -64]) assert res == 5 res = interp.eval_graph(graph, [2, -897]) assert res == 6 res = interp.eval_graph(graph, [3, -9831]) assert res == 7
def build_generator(func, argtypes=[]): context = TranslationContext() context.buildannotator().build_types(func, argtypes) context.buildrtyper(type_system="ootype").specialize() if conftest.option.view: context.view() gen = GenCL(context, func) return gen
def gengraph(f, args=[], viewBefore=False, viewAfter=False, mangle=True): t = TranslationContext() t.config.translation.ootype.mangle = mangle t.buildannotator().build_types(f, args) if viewBefore or conftest.option.view: t.view() t.buildrtyper(type_system="ootype").specialize() if viewAfter or conftest.option.view: t.view() return graphof(t, f)
def build_sqfunc(func, args=[], view=False): try: func = func.im_func except AttributeError: pass t = TranslationContext() t.buildannotator().build_types(func, args) t.buildrtyper(type_system="ootype").specialize() if view or conftest.option.view: t.viewcg() gen = GenSqueak(udir, t) gen.gen()
def gengraph(f, args=[], viewBefore=False, viewAfter=False, mangle=True): t = TranslationContext() t.config.translation.ootype.mangle = mangle t.buildannotator().build_types(f, args) if viewBefore or conftest.option.view: t.view() t.buildrtyper(type_system="ootype").specialize() if viewAfter or conftest.option.view: t.view() return graphof(t, f)
def test_preserve_can_raise(self): def f(x): raise ValueError t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper(type_system=self.type_system).specialize() g = graphof(t, f) etrafo = exceptiontransform.ExceptionTransformer(t) etrafo.create_exception_handling(g) assert etrafo.raise_analyzer.analyze_direct_call(g)
def test_preserve_can_raise(self): def f(x): raise ValueError t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper(type_system=self.type_system).specialize() g = graphof(t, f) etrafo = exceptiontransform.ExceptionTransformer(t) etrafo.create_exception_handling(g) assert etrafo.raise_analyzer.analyze_direct_call(g)
def translateopt(self, func, sig, **optflags): t = TranslationContext() t.buildannotator().build_types(func, sig) t.buildrtyper(type_system=self.type_system).specialize() if conftest.option.view: t.view() backend_optimizations(t, **optflags) if conftest.option.view: t.view() return t
def _makefunc_str_int(cls, func): def main(argv): arg0 = argv[1] arg1 = int(argv[2]) try: res = func(arg0, arg1) except MemoryError: print 'Result: MemoryError' else: print 'Result: "%s"' % (res,) return 0 from pypy.config.pypyoption import get_pypy_config config = get_pypy_config(translating=True) config.translation.gc = cls.gcpolicy config.translation.gcrootfinder = "asmgcc" if sys.platform == 'win32': config.translation.cc = 'mingw32' t = TranslationContext(config=config) a = t.buildannotator() a.build_types(main, [s_list_of_strings]) t.buildrtyper().specialize() t.checkgraphs() cbuilder = CStandaloneBuilder(t, main, config=config) c_source_filename = cbuilder.generate_source( defines = cbuilder.DEBUG_DEFINES) cls._patch_makefile(cbuilder.targetdir) if conftest.option.view: t.view() exe_name = cbuilder.compile() def run(arg0, arg1): lines = [] print >> sys.stderr, 'RUN: starting', exe_name if sys.platform == 'win32': redirect = ' 2> NUL' else: redirect = '' g = os.popen('"%s" %s %d%s' % (exe_name, arg0, arg1, redirect), 'r') for line in g: print >> sys.stderr, 'RUN:', line.rstrip() lines.append(line) g.close() if not lines: py.test.fail("no output from subprocess") if not lines[-1].startswith('Result:'): py.test.fail("unexpected output from subprocess") result = lines[-1][len('Result:'):].strip() if result == 'MemoryError': raise MemoryError("subprocess got an RPython MemoryError") if result.startswith('"') and result.endswith('"'): return result[1:-1] else: return int(result) return run
def build_adi(function, types): t = TranslationContext() t.buildannotator().build_types(function, types) t.buildrtyper().specialize() if conftest.option.view: t.view() adi = AbstractDataFlowInterpreter(t) graph = graphof(t, function) adi.schedule_function(graph) adi.complete() return t, adi, graph
def test_no_multiple_transform(self): def f(x): return x + 1 t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper(type_system=self.type_system).specialize() g = graphof(t, f) etrafo = exceptiontransform.ExceptionTransformer(t) etrafo.create_exception_handling(g) etrafo2 = exceptiontransform.ExceptionTransformer(t) py.test.raises(AssertionError, etrafo2.create_exception_handling, g)
def test_os_setpgrp(self): def entry_point(argv): os.setpgrp() return 0 t = TranslationContext(self.config) t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() cbuilder = CStandaloneBuilder(t, entry_point, t.config) cbuilder.generate_source() cbuilder.compile()
def test_retval_None(): def f(x): pass t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() #t.view() t.checkgraphs() graph = graphof(t, f) assert graph.getreturnvar().concretetype == Void assert graph.startblock.exits[0].args[0].concretetype == Void
def test_del_inheritance(self): from pypy.rlib import rgc class State: pass s = State() s.a_dels = 0 s.b_dels = 0 class A(object): def __del__(self): s.a_dels += 1 class B(A): def __del__(self): s.b_dels += 1 class C(A): pass def f(): A() B() C() A() B() C() rgc.collect() return s.a_dels * 10 + s.b_dels res = f() assert res == 42 t = TranslationContext() t.buildannotator().build_types(f, []) t.buildrtyper().specialize() graph = graphof(t, f) TYPEA = graph.startblock.operations[0].args[0].value RTTIA = getRuntimeTypeInfo(TYPEA) TYPEB = graph.startblock.operations[3].args[0].value RTTIB = getRuntimeTypeInfo(TYPEB) TYPEC = graph.startblock.operations[6].args[0].value RTTIC = getRuntimeTypeInfo(TYPEC) queryptra = RTTIA._obj.query_funcptr # should not raise queryptrb = RTTIB._obj.query_funcptr # should not raise queryptrc = RTTIC._obj.query_funcptr # should not raise destrptra = RTTIA._obj.destructor_funcptr destrptrb = RTTIB._obj.destructor_funcptr destrptrc = RTTIC._obj.destructor_funcptr assert destrptra == destrptrc assert typeOf(destrptra).TO.ARGS[0] != typeOf(destrptrb).TO.ARGS[0] assert destrptra is not None assert destrptrb is not None
def test_find_loop_blocks_simple(): def f(a): if a <= 0: return 1 return f(a - 1) t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) backedges = find_backedges(graph) assert backedges == [] loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 0
def test_simple_lambda(): f = lambda x: x * 2 t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() t.config.translation.countmallocs = True builder = genc.CExtModuleBuilder(t, f, config=t.config) builder.generate_source() builder.compile() f1 = builder.get_entry_point() assert f1(5) == 10
def get_graph(fn, signature, all_opts=True): t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper().specialize() if all_opts: backend_optimizations(t, inline_threshold=INLINE_THRESHOLD_FOR_TEST, constfold=False, raisingop2direct_call=False) graph = graphof(t, fn) if conftest.option.view: t.view() return graph, t
def compile(self, entry_point): t = TranslationContext(self.config) t.config.translation.gc = "semispace" t.config.translation.gcrootfinder = self.gcrootfinder t.config.translation.thread = True t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() # cbuilder = CStandaloneBuilder(t, entry_point, t.config) cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES) cbuilder.compile() # return t, cbuilder
def specialize(self, func, argtypes): from pypy.rpython.llinterp import LLInterpreter t = TranslationContext(list_comprehension_operations=True) t.buildannotator().build_types(func, argtypes) if conftest.option.view: t.view() t.buildrtyper(self.typesystem).specialize() backend_optimizations(t) if conftest.option.view: t.view() graph = graphof(t, func) interp = LLInterpreter(t.rtyper) return interp, graph
def getcompiled(self, func): def main(argv): try: res = func() except MemoryError: print 'Result: MemoryError' else: if isinstance(res, int): print 'Result:', res else: print 'Result: "%s"' % (res,) return 0 from pypy.config.pypyoption import get_pypy_config config = get_pypy_config(translating=True) config.translation.gc = self.gcpolicy config.translation.gcrootfinder = "asmgcc" t = TranslationContext(config=config) self.t = t a = t.buildannotator() a.build_types(main, [s_list_of_strings]) t.buildrtyper().specialize() t.checkgraphs() cbuilder = CStandaloneBuilder(t, main, config=config) c_source_filename = cbuilder.generate_source( defines = cbuilder.DEBUG_DEFINES) self.patch_makefile(cbuilder.targetdir) if conftest.option.view: t.view() exe_name = cbuilder.compile() def run(): lines = [] print >> sys.stderr, 'RUN: starting', exe_name g = os.popen("'%s'" % (exe_name,), 'r') for line in g: print >> sys.stderr, 'RUN:', line.rstrip() lines.append(line) g.close() if not lines: py.test.fail("no output from subprocess") if not lines[-1].startswith('Result:'): py.test.fail("unexpected output from subprocess") result = lines[-1][len('Result:'):].strip() if result == 'MemoryError': raise MemoryError("subprocess got an RPython MemoryError") if result.startswith('"') and result.endswith('"'): return result[1:-1] else: return int(result) return run
def test_find_loop_blocks(): def f(k): result = 0 for i in range(k): result += 1 for j in range(k): result += 1 return result t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 4
def analyze(self, func, sig, func_to_analyze=None, backendopt=False): if func_to_analyze is None: func_to_analyze = func t = TranslationContext() t.buildannotator().build_types(func, sig) t.buildrtyper(type_system=self.type_system).specialize() if backendopt: backend_optimizations(t) if option.view: t.view() a = FinalizerAnalyzer(t) fgraph = graphof(t, func_to_analyze) result = a.analyze_light_finalizer(fgraph) return result
def compile(self, entry_point, debug=True): t = TranslationContext(self.config) t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() cbuilder = CStandaloneBuilder(t, entry_point, t.config) if debug: cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES) else: cbuilder.generate_source() cbuilder.compile() if option.view: t.view() return t, cbuilder