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

antocuni at codespeak.net antocuni at codespeak.net
Fri Mar 24 18:19:14 CET 2006


Author: antocuni
Date: Fri Mar 24 18:18:54 2006
New Revision: 24973

Modified:
   pypy/dist/pypy/translator/cli/cts.py
   pypy/dist/pypy/translator/cli/function.py
   pypy/dist/pypy/translator/cli/opcodes.py
   pypy/dist/pypy/translator/cli/test/compile.py
   pypy/dist/pypy/translator/cli/test/runtest.py
   pypy/dist/pypy/translator/cli/test/test_op.py
Log:
Addedd support to float, unsigned, and long types.


Modified: pypy/dist/pypy/translator/cli/cts.py
==============================================================================
--- pypy/dist/pypy/translator/cli/cts.py	(original)
+++ pypy/dist/pypy/translator/cli/cts.py	Fri Mar 24 18:18:54 2006
@@ -2,7 +2,8 @@
 Translate between PyPy ootypesystem and .NET Common Type System
 """
 
-from pypy.rpython.lltypesystem.lltype import Signed, Void, Bool
+from pypy.rpython.lltypesystem.lltype import Signed, Unsigned, Void, Bool, Float
+from pypy.rpython.lltypesystem.lltype import SignedLongLong, UnsignedLongLong
 from pypy.translator.cli import conftest
 
 from pypy.tool.ansi_print import ansi_log
@@ -12,14 +13,22 @@
 
 
 _lltype_to_cts = {
-    Signed: 'int32',
     Void: 'void',
+    Signed: 'int32',    
+    Unsigned: 'unsigned int32',
+    SignedLongLong: 'int64',
+    UnsignedLongLong: 'unsigned int64',
     Bool: 'bool',
+    Float: 'float64',
     }
 
 _cts_to_ilasm = {
     'int32': 'i4',
-    'bool': 'i4'
+    'unsigned int32': 'i4',
+    'int64': 'i8',
+    'unsigned int64': 'i8',
+    'bool': 'i4',
+    'float64': 'r8',
     }
 
 def lltype_to_cts(t):
@@ -33,15 +42,19 @@
             assert False, 'Unknown type %s' % t
 
 def lltype_to_ilasm(t):
+    return ctstype_to_ilasm(lltype_to_cts(t))
+
+def ctstype_to_ilasm(t):
     try:
-        return _cts_to_ilasm[lltype_to_cts(t)]
+        return _cts_to_ilasm[t]
     except KeyError:
         if conftest.option.nostop:
             log.WARNING('Unknown ilasm type %s' % t)
             return t
         else:
             assert False, 'Unknown ilasm type %s' % t
-    
+
+
 def llvar_to_cts(var):
     return lltype_to_cts(var.concretetype), var.name
 
@@ -49,11 +62,16 @@
     return lltype_to_cts(const.concretetype), const.value
 
 def llconst_to_ilasm(const):
+    """
+    Return the const as a string suitable for ilasm.
+    """
     ilasm_type = lltype_to_ilasm(const.concretetype)
     if const.concretetype is Bool:
-        return ilasm_type, int(const.value)
+        return ilasm_type, str(int(const.value))
+    elif const.concretetype is Float:
+        return ilasm_type, repr(const.value)
     else:
-        return ilasm_type, const.value
+        return ilasm_type, str(const.value)
 
 def graph_to_signature(graph):
     ret_type, ret_var = llvar_to_cts(graph.getreturnvar())

Modified: pypy/dist/pypy/translator/cli/function.py
==============================================================================
--- pypy/dist/pypy/translator/cli/function.py	(original)
+++ pypy/dist/pypy/translator/cli/function.py	Fri Mar 24 18:18:54 2006
@@ -72,7 +72,8 @@
         # .NET runtime that seems to need a return statement at the
         # end of the function
         if returntype != 'void':
-            self.ilasm.opcode('ldc.i4.0')
+            ilasm_type = cts.ctstype_to_ilasm(returntype)
+            self.ilasm.opcode('ldc.%s 0' % ilasm_type)
 
         self.ilasm.opcode('ret')
         

Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Fri Mar 24 18:18:54 2006
@@ -10,21 +10,21 @@
 
 opcodes = {
     'same_as':                  DoNothing, # TODO: does same_as really do nothing else than renaming?    
-    'direct_call':              None, # for now it's a special case
-    'indirect_call':            None,
+    'direct_call':              None,      # for now it's a special case
+    'indirect_call':            None,      # when it's generated?
 
     # __________ numeric operations __________
 
     'bool_not':                 Not,
 
-    'char_lt':                  'clt',
+    'char_lt':                  None,
     'char_le':                  None,
-    'char_eq':                  'ceq',
+    'char_eq':                  None,
     'char_ne':                  None,
     'char_gt':                  None,
     'char_ge':                  None,
 
-    'unichar_eq':               None,
+    'unichar_eq':               None,      # should we unify unichar and char, as Jython does?
     'unichar_ne':               None,
 
     'int_is_true':              DoNothing,
@@ -73,87 +73,87 @@
     'int_floordiv_ovf_zer':     None,
     'int_mod_ovf_zer':          None,
 
-    'uint_is_true':             None,
-    'uint_neg':                 None,
-    'uint_abs':                 None,
-    'uint_invert':              None,
-
-    'uint_add':                 None,
-    'uint_sub':                 None,
-    'uint_mul':                 None,
-    'uint_div':                 None,
-    'uint_truediv':             None,
-    'uint_floordiv':            None,
-    'uint_mod':                 None,
-    'uint_lt':                  None,
-    'uint_le':                  None,
-    'uint_eq':                  None,
-    'uint_ne':                  None,
-    'uint_gt':                  None,
-    'uint_ge':                  None,
-    'uint_and':                 None,
-    'uint_or':                  None,
-    'uint_lshift':              None,
-    'uint_rshift':              None,
-    'uint_xor':                 None,
-
-    'float_is_true':            None,
-    'float_neg':                None,
-    'float_abs':                None,
-
-    'float_add':                None,
-    'float_sub':                None,
-    'float_mul':                None,
-    'float_div':                None,
-    'float_truediv':            None,
-    'float_floordiv':           None,
-    'float_mod':                None,
-    'float_lt':                 None,
-    'float_le':                 None,
-    'float_eq':                 None,
-    'float_ne':                 None,
-    'float_gt':                 None,
-    'float_ge':                 None,
-    'float_floor':              None,
-    'float_fmod':               None,
-
-    'llong_is_true':            None,
-    'llong_neg':                None,
-    'llong_abs':                None,
-    'llong_invert':             None,
-
-    'llong_add':                None,
-    'llong_sub':                None,
-    'llong_mul':                None,
-    'llong_div':                None,
-    'llong_truediv':            None,
-    'llong_floordiv':           None,
-    'llong_mod':                None,
-    'llong_lt':                 None,
-    'llong_le':                 None,
-    'llong_eq':                 None,
-    'llong_ne':                 None,
-    'llong_gt':                 None,
-    'llong_ge':                 None,
-
-    'ullong_is_true':           None,
-    'ullong_neg':               None,
-    'ullong_abs':               None,
-    'ullong_invert':            None,
-
-    'ullong_add':               None,
-    'ullong_sub':               None,
-    'ullong_mul':               None,
-    'ullong_div':               None,
-    'ullong_truediv':           None,
-    'ullong_floordiv':          None,
-    'ullong_mod':               None,
-    'ullong_lt':                None,
-    'ullong_le':                None,
-    'ullong_eq':                None,
-    'ullong_ne':                None,
-    'ullong_gt':                None,
-    'ullong_ge':                None,
+    'uint_is_true':             DoNothing,
+    'uint_neg':                 None,      # What's the meaning?
+    'uint_abs':                 None, # TODO
+    'uint_invert':              'not',
+
+    'uint_add':                 'add',
+    'uint_sub':                 'sub',
+    'uint_mul':                 'mul',
+    'uint_div':                 'div.un',
+    'uint_truediv':             None, # TODO
+    'uint_floordiv':            'div.un',
+    'uint_mod':                 'rem.un',
+    'uint_lt':                  'clt.un',
+    'uint_le':                  _not('cgt.un'),
+    'uint_eq':                  'ceq',
+    'uint_ne':                  _not('ceq'),
+    'uint_gt':                  'cgt.un',
+    'uint_ge':                  _not('clt.un'),
+    'uint_and':                 'and',
+    'uint_or':                  'or',
+    'uint_lshift':              'shl',
+    'uint_rshift':              'shr.un',
+    'uint_xor':                 'xor',
+
+    'float_is_true':            [PushArgs, 'ldc.r8 0', 'ceq']+Not,
+    'float_neg':                'neg',
+    'float_abs':                None, # TODO
+
+    'float_add':                'add',
+    'float_sub':                'sub',
+    'float_mul':                'mul',
+    'float_div':                'div',
+    'float_truediv':            'div', 
+    'float_floordiv':           None, # TODO
+    'float_mod':                'rem',
+    'float_lt':                 'clt',
+    'float_le':                 _not('cgt'),
+    'float_eq':                 'ceq',
+    'float_ne':                 _not('ceq'),
+    'float_gt':                 'cgt',
+    'float_ge':                 _not('clt'),
+    'float_floor':              None, # TODO
+    'float_fmod':               None, # TODO
+
+    'llong_is_true':            [PushArgs, 'ldc.i8 0', 'ceq']+Not,
+    'llong_neg':                'neg',
+    'llong_abs':                None, # TODO
+    'llong_invert':             'not',
+
+    'llong_add':                'add',
+    'llong_sub':                'sub',
+    'llong_mul':                'mul',
+    'llong_div':                'div',
+    'llong_truediv':            None, # TODO
+    'llong_floordiv':           'div',
+    'llong_mod':                'rem',
+    'llong_lt':                 'clt',
+    'llong_le':                 _not('cgt'),
+    'llong_eq':                 'ceq',
+    'llong_ne':                 _not('ceq'),
+    'llong_gt':                 'cgt',
+    'llong_ge':                 _not('clt'),
+
+    'ullong_is_true':            [PushArgs, 'ldc.i8 0', 'ceq']+Not,
+    'ullong_neg':                None,
+    'ullong_abs':                None, # TODO
+    'ullong_invert':             'not',
+
+    'ullong_add':               'add',
+    'ullong_sub':               'sub',
+    'ullong_mul':               'mul',
+    'ullong_div':               'div.un',
+    'ullong_truediv':           None, # TODO
+    'ullong_floordiv':          'div.un',
+    'ullong_mod':               'rem.un',
+    'ullong_lt':                'clt.un',
+    'ullong_le':                _not('cgt.un'),
+    'ullong_eq':                'ceq',
+    'ullong_ne':                _not('ceq.un'),
+    'ullong_gt':                'cgt.un',
+    'ullong_ge':                _not('clt.un'),
 
     'cast_bool_to_int':         None,
     'cast_bool_to_uint':        None,

Modified: pypy/dist/pypy/translator/cli/test/compile.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/compile.py	(original)
+++ pypy/dist/pypy/translator/cli/test/compile.py	Fri Mar 24 18:18:54 2006
@@ -1,6 +1,8 @@
 #!/bin/env python
 
+import sys
 import py
+from pypy.rpython.rarithmetic import r_int, r_uint, r_ulonglong, r_longlong
 from pypy.translator.test import snippet as s
 from pypy.translator.cli import conftest
 from pypy.translator.cli.test.runtest import compile_function
@@ -22,17 +24,17 @@
         print 'OK'
 
 
-def bar(x, y):
-    if x>y:
-        return x
-    else:
-        return y
 
+def bar(x, y):
+    return x and (not y)
 
+f = compile_function(bar, [r_uint, r_uint])
 
-f = compile_function(bar, [int, int])
+try:
+    check(f, bar, r_uint(sys.maxint+1), r_uint(42))
+    check(f, bar, 4, 5)    
+except py.test.Item.Skipped:
+    print 'Test skipped'
 
-check(f, bar, 3, 3)
-check(f, bar, 4, 5)
 
 #compile_function(s.is_perfect_number, [int])

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	Fri Mar 24 18:18:54 2006
@@ -11,9 +11,17 @@
 from pypy.translator.cli.cts import graph_to_signature
 from pypy.translator.cli.cts import llvar_to_cts
 
+FLOAT_PRECISION = 8
+
 def check(func, annotation, args):
     mono = compile_function(func, annotation)
-    assert func(*args) == mono(*args)
+    res1 = func(*args)
+    res2 = mono(*args)
+
+    if type(res1) is float:
+        assert round(res1, FLOAT_PRECISION) == round(res2, FLOAT_PRECISION)
+    else:
+        assert res1 == res2
 
 class TestEntryPoint(Node):
     """
