[pypy-svn] r36739 - in pypy/dist/pypy/jit/codegen: i386/test llvm/test ppc ppc/test test

mwh at codespeak.net mwh at codespeak.net
Sun Jan 14 15:30:52 CET 2007


Author: mwh
Date: Sun Jan 14 15:30:49 2007
New Revision: 36739

Added:
   pypy/dist/pypy/jit/codegen/ppc/test/test_operation.py   (contents, props changed)
   pypy/dist/pypy/jit/codegen/test/operation_tests.py   (contents, props changed)
Modified:
   pypy/dist/pypy/jit/codegen/i386/test/test_operation.py
   pypy/dist/pypy/jit/codegen/llvm/test/test_operation.py
   pypy/dist/pypy/jit/codegen/ppc/rgenop.py
Log:
convert i386/test/test_operation.py to test/operation_tests.py that can be run
on all backends.  make it run on the ppc backend and make it work there.


Modified: pypy/dist/pypy/jit/codegen/i386/test/test_operation.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/i386/test/test_operation.py	(original)
+++ pypy/dist/pypy/jit/codegen/i386/test/test_operation.py	Sun Jan 14 15:30:49 2007
@@ -1,14 +1,7 @@
-import py
 from pypy.rlib.objectmodel import specialize
-from pypy.annotation import model as annmodel
-from pypy.rpython.lltypesystem import lltype
-from pypy.translator.translator import TranslationContext, graphof
-from pypy.jit.codegen import graph2rgenop
+from pypy.jit.codegen.test.operation_tests import OperationTests
 from pypy.jit.codegen.i386.rgenop import RI386GenOp
 from pypy.rpython.memory.lltypelayout import convert_offset_to_int
-from pypy.rlib.rarithmetic import r_uint
-from ctypes import cast, c_void_p, CFUNCTYPE, c_int, c_float
-from pypy import conftest
 
 def conv(n):
     if not isinstance(n, int):
@@ -43,229 +36,9 @@
         return tuple(map(conv, RI386GenOp.varsizeAllocToken(A)))
 
 
-class I386TestBasicMixin(object):
+class I386TestMixin(object):
     RGenOp = RGenOpPacked
-    
 
