Exemplo n.º 1
0
def test_repr_of_descr():
    c0 = GcCache(False)
    T = lltype.GcStruct('T')
    S = lltype.GcStruct('S', ('x', lltype.Char), ('y', lltype.Ptr(T)),
                        ('z', lltype.Ptr(T)))
    descr1 = get_size_descr(c0, S)
    s = symbolic.get_size(S, False)
    assert descr1.repr_of_descr() == '<SizeDescr %d>' % s
    #
    descr2 = get_field_descr(c0, S, 'y')
    o, _ = symbolic.get_field_token(S, 'y', False)
    assert descr2.repr_of_descr() == '<GcPtrFieldDescr %d>' % o
    #
    descr2i = get_field_descr(c0, S, 'x')
    o, _ = symbolic.get_field_token(S, 'x', False)
    assert descr2i.repr_of_descr() == '<CharFieldDescr %d>' % o
    #
    descr3 = get_array_descr(c0, lltype.GcArray(lltype.Ptr(S)))
    assert descr3.repr_of_descr() == '<GcPtrArrayDescr>'
    #
    descr3i = get_array_descr(c0, lltype.GcArray(lltype.Char))
    assert descr3i.repr_of_descr() == '<CharArrayDescr>'
    #
    cache = {}
    descr4 = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Ptr(S))
    assert 'GcPtrCallDescr' in descr4.repr_of_descr()
    #
    descr4i = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Char)
    assert 'CharCallDescr' in descr4i.repr_of_descr()
    #
    descr4f = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Float)
    assert 'FloatCallDescr' in descr4f.repr_of_descr()
Exemplo n.º 2
0
 def __init__(self, rtyper, stats=None, translate_support_code=False,
              annmixlevel=None, gcdescr=None):
     self.rtyper = rtyper
     self.translate_support_code = translate_support_code
     self.compiled_functions = []
     self.fail_ops = []
     self.in_out_args = []
     if translate_support_code:
         get_size = llmemory.sizeof
     else:
         get_size = rffi.sizeof
     self._arraydescrs = [
         ArrayDescr(get_size(llmemory.GCREF), self.SIZE_GCPTR),    # 0
         ArrayDescr(get_size(lltype.Signed),  self.SIZE_INT),      # 1
         ArrayDescr(get_size(lltype.Char),    self.SIZE_CHAR),     # 2
         ArrayDescr(get_size(lltype.UniChar), self.SIZE_UNICHAR),  # 3
         ]
     self._descr_caches = {}
     self.fielddescr_vtable = self.fielddescrof(rclass.OBJECT, 'typeptr')
     if sys.maxint == 2147483647:
         self.size_of_int = 4
     else:
         self.size_of_int = 8
     if runicode.MAXUNICODE > 0xffff:
         self.size_of_unicode = 4
     else:
         self.size_of_unicode = 2
     self.gcarray_gcref   = lltype.GcArray(llmemory.GCREF)
     self.gcarray_signed  = lltype.GcArray(lltype.Signed)
     self.gcarray_char    = lltype.GcArray(lltype.Char)
     self.gcarray_unichar = lltype.GcArray(lltype.UniChar)
     basesize, _, ofs_length = symbolic.get_array_token(
         self.gcarray_signed, self.translate_support_code)
     self.array_index_array = basesize
     self.array_index_length = ofs_length
     basesize, _, ofs_length = symbolic.get_array_token(
         rstr.STR, self.translate_support_code)
     self.string_index_array = basesize
     self.string_index_length = ofs_length
     basesize, _, ofs_length = symbolic.get_array_token(
         rstr.UNICODE, self.translate_support_code)
     self.unicode_index_array = basesize
     self.unicode_index_length = ofs_length
     self.vtable_descr = self.fielddescrof(rclass.OBJECT, 'typeptr')
     self._ovf_error_instance = self._get_prebuilt_error(OverflowError)
     self._zer_error_instance = self._get_prebuilt_error(ZeroDivisionError)
     #
     # temporary (Boehm only)
     from pypy.translator.tool.cbuild import ExternalCompilationInfo
     compilation_info = ExternalCompilationInfo(libraries=['gc'])
     self.malloc_fn_ptr = rffi.llexternal("GC_malloc",
                                          [rffi.SIZE_T],
                                          llmemory.GCREF,
                                          compilation_info=compilation_info,
                                          sandboxsafe=True,
                                          _nowrapper=True)
     assert rffi.sizeof(rffi.SIZE_T) == self.size_of_int
