[pypy-svn] r14709 - in pypy/dist/pypy/translator/llvm2: . test

rxe at codespeak.net rxe at codespeak.net
Fri Jul 15 21:45:26 CEST 2005


Author: rxe
Date: Fri Jul 15 21:45:24 2005
New Revision: 14709

Modified:
   pypy/dist/pypy/translator/llvm2/arraynode.py
   pypy/dist/pypy/translator/llvm2/database.py
   pypy/dist/pypy/translator/llvm2/funcnode.py
   pypy/dist/pypy/translator/llvm2/structnode.py
   pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
Log:
Arghh - backtrack of ideas from about 10 days ago.  We now create a node for
every pbc recursively in the database and ensure they are created at setup
time.  We need to do this so we because a pointer in a pbc can be nested too
deep and to be seen at setup time and then it is never instantiated.
Actually it feels a lot cleaner this way.

Also added cast_pointer.



Modified: pypy/dist/pypy/translator/llvm2/arraynode.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/arraynode.py	(original)
+++ pypy/dist/pypy/translator/llvm2/arraynode.py	Fri Jul 15 21:45:24 2005
@@ -69,9 +69,8 @@
         return "<ArrayNode %r>" % (self.ref,)
 
     def setup(self):
-        if isinstance(self.arraytype, lltype.Ptr):
-            for item in self.value.items:
-                self.db.prepare_ptr(item)
+        for item in self.value.items:
+            self.db.prepare_constant(self.arraytype, item)
 
         # set castref (note we must ensure that types are "setup" before we can
         # get typeval)

Modified: pypy/dist/pypy/translator/llvm2/database.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/database.py	(original)
+++ pypy/dist/pypy/translator/llvm2/database.py	Fri Jul 15 21:45:24 2005
@@ -72,7 +72,7 @@
         self._pendingsetup = []
         self._tmpcount = 1
 