-class BasicTests(object):
-    @staticmethod
-    def _to_ctypes(t): #limited type support for now
-        if t is float:
-            return c_float
-        return c_int
-
-    def rgen(self, ll_function, argtypes, rettype=int): #XXX get rettype from annotation
-        t = TranslationContext()
-        t.buildannotator().build_types(ll_function, argtypes)
-        t.buildrtyper().specialize()
-        graph = graphof(t, ll_function)
-        if conftest.option.view:
-            graph.show()
-        rgenop = self.RGenOp()
-        self.rgenop = rgenop      # keep this alive!
-        gv_generated = graph2rgenop.compile_graph(rgenop, graph)
-        ctypestypes = [BasicTests._to_ctypes(t) for t in argtypes]
-        fp = cast(c_void_p(gv_generated.value),
-                  CFUNCTYPE(BasicTests._to_ctypes(rettype), *ctypestypes))
-        return fp
-
-    def test_arithmetic(self):
-        for fn in [lambda x, y: x + y,
-                   lambda x, y: x - y,
-                   lambda x, y: x * y,
-                   lambda x, y: x // y,
-                   lambda x, y: x % y,
-                   lambda x, y: x << y,
-                   lambda x, y: x >> y,
-                   lambda x, y: x ^ y,
-                   lambda x, y: x & y,
-                   lambda x, y: x | y,
-                   lambda x, y: -y,
-                   lambda x, y: ~y,
-                   lambda x, y: abs(y),
-                   lambda x, y: abs(-x),
-                   ]:
-            fp = self.rgen(fn, [int, int])
-            assert fp(40, 2) == fn(40, 2)
-            assert fp(25, 3) == fn(25, 3)
-
-    def test_comparison(self):
-        for fn in [lambda x, y: int(x <  y),
-                   lambda x, y: int(x <= y),
-                   lambda x, y: int(x == y),
-                   lambda x, y: int(x != y),
-                   lambda x, y: int(x >  y),
-                   lambda x, y: int(x >= y),
-                   ]:
-            fp = self.rgen(fn, [int, int])
-            assert fp(12, 11) == fn(12, 11)
-            assert fp(12, 12) == fn(12, 12)
-            assert fp(12, 13) == fn(12, 13)
-            assert fp(-12, 11) == fn(-12, 11)
-            assert fp(-12, 12) == fn(-12, 12)
-            assert fp(-12, 13) == fn(-12, 13)
-            assert fp(12, -11) == fn(12, -11)
-            assert fp(12, -12) == fn(12, -12)
-            assert fp(12, -13) == fn(12, -13)
-            assert fp(-12, -11) == fn(-12, -11)
-            assert fp(-12, -12) == fn(-12, -12)
-            assert fp(-12, -13) == fn(-12, -13)
-
-    def test_char_comparison(self):
-        for fn in [lambda x, y: int(chr(x) <  chr(y)),
-                   lambda x, y: int(chr(x) <= chr(y)),
-                   lambda x, y: int(chr(x) == chr(y)),
-                   lambda x, y: int(chr(x) != chr(y)),
-                   lambda x, y: int(chr(x) >  chr(y)),
-                   lambda x, y: int(chr(x) >= chr(y)),
-                   ]:
-            fp = self.rgen(fn, [int, int])
-            assert fp(12, 11) == fn(12, 11)
-            assert fp(12, 12) == fn(12, 12)
-            assert fp(12, 13) == fn(12, 13)
-            assert fp(182, 11) == fn(182, 11)
-            assert fp(182, 12) == fn(182, 12)
-            assert fp(182, 13) == fn(182, 13)
-            assert fp(12, 181) == fn(12, 181)
-            assert fp(12, 182) == fn(12, 182)
-            assert fp(12, 183) == fn(12, 183)
-            assert fp(182, 181) == fn(182, 181)
-            assert fp(182, 182) == fn(182, 182)
-            assert fp(182, 183) == fn(182, 183)
-
-    def test_unichar_comparison(self):
-        for fn in [lambda x, y: int(unichr(x) == unichr(y)),
-                   lambda x, y: int(unichr(x) != unichr(y)),
-                   ]:
-            fp = self.rgen(fn, [int, int])
-            assert fp(12, 11) == fn(12, 11)
-            assert fp(12, 12) == fn(12, 12)
-            assert fp(12, 13) == fn(12, 13)
-            assert fp(53182, 11) == fn(53182, 11)
-            assert fp(53182, 12) == fn(53182, 12)
-            assert fp(53182, 13) == fn(53182, 13)
-            assert fp(12, 53181) == fn(12, 53181)
-            assert fp(12, 53182) == fn(12, 53182)
-            assert fp(12, 53183) == fn(12, 53183)
-            assert fp(53182, 53181) == fn(53182, 53181)
-            assert fp(53182, 53182) == fn(53182, 53182)
-            assert fp(53182, 53183) == fn(53182, 53183)
-
-    def test_char_array(self):
-        A = lltype.GcArray(lltype.Char)
-        def fn(n):
-            a = lltype.malloc(A, 5) #XXX this boils down to rgenop.genop_malloc_varsize() ?
-            a[4] = 'H'
-            a[3] = 'e'
-            a[2] = 'l'
-            a[1] = 'l'
-            a[0] = 'o'
-            return ord(a[n])
-        fp = self.rgen(fn, [int])
-        for i in range(5):
-            assert fp(i) == fn(i)
-
-    def test_char_varsize_array(self):
-        A = lltype.GcArray(lltype.Char)
-        def fn(n):
-            a = lltype.malloc(A, n)
-            a[4] = 'H'
-            a[3] = 'e'
-            a[2] = 'l'
-            a[1] = 'l'
-            a[0] = 'o'
-            return ord(a[n-1])
-        fp = self.rgen(fn, [int])
-        assert fp(5) == fn(5)
-
-    def test_unichar_array(self):
-        A = lltype.GcArray(lltype.UniChar)
-        def fn(n):
-            a = lltype.malloc(A, 5)
-            a[4] = u'H'
-            a[3] = u'e'
-            a[2] = u'l'
-            a[1] = u'l'
-            a[0] = u'o'
-            return ord(a[n])
-        fp = self.rgen(fn, [int])
-        for i in range(5):
-            assert fp(i) == fn(i)
-
-    def test_char_unichar_fields(self):
-        S = lltype.GcStruct('S', ('a', lltype.Char),
-                                 ('b', lltype.Char),
-                                 ('c', lltype.UniChar),
-                                 ('d', lltype.UniChar),
-                                 ('e', lltype.Signed))
-        def fn():
-            s = lltype.malloc(S)
-            s.a = 'A'
-            s.b = 'b'
-            s.c = unichr(0x5a6b)
-            s.d = unichr(0x7c8d)
-            s.e = -1612
-            return ((s.a == 'A') +
-                    (s.b == 'b') +
-                    (s.c == unichr(0x5a6b)) +
-                    (s.d == unichr(0x7c8d)) +
-                    (s.e == -1612))
-        fp = self.rgen(fn, [])
-        res = fp()
-        assert res == 5
-
-    def test_unsigned(self):
-        for fn in [lambda x, y: x + y,
-                   lambda x, y: x - y,
-                   lambda x, y: x * y,
-                   lambda x, y: x // y,
-                   lambda x, y: x % y,
-                   lambda x, y: x << y,
-                   lambda x, y: x >> y,
-                   lambda x, y: x ^ y,
-                   lambda x, y: x & y,
-                   lambda x, y: x | y,
-                   lambda x, y: -y,
-                   lambda x, y: ~y,
-                   ]:
-            fp = self.rgen(fn, [r_uint, r_uint])
-            assert fp(40, 2) == fn(40, 2)
-            assert fp(25, 3) == fn(25, 3)
-
-    def test_float_arithmetic(self):
-        for fn in [lambda x, y: x + y,
-                   lambda x, y: x - y,
-                   lambda x, y: x * y,
-                   lambda x, y: x / y,
-                   #lambda x, y: x % y,  #not used?
-                   lambda x, y: -y,
-                   #lambda x, y: ~y,    #TypeError: bad operand type for unary ~
-                   lambda x, y: abs(y),
-                   lambda x, y: abs(-x),
-                   ]:
-            fp = self.rgen(fn, [float, float], float)
-            assert fp(40.0, 2.0) == fn(40.0, 2.0)
-            assert fp(25.125, 1.5) == fn(25.125, 1.5)
-
-    def test_float_pow(self): #harder test for llvm
-        for fn in [lambda x, y: x ** y,    #not supported in llvm backend
-                   ]:
-            fp = self.rgen(fn, [float, float], float)
-            assert fp(40.0, 2.0) == fn(40.0, 2.0)
-            assert fp(25.125, 1.5) == fn(25.125, 1.5)
-
-    def test_float_cast(self): #because of different rettype
-        for fn in [lambda x: bool(x),
-                   lambda x: bool(x - 2.0),
-                   ]:
-            fp = self.rgen(fn, [float], bool)
-            assert fp(6.0) == fn(6.0)
-            assert fp(2.0) == fn(2.0)
-            assert fp(0.0) == fn(0.0)
-            assert fp(-2.0) == fn(-2.0)
-
-
-class TestBasic(I386TestBasicMixin,
-                BasicTests):
+class TestOperation(I386TestMixin, OperationTests):
     pass
 

