[pypy-svn] r29507 - in pypy/dist/pypy/jit: llabstractinterp/test timeshifter/test tl tl/test

arigo at codespeak.net arigo at codespeak.net
Thu Jun 29 19:14:54 CEST 2006


Author: arigo
Date: Thu Jun 29 19:14:51 2006
New Revision: 29507

Added:
   pypy/dist/pypy/jit/timeshifter/test/test_tl.py   (contents, props changed)
Modified:
   pypy/dist/pypy/jit/llabstractinterp/test/test_jit_tl.py
   pypy/dist/pypy/jit/tl/opcode.py
   pypy/dist/pypy/jit/tl/test/test_tl.py
   pypy/dist/pypy/jit/tl/tl.py
Log:
(arre, pedronis, arigo)

Modified the TL to take an input integer argument, available with a
PUSHARG instruction.  There are also now two variants of the TL, with or
without the recursive call.

Corresponding JIT test in-progress.



Modified: pypy/dist/pypy/jit/llabstractinterp/test/test_jit_tl.py
==============================================================================
--- pypy/dist/pypy/jit/llabstractinterp/test/test_jit_tl.py	(original)
+++ pypy/dist/pypy/jit/llabstractinterp/test/test_jit_tl.py	Thu Jun 29 19:14:51 2006
@@ -11,7 +11,7 @@
 
 def setup_module(mod):
     t = TranslationContext()
-    t.buildannotator().build_types(tl.interp, [str, int])
+    t.buildannotator().build_types(tl.interp, [str, int, int])
     rtyper = t.buildrtyper()
     rtyper.specialize()
     #inline.auto_inlining(t, 0.3)
@@ -26,8 +26,9 @@
              1: 0}
     graph2 = interp.eval(graph1, hints)
 
-    result1 = llinterp.eval_graph(graph1, [string_repr.convert_const(code), 0])
-    result2 = llinterp.eval_graph(graph2, [])
+    result1 = llinterp.eval_graph(graph1, [string_repr.convert_const(code),
+                                           0, 0])
+    result2 = llinterp.eval_graph(graph2, [0])
 
     assert result1 == result2
 

Added: pypy/dist/pypy/jit/timeshifter/test/test_tl.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/timeshifter/test/test_tl.py	Thu Jun 29 19:14:51 2006
@@ -0,0 +1,15 @@
+from pypy.rpython.lltypesystem.rstr import string_repr
+from pypy.jit.timeshifter.test.test_timeshift import timeshift
+from pypy.jit.timeshifter.test.test_vlist import P_OOPSPEC
+
+from pypy.jit.tl import tl
+from pypy.jit.tl.test.test_tl import FACTORIAL_SOURCE
+
+
+def test_tl():
+    import py; py.test.skip("in-progress")
+    code = tl.compile(FACTORIAL_SOURCE)
+    ll_code = string_repr.convert_const(code)
+    insns, res = timeshift(tl.interp_without_call, [ll_code, 0, 5], [0, 1],
+                           policy=P_OOPSPEC)
+    assert res == 120

Modified: pypy/dist/pypy/jit/tl/opcode.py
==============================================================================
--- pypy/dist/pypy/jit/tl/opcode.py	(original)
+++ pypy/dist/pypy/jit/tl/opcode.py	Thu Jun 29 19:14:51 2006
@@ -31,6 +31,8 @@
 opcode(20, "CALL")  #1 operand offset
 opcode(21, "RETURN")
 
-opcode(22, "INVALID")
+opcode(22, "PUSHARG")
+
+opcode(23, "INVALID")
 
 del opcode

Modified: pypy/dist/pypy/jit/tl/test/test_tl.py
==============================================================================
--- pypy/dist/pypy/jit/tl/test/test_tl.py	(original)
+++ pypy/dist/pypy/jit/tl/test/test_tl.py	Thu Jun 29 19:14:51 2006
@@ -48,7 +48,7 @@
 
 def test_tl_translatable():
     code = list2bytecode([PUSH,42, PUSH,100, ADD])
-    fn = translate(interp, [str, int])
+    fn = translate(interp, [str, int, int])
     assert interp(code) == fn(code)
 
 def test_swap():
@@ -170,7 +170,7 @@
                                   PUSH,2, RETURN,
                                   PUSH,4, PUSH,5, ADD, RETURN])
 
