Exemplo n.º 1
0
def test_wrap():
    def _is(box1, box2):
        return box1.__class__ == box2.__class__ and box1.value == box2.value

    p = lltype.malloc(lltype.GcStruct("S"))
    po = lltype.cast_opaque_ptr(llmemory.GCREF, p)
    assert _is(wrap(None, 42), BoxInt(42))
    assert _is(wrap(None, 42.5), boxfloat(42.5))
    assert _is(wrap(None, p), BoxPtr(po))
    assert _is(wrap(None, 42, in_const_box=True), ConstInt(42))
    assert _is(wrap(None, 42.5, in_const_box=True), constfloat(42.5))
    assert _is(wrap(None, p, in_const_box=True), ConstPtr(po))
    if longlong.supports_longlong:
        import sys
        from pypy.rlib.rarithmetic import r_longlong, r_ulonglong

        value = r_longlong(-sys.maxint * 17)
        assert _is(wrap(None, value), BoxFloat(value))
        assert _is(wrap(None, value, in_const_box=True), ConstFloat(value))
        value_unsigned = r_ulonglong(-sys.maxint * 17)
        assert _is(wrap(None, value_unsigned), BoxFloat(value))
    sfval = r_singlefloat(42.5)
    ival = longlong.singlefloat2int(sfval)
    assert _is(wrap(None, sfval), BoxInt(ival))
    assert _is(wrap(None, sfval, in_const_box=True), ConstInt(ival))
Exemplo n.º 2
0
def test_specialize_value():
    assert specialize_value(lltype.Char, 0x41) == '\x41'
    if longlong.supports_longlong:
        import sys
        value = longlong.r_float_storage(sys.maxint*17)
        assert specialize_value(lltype.SignedLongLong, value) == sys.maxint*17
    sfval = r_singlefloat(42.5)
    ival = longlong.singlefloat2int(sfval)
    assert specialize_value(rffi.FLOAT, ival) == sfval
Exemplo n.º 3
0
def test_specialize_value():
    assert specialize_value(lltype.Char, 0x41) == '\x41'
    if longlong.supports_longlong:
        import sys
        value = longlong.r_float_storage(sys.maxint * 17)
        assert specialize_value(lltype.SignedLongLong,
                                value) == sys.maxint * 17
    sfval = r_singlefloat(42.5)
    ival = longlong.singlefloat2int(sfval)
    assert specialize_value(rffi.FLOAT, ival) == sfval
Exemplo n.º 4
0
    def test_call_with_singlefloats(self):
        cpu = self.cpu
        if not cpu.supports_floats or not cpu.supports_singlefloats:
            py.test.skip('requires floats and singlefloats')

        import random
        from pypy.rlib.libffi import types
        from pypy.rlib.rarithmetic import r_singlefloat

        def func(*args):
            res = 0.0
            for i, x in enumerate(args):
                res += (i + 1.1) * float(x)
            return res

        F = lltype.Float
        S = lltype.SingleFloat
        I = lltype.Signed
        floats = [random.random() - 0.5 for i in range(8)]
        singlefloats = [r_singlefloat(random.random() - 0.5) for i in range(8)]
        ints = [random.randrange(-99, 99) for i in range(8)]
        for repeat in range(100):
            args = []
            argvalues = []
            argslist = []
            local_floats = list(floats)
            local_singlefloats = list(singlefloats)
            local_ints = list(ints)
            for i in range(8):
                case = random.randrange(0, 3)
                if case == 0:
                    args.append(F)
                    arg = local_floats.pop()
                    argslist.append(boxfloat(arg))
                elif case == 1:
                    args.append(S)
                    arg = local_singlefloats.pop()
                    argslist.append(BoxInt(longlong.singlefloat2int(arg)))
                else:
                    args.append(I)
                    arg = local_ints.pop()
                    argslist.append(BoxInt(arg))
                argvalues.append(arg)
            FUNC = self.FuncType(args, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            res = self.execute_operation(rop.CALL, [funcbox] + argslist,
                                         'float',
                                         descr=calldescr)
            expected = func(*argvalues)
            assert abs(res.getfloat() - expected) < 0.0001
Exemplo n.º 5
0
    def test_call_with_singlefloats(self):
        cpu = self.cpu
        if not cpu.supports_floats or not cpu.supports_singlefloats:
            py.test.skip('requires floats and singlefloats')

        import random
        from pypy.rlib.libffi import types
        from pypy.rlib.rarithmetic import r_singlefloat

        def func(*args):
            res = 0.0
            for i, x in enumerate(args):
                res += (i + 1.1) * float(x)
            return res

        F = lltype.Float
        S = lltype.SingleFloat
        I = lltype.Signed
        floats = [random.random() - 0.5 for i in range(8)]
        singlefloats = [r_singlefloat(random.random() - 0.5) for i in range(8)]
        ints = [random.randrange(-99, 99) for i in range(8)]
        for repeat in range(100):
            args = []
            argvalues = []
            argslist = []
            local_floats = list(floats)
            local_singlefloats = list(singlefloats)
            local_ints = list(ints)
            for i in range(8):
                case = random.randrange(0, 3)
                if case == 0:
                    args.append(F)
                    arg = local_floats.pop()
                    argslist.append(boxfloat(arg))
                elif case == 1:
                    args.append(S)
                    arg = local_singlefloats.pop()
                    argslist.append(BoxInt(longlong.singlefloat2int(arg)))
                else:
                    args.append(I)
                    arg = local_ints.pop()
                    argslist.append(BoxInt(arg))
                argvalues.append(arg)
            FUNC = self.FuncType(args, F)
            FPTR = self.Ptr(FUNC)
            func_ptr = llhelper(FPTR, func)
            calldescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT,
                                        EffectInfo.MOST_GENERAL)
            funcbox = self.get_funcbox(cpu, func_ptr)

            res = self.execute_operation(rop.CALL,
                                         [funcbox] + argslist,
                                         'float', descr=calldescr)
            expected = func(*argvalues)
            assert abs(res.getfloat() - expected) < 0.0001