Modified: pypy/dist/pypy/jit/codegen/llvm/test/test_operation.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/test/test_operation.py	(original)
+++ pypy/dist/pypy/jit/codegen/llvm/test/test_operation.py	Sun Jan 14 15:30:49 2007
@@ -2,7 +2,7 @@
 from pypy.rlib.objectmodel import specialize
 from pypy.rpython.memory.lltypelayout import convert_offset_to_int
 from pypy.jit.codegen.llvm.test.test_llvmjit import skip_unsupported_platform
-from pypy.jit.codegen.i386.test.test_operation import BasicTests
+from pypy.jit.codegen.test.operations_test import OperationTests
 from pypy.jit.codegen.llvm.rgenop import RLLVMGenOp
 from pypy.jit.codegen.llvm.llvmjit import llvm_version, MINIMAL_VERSION
 
@@ -45,10 +45,10 @@
 
 
 class TestBasic(LLVMTestBasicMixin,
-                BasicTests):
+                OperationTests):
 
     # for the individual tests see
-    # ====> ../../i386/test/test_operation.py
+    # ====> ../../test/operation_tests.py
 
     def skip(self):
         py.test.skip('WIP')

Modified: pypy/dist/pypy/jit/codegen/ppc/rgenop.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/ppc/rgenop.py	(original)
+++ pypy/dist/pypy/jit/codegen/ppc/rgenop.py	Sun Jan 14 15:30:49 2007
@@ -1,3 +1,4 @@
+import py
 from pypy.jit.codegen.model import AbstractRGenOp, GenLabel, GenBuilder
 from pypy.jit.codegen.model import GenVar, GenConst, CodeGenSwitch
 from pypy.rpython.lltypesystem import lltype, llmemory
@@ -245,76 +246,59 @@
         return gv_result
 
     def genop_getfield(self, fieldtoken, gv_ptr):
-        gv_result = Var()
-        self.insns.append(
-            insn.Insn_GPR__GPR_IMM(_PPC.lwz,
-                                   gv_result, [gv_ptr, IntConst(fieldtoken)]))
-        return gv_result
+        fieldoffset, fieldsize = fieldtoken
+        opcode = {1:_PPC.lbz, 2:_PPC.lhz, 4:_PPC.lwz}[fieldsize]
+        return self._arg_imm_op(gv_ptr, IntConst(fieldoffset), opcode)
 
     def genop_setfield(self, fieldtoken, gv_ptr, gv_value):
         gv_result = Var()