Exemplo n.º 3
0
def test_array_token():
    A = lltype.GcArray(lltype.Char)
    basesize, itemsize, ofs_length = get_array_token(A, False)
    assert basesize >= WORD  # at least the 'length', maybe some gc headers
    assert itemsize == 1
    assert ofs_length == basesize - WORD
    A = lltype.GcArray(lltype.Signed)
    basesize, itemsize, ofs_length = get_array_token(A, False)
    assert basesize >= WORD  # at least the 'length', maybe some gc headers
    assert itemsize == WORD
    assert ofs_length == basesize - WORD
Exemplo n.º 4
0
def test_array_token():
    for translate_support_code in (True, False):
        A = lltype.GcArray(lltype.Char)
        arraytok = get_array_token(A, translate_support_code)
        basesize, itemsize, ofs_length = convert(arraytok)
        assert basesize >= WORD  # at least the 'length', maybe some gc headers
        assert itemsize == 1
        assert ofs_length == basesize - WORD
        A = lltype.GcArray(lltype.Signed)
        arraytok = get_array_token(A, translate_support_code)
        basesize, itemsize, ofs_length = convert(arraytok)
        assert basesize >= WORD  # at least the 'length', maybe some gc headers
        assert itemsize == WORD
        assert ofs_length == basesize - WORD
Exemplo n.º 5
0
    def define_arraycopy_writebarrier_ptr(cls):
        TP = lltype.GcArray(lltype.Ptr(lltype.GcArray(lltype.Signed)))
        def fn():
            l = lltype.malloc(TP, 100)
            for i in range(100):
                l[i] = lltype.malloc(TP.OF.TO, i)
            l2 = lltype.malloc(TP, 50)
            rgc.ll_arraycopy(l, l2, 40, 0, 50)
            rgc.collect()
            for i in range(50):
                assert l2[i] == l[40 + i]
            return 0

        return fn
Exemplo n.º 6
0
def test_is_pure():
    from pypy.objspace.flow.model import Variable, Constant
    from pypy.rpython import rclass
    assert llop.bool_not.is_pure([Variable()])
    assert llop.debug_assert.is_pure([Variable()])
    assert not llop.int_add_ovf.is_pure([Variable(), Variable()])
    #
    S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
    v_s1 = Variable()
    v_s1.concretetype = lltype.Ptr(S1)
    assert not llop.setfield.is_pure([v_s1, Constant('x'), Variable()])
    assert not llop.getfield.is_pure([v_s1, Constant('y')])
    #
    A1 = lltype.GcArray(lltype.Signed)
    v_a1 = Variable()
    v_a1.concretetype = lltype.Ptr(A1)
    assert not llop.setarrayitem.is_pure([v_a1, Variable(), Variable()])
    assert not llop.getarrayitem.is_pure([v_a1, Variable()])
    assert llop.getarraysize.is_pure([v_a1])
    #
    S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
                         hints={'immutable': True})
    v_s2 = Variable()
    v_s2.concretetype = lltype.Ptr(S2)
    assert not llop.setfield.is_pure([v_s2, Constant('x'), Variable()])
    assert llop.getfield.is_pure([v_s2, Constant('y')])
    #
    A2 = lltype.GcArray(lltype.Signed, hints={'immutable': True})
    v_a2 = Variable()
    v_a2.concretetype = lltype.Ptr(A2)
    assert not llop.setarrayitem.is_pure([v_a2, Variable(), Variable()])
    assert llop.getarrayitem.is_pure([v_a2, Variable()])
    assert llop.getarraysize.is_pure([v_a2])
    #
    for kind in [
            rclass.IR_MUTABLE, rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY,
            rclass.IR_QUASIIMMUTABLE, rclass.IR_QUASIIMMUTABLE_ARRAY
    ]:
        accessor = rclass.FieldListAccessor()
        S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
                             hints={'immutable_fields': accessor})
        accessor.initialize(S3, {'x': kind})
        v_s3 = Variable()
        v_s3.concretetype = lltype.Ptr(S3)
        assert not llop.setfield.is_pure([v_s3, Constant('x'), Variable()])
        assert not llop.setfield.is_pure([v_s3, Constant('y'), Variable()])
        assert llop.getfield.is_pure([v_s3, Constant('x')]) is kind
        assert not llop.getfield.is_pure([v_s3, Constant('y')])
