[pypy-svn] r25382 - in pypy/dist/pypy: objspace/flow rpython rpython/lltypesystem rpython/test

arigo at codespeak.net arigo at codespeak.net
Wed Apr 5 15:14:18 CEST 2006


Author: arigo
Date: Wed Apr  5 15:14:17 2006
New Revision: 25382

Modified:
   pypy/dist/pypy/objspace/flow/operation.py
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/lltypesystem/lloperation.py
   pypy/dist/pypy/rpython/rint.py
   pypy/dist/pypy/rpython/test/snippet.py
Log:
Cleaned up the list of overflow-raising low-level operations.
Made the table more regular.
Removed int_pow (not implemented in the C back-end anyway).


Modified: pypy/dist/pypy/objspace/flow/operation.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/operation.py	(original)
+++ pypy/dist/pypy/objspace/flow/operation.py	Wed Apr  5 15:14:17 2006
@@ -125,8 +125,8 @@
 def mod_ovf(x, y):
     return ovfcheck(x % y)
 
-def pow_ovf(*two_or_three_args):
-    return ovfcheck(pow(*two_or_three_args))
+##def pow_ovf(*two_or_three_args):
+##    return ovfcheck(pow(*two_or_three_args))
 
 def lshift_ovf(x, y):
     return ovfcheck_lshift(x, y)
@@ -194,7 +194,6 @@
     ('floordiv_ovf',    floordiv_ovf),
     ('div_ovf',         div_ovf),
     ('mod_ovf',         mod_ovf),
-    ('pow_ovf',         pow_ovf),
     ('lshift_ovf',      lshift_ovf),
     ]
 

Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Wed Apr  5 15:14:17 2006
@@ -370,6 +370,15 @@
     def op_hint(self, x, hints):
         return x
 
+    def op_decode_arg(self, fname, i, name, vargs, vkwds):
+        raise NotImplementedError("decode_arg")
+
+    def op_decode_arg_def(fname, i, name, vargs, vkwds, default):
+        raise NotImplementedError("decode_arg_def")
+
+    def op_check_no_more_arg(fname, n, vargs):
+        raise NotImplementedError("check_no_more_arg")
+
     def op_setfield(self, obj, fieldname, fieldvalue):
         # obj should be pointer
         FIELDTYPE = getattr(lltype.typeOf(obj).TO, fieldname)
@@ -583,20 +592,6 @@
         assert -sys.maxint-1 <= b <= sys.maxint
         return int(b)
 
-    def op_int_floordiv_ovf_zer(self, a, b):
-        assert type(a) is int
-        assert type(b) is int
-        if b == 0:
-            self.make_llexception(ZeroDivisionError())
-        return self.op_int_floordiv_ovf(a, b)
-            
-    def op_int_mod_ovf_zer(self, a, b):
-        assert type(a) is int
-        assert type(b) is int
-        if b == 0:
-            self.make_llexception(ZeroDivisionError())
-        return self.op_int_mod_ovf(a, b)
-            
     def op_float_floor(self, b):
         assert type(b) is float
         return math.floor(b)
@@ -606,6 +601,11 @@
         assert type(c) is float
         return math.fmod(b,c)
 
+    def op_float_pow(self, b,c):
+        assert type(b) is float
+        assert type(c) is float
+        return math.pow(b,c)
+
     def op_gc__collect(self):
         import gc
         gc.collect()
@@ -622,6 +622,24 @@
     def op_gc_call_rtti_destructor(self, rtti, addr):
         raise NotImplementedError("gc_call_rtti_destructor")
 
