[pypy-svn] r46976 - in pypy/dist/pypy/translator/jvm: . src/pypy

niko at codespeak.net niko at codespeak.net
Thu Sep 27 22:18:41 CEST 2007


Author: niko
Date: Thu Sep 27 22:18:41 2007
New Revision: 46976

Added:
   pypy/dist/pypy/translator/jvm/src/pypy/PyPyWeakRef.java
Modified:
   pypy/dist/pypy/translator/jvm/builtin.py
   pypy/dist/pypy/translator/jvm/constant.py
   pypy/dist/pypy/translator/jvm/database.py
   pypy/dist/pypy/translator/jvm/generator.py
   pypy/dist/pypy/translator/jvm/metavm.py
   pypy/dist/pypy/translator/jvm/opcodes.py
   pypy/dist/pypy/translator/jvm/typesystem.py
Log:
implement new-style weakrefs in the jvm



Modified: pypy/dist/pypy/translator/jvm/builtin.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/builtin.py	(original)
+++ pypy/dist/pypy/translator/jvm/builtin.py	Thu Sep 27 22:18:41 2007
@@ -52,7 +52,7 @@
         ARGS, RESULT = self.gen.erased_types(methodnm)
         jargtypes = [self.db.lltype_to_cts(P) for P in ARGS]
         jrettype = self.db.lltype_to_cts(RESULT)
-
+        
         if self.OOTYPE.__class__ in bridged_objects:
             # Bridged objects are ones where we have written a java class
             # that has methods with the correct names and types already
@@ -71,6 +71,7 @@
 
 bridged_objects = (
     ootype.DictItemsIterator,
+    ootype.WeakReference.__class__
     )
 
 built_in_methods = {

Modified: pypy/dist/pypy/translator/jvm/constant.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/constant.py	(original)
+++ pypy/dist/pypy/translator/jvm/constant.py	Thu Sep 27 22:18:41 2007
@@ -7,7 +7,7 @@
      StaticMethodConst, CustomDictConst, WeakRefConst, push_constant, \
      MAX_CONST_PER_STEP
 from pypy.translator.jvm.typesystem import \
-     jObject, jVoid, jWeakRef, JvmClassType
+     jObject, jVoid, jPyPyWeakRef, JvmClassType
 
 jPyPyConstantInit = JvmClassType('pypy.ConstantInit')
 jPyPyConstantInitMethod = Method.s(jPyPyConstantInit, 'init', [], jVoid)
@@ -188,17 +188,16 @@
     PRIORITY = 200
 
     def jtype(self):
-        return jWeakRef
+        return jPyPyWeakRef
 
-    def create_pointer(self, gen):
-        gen.prepare_cast_ptr_to_weak_address()
+    def create_pointer(self, gen):        
         if not self.value:
             TYPE = ootype.ROOT
             gen.push_null(TYPE)
         else:
             TYPE = self.value._TYPE
             push_constant(self.db, self.value._TYPE, self.value, gen)
-        gen.finalize_cast_ptr_to_weak_address(TYPE)
+        gen.create_weakref(TYPE)
 
     def initialize_data(self, constgen, gen):
         gen.pop(ootype.ROOT)

Modified: pypy/dist/pypy/translator/jvm/database.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/database.py	(original)
+++ pypy/dist/pypy/translator/jvm/database.py	Thu Sep 27 22:18:41 2007
@@ -460,7 +460,6 @@
         ootype.UniChar:          jvmtype.jChar,
         ootype.Class:            jvmtype.jClass,
         ootype.ROOT:             jvmtype.jObject,  # treat like a scalar
-        #WeakGcAddress:           jvmtype.jWeakRef,
     }
 
     # Dictionary for non-scalar types; in this case, if we see the key, we
@@ -472,6 +471,7 @@
         ootype.Dict:             jvmtype.jHashMap,
         ootype.DictItemsIterator:jvmtype.jPyPyDictItemsIterator,
         ootype.CustomDict:       jvmtype.jPyPyCustomDict,
