Пример #1
0
def test_loop():
    def f(a, b):
        while a > 0:
            b += a
            a -= 1
        return b

    cw = CodeWriter()
    jitcode = cw.transform_func_to_jitcode(f, [5, 6])
    assert jitcode.code == (
        "\x00\x00\x00\x10\x00" "\x01\x01\x00\x01" "\x02\x00\x01\x00" "\x03\x00\x00" "\x04\x01"  # ends at 5
    )
    assert cw.assembler.insns == {
        "goto_if_not_int_gt/icL": 0,
        "int_add/ii>i": 1,
        "int_sub/ic>i": 2,
        "goto/L": 3,
        "int_return/i": 4,
    }
    assert jitcode.num_regs_i() == 2
    assert jitcode.num_regs_r() == 0
    assert jitcode.num_regs_f() == 0
    assert jitcode._live_vars(5) == "%i0 %i1"
    #
    from pypy.jit.codewriter.jitcode import MissingLiveness

    for i in range(len(jitcode.code) + 1):
        if i != 5:
            py.test.raises(MissingLiveness, jitcode._live_vars, i)
Пример #2
0
def test_int_abs():
    def f(n):
        return abs(n)

    rtyper = support.annotate(f, [35])
    jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
    cw = CodeWriter(FakeCPU(rtyper), [jitdriver_sd])
    cw.find_all_graphs(FakePolicy())
    cw.make_jitcodes(verbose=True)
    #
    s = jitdriver_sd.mainjitcode.dump()
    assert "inline_call_ir_i <JitCode '_ll_1_int_abs__Signed'>" in s
Пример #3
0
def test_instantiate():
    class A1:
        id = 651

    class A2(A1):
        id = 652

    class B1:
        id = 661

    class B2(B1):
        id = 662

    def dont_look(n):
        return n + 1

    def f(n):
        if n > 5:
            x, y = A1, B1
        else:
            x, y = A2, B2
        return x().id + y().id + dont_look(n)

    rtyper = support.annotate(f, [35])
    maingraph = rtyper.annotator.translator.graphs[0]
    cw = CodeWriter(FakeCPU(rtyper), [FakeJitDriverSD(maingraph)])
    cw.find_all_graphs(FakePolicy())
    cw.make_jitcodes(verbose=True)
    #
    assert len(cw.assembler.indirectcalltargets) == 4
    names = [jitcode.name for jitcode in cw.assembler.indirectcalltargets]
    for expected in ["A1", "A2", "B1", "B2"]:
        for name in names:
            if name.startswith("instantiate_") and name.endswith(expected):
                break
        else:
            assert 0, "missing instantiate_*_%s in:\n%r" % (expected, names)
    #
    print cw.assembler.list_of_addr2name
    names = dict.fromkeys([value for key, value in cw.assembler.list_of_addr2name])
    assert "A1" in names
    assert "B1" in names
    assert "A2" in names
    assert "B2" in names
    assert "dont_look" in names
Пример #4
0
def test_integration():
    from pypy.jit.metainterp.blackhole import BlackholeInterpBuilder

    def f(a, b):
        while a > 2:
            b += a
            a -= 1
        return b

    cw = CodeWriter()
    jitcode = cw.transform_func_to_jitcode(f, [5, 6])
    blackholeinterpbuilder = BlackholeInterpBuilder(cw)
    blackholeinterp = blackholeinterpbuilder.acquire_interp()
    blackholeinterp.setposition(jitcode, 0)
    blackholeinterp.setarg_i(0, 6)
    blackholeinterp.setarg_i(1, 100)
    blackholeinterp.run()
    assert blackholeinterp.get_tmpreg_i() == 100 + 6 + 5 + 4 + 3
Пример #5
0
def test_raw_malloc_and_access():
    TP = rffi.CArray(lltype.Signed)

    def f(n):
        a = lltype.malloc(TP, n, flavor="raw")
        a[0] = n
        res = a[0]
        lltype.free(a, flavor="raw")
        return res

    rtyper = support.annotate(f, [35])
    jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
    cw = CodeWriter(FakeCPU(rtyper), [jitdriver_sd])
    cw.find_all_graphs(FakePolicy())
    cw.make_jitcodes(verbose=True)
    #
    s = jitdriver_sd.mainjitcode.dump()
    assert "residual_call_ir_i $<* fn _ll_1_raw_malloc__Signed>" in s
    assert "setarrayitem_raw_i" in s
    assert "getarrayitem_raw_i" in s
    assert "residual_call_ir_v $<* fn _ll_1_raw_free__arrayPtr>" in s
Пример #6
0
def test_call():
    def ggg(x):
        return x * 2

    def fff(a, b):
        return ggg(b) - ggg(a)

    rtyper = support.annotate(fff, [35, 42])
    jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
    cw = CodeWriter(FakeCPU(rtyper), [jitdriver_sd])
    cw.find_all_graphs(FakePolicy())
    cw.make_jitcodes(verbose=True)
    jitcode = jitdriver_sd.mainjitcode
    print jitcode.dump()
    [jitcode2] = cw.assembler.descrs
    print jitcode2.dump()
    assert jitcode is not jitcode2
    assert jitcode.name == "fff"
    assert jitcode2.name == "ggg"
    assert "ggg" in jitcode.dump()
    assert lltype.typeOf(jitcode2.fnaddr) == llmemory.Address
    assert isinstance(jitcode2.calldescr, FakeCallDescr)
Пример #7
0
def test_instantiate_with_unreasonable_attr():
    # It is possible to have in real code the instantiate() function for
    # a class be dont-look-inside.  This is caused by the code that
    # initialize the instance attributes: if one attribute has a strange
    # type, the whole function is disabled.  Check that it still works.
    class MyFakePolicy:
        def look_inside_graph(self, graph):
            name = graph.name
            return not (name.startswith("instantiate_") and name.endswith("A2"))

    class A1:
        pass

    class A2(A1):
        pass

    def f(n):
        if n > 5:
            x = A1
        else:
            x = A2
        x()

    rtyper = support.annotate(f, [35])
    maingraph = rtyper.annotator.translator.graphs[0]
    cw = CodeWriter(FakeCPU(rtyper), [FakeJitDriverSD(maingraph)])
    cw.find_all_graphs(MyFakePolicy())
    cw.make_jitcodes(verbose=True)
    #
    names = [jitcode.name for jitcode in cw.assembler.indirectcalltargets]
    assert len(names) == 1
    assert names[0].startswith("instantiate_") and names[0].endswith("A1")
    #
    print cw.assembler.list_of_addr2name
    names = dict.fromkeys([value for key, value in cw.assembler.list_of_addr2name])
    assert "A1" in names
    assert "A2" in names