[pypy-svn] r23056 - in pypy/dist/pypy/rpython/memory: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Feb 6 03:12:56 CET 2006


Author: cfbolz
Date: Mon Feb  6 03:12:54 2006
New Revision: 23056

Modified:
   pypy/dist/pypy/rpython/memory/gctransform.py
   pypy/dist/pypy/rpython/memory/test/test_gctransform.py
Log:
feeble attempt to implement push_alive of a RefcountingGCTransformer (incref)
directly as a number of operations. looks ok but is a bit tedious.


Modified: pypy/dist/pypy/rpython/memory/gctransform.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gctransform.py	(original)
+++ pypy/dist/pypy/rpython/memory/gctransform.py	Mon Feb  6 03:12:54 2006
@@ -1,8 +1,9 @@
-from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.objspace.flow.model import SpaceOperation, Variable, Constant, \
      c_last_exception, FunctionGraph, Block, Link, checkgraph
 from pypy.translator.unsimplify import insert_empty_block
 from pypy.rpython import rmodel
+from pypy.rpython.memory import gc
 import sets
 
 """
@@ -203,10 +204,10 @@
 
     # ----------------------------------------------------------------
 
-    def _deallocator_body_for_type(self, v, TYPE, depth=1):
+    def _static_deallocator_body_for_type(self, v, TYPE, depth=1):
         if isinstance(TYPE, lltype.Array):
             
-            inner = list(self._deallocator_body_for_type('v_%i'%depth, TYPE.OF, depth+1))
+            inner = list(self._static_deallocator_body_for_type('v_%i'%depth, TYPE.OF, depth+1))
             if inner:
                 yield '    '*depth + 'i_%d = 0'%(depth,)
                 yield '    '*depth + 'l_%d = len(%s)'%(depth, v)
@@ -217,7 +218,7 @@
                 yield '    '*depth + '    i_%d += 1'%(depth,)
         elif isinstance(TYPE, lltype.Struct):
             for name in TYPE._names:
-                inner = list(self._deallocator_body_for_type(
+                inner = list(self._static_deallocator_body_for_type(
                     v + '_' + name, TYPE._flds[name], depth))
                 if inner:
                     yield '    '*depth + v + '_' + name + ' = ' + v + '.' + name
@@ -226,7 +227,7 @@
         elif isinstance(TYPE, lltype.Ptr):
             yield '    '*depth + 'pop_alive(%s)'%v
 
-    def deallocation_graph_for_type(self, translator, TYPE, var):
+    def static_deallocation_graph_for_type(self, translator, TYPE, var):
         def compute_pop_alive_ll_ops(hop):
             hop.llops.extend(self.pop_alive(hop.args_v[1]))
             return hop.inputconst(hop.r_result.lowleveltype, hop.s_result.const)
@@ -256,7 +257,7 @@
 
         assert destrptr is None
 
-        body = '\n'.join(self._deallocator_body_for_type('v', TYPE))
+        body = '\n'.join(self._static_deallocator_body_for_type('v', TYPE))
         
         src = 'def deallocator(v):\n' + body + '\n    destroy(v)\n'
         d = {'pop_alive':pop_alive,
@@ -275,3 +276,30 @@
             return None
         else:
             return g
+
+class RefcountingGCTransformer(GCTransformer):
+    gc_header_offset = gc.GCHeaderOffset(lltype.Struct("header", ("refcount", lltype.Signed)))
+    def push_alive_nopyobj(self, var):
+        adr1 = varoftype(llmemory.Address)
+        result = [SpaceOperation("cast_ptr_to_adr", [var], adr1)]
+        adr2 = varoftype(llmemory.Address)
+        offset = rmodel.inputconst(lltype.Signed, self.gc_header_offset)
+        result.append(SpaceOperation("adr_sub", [adr1, offset], adr2))
+        zero = rmodel.inputconst(lltype.Signed, 0)
+        intconst = rmodel.inputconst(lltype.Void, int)
+        refcount = varoftype(lltype.Signed)
+        result.append(SpaceOperation("raw_load", [adr2, intconst, zero], refcount))
+        newrefcount = Variable()
+        newrefcount.concretetype = lltype.Signed
+        result.append(SpaceOperation("int_add",
+                                     [refcount, rmodel.inputconst(lltype.Signed, 1)],
+                                     newrefcount))
+        result.append(SpaceOperation("raw_store",
+                                     [adr2, intconst, zero, newrefcount],
+                                     varoftype(lltype.Void)))
+        return result
+
+def varoftype(concretetype):
+    var = Variable()
+    var.concretetype = concretetype
+    return var

Modified: pypy/dist/pypy/rpython/memory/test/test_gctransform.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/test/test_gctransform.py	(original)
+++ pypy/dist/pypy/rpython/memory/test/test_gctransform.py	Mon Feb  6 03:12:54 2006
@@ -4,6 +4,7 @@
 from pypy.translator.translator import TranslationContext, graphof
 from pypy.rpython.lltypesystem import lltype
 from pypy.objspace.flow.model import Variable
+from pypy import conftest
 import py
 
 def checkblock(block):
@@ -46,18 +47,20 @@
             ops.setdefault(op.opname, []).append(op)
     return ops
 
-def rtype_and_transform(func, inputtypes, transformcls, specialize=True):
+def rtype_and_transform(func, inputtypes, transformcls, specialize=True, check=True):
     t = TranslationContext()
     t.buildannotator().build_types(func, inputtypes)
     if specialize:
         t.buildrtyper().specialize(t)
     transformer = transformcls()
     transformer.transform(t.graphs)
-#    t.view()
+    if conftest.option.view:
+        t.view()
     t.checkgraphs()
-    for graph in t.graphs:
-        for block in graph.iterblocks():
-            checkblock(block)
+    if check:
+        for graph in t.graphs:
+            for block in graph.iterblocks():
+                checkblock(block)
     return t
 
 def test_simple():
@@ -249,6 +252,16 @@
         return 1
     t = rtype_and_transform(f, [], gctransform.GCTransformer)
 
+def test_refcounting_incref_simple():
+    class C:
+        pass
+    def f():
+        c = C()
+        c.x = 1
+        return c.x
+    t = rtype_and_transform(f, [], gctransform.RefcountingGCTransformer, check=False)
+   
+
 # ______________________________________________________________________
 # test write barrier placement
 
@@ -302,7 +315,7 @@
     transformer = gctransform.GCTransformer()
     v = Variable()
     v.concretetype = TYPE
-    graph = transformer.deallocation_graph_for_type(t, TYPE, v)
+    graph = transformer.static_deallocation_graph_for_type(t, TYPE, v)
     if view:
         t.view()
     return graph



More information about the Pypy-commit mailing list