def test_annotate_byval(self): t = TranslationContext() a = t.buildannotator() s = a.build_types(test_testfunc_byval, []) if conftest.option.view: t.view() assert s.knowntype == int
def test_runtime_type_info(): S = GcStruct('s', ('is_actually_s1', Bool), rtti=True) S1 = GcStruct('s1', ('sub', S), rtti=True) def rtti_S(p): if p.is_actually_s1: return getRuntimeTypeInfo(S1) else: return getRuntimeTypeInfo(S) def rtti_S1(p): return getRuntimeTypeInfo(S1) def does_stuff(): p = malloc(S) p.is_actually_s1 = False p1 = malloc(S1) p1.sub.is_actually_s1 = True # and no crash when p and p1 are decref'ed return None t = TranslationContext() t.buildannotator().build_types(does_stuff, []) rtyper = t.buildrtyper() rtyper.attachRuntimeTypeInfoFunc(S, rtti_S) rtyper.attachRuntimeTypeInfoFunc(S1, rtti_S1) rtyper.specialize() #t.view() from pypy.translator.c import genc t.config.translation.countmallocs = True builder = genc.CExtModuleBuilder(t, does_stuff, config=t.config) builder.generate_source() builder.compile() f1 = builder.get_entry_point() f1() mallocs, frees = builder.get_malloc_counters()() assert mallocs == frees
def hannotate(func, argtypes, policy=P_DEFAULT, annotator=False, inline=None, backendoptimize=False): # build the normal ll graphs for ll_function t = TranslationContext() a = t.buildannotator() a.build_types(func, argtypes) rtyper = t.buildrtyper() rtyper.specialize() if inline: auto_inlining(t, threshold=inline) if backendoptimize: from pypy.translator.backendopt.all import backend_optimizations backend_optimizations(t) graph1 = graphof(t, func) # build hint annotator types hannotator = HintAnnotator(base_translator=t, policy=policy) hs = hannotator.build_types(graph1, [SomeLLAbstractConstant(v.concretetype, {OriginFlags(): True}) for v in graph1.getargs()]) hannotator.simplify() t = hannotator.translator if conftest.option.view: t.view() if annotator: return hs, hannotator else: return hs
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 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 getcompiled(self, func): t = TranslationContext() # builds starting-types from func_defs argstypelist = [] if func.func_defaults: for spec in func.func_defaults: if isinstance(spec, tuple): spec = spec[0] # use the first type only for the tests argstypelist.append(spec) t.buildannotator().build_types(func, argstypelist) name = func.func_name blobs = [] for graph in t.graphs: g = GenPyrex(graph) g.by_the_way_the_function_was = graph.func # XXX g.setannotator(t.annotator) blobs.append(g.emitcode()) code = g.globaldeclarations() # any 'g' is fine here... if code: blobs.insert(0, code) pyxcode = '\n\n#_________________\n\n'.join(blobs) mod = make_module_from_pyxstring(name, udir, pyxcode) return getattr(mod, name)
def test_wrong_startblock_incref(): class B(object): pass def g(b): while True: b.x -= 10 if b.x < 0: return b.x def f(n): b = B() b.x = n return g(b) # XXX obscure: remove the first empty block in the graph of 'g' t = TranslationContext() graph = t.buildflowgraph(g) assert graph.startblock.operations == [] graph.startblock = graph.startblock.exits[0].target from pypy.objspace.flow.model import checkgraph checkgraph(graph) t._prebuilt_graphs[g] = graph fn = compile_func(f, [int], t) res = fn(112) assert res == -8
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 test_del_basic(): for gcpolicy in ["ref"]: #, "framework"]: S = lltype.GcStruct('S', ('x', lltype.Signed), rtti=True) TRASH = lltype.GcStruct('TRASH', ('x', lltype.Signed)) GLOBAL = lltype.Struct('GLOBAL', ('x', lltype.Signed)) glob = lltype.malloc(GLOBAL, immortal=True) def destructor(s): glob.x = s.x + 1 def type_info_S(s): return lltype.getRuntimeTypeInfo(S) def g(n): s = lltype.malloc(S) s.x = n # now 's' should go away def entrypoint(n): g(n) # llop.gc__collect(lltype.Void) return glob.x t = TranslationContext() t.buildannotator().build_types(entrypoint, [int]) rtyper = t.buildrtyper() destrptr = rtyper.annotate_helper_fn(destructor, [lltype.Ptr(S)]) rtyper.attachRuntimeTypeInfoFunc(S, type_info_S, destrptr=destrptr) rtyper.specialize() fn = compile_func(entrypoint, None, t, gcpolicy=gcpolicy) res = fn(123) assert res == 124
def test_merge_several(): def merge(n, m): r = -1 if n == 0: if m == 0: r = 0 elif m == 1: r = 1 else: r = 2 elif n == 1: r = 4 else: r = 6 return r t = TranslationContext() a = t.buildannotator() a.build_types(merge, [int, int]) rtyper = t.buildrtyper() rtyper.specialize() graph = tgraphof(t, merge) remove_same_as(graph) merge_if_blocks(graph) assert len(graph.startblock.exits) == 3 assert len(list(graph.iterblocks())) == 3 interp = LLInterpreter(rtyper) for m in range(3): res = interp.eval_graph(graph, [0, m]) assert res == m res = interp.eval_graph(graph, [1, 0]) assert res == 4 res = interp.eval_graph(graph, [2, 0]) assert res == 6
def test_replace_exitswitch_by_constant_bug(): class X: pass def constant9(): x = X() x.n = 3 x.n = 9 return x.n def fn(): n = constant9() if n == 1: return 5 elif n == 2: return 6 elif n == 3: return 8 elif n == 4: return -123 elif n == 5: return 12973 else: return n t = TranslationContext() a = t.buildannotator() a.build_types(fn, []) rtyper = t.buildrtyper() rtyper.specialize() graph = t.graphs[0] remove_same_as(graph) merge_if_blocks_once(graph) from pypy.translator.backendopt import malloc, inline inline.auto_inlining(t, 20) malloc.remove_mallocs(t, t.graphs) from pypy.translator import simplify simplify.join_blocks(graph)
def rtype(self, fn, argtypes, resulttype, checkfunction=None): t = TranslationContext() a = t.buildannotator() a.build_types(prefn, [int]) typer = t.buildrtyper() typer.specialize() #t.view() s_result = a.typeannotation(resulttype) from pypy.rpython import annlowlevel # annotate, normalize and rtype fn after the fact annhelper = annlowlevel.MixLevelHelperAnnotator(typer) graph = annhelper.getgraph(fn, [a.typeannotation(argtype) for argtype in argtypes], s_result) annhelper.finish() t.checkgraphs() if checkfunction is not None: checkfunction(t) # sanity check prefn llinterp = LLInterpreter(typer) res = llinterp.eval_graph(graphof(t, prefn), [1]) assert res == 100 res = llinterp.eval_graph(graphof(t, prefn), [2]) assert res == 201 return 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_type_erase_var_size(self): class A(object): pass class B(object): pass def f(): la = [A()] lb = [B()] la.append(None) lb.append(None) return la, lb t = TranslationContext() s = t.buildannotator().build_types(f, []) rtyper = t.buildrtyper(type_system=self.type_system) rtyper.specialize() s_A_list = s.items[0] s_B_list = s.items[1] r_A_list = rtyper.getrepr(s_A_list) assert isinstance(r_A_list, self.rlist.ListRepr) r_B_list = rtyper.getrepr(s_B_list) assert isinstance(r_B_list, self.rlist.ListRepr) assert r_A_list.lowleveltype == r_B_list.lowleveltype
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 test_pseudohighlevelcallable(): t = TranslationContext() t.buildannotator() rtyper = t.buildrtyper() rtyper.specialize() a = MixLevelHelperAnnotator(rtyper) class A: value = 5 def double(self): return self.value * 2 def fn1(a): a2 = A() a2.value = a.double() return a2 s_A, r_A = a.s_r_instanceof(A) fn1ptr = a.delayedfunction(fn1, [s_A], s_A) pseudo = PseudoHighLevelCallable(fn1ptr, [s_A], s_A) def fn2(n): a = A() a.value = n a2 = pseudo(a) return a2.value graph = a.getgraph(fn2, [annmodel.SomeInteger()], annmodel.SomeInteger()) a.finish() llinterp = LLInterpreter(rtyper) res = llinterp.eval_graph(graph, [21]) assert res == 42
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 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 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 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 test_annotate_array_access_float(self): t = TranslationContext() a = t.buildannotator() s = a.build_types(access_array, [float]) assert s.knowntype == float if conftest.option.view: t.view()
def test_annotate_int2addr(self): def fn(): return c_void_p(123) t = TranslationContext() a = t.buildannotator() s = a.build_types(fn, []) assert s.knowntype == c_void_p
def test_oswrite(): def f(): import os os.write(1,"o") t = TranslationContext() s = t.buildannotator().build_types(f, []) rtyper = t.buildrtyper(type_system="lltype") rtyper.specialize()
def test_annotate_array_dtype(self): def f(): a1 = numpy.array([1, 2], dtype="d") return a1 t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert s.typecode == "d"
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_annotate_variants(self): func, expected = maketest() assert func() == expected t = TranslationContext() a = t.buildannotator() s = a.build_types(func, []) if conftest.option.view: a.translator.view() assert s.knowntype == int
def test_annotate_array_add_scalar_coerce(self): def f(): a = numpy.array([1, 2]) return a + 3.0 t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert s.typecode == "d"
def test_annotate_array_attr(self): def fget(): a1 = numpy.array([1, 2]) return a1.shape t = TranslationContext() a = t.buildannotator() s = a.build_types(fget, []) assert type(s) == SomeTuple
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 test_annotate_r_dict(): t = TranslationContext() a = t.buildannotator() a.build_types(test_r_dict, []) #t.view() graph = graphof(t, strange_key_eq) assert a.binding(graph.getargs()[0]).knowntype == str assert a.binding(graph.getargs()[1]).knowntype == str graph = graphof(t, strange_key_hash) assert a.binding(graph.getargs()[0]).knowntype == str
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 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 test_remove_identical_variables(): def g(code): pc = 0 while pc < len(code): pc += 1 return pc graph = TranslationContext().buildflowgraph(g) for block in graph.iterblocks(): assert len(block.inputargs) <= 2 # at most 'pc' and 'code'
def test_annotate_attr(self): def f(): a = numpy.empty((3, 4, 5)) return a.ndim t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert isinstance(s, SomeInteger) def f(): a = numpy.empty((3, 4, 5)) return a.shape t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert isinstance(s, SomeTuple) def f(): a = numpy.empty((3, 4, 5)) return a.dtype t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert isinstance(s, SomeChar)
def rcompile(rgenop, entrypoint, argtypes, random_seed=0, type_system='lltype'): from pypy.translator.translator import TranslationContext from pypy.annotation.policy import AnnotatorPolicy from pypy import conftest t = TranslationContext() policy = AnnotatorPolicy() policy.allow_someobjects = False t.buildannotator(policy=policy).build_types(entrypoint, argtypes) t.buildrtyper(type_system=type_system).specialize() # note that backend optimizations will constant-fold simple operations, # which is required by some backends that don't accept calls like # genop1("add", constant, constant). from pypy.translator.backendopt.all import backend_optimizations backend_optimizations(t) if conftest.option.view: t.view() entrygraph = t._graphof(entrypoint) return compile_graph(rgenop, entrygraph, random_seed=random_seed)
def test_annotate_indexing(self): def f(): a = numpy.empty((4, 3), dtype='i') c = a[:, 0] return c t = TranslationContext() a = t.buildannotator() s_array = a.build_types(f, []) assert type(s_array) == SomeArray assert s_array.ndim == 1
def annotatefunc(self, func, argtypes=None): from pypy.config.pypyoption import get_pypy_config config = get_pypy_config(translating=True) config.translation.gc = "ref" config.translation.simplifying = True t = TranslationContext(config=config) if argtypes is None: argtypes = [] a = t.buildannotator() a.build_types(func, argtypes) a.simplify() return t
def test_annotate_array_access_bytype(self): def access_array_bytype(dummy): my_array = numpy.array([1], 'd') return my_array[0] t = TranslationContext() a = t.buildannotator() s = a.build_types(access_array_bytype, [int]) assert s.knowntype == float if conftest.option.view: t.view()
def test_annotate_broadcast(self): def f(): a = numpy.empty((4, 3), dtype='i') b = numpy.array([33]) a[:] = b return a t = TranslationContext() a = t.buildannotator() s_array = a.build_types(f, []) assert type(s_array) == SomeArray assert s_array.ndim == 2
def getgraph(f, argtypes): from pypy.translator.translator import TranslationContext, graphof from pypy.translator.backendopt.all import backend_optimizations t = TranslationContext() a = t.buildannotator() typer = t.buildrtyper() a.build_types(f, argtypes) typer.specialize() backend_optimizations(t) graph = graphof(t, f) if option.view: graph.show() return graph
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 test_annotate_array_access_variable(self): def access_with_variable(): my_array = numpy.array(range(10)) my_array[2] = 2 sum = 0 for idx in range(10): sum += my_array[idx] return sum t = TranslationContext() a = t.buildannotator() s = a.build_types(access_with_variable, []) assert s.knowntype == rffi.r_int
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 gengraph(func, argtypes=[], viewbefore='auto', policy=None, type_system="lltype", backendopt=False, config=None, **extraconfigopts): t = TranslationContext(config=config) t.config.set(**extraconfigopts) a = t.buildannotator(policy=policy) timelog("annotating", a.build_types, func, argtypes, main_entry_point=True) if viewbefore == 'auto': viewbefore = getattr(conftest.option, 'view', False) if viewbefore: a.simplify() t.view() global typer # we need it for find_exception typer = t.buildrtyper(type_system=type_system) timelog("rtyper-specializing", typer.specialize) #t.view() timelog("checking graphs", t.checkgraphs) if backendopt: from pypy.translator.backendopt.all import backend_optimizations backend_optimizations(t) timelog("checking graphs", t.checkgraphs) if viewbefore: t.view() desc = t.annotator.bookkeeper.getdesc(func) graph = desc.specialize(argtypes) return t, typer, graph
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 make_deallocator(TYPE, attr="static_deallocation_funcptr_for_type", cls=RefcountingGCTransformer): if TYPE._is_varsize(): def f(): return lltype.malloc(TYPE, 1) else: def f(): return lltype.malloc(TYPE) t = TranslationContext() t.buildannotator().build_types(f, []) t.buildrtyper().specialize() transformer = cls(t) fptr = getattr(transformer, attr)(TYPE) transformer.transform_graph(graphof(t, f)) transformer.finish(backendopt=False) if conftest.option.view: t.view() if fptr: return fptr._obj.graph, t else: return None, t
def test_identity_hash_is_fast(self): class A(object): pass def f(): return {A(): 1} t = TranslationContext() s = t.buildannotator().build_types(f, []) rtyper = t.buildrtyper() rtyper.specialize() r_dict = rtyper.getrepr(s) assert not hasattr(r_dict.lowleveltype.TO.entries.TO.OF, "f_hash")
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 test_stackless(): t = TranslationContext() a = t.buildannotator() a.build_types(g, [int]) a.simplify() t.buildrtyper().specialize() backend_optimizations(t) t.checkgraphs() n = insert_ll_stackcheck(t) t.checkgraphs() assert n == 1 t.config.translation.stackless = True stacklesstransf = StacklessTransformer(t, g) f_graph = graphof(t, f) stacklesstransf.transform_graph(f_graph) if conftest.option.view: f_graph.show() exctransf = t.getexceptiontransformer() exctransf.create_exception_handling(f_graph) if conftest.option.view: f_graph.show() check(f_graph, 'f', 'fetch_retval_void') class GCTransform(framework.FrameworkGCTransformer): from pypy.rpython.memory.gc.generation import GenerationGC as \ GCClass GC_PARAMS = {} gctransf = GCTransform(t) gctransf.transform_graph(f_graph) if conftest.option.view: f_graph.show() relevant = check(f_graph, 'f', 'fetch_retval_void') for p in relevant: in_between = False reload = 0 for spaceop in p: if spaceop.opname == 'direct_call': target = direct_target(spaceop) if target == 'f': in_between = False elif target == 'stack_check___': in_between = True if in_between and spaceop.opname == 'gc_reload_possibly_moved': reload += 1 assert reload == 1
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 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 test_annotate_indexing(self): def f(): a = numpy.empty((3, 4, 5)) b = a[0] a[0, 1, 2] = 1. b[0, 1] = 2. b[:] = a[1] b[:, :] = a[1] b[0, :] = a[1, 2] return b t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert s.ndim == 2
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 transform_func(self, fn, inputtypes, backendopt=False): t = TranslationContext() t.buildannotator().build_types(fn, inputtypes) t.buildrtyper(type_system=self.type_system).specialize() if conftest.option.view: t.view() if backendopt: backend_optimizations(t) g = graphof(t, fn) etrafo = exceptiontransform.ExceptionTransformer(t) etrafo.create_exception_handling(g) join_blocks(g) if conftest.option.view: t.view() return t, g
def test_two_constants(): def fn(): r = range(10, 37, 4) r.reverse() return r[0] t = TranslationContext() a = t.buildannotator() a.build_types(fn, []) rtyper = t.buildrtyper() rtyper.specialize() backend_optimizations(t, merge_if_blocks=True) graph = tgraphof(t, fn) blocknum = len(list(graph.iterblocks())) merge_if_blocks(graph) assert blocknum == len(list(graph.iterblocks()))
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_listtype_explosion(self): def f(x): l1 = [x] l2 = [x] return l1, l2 t = TranslationContext() a = t.buildannotator() s = a.build_types(f, [int]) typer = t.buildrtyper(type_system="ootype") typer.specialize() s_l1, s_l2 = s.items r_l1 = typer.getrepr(s_l1) r_l2 = typer.getrepr(s_l2) assert r_l1.lowleveltype == r_l2.lowleveltype
def test_tupletype_explosion(self): def f(x): t1 = ([x], [x, x]) t2 = ([x, x], [x]) return t1, t2 t = TranslationContext() a = t.buildannotator() s = a.build_types(f, [int]) typer = t.buildrtyper(type_system="ootype") typer.specialize() s_t1, s_t2 = s.items r_t1 = typer.getrepr(s_t1) r_t2 = typer.getrepr(s_t2) assert r_t1.lowleveltype == r_t2.lowleveltype