Exemple #1
0
class CConfig:
    _compilation_info_ = eci
    calling_conv = 'c'

    CHECK_LIBRARY = platform.Has('dump("x", (int)&BZ2_bzCompress)')

    off_t = platform.SimpleType("off_t", rffi.LONGLONG)
    size_t = platform.SimpleType("size_t", rffi.ULONG)
    BUFSIZ = platform.ConstantInteger("BUFSIZ")
    _alloc_type = lltype.FuncType([rffi.VOIDP, rffi.INT, rffi.INT], rffi.VOIDP)
    _free_type = lltype.FuncType([rffi.VOIDP, rffi.VOIDP], lltype.Void)
    SEEK_SET = platform.ConstantInteger("SEEK_SET")
    bz_stream = platform.Struct('bz_stream',
                                [('next_in', rffi.CCHARP),
                                 ('avail_in', rffi.UINT),
                                 ('total_in_lo32', rffi.UINT),
                                 ('total_in_hi32', rffi.UINT),
                                 ('next_out', rffi.CCHARP),
                                 ('avail_out', rffi.UINT),
                                 ('total_out_lo32', rffi.UINT),
                                 ('total_out_hi32', rffi.UINT),
                                 ('state', rffi.VOIDP),
                                 ('bzalloc', lltype.Ptr(_alloc_type)),
                                 ('bzfree', lltype.Ptr(_free_type)),
                                 ('opaque', rffi.VOIDP),
                                 ])
Exemple #2
0
class CConfig:
    _compilation_info_ = ExternalCompilationInfo(
        includes=['stdio.h', 'sys/types.h', 'bzlib.h'],
        libraries=['bz2'],
    )
    calling_conv = 'c'

    off_t = platform.SimpleType("off_t", rffi.LONGLONG)
    size_t = platform.SimpleType("size_t", rffi.ULONG)
    BUFSIZ = platform.ConstantInteger("BUFSIZ")
    _alloc_type = lltype.FuncType([rffi.VOIDP, rffi.INT, rffi.INT], rffi.VOIDP)
    _free_type = lltype.FuncType([rffi.VOIDP, rffi.VOIDP], lltype.Void)
    SEEK_SET = platform.ConstantInteger("SEEK_SET")
    bz_stream = platform.Struct('bz_stream', [
        ('next_in', rffi.CCHARP),
        ('avail_in', rffi.UINT),
        ('total_in_lo32', rffi.UINT),
        ('total_in_hi32', rffi.UINT),
        ('next_out', rffi.CCHARP),
        ('avail_out', rffi.UINT),
        ('total_out_lo32', rffi.UINT),
        ('total_out_hi32', rffi.UINT),
        ('state', rffi.VOIDP),
        ('bzalloc', lltype.Ptr(_alloc_type)),
        ('bzfree', lltype.Ptr(_free_type)),
        ('opaque', rffi.VOIDP),
    ])
Exemple #3
0
class ComplexCConfig:
    """
    Definitions of structure types defined by zlib and based on SimpleCConfig
    definitions.
    """
    _compilation_info_ = eci

    z_stream = rffi_platform.Struct(
        'z_stream',
        [('next_in', Bytefp),
         ('avail_in', uInt),
         ('total_in', uLong),

         ('next_out', Bytefp),
         ('avail_out', uInt),
         ('total_out', uLong),

         ('msg', rffi.CCHARP),

         ('zalloc', lltype.Ptr(
                    lltype.FuncType([voidpf, uInt, uInt], voidpf))),
         ('zfree', lltype.Ptr(
                    lltype.FuncType([voidpf, voidpf], lltype.Void))),

         ('opaque', voidpf),

         ('data_type', rffi.INT),
         ('adler', uLong),
         ('reserved', uLong)
         ])
Exemple #4
0
 def _setup_write_barrier(self):
     self.WB_FUNCPTR = lltype.Ptr(
         lltype.FuncType([llmemory.Address, llmemory.Address], lltype.Void))
     self.WB_ARRAY_FUNCPTR = lltype.Ptr(
         lltype.FuncType(
             [llmemory.Address, lltype.Signed, llmemory.Address],
             lltype.Void))
     self.write_barrier_descr = WriteBarrierDescr(self)