Exemplo n.º 6
0
 def emit_const(self, const, kind, allow_short=False):
     value = const.value
     if kind == 'int':
         TYPE = const.concretetype
         if isinstance(TYPE, lltype.Ptr):
             assert TYPE.TO._gckind == 'raw'
             self.see_raw_object(value)
             value = llmemory.cast_ptr_to_adr(value)
             TYPE = llmemory.Address
         if TYPE == llmemory.Address:
             value = heaptracker.adr2int(value)
         if TYPE is lltype.SingleFloat:
             value = longlong.singlefloat2int(value)
         if not isinstance(value,
                           (llmemory.AddressAsInt, ComputedIntSymbolic)):
             value = lltype.cast_primitive(lltype.Signed, value)
             if allow_short:
                 try:
                     short_num = -128 <= value <= 127
                 except TypeError:  # "Symbolics cannot be compared!"
                     short_num = False
                 if short_num:
                     # emit the constant as a small integer
                     self.code.append(chr(value & 0xFF))
                     return True
         constants = self.constants_i
     elif kind == 'ref':
         value = lltype.cast_opaque_ptr(llmemory.GCREF, value)
         constants = self.constants_r
     elif kind == 'float':
         if const.concretetype == lltype.Float:
             value = longlong.getfloatstorage(value)
         else:
             assert longlong.is_longlong(const.concretetype)
             value = rffi.cast(lltype.SignedLongLong, value)
         constants = self.constants_f
     else:
         raise AssemblerError('unimplemented %r in %r' %
                              (const, self.ssareprname))
     key = (kind, Constant(value))
     if key not in self.constants_dict:
         constants.append(value)
         self.constants_dict[key] = 256 - len(constants)
     # emit the constant normally, as one byte that is an index in the
     # list of constants
     self.code.append(chr(self.constants_dict[key]))
     return False
Exemplo n.º 7
0
 def emit_const(self, const, kind, allow_short=False):
     value = const.value
     if kind == 'int':
         TYPE = const.concretetype
         if isinstance(TYPE, lltype.Ptr):
             assert TYPE.TO._gckind == 'raw'
             self.see_raw_object(value)
             value = llmemory.cast_ptr_to_adr(value)
             TYPE = llmemory.Address
         if TYPE == llmemory.Address:
             value = heaptracker.adr2int(value)
         if TYPE is lltype.SingleFloat:
             value = longlong.singlefloat2int(value)
         if not isinstance(value, (llmemory.AddressAsInt,
                                   ComputedIntSymbolic)):
             value = lltype.cast_primitive(lltype.Signed, value)
             if allow_short:
                 try:
                     short_num = -128 <= value <= 127
                 except TypeError:    # "Symbolics cannot be compared!"
                     short_num = False
                 if short_num:
                     # emit the constant as a small integer
                     self.code.append(chr(value & 0xFF))
                     return True
         constants = self.constants_i
     elif kind == 'ref':
         value = lltype.cast_opaque_ptr(llmemory.GCREF, value)
         constants = self.constants_r
     elif kind == 'float':
         if const.concretetype == lltype.Float:
             value = longlong.getfloatstorage(value)
         else:
             assert longlong.is_longlong(const.concretetype)
             value = rffi.cast(lltype.SignedLongLong, value)
         constants = self.constants_f
     else:
         raise AssemblerError('unimplemented %r in %r' %
                              (const, self.ssareprname))
     key = (kind, Constant(value))
     if key not in self.constants_dict:
         constants.append(value)
         self.constants_dict[key] = 256 - len(constants)
     # emit the constant normally, as one byte that is an index in the
     # list of constants
     self.code.append(chr(self.constants_dict[key]))
     return False