Exemplo n.º 7
0
def test_decode_builtin_call_method():
    A = lltype.GcArray(lltype.Signed)
    def myfoobar(a, i, marker, c):
        assert marker == 'mymarker'
        return a[i] * ord(c)
    myfoobar.oopspec = 'spam.foobar(a, 2, c, i)'
    TYPE = lltype.FuncType([lltype.Ptr(A), lltype.Signed,
                            lltype.Void, lltype.Char],
                           lltype.Signed)
    fnobj = lltype.functionptr(TYPE, 'foobar', _callable=myfoobar)
    vi = Variable('i')
    vi.concretetype = lltype.Signed
    vc = Variable('c')
    vc.concretetype = lltype.Char
    v_result = Variable('result')
    v_result.concretetype = lltype.Signed
    myarray = lltype.malloc(A, 10)
    myarray[5] = 42
    op = SpaceOperation('direct_call', [newconst(fnobj),
                                        newconst(myarray),
                                        vi,
                                        voidconst('mymarker'),
                                        vc],
                        v_result)
    oopspec, opargs = decode_builtin_call(op)
    assert oopspec == 'spam.foobar'
    assert opargs == [newconst(myarray), newconst(2), vc, vi]
Exemplo n.º 8
0
    def test_arrayitems(self):
        TP = lltype.GcArray(lltype.Signed)
        ofs = symbolic.get_field_token(TP, 'length', False)[0]
        itemsofs = symbolic.get_field_token(TP, 'items', False)[0]
        descr = self.cpu.arraydescrof(TP)
        res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], 'ref',
                                     descr)
        resbuf = self._resbuf(res)
        assert resbuf[ofs / WORD] == 10
        self.execute_operation(rop.SETARRAYITEM_GC,
                               [res, ConstInt(2), BoxInt(38)], 'void', descr)
        assert resbuf[itemsofs / WORD + 2] == 38

        self.execute_operation(rop.SETARRAYITEM_GC,
                               [res, BoxInt(3), BoxInt(42)], 'void', descr)
        assert resbuf[itemsofs / WORD + 3] == 42

        r = self.execute_operation(rop.GETARRAYITEM_GC, [res, ConstInt(2)],
                                   'int', descr)
        assert r.value == 38
        r = self.execute_operation(rop.GETARRAYITEM_GC,
                                   [res.constbox(), BoxInt(2)], 'int', descr)
        assert r.value == 38
        r = self.execute_operation(
            rop.GETARRAYITEM_GC, [res.constbox(), ConstInt(2)], 'int', descr)
        assert r.value == 38
        r = self.execute_operation(rop.GETARRAYITEM_GC, [res, BoxInt(2)],
                                   'int', descr)
        assert r.value == 38

        r = self.execute_operation(rop.GETARRAYITEM_GC, [res, BoxInt(3)],
                                   'int', descr)
        assert r.value == 42
Exemplo n.º 9
0
 def test_subclass(self):
     myjitdriver = JitDriver(greens = [], reds = ['n', 'xy2'],
                             virtualizables = ['xy2'])
     ARRAY = lltype.GcArray(lltype.Signed)
     def g(xy2, n):
         while n > 0:
             myjitdriver.can_enter_jit(xy2=xy2, n=n)
             myjitdriver.jit_merge_point(xy2=xy2, n=n)
             parent = xy2.parent
             promote_virtualizable(parent, 'inst_x')                
             promote_virtualizable(parent, 'inst_l2')                
             parent.inst_l2[0] += parent.inst_x
             n -= 1
     def f(n):
         xy2 = self.setup2sub()
         xy2.parent.inst_x = 2
         xy2.parent.inst_l1 = lltype.malloc(ARRAY, 2)
         xy2.parent.inst_l1[0] = 1941309
         xy2.parent.inst_l1[1] = 2941309
         xy2.parent.inst_l2 = lltype.malloc(ARRAY, 1)
         xy2.parent.inst_l2[0] = 10000
         m = 10
         while m > 0:
             g(xy2, n)
             m -= 1
         return xy2.parent.inst_l2[0]
     assert f(18) == 10360
     res = self.meta_interp(f, [18])
     assert res == 10360
     self.check_simple_loop(getfield_gc=0, getarrayitem_gc=0,
                            setfield_gc=0, setarrayitem_gc=0)
