[pypy-svn] r60497 - in pypy/branch/oo-jit/pypy/jit/tl: . test

antocuni at codespeak.net antocuni at codespeak.net
Sun Dec 14 14:21:36 CET 2008


Author: antocuni
Date: Sun Dec 14 14:21:34 2008
New Revision: 60497

Added:
   pypy/branch/oo-jit/pypy/jit/tl/binarytree.tlc.src
Modified:
   pypy/branch/oo-jit/pypy/jit/tl/test/test_tlc.py
Log:
implement a binary search tree in tlc, with tests. This could become a benchmark in the future



Added: pypy/branch/oo-jit/pypy/jit/tl/binarytree.tlc.src
==============================================================================
--- (empty file)
+++ pypy/branch/oo-jit/pypy/jit/tl/binarytree.tlc.src	Sun Dec 14 14:21:34 2008
@@ -0,0 +1,138 @@
+main:
+    CALL newnode
+    PICK 0
+    PUSH 20
+    SEND insert/1
+
+    PICK 0
+    PUSH 10
+    SEND insert/1
+
+    PICK 0
+    PUSH 15
+    SEND insert/1
+
+    PICK 0
+    PUSH 30
+    SEND insert/1
+
+    PICK 0
+    PUSHARG
+    SEND search/1
+
+    RETURN
+
+
+newnode:
+    NEW value,left,right,isempty=isempty,insert=insert,search=search
+    RETURN
+
+isempty:
+    PUSHARG
+    GETATTR value
+    BR_COND isempty_not
+    PUSH 1
+    RETURN
+  isempty_not:
+    PUSH 0
+    RETURN
+
+insert: # (n)
+    # if self.isempty goto insert_empty
+    PUSHARG
+    SEND isempty/0
+    BR_COND insert_empty
+
+    # if n == self.value goto insert_found
+    PUSHARGN 1
+    PUSHARG
+    GETATTR value
+    EQ
+    BR_COND insert_found
+
+    # if n < self.value goto insert_left
+    PUSHARGN 1
+    PUSHARG
+    GETATTR value
+    LT
+    BR_COND insert_left
+
+  insert_right:
+    # self.right.insert(n)
+    PUSHARG
+    GETATTR right
+    PUSHARGN 1
+    SEND insert/1
+    RETURN
+
+  insert_left:
+    # self.left.insert(n)
+    PUSHARG
+    GETATTR left
+    PUSHARGN 1
+    SEND insert/1
+    RETURN
+
+  insert_found:
+    RETURN
+
+  insert_empty:
+    # self.value = n
+    PUSHARG
+    PUSHARGN 1
+    SETATTR value
+
+    # self.left = Node()
+    PUSHARG
+    CALL newnode
+    SETATTR left
+
+    # self.right = Node()
+    PUSHARG
+    CALL newnode
+    SETATTR right
+
+    RETURN
+
+
+search: # (n)
+    # if self.isempty goto search_empty
+    PUSHARG
+    SEND isempty/0
+    BR_COND search_empty
+
+    # if n == self.value goto search_found
+    PUSHARGN 1
+    PUSHARG
+    GETATTR value
+    EQ
+    BR_COND search_found
+
+    # if n < self.value goto search_left
+    PUSHARGN 1
+    PUSHARG
+    GETATTR value
+    LT
+    BR_COND search_left
+
+  search_right:
+    PUSHARG
+    GETATTR right
+    PUSHARGN 1
+    SEND search/1
+    RETURN
+
+  search_left:
+    PUSHARG
+    GETATTR left
+    PUSHARGN 1
+    SEND search/1
+    RETURN
+
+  search_found:
+    PUSH 1
+    RETURN
+
+  search_empty:
+    PUSH 0
+    RETURN

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	Sun Dec 14 14:21:34 2008
@@ -141,12 +141,12 @@
         py.test.raises(TypeError, self.interp, bytecode, 0, 0)
 
     def test_new_obj(self):
-        from pypy.jit.tl.tlc import interp_eval, InstanceObj
+        from pypy.jit.tl.tlc import interp_eval, InstanceObj, nil
         pool = ConstantPool()
         bytecode = compile("""
             NEW foo,bar
         """, pool)
-        obj = interp_eval(bytecode, 0, None, pool)
+        obj = interp_eval(bytecode, 0, [nil], pool)
         assert isinstance(obj, InstanceObj)
         assert len(obj.values) == 2
         assert sorted(obj.cls.attributes.keys()) == ['bar', 'foo']
@@ -160,7 +160,7 @@
             PUSH 42
             SETATTR foo
         """, pool)
-        obj = interp_eval(bytecode, 0, None, pool)
+        obj = interp_eval(bytecode, 0, [nil], pool)
         assert obj.values[0].int_o() == 42
         assert obj.values[1] is nil
 
@@ -174,7 +174,7 @@
             SETATTR bar
             GETATTR bar
         """, pool)
-        res = interp_eval(bytecode, 0, nil, pool)
+        res = interp_eval(bytecode, 0, [nil], pool)
         assert res.int_o() == 42
 
     def test_obj_truth(self):
@@ -191,7 +191,7 @@
         exit:
             RETURN
         """, pool)
-        res = interp_eval(bytecode, 0, nil, pool)
+        res = interp_eval(bytecode, 0, [nil], pool)
         assert res.int_o() == 42
 
     def test_obj_equality(self):
@@ -202,7 +202,7 @@
             NEW foo,bar
             EQ
         """, pool)
-        res = interp_eval(bytecode, 0, nil, pool)
+        res = interp_eval(bytecode, 0, [nil], pool)
         assert res.int_o() == 0
 
     def test_method(self):
@@ -220,7 +220,7 @@
             GETATTR foo
             RETURN
         """, pool)
-        res = interp_eval(bytecode, 0, nil, pool)
+        res = interp_eval(bytecode, 0, [nil], pool)
         assert res.int_o() == 42
 
     def test_method_arg(self):
@@ -241,7 +241,7 @@
             ADD
             RETURN
         """, pool)
-        res = interp_eval(bytecode, 0, nil, pool)
+        res = interp_eval(bytecode, 0, [nil], pool)
         assert res.int_o() == 42
 
     def test_call_without_return_value(self):
@@ -254,5 +254,24 @@
         foo:
             RETURN
         """, pool)
-        res = interp_eval(bytecode, 0, nil, pool)
+        res = interp_eval(bytecode, 0, [nil], pool)
         assert res.int_o() == 42
+
+    def test_binarytree(self):
+        from pypy.jit.tl.tlc import interp_eval, IntObj
+        pool = ConstantPool()
+        path = py.path.local(__file__).join('../../binarytree.tlc.src')
+        src = path.read()
+        bytecode = compile(src, pool)
+        def search(n):
+            obj = IntObj(n)
+            res = interp_eval(bytecode, 0, [obj], pool)
+            return res.int_o()
+        assert search(20)
+        assert search(10)
+        assert search(15)
+        assert search(30)
+        assert not search(1)
+        assert not search(40)
+        assert not search(12)
+        assert not search(27)



More information about the Pypy-commit mailing list