def test_ll_calling_ll2(): import test_llann tst = test_llann.TestLowLevelAnnotateTestCase() a, vTs = tst.test_ll_calling_ll2() rt = RPythonTyper(a) rt.specialize() assert [vT.concretetype for vT in vTs] == [Void] * 3
def test_implicit_cast(self): z = llexternal('z', [USHORT, ULONG, USHORT, DOUBLE], USHORT) def f(x, y, xx, yy): return z(x, y, xx, yy) a = RPythonAnnotator() r = a.build_types(f, [int, int, int, int]) rtyper = RPythonTyper(a) rtyper.specialize() a.translator.rtyper = rtyper backend_optimizations(a.translator) if option.view: a.translator.view() graph = graphof(a.translator, f) s = summary(graph) # there should be not too many operations here by now expected = { 'cast_int_to_uint': 1, 'direct_call': 1, 'cast_primitive': 2, 'cast_int_to_float': 1 } for k, v in expected.items(): assert s[k] == v
def test_raw_free(self): def f(addr): raw_free(addr) a = RPythonAnnotator() s = a.build_types(f, [annmodel.SomeAddress()]) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise
def test_raw_malloc(self): def f(): return raw_malloc(100) a = RPythonAnnotator() s = a.build_types(f, []) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise
def test_isinstance(): class A: _alloc_flavor_ = "raw" class B(A): pass class C(B): pass def f(i): if i == 0: o = None elif i == 1: o = A() elif i == 2: o = B() else: o = C() return 100*isinstance(o, A)+10*isinstance(o, B)+1*isinstance(o ,C) a = RPythonAnnotator() #does not raise: s = a.build_types(f, [int]) assert s.knowntype == int rtyper = RPythonTyper(a) rtyper.specialize() res = interpret(f, [1]) assert res == 100 res = interpret(f, [2]) assert res == 110 res = interpret(f, [3]) assert res == 111 res = interpret(f, [0]) assert res == 0
def test_reprkeys_dont_clash(): stup1 = annmodel.SomeTuple((annmodel.SomeFloat(), annmodel.SomeInteger())) stup2 = annmodel.SomeTuple((annmodel.SomeString(), annmodel.SomeInteger())) rtyper = RPythonTyper(annrpython.RPythonAnnotator(None)) key1 = rtyper.makekey(stup1) key2 = rtyper.makekey(stup2) assert key1 != key2
def test_memcopy(self): def f(addr1, addr2): raw_memcopy(addr1, addr2, 100) a = RPythonAnnotator() #does not raise: s = a.build_types(f, [annmodel.SomeAddress(), annmodel.SomeAddress()]) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise
def test_memory_access(self): def f(offset, value): addr = raw_malloc(offset * 2 + 1) addr.signed[offset] = value return addr.signed[offset] a = RPythonAnnotator() s = a.build_types(f, [annmodel.SomeInteger(), annmodel.SomeInteger()]) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise
def test_address_comparison(self): def f(offset): return NULL < NULL + offset a = RPythonAnnotator() s = a.build_types(f, [annmodel.SomeInteger()]) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise graph = graphof(a.translator, f) assert graph.startblock.operations[0].result.concretetype == Address
def test_null(self): def f(): return NULL a = RPythonAnnotator() s = a.build_types(f, []) rtyper = RPythonTyper(a) rtyper.specialize() rtyp = graphof(a.translator, f).returnblock.inputargs[0].concretetype assert rtyp == Address
def ll_rtype(llfn, argtypes=[]): a = RPythonAnnotator() graph = annotate_lowlevel_helper(a, llfn, argtypes) s = a.binding(graph.getreturnvar()) t = a.translator typer = RPythonTyper(a) typer.specialize() #t.view() t.checkgraphs() return s, t
def test_address_arithmetic(self): def f(offset, char): addr = raw_malloc(10000) same_offset = (addr + offset) - addr addr.char[offset] = char return (addr + same_offset).char[0] a = RPythonAnnotator() s = a.build_types(f, [annmodel.SomeInteger(), annmodel.SomeChar()]) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise
def test_isinstance(): class A(object): _alloc_flavor_ = "raw" class B(A): pass class C(B): pass def f(i): if i == 0: o = None elif i == 1: o = A() elif i == 2: o = B() else: o = C() res = 100 * isinstance(o, A) + 10 * isinstance(o, B) + 1 * isinstance( o, C) if i == 0: pass elif i == 1: assert isinstance(o, A) free_non_gc_object(o) elif i == 2: assert isinstance(o, B) free_non_gc_object(o) else: assert isinstance(o, C) free_non_gc_object(o) return res assert f(1) == 100 assert f(2) == 110 assert f(3) == 111 assert f(0) == 0 a = RPythonAnnotator() #does not raise: s = a.build_types(f, [int]) assert s.knowntype == int rtyper = RPythonTyper(a) rtyper.specialize() res = interpret(f, [1]) assert res == 100 res = interpret(f, [2]) assert res == 110 res = interpret(f, [3]) assert res == 111 res = interpret(f, [0]) assert res == 0
def test_addr_as_bool(self): def f(addr1, addr2): if addr1: return 1 else: if not addr2: return 0 else: return -1 a = RPythonAnnotator() #does not raise: s = a.build_types(f, [annmodel.SomeAddress(), annmodel.SomeAddress()]) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise
def test_isinstance(): class A(object): _alloc_flavor_ = "raw" class B(A): pass class C(B): pass def f(i): if i == 0: o = None elif i == 1: o = A() elif i == 2: o = B() else: o = C() res = 100*isinstance(o, A) + 10*isinstance(o, B) + 1*isinstance(o, C) if i == 0: pass elif i == 1: assert isinstance(o, A) free_non_gc_object(o) elif i == 2: assert isinstance(o, B) free_non_gc_object(o) else: assert isinstance(o, C) free_non_gc_object(o) return res assert f(1) == 100 assert f(2) == 110 assert f(3) == 111 assert f(0) == 0 a = RPythonAnnotator() #does not raise: s = a.build_types(f, [int]) assert s.knowntype == int rtyper = RPythonTyper(a) rtyper.specialize() res = interpret(f, [1]) assert res == 100 res = interpret(f, [2]) assert res == 110 res = interpret(f, [3]) assert res == 111 res = interpret(f, [0]) assert res == 0
def test_alloc_flavor(): class A: _alloc_flavor_ = "raw" def f(): return A() a = RPythonAnnotator() #does not raise: s = a.build_types(f, []) Adef = a.bookkeeper.getuniqueclassdef(A) assert s.knowntype == Adef rtyper = RPythonTyper(a) rtyper.specialize() assert (Adef, 'raw') in rtyper.instance_reprs assert (Adef, 'gc') not in rtyper.instance_reprs
def test_c_callback_with_void_arg_3(self): import pypy def f(i): x = 'X' * i return x[-2] a = RPythonAnnotator() r = a.build_types(f, [int]) rtyper = RPythonTyper(a) rtyper.specialize() a.translator.rtyper = rtyper graph = a.translator.graphs[0] op = graph.startblock.operations[-1] assert op.opname == 'direct_call' assert op.args[0].value._obj._callable == pypy.rpython.lltypesystem.rstr.LLHelpers.ll_stritem.im_func assert op.args[1].value == pypy.rpython.lltypesystem.rstr.LLHelpers assert op.args[3].value == -2
def buildrtyper(self, type_system="lltype"): if self.annotator is None: raise ValueError("no annotator") if self.rtyper is not None: raise ValueError("we already have an rtyper") from pypy.rpython.rtyper import RPythonTyper self.rtyper = RPythonTyper(self.annotator, type_system=type_system) return self.rtyper
def test_is(): class A: _alloc_flavor_ = "raw" pass class B(A): pass class C: _alloc_flavor_ = "raw" def f(i): a = A() b = B() c = C() d = None e = None if i == 0: d = a elif i == 1: d = b elif i == 2: e = c res = (0x0001 * (a is b) | 0x0002 * (a is c) | 0x0004 * (a is d) | 0x0008 * (a is e) | 0x0010 * (b is c) | 0x0020 * (b is d) | 0x0040 * (b is e) | 0x0080 * (c is d) | 0x0100 * (c is e) | 0x0200 * (d is e)) free_non_gc_object(a) free_non_gc_object(b) free_non_gc_object(c) return res a = RPythonAnnotator() #does not raise: s = a.build_types(f, [int]) assert s.knowntype == int rtyper = RPythonTyper(a) rtyper.specialize() res = interpret(f, [0]) assert res == 0x0004 res = interpret(f, [1]) assert res == 0x0020 res = interpret(f, [2]) assert res == 0x0100 res = interpret(f, [3]) assert res == 0x0200
def annotate_rtype_gc(self): # annotate and specialize functions gc_class = self.gc.__class__ AddressLinkedList = self.AddressLinkedList def instantiate_linked_list(): return AddressLinkedList() f1, f2, f3, f4, f5, f6, f7, f8 = self.query_types.create_query_functions() the_gc = gc_class(AddressLinkedList) def instantiate_gc(): the_gc.set_query_functions(f1, f2, f3, f4, f5, f6, f7, f8) the_gc.setup() return the_gc func, dummy_get_roots1, dummy_get_roots2 = gc.get_dummy_annotate( the_gc, self.AddressLinkedList) self.gc.get_roots = dummy_get_roots1 a = RPythonAnnotator() a.build_types(instantiate_gc, []) a.build_types(func, []) a.build_types(instantiate_linked_list, []) typer = RPythonTyper(a) typer.specialize() self.annotator = a # convert constants fgcc = FlowGraphConstantConverter(a.translator.graphs) fgcc.convert() self.malloc_graph = a.bookkeeper.getdesc(self.gc.malloc.im_func).getuniquegraph() self.write_barrier_graph = a.bookkeeper.getdesc(self.gc.write_barrier.im_func).getuniquegraph() # create a gc via invoking instantiate_gc self.gcptr = self.llinterp.eval_graph( a.bookkeeper.getdesc(instantiate_gc).getuniquegraph()) GETROOTS_FUNCTYPE = lltype.typeOf( getfunctionptr(a, dummy_get_roots1)).TO setattr(self.gcptr, "inst_get_roots", lltypesimulation.functionptr(GETROOTS_FUNCTYPE, "get_roots", _callable=self.get_roots)) #get funcptrs neccessary to build the result of get_roots self.instantiate_linked_list = getfunctionptr( a, instantiate_linked_list) self.append_linked_list = getfunctionptr( a, AddressLinkedList.append.im_func) self.pop_linked_list = getfunctionptr( a, AddressLinkedList.pop.im_func) self.gc.get_roots = None self.translator = a.translator
def test_rtype_nongc_object(): class TestClass(object): _alloc_flavor_ = "raw" def __init__(self, a): self.a = a def method1(self): return self.a def malloc_and_free(a): ci = TestClass(a) b = ci.method1() free_non_gc_object(ci) return b a = RPythonAnnotator() #does not raise: s = a.build_types(malloc_and_free, [annmodel.SomeAddress()]) assert isinstance(s, annmodel.SomeAddress) rtyper = RPythonTyper(a) rtyper.specialize()
def test_c_callback_with_void_arg_3(self): import pypy def f(i): x = 'X' * i return x[-2] a = RPythonAnnotator() r = a.build_types(f, [int]) rtyper = RPythonTyper(a) rtyper.specialize() a.translator.rtyper = rtyper graph = a.translator.graphs[0] op = graph.startblock.operations[-1] assert op.opname == 'direct_call' assert op.args[ 0].value._obj._callable == pypy.rpython.lltypesystem.rstr.LLHelpers.ll_stritem.im_func assert op.args[1].value == pypy.rpython.lltypesystem.rstr.LLHelpers assert op.args[3].value == -2
def test_alloc_flavor_subclassing(): class A: _alloc_flavor_ = "raw" class B(A): def __init__(self, a): self.a = a def f(): return B(0) a = RPythonAnnotator() #does not raise: s = a.build_types(f, []) Adef = a.bookkeeper.getuniqueclassdef(A) Bdef = a.bookkeeper.getuniqueclassdef(B) assert s.knowntype == Bdef rtyper = RPythonTyper(a) rtyper.specialize() assert (Adef, 'raw') in rtyper.instance_reprs assert (Adef, 'gc') not in rtyper.instance_reprs assert (Bdef, 'raw') in rtyper.instance_reprs assert (Bdef, 'gc') not in rtyper.instance_reprs
def test_is(): class A: _alloc_flavor_ = "raw" pass class B(A): pass class C: _alloc_flavor_ = "raw" def f(i): a = A() b = B() c = C() d = None e = None if i == 0: d = a elif i == 1: d = b elif i == 2: e = c res = (0x0001*(a is b) | 0x0002*(a is c) | 0x0004*(a is d) | 0x0008*(a is e) | 0x0010*(b is c) | 0x0020*(b is d) | 0x0040*(b is e) | 0x0080*(c is d) | 0x0100*(c is e) | 0x0200*(d is e)) free_non_gc_object(a) free_non_gc_object(b) free_non_gc_object(c) return res a = RPythonAnnotator() #does not raise: s = a.build_types(f, [int]) assert s.knowntype == int rtyper = RPythonTyper(a) rtyper.specialize() res = interpret(f, [0]) assert res == 0x0004 res = interpret(f, [1]) assert res == 0x0020 res = interpret(f, [2]) assert res == 0x0100 res = interpret(f, [3]) assert res == 0x0200
def test_implicit_cast(self): z = llexternal('z', [USHORT, ULONG, USHORT, DOUBLE], USHORT, sandboxsafe=True) # to allow the wrapper to be inlined def f(x, y, xx, yy): return z(x, y, xx, yy) a = RPythonAnnotator() r = a.build_types(f, [int, int, int, int]) rtyper = RPythonTyper(a) rtyper.specialize() a.translator.rtyper = rtyper backend_optimizations(a.translator) if option.view: a.translator.view() graph = graphof(a.translator, f) s = summary(graph) # there should be not too many operations here by now expected = {'force_cast': 3, 'cast_int_to_float': 1, 'direct_call': 1} for k, v in expected.items(): assert s[k] == v
def test_unsupported(): class A: _alloc_flavor_ = "raw" def f(): return str(A()) a = RPythonAnnotator() #does not raise: s = a.build_types(f, []) assert s.knowntype == str rtyper = RPythonTyper(a) py.test.raises(TypeError, rtyper.specialize) # results in an invalid cast
def test_isinstance(): class A: _alloc_flavor_ = "raw" class B(A): pass class C(B): pass def f(i): if i == 0: o = None elif i == 1: o = A() elif i == 2: o = B() else: o = C() return 100 * isinstance(o, A) + 10 * isinstance(o, B) + 1 * isinstance( o, C) a = RPythonAnnotator() #does not raise: s = a.build_types(f, [int]) assert s.knowntype == int rtyper = RPythonTyper(a) rtyper.specialize() res = interpret(f, [1]) assert res == 100 res = interpret(f, [2]) assert res == 110 res = interpret(f, [3]) assert res == 111 res = interpret(f, [0]) assert res == 0
def test_simple_offsetof(self): from pypy.rpython.lltypesystem import lltype from pypy.rpython.lltypesystem.llmemory import offsetof S = lltype.GcStruct('S', ('x', lltype.Bool), ('y', lltype.Signed)) def f(): return offsetof(S, 'x') f() a = RPythonAnnotator() s = a.build_types(f, []) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise coff = offsetof(S, 'y') def f(): return coff f() a = RPythonAnnotator() s = a.build_types(f, []) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise
def test_offset_addition(self): from pypy.rpython.lltypesystem import lltype from pypy.rpython.lltypesystem.llmemory import offsetof S = lltype.Struct('S', ('x', lltype.Bool), ('y', lltype.Signed)) T = lltype.GcStruct('T', ('r', lltype.Float), ('s1', S), ('s2', S)) def f(): return offsetof(T, 's1') + offsetof(S, 'x') f() a = RPythonAnnotator() s = a.build_types(f, []) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise coff = offsetof(T, 's2') + offsetof(S, 'y') def f(): return coff f() a = RPythonAnnotator() s = a.build_types(f, []) rtyper = RPythonTyper(a) rtyper.specialize() #does not raise