[pypy-svn] r74225 - in pypy/branch/blackhole-improvement/pypy/jit: codewriter codewriter/test metainterp metainterp/test

arigo at codespeak.net arigo at codespeak.net
Thu Apr 29 16:36:21 CEST 2010


Author: arigo
Date: Thu Apr 29 16:36:20 2010
New Revision: 74225

Modified:
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/jitter.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_codewriter.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_jitter.py
   pypy/branch/blackhole-improvement/pypy/jit/metainterp/blackhole.py
   pypy/branch/blackhole-improvement/pypy/jit/metainterp/executor.py
   pypy/branch/blackhole-improvement/pypy/jit/metainterp/pyjitpl.py
   pypy/branch/blackhole-improvement/pypy/jit/metainterp/test/test_basic.py
Log:
More rewriting, and implement the float operations.


Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py	Thu Apr 29 16:36:20 2010
@@ -182,13 +182,14 @@
             linkfalse, linktrue = block.exits
             if linkfalse.llexitcase == True:
                 linkfalse, linktrue = linktrue, linkfalse
+            opname = 'goto_if_not'
             if isinstance(block.exitswitch, tuple):
                 # special case produced by jitter.optimize_goto_if_not()
-                opname = 'goto_if_not_' + block.exitswitch[0]
+                if block.exitswitch[0] != 'int_is_true':
+                    opname = 'goto_if_not_' + block.exitswitch[0]
                 opargs = block.exitswitch[1:]
             else:
                 assert block.exitswitch.concretetype == lltype.Bool
-                opname = 'goto_if_not'
                 opargs = [block.exitswitch]
             #
             self.emitline(opname, TLabel(linkfalse),

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/jitter.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/jitter.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/jitter.py	Thu Apr 29 16:36:20 2010
@@ -1,4 +1,4 @@
-import sys
+import py, sys
 from pypy.rpython.lltypesystem import lltype, rstr
 from pypy.jit.metainterp.history import getkind
 from pypy.objspace.flow.model import SpaceOperation, Variable, Constant
@@ -99,7 +99,8 @@
                 return False   # variable is also used in cur block
             if v is op.result:
                 if op.opname not in ('int_lt', 'int_le', 'int_eq', 'int_ne',
-                                     'int_gt', 'int_ge', 'int_is_true',
+                                     'int_gt', 'int_ge',
+                                     'int_is_zero', 'int_is_true',
                                      'ptr_eq', 'ptr_ne',
                                      'ptr_iszero', 'ptr_nonzero'):
                     return False    # not a supported operation
@@ -314,7 +315,7 @@
                 op.args[0].concretetype.TO._hints.get('typeptr'))
 
     def handle_getfield_typeptr(self, op):
-        return SpaceOperation('classof', [op.args[0]], op.result)
+        return SpaceOperation('guard_class', [op.args[0]], op.result)
 
     def rewrite_op_malloc(self, op):
         assert op.args[1].value == {'flavor': 'gc'}
@@ -387,8 +388,13 @@
         else:
             raise NoOp
 
-    def rewrite_op_bool_not(self, op):
-        return SpaceOperation('int_is_zero', op.args, op.result)
+    for _old, _new in [('bool_not', 'int_is_zero'),
+                       ('cast_bool_to_float', 'cast_int_to_float'),
+                       ]:
+        exec py.code.Source('''
+            def rewrite_op_%s(self, op):
+                return SpaceOperation(%r, op.args, op.result)
+        ''' % (_old, _new)).compile()
 
 # ____________________________________________________________
 

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_codewriter.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_codewriter.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_codewriter.py	Thu Apr 29 16:36:20 2010
@@ -34,4 +34,4 @@
     blackholeinterp.setarg_i(0, 6)
     blackholeinterp.setarg_i(1, 100)
     blackholeinterp.run(jitcode, 0)
-    assert blackholeinterp.result_i == 100+6+5+4+3
+    assert blackholeinterp.get_result_i() == 100+6+5+4+3

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py	Thu Apr 29 16:36:20 2010
@@ -332,8 +332,10 @@
         def f(i):
             return not i
 
