Esempio n. 1
0
    def test_profopt(self):
        def add(a, b):
            return a + b - b + b - b + b - b + b - b + b - b + b - b + b

        def entry_point(argv):
            tot = 0
            x = int(argv[1])
            while x > 0:
                tot = add(tot, x)
                x -= 1
            os.write(1, str(tot))
            return 0

        from pypy.translator.interactive import Translation
        # XXX this is mostly a "does not crash option"
        t = Translation(entry_point, backend='c', standalone=True, profopt="")
        # no counters
        t.backendopt()
        exe = t.compile()
        out = py.process.cmdexec("%s 500" % exe)
        assert int(out) == 500 * 501 / 2
        t = Translation(entry_point,
                        backend='c',
                        standalone=True,
                        profopt="",
                        noprofopt=True)
        # no counters
        t.backendopt()
        exe = t.compile()
        out = py.process.cmdexec("%s 500" % exe)
        assert int(out) == 500 * 501 / 2
Esempio n. 2
0
def test_translate_compiled_parser():
    r0 = Rule("expression", [["additive", "EOF"]])
    r1 = Rule("additive", [["multitive", "+", "additive"], ["multitive"]])
    r2 = Rule("multitive", [["primary", "*", "multitive"], ["primary"]])
    r3 = Rule("primary", [["(", "additive", ")"], ["decimal"]])
    r4 = Rule("decimal", [[symb] for symb in "0123456789"])
    p = PackratParser([r0, r1, r2, r3, r4], "expression")
    compiler = ParserCompiler(p)
    kls = compiler.compile()
    p = kls()
    tree = p.parse([Token(c, i, SourcePos(i, 0, i))
                        for i, c in enumerate(list("2*(3+4)") + ["EOF"])])
    data = [Token(c, i, SourcePos(i, 0, i))
               for i, c in enumerate(list("2*(3+4)") + ["EOF"])]
    print tree
    p = kls()
    def parse(choose):
        tree = p.parse(data)
        return tree.symbol + " " + "-%-".join([c.symbol for c in tree.children])
    t = Translation(parse)
    t.annotate([bool])
    t.backendopt()
    t.rtype()
    func = t.compile_c()
    res1 = parse(True)
    res2 = func(True)
    assert res1 == res2
Esempio n. 3
0
def test_profopt():
    if sys.platform == 'win32':
        py.test.skip("instrumentation support is unix only for now")
    def add(a,b):
        return a + b - b + b - b + b - b + b - b + b - b + b - b + b
    def entry_point(argv):
        tot =  0
        x = int(argv[1])
        while x > 0:
            tot = add(tot, x)
            x -= 1
        os.write(1, str(tot))
        return 0
    from pypy.translator.interactive import Translation
    # XXX this is mostly a "does not crash option"
    t = Translation(entry_point, backend='c', standalone=True, profopt="")
    # no counters
    t.backendopt()
    exe = t.compile()
    out = py.process.cmdexec("%s 500" % exe)
    assert int(out) == 500*501/2
    t = Translation(entry_point, backend='c', standalone=True, profopt="",
                    noprofopt=True)
    # no counters
    t.backendopt()
    exe = t.compile()
    out = py.process.cmdexec("%s 500" % exe)
    assert int(out) == 500*501/2
Esempio n. 4
0
    def test_profopt_mac_osx_bug(self):
        if sys.platform == 'win32':
            py.test.skip("no profopt on win32")

        def entry_point(argv):
            import os
            pid = os.fork()
            if pid:
                os.waitpid(pid, 0)
            else:
                os._exit(0)
            return 0

        from pypy.translator.interactive import Translation
        # XXX this is mostly a "does not crash option"
        t = Translation(entry_point, backend='c', standalone=True, profopt="")
        # no counters
        t.backendopt()
        exe = t.compile()
        #py.process.cmdexec(exe)
        t = Translation(entry_point,
                        backend='c',
                        standalone=True,
                        profopt="",
                        noprofopt=True)
        # no counters
        t.backendopt()
        exe = t.compile()