Exemplo n.º 10
0
    def define_write_typeids_z(self):
        U = lltype.GcForwardReference()
        U.become(lltype.GcStruct('U', ('next', lltype.Ptr(U)),
                                 ('x', lltype.Signed)))
        S = lltype.GcStruct('S', ('u', lltype.Ptr(U)))
        A = lltype.GcArray(lltype.Ptr(S))
        filename = self.filename_dump_typeids_z
        open_flags = os.O_WRONLY | os.O_CREAT | getattr(os, 'O_BINARY', 0)

        def fn():
            s = lltype.malloc(S)
            s.u = lltype.malloc(U)
            s.u.next = lltype.malloc(U)
            s.u.next.next = lltype.malloc(U)
            a = lltype.malloc(A, 1000)
            s2 = lltype.malloc(S)
            #
            p = rgc.get_typeids_z()
            s = ''.join([p[i] for i in range(len(p))])
            fd = os.open(filename, open_flags, 0666)
            os.write(fd, s)
            os.close(fd)
            return 0

        return fn
Exemplo n.º 11
0
    def define_get_rpy_type_index(self):
        U = lltype.GcStruct('U', ('x', lltype.Signed))
        S = lltype.GcStruct('S', ('u', lltype.Ptr(U)))
        A = lltype.GcArray(lltype.Ptr(S))

        def fn():
            s = lltype.malloc(S)
            s.u = lltype.malloc(U)
            a = lltype.malloc(A, 1000)
            s2 = lltype.malloc(S)
            gcref1 = lltype.cast_opaque_ptr(llmemory.GCREF, s)
            int1 = rgc.get_rpy_type_index(gcref1)
            gcref2 = lltype.cast_opaque_ptr(llmemory.GCREF, s.u)
            int2 = rgc.get_rpy_type_index(gcref2)
            gcref3 = lltype.cast_opaque_ptr(llmemory.GCREF, a)
            int3 = rgc.get_rpy_type_index(gcref3)
            gcref4 = lltype.cast_opaque_ptr(llmemory.GCREF, s2)
            int4 = rgc.get_rpy_type_index(gcref4)
            assert int1 != int2
            assert int1 != int3
            assert int2 != int3
            assert int1 == int4
            return 0

        return fn
Exemplo n.º 12
0
    def define_can_move(cls):
        TP = lltype.GcArray(lltype.Float)

        def func():
            return rgc.can_move(lltype.malloc(TP, 1))

        return func
Exemplo n.º 13
0
def test_keepalive_const_arrayitems():
    A1 = lltype.GcArray(lltype.Signed)
    a1 = lltype.malloc(A1, 10)
    a1[6] = 1234

    def fn():
        p1 = lltype.direct_arrayitems(a1)
        p2 = lltype.direct_ptradd(p1, 6)
        return p2[0]

    graph, t = get_graph(fn, [])
    assert summary(graph) == {
        'direct_arrayitems': 1,
        'direct_ptradd': 1,
        'getarrayitem': 1
    }
    constant_fold_graph(graph)

    # kill all references to 'a1'
    a1 = fn = None
    del graph.func
    import gc
    gc.collect()

    assert summary(graph) == {'getarrayitem': 1}
    check_graph(graph, [], 1234, t)
Exemplo n.º 14
0
    def test_immutable_to_old_promotion(self):
        T_CHILD = lltype.Ptr(lltype.GcStruct('Child', ('field', lltype.Signed)))
        T_PARENT = lltype.Ptr(lltype.GcStruct('Parent', ('sub', T_CHILD)))
        child = lltype.malloc(T_CHILD.TO)
        child2 = lltype.malloc(T_CHILD.TO)
        parent = lltype.malloc(T_PARENT.TO)
        parent2 = lltype.malloc(T_PARENT.TO)
        parent.sub = child
        child.field = 3
        parent2.sub = child2
        child2.field = 8

        T_ALL = lltype.Ptr(lltype.GcArray(T_PARENT))
        all = lltype.malloc(T_ALL.TO, 2)
        all[0] = parent
        all[1] = parent2

        def f(x, y):
            res = all[x]
            #all[x] = lltype.nullptr(T_PARENT.TO)
            return res.sub.field

        run, transformer = self.runner(f, nbargs=2, transformer=True)
        run([1, 4])
        if not transformer.GCClass.prebuilt_gc_objects_are_static_roots:
            assert len(transformer.layoutbuilder.addresses_of_static_ptrs) == 0
        else:
            assert len(transformer.layoutbuilder.addresses_of_static_ptrs) >= 4
