[pypy-commit] pypy stmgc-c7: Add 'cast_gcref_to_instance' here. The JIT has code that does

arigo noreply at buildbot.pypy.org
Tue Nov 11 16:21:26 CET 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: stmgc-c7
Changeset: r74446:d93251afcbd4
Date: 2014-11-11 15:15 +0100
http://bitbucket.org/pypy/pypy/changeset/d93251afcbd4/

Log:	Add 'cast_gcref_to_instance' here. The JIT has code that does
	indirectly the same thing, but it's cleaner to have a direct inverse
	of 'cast_instance_to_gcref'. (grafted from
	5171dd85f03b3b2eea33c6402e12a4a9d6129d9d)

diff --git a/rpython/rtyper/annlowlevel.py b/rpython/rtyper/annlowlevel.py
--- a/rpython/rtyper/annlowlevel.py
+++ b/rpython/rtyper/annlowlevel.py
@@ -509,6 +509,13 @@
                                   % (ptr, Class))
     return ptr
 
+ at specialize.arg(0)
+def cast_gcref_to_instance(Class, ptr):
+    """Reverse the hacking done in cast_instance_to_gcref()."""
+    from rpython.rtyper.rclass import OBJECTPTR
+    ptr = lltype.cast_opaque_ptr(OBJECTPTR, ptr)
+    return cast_base_ptr_to_instance(Class, ptr)
+
 class CastBasePtrToInstanceEntry(extregistry.ExtRegistryEntry):
     _about_ = cast_base_ptr_to_instance
 
diff --git a/rpython/rtyper/test/test_annlowlevel.py b/rpython/rtyper/test/test_annlowlevel.py
--- a/rpython/rtyper/test/test_annlowlevel.py
+++ b/rpython/rtyper/test/test_annlowlevel.py
@@ -4,7 +4,7 @@
 
 from rpython.rtyper.test.tool import BaseRtypingTest
 from rpython.rtyper.lltypesystem.rstr import mallocstr, mallocunicode
-from rpython.rtyper.lltypesystem import lltype
+from rpython.rtyper.lltypesystem import lltype, llmemory
 from rpython.rtyper.annlowlevel import hlstr, llstr
 from rpython.rtyper.annlowlevel import hlunicode, llunicode
 from rpython.rtyper import annlowlevel
@@ -65,6 +65,15 @@
         y = annlowlevel.cast_base_ptr_to_instance(X, ptr)
         assert y is x
 
+    def test_cast_instance_to_gcref(self):
+        class X(object):
+            pass
+        x = X()
+        ptr = annlowlevel.cast_instance_to_gcref(x)
+        assert lltype.typeOf(ptr) == llmemory.GCREF
+        y = annlowlevel.cast_gcref_to_instance(X, ptr)
+        assert y is x
+
     def test_delayedptr(self):
         FUNCTYPE = lltype.FuncType([], lltype.Signed)
         name = "delayed!myfunc"


More information about the pypy-commit mailing list