Example #1
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

    classes = [A1, A2]

    def f(n):
        x = classes[n]
        x()
    rtyper = support.annotate(f, [1])
    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')
Example #2
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

    classes = [A1, A2]

    def f(n):
        x = classes[n]
        x()
    rtyper = support.annotate(f, [1])
    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')
Example #3
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
Example #4
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
Example #5
0
def test_newlist_negativ():
    def f(n):
        l = [0] * n
        return len(l)

    rtyper = support.annotate(f, [-1])
    jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
    cw = CodeWriter(FakeCPU(rtyper), [jitdriver_sd])
    graphs = cw.find_all_graphs(FakePolicy())
    backend_optimizations(rtyper.annotator.translator, graphs=graphs)
    cw.make_jitcodes(verbose=True)
    s = jitdriver_sd.mainjitcode.dump()
    assert 'int_force_ge_zero' in s
    assert 'new_array' in s
Example #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)
Example #7
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)
Example #8
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

    classes = [
        (A1, B1),
        (A2, B2)
    ]

    def f(n):
        x, y = classes[n]
        return x().id + y().id + dont_look(n)
    rtyper = support.annotate(f, [0])
    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)
    names = set([value for key, value in cw.assembler.list_of_addr2name])
    assert 'dont_look' in names
Example #9
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

    classes = [
        (A1, B1),
        (A2, B2)
    ]

    def f(n):
        x, y = classes[n]
        return x().id + y().id + dont_look(n)
    rtyper = support.annotate(f, [0])
    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)
    names = set([value for key, value in cw.assembler.list_of_addr2name])
    assert 'dont_look' in names
Example #10
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_varsize__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
Example #11
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_varsize__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