[pypy-svn] r32697 - pypy/branch/jit-promotion/pypy/jit/codegen/llgraph/test

ac at codespeak.net ac at codespeak.net
Thu Sep 28 17:23:09 CEST 2006


Author: ac
Date: Thu Sep 28 17:23:09 2006
New Revision: 32697

Removed:
   pypy/branch/jit-promotion/pypy/jit/codegen/llgraph/test/test_llimpl.py
Modified:
   pypy/branch/jit-promotion/pypy/jit/codegen/llgraph/test/test_rgenop.py
Log:
(arre, pedronis)

refactored the old tests to exercize the external API. these new tests now pass again.



Modified: pypy/branch/jit-promotion/pypy/jit/codegen/llgraph/test/test_rgenop.py
==============================================================================
--- pypy/branch/jit-promotion/pypy/jit/codegen/llgraph/test/test_rgenop.py	(original)
+++ pypy/branch/jit-promotion/pypy/jit/codegen/llgraph/test/test_rgenop.py	Thu Sep 28 17:23:09 2006
@@ -2,46 +2,49 @@
 from pypy.jit.codegen.llgraph.llimpl import testgengraph
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.llinterp import LLInterpreter
-from pypy.rpython.test.test_llinterp import interpret
+from pypy.rpython.test.test_llinterp import interpret as _interpret
 from pypy.rpython.module.support import from_opaque_object
 from pypy.objspace.flow import model as flowmodel
+from pypy.rpython import annlowlevel
+
+class GenconstPolicy(annlowlevel.MixLevelAnnotatorPolicy):
+
+    def __init__(self):
+        pass
+
+genconst_policy = GenconstPolicy()
+
+def interpret(fn, args):
+    return _interpret(fn, args, policy=genconst_policy)
 
 F1 = lltype.FuncType([lltype.Signed], lltype.Signed)
+f1_token = rgenop.sigToken(F1)
+signed_tok = rgenop.kindToken(lltype.Signed)
+
+def runner(build):
+    def run(x):
+        fptr = build()
+        return fptr(x)
+    return run
+
 
 def build_square():
     """def square(v0): return v0*v0"""
-    sig_token = rgenop.sigToken(F1)
-    builder, graph, (gv0,) = rgenop.newgraph(sig_token)
+    builder, graph, (gv0,) = rgenop.newgraph(f1_token)
     gv1 = builder.genop2('int_mul', gv0, gv0)
-    builder.finish_and_return(sig_token, gv1)
-    gv_square = rgenop.gencallableconst(sig_token, "square", graph)
-    return gv_square
+    builder.finish_and_return(f1_token, gv1)
+    gv_square = rgenop.gencallableconst(f1_token, "square", graph)
+    square_ptr = gv_square.revealconst(lltype.Ptr(F1))
+    return square_ptr
+
 
 def test_square():
-    gv_square = build_square()
-    square_ptr = gv_square.revealconst(lltype.Ptr(F1))
+    square_ptr = build_square()
     res = testgengraph(square_ptr._obj.graph, [17])
     assert res == 289
-
-def test_rtype_newblock():
-    def emptyblock():
-        return newblock()
-    blockcontainer = interpret(emptyblock, [])
-    block = from_opaque_object(blockcontainer.obj)
-    assert isinstance(block, flowmodel.Block)
-
-def test_rtype_geninputarg():
-    def onearg():
-        block = newblock()
-        v0 = geninputarg(block, constTYPE(Signed))
-        return v0
-    opaquev = interpret(onearg, [])
-    v = from_opaque_object(opaquev)
-    assert isinstance(v, flowmodel.Variable)
     
-def test_rtype_build_square():
-    blockcontainer = interpret(build_square, [])
-    res = runblock(blockcontainer, F1, [17])
+def test_interpret_square():
+    res = interpret(runner(build_square), [17])
     assert res == 289
 
 def build_if():
@@ -52,27 +55,30 @@
         else:
             return v0
     """
-    block = newblock()
-    v0 = geninputarg(block, constTYPE(Signed))
-    const0 = genconst(0)
-    v1 = genop(block, 'int_lt', [v0, const0], constTYPE(Bool))
-    false_link, true_link = closeblock2(block, v1)
-    closereturnlink(true_link, const0)
-    closereturnlink(false_link, v0)
-    return block
+    builder, graph, (gv0,) = rgenop.newgraph(f1_token)
+ 
+    const0 = rgenop.genconst(0)
+    gv1 = builder.genop2('int_lt', gv0, const0)
+    false_builder = builder.jump_if_false(gv1)
+    builder.finish_and_return(f1_token, const0)
+    false_builder.finish_and_return(f1_token, gv0)
+    gv_if = rgenop.gencallableconst(f1_token, "if", graph)
+    if_ptr = gv_if.revealconst(lltype.Ptr(F1))
+    return if_ptr
 
 def test_if():
-    block = build_if()
-    res = runblock(block, F1, [-1])
+    if_ptr = build_if()
+    if_graph = if_ptr._obj.graph
+    res = testgengraph(if_graph, [-1])
     assert res == 0
-    res = runblock(block, F1, [42])
+    res = testgengraph(if_graph, [42])
     assert res == 42
 
-def test_rtype_build_if():
-    blockcontainer = interpret(build_if, [])
-    res = runblock(blockcontainer, F1, [-1])
+def test_interpret_if():
+    run_if = runner(build_if)
+    res = interpret(run_if, [-1])
     assert res == 0
-    res = runblock(blockcontainer, F1, [42])
+    res = interpret(run_if, [42])
     assert res == 42
 
 def build_loop():
@@ -80,76 +86,62 @@
     def f(v0):
         i = 1
         result = 1
-        while i <= v0:
+        while True:
             result *= i
             i += 1
+            if i > v0: break
         return result
     """