Exemplo n.º 15
0
 def test_specific_bug(self):
     rgenop = self.RGenOp()
     FUNC0 = lltype.FuncType([], lltype.Signed)
     A = lltype.GcArray(lltype.Signed)
     a = lltype.malloc(A, 2, immortal=True)
     gv_a = rgenop.genconst(a)
     signed_kind = rgenop.kindToken(lltype.Signed)
     arraytoken = rgenop.arrayToken(A)
     builder0, gv_callable, _ = rgenop.newgraph(rgenop.sigToken(FUNC0),
                                                'generated')
     builder0.start_writing()
     builder0.genop_setarrayitem(arraytoken, gv_a, rgenop.genconst(0),
                                 rgenop.genconst(1))
     builder0.genop_setarrayitem(arraytoken, gv_a, rgenop.genconst(1),
                                 rgenop.genconst(2))
     v0 = builder0.genop_getarrayitem(arraytoken, gv_a, rgenop.genconst(0))
     v1 = builder0.genop_getarrayitem(arraytoken, gv_a, rgenop.genconst(1))
     v2 = builder0.genop2('int_add', v0, v1)
     builder1 = builder0.pause_writing([v2])
     builder1.start_writing()
     args_gv = [v2]
     label0 = builder1.enter_next_block([signed_kind], args_gv)
     [v3] = args_gv
     args_gv = [v3]
     label1 = builder1.enter_next_block([signed_kind], args_gv)
     [v4] = args_gv
     builder1.finish_and_return(rgenop.sigToken(FUNC0), v4)
     builder0.end()
Exemplo n.º 16
0
 def test_access_list_fields(self):
     myjitdriver = JitDriver(greens = [], reds = ['n', 'xy2'],
                             virtualizables = ['xy2'])
     ARRAY = lltype.GcArray(lltype.Signed)
     def f(n):
         xy2 = self.setup2()
         xy2.inst_x = 100
         xy2.inst_l1 = lltype.malloc(ARRAY, 3)
         xy2.inst_l1[0] = -9999999
         xy2.inst_l1[1] = -9999999
         xy2.inst_l1[2] = 3001
         xy2.inst_l2 = lltype.malloc(ARRAY, 2)
         xy2.inst_l2[0] = 80
         xy2.inst_l2[1] = -9999999
         while n > 0:
             myjitdriver.can_enter_jit(xy2=xy2, n=n)
             myjitdriver.jit_merge_point(xy2=xy2, n=n)
             promote_virtualizable(xy2, 'inst_l1')                
             promote_virtualizable(xy2, 'inst_l2')
             xy2.inst_l1[2] += xy2.inst_l2[0]
             n -= 1
         promote_virtualizable(xy2, 'inst_l1')                
         return xy2.inst_l1[2]
     res = self.meta_interp(f, [16])
     assert res == 3001 + 16 * 80
     self.check_simple_loop(setarrayitem_gc=0, setfield_gc=0,
                            getarrayitem_gc=0, getfield_gc=0)
Exemplo n.º 17
0
 def test_simple_all_removed(self):
     myjitdriver = JitDriver(greens = [], reds = ['n'])
     #
     A = lltype.GcArray(lltype.Signed)
     class XY:
         pass
     class ExCtx:
         pass
     exctx = ExCtx()
     #
     @dont_look_inside
     def externalfn(n):
         return 1
     #
     def f(n):
         while n > 0:
             myjitdriver.can_enter_jit(n=n)
             myjitdriver.jit_merge_point(n=n)
             xy = XY()
             xy.next1 = lltype.malloc(A, 0)
             xy.next2 = lltype.malloc(A, 0)
             xy.next3 = lltype.malloc(A, 0)
             exctx.topframeref = virtual_ref(xy)
             n -= externalfn(n)
             exctx.topframeref = vref_None
             xy.next1 = lltype.nullptr(A)
             xy.next2 = lltype.nullptr(A)
             xy.next3 = lltype.nullptr(A)
             virtual_ref_finish(xy)
     #
     self.meta_interp(f, [15])
     self.check_loops(new_with_vtable=0,     # all virtualized
                      new_array=0)
     self.check_aborted_count(0)