+        fieldoffset, fieldsize = fieldtoken
+        opcode = {1:_PPC.stb, 2:_PPC.sth, 4:_PPC.stw}[fieldsize]
         self.insns.append(
-            insn.Insn_None__GPR_GPR_IMM(_PPC.stw,
-                                        [gv_value, gv_ptr, IntConst(fieldtoken)]))
+            insn.Insn_None__GPR_GPR_IMM(opcode,
+                                        [gv_value, gv_ptr, IntConst(fieldoffset)]))
         return gv_result
 
     def genop_getsubstruct(self, fieldtoken, gv_ptr):
-        gv_result = Var()
-        self.insns.append(
-            insn.Insn_GPR__GPR_IMM(RPPCAssembler.addi,
-                                   gv_result, [gv_ptr, IntConst(fieldtoken)]))
-        return gv_result
+        return self._arg_imm_op(gv_ptr, IntConst(fieldtoken), _PPC.addi)
 
     def genop_getarrayitem(self, arraytoken, gv_ptr, gv_index):
         _, _, itemsize = arraytoken
-        assert itemsize == 4
+        opcode = {1:_PPC.lbzx,
+                  2:_PPC.lhzx,
+                  4:_PPC.lwzx}[itemsize]
+        opcodei = {1:_PPC.lbz,
+                   2:_PPC.lhz,
+                   4:_PPC.lwz}[itemsize]
         gv_itemoffset = self.itemoffset(arraytoken, gv_index)
-        gv_result = Var()
-        if gv_itemoffset.fits_in_immediate():
-            self.insns.append(
-                insn.Insn_GPR__GPR_IMM(RPPCAssembler.lwz,
-                                       gv_result, [gv_ptr, gv_itemoffset]))
-        else:
-            self.insns.append(
-                insn.Insn_GPR__GPR_GPR(RPPCAssembler.lwzx,
-                                       gv_result, [gv_ptr, gv_itemoffset]))
-        return gv_result
+        return self._arg_arg_op_with_imm(gv_ptr, gv_itemoffset, False, opcode, opcodei)
 
     def genop_getarraysubstruct(self, arraytoken, gv_ptr, gv_index):
         _, _, itemsize = arraytoken
         assert itemsize == 4
         gv_itemoffset = self.itemoffset(arraytoken, gv_index)
-        gv_result = Var()
-        if gv_itemoffset.fits_in_immediate():
-            self.insns.append(
-                insn.Insn_GPR__GPR_IMM(RPPCAssembler.addi,
-                                       gv_result, [gv_ptr, gv_itemoffset]))
-        else:
-            self.insns.append(
-                insn.Insn_GPR__GPR_GPR(RPPCAssembler.add,
-                                       gv_result, [gv_ptr, gv_itemoffset]))
-        return gv_result
+        return self._arg_arg_op_with_imm(gv_ptr, gv_itemoffset, True, _PPC.add, _PPC.addi)
 
     def genop_getarraysize(self, arraytoken, gv_ptr):
-        lengthoffset, _, _ = arraytoken
-        gv_result = Var()
-        self.insns.append(
-                insn.Insn_GPR__GPR_IMM(RPPCAssembler.lwz,
-                                       gv_result, [gv_ptr, IntConst(lengthoffset)]))
-        return gv_result
+        return self._arg_imm_op(gv_ptr, IntConst(lengthoffset), _PPC.lwz)
 
     def genop_setarrayitem(self, arraytoken, gv_ptr, gv_index, gv_value):
         _, _, itemsize = arraytoken
-        assert itemsize == 4
         gv_itemoffset = self.itemoffset(arraytoken, gv_index)
         gv_result = Var()
         if gv_itemoffset.fits_in_immediate():