@@ -36,7 +44,8 @@
             ilasm.opcode('ldc.i4.%d' % i)
             ilasm.opcode('ldelem.ref')
             arg_type, arg_var = llvar_to_cts(arg)
-            ilasm.call('int32 class [mscorlib]System.Convert::To%s(string)' % arg_type.capitalize())
+            ilasm.call('%s class [mscorlib]System.Convert::%s(string)' %
+                       (arg_type, self.__convert_method(arg_type)))
 
         ilasm.call(graph_to_signature(self.graph))
 
@@ -46,6 +55,21 @@
         ilasm.opcode('ret')
         ilasm.end_function()
 
+    def __convert_method(self, arg_type):
+        _conv = {
+            'int32': 'ToInt32',
+            'unsigned int32': 'ToUInt32',
+            'int64': 'ToInt64',
+            'unsigned int64': 'ToUInt64',
+            'bool': 'ToBool',
+            'float64': 'ToDouble'
+            }
+
+        try:
+            return _conv[arg_type]
+        except KeyError:
+            assert False, 'Input type %s not supported' % arg_type
+
 
 class compile_function:
     def __init__(self, func, annotation=[], graph=None):
@@ -100,7 +124,7 @@
 
         self.__check_helper("ilasm")
         retval = subprocess.call(["ilasm", tmpfile], stdout=subprocess.PIPE)