Exemple #5
0
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
Exemple #6
0
class CConfig:
    _compilation_info_ = eci

    OPENSSL_VERSION_NUMBER = rffi_platform.ConstantInteger(
        "OPENSSL_VERSION_NUMBER")
    SSLEAY_VERSION = rffi_platform.DefinedConstantString(
        "SSLEAY_VERSION", "SSLeay_version(SSLEAY_VERSION)")
    OPENSSL_NO_SSL2 = rffi_platform.Defined("OPENSSL_NO_SSL2")
    SSL_FILETYPE_PEM = rffi_platform.ConstantInteger("SSL_FILETYPE_PEM")
    SSL_OP_ALL = rffi_platform.ConstantInteger("SSL_OP_ALL")
    SSL_VERIFY_NONE = rffi_platform.ConstantInteger("SSL_VERIFY_NONE")
    SSL_VERIFY_PEER = rffi_platform.ConstantInteger("SSL_VERIFY_PEER")
    SSL_VERIFY_FAIL_IF_NO_PEER_CERT = rffi_platform.ConstantInteger(
        "SSL_VERIFY_FAIL_IF_NO_PEER_CERT")
    SSL_ERROR_WANT_READ = rffi_platform.ConstantInteger("SSL_ERROR_WANT_READ")
    SSL_ERROR_WANT_WRITE = rffi_platform.ConstantInteger(
        "SSL_ERROR_WANT_WRITE")
    SSL_ERROR_ZERO_RETURN = rffi_platform.ConstantInteger(
        "SSL_ERROR_ZERO_RETURN")
    SSL_ERROR_WANT_X509_LOOKUP = rffi_platform.ConstantInteger(
        "SSL_ERROR_WANT_X509_LOOKUP")
    SSL_ERROR_WANT_CONNECT = rffi_platform.ConstantInteger(
        "SSL_ERROR_WANT_CONNECT")
    SSL_ERROR_SYSCALL = rffi_platform.ConstantInteger("SSL_ERROR_SYSCALL")
    SSL_ERROR_SSL = rffi_platform.ConstantInteger("SSL_ERROR_SSL")
    SSL_RECEIVED_SHUTDOWN = rffi_platform.ConstantInteger(
        "SSL_RECEIVED_SHUTDOWN")
    SSL_MODE_AUTO_RETRY = rffi_platform.ConstantInteger("SSL_MODE_AUTO_RETRY")

    NID_subject_alt_name = rffi_platform.ConstantInteger(
        "NID_subject_alt_name")
    GEN_DIRNAME = rffi_platform.ConstantInteger("GEN_DIRNAME")

    CRYPTO_LOCK = rffi_platform.ConstantInteger("CRYPTO_LOCK")

    # Some structures, with only the fields used in the _ssl module
    X509_name_entry_st = rffi_platform.Struct('struct X509_name_entry_st',
                                              [('set', rffi.INT)])
    asn1_string_st = rffi_platform.Struct('struct asn1_string_st',
                                          [('length', rffi.INT),
                                           ('data', rffi.CCHARP)])
    X509_extension_st = rffi_platform.Struct('struct X509_extension_st',
                                             [('value', ASN1_STRING)])
    ASN1_ITEM_EXP = lltype.FuncType([], ASN1_ITEM)
    X509V3_EXT_D2I = lltype.FuncType([rffi.VOIDP, rffi.CCHARPP, rffi.LONG],
                                     rffi.VOIDP)
    v3_ext_method = rffi_platform.Struct('struct v3_ext_method',
                                         [('it', lltype.Ptr(ASN1_ITEM_EXP)),
                                          ('d2i', lltype.Ptr(X509V3_EXT_D2I))])
    GENERAL_NAME_st = rffi_platform.Struct('struct GENERAL_NAME_st', [
        ('type', rffi.INT),
    ])
    EVP_MD_st = rffi_platform.Struct('EVP_MD', [('md_size', rffi.INT),
                                                ('block_size', rffi.INT)])
    EVP_MD_SIZE = rffi_platform.SizeOf('EVP_MD')
    EVP_MD_CTX_SIZE = rffi_platform.SizeOf('EVP_MD_CTX')
