| 1 |
import py |
| 2 |
import sys |
| 3 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 4 |
from rpython.jit.metainterp.resume import ( |
| 5 |
ResumeDataVirtualAdder, AbstractResumeDataReader, get_VirtualCache_class, |
| 6 |
ResumeDataBoxReader, tag, TagOverflow, untag, tagged_eq, UNASSIGNED, |
| 7 |
TAGBOX, TAGVIRTUAL, tagged_list_eq, AbstractVirtualInfo, TAGCONST, |
| 8 |
NULLREF, ResumeDataDirectReader, TAGINT, REF, VirtualInfo, VStructInfo, |
| 9 |
VArrayInfoNotClear, VStrPlainInfo, VStrConcatInfo, VStrSliceInfo, |
| 10 |
VUniPlainInfo, VUniConcatInfo, VUniSliceInfo, |
| 11 |
ResumeDataLoopMemo, UNASSIGNEDVIRTUAL, INT, annlowlevel, PENDINGFIELDSP, |
| 12 |
TAG_CONST_OFFSET) |
| 13 |
from rpython.jit.metainterp.resumecode import ( |
| 14 |
unpack_numbering, create_numbering) |
| 15 |
from rpython.jit.metainterp.opencoder import Trace |
| 16 |
|
| 17 |
from rpython.jit.metainterp.optimizeopt import info |
| 18 |
from rpython.jit.metainterp.history import ( |
| 19 |
ConstInt, Const, AbstractDescr, ConstPtr, ConstFloat, IntFrontendOp, |
| 20 |
RefFrontendOp, CONST_NULL) |
| 21 |
from rpython.jit.metainterp.support import ptr2int |
| 22 |
from rpython.jit.metainterp.optimizeopt.test.test_util import LLtypeMixin |
| 23 |
from rpython.jit.metainterp import executor |
| 24 |
from rpython.jit.codewriter import longlong |
| 25 |
from rpython.jit.metainterp.resoperation import ResOperation, rop |
| 26 |
from rpython.rlib.debug import debug_start, debug_stop, debug_print,\ |
| 27 |
have_debug_prints |
| 28 |
from rpython.jit.metainterp.test.strategies import intconsts |
| 29 |
|
| 30 |
from hypothesis import given, strategies |
| 31 |
|
| 32 |
class Storage: |
| 33 |
rd_frame_info_list = None |
| 34 |
rd_numb = None |
| 35 |
rd_consts = [] |
| 36 |
rd_virtuals = None |
| 37 |
rd_pendingfields = None |
| 38 |
|
| 39 |
dummyref = RefFrontendOp._resref |
| 40 |
|
| 41 |
|
| 42 |
class FakeOptimizer(object): |
| 43 |
metainterp_sd = None |
| 44 |
optheap = optrewrite = None |
| 45 |
class jitdriver_sd: |
| 46 |
virtualizable_info = None |
| 47 |
|
| 48 |
def __init__(self, trace=None): |
| 49 |
self.trace = trace |
| 50 |
|
| 51 |
def get_box_replacement(self, op): |
| 52 |
while (op.get_forwarded() is not None and |
| 53 |
not isinstance(op.get_forwarded(), info.AbstractInfo)): |
| 54 |
op = op.get_forwarded() |
| 55 |
return op |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
def dump_storage(storage, liveboxes): |
| 61 |
"For profiling only." |
| 62 |
debug_start("jit-resume") |
| 63 |
return |
| 64 |
if have_debug_prints(): |
| 65 |
debug_print('Log storage', compute_unique_id(storage)) |
| 66 |
frameinfo = storage.rd_frame_info_list |
| 67 |
while frameinfo is not None: |
| 68 |
try: |
| 69 |
jitcodename = frameinfo.jitcode.name |
| 70 |
except AttributeError: |
| 71 |
jitcodename = str(compute_unique_id(frameinfo.jitcode)) |
| 72 |
debug_print('\tjitcode/pc', jitcodename, |
| 73 |
frameinfo.pc, |
| 74 |
'at', compute_unique_id(frameinfo)) |
| 75 |
frameinfo = frameinfo.prev |
| 76 |
numb = storage.rd_numb |
| 77 |
while numb: |
| 78 |
debug_print('\tnumb', str([untag(numb.nums[i]) |
| 79 |
for i in range(len(numb.nums))]), |
| 80 |
'at', compute_unique_id(numb)) |
| 81 |
numb = numb.prev |
| 82 |
for const in storage.rd_consts: |
| 83 |
debug_print('\tconst', const.repr_rpython()) |
| 84 |
for box in liveboxes: |
| 85 |
if box is None: |
| 86 |
debug_print('\tbox', 'None') |
| 87 |
else: |
| 88 |
debug_print('\tbox', box.repr_rpython()) |
| 89 |
if storage.rd_virtuals is not None: |
| 90 |
for virtual in storage.rd_virtuals: |
| 91 |
if virtual is None: |
| 92 |
debug_print('\t\t', 'None') |
| 93 |
else: |
| 94 |
virtual.debug_prints() |
| 95 |
if storage.rd_pendingfields: |
| 96 |
debug_print('\tpending setfields') |
| 97 |
for i in range(len(storage.rd_pendingfields)): |
| 98 |
lldescr = storage.rd_pendingfields[i].lldescr |
| 99 |
num = storage.rd_pendingfields[i].num |
| 100 |
fieldnum = storage.rd_pendingfields[i].fieldnum |
| 101 |
itemindex = storage.rd_pendingfields[i].itemindex |
| 102 |
debug_print("\t\t", str(lldescr), str(untag(num)), str(untag(fieldnum)), itemindex) |
| 103 |
|
| 104 |
debug_stop("jit-resume") |
| 105 |
|
| 106 |
|
| 107 |
def test_tag(): |
| 108 |
assert tag(3, 1) == rffi.r_short(3<<2|1) |
| 109 |
assert tag(-3, 2) == rffi.r_short(-3<<2|2) |
| 110 |
assert tag((1<<13)-1, 3) == rffi.r_short(((1<<15)-1)|3) |
| 111 |
assert tag(-1<<13, 3) == rffi.r_short((-1<<15)|3) |
| 112 |
py.test.raises(AssertionError, tag, 3, 5) |
| 113 |
py.test.raises(TagOverflow, tag, 1<<13, 0) |
| 114 |
py.test.raises(TagOverflow, tag, (1<<13)+1, 0) |
| 115 |
py.test.raises(TagOverflow, tag, (-1<<13)-1, 0) |
| 116 |
py.test.raises(TagOverflow, tag, (-1<<13)-5, 0) |
| 117 |
|
| 118 |
def test_untag(): |
| 119 |
assert untag(tag(3, 1)) == (3, 1) |
| 120 |
assert untag(tag(-3, 2)) == (-3, 2) |
| 121 |
assert untag(tag((1<<13)-1, 3)) == ((1<<13)-1, 3) |
| 122 |
assert untag(tag(-1<<13, 3)) == (-1<<13, 3) |
| 123 |
|
| 124 |
def test_tagged_eq(): |
| 125 |
assert tagged_eq(UNASSIGNED, UNASSIGNED) |
| 126 |
assert not tagged_eq(tag(1, TAGBOX), UNASSIGNED) |
| 127 |
|
| 128 |
def test_tagged_list_eq(): |
| 129 |
assert tagged_list_eq([UNASSIGNED, tag(1, TAGBOX), tag(-2, TAGVIRTUAL)], |
| 130 |
[UNASSIGNED, tag(1, TAGBOX), tag(-2, TAGVIRTUAL)]) |
| 131 |
assert not tagged_list_eq([tag(1, TAGBOX)], [tag(-2, TAGBOX)]) |
| 132 |
assert not tagged_list_eq([tag(1, TAGBOX), tag(-2, TAGBOX)], [tag(1, TAGBOX)]) |
| 133 |
|
| 134 |
def test_vinfo(): |
| 135 |
v1 = AbstractVirtualInfo() |
| 136 |
v1.set_content([1, 2, 4]) |
| 137 |
assert v1.equals([1, 2, 4]) |
| 138 |
assert not v1.equals([1, 2, 6]) |
| 139 |
|
| 140 |
def test_reuse_vinfo(): |
| 141 |
class FakeVInfo(object): |
| 142 |
def set_content(self, fieldnums): |
| 143 |
self.fieldnums = fieldnums |
| 144 |
def equals(self, fieldnums): |
| 145 |
return self.fieldnums == fieldnums |
| 146 |
class FakeVirtualValue(info.AbstractVirtualPtrInfo): |
| 147 |
def visitor_dispatch_virtual_type(self, *args): |
| 148 |
return FakeVInfo() |
| 149 |
modifier = ResumeDataVirtualAdder(None, None, None, None, None) |
| 150 |
v1 = FakeVirtualValue() |
| 151 |
vinfo1 = modifier.make_virtual_info(v1, [1, 2, 4]) |
| 152 |
vinfo2 = modifier.make_virtual_info(v1, [1, 2, 4]) |
| 153 |
assert vinfo1 is vinfo2 |
| 154 |
vinfo3 = modifier.make_virtual_info(v1, [1, 2, 6]) |
| 155 |
assert vinfo3 is not vinfo2 |
| 156 |
vinfo4 = modifier.make_virtual_info(v1, [1, 2, 6]) |
| 157 |
assert vinfo3 is vinfo4 |
| 158 |
|
| 159 |
def setvalue(op, val): |
| 160 |
if op.type == 'i': |
| 161 |
op.setint(val) |
| 162 |
elif op.type == 'r': |
| 163 |
op.setref_base(val) |
| 164 |
elif op.type == 'f': |
| 165 |
op.setfloatstorage(val) |
| 166 |
else: |
| 167 |
assert op.type == 'v' |
| 168 |
|
| 169 |
class MyMetaInterp: |
| 170 |
_already_allocated_resume_virtuals = None |
| 171 |
callinfocollection = None |
| 172 |
|
| 173 |
def __init__(self, cpu=None): |
| 174 |
if cpu is None: |
| 175 |
cpu = LLtypeMixin.cpu |
| 176 |
self.cpu = cpu |
| 177 |
self.staticdata = FakeMetaInterpStaticData() |
| 178 |
self.staticdata.cpu = cpu |
| 179 |
self.trace = [] |
| 180 |
self.framestack = [] |
| 181 |
|
| 182 |
def newframe(self, jitcode): |
| 183 |
frame = FakeFrame(jitcode, -1) |
| 184 |
self.framestack.append(frame) |
| 185 |
return frame |
| 186 |
|
| 187 |
def execute_and_record(self, opnum, descr, *argboxes): |
| 188 |
resvalue = executor.execute(self.cpu, None, opnum, descr, *argboxes) |
| 189 |
if isinstance(resvalue, int): |
| 190 |
op = IntFrontendOp(0, resvalue) |
| 191 |
else: |
| 192 |
op = RefFrontendOp(0, resvalue) |
| 193 |
self.trace.append((opnum, list(argboxes), resvalue, descr)) |
| 194 |
return op |
| 195 |
|
| 196 |
def execute_new_with_vtable(self, descr=None): |
| 197 |
return self.execute_and_record(rop.NEW_WITH_VTABLE, descr) |
| 198 |
|
| 199 |
def execute_new(self, typedescr): |
| 200 |
return self.execute_and_record(rop.NEW, typedescr) |
| 201 |
|
| 202 |
def execute_new_array(self, itemsizedescr, lengthbox): |
| 203 |
return self.execute_and_record(rop.NEW_ARRAY, itemsizedescr, |
| 204 |
lengthbox) |
| 205 |
|
| 206 |
def execute_setfield_gc(self, fielddescr, box, valuebox): |
| 207 |
self.execute_and_record(rop.SETFIELD_GC, fielddescr, box, valuebox) |
| 208 |
|
| 209 |
def execute_setarrayitem_gc(self, arraydescr, arraybox, indexbox, itembox): |
| 210 |
self.execute_and_record(rop.SETARRAYITEM_GC, arraydescr, |
| 211 |
arraybox, indexbox, itembox) |
| 212 |
|
| 213 |
def execute_setinteriorfield_gc(self, descr, array, index, value): |
| 214 |
self.execute_and_record(rop.SETINTERIORFIELD_GC, descr, |
| 215 |
array, index, value) |
| 216 |
|
| 217 |
def execute_raw_store(self, arraydescr, addrbox, offsetbox, valuebox): |
| 218 |
self.execute_and_record(rop.RAW_STORE, arraydescr, |
| 219 |
addrbox, offsetbox, valuebox) |
| 220 |
|
| 221 |
S = lltype.GcStruct('S') |
| 222 |
gcref1 = lltype.cast_opaque_ptr(llmemory.GCREF, lltype.malloc(S)) |
| 223 |
gcref2 = lltype.cast_opaque_ptr(llmemory.GCREF, lltype.malloc(S)) |
| 224 |
gcrefnull = lltype.nullptr(llmemory.GCREF.TO) |
| 225 |
|
| 226 |
class MyCPU: |
| 227 |
def __init__(self, values): |
| 228 |
self.values = values |
| 229 |
|
| 230 |
def get_int_value(self, deadframe, index): |
| 231 |
assert deadframe == "deadframe" |
| 232 |
return self.values[index] |
| 233 |
|
| 234 |
def get_ref_value(self, deadframe, index): |
| 235 |
assert deadframe == "deadframe" |
| 236 |
return self.values[index] |
| 237 |
|
| 238 |
def get_float_value(self, deadframe, index): |
| 239 |
assert deadframe == "deadframe" |
| 240 |
return self.values[index] |
| 241 |
|
| 242 |
class MyBlackholeInterp: |
| 243 |
def __init__(self, ARGS): |
| 244 |
self.written_i = [] |
| 245 |
self.written_r = [] |
| 246 |
self.written_f = [] |
| 247 |
self.ARGS = ARGS |
| 248 |
|
| 249 |
def fake_consume_one_section(self, callback_i, callback_r, callback_f): |
| 250 |
count_i = count_r = count_f = 0 |
| 251 |
for ARG in self.ARGS: |
| 252 |
if ARG == lltype.Signed: |
| 253 |
callback_i(count_i); count_i += 1 |
| 254 |
elif ARG == llmemory.GCREF: |
| 255 |
callback_r(count_r); count_r += 1 |
| 256 |
elif ARG == longlong.FLOATSTORAGE: |
| 257 |
callback_f(count_f); count_f += 1 |
| 258 |
else: |
| 259 |
assert 0 |
| 260 |
|
| 261 |
|
| 262 |
def fake_consume_boxes(self, reader, boxes_i, boxes_r, boxes_f): |
| 263 |
reader.boxes_i = boxes_i |
| 264 |
reader.boxes_r = boxes_r |
| 265 |
reader.boxes_f = boxes_f |
| 266 |
self.fake_consume_one_section(reader._callback_i, reader._callback_r, |
| 267 |
reader._callback_f) |
| 268 |
|
| 269 |
def setarg_i(self, index, value): |
| 270 |
assert index == len(self.written_i) |
| 271 |
self.written_i.append(value) |
| 272 |
|
| 273 |
def setarg_r(self, index, value): |
| 274 |
assert index == len(self.written_r) |
| 275 |
self.written_r.append(value) |
| 276 |
|
| 277 |
def setarg_f(self, index, value): |
| 278 |
assert index == len(self.written_f) |
| 279 |
self.written_f.append(value) |
| 280 |
|
| 281 |
def _next_section(reader, *expected): |
| 282 |
bh = MyBlackholeInterp(map(lltype.typeOf, expected)) |
| 283 |
reader.blackholeinterp = bh |
| 284 |
bh.fake_consume_one_section(reader._callback_i, reader._callback_r, reader._callback_f) |
| 285 |
expected_i = [x for x in expected if lltype.typeOf(x) == lltype.Signed] |
| 286 |
expected_r = [x for x in expected if lltype.typeOf(x) == llmemory.GCREF] |
| 287 |
expected_f = [x for x in expected if lltype.typeOf(x) == |
| 288 |
longlong.FLOATSTORAGE] |
| 289 |
assert bh.written_i == expected_i |
| 290 |
assert bh.written_r == expected_r |
| 291 |
assert bh.written_f == expected_f |
| 292 |
|
| 293 |
|
| 294 |
def Numbering(l): |
| 295 |
return create_numbering([len(l)] + l) |
| 296 |
|
| 297 |
def tagconst(i): |
| 298 |
return tag(i + TAG_CONST_OFFSET, TAGCONST) |
| 299 |
|
| 300 |
def test_simple_read(): |
| 301 |
|
| 302 |
c1, c2, c3 = [ConstInt(111), ConstInt(222), ConstInt(333)] |
| 303 |
storage = Storage() |
| 304 |
storage.rd_consts = [c1, c2, c3] |
| 305 |
numb = Numbering([3, tag(0, TAGBOX), tagconst(0), |
| 306 |
NULLREF, tag(0, TAGBOX), tag(1, TAGBOX)] + |
| 307 |
[tagconst(1), tagconst(2)] + |
| 308 |
[tag(0, TAGBOX), tag(1, TAGBOX), tag(2, TAGBOX)]) |
| 309 |
storage.rd_numb = numb |
| 310 |
|
| 311 |
cpu = MyCPU([42, gcref1, -66]) |
| 312 |
metainterp = MyMetaInterp(cpu) |
| 313 |
reader = ResumeDataDirectReader(metainterp, storage, "deadframe") |
| 314 |
_next_section(reader, 42, 111, gcrefnull, 42, gcref1) |
| 315 |
_next_section(reader, 222, 333) |
| 316 |
_next_section(reader, 42, gcref1, -66) |
| 317 |
|
| 318 |
reader = ResumeDataBoxReader(storage, "deadframe", metainterp) |
| 319 |
bi, br, bf = [None]*3, [None]*2, [None]*0 |
| 320 |
bh = MyBlackholeInterp([lltype.Signed, lltype.Signed, |
| 321 |
llmemory.GCREF, lltype.Signed, |
| 322 |
llmemory.GCREF]) |
| 323 |
bh.fake_consume_boxes(reader, bi, br, bf) |
| 324 |
b1s = reader.liveboxes[0] |
| 325 |
b2s = reader.liveboxes[1] |
| 326 |
assert_same(bi, [b1s, ConstInt(111), b1s]) |
| 327 |
assert_same(br, [ConstPtr(gcrefnull), b2s]) |
| 328 |
bi, br, bf = [None]*2, [None]*0, [None]*0 |
| 329 |
bh = MyBlackholeInterp([lltype.Signed, |
| 330 |
lltype.Signed]) |
| 331 |
bh.fake_consume_boxes(reader, bi, br, bf) |
| 332 |
assert_same(bi, [ConstInt(222), ConstInt(333)]) |
| 333 |
bi, br, bf = [None]*2, [None]*1, [None]*0 |
| 334 |
bh = MyBlackholeInterp([lltype.Signed, llmemory.GCREF, |
| 335 |
lltype.Signed]) |
| 336 |
bh.fake_consume_boxes(reader, bi, br, bf) |
| 337 |
b3s = reader.liveboxes[2] |
| 338 |
assert_same(bi, [b1s, b3s]) |
| 339 |
assert_same(br, [b2s]) |
| 340 |
|
| 341 |
|
| 342 |
def assert_same(list1, list2): |
| 343 |
assert len(list1) == len(list2) |
| 344 |
for b1, b2 in zip(list1, list2): |
| 345 |
assert b1.same_box(b2) |
| 346 |
|
| 347 |
def test_simple_read_tagged_ints(): |
| 348 |
storage = Storage() |
| 349 |
storage.rd_consts = [] |
| 350 |
numb = Numbering([1, tag(100, TAGINT)]) |
| 351 |
storage.rd_numb = numb |
| 352 |
|
| 353 |
cpu = MyCPU([]) |
| 354 |
reader = ResumeDataDirectReader(MyMetaInterp(cpu), storage, "deadframe") |
| 355 |
_next_section(reader, 100) |
| 356 |
|
| 357 |
|
| 358 |
def test_prepare_virtuals(): |
| 359 |
class FakeVinfo(object): |
| 360 |
kind = REF |
| 361 |
def allocate(self, decoder, index): |
| 362 |
s = "allocated" |
| 363 |
decoder.virtuals_cache.set_ptr(index, s) |
| 364 |
return s |
| 365 |
class FakeStorage(object): |
| 366 |
rd_virtuals = [FakeVinfo(), None] |
| 367 |
rd_numb = Numbering([1]) |
| 368 |
rd_consts = [] |
| 369 |
rd_pendingfields = None |
| 370 |
class FakeMetainterp(object): |
| 371 |
_already_allocated_resume_virtuals = None |
| 372 |
cpu = None |
| 373 |
reader = ResumeDataDirectReader(MyMetaInterp(None), FakeStorage(), |
| 374 |
"deadframe") |
| 375 |
cache = reader.force_all_virtuals() |
| 376 |
assert cache.virtuals_ptr_cache == ["allocated", reader.virtual_ptr_default] |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
class FakeResumeDataReader(AbstractResumeDataReader): |
| 381 |
VirtualCache = get_VirtualCache_class('Fake') |
| 382 |
|
| 383 |
def allocate_with_vtable(self, descr): |
| 384 |
return FakeBuiltObject(vtable=descr) |
| 385 |
def allocate_struct(self, typedescr): |
| 386 |
return FakeBuiltObject(typedescr=typedescr) |
| 387 |
def allocate_array(self, length, arraydescr, clear): |
| 388 |
assert not clear |
| 389 |
return FakeBuiltObject(arraydescr=arraydescr, items=[None]*length) |
| 390 |
def setfield(self, struct, fieldnum, descr): |
| 391 |
setattr(struct, descr, fieldnum) |
| 392 |
def setarrayitem_int(self, array, i, fieldnum, arraydescr): |
| 393 |
assert 0 <= i < len(array.items) |
| 394 |
assert arraydescr is array.arraydescr |
| 395 |
array.items[i] = fieldnum |
| 396 |
def allocate_string(self, length): |
| 397 |
return FakeBuiltObject(string=[None]*length) |
| 398 |
def string_setitem(self, string, i, fieldnum): |
| 399 |
value, tag = untag(fieldnum) |
| 400 |
assert tag == TAGINT |
| 401 |
assert 0 <= i < len(string.string) |
| 402 |
string.string[i] = value |
| 403 |
def concat_strings(self, left, right): |
| 404 |
return FakeBuiltObject(strconcat=[left, right]) |
| 405 |
def slice_string(self, str, start, length): |
| 406 |
return FakeBuiltObject(strslice=[str, start, length]) |
| 407 |
def allocate_unicode(self, length): |
| 408 |
return FakeBuiltObject(unistring=[None]*length) |
| 409 |
def unicode_setitem(self, unistring, i, fieldnum): |
| 410 |
value, tag = untag(fieldnum) |
| 411 |
assert tag == TAGINT |
| 412 |
assert 0 <= i < len(unistring.unistring) |
| 413 |
unistring.unistring[i] = value |
| 414 |
def concat_unicodes(self, left, right): |
| 415 |
return FakeBuiltObject(uniconcat=[left, right]) |
| 416 |
def slice_unicode(self, str, start, length): |
| 417 |
return FakeBuiltObject(unislice=[str, start, length]) |
| 418 |
|
| 419 |
class FakeBuiltObject(object): |
| 420 |
def __init__(self, **kwds): |
| 421 |
self.__dict__ = kwds |
| 422 |
def __eq__(self, other): |
| 423 |
return (self.__class__ == other.__class__ and |
| 424 |
self.__dict__ == other.__dict__) |
| 425 |
def __repr__(self): |
| 426 |
return 'FakeBuiltObject(%s)' % ( |
| 427 |
', '.join(['%s=%r' % item for item in self.__dict__.items()])) |
| 428 |
|
| 429 |
class FakeArrayDescr(object): |
| 430 |
def is_array_of_pointers(self): return False |
| 431 |
def is_array_of_floats(self): return False |
| 432 |
|
| 433 |
def test_virtualinfo(): |
| 434 |
info = VirtualInfo(123, ["fielddescr1"]) |
| 435 |
info.fieldnums = [tag(456, TAGINT)] |
| 436 |
reader = FakeResumeDataReader() |
| 437 |
reader._prepare_virtuals([info]) |
| 438 |
cache = reader.force_all_virtuals() |
| 439 |
assert cache.virtuals_ptr_cache == [ |
| 440 |
FakeBuiltObject(vtable=123, fielddescr1=tag(456, TAGINT))] |
| 441 |
|
| 442 |
def test_vstructinfo(): |
| 443 |
info = VStructInfo(124, ["fielddescr1"]) |
| 444 |
info.fieldnums = [tag(456, TAGINT)] |
| 445 |
reader = FakeResumeDataReader() |
| 446 |
reader._prepare_virtuals([info]) |
| 447 |
cache = reader.force_all_virtuals() |
| 448 |
assert cache.virtuals_ptr_cache == [ |
| 449 |
FakeBuiltObject(typedescr=124, fielddescr1=tag(456, TAGINT))] |
| 450 |
|
| 451 |
def test_varrayinfo(): |
| 452 |
arraydescr = FakeArrayDescr() |
| 453 |
info = VArrayInfoNotClear(arraydescr) |
| 454 |
info.fieldnums = [tag(456, TAGINT)] |
| 455 |
reader = FakeResumeDataReader() |
| 456 |
reader._prepare_virtuals([info]) |
| 457 |
assert reader.force_all_virtuals().virtuals_ptr_cache == [ |
| 458 |
FakeBuiltObject(arraydescr=arraydescr, items=[tag(456, TAGINT)])] |
| 459 |
|
| 460 |
def test_vstrplaininfo(): |
| 461 |
info = VStrPlainInfo() |
| 462 |
info.fieldnums = [tag(60, TAGINT)] |
| 463 |
reader = FakeResumeDataReader() |
| 464 |
reader._prepare_virtuals([info]) |
| 465 |
assert reader.force_all_virtuals().virtuals_ptr_cache == [ |
| 466 |
FakeBuiltObject(string=[60])] |
| 467 |
|
| 468 |
def test_vstrconcatinfo(): |
| 469 |
info = VStrConcatInfo() |
| 470 |
info.fieldnums = [tag(10, TAGBOX), tag(20, TAGBOX)] |
| 471 |
reader = FakeResumeDataReader() |
| 472 |
reader._prepare_virtuals([info]) |
| 473 |
assert reader.force_all_virtuals().virtuals_ptr_cache == [ |
| 474 |
FakeBuiltObject(strconcat=info.fieldnums)] |
| 475 |
|
| 476 |
def test_vstrsliceinfo(): |
| 477 |
info = VStrSliceInfo() |
| 478 |
info.fieldnums = [tag(10, TAGBOX), tag(20, TAGBOX), tag(30, TAGBOX)] |
| 479 |
reader = FakeResumeDataReader() |
| 480 |
reader._prepare_virtuals([info]) |
| 481 |
assert reader.force_all_virtuals().virtuals_ptr_cache == [ |
| 482 |
FakeBuiltObject(strslice=info.fieldnums)] |
| 483 |
|
| 484 |
def test_vuniplaininfo(): |
| 485 |
info = VUniPlainInfo() |
| 486 |
info.fieldnums = [tag(60, TAGINT)] |
| 487 |
reader = FakeResumeDataReader() |
| 488 |
reader._prepare_virtuals([info]) |
| 489 |
assert reader.force_all_virtuals().virtuals_ptr_cache == [ |
| 490 |
FakeBuiltObject(unistring=[60])] |
| 491 |
|
| 492 |
def test_vuniconcatinfo(): |
| 493 |
info = VUniConcatInfo() |
| 494 |
info.fieldnums = [tag(10, TAGBOX), tag(20, TAGBOX)] |
| 495 |
reader = FakeResumeDataReader() |
| 496 |
reader._prepare_virtuals([info]) |
| 497 |
assert reader.force_all_virtuals().virtuals_ptr_cache == [ |
| 498 |
FakeBuiltObject(uniconcat=info.fieldnums)] |
| 499 |
|
| 500 |
def test_vunisliceinfo(): |
| 501 |
info = VUniSliceInfo() |
| 502 |
info.fieldnums = [tag(10, TAGBOX), tag(20, TAGBOX), tag(30, TAGBOX)] |
| 503 |
reader = FakeResumeDataReader() |
| 504 |
reader._prepare_virtuals([info]) |
| 505 |
assert reader.force_all_virtuals().virtuals_ptr_cache == [ |
| 506 |
FakeBuiltObject(unislice=info.fieldnums)] |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
class FakeFrame(object): |
| 513 |
parent_resume_position = -1 |
| 514 |
|
| 515 |
def __init__(self, code, pc, *boxes): |
| 516 |
self.jitcode = code |
| 517 |
self.pc = pc |
| 518 |
self._env = list(boxes) |
| 519 |
|
| 520 |
def get_list_of_active_boxes(self, flag): |
| 521 |
return self._env |
| 522 |
|
| 523 |
def setup_resume_at_op(self, pc, exception_target, env): |
| 524 |
self.__init__(self.jitcode, pc, exception_target, *env) |
| 525 |
|
| 526 |
def __eq__(self, other): |
| 527 |
return self.__dict__ == other.__dict__ |
| 528 |
|
| 529 |
def __ne__(self, other): |
| 530 |
return self.__dict__ != other.__dict__ |
| 531 |
|
| 532 |
def __repr__(self): |
| 533 |
return "<FF %s %s %s>" % (self.jitcode, self.pc, self._env) |
| 534 |
|
| 535 |
class FakeJitCode(object): |
| 536 |
def __init__(self, name, index): |
| 537 |
self.name = name |
| 538 |
self.index = index |
| 539 |
|
| 540 |
class FakeMetaInterpStaticData: |
| 541 |
cpu = LLtypeMixin.cpu |
| 542 |
all_descrs = [] |
| 543 |
|
| 544 |
class options: |
| 545 |
failargs_limit = 100 |
| 546 |
|
| 547 |
|
| 548 |
def test_ResumeDataLoopMemo_ints(): |
| 549 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 550 |
tagged = memo.getconst(ConstInt(44)) |
| 551 |
assert untag(tagged) == (44, TAGINT) |
| 552 |
tagged = memo.getconst(ConstInt(-3)) |
| 553 |
assert untag(tagged) == (-3, TAGINT) |
| 554 |
const = ConstInt(5000000) |
| 555 |
tagged = memo.getconst(const) |
| 556 |
index, tagbits = untag(tagged) |
| 557 |
assert tagbits == TAGCONST |
| 558 |
assert memo.consts[index - TAG_CONST_OFFSET] is const |
| 559 |
tagged = memo.getconst(ConstInt(5000000)) |
| 560 |
index2, tagbits = untag(tagged) |
| 561 |
assert tagbits == TAGCONST |
| 562 |
assert index2 == index |
| 563 |
|
| 564 |
demo55 = lltype.malloc(LLtypeMixin.NODE) |
| 565 |
demo55o = lltype.cast_opaque_ptr(llmemory.GCREF, demo55) |
| 566 |
demo66 = lltype.malloc(LLtypeMixin.NODE) |
| 567 |
demo66o = lltype.cast_opaque_ptr(llmemory.GCREF, demo66) |
| 568 |
|
| 569 |
def test_ResumeDataLoopMemo_refs(): |
| 570 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 571 |
const = ConstPtr(demo55o) |
| 572 |
tagged = memo.getconst(const) |
| 573 |
index, tagbits = untag(tagged) |
| 574 |
assert tagbits == TAGCONST |
| 575 |
assert memo.consts[index - TAG_CONST_OFFSET] is const |
| 576 |
tagged = memo.getconst(ConstPtr(demo55o)) |
| 577 |
index2, tagbits = untag(tagged) |
| 578 |
assert tagbits == TAGCONST |
| 579 |
assert index2 == index |
| 580 |
tagged = memo.getconst(ConstPtr(demo66o)) |
| 581 |
index3, tagbits = untag(tagged) |
| 582 |
assert tagbits == TAGCONST |
| 583 |
assert index3 != index |
| 584 |
tagged = memo.getconst(CONST_NULL) |
| 585 |
assert tagged == NULLREF |
| 586 |
|
| 587 |
def test_ResumeDataLoopMemo_other(): |
| 588 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 589 |
const = ConstFloat(longlong.getfloatstorage(-1.0)) |
| 590 |
tagged = memo.getconst(const) |
| 591 |
index, tagbits = untag(tagged) |
| 592 |
assert tagbits == TAGCONST |
| 593 |
assert memo.consts[index - TAG_CONST_OFFSET] is const |
| 594 |
|
| 595 |
class Frame(object): |
| 596 |
def __init__(self, jitcode, pc, boxes): |
| 597 |
self.jitcode = jitcode |
| 598 |
self.pc = pc |
| 599 |
self.boxes = boxes |
| 600 |
|
| 601 |
def get_list_of_active_boxes(self, flag, new_array, encode, after_residual_call=False): |
| 602 |
a = new_array(len(self.boxes)) |
| 603 |
for i, box in enumerate(self.boxes): |
| 604 |
encode(box) |
| 605 |
return a |
| 606 |
|
| 607 |
def test_ResumeDataLoopMemo_number(): |
| 608 |
b1, b2, b3, b4, b5 = [IntFrontendOp(0, 0), IntFrontendOp(1, 0), IntFrontendOp(2, 0), |
| 609 |
RefFrontendOp(3, dummyref), RefFrontendOp(4, dummyref)] |
| 610 |
c1, c2, c3, c4 = [ConstInt(1), ConstInt(2), ConstInt(3), ConstInt(4)] |
| 611 |
|
| 612 |
env = [b1, c1, b2, b1, c2] |
| 613 |
metainterp_sd = FakeMetaInterpStaticData() |
| 614 |
t = Trace([b1, b2, b3, b4, b5], metainterp_sd) |
| 615 |
env1 = [c3, b3, b1, c1] |
| 616 |
t.append_int(0) |
| 617 |
snap1 = t.create_top_snapshot(Frame(FakeJitCode("jitcode", 0), 2, env1), [], []) |
| 618 |
snap = t.create_snapshot(Frame(FakeJitCode("jitcode", 0), 0, env), is_last=True) |
| 619 |
|
| 620 |
env2 = [c3, b3, b1, c3] |
| 621 |
env3 = [c3, b3, b1, c3] |
| 622 |
env4 = [c3, b4, b1, c3] |
| 623 |
env5 = [b1, b4, b5] |
| 624 |
|
| 625 |
memo = ResumeDataLoopMemo(metainterp_sd) |
| 626 |
|
| 627 |
iter = t.get_iter() |
| 628 |
b1, b2, b3, b4, b5 = iter.inputargs |
| 629 |
numb_state = memo.number(snap1, iter) |
| 630 |
numb = numb_state.create_numbering() |
| 631 |
assert numb_state.num_virtuals == 0 |
| 632 |
|
| 633 |
assert numb_state.liveboxes == {b1: tag(0, TAGBOX), b2: tag(1, TAGBOX), |
| 634 |
b3: tag(2, TAGBOX)} |
| 635 |
base = [0, 0, tag(0, TAGBOX), tag(1, TAGINT), |
| 636 |
tag(1, TAGBOX), tag(0, TAGBOX), tag(2, TAGINT)] |
| 637 |
|
| 638 |
assert unpack_numbering(numb) == [17, 0, 0, 0] + base + [0, 2, tag(3, TAGINT), tag(2, TAGBOX), |
| 639 |
tag(0, TAGBOX), tag(1, TAGINT)] |
| 640 |
t.append_int(0) |
| 641 |
snap2 = t.create_top_snapshot(Frame(FakeJitCode("jitcode", 0), 2, env2), |
| 642 |
[], []) |
| 643 |
t.snapshot_add_prev(snap) |
| 644 |
|
| 645 |
numb_state2 = memo.number(snap2, iter) |
| 646 |
numb2 = numb_state2.create_numbering() |
| 647 |
assert numb_state2.num_virtuals == 0 |
| 648 |
|
| 649 |
assert numb_state2.liveboxes == {b1: tag(0, TAGBOX), b2: tag(1, TAGBOX), |
| 650 |
b3: tag(2, TAGBOX)} |
| 651 |
assert numb_state2.liveboxes is not numb_state.liveboxes |
| 652 |
assert unpack_numbering(numb2) == [17, 0, 0, 0] + base + [0, 2, tag(3, TAGINT), tag(2, TAGBOX), |
| 653 |
tag(0, TAGBOX), tag(3, TAGINT)] |
| 654 |
|
| 655 |
t.append_int(0) |
| 656 |
snap3 = t.create_top_snapshot(Frame(FakeJitCode("jitcode", 0), 2, []), |
| 657 |
[], env3) |
| 658 |
t.snapshot_add_prev(snap) |
| 659 |
|
| 660 |
class FakeVirtualInfo(info.AbstractVirtualPtrInfo): |
| 661 |
def __init__(self, virt): |
| 662 |
self._is_virtual = virt |
| 663 |
|
| 664 |
def is_virtual(self): |
| 665 |
return self._is_virtual |
| 666 |
|
| 667 |
|
| 668 |
b3.set_forwarded(c4) |
| 669 |
numb_state3 = memo.number(snap3, iter) |
| 670 |
numb3 = numb_state3.create_numbering() |
| 671 |
assert numb_state3.num_virtuals == 0 |
| 672 |
|
| 673 |
assert numb_state3.liveboxes == {b1: tag(0, TAGBOX), b2: tag(1, TAGBOX)} |
| 674 |
assert unpack_numbering(numb3) == ([17, 0, 0, 2, tag(3, TAGINT), tag(4, TAGINT), |
| 675 |
tag(0, TAGBOX), tag(3, TAGINT)] + |
| 676 |
base + [0, 2]) |
| 677 |
|
| 678 |
|
| 679 |
t.append_int(0) |
| 680 |
snap4 = t.create_top_snapshot(Frame(FakeJitCode("jitcode", 0), 2, []), |
| 681 |
[], env4) |
| 682 |
t.snapshot_add_prev(snap) |
| 683 |
|
| 684 |
b4.set_forwarded(FakeVirtualInfo(True)) |
| 685 |
numb_state4 = memo.number(snap4, iter) |
| 686 |
numb4 = numb_state4.create_numbering() |
| 687 |
assert numb_state4.num_virtuals == 1 |
| 688 |
|
| 689 |
assert numb_state4.liveboxes == {b1: tag(0, TAGBOX), b2: tag(1, TAGBOX), |
| 690 |
b4: tag(0, TAGVIRTUAL)} |
| 691 |
assert unpack_numbering(numb4) == [17, 0, 0, 2, tag(3, TAGINT), tag(0, TAGVIRTUAL), |
| 692 |
tag(0, TAGBOX), tag(3, TAGINT)] + base + [0, 2] |
| 693 |
|
| 694 |
t.append_int(0) |
| 695 |
snap5 = t.create_top_snapshot(Frame(FakeJitCode("jitcode", 0), 0, []), |
| 696 |
env5, []) |
| 697 |
snap4 = t.create_snapshot(Frame(FakeJitCode("jitcode", 2), 1, env4)) |
| 698 |
t.snapshot_add_prev(snap) |
| 699 |
|
| 700 |
b4.set_forwarded(FakeVirtualInfo(True)) |
| 701 |
b5.set_forwarded(FakeVirtualInfo(True)) |
| 702 |
numb_state5 = memo.number(snap5, iter) |
| 703 |
numb5 = numb_state5.create_numbering() |
| 704 |
assert numb_state5.num_virtuals == 2 |
| 705 |
|
| 706 |
assert numb_state5.liveboxes == {b1: tag(0, TAGBOX), b2: tag(1, TAGBOX), |
| 707 |
b4: tag(1, TAGVIRTUAL), b5: tag(0, TAGVIRTUAL)} |
| 708 |
assert unpack_numbering(numb5) == [22, 0, |
| 709 |
3, tag(0, TAGVIRTUAL), tag(0, TAGBOX), tag(1, TAGVIRTUAL), |
| 710 |
0] + base + [ |
| 711 |
2, 1, tag(3, TAGINT), tag(1, TAGVIRTUAL), tag(0, TAGBOX), tag(3, TAGINT) |
| 712 |
] + [0, 0] |
| 713 |
|
| 714 |
@given(strategies.lists( |
| 715 |
strategies.builds(IntFrontendOp, strategies.just(0), strategies.just(1)) | intconsts, |
| 716 |
min_size=1)) |
| 717 |
def test_ResumeDataLoopMemo_random(lst): |
| 718 |
inpargs = [box for box in lst if not isinstance(box, Const)] |
| 719 |
metainterp_sd = FakeMetaInterpStaticData() |
| 720 |
t = Trace(inpargs, metainterp_sd) |
| 721 |
t.append_int(0) |
| 722 |
i = t.get_iter() |
| 723 |
t.create_top_snapshot(Frame(FakeJitCode("", 0), 0, lst), [], [], is_last=True) |
| 724 |
memo = ResumeDataLoopMemo(metainterp_sd) |
| 725 |
numb_state = memo.number(0, i) |
| 726 |
numb = numb_state.create_numbering() |
| 727 |
l = unpack_numbering(numb) |
| 728 |
assert l[0] == len(l) |
| 729 |
assert l[1] == 0 |
| 730 |
assert l[1] == 0 |
| 731 |
assert l[2] == 0 |
| 732 |
assert l[3] == 0 |
| 733 |
assert l[4] == 0 |
| 734 |
mapping = dict(zip(inpargs, i.inputargs)) |
| 735 |
for i, item in enumerate(lst): |
| 736 |
v, tag = untag(l[i + 6]) |
| 737 |
if tag == TAGBOX: |
| 738 |
assert l[i + 6] == numb_state.liveboxes[mapping[item]] |
| 739 |
elif tag == TAGCONST: |
| 740 |
assert memo.consts[v].getint() == item.getint() |
| 741 |
elif tag == TAGINT: |
| 742 |
assert v == item.getint() |
| 743 |
|
| 744 |
def test_ResumeDataLoopMemo_number_boxes(): |
| 745 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 746 |
b1, b2 = [IntFrontendOp(0, 0), IntFrontendOp(0, 0)] |
| 747 |
assert memo.num_cached_boxes() == 0 |
| 748 |
boxes = [] |
| 749 |
num = memo.assign_number_to_box(b1, boxes) |
| 750 |
assert num == -1 |
| 751 |
assert boxes == [b1] |
| 752 |
assert memo.num_cached_boxes() == 1 |
| 753 |
boxes = [None] |
| 754 |
num = memo.assign_number_to_box(b1, boxes) |
| 755 |
assert num == -1 |
| 756 |
assert boxes == [b1] |
| 757 |
num = memo.assign_number_to_box(b2, boxes) |
| 758 |
assert num == -2 |
| 759 |
assert boxes == [b1, b2] |
| 760 |
|
| 761 |
assert memo.num_cached_boxes() == 2 |
| 762 |
boxes = [None, None] |
| 763 |
num = memo.assign_number_to_box(b2, boxes) |
| 764 |
assert num == -2 |
| 765 |
assert boxes == [None, b2] |
| 766 |
num = memo.assign_number_to_box(b1, boxes) |
| 767 |
assert num == -1 |
| 768 |
assert boxes == [b1, b2] |
| 769 |
|
| 770 |
memo.clear_box_virtual_numbers() |
| 771 |
assert memo.num_cached_boxes() == 0 |
| 772 |
|
| 773 |
def test_ResumeDataLoopMemo_number_virtuals(): |
| 774 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 775 |
b1, b2 = [IntFrontendOp(0, 0), IntFrontendOp(0, 0)] |
| 776 |
assert memo.num_cached_virtuals() == 0 |
| 777 |
num = memo.assign_number_to_virtual(b1) |
| 778 |
assert num == -1 |
| 779 |
assert memo.num_cached_virtuals() == 1 |
| 780 |
num = memo.assign_number_to_virtual(b1) |
| 781 |
assert num == -1 |
| 782 |
num = memo.assign_number_to_virtual(b2) |
| 783 |
assert num == -2 |
| 784 |
|
| 785 |
assert memo.num_cached_virtuals() == 2 |
| 786 |
num = memo.assign_number_to_virtual(b2) |
| 787 |
assert num == -2 |
| 788 |
num = memo.assign_number_to_virtual(b1) |
| 789 |
assert num == -1 |
| 790 |
|
| 791 |
memo.clear_box_virtual_numbers() |
| 792 |
assert memo.num_cached_virtuals() == 0 |
| 793 |
|
| 794 |
def test_register_virtual_fields(): |
| 795 |
b1, b2 = IntFrontendOp(0, 0), IntFrontendOp(1, 0) |
| 796 |
vbox = RefFrontendOp(2, dummyref) |
| 797 |
modifier = ResumeDataVirtualAdder(FakeOptimizer(), None, None, None, None) |
| 798 |
modifier.liveboxes_from_env = {} |
| 799 |
modifier.liveboxes = {} |
| 800 |
modifier.vfieldboxes = {} |
| 801 |
modifier.register_virtual_fields(vbox, [b1, b2]) |
| 802 |
assert modifier.liveboxes == {vbox: UNASSIGNEDVIRTUAL, b1: UNASSIGNED, |
| 803 |
b2: UNASSIGNED} |
| 804 |
assert modifier.vfieldboxes == {vbox: [b1, b2]} |
| 805 |
|
| 806 |
modifier = ResumeDataVirtualAdder(FakeOptimizer(), None, None, None, None) |
| 807 |
modifier.liveboxes_from_env = {vbox: tag(0, TAGVIRTUAL)} |
| 808 |
modifier.liveboxes = {} |
| 809 |
modifier.vfieldboxes = {} |
| 810 |
modifier.register_virtual_fields(vbox, [b1, b2, vbox]) |
| 811 |
assert modifier.liveboxes == {b1: UNASSIGNED, b2: UNASSIGNED, |
| 812 |
vbox: tag(0, TAGVIRTUAL)} |
| 813 |
assert modifier.vfieldboxes == {vbox: [b1, b2, vbox]} |
| 814 |
|
| 815 |
def _resume_remap(liveboxes, expected, *newvalues): |
| 816 |
newboxes = [] |
| 817 |
for box in liveboxes: |
| 818 |
i = expected.index(box) |
| 819 |
newboxes.append(newvalues[i]) |
| 820 |
assert len(newboxes) == len(expected) |
| 821 |
return newboxes |
| 822 |
|
| 823 |
def make_storage(b1, b2, b3): |
| 824 |
t = Trace([box for box in [b1, b2, b3] if not isinstance(box, Const)], |
| 825 |
FakeMetaInterpStaticData()) |
| 826 |
t.append_int(0) |
| 827 |
storage = Storage() |
| 828 |
snap3 = t.create_top_snapshot(Frame(FakeJitCode("code1", 21), 22, [b1, b2, b3]), [], []) |
| 829 |
snap2 = t.create_snapshot(Frame(FakeJitCode("code2", 31), 32, [ConstInt(2), ConstInt(3)])) |
| 830 |
snap1 = t.create_snapshot(Frame(FakeJitCode("code3", 41), 42, [b1, ConstInt(1), b1, b2]), is_last=True) |
| 831 |
storage.rd_resume_position = 0 |
| 832 |
return storage, t |
| 833 |
|
| 834 |
def test_virtual_adder_int_constants(): |
| 835 |
b1s, b2s, b3s = [ConstInt(sys.maxint), ConstInt(2**16), ConstInt(-65)] |
| 836 |
storage, t = make_storage(b1s, b2s, b3s) |
| 837 |
metainterp_sd = FakeMetaInterpStaticData() |
| 838 |
memo = ResumeDataLoopMemo(metainterp_sd) |
| 839 |
i = t.get_iter() |
| 840 |
modifier = ResumeDataVirtualAdder(FakeOptimizer(i), storage, storage, i, memo) |
| 841 |
liveboxes = modifier.finish() |
| 842 |
cpu = MyCPU([]) |
| 843 |
reader = ResumeDataDirectReader(MyMetaInterp(cpu), storage, "deadframe") |
| 844 |
reader.consume_vref_and_vable(None, None, None) |
| 845 |
reader.resumecodereader.jump(2) |
| 846 |
_next_section(reader, sys.maxint, 1, sys.maxint, 2**16) |
| 847 |
reader.resumecodereader.jump(2) |
| 848 |
_next_section(reader, 2, 3) |
| 849 |
reader.resumecodereader.jump(2) |
| 850 |
_next_section(reader, sys.maxint, 2**16, -65) |
| 851 |
|
| 852 |
def test_virtual_adder_memo_const_sharing(): |
| 853 |
b1s, b2s, b3s = [ConstInt(sys.maxint), ConstInt(2**23), ConstInt(-65)] |
| 854 |
storage, t = make_storage(b1s, b2s, b3s) |
| 855 |
metainterp_sd = FakeMetaInterpStaticData() |
| 856 |
memo = ResumeDataLoopMemo(metainterp_sd) |
| 857 |
i = t.get_iter() |
| 858 |
modifier = ResumeDataVirtualAdder(FakeOptimizer(i), storage, storage, i, memo) |
| 859 |
modifier.finish() |
| 860 |
assert len(memo.consts) == 2 |
| 861 |
assert storage.rd_consts is memo.consts |
| 862 |
|
| 863 |
b1s, b2s, b3s = [ConstInt(sys.maxint), ConstInt(2**24), ConstInt(-65)] |
| 864 |
storage2, t = make_storage(b1s, b2s, b3s) |
| 865 |
i = t.get_iter() |
| 866 |
modifier2 = ResumeDataVirtualAdder(FakeOptimizer(i), storage2, storage2, |
| 867 |
i, memo) |
| 868 |
modifier2.finish() |
| 869 |
assert len(memo.consts) == 3 |
| 870 |
assert storage2.rd_consts is memo.consts |
| 871 |
|
| 872 |
|
| 873 |
class ResumeDataFakeReader(ResumeDataBoxReader): |
| 874 |
"""Another subclass of AbstractResumeDataReader meant for tests.""" |
| 875 |
def __init__(self, storage, newboxes, metainterp): |
| 876 |
self._init(metainterp.staticdata, storage) |
| 877 |
self.liveboxes = newboxes |
| 878 |
self.metainterp = metainterp |
| 879 |
self._prepare(storage) |
| 880 |
|
| 881 |
def consume_boxes(self): |
| 882 |
self.lst = [] |
| 883 |
class Whatever: |
| 884 |
def __eq__(self, other): |
| 885 |
return True |
| 886 |
def enumerate_vars(callback_i, callback_r, callback_f): |
| 887 |
|
| 888 |
index = 0 |
| 889 |
while not self.done_reading(): |
| 890 |
tagged = self.resumecodereader.peek() |
| 891 |
_, tag = untag(tagged) |
| 892 |
if tag == TAGVIRTUAL: |
| 893 |
kind = REF |
| 894 |
else: |
| 895 |
kind = Whatever() |
| 896 |
box = self.decode_box(tagged, kind) |
| 897 |
if box.type == INT: |
| 898 |
callback_i(index) |
| 899 |
elif box.type == REF: |
| 900 |
callback_r(index) |
| 901 |
elif box.type == FLOAT: |
| 902 |
callback_f(index) |
| 903 |
else: |
| 904 |
assert 0 |
| 905 |
index += 1 |
| 906 |
|
| 907 |
size = self.resumecodereader.next_item() |
| 908 |
assert size == 0 |
| 909 |
size = self.resumecodereader.next_item() |
| 910 |
assert size == 0 |
| 911 |
pc = self.resumecodereader.next_item() |
| 912 |
jitcode_pos = self.resumecodereader.next_item() |
| 913 |
|
| 914 |
enumerate_vars(self._callback_i, self._callback_r, self._callback_f) |
| 915 |
return self.lst |
| 916 |
|
| 917 |
def write_an_int(self, count_i, box): |
| 918 |
assert box.type == INT |
| 919 |
self.lst.append(box) |
| 920 |
def write_a_ref(self, count_r, box): |
| 921 |
assert box.type == REF |
| 922 |
self.lst.append(box) |
| 923 |
def write_a_float(self, count_f, box): |
| 924 |
assert box.type == FLOAT |
| 925 |
self.lst.append(box) |
| 926 |
|
| 927 |
|
| 928 |
def test_virtual_adder_no_op_renaming(): |
| 929 |
py.test.skip("rewrite fake reader") |
| 930 |
b1s, b2s, b3s = [InputArgInt(1), InputArgInt(2), InputArgInt(3)] |
| 931 |
storage = make_storage(b1s, b2s, b3s) |
| 932 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 933 |
b1_2 = InputArgInt() |
| 934 |
modifier = ResumeDataVirtualAdder(FakeOptimizer(), storage, storage, memo) |
| 935 |
|
| 936 |
b1s.set_forwarded(b1_2) |
| 937 |
b2s.set_forwarded(b1_2) |
| 938 |
liveboxes = modifier.finish() |
| 939 |
assert storage.rd_snapshot is None |
| 940 |
b1t, b3t = [InputArgInt(11), InputArgInt(33)] |
| 941 |
newboxes = _resume_remap(liveboxes, [b1_2, b3s], b1t, b3t) |
| 942 |
metainterp = MyMetaInterp() |
| 943 |
reader = ResumeDataFakeReader(storage, newboxes, metainterp) |
| 944 |
lst = reader.consume_boxes() |
| 945 |
assert_same(lst, [b1t, b1t, b3t]) |
| 946 |
lst = reader.consume_boxes() |
| 947 |
assert_same(lst, [ConstInt(2), ConstInt(3)]) |
| 948 |
lst = reader.consume_boxes() |
| 949 |
assert_same(lst, [b1t, ConstInt(1), b1t, b1t]) |
| 950 |
assert metainterp.trace == [] |
| 951 |
|
| 952 |
|
| 953 |
def test_virtual_adder_make_constant(): |
| 954 |
py.test.skip("rewrite fake reader") |
| 955 |
b1s, b2s, b3s = [InputArgInt(1), InputArgRef(), InputArgInt(3)] |
| 956 |
b1s = ConstInt(111) |
| 957 |
storage = make_storage(b1s, b2s, b3s) |
| 958 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 959 |
modifier = ResumeDataVirtualAdder(FakeOptimizer(), storage, storage, memo) |
| 960 |
liveboxes = modifier.finish() |
| 961 |
b2t, b3t = [InputArgRef(demo55o), InputArgInt(33)] |
| 962 |
newboxes = _resume_remap(liveboxes, [b2s, b3s], b2t, b3t) |
| 963 |
metainterp = MyMetaInterp() |
| 964 |
reader = ResumeDataFakeReader(storage, newboxes, metainterp) |
| 965 |
lst = reader.consume_boxes() |
| 966 |
c1t = ConstInt(111) |
| 967 |
assert_same(lst, [c1t, b2t, b3t]) |
| 968 |
lst = reader.consume_boxes() |
| 969 |
assert_same(lst, [ConstInt(2), ConstInt(3)]) |
| 970 |
lst = reader.consume_boxes() |
| 971 |
assert_same(lst, [c1t, ConstInt(1), c1t, b2t]) |
| 972 |
assert metainterp.trace == [] |
| 973 |
|
| 974 |
|
| 975 |
def test_virtual_adder_make_virtual(): |
| 976 |
b2s, b3s, b4s, b5s = [RefFrontendOp(0, dummyref), IntFrontendOp(0, 0), RefFrontendOp(0, dummyref), |
| 977 |
RefFrontendOp(0, dummyref)] |
| 978 |
c1s = ConstInt(111) |
| 979 |
storage = Storage() |
| 980 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 981 |
modifier = ResumeDataVirtualAdder(FakeOptimizer(), storage, storage, None, memo) |
| 982 |
modifier.liveboxes_from_env = {} |
| 983 |
modifier.liveboxes = {} |
| 984 |
modifier.vfieldboxes = {} |
| 985 |
|
| 986 |
vdescr = LLtypeMixin.nodesize2 |
| 987 |
ca = ConstInt(ptr2int(LLtypeMixin.node_vtable2)) |
| 988 |
v4 = info.InstancePtrInfo(vdescr, ca, True) |
| 989 |
b4s.set_forwarded(v4) |
| 990 |
v4.setfield(LLtypeMixin.nextdescr, ca, b2s) |
| 991 |
v4.setfield(LLtypeMixin.valuedescr, ca, b3s) |
| 992 |
v4.setfield(LLtypeMixin.otherdescr, ca, b5s) |
| 993 |
ca = ConstInt(ptr2int(LLtypeMixin.node_vtable)) |
| 994 |
v2 = info.InstancePtrInfo(LLtypeMixin.nodesize, ca, True) |
| 995 |
v2.setfield(LLtypeMixin.nextdescr, b4s, ca) |
| 996 |
v2.setfield(LLtypeMixin.valuedescr, c1s, ca) |
| 997 |
b2s.set_forwarded(v2) |
| 998 |
|
| 999 |
modifier.register_virtual_fields(b2s, [c1s, None, None, None, b4s]) |
| 1000 |
modifier.register_virtual_fields(b4s, [b3s, None, None, None, b2s, b5s]) |
| 1001 |
|
| 1002 |
liveboxes = [] |
| 1003 |
modifier._number_virtuals(liveboxes, 0) |
| 1004 |
storage.rd_consts = memo.consts[:] |
| 1005 |
storage.rd_numb = Numbering([0]) |
| 1006 |
|
| 1007 |
b3t, b5t = [IntFrontendOp(0, 0), RefFrontendOp(0, dummyref)] |
| 1008 |
b5t.setref_base(demo55o) |
| 1009 |
b3t.setint(33) |
| 1010 |
newboxes = _resume_remap(liveboxes, [ |
| 1011 |
b3s, |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
b5s], b3t, b5t) |
| 1016 |
|
| 1017 |
metainterp = MyMetaInterp() |
| 1018 |
reader = ResumeDataFakeReader(storage, newboxes, metainterp) |
| 1019 |
assert len(reader.virtuals_cache.virtuals_ptr_cache) == 2 |
| 1020 |
b2t = reader.decode_ref(modifier._gettagged(b2s)) |
| 1021 |
b4t = reader.decode_ref(modifier._gettagged(b4s)) |
| 1022 |
trace = metainterp.trace |
| 1023 |
b2new = (rop.NEW_WITH_VTABLE, [], b2t.getref_base(), LLtypeMixin.nodesize) |
| 1024 |
b4new = (rop.NEW_WITH_VTABLE, [], b4t.getref_base(), LLtypeMixin.nodesize2) |
| 1025 |
b2set = [(rop.SETFIELD_GC, [b2t, b4t], None, LLtypeMixin.nextdescr), |
| 1026 |
(rop.SETFIELD_GC, [b2t, c1s], None, LLtypeMixin.valuedescr)] |
| 1027 |
b4set = [(rop.SETFIELD_GC, [b4t, b2t], None, LLtypeMixin.nextdescr), |
| 1028 |
(rop.SETFIELD_GC, [b4t, b3t], None, LLtypeMixin.valuedescr), |
| 1029 |
(rop.SETFIELD_GC, [b4t, b5t], None, LLtypeMixin.otherdescr)] |
| 1030 |
expected = [b2new, b4new] + b4set + b2set |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
assert len(trace) == len(expected) |
| 1035 |
orig = trace[:] |
| 1036 |
with CompareableConsts(): |
| 1037 |
for x in trace: |
| 1038 |
assert x in expected |
| 1039 |
expected.remove(x) |
| 1040 |
|
| 1041 |
ptr = b2t.getref_base()._obj.container._as_ptr() |
| 1042 |
assert lltype.typeOf(ptr) == lltype.Ptr(LLtypeMixin.NODE) |
| 1043 |
assert ptr.value == 111 |
| 1044 |
ptr2 = ptr.next |
| 1045 |
ptr2 = lltype.cast_pointer(lltype.Ptr(LLtypeMixin.NODE2), ptr2) |
| 1046 |
assert ptr2.other == demo55 |
| 1047 |
assert ptr2.parent.value == 33 |
| 1048 |
assert ptr2.parent.next == ptr |
| 1049 |
|
| 1050 |
class CompareableConsts(object): |
| 1051 |
def __enter__(self): |
| 1052 |
Const.__eq__ = Const.same_box |
| 1053 |
|
| 1054 |
def __exit__(self, type, value, traceback): |
| 1055 |
del Const.__eq__ |
| 1056 |
|
| 1057 |
def test_virtual_adder_make_varray(): |
| 1058 |
b2s, b4s = [RefFrontendOp(0, dummyref), IntFrontendOp(0, 0)] |
| 1059 |
b4s.setint(4) |
| 1060 |
c1s = ConstInt(111) |
| 1061 |
storage = Storage() |
| 1062 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 1063 |
modifier = ResumeDataVirtualAdder(FakeOptimizer(), storage, storage, None, memo) |
| 1064 |
modifier.liveboxes_from_env = {} |
| 1065 |
modifier.liveboxes = {} |
| 1066 |
modifier.vfieldboxes = {} |
| 1067 |
|
| 1068 |
v2 = info.ArrayPtrInfo(LLtypeMixin.arraydescr, size=2, is_virtual=True) |
| 1069 |
b2s.set_forwarded(v2) |
| 1070 |
v2._items = [b4s, c1s] |
| 1071 |
modifier.register_virtual_fields(b2s, [b4s, c1s]) |
| 1072 |
liveboxes = [] |
| 1073 |
modifier._number_virtuals(liveboxes, 0) |
| 1074 |
dump_storage(storage, liveboxes) |
| 1075 |
storage.rd_consts = memo.consts[:] |
| 1076 |
storage.rd_numb = Numbering([0]) |
| 1077 |
|
| 1078 |
b1t, b3t, b4t = [IntFrontendOp(0, 0), IntFrontendOp(0, 0), IntFrontendOp(0, 0)] |
| 1079 |
b1t.setint(11) |
| 1080 |
b3t.setint(33) |
| 1081 |
b4t.setint(44) |
| 1082 |
newboxes = _resume_remap(liveboxes, [ |
| 1083 |
b4s], |
| 1084 |
b4t) |
| 1085 |
|
| 1086 |
metainterp = MyMetaInterp() |
| 1087 |
reader = ResumeDataFakeReader(storage, newboxes, metainterp) |
| 1088 |
assert len(reader.virtuals_cache.virtuals_ptr_cache) == 1 |
| 1089 |
b2t = reader.decode_ref(tag(0, TAGVIRTUAL)) |
| 1090 |
trace = metainterp.trace |
| 1091 |
expected = [ |
| 1092 |
(rop.NEW_ARRAY, [ConstInt(2)], b2t.getref_base(), |
| 1093 |
LLtypeMixin.arraydescr), |
| 1094 |
(rop.SETARRAYITEM_GC, [b2t,ConstInt(0), b4t],None, |
| 1095 |
LLtypeMixin.arraydescr), |
| 1096 |
(rop.SETARRAYITEM_GC, [b2t,ConstInt(1), c1s], None, |
| 1097 |
LLtypeMixin.arraydescr), |
| 1098 |
] |
| 1099 |
with CompareableConsts(): |
| 1100 |
for x, y in zip(expected, trace): |
| 1101 |
assert x == y |
| 1102 |
|
| 1103 |
ptr = b2t.getref_base()._obj.container._as_ptr() |
| 1104 |
assert lltype.typeOf(ptr) == lltype.Ptr(lltype.GcArray(lltype.Signed)) |
| 1105 |
assert len(ptr) == 2 |
| 1106 |
assert ptr[0] == 44 |
| 1107 |
assert ptr[1] == 111 |
| 1108 |
|
| 1109 |
|
| 1110 |
def test_virtual_adder_make_vstruct(): |
| 1111 |
b2s, b4s = [RefFrontendOp(0, dummyref), RefFrontendOp(0, dummyref)] |
| 1112 |
c1s = ConstInt(111) |
| 1113 |
storage = Storage() |
| 1114 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 1115 |
modifier = ResumeDataVirtualAdder(FakeOptimizer(), storage, storage, None, memo) |
| 1116 |
modifier.liveboxes_from_env = {} |
| 1117 |
modifier.liveboxes = {} |
| 1118 |
modifier.vfieldboxes = {} |
| 1119 |
v2 = info.StructPtrInfo(LLtypeMixin.ssize, is_virtual=True) |
| 1120 |
b2s.set_forwarded(v2) |
| 1121 |
v2.setfield(LLtypeMixin.adescr, b2s, c1s) |
| 1122 |
v2.setfield(LLtypeMixin.abisdescr, b2s, c1s) |
| 1123 |
v2.setfield(LLtypeMixin.bdescr, b2s, b4s) |
| 1124 |
modifier.register_virtual_fields(b2s, [c1s, c1s, b4s]) |
| 1125 |
liveboxes = [] |
| 1126 |
modifier._number_virtuals(liveboxes, 0) |
| 1127 |
dump_storage(storage, liveboxes) |
| 1128 |
storage.rd_consts = memo.consts[:] |
| 1129 |
storage.rd_numb = Numbering([0]) |
| 1130 |
b4t = RefFrontendOp(0, dummyref) |
| 1131 |
newboxes = _resume_remap(liveboxes, [ |
| 1132 |
b4s], b4t) |
| 1133 |
|
| 1134 |
NULL = ConstPtr.value |
| 1135 |
metainterp = MyMetaInterp() |
| 1136 |
reader = ResumeDataFakeReader(storage, newboxes, metainterp) |
| 1137 |
assert len(reader.virtuals_cache.virtuals_ptr_cache) == 1 |
| 1138 |
b2t = reader.decode_ref(tag(0, TAGVIRTUAL)) |
| 1139 |
|
| 1140 |
trace = metainterp.trace |
| 1141 |
expected = [ |
| 1142 |
(rop.NEW, [], b2t.getref_base(), LLtypeMixin.ssize), |
| 1143 |
(rop.SETFIELD_GC, [b2t, c1s], None, LLtypeMixin.adescr), |
| 1144 |
(rop.SETFIELD_GC, [b2t, c1s], None, LLtypeMixin.abisdescr), |
| 1145 |
(rop.SETFIELD_GC, [b2t, b4t], None, LLtypeMixin.bdescr), |
| 1146 |
] |
| 1147 |
with CompareableConsts(): |
| 1148 |
for x, y in zip(expected, trace): |
| 1149 |
assert x == y |
| 1150 |
|
| 1151 |
ptr = b2t.getref_base()._obj.container._as_ptr() |
| 1152 |
assert lltype.typeOf(ptr) == lltype.Ptr(LLtypeMixin.S) |
| 1153 |
assert ptr.a == 111 |
| 1154 |
assert ptr.b == lltype.nullptr(LLtypeMixin.NODE) |
| 1155 |
|
| 1156 |
|
| 1157 |
def test_virtual_adder_pending_fields(): |
| 1158 |
b2s, b4s = [RefFrontendOp(0, dummyref), RefFrontendOp(0, dummyref)] |
| 1159 |
storage = Storage() |
| 1160 |
memo = ResumeDataLoopMemo(FakeMetaInterpStaticData()) |
| 1161 |
modifier = ResumeDataVirtualAdder(None, storage, storage, None, memo) |
| 1162 |
modifier.liveboxes_from_env = {} |
| 1163 |
modifier.liveboxes = {} |
| 1164 |
modifier.vfieldboxes = {} |
| 1165 |
|
| 1166 |
modifier.register_box(b2s) |
| 1167 |
modifier.register_box(b4s) |
| 1168 |
|
| 1169 |
liveboxes = [] |
| 1170 |
modifier._number_virtuals(liveboxes, 0) |
| 1171 |
assert liveboxes == [b2s, b4s] or liveboxes == [b4s, b2s] |
| 1172 |
modifier._add_pending_fields([ |
| 1173 |
ResOperation(rop.SETFIELD_GC, [b2s, b4s], descr=LLtypeMixin.nextdescr)]) |
| 1174 |
storage.rd_consts = memo.consts[:] |
| 1175 |
storage.rd_numb = Numbering([0]) |
| 1176 |
|
| 1177 |
demo55.next = lltype.nullptr(LLtypeMixin.NODE) |
| 1178 |
b2t = RefFrontendOp(0, dummyref) |
| 1179 |
b2t.setref_base(demo55o) |
| 1180 |
b4t = RefFrontendOp(0, dummyref) |
| 1181 |
b4t.setref_base(demo66o) |
| 1182 |
newboxes = _resume_remap(liveboxes, [b2s, b4s], b2t, b4t) |
| 1183 |
|
| 1184 |
metainterp = MyMetaInterp() |
| 1185 |
reader = ResumeDataFakeReader(storage, newboxes, metainterp) |
| 1186 |
assert reader.virtuals_cache is None |
| 1187 |
trace = metainterp.trace |
| 1188 |
b2set = (rop.SETFIELD_GC, [b2t, b4t], None, LLtypeMixin.nextdescr) |
| 1189 |
expected = [b2set] |
| 1190 |
|
| 1191 |
for x, y in zip(expected, trace): |
| 1192 |
assert x == y |
| 1193 |
assert len(expected) == len(trace) |
| 1194 |
assert demo55.next == demo66 |
| 1195 |
|
| 1196 |
def test_virtual_adder_pending_fields_and_arrayitems(): |
| 1197 |
class Storage(object): |
| 1198 |
pass |
| 1199 |
storage = Storage() |
| 1200 |
modifier = ResumeDataVirtualAdder(None, storage, storage, None, None) |
| 1201 |
modifier._add_pending_fields([]) |
| 1202 |
assert not storage.rd_pendingfields |
| 1203 |
|
| 1204 |
class FieldDescr(AbstractDescr): |
| 1205 |
def is_array_of_primitives(self): |
| 1206 |
return False |
| 1207 |
field_a = FieldDescr() |
| 1208 |
storage = Storage() |
| 1209 |
modifier = ResumeDataVirtualAdder(None, storage, storage, None, None) |
| 1210 |
a = IntFrontendOp(0, 0) |
| 1211 |
b = IntFrontendOp(0, 0) |
| 1212 |
modifier.liveboxes_from_env = {a: rffi.cast(rffi.SHORT, 1042), |
| 1213 |
b: rffi.cast(rffi.SHORT, 1061)} |
| 1214 |
modifier._add_pending_fields( |
| 1215 |
[ResOperation(rop.SETFIELD_GC, [a, b], descr=field_a)]) |
| 1216 |
pf = storage.rd_pendingfields |
| 1217 |
assert len(pf) == 1 |
| 1218 |
assert (annlowlevel.cast_base_ptr_to_instance(FieldDescr, pf[0].lldescr) |
| 1219 |
is field_a) |
| 1220 |
assert rffi.cast(lltype.Signed, pf[0].num) == 1042 |
| 1221 |
assert rffi.cast(lltype.Signed, pf[0].fieldnum) == 1061 |
| 1222 |
assert rffi.cast(lltype.Signed, pf[0].itemindex) == -1 |
| 1223 |
|
| 1224 |
array_a = FieldDescr() |
| 1225 |
storage = Storage() |
| 1226 |
modifier = ResumeDataVirtualAdder(None, storage, storage, None, None) |
| 1227 |
a42 = IntFrontendOp(0, 0) |
| 1228 |
a61 = IntFrontendOp(0, 0) |
| 1229 |
a62 = IntFrontendOp(0, 0) |
| 1230 |
a63 = IntFrontendOp(0, 0) |
| 1231 |
modifier.liveboxes_from_env = {a42: rffi.cast(rffi.SHORT, 1042), |
| 1232 |
a61: rffi.cast(rffi.SHORT, 1061), |
| 1233 |
a62: rffi.cast(rffi.SHORT, 1062), |
| 1234 |
a63: rffi.cast(rffi.SHORT, 1063)} |
| 1235 |
modifier._add_pending_fields([ |
| 1236 |
ResOperation(rop.SETARRAYITEM_GC, [a42, ConstInt(0), a61], |
| 1237 |
descr=array_a), |
| 1238 |
ResOperation(rop.SETARRAYITEM_GC, [a42, ConstInt(2147483647), a62], |
| 1239 |
descr=array_a)]) |
| 1240 |
pf = storage.rd_pendingfields |
| 1241 |
assert len(pf) == 2 |
| 1242 |
assert (annlowlevel.cast_base_ptr_to_instance(FieldDescr, pf[0].lldescr) |
| 1243 |
is array_a) |
| 1244 |
assert rffi.cast(lltype.Signed, pf[0].num) == 1042 |
| 1245 |
assert rffi.cast(lltype.Signed, pf[0].fieldnum) == 1061 |
| 1246 |
assert rffi.cast(lltype.Signed, pf[0].itemindex) == 0 |
| 1247 |
assert (annlowlevel.cast_base_ptr_to_instance(FieldDescr, pf[1].lldescr) |
| 1248 |
is array_a) |
| 1249 |
assert rffi.cast(lltype.Signed, pf[1].num) == 1042 |
| 1250 |
assert rffi.cast(lltype.Signed, pf[1].fieldnum) == 1062 |
| 1251 |
assert rffi.cast(lltype.Signed, pf[1].itemindex) == 2147483647 |
| 1252 |
|
| 1253 |
if sys.maxint >= 2147483648: |
| 1254 |
with py.test.raises(TagOverflow): |
| 1255 |
modifier._add_pending_fields( |
| 1256 |
[ResOperation(rop.SETARRAYITEM_GC, |
| 1257 |
[a42, ConstInt(2147483648), a63], descr=array_a)]) |
| 1258 |
|
| 1259 |
def test_resume_reader_fields_and_arrayitems(): |
| 1260 |
class ResumeReader(AbstractResumeDataReader): |
| 1261 |
def __init__(self, got=None, got_array=None): |
| 1262 |
self.got = got |
| 1263 |
self.got_array = got_array |
| 1264 |
def setfield(self, struct, fieldnum, descr): |
| 1265 |
assert lltype.typeOf(struct) is lltype.Signed |
| 1266 |
assert lltype.typeOf(fieldnum) is rffi.SHORT |
| 1267 |
fieldnum = rffi.cast(lltype.Signed, fieldnum) |
| 1268 |
self.got.append((descr, struct, fieldnum)) |
| 1269 |
def setarrayitem(self, array, index, fieldnum, arraydescr): |
| 1270 |
assert lltype.typeOf(array) is lltype.Signed |
| 1271 |
assert lltype.typeOf(index) is lltype.Signed |
| 1272 |
assert lltype.typeOf(fieldnum) is rffi.SHORT |
| 1273 |
fieldnum = rffi.cast(lltype.Signed, fieldnum) |
| 1274 |
self.got_array.append((arraydescr, array, index, fieldnum)) |
| 1275 |
def decode_ref(self, num): |
| 1276 |
return rffi.cast(lltype.Signed, num) * 100 |
| 1277 |
got = [] |
| 1278 |
pf = lltype.nullptr(PENDINGFIELDSP.TO) |
| 1279 |
ResumeReader(got)._prepare_pendingfields(pf) |
| 1280 |
assert got == [] |
| 1281 |
|
| 1282 |
class FieldDescr(AbstractDescr): |
| 1283 |
pass |
| 1284 |
field_a = FieldDescr() |
| 1285 |
field_b = FieldDescr() |
| 1286 |
pf = lltype.malloc(PENDINGFIELDSP.TO, 2) |
| 1287 |
pf[0].lldescr = annlowlevel.cast_instance_to_base_ptr(field_a) |
| 1288 |
pf[0].num = rffi.cast(rffi.SHORT, 1042) |
| 1289 |
pf[0].fieldnum = rffi.cast(rffi.SHORT, 1061) |
| 1290 |
pf[0].itemindex = rffi.cast(rffi.INT, -1) |
| 1291 |
pf[1].lldescr = annlowlevel.cast_instance_to_base_ptr(field_b) |
| 1292 |
pf[1].num = rffi.cast(rffi.SHORT, 2042) |
| 1293 |
pf[1].fieldnum = rffi.cast(rffi.SHORT, 2061) |
| 1294 |
pf[1].itemindex = rffi.cast(rffi.INT, -1) |
| 1295 |
got = [] |
| 1296 |
ResumeReader(got)._prepare_pendingfields(pf) |
| 1297 |
assert got == [(field_a, 104200, 1061), (field_b, 204200, 2061)] |
| 1298 |
|
| 1299 |
array_a = FieldDescr() |
| 1300 |
pf = lltype.malloc(PENDINGFIELDSP.TO, 1) |
| 1301 |
pf[0].lldescr = annlowlevel.cast_instance_to_base_ptr(array_a) |
| 1302 |
pf[0].num = rffi.cast(rffi.SHORT, 1042) |
| 1303 |
pf[0].fieldnum = rffi.cast(rffi.SHORT, 1063) |
| 1304 |
pf[0].itemindex = rffi.cast(rffi.INT, 123) |
| 1305 |
got_array = [] |
| 1306 |
ResumeReader(got_array=got_array)._prepare_pendingfields(pf) |
| 1307 |
assert got_array == [(array_a, 104200, 123, 1063)] |
| 1308 |
|
| 1309 |
|
| 1310 |
def test_invalidation_needed(): |
| 1311 |
class options: |
| 1312 |
failargs_limit = 10 |
| 1313 |
|
| 1314 |
metainterp_sd = FakeMetaInterpStaticData() |
| 1315 |
metainterp_sd.options = options |
| 1316 |
memo = ResumeDataLoopMemo(metainterp_sd) |
| 1317 |
modifier = ResumeDataVirtualAdder(None, None, None, None, memo) |
| 1318 |
|
| 1319 |
for i in range(5): |
| 1320 |
assert not modifier._invalidation_needed(5, i) |
| 1321 |
|
| 1322 |
assert not modifier._invalidation_needed(7, 2) |
| 1323 |
assert modifier._invalidation_needed(7, 3) |
| 1324 |
|
| 1325 |
assert not modifier._invalidation_needed(10, 2) |
| 1326 |
assert not modifier._invalidation_needed(10, 3) |
| 1327 |
assert modifier._invalidation_needed(10, 4) |