[pypy-svn] r60489 - in pypy/branch/oo-jit/pypy/jit: rainbow/test tl tl/test

antocuni at codespeak.net antocuni at codespeak.net
Sat Dec 13 17:30:51 CET 2008


Author: antocuni
Date: Sat Dec 13 17:30:49 2008
New Revision: 60489

Modified:
   pypy/branch/oo-jit/pypy/jit/rainbow/test/test_0tlc.py
   pypy/branch/oo-jit/pypy/jit/tl/factorial.tlc
   pypy/branch/oo-jit/pypy/jit/tl/targettlc.py
   pypy/branch/oo-jit/pypy/jit/tl/test/test_tlc.py
   pypy/branch/oo-jit/pypy/jit/tl/tlc.py
   pypy/branch/oo-jit/pypy/jit/tl/tlopcode.py
Log:
use the same (de)serialization code for both targettlc and the tests



Modified: pypy/branch/oo-jit/pypy/jit/rainbow/test/test_0tlc.py
==============================================================================
--- pypy/branch/oo-jit/pypy/jit/rainbow/test/test_0tlc.py	(original)
+++ pypy/branch/oo-jit/pypy/jit/rainbow/test/test_0tlc.py	Sat Dec 13 17:30:49 2008
@@ -7,6 +7,7 @@
 from pypy.jit.conftest import Benchmark
 
 from pypy.jit.tl import tlc
+from pypy.jit.tl.tlopcode import serialize_program, decode_program
 from pypy.jit.tl.test.test_tl import FACTORIAL_SOURCE
 
 
@@ -14,90 +15,36 @@
     small = False
 
     def _get_interp(self):
-        def decode_descr(encdescr):
-            items = encdescr.split(',')
-            attributes = []
-            methods = []
-            for item in items:
-                if '=' in item:
-                    methname, pc = item.split('=')
-                    methods.append((methname, int(pc)))
-                else:
-                    attributes.append(item)
-            return tlc.ClassDescr(attributes, methods)
-
-        def encode_descr(descr):
-            parts = []
-            parts += descr.attributes
-            parts += ['%s=%s' % item for item in descr.methods]
-            return ','.join(parts)
-        
-        def decode_pool(encpool):
-            """
-            encpool is encoded in this way:
-
-            attr1,attr2,foo=3|attr1,bar=5|...
-            attr1,attr2,foo,bar,hello,world,...
-            """
-            if encpool == '':
-                return None
-            lines = encpool.split('\n')
-            assert len(lines) == 2
-            encdescrs = lines[0].split('|')
-            classdescrs = [decode_descr(enc) for enc in encdescrs]
-            strings = lines[1].split(',')
-            pool = tlc.ConstantPool()
-            pool.classdescrs = classdescrs
-            pool.strings = strings
-            return pool
-
-        def encode_pool(pool):
-            if pool is None:
-                return ''
-            encdescrs = '|'.join([encode_descr(descr) for descr in pool.classdescrs])
-            encstrings = ','.join(pool.strings)
-            return '%s\n%s' % (encdescrs, encstrings)
             
-        def interp(llbytecode, pc, inputarg, llencpool):
+        def interp(llprogram, inputarg):
             from pypy.rpython.annlowlevel import hlstr
-            bytecode = hlstr(llbytecode)
-            encpool = hlstr(llencpool)
-            pool = decode_pool(encpool)
-            obj = tlc.interp_eval(bytecode,
-                                  pc,
-                                  [tlc.IntObj(inputarg)],
-                                  pool)
+            program = hlstr(llprogram)
+            bytecode, pool = decode_program(program)
+            args = [tlc.IntObj(inputarg)]
+            obj = tlc.interp_eval(bytecode, 0, args, pool)
             return obj.int_o()
-        
-        to_rstr = self.to_rstr
-        def build_bytecode(s):
-            result = ''.join([chr(int(t)) for t in s.split(',')])
-            return to_rstr(result)
-        def build_pool(pool):
-            return to_rstr(encode_pool(pool))
-        interp.convert_arguments = [build_bytecode, int, int, build_pool]
         return interp
 
