[pypy-svn] r67198 - in pypy/branch/pyjitpl5/pypy: jit/backend/x86 jit/backend/x86/test rpython/lltypesystem rpython/lltypesystem/test

pedronis at codespeak.net pedronis at codespeak.net
Tue Aug 25 16:25:18 CEST 2009


Author: pedronis
Date: Tue Aug 25 16:25:15 2009
New Revision: 67198

Modified:
   pypy/branch/pyjitpl5/pypy/jit/backend/x86/runner.py
   pypy/branch/pyjitpl5/pypy/jit/backend/x86/test/test_runner.py
   pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/ll2ctypes.py
   pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/lltype.py
   pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
Log:
(fijal, pedronis, a bit of cfboly)

fix gcref ll2ctypes/lltypes comparisons by making the old _llgcref into an opaque like container wrapper by a pointer



Modified: pypy/branch/pyjitpl5/pypy/jit/backend/x86/runner.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/backend/x86/runner.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/backend/x86/runner.py	Tue Aug 25 16:25:15 2009
@@ -569,11 +569,7 @@
     def cast_int_to_adr(x):
         if not we_are_translated():
             _check_addr_range(x)
-        if we_are_translated():
-            return rffi.cast(llmemory.Address, x)
-        else:
-            # indirect casting because the above doesn't work with ll2ctypes
-            return llmemory.cast_ptr_to_adr(rffi.cast(llmemory.GCREF, x))
+        return rffi.cast(llmemory.Address, x)
 
     def cast_gcref_to_int(self, x):
         return rffi.cast(lltype.Signed, x)

Modified: pypy/branch/pyjitpl5/pypy/jit/backend/x86/test/test_runner.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/backend/x86/test/test_runner.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/backend/x86/test/test_runner.py	Tue Aug 25 16:25:15 2009
@@ -120,7 +120,10 @@
                                'void')
         assert u.chars[2] == u'z'
         assert u.chars[3] == u'd'
-        
+
+    @staticmethod
+    def _resbuf(res, item_tp=ctypes.c_int):
+        return ctypes.cast(res.value._obj.intval, ctypes.POINTER(item_tp))
 
     def test_allocations(self):
         from pypy.rpython.lltypesystem import rstr
@@ -139,17 +142,17 @@
             saved_addr = self.cpu.assembler.malloc_func_addr
             self.cpu.assembler.malloc_func_addr = addr
             ofs = symbolic.get_field_token(rstr.STR, 'chars', False)[0]
-            
+
             res = self.execute_operation(rop.NEWSTR, [ConstInt(7)], 'ptr')
             assert allocs[0] == 7 + ofs + WORD
-            resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
+            resbuf = self._resbuf(res)
             assert resbuf[ofs/WORD] == 7
             
             # ------------------------------------------------------------
 
             res = self.execute_operation(rop.NEWSTR, [BoxInt(7)], 'ptr')
             assert allocs[0] == 7 + ofs + WORD
-            resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
+            resbuf = self._resbuf(res)
             assert resbuf[ofs/WORD] == 7
 
             # ------------------------------------------------------------
@@ -161,7 +164,7 @@
             res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)],
                                              'ptr', descr)
             assert allocs[0] == 10*WORD + ofs + WORD
-            resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
+            resbuf = self._resbuf(res)            
             assert resbuf[ofs/WORD] == 10
 
             # ------------------------------------------------------------
@@ -169,7 +172,7 @@
             res = self.execute_operation(rop.NEW_ARRAY, [BoxInt(10)],
                                              'ptr', descr)
             assert allocs[0] == 10*WORD + ofs + WORD
-            resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
+            resbuf = self._resbuf(res)                        
             assert resbuf[ofs/WORD] == 10
             
         finally:
@@ -182,7 +185,7 @@
 
         res = self.execute_operation(rop.NEWSTR, [ConstInt(10)], 'ptr')
         self.execute_operation(rop.STRSETITEM, [res, ConstInt(2), ConstInt(ord('d'))], 'void')
