| 1 |
import py |
| 2 |
import sys, random |
| 3 |
from rpython.rlib.rarithmetic import r_uint, intmask |
| 4 |
from rpython.jit.metainterp.executor import execute, wrap_constant |
| 5 |
from rpython.jit.metainterp.executor import execute_varargs, _execute_arglist |
| 6 |
from rpython.jit.metainterp.resoperation import rop, opname, opclasses,\ |
| 7 |
InputArgInt, InputArgFloat, InputArgRef |
| 8 |
from rpython.jit.metainterp.history import ConstInt, ConstPtr, ConstFloat |
| 9 |
from rpython.jit.metainterp.history import AbstractDescr |
| 10 |
from rpython.jit.metainterp import history |
| 11 |
from rpython.jit.codewriter import longlong |
| 12 |
from rpython.jit.backend.model import AbstractCPU |
| 13 |
from rpython.rtyper.lltypesystem import llmemory, rffi |
| 14 |
|
| 15 |
class FakeDescr(AbstractDescr): |
| 16 |
pass |
| 17 |
|
| 18 |
class FakeCallDescr(FakeDescr): |
| 19 |
def get_result_type(self): |
| 20 |
return history.FLOAT |
| 21 |
|
| 22 |
class FakeFieldDescr(FakeDescr): |
| 23 |
def is_pointer_field(self): |
| 24 |
return False |
| 25 |
def is_float_field(self): |
| 26 |
return True |
| 27 |
|
| 28 |
class FakeArrayDescr(FakeDescr): |
| 29 |
def is_array_of_pointers(self): |
| 30 |
return False |
| 31 |
def is_array_of_floats(self): |
| 32 |
return True |
| 33 |
|
| 34 |
class FakeResultR: |
| 35 |
_TYPE = llmemory.GCREF |
| 36 |
def __init__(self, *args): |
| 37 |
self.fakeargs = args |
| 38 |
|
| 39 |
class FakeMetaInterp: |
| 40 |
pass |
| 41 |
|
| 42 |
class FakeCPU(AbstractCPU): |
| 43 |
supports_floats = True |
| 44 |
|
| 45 |
def bh_new(self, descr): |
| 46 |
return FakeResultR('new', descr) |
| 47 |
|
| 48 |
def bh_arraylen_gc(self, array, descr): |
| 49 |
assert not array |
| 50 |
assert isinstance(descr, FakeDescr) |
| 51 |
return 55 |
| 52 |
|
| 53 |
def bh_setfield_gc_f(self, struct, newvalue, fielddescr): |
| 54 |
self.fakesetfield = (struct, newvalue, fielddescr) |
| 55 |
|
| 56 |
def bh_setarrayitem_gc_f(self, array, index, newvalue, arraydescr): |
| 57 |
self.fakesetarrayitem = (array, index, newvalue, arraydescr) |
| 58 |
|
| 59 |
def bh_call_f(self, func, args_i, args_r, args_f, calldescr): |
| 60 |
self.fakecalled = (func, args_i, args_r, args_f, calldescr) |
| 61 |
return longlong.getfloatstorage(42.5) |
| 62 |
|
| 63 |
def bh_strsetitem(self, string, index, newvalue): |
| 64 |
self.fakestrsetitem = (string, index, newvalue) |
| 65 |
|
| 66 |
|
| 67 |
boxfloat = InputArgFloat.fromfloat |
| 68 |
constfloat = ConstFloat.fromfloat |
| 69 |
|
| 70 |
|
| 71 |
def test_execute(): |
| 72 |
cpu = FakeCPU() |
| 73 |
descr = FakeDescr() |
| 74 |
box = execute(cpu, None, rop.INT_ADD, None, InputArgInt(40), ConstInt(2)) |
| 75 |
assert box == 42 |
| 76 |
box = execute(cpu, None, rop.NEW, descr) |
| 77 |
assert box.fakeargs == ('new', descr) |
| 78 |
|
| 79 |
def test_execute_varargs(): |
| 80 |
cpu = FakeCPU() |
| 81 |
descr = FakeCallDescr() |
| 82 |
argboxes = [InputArgInt(99999), InputArgInt(321), constfloat(2.25), ConstInt(123), |
| 83 |
InputArgRef(), boxfloat(5.5)] |
| 84 |
box = execute_varargs(cpu, FakeMetaInterp(), rop.CALL_F, argboxes, descr) |
| 85 |
assert longlong.getrealfloat(box) == 42.5 |
| 86 |
assert cpu.fakecalled == (99999, [321, 123], |
| 87 |
[ConstPtr.value], |
| 88 |
[longlong.getfloatstorage(2.25), |
| 89 |
longlong.getfloatstorage(5.5)], |
| 90 |
descr) |
| 91 |
|
| 92 |
def test_execute_nonspec(): |
| 93 |
cpu = FakeCPU() |
| 94 |
descr = FakeDescr() |
| 95 |
|
| 96 |
|
| 97 |
argboxes = [InputArgInt(321), ConstInt(123)] |
| 98 |
box = _execute_arglist(cpu, FakeMetaInterp(), rop.CALL_F, |
| 99 |
argboxes, FakeCallDescr()) |
| 100 |
assert longlong.getrealfloat(box) == 42.5 |
| 101 |
|
| 102 |
box = _execute_arglist(cpu, None, rop.NEW, [], descr) |
| 103 |
assert box.fakeargs == ('new', descr) |
| 104 |
|
| 105 |
box1 = InputArgRef() |
| 106 |
box = _execute_arglist(cpu, None, rop.ARRAYLEN_GC, [box1], descr) |
| 107 |
assert box == 55 |
| 108 |
|
| 109 |
box2 = boxfloat(222.2) |
| 110 |
fielddescr = FakeFieldDescr() |
| 111 |
_execute_arglist(cpu, None, rop.SETFIELD_GC, [box1, box2], fielddescr) |
| 112 |
assert cpu.fakesetfield == (box1.getref_base(), box2.getfloatstorage(), |
| 113 |
fielddescr) |
| 114 |
|
| 115 |
box3 = InputArgInt(33) |
| 116 |
arraydescr = FakeArrayDescr() |
| 117 |
_execute_arglist(cpu, None, rop.SETARRAYITEM_GC, [box1, box3, box2], |
| 118 |
arraydescr) |
| 119 |
assert cpu.fakesetarrayitem == (box1.getref_base(), box3.getint(), |
| 120 |
box2.getfloatstorage(), arraydescr) |
| 121 |
|
| 122 |
|
| 123 |
box = _execute_arglist(cpu, None, rop.INT_INVERT, [box3]) |
| 124 |
assert box == ~33 |
| 125 |
|
| 126 |
box = _execute_arglist(cpu, None, rop.INT_LSHIFT, [box3, InputArgInt(3)]) |
| 127 |
assert box == 33 << 3 |
| 128 |
|
| 129 |
_execute_arglist(cpu, None, rop.STRSETITEM, [box1, InputArgInt(3), box3]) |
| 130 |
assert cpu.fakestrsetitem == (box1.getref_base(), 3, box3.getint()) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
def _int_binary_operations(): |
| 135 |
minint = -sys.maxint-1 |
| 136 |
|
| 137 |
|
| 138 |
for opnum, testcases in [ |
| 139 |
(rop.INT_ADD, [(10, -2, 8), |
| 140 |
(-60, -60, -120)]), |
| 141 |
(rop.INT_SUB, [(10, -2, 12), |
| 142 |
(133, 133, 0)]), |
| 143 |
(rop.INT_MUL, [(-6, -3, 18), |
| 144 |
(15, 15, 225)]), |
| 145 |
(rop.INT_AND, [(0xFF00, 0x0FF0, 0x0F00), |
| 146 |
(-111, -111, -111)]), |
| 147 |
(rop.INT_OR, [(0xFF00, 0x0FF0, 0xFFF0), |
| 148 |
(-111, -111, -111)]), |
| 149 |
(rop.INT_XOR, [(0xFF00, 0x0FF0, 0xF0F0), |
| 150 |
(-111, -111, 0)]), |
| 151 |
(rop.INT_LSHIFT, [(10, 4, 10<<4), |
| 152 |
(-5, 2, -20), |
| 153 |
(-5, 0, -5), |
| 154 |
(3, 3, 24)]), |
| 155 |
(rop.INT_RSHIFT, [(-17, 2, -5), |
| 156 |
(19, 1, 9), |
| 157 |
(3, 3, 0)]), |
| 158 |
(rop.UINT_RSHIFT, [(-1, 4, intmask(r_uint(-1) >> r_uint(4))), |
| 159 |
( 1, 4, intmask(r_uint(1) >> r_uint(4))), |
| 160 |
( 3, 3, 0)]), |
| 161 |
(rop.UINT_MUL_HIGH, [(5, 6, 0), |
| 162 |
(0xffff, 0xffff, 0), |
| 163 |
(-1, -1, -2), |
| 164 |
(-1, 123, 122)]), |
| 165 |
]: |
| 166 |
for x, y, z in testcases: |
| 167 |
yield opnum, [x, y], z |
| 168 |
|
| 169 |
def _int_comparison_operations(): |
| 170 |
cpu = FakeCPU() |
| 171 |
random_numbers = [-sys.maxint-1, -1, 0, 1, sys.maxint] |
| 172 |
def pick(): |
| 173 |
r = random.randrange(-99999, 100000) |
| 174 |
if r & 1: |
| 175 |
return r |
| 176 |
else: |
| 177 |
return random_numbers[r % len(random_numbers)] |
| 178 |
minint = -sys.maxint-1 |
| 179 |
for opnum, operation in [ |
| 180 |
(rop.INT_LT, lambda x, y: x < y), |
| 181 |
(rop.INT_LE, lambda x, y: x <= y), |
| 182 |
(rop.INT_EQ, lambda x, y: x == y), |
| 183 |
(rop.INT_NE, lambda x, y: x != y), |
| 184 |
(rop.INT_GT, lambda x, y: x > y), |
| 185 |
(rop.INT_GE, lambda x, y: x >= y), |
| 186 |
(rop.UINT_LT, lambda x, y: r_uint(x) < r_uint(y)), |
| 187 |
(rop.UINT_LE, lambda x, y: r_uint(x) <= r_uint(y)), |
| 188 |
(rop.UINT_GT, lambda x, y: r_uint(x) > r_uint(y)), |
| 189 |
(rop.UINT_GE, lambda x, y: r_uint(x) >= r_uint(y)), |
| 190 |
]: |
| 191 |
for i in range(20): |
| 192 |
x = pick() |
| 193 |
if i == 1: |
| 194 |
y = x |
| 195 |
else: |
| 196 |
y = pick() |
| 197 |
z = int(operation(x, y)) |
| 198 |
yield opnum, [x, y], z |
| 199 |
|
| 200 |
def _int_unary_operations(): |
| 201 |
minint = -sys.maxint-1 |
| 202 |
for opnum, testcases in [ |
| 203 |
(rop.INT_IS_TRUE, [(0, 0), (1, 1), (2, 1), (-1, 1), (minint, 1)]), |
| 204 |
(rop.INT_NEG, [(0, 0), (123, -123), (-23127, 23127)]), |
| 205 |
(rop.INT_INVERT, [(0, ~0), (-1, ~(-1)), (123, ~123)]), |
| 206 |
(rop.INT_IS_ZERO, [(0, 1), (1, 0), (2, 0), (-1, 0), (minint, 0)]), |
| 207 |
]: |
| 208 |
for x, y in testcases: |
| 209 |
yield opnum, [x], y |
| 210 |
|
| 211 |
def get_int_tests(): |
| 212 |
for opnum, args, retvalue in ( |
| 213 |
list(_int_binary_operations()) + |
| 214 |
list(_int_comparison_operations()) + |
| 215 |
list(_int_unary_operations())): |
| 216 |
yield opnum, [InputArgInt(x) for x in args], retvalue |
| 217 |
if len(args) > 1: |
| 218 |
assert len(args) == 2 |
| 219 |
yield opnum, [InputArgInt(args[0]), ConstInt(args[1])], retvalue |
| 220 |
yield opnum, [ConstInt(args[0]), InputArgInt(args[1])], retvalue |
| 221 |
if args[0] == args[1]: |
| 222 |
commonbox = InputArgInt(args[0]) |
| 223 |
yield opnum, [commonbox, commonbox], retvalue |
| 224 |
|
| 225 |
|
| 226 |
def test_int_ops(): |
| 227 |
cpu = FakeCPU() |
| 228 |
for opnum, boxargs, retvalue in get_int_tests(): |
| 229 |
r = _execute_arglist(cpu, None, opnum, boxargs) |
| 230 |
assert r == retvalue |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
def _float_binary_operations(): |
| 235 |
|
| 236 |
|
| 237 |
for opnum, testcases in [ |
| 238 |
(rop.FLOAT_ADD, [(10.5, -2.25, 8.25), |
| 239 |
(5.25, 5.25, 10.5)]), |
| 240 |
(rop.FLOAT_SUB, [(10.5, -2.25, 12.75), |
| 241 |
(5.25, 5.25, 0.0)]), |
| 242 |
(rop.FLOAT_MUL, [(-6.5, -3.5, 22.75), |
| 243 |
(1.5, 1.5, 2.25)]), |
| 244 |
(rop.FLOAT_TRUEDIV, [(118.75, 12.5, 9.5), |
| 245 |
(-6.5, -6.5, 1.0)]), |
| 246 |
]: |
| 247 |
for x, y, z in testcases: |
| 248 |
yield (opnum, [x, y], 'float', z) |
| 249 |
|
| 250 |
def _float_comparison_operations(): |
| 251 |
|
| 252 |
|
| 253 |
for y in [-522.25, 10.125, 22.6]: |
| 254 |
yield (rop.FLOAT_LT, [10.125, y], 'int', 10.125 < y) |
| 255 |
yield (rop.FLOAT_LE, [10.125, y], 'int', 10.125 <= y) |
| 256 |
yield (rop.FLOAT_EQ, [10.125, y], 'int', 10.125 == y) |
| 257 |
yield (rop.FLOAT_NE, [10.125, y], 'int', 10.125 != y) |
| 258 |
yield (rop.FLOAT_GT, [10.125, y], 'int', 10.125 > y) |
| 259 |
yield (rop.FLOAT_GE, [10.125, y], 'int', 10.125 >= y) |
| 260 |
yield (rop.FLOAT_EQ, [0.0, -0.0], 'int', 0.0 == -0.0) |
| 261 |
|
| 262 |
def _float_unary_operations(): |
| 263 |
yield (rop.FLOAT_NEG, [-5.9], 'float', 5.9) |
| 264 |
yield (rop.FLOAT_NEG, [15.9], 'float', -15.9) |
| 265 |
yield (rop.FLOAT_ABS, [-5.9], 'float', 5.9) |
| 266 |
yield (rop.FLOAT_ABS, [15.9], 'float', 15.9) |
| 267 |
yield (rop.CAST_FLOAT_TO_INT, [-5.9], 'int', -5) |
| 268 |
yield (rop.CAST_FLOAT_TO_INT, [5.9], 'int', 5) |
| 269 |
yield (rop.CAST_INT_TO_FLOAT, [123], 'float', 123.0) |
| 270 |
yield (rop.CAST_INT_TO_FLOAT, [-123], 'float', -123.0) |
| 271 |
|
| 272 |
def get_float_tests(cpu): |
| 273 |
if not cpu.supports_floats: |
| 274 |
py.test.skip("requires float support from the backend") |
| 275 |
for opnum, args, rettype, retvalue in ( |
| 276 |
list(_float_binary_operations()) + |
| 277 |
list(_float_comparison_operations()) + |
| 278 |
list(_float_unary_operations())): |
| 279 |
boxargs = [] |
| 280 |
for x in args: |
| 281 |
if isinstance(x, float): |
| 282 |
boxargs.append(boxfloat(x)) |
| 283 |
else: |
| 284 |
boxargs.append(InputArgInt(x)) |
| 285 |
yield opnum, boxargs, rettype, retvalue |
| 286 |
if len(args) > 1: |
| 287 |
assert len(args) == 2 |
| 288 |
yield opnum, [boxfloat(args[0]), wrap_constant(args[1])], rettype, retvalue |
| 289 |
yield opnum, [wrap_constant(args[0]), boxfloat(args[1])], rettype, retvalue |
| 290 |
if (isinstance(args[0], float) and |
| 291 |
isinstance(args[1], float) and |
| 292 |
args[0] == args[1]): |
| 293 |
commonbox = boxfloat(args[0]) |
| 294 |
yield opnum, [commonbox, commonbox], rettype, retvalue |
| 295 |
|
| 296 |
def test_float_ops(): |
| 297 |
cpu = FakeCPU() |
| 298 |
for opnum, boxargs, rettype, retvalue in get_float_tests(cpu): |
| 299 |
res = _execute_arglist(cpu, None, opnum, boxargs) |
| 300 |
if rettype == 'float': |
| 301 |
res = longlong.getrealfloat(res) |
| 302 |
assert res == retvalue |
| 303 |
|
| 304 |
def make_args_for_op(op, a, b): |
| 305 |
n=opname[op] |
| 306 |
if n[0:3] == 'INT' or n[0:4] == 'UINT': |
| 307 |
arg1 = ConstInt(a) |
| 308 |
arg2 = ConstInt(b) |
| 309 |
elif n[0:5] == 'FLOAT': |
| 310 |
arg1 = constfloat(float(a)) |
| 311 |
arg2 = constfloat(float(b)) |
| 312 |
elif n[0:3] == 'PTR': |
| 313 |
arg1 = ConstPtr(rffi.cast(llmemory.GCREF, a)) |
| 314 |
arg2 = ConstPtr(rffi.cast(llmemory.GCREF, b)) |
| 315 |
else: |
| 316 |
raise NotImplementedError( |
| 317 |
"Don't know how to make args for " + n) |
| 318 |
return arg1, arg2 |
| 319 |
|
| 320 |
|
| 321 |
def test_opboolinvers(): |
| 322 |
cpu = FakeCPU() |
| 323 |
for op1 in opclasses: |
| 324 |
if op1 is None or op1.boolinverse == -1: |
| 325 |
continue |
| 326 |
op2 = opclasses[op1.boolinverse].opnum |
| 327 |
op1 = op1.opnum |
| 328 |
for a in (1,2,3): |
| 329 |
for b in (1,2,3): |
| 330 |
arg1, arg2 = make_args_for_op(op1, a, b) |
| 331 |
box1 = execute(cpu, None, op1, None, arg1, arg2) |
| 332 |
box2 = execute(cpu, None, op2, None, arg1, arg2) |
| 333 |
assert box1 == (not box2) |
| 334 |
|
| 335 |
def test_opboolreflex(): |
| 336 |
cpu = FakeCPU() |
| 337 |
for op1 in opclasses: |
| 338 |
if op1 is None or op1.boolreflex == -1: |
| 339 |
continue |
| 340 |
op2 = opclasses[op1.boolreflex].opnum |
| 341 |
op1 = op1.opnum |
| 342 |
for a in (1,2,3): |
| 343 |
for b in (1,2,3): |
| 344 |
arg1, arg2 = make_args_for_op(op1, a, b) |
| 345 |
box1 = execute(cpu, None, op1, None, arg1, arg2) |
| 346 |
box2 = execute(cpu, None, op2, None, arg2, arg1) |
| 347 |
assert box1 == box2 |