[pypy-svn] r24991 - in pypy/dist/pypy/translator/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Sat Mar 25 12:07:23 CET 2006


Author: antocuni
Date: Sat Mar 25 12:07:11 2006
New Revision: 24991

Added:
   pypy/dist/pypy/translator/cli/test/test_cast.py   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/cli/opcodes.py
   pypy/dist/pypy/translator/cli/test/runtest.py
   pypy/dist/pypy/translator/cli/test/test_op.py
Log:
Add support for casts


Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Sat Mar 25 12:07:11 2006
@@ -155,18 +155,22 @@
     'ullong_gt':                'cgt.un',
     'ullong_ge':                _not('clt.un'),
 
-    'cast_bool_to_int':         None,
-    'cast_bool_to_uint':        None,
-    'cast_bool_to_float':       None,
+    # when casting from bool we want that every truth value is casted
+    # to 1: we can't simply DoNothing, because the CLI stack could
+    # contains a truth value not equal to 1, so we should use the !=0
+    # trick.
+    'cast_bool_to_int':         [PushArgs, 'ldc.i4.0', 'ceq']+Not,
+    'cast_bool_to_uint':        [PushArgs, 'ldc.i4.0', 'ceq']+Not,
+    'cast_bool_to_float':       [PushArgs, 'ldc.i4 0', 'ceq']+Not+['conv.r8'],
     'cast_char_to_int':         None,
     'cast_unichar_to_int':      None,
     'cast_int_to_char':         None,
     'cast_int_to_unichar':      None,
-    'cast_int_to_uint':         None,
-    'cast_int_to_float':        None,
-    'cast_int_to_longlong':     None,
-    'cast_uint_to_int':         None,
-    'cast_float_to_int':        None,
-    'cast_float_to_uint':       None,
-    'truncate_longlong_to_int': None
+    'cast_int_to_uint':         DoNothing,
+    'cast_int_to_float':        'conv.r8',
+    'cast_int_to_longlong':     'conv.i8',
+    'cast_uint_to_int':         DoNothing,
+    'cast_float_to_int':        'conv.i4',
+    'cast_float_to_uint':       'conv.i4',
+    'truncate_longlong_to_int': 'conv.i4',
 }

Modified: pypy/dist/pypy/translator/cli/test/runtest.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/runtest.py	(original)
+++ pypy/dist/pypy/translator/cli/test/runtest.py	Sat Mar 25 12:07:11 2006
@@ -61,7 +61,7 @@
             'unsigned int32': 'ToUInt32',
             'int64': 'ToInt64',
             'unsigned int64': 'ToUInt64',
-            'bool': 'ToBool',
+            'bool': 'ToBoolean',
             'float64': 'ToDouble'
             }
 

Added: pypy/dist/pypy/translator/cli/test/test_cast.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/cli/test/test_cast.py	Sat Mar 25 12:07:11 2006
@@ -0,0 +1,46 @@
+import sys
+
+from pypy.translator.cli.test.runtest import check
+from pypy.rpython.rarithmetic import r_uint, r_ulonglong, r_longlong, intmask
+
+def to_int(x):
+    return int(x)
+
+def to_uint(x):
+    return r_uint(x)
+
+def to_float(x):
+    return float(x)
+
+def to_longlong(x):
+    return r_longlong(x)
+
+
+def test_bool_to_int():
+    check(to_int, [bool], (True,))
+    check(to_int, [bool], (False,))    
+
+def test_bool_to_uint():
+    check(to_uint, [bool], (True,))
+    check(to_uint, [bool], (False,))
+
+def test_bool_to_float():
+    check(to_float, [bool], (True,))
+    check(to_float, [bool], (False,))
+
+def test_int_to_uint():
+    check(to_uint, [int], (42,))
+
+def test_int_to_float():
+    check(to_float, [int], (42,))
+
+def test_int_to_longlong():
+    check(to_longlong, [int], (42,))
+
+def test_uint_to_int():
+    def uint_to_int(x):
+        return intmask(x)
+    check(uint_to_int, [r_uint], (sys.maxint+1,))
+
+def test_float_to_int():
+    check(to_int, [float], (42.5,))

Modified: pypy/dist/pypy/translator/cli/test/test_op.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_op.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_op.py	Sat Mar 25 12:07:11 2006
@@ -50,4 +50,3 @@
 def op_any_operations(x, y):
     return (x*y) + (x-y) + (x/y)
 
-print op_int_uint_bitwise(r_uint(42), r_uint(13))



More information about the Pypy-commit mailing list