-    def create_constant_node(self, type_, value, setup=False):
+    def create_constant_node(self, type_, value):
         node = None
         if isinstance(type_, lltype.FuncType):
             if value._callable and (not hasattr(value, "graph") or value.graph is None 
@@ -93,9 +93,7 @@
         elif isinstance(type_, lltype.OpaqueType):
             node = OpaqueNode(self, value)
 
-        assert node is not None, "%s not supported %s" % (type_, lltype.typeOf(value))
-        if setup:
-            node.setup()
+        assert node is not None, "%s not supported" % (type_)
         return node
 
     def addpending(self, key, node):
@@ -168,13 +166,19 @@
         log.prepare(const_or_var)
         self.prepare_repr_arg_type(const_or_var.concretetype)
         self.prepare_repr_arg(const_or_var)
-            
-    def prepare_ptr(self, ptrvalue):        
-        assert isinstance(lltype.typeOf(ptrvalue), lltype.Ptr)
-        value = ptrvalue._obj
-        type_ = lltype.typeOf(ptrvalue).TO
-        if value in self.obj2node or value is None:
+
+    def prepare_constant(self, type_, value):        
+        if isinstance(type_, lltype.Primitive):
+            log.prepare_constant(value, "(is primitive)")
             return
+        elif isinstance(type_, lltype.Ptr):
+            type_ = type_.TO
+            value = value._obj
+
+            # we can share data via pointers & dont need a node for nulls
+            if value in self.obj2node or value is None:
+                return
+
         self.addpending(value,
                         self.create_constant_node(type_, value))
 
@@ -262,7 +266,8 @@
             return node, "%s %s" % (toptr, ref)
 
         elif isinstance(type_, lltype.Array) or isinstance(type_, lltype.Struct):
-            node = self.create_constant_node(type_, value, setup=True)
+            assert value in self.obj2node, "XXX tmp assert"
+            node = self.obj2node[value]
             return node, node.constantvalue()
 
         assert False, "%s not supported" % (type(value))

Modified: pypy/dist/pypy/translator/llvm2/funcnode.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/funcnode.py	(original)
+++ pypy/dist/pypy/translator/llvm2/funcnode.py	Fri Jul 15 21:45:24 2005
@@ -324,6 +324,7 @@
         fromtype = self.db.repr_arg_type(op.args[0])
         self.codewriter.cast(targetvar, fromtype, fromvar, targettype)
 
+    cast_pointer = cast_primitive
     cast_bool_to_int = cast_primitive
     cast_bool_to_uint = uint_is_true = cast_primitive
     cast_int_to_char = cast_char_to_int = cast_primitive
@@ -389,6 +390,7 @@
         self.codewriter.getelementptr(tmpvar, structtype, struct, ("uint", index))        
         targetvar = self.db.repr_arg(op.result)
         targettype = self.db.repr_arg_type(op.result)
+        r = op.result
         assert targettype != "void"
         self.codewriter.load(targetvar, targettype, tmpvar)
 

Modified: pypy/dist/pypy/translator/llvm2/structnode.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/structnode.py	(original)
+++ pypy/dist/pypy/translator/llvm2/structnode.py	Fri Jul 15 21:45:24 2005
@@ -108,8 +108,9 @@
     def setup(self):
         for name, T in self._gettypes():
             assert T is not lltype.Void
-            if isinstance(T, lltype.Ptr):
-                self.db.prepare_ptr(getattr(self.value, name))
+            value = getattr(self.value, name)
+            self.db.prepare_constant(T, value)
+                
         self._issetup = True
 
     def get_typerepr(self):
@@ -148,28 +149,29 @@
             values.append(self.db.repr_constant(value)[1])
         values.append(self._get_lastnoderepr())
         return values
-    
+
+    def _get_lastnode_helper(self):
+        lastname, LASTT = self._gettypes()[-1]
+        assert isinstance(LASTT, lltype.Array) or (
+            isinstance(LASTT, lltype.Struct) and LASTT._arrayfld)
+        value = getattr(self.value, lastname)
+        return self.db.repr_constant(value)
+
     def _get_lastnode(self):
-        if not hasattr(self, "lastnode"):
-            lastname, LASTT = self._gettypes()[-1]
-            assert isinstance(LASTT, lltype.Array) or (
-                isinstance(LASTT, lltype.Struct) and LASTT._arrayfld)
-            value = getattr(self.value, lastname)
-            self.lastnode, self.lastnode_repr = self.db.repr_constant(value)
-        return self.lastnode
+        return self._get_lastnode_helper()[0]
 
     def _get_lastnoderepr(self):
-        self._get_lastnode()
-        return self.lastnode_repr
+        return self._get_lastnode_helper()[1]
 
     def setup(self):
+        super(StructVarsizeNode, self).setup()
+
         # set castref (note we must ensure that types are "setup" before we can
         # get typeval)
         typeval = self.db.repr_arg_type(lltype.typeOf(self.value))
         self.castref = "cast (%s* %s to %s*)" % (self.get_typerepr(),
                                                  self.ref,
                                                  typeval)
-        super(StructVarsizeNode, self).setup()
     
     def get_typerepr(self):
         # last type is a special case and need to be worked out recursively

Modified: pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	Fri Jul 15 21:45:24 2005
@@ -319,10 +319,29 @@
     def method_call(a, b):
         obj = MyBase()
         obj.z = a
-        return obj.m(b)
-    f = compile_function(f, [int, int], view=True)
+        return obj.z + b
+    f = compile_function(method_call, [int, int], view=True)
     assert f(4, 5) == 9
 
+def test_attrs_class():
+    class MyBase:
+        pass
+    def attrs_class(a):
+        obj = MyBase()
+        obj.z = a
+        return obj.z * 4
+    f = compile_function(attrs_class, [int])
+    assert f(4) == 16
+
+def test_attrs_class_pbc():
+    class MyBase:
+        pass
+    obj = MyBase()
+    obj.z = 4
+    def attrs_class_pbc():
+        return obj.z * 4
+    f = compile_function(attrs_class_pbc, [])
+    assert f() == 16
 
 class TestException(Exception):
     pass



More information about the Pypy-commit mailing list