-def test_factorial():
+def test_factorial_seven():
     code = compile('''
             PUSH 1   #  accumulator
             PUSH 7   #  N
@@ -197,7 +197,7 @@
     res = interp(code)
     assert res == 5040
 
-def test_factorial_harder():
+def test_factorial_seven_harder():
     code = compile('''
             PUSH 1   #  accumulator
             PUSH 7   #  N
@@ -225,3 +225,33 @@
     ''')
     res = interp(code)
     assert res == 5040
+
+
+FACTORIAL_SOURCE = '''
+            PUSH 1   #  accumulator
+            PUSHARG
+
+        start:
+            PICK 0
+            PUSH 1
+            LE
+            BR_COND exit
+
+            SWAP
+            PICK 1
+            MUL
+            SWAP
+            PUSH 1
+            SUB
+            PUSH 1
+            BR_COND start
+
+        exit:
+            POP
+            RETURN
+    '''
+
+def test_factorial_with_arg():
+    code = compile(FACTORIAL_SOURCE)
+    res = interp(code, 0, 6)
+    assert res == 720

Modified: pypy/dist/pypy/jit/tl/tl.py
==============================================================================
--- pypy/dist/pypy/jit/tl/tl.py	(original)
+++ pypy/dist/pypy/jit/tl/tl.py	Thu Jun 29 19:14:51 2006
@@ -11,119 +11,130 @@
         t = -(-ord(c) & 0xff)
     return t
 
-def interp(code='', pc=0):
-    if not isinstance(code,str):
-        raise TypeError("code '%s' should be a string" % str(code))
-
-    code_len = len(code)
-    stack = []
-
-    while pc < code_len:
-        opcode = ord(code[pc])
-        opcode = hint(opcode, concrete=True)
-        pc += 1
+def make_interp(supports_call):
+    def interp(code='', pc=0, inputarg=0):
+        if not isinstance(code,str):
+            raise TypeError("code '%s' should be a string" % str(code))
 
-        if opcode == NOP:
-            pass
+        code_len = len(code)
+        stack = []
 
-        elif opcode == PUSH:
-            stack.append( char2int(code[pc]) )
+        while pc < code_len:
+            opcode = ord(code[pc])
+            opcode = hint(opcode, concrete=True)
             pc += 1
 
-        elif opcode == POP:
-            stack.pop()
+            if opcode == NOP:
+                pass
 
-        elif opcode == SWAP:
-            a, b = stack.pop(), stack.pop()
-            stack.append(a)
-            stack.append(b)
-
-        elif opcode == ROLL: #rotate stack top to somewhere below
-            r = char2int(code[pc])
-            if r < -1:
-                i = len(stack) + r
-                if i < 0:
-                    raise IndexError
-                stack.insert( i, stack.pop() )
-            elif r > 1:
-                i = len(stack) - r
-                if i < 0:
-                    raise IndexError
-                stack.append(stack.pop(i))
-                
-            pc += 1
+            elif opcode == PUSH:
+                stack.append( char2int(code[pc]) )
+                pc += 1
 
-        elif opcode == PICK:
-            stack.append( stack[-1 - char2int(code[pc])] )
-            pc += 1
+            elif opcode == POP:
+                stack.pop()
 
-        elif opcode == PUT:
-            stack[-1 - char2int(code[pc])] = stack.pop()
-            pc += 1
+            elif opcode == SWAP:
+                a, b = stack.pop(), stack.pop()
+                stack.append(a)
+                stack.append(b)
 
-        elif opcode == ADD:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b + a )
-
-        elif opcode == SUB:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b - a )
-
-        elif opcode == MUL:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b * a )
-
-        elif opcode == DIV:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b / a )
-
-        elif opcode == EQ:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b == a )
-
-        elif opcode == NE:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b != a )
-
-        elif opcode == LT:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b <  a )
-
-        elif opcode == LE:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b <= a )
-
-        elif opcode == GT:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b >  a )
-
-        elif opcode == GE:
-            a, b = stack.pop(), stack.pop()
-            stack.append( b >= a )
-
-        elif opcode == BR_COND:
-            if stack.pop():
-                pc += char2int(code[pc])
-            pc += 1
+            elif opcode == ROLL: #rotate stack top to somewhere below
+                r = char2int(code[pc])
+                if r < -1:
+                    i = len(stack) + r
+                    if i < 0:
+                        raise IndexError
+                    stack.insert( i, stack.pop() )
+                elif r > 1:
+                    i = len(stack) - r
+                    if i < 0:
+                        raise IndexError
+                    stack.append(stack.pop(i))
 
-        elif opcode == BR_COND_STK:
-            offset = stack.pop()
-            if stack.pop():
-                pc += hint(offset, forget=True)
+                pc += 1
 
-        elif opcode == CALL:
-            offset = char2int(code[pc])
-            pc += 1
-            res = interp(code, pc + offset)
-            stack.append( res )
+            elif opcode == PICK:
+                stack.append( stack[-1 - char2int(code[pc])] )
+                pc += 1
+
+            elif opcode == PUT:
+                stack[-1 - char2int(code[pc])] = stack.pop()
+                pc += 1
+
+            elif opcode == ADD:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b + a )
+
+            elif opcode == SUB:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b - a )
+
+            elif opcode == MUL:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b * a )
+
+            elif opcode == DIV:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b / a )
+
+            elif opcode == EQ:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b == a )
+
+            elif opcode == NE:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b != a )
+
+            elif opcode == LT:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b <  a )
+
+            elif opcode == LE:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b <= a )
+
+            elif opcode == GT:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b >  a )
+
+            elif opcode == GE:
+                a, b = stack.pop(), stack.pop()
+                stack.append( b >= a )
+
+            elif opcode == BR_COND:
+                if stack.pop():
+                    pc += char2int(code[pc])
+                pc += 1
+
+            elif opcode == BR_COND_STK:
+                offset = stack.pop()
+                if stack.pop():
+                    pc += hint(offset, forget=True)
+
+            elif supports_call and opcode == CALL:
+                offset = char2int(code[pc])
+                pc += 1
+                res = interp(code, pc + offset)
+                stack.append( res )
+
+            elif opcode == RETURN:
+                break
+
+            elif opcode == PUSHARG:
+                stack.append( inputarg )
+
+            else:
+                raise RuntimeError("unknown opcode: " + str(opcode))
+
+        return stack[-1]
+
+    return interp
 
-        elif opcode == RETURN:
-            break
 
-        else:
-            raise RuntimeError("unknown opcode: " + str(opcode))
+interp              = make_interp(supports_call = True)
+interp_without_call = make_interp(supports_call = False)
 
-    return stack[-1]
 
 def compile(code=''):
     bytecode = []



More information about the Pypy-commit mailing list