-        resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_char))
+        resbuf = self._resbuf(res, ctypes.c_char)
         assert resbuf[ofs + ofs_items + 2] == 'd'
         self.execute_operation(rop.STRSETITEM, [res, BoxInt(2), ConstInt(ord('z'))], 'void')
         assert resbuf[ofs + ofs_items + 2] == 'z'
@@ -196,7 +199,7 @@
         descr = self.cpu.arraydescrof(TP)
         res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)],
                                      'ptr', descr)
-        resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
+        resbuf = self._resbuf(res)
         assert resbuf[ofs/WORD] == 10
         self.execute_operation(rop.SETARRAYITEM_GC, [res,
                                                      ConstInt(2), BoxInt(38)],
@@ -235,7 +238,7 @@
         descr = self.cpu.arraydescrof(TP)
         res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)],
                                      'ptr', descr)
-        resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_char))
+        resbuf = self._resbuf(res, ctypes.c_char)
         assert resbuf[ofs] == chr(10)
         for i in range(10):
             self.execute_operation(rop.SETARRAYITEM_GC, [res,

Modified: pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/ll2ctypes.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/ll2ctypes.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/ll2ctypes.py	Tue Aug 25 16:25:15 2009
@@ -533,8 +533,8 @@
         if T is base_ptr_lltype():
             return new_opaque_object(llobj)
         if T == llmemory.GCREF:
-            if isinstance(llobj, _llgcref):
-                return ctypes.c_void_p(llobj.intval)
+            if isinstance(llobj._obj, _llgcopaque):
+                return ctypes.c_void_p(llobj._obj.intval)
             container = llobj._obj.container
             T = lltype.Ptr(lltype.typeOf(container))
             # otherwise it came from integer and we want a c_void_p with
@@ -721,8 +721,7 @@
                                           _callable=_callable)
         elif isinstance(T.TO, lltype.OpaqueType):
             if T == llmemory.GCREF:
-                # XXX obscure hack
-                return _llgcref(cobj)
+                container = _llgcopaque(cobj)
             else:
                 container = lltype._opaque(T.TO)
         else:
@@ -1085,8 +1084,9 @@
     def __ne__(self, other):
         return not self == other
 
-class _llgcref(object):
-    _TYPE = llmemory.GCREF
+class _llgcopaque(lltype._container):
+    _TYPE = llmemory.GCREF.TO
+    _name = "_llgcopaque"
 
     def __init__(self, void_p):
         if isinstance(void_p, (int, long)):
@@ -1095,24 +1095,23 @@
             self.intval = intmask(void_p.value)
 
     def __eq__(self, other):
-        if isinstance(other, _llgcref):
+        if isinstance(other, _llgcopaque):
             return self.intval == other.intval
-        return force_cast(lltype.Signed, other) == self.intval
+        if other.container._storage in (None, True):
+            return False
+        return force_cast(lltype.Signed, other._as_ptr()) == self.intval
 
     def __ne__(self, other):
         return not self == other
 
-    def __nonzero__(self):
-        return bool(self.intval)
-
     def _cast_to_ptr(self, PTRTYPE):
-        return force_cast(PTRTYPE, self.intval)
+         return force_cast(PTRTYPE, self.intval)
 
-    def _cast_to_int(self):
-        return self.intval
+##     def _cast_to_int(self):
+##         return self.intval
 
-    def _cast_to_adr(self):
-        return _lladdress(self.intval)
+##     def _cast_to_adr(self):
+##         return _lladdress(self.intval)
 
 def cast_adr_to_int(addr):
     if isinstance(addr, llmemory.fakeaddress):

Modified: pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/lltype.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/lltype.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/lltype.py	Tue Aug 25 16:25:15 2009
@@ -798,8 +798,8 @@
                         "%s to %s" % (CURTYPE, PTRTYPE))
     if (isinstance(CURTYPE.TO, OpaqueType)
         and not isinstance(PTRTYPE.TO, OpaqueType)):
-        if hasattr(ptr, '_cast_to_ptr'):
-            return ptr._cast_to_ptr(PTRTYPE)
+        if hasattr(ptr._obj, '_cast_to_ptr'):
+            return ptr._obj._cast_to_ptr(PTRTYPE)
         if not ptr:
             return nullptr(PTRTYPE.TO)
         try:
