[pypy-svn] r20855 - in pypy/dist/pypy/jit: . test

ericvrp at codespeak.net ericvrp at codespeak.net
Wed Dec 7 18:30:26 CET 2005


Author: ericvrp
Date: Wed Dec  7 18:30:24 2005
New Revision: 20855

Added:
   pypy/dist/pypy/jit/opcode.py
      - copied, changed from r20848, pypy/dist/pypy/jit/bytecode.py
Removed:
   pypy/dist/pypy/jit/bytecode.py
Modified:
   pypy/dist/pypy/jit/test/test_tl.py
   pypy/dist/pypy/jit/tl.py
Log:
(arre, ericvrp)
Added simple source to bytecode parser/compiler.


Modified: pypy/dist/pypy/jit/test/test_tl.py
==============================================================================
--- pypy/dist/pypy/jit/test/test_tl.py	(original)
+++ pypy/dist/pypy/jit/test/test_tl.py	Wed Dec  7 18:30:24 2005
@@ -1,12 +1,8 @@
 import py
 import operator
-from pypy.jit.tl import interp
-from pypy.jit.bytecode import *
+from pypy.jit.tl import interp, compile
+from pypy.jit.opcode import *
 
-#from pypy.rpython.l3interp import l3interp
-#from pypy.rpython.l3interp import model
-#from pypy.rpython.l3interp.model import Op
-#from pypy.translator.c.test.test_genc import compile 
 from pypy.translator.translator import TranslationContext
 from pypy.annotation import policy
 
@@ -25,44 +21,44 @@
     builder.import_module()
     return builder.get_entry_point()  
 
-def compile(insn):
+def list2bytecode(insn):
     return ''.join([chr(i & 0xff) for i in insn])
 
 # actual tests go here
 
 def test_tl_push():
-    assert interp(compile([PUSH, 16])) == 16
+    assert interp(list2bytecode([PUSH, 16])) == 16
 
 def test_tl_pop():
-    assert interp( compile([PUSH,16, PUSH,42, PUSH,100, POP]) ) == 42
+    assert interp( list2bytecode([PUSH,16, PUSH,42, PUSH,100, POP]) ) == 42
 
 def test_tl_add():
-    assert interp( compile([PUSH,42, PUSH,100, ADD]) ) == 142
-    assert interp( compile([PUSH,16, PUSH,42, PUSH,100, ADD]) ) == 142
+    assert interp( list2bytecode([PUSH,42, PUSH,100, ADD]) ) == 142
+    assert interp( list2bytecode([PUSH,16, PUSH,42, PUSH,100, ADD]) ) == 142
 
 def test_tl_error():
-    py.test.raises(IndexError, interp,compile([POP]))
-    py.test.raises(IndexError, interp,compile([ADD]))
-    py.test.raises(IndexError, interp,compile([PUSH,100, ADD]) )
+    py.test.raises(IndexError, interp,list2bytecode([POP]))
+    py.test.raises(IndexError, interp,list2bytecode([ADD]))
+    py.test.raises(IndexError, interp,list2bytecode([PUSH,100, ADD]) )
 
 def test_tl_invalid_codetype():
     py.test.raises(TypeError, interp,[INVALID])
 
 def test_tl_invalid_bytecode():
-    py.test.raises(RuntimeError, interp,compile([INVALID]))
+    py.test.raises(RuntimeError, interp,list2bytecode([INVALID]))
 
 def test_tl_translatable():
-    code = compile([PUSH,42, PUSH,100, ADD])
+    code = list2bytecode([PUSH,42, PUSH,100, ADD])
     fn = translate(interp, [str])
     assert interp(code) == fn(code)
 
 def test_swap():
     code = [PUSH,42, PUSH, 84]
-    assert interp(compile(code)) == 84
+    assert interp(list2bytecode(code)) == 84
     code.append(SWAP)
-    assert interp(compile(code)) == 42
+    assert interp(list2bytecode(code)) == 42
     code.append(POP)
-    assert interp(compile(code)) == 84
+    assert interp(list2bytecode(code)) == 84
 
 def test_pick():
     values = [7, 8, 9]
@@ -71,7 +67,7 @@
         code.extend([PUSH, v])
 
     for i, v in enumerate(values):
-        assert interp(compile(code + [PICK,i])) == v
+        assert interp(list2bytecode(code + [PICK,i])) == v
 
 def test_put():
     values = [1,2,7,-3]
@@ -80,7 +76,7 @@
         code += [PUSH,v, PUT,i]
 
     for i, v in enumerate(values):