+        ootype.WeakReference:    jvmtype.jPyPyWeakRef,
         ll_os.STAT_RESULT:       jvmtype.jPyPyStatResult,
         }
 

Modified: pypy/dist/pypy/translator/jvm/generator.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/generator.py	(original)
+++ pypy/dist/pypy/translator/jvm/generator.py	Thu Sep 27 22:18:41 2007
@@ -12,7 +12,7 @@
      jObject, jByteArray, jPyPyExcWrap, jIntegerClass, jLongClass, \
      jDoubleClass, jCharClass, jStringBuilder, JvmScalarType, jArrayList, \
      jObjectArray, jPyPyInterlink, jPyPyCustomDict, jPyPyEquals, \
-     jPyPyHashCode, jMap, jWeakRef, jSystem, jll_os
+     jPyPyHashCode, jMap, jPyPyWeakRef, jSystem, jll_os
 
 # ___________________________________________________________________________
 # Miscellaneous helper functions
@@ -429,6 +429,8 @@
 CLASSGETNAME =          Method.v(jClass, 'getName', (), jString)
 CUSTOMDICTMAKE =        Method.s(jPyPyCustomDict, 'make',
                                  (jPyPyEquals, jPyPyHashCode), jPyPyCustomDict)
+PYPYWEAKREFCREATE =     Method.s(jPyPyWeakRef, 'create', (jObject,), jPyPyWeakRef)
+PYPYWEAKREFGET =        Method.v(jPyPyWeakRef, 'll_get', (), jObject)
 
 # ___________________________________________________________________________
 # Fields
@@ -1137,17 +1139,8 @@
         else:
             # Big hack to avoid exponential notation:
             self.emit(LDC2, "%22.22f" % value)
-
-    def prepare_cast_ptr_to_weak_address(self):
-        """
-        To cast a pointer to a weak ref is a 2-step process.
-        First, invoke this routine.  Then, invoke what is needed
-        to push the value, then invoke finalize_cast_ptr_to_weak_address 
-        """
-        self.emit(NEW, jWeakRef)
-        self.emit(DUP)
         
-    def finalize_cast_ptr_to_weak_address(self, OOTYPE):
+    def create_weakref(self, OOTYPE):
         """
         After prepare_cast_ptr_to_weak_address has been called, and the
         ptr to cast has been pushed, you can invoke this routine.
@@ -1155,17 +1148,15 @@
         The result will be that at the top of the stack is a weak reference.
         """
         self.prepare_generic_argument(OOTYPE) 
-        ctor = Method.c(jWeakRef, (jObject,))
-        self.emit(ctor)
-
-    def cast_weak_address_to_ptr(self, OOTYPE):
+        self.emit(PYPYWEAKREFCREATE)
+    
+    def deref_weakref(self, OOTYPE):
         """
         If a weak ref is at the top of the stack, yields the object
         that this weak ref is a pointer to.  OOTYPE is the kind of object
         you had a weak reference to.
         """
-        get_mthd = Method.v(jWeakRef, 'get', (), jObject)
-        self.emit(get_mthd)
+        self.emit(PYPYWEAKREFGET)
         self.prepare_generic_result(OOTYPE)
 
     # __________________________________________________________________

Modified: pypy/dist/pypy/translator/jvm/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/metavm.py	(original)
+++ pypy/dist/pypy/translator/jvm/metavm.py	Thu Sep 27 22:18:41 2007
@@ -126,20 +126,20 @@
         generator.emit(jvmgen.CUSTOMDICTMAKE)
 NewCustomDict = _NewCustomDict()
 
-#XXX adapt to new way of things
+#XXX These classes have been adapted to the new
+#XXX WeakRef methods, but don't appear to be needed.
 #class _CastPtrToWeakAddress(MicroInstruction):
 #    def render(self, generator, op):
 #        arg = op.args[0]
