[pypy-svn] r22467 - in pypy/dist/pypy/rpython/memory: . test

mwh at codespeak.net mwh at codespeak.net
Fri Jan 20 12:11:51 CET 2006


Author: mwh
Date: Fri Jan 20 12:11:50 2006
New Revision: 22467

Modified:
   pypy/dist/pypy/rpython/memory/lladdress.py
   pypy/dist/pypy/rpython/memory/test/test_address.py
Log:
check that you can actually _call_ these offset manipulations.
implement offset addition, in a very type-anal way.


Modified: pypy/dist/pypy/rpython/memory/lladdress.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/lladdress.py	(original)
+++ pypy/dist/pypy/rpython/memory/lladdress.py	Fri Jan 20 12:11:50 2006
@@ -126,9 +126,9 @@
 
 class OffsetOf(Symbolic):
 
-    def __init__(self, TYPE, fldname):
+    def __init__(self, TYPE, *fldnames):
         self.TYPE = TYPE
-        self.fldname = fldname
+        self.fldnames = fldnames
 
     def annotation(self):
         from pypy.annotation import model
@@ -138,9 +138,18 @@
         return Offset
 
     def __repr__(self):
-        return "<OffsetOf %r %r>" % (self.TYPE, self.fldname)
+        return "<OffsetOf %r %r>" % (self.TYPE, self.fldnames)
 
-Offset = lltype.Primitive("Offset", OffsetOf(lltype.Void, ''))
+    def __add__(self, other):
+        if not isinstance(other, OffsetOf):
+            return NotImplemented
+        t = self.TYPE
+        for f in self.fldnames:
+            t = t._flds[f]
+        assert t == other.TYPE
+        return OffsetOf(self.TYPE, *(self.fldnames + other.fldnames))
+
+Offset = lltype.Primitive("Offset", OffsetOf(lltype.Void))
 
 def sizeof(TYPE, n=None):
     pass

Modified: pypy/dist/pypy/rpython/memory/test/test_address.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/test/test_address.py	(original)
+++ pypy/dist/pypy/rpython/memory/test/test_address.py	Fri Jan 20 12:11:50 2006
@@ -117,17 +117,41 @@
         from pypy.rpython.memory.lladdress import offsetof
         S = lltype.GcStruct('S', ('x', lltype.Bool), ('y', lltype.Signed))
         def f():
-            return offsetof(S, 'x') + offsetof(S, 'y')
+            return offsetof(S, 'x')
+        f()
         a = RPythonAnnotator()
         s = a.build_types(f, [])
         assert isinstance(s, annmodel.SomeOffset)
+
         coff = offsetof(S, 'y')
         def f():
             return coff
+        f()
+        a = RPythonAnnotator()
+        s = a.build_types(f, [])
+        assert isinstance(s, annmodel.SomeOffset)
+
+    def test_offset_addition(self):
+        from pypy.rpython.lltypesystem import lltype
+        from pypy.rpython.memory.lladdress import offsetof
+        S = lltype.Struct('S', ('x', lltype.Bool), ('y', lltype.Signed))
+        T = lltype.GcStruct('T', ('r', lltype.Float), ('s1', S), ('s2', S))
+        def f():
+            return offsetof(T, 's1') + offsetof(S, 'x')
+        f()
         a = RPythonAnnotator()
         s = a.build_types(f, [])
         assert isinstance(s, annmodel.SomeOffset)
 
+        coff = offsetof(T, 's2') + offsetof(S, 'y')
+        def f():
+            return coff
+        f()
+        a = RPythonAnnotator()
+        s = a.build_types(f, [])
+        assert isinstance(s, annmodel.SomeOffset)
+        
+        
 class TestAddressRTyping(object):
     def test_null(self):
         def f():
@@ -223,7 +247,8 @@
         from pypy.rpython.memory.lladdress import offsetof
         S = lltype.GcStruct('S', ('x', lltype.Bool), ('y', lltype.Signed))
         def f():
-            return offsetof(S, 'x') + offsetof(S, 'y')
+            return offsetof(S, 'x')
+        f()
         a = RPythonAnnotator()
         s = a.build_types(f, [])
         rtyper = RPythonTyper(a)
@@ -232,6 +257,29 @@
         coff = offsetof(S, 'y')
         def f():
             return coff
+        f()
+        a = RPythonAnnotator()
+        s = a.build_types(f, [])
+        rtyper = RPythonTyper(a)
+        rtyper.specialize() #does not raise
+
+    def test_offset_addition(self):
+        from pypy.rpython.lltypesystem import lltype
+        from pypy.rpython.memory.lladdress import offsetof
+        S = lltype.Struct('S', ('x', lltype.Bool), ('y', lltype.Signed))
+        T = lltype.GcStruct('T', ('r', lltype.Float), ('s1', S), ('s2', S))
+        def f():
+            return offsetof(T, 's1') + offsetof(S, 'x')
+        f()
+        a = RPythonAnnotator()
+        s = a.build_types(f, [])
+        rtyper = RPythonTyper(a)
+        rtyper.specialize() #does not raise
+
+        coff = offsetof(T, 's2') + offsetof(S, 'y')
+        def f():
+            return coff
+        f()
         a = RPythonAnnotator()
         s = a.build_types(f, [])
         rtyper = RPythonTyper(a)



More information about the Pypy-commit mailing list