Exemple #7
0
 def test_mark_gc_roots(self):
     cpu = CPU(None, None)
     regalloc = RegAlloc(MockAssembler(cpu, MockGcDescr(False)))
     boxes = [BoxPtr() for i in range(len(X86RegisterManager.all_regs))]
     longevity = {}
     for box in boxes:
         longevity[box] = (0, 1)
     regalloc.sm = X86StackManager()
     regalloc.rm = X86RegisterManager(longevity,
                                      regalloc.sm,
                                      assembler=regalloc.assembler)
     regalloc.xrm = X86XMMRegisterManager(longevity,
                                          regalloc.sm,
                                          assembler=regalloc.assembler)
     cpu = regalloc.assembler.cpu
     for box in boxes:
         regalloc.rm.try_allocate_reg(box)
     TP = lltype.FuncType([], lltype.Signed)
     calldescr = cpu.calldescrof(TP, TP.ARGS, TP.RESULT)
     regalloc.rm._check_invariants()
     box = boxes[0]
     regalloc.position = 0
     regalloc.consider_call(
         ResOperation(rop.CALL, [box], BoxInt(), calldescr), None)
     assert len(regalloc.assembler.movs) == 3
     #
     mark = regalloc.get_mark_gc_roots(cpu.gc_ll_descr.gcrootmap)
     assert mark[0] == 'compressed'
     expected = ['ebx', 'esi', 'edi', -16, -20, -24]
     assert dict.fromkeys(mark[1:]) == dict.fromkeys(expected)
Exemple #8
0
    def test_llexternal_with_callback(self):
        from pypy.rpython.lltypesystem.rffi import llexternal
        from pypy.rpython.lltypesystem import lltype

        class Abc:
            pass
        abc = Abc()

        FUNC = lltype.FuncType([lltype.Signed], lltype.Signed)
        z = llexternal('z', [lltype.Ptr(FUNC)], lltype.Signed)
        def g(n):
            abc.foobar = n
            return n + 1
        def f(x):
            return z(g)
        t, wa = self.translate(f, [int])
        fgraph = graphof(t, f)
        backend_optimizations(t)
        assert fgraph.startblock.operations[0].opname == 'direct_call'

        result = wa.analyze(fgraph.startblock.operations[0])
        assert len(result) == 1
        (struct, T, name), = result
        assert struct == "struct"
        assert name.endswith("foobar")
Exemple #9
0
def test_deallocator_with_destructor():
    S = lltype.GcStruct("S", ('x', lltype.Signed))
    def f(s):
        s.x = 1
    def type_info_S(p):
        return lltype.getRuntimeTypeInfo(S)
    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)
    pinf = lltype.attachRuntimeTypeInfo(S, qp, destrptr=dp)
    graph, t = make_deallocator(S)
Exemple #10
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()
Exemple #11
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]
Exemple #12
0
def test_set_accessor():

    G = lltype.FuncType([rclass.OBJECTPTR], lltype.Void)

    witness = []

    def setv(vinst, val):
        witness.append(val)
        vinst.inst_v = val

    def g(vobj):
        vobj = lltype.normalizeptr(vobj)
        LLV = lltype.typeOf(vobj).TO
        ACCESS = LLV.vable_access.TO
        access = lltype.malloc(ACCESS, immortal=True)
        access.set_inst_v = lltype.functionptr(ACCESS.set_inst_v.TO,
                                               'setv',
                                               _callable=setv)
        vobj.vable_access = access

    gptr = lltype.functionptr(G, 'g', _callable=g)

    def f(v):
        vinst = V(v)
        vobj = cast_instance_to_base_ptr(vinst)
        gptr(vobj)
        vinst.v = 33

    res = interpret(f, [42])

    assert witness == [33]
Exemple #13
0
def test_unicode_concat():
    # test that the oopspec is present and correctly transformed
    PSTR = lltype.Ptr(rstr.UNICODE)
    FUNC = lltype.FuncType([PSTR, PSTR], PSTR)
    func = lltype.functionptr(FUNC,
                              'll_strconcat',
                              _callable=rstr.LLHelpers.ll_strconcat)
    v1 = varoftype(PSTR)
    v2 = varoftype(PSTR)
    v3 = varoftype(PSTR)
    op = SpaceOperation('direct_call', [const(func), v1, v2], v3)
    cc = FakeBuiltinCallControl()
    tr = Transformer(FakeCPU(), cc)
    op1 = tr.rewrite_operation(op)
    assert op1.opname == 'residual_call_r_r'
    assert op1.args[0].value == func
    assert op1.args[1] == 'calldescr-%d' % effectinfo.EffectInfo.OS_UNI_CONCAT
    assert op1.args[2] == ListOfKind('ref', [v1, v2])
    assert op1.result == v3
    #
    # check the callinfo_for_oopspec
    got = cc.callinfocollection.seen[0]
    assert got[0] == effectinfo.EffectInfo.OS_UNI_CONCAT
    assert got[1] == op1.args[1]  # the calldescr
    assert heaptracker.int2adr(got[2]) == llmemory.cast_ptr_to_adr(func)
