[pypy-svn] r41993 - in pypy/dist/pypy/jit/codegen: . dump i386 llgraph test

arigo at codespeak.net arigo at codespeak.net
Tue Apr 10 20:40:00 CEST 2007


Author: arigo
Date: Tue Apr 10 20:39:58 2007
New Revision: 41993

Modified:
   pypy/dist/pypy/jit/codegen/dump/rgenop.py
   pypy/dist/pypy/jit/codegen/graph2rgenop.py
   pypy/dist/pypy/jit/codegen/i386/rgenop.py
   pypy/dist/pypy/jit/codegen/llgraph/rgenop.py
   pypy/dist/pypy/jit/codegen/model.py
   pypy/dist/pypy/jit/codegen/test/rgenop_tests.py
Log:
Cast tests.  Support for cast_int_to_ptr too, just in case.


Modified: pypy/dist/pypy/jit/codegen/dump/rgenop.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/dump/rgenop.py	(original)
+++ pypy/dist/pypy/jit/codegen/dump/rgenop.py	Tue Apr 10 20:39:58 2007
@@ -181,6 +181,15 @@
             self.rgenop.vname(gv_ptr2)))
         return v
 
+    def genop_cast_int_to_ptr(self, kind, gv_int):
+        v = self.llbuilder.genop_cast_int_to_ptr(kind, gv_int)
+        self.dump("%s = %s.genop_cast_int_to_ptr(%s, %s)" % (
+            self.rgenop.vname(v),
+            self.name,
+            self.rgenop.kindtokenname(kind),
+            self.rgenop.vname(gv_int)))
+        return v
+
     def genop_same_as(self, kind, gv_x):
         v = self.llbuilder.genop_same_as(kind, gv_x)
         self.dump("%s = %s.genop_same_as(%s, %s)" % (

Modified: pypy/dist/pypy/jit/codegen/graph2rgenop.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/graph2rgenop.py	(original)
+++ pypy/dist/pypy/jit/codegen/graph2rgenop.py	Tue Apr 10 20:39:58 2007
@@ -171,7 +171,12 @@
         gv_result = builder.genop_ptr_ne(token,
                                          var2gv(op.args[0]),
                                          var2gv(op.args[1]))
-        
+
+    elif op.opname == 'cast_int_to_ptr':
+        token = rgenop.kindToken(op.result.concretetype)
+        gv_result = builder.genop_cast_int_to_ptr(token,
+                                                  var2gv(op.args[0]))
+
     elif len(op.args) == 1:
         gv_result = builder.genop1(op.opname, var2gv(op.args[0]))
     elif len(op.args) == 2:

Modified: pypy/dist/pypy/jit/codegen/i386/rgenop.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/i386/rgenop.py	(original)
+++ pypy/dist/pypy/jit/codegen/i386/rgenop.py	Tue Apr 10 20:39:58 2007
@@ -419,6 +419,9 @@
         self.operations.append(op)
         return op
 
+    def genop_cast_int_to_ptr(self, kind, gv_int):
+        return gv_int     # identity
+
     def genop_same_as(self, kind, gv_x):
         if gv_x.is_const:    # must always return a var
             op = OpSameAs(gv_x)

Modified: pypy/dist/pypy/jit/codegen/llgraph/rgenop.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/llgraph/rgenop.py	(original)
+++ pypy/dist/pypy/jit/codegen/llgraph/rgenop.py	Tue Apr 10 20:39:58 2007
@@ -254,6 +254,12 @@
         return LLVar(llimpl.genop(self.b, 'ptr_ne', [gv_ptr1, gv_ptr2],
                                   gv_Bool.v))
 
+    def genop_cast_int_to_ptr(self, gv_PTRTYPE, gv_int):
+        debug_assert(self.rgenop.currently_writing is self,
+                     "genop_cast_int_to_ptr: bad currently_writing")
+        return LLVar(llimpl.genop(self.b, 'cast_int_to_ptr', [gv_int],
+                                  gv_PTRTYPE.v))
+
     def _newblock(self, kinds):
         self.b = newb = llimpl.newblock()
         return [LLVar(llimpl.geninputarg(newb, kind.v)) for kind in kinds]

Modified: pypy/dist/pypy/jit/codegen/model.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/model.py	(original)
+++ pypy/dist/pypy/jit/codegen/model.py	Tue Apr 10 20:39:58 2007
@@ -88,6 +88,7 @@
 ##     def genop_ptr_nonzero(self, kindtoken, gv_ptr)
 ##     def genop_ptr_eq(self, kindtoken, gv_ptr1, gv_ptr2)
 ##     def genop_ptr_ne(self, kindtoken, gv_ptr1, gv_ptr2)
+##     def genop_cast_int_to_ptr(self, kindtoken, gv_int)
 
     # the other thing that happens for a given chunk is entering and
     # leaving basic blocks inside it.

Modified: pypy/dist/pypy/jit/codegen/test/rgenop_tests.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/test/rgenop_tests.py	(original)
+++ pypy/dist/pypy/jit/codegen/test/rgenop_tests.py	Tue Apr 10 20:39:58 2007
@@ -1,6 +1,6 @@
 import random, sys
 from pypy.rpython.annlowlevel import MixLevelAnnotatorPolicy, llhelper
-from pypy.rlib.rarithmetic import intmask
+from pypy.rlib.rarithmetic import intmask, r_uint
 from pypy.rlib.objectmodel import keepalive_until_here
 from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.translator.c.test import test_boehm
@@ -2038,3 +2038,40 @@
                 assert (res & 1) == 1
             else:
                 assert res == intmask(expected << 1) | 0
+
+    def test_cast_direct(self):
+        yield self.cast_direct, ["int_is_true", "cast_bool_to_int"], bool
+        yield self.cast_direct, ["int_is_true",
+                                 "cast_bool_to_uint",
+                                 "cast_uint_to_int"], bool
+        yield self.cast_direct, ["cast_int_to_char",
+                                 "cast_char_to_int"], int, 255
+        yield self.cast_direct, ["cast_int_to_unichar",
+                                 "cast_unichar_to_int"], int, sys.maxunicode
+        yield self.cast_direct, ["cast_int_to_uint", "cast_uint_to_int"], int
+        yield self.cast_direct, ["cast_int_to_ptr", "cast_ptr_to_int"], int
+
+    def cast_direct(self, operations, expected, max=r_uint(-1)):
+        need_odd_integer = False
+        rgenop = self.RGenOp()
+        sigtoken = rgenop.sigToken(FUNC)
+        builder, gv_fn, [gv_x] = rgenop.newgraph(sigtoken, "cast")
+        builder.start_writing()
+        for opname in operations:
+            if opname == "cast_int_to_ptr":
+                S = lltype.GcStruct('s', ('x', lltype.Signed))
+                ptrkind = rgenop.kindToken(lltype.Ptr(S))
+                gv_x = builder.genop_cast_int_to_ptr(ptrkind, gv_x)
+                need_odd_integer = True
+            else:
+                gv_x = builder.genop1(opname, gv_x)
+        builder.finish_and_return(sigtoken, gv_x)
+        builder.end()
+
+        fnptr = self.cast(gv_fn, 1)
+        for x in [0, 1, max // 6, max // 2, max - 1, max]:
+            x = intmask(x)
+            if need_odd_integer:
+                x |= 1
+            result = fnptr(x)
+            assert result == expected(x)



More information about the Pypy-commit mailing list