| 1 |
import sys |
| 2 |
import pytest |
| 3 |
from rpython.rlib.rarithmetic import intmask, LONG_BIT |
| 4 |
from rpython.jit.metainterp.history import BasicFailDescr, TreeLoop, BasicFinalDescr |
| 5 |
from rpython.jit.metainterp.history import INT, ConstInt, JitCellToken |
| 6 |
from rpython.jit.metainterp.history import REF, ConstPtr, TargetToken |
| 7 |
from rpython.jit.metainterp.history import FLOAT, ConstFloat, Const, VOID |
| 8 |
from rpython.jit.metainterp.resoperation import ResOperation, rop |
| 9 |
from rpython.jit.metainterp.resoperation import InputArgInt, InputArgRef |
| 10 |
from rpython.jit.metainterp.resoperation import InputArgFloat |
| 11 |
from rpython.jit.metainterp.executor import _execute_arglist, wrap_constant |
| 12 |
from rpython.jit.metainterp.resoperation import opname |
| 13 |
from rpython.jit.codewriter import longlong |
| 14 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rstr |
| 15 |
from rpython.rtyper import rclass |
| 16 |
|
| 17 |
|
| 18 |
class DummyLoop(object): |
| 19 |
def __init__(self, subops): |
| 20 |
self.operations = subops |
| 21 |
|
| 22 |
class FakeMetaInterp(object): |
| 23 |
ovf_flag = False |
| 24 |
|
| 25 |
def execute_raised(self, exc, constant=False): |
| 26 |
self._got_exc = exc |
| 27 |
|
| 28 |
|
| 29 |
def getint(v): |
| 30 |
if isinstance(v, (ConstInt, InputArgInt)): |
| 31 |
return v.getint() |
| 32 |
else: |
| 33 |
return v._example_int |
| 34 |
|
| 35 |
def getfloatstorage(v): |
| 36 |
if isinstance(v, (ConstFloat, InputArgFloat)): |
| 37 |
return v.getfloatstorage() |
| 38 |
else: |
| 39 |
return v._example_float |
| 40 |
|
| 41 |
def getfloat(v): |
| 42 |
return longlong.getrealfloat(getfloatstorage(v)) |
| 43 |
|
| 44 |
def getref_base(v): |
| 45 |
if isinstance(v, (ConstPtr, InputArgRef)): |
| 46 |
return v.getref_base() |
| 47 |
else: |
| 48 |
return v._example_ref |
| 49 |
|
| 50 |
def getref(PTR, v): |
| 51 |
return lltype.cast_opaque_ptr(PTR, getref_base(v)) |
| 52 |
|
| 53 |
def constbox(v): |
| 54 |
if v.type == INT: |
| 55 |
return ConstInt(getint(v)) |
| 56 |
if v.type == FLOAT: |
| 57 |
return ConstFloat(getfloatstorage(v)) |
| 58 |
if v.type == REF: |
| 59 |
return ConstPtr(getref_base(v)) |
| 60 |
assert 0, v.type |
| 61 |
|
| 62 |
|
| 63 |
class OperationBuilder(object): |
| 64 |
produce_failing_guards = True |
| 65 |
|
| 66 |
def __init__(self, cpu, loop, vars): |
| 67 |
self.cpu = cpu |
| 68 |
if not hasattr(cpu, '_faildescr_keepalive'): |
| 69 |
cpu._faildescr_keepalive = [] |
| 70 |
self.fakemetainterp = FakeMetaInterp() |
| 71 |
self.loop = loop |
| 72 |
self.intvars = [box for box in vars if box.type == INT] |
| 73 |
self.boolvars = [] |
| 74 |
self.ptrvars = [] |
| 75 |
self.prebuilt_ptr_consts = [] |
| 76 |
floatvars = [box for box in vars if box.type == FLOAT] |
| 77 |
if cpu.supports_floats: |
| 78 |
self.floatvars = floatvars |
| 79 |
else: |
| 80 |
assert floatvars == [] |
| 81 |
self.should_fail_by = None |
| 82 |
self.counter = 0 |
| 83 |
assert len(self.intvars) == len(dict.fromkeys(self.intvars)) |
| 84 |
self.descr_counters = {} |
| 85 |
|
| 86 |
def fork(self, cpu, loop, vars): |
| 87 |
fork = self.__class__(cpu, loop, vars) |
| 88 |
fork.prebuilt_ptr_consts = self.prebuilt_ptr_consts |
| 89 |
fork.descr_counters = self.descr_counters |
| 90 |
return fork |
| 91 |
|
| 92 |
def do(self, opnum, argboxes, descr=None): |
| 93 |
self.fakemetainterp._got_exc = None |
| 94 |
op = ResOperation(opnum, argboxes, descr) |
| 95 |
argboxes = map(constbox, argboxes) |
| 96 |
result = _execute_arglist(self.cpu, self.fakemetainterp, |
| 97 |
opnum, argboxes, descr) |
| 98 |
if result is not None: |
| 99 |
if lltype.typeOf(result) == lltype.Signed: |
| 100 |
op._example_int = result |
| 101 |
elif isinstance(result, bool): |
| 102 |
op._example_int = int(result) |
| 103 |
elif lltype.typeOf(result) == longlong.FLOATSTORAGE: |
| 104 |
op._example_float = result |
| 105 |
elif isinstance(result, float): |
| 106 |
op._example_float = longlong.getfloatstorage(result) |
| 107 |
else: |
| 108 |
assert lltype.typeOf(result) == llmemory.GCREF |
| 109 |
op._example_ref = result |
| 110 |
self.loop.operations.append(op) |
| 111 |
return op |
| 112 |
|
| 113 |
def get_bool_var(self, r): |
| 114 |
if self.boolvars and r.random() < 0.8: |
| 115 |
return r.choice(self.boolvars) |
| 116 |
elif self.ptrvars and r.random() < 0.4: |
| 117 |
v, S = r.choice(self.ptrvars + self.prebuilt_ptr_consts)[:2] |
| 118 |
v2, S2 = r.choice(self.ptrvars + self.prebuilt_ptr_consts)[:2] |
| 119 |
if S == S2 and not (isinstance(v, ConstPtr) and |
| 120 |
isinstance(v2, ConstPtr)): |
| 121 |
if r.random() < 0.5: |
| 122 |
return self.do(rop.PTR_EQ, [v, v2]) |
| 123 |
else: |
| 124 |
return self.do(rop.PTR_NE, [v, v2]) |
| 125 |
v = r.choice(self.intvars) |
| 126 |
if r.random() < 0.7: |
| 127 |
return self.do(rop.INT_IS_TRUE, [v]) |
| 128 |
else: |
| 129 |
return self.do(rop.INT_IS_ZERO, [v]) |
| 130 |
|
| 131 |
def subset_of_intvars(self, r): |
| 132 |
subset = [] |
| 133 |
k = r.random() |
| 134 |
num = int(k * len(self.intvars)) |
| 135 |
seen = {} |
| 136 |
for i in range(num): |
| 137 |
v = r.choice(self.intvars) |
| 138 |
if v not in seen: |
| 139 |
subset.append(v) |
| 140 |
seen[v] = True |
| 141 |
return subset |
| 142 |
|
| 143 |
def process_operation(self, s, op, names): |
| 144 |
args = [] |
| 145 |
for v in op.getarglist(): |
| 146 |
if v in names: |
| 147 |
args.append(names[v]) |
| 148 |
elif isinstance(v, ConstPtr): |
| 149 |
assert not getref_base(v) |
| 150 |
args.append('ConstPtr(lltype.nullptr(llmemory.GCREF.TO))') |
| 151 |
elif isinstance(v, ConstFloat): |
| 152 |
args.append('ConstFloat(longlong.getfloatstorage(%r))' |
| 153 |
% v.getfloat()) |
| 154 |
elif isinstance(v, ConstInt): |
| 155 |
args.append('ConstInt(%s)' % v.getint()) |
| 156 |
else: |
| 157 |
raise NotImplementedError(v) |
| 158 |
if op.getdescr() is None: |
| 159 |
descrstr = '' |
| 160 |
else: |
| 161 |
try: |
| 162 |
descrstr = ', ' + getattr(op.getdescr(), '_random_info') |
| 163 |
except AttributeError: |
| 164 |
if op.opnum == rop.LABEL: |
| 165 |
descrstr = ', TargetToken()' |
| 166 |
else: |
| 167 |
descrstr = ', descr=' + self.descr_counters.get(op.getdescr(), '...') |
| 168 |
print >>s, ' %s = ResOperation(rop.%s, [%s]%s); operations.append(%s)' % ( |
| 169 |
names[op], opname[op.getopnum()], ', '.join(args), descrstr, names[op]) |
| 170 |
|
| 171 |
def print_loop(self, output, fail_descr=None, fail_args=None): |
| 172 |
def update_names(ops): |
| 173 |
for op in ops: |
| 174 |
v = op |
| 175 |
if v not in names: |
| 176 |
writevar(v, 'tmp') |
| 177 |
if op.is_guard() or op.opnum == rop.FINISH: |
| 178 |
descr = op.getdescr() |
| 179 |
no = len(self.descr_counters) |
| 180 |
if op.is_guard(): |
| 181 |
name = 'faildescr%d' % no |
| 182 |
clsname = 'BasicFailDescr' |
| 183 |
else: |
| 184 |
name = 'finishdescr%d' % no |
| 185 |
clsname = 'BasicFinalDescr' |
| 186 |
self.descr_counters[descr] = name |
| 187 |
print >>s, " %s = %s()" % (name, clsname) |
| 188 |
|
| 189 |
def print_loop_prebuilt(ops): |
| 190 |
for op in ops: |
| 191 |
for arg in op.getarglist(): |
| 192 |
if isinstance(arg, ConstPtr): |
| 193 |
if arg not in names: |
| 194 |
writevar(arg, 'const_ptr') |
| 195 |
|
| 196 |
def type_descr(TP): |
| 197 |
if TP in TYPE_NAMES: |
| 198 |
return TYPE_NAMES[TP] |
| 199 |
elif isinstance(TP, lltype.Primitive): |
| 200 |
return _type_descr(TP) |
| 201 |
if isinstance(TP, lltype.Ptr): |
| 202 |
if TP == llmemory.GCREF: |
| 203 |
return "llmemory.GCREF" |
| 204 |
return "lltype.Ptr(%s)" % type_descr(TP.TO) |
| 205 |
else: |
| 206 |
no = len(TYPE_NAMES) |
| 207 |
tp_name = 'S' + str(no) |
| 208 |
descr = _type_descr(TP, tp_name) |
| 209 |
TYPE_NAMES[TP] = tp_name |
| 210 |
print >>s, ' %s = %s' % (tp_name, descr) |
| 211 |
return tp_name |
| 212 |
|
| 213 |
def _type_descr(TP, tp_name=None): |
| 214 |
if isinstance(TP, lltype.Struct): |
| 215 |
if TP._gckind == 'gc': |
| 216 |
pref = 'Gc' |
| 217 |
else: |
| 218 |
pref = '' |
| 219 |
fields = [] |
| 220 |
for k in TP._names: |
| 221 |
v = getattr(TP, k) |
| 222 |
subdescr = type_descr(v) |
| 223 |
fields.append('("%s", %s)' % (k, subdescr)) |
| 224 |
return "lltype.%sStruct(%r, %s)" % (pref, tp_name, |
| 225 |
", ".join(fields)) |
| 226 |
elif isinstance(TP, lltype.GcArray): |
| 227 |
return "lltype.GcArray(%s)" % (type_descr(TP.OF),) |
| 228 |
if hasattr(TP, '_name'): |
| 229 |
if TP._name.upper() == TP._name: |
| 230 |
return 'rffi.%s' % TP._name |
| 231 |
return 'lltype.%s' % TP._name |
| 232 |
import pdb;pdb.set_trace() |
| 233 |
|
| 234 |
|
| 235 |
s = output |
| 236 |
names = {None: 'None'} |
| 237 |
TYPE_NAMES = { |
| 238 |
rstr.STR: 'rstr.STR', |
| 239 |
rstr.UNICODE: 'rstr.UNICODE', |
| 240 |
rclass.OBJECT: 'rclass.OBJECT', |
| 241 |
rclass.OBJECT_VTABLE: 'rclass.OBJECT_VTABLE', |
| 242 |
} |
| 243 |
def print_vtable(vtable, seen_vtables={}): |
| 244 |
tp_name = type_descr(descr.S) |
| 245 |
if tp_name in seen_vtables: |
| 246 |
return |
| 247 |
seen_vtables[tp_name] = True |
| 248 |
print >> s, """\ |
| 249 |
%s_vtable = vtable = lltype.malloc(rclass.OBJECT_VTABLE, immortal=True) |
| 250 |
vtable.subclassrange_min = %s |
| 251 |
vtable.subclassrange_max = %s |
| 252 |
heaptracker.set_testing_vtable_for_gcstruct(%s, vtable, %r)\ |
| 253 |
""" % (tp_name, vtable.subclassrange_min, vtable.subclassrange_max, tp_name, "".join(vtable.name.chars)) |
| 254 |
|
| 255 |
for op in self.loop.operations: |
| 256 |
descr = op.getdescr() |
| 257 |
if hasattr(descr, '_random_info'): |
| 258 |
tp_name = type_descr(descr._random_type) |
| 259 |
descr._random_info = descr._random_info.replace('...', tp_name) |
| 260 |
if hasattr(descr, 'get_parent_descr'): |
| 261 |
|
| 262 |
parentdescr = descr.get_parent_descr() |
| 263 |
if parentdescr.is_object(): |
| 264 |
print_vtable(parentdescr.get_vtable().adr.ptr) |
| 265 |
if hasattr(descr, 'is_object') and descr.is_object(): |
| 266 |
print_vtable(descr.get_vtable().adr.ptr) |
| 267 |
|
| 268 |
|
| 269 |
def writevar(v, nameprefix, init=''): |
| 270 |
name = names[v] = '%s%d' % (nameprefix, len(names)) |
| 271 |
if nameprefix == 'const_ptr': |
| 272 |
if not getref_base(v): |
| 273 |
return 'lltype.nullptr(llmemory.GCREF.TO)' |
| 274 |
TYPE = getref_base(v)._obj.ORIGTYPE |
| 275 |
cont = lltype.cast_opaque_ptr(TYPE, getref_base(v)) |
| 276 |
if TYPE.TO._is_varsize(): |
| 277 |
if isinstance(TYPE.TO, lltype.GcStruct): |
| 278 |
lgt = len(cont.chars) |
| 279 |
else: |
| 280 |
lgt = len(cont) |
| 281 |
init = 'lltype.malloc(%s, %d)' % (TYPE_NAMES[TYPE.TO], |
| 282 |
lgt) |
| 283 |
else: |
| 284 |
init = 'lltype.malloc(%s)' % TYPE_NAMES[TYPE.TO] |
| 285 |
if hasattr(TYPE.TO, 'parent'): |
| 286 |
print >> s, ' %s = %s' % (name, init) |
| 287 |
tp_name = type_descr(TYPE.TO) |
| 288 |
print >> s, ' %s.parent.typeptr = %s_vtable' % (name, tp_name) |
| 289 |
init = name |
| 290 |
init = 'lltype.cast_opaque_ptr(llmemory.GCREF, %s)' % init |
| 291 |
if v.is_constant() or isinstance(v, (InputArgInt, InputArgFloat, InputArgRef)): |
| 292 |
print >>s, ' %s = %s(%s)' % (name, v.__class__.__name__, |
| 293 |
init) |
| 294 |
|
| 295 |
for v in self.intvars: |
| 296 |
writevar(v, 'v') |
| 297 |
for v in self.floatvars: |
| 298 |
writevar(v, 'f') |
| 299 |
for v, S in self.ptrvars: |
| 300 |
writevar(v, 'p') |
| 301 |
update_names(self.loop.operations) |
| 302 |
print_loop_prebuilt(self.loop.operations) |
| 303 |
|
| 304 |
if fail_descr is None: |
| 305 |
print >>s, ' cpu = CPU(None, None)' |
| 306 |
print >>s, ' cpu.setup_once()' |
| 307 |
if hasattr(self.loop, 'inputargs'): |
| 308 |
print >>s, ' inputargs = [%s]' % ( |
| 309 |
', '.join([names[v] for v in self.loop.inputargs])) |
| 310 |
else: |
| 311 |
print >>s, ' inputargs = [%s]' % ( |
| 312 |
', '.join([names[v] for v in fail_args])) |
| 313 |
print >>s, ' operations = []' |
| 314 |
for op in self.loop.operations: |
| 315 |
self.process_operation(s, op, names) |
| 316 |
for i, op in enumerate(self.loop.operations): |
| 317 |
if op.is_guard(): |
| 318 |
fa = ", ".join([names[v] for v in op.getfailargs()]) |
| 319 |
print >>s, ' operations[%d].setfailargs([%s])' % (i, fa) |
| 320 |
if fail_descr is None: |
| 321 |
print >>s, ' looptoken = JitCellToken()' |
| 322 |
print >>s, ' cpu.compile_loop(inputargs, operations, looptoken)' |
| 323 |
else: |
| 324 |
print >>s, ' cpu.compile_bridge(%s, inputargs, operations, looptoken)' % self.descr_counters[fail_descr] |
| 325 |
if hasattr(self.loop, 'inputargs'): |
| 326 |
vals = [] |
| 327 |
for i, v in enumerate(self.loop.inputargs): |
| 328 |
assert not isinstance(v, Const) |
| 329 |
if v.type == FLOAT: |
| 330 |
vals.append("longlong.getfloatstorage(%r)" % getfloat(v)) |
| 331 |
else: |
| 332 |
vals.append("%r" % getint(v)) |
| 333 |
print >>s, ' loop_args = [%s]' % ", ".join(vals) |
| 334 |
print >>s, ' frame = cpu.execute_token(looptoken, *loop_args)' |
| 335 |
if self.should_fail_by is None: |
| 336 |
fail_args = self.loop.operations[-1].getarglist() |
| 337 |
else: |
| 338 |
fail_args = self.should_fail_by.getfailargs() |
| 339 |
for i, v in enumerate(fail_args): |
| 340 |
if v.type == FLOAT: |
| 341 |
print >>s, (' assert longlong.getrealfloat(' |
| 342 |
'cpu.get_float_value(frame, %d)) == %r' % (i, getfloatstorage(v))) |
| 343 |
else: |
| 344 |
print >>s, (' assert cpu.get_int_value(frame, %d) == %d' |
| 345 |
% (i, getint(v))) |
| 346 |
self.names = names |
| 347 |
s.flush() |
| 348 |
|
| 349 |
def getfaildescr(self, is_finish=False): |
| 350 |
if is_finish: |
| 351 |
descr = BasicFinalDescr() |
| 352 |
else: |
| 353 |
descr = BasicFailDescr() |
| 354 |
self.cpu._faildescr_keepalive.append(descr) |
| 355 |
return descr |
| 356 |
|
| 357 |
class CannotProduceOperation(Exception): |
| 358 |
pass |
| 359 |
|
| 360 |
class AbstractOperation(object): |
| 361 |
def __init__(self, opnum, boolres=False): |
| 362 |
self.opnum = opnum |
| 363 |
self.boolres = boolres |
| 364 |
def filter(self, builder): |
| 365 |
pass |
| 366 |
def put(self, builder, args, descr=None): |
| 367 |
v_result = builder.do(self.opnum, args, descr=descr) |
| 368 |
if v_result is not None: |
| 369 |
if v_result.type == INT: |
| 370 |
builder.intvars.append(v_result) |
| 371 |
boolres = self.boolres |
| 372 |
if boolres == 'sometimes': |
| 373 |
boolres = getint(v_result) in [0, 1] |
| 374 |
if boolres: |
| 375 |
builder.boolvars.append(v_result) |
| 376 |
elif v_result.type == FLOAT: |
| 377 |
builder.floatvars.append(v_result) |
| 378 |
assert self.boolres != True |
| 379 |
elif v_result.type == VOID: |
| 380 |
assert self.boolres != True |
| 381 |
else: |
| 382 |
raise NotImplementedError(v_result) |
| 383 |
|
| 384 |
class UnaryOperation(AbstractOperation): |
| 385 |
def produce_into(self, builder, r): |
| 386 |
self.put(builder, [r.choice(builder.intvars)]) |
| 387 |
|
| 388 |
class BooleanUnaryOperation(UnaryOperation): |
| 389 |
def produce_into(self, builder, r): |
| 390 |
v = builder.get_bool_var(r) |
| 391 |
self.put(builder, [v]) |
| 392 |
|
| 393 |
class ConstUnaryOperation(UnaryOperation): |
| 394 |
def produce_into(self, builder, r): |
| 395 |
if r.random() < 0.4: |
| 396 |
UnaryOperation.produce_into(self, builder, r) |
| 397 |
else: |
| 398 |
self.put(builder, [ConstInt(r.random_integer())]) |
| 399 |
|
| 400 |
class SignExtOperation(AbstractOperation): |
| 401 |
def produce_into(self, builder, r): |
| 402 |
sizes = [1, 2] |
| 403 |
if sys.maxint > (1 << 32): |
| 404 |
sizes.append(4) |
| 405 |
self.put(builder, [r.choice(builder.intvars), |
| 406 |
ConstInt(r.choice(sizes))]) |
| 407 |
|
| 408 |
class IntForceGeZeroOperation(AbstractOperation): |
| 409 |
def produce_into(self, builder, r): |
| 410 |
self.put(builder, [r.choice(builder.intvars), |
| 411 |
ConstInt(r.choice(sizes))]) |
| 412 |
|
| 413 |
class BinaryOperation(AbstractOperation): |
| 414 |
def __init__(self, opnum, and_mask=-1, or_mask=0, boolres=False): |
| 415 |
AbstractOperation.__init__(self, opnum, boolres=boolres) |
| 416 |
self.and_mask = and_mask |
| 417 |
self.or_mask = or_mask |
| 418 |
def produce_into(self, builder, r): |
| 419 |
k = r.random() |
| 420 |
if k < 0.2: |
| 421 |
v_first = ConstInt(r.random_integer()) |
| 422 |
else: |
| 423 |
v_first = r.choice(builder.intvars) |
| 424 |
if k > 0.75: |
| 425 |
value = r.random_integer() |
| 426 |
v_second = ConstInt((value & self.and_mask) | self.or_mask) |
| 427 |
else: |
| 428 |
v = r.choice(builder.intvars) |
| 429 |
v_value = getint(v) |
| 430 |
if (v_value & self.and_mask) != v_value: |
| 431 |
v = builder.do(rop.INT_AND, [v, ConstInt(self.and_mask)]) |
| 432 |
v_value = getint(v) |
| 433 |
if (v_value | self.or_mask) != v_value: |
| 434 |
v = builder.do(rop.INT_OR, [v, ConstInt(self.or_mask)]) |
| 435 |
v_second = v |
| 436 |
self.put(builder, [v_first, v_second]) |
| 437 |
|
| 438 |
class AbstractOvfOperation(AbstractOperation): |
| 439 |
def produce_into(self, builder, r): |
| 440 |
fail_subset = builder.subset_of_intvars(r) |
| 441 |
original_intvars = builder.intvars[:] |
| 442 |
builder.fakemetainterp.ovf_flag = False |
| 443 |
super(AbstractOvfOperation, self).produce_into(builder, r) |
| 444 |
if builder.fakemetainterp.ovf_flag: |
| 445 |
op = ResOperation(rop.GUARD_OVERFLOW, []) |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
fail_subset = builder.subset_of_intvars(r) |
| 450 |
builder.intvars[:] = original_intvars |
| 451 |
else: |
| 452 |
op = ResOperation(rop.GUARD_NO_OVERFLOW, []) |
| 453 |
op.setdescr(builder.getfaildescr()) |
| 454 |
op.setfailargs(fail_subset) |
| 455 |
builder.loop.operations.append(op) |
| 456 |
|
| 457 |
class BinaryOvfOperation(AbstractOvfOperation, BinaryOperation): |
| 458 |
pass |
| 459 |
|
| 460 |
class AbstractFloatOperation(AbstractOperation): |
| 461 |
def filter(self, builder): |
| 462 |
if not builder.cpu.supports_floats: |
| 463 |
raise CannotProduceOperation |
| 464 |
|
| 465 |
class BinaryFloatOperation(AbstractFloatOperation): |
| 466 |
def produce_into(self, builder, r): |
| 467 |
if not builder.floatvars: |
| 468 |
raise CannotProduceOperation |
| 469 |
k = r.random() |
| 470 |
if k < 0.18: |
| 471 |
v_first = ConstFloat(r.random_float_storage()) |
| 472 |
else: |
| 473 |
v_first = r.choice(builder.floatvars) |
| 474 |
if k > 0.82: |
| 475 |
v_second = ConstFloat(r.random_float_storage()) |
| 476 |
else: |
| 477 |
v_second = r.choice(builder.floatvars) |
| 478 |
if abs(getfloat(v_first)) > 1E100 or abs(getfloat(v_second)) > 1E100: |
| 479 |
raise CannotProduceOperation |
| 480 |
if abs(getfloat(v_second)) < 1E-100: |
| 481 |
raise CannotProduceOperation |
| 482 |
self.put(builder, [v_first, v_second]) |
| 483 |
|
| 484 |
class UnaryFloatOperation(AbstractFloatOperation): |
| 485 |
def produce_into(self, builder, r): |
| 486 |
if not builder.floatvars: |
| 487 |
raise CannotProduceOperation |
| 488 |
self.put(builder, [r.choice(builder.floatvars)]) |
| 489 |
|
| 490 |
class ConstUnaryFloatOperation(UnaryFloatOperation): |
| 491 |
def produce_into(self, builder, r): |
| 492 |
if r.random() < 0.4: |
| 493 |
UnaryFloatOperation.produce_into(self, builder, r) |
| 494 |
else: |
| 495 |
self.put(builder, [ConstFloat(r.random_float_storage())]) |
| 496 |
|
| 497 |
class CastIntToFloatOperation(AbstractFloatOperation): |
| 498 |
def produce_into(self, builder, r): |
| 499 |
self.put(builder, [r.choice(builder.intvars)]) |
| 500 |
|
| 501 |
class CastLongLongToFloatOperation(AbstractFloatOperation): |
| 502 |
def produce_into(self, builder, r): |
| 503 |
if longlong.is_64_bit: |
| 504 |
self.put(builder, [r.choice(builder.intvars)]) |
| 505 |
else: |
| 506 |
if not builder.floatvars: |
| 507 |
raise CannotProduceOperation |
| 508 |
self.put(builder, [r.choice(builder.floatvars)]) |
| 509 |
|
| 510 |
class CastFloatToIntOperation(AbstractFloatOperation): |
| 511 |
def produce_into(self, builder, r): |
| 512 |
if not builder.floatvars: |
| 513 |
raise CannotProduceOperation |
| 514 |
box = r.choice(builder.floatvars) |
| 515 |
if not (-sys.maxint-1 <= getfloat(box) <= sys.maxint): |
| 516 |
raise CannotProduceOperation |
| 517 |
self.put(builder, [box]) |
| 518 |
|
| 519 |
class GuardOperation(AbstractOperation): |
| 520 |
def gen_guard(self, builder, r): |
| 521 |
v = builder.get_bool_var(r) |
| 522 |
if not builder.produce_failing_guards: |
| 523 |
if getint(v): |
| 524 |
opnum = rop.GUARD_TRUE |
| 525 |
else: |
| 526 |
opnum = rop.GUARD_FALSE |
| 527 |
else: |
| 528 |
opnum = self.opnum |
| 529 |
op = ResOperation(opnum, [v]) |
| 530 |
passing = ((opnum == rop.GUARD_TRUE and getint(v)) or |
| 531 |
(opnum == rop.GUARD_FALSE and not getint(v))) |
| 532 |
return op, passing |
| 533 |
|
| 534 |
def produce_into(self, builder, r): |
| 535 |
op, passing = self.gen_guard(builder, r) |
| 536 |
builder.loop.operations.append(op) |
| 537 |
op.setdescr(builder.getfaildescr()) |
| 538 |
op.setfailargs(builder.subset_of_intvars(r)) |
| 539 |
if not passing: |
| 540 |
assert builder.produce_failing_guards |
| 541 |
builder.should_fail_by = op |
| 542 |
builder.guard_op = op |
| 543 |
|
| 544 |
class GuardPtrOperation(GuardOperation): |
| 545 |
def gen_guard(self, builder, r): |
| 546 |
if not builder.ptrvars: |
| 547 |
raise CannotProduceOperation |
| 548 |
box = r.choice(builder.ptrvars)[0] |
| 549 |
if not builder.produce_failing_guards: |
| 550 |
if getref_base(box): |
| 551 |
opnum = rop.GUARD_NONNULL |
| 552 |
else: |
| 553 |
opnum = rop.GUARD_ISNULL |
| 554 |
else: |
| 555 |
opnum = self.opnum |
| 556 |
op = ResOperation(opnum, [box]) |
| 557 |
passing = ((opnum == rop.GUARD_NONNULL and getref_base(box)) or |
| 558 |
(opnum == rop.GUARD_ISNULL and not getref_base(box))) |
| 559 |
return op, passing |
| 560 |
|
| 561 |
class GuardValueOperation(GuardOperation): |
| 562 |
def gen_guard(self, builder, r): |
| 563 |
v = r.choice(builder.intvars) |
| 564 |
if not builder.produce_failing_guards: |
| 565 |
other = ConstInt(getint(v)) |
| 566 |
elif r.random() > 0.8: |
| 567 |
other = ConstInt(getint(r.choice(builder.intvars))) |
| 568 |
else: |
| 569 |
if r.random() < 0.75: |
| 570 |
value = getint(v) |
| 571 |
elif r.random() < 0.5: |
| 572 |
value = getint(v) ^ 1 |
| 573 |
else: |
| 574 |
value = r.random_integer() |
| 575 |
other = ConstInt(value) |
| 576 |
op = ResOperation(self.opnum, [v, other]) |
| 577 |
return op, (getint(v) == getint(other)) |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
OPERATIONS = [] |
| 582 |
|
| 583 |
for _op in [rop.INT_ADD, |
| 584 |
rop.INT_SUB, |
| 585 |
rop.INT_MUL, |
| 586 |
rop.INT_AND, |
| 587 |
rop.INT_OR, |
| 588 |
rop.INT_XOR, |
| 589 |
rop.UINT_MUL_HIGH, |
| 590 |
]: |
| 591 |
OPERATIONS.append(BinaryOperation(_op)) |
| 592 |
|
| 593 |
for _op in [rop.INT_LT, |
| 594 |
rop.INT_LE, |
| 595 |
rop.INT_EQ, |
| 596 |
rop.INT_NE, |
| 597 |
rop.INT_GT, |
| 598 |
rop.INT_GE, |
| 599 |
rop.UINT_LT, |
| 600 |
rop.UINT_LE, |
| 601 |
rop.UINT_GT, |
| 602 |
rop.UINT_GE, |
| 603 |
]: |
| 604 |
OPERATIONS.append(BinaryOperation(_op, boolres=True)) |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
OPERATIONS.append(BinaryOperation(rop.INT_RSHIFT, LONG_BIT-1)) |
| 609 |
OPERATIONS.append(BinaryOperation(rop.INT_LSHIFT, LONG_BIT-1)) |
| 610 |
OPERATIONS.append(BinaryOperation(rop.UINT_RSHIFT, LONG_BIT-1)) |
| 611 |
|
| 612 |
OPERATIONS.append(GuardOperation(rop.GUARD_TRUE)) |
| 613 |
OPERATIONS.append(GuardOperation(rop.GUARD_TRUE)) |
| 614 |
OPERATIONS.append(GuardOperation(rop.GUARD_FALSE)) |
| 615 |
OPERATIONS.append(GuardOperation(rop.GUARD_FALSE)) |
| 616 |
OPERATIONS.append(GuardPtrOperation(rop.GUARD_NONNULL)) |
| 617 |
OPERATIONS.append(GuardPtrOperation(rop.GUARD_ISNULL)) |
| 618 |
OPERATIONS.append(GuardValueOperation(rop.GUARD_VALUE)) |
| 619 |
|
| 620 |
for _op in [rop.INT_NEG, |
| 621 |
rop.INT_INVERT, |
| 622 |
rop.INT_FORCE_GE_ZERO, |
| 623 |
]: |
| 624 |
OPERATIONS.append(UnaryOperation(_op)) |
| 625 |
|
| 626 |
OPERATIONS.append(UnaryOperation(rop.INT_IS_TRUE, boolres=True)) |
| 627 |
OPERATIONS.append(UnaryOperation(rop.INT_IS_ZERO, boolres=True)) |
| 628 |
OPERATIONS.append(ConstUnaryOperation(rop.SAME_AS_I, boolres='sometimes')) |
| 629 |
OPERATIONS.append(ConstUnaryFloatOperation(rop.SAME_AS_F)) |
| 630 |
OPERATIONS.append(SignExtOperation(rop.INT_SIGNEXT)) |
| 631 |
|
| 632 |
for _op in [rop.INT_ADD_OVF, |
| 633 |
rop.INT_SUB_OVF, |
| 634 |
rop.INT_MUL_OVF, |
| 635 |
]: |
| 636 |
OPERATIONS.append(BinaryOvfOperation(_op)) |
| 637 |
|
| 638 |
for _op in [rop.FLOAT_ADD, |
| 639 |
rop.FLOAT_SUB, |
| 640 |
rop.FLOAT_MUL, |
| 641 |
rop.FLOAT_TRUEDIV, |
| 642 |
]: |
| 643 |
OPERATIONS.append(BinaryFloatOperation(_op)) |
| 644 |
|
| 645 |
for _op in [rop.FLOAT_NEG, |
| 646 |
rop.FLOAT_ABS, |
| 647 |
]: |
| 648 |
OPERATIONS.append(UnaryFloatOperation(_op)) |
| 649 |
|
| 650 |
OPERATIONS.append(CastFloatToIntOperation(rop.CAST_FLOAT_TO_INT)) |
| 651 |
OPERATIONS.append(CastIntToFloatOperation(rop.CAST_INT_TO_FLOAT)) |
| 652 |
OPERATIONS.append(CastFloatToIntOperation(rop.CONVERT_FLOAT_BYTES_TO_LONGLONG)) |
| 653 |
OPERATIONS.append(CastLongLongToFloatOperation(rop.CONVERT_LONGLONG_BYTES_TO_FLOAT)) |
| 654 |
|
| 655 |
OperationBuilder.OPERATIONS = OPERATIONS |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
def do_assert(condition, error_message): |
| 660 |
if condition: |
| 661 |
return |
| 662 |
seed = pytest.config.option.randomseed |
| 663 |
message = "%s\nPython: %s\nRandom seed: %r" % ( |
| 664 |
error_message, |
| 665 |
sys.executable, |
| 666 |
seed) |
| 667 |
raise AssertionError(message) |
| 668 |
|
| 669 |
def Random(r=None): |
| 670 |
import random |
| 671 |
seed = pytest.config.option.randomseed |
| 672 |
if r is None: |
| 673 |
print |
| 674 |
print 'Random start seed value is %d.' % (seed,) |
| 675 |
print |
| 676 |
r = random.Random() |
| 677 |
r.seed(seed) |
| 678 |
def get_random_integer(): |
| 679 |
while True: |
| 680 |
result = int(r.expovariate(0.05)) |
| 681 |
if result <= sys.maxint: |
| 682 |
break |
| 683 |
if r.randrange(0, 5) <= 1: |
| 684 |
result = -result |
| 685 |
if result not in (0, -1) and r.random() < 0.1: |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
while intmask(result << 2) == (result << 2): |
| 690 |
result = (result << 2) | (result & 0x3) |
| 691 |
return result |
| 692 |
def get_random_char(): |
| 693 |
return chr(get_random_integer() % 256) |
| 694 |
def get_random_float(): |
| 695 |
x = float(get_random_integer()) |
| 696 |
k = r.random() * 1.2 |
| 697 |
if k < 1.0: |
| 698 |
x += k |
| 699 |
return x |
| 700 |
def get_random_float_storage(): |
| 701 |
x = get_random_float() |
| 702 |
return longlong.getfloatstorage(x) |
| 703 |
r.random_integer = get_random_integer |
| 704 |
r.random_char = get_random_char |
| 705 |
r.random_float = get_random_float |
| 706 |
r.random_float_storage = get_random_float_storage |
| 707 |
return r |
| 708 |
|
| 709 |
def get_cpu(): |
| 710 |
if pytest.config.option.backend == 'llgraph': |
| 711 |
from rpython.jit.backend.llgraph.runner import LLGraphCPU |
| 712 |
return LLGraphCPU(None) |
| 713 |
elif pytest.config.option.backend == 'cpu': |
| 714 |
from rpython.jit.backend.detect_cpu import getcpuclass |
| 715 |
return getcpuclass()(None, None) |
| 716 |
else: |
| 717 |
assert 0, "unknown backend %r" % pytest.config.option.backend |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
class RandomLoop(object): |
| 722 |
dont_generate_more = False |
| 723 |
|
| 724 |
def __init__(self, cpu, builder_factory, r, startvars=None, output=None): |
| 725 |
self.cpu = cpu |
| 726 |
self.output = output |
| 727 |
if startvars is None: |
| 728 |
startvars = [] |
| 729 |
|
| 730 |
n_vars = r.randrange(pytest.config.option.n_vars) + 1 |
| 731 |
if cpu.supports_floats: |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
k = r.random() |
| 736 |
|
| 737 |
at_least_once = r.randrange(n_vars) |
| 738 |
else: |
| 739 |
k = -1 |
| 740 |
at_least_once = 0 |
| 741 |
for i in range(n_vars): |
| 742 |
if r.random() < k and i != at_least_once: |
| 743 |
startvars.append(InputArgFloat(r.random_float_storage())) |
| 744 |
else: |
| 745 |
startvars.append(InputArgInt(r.random_integer())) |
| 746 |
allow_delay = True |
| 747 |
else: |
| 748 |
allow_delay = False |
| 749 |
assert len(dict.fromkeys(startvars)) == len(startvars) |
| 750 |
self.startvars = startvars |
| 751 |
self.prebuilt_ptr_consts = [] |
| 752 |
self.r = r |
| 753 |
self.subloops = [] |
| 754 |
self.build_random_loop(cpu, builder_factory, r, startvars, allow_delay) |
| 755 |
|
| 756 |
def build_random_loop(self, cpu, builder_factory, r, startvars, |
| 757 |
allow_delay): |
| 758 |
|
| 759 |
loop = TreeLoop('test_random_function') |
| 760 |
loop.inputargs = startvars[:] |
| 761 |
loop.operations = [] |
| 762 |
loop._jitcelltoken = JitCellToken() |
| 763 |
builder = builder_factory(cpu, loop, startvars[:]) |
| 764 |
if allow_delay: |
| 765 |
needs_a_label = True |
| 766 |
else: |
| 767 |
self.insert_label(loop, 0, r) |
| 768 |
needs_a_label = False |
| 769 |
self.generate_ops(builder, r, loop, startvars, needs_a_label=needs_a_label) |
| 770 |
self.builder = builder |
| 771 |
self.loop = loop |
| 772 |
dump(loop) |
| 773 |
cpu.compile_loop(loop.inputargs, loop.operations, loop._jitcelltoken) |
| 774 |
if self.output: |
| 775 |
builder.print_loop(self.output) |
| 776 |
|
| 777 |
def insert_label(self, loop, position, r): |
| 778 |
assert not hasattr(loop, '_targettoken') |
| 779 |
for i in range(position): |
| 780 |
op = loop.operations[i] |
| 781 |
if (not rop.has_no_side_effect(op.opnum) |
| 782 |
or op.type not in (INT, FLOAT)): |
| 783 |
position = i |
| 784 |
break |
| 785 |
randompos = r.randrange(0, len(self.startvars)+1) |
| 786 |
self.startvars.insert(randompos, op) |
| 787 |
loop._targettoken = TargetToken() |
| 788 |
loop.operations.insert(position, ResOperation(rop.LABEL, self.startvars, |
| 789 |
loop._targettoken)) |
| 790 |
|
| 791 |
def generate_ops(self, builder, r, loop, startvars, needs_a_label=False): |
| 792 |
block_length = pytest.config.option.block_length // 2 |
| 793 |
block_length += int(r.expovariate(1.0 / block_length)) |
| 794 |
istart = 0 |
| 795 |
|
| 796 |
for i in range(block_length): |
| 797 |
istart = len(loop.operations) |
| 798 |
try: |
| 799 |
op = r.choice(builder.OPERATIONS) |
| 800 |
op.filter(builder) |
| 801 |
op.produce_into(builder, r) |
| 802 |
except CannotProduceOperation: |
| 803 |
pass |
| 804 |
if builder.should_fail_by is not None: |
| 805 |
break |
| 806 |
if needs_a_label and r.random() < 0.2: |
| 807 |
self.insert_label(loop, istart, r) |
| 808 |
needs_a_label = False |
| 809 |
if needs_a_label: |
| 810 |
self.insert_label(loop, istart, r) |
| 811 |
|
| 812 |
endvars = [] |
| 813 |
used_later = {} |
| 814 |
for op in loop.operations: |
| 815 |
for v in op.getarglist(): |
| 816 |
used_later[v] = True |
| 817 |
for v in startvars: |
| 818 |
if v not in used_later: |
| 819 |
endvars.append(v) |
| 820 |
r.shuffle(endvars) |
| 821 |
endvars = endvars[:1] |
| 822 |
loop.operations.append(ResOperation(rop.FINISH, endvars, |
| 823 |
descr=builder.getfaildescr(is_finish=True))) |
| 824 |
if builder.should_fail_by: |
| 825 |
self.should_fail_by = builder.should_fail_by |
| 826 |
self.guard_op = builder.guard_op |
| 827 |
else: |
| 828 |
self.should_fail_by = loop.operations[-1] |
| 829 |
self.guard_op = None |
| 830 |
self.prebuilt_ptr_consts.extend(builder.prebuilt_ptr_consts) |
| 831 |
endvars = self.get_fail_args() |
| 832 |
self.expected = {} |
| 833 |
for v in endvars: |
| 834 |
if v.type == INT: |
| 835 |
self.expected[v] = getint(v) |
| 836 |
elif v.type == FLOAT: |
| 837 |
self.expected[v] = getfloatstorage(v) |
| 838 |
else: |
| 839 |
assert 0, v.type |
| 840 |
|
| 841 |
def runjitcelltoken(self): |
| 842 |
if self.startvars == self.loop.inputargs: |
| 843 |
return self.loop._jitcelltoken |
| 844 |
if not hasattr(self, '_initialjumploop_celltoken'): |
| 845 |
self._initialjumploop_celltoken = JitCellToken() |
| 846 |
args = [] |
| 847 |
for box in self.startvars: |
| 848 |
if box not in self.loop.inputargs: |
| 849 |
box = constbox(box) |
| 850 |
args.append(box) |
| 851 |
for box in self.loop.inputargs: |
| 852 |
box.set_forwarded(None) |
| 853 |
self.cpu.compile_loop(self.loop.inputargs, |
| 854 |
[ResOperation(rop.JUMP, args, |
| 855 |
descr=self.loop._targettoken)], |
| 856 |
self._initialjumploop_celltoken) |
| 857 |
return self._initialjumploop_celltoken |
| 858 |
|
| 859 |
def get_fail_args(self): |
| 860 |
if self.should_fail_by.is_guard(): |
| 861 |
assert self.should_fail_by.getfailargs() is not None |
| 862 |
return self.should_fail_by.getfailargs() |
| 863 |
else: |
| 864 |
assert self.should_fail_by.getopnum() == rop.FINISH |
| 865 |
return self.should_fail_by.getarglist() |
| 866 |
|
| 867 |
def clear_state(self): |
| 868 |
for v, S, fields in self.prebuilt_ptr_consts: |
| 869 |
container = getref_base(v)._obj.container |
| 870 |
for name, value in fields.items(): |
| 871 |
if isinstance(name, str): |
| 872 |
setattr(container, name, value) |
| 873 |
elif isinstance(value, dict): |
| 874 |
item = container.getitem(name) |
| 875 |
for key1, value1 in value.items(): |
| 876 |
setattr(item, key1, value1) |
| 877 |
else: |
| 878 |
container.setitem(name, value) |
| 879 |
|
| 880 |
def run_loop(self): |
| 881 |
cpu = self.builder.cpu |
| 882 |
self.clear_state() |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
arguments = [] |
| 888 |
for box in self.loop.inputargs: |
| 889 |
if box.type == INT: |
| 890 |
arguments.append(getint(box)) |
| 891 |
elif box.type == FLOAT: |
| 892 |
arguments.append(getfloatstorage(box)) |
| 893 |
else: |
| 894 |
assert 0, box.type |
| 895 |
deadframe = cpu.execute_token(self.runjitcelltoken(), *arguments) |
| 896 |
fail = cpu.get_latest_descr(deadframe) |
| 897 |
do_assert(fail is self.should_fail_by.getdescr(), |
| 898 |
"Got %r, expected %r" % (fail, |
| 899 |
self.should_fail_by.getdescr())) |
| 900 |
for i, v in enumerate(self.get_fail_args()): |
| 901 |
if v not in self.expected: |
| 902 |
assert v.getopnum() == rop.SAME_AS_I |
| 903 |
assert isinstance(v.getarg(0), ConstInt) |
| 904 |
self.expected[v] = getint(v.getarg(0)) |
| 905 |
if v.type == FLOAT: |
| 906 |
value = cpu.get_float_value(deadframe, i) |
| 907 |
else: |
| 908 |
value = cpu.get_int_value(deadframe, i) |
| 909 |
do_assert(value == self.expected[v], |
| 910 |
"Got %r, expected %r for value #%d" % (value, |
| 911 |
self.expected[v], |
| 912 |
i) |
| 913 |
) |
| 914 |
exc = cpu.grab_exc_value(deadframe) |
| 915 |
if (self.guard_op is not None and |
| 916 |
rop.is_guard_exception(self.guard_op.getopnum())): |
| 917 |
if self.guard_op.getopnum() == rop.GUARD_NO_EXCEPTION: |
| 918 |
do_assert(exc, |
| 919 |
"grab_exc_value() should not be %r" % (exc,)) |
| 920 |
else: |
| 921 |
do_assert(not exc, |
| 922 |
"unexpected grab_exc_value(): %r" % (exc,)) |
| 923 |
|
| 924 |
def build_bridge(self): |
| 925 |
def exc_handling(guard_op): |
| 926 |
|
| 927 |
if guard_op._exc_box is None: |
| 928 |
op = ResOperation(rop.GUARD_NO_EXCEPTION, []) |
| 929 |
else: |
| 930 |
op = ResOperation(rop.GUARD_EXCEPTION, [guard_op._exc_box]) |
| 931 |
op.setdescr(self.builder.getfaildescr()) |
| 932 |
op.setfailargs([]) |
| 933 |
return op |
| 934 |
|
| 935 |
if self.dont_generate_more: |
| 936 |
return False |
| 937 |
r = self.r |
| 938 |
guard_op = self.guard_op |
| 939 |
fail_args = guard_op.getfailargs() |
| 940 |
fail_descr = guard_op.getdescr() |
| 941 |
op = self.should_fail_by |
| 942 |
if not op.getfailargs(): |
| 943 |
return False |
| 944 |
for _fail_box in fail_args: |
| 945 |
_fail_box.set_forwarded(None) |
| 946 |
|
| 947 |
subloop = DummyLoop([]) |
| 948 |
self.subloops.append(subloop) |
| 949 |
if rop.is_guard_exception(guard_op.getopnum()): |
| 950 |
subloop.operations.append(exc_handling(guard_op)) |
| 951 |
bridge_builder = self.builder.fork(self.builder.cpu, subloop, |
| 952 |
op.getfailargs()[:]) |
| 953 |
self.generate_ops(bridge_builder, r, subloop, op.getfailargs()[:]) |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
if r.random() < 0.1 and self.guard_op is None: |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
subset = [] |
| 971 |
for box in self.loop.inputargs: |
| 972 |
srcbox = r.choice(fail_args) |
| 973 |
if srcbox.type != box.type: |
| 974 |
if box.type == INT: |
| 975 |
srcbox = ConstInt(r.random_integer()) |
| 976 |
elif box.type == FLOAT: |
| 977 |
srcbox = ConstFloat(r.random_float_storage()) |
| 978 |
else: |
| 979 |
raise AssertionError(box.type) |
| 980 |
subset.append(srcbox) |
| 981 |
|
| 982 |
args = [] |
| 983 |
for x in subset: |
| 984 |
if x.type == INT: |
| 985 |
args.append(InputArgInt(getint(x))) |
| 986 |
elif x.type == FLOAT: |
| 987 |
args.append(InputArgFloat(getfloatstorage(x))) |
| 988 |
else: |
| 989 |
assert 0, x.type |
| 990 |
rl = RandomLoop(self.builder.cpu, self.builder.fork, |
| 991 |
r, args) |
| 992 |
|
| 993 |
self.should_fail_by = rl.should_fail_by |
| 994 |
self.expected = rl.expected |
| 995 |
assert len(rl.loop.inputargs) == len(args) |
| 996 |
|
| 997 |
|
| 998 |
jump_op = ResOperation(rop.JUMP, subset, |
| 999 |
descr=rl.loop._targettoken) |
| 1000 |
subloop.operations[-1] = jump_op |
| 1001 |
self.guard_op = rl.guard_op |
| 1002 |
self.prebuilt_ptr_consts += rl.prebuilt_ptr_consts |
| 1003 |
self.loop._jitcelltoken.record_jump_to(rl.loop._jitcelltoken) |
| 1004 |
self.dont_generate_more = True |
| 1005 |
if r.random() < .05: |
| 1006 |
return False |
| 1007 |
dump(subloop) |
| 1008 |
self.builder.cpu.compile_bridge(fail_descr, fail_args, |
| 1009 |
subloop.operations, |
| 1010 |
self.loop._jitcelltoken) |
| 1011 |
|
| 1012 |
if self.output: |
| 1013 |
bridge_builder.print_loop(self.output, fail_descr, fail_args) |
| 1014 |
return True |
| 1015 |
|
| 1016 |
def dump(loop): |
| 1017 |
print >> sys.stderr, loop |
| 1018 |
if hasattr(loop, 'inputargs'): |
| 1019 |
print >> sys.stderr, '\t', loop.inputargs |
| 1020 |
for op in loop.operations: |
| 1021 |
if op.is_guard(): |
| 1022 |
print >> sys.stderr, '\t', op, op.getfailargs() |
| 1023 |
else: |
| 1024 |
print >> sys.stderr, '\t', op |
| 1025 |
|
| 1026 |
def check_random_function(cpu, BuilderClass, r, num=None, max=None): |
| 1027 |
if pytest.config.option.output: |
| 1028 |
output = open(pytest.config.option.output, "w") |
| 1029 |
else: |
| 1030 |
output = None |
| 1031 |
loop = RandomLoop(cpu, BuilderClass, r, output=output) |
| 1032 |
while True: |
| 1033 |
loop.run_loop() |
| 1034 |
if loop.guard_op is not None: |
| 1035 |
if not loop.build_bridge(): |
| 1036 |
break |
| 1037 |
else: |
| 1038 |
break |
| 1039 |
if num is not None: |
| 1040 |
print ' # passed (%d/%d).' % (num + 1, max) |
| 1041 |
else: |
| 1042 |
print ' # passed.' |
| 1043 |
if pytest.config.option.output: |
| 1044 |
output.close() |
| 1045 |
print |
| 1046 |
|
| 1047 |
def test_random_function(BuilderClass=OperationBuilder): |
| 1048 |
r = Random() |
| 1049 |
cpu = get_cpu() |
| 1050 |
cpu.setup_once() |
| 1051 |
if pytest.config.option.repeat == -1: |
| 1052 |
while 1: |
| 1053 |
check_random_function(cpu, BuilderClass, r) |
| 1054 |
else: |
| 1055 |
for i in range(pytest.config.option.repeat): |
| 1056 |
check_random_function(cpu, BuilderClass, r, i, |
| 1057 |
pytest.config.option.repeat) |