Exemple #14
0
 def test_mark_gc_roots(self):
     cpu = CPU(None, None)
     cpu.setup_once()
     regalloc = RegAlloc(MockAssembler(cpu, MockGcDescr(False)))
     regalloc.assembler.datablockwrapper = 'fakedatablockwrapper'
     boxes = [BoxPtr() for i in range(len(X86RegisterManager.all_regs))]
     longevity = {}
     for box in boxes:
         longevity[box] = (0, 1)
     regalloc.fm = X86FrameManager()
     regalloc.rm = X86RegisterManager(longevity,
                                      regalloc.fm,
                                      assembler=regalloc.assembler)
     regalloc.xrm = X86XMMRegisterManager(longevity,
                                          regalloc.fm,
                                          assembler=regalloc.assembler)
     cpu = regalloc.assembler.cpu
     for box in boxes:
         regalloc.rm.try_allocate_reg(box)
     TP = lltype.FuncType([], lltype.Signed)
     calldescr = cpu.calldescrof(TP, TP.ARGS, TP.RESULT,
                                 EffectInfo.MOST_GENERAL)
     regalloc.rm._check_invariants()
     box = boxes[0]
     regalloc.position = 0
     regalloc.consider_call(
         ResOperation(rop.CALL, [box], BoxInt(), calldescr))
     assert len(regalloc.assembler.movs) == 3
     #
     mark = regalloc.get_mark_gc_roots(cpu.gc_ll_descr.gcrootmap)
     assert mark[0] == 'compressed'
     base = -WORD * FRAME_FIXED_SIZE
     expected = ['ebx', 'esi', 'edi', base, base - WORD, base - WORD * 2]
     assert dict.fromkeys(mark[1:]) == dict.fromkeys(expected)
Exemple #15
0
def test_simple():
    def f(v):
        vinst = V(v)
        return vinst, vinst.v

    res = interpret(f, [42])
    assert res.item1 == 42
    res = lltype.normalizeptr(res.item0)
    assert res.inst_v == 42
    assert not res.vable_access
    LLV = lltype.typeOf(res)
    ACCESS = lltype.typeOf(res.vable_access).TO
    assert ACCESS.get_inst_v == lltype.Ptr(
        lltype.FuncType([LLV], lltype.Signed))
    assert ACCESS.set_inst_v == lltype.Ptr(
        lltype.FuncType([LLV, lltype.Signed], lltype.Void))
Exemple #16
0
def test_cancollect_external():
    fext1 = rffi.llexternal('fext1', [], lltype.Void, threadsafe=False)

    def g():
        fext1()

    t = rtype(g, [])
    gg = graphof(t, g)
    assert not CollectAnalyzer(t).analyze_direct_call(gg)

    fext2 = rffi.llexternal('fext2', [], lltype.Void, threadsafe=True)

    def g():
        fext2()

    t = rtype(g, [])
    gg = graphof(t, g)
    assert CollectAnalyzer(t).analyze_direct_call(gg)

    S = lltype.GcStruct('S', ('x', lltype.Signed))
    FUNC = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Void))
    fext3 = rffi.llexternal('fext3', [FUNC], lltype.Void, threadsafe=False)

    def h(x):
        lltype.malloc(S, zero=True)

    def g():
        fext3(h)

    t = rtype(g, [])
    gg = graphof(t, g)
    assert CollectAnalyzer(t).analyze_direct_call(gg)
