[pypy-svn] r12204 - in pypy/dist/pypy: annotation rpython rpython/test

pedronis at codespeak.net pedronis at codespeak.net
Thu May 12 00:31:46 CEST 2005


Author: pedronis
Date: Thu May 12 00:31:46 2005
New Revision: 12204

Modified:
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/rpython/lltypes.py
   pypy/dist/pypy/rpython/test/test_llann.py
Log:
annotation for low-level cast_flags/cast_parent



Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Thu May 12 00:31:46 2005
@@ -244,8 +244,28 @@
         n = 1
     p = lltypes.malloc(T.const, n)
     r = SomePtr(lltypes.typeOf(p))
-    print r
+    #print "MALLOC", r
     return r
 
+def cast_flags(PtrT, s_p):
+    #print "CAST", s_p
+    assert isinstance(s_p, SomePtr), "casting of non-pointer: %r" % s_p
+    assert PtrT.is_constant()
+    return SomePtr(ll_ptrtype=lltypes.typeOf(lltypes.cast_flags(PtrT.const, s_p.ll_ptrtype._example())))
+
+def cast_parent(PtrT, s_p):
+    assert isinstance(s_p, SomePtr), "casting of non-pointer: %r" % s_p
+    assert PtrT.is_constant()
+    PtrT = PtrT.const
+    parent_example_p = PtrT._example()
+    first_p = parent_example_p._first()
+    if s_p.ll_ptrtype == lltypes.typeOf(first_p):
+        candidate_p = first_p
+    else:
+        candidate_p = s_p.ll_ptrtype._example()
+    return SomePtr(ll_ptrtype=lltypes.typeOf(lltypes.cast_parent(PtrT, candidate_p)))
+
 BUILTIN_ANALYZERS[lltypes.malloc] = malloc
+BUILTIN_ANALYZERS[lltypes.cast_flags] = cast_flags
+BUILTIN_ANALYZERS[lltypes.cast_parent] = cast_parent
 

Modified: pypy/dist/pypy/rpython/lltypes.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypes.py	(original)
+++ pypy/dist/pypy/rpython/lltypes.py	Thu May 12 00:31:46 2005
@@ -272,6 +272,11 @@
         raise AttributeError("%r instance has no field %r" % (self._T,
                                                               field_name))
 
+    def _first(self):
+        if isinstance(self._T, Struct) and self._T._names:
+            return self.__getattr__(self._T._names[0])
+        raise AttributeError("%r instance has no first field" % (self._T,))
+
     def __setattr__(self, field_name, val):
         if isinstance(self._T, Struct):
             if field_name in self._T._flds:

Modified: pypy/dist/pypy/rpython/test/test_llann.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_llann.py	(original)
+++ pypy/dist/pypy/rpython/test/test_llann.py	Thu May 12 00:31:46 2005
@@ -1,4 +1,5 @@
 from pypy.rpython.lltypes import *
+from pypy.annotation import model as annmodel
 
 
 class TestLowLevelAnnotateTestCase:
@@ -33,4 +34,29 @@
         a = self.RPythonAnnotator()
         s = a.build_types(llf, [])
         assert s.knowntype == int
-    
+
+    def test_cast_flags(self):
+        S1 = Struct("s1", ('a', Signed), ('b', Unsigned))
+        NGCPS1 = NonGcPtr(S1)
+        def llf():
+            p1 = malloc(S1)
+            p2 = cast_flags(NGCPS1, p1)
+            return p2
+        a = self.RPythonAnnotator()
+        s = a.build_types(llf, [])
+        assert isinstance(s, annmodel.SomePtr)
+        assert s.ll_ptrtype == NGCPS1
+        
+    def test_cast_parent(self):
+        S2 = Struct("s2", ('a', Signed))
+        S1 = Struct("s1", ('sub1', S2), ('sub2', S2))
+        GCPS1 = GcPtr(S1)
+        def llf():
+            p1 = malloc(S1)
+            p2 = p1.sub1
+            p3 = cast_parent(GCPS1, p2)
+            return p3
+        a = self.RPythonAnnotator()
+        s = a.build_types(llf, [])
+        assert isinstance(s, annmodel.SomePtr)
+        assert s.ll_ptrtype == GCPS1



More information about the Pypy-commit mailing list