Exemplo n.º 18
0
 def test_array_length(self):
     myjitdriver = JitDriver(greens = [], reds = ['n', 'xy2'],
                             virtualizables = ['xy2'])
     ARRAY = lltype.GcArray(lltype.Signed)
     def g(xy2, n):
         while n > 0:
             myjitdriver.can_enter_jit(xy2=xy2, n=n)
             myjitdriver.jit_merge_point(xy2=xy2, n=n)
             promote_virtualizable(xy2, 'inst_l1')
             promote_virtualizable(xy2, 'inst_l2')                
             xy2.inst_l1[1] += len(xy2.inst_l2)
             n -= 1
     def f(n):
         xy2 = self.setup2()
         xy2.inst_x = 2
         xy2.inst_l1 = lltype.malloc(ARRAY, 2)
         xy2.inst_l1[0] = 1941309
         xy2.inst_l1[1] = 2941309
         xy2.inst_l2 = lltype.malloc(ARRAY, 1)
         xy2.inst_l2[0] = 10000
         g(xy2, n)
         return xy2.inst_l1[1]
     res = self.meta_interp(f, [18])
     assert res == 2941309 + 18
     self.check_simple_loop(setfield_gc=0, getarrayitem_gc=0,
                            arraylen_gc=0, getfield_gc=0)
Exemplo n.º 19
0
    def define_get_rpy_memory_usage(self):
        U = lltype.GcStruct('U', ('x1', lltype.Signed),
                                 ('x2', lltype.Signed),
                                 ('x3', lltype.Signed),
                                 ('x4', lltype.Signed),
                                 ('x5', lltype.Signed),
                                 ('x6', lltype.Signed),
                                 ('x7', lltype.Signed),
                                 ('x8', lltype.Signed))
        S = lltype.GcStruct('S', ('u', lltype.Ptr(U)))
        A = lltype.GcArray(lltype.Ptr(S))

        def fn():
            s = lltype.malloc(S)
            s.u = lltype.malloc(U)
            a = lltype.malloc(A, 1000)
            gcref1 = lltype.cast_opaque_ptr(llmemory.GCREF, s)
            int1 = rgc.get_rpy_memory_usage(gcref1)
            assert 8 <= int1 <= 32
            gcref2 = lltype.cast_opaque_ptr(llmemory.GCREF, s.u)
            int2 = rgc.get_rpy_memory_usage(gcref2)
            assert 4*9 <= int2 <= 8*12
            gcref3 = lltype.cast_opaque_ptr(llmemory.GCREF, a)
            int3 = rgc.get_rpy_memory_usage(gcref3)
            assert 4*1001 <= int3 <= 8*1010
            return 0

        return fn
Exemplo n.º 20
0
 def test_residual_function(self):
     myjitdriver = JitDriver(greens = [], reds = ['n', 'xy2'],
                             virtualizables = ['xy2'])
     ARRAY = lltype.GcArray(lltype.Signed)
     #
     @dont_look_inside
     def h(xy2):
         # this function is marked for residual calls because
         # it does something with a virtualizable's array that is not
         # just accessing an item
         return xy2.inst_l2
     #
     def g(xy2, n):
         while n > 0:
             myjitdriver.can_enter_jit(xy2=xy2, n=n)
             myjitdriver.jit_merge_point(xy2=xy2, n=n)
             promote_virtualizable(xy2, 'inst_l1')
             xy2.inst_l1[1] = xy2.inst_l1[1] + len(h(xy2))
             n -= 1
     def f(n):
         xy2 = self.setup2()
         xy2.inst_x = 2
         xy2.inst_l1 = lltype.malloc(ARRAY, 2)
         xy2.inst_l1[0] = 1941309
         xy2.inst_l1[1] = 2941309
         xy2.inst_l2 = lltype.malloc(ARRAY, 1)
         xy2.inst_l2[0] = 10000
         g(xy2, n)
         return xy2.inst_l1[1]
     res = self.meta_interp(f, [18])
     assert res == 2941309 + 18
     self.check_simple_loop(call=1, setfield_gc=0, getarrayitem_gc=0,
                            arraylen_gc=1, getfield_gc=0)
Exemplo n.º 21
0
    def define_dump_rpy_heap(self):
        U = lltype.GcForwardReference()
        U.become(lltype.GcStruct('U', ('next', lltype.Ptr(U)),
                                 ('x', lltype.Signed)))
        S = lltype.GcStruct('S', ('u', lltype.Ptr(U)))
        A = lltype.GcArray(lltype.Ptr(S))
        filename1 = self.filename1_dump
        filename2 = self.filename2_dump

        def fn():
            s = lltype.malloc(S)
            s.u = lltype.malloc(U)
            s.u.next = lltype.malloc(U)
            s.u.next.next = lltype.malloc(U)
            a = lltype.malloc(A, 1000)
            s2 = lltype.malloc(S)
            #
            fd1 = os.open(filename1, os.O_WRONLY | os.O_CREAT, 0666)
            fd2 = os.open(filename2, os.O_WRONLY | os.O_CREAT, 0666)
            rgc.dump_rpy_heap(fd1)
            rgc.dump_rpy_heap(fd2)      # try twice in a row
            keepalive_until_here(s2)
            keepalive_until_here(s)
            keepalive_until_here(a)
            os.close(fd1)
            os.close(fd2)
            return 0

        return fn