-    block = newblock()
-    v0 = geninputarg(block, constTYPE(Signed))
-    const1 = genconst(1)
-    link = closeblock1(block)
-    loopblock = newblock()
-    result0 = geninputarg(loopblock, constTYPE(Signed))
-    i0 = geninputarg(loopblock, constTYPE(Signed))
-    v1 = geninputarg(loopblock, constTYPE(Signed))
-    closelink(link, [const1, const1, v0], loopblock)
-    const1 = genconst(1)
-    result1 = genop(loopblock, 'int_mul', [result0, i0], constTYPE(Signed))
-    i1 = genop(loopblock, 'int_add', [i0, const1], constTYPE(Signed))
-    v2 = genop(loopblock, 'int_le', [i1, v1], constTYPE(Bool))
-    false_link, true_link = closeblock2(loopblock, v2)
-    closereturnlink(false_link, result1)
-    closelink(true_link, [result1, i1, v1], loopblock)
-    return block    
+    builder, graph, (gv0,) = rgenop.newgraph(f1_token)
+    const1 = rgenop.genconst(1)
+
+    args_gv = [const1, const1, gv0]
+    loopblock = builder.enter_next_block([signed_tok, signed_tok, signed_tok],
+                                         args_gv)
+    gv_result0, gv_i0, gv1 = args_gv
+    gv_result1 = builder.genop2('int_mul', gv_result0, gv_i0)
+    gv_i1 = builder.genop2('int_add', gv_i0, const1)
+    gv2 = builder.genop2('int_le', gv_i1, gv1)
+    loop_builder = builder.jump_if_true(gv2)
+    builder.finish_and_return(f1_token, gv_result1)
+    loop_builder.finish_and_goto([gv_result1, gv_i1, gv1], loopblock)
+
+    gv_loop = rgenop.gencallableconst(f1_token, "loop", graph)
+    loop_ptr = gv_loop.revealconst(lltype.Ptr(F1))
+    return loop_ptr
 
 def test_loop():
-    block = build_loop()
-    res = runblock(block, F1, [0])
+    loop_ptr = build_loop()
+    loop_graph = loop_ptr._obj.graph
+    res = testgengraph(loop_graph, [0])
     assert res == 1
-    res = runblock(block, F1, [1])
+    res = testgengraph(loop_graph, [1])
     assert res == 1
-    res = runblock(block, F1, [7])
+    res = testgengraph(loop_graph, [7])
     assert res == 5040
 
-def test_rtype_build_loop():
-    blockcontainer = interpret(build_loop, [])
-    res = runblock(blockcontainer, F1, [0])
+def test_interpret_loop():
+    run_loop = runner(build_loop)
+    res = interpret(run_loop, [0])
     assert res == 1
-    res = runblock(blockcontainer, F1, [1])
+    res = interpret(run_loop, [1])
     assert res == 1
-    res = runblock(blockcontainer, F1, [7])
+    res = interpret(run_loop, [7])
     assert res == 5040
 
-def test_rtype_void_constant_construction():
-    def fieldname_foo():
-        return constFieldName("foo")
-    res = interpret(fieldname_foo, [])
-    c = from_opaque_object(res)
-    assert isinstance(c, flowmodel.Constant)
-    assert c.concretetype == lltype.Void
-    assert c.value == "foo"
-
-    def type_Signed():
-        return constTYPE(lltype.Signed)
-    res = interpret(type_Signed, [])
-    c = from_opaque_object(res)
-    assert isinstance(c, flowmodel.Constant)
-    assert c.concretetype == lltype.Void
-    assert c.value == lltype.Signed
 
-def test_rtype_revealcosnt():
+def test_interpret_revealcosnt():
     def hide_and_reveal(v):
-        gv = genconst(v)
-        return revealconst(lltype.Signed, gv)
+        gv = rgenop.genconst(v)
+        return gv.revealconst(lltype.Signed)
     res = interpret(hide_and_reveal, [42])
     assert res == 42
 
     S = lltype.GcStruct('s', ('x', lltype.Signed))
     S_PTR = lltype.Ptr(S)
     def hide_and_reveal_p(p):
-        gv = genconst(p)
-        return revealconst(S_PTR, gv)
-    s = malloc(S)
+        gv = rgenop.genconst(p)
+        return gv.revealconst(S_PTR)
+    s = lltype.malloc(S)
     res = interpret(hide_and_reveal_p, [s])
     assert res == s



More information about the Pypy-commit mailing list