[pypy-svn] r75115 - in pypy/branch/blackhole-improvement/pypy: annotation rpython rpython/lltypesystem rpython/test translator/c/src

arigo at codespeak.net arigo at codespeak.net
Fri Jun 4 18:00:08 CEST 2010


Author: arigo
Date: Fri Jun  4 18:00:06 2010
New Revision: 75115

Modified:
   pypy/branch/blackhole-improvement/pypy/annotation/builtin.py
   pypy/branch/blackhole-improvement/pypy/rpython/llinterp.py
   pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/llmemory.py
   pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/rffi.py
   pypy/branch/blackhole-improvement/pypy/rpython/rbuiltin.py
   pypy/branch/blackhole-improvement/pypy/rpython/test/test_rptr.py
   pypy/branch/blackhole-improvement/pypy/translator/c/src/address.h
Log:
Refactor cast_adr_to_int(), step 1.


Modified: pypy/branch/blackhole-improvement/pypy/annotation/builtin.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/annotation/builtin.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/annotation/builtin.py	Fri Jun  4 18:00:06 2010
@@ -340,7 +340,7 @@
     assert s_type.is_constant()
     return SomePtr(s_type.const)
 
-def llmemory_cast_adr_to_int(s):
+def llmemory_cast_adr_to_int(s, s_mode=None):
     return SomeInteger() # xxx
 
 def llmemory_cast_int_to_adr(s):

Modified: pypy/branch/blackhole-improvement/pypy/rpython/llinterp.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/rpython/llinterp.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/rpython/llinterp.py	Fri Jun  4 18:00:06 2010
@@ -793,9 +793,9 @@
         checkptr(ptr)
         return llmemory.cast_ptr_to_adr(ptr)
 
-    def op_cast_adr_to_int(self, adr):
+    def op_cast_adr_to_int(self, adr, mode):
         checkadr(adr)
-        return llmemory.cast_adr_to_int(adr)
+        return llmemory.cast_adr_to_int(adr, mode)
 
     def op_weakref_create(self, v_obj):
         def objgetter():    # special support for gcwrapper.py

Modified: pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/llmemory.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/llmemory.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/llmemory.py	Fri Jun  4 18:00:06 2010
@@ -479,9 +479,14 @@
         else:
             return lltype.nullptr(EXPECTED_TYPE.TO)
 
-    def _cast_to_int(self):
+    def _cast_to_int(self, symbolic=False):
         if self:
-            return AddressAsInt(self)
+            if symbolic:
+                return AddressAsInt(self)
+            else:
+                # This is a bit annoying. We want this method to still work
+                # when the pointed-to object is dead
+                return self.ptr._cast_to_int(False)
         else:
             return 0
 
@@ -625,8 +630,21 @@
 def cast_adr_to_ptr(adr, EXPECTED_TYPE):
     return adr._cast_to_ptr(EXPECTED_TYPE)
 
-def cast_adr_to_int(adr):
-    return adr._cast_to_int()
+def cast_adr_to_int(adr, mode="emulated"):
+    # The following modes are supported before translation (after
+    # translation, it's all just a cast):
+    # * mode="emulated": goes via lltype.cast_ptr_to_int(), which returns some
+    #     number based on id().  The difference is that it works even if the
+    #     address is that of a dead object.
+    # * mode="symbolic": returns an AddressAsInt instance, which can only be
+    #     cast back to an address later.
+    # * mode="forced": uses rffi.cast() to return a real number.
+    assert mode in ("emulated", "symbolic", "forced")
+    res = adr._cast_to_int(symbolic = (mode != "emulated"))
+    if mode == "forced":
+        from pypy.rpython.lltypesystem.rffi import cast
+        res = cast(lltype.Signed, res)
+    return res
 
 _NONGCREF = lltype.Ptr(lltype.OpaqueType('NONGCREF'))
 def cast_int_to_adr(int):