+    def op_gc_push_alive_pyobj(self, pyobj):
+        raise NotImplementedError("gc_push_alive_pyobj")
+
+    def op_gc_pop_alive_pyobj(self, pyobj):
+        raise NotImplementedError("gc_pop_alive_pyobj")
+
+    def op_gc_protect(self, obj):
+        raise NotImplementedError("gc_protect")
+
+    def op_gc_unprotect(self, obj):
+        raise NotImplementedError("gc_unprotect")
+
+    def op_gc_reload_possibly_moved(self, newaddr, ptr):
+        raise NotImplementedError("gc_reload_possibly_moved")
+
+    def op_yield_current_frame_to_caller(self):
+        raise NotImplementedError("yield_current_frame_to_caller")
+
     # operations on pyobjects!
     for opname in opimpls.keys():
         exec py.code.Source("""
@@ -697,86 +715,112 @@
     # __________________________________________________________
     # primitive operations
 
-    for typ in (float, int, r_uint, r_longlong, r_ulonglong):
-        typname = typ.__name__
-        optup = ('add', 'sub', 'mul', 'truediv', 'floordiv',
-                 'mod', 'gt', 'lt', 'ge', 'ne', 'le', 'eq',)
-        if typ is r_uint:
-            opnameprefix = 'uint'
-        elif typ is r_longlong:
-            opnameprefix = 'llong'
-        elif typ is r_ulonglong:
-            opnameprefix = 'ullong'
-        else:
-            opnameprefix = typname
-        if typ in (int, r_uint):
-            optup += 'and_', 'or_', 'lshift', 'rshift', 'xor'
-        for opname in optup:
-            assert opname in opimpls
-            if typ is float and opname == 'floordiv':
-                continue    # 'floordiv' is for integer types
-            if typ is not float and opname == 'truediv':
-                continue    # 'truediv' is for floats only
-            if typ is int and opname not in ops_returning_a_bool:
-                adjust_result = 'intmask'
+    def setup_primitive_operations():
+        for typ in (float, int, r_uint, r_longlong, r_ulonglong):
+            typname = typ.__name__
+            optup = ('add', 'sub', 'mul', 'truediv', 'floordiv',
+                     'mod', 'gt', 'lt', 'ge', 'ne', 'le', 'eq',)
+            overflowing_operations = ('add', 'sub', 'mul', 'floordiv',
+                                      'mod', 'lshift')
+            if typ is r_uint:
+                opnameprefix = 'uint'
+            elif typ is r_longlong:
+                opnameprefix = 'llong'
+            elif typ is r_ulonglong:
+                opnameprefix = 'ullong'
             else:
-                adjust_result = ''
-            pureopname = opname.rstrip('_')
-            exec py.code.Source("""
-                def op_%(opnameprefix)s_%(pureopname)s(self, x, y):
-                    assert isinstance(x, %(typname)s)
-                    assert isinstance(y, %(typname)s)
-                    func = opimpls[%(opname)r]
-                    return %(adjust_result)s(func(x, y))
-            """ % locals()).compile()
-            if typ is int:
-                opname += '_ovf'
-                exec py.code.Source("""
-                    def op_%(opnameprefix)s_%(pureopname)s_ovf(self, x, y):
+                opnameprefix = typname
+            if typ is not float:
+                optup += 'and_', 'or_', 'lshift', 'rshift', 'xor'
+            for opname in optup:
+                assert opname in opimpls
+                if typ is float and opname == 'floordiv':
+                    continue    # 'floordiv' is for integer types
+                if typ is not float and opname == 'truediv':
+                    continue    # 'truediv' is for floats only
+                if typ is int and opname not in ops_returning_a_bool:
+                    adjust_result = 'intmask'
+                else:
+                    adjust_result = ''
+                pureopname = opname.rstrip('_')
+                yield """
+                    def op_%(opnameprefix)s_%(pureopname)s(self, x, y):
                         assert isinstance(x, %(typname)s)
                         assert isinstance(y, %(typname)s)
                         func = opimpls[%(opname)r]
-                        try:
-                            return %(adjust_result)s(func(x, y))
-                        except OverflowError:
-                            self.make_llexception()
-                """ % locals()).compile()
-        for opname in 'is_true', 'neg', 'abs', 'invert':
-            assert opname in opimpls
-            if typ is float and opname == 'invert':
-                continue
-            if typ is int and opname not in ops_returning_a_bool:
-                adjust_result = 'intmask'
-            else:
-                adjust_result = ''
-            exec py.code.Source("""
-                def op_%(opnameprefix)s_%(opname)s(self, x):
-                    assert isinstance(x, %(typname)s)
-                    func = opimpls[%(opname)r]
-                    return %(adjust_result)s(func(x))
-            """ % locals()).compile()
-            if typ is int and opname in ('neg', 'abs'):
-                opname += '_ovf'
-                exec py.code.Source("""
+                        return %(adjust_result)s(func(x, y))
+                """ % locals()
+
+                suffixes = []
+                if typ is not float:
+                    if opname in ('lshift', 'rshift'):
+                        suffixes.append(('_val', 'ValueError'))
+                    if opname in ('floordiv', 'mod'):
+                        suffixes.append(('_zer', 'ZeroDivisionError'))
+                    if typ is int and opname in overflowing_operations:
+                        for suffix1, exccls1 in suffixes[:]:
+                            suffixes.append(('_ovf'+suffix1,
+                                             '(OverflowError, %s)' % exccls1))
+                        suffixes.append(('_ovf', 'OverflowError'))
+
+                for suffix, exceptionclasses in suffixes:
+                    if '_ovf' in suffix:
+                        opname_ex = opname + '_ovf'
+                    else:
+                        opname_ex = opname
+                    yield """
+                        def op_%(opnameprefix)s_%(pureopname)s%(suffix)s(self, x, y):
+                            assert isinstance(x, %(typname)s)
+                            assert isinstance(y, %(typname)s)
+                            func = opimpls[%(opname_ex)r]
+                            try:
+                                return %(adjust_result)s(func(x, y))
+                            except %(exceptionclasses)s:
+                                self.make_llexception()
+                    """ % locals()
+            for opname in 'is_true', 'neg', 'abs', 'invert':
+                assert opname in opimpls
+                if typ is float and opname == 'invert':
+                    continue
+                if typ is int and opname not in ops_returning_a_bool:
+                    adjust_result = 'intmask'
+                else:
+                    adjust_result = ''
+                yield """
                     def op_%(opnameprefix)s_%(opname)s(self, x):
                         assert isinstance(x, %(typname)s)
                         func = opimpls[%(opname)r]
-                        try:
-                            return %(adjust_result)s(func(x))
-                        except OverflowError:
-                            self.make_llexception()
-                """ % locals()).compile()
-            
-    for opname in ('gt', 'lt', 'ge', 'ne', 'le', 'eq'):
-        assert opname in opimpls
-        exec py.code.Source("""
-            def op_char_%(opname)s(self, x, y):
-                assert isinstance(x, str) and len(x) == 1
-                assert isinstance(y, str) and len(y) == 1
-                func = opimpls[%(opname)r]
-                return func(x, y)
-        """ % locals()).compile()
-    
+                        return %(adjust_result)s(func(x))
+                """ % locals()
+                if typ is int and opname in ('neg', 'abs'):
+                    opname += '_ovf'
+                    yield """
+                        def op_%(opnameprefix)s_%(opname)s(self, x):
+                            assert isinstance(x, %(typname)s)
+                            func = opimpls[%(opname)r]
+                            try:
+                                return %(adjust_result)s(func(x))
+                            except OverflowError:
+                                self.make_llexception()
+                    """ % locals()
+
+        for opname in ('gt', 'lt', 'ge', 'ne', 'le', 'eq'):
+            assert opname in opimpls
+            yield """
+                def op_char_%(opname)s(self, x, y):
+                    assert isinstance(x, str) and len(x) == 1
+                    assert isinstance(y, str) and len(y) == 1
+                    func = opimpls[%(opname)r]
+                    return func(x, y)
+            """ % locals()
+
+    for _src in setup_primitive_operations():
+        exec py.code.Source(_src).compile()
+        del _src
+    del setup_primitive_operations
+
+    # ____________________________________________________________
+
     original_int_add = op_int_add
 
     def op_int_add(self, x, y):
@@ -796,14 +840,12 @@
     def op_unichar_eq(self, x, y):
         assert isinstance(x, unicode) and len(x) == 1
         assert isinstance(y, unicode) and len(y) == 1
-        func = opimpls['eq']
-        return func(x, y)
+        return x == y
 
     def op_unichar_ne(self, x, y):
         assert isinstance(x, unicode) and len(x) == 1
         assert isinstance(y, unicode) and len(y) == 1
-        func = opimpls['ne']
-        return func(x, y)
+        return x != y
 
     #Operation of ootype
 

Modified: pypy/dist/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/lloperation.py	Wed Apr  5 15:14:17 2006
@@ -20,6 +20,7 @@
 
         # Exceptions that can be raised
         self.canraise = canraise
+        assert isinstance(canraise, tuple)
 
         # The operation manipulates PyObjects
         self.pyobj = pyobj
@@ -62,7 +63,6 @@
 # This list corresponds to the operations implemented by the LLInterpreter.
 # XXX Some clean-ups are needed:
 #      * many exception-raising operations are being replaced by calls to helpers
-#      * there are still many _ovf operations that cannot really raise OverflowError
 #      * float_mod vs float_fmod ?
 # Run test_lloperation after changes.  Feel free to clean up LLInterpreter too :-)
 
@@ -96,7 +96,9 @@
     'int_sub':              LLOp(canfold=True),
     'int_mul':              LLOp(canfold=True),
     'int_floordiv':         LLOp(canfold=True),
+    'int_floordiv_zer':     LLOp(canfold=True, canraise=(ZeroDivisionError,)),
     'int_mod':              LLOp(canfold=True),
+    'int_mod_zer':          LLOp(canfold=True, canraise=(ZeroDivisionError,)),
     'int_lt':               LLOp(canfold=True),
     'int_le':               LLOp(canfold=True),
     'int_eq':               LLOp(canfold=True),
@@ -106,31 +108,20 @@
     'int_and':              LLOp(canfold=True),
     'int_or':               LLOp(canfold=True),
     'int_lshift':           LLOp(canfold=True),
+    'int_lshift_val':       LLOp(canfold=True, canraise=(ValueError,)),
     'int_rshift':           LLOp(canfold=True),
+    'int_rshift_val':       LLOp(canfold=True, canraise=(ValueError,)),
     'int_xor':              LLOp(canfold=True),
-    'int_floordiv_zer':     LLOp(canfold=True, canraise=(ZeroDivisionError,)),
+
     'int_add_ovf':          LLOp(canfold=True, canraise=(OverflowError,)),
     'int_sub_ovf':          LLOp(canfold=True, canraise=(OverflowError,)),
     'int_mul_ovf':          LLOp(canfold=True, canraise=(OverflowError,)),
     'int_floordiv_ovf':     LLOp(canfold=True, canraise=(OverflowError,)),
+    'int_floordiv_ovf_zer': LLOp(canfold=True, canraise=(OverflowError, ZeroDivisionError)),
     'int_mod_ovf':          LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_lt_ovf':           LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_le_ovf':           LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_eq_ovf':           LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_ne_ovf':           LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_gt_ovf':           LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_ge_ovf':           LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_and_ovf':          LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_or_ovf':           LLOp(canfold=True, canraise=(OverflowError,)),
+    'int_mod_ovf_zer':      LLOp(canfold=True, canraise=(OverflowError, ZeroDivisionError)),
     'int_lshift_ovf':       LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_lshift_val':       LLOp(canfold=True, canraise=(ValueError,)),
     'int_lshift_ovf_val':   LLOp(canfold=True, canraise=(OverflowError, ValueError,)),
-    'int_rshift_ovf':       LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_rshift_val':       LLOp(canfold=True, canraise=(ValueError,)),
-    'int_rshift_ovf_val':   LLOp(canfold=True, canraise=(OverflowError, ValueError,)), 
-    'int_xor_ovf':          LLOp(canfold=True, canraise=(OverflowError,)),
-    'int_floordiv_ovf_zer': LLOp(canfold=True, canraise=(OverflowError, ZeroDivisionError)),
-    'int_mod_ovf_zer':      LLOp(canfold=True, canraise=(OverflowError, ZeroDivisionError)),
 
     'uint_is_true':         LLOp(canfold=True),
     'uint_neg':             LLOp(canfold=True),
@@ -143,6 +134,7 @@
     'uint_floordiv':        LLOp(canfold=True),
     'uint_floordiv_zer':    LLOp(canfold=True, canraise=(ZeroDivisionError,)),
     'uint_mod':             LLOp(canfold=True),
+    'uint_mod_zer':         LLOp(canfold=True, canraise=(ZeroDivisionError,)),
     'uint_lt':              LLOp(canfold=True),
     'uint_le':              LLOp(canfold=True),
     'uint_eq':              LLOp(canfold=True),
@@ -152,7 +144,9 @@
     'uint_and':             LLOp(canfold=True),
     'uint_or':              LLOp(canfold=True),
     'uint_lshift':          LLOp(canfold=True),
+    'uint_lshift_val':      LLOp(canfold=True, canraise=(ValueError,)),
     'uint_rshift':          LLOp(canfold=True),
+    'uint_rshift_val':      LLOp(canfold=True, canraise=(ValueError,)),
     'uint_xor':             LLOp(canfold=True),
 
     'float_is_true':        LLOp(canfold=True),
@@ -183,7 +177,9 @@
     'llong_sub':            LLOp(canfold=True),
     'llong_mul':            LLOp(canfold=True),
     'llong_floordiv':       LLOp(canfold=True),
+    'llong_floordiv_zer':   LLOp(canfold=True, canraise=(ZeroDivisionError,)),
     'llong_mod':            LLOp(canfold=True),
+    'llong_mod_zer':        LLOp(canfold=True, canraise=(ZeroDivisionError,)),
     'llong_lt':             LLOp(canfold=True),
     'llong_le':             LLOp(canfold=True),
     'llong_eq':             LLOp(canfold=True),
@@ -193,7 +189,9 @@
     'llong_and':            LLOp(canfold=True),
     'llong_or':             LLOp(canfold=True),
     'llong_lshift':         LLOp(canfold=True),
+    'llong_lshift_val':     LLOp(canfold=True, canraise=(ValueError,)),
     'llong_rshift':         LLOp(canfold=True),
+    'llong_rshift_val':     LLOp(canfold=True, canraise=(ValueError,)),
     'llong_xor':            LLOp(canfold=True),
 
     'ullong_is_true':       LLOp(canfold=True),
@@ -205,18 +203,22 @@
     'ullong_sub':           LLOp(canfold=True),
     'ullong_mul':           LLOp(canfold=True),
     'ullong_floordiv':      LLOp(canfold=True),
+    'ullong_floordiv_zer':  LLOp(canfold=True, canraise=(ZeroDivisionError,)),
     'ullong_mod':           LLOp(canfold=True),
+    'ullong_mod_zer':       LLOp(canfold=True, canraise=(ZeroDivisionError,)),
     'ullong_lt':            LLOp(canfold=True),
     'ullong_le':            LLOp(canfold=True),
     'ullong_eq':            LLOp(canfold=True),
     'ullong_ne':            LLOp(canfold=True),
     'ullong_gt':            LLOp(canfold=True),
     'ullong_ge':            LLOp(canfold=True),
-    'ulong_and':            LLOp(canfold=True),
-    'ulong_or':             LLOp(canfold=True),
-    'ulong_lshift':         LLOp(canfold=True),
-    'ulong_rshift':         LLOp(canfold=True),
-    'ulong_xor':            LLOp(canfold=True),
+    'ullong_and':           LLOp(canfold=True),
+    'ullong_or':            LLOp(canfold=True),
+    'ullong_lshift':        LLOp(canfold=True),
+    'ullong_lshift_val':    LLOp(canfold=True, canraise=(ValueError,)),
+    'ullong_rshift':        LLOp(canfold=True),
+    'ullong_rshift_val':    LLOp(canfold=True, canraise=(ValueError,)),
+    'ullong_xor':           LLOp(canfold=True),
 
     'cast_bool_to_int':     LLOp(canfold=True),
     'cast_bool_to_uint':    LLOp(canfold=True),

Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py	(original)
+++ pypy/dist/pypy/rpython/rint.py	Wed Apr  5 15:14:17 2006
@@ -118,26 +118,26 @@
         return _rtype_template(hop, 'rshift', [ValueError])
     rtype_inplace_rshift = rtype_rshift
 
-    def rtype_pow(_, hop, suffix=''):
-        if hop.has_implicit_exception(ZeroDivisionError):
-            suffix += '_zer'
-        s_int3 = hop.args_s[2]
-        rresult = hop.rtyper.makerepr(hop.s_result)
-        if s_int3.is_constant() and s_int3.const is None:
-            vlist = hop.inputargs(rresult, rresult, Void)[:2]
-        else:
-            vlist = hop.inputargs(rresult, rresult, rresult)
-        hop.exception_is_here()
-        return hop.genop(rresult.opprefix + 'pow' + suffix, vlist, resulttype=rresult)
-
-    def rtype_pow_ovf(_, hop):
-        if hop.s_result.unsigned:
-            raise TyperError("forbidden uint_pow_ovf")
-        hop.has_implicit_exception(OverflowError) # record that we know about it
-        return self.rtype_pow(_, hop, suffix='_ovf')
+##    def rtype_pow(_, hop, suffix=''):
+##        if hop.has_implicit_exception(ZeroDivisionError):
+##            suffix += '_zer'
+##        s_int3 = hop.args_s[2]
+##        rresult = hop.rtyper.makerepr(hop.s_result)
+##        if s_int3.is_constant() and s_int3.const is None:
+##            vlist = hop.inputargs(rresult, rresult, Void)[:2]
+##        else:
+##            vlist = hop.inputargs(rresult, rresult, rresult)
+##        hop.exception_is_here()
+##        return hop.genop(rresult.opprefix + 'pow' + suffix, vlist, resulttype=rresult)
+
+##    def rtype_pow_ovf(_, hop):
+##        if hop.s_result.unsigned:
+##            raise TyperError("forbidden uint_pow_ovf")
+##        hop.has_implicit_exception(OverflowError) # record that we know about it
+##        return self.rtype_pow(_, hop, suffix='_ovf')
 
-    def rtype_inplace_pow(_, hop):
-        return _rtype_template(hop, 'pow', [ZeroDivisionError])
+##    def rtype_inplace_pow(_, hop):
+##        return _rtype_template(hop, 'pow', [ZeroDivisionError])
 
     #comparisons: eq is_ ne lt le gt ge
 

Modified: pypy/dist/pypy/rpython/test/snippet.py
==============================================================================
--- pypy/dist/pypy/rpython/test/snippet.py	(original)
+++ pypy/dist/pypy/rpython/test/snippet.py	Wed Apr  5 15:14:17 2006
@@ -74,8 +74,8 @@
     i += abs(i)
     i &= 255
 
-    i **= n
-    i += n**3
+    #i **= n
+    #i += n**3
 
     i += -n
     i += +n



More information about the Pypy-commit mailing list