| 1 |
import py |
| 2 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi, rstr |
| 3 |
from rpython.jit.metainterp.history import ResOperation, TargetToken,\ |
| 4 |
JitCellToken |
| 5 |
from rpython.jit.metainterp.history import (ConstInt, ConstPtr, Const, |
| 6 |
BasicFailDescr, BasicFinalDescr) |
| 7 |
from rpython.jit.backend.detect_cpu import getcpuclass |
| 8 |
from rpython.jit.backend.x86.arch import WORD, WIN64 |
| 9 |
from rpython.jit.backend.x86.rx86 import fits_in_32bits |
| 10 |
from rpython.jit.backend.llsupport import symbolic |
| 11 |
from rpython.jit.metainterp.resoperation import rop, InputArgInt, InputArgRef |
| 12 |
from rpython.jit.metainterp.executor import execute |
| 13 |
from rpython.jit.backend.test.runner_test import LLtypeBackendTest |
| 14 |
from rpython.jit.tool.oparser import parse |
| 15 |
import ctypes |
| 16 |
|
| 17 |
CPU = getcpuclass() |
| 18 |
|
| 19 |
class FakeStats(object): |
| 20 |
pass |
| 21 |
|
| 22 |
U = LLtypeBackendTest.U |
| 23 |
S = LLtypeBackendTest.S |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class TestX86(LLtypeBackendTest): |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
if WORD == 4: |
| 33 |
add_loop_instructions = ('mov; ' |
| 34 |
'lea; ' |
| 35 |
'add; test; je; jmp;') |
| 36 |
bridge_loop_instructions = 'cmp; jl; jmp;' |
| 37 |
else: |
| 38 |
add_loop_instructions = ('mov; ' |
| 39 |
'nop; ' |
| 40 |
'add; test; je; jmp;') |
| 41 |
bridge_loop_instructions = ( |
| 42 |
'cmp; jl; mov(abs)?; jmp;') |
| 43 |
|
| 44 |
def get_cpu(self): |
| 45 |
cpu = CPU(rtyper=None, stats=FakeStats()) |
| 46 |
cpu.setup_once() |
| 47 |
return cpu |
| 48 |
|
| 49 |
def test_execute_ptr_operation(self): |
| 50 |
cpu = self.cpu |
| 51 |
u_box, _, _ = self.alloc_instance(U) |
| 52 |
u = u_box.getref(lltype.Ptr(U)) |
| 53 |
ofs = cpu.fielddescrof(S, 'value') |
| 54 |
assert self.execute_operation(rop.SETFIELD_GC, |
| 55 |
[u_box, InputArgInt(3)], |
| 56 |
'void', ofs) == None |
| 57 |
assert u.parent.parent.value == 3 |
| 58 |
u.parent.parent.value += 100 |
| 59 |
assert (self.execute_operation(rop.GETFIELD_GC_I, [u_box], 'int', ofs) |
| 60 |
== 103) |
| 61 |
|
| 62 |
def test_unicode(self): |
| 63 |
ofs = symbolic.get_field_token(rstr.UNICODE, 'chars', False)[0] |
| 64 |
u = rstr.mallocunicode(13) |
| 65 |
for i in range(13): |
| 66 |
u.chars[i] = unichr(ord(u'a') + i) |
| 67 |
b = InputArgRef(lltype.cast_opaque_ptr(llmemory.GCREF, u)) |
| 68 |
r = self.execute_operation(rop.UNICODEGETITEM, [b, ConstInt(2)], 'int') |
| 69 |
assert r == ord(u'a') + 2 |
| 70 |
self.execute_operation(rop.UNICODESETITEM, [b, ConstInt(2), |
| 71 |
ConstInt(ord(u'z'))], |
| 72 |
'void') |
| 73 |
assert u.chars[2] == u'z' |
| 74 |
assert u.chars[3] == u'd' |
| 75 |
|
| 76 |
@staticmethod |
| 77 |
def _resbuf(res, item_tp=ctypes.c_long if not WIN64 else ctypes.c_longlong): |
| 78 |
return ctypes.cast(res._obj.intval, ctypes.POINTER(item_tp)) |
| 79 |
|
| 80 |
def test_allocations(self): |
| 81 |
py.test.skip("rewrite or kill") |
| 82 |
from rpython.rtyper.lltypesystem import rstr |
| 83 |
|
| 84 |
allocs = [None] |
| 85 |
all = [] |
| 86 |
orig_new = self.cpu.gc_ll_descr.funcptr_for_new |
| 87 |
def f(size): |
| 88 |
allocs.insert(0, size) |
| 89 |
return orig_new(size) |
| 90 |
|
| 91 |
self.cpu.assembler.setup_once() |
| 92 |
self.cpu.gc_ll_descr.funcptr_for_new = f |
| 93 |
ofs = symbolic.get_field_token(rstr.STR, 'chars', False)[0] |
| 94 |
|
| 95 |
res = self.execute_operation(rop.NEWSTR, [ConstInt(7)], 'ref') |
| 96 |
assert allocs[0] == 7 + ofs + WORD |
| 97 |
resbuf = self._resbuf(res) |
| 98 |
assert resbuf[ofs/WORD] == 7 |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
res = self.execute_operation(rop.NEWSTR, [InputArgInt(7)], 'ref') |
| 103 |
assert allocs[0] == 7 + ofs + WORD |
| 104 |
resbuf = self._resbuf(res) |
| 105 |
assert resbuf[ofs/WORD] == 7 |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
TP = lltype.GcArray(lltype.Signed) |
| 110 |
ofs = symbolic.get_field_token(TP, 'length', False)[0] |
| 111 |
descr = self.cpu.arraydescrof(TP) |
| 112 |
|
| 113 |
res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], |
| 114 |
'ref', descr) |
| 115 |
assert allocs[0] == 10*WORD + ofs + WORD |
| 116 |
resbuf = self._resbuf(res) |
| 117 |
assert resbuf[ofs/WORD] == 10 |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
res = self.execute_operation(rop.NEW_ARRAY, [InputArgInt(10)], |
| 122 |
'ref', descr) |
| 123 |
assert allocs[0] == 10*WORD + ofs + WORD |
| 124 |
resbuf = self._resbuf(res) |
| 125 |
assert resbuf[ofs/WORD] == 10 |
| 126 |
|
| 127 |
def test_stringitems(self): |
| 128 |
from rpython.rtyper.lltypesystem.rstr import STR |
| 129 |
ofs = symbolic.get_field_token(STR, 'chars', False)[0] |
| 130 |
ofs_items = symbolic.get_field_token(STR.chars, 'items', False)[0] |
| 131 |
|
| 132 |
res = self.execute_operation(rop.NEWSTR, [ConstInt(10)], 'ref') |
| 133 |
self.execute_operation(rop.STRSETITEM, [InputArgRef(res), ConstInt(2), ConstInt(ord('d'))], 'void') |
| 134 |
resbuf = self._resbuf(res, ctypes.c_char) |
| 135 |
assert resbuf[ofs + ofs_items + 2] == 'd' |
| 136 |
self.execute_operation(rop.STRSETITEM, [InputArgRef(res), InputArgInt(2), ConstInt(ord('z'))], 'void') |
| 137 |
assert resbuf[ofs + ofs_items + 2] == 'z' |
| 138 |
r = self.execute_operation(rop.STRGETITEM, [InputArgRef(res), InputArgInt(2)], 'int') |
| 139 |
assert r == ord('z') |
| 140 |
|
| 141 |
def test_arrayitems(self): |
| 142 |
TP = lltype.GcArray(lltype.Signed) |
| 143 |
ofs = symbolic.get_field_token(TP, 'length', False)[0] |
| 144 |
itemsofs = symbolic.get_field_token(TP, 'items', False)[0] |
| 145 |
descr = self.cpu.arraydescrof(TP) |
| 146 |
res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], |
| 147 |
'ref', descr) |
| 148 |
resbuf = self._resbuf(res) |
| 149 |
assert resbuf[ofs/WORD] == 10 |
| 150 |
self.execute_operation(rop.SETARRAYITEM_GC, [InputArgRef(res), |
| 151 |
ConstInt(2), InputArgInt(38)], |
| 152 |
'void', descr) |
| 153 |
assert resbuf[itemsofs/WORD + 2] == 38 |
| 154 |
|
| 155 |
self.execute_operation(rop.SETARRAYITEM_GC, [InputArgRef(res), |
| 156 |
InputArgInt(3), InputArgInt(42)], |
| 157 |
'void', descr) |
| 158 |
assert resbuf[itemsofs/WORD + 3] == 42 |
| 159 |
|
| 160 |
r = self.execute_operation(rop.GETARRAYITEM_GC_I, [InputArgRef(res), ConstInt(2)], |
| 161 |
'int', descr) |
| 162 |
assert r == 38 |
| 163 |
r = self.execute_operation(rop.GETARRAYITEM_GC_I, [ConstPtr(res), |
| 164 |
InputArgInt(2)], |
| 165 |
'int', descr) |
| 166 |
assert r == 38 |
| 167 |
r = self.execute_operation(rop.GETARRAYITEM_GC_I, [ConstPtr(res), |
| 168 |
ConstInt(2)], |
| 169 |
'int', descr) |
| 170 |
assert r == 38 |
| 171 |
r = self.execute_operation(rop.GETARRAYITEM_GC_I, [InputArgRef(res), |
| 172 |
InputArgInt(2)], |
| 173 |
'int', descr) |
| 174 |
assert r == 38 |
| 175 |
|
| 176 |
r = self.execute_operation(rop.GETARRAYITEM_GC_I, [InputArgRef(res), InputArgInt(3)], |
| 177 |
'int', descr) |
| 178 |
assert r == 42 |
| 179 |
|
| 180 |
def test_arrayitems_not_int(self): |
| 181 |
TP = lltype.GcArray(lltype.Char) |
| 182 |
ofs = symbolic.get_field_token(TP, 'length', False)[0] |
| 183 |
itemsofs = symbolic.get_field_token(TP, 'items', False)[0] |
| 184 |
descr = self.cpu.arraydescrof(TP) |
| 185 |
res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)], |
| 186 |
'ref', descr) |
| 187 |
resbuf = self._resbuf(res, ctypes.c_char) |
| 188 |
res = InputArgRef(res) |
| 189 |
assert resbuf[ofs] == chr(10) |
| 190 |
for i in range(10): |
| 191 |
self.execute_operation(rop.SETARRAYITEM_GC, [res, |
| 192 |
ConstInt(i), InputArgInt(i)], |
| 193 |
'void', descr) |
| 194 |
for i in range(10): |
| 195 |
assert resbuf[itemsofs + i] == chr(i) |
| 196 |
for i in range(10): |
| 197 |
r = self.execute_operation(rop.GETARRAYITEM_GC_I, [res, |
| 198 |
ConstInt(i)], |
| 199 |
'int', descr) |
| 200 |
assert r == i |
| 201 |
|
| 202 |
def test_getfield_setfield(self): |
| 203 |
TP = lltype.GcStruct('x', ('s', lltype.Signed), |
| 204 |
('i', rffi.INT), |
| 205 |
('f', lltype.Float), |
| 206 |
('u', rffi.USHORT), |
| 207 |
('c1', lltype.Char), |
| 208 |
('c2', lltype.Char), |
| 209 |
('c3', lltype.Char)) |
| 210 |
res = InputArgRef(self.execute_operation(rop.NEW, [], |
| 211 |
'ref', self.cpu.sizeof(TP))) |
| 212 |
ofs_s = self.cpu.fielddescrof(TP, 's') |
| 213 |
ofs_i = self.cpu.fielddescrof(TP, 'i') |
| 214 |
|
| 215 |
ofs_u = self.cpu.fielddescrof(TP, 'u') |
| 216 |
ofsc1 = self.cpu.fielddescrof(TP, 'c1') |
| 217 |
ofsc2 = self.cpu.fielddescrof(TP, 'c2') |
| 218 |
ofsc3 = self.cpu.fielddescrof(TP, 'c3') |
| 219 |
self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(3)], 'void', |
| 220 |
ofs_s) |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
s = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofs_s) |
| 226 |
assert s == 3 |
| 227 |
self.execute_operation(rop.SETFIELD_GC, [res, InputArgInt(3)], 'void', |
| 228 |
ofs_s) |
| 229 |
s = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofs_s) |
| 230 |
assert s == 3 |
| 231 |
|
| 232 |
self.execute_operation(rop.SETFIELD_GC, [res, InputArgInt(1234)], 'void', ofs_i) |
| 233 |
i = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofs_i) |
| 234 |
assert i == 1234 |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(1)], 'void', |
| 239 |
ofsc1) |
| 240 |
self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(3)], 'void', |
| 241 |
ofsc3) |
| 242 |
self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(2)], 'void', |
| 243 |
ofsc2) |
| 244 |
c = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofsc1) |
| 245 |
assert c == 1 |
| 246 |
c = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofsc2) |
| 247 |
assert c == 2 |
| 248 |
c = self.execute_operation(rop.GETFIELD_GC_I, [res], 'int', ofsc3) |
| 249 |
assert c == 3 |
| 250 |
|
| 251 |
def test_bug_setfield_64bit(self): |
| 252 |
if WORD == 4: |
| 253 |
py.test.skip("only for 64 bits") |
| 254 |
TP = lltype.GcStruct('S', ('i', lltype.Signed)) |
| 255 |
ofsi = self.cpu.fielddescrof(TP, 'i') |
| 256 |
for i in range(500): |
| 257 |
p = lltype.malloc(TP) |
| 258 |
addr = rffi.cast(lltype.Signed, p) |
| 259 |
if fits_in_32bits(addr): |
| 260 |
break |
| 261 |
else: |
| 262 |
py.test.skip("cannot get a 32-bit pointer") |
| 263 |
res = ConstPtr(rffi.cast(llmemory.GCREF, addr)) |
| 264 |
self.execute_operation(rop.SETFIELD_RAW, [res, ConstInt(3**33)], |
| 265 |
'void', ofsi) |
| 266 |
assert p.i == 3**33 |
| 267 |
|
| 268 |
def test_getfield_64bit_offset(self): |
| 269 |
if WORD == 4: |
| 270 |
py.test.skip("only for 64 bits") |
| 271 |
TP = lltype.Struct('S', ('i', lltype.Signed)) |
| 272 |
p = lltype.malloc(TP, flavor='raw') |
| 273 |
p.i = 0x123456789ABC |
| 274 |
offset = 3**33 |
| 275 |
val = rffi.cast(lltype.Signed, rffi.cast(lltype.Signed, p) - offset) |
| 276 |
res = self.execute_operation(rop.GC_LOAD_I, |
| 277 |
[InputArgInt(val), |
| 278 |
ConstInt(offset), |
| 279 |
ConstInt(WORD)], |
| 280 |
'int') |
| 281 |
assert res == 0x123456789ABC |
| 282 |
lltype.free(p, flavor='raw') |
| 283 |
|
| 284 |
def test_and_mask_common_patterns(self): |
| 285 |
cases = [8, 16, 24] |
| 286 |
if WORD == 8: |
| 287 |
cases.append(32) |
| 288 |
bigvalue = 0xAAAAAAAAAAAA |
| 289 |
else: |
| 290 |
bigvalue = 0xAAAAAAA |
| 291 |
for i in cases: |
| 292 |
box = InputArgInt(bigvalue) |
| 293 |
res = self.execute_operation(rop.INT_AND, |
| 294 |
[box, ConstInt(2 ** i - 1)], |
| 295 |
'int') |
| 296 |
assert res == bigvalue & (2 ** i - 1) |
| 297 |
|
| 298 |
def test_nullity_with_guard(self): |
| 299 |
allops = [rop.INT_IS_TRUE] |
| 300 |
guards = [rop.GUARD_TRUE, rop.GUARD_FALSE] |
| 301 |
p = lltype.cast_opaque_ptr(llmemory.GCREF, |
| 302 |
lltype.malloc(lltype.GcStruct('x'))) |
| 303 |
nullptr = lltype.nullptr(llmemory.GCREF.TO) |
| 304 |
f = InputArgInt() |
| 305 |
for op in allops: |
| 306 |
for guard in guards: |
| 307 |
if op == rop.INT_IS_TRUE: |
| 308 |
bp = InputArgInt(1) |
| 309 |
n = InputArgInt(0) |
| 310 |
else: |
| 311 |
bp = InputArgRef(p) |
| 312 |
n = InputArgRef(nullptr) |
| 313 |
for b in (bp, n): |
| 314 |
i1 = ResOperation(rop.SAME_AS_I, [ConstInt(1)]) |
| 315 |
f = ResOperation(op, [b]) |
| 316 |
ops = [ |
| 317 |
i1, |
| 318 |
f, |
| 319 |
ResOperation(guard, [f], |
| 320 |
descr=BasicFailDescr()), |
| 321 |
ResOperation(rop.FINISH, [ConstInt(0)], |
| 322 |
descr=BasicFinalDescr()), |
| 323 |
] |
| 324 |
ops[-2].setfailargs([i1]) |
| 325 |
looptoken = JitCellToken() |
| 326 |
self.cpu.compile_loop([b], ops, looptoken) |
| 327 |
deadframe = self.cpu.execute_token(looptoken, b.getint()) |
| 328 |
result = self.cpu.get_int_value(deadframe, 0) |
| 329 |
if guard == rop.GUARD_FALSE: |
| 330 |
assert result == execute(self.cpu, None, |
| 331 |
op, None, b) |
| 332 |
else: |
| 333 |
assert result != execute(self.cpu, None, |
| 334 |
op, None, b) |
| 335 |
|
| 336 |
|
| 337 |
def test_stuff_followed_by_guard(self): |
| 338 |
boxes = [(InputArgInt(1), InputArgInt(0)), |
| 339 |
(InputArgInt(0), InputArgInt(1)), |
| 340 |
(InputArgInt(1), InputArgInt(1)), |
| 341 |
(InputArgInt(-1), InputArgInt(1)), |
| 342 |
(InputArgInt(1), InputArgInt(-1)), |
| 343 |
(ConstInt(1), InputArgInt(0)), |
| 344 |
(ConstInt(0), InputArgInt(1)), |
| 345 |
(ConstInt(1), InputArgInt(1)), |
| 346 |
(ConstInt(-1), InputArgInt(1)), |
| 347 |
(ConstInt(1), InputArgInt(-1)), |
| 348 |
(InputArgInt(1), ConstInt(0)), |
| 349 |
(InputArgInt(0), ConstInt(1)), |
| 350 |
(InputArgInt(1), ConstInt(1)), |
| 351 |
(InputArgInt(-1), ConstInt(1)), |
| 352 |
(InputArgInt(1), ConstInt(-1))] |
| 353 |
guards = [rop.GUARD_FALSE, rop.GUARD_TRUE] |
| 354 |
all = [rop.INT_EQ, rop.INT_NE, rop.INT_LE, rop.INT_LT, rop.INT_GT, |
| 355 |
rop.INT_GE, rop.UINT_GT, rop.UINT_LT, rop.UINT_LE, rop.UINT_GE] |
| 356 |
for a, b in boxes: |
| 357 |
for guard in guards: |
| 358 |
for op in all: |
| 359 |
i1 = ResOperation(rop.SAME_AS_I, [ConstInt(1)]) |
| 360 |
res = ResOperation(op, [a, b]) |
| 361 |
ops = [ |
| 362 |
i1, res, |
| 363 |
ResOperation(guard, [res], |
| 364 |
descr=BasicFailDescr()), |
| 365 |
ResOperation(rop.FINISH, [ConstInt(0)], |
| 366 |
descr=BasicFinalDescr()), |
| 367 |
] |
| 368 |
ops[-2].setfailargs([i1]) |
| 369 |
inputargs = [i for i in (a, b) if not isinstance(i, Const)] |
| 370 |
looptoken = JitCellToken() |
| 371 |
self._prepare_reuse(inputargs, ops) |
| 372 |
self.cpu.compile_loop(inputargs, ops, looptoken) |
| 373 |
inputvalues = [box.getint() for box in inputargs] |
| 374 |
deadframe = self.cpu.execute_token(looptoken, *inputvalues) |
| 375 |
result = self.cpu.get_int_value(deadframe, 0) |
| 376 |
expected = execute(self.cpu, None, op, None, a, b) |
| 377 |
if guard == rop.GUARD_FALSE: |
| 378 |
assert result == expected |
| 379 |
else: |
| 380 |
assert result != expected |
| 381 |
|
| 382 |
def test_compile_bridge_check_profile_info(self): |
| 383 |
py.test.skip("does not work, reinvestigate") |
| 384 |
class FakeProfileAgent(object): |
| 385 |
def __init__(self): |
| 386 |
self.functions = [] |
| 387 |
def native_code_written(self, name, address, size): |
| 388 |
self.functions.append((name, address, size)) |
| 389 |
self.cpu.profile_agent = agent = FakeProfileAgent() |
| 390 |
|
| 391 |
i0 = InputArgInt() |
| 392 |
i1 = InputArgInt() |
| 393 |
i2 = InputArgInt() |
| 394 |
targettoken = TargetToken() |
| 395 |
faildescr1 = BasicFailDescr(1) |
| 396 |
faildescr2 = BasicFailDescr(2) |
| 397 |
looptoken = JitCellToken() |
| 398 |
looptoken.number = 17 |
| 399 |
class FakeString(object): |
| 400 |
def __init__(self, val): |
| 401 |
self.val = val |
| 402 |
|
| 403 |
def _get_str(self): |
| 404 |
return self.val |
| 405 |
|
| 406 |
operations = [ |
| 407 |
ResOperation(rop.LABEL, [i0], None, descr=targettoken), |
| 408 |
ResOperation(rop.DEBUG_MERGE_POINT, [FakeString("hello"), 0, 0], None), |
| 409 |
ResOperation(rop.INT_ADD, [i0, ConstInt(1)], i1), |
| 410 |
ResOperation(rop.INT_LE, [i1, ConstInt(9)], i2), |
| 411 |
ResOperation(rop.GUARD_TRUE, [i2], None, descr=faildescr1), |
| 412 |
ResOperation(rop.JUMP, [i1], None, descr=targettoken), |
| 413 |
] |
| 414 |
inputargs = [i0] |
| 415 |
operations[-2].setfailargs([i1]) |
| 416 |
self.cpu.compile_loop(inputargs, operations, looptoken) |
| 417 |
name, loopaddress, loopsize = agent.functions[0] |
| 418 |
assert name == "Loop # 17: hello (loop counter 0)" |
| 419 |
assert loopaddress <= looptoken._ll_loop_code |
| 420 |
assert loopsize >= 40 |
| 421 |
|
| 422 |
i1b = InputArgInt() |
| 423 |
i3 = InputArgInt() |
| 424 |
bridge = [ |
| 425 |
ResOperation(rop.INT_LE, [i1b, ConstInt(19)], i3), |
| 426 |
ResOperation(rop.GUARD_TRUE, [i3], None, descr=faildescr2), |
| 427 |
ResOperation(rop.DEBUG_MERGE_POINT, [FakeString("bye"), 0, 0], None), |
| 428 |
ResOperation(rop.JUMP, [i1b], None, descr=targettoken), |
| 429 |
] |
| 430 |
bridge[1].setfailargs([i1b]) |
| 431 |
|
| 432 |
self.cpu.compile_bridge(faildescr1, [i1b], bridge, looptoken) |
| 433 |
name, address, size = agent.functions[1] |
| 434 |
assert name == "Bridge # 0: bye (loop counter 1)" |
| 435 |
|
| 436 |
|
| 437 |
assert address >= loopaddress + loopsize |
| 438 |
assert size >= 10 |
| 439 |
|
| 440 |
deadframe = self.cpu.execute_token(looptoken, 2) |
| 441 |
fail = self.cpu.get_latest_descr(deadframe) |
| 442 |
assert fail.identifier == 2 |
| 443 |
res = self.cpu.get_int_value(deadframe, 0) |
| 444 |
assert res == 20 |
| 445 |
|
| 446 |
def test_ops_offset(self): |
| 447 |
from rpython.rlib import debug |
| 448 |
looptoken = JitCellToken() |
| 449 |
targettoken = TargetToken() |
| 450 |
loop = parse(""" |
| 451 |
[i0] |
| 452 |
label(i0, descr=targettoken) |
| 453 |
i1 = int_add(i0, 1) |
| 454 |
i2 = int_le(i1, 9) |
| 455 |
jump(i1, descr=targettoken) |
| 456 |
""", namespace=locals()) |
| 457 |
debug._log = dlog = debug.DebugLog() |
| 458 |
info = self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken) |
| 459 |
ops_offset = info.ops_offset |
| 460 |
debug._log = None |
| 461 |
|
| 462 |
assert ops_offset is looptoken._x86_ops_offset |
| 463 |
|
| 464 |
assert len(ops_offset) == 2 + len(loop.operations) + 1 |
| 465 |
assert (ops_offset[loop.operations[0]] <= |
| 466 |
ops_offset[loop.operations[1]] <= |
| 467 |
ops_offset[loop.operations[2]] <= |
| 468 |
ops_offset[None]) |
| 469 |
|
| 470 |
def test_calling_convention(self, monkeypatch): |
| 471 |
if WORD != 4: |
| 472 |
py.test.skip("32-bit only test") |
| 473 |
from rpython.jit.backend.x86.regloc import eax, edx |
| 474 |
from rpython.jit.backend.x86 import codebuf, callbuilder |
| 475 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 476 |
from rpython.rlib.libffi import types, clibffi |
| 477 |
had_stdcall = hasattr(clibffi, 'FFI_STDCALL') |
| 478 |
if not had_stdcall: |
| 479 |
monkeypatch.setattr(clibffi, 'FFI_STDCALL', 12345, raising=False) |
| 480 |
monkeypatch.setattr(callbuilder, 'stdcall_or_cdecl', True) |
| 481 |
else: |
| 482 |
assert callbuilder.stdcall_or_cdecl |
| 483 |
|
| 484 |
for real_ffi, reported_ffi in [ |
| 485 |
(clibffi.FFI_DEFAULT_ABI, clibffi.FFI_DEFAULT_ABI), |
| 486 |
(clibffi.FFI_STDCALL, clibffi.FFI_DEFAULT_ABI), |
| 487 |
(clibffi.FFI_STDCALL, clibffi.FFI_STDCALL)]: |
| 488 |
cpu = self.cpu |
| 489 |
mc = codebuf.MachineCodeBlockWrapper() |
| 490 |
mc.MOV_rs(eax.value, 4) |
| 491 |
mc.MOV_rs(edx.value, 40) |
| 492 |
mc.SUB_rr(eax.value, edx.value) |
| 493 |
if real_ffi == clibffi.FFI_DEFAULT_ABI: |
| 494 |
mc.RET() |
| 495 |
else: |
| 496 |
mc.RET16_i(40) |
| 497 |
rawstart = mc.materialize(cpu, []) |
| 498 |
|
| 499 |
calldescr = cpu._calldescr_dynamic_for_tests([types.slong] * 10, |
| 500 |
types.slong) |
| 501 |
calldescr.get_call_conv = lambda: reported_ffi |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
funcbox = ConstInt(rawstart) |
| 506 |
i1 = InputArgInt() |
| 507 |
i2 = InputArgInt() |
| 508 |
c = ConstInt(-1) |
| 509 |
faildescr = BasicFailDescr(1) |
| 510 |
cz = ConstInt(0) |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
ops = [ |
| 516 |
ResOperation(rop.CALL_RELEASE_GIL_I, |
| 517 |
[cz, funcbox, i1, c, c, c, c, c, c, c, c, i2], |
| 518 |
descr=calldescr), |
| 519 |
ResOperation(rop.GUARD_NOT_FORCED, [], descr=faildescr), |
| 520 |
|
| 521 |
ResOperation(rop.CALL_RELEASE_GIL_I, |
| 522 |
[cz, funcbox, i1, c, c, c, c, c, c, c, c, i2], |
| 523 |
descr=calldescr), |
| 524 |
ResOperation(rop.GUARD_NOT_FORCED, [], descr=faildescr), |
| 525 |
|
| 526 |
ResOperation(rop.CALL_RELEASE_GIL_I, |
| 527 |
[cz, funcbox, i1, c, c, c, c, c, c, c, c, i2], |
| 528 |
descr=calldescr), |
| 529 |
ResOperation(rop.GUARD_NOT_FORCED, [], descr=faildescr), |
| 530 |
|
| 531 |
ResOperation(rop.CALL_RELEASE_GIL_I, |
| 532 |
[cz, funcbox, i1, c, c, c, c, c, c, c, c, i2], |
| 533 |
descr=calldescr), |
| 534 |
ResOperation(rop.GUARD_NOT_FORCED, [], descr=faildescr), |
| 535 |
] |
| 536 |
i3 = ops[0] |
| 537 |
i4 = ops[2] |
| 538 |
i5 = ops[4] |
| 539 |
i6 = ops[6] |
| 540 |
|
| 541 |
ops += [ |
| 542 |
ResOperation(rop.GUARD_FALSE, [i3], |
| 543 |
descr=BasicFailDescr(0)), |
| 544 |
ResOperation(rop.FINISH, [], |
| 545 |
descr=BasicFinalDescr(1)) |
| 546 |
] |
| 547 |
ops[-2].setfailargs([i3, i4, i5, i6]) |
| 548 |
ops[1].setfailargs([]) |
| 549 |
ops[3].setfailargs([]) |
| 550 |
ops[5].setfailargs([]) |
| 551 |
ops[7].setfailargs([]) |
| 552 |
looptoken = JitCellToken() |
| 553 |
self.cpu.compile_loop([i1, i2], ops, looptoken) |
| 554 |
|
| 555 |
deadframe = self.cpu.execute_token(looptoken, 123450, 123408) |
| 556 |
fail = self.cpu.get_latest_descr(deadframe) |
| 557 |
assert fail.identifier == 0 |
| 558 |
assert self.cpu.get_int_value(deadframe, 0) == 42 |
| 559 |
assert self.cpu.get_int_value(deadframe, 1) == 42 |
| 560 |
assert self.cpu.get_int_value(deadframe, 2) == 42 |
| 561 |
assert self.cpu.get_int_value(deadframe, 3) == 42 |
| 562 |
|
| 563 |
|
| 564 |
class TestDebuggingAssembler(object): |
| 565 |
def setup_method(self, meth): |
| 566 |
self.cpu = CPU(rtyper=None, stats=FakeStats()) |
| 567 |
self.cpu.setup_once() |
| 568 |
|
| 569 |
def test_debugger_on(self): |
| 570 |
from rpython.tool.logparser import parse_log_file, extract_category |
| 571 |
from rpython.rlib import debug |
| 572 |
|
| 573 |
targettoken, preambletoken = TargetToken(), TargetToken() |
| 574 |
loop = """ |
| 575 |
[i0] |
| 576 |
label(i0, descr=preambletoken) |
| 577 |
debug_merge_point('xyz', 0, 0) |
| 578 |
i1 = int_add(i0, 1) |
| 579 |
i2 = int_ge(i1, 10) |
| 580 |
guard_false(i2) [] |
| 581 |
label(i1, descr=targettoken) |
| 582 |
debug_merge_point('xyz', 0, 0) |
| 583 |
i11 = int_add(i1, 1) |
| 584 |
i12 = int_ge(i11, 10) |
| 585 |
guard_false(i12) [] |
| 586 |
jump(i11, descr=targettoken) |
| 587 |
""" |
| 588 |
ops = parse(loop, namespace={'targettoken': targettoken, |
| 589 |
'preambletoken': preambletoken}) |
| 590 |
debug._log = dlog = debug.DebugLog() |
| 591 |
try: |
| 592 |
self.cpu.assembler.set_debug(True) |
| 593 |
looptoken = JitCellToken() |
| 594 |
self.cpu.compile_loop(ops.inputargs, ops.operations, looptoken) |
| 595 |
self.cpu.execute_token(looptoken, 0) |
| 596 |
|
| 597 |
struct = self.cpu.assembler.get_loop_run_counters(0) |
| 598 |
assert struct.i == 1 |
| 599 |
struct = self.cpu.assembler.get_loop_run_counters(1) |
| 600 |
assert struct.i == 1 |
| 601 |
struct = self.cpu.assembler.get_loop_run_counters(2) |
| 602 |
assert struct.i == 9 |
| 603 |
self.cpu.finish_once() |
| 604 |
finally: |
| 605 |
debug._log = None |
| 606 |
l0 = ('debug_print', 'entry -1:1') |
| 607 |
l1 = ('debug_print', preambletoken.repr_of_descr() + ':1') |
| 608 |
l2 = ('debug_print', targettoken.repr_of_descr() + ':9') |
| 609 |
assert ('jit-backend-counts', [l0, l1, l2]) in dlog |