Exemplo n.º 22
0
    def test_framework_varsized(self):
        S = lltype.GcStruct("S", ('x', lltype.Signed))
        T = lltype.GcStruct("T", ('y', lltype.Signed), ('s', lltype.Ptr(S)))
        ARRAY_Ts = lltype.GcArray(lltype.Ptr(T))

        def f():
            r = 0
            for i in range(30):
                a = lltype.malloc(ARRAY_Ts, i)
                for j in range(i):
                    a[j] = lltype.malloc(T)
                    a[j].y = i
                    a[j].s = lltype.malloc(S)
                    a[j].s.x = 2 * i
                    r += a[j].y + a[j].s.x
                    a[j].s = lltype.malloc(S)
                    a[j].s.x = 3 * i
                    r -= a[j].s.x
                for j in range(i):
                    r += a[j].y
            return r

        fn = self.getcompiled(f)
        res = fn()
        assert res == f()
Exemplo n.º 23
0
    def test_unerased_pointers_in_short_preamble(self):
        from pypy.rlib.rerased import new_erasing_pair
        from pypy.rpython.lltypesystem import lltype

        class A(object):
            def __init__(self, val):
                self.val = val

        erase_A, unerase_A = new_erasing_pair('A')
        erase_TP, unerase_TP = new_erasing_pair('TP')
        TP = lltype.GcArray(lltype.Signed)
        myjitdriver = JitDriver(greens=[],
                                reds=['n', 'm', 'i', 'j', 'sa', 'p'])

        def f(n, m, j):
            i = sa = 0
            p = erase_A(A(7))
            while i < n:
                myjitdriver.jit_merge_point(n=n, m=m, i=i, j=j, sa=sa, p=p)
                if i < m:
                    sa += unerase_A(p).val
                elif i == m:
                    a = lltype.malloc(TP, 5)
                    a[0] = 42
                    p = erase_TP(a)
                else:
                    sa += unerase_TP(p)[0]
                sa += A(i).val
                assert n > 0 and m > 0
                i += j
            return sa

        res = self.meta_interp(f, [20, 10, 1])
        assert res == f(20, 10, 1)
Exemplo n.º 24
0
    def test_can_move(self):
        TP = lltype.GcArray(lltype.Float)

        def func():
            return rgc.can_move(lltype.malloc(TP, 1))

        assert self.interpret(func, []) == self.GC_CAN_MOVE
Exemplo n.º 25
0
def test_boehm():
    gc_ll_descr = GcLLDescr_boehm(None, None)
    #
    record = []
    prev_funcptr_for_new = gc_ll_descr.funcptr_for_new
    def my_funcptr_for_new(size):
        p = prev_funcptr_for_new(size)
        record.append((size, p))
        return p
    gc_ll_descr.funcptr_for_new = my_funcptr_for_new
    #
    # ---------- gc_malloc ----------
    S = lltype.GcStruct('S', ('x', lltype.Signed))
    sizedescr = get_size_descr(gc_ll_descr, S)
    p = gc_ll_descr.gc_malloc(sizedescr)
    assert record == [(sizedescr.size, p)]
    del record[:]
    # ---------- gc_malloc_array ----------
    A = lltype.GcArray(lltype.Signed)
    arraydescr = get_array_descr(gc_ll_descr, A)
    p = gc_ll_descr.gc_malloc_array(arraydescr, 10)
    assert record == [(arraydescr.get_base_size(False) +
                       10 * arraydescr.get_item_size(False), p)]
    del record[:]
    # ---------- gc_malloc_str ----------
    p = gc_ll_descr.gc_malloc_str(10)
    basesize, itemsize, ofs_length = symbolic.get_array_token(rstr.STR, False)
    assert record == [(basesize + 10 * itemsize, p)]
    del record[:]
    # ---------- gc_malloc_unicode ----------
    p = gc_ll_descr.gc_malloc_unicode(10)
    basesize, itemsize, ofs_length = symbolic.get_array_token(rstr.UNICODE,
                                                              False)
    assert record == [(basesize + 10 * itemsize, p)]
    del record[:]