-        assert retval == 0, 'ilasm failed to assemble %s' % tmpfile
+        assert retval == 0, 'ilasm failed to assemble %s (%s)' % (self.graph.name, tmpfile)
         return tmpfile.replace('.il', '.exe')
 
     def __call__(self, *args):
@@ -115,8 +139,10 @@
         assert retval == 0, stderr
 
         ret_type, ret_var = llvar_to_cts(self.graph.getreturnvar())
-        if ret_type == 'int32':
+        if 'int' in ret_type:
             return int(stdout)
+        elif ret_type == 'float64':
+            return float(stdout)
         elif ret_type == 'bool':
             return stdout.strip().lower() == 'true'
         else:

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	Fri Mar 24 18:18:54 2006
@@ -1,25 +1,53 @@
 from pypy.translator.cli.test.runtest import check
+from pypy.rpython.rarithmetic import r_uint, r_ulonglong, r_longlong
+
+import sys
 
 def test_op():
     for name, func in globals().iteritems():
-        if name.startswith('op_'):
-            yield check, func, [int, int], (3, 4)
+        if not name.startswith('op_'):
+            continue
+
+        any = '_any_' in name
+        if any or '_int_' in name:
+            yield check, func, [int, int], (42, 13)
+
+        if any or '_uint_' in name:
+            yield check, func, [r_uint, r_uint], (r_uint(sys.maxint+1), r_uint(42))
+
+        if any or '_long_' in name:
+            yield check, func, [r_longlong, r_longlong], (r_longlong(sys.maxint*3), r_longlong(42))
+
+        if any or '_ulong_' in name:
+            yield check, func, [r_ulonglong, r_ulonglong], (r_ulonglong(sys.maxint*3), r_ulonglong(42))
 
+        if any or '_float_' in name:
+            yield check, func, [float, float], (42.0, (10.0/3))
 
-def op_neg(x, y):
+
+
+def op_int_long_float_neg(x, y):
     return -x
 
-def op_less_equal(x, y):
+def op_any_ge(x, y):
+    return x>=y
+
+def op_any_le(x, y):
     return x<=y
 
-def op_and_not(x, y):
+def op_int_float_and_not(x, y):
     return x and (not y)
 
-def op_shift(x, y):
+def op_int_uint_shift(x, y):
     return x<<3 + y>>4
 
-def op_bit_and_or_not_xor(x, y):
+def op_int_uint_bitwise(x, y):
     return (x&y) | ~(x^y)
 
-def op_operations(x, y):
-    return (x*y) / (x-y) +(-x)
+def op_int_long_uint_ulong_modulo(x, y):
+    return x%y
+
+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