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

mwh at codespeak.net mwh at codespeak.net
Tue May 16 13:47:09 CEST 2006


Author: mwh
Date: Tue May 16 13:47:06 2006
New Revision: 27269

Modified:
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/lltypesystem/llmemory.py
   pypy/dist/pypy/rpython/lltypesystem/lloperation.py
   pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py
   pypy/dist/pypy/rpython/raddress.py
   pypy/dist/pypy/rpython/rbuiltin.py
Log:
add a weakgcaddress primitive and casts to and from pointers.  and associated
hair.

the purpose of this is to have a type that will be edited as necessary by a
moving GC, but not treated as keeping the addressed object alive.


Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Tue May 16 13:47:06 2006
@@ -6,7 +6,7 @@
 from pypy.annotation.model import SomeInteger, SomeObject, SomeChar, SomeBool
 from pypy.annotation.model import SomeString, SomeTuple, SomeSlice
 from pypy.annotation.model import SomeUnicodeCodePoint, SomeAddress
-from pypy.annotation.model import SomeFloat, unionof
+from pypy.annotation.model import SomeFloat, SomeWeakGcAddress, unionof
 from pypy.annotation.model import SomePBC, SomeInstance, SomeDict
 from pypy.annotation.model import SomeExternalObject
 from pypy.annotation.model import annotation_to_lltype, lltype_to_annotation, ll_to_annotation
@@ -305,6 +305,13 @@
     assert s_type.is_constant()
     return SomePtr(s_type.const)
 
+def llmemory_cast_ptr_to_weakadr(s):
+    return SomeWeakGcAddress()
+
+def llmemory_cast_weakadr_to_ptr(s, s_type):
+    assert s_type.is_constant()
+    return SomePtr(s_type.const)
+
 def llmemory_cast_adr_to_int(s):
     return SomeInteger() # xxx
 
@@ -353,6 +360,8 @@
 BUILTIN_ANALYZERS[pypy.rpython.lltypesystem.llmemory.cast_ptr_to_adr] = llmemory_cast_ptr_to_adr
 BUILTIN_ANALYZERS[pypy.rpython.lltypesystem.llmemory.cast_adr_to_ptr] = llmemory_cast_adr_to_ptr
 BUILTIN_ANALYZERS[pypy.rpython.lltypesystem.llmemory.cast_adr_to_int] = llmemory_cast_adr_to_int
+BUILTIN_ANALYZERS[pypy.rpython.lltypesystem.llmemory.cast_ptr_to_weakadr] = llmemory_cast_ptr_to_weakadr
+BUILTIN_ANALYZERS[pypy.rpython.lltypesystem.llmemory.cast_weakadr_to_ptr] = llmemory_cast_weakadr_to_ptr
 BUILTIN_ANALYZERS[pypy.rpython.rstack.yield_current_frame_to_caller] = (
     rstack_yield_current_frame_to_caller)
 

Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Tue May 16 13:47:06 2006
@@ -461,6 +461,8 @@
     def can_be_none(self):
         return False
 
+class SomeWeakGcAddress(SomeObject):
+    immutable = True
 
 # The following class is used to annotate the intermediate value that
 # appears in expressions of the form:

Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Tue May 16 13:47:06 2006
@@ -334,7 +334,8 @@
         vals = [self.getval(x) for x in operation.args]
         # XXX these special cases DO pile up, do something better here
         if operation.opname in ['cast_pointer', 'ooupcast', 'oodowncast',
-                                'cast_adr_to_ptr', 'cast_int_to_ptr',
+                                'cast_adr_to_ptr', 'cast_weakadr_to_ptr',
+                                'cast_int_to_ptr',
                                 'cast_opaque_ptr', 'unsafe_call',
                                 'cast_primitive']:
             vals.insert(0, operation.result.concretetype)
@@ -616,6 +617,14 @@
         assert checkadr(adr)
         return llmemory.cast_adr_to_int(adr)
 
+    def op_cast_ptr_to_weakadr(self, ptr):
+        assert checkptr(ptr)
+        return llmemory.cast_ptr_to_weakadr(ptr)
+
+    def op_cast_weakadr_to_ptr(self, TYPE, wadr):
+        assert lltype.typeOf(wadr) == llmemory.WeakGcAddress
+        return llmemory.cast_weakadr_to_ptr(wadr, TYPE)
+
     def op_cast_int_to_float(self, i):
         assert type(i) is int
         return float(i)

Modified: pypy/dist/pypy/rpython/lltypesystem/llmemory.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/llmemory.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/llmemory.py	Tue May 16 13:47:06 2006
@@ -463,6 +463,35 @@
 # ____________________________________________________________
 
 import weakref
