[pypy-svn] r36007 - in pypy/dist/pypy/rpython: . test

arigo at codespeak.net arigo at codespeak.net
Wed Dec 27 21:00:03 CET 2006


Author: arigo
Date: Wed Dec 27 21:00:00 2006
New Revision: 36007

Modified:
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/test/test_rbuiltin.py
Log:
If necessary, generate a 'cast_primitive' operation when rtyping
lltype.cast_primitive().



Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Wed Dec 27 21:00:00 2006
@@ -434,13 +434,17 @@
         return v_value
     if (isinstance(TGT, lltype.Primitive) and
         isinstance(ORIG, lltype.Primitive)):
-        op = _cast_to_Signed[ORIG]
-        if op:
-            v_value = llops.genop(op, [v_value], resulttype = lltype.Signed)
-        op = _cast_from_Signed[TGT]
-        if op:
-            v_value = llops.genop(op, [v_value], resulttype = TGT)
-        return v_value
+        if ORIG in _cast_to_Signed and TGT in _cast_from_Signed:
+            op = _cast_to_Signed[ORIG]
+            if op:
+                v_value = llops.genop(op, [v_value], resulttype=lltype.Signed)
+            op = _cast_from_Signed[TGT]
+            if op:
+                v_value = llops.genop(op, [v_value], resulttype=TGT)
+            return v_value
+        else:
+            # use the generic operation if there is no alternative
+            return llops.genop('cast_primitive', [v_value], resulttype=TGT)
     elif isinstance(TGT, lltype.Ptr):
         if isinstance(ORIG, lltype.Ptr):
             if (isinstance(TGT.TO, lltype.OpaqueType) or

Modified: pypy/dist/pypy/rpython/test/test_rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rbuiltin.py	Wed Dec 27 21:00:00 2006
@@ -6,6 +6,7 @@
 from pypy.rlib.rarithmetic import r_uint, intmask
 from pypy.annotation.builtin import *
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
+from pypy.rpython.rctypes.rcarithmetic import CShort
 import py
 
 
@@ -386,6 +387,10 @@
             return lltype.cast_primitive(lltype.UniChar, v)
         res = self.interpret(llf, [ord('x')], policy=LowLevelAnnotatorPolicy())
         assert res == u'x'
+        def llf(v):
+            return lltype.cast_primitive(CShort, v)
+        res = self.interpret(llf, [123], policy=LowLevelAnnotatorPolicy())
+        assert res == 123
 
 
     



More information about the Pypy-commit mailing list