| 1 |
"""This implements pyjitpl's execution of operations. |
| 2 |
""" |
| 3 |
|
| 4 |
from rpython.rtyper.lltypesystem import lltype, rstr, llmemory |
| 5 |
from rpython.rlib.rarithmetic import ovfcheck, r_longlong, is_valid_int |
| 6 |
from rpython.rlib.unroll import unrolling_iterable |
| 7 |
from rpython.rlib.objectmodel import specialize |
| 8 |
from rpython.rlib.debug import fatalerror |
| 9 |
from rpython.jit.metainterp.history import check_descr |
| 10 |
from rpython.jit.metainterp.history import INT, REF, FLOAT, VOID, AbstractDescr |
| 11 |
from rpython.jit.metainterp.history import ConstInt, ConstFloat, ConstPtr |
| 12 |
from rpython.jit.metainterp import resoperation |
| 13 |
from rpython.jit.metainterp.resoperation import rop, opname |
| 14 |
from rpython.jit.metainterp.blackhole import BlackholeInterpreter, NULL |
| 15 |
from rpython.jit.codewriter import longlong |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
@specialize.arg(4) |
| 20 |
def _do_call(cpu, metainterp, argboxes, descr, rettype): |
| 21 |
assert metainterp is not None |
| 22 |
|
| 23 |
count_i = count_r = count_f = 0 |
| 24 |
for i in range(1, len(argboxes)): |
| 25 |
type = argboxes[i].type |
| 26 |
if type == INT: count_i += 1 |
| 27 |
elif type == REF: count_r += 1 |
| 28 |
elif type == FLOAT: count_f += 1 |
| 29 |
|
| 30 |
if count_i: args_i = [0] * count_i |
| 31 |
else: args_i = None |
| 32 |
if count_r: args_r = [NULL] * count_r |
| 33 |
else: args_r = None |
| 34 |
if count_f: args_f = [longlong.ZEROF] * count_f |
| 35 |
else: args_f = None |
| 36 |
|
| 37 |
count_i = count_r = count_f = 0 |
| 38 |
for i in range(1, len(argboxes)): |
| 39 |
box = argboxes[i] |
| 40 |
if box.type == INT: |
| 41 |
args_i[count_i] = box.getint() |
| 42 |
count_i += 1 |
| 43 |
elif box.type == REF: |
| 44 |
args_r[count_r] = box.getref_base() |
| 45 |
count_r += 1 |
| 46 |
elif box.type == FLOAT: |
| 47 |
args_f[count_f] = box.getfloatstorage() |
| 48 |
count_f += 1 |
| 49 |
|
| 50 |
func = argboxes[0].getint() |
| 51 |
|
| 52 |
if rettype == INT: |
| 53 |
try: |
| 54 |
result = cpu.bh_call_i(func, args_i, args_r, args_f, descr) |
| 55 |
except Exception as e: |
| 56 |
metainterp.execute_raised(e) |
| 57 |
result = 0 |
| 58 |
return result |
| 59 |
if rettype == REF: |
| 60 |
try: |
| 61 |
result = cpu.bh_call_r(func, args_i, args_r, args_f, descr) |
| 62 |
except Exception as e: |
| 63 |
metainterp.execute_raised(e) |
| 64 |
result = NULL |
| 65 |
return result |
| 66 |
if rettype == FLOAT: |
| 67 |
try: |
| 68 |
result = cpu.bh_call_f(func, args_i, args_r, args_f, descr) |
| 69 |
except Exception as e: |
| 70 |
metainterp.execute_raised(e) |
| 71 |
result = longlong.ZEROF |
| 72 |
return result |
| 73 |
if rettype == VOID: |
| 74 |
try: |
| 75 |
cpu.bh_call_v(func, args_i, args_r, args_f, descr) |
| 76 |
except Exception as e: |
| 77 |
metainterp.execute_raised(e) |
| 78 |
return None |
| 79 |
raise AssertionError("bad rettype") |
| 80 |
|
| 81 |
def new_do_call(rettype): |
| 82 |
def do_call(cpu, metainterp, argboxes, descr): |
| 83 |
return _do_call(cpu, metainterp, argboxes, descr, rettype) |
| 84 |
do_call.__name__ = "do_call_" + rettype |
| 85 |
return do_call |
| 86 |
|
| 87 |
do_call_r = new_do_call("r") |
| 88 |
do_call_i = new_do_call("i") |
| 89 |
do_call_f = new_do_call("f") |
| 90 |
do_call_n = new_do_call("v") |
| 91 |
do_call_loopinvariant_r = do_call_r |
| 92 |
do_call_loopinvariant_i = do_call_i |
| 93 |
do_call_loopinvariant_f = do_call_f |
| 94 |
do_call_loopinvariant_n = do_call_n |
| 95 |
do_call_may_force_r = do_call_r |
| 96 |
do_call_may_force_i = do_call_i |
| 97 |
do_call_may_force_f = do_call_f |
| 98 |
do_call_may_force_n = do_call_n |
| 99 |
|
| 100 |
def do_cond_call(cpu, metainterp, argboxes, descr): |
| 101 |
condbox = argboxes[0] |
| 102 |
if condbox.getint(): |
| 103 |
do_call_n(cpu, metainterp, argboxes[1:], descr) |
| 104 |
|
| 105 |
def do_cond_call_value_i(cpu, metainterp, argboxes, descr): |
| 106 |
value = argboxes[0].getint() |
| 107 |
if value == 0: |
| 108 |
value = do_call_i(cpu, metainterp, argboxes[1:], descr) |
| 109 |
return value |
| 110 |
|
| 111 |
def do_cond_call_value_r(cpu, metainterp, argboxes, descr): |
| 112 |
value = argboxes[0].getref_base() |
| 113 |
if not value: |
| 114 |
value = do_call_r(cpu, metainterp, argboxes[1:], descr) |
| 115 |
return value |
| 116 |
|
| 117 |
def do_getarrayitem_gc_i(cpu, _, arraybox, indexbox, arraydescr): |
| 118 |
array = arraybox.getref_base() |
| 119 |
index = indexbox.getint() |
| 120 |
return cpu.bh_getarrayitem_gc_i(array, index, arraydescr) |
| 121 |
|
| 122 |
def do_getarrayitem_gc_r(cpu, _, arraybox, indexbox, arraydescr): |
| 123 |
array = arraybox.getref_base() |
| 124 |
index = indexbox.getint() |
| 125 |
return cpu.bh_getarrayitem_gc_r(array, index, arraydescr) |
| 126 |
|
| 127 |
def do_getarrayitem_gc_f(cpu, _, arraybox, indexbox, arraydescr): |
| 128 |
array = arraybox.getref_base() |
| 129 |
index = indexbox.getint() |
| 130 |
return cpu.bh_getarrayitem_gc_f(array, index, arraydescr) |
| 131 |
|
| 132 |
def do_getarrayitem_raw_i(cpu, _, arraybox, indexbox, arraydescr): |
| 133 |
array = arraybox.getint() |
| 134 |
index = indexbox.getint() |
| 135 |
return cpu.bh_getarrayitem_raw_i(array, index, arraydescr) |
| 136 |
|
| 137 |
def do_getarrayitem_raw_f(cpu, _, arraybox, indexbox, arraydescr): |
| 138 |
array = arraybox.getint() |
| 139 |
index = indexbox.getint() |
| 140 |
return cpu.bh_getarrayitem_raw_f(array, index, arraydescr) |
| 141 |
|
| 142 |
def do_setarrayitem_gc(cpu, _, arraybox, indexbox, itembox, arraydescr): |
| 143 |
array = arraybox.getref_base() |
| 144 |
index = indexbox.getint() |
| 145 |
if arraydescr.is_array_of_pointers(): |
| 146 |
cpu.bh_setarrayitem_gc_r(array, index, itembox.getref_base(), |
| 147 |
arraydescr) |
| 148 |
elif arraydescr.is_array_of_floats(): |
| 149 |
cpu.bh_setarrayitem_gc_f(array, index, itembox.getfloatstorage(), |
| 150 |
arraydescr) |
| 151 |
else: |
| 152 |
cpu.bh_setarrayitem_gc_i(array, index, itembox.getint(), arraydescr) |
| 153 |
|
| 154 |
def do_setarrayitem_raw(cpu, _, arraybox, indexbox, itembox, arraydescr): |
| 155 |
array = arraybox.getint() |
| 156 |
index = indexbox.getint() |
| 157 |
assert not arraydescr.is_array_of_pointers() |
| 158 |
if arraydescr.is_array_of_floats(): |
| 159 |
cpu.bh_setarrayitem_raw_f(array, index, itembox.getfloatstorage(), |
| 160 |
arraydescr) |
| 161 |
else: |
| 162 |
cpu.bh_setarrayitem_raw_i(array, index, itembox.getint(), arraydescr) |
| 163 |
|
| 164 |
def do_getinteriorfield_gc(cpu, _, arraybox, indexbox, descr): |
| 165 |
raise Exception("implement me") |
| 166 |
xxxx |
| 167 |
array = arraybox.getref_base() |
| 168 |
index = indexbox.getint() |
| 169 |
if descr.is_pointer_field(): |
| 170 |
return BoxPtr(cpu.bh_getinteriorfield_gc_r(array, index, descr)) |
| 171 |
elif descr.is_float_field(): |
| 172 |
return BoxFloat(cpu.bh_getinteriorfield_gc_f(array, index, descr)) |
| 173 |
else: |
| 174 |
return BoxInt(cpu.bh_getinteriorfield_gc_i(array, index, descr)) |
| 175 |
|
| 176 |
def do_setinteriorfield_gc(cpu, _, arraybox, indexbox, valuebox, descr): |
| 177 |
array = arraybox.getref_base() |
| 178 |
index = indexbox.getint() |
| 179 |
if descr.is_pointer_field(): |
| 180 |
cpu.bh_setinteriorfield_gc_r(array, index, valuebox.getref_base(), |
| 181 |
descr) |
| 182 |
elif descr.is_float_field(): |
| 183 |
cpu.bh_setinteriorfield_gc_f(array, index, valuebox.getfloatstorage(), |
| 184 |
descr) |
| 185 |
else: |
| 186 |
cpu.bh_setinteriorfield_gc_i(array, index, valuebox.getint(), descr) |
| 187 |
|
| 188 |
def do_getfield_gc_i(cpu, _, structbox, fielddescr): |
| 189 |
struct = structbox.getref_base() |
| 190 |
return cpu.bh_getfield_gc_i(struct, fielddescr) |
| 191 |
|
| 192 |
def do_getfield_gc_r(cpu, _, structbox, fielddescr): |
| 193 |
struct = structbox.getref_base() |
| 194 |
return cpu.bh_getfield_gc_r(struct, fielddescr) |
| 195 |
|
| 196 |
def do_getfield_gc_f(cpu, _, structbox, fielddescr): |
| 197 |
struct = structbox.getref_base() |
| 198 |
return cpu.bh_getfield_gc_f(struct, fielddescr) |
| 199 |
|
| 200 |
def do_getfield_raw_i(cpu, _, structbox, fielddescr): |
| 201 |
check_descr(fielddescr) |
| 202 |
struct = structbox.getint() |
| 203 |
return cpu.bh_getfield_raw_i(struct, fielddescr) |
| 204 |
|
| 205 |
def do_getfield_raw_f(cpu, _, structbox, fielddescr): |
| 206 |
check_descr(fielddescr) |
| 207 |
struct = structbox.getint() |
| 208 |
return cpu.bh_getfield_raw_f(struct, fielddescr) |
| 209 |
|
| 210 |
def do_getfield_raw_r(cpu, _, structbox, fielddescr): |
| 211 |
check_descr(fielddescr) |
| 212 |
struct = structbox.getint() |
| 213 |
return cpu.bh_getfield_raw_r(struct, fielddescr) |
| 214 |
|
| 215 |
def do_setfield_gc(cpu, _, structbox, itembox, fielddescr): |
| 216 |
struct = structbox.getref_base() |
| 217 |
if fielddescr.is_pointer_field(): |
| 218 |
cpu.bh_setfield_gc_r(struct, itembox.getref_base(), fielddescr) |
| 219 |
elif fielddescr.is_float_field(): |
| 220 |
cpu.bh_setfield_gc_f(struct, itembox.getfloatstorage(), fielddescr) |
| 221 |
else: |
| 222 |
cpu.bh_setfield_gc_i(struct, itembox.getint(), fielddescr) |
| 223 |
|
| 224 |
def do_setfield_raw(cpu, _, structbox, itembox, fielddescr): |
| 225 |
struct = structbox.getint() |
| 226 |
assert not fielddescr.is_pointer_field() |
| 227 |
if fielddescr.is_float_field(): |
| 228 |
cpu.bh_setfield_raw_f(struct, itembox.getfloatstorage(), fielddescr) |
| 229 |
else: |
| 230 |
cpu.bh_setfield_raw_i(struct, itembox.getint(), fielddescr) |
| 231 |
|
| 232 |
def do_raw_store(cpu, _, addrbox, offsetbox, valuebox, arraydescr): |
| 233 |
addr = addrbox.getint() |
| 234 |
offset = offsetbox.getint() |
| 235 |
if arraydescr.is_array_of_pointers(): |
| 236 |
raise AssertionError("cannot store GC pointers in raw store") |
| 237 |
elif arraydescr.is_array_of_floats(): |
| 238 |
cpu.bh_raw_store_f(addr, offset, valuebox.getfloatstorage(),arraydescr) |
| 239 |
else: |
| 240 |
cpu.bh_raw_store_i(addr, offset, valuebox.getint(), arraydescr) |
| 241 |
|
| 242 |
def do_raw_load(cpu, _, addrbox, offsetbox, arraydescr): |
| 243 |
raise Exception("implement me") |
| 244 |
xxx |
| 245 |
addr = addrbox.getint() |
| 246 |
offset = offsetbox.getint() |
| 247 |
if arraydescr.is_array_of_pointers(): |
| 248 |
raise AssertionError("cannot store GC pointers in raw store") |
| 249 |
elif arraydescr.is_array_of_floats(): |
| 250 |
return BoxFloat(cpu.bh_raw_load_f(addr, offset, arraydescr)) |
| 251 |
else: |
| 252 |
return BoxInt(cpu.bh_raw_load_i(addr, offset, arraydescr)) |
| 253 |
|
| 254 |
def do_gc_store_indexed(cpu, _, addrbox, indexbox, valuebox, scalebox, |
| 255 |
base_ofsbox, bytesbox, arraydescr): |
| 256 |
addr = addrbox.getref_base() |
| 257 |
index = indexbox.getint() |
| 258 |
scale = scalebox.getint() |
| 259 |
base_ofs = base_ofsbox.getint() |
| 260 |
bytes = bytesbox.getint() |
| 261 |
if arraydescr.is_array_of_pointers(): |
| 262 |
raise AssertionError("cannot store GC pointers in gc_store_indexed for now") |
| 263 |
elif arraydescr.is_array_of_floats(): |
| 264 |
floatval = valuebox.getfloatstorage() |
| 265 |
cpu.bh_gc_store_indexed_f(addr, index, floatval, scale, base_ofs, bytes, |
| 266 |
arraydescr) |
| 267 |
else: |
| 268 |
intval = valuebox.getint() |
| 269 |
cpu.bh_gc_store_indexed_i(addr, index, intval, scale, base_ofs, bytes, |
| 270 |
arraydescr) |
| 271 |
|
| 272 |
|
| 273 |
def exec_new_with_vtable(cpu, descr): |
| 274 |
return cpu.bh_new_with_vtable(descr) |
| 275 |
|
| 276 |
def do_new_with_vtable(cpu, _, clsbox): |
| 277 |
return exec_new_with_vtable(cpu, clsbox) |
| 278 |
|
| 279 |
def do_int_add_ovf(cpu, metainterp, box1, box2): |
| 280 |
|
| 281 |
|
| 282 |
a = box1.getint() |
| 283 |
b = box2.getint() |
| 284 |
try: |
| 285 |
z = ovfcheck(a + b) |
| 286 |
except OverflowError: |
| 287 |
assert metainterp is not None |
| 288 |
metainterp.ovf_flag = True |
| 289 |
z = 0 |
| 290 |
return z |
| 291 |
|
| 292 |
def do_int_sub_ovf(cpu, metainterp, box1, box2): |
| 293 |
a = box1.getint() |
| 294 |
b = box2.getint() |
| 295 |
try: |
| 296 |
z = ovfcheck(a - b) |
| 297 |
except OverflowError: |
| 298 |
assert metainterp is not None |
| 299 |
metainterp.ovf_flag = True |
| 300 |
z = 0 |
| 301 |
return z |
| 302 |
|
| 303 |
def do_int_mul_ovf(cpu, metainterp, box1, box2): |
| 304 |
a = box1.getint() |
| 305 |
b = box2.getint() |
| 306 |
try: |
| 307 |
z = ovfcheck(a * b) |
| 308 |
except OverflowError: |
| 309 |
assert metainterp is not None |
| 310 |
metainterp.ovf_flag = True |
| 311 |
z = 0 |
| 312 |
return z |
| 313 |
|
| 314 |
def do_same_as_i(cpu, _, v): |
| 315 |
return v.getint() |
| 316 |
|
| 317 |
def do_same_as_r(cpu, _, v): |
| 318 |
return v.getref_base() |
| 319 |
|
| 320 |
def do_same_as_f(cpu, _, v): |
| 321 |
return v.getfloatstorage() |
| 322 |
|
| 323 |
def do_copystrcontent(cpu, _, srcbox, dstbox, |
| 324 |
srcstartbox, dststartbox, lengthbox): |
| 325 |
src = srcbox.getref(lltype.Ptr(rstr.STR)) |
| 326 |
dst = dstbox.getref(lltype.Ptr(rstr.STR)) |
| 327 |
srcstart = srcstartbox.getint() |
| 328 |
dststart = dststartbox.getint() |
| 329 |
length = lengthbox.getint() |
| 330 |
rstr.copy_string_contents(src, dst, srcstart, dststart, length) |
| 331 |
|
| 332 |
def do_copyunicodecontent(cpu, _, srcbox, dstbox, |
| 333 |
srcstartbox, dststartbox, lengthbox): |
| 334 |
src = srcbox.getref(lltype.Ptr(rstr.UNICODE)) |
| 335 |
dst = dstbox.getref(lltype.Ptr(rstr.UNICODE)) |
| 336 |
srcstart = srcstartbox.getint() |
| 337 |
dststart = dststartbox.getint() |
| 338 |
length = lengthbox.getint() |
| 339 |
rstr.copy_unicode_contents(src, dst, srcstart, dststart, length) |
| 340 |
|
| 341 |
def do_keepalive(cpu, _, x): |
| 342 |
pass |
| 343 |
|
| 344 |
def do_assert_not_none(cpu, _, box): |
| 345 |
if not box.getref_base(): |
| 346 |
fatalerror("found during JITting: ll_assert_not_none() failed") |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
def _make_execute_list(): |
| 352 |
execute_by_num_args = {} |
| 353 |
for key in opname.values(): |
| 354 |
value = getattr(rop, key) |
| 355 |
if not key.startswith('_'): |
| 356 |
if (rop._FINAL_FIRST <= value <= rop._FINAL_LAST or |
| 357 |
rop._GUARD_FIRST <= value <= rop._GUARD_LAST): |
| 358 |
continue |
| 359 |
|
| 360 |
num_args = resoperation.oparity[value] |
| 361 |
withdescr = resoperation.opwithdescr[value] |
| 362 |
dictkey = num_args, withdescr |
| 363 |
if dictkey not in execute_by_num_args: |
| 364 |
execute_by_num_args[dictkey] = [None] * (rop._LAST+1) |
| 365 |
execute = execute_by_num_args[dictkey] |
| 366 |
|
| 367 |
if execute[value] is not None: |
| 368 |
raise AssertionError("duplicate entry for op number %d"% value) |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
name = 'do_' + key.lower() |
| 375 |
if name in globals(): |
| 376 |
execute[value] = globals()[name] |
| 377 |
continue |
| 378 |
|
| 379 |
|
| 380 |
if key[-7:-2] == '_PURE': |
| 381 |
key = key[:-7] + key[-2:] |
| 382 |
name = 'do_' + key.lower() |
| 383 |
if name in globals(): |
| 384 |
execute[value] = globals()[name] |
| 385 |
continue |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
name = 'bhimpl_' + key.lower() |
| 392 |
if hasattr(BlackholeInterpreter, name): |
| 393 |
func = make_execute_function( |
| 394 |
key.lower(), |
| 395 |
getattr(BlackholeInterpreter, name).im_func) |
| 396 |
if func is not None: |
| 397 |
execute[value] = func |
| 398 |
continue |
| 399 |
if value in (rop.FORCE_TOKEN, |
| 400 |
rop.CALL_ASSEMBLER_R, |
| 401 |
rop.CALL_ASSEMBLER_F, |
| 402 |
rop.CALL_ASSEMBLER_I, |
| 403 |
rop.CALL_ASSEMBLER_N, |
| 404 |
rop.INCREMENT_DEBUG_COUNTER, |
| 405 |
rop.COND_CALL_VALUE_R, |
| 406 |
rop.COND_CALL_VALUE_I, |
| 407 |
rop.COND_CALL_GC_WB, |
| 408 |
rop.COND_CALL_GC_WB_ARRAY, |
| 409 |
rop.ZERO_ARRAY, |
| 410 |
rop.DEBUG_MERGE_POINT, |
| 411 |
rop.JIT_DEBUG, |
| 412 |
rop.ENTER_PORTAL_FRAME, |
| 413 |
rop.LEAVE_PORTAL_FRAME, |
| 414 |
rop.SETARRAYITEM_RAW, |
| 415 |
rop.SETINTERIORFIELD_RAW, |
| 416 |
rop.CALL_RELEASE_GIL_I, |
| 417 |
rop.CALL_RELEASE_GIL_F, |
| 418 |
rop.CALL_RELEASE_GIL_N, |
| 419 |
rop.QUASIIMMUT_FIELD, |
| 420 |
rop.CHECK_MEMORY_ERROR, |
| 421 |
rop.CALL_MALLOC_NURSERY, |
| 422 |
rop.CALL_MALLOC_NURSERY_VARSIZE, |
| 423 |
rop.CALL_MALLOC_NURSERY_VARSIZE_FRAME, |
| 424 |
rop.NURSERY_PTR_INCREMENT, |
| 425 |
rop.LABEL, |
| 426 |
rop.ESCAPE_I, |
| 427 |
rop.ESCAPE_N, |
| 428 |
rop.ESCAPE_R, |
| 429 |
rop.ESCAPE_F, |
| 430 |
rop.FORCE_SPILL, |
| 431 |
rop.SAVE_EXC_CLASS, |
| 432 |
rop.SAVE_EXCEPTION, |
| 433 |
rop.RESTORE_EXCEPTION, |
| 434 |
rop.VEC_LOAD_I, |
| 435 |
rop.VEC_LOAD_F, |
| 436 |
rop.GC_LOAD_I, |
| 437 |
rop.GC_LOAD_R, |
| 438 |
rop.GC_LOAD_F, |
| 439 |
rop.GC_LOAD_INDEXED_R, |
| 440 |
rop.VEC_STORE, |
| 441 |
rop.GC_STORE, |
| 442 |
rop.GC_STORE_INDEXED, |
| 443 |
rop.LOAD_FROM_GC_TABLE, |
| 444 |
rop.LOAD_EFFECTIVE_ADDRESS, |
| 445 |
rop.RECORD_KNOWN_RESULT, |
| 446 |
): |
| 447 |
continue |
| 448 |
if rop._VEC_PURE_FIRST <= value <= rop._VEC_PURE_LAST: |
| 449 |
continue |
| 450 |
|
| 451 |
raise AssertionError("missing %r" % (key,)) |
| 452 |
return execute_by_num_args |
| 453 |
|
| 454 |
def make_execute_function(name, func): |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
for argtype in func.argtypes: |
| 459 |
if argtype not in ('i', 'r', 'f', 'd', 'cpu'): |
| 460 |
return None |
| 461 |
if list(func.argtypes).count('d') > 1: |
| 462 |
return None |
| 463 |
argtypes = unrolling_iterable(func.argtypes) |
| 464 |
|
| 465 |
def do(cpu, _, *argboxes): |
| 466 |
newargs = () |
| 467 |
for argtype in argtypes: |
| 468 |
if argtype == 'cpu': |
| 469 |
value = cpu |
| 470 |
elif argtype == 'd': |
| 471 |
value = argboxes[-1] |
| 472 |
assert isinstance(value, AbstractDescr) |
| 473 |
argboxes = argboxes[:-1] |
| 474 |
else: |
| 475 |
argbox = argboxes[0] |
| 476 |
argboxes = argboxes[1:] |
| 477 |
if argtype == 'i': value = argbox.getint() |
| 478 |
elif argtype == 'r': value = argbox.getref_base() |
| 479 |
elif argtype == 'f': value = argbox.getfloatstorage() |
| 480 |
newargs = newargs + (value,) |
| 481 |
assert not argboxes |
| 482 |
|
| 483 |
return func(*newargs) |
| 484 |
|
| 485 |
do.__name__ = 'do_' + name |
| 486 |
return do |
| 487 |
|
| 488 |
@specialize.memo() |
| 489 |
def get_execute_funclist(num_args, withdescr): |
| 490 |
|
| 491 |
return EXECUTE_BY_NUM_ARGS[num_args, withdescr] |
| 492 |
|
| 493 |
@specialize.memo() |
| 494 |
def get_execute_function(opnum, num_args, withdescr): |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
func = EXECUTE_BY_NUM_ARGS[num_args, withdescr][opnum] |
| 500 |
|
| 501 |
|
| 502 |
return func |
| 503 |
|
| 504 |
@specialize.memo() |
| 505 |
def has_descr(opnum): |
| 506 |
|
| 507 |
return resoperation.opwithdescr[opnum] |
| 508 |
|
| 509 |
@specialize.arg(2) |
| 510 |
def execute(cpu, metainterp, opnum, descr, *argboxes): |
| 511 |
|
| 512 |
num_args = len(argboxes) |
| 513 |
withdescr = has_descr(opnum) |
| 514 |
if withdescr: |
| 515 |
check_descr(descr) |
| 516 |
argboxes = argboxes + (descr,) |
| 517 |
else: |
| 518 |
assert descr is None |
| 519 |
func = get_execute_function(opnum, num_args, withdescr) |
| 520 |
return func(cpu, metainterp, *argboxes) |
| 521 |
|
| 522 |
|
| 523 |
@specialize.arg(2) |
| 524 |
def execute_varargs(cpu, metainterp, opnum, argboxes, descr): |
| 525 |
|
| 526 |
check_descr(descr) |
| 527 |
func = get_execute_function(opnum, -1, True) |
| 528 |
return func(cpu, metainterp, argboxes, descr) |
| 529 |
|
| 530 |
@specialize.argtype(0) |
| 531 |
def wrap_constant(value): |
| 532 |
if lltype.typeOf(value) == lltype.Signed: |
| 533 |
return ConstInt(value) |
| 534 |
elif isinstance(value, bool): |
| 535 |
return ConstInt(int(value)) |
| 536 |
elif lltype.typeOf(value) == longlong.FLOATSTORAGE: |
| 537 |
return ConstFloat(value) |
| 538 |
elif isinstance(value, float): |
| 539 |
return ConstFloat(longlong.getfloatstorage(value)) |
| 540 |
else: |
| 541 |
assert lltype.typeOf(value) == llmemory.GCREF |
| 542 |
return ConstPtr(value) |
| 543 |
|
| 544 |
def constant_from_op(op): |
| 545 |
if op.type == 'i': |
| 546 |
return ConstInt(op.getint()) |
| 547 |
elif op.type == 'r': |
| 548 |
return ConstPtr(op.getref_base()) |
| 549 |
else: |
| 550 |
assert op.type == 'f' |
| 551 |
return ConstFloat(op.getfloatstorage()) |
| 552 |
|
| 553 |
unrolled_range = unrolling_iterable(range(rop._LAST)) |
| 554 |
|
| 555 |
def execute_nonspec_const(cpu, metainterp, opnum, argboxes, descr=None, |
| 556 |
type='i'): |
| 557 |
for num in unrolled_range: |
| 558 |
if num == opnum: |
| 559 |
return wrap_constant(_execute_arglist(cpu, metainterp, num, |
| 560 |
argboxes, descr)) |
| 561 |
assert False |
| 562 |
|
| 563 |
@specialize.arg(2) |
| 564 |
def _execute_arglist(cpu, metainterp, opnum, argboxes, descr=None): |
| 565 |
arity = resoperation.oparity[opnum] |
| 566 |
assert arity == -1 or len(argboxes) == arity |
| 567 |
if resoperation.opwithdescr[opnum]: |
| 568 |
check_descr(descr) |
| 569 |
if arity == -1: |
| 570 |
func = get_execute_function(opnum, -1, True) |
| 571 |
if func: |
| 572 |
return func(cpu, metainterp, argboxes, descr) |
| 573 |
if arity == 0: |
| 574 |
func = get_execute_function(opnum, 0, True) |
| 575 |
if func: |
| 576 |
return func(cpu, metainterp, descr) |
| 577 |
if arity == 1: |
| 578 |
func = get_execute_function(opnum, 1, True) |
| 579 |
if func: |
| 580 |
return func(cpu, metainterp, argboxes[0], descr) |
| 581 |
if arity == 2: |
| 582 |
func = get_execute_function(opnum, 2, True) |
| 583 |
if func: |
| 584 |
return func(cpu, metainterp, argboxes[0], argboxes[1], descr) |
| 585 |
if arity == 3: |
| 586 |
func = get_execute_function(opnum, 3, True) |
| 587 |
if func: |
| 588 |
return func(cpu, metainterp, argboxes[0], argboxes[1], |
| 589 |
argboxes[2], descr) |
| 590 |
else: |
| 591 |
assert descr is None |
| 592 |
if arity == 1: |
| 593 |
func = get_execute_function(opnum, 1, False) |
| 594 |
if func: |
| 595 |
return func(cpu, metainterp, argboxes[0]) |
| 596 |
if arity == 2: |
| 597 |
func = get_execute_function(opnum, 2, False) |
| 598 |
if func: |
| 599 |
return func(cpu, metainterp, argboxes[0], argboxes[1]) |
| 600 |
if arity == 3: |
| 601 |
func = get_execute_function(opnum, 3, False) |
| 602 |
if func: |
| 603 |
return func(cpu, metainterp, argboxes[0], argboxes[1], |
| 604 |
argboxes[2]) |
| 605 |
if arity == 5: |
| 606 |
func = get_execute_function(opnum, 5, False) |
| 607 |
if func: |
| 608 |
return func(cpu, metainterp, argboxes[0], argboxes[1], |
| 609 |
argboxes[2], argboxes[3], argboxes[4]) |
| 610 |
raise NotImplementedError |
| 611 |
|
| 612 |
|
| 613 |
EXECUTE_BY_NUM_ARGS = _make_execute_list() |