-#        generator.prepare_cast_ptr_to_weak_address()
 #        generator.load(arg)
-#        generator.finalize_cast_ptr_to_weak_address(arg.concretetype)
+#        generator.create_weakref(arg.concretetype)
 #        generator.store(op.result)
 #CastPtrToWeakAddress = _CastPtrToWeakAddress()
         
 #class _CastWeakAddressToPtr(MicroInstruction):
 #    def render(self, generator, op):
 #        RESULTTYPE = op.result.concretetype
-#        generator.cast_weak_address_to_ptr(RESULTTYPE)
+#        generator.deref_weakref(RESULTTYPE)
 #CastWeakAddressToPtr = _CastWeakAddressToPtr()
 
 

Modified: pypy/dist/pypy/translator/jvm/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/opcodes.py	(original)
+++ pypy/dist/pypy/translator/jvm/opcodes.py	Thu Sep 27 22:18:41 2007
@@ -65,6 +65,7 @@
     'direct_call':              [Call, StoreResult],
     'indirect_call':            [PushAllArgs, IndirectCall, StoreResult],
 
+    #'cast_ptr_to_weakadr':      CastPtrToWeakAddress
     'gc__collect':              jvmgen.SYSTEMGC,
     'gc_set_max_heap_size':     Ignore,
     'resume_point':             Ignore,

Added: pypy/dist/pypy/translator/jvm/src/pypy/PyPyWeakRef.java
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/jvm/src/pypy/PyPyWeakRef.java	Thu Sep 27 22:18:41 2007
@@ -0,0 +1,23 @@
+package pypy;
+
+import java.lang.ref.WeakReference;
+
+public final class PyPyWeakRef {
+    WeakReference wref;
+    
+    public static PyPyWeakRef create(Object obj) {
+        PyPyWeakRef res = new PyPyWeakRef();
+        res.ll_set(obj);
+        return res;
+    }
+
+    public void ll_set(Object obj)
+    {            
+        this.wref = new WeakReference(obj);
+    }
+
+    public Object ll_deref()
+    {
+        return this.wref.get();
+    }
+}

Modified: pypy/dist/pypy/translator/jvm/typesystem.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/typesystem.py	(original)
+++ pypy/dist/pypy/translator/jvm/typesystem.py	Thu Sep 27 22:18:41 2007
@@ -20,8 +20,8 @@
 
 Python Unicode strings, on the other hand, map directly to Java Strings.
 
-WeakRefs can hopefully map to Java Weak References in a straight
-forward fashion.
+WeakRefs are mapped to a thin wrapper class, PyPyWeakRef, to allow for
+mutation of the object being referenced (the ll_set method).
 
 Collections can hopefully map to Java collections instances.  Note
 that JVM does not have an idea of generic typing at its lowest level
@@ -170,7 +170,6 @@
 jMath = JvmClassType('java.lang.Math')
 jList = JvmInterfaceType('java.util.List')
 jArrayList = JvmClassType('java.util.ArrayList')
-jWeakRef = JvmClassType('java.lang.ref.WeakReference')
 jPyPy = JvmClassType('pypy.PyPy')
 jPyPyExcWrap = JvmClassType('pypy.ExceptionWrapper')
 jPyPyMain = JvmClassType('pypy.Main')
@@ -178,6 +177,7 @@
 jPyPyInterlink = JvmClassType('pypy.Interlink')
 jPyPyCustomDict = JvmClassType('pypy.CustomDict')
 jPyPyStatResult = JvmClassType('pypy.StatResult')
+jPyPyWeakRef = JvmClassType('pypy.PyPyWeakRef')
 jll_os = JvmClassType('pypy.ll_os')
 
 jArithmeticException = JvmClassType('java.lang.ArithmeticException', throwable=True)



More information about the Pypy-commit mailing list