-        assert interp(compile(code + [PICK,i])) == v
+        assert interp(list2bytecode(code + [PICK,i])) == v
 
 ops = [ (ADD, operator.add, ((2, 4), (1, 1), (-1, 1))),
         (SUB, operator.sub, ((2, 4), (4, 2), (1, 1))),
@@ -98,35 +94,68 @@
       for insn, pyop, values in ops:
           for first, second in values:
               code = [PUSH, first, PUSH, second, insn]
-              assert interp(compile(code)) == pyop(first, second)
+              assert interp(list2bytecode(code)) == pyop(first, second)
 
 
 def test_branch_forward():
-    assert interp(compile([PUSH,1, PUSH,0, BR_COND,2, PUSH,-1])) == -1
-    assert interp(compile([PUSH,1, PUSH,1, BR_COND,2, PUSH,-1])) == 1
-    assert interp(compile([PUSH,1, PUSH,-1, BR_COND,2, PUSH,-1])) == 1
+    assert interp(list2bytecode([PUSH,1, PUSH,0, BR_COND,2, PUSH,-1])) == -1
+    assert interp(list2bytecode([PUSH,1, PUSH,1, BR_COND,2, PUSH,-1])) == 1
+    assert interp(list2bytecode([PUSH,1, PUSH,-1, BR_COND,2, PUSH,-1])) == 1
 
 def test_branch_backwards():
-    assert interp(compile([PUSH,0, PUSH,1, BR_COND,6, PUSH,-1, PUSH,3, BR_COND,4, PUSH,2, BR_COND,-10])) == -1
+    assert interp(list2bytecode([PUSH,0, PUSH,1, BR_COND,6, PUSH,-1, PUSH,3, BR_COND,4, PUSH,2, BR_COND,-10])) == -1
 
 def test_branch0():
-    assert interp(compile([PUSH,7, PUSH,1, BR_COND,0])) == 7
+    assert interp(list2bytecode([PUSH,7, PUSH,1, BR_COND,0])) == 7
 
 def test_exit():
-    assert py.test.raises(IndexError, interp, compile([EXIT]))
-    assert interp(compile([PUSH,7, EXIT, PUSH,5])) == 7
+    assert py.test.raises(IndexError, interp, list2bytecode([EXIT]))
+    assert interp(list2bytecode([PUSH,7, EXIT, PUSH,5])) == 7
 
 def test_rot():
     code = [PUSH,1, PUSH,2, PUSH,3, ROT,3] 
-    assert interp(compile(code)) == 2
-    assert interp(compile(code + [POP])) == 1
-    assert interp(compile(code + [POP, POP])) == 3
+    assert interp(list2bytecode(code)) == 2
+    assert interp(list2bytecode(code + [POP])) == 1
+    assert interp(list2bytecode(code + [POP, POP])) == 3
 
-    py.test.raises(IndexError, interp, compile([PUSH,1, PUSH,2, PUSH,3, ROT,4]))
+    py.test.raises(IndexError, interp, list2bytecode([PUSH,1, PUSH,2, PUSH,3, ROT,4]))
 
 def test_call_ret():
-    assert py.test.raises(IndexError, interp, compile([RETURN]))
-    assert interp(compile([PUSH,6, RETURN, PUSH,4, EXIT, PUSH,9])) == 9
-    assert interp(compile([CALL,0])) == 2
-
-    assert interp(compile([PUSH,1, CALL,5, PUSH,2, CALL,2, EXIT, RETURN, ROT,3, ADD, SWAP, RETURN])) == 3
+    assert py.test.raises(IndexError, interp, list2bytecode([RETURN]))
+    assert interp(list2bytecode([PUSH,6, RETURN, PUSH,4, EXIT, PUSH,9])) == 9
+    assert interp(list2bytecode([CALL,0])) == 2
+    assert interp(list2bytecode([PUSH,1, CALL,5, PUSH,2, CALL,2, EXIT, RETURN, ROT,3, ADD, SWAP, RETURN])) == 3
+
+def test_compile_branch_backwards():
+    code = compile("""
+main:
+    PUSH 0
+    PUSH 1
+    BR_COND somename
+label1:
+    PUSH -1
+    PUSH 3
+    BR_COND end
+somename:
+    PUSH 2
+    BR_COND label1
+end:
+""")
+    assert code == list2bytecode([PUSH,0, PUSH,1, BR_COND,6, PUSH,-1, PUSH,3, BR_COND,4, PUSH,2, BR_COND,-10])
+
+def test_compile_call_ret():
+    code = compile("""PUSH 1
+    CALL func1
+    PUSH 2
+    CALL func2
+    EXIT
+
+func1:
+    RETURN
+
+func2:
+    ROT 3
+    ADD 
+    SWAP
+    RETURN""")
+    assert code == list2bytecode([PUSH,1, CALL,5, PUSH,2, CALL,2, EXIT, RETURN, ROT,3, ADD, SWAP, RETURN])

Modified: pypy/dist/pypy/jit/tl.py
==============================================================================
--- pypy/dist/pypy/jit/tl.py	(original)
+++ pypy/dist/pypy/jit/tl.py	Wed Dec  7 18:30:24 2005
@@ -1,7 +1,8 @@
 '''Toy Language'''
 
 import py
-from bytecode import *
+from opcode import *
+import opcode
 
 def char2int(c):
     t = ord(c)
@@ -109,3 +110,28 @@
             raise RuntimeError("unknown opcode: " + str(opcode))
 
     return stack[-1]
+
+def compile(code=''):
+    bytecode = []
+    labels   = {}       #[key] = pc
+    label_usage = []    #(name, pc)
+    for s in code.split('\n'):
+        for comment in '; # //'.split():
+            s = s.split(comment, 1)[0]
+        s = s.strip()
+        if not s:
+            continue
+        t = s.split()
+        if t[0].endswith(':'):
+            labels[ t[0][:-1] ] = len(bytecode)
+            continue
+        bytecode.append( opcode.names[ t[0] ] )
+        if len(t) > 1:
+            try:
+                bytecode.append( int(t[1]) )
+            except ValueError:
+                label_usage.append( (t[1], len(bytecode)) )
+                bytecode.append( 0 )
+    for label, pc in label_usage:
+        bytecode[pc] = labels[label] - pc - 1
+    return ''.join([chr(i & 0xff) for i in bytecode])  



More information about the Pypy-commit mailing list