Esempio n. 5
0
def test_translate_compiled_parser():
    r0 = Rule("expression", [["additive", "EOF"]])
    r1 = Rule("additive", [["multitive", "+", "additive"], ["multitive"]])
    r2 = Rule("multitive", [["primary", "*", "multitive"], ["primary"]])
    r3 = Rule("primary", [["(", "additive", ")"], ["decimal"]])
    r4 = Rule("decimal", [[symb] for symb in "0123456789"])
    p = PackratParser([r0, r1, r2, r3, r4], "expression")
    compiler = ParserCompiler(p)
    kls = compiler.compile()
    p = kls()
    tree = p.parse([
        Token(c, i, SourcePos(i, 0, i))
        for i, c in enumerate(list("2*(3+4)") + ["EOF"])
    ])
    data = [
        Token(c, i, SourcePos(i, 0, i))
        for i, c in enumerate(list("2*(3+4)") + ["EOF"])
    ]
    print tree
    p = kls()

    def parse(choose):
        tree = p.parse(data)
        return tree.symbol + " " + "-%-".join(
            [c.symbol for c in tree.children])

    t = Translation(parse)
    t.annotate([bool])
    t.backendopt()
    t.rtype()
    func = t.compile_c()
    res1 = parse(True)
    res2 = func(True)
    assert res1 == res2
Esempio n. 6
0
    def test_prof_inline(self):
        if sys.platform == 'win32':
            py.test.skip("instrumentation support is unix only for now")

        def add(a, b):
            return a + b - b + b - b + b - b + b - b + b - b + b - b + b

        def entry_point(argv):
            tot = 0
            x = int(argv[1])
            while x > 0:
                tot = add(tot, x)
                x -= 1
            os.write(1, str(tot))
            return 0

        from pypy.translator.interactive import Translation
        t = Translation(entry_point, backend='c', standalone=True)
        # no counters
        t.backendopt(inline_threshold=100, profile_based_inline="500")
        exe = t.compile()
        out = py.process.cmdexec("%s 500" % exe)
        assert int(out) == 500 * 501 / 2

        t = Translation(entry_point, backend='c', standalone=True)
        # counters
        t.backendopt(inline_threshold=all.INLINE_THRESHOLD_FOR_TEST * 0.5,
                     profile_based_inline="500")
        exe = t.compile()
        out = py.process.cmdexec("%s 500" % exe)
        assert int(out) == 500 * 501 / 2
Esempio n. 7
0
def test_translate_pypackrat_regex():
    from pypy.rlib.parsing.pypackrat import PackratParser

    class parser(PackratParser):
        """
        num:
            `([1-9][0-9]*)|0`;
        """

    print parser._code

    def parse(s):
        p = parser(s)
        return p.num()

    res = parse("1234")
    assert res == '1234'
    t = Translation(parse)
    t.annotate([str])
    t.rtype()
    t.backendopt()
    if option.view:
        t.view()
    func = t.compile_c()
    res = func("12345")
    assert res == '12345'
    res = func("0")
    assert res == '0'
Esempio n. 8
0
 def test_profopt(self):
     def add(a,b):
         return a + b - b + b - b + b - b + b - b + b - b + b - b + b
     def entry_point(argv):
         tot =  0
         x = int(argv[1])
         while x > 0:
             tot = add(tot, x)
             x -= 1
         os.write(1, str(tot))
         return 0
     from pypy.translator.interactive import Translation
     # XXX this is mostly a "does not crash option"
     t = Translation(entry_point, backend='c', standalone=True, profopt="100")
     # no counters
     t.backendopt()
     exe = t.compile()
     out = py.process.cmdexec("%s 500" % exe)
     assert int(out) == 500*501/2
     t = Translation(entry_point, backend='c', standalone=True, profopt="100",
                     noprofopt=True)
     # no counters
     t.backendopt()
     exe = t.compile()
     out = py.process.cmdexec("%s 500" % exe)
     assert int(out) == 500*501/2
Esempio n. 9
0
def test_translate_ast_visitor():
    from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
    regexs, rules, ToAST = parse_ebnf("""
DECIMAL: "0|[1-9][0-9]*";
IGNORE: " ";
additive: multitive ["+!"] additive | <multitive>;
multitive: primary ["*!"] multitive | <primary>; #nonsense!
primary: "(" <additive> ")" | <DECIMAL>;
""")
    parse = make_parse_function(regexs, rules)

    def f():
        tree = parse("(0 +! 10) *! (999 +! 10) +! 1")
        tree = ToAST().visit_additive(tree)
        assert len(tree) == 1
        tree = tree[0]
        return tree.symbol + " " + "-&-".join(
            [c.symbol for c in tree.children])

    res1 = f()
    t = Translation(f)
    t.annotate()
    t.rtype()
    t.backendopt()
    func = t.compile_c()
    res2 = func()
    assert res1 == res2