@@ -1696,7 +1696,7 @@
 
     def __eq__(self, other):
         if self.__class__ is not other.__class__:
-            return False
+            return NotImplemented
         if hasattr(self, 'container') and hasattr(other, 'container'):
             obj1 = self.container._normalizedcontainer()
             obj2 = other.container._normalizedcontainer()
@@ -1705,6 +1705,8 @@
             return self is other
 
     def __ne__(self, other):
+        if self.__class__ is not other.__class__:
+            return NotImplemented
         return not (self == other)
 
     def __hash__(self):

Modified: pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/test/test_ll2ctypes.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/test/test_ll2ctypes.py	Tue Aug 25 16:25:15 2009
@@ -998,7 +998,7 @@
         assert v
         v2 = ctypes2lltype(llmemory.GCREF, ctypes.c_void_p(1235))
         assert v2 != v
-        
+
     def test_gcref_type(self):
         NODE = lltype.GcStruct('NODE')
         node = lltype.malloc(NODE)
@@ -1020,3 +1020,77 @@
         ref = llmemory.NULL
         value = rffi.cast(lltype.Signed, ref)
         assert value == 0
+
+    def test_gcref_truth(self):
+        p0 = ctypes.c_void_p(0)
+        ref0 = ctypes2lltype(llmemory.GCREF, p0)
+        assert not ref0
+
+        p1234 = ctypes.c_void_p(1234)
+        ref1234 = ctypes2lltype(llmemory.GCREF, p1234)        
+        assert p1234
+
+    def test_gcref_casts(self):
+        p0 = ctypes.c_void_p(0)
+        ref0 = ctypes2lltype(llmemory.GCREF, p0)
+
+        assert lltype.cast_ptr_to_int(ref0) == 0
+        assert llmemory.cast_ptr_to_adr(ref0) == llmemory.NULL
+
+        NODE = lltype.GcStruct('NODE')
+        assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref0) == lltype.nullptr(NODE)
+
+        node = lltype.malloc(NODE)
+        ref1 = lltype.cast_opaque_ptr(llmemory.GCREF, node)
+
+        intval  = rffi.cast(lltype.Signed, node)
+        intval1 = rffi.cast(lltype.Signed, ref1)
+
+        assert intval == intval1
+
+        ref2 = ctypes2lltype(llmemory.GCREF, intval1)
+
+        assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref2) == node
+        
+        #addr = llmemory.cast_ptr_to_adr(ref1)
+        #assert llmemory.cast_adr_to_int(addr) == intval
+
+        #assert lltype.cast_ptr_to_int(ref1) == intval
+
+    def test_mixed_gcref_comparison(self):
+        NODE = lltype.GcStruct('NODE')
+        node = lltype.malloc(NODE)
+        ref1 = lltype.cast_opaque_ptr(llmemory.GCREF, node)
+        ref2 = rffi.cast(llmemory.GCREF, 123)
+
+        assert ref1 != ref2
+        assert not (ref1 == ref2)
+
+        assert ref2 != ref1
+        assert not (ref2 == ref1)
+
+        assert node._obj._storage is True
+
+        # forced!
+        rffi.cast(lltype.Signed, ref1)
+        assert node._obj._storage not in (True, None)
+
+        assert ref1 != ref2
+        assert not (ref1 == ref2)
+
+        assert ref2 != ref1
+        assert not (ref2 == ref1)
+
+    def test_gcref_comparisons_back_and_forth(self):
+        NODE = lltype.GcStruct('NODE')
+        node = lltype.malloc(NODE)
+        ref1 = lltype.cast_opaque_ptr(llmemory.GCREF, node)
+        numb = rffi.cast(lltype.Signed, ref1)
+        ref2 = rffi.cast(llmemory.GCREF, numb)
+        assert ref1 == ref2
+        assert ref2 == ref1
+        assert not (ref1 != ref2)
+        assert not (ref2 != ref1)        
+   
+        
+    



More information about the Pypy-commit mailing list