Exemplo n.º 26
0
    def test_jit_force_virtual_seen(self):
        myjitdriver = JitDriver(greens=[], reds=['n'])
        #
        A = lltype.GcArray(lltype.Signed)

        class XY:
            pass

        class ExCtx:
            pass

        exctx = ExCtx()

        #
        def f(n):
            while n > 0:
                myjitdriver.can_enter_jit(n=n)
                myjitdriver.jit_merge_point(n=n)
                xy = XY()
                xy.n = n
                exctx.topframeref = vref = virtual_ref(xy)
                xy.next1 = lltype.malloc(A, 0)
                n = exctx.topframeref().n - 1
                xy.next1 = lltype.nullptr(A)
                exctx.topframeref = vref_None
                virtual_ref_finish(vref, xy)
            return 1

        #
        res = self.meta_interp(f, [15])
        assert res == 1
        self.check_resops(
            new_with_vtable=4,  # vref, xy
            new_array=2)  # next1
        self.check_aborted_count(0)
Exemplo n.º 27
0
def test_fakeadr_eq():
    S = lltype.GcStruct("S", ("x", lltype.Signed), ("y", lltype.Signed))
    s = lltype.malloc(S)

    assert cast_ptr_to_adr(s) == cast_ptr_to_adr(s)

    adr1 = cast_ptr_to_adr(s) + FieldOffset(S, "x")
    adr2 = cast_ptr_to_adr(s) + FieldOffset(S, "y")
    adr3 = cast_ptr_to_adr(s) + FieldOffset(S, "y")
    assert adr1 != adr2
    assert adr2 == adr3

    A = lltype.GcArray(lltype.Char)
    a = lltype.malloc(A, 5)
    adr1 = cast_ptr_to_adr(a) + ArrayLengthOffset(A)
    adr2 = cast_ptr_to_adr(a) + ArrayLengthOffset(A)
    assert adr1 == adr2

    adr1 = cast_ptr_to_adr(a) + ArrayItemsOffset(A)
    adr2 = cast_ptr_to_adr(a) + ArrayItemsOffset(A)
    assert adr1 == adr2
    adr2 += ItemOffset(lltype.Char, 0)
    assert adr1 == adr2

    adr1 += ItemOffset(lltype.Char, 2)
    adr2 += ItemOffset(lltype.Char, 3)
    assert adr1 != adr2
    adr2 += ItemOffset(lltype.Char, -1)
    assert adr1 == adr2
Exemplo n.º 28
0
def test_gc_offsets():
    STRUCT = lltype.GcStruct('S1', ('x', lltype.Signed), ('y', lltype.Char))
    ARRAY = lltype.GcArray(lltype.Signed)
    s1 = llarena.round_up_for_allocation(llmemory.sizeof(STRUCT))
    s2 = llmemory.offsetof(STRUCT, 'x')
    s3 = llmemory.ArrayLengthOffset(ARRAY)
    s4 = llmemory.sizeof(ARRAY, 0)
    s5 = llmemory.ArrayItemsOffset(ARRAY)

    def fn():
        return (s1 * 100000000 + s2 * 1000000 + s3 * 10000 + s4 * 100 + s5)

    mod, f = compile_test(fn, [], gcpolicy="semispace")
    res = f()
    i1 = (res // 100000000) % 100
    i2 = (res // 1000000) % 100
    i3 = (res // 10000) % 100
    i4 = (res // 100) % 100
    i5 = (res // 1) % 100
    assert i1 % 4 == 0
    assert 12 <= i1 <= 24
    assert 4 <= i2 <= i1 - 8
    assert 4 <= i3 <= 12
    assert i4 == i5
    assert i3 + 4 <= i5
Exemplo n.º 29
0
def test_include_write_array():
    A = lltype.GcArray(lltype.Signed)
    effects = frozenset([("array", lltype.Ptr(A))])
    effectinfo = effectinfo_from_writeanalyze(effects, FakeCPU())
    assert not effectinfo.readonly_descrs_fields
    assert not effectinfo.write_descrs_fields
    assert list(effectinfo.write_descrs_arrays) == [('arraydescr', A)]
Exemplo n.º 30
0
 def test_gc_malloc_array(self):
     A = lltype.GcArray(lltype.Signed)
     arraydescr = get_array_descr(self.gc_ll_descr, A)
     p = self.gc_ll_descr.gc_malloc_array(arraydescr, 10)
     assert self.llop1.record == [
         ("varsize", arraydescr.tid, 10, repr(arraydescr.basesize),
          repr(arraydescr.itemsize), repr(arraydescr.lendescr.offset), p)
     ]