Esempio n. 10
0
def test_prof_inline():
    if sys.platform == 'win32':
        py.test.skip("instrumentation support is unix only for now")
    def add(a,b):
        return a + b - b + b - b + b - b + b - b + b - b + b - b + b
    def entry_point(argv):
        tot =  0
        x = int(argv[1])
        while x > 0:
            tot = add(tot, x)
            x -= 1
        os.write(1, str(tot))
        return 0
    from pypy.translator.interactive import Translation
    t = Translation(entry_point, backend='c', standalone=True)
    # no counters
    t.backendopt(inline_threshold=100, profile_based_inline="500")
    exe = t.compile()
    out = py.process.cmdexec("%s 500" % exe)
    assert int(out) == 500*501/2

    t = Translation(entry_point, backend='c', standalone=True)
    # counters
    t.backendopt(inline_threshold=all.INLINE_THRESHOLD_FOR_TEST*0.5,
                 profile_based_inline="500")
    exe = t.compile()
    out = py.process.cmdexec("%s 500" % exe)
    assert int(out) == 500*501/2
Esempio n. 11
0
def compile_rex(rex):
    try:
        from pypy.translator.interactive import Translation
    except ImportError:
        py.test.skip("pypy not found")
    fda = rex.make_automaton().make_deterministic()
    fda.optimize()
    fn = fda.make_code()
    t = Translation(fn)
    t.backendopt([str], backend="c")
    if py.test.config.option.view:
        t.view()
    return t.compile_c()
Esempio n. 12
0
def test_simple_backendopt():
    def f(x, y):
        return x, y

    t = Translation(f, [int, int], backend='c')
    t.backendopt()

    assert 'backendopt_lltype' in t.driver.done

    t = Translation(f, [int, int])
    t.backendopt()

    assert 'backendopt_lltype' in t.driver.done
def test_simple_backendopt():
    def f(x, y):
        return x,y

    t = Translation(f, [int, int], backend='c')
    t.backendopt()
    
    assert 'backendopt_lltype' in t.driver.done

    t = Translation(f, [int, int])
    t.backendopt()

    assert 'backendopt_lltype' in t.driver.done
Esempio n. 14
0
def compile_rex(rex):
    try:
        from pypy.translator.interactive import Translation
    except ImportError:
        py.test.skip("pypy not found")
    fda = rex.make_automaton().make_deterministic()
    fda.optimize()
    fn = fda.make_code()
    t = Translation(fn)
    t.backendopt([str], backend="c")
    if py.test.config.option.view:
        t.view()
    return t.compile_c()
def test_parser():
    def f(x):
        if x:
            s = "a(X, Y, Z)."
        else:
            s = "f(a, X, _, _, X, f(X, 2.455))."
        term = parsing.parse_file(s)
        assert isinstance(term, parsing.Nonterminal)
        return term.symbol
    assert f(True) == "file"
    assert f(True) == "file"
    t = Translation(f)
    t.annotate([bool])
    t.rtype()
    t.backendopt()
    func = t.compile_c()
    assert func(True) == "file"
    assert func(False) == "file"
Esempio n. 16
0
def test_parser():
    def f(x):
        if x:
            s = "a(X, Y, Z)."
        else:
            s = "f(a, X, _, _, X, f(X, 2.455))."
        term = parsing.parse_file(s)
        assert isinstance(term, parsing.Nonterminal)
        return term.symbol

    assert f(True) == "file"
    assert f(True) == "file"
    t = Translation(f)
    t.annotate([bool])
    t.rtype()
    t.backendopt()
    func = t.compile_c()
    assert func(True) == "file"
    assert func(False) == "file"
Esempio n. 17
0
def test_compile_recognizer():
    try:
        from pypy.translator.interactive import Translation
    except ImportError:
        py.test.skip("pypy not found on path")
    a = DFA()
    s0 = a.add_state("start")
    s1 = a.add_state()
    s2 = a.add_state(final=True)
    a[s0, "a"] = s0
    a[s0, "c"] = s1
    a[s0, "b"] = s2
    a[s1, "b"] = s2
    recognize = a.make_code()
    t = Translation(recognize)
    t.backendopt([str], backend="c")
    cfn = t.compile_c()
    assert cfn("aaaaaaaaaab")
    assert cfn("b")
    assert cfn("aaaacb")
