Beispiel #1
0
def test_remove_same_as():
    def nothing(x):
        return x

    def f():
        nothing(False)
        if nothing(True):
            return 42
        else:
            return 666

    t = TranslationContext()
    t.buildannotator().build_types(f, [])
    t.buildrtyper().specialize()
    # now we make the 'if True' appear
    f_graph = graphof(t, f)
    simple_inline_function(t, nothing, f_graph)
    # here, the graph looks like  v21=same_as(True);  exitswitch: v21
    remove_same_as(f_graph)
    t.checkgraphs()
    # only one path should be left
    for block in f_graph.iterblocks():
        assert len(block.exits) <= 1

    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(f_graph, [])
    assert result == 42
Beispiel #2
0
def test_remove_same_as():
    def nothing(x):
        return x

    def f():
        nothing(False)
        if nothing(True):
            return 42
        else:
            return 666

    t = TranslationContext()
    t.buildannotator().build_types(f, [])
    t.buildrtyper().specialize()
    # now we make the 'if True' appear
    f_graph = graphof(t, f)
    simple_inline_function(t, nothing, f_graph)
    # here, the graph looks like  v21=same_as(True);  exitswitch: v21
    remove_same_as(f_graph)
    t.checkgraphs()
    # only one path should be left
    for block in f_graph.iterblocks():
        assert len(block.exits) <= 1

    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(f_graph, [])
    assert result == 42
Beispiel #3
0
 def test_inline_all(self):
     def g(x):
         return x + 1
     def f(x):
         return g(x) * g(x+1) * g(x+2) * g(x+3) * g(x+4) * g(x+5)
     t = self.translate(f, [int])
     sanity_check(t)    # also check before inlining (so we don't blame it)
     simple_inline_function(t, graphof(t, g), graphof(t, f))
     sanity_check(t)
     assert summary(graphof(t, f)) == {'int_add': 11, 'int_mul': 5}
     interp = LLInterpreter(t.rtyper)
     result = interp.eval_graph(graphof(t, f), [10])
     assert result == f(10)
Beispiel #4
0
    def test_inline_all(self):
        def g(x):
            return x + 1

        def f(x):
            return g(x) * g(x + 1) * g(x + 2) * g(x + 3) * g(x + 4) * g(x + 5)

        t = self.translate(f, [int])
        sanity_check(t)  # also check before inlining (so we don't blame it)
        simple_inline_function(t, graphof(t, g), graphof(t, f))
        sanity_check(t)
        assert summary(graphof(t, f)) == {"int_add": 11, "int_mul": 5}
        interp = LLInterpreter(t.rtyper)
        result = interp.eval_graph(graphof(t, f), [10])
        assert result == f(10)
Beispiel #5
0
 def test_for_loop(self):
     def f(x):
         result = 0
         for i in range(0, x):
             result += i
         return result
     t = self.translate(f, [int])
     sanity_check(t)    # also check before inlining (so we don't blame it)
     for graph in t.graphs:
         if graph.name.startswith('ll_rangenext'):
             break
     else:
         assert 0, "cannot find ll_rangenext_*() function"
     simple_inline_function(t, graph, graphof(t, f))
     sanity_check(t)
     interp = LLInterpreter(t.rtyper)
     result = interp.eval_graph(graphof(t, f), [10])
     assert result == 45
Beispiel #6
0
    def test_inline_all_exc(self):
        def g(x):
            if x < -100:
                raise ValueError
            return x + 1

        def f(x):
            n1 = g(x) * g(x + 1)
            try:
                n2 = g(x + 2) * g(x + 3)
            except ValueError:
                n2 = 1
            n3 = g(x + 4) * g(x + 5)
            return n1 * n2 * n3

        t = self.translate(f, [int])
        sanity_check(t)  # also check before inlining (so we don't blame it)
        simple_inline_function(t, graphof(t, g), graphof(t, f))
        sanity_check(t)
        assert summary(graphof(t, f)) == {"int_add": 11, "int_mul": 5, "cast_pointer": 12, "getfield": 6, "int_lt": 6}
        interp = LLInterpreter(t.rtyper)
        result = interp.eval_graph(graphof(t, f), [10])
        assert result == f(10)
Beispiel #7
0
 def test_inline_all_exc(self):
     def g(x):
         if x < -100:
             raise ValueError
         return x + 1
     def f(x):
         n1 = g(x) * g(x+1)
         try:
             n2 = g(x+2) * g(x+3)
         except ValueError:
             n2 = 1
         n3 = g(x+4) * g(x+5)
         return n1 * n2 * n3
     t = self.translate(f, [int])
     sanity_check(t)    # also check before inlining (so we don't blame it)
     simple_inline_function(t, graphof(t, g), graphof(t, f))
     sanity_check(t)
     assert summary(graphof(t, f)) == {'int_add': 11, 'int_mul': 5,
                                       'cast_pointer': 12, 'getfield': 6,
                                       'int_lt': 6}
     interp = LLInterpreter(t.rtyper)
     result = interp.eval_graph(graphof(t, f), [10])
     assert result == f(10)