+            opcode = {1:_PPC.stb,
+                      2:_PPC.sth,
+                      4:_PPC.stw}[itemsize]
             self.insns.append(
-                insn.Insn_None__GPR_GPR_IMM(RPPCAssembler.stw,
+                insn.Insn_None__GPR_GPR_IMM(opcode,
                                             [gv_value, gv_ptr, gv_itemoffset]))
         else:
+            opcode = {1:_PPC.stbx,
+                      2:_PPC.sthx,
+                      4:_PPC.stwx}[itemsize]
             self.insns.append(
-                insn.Insn_None__GPR_GPR_GPR(RPPCAssembler.stwx,
+                insn.Insn_None__GPR_GPR_GPR(opcode,
                                             [gv_value, gv_ptr, gv_itemoffset]))
 
     def genop_malloc_fixedsize(self, alloctoken):
@@ -329,7 +313,7 @@
                                     [gv_itemoffset])
         lengthoffset, _, _ = varsizealloctoken
         self.insns.append(
-            insn.Insn_None__GPR_GPR_IMM(RPPCAssembler.stw,
+            insn.Insn_None__GPR_GPR_IMM(_PPC.stw,
                                         [gv_size, gv_result, IntConst(lengthoffset)]))
         return gv_result
 
@@ -653,39 +637,31 @@
 
     def _arg_op(self, gv_arg, opcode):
         gv_result = Var()
-        self.insns.append(insn.Insn_GPR__GPR(opcode, gv_result, gv_arg))
-        return gv_result
-
-    def _arg_arg_op_with_imm(self, gv_x, gv_y, commutative, opcode, opcodei):
-        gv_result = Var()
-        if gv_y.fits_in_immediate():
-            self.insns.append(
-                insn.Insn_GPR__GPR_IMM(opcodei,
-                                       gv_result, [gv_x, gv_y]))
-        elif gv_x.fits_in_immediate() and commutative:
-            self.insns.append(
-                insn.Insn_GPR__GPR_IMM(opcodei,
-                                       gv_result, [gv_y, gv_x]))
-        else:
-            self.insns.append(
-                insn.Insn_GPR__GPR_GPR(opcode,
-                                       gv_result, [gv_x, gv_y]))
+        self.insns.append(
+            insn.Insn_GPR__GPR(opcode, gv_result, gv_arg))
         return gv_result
 
     def _arg_arg_op(self, gv_x, gv_y, opcode):
         gv_result = Var()
         self.insns.append(
-            insn.Insn_GPR__GPR_GPR(opcode,
-                                   gv_result, [gv_x, gv_y]))
+            insn.Insn_GPR__GPR_GPR(opcode, gv_result, [gv_x, gv_y]))
         return gv_result
 
     def _arg_imm_op(self, gv_x, gv_imm, opcode):
+        assert gv_imm.fits_in_immediate()
         gv_result = Var()
         self.insns.append(
-            insn.Insn_GPR__GPR_IMM(opcode,
-                                   gv_result, [gv_x, gv_imm]))
+            insn.Insn_GPR__GPR_IMM(opcode, gv_result, [gv_x, gv_imm]))
         return gv_result
 
+    def _arg_arg_op_with_imm(self, gv_x, gv_y, commutative, opcode, opcodei):
+        if gv_y.fits_in_immediate():
+            return self._arg_imm_op(gv_x, gv_y, opcodei)
+        elif gv_x.fits_in_immediate() and commutative:
+            return self._arg_imm_op(gv_y, gv_x, opcodei)
+        else:
+            return self._arg_arg_op(gv_x, gv_y, opcode)
+
     def _identity(self, gv_arg):
         return gv_arg
 
@@ -759,7 +735,11 @@
 
     ## op_int_neg_ovf(self, gv_arg) XXX
 
-    ## op_int_abs(self, gv_arg):
+    def op_int_abs(self, gv_arg):
+        gv_sign = self._arg_imm_op(gv_arg, self.rgenop.genconst(31), _PPC.srawi)
+        gv_maybe_inverted = self._arg_arg_op(gv_arg, gv_sign, _PPC.xor)
+        return self._arg_arg_op(gv_sign, gv_maybe_inverted, _PPC.subf)
+
     ## op_int_abs_ovf(self, gv_arg):
 
     def op_int_invert(self, gv_arg):
@@ -778,7 +758,12 @@
         return self._arg_arg_op(gv_x, gv_y, _PPC.divw)
 
     ## def op_int_floordiv_zer(self, gv_x, gv_y):
-    ## def op_int_mod(self, gv_x, gv_y):
+
+    def op_int_mod(self, gv_x, gv_y):
+        gv_dividend = self.op_int_floordiv(gv_x, gv_y)
+        gv_z = self.op_int_mul(gv_dividend, gv_y)
+        return self.op_int_sub(gv_x, gv_z)
+
     ## def op_int_mod_zer(self, gv_x, gv_y):
 
     def op_int_lt(self, gv_x, gv_y):
@@ -799,6 +784,16 @@
     def op_int_ge(self, gv_x, gv_y):
         return self._compare('ge', gv_x, gv_y)
 
+    op_char_lt = op_int_lt
+    op_char_le = op_int_le
+    op_char_eq = op_int_eq
+    op_char_ne = op_int_ne
+    op_char_gt = op_int_gt
+    op_char_ge = op_int_ge
+
+    op_unichar_eq = op_int_eq
+    op_unichar_ne = op_int_ne
+
     def op_int_and(self, gv_x, gv_y):
         return self._arg_arg_op(gv_x, gv_y, _PPC.and_)
 