+        # note that 'goto_if_not_int_is_true' is actually the same thing
+        # as just 'goto_if_not'.
         self.encoding_test(f, [7], """
-            goto_if_not_int_is_true L1, %i0
+            goto_if_not L1, %i0
             int_return $False
             L1:
             int_return $True

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_jitter.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_jitter.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_jitter.py	Thu Apr 29 16:36:20 2010
@@ -180,7 +180,7 @@
     v_result = varoftype(rclass.OBJECT.typeptr)
     op = SpaceOperation('getfield', [v_parent, c_name], v_result)
     op1 = Transformer(FakeCPU()).rewrite_operation(op)
-    assert op1.opname == 'classof'
+    assert op1.opname == 'guard_class'
     assert op1.args == [v_parent]
     assert op1.result == v_result
 

Modified: pypy/branch/blackhole-improvement/pypy/jit/metainterp/blackhole.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/metainterp/blackhole.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/metainterp/blackhole.py	Thu Apr 29 16:36:20 2010
@@ -162,7 +162,9 @@
                 # argcode should be 'i' too
                 assert argcodes[next_argcode] == 'i'
                 next_argcode = next_argcode + 1
-                assert lltype.typeOf(result) == lltype.Signed
+                if lltype.typeOf(result) is lltype.Bool:
+                    result = int(result)
+                assert lltype.typeOf(result) is lltype.Signed
                 self.registers_i[ord(code[position])] = result
                 position += 1
             elif resulttype == 'r':
@@ -176,7 +178,7 @@
                 # argcode should be 'f' too
                 assert argcodes[next_argcode] == 'f'
                 next_argcode = next_argcode + 1
-                assert lltype.typeOf(result) == lltype.Float
+                assert lltype.typeOf(result) is lltype.Float
                 self.registers_f[ord(code[position])] = result
                 position += 1
             elif resulttype == 'L':
@@ -273,6 +275,14 @@
     def get_result_f(self):
         return self.registers_f[0]
 
+    def _get_result_anytype(self):
+        "NOT_RPYTHON"
+        if self._return_type == 'int': return self.get_result_i()
+        if self._return_type == 'ref': return self.get_result_r()
+        if self._return_type == 'float': return self.get_result_f()
+        if self._return_type == 'void': return None
+        raise ValueError(self._return_type)
+
     def cleanup_registers_r(self):
         # To avoid keeping references alive, this cleans up the registers_r.
         # It does not clear the references set by copy_constants(), but
@@ -394,84 +404,160 @@
 
     @arguments("i", "i", returns="i")
     def opimpl_int_lt(a, b):
-        return intmask(a < b)
+        return a < b
     @arguments("i", "i", returns="i")
     def opimpl_int_le(a, b):
-        return intmask(a <= b)
+        return a <= b
     @arguments("i", "i", returns="i")
     def opimpl_int_eq(a, b):
-        return intmask(a == b)
+        return a == b
     @arguments("i", "i", returns="i")
     def opimpl_int_ne(a, b):
-        return intmask(a != b)
+        return a != b
     @arguments("i", "i", returns="i")
     def opimpl_int_gt(a, b):
-        return intmask(a > b)
+        return a > b
     @arguments("i", "i", returns="i")
     def opimpl_int_ge(a, b):
-        return intmask(a >= b)
+        return a >= b
     @arguments("i", returns="i")
     def opimpl_int_is_zero(a):
-        return intmask(not a)
+        return not a
     @arguments("i", returns="i")
     def opimpl_int_is_true(a):
-        return intmask(bool(a))
+        return bool(a)
 
     @arguments("i", "i", returns="i")
     def opimpl_uint_lt(a, b):
-        return intmask(r_uint(a) < r_uint(b))
+        return r_uint(a) < r_uint(b)
     @arguments("i", "i", returns="i")
     def opimpl_uint_le(a, b):
-        return intmask(r_uint(a) <= r_uint(b))
+        return r_uint(a) <= r_uint(b)
     @arguments("i", "i", returns="i")
     def opimpl_uint_gt(a, b):
-        return intmask(r_uint(a) > r_uint(b))
+        return r_uint(a) > r_uint(b)
     @arguments("i", "i", returns="i")
     def opimpl_uint_ge(a, b):
-        return intmask(r_uint(a) >= r_uint(b))
+        return r_uint(a) >= r_uint(b)
 
     @arguments("r", "r", returns="i")
     def opimpl_ptr_eq(a, b):
-        return intmask(a == b)
+        return a == b
     @arguments("r", "r", returns="i")
     def opimpl_ptr_ne(a, b):
-        return intmask(a != b)
+        return a != b
     @arguments("r", returns="i")
     def opimpl_ptr_iszero(a):
-        return intmask(not a)
+        return not a
     @arguments("r", returns="i")
     def opimpl_ptr_nonzero(a):
-        return intmask(bool(a))
+        return bool(a)
+
+    @arguments("i", returns="i")
+    def opimpl_int_copy(a):
+        return a
+    @arguments("r", returns="r")
+    def opimpl_ref_copy(a):
+        return a
+    @arguments("f", returns="f")
+    def opimpl_float_copy(a):
+        return a
+
+    opimpl_int_guard_value = opimpl_int_copy
+    opimpl_ref_guard_value = opimpl_ref_copy
+    opimpl_float_guard_value = opimpl_float_copy
+
+    # ----------
+    # float operations
+
+    @arguments("f", returns="f")
+    def opimpl_float_neg(a):
+        return -a
+    @arguments("f", returns="f")
+    def opimpl_float_abs(a):
+        return abs(a)
+    @arguments("f", returns="i")
+    def opimpl_float_is_true(a):
+        return bool(a)
+
+    @arguments("f", "f", returns="f")
+    def opimpl_float_add(a, b):
+        return a + b
+    @arguments("f", "f", returns="f")
+    def opimpl_float_sub(a, b):
+        return a - b
+    @arguments("f", "f", returns="f")
+    def opimpl_float_mul(a, b):
+        return a * b
+    @arguments("f", "f", returns="f")
+    def opimpl_float_truediv(a, b):
+        return a / b
+
+    @arguments("f", "f", returns="i")
+    def opimpl_float_lt(a, b):
+        return a < b
+    @arguments("f", "f", returns="i")
+    def opimpl_float_le(a, b):
+        return a <= b
+    @arguments("f", "f", returns="i")
+    def opimpl_float_eq(a, b):
+        return a == b
+    @arguments("f", "f", returns="i")
+    def opimpl_float_ne(a, b):
+        return a != b
+    @arguments("f", "f", returns="i")
+    def opimpl_float_gt(a, b):
+        return a > b
+    @arguments("f", "f", returns="i")
+    def opimpl_float_ge(a, b):
+        return a >= b
+
+    @arguments("f", returns="i")
+    def opimpl_cast_float_to_int(a):
+        # note: we need to call int() twice to care for the fact that
+        # int(-2147483648.0) returns a long :-(
+        return int(int(a))
+
+    @arguments("i", returns="f")
+    def opimpl_cast_int_to_float(a):
+        return float(a)
+
+    # ----------
+    # control flow operations
 
     @arguments("self", "i")
     def opimpl_int_return(self, a):
         self.registers_i[0] = a
+        if not we_are_translated():
+            self._return_type = "int"
         raise LeaveFrame
+
     @arguments("self", "r")
     def opimpl_ref_return(self, a):
         self.registers_r[0] = a
+        if not we_are_translated():
+            self._return_type = "ref"
         raise LeaveFrame
+
     @arguments("self", "f")
     def opimpl_float_return(self, a):
         self.registers_f[0] = a
+        if not we_are_translated():
+            self._return_type = "float"
         raise LeaveFrame
+
     @arguments("self")
     def opimpl_void_return(self):
+        if not we_are_translated():
+            self._return_type = "void"
         raise LeaveFrame
 
-    @arguments("i", returns="i")
-    def opimpl_int_copy(a):
-        return a
-    @arguments("r", returns="r")
-    def opimpl_ref_copy(a):
-        return a
-    @arguments("f", returns="f")
-    def opimpl_float_copy(a):
-        return a
-
-    opimpl_int_guard_value = opimpl_int_copy
-    opimpl_ref_guard_value = opimpl_ref_copy
-    opimpl_float_guard_value = opimpl_float_copy
+    @arguments("L", "i", "pc", returns="L")
+    def opimpl_goto_if_not(target, a, pc):
+        if a:
+            return pc
+        else:
+            return target
 
     @arguments("L", "i", "i", "pc", returns="L")
     def opimpl_goto_if_not_int_lt(target, a, b, pc):
@@ -522,13 +608,6 @@
         else:
             return target
 
-    @arguments("L", "i", "pc", returns="L")
-    def opimpl_goto_if_not_int_is_true(target, a, pc):
-        if a:
-            return pc
-        else:
-            return target
-
     @arguments("L", "r", "r", "pc", returns="L")
     def opimpl_goto_if_not_ptr_eq(target, a, b, pc):
         if a == b:
@@ -740,7 +819,7 @@
         return self.cpu.bh_new_with_vtable(descr)
 
     @arguments("self", "r", returns="i")
-    def opimpl_classof(self, struct):
+    def opimpl_guard_class(self, struct):
         return self.cpu.bh_classof(struct)
 
     @arguments("self", "r", returns="i")

Modified: pypy/branch/blackhole-improvement/pypy/jit/metainterp/executor.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/metainterp/executor.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/metainterp/executor.py	Thu Apr 29 16:36:20 2010
@@ -14,47 +14,6 @@
 
 # ____________________________________________________________
 
-def do_int_add_ovf(cpu, box1, box2):
-    x = box1.getint()
-    y = box2.getint()
-    try:
-        z = ovfcheck(x + y)
-    except OverflowError:
-        ovf = True
-        z = 0
-    else:
-        ovf = False
-    cpu._overflow_flag = ovf
-    return BoxInt(z)
-
-def do_int_sub_ovf(cpu, box1, box2):
-    x = box1.getint()
-    y = box2.getint()
-    try:
-        z = ovfcheck(x - y)
-    except OverflowError:
-        ovf = True
-        z = 0
-    else:
-        ovf = False
-    cpu._overflow_flag = ovf
-    return BoxInt(z)
-
-def do_int_mul_ovf(cpu, box1, box2):
-    x = box1.getint()
-    y = box2.getint()
-    try:
-        z = ovfcheck(x * y)
-    except OverflowError:
-        ovf = True
-        z = 0
-    else:
-        ovf = False
-    cpu._overflow_flag = ovf
-    return BoxInt(z)
-
-# ----------
-
 def do_float_neg(cpu, box1):
     return ConstFloat(-box1.getfloat())
 

Modified: pypy/branch/blackhole-improvement/pypy/jit/metainterp/pyjitpl.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/metainterp/pyjitpl.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/metainterp/pyjitpl.py	Thu Apr 29 16:36:20 2010
@@ -211,6 +211,10 @@
     def opimpl_void_return(self):
         self.metainterp.finishframe(None)
 
+    @arguments("label")
+    def opimpl_catch_exception(self, target):
+        pass      # see comment in blackhole.py:opimpl_catch_exception.
+
     @arguments("jumptarget")
     def opimpl_goto(self, target):
         self.pc = target
@@ -2060,6 +2064,11 @@
                 index = ord(code[position]) | (ord(code[position+1])<<8)
                 value = self.metainterp.staticdata.opcode_descrs[index]
                 position += 2
+            elif argtype == "label":
+                assert argcodes[next_argcode] == 'L'
+                next_argcode = next_argcode + 1
+                value = ord(code[position]) | (ord(code[position+1])<<8)
+                position += 2
             elif argtype == "boxes":     # a list of boxes of some type
                 length = ord(code[position])
                 value = [None] * length

Modified: pypy/branch/blackhole-improvement/pypy/jit/metainterp/test/test_basic.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/metainterp/test/test_basic.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/metainterp/test/test_basic.py	Thu Apr 29 16:36:20 2010
@@ -43,7 +43,7 @@
         else:
             raise TypeError(T)
     blackholeinterp.run(mainjitcode, 0)
-    return blackholeinterp.get_result_i()
+    return blackholeinterp._get_result_anytype()
 
 def _run_with_pyjitpl(cw, mainjitcode, args, testself):
     from pypy.jit.metainterp import simple_optimize



More information about the Pypy-commit mailing list