[pypy-svn] r23563 - in pypy/dist/pypy: rpython rpython/lltypesystem rpython/lltypesystem/test rpython/memory rpython/test translator translator/c/test

arigo at codespeak.net arigo at codespeak.net
Tue Feb 21 17:06:10 CET 2006


Author: arigo
Date: Tue Feb 21 17:06:09 2006
New Revision: 23563

Modified:
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/lltypesystem/lloperation.py
   pypy/dist/pypy/rpython/lltypesystem/test/test_lloperation.py
   pypy/dist/pypy/rpython/memory/gctransform.py
   pypy/dist/pypy/rpython/objectmodel.py
   pypy/dist/pypy/rpython/test/test_objectmodel.py
   pypy/dist/pypy/translator/c/test/test_boehm.py
   pypy/dist/pypy/translator/c/test/test_stackless.py
   pypy/dist/pypy/translator/simplify.py
Log:
Merged objectmodel.llop with lltypesystem.lloperation.  Now you
need to import llop from pypy.rpython.lltypesystem.lloperation...


Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Tue Feb 21 17:06:09 2006
@@ -540,6 +540,22 @@
         assert type(c) is float
         return math.fmod(b,c)
 
+    def op_gc__collect(self):
+        import gc
+        gc.collect()
+
+    def op_gc_free(self, addr):
+        raise NotImplementedError("gc_free")
+
+    def op_gc_fetch_exception(self):
+        raise NotImplementedError("gc_fetch_exception")
+
+    def op_gc_restore_exception(self, exc):
+        raise NotImplementedError("gc_restore_exception")
+
+    def op_gc_call_rtti_destructor(self, rtti, addr):
+        raise NotImplementedError("gc_call_rtti_destructor")
+
     # operations on pyobjects!
     for opname in opimpls.keys():
         exec py.code.Source("""

Modified: pypy/dist/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/lloperation.py	Tue Feb 21 17:06:09 2006
@@ -5,6 +5,8 @@
 class LLOp(object):
 
     def __init__(self, sideeffects=True, canfold=False, canraise=(), pyobj=False):
+        # self.opname = ... (set afterwards)
+
         if canfold:
             sideeffects = False
 
@@ -22,6 +24,39 @@
         # The operation manipulates PyObjects
         self.pyobj = pyobj
 
+    # __________ make the LLOp instances callable from LL helpers __________
+
+    __name__ = property(lambda self: 'llop_'+self.opname)
+
+    def __call__(self, RESULTTYPE, *args):
+        raise TypeError, "llop is meant to be rtyped and not called direclty"
+
+    def compute_result_annotation(self, RESULTTYPE, *args):
+        from pypy.annotation.model import lltype_to_annotation
+        assert RESULTTYPE.is_constant()
+        return lltype_to_annotation(RESULTTYPE.const)
+
+    def specialize(self, hop):
+        args_v = [hop.inputarg(r, i+1) for i, r in enumerate(hop.args_r[1:])]
+        hop.exception_is_here()
+        return hop.genop(self.opname, args_v, resulttype=hop.r_result.lowleveltype)
+
+
+def enum_ops_without_sideeffects(raising_is_ok=False):
+    """Enumerate operations that have no side-effects
+    (see also enum_foldable_ops)."""
+    for opname, opdesc in LL_OPERATIONS.iteritems():
+        if not opdesc.sideeffects:
+            if not opdesc.canraise or raising_is_ok:
+                yield opname
+
+def enum_foldable_ops(raising_is_ok=False):
+    """Enumerate operations that can be constant-folded."""
+    for opname, opdesc in LL_OPERATIONS.iteritems():
+        if opdesc.canfold:
+            if not opdesc.canraise or raising_is_ok:
+                yield opname
+
 # ____________________________________________________________
 #
 # This list corresponds to the operations implemented by the LLInterpreter.
@@ -233,6 +268,14 @@
     'cast_ptr_to_adr':      LLOp(canfold=True),
     'cast_adr_to_ptr':      LLOp(canfold=True),
 
+    # __________ GC operations __________
+
+    'gc__collect':          LLOp(),
+    'gc_free':              LLOp(),
+    'gc_fetch_exception':   LLOp(),
+    'gc_restore_exception': LLOp(),
+    'gc_call_rtti_destructor': LLOp(),
+
     # __________ misc operations __________
 
     'keepalive':            LLOp(),
@@ -249,3 +292,22 @@
 for opname in opimpls:
     LL_OPERATIONS[opname] = LLOp(canraise=(Exception,), pyobj=True)
 del opname, opimpls, FunctionByName
+
+# ____________________________________________________________
+# Post-processing
+
+# Stick the opnames into the LLOp instances
+for opname, opdesc in LL_OPERATIONS.iteritems():
+    opdesc.opname = opname
+del opname, opdesc
+
+# Also export all operations in an attribute-based namespace.
+# Example usage from LL helpers:  z = llop.int_add(Signed, x, y)
+
+class LLOP(object):
+    def _freeze_(self):
+        return True
+llop = LLOP()
+for opname, opdesc in LL_OPERATIONS.iteritems():
+    setattr(llop, opname, opdesc)
+del opname, opdesc

Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_lloperation.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_lloperation.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_lloperation.py	Tue Feb 21 17:06:09 2006
@@ -1,5 +1,6 @@
-from pypy.rpython.lltypesystem.lloperation import LL_OPERATIONS
+from pypy.rpython.lltypesystem.lloperation import LL_OPERATIONS, llop
 from pypy.rpython.llinterp import LLFrame
+from pypy.rpython.test.test_llinterp import interpret
 
 # This tests that the LLInterpreter and the LL_OPERATIONS tables are in sync.
 
@@ -20,3 +21,11 @@
 def test_llinterp_complete():
     for opname in LL_OPERATIONS:
         assert opname in LL_INTERP_OPERATIONS
+
+def test_llop():
+    from pypy.rpython.annlowlevel import LowLevelAnnotatorPolicy
+    from pypy.rpython.lltypesystem import lltype
+    def llf(x, y):
+        return llop.int_add(lltype.Signed, x, y)
+    res = interpret(llf, [5, 7], policy=LowLevelAnnotatorPolicy())
+    assert res == 12

Modified: pypy/dist/pypy/rpython/memory/gctransform.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/gctransform.py	(original)
+++ pypy/dist/pypy/rpython/memory/gctransform.py	Tue Feb 21 17:06:09 2006
@@ -1,5 +1,6 @@
 import py
 from pypy.rpython.lltypesystem import lltype, llmemory
+from pypy.rpython.lltypesystem.lloperation import llop
 from pypy.objspace.flow.model import SpaceOperation, Variable, Constant, \
      c_last_exception, FunctionGraph, Block, Link, checkgraph
 from pypy.translator.unsimplify import insert_empty_block
@@ -286,7 +287,7 @@
                 if refcount == 0:
                     dealloc(adr)
         def no_pointer_dealloc(adr):
-            objectmodel.llop.gc_free(lltype.Void, adr)
+            llop.gc_free(lltype.Void, adr)
         if self.translator is not None and self.translator.rtyper is not None:
             self.increfptr = self.inittime_helper(
                 incref, [annmodel.SomeAddress()])
@@ -396,7 +397,7 @@
             body = '\n'.join(_static_deallocator_body_for_type('v', TYPE, 3))
             src = """
 def deallocator(addr):
-    exc_instance = objectmodel.llop.gc_fetch_exception(EXC_INSTANCE_TYPE)
+    exc_instance = llop.gc_fetch_exception(EXC_INSTANCE_TYPE)
     try:
         v = cast_adr_to_ptr(addr, PTR_TYPE)
         gcheader = addr - gc_header_offset
@@ -414,19 +415,19 @@
         gcheader.signed[0] = refcount
         if refcount == 0:
 %s
-            objectmodel.llop.gc_free(lltype.Void, addr)
+            llop.gc_free(lltype.Void, addr)
     except:
         pass
-    objectmodel.llop.gc_restore_exception(lltype.Void, exc_instance)
+    llop.gc_restore_exception(lltype.Void, exc_instance)
         
 """ % (body, )
         else:
             call_del = None
             body = '\n'.join(_static_deallocator_body_for_type('v', TYPE))
             src = ('def deallocator(addr):\n    v = cast_adr_to_ptr(addr, PTR_TYPE)\n' +
-                   body + '\n    objectmodel.llop.gc_free(lltype.Void, addr)\n')
+                   body + '\n    llop.gc_free(lltype.Void, addr)\n')
         d = {'pop_alive': pop_alive,
-             'objectmodel': objectmodel,
+             'llop': llop,
              'lltype': lltype,
              'destrptr': destrptr,
              'gc_header_offset': RefcountingGCTransformer.gc_header_offset,
@@ -475,7 +476,7 @@
             v = objectmodel.cast_adr_to_ptr(addr, QUERY_ARG_TYPE)
             rtti = queryptr(v)
             gcheader.signed[0] = 0
-            objectmodel.llop.gc_call_rtti_destructor(lltype.Void, rtti, addr)
+            llop.gc_call_rtti_destructor(lltype.Void, rtti, addr)
         g = self.annotate_helper(dealloc, [llmemory.Address])
         self.specialize_more_blocks()
         nsafecalls = exception_clean(g)
@@ -583,8 +584,7 @@
         elif destrptr:
             EXC_INSTANCE_TYPE = self.translator.rtyper.exceptiondata.lltype_of_exception_value
             def finalizer(addr):
-                exc_instance = objectmodel.llop.gc_fetch_exception(
-                    EXC_INSTANCE_TYPE)
+                exc_instance = llop.gc_fetch_exception(EXC_INSTANCE_TYPE)
                 try:
                     v = objectmodel.cast_adr_to_ptr(addr, DESTR_ARG)
                     destrptr(v)
@@ -593,7 +593,7 @@
                         os.write(2, "a destructor raised an exception, ignoring it\n")
                     except:
                         pass
-                objectmodel.llop.gc_restore_exception(lltype.Void, exc_instance)
+                llop.gc_restore_exception(lltype.Void, exc_instance)
             g = self.annotate_helper(finalizer, [llmemory.Address])
         else:
             g = None

Modified: pypy/dist/pypy/rpython/objectmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/objectmodel.py	(original)
+++ pypy/dist/pypy/rpython/objectmodel.py	Tue Feb 21 17:06:09 2006
@@ -62,49 +62,6 @@
 def hlinvoke(repr, llcallable, *args):
     raise TypeError, "hlinvoke is meant to be rtyped and not called direclty"
 
-# generically insert ll ops
-
-# xxx Another approach would combine a llop function with a factory of names
-
-class LLOp(object):
-
-    def __init__(self, opname):
-        self.opname = opname
-
-    __name__ = property(lambda self: 'llop_'+self.opname)
-
-    def __call__(self, RESULTTYPE, *args):
-        raise TypeError, "llop is meant to be rtyped and not called direclty"
-
-    def compute_result_annotation(self, RESULTTYPE, *args):
-        from pypy.annotation.model import lltype_to_annotation
-        assert RESULTTYPE.is_constant()
-        return lltype_to_annotation(RESULTTYPE.const)
-
-    def specialize(self, hop):
-        args_v = [hop.inputarg(r, i+1) for i, r in enumerate(hop.args_r[1:])]
-        hop.exception_is_here()
-        return hop.genop(self.opname, args_v, resulttype=hop.r_result.lowleveltype)
-
-class LLOpFactory(object):
-    def __init__(self):
-        self._cache = {}
-        
-    def _freeze_(self):
-        return True
-
-    def __getattr__(self, opname):
-        if opname == 'compute_result_annotation':
-            raise AttributeError
-        try:
-            return self._cache[opname]
-        except KeyError:
-            llop = self._cache[opname] = LLOp(opname)
-            return llop
-        
-llop = LLOpFactory()
-
-
 # ____________________________________________________________
 
 

Modified: pypy/dist/pypy/rpython/test/test_objectmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_objectmodel.py	(original)
+++ pypy/dist/pypy/rpython/test/test_objectmodel.py	Tue Feb 21 17:06:09 2006
@@ -196,11 +196,3 @@
         return s1 == s2
     res = interpret(f, [])
     assert res
-
-def test_llop():
-    from pypy.rpython.annlowlevel import LowLevelAnnotatorPolicy
-    from pypy.rpython import objectmodel
-    from pypy.rpython.lltypesystem import lltype
-    def llf(x, y):
-        return objectmodel.llop.int_add(lltype.Signed, x, y)
-    res = interpret(llf, [5, 7], policy=LowLevelAnnotatorPolicy())

Modified: pypy/dist/pypy/translator/c/test/test_boehm.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_boehm.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_boehm.py	Tue Feb 21 17:06:09 2006
@@ -56,7 +56,7 @@
         fn()
 
     def test__del__(self):
-        from pypy.rpython import objectmodel
+        from pypy.rpython.lltypesystem.lloperation import llop
         from pypy.rpython.lltypesystem import lltype
         class State:
             pass
@@ -78,7 +78,7 @@
             A()
             B()
             C()
-            objectmodel.llop.gc__collect(lltype.Void)
+            llop.gc__collect(lltype.Void)
             return s.a_dels * 10 + s.b_dels
         fn = self.getcompiled(f)
         # we can't demand that boehm has collected all of the objects,
@@ -92,7 +92,7 @@
         assert 0 < res <= 84
 
     def test_del_raises(self):
-        from pypy.rpython import objectmodel
+        from pypy.rpython.lltypesystem.lloperation import llop
         from pypy.rpython.lltypesystem import lltype
         import os
         class A(object):
@@ -109,7 +109,7 @@
             s.dels = 0
             for i in range(10):
                 g()
-            objectmodel.llop.gc__collect(lltype.Void)
+            llop.gc__collect(lltype.Void)
             return s.dels
         fn = self.getcompiled(f)
         # we can't demand that boehm has collected all of the objects,

Modified: pypy/dist/pypy/translator/c/test/test_stackless.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_stackless.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_stackless.py	Tue Feb 21 17:06:09 2006
@@ -254,9 +254,9 @@
         while j < 20:
             j += 1
             a.append(j)
-    from pypy.rpython import objectmodel
+    from pypy.rpython.lltypesystem.lloperation import llop
     from pypy.rpython.lltypesystem import lltype
-    objectmodel.llop.gc__collect(lltype.Void)
+    llop.gc__collect(lltype.Void)
 
 # ____________________________________________________________
     

Modified: pypy/dist/pypy/translator/simplify.py
==============================================================================
--- pypy/dist/pypy/translator/simplify.py	(original)
+++ pypy/dist/pypy/translator/simplify.py	Tue Feb 21 17:06:09 2006
@@ -417,7 +417,10 @@
         pos neg nonzero abs hex oct ord invert add sub mul
         truediv floordiv div mod divmod pow lshift rshift and_ or_
         xor int float long lt le eq ne gt ge cmp coerce contains
-        iter get same_as cast_pointer getfield getarrayitem getsubstruct'''.split():
+        iter get'''.split():
+    CanRemove[_op] = True
+from pypy.rpython.lltypesystem.lloperation import enum_ops_without_sideeffects
+for _op in enum_ops_without_sideeffects():
     CanRemove[_op] = True
 del _op
 CanRemoveBuiltins = {



More information about the Pypy-commit mailing list