[pypy-svn] r62384 - in pypy/branch/pyjitpl5/pypy/translator/c: . test

arigo at codespeak.net arigo at codespeak.net
Mon Mar 2 14:42:11 CET 2009


Author: arigo
Date: Mon Mar  2 14:42:10 2009
New Revision: 62384

Modified:
   pypy/branch/pyjitpl5/pypy/translator/c/database.py
   pypy/branch/pyjitpl5/pypy/translator/c/primitive.py
   pypy/branch/pyjitpl5/pypy/translator/c/test/test_lladdresses.py
Log:
Support for GCREF instances in the C backend.  Implemented by
considering them as primitive type (hackish, but less than other
solutions I thought about...).


Modified: pypy/branch/pyjitpl5/pypy/translator/c/database.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/translator/c/database.py	(original)
+++ pypy/branch/pyjitpl5/pypy/translator/c/database.py	Mon Mar  2 14:42:10 2009
@@ -3,7 +3,7 @@
      Struct, Array, FuncType, PyObject, Void, \
      ContainerType, OpaqueType, FixedSizeArray, _uninitialized
 from pypy.rpython.lltypesystem import lltype
-from pypy.rpython.lltypesystem.llmemory import WeakRef, _WeakRefType
+from pypy.rpython.lltypesystem.llmemory import WeakRef, _WeakRefType, GCREF
 from pypy.rpython.lltypesystem.rffi import CConstant
 from pypy.tool.sourcetools import valid_identifier
 from pypy.translator.c.primitive import PrimitiveName, PrimitiveType
@@ -95,7 +95,7 @@
         return node
 
     def gettype(self, T, varlength=1, who_asks=None, argnames=[]):
-        if isinstance(T, Primitive):
+        if isinstance(T, Primitive) or T == GCREF:
             return PrimitiveType[T]
         elif isinstance(T, Ptr):
             try:
@@ -174,7 +174,7 @@
             if isinstance(obj, CConstant):
                 return obj.c_name  # without further checks
             T = typeOf(obj)
-            if isinstance(T, Primitive):
+            if isinstance(T, Primitive) or T == GCREF:
                 return PrimitiveName[T](obj, self)
             elif isinstance(T, Ptr):
                 if obj:   # test if the ptr is non-NULL

Modified: pypy/branch/pyjitpl5/pypy/translator/c/primitive.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/translator/c/primitive.py	(original)
+++ pypy/branch/pyjitpl5/pypy/translator/c/primitive.py	Mon Mar  2 14:42:10 2009
@@ -7,7 +7,7 @@
 from pypy.rpython.lltypesystem.llmemory import Address, \
      AddressOffset, ItemOffset, ArrayItemsOffset, FieldOffset, \
      CompositeOffset, ArrayLengthOffset, \
-     GCHeaderOffset
+     GCHeaderOffset, GCREF
 from pypy.rpython.lltypesystem.llarena import RoundedUpForAllocation
 from pypy.translator.c.support import cdecl, barebonearray
 
@@ -128,6 +128,14 @@
     else:
         return 'NULL'
 
+def name_gcref(value, db):
+    if value:
+        realobj = value._obj.container
+        realvalue = cast_opaque_ptr(Ptr(typeOf(realobj)), value)
+        return db.get(realvalue)
+    else:
+        return 'NULL'
+
 # On 64 bit machines, SignedLongLong and Signed are the same, so the
 # order matters, because we want the Signed implementation.
 PrimitiveName = {
@@ -142,6 +150,7 @@
     Bool:     name_bool,
     Void:     name_void,
     Address:  name_address,
+    GCREF:    name_gcref,
     }
 
 PrimitiveType = {
@@ -156,6 +165,7 @@
     Bool:     'bool_t @',
     Void:     'void @',
     Address:  'void* @',
+    GCREF:    'void* @',
     }
 
 def define_c_primitive(ll_type, c_name):

Modified: pypy/branch/pyjitpl5/pypy/translator/c/test/test_lladdresses.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/translator/c/test/test_lladdresses.py	(original)
+++ pypy/branch/pyjitpl5/pypy/translator/c/test/test_lladdresses.py	Mon Mar  2 14:42:10 2009
@@ -151,3 +151,25 @@
         return result
     fn = compile(f, [int])
     assert fn(1) == 2
+
+def test_gcref():
+    S = lltype.GcStruct("S", ("x", lltype.Signed))
+    s = lltype.malloc(S)
+    s.x = 123
+    g1 = lltype.cast_opaque_ptr(GCREF, s)
+    g2 = lltype.cast_opaque_ptr(GCREF, lltype.nullptr(S))
+    def f2(x):
+        if x > 0:
+            return g1
+        else:
+            return g2
+    def f(x):
+        gref = f2(x)
+        g = lltype.cast_opaque_ptr(lltype.Ptr(S), gref)
+        if g:
+            return g.x
+        else:
+            return -42
+    fn = compile(f, [int], gcpolicy='boehm')
+    assert fn(3) == 123
+    assert fn(-3) == -42



More information about the Pypy-commit mailing list