-    def exec_code(self, code, inputarg, pool=None):
-        bytecode = ','.join([str(ord(c)) for c in code])
+    def exec_code(self, src, inputarg): #, pool=None):
+        pool = tlc.ConstantPool()
+        bytecode = tlc.compile(src, pool)
+        program = serialize_program(bytecode, pool)
+        llprogram = self.to_rstr(program)
         interp = self._get_interp()
         res = self.timeshift_from_portal(interp,
                                          tlc.interp_eval,
-                                         [bytecode, 0, inputarg, pool],
+                                         [llprogram, inputarg],
                                          policy=P_OOPSPEC)
         return res
 
     def test_factorial(self):
-        code = tlc.compile(FACTORIAL_SOURCE)
-        n = 5
-        expected = 120
-        res = self.exec_code(code, n)
-        assert res == expected
+        res = self.exec_code(FACTORIAL_SOURCE, 5)
+        assert res == 120
         self.check_insns(malloc=1)
 
     def test_nth_item(self):
         # get the nth item of a chained list
-        code = tlc.compile("""
+        code = """
             NIL
             PUSH 40
             CONS
@@ -107,26 +54,24 @@
             CONS
             PUSHARG
             DIV
-        """)
+        """
         res = self.exec_code(code, 1)
         assert res == 20
 
     def test_getattr(self):
-        pool = tlc.ConstantPool()
-        code = tlc.compile("""
+        code = """
             NEW foo,bar
             PICK 0
             PUSH 42
             SETATTR bar,
             GETATTR bar,
-        """, pool)
-        res = self.exec_code(code, 0, pool)
+        """
+        res = self.exec_code(code, 0)
         assert res == 42
         self.check_insns(malloc=1, direct_call=0)
 
     def test_method(self):
-        pool = tlc.ConstantPool()
-        code = tlc.compile("""
+        code = """
             NEW foo,meth=meth
             PICK 0
             PUSH 40
@@ -140,8 +85,8 @@
             PUSHARGN 1
             ADD
             RETURN
-        """, pool)
-        res = self.exec_code(code, 0, pool)
+        """
+        res = self.exec_code(code, 0)
         assert res == 42
 
 class TestLLType(BaseTestTLC):

Modified: pypy/branch/oo-jit/pypy/jit/tl/factorial.tlc
==============================================================================
Binary files. No diff available.

Modified: pypy/branch/oo-jit/pypy/jit/tl/targettlc.py
==============================================================================
--- pypy/branch/oo-jit/pypy/jit/tl/targettlc.py	(original)
+++ pypy/branch/oo-jit/pypy/jit/tl/targettlc.py	Sat Dec 13 17:30:49 2008
@@ -45,20 +45,12 @@
 
     return 0
 
-def decode_poolcode(s):
-    pool = ConstantPool()
-    lists = s.split('|')
-    pool.strlists = [lst.split(',') for lst in lists]
-    return pool
 
 def load_bytecode(filename):
     from pypy.rlib.streamio import open_file_as_stream
+    from pypy.jit.tl.tlopcode import decode_program
     f = open_file_as_stream(filename)
-    poolcode = f.readline()[:-1]
-    bytecode = f.readall()[:-1]
-    f.close()
-    pool = decode_poolcode(poolcode)
-    return bytecode, pool
+    return decode_program(f.readall())
 
 def target(driver, args):
     return entry_point, None

Modified: pypy/branch/oo-jit/pypy/jit/tl/test/test_tlc.py
==============================================================================
--- pypy/branch/oo-jit/pypy/jit/tl/test/test_tlc.py	(original)
+++ pypy/branch/oo-jit/pypy/jit/tl/test/test_tlc.py	Sat Dec 13 17:30:49 2008
@@ -17,6 +17,19 @@
     assert descr.attributes == ['foo', 'bar']
     assert descr.methods == [('meth', 2)]
 
+def test_serialization():
+    from pypy.jit.tl.tlopcode import serialize_program, decode_program
+    pool = ConstantPool()
+    bytecode = compile("""
+        NEW foo,bar,meth=f
+        SETATTR foobar
+      f:
+        RETURN
+    """, pool)
+    s = serialize_program(bytecode, pool)
+    bytecode2, pool2 = decode_program(s)
+    assert bytecode == bytecode2
+    assert pool == pool2
 
 class TestTLC(test_tl.TestTL):
     @staticmethod

Modified: pypy/branch/oo-jit/pypy/jit/tl/tlc.py
==============================================================================
--- pypy/branch/oo-jit/pypy/jit/tl/tlc.py	(original)
+++ pypy/branch/oo-jit/pypy/jit/tl/tlc.py	Sat Dec 13 17:30:49 2008
@@ -38,6 +38,10 @@
         self.attributes = attributes
         self.methods = methods
 
+    def __eq__(self, other):
+        "NOT_RPYTHON"
+        return self.__dict__ == other.__dict__
+
 class ConstantPool(object):
 
     def __init__(self):
@@ -58,6 +62,10 @@
             self.strings.append(s)
             return idx
 
+    def __eq__(self, other):
+        "NOT_RPYTHON"
+        return self.__dict__ == other.__dict__
+
 class Class(object):
 
     classes = [] # [(descr, cls), ...]
@@ -467,5 +475,4 @@
 
     pool = ConstantPool()
     bytecode = compile(src, pool)
-    print serialize_pool(pool)
-    print bytecode
+    sys.stdout.write(serialize_program(bytecode, pool))

Modified: pypy/branch/oo-jit/pypy/jit/tl/tlopcode.py
==============================================================================
--- pypy/branch/oo-jit/pypy/jit/tl/tlopcode.py	(original)
+++ pypy/branch/oo-jit/pypy/jit/tl/tlopcode.py	Sat Dec 13 17:30:49 2008
@@ -114,6 +114,63 @@
             methods[i] = (methname, pc)
     return ''.join([chr(i & 0xff) for i in bytecode])  
 
+
+def decode_descr(encdescr):
+    from pypy.jit.tl.tlc import ClassDescr
+    items = encdescr.split(',')
+    attributes = []
+    methods = []
+    for item in items:
+        if '=' in item:
+            methname, pc = item.split('=')
+            methods.append((methname, int(pc)))
+        else:
+            attributes.append(item)
+    return ClassDescr(attributes, methods)
+
+def decode_pool(encpool):
+    """
+    encpool is encoded in this way:
+
+    attr1,attr2,foo=3|attr1,bar=5|...
+    attr1,attr2,foo,bar,hello,world,...
+    """
+    from pypy.jit.tl.tlc import ConstantPool
+    if encpool == '':
+        return None
+    lines = encpool.split('\n')
+    assert len(lines) == 2
+    encdescrs = lines[0].split('|')
+    classdescrs = [decode_descr(enc) for enc in encdescrs]
+    strings = lines[1].split(',')
+    pool = ConstantPool()
+    pool.classdescrs = classdescrs
+    pool.strings = strings
+    return pool
+
+def serialize_descr(descr):
+    parts = []
+    parts += descr.attributes
+    parts += ['%s=%s' % item for item in descr.methods]
+    return ','.join(parts)
+
 def serialize_pool(pool):
-    lists = [','.join(lst) for lst in pool.strlists]
-    return '|'.join(lists)
+    if pool is None:
+        return ''
+    encdescrs = '|'.join([serialize_descr(descr) for descr in pool.classdescrs])
+    encstrings = ','.join(pool.strings)
+    return '%s\n%s' % (encdescrs, encstrings)
+
+def serialize_program(bytecode, pool):
+    poolcode = serialize_pool(pool)
+    return '%s\n%s' % (poolcode, bytecode)
+
+def decode_program(s):
+    idx1 = s.find('\n')
+    assert idx1 >= 0
+    idx2 = s.find('\n', idx1+1)
+    assert idx2 >= 0
+    poolcode = s[:idx2]
+    bytecode = s[idx2+1:] # remove the 2nd newline
+    pool = decode_pool(poolcode)
+    return bytecode, pool



More information about the Pypy-commit mailing list