Esempio n. 18
0
def test_translate_pypackrat():
    from pypy.rlib.parsing.pypackrat import PackratParser

    class parser(PackratParser):
        """
        expr:
            additive;
        additive:
            a = additive
            '-'
            b = multitive
            return {'(%s - %s)' % (a, b)}
          | multitive;
        multitive:
            a = multitive
            '*'
            b = simple
            return {'(%s * %s)' % (a, b)}
          | simple;
        simple:
            ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9');
        """

    print parser._code

    def parse(s):
        p = parser(s)
        return p.expr()

    res = parse("5-5-5")
    assert res == '((5 - 5) - 5)'
    t = Translation(parse)
    t.annotate([str])
    t.rtype()
    t.backendopt()
    if option.view:
        t.view()
    func = t.compile_c()
    res = func("5-5-5")
    assert res == '((5 - 5) - 5)'
 def test_profopt_mac_osx_bug(self):
     if sys.platform == 'win32':
         py.test.skip("no profopt on win32")
     def entry_point(argv):
         import os
         pid = os.fork()
         if pid:
             os.waitpid(pid, 0)
         else:
             os._exit(0)
         return 0
     from pypy.translator.interactive import Translation
     # XXX this is mostly a "does not crash option"
     t = Translation(entry_point, backend='c', standalone=True, profopt="")
     # no counters
     t.backendopt()
     exe = t.compile()
     #py.process.cmdexec(exe)
     t = Translation(entry_point, backend='c', standalone=True, profopt="",
                     noprofopt=True)
     # no counters
     t.backendopt()
     exe = t.compile()
Esempio n. 20
0
def test_translate_pypackrat():
    from pypy.rlib.parsing.pypackrat import PackratParser
    class parser(PackratParser):
        """
        expr:
            additive;
        additive:
            a = additive
            '-'
            b = multitive
            return {'(%s - %s)' % (a, b)}
          | multitive;
        multitive:
            a = multitive
            '*'
            b = simple
            return {'(%s * %s)' % (a, b)}
          | simple;
        simple:
            ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9');
        """
    print parser._code
    def parse(s):
        p = parser(s)
        return p.expr()
    res = parse("5-5-5")
    assert res == '((5 - 5) - 5)'
    t = Translation(parse)
    t.annotate([str])
    t.rtype()
    t.backendopt()
    if option.view:
        t.view()
    func = t.compile_c()
    res = func("5-5-5")
    assert res == '((5 - 5) - 5)'
Esempio n. 21
0
def test_translate_ast_visitor():
    from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
    regexs, rules, ToAST = parse_ebnf("""
DECIMAL: "0|[1-9][0-9]*";
IGNORE: " ";
additive: multitive ["+!"] additive | <multitive>;
multitive: primary ["*!"] multitive | <primary>; #nonsense!
primary: "(" <additive> ")" | <DECIMAL>;
""")
    parse = make_parse_function(regexs, rules)
    def f():
        tree = parse("(0 +! 10) *! (999 +! 10) +! 1")
        tree = ToAST().visit_additive(tree)
        assert len(tree) == 1
        tree = tree[0]
        return tree.symbol + " " + "-&-".join([c.symbol for c in tree.children])
    res1 = f()
    t = Translation(f)
    t.annotate()
    t.rtype()
    t.backendopt()
    func = t.compile_c()
    res2 = func()
    assert res1 == res2
Esempio n. 22
0
def test_translate_pypackrat_regex():
    from pypy.rlib.parsing.pypackrat import PackratParser
    class parser(PackratParser):
        """
        num:
            `([1-9][0-9]*)|0`;
        """
    print parser._code
    def parse(s):
        p = parser(s)
        return p.num()
    res = parse("1234")
    assert res == '1234'
    t = Translation(parse)
    t.annotate([str])
    t.rtype()
    t.backendopt()
    if option.view:
        t.view()
    func = t.compile_c()
    res = func("12345")
    assert res == '12345'
    res = func("0")
    assert res == '0'