Exemplo n.º 8
0
def wrap(cpu, value, in_const_box=False):
    if isinstance(lltype.typeOf(value), lltype.Ptr):
        if lltype.typeOf(value).TO._gckind == 'gc':
            value = lltype.cast_opaque_ptr(llmemory.GCREF, value)
            if in_const_box:
                return history.ConstPtr(value)
            else:
                return history.BoxPtr(value)
        else:
            adr = llmemory.cast_ptr_to_adr(value)
            value = heaptracker.adr2int(adr)
            # fall through to the end of the function
    elif isinstance(lltype.typeOf(value), ootype.OOType):
        value = ootype.cast_to_object(value)
        if in_const_box:
            return history.ConstObj(value)
        else:
            return history.BoxObj(value)
    elif (isinstance(value, float)
          or longlong.is_longlong(lltype.typeOf(value))):
        if isinstance(value, float):
            value = longlong.getfloatstorage(value)
        else:
            value = rffi.cast(lltype.SignedLongLong, value)
        if in_const_box:
            return history.ConstFloat(value)
        else:
            return history.BoxFloat(value)
    elif isinstance(value, str) or isinstance(value, unicode):
        assert len(value) == 1  # must be a character
        value = ord(value)
    elif lltype.typeOf(value) is lltype.SingleFloat:
        value = longlong.singlefloat2int(value)
    else:
        value = intmask(value)
    if in_const_box:
        return history.ConstInt(value)
    else:
        return history.BoxInt(value)
Exemplo n.º 9
0
def wrap(cpu, value, in_const_box=False):
    if isinstance(lltype.typeOf(value), lltype.Ptr):
        if lltype.typeOf(value).TO._gckind == 'gc':
            value = lltype.cast_opaque_ptr(llmemory.GCREF, value)
            if in_const_box:
                return history.ConstPtr(value)
            else:
                return history.BoxPtr(value)
        else:
            adr = llmemory.cast_ptr_to_adr(value)
            value = heaptracker.adr2int(adr)
            # fall through to the end of the function
    elif isinstance(lltype.typeOf(value), ootype.OOType):
        value = ootype.cast_to_object(value)
        if in_const_box:
            return history.ConstObj(value)
        else:
            return history.BoxObj(value)
    elif (isinstance(value, float) or
          longlong.is_longlong(lltype.typeOf(value))):
        if isinstance(value, float):
            value = longlong.getfloatstorage(value)
        else:
            value = rffi.cast(lltype.SignedLongLong, value)
        if in_const_box:
            return history.ConstFloat(value)
        else:
            return history.BoxFloat(value)
    elif isinstance(value, str) or isinstance(value, unicode):
        assert len(value) == 1     # must be a character
        value = ord(value)
    elif lltype.typeOf(value) is lltype.SingleFloat:
        value = longlong.singlefloat2int(value)
    else:
        value = intmask(value)
    if in_const_box:
        return history.ConstInt(value)
    else:
        return history.BoxInt(value)
Exemplo n.º 10
0
def test_wrap():
    def _is(box1, box2):
        return (box1.__class__ == box2.__class__ and box1.value == box2.value)

    p = lltype.malloc(lltype.GcStruct('S'))
    po = lltype.cast_opaque_ptr(llmemory.GCREF, p)
    assert _is(wrap(None, 42), BoxInt(42))
    assert _is(wrap(None, 42.5), boxfloat(42.5))
    assert _is(wrap(None, p), BoxPtr(po))
    assert _is(wrap(None, 42, in_const_box=True), ConstInt(42))
    assert _is(wrap(None, 42.5, in_const_box=True), constfloat(42.5))
    assert _is(wrap(None, p, in_const_box=True), ConstPtr(po))
    if longlong.supports_longlong:
        import sys
        from pypy.rlib.rarithmetic import r_longlong, r_ulonglong
        value = r_longlong(-sys.maxint * 17)
        assert _is(wrap(None, value), BoxFloat(value))
        assert _is(wrap(None, value, in_const_box=True), ConstFloat(value))
        value_unsigned = r_ulonglong(-sys.maxint * 17)
        assert _is(wrap(None, value_unsigned), BoxFloat(value))
    sfval = r_singlefloat(42.5)
    ival = longlong.singlefloat2int(sfval)
    assert _is(wrap(None, sfval), BoxInt(ival))
    assert _is(wrap(None, sfval, in_const_box=True), ConstInt(ival))