Modified: pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/rffi.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/rffi.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/rpython/lltypesystem/rffi.py	Fri Jun  4 18:00:06 2010
@@ -905,11 +905,3 @@
     """
     return cast(lltype.Signed, getattr(pdst, fieldname))
 getintfield._annspecialcase_ = 'specialize:ll_and_arg(1)'
-
-def get_real_int(i):
-    """No-op after translation.  Before translation, it convert an
-    AddressAsInt into a real integer, to use e.g. as a hash.  Do not store
-    the real integer (or the dict using this as a hash) across translation."""
-    # although it's not really a cast, this force_cast forces
-    # the AddressAsInt to be evaluated with ctypes
-    return cast(lltype.Signed, i)

Modified: pypy/branch/blackhole-improvement/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/rpython/rbuiltin.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/rpython/rbuiltin.py	Fri Jun  4 18:00:06 2010
@@ -604,9 +604,14 @@
 
 def rtype_cast_adr_to_int(hop):
     assert isinstance(hop.args_r[0], raddress.AddressRepr)
-    adr, = hop.inputargs(hop.args_r[0])
+    adr = hop.inputarg(hop.args_r[0], arg=0)
+    if len(hop.args_s) == 1:
+        mode = "emulated"
+    else:
+        mode = hop.args_s[1].const
     hop.exception_cannot_occur()
-    return hop.genop('cast_adr_to_int', [adr],
+    return hop.genop('cast_adr_to_int',
+                     [adr, hop.inputconst(lltype.Void, mode)],
                      resulttype = lltype.Signed)
 
 def rtype_cast_int_to_adr(hop):

Modified: pypy/branch/blackhole-improvement/pypy/rpython/test/test_rptr.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/rpython/test/test_rptr.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/rpython/test/test_rptr.py	Fri Jun  4 18:00:06 2010
@@ -176,6 +176,31 @@
     res = interpret(fn, [5])
     assert res is True
 
+def test_cast_adr_to_int():
+    S = Struct('S')
+    p = malloc(S, immortal=True)
+    def fn(n):
+        a = llmemory.cast_ptr_to_adr(p)
+        if n == 2:
+            return llmemory.cast_adr_to_int(a, "emulated")
+        elif n == 4:
+            return llmemory.cast_adr_to_int(a, "symbolic")
+        else:
+            return llmemory.cast_adr_to_int(a, "forced")
+
+    res = interpret(fn, [2])
+    assert type(res) is int
+    assert res == cast_ptr_to_int(p)
+    #
+    res = interpret(fn, [4])
+    assert isinstance(res, llmemory.AddressAsInt)
+    assert llmemory.cast_int_to_adr(res) == llmemory.cast_ptr_to_adr(p)
+    #
+    res = interpret(fn, [6])
+    assert type(res) is int
+    from pypy.rpython.lltypesystem import rffi
+    assert res == rffi.cast(Signed, p)
+
 def test_flavored_malloc():
     T = GcStruct('T', ('y', Signed))
     def fn(n):

Modified: pypy/branch/blackhole-improvement/pypy/translator/c/src/address.h
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/translator/c/src/address.h	(original)
+++ pypy/branch/blackhole-improvement/pypy/translator/c/src/address.h	Fri Jun  4 18:00:06 2010
@@ -16,5 +16,5 @@
 #define OP_ADR_LT(x,y,r)	  r = ((x) <  (y))
 #define OP_ADR_GE(x,y,r)	  r = ((x) >= (y))
 
-#define OP_CAST_ADR_TO_INT(x, r)     r = ((long)x)
-#define OP_CAST_INT_TO_ADR(x, r)     r = ((void *)(x))
+#define OP_CAST_ADR_TO_INT(x, mode, r)   r = ((long)x)
+#define OP_CAST_INT_TO_ADR(x, r)         r = ((void *)(x))



More information about the Pypy-commit mailing list