@@ -826,10 +821,15 @@
     op_uint_mul = op_int_mul
 
     def op_uint_floordiv(self, gv_x, gv_y):
-        return self._two_arg_op(gv_x, gv_y, _PPC.divwu)
+        return self._arg_arg_op(gv_x, gv_y, _PPC.divwu)
 
     ## def op_uint_floordiv_zer(self, gv_x, gv_y):
-    ## def op_uint_mod(self, gv_x, gv_y):
+
+    def op_uint_mod(self, gv_x, gv_y):
+        gv_dividend = self.op_uint_floordiv(gv_x, gv_y)
+        gv_z = self.op_uint_mul(gv_dividend, gv_y)
+        return self.op_uint_sub(gv_x, gv_z)
+
     ## def op_uint_mod_zer(self, gv_x, gv_y):
 
     def op_uint_lt(self, gv_x, gv_y):
@@ -850,7 +850,7 @@
     def op_uint_ge(self, gv_x, gv_y):
         return self._compare_u('ge', gv_x, gv_y)
 
-    op_uint_and = op_int_add
+    op_uint_and = op_int_and
     op_uint_or = op_int_or
 
     op_uint_lshift = op_int_lshift
@@ -875,8 +875,9 @@
     ## def op_cast_bool_to_float(self, gv_arg):
     op_cast_char_to_int = _identity
     op_cast_unichar_to_int = _identity
-    ## def op_cast_int_to_char(self, gv_arg):
-    ## def op_cast_int_to_unichar(self, gv_arg):
+    op_cast_int_to_char = _identity
+
+    op_cast_int_to_unichar = _identity
     op_cast_int_to_uint = _identity
     ## def op_cast_int_to_float(self, gv_arg):
     ## def op_cast_int_to_longlong(self, gv_arg):
@@ -956,7 +957,7 @@
     @staticmethod
     @specialize.memo()
     def fieldToken(T, name):
-        return llmemory.offsetof(T, name)
+        return (llmemory.offsetof(T, name), llmemory.sizeof(getattr(T, name)))
 
     @staticmethod
     @specialize.memo()
@@ -989,6 +990,8 @@
     @staticmethod
     @specialize.memo()
     def kindToken(T):
+        if T is lltype.Float:
+            py.test.skip("not implemented: floats in the i386^WPPC back-end")
         return None                   # for now
 
     @staticmethod

Added: pypy/dist/pypy/jit/codegen/ppc/test/test_operation.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/codegen/ppc/test/test_operation.py	Sun Jan 14 15:30:49 2007
@@ -0,0 +1,43 @@
+from pypy.jit.codegen.test.operation_tests import OperationTests
+from pypy.jit.codegen.ppc.rgenop import RPPCGenOp
+from pypy.rpython.memory.lltypelayout import convert_offset_to_int
+from pypy.rlib.objectmodel import specialize
+
+def conv(n):
+    if not isinstance(n, int):
+        n = convert_offset_to_int(n)
+    return n
+
+
+class RGenOpPacked(RPPCGenOp):
+    """Like RPPCGenOp, but produces concrete offsets in the tokens
+    instead of llmemory.offsets.  These numbers may not agree with
+    your C compiler's.
+    """
+
+    @staticmethod
+    @specialize.memo()
+    def fieldToken(T, name):
+        return tuple(map(conv, RPPCGenOp.fieldToken(T, name)))
+
+    @staticmethod
+    @specialize.memo()
+    def arrayToken(A):
+        return tuple(map(conv, RPPCGenOp.arrayToken(A)))
+
+    @staticmethod
+    @specialize.memo()
+    def allocToken(T):
+        return conv(RPPCGenOp.allocToken(T))
+
+    @staticmethod
+    @specialize.memo()
+    def varsizeAllocToken(A):
+        return tuple(map(conv, RPPCGenOp.varsizeAllocToken(A)))
+
+
+class PPCTestMixin(object):
+    RGenOp = RGenOpPacked
+
+class TestOperation(PPCTestMixin, OperationTests):
+    pass