Exemple #17
0
    def define_callback_simple(cls):
        import gc
        from pypy.rpython.lltypesystem import lltype, rffi
        from pypy.rpython.annlowlevel import llhelper
        from pypy.translator.tool.cbuild import ExternalCompilationInfo

        c_source = py.code.Source("""
        int mystuff(int(*cb)(int, int))
        {
            return cb(40, 2) + cb(3, 4);
        }
        """)
        eci = ExternalCompilationInfo(separate_module_sources=[c_source])
        S = lltype.GcStruct('S', ('x', lltype.Signed))
        CALLBACK = lltype.FuncType([lltype.Signed, lltype.Signed],
                                   lltype.Signed)
        z = rffi.llexternal('mystuff', [lltype.Ptr(CALLBACK)], lltype.Signed,
                            compilation_info=eci)

        def mycallback(a, b):
            gc.collect()
            return a + b

        def f():
            p = lltype.malloc(S)
            p.x = 100
            result = z(mycallback)
            return result * p.x

        return f
Exemple #18
0
    def test_llexternal(self):
        from pypy.rpython.lltypesystem.rffi import llexternal
        from pypy.rpython.lltypesystem import lltype
        z = llexternal('z', [lltype.Signed], lltype.Signed)

        def f(x):
            return z(x)

        t, ra = self.translate(f, [int])
        fgraph = graphof(t, f)
        backend_optimizations(t)
        assert fgraph.startblock.operations[0].opname == 'direct_call'

        result = ra.can_raise(fgraph.startblock.operations[0])
        assert not result

        z = lltype.functionptr(lltype.FuncType([lltype.Signed], lltype.Signed),
                               'foobar')

        def g(x):
            return z(x)

        t, ra = self.translate(g, [int])
        ggraph = graphof(t, g)

        assert ggraph.startblock.operations[0].opname == 'direct_call'

        result = ra.can_raise(ggraph.startblock.operations[0])
        assert result
Exemple #19
0
 def define_custom_trace(cls):
     from pypy.rpython.annlowlevel import llhelper
     from pypy.rpython.lltypesystem import llmemory
     #
     S = lltype.GcStruct('S', ('x', llmemory.Address), rtti=True)
     offset_of_x = llmemory.offsetof(S, 'x')
     def customtrace(obj, prev):
         if not prev:
             return obj + offset_of_x
         else:
             return llmemory.NULL
     CUSTOMTRACEFUNC = lltype.FuncType([llmemory.Address, llmemory.Address],
                                       llmemory.Address)
     customtraceptr = llhelper(lltype.Ptr(CUSTOMTRACEFUNC), customtrace)
     lltype.attachRuntimeTypeInfo(S, customtraceptr=customtraceptr)
     #
     def setup():
         s = lltype.nullptr(S)
         for i in range(10000):
             t = lltype.malloc(S)
             t.x = llmemory.cast_ptr_to_adr(s)
             s = t
         return s
     def measure_length(s):
         res = 0
         while s:
             res += 1
             s = llmemory.cast_adr_to_ptr(s.x, lltype.Ptr(S))
         return res
     def f(n):
         s1 = setup()
         llop.gc__collect(lltype.Void)
         return measure_length(s1)
     return f
Exemple #20
0
    def define_custom_trace(cls):
        from pypy.rpython.annlowlevel import llhelper
        from pypy.rpython.lltypesystem import llmemory
        #
        S = lltype.GcStruct('S', ('x', llmemory.Address), rtti=True)
        T = lltype.GcStruct('T', ('z', lltype.Signed))
        offset_of_x = llmemory.offsetof(S, 'x')

        def customtrace(obj, prev):
            if not prev:
                return obj + offset_of_x
            else:
                return llmemory.NULL

        CUSTOMTRACEFUNC = lltype.FuncType([llmemory.Address, llmemory.Address],
                                          llmemory.Address)
        customtraceptr = llhelper(lltype.Ptr(CUSTOMTRACEFUNC), customtrace)
        lltype.attachRuntimeTypeInfo(S, customtraceptr=customtraceptr)

        #
        def setup():
            s1 = lltype.malloc(S)
            tx = lltype.malloc(T)
            tx.z = 4243
            s1.x = llmemory.cast_ptr_to_adr(tx)
            return s1

        def f():
            s1 = setup()
            llop.gc__collect(lltype.Void)
            return llmemory.cast_adr_to_ptr(s1.x, lltype.Ptr(T)).z

        return f
