| 1 |
import py |
| 2 |
import pytest |
| 3 |
import platform |
| 4 |
import re |
| 5 |
import sys |
| 6 |
from rpython.jit.metainterp import resoperation as rop |
| 7 |
from rpython.jit.metainterp.history import AbstractDescr, AbstractFailDescr |
| 8 |
from rpython.jit.metainterp.history import ConstInt |
| 9 |
from rpython.jit.backend.llsupport.symbolic import (WORD as INT_WORD, |
| 10 |
SIZEOF_FLOAT as FLOAT_WORD) |
| 11 |
from rpython.jit.backend.llsupport.descr import ArrayDescr |
| 12 |
|
| 13 |
def test_arity_mixins(): |
| 14 |
cases = [ |
| 15 |
(0, rop.NullaryOp), |
| 16 |
(1, rop.UnaryOp), |
| 17 |
(2, rop.BinaryOp), |
| 18 |
(3, rop.TernaryOp), |
| 19 |
(9, rop.N_aryOp) |
| 20 |
] |
| 21 |
|
| 22 |
def test_case(n, cls): |
| 23 |
obj = cls() |
| 24 |
obj.initarglist(range(n)) |
| 25 |
assert obj.getarglist() == range(n) |
| 26 |
for i in range(n): |
| 27 |
obj.setarg(i, i*2) |
| 28 |
assert obj.numargs() == n |
| 29 |
for i in range(n): |
| 30 |
assert obj.getarg(i) == i*2 |
| 31 |
py.test.raises(IndexError, obj.getarg, n+1) |
| 32 |
py.test.raises(IndexError, obj.setarg, n+1, 0) |
| 33 |
|
| 34 |
for n, cls in cases: |
| 35 |
test_case(n, cls) |
| 36 |
|
| 37 |
def test_concrete_classes(): |
| 38 |
cls = rop.opclasses[rop.rop.INT_ADD] |
| 39 |
assert issubclass(cls, rop.PlainResOp) |
| 40 |
assert issubclass(cls, rop.BinaryOp) |
| 41 |
assert cls.getopnum.im_func(cls) == rop.rop.INT_ADD |
| 42 |
|
| 43 |
cls = rop.opclasses[rop.rop.CALL_N] |
| 44 |
assert issubclass(cls, rop.ResOpWithDescr) |
| 45 |
assert issubclass(cls, rop.N_aryOp) |
| 46 |
assert cls.getopnum.im_func(cls) == rop.rop.CALL_N |
| 47 |
|
| 48 |
cls = rop.opclasses[rop.rop.GUARD_TRUE] |
| 49 |
assert issubclass(cls, rop.GuardResOp) |
| 50 |
assert issubclass(cls, rop.UnaryOp) |
| 51 |
assert cls.getopnum.im_func(cls) == rop.rop.GUARD_TRUE |
| 52 |
|
| 53 |
def test_mixins_in_common_base(): |
| 54 |
INT_ADD = rop.opclasses[rop.rop.INT_ADD] |
| 55 |
assert len(INT_ADD.__bases__) == 1 |
| 56 |
BinaryPlainResOp = INT_ADD.__bases__[0] |
| 57 |
assert BinaryPlainResOp.__name__ == 'BinaryPlainResOp' |
| 58 |
assert BinaryPlainResOp.__bases__ == (rop.BinaryOp, rop.PlainResOp) |
| 59 |
INT_SUB = rop.opclasses[rop.rop.INT_SUB] |
| 60 |
assert INT_SUB.__bases__[0] is BinaryPlainResOp |
| 61 |
|
| 62 |
def test_instantiate(): |
| 63 |
a = ConstInt(1) |
| 64 |
b = ConstInt(2) |
| 65 |
op = rop.ResOperation(rop.rop.INT_ADD, [a, b]) |
| 66 |
assert op.getarglist() == [a, b] |
| 67 |
|
| 68 |
|
| 69 |
mydescr = AbstractDescr() |
| 70 |
op = rop.ResOperation(rop.rop.CALL_I, [a, b], descr=mydescr) |
| 71 |
assert op.getarglist() == [a, b] |
| 72 |
assert op.getdescr() is mydescr |
| 73 |
|
| 74 |
|
| 75 |
mydescr = AbstractFailDescr() |
| 76 |
op = rop.ResOperation(rop.rop.GUARD_NO_EXCEPTION, [], descr=mydescr) |
| 77 |
|
| 78 |
|
| 79 |
def test_can_malloc(): |
| 80 |
assert rop.rop.can_malloc(rop.rop.NEW) |
| 81 |
assert rop.rop.can_malloc(rop.rop.CALL_N) |
| 82 |
assert not rop.rop.can_malloc(rop.rop.INT_ADD) |
| 83 |
|
| 84 |
def test_get_deep_immutable_oplist(): |
| 85 |
a = ConstInt(1) |
| 86 |
b = ConstInt(2) |
| 87 |
ops = [rop.ResOperation(rop.rop.INT_ADD, [a, b])] |
| 88 |
newops = rop.get_deep_immutable_oplist(ops) |
| 89 |
py.test.raises(TypeError, "newops.append('foobar')") |
| 90 |
py.test.raises(TypeError, "newops[0] = 'foobar'") |
| 91 |
py.test.raises(AssertionError, "newops[0].setarg(0, 'd')") |
| 92 |
py.test.raises(AssertionError, "newops[0].setdescr('foobar')") |
| 93 |
|
| 94 |
VARI = rop.InputArgInt() |
| 95 |
VARF = rop.InputArgFloat() |
| 96 |
args = [ (rop.rop.INT_SIGNEXT, [VARI, ConstInt(2)], {'from': INT_WORD, 'to': 2, 'cast_to': ('i', 2) }), |
| 97 |
(rop.rop.CAST_FLOAT_TO_INT, [VARF], {'from': 8, 'to': 4}), |
| 98 |
(rop.rop.CAST_SINGLEFLOAT_TO_FLOAT, [VARI], {'from': 4, 'to': 8}), |
| 99 |
(rop.rop.CAST_FLOAT_TO_SINGLEFLOAT, [VARF], {'from': 8, 'to': 4}), |
| 100 |
] |
| 101 |
if not platform.machine().startswith('x86'): |
| 102 |
del args[1] |
| 103 |
args.append((rop.rop.CAST_FLOAT_TO_INT, [VARF], {'from': 8, 'to': 8})) |
| 104 |
@py.test.mark.parametrize('opnum,args,kwargs', args) |
| 105 |
@pytest.mark.skipif("sys.maxint == 2**31-1") |
| 106 |
def test_cast_ops(opnum, args, kwargs): |
| 107 |
op = rop.ResOperation(opnum, args) |
| 108 |
assert op.is_typecast() |
| 109 |
assert op.cast_from_bytesize() == kwargs['from'] |
| 110 |
assert op.cast_to_bytesize() == kwargs['to'] |
| 111 |
if 'cast_to' in kwargs: |
| 112 |
assert op.cast_to() == kwargs['cast_to'] |
| 113 |
del args |
| 114 |
|
| 115 |
def test_unpack_1(): |
| 116 |
op = rop.ResOperation(rop.rop.VEC_UNPACK_I, |
| 117 |
[rop.InputArgVector(), ConstInt(0), ConstInt(1)]) |
| 118 |
assert (op.type, op.is_vector()) == ('i', False) |
| 119 |
op = rop.ResOperation(rop.rop.VEC_UNPACK_I, |
| 120 |
[rop.InputArgVector(), ConstInt(0), ConstInt(2)]) |
| 121 |
assert (op.type, op.is_vector()) == ('i', True) |
| 122 |
|
| 123 |
def test_load_singlefloat(): |
| 124 |
descr = ArrayDescr(8,4, None, 'S', concrete_type='f') |
| 125 |
args = [rop.InputArgInt(), ConstInt(0)] |
| 126 |
baseop = rop.ResOperation(rop.rop.RAW_LOAD_I, args, descr=descr) |
| 127 |
baseop.set_forwarded(rop.VectorizationInfo(baseop)) |
| 128 |
op = rop.VecOperation(rop.rop.VEC_LOAD_I, args + [ConstInt(1), ConstInt(0)], |
| 129 |
baseop, 4, descr=descr) |
| 130 |
assert (op.type, op.datatype, op.bytesize, op.is_vector()) == ('i', 'i', 4, True) |
| 131 |
|
| 132 |
def test_vec_store(): |
| 133 |
descr = ArrayDescr(0,8, None, 'F', concrete_type='f') |
| 134 |
vec = rop.InputArgVector() |
| 135 |
args = [rop.InputArgRef(), ConstInt(0), vec] |
| 136 |
baseop = rop.ResOperation(rop.rop.RAW_STORE, args, descr=descr) |
| 137 |
baseop.set_forwarded(rop.VectorizationInfo(baseop)) |
| 138 |
op = rop.VecOperation(rop.rop.VEC_STORE, args + [ConstInt(1), ConstInt(0)], |
| 139 |
baseop, 2, descr=descr) |
| 140 |
assert (op.type, op.datatype, op.bytesize, op.is_vector()) == ('v', 'v', 8, True) |
| 141 |
|
| 142 |
def test_vec_guard(): |
| 143 |
vec = rop.InputArgVector() |
| 144 |
vec.bytesize = 4 |
| 145 |
vec.type = vec.datatype = 'i' |
| 146 |
vec.sigend = True |
| 147 |
baseop = rop.ResOperation(rop.rop.GUARD_TRUE, [vec]) |
| 148 |
baseop.set_forwarded(rop.VectorizationInfo(baseop)) |
| 149 |
op = rop.VecOperation(rop.rop.VEC_GUARD_TRUE, [vec], baseop, 4) |
| 150 |
assert (op.type, op.datatype, op.bytesize, op.is_vector()) == ('v', 'v', 0, False) |
| 151 |
|
| 152 |
def test_types(): |
| 153 |
op = rop.ResOperation(rop.rop.INT_ADD, [ConstInt(0),ConstInt(1)]) |
| 154 |
op.set_forwarded(rop.VectorizationInfo(op)) |
| 155 |
baseop = rop.ResOperation(rop.rop.CAST_FLOAT_TO_SINGLEFLOAT, [op]) |
| 156 |
baseop.set_forwarded(rop.VectorizationInfo(baseop)) |
| 157 |
op = rop.VecOperation(rop.rop.VEC_CAST_FLOAT_TO_SINGLEFLOAT, [op], baseop, 2) |
| 158 |
assert op.type == 'i' |
| 159 |
assert op.datatype == 'i' |
| 160 |
assert op.bytesize == 4 |