Added: pypy/dist/pypy/jit/codegen/test/operation_tests.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/codegen/test/operation_tests.py	Sun Jan 14 15:30:49 2007
@@ -0,0 +1,224 @@
+from pypy.annotation import model as annmodel
+from pypy.translator.translator import TranslationContext, graphof
+from pypy.jit.codegen import graph2rgenop
+from pypy.rpython.lltypesystem import lltype
+from pypy.rlib.rarithmetic import r_uint
+from ctypes import cast, c_void_p, CFUNCTYPE, c_int, c_float
+from pypy import conftest
+
+class OperationTests(object):
+    @staticmethod
+    def _to_ctypes(t): #limited type support for now
+        if t is float:
+            return c_float
+        return c_int
+
+    def rgen(self, ll_function, argtypes, rettype=int): #XXX get rettype from annotation
+        t = TranslationContext()
+        t.buildannotator().build_types(ll_function, argtypes)
+        t.buildrtyper().specialize()
+        graph = graphof(t, ll_function)
+        if conftest.option.view:
+            graph.show()
+        rgenop = self.RGenOp()
+        self.rgenop = rgenop      # keep this alive!
+        gv_generated = graph2rgenop.compile_graph(rgenop, graph)
+        ctypestypes = [OperationTests._to_ctypes(t) for t in argtypes]
+        fp = cast(c_void_p(gv_generated.value),
+                  CFUNCTYPE(OperationTests._to_ctypes(rettype), *ctypestypes))
+        return fp
+
+    def test_arithmetic(self):
+        for fn in [lambda x, y: x + y,
+                   lambda x, y: x - y,
+                   lambda x, y: x * y,
+                   lambda x, y: x // y,
+                   lambda x, y: x % y,
+                   lambda x, y: x << y,
+                   lambda x, y: x >> y,
+                   lambda x, y: x ^ y,
+                   lambda x, y: x & y,
+                   lambda x, y: x | y,
+                   lambda x, y: -y,
+                   lambda x, y: ~y,
+                   lambda x, y: abs(y),
+                   lambda x, y: abs(-x),
+                   ]:
+            fp = self.rgen(fn, [int, int])
+            assert fp(40, 2) == fn(40, 2)
+            assert fp(25, 3) == fn(25, 3)
+
+    def test_comparison(self):
+        for fn in [lambda x, y: int(x <  y),
+                   lambda x, y: int(x <= y),
+                   lambda x, y: int(x == y),
+                   lambda x, y: int(x != y),
+                   lambda x, y: int(x >  y),
+                   lambda x, y: int(x >= y),
+                   ]:
+            fp = self.rgen(fn, [int, int])
+            assert fp(12, 11) == fn(12, 11)
+            assert fp(12, 12) == fn(12, 12)
+            assert fp(12, 13) == fn(12, 13)
+            assert fp(-12, 11) == fn(-12, 11)
+            assert fp(-12, 12) == fn(-12, 12)
+            assert fp(-12, 13) == fn(-12, 13)
+            assert fp(12, -11) == fn(12, -11)
+            assert fp(12, -12) == fn(12, -12)
+            assert fp(12, -13) == fn(12, -13)
+            assert fp(-12, -11) == fn(-12, -11)
+            assert fp(-12, -12) == fn(-12, -12)
+            assert fp(-12, -13) == fn(-12, -13)
+
+    def test_char_comparison(self):
+        for fn in [lambda x, y: int(chr(x) <  chr(y)),
+                   lambda x, y: int(chr(x) <= chr(y)),
+                   lambda x, y: int(chr(x) == chr(y)),
+                   lambda x, y: int(chr(x) != chr(y)),
+                   lambda x, y: int(chr(x) >  chr(y)),
+                   lambda x, y: int(chr(x) >= chr(y)),
+                   ]:
+            fp = self.rgen(fn, [int, int])
+            assert fp(12, 11) == fn(12, 11)
+            assert fp(12, 12) == fn(12, 12)
+            assert fp(12, 13) == fn(12, 13)
+            assert fp(182, 11) == fn(182, 11)
+            assert fp(182, 12) == fn(182, 12)
+            assert fp(182, 13) == fn(182, 13)
+            assert fp(12, 181) == fn(12, 181)
+            assert fp(12, 182) == fn(12, 182)
+            assert fp(12, 183) == fn(12, 183)
+            assert fp(182, 181) == fn(182, 181)
+            assert fp(182, 182) == fn(182, 182)
+            assert fp(182, 183) == fn(182, 183)
+
+    def test_unichar_comparison(self):
+        for fn in [lambda x, y: int(unichr(x) == unichr(y)),
+                   lambda x, y: int(unichr(x) != unichr(y)),
+                   ]:
+            fp = self.rgen(fn, [int, int])
+            assert fp(12, 11) == fn(12, 11)
+            assert fp(12, 12) == fn(12, 12)
+            assert fp(12, 13) == fn(12, 13)
+            assert fp(53182, 11) == fn(53182, 11)
+            assert fp(53182, 12) == fn(53182, 12)
+            assert fp(53182, 13) == fn(53182, 13)
+            assert fp(12, 53181) == fn(12, 53181)
+            assert fp(12, 53182) == fn(12, 53182)
+            assert fp(12, 53183) == fn(12, 53183)
+            assert fp(53182, 53181) == fn(53182, 53181)
+            assert fp(53182, 53182) == fn(53182, 53182)
+            assert fp(53182, 53183) == fn(53182, 53183)
+
+    def test_char_array(self):
+        A = lltype.GcArray(lltype.Char)
+        def fn(n):
+            a = lltype.malloc(A, 5) #XXX this boils down to rgenop.genop_malloc_varsize() ?
+            a[4] = 'H'
+            a[3] = 'e'
+            a[2] = 'l'
+            a[1] = 'l'
+            a[0] = 'o'
+            return ord(a[n])
+        fp = self.rgen(fn, [int])
+        for i in range(5):
+            assert fp(i) == fn(i)
+
+    def test_char_varsize_array(self):
+        A = lltype.GcArray(lltype.Char)
+        def fn(n):
+            a = lltype.malloc(A, n)
+            a[4] = 'H'
+            a[3] = 'e'
+            a[2] = 'l'
+            a[1] = 'l'
+            a[0] = 'o'
+            return ord(a[n-1])
+        fp = self.rgen(fn, [int])
+        assert fp(5) == fn(5)
+
+    def test_unichar_array(self):
+        A = lltype.GcArray(lltype.UniChar)
+        def fn(n):
+            a = lltype.malloc(A, 5)
+            a[4] = u'H'
+            a[3] = u'e'
+            a[2] = u'l'
+            a[1] = u'l'
+            a[0] = u'o'
+            return ord(a[n])
+        fp = self.rgen(fn, [int])
+        for i in range(5):
+            assert fp(i) == fn(i)
+
+    def test_char_unichar_fields(self):
+        S = lltype.GcStruct('S', ('a', lltype.Char),
+                                 ('b', lltype.Char),
+                                 ('c', lltype.UniChar),
+                                 ('d', lltype.UniChar),
+                                 ('e', lltype.Signed))
+        def fn():
+            s = lltype.malloc(S)
+            s.a = 'A'
+            s.b = 'b'
+            s.c = unichr(0x5a6b)
+            s.d = unichr(0x7c8d)
+            s.e = -1612
+            return ((s.a == 'A') +
+                    (s.b == 'b') +
+                    (s.c == unichr(0x5a6b)) +
+                    (s.d == unichr(0x7c8d)) +
+                    (s.e == -1612))
+        fp = self.rgen(fn, [])
+        res = fp()
+        assert res == 5
+
+    def test_unsigned(self):
+        for fn in [lambda x, y: x + y,
+                   lambda x, y: x - y,
+                   lambda x, y: x * y,
+                   lambda x, y: x // y,
+                   lambda x, y: x % y,
+                   lambda x, y: x << y,
+                   lambda x, y: x >> y,
+                   lambda x, y: x ^ y,
+                   lambda x, y: x & y,
+                   lambda x, y: x | y,
+                   lambda x, y: -y,
+                   lambda x, y: ~y,
+                   ]:
+            fp = self.rgen(fn, [r_uint, r_uint])
+            assert fp(40, 2) == fn(40, 2)
+            assert fp(25, 3) == fn(25, 3)
+
+    def test_float_arithmetic(self):
+        for fn in [lambda x, y: x + y,
+                   lambda x, y: x - y,
+                   lambda x, y: x * y,
+                   lambda x, y: x / y,
+                   #lambda x, y: x % y,  #not used?
+                   lambda x, y: -y,
+                   #lambda x, y: ~y,    #TypeError: bad operand type for unary ~
+                   lambda x, y: abs(y),
+                   lambda x, y: abs(-x),
+                   ]:
+            fp = self.rgen(fn, [float, float], float)
+            assert fp(40.0, 2.0) == fn(40.0, 2.0)
+            assert fp(25.125, 1.5) == fn(25.125, 1.5)
+
+    def test_float_pow(self): #harder test for llvm
+        for fn in [lambda x, y: x ** y,    #not supported in llvm backend
+                   ]:
+            fp = self.rgen(fn, [float, float], float)
+            assert fp(40.0, 2.0) == fn(40.0, 2.0)
+            assert fp(25.125, 1.5) == fn(25.125, 1.5)
+
+    def test_float_cast(self): #because of different rettype
+        for fn in [lambda x: bool(x),
+                   lambda x: bool(x - 2.0),
+                   ]:
+            fp = self.rgen(fn, [float], bool)
+            assert fp(6.0) == fn(6.0)
+            assert fp(2.0) == fn(2.0)
+            assert fp(0.0) == fn(0.0)
+            assert fp(-2.0) == fn(-2.0)



More information about the Pypy-commit mailing list