+
+class fakeweakaddress(object):
+    def __init__(self, ob):
+        if ob is not None:
+            self.ref = weakref.ref(ob)
+        else:
+            self.ref = None
+    def get(self):
+        if self.ref is None:
+            raise NullAddressError
+        ob = self.ref()
+        if ob is None:
+            raise NullAddressError
+        return ob
+
+WeakGcAddress = lltype.Primitive("WeakGcAddress",
+                                 fakeweakaddress(None))
+
+def cast_ptr_to_weakadr(obj):
+    assert isinstance(lltype.typeOf(obj), lltype.Ptr)
+    return fakeweakaddress(obj)
+
+def cast_weakadr_to_ptr(adr, EXPECTED_TYPE):
+    return adr.get()
+
+fakeweakaddress._TYPE = WeakGcAddress
+
+# ____________________________________________________________
+
 _gc_struct2header = weakref.WeakKeyDictionary()
 _gc_header2struct = weakref.WeakKeyDictionary()
 

Modified: pypy/dist/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/lloperation.py	Tue May 16 13:47:06 2006
@@ -301,6 +301,8 @@
     'adr_ge':               LLOp(canfold=True),
     'cast_ptr_to_adr':      LLOp(canfold=True),
     'cast_adr_to_ptr':      LLOp(canfold=True),
+    'cast_ptr_to_weakadr':  LLOp(canfold=True),
+    'cast_weakadr_to_ptr':  LLOp(canfold=True),
     'cast_adr_to_int':      LLOp(canfold=True),
 
     # __________ GC operations __________

Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_llmemory.py	Tue May 16 13:47:06 2006
@@ -98,6 +98,18 @@
     res = interpret(f, [])
     assert res
 
+def test_weak_casts():
+    from pypy.rpython.memory.test.test_llinterpsim import interpret
+    S = lltype.GcStruct("S", ("x", lltype.Signed))
+    Sptr = lltype.Ptr(S)
+    def f():
+        s1 = lltype.malloc(S)
+        adr = cast_ptr_to_weakadr(s1)
+        s2 = cast_weakadr_to_ptr(adr, Sptr)
+        return s1 == s2
+    res = interpret(f, [])
+    assert res
+
 def test_fakeaccessor():
     S = lltype.GcStruct("S", ("x", lltype.Signed), ("y", lltype.Signed))
     s = lltype.malloc(S)

Modified: pypy/dist/pypy/rpython/raddress.py
==============================================================================
--- pypy/dist/pypy/rpython/raddress.py	(original)
+++ pypy/dist/pypy/rpython/raddress.py	Tue May 16 13:47:06 2006
@@ -2,7 +2,8 @@
 from pypy.annotation.pairtype import pairtype
 from pypy.annotation import model as annmodel
 from pypy.rpython.memory.lladdress import _address
-from pypy.rpython.lltypesystem.llmemory import NULL, Address, cast_adr_to_int
+from pypy.rpython.lltypesystem.llmemory import NULL, Address, \
+     cast_adr_to_int, WeakGcAddress
 from pypy.rpython.rmodel import Repr, IntegerRepr
 from pypy.rpython.rptr import PtrRepr
 from pypy.rpython.lltypesystem import lltype
@@ -14,6 +15,13 @@
     def rtyper_makekey(self):
         return self.__class__,
 
+class __extend__(annmodel.SomeWeakGcAddress):
+    def rtyper_makerepr(self, rtyper):
+        return weakgcaddress_repr
+    
+    def rtyper_makekey(self):
+        return self.__class__,
+
 class __extend__(annmodel.SomeTypedAddressAccess):
     def rtyper_makerepr(self, rtyper):
         return TypedAddressAccessRepr(self.type)
@@ -128,3 +136,8 @@
 
     def convert_from_to((r_ptr, r_addr), v, llops):
         return llops.genop('cast_ptr_to_adr', [v], resulttype=Address)
+
+class WeakGcAddressRepr(Repr):
+    lowleveltype = WeakGcAddress
+
+weakgcaddress_repr = WeakGcAddressRepr()

Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Tue May 16 13:47:06 2006
@@ -594,7 +594,23 @@
     return hop.genop('cast_adr_to_int', [adr],
                      resulttype = lltype.Signed)
 
+def rtype_cast_ptr_to_weakadr(hop):
+    vlist = hop.inputargs(hop.args_r[0])
+    assert isinstance(vlist[0].concretetype, lltype.Ptr)
+    hop.exception_cannot_occur()
+    return hop.genop('cast_ptr_to_weakadr', vlist,
+                     resulttype = llmemory.WeakGcAddress)
+
+def rtype_cast_weakadr_to_ptr(hop):
+    assert isinstance(hop.args_r[0], raddress.WeakGcAddressRepr)
+    adr, TYPE = hop.inputargs(hop.args_r[0], lltype.Void)
+    hop.exception_cannot_occur()
+    return hop.genop('cast_weakadr_to_ptr', [adr],
+                     resulttype = TYPE.value)
+
 BUILTIN_TYPER[llmemory.cast_ptr_to_adr] = rtype_cast_ptr_to_adr
 BUILTIN_TYPER[llmemory.cast_adr_to_ptr] = rtype_cast_adr_to_ptr
 BUILTIN_TYPER[llmemory.cast_adr_to_int] = rtype_cast_adr_to_int
+BUILTIN_TYPER[llmemory.cast_ptr_to_weakadr] = rtype_cast_ptr_to_weakadr
+BUILTIN_TYPER[llmemory.cast_weakadr_to_ptr] = rtype_cast_weakadr_to_ptr
 



More information about the Pypy-commit mailing list