Exemple #21
0
    def define_callback_simple(cls):
        c_source = py.code.Source("""
        int mystuff(int(*cb)(int, int))
        {
            return cb(40, 2) + cb(3, 4);
        }
        """)
        eci = ExternalCompilationInfo(separate_module_sources=[c_source])
        S = lltype.GcStruct('S', ('x', lltype.Signed))
        CALLBACK = lltype.FuncType([lltype.Signed, lltype.Signed],
                                   lltype.Signed)
        z = rffi.llexternal('mystuff', [lltype.Ptr(CALLBACK)],
                            lltype.Signed,
                            compilation_info=eci)

        def mycallback(a, b):
            gc.collect()
            return a + b

        def f():
            p = lltype.malloc(S)
            p.x = 100
            result = z(mycallback)
            return result * p.x

        return f
Exemple #22
0
def test_make_jitdriver_callbacks_5():
    def can_never_inline(x, y):
        assert x == 5
        assert y == 42.5
        return True

    CAN_NEVER_INLINE = lltype.Ptr(
        lltype.FuncType([lltype.Signed, lltype.Float], lltype.Bool))

    class FakeWarmRunnerDesc:
        rtyper = None
        cpu = None
        memory_manager = None

    class FakeJitDriverSD:
        jitdriver = None
        _green_args_spec = [lltype.Signed, lltype.Float]
        _get_printable_location_ptr = None
        _confirm_enter_jit_ptr = None
        _can_never_inline_ptr = llhelper(CAN_NEVER_INLINE, can_never_inline)
        _get_jitcell_at_ptr = None
        _should_unroll_one_iteration_ptr = None
        red_args_types = []

    state = WarmEnterState(FakeWarmRunnerDesc(), FakeJitDriverSD())
    state.make_jitdriver_callbacks()
    res = state.can_never_inline(5, 42.5)
    assert res is True
Exemple #23
0
def test_make_jitdriver_callbacks_2():
    def can_inline(x, y):
        assert x == 5
        assert y == 42.5
        return False

    CAN_INLINE = lltype.Ptr(
        lltype.FuncType([lltype.Signed, lltype.Float], lltype.Bool))

    class FakeCell:
        dont_trace_here = False

    class FakeWarmRunnerDesc:
        rtyper = None
        green_args_spec = [lltype.Signed, lltype.Float]
        can_inline_ptr = llhelper(CAN_INLINE, can_inline)
        get_printable_location_ptr = None
        confirm_enter_jit_ptr = None

    state = WarmEnterState(FakeWarmRunnerDesc())

    def jit_getter(*args):
        return FakeCell()

    state.jit_getter = jit_getter
    state.make_jitdriver_callbacks()
    res = state.can_inline_callable([ConstInt(5), ConstFloat(42.5)])
    assert res is False
Exemple #24
0
def test_make_jitdriver_callbacks_4():
    def confirm_enter_jit(x, y, z):
        assert x == 5
        assert y == 42.5
        assert z == 3
        return True

    ENTER_JIT = lltype.Ptr(
        lltype.FuncType([lltype.Signed, lltype.Float, lltype.Signed],
                        lltype.Bool))

    class FakeWarmRunnerDesc:
        rtyper = None
        cpu = None
        memory_manager = None

    class FakeJitDriverSD:
        jitdriver = None
        _green_args_spec = [lltype.Signed, lltype.Float]
        _get_printable_location_ptr = None
        _confirm_enter_jit_ptr = llhelper(ENTER_JIT, confirm_enter_jit)
        _can_never_inline_ptr = None
        _get_jitcell_at_ptr = None
        _should_unroll_one_iteration_ptr = None
        red_args_types = []

    state = WarmEnterState(FakeWarmRunnerDesc(), FakeJitDriverSD())
    state.make_jitdriver_callbacks()
    res = state.confirm_enter_jit(5, 42.5, 3)
    assert res is True
Exemple #25
0
def test_make_jitdriver_callbacks_3():
    def get_location(x, y):
        assert x == 5
        assert y == 42.5
        return "hi there"  # abuse the return type, but nobody checks it

    GET_LOCATION = lltype.Ptr(
        lltype.FuncType([lltype.Signed, lltype.Float], lltype.Ptr(rstr.STR)))

    class FakeWarmRunnerDesc:
        rtyper = None
        cpu = None
        memory_manager = None

    class FakeJitDriverSD:
        jitdriver = None
        _green_args_spec = [lltype.Signed, lltype.Float]
        _get_printable_location_ptr = llhelper(GET_LOCATION, get_location)
        _confirm_enter_jit_ptr = None
        _can_never_inline_ptr = None
        _get_jitcell_at_ptr = None
        _should_unroll_one_iteration_ptr = None
        red_args_types = []

    state = WarmEnterState(FakeWarmRunnerDesc(), FakeJitDriverSD())
    state.make_jitdriver_callbacks()
    res = state.get_location_str([ConstInt(5), constfloat(42.5)])
    assert res == "hi there"
Exemple #26
0
    def test_qsort(self):
        CMPFUNC = lltype.FuncType([rffi.VOIDP, rffi.VOIDP], rffi.INT)
        qsort = rffi.llexternal(
            'qsort',
            [rffi.VOIDP, rffi.SIZE_T, rffi.SIZE_T,
             lltype.Ptr(CMPFUNC)], lltype.Void)

        lst = [23, 43, 24, 324, 242, 34, 78, 5, 3, 10]
        A = lltype.Array(lltype.Signed, hints={'nolength': True})
        a = lltype.malloc(A, 10, flavor='raw')
        for i in range(10):
            a[i] = lst[i]

        SIGNEDPTR = lltype.Ptr(lltype.FixedSizeArray(lltype.Signed, 1))

        def my_compar(p1, p2):
            p1 = rffi.cast(SIGNEDPTR, p1)
            p2 = rffi.cast(SIGNEDPTR, p2)
            print 'my_compar:', p1[0], p2[0]
            return rffi.cast(rffi.INT, cmp(p1[0], p2[0]))

        qsort(rffi.cast(rffi.VOIDP, a), rffi.cast(rffi.SIZE_T, 10),
              rffi.cast(rffi.SIZE_T, llmemory.sizeof(lltype.Signed)),
              llhelper(lltype.Ptr(CMPFUNC), my_compar))

        for i in range(10):
            print a[i],
        print
        lst.sort()
        for i in range(10):
            assert a[i] == lst[i]
        lltype.free(a, flavor='raw')
        assert not ALLOCATED  # detects memory leaks in the test
Exemple #27
0
def test_get_accessor():

    G = lltype.FuncType([rclass.OBJECTPTR], lltype.Void)

    witness = []

    def getv(vinst):
        value = vinst.inst_v
        witness.append(value)
        return value

    def g(vobj):
        vobj = lltype.normalizeptr(vobj)
        LLV = lltype.typeOf(vobj).TO
        ACCESS = LLV.vable_access.TO
        access = lltype.malloc(ACCESS, immortal=True)
        access.get_inst_v = lltype.functionptr(ACCESS.get_inst_v.TO,
                                               'getv',
                                               _callable=getv)
        vobj.vable_access = access

    gptr = lltype.functionptr(G, 'g', _callable=g)

    def f(v):
        vinst = V(v)
        vobj = cast_instance_to_base_ptr(vinst)
        gptr(vobj)
        x = vinst.v
        return x

    res = interpret(f, [42])
    assert res == 42

    assert witness == [42]
Exemple #28
0
def annotate(translator, func, result, args):
    args = [arg.concretetype for arg in args]
    graph = translator.rtyper.annotate_helper(func, args)
    fptr = lltype.functionptr(lltype.FuncType(args, result.concretetype),
                              func.func_name,
                              graph=graph)
    c = inputconst(lltype.typeOf(fptr), fptr)
    return c
 def decorate(fn):
     fn_name = fn.func_name
     fn_ll_type = lltype.Ptr(lltype.FuncType(arg_types, res_type))
     # Enable for API tracing
     #fn = _trace_api_func(fn)
     _API_FUNCS.append((fn_name, fn, fn_ll_type, res_type, arg_types))
     fn._always_inline_ = True
     return fn
Exemple #30
0
def get_direct_call_op(argtypes, restype):
    FUNC = lltype.FuncType(argtypes, restype)
    fnptr = lltype.functionptr(FUNC, "g")  # no graph
    c_fnptr = const(fnptr)
    vars = [varoftype(TYPE) for TYPE in argtypes]
    v_result = varoftype(restype)
    op = SpaceOperation('direct_call', [c_fnptr] + vars, v_result)
    return op