| 1 |
import random, sys |
| 2 |
from rpython.flowspace.operation import op |
| 3 |
from rpython.rlib import debug |
| 4 |
from rpython.rlib.rarithmetic import is_valid_int |
| 5 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 6 |
from rpython.tool.sourcetools import func_with_new_name |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
ops_returning_a_bool = {'gt': True, 'ge': True, |
| 14 |
'lt': True, 'le': True, |
| 15 |
'eq': True, 'ne': True, |
| 16 |
'bool': True, 'is_true':True} |
| 17 |
|
| 18 |
|
| 19 |
from rpython.rlib.rarithmetic import intmask |
| 20 |
from rpython.rlib.rarithmetic import r_int, r_uint, r_longlong, r_ulonglong, r_longlonglong, r_ulonglonglong |
| 21 |
from rpython.rtyper.lltypesystem.llmemory import AddressAsInt |
| 22 |
|
| 23 |
if r_longlong is r_int: |
| 24 |
r_longlong_arg = (r_longlong, int, long) |
| 25 |
r_longlong_result = long |
| 26 |
else: |
| 27 |
r_longlong_arg = r_longlong |
| 28 |
r_longlong_result = r_longlong |
| 29 |
|
| 30 |
|
| 31 |
r_longlonglong_arg = r_longlonglong |
| 32 |
r_longlonglong_result = r_longlonglong |
| 33 |
|
| 34 |
argtype_by_name = { |
| 35 |
'int': (int, long), |
| 36 |
'float': float, |
| 37 |
'uint': r_uint, |
| 38 |
'llong': r_longlong_arg, |
| 39 |
'ullong': r_ulonglong, |
| 40 |
'lllong': r_longlonglong, |
| 41 |
'ulllong': r_ulonglonglong, |
| 42 |
} |
| 43 |
|
| 44 |
def no_op(x): |
| 45 |
return x |
| 46 |
|
| 47 |
def get_primitive_op_src(fullopname): |
| 48 |
assert '_' in fullopname, "%s: not a primitive op" % (fullopname,) |
| 49 |
typname, opname = fullopname.split('_', 1) |
| 50 |
if hasattr(op, opname): |
| 51 |
oper = getattr(op, opname) |
| 52 |
elif hasattr(op, opname + '_'): |
| 53 |
oper = getattr(op, opname + '_') |
| 54 |
else: |
| 55 |
raise ValueError("%s: not a primitive op" % (fullopname,)) |
| 56 |
func = oper.pyfunc |
| 57 |
|
| 58 |
if typname == 'char': |
| 59 |
|
| 60 |
def op_function(x, y): |
| 61 |
if not isinstance(x, str) or len(x) != 1: |
| 62 |
raise TypeError("%r arg must be a char, got %r instead" % ( |
| 63 |
fullopname, type(x).__name__)) |
| 64 |
if not isinstance(y, str) or len(y) != 1: |
| 65 |
raise TypeError("%r arg must be a char, got %r instead" % ( |
| 66 |
fullopname, type(y).__name__)) |
| 67 |
return func(x, y) |
| 68 |
|
| 69 |
else: |
| 70 |
if typname == 'int' and opname not in ops_returning_a_bool: |
| 71 |
adjust_result = intmask |
| 72 |
else: |
| 73 |
adjust_result = no_op |
| 74 |
assert typname in argtype_by_name, "%s: not a primitive op" % ( |
| 75 |
fullopname,) |
| 76 |
argtype = argtype_by_name[typname] |
| 77 |
|
| 78 |
if oper.arity == 1: |
| 79 |
def op_function(x): |
| 80 |
if not isinstance(x, argtype): |
| 81 |
raise TypeError("%r arg must be %s, got %r instead" % ( |
| 82 |
fullopname, typname, type(x).__name__)) |
| 83 |
return adjust_result(func(x)) |
| 84 |
else: |
| 85 |
def op_function(x, y): |
| 86 |
if not isinstance(x, argtype): |
| 87 |
raise TypeError("%r arg 1 must be %s, got %r instead"% ( |
| 88 |
fullopname, typname, type(x).__name__)) |
| 89 |
if not isinstance(y, argtype): |
| 90 |
raise TypeError("%r arg 2 must be %s, got %r instead"% ( |
| 91 |
fullopname, typname, type(y).__name__)) |
| 92 |
return adjust_result(func(x, y)) |
| 93 |
|
| 94 |
return func_with_new_name(op_function, 'op_' + fullopname) |
| 95 |
|
| 96 |
def checkptr(ptr): |
| 97 |
if not isinstance(lltype.typeOf(ptr), lltype.Ptr): |
| 98 |
raise TypeError("arg must be a pointer, got %r instead" % ( |
| 99 |
lltype.typeOf(ptr),)) |
| 100 |
|
| 101 |
def checkadr(adr): |
| 102 |
if lltype.typeOf(adr) is not llmemory.Address: |
| 103 |
raise TypeError("arg must be an address, got %r instead" % ( |
| 104 |
lltype.typeOf(adr),)) |
| 105 |
|
| 106 |
|
| 107 |
def op_int_eq(x, y): |
| 108 |
if not isinstance(x, (int, long)): |
| 109 |
from rpython.rtyper.lltypesystem import llgroup |
| 110 |
assert isinstance(x, llgroup.CombinedSymbolic), ( |
| 111 |
"'int_eq' arg 1 must be int-like, got %r instead" % ( |
| 112 |
type(x).__name__,)) |
| 113 |
if not isinstance(y, (int, long)): |
| 114 |
from rpython.rtyper.lltypesystem import llgroup |
| 115 |
assert isinstance(y, llgroup.CombinedSymbolic), ( |
| 116 |
"'int_eq' arg 2 must be int-like, got %r instead" % ( |
| 117 |
type(y).__name__,)) |
| 118 |
return x == y |
| 119 |
|
| 120 |
def op_ptr_eq(ptr1, ptr2): |
| 121 |
checkptr(ptr1) |
| 122 |
checkptr(ptr2) |
| 123 |
return ptr1 == ptr2 |
| 124 |
|
| 125 |
def op_ptr_ne(ptr1, ptr2): |
| 126 |
checkptr(ptr1) |
| 127 |
checkptr(ptr2) |
| 128 |
return ptr1 != ptr2 |
| 129 |
|
| 130 |
def op_ptr_nonzero(ptr1): |
| 131 |
checkptr(ptr1) |
| 132 |
return bool(ptr1) |
| 133 |
|
| 134 |
def op_ptr_iszero(ptr1): |
| 135 |
checkptr(ptr1) |
| 136 |
return not bool(ptr1) |
| 137 |
|
| 138 |
def op_getsubstruct(obj, field): |
| 139 |
checkptr(obj) |
| 140 |
|
| 141 |
assert isinstance(getattr(lltype.typeOf(obj).TO, field), |
| 142 |
lltype.ContainerType) |
| 143 |
return getattr(obj, field) |
| 144 |
|
| 145 |
def op_getarraysubstruct(array, index): |
| 146 |
checkptr(array) |
| 147 |
result = array[index] |
| 148 |
return result |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
def op_getinteriorarraysize(obj, *offsets): |
| 153 |
checkptr(obj) |
| 154 |
ob = obj |
| 155 |
for o in offsets: |
| 156 |
if isinstance(o, str): |
| 157 |
ob = getattr(ob, o) |
| 158 |
else: |
| 159 |
ob = ob[o] |
| 160 |
return len(ob) |
| 161 |
|
| 162 |
def op_getinteriorfield(obj, *offsets): |
| 163 |
checkptr(obj) |
| 164 |
ob = obj |
| 165 |
for o in offsets: |
| 166 |
innermostcontainer = ob |
| 167 |
if isinstance(o, str): |
| 168 |
ob = getattr(ob, o) |
| 169 |
else: |
| 170 |
ob = ob[o] |
| 171 |
|
| 172 |
|
| 173 |
T = lltype.typeOf(innermostcontainer).TO |
| 174 |
if not T._immutable_field(offsets[-1]): |
| 175 |
raise TypeError("cannot fold getinteriorfield on mutable struct") |
| 176 |
assert not isinstance(ob, lltype._interior_ptr) |
| 177 |
return ob |
| 178 |
|
| 179 |
def op_getarraysize(array): |
| 180 |
checkptr(array) |
| 181 |
return len(array) |
| 182 |
|
| 183 |
def op_direct_fieldptr(obj, field): |
| 184 |
checkptr(obj) |
| 185 |
assert isinstance(field, str) |
| 186 |
return lltype.direct_fieldptr(obj, field) |
| 187 |
|
| 188 |
def op_direct_arrayitems(obj): |
| 189 |
checkptr(obj) |
| 190 |
return lltype.direct_arrayitems(obj) |
| 191 |
|
| 192 |
def op_direct_ptradd(obj, index): |
| 193 |
checkptr(obj) |
| 194 |
assert is_valid_int(index) |
| 195 |
if not obj: |
| 196 |
raise AssertionError("direct_ptradd on null pointer") |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
return lltype.direct_ptradd(obj, index) |
| 202 |
|
| 203 |
|
| 204 |
def op_bool_not(b): |
| 205 |
assert type(b) is bool |
| 206 |
return not b |
| 207 |
|
| 208 |
def op_int_add(x, y): |
| 209 |
if not isinstance(x, (int, long, llmemory.AddressOffset)): |
| 210 |
from rpython.rtyper.lltypesystem import llgroup |
| 211 |
assert isinstance(x, llgroup.CombinedSymbolic) |
| 212 |
assert isinstance(y, (int, long, llmemory.AddressOffset)) |
| 213 |
return intmask(x + y) |
| 214 |
|
| 215 |
def op_int_sub(x, y): |
| 216 |
if not is_valid_int(x): |
| 217 |
from rpython.rtyper.lltypesystem import llgroup |
| 218 |
assert isinstance(x, llgroup.CombinedSymbolic) |
| 219 |
assert is_valid_int(y) |
| 220 |
return intmask(x - y) |
| 221 |
|
| 222 |
def op_int_ge(x, y): |
| 223 |
|
| 224 |
assert isinstance(x, (int, long, llmemory.AddressOffset)) |
| 225 |
assert is_valid_int(y) |
| 226 |
return x >= y |
| 227 |
|
| 228 |
def op_int_lt(x, y): |
| 229 |
|
| 230 |
|
| 231 |
assert isinstance(x, (int, long, llmemory.AddressOffset)) |
| 232 |
assert is_valid_int(y) |
| 233 |
return x < y |
| 234 |
|
| 235 |
def op_int_between(a, b, c): |
| 236 |
assert lltype.typeOf(a) is lltype.Signed |
| 237 |
assert lltype.typeOf(b) is lltype.Signed |
| 238 |
assert lltype.typeOf(c) is lltype.Signed |
| 239 |
return a <= b < c |
| 240 |
|
| 241 |
def op_int_force_ge_zero(a): |
| 242 |
assert lltype.typeOf(a) is lltype.Signed |
| 243 |
if a < 0: |
| 244 |
return 0 |
| 245 |
return a |
| 246 |
|
| 247 |
def op_int_and(x, y): |
| 248 |
if not is_valid_int(x): |
| 249 |
from rpython.rtyper.lltypesystem import llgroup |
| 250 |
assert isinstance(x, llgroup.CombinedSymbolic) |
| 251 |
assert is_valid_int(y) |
| 252 |
return x & y |
| 253 |
|
| 254 |
def op_int_or(x, y): |
| 255 |
if not is_valid_int(x): |
| 256 |
from rpython.rtyper.lltypesystem import llgroup |
| 257 |
assert isinstance(x, llgroup.CombinedSymbolic) |
| 258 |
assert is_valid_int(y) |
| 259 |
return x | y |
| 260 |
|
| 261 |
def op_int_xor(x, y): |
| 262 |
|
| 263 |
if isinstance(x, AddressAsInt): x = llmemory.cast_adr_to_int(x.adr) |
| 264 |
if isinstance(y, AddressAsInt): y = llmemory.cast_adr_to_int(y.adr) |
| 265 |
assert is_valid_int(x) |
| 266 |
assert is_valid_int(y) |
| 267 |
return x ^ y |
| 268 |
|
| 269 |
def op_int_mul(x, y): |
| 270 |
assert isinstance(x, (int, long, llmemory.AddressOffset)) |
| 271 |
assert isinstance(y, (int, long, llmemory.AddressOffset)) |
| 272 |
return intmask(x * y) |
| 273 |
|
| 274 |
def op_int_rshift(x, y): |
| 275 |
if not is_valid_int(x): |
| 276 |
from rpython.rtyper.lltypesystem import llgroup |
| 277 |
assert isinstance(x, llgroup.CombinedSymbolic) |
| 278 |
assert is_valid_int(y) |
| 279 |
return x >> y |
| 280 |
|
| 281 |
def op_int_floordiv(x, y): |
| 282 |
|
| 283 |
assert isinstance(x, (int, long, llmemory.AddressOffset)) |
| 284 |
assert isinstance(y, (int, long, llmemory.AddressOffset)) |
| 285 |
r = x//y |
| 286 |
if x^y < 0 and x%y != 0: |
| 287 |
r += 1 |
| 288 |
return r |
| 289 |
|
| 290 |
def op_int_mod(x, y): |
| 291 |
assert isinstance(x, (int, long, llmemory.AddressOffset)) |
| 292 |
assert isinstance(y, (int, long, llmemory.AddressOffset)) |
| 293 |
r = x%y |
| 294 |
if x^y < 0 and x%y != 0: |
| 295 |
r -= y |
| 296 |
return r |
| 297 |
|
| 298 |
def op_llong_floordiv(x, y): |
| 299 |
assert isinstance(x, r_longlong_arg) |
| 300 |
assert isinstance(y, r_longlong_arg) |
| 301 |
r = x//y |
| 302 |
if x^y < 0 and x%y != 0: |
| 303 |
r += 1 |
| 304 |
return r |
| 305 |
|
| 306 |
def op_llong_mod(x, y): |
| 307 |
assert isinstance(x, r_longlong_arg) |
| 308 |
assert isinstance(y, r_longlong_arg) |
| 309 |
r = x%y |
| 310 |
if x^y < 0 and x%y != 0: |
| 311 |
r -= y |
| 312 |
return r |
| 313 |
|
| 314 |
def op_lllong_floordiv(x, y): |
| 315 |
assert isinstance(x, r_longlonglong_arg) |
| 316 |
assert isinstance(y, r_longlonglong_arg) |
| 317 |
r = x//y |
| 318 |
if x^y < 0 and x%y != 0: |
| 319 |
r += 1 |
| 320 |
return r |
| 321 |
|
| 322 |
def op_lllong_mod(x, y): |
| 323 |
assert isinstance(x, r_longlonglong_arg) |
| 324 |
assert isinstance(y, r_longlonglong_arg) |
| 325 |
r = x%y |
| 326 |
if x^y < 0 and x%y != 0: |
| 327 |
r -= y |
| 328 |
return r |
| 329 |
|
| 330 |
def op_uint_lshift(x, y): |
| 331 |
assert isinstance(x, r_uint) |
| 332 |
assert is_valid_int(y) |
| 333 |
return r_uint(x << y) |
| 334 |
|
| 335 |
def op_uint_rshift(x, y): |
| 336 |
assert isinstance(x, r_uint) |
| 337 |
assert is_valid_int(y) |
| 338 |
return r_uint(x >> y) |
| 339 |
|
| 340 |
def op_llong_lshift(x, y): |
| 341 |
assert isinstance(x, r_longlong_arg) |
| 342 |
assert is_valid_int(y) |
| 343 |
return r_longlong_result(x << y) |
| 344 |
|
| 345 |
def op_llong_rshift(x, y): |
| 346 |
assert isinstance(x, r_longlong_arg) |
| 347 |
assert is_valid_int(y) |
| 348 |
return r_longlong_result(x >> y) |
| 349 |
|
| 350 |
def op_lllong_lshift(x, y): |
| 351 |
assert isinstance(x, r_longlonglong_arg) |
| 352 |
assert is_valid_int(y) |
| 353 |
return r_longlonglong_result(x << y) |
| 354 |
|
| 355 |
def op_lllong_rshift(x, y): |
| 356 |
assert isinstance(x, r_longlonglong_arg) |
| 357 |
assert is_valid_int(y) |
| 358 |
return r_longlonglong_result(x >> y) |
| 359 |
|
| 360 |
def op_ullong_lshift(x, y): |
| 361 |
assert isinstance(x, r_ulonglong) |
| 362 |
assert isinstance(y, int) |
| 363 |
return r_ulonglong(x << y) |
| 364 |
|
| 365 |
def op_ullong_rshift(x, y): |
| 366 |
assert isinstance(x, r_ulonglong) |
| 367 |
assert is_valid_int(y) |
| 368 |
return r_ulonglong(x >> y) |
| 369 |
|
| 370 |
def op_ulllong_lshift(x, y): |
| 371 |
assert isinstance(x, r_ulonglonglong) |
| 372 |
assert isinstance(y, int) |
| 373 |
return r_ulonglonglong(x << y) |
| 374 |
|
| 375 |
def op_ulllong_rshift(x, y): |
| 376 |
assert isinstance(x, r_ulonglonglong) |
| 377 |
assert is_valid_int(y) |
| 378 |
return r_ulonglonglong(x >> y) |
| 379 |
|
| 380 |
def op_same_as(x): |
| 381 |
return x |
| 382 |
|
| 383 |
def op_cast_primitive(TYPE, value): |
| 384 |
assert isinstance(lltype.typeOf(value), lltype.Primitive) |
| 385 |
return lltype.cast_primitive(TYPE, value) |
| 386 |
op_cast_primitive.need_result_type = True |
| 387 |
|
| 388 |
def op_cast_int_to_float(i): |
| 389 |
|
| 390 |
assert is_valid_int(i) |
| 391 |
return float(i) |
| 392 |
|
| 393 |
def op_cast_uint_to_float(u): |
| 394 |
assert type(u) is r_uint |
| 395 |
return float(u) |
| 396 |
|
| 397 |
def op_cast_longlong_to_float(i): |
| 398 |
assert isinstance(i, r_longlong_arg) |
| 399 |
|
| 400 |
li = float(int(i & r_longlong(0x7fffffff))) |
| 401 |
ui = float(int(i >> 31)) * float(0x80000000) |
| 402 |
return ui + li |
| 403 |
|
| 404 |
def op_cast_ulonglong_to_float(i): |
| 405 |
assert isinstance(i, r_ulonglong) |
| 406 |
|
| 407 |
li = float(int(i & r_ulonglong(0xffffffff))) |
| 408 |
ui = float(int(i >> 32)) * float(0x100000000) |
| 409 |
return ui + li |
| 410 |
|
| 411 |
def op_cast_int_to_char(b): |
| 412 |
|
| 413 |
assert is_valid_int(b) |
| 414 |
return chr(b) |
| 415 |
|
| 416 |
def op_cast_bool_to_int(b): |
| 417 |
assert type(b) is bool |
| 418 |
return int(b) |
| 419 |
|
| 420 |
def op_cast_bool_to_uint(b): |
| 421 |
assert type(b) is bool |
| 422 |
return r_uint(int(b)) |
| 423 |
|
| 424 |
def op_cast_bool_to_float(b): |
| 425 |
assert type(b) is bool |
| 426 |
return float(b) |
| 427 |
|
| 428 |
def op_cast_float_to_int(f): |
| 429 |
assert type(f) is float |
| 430 |
return intmask(int(f)) |
| 431 |
|
| 432 |
def op_cast_float_to_uint(f): |
| 433 |
assert type(f) is float |
| 434 |
return r_uint(long(f)) |
| 435 |
|
| 436 |
def op_cast_float_to_longlong(f): |
| 437 |
assert type(f) is float |
| 438 |
r = float(0x100000000) |
| 439 |
small = f / r |
| 440 |
high = int(small) |
| 441 |
truncated = int((small - high) * r) |
| 442 |
return r_longlong_result(high) * 0x100000000 + truncated |
| 443 |
|
| 444 |
def op_cast_float_to_ulonglong(f): |
| 445 |
assert type(f) is float |
| 446 |
return r_ulonglong(long(f)) |
| 447 |
|
| 448 |
def op_cast_char_to_int(b): |
| 449 |
assert type(b) is str and len(b) == 1 |
| 450 |
return ord(b) |
| 451 |
|
| 452 |
def op_cast_unichar_to_int(b): |
| 453 |
assert type(b) is unicode and len(b) == 1 |
| 454 |
return ord(b) |
| 455 |
|
| 456 |
def op_cast_int_to_unichar(b): |
| 457 |
assert is_valid_int(b) |
| 458 |
return unichr(b) |
| 459 |
|
| 460 |
def op_cast_int_to_uint(b): |
| 461 |
|
| 462 |
assert is_valid_int(b) |
| 463 |
return r_uint(b) |
| 464 |
|
| 465 |
def op_cast_uint_to_int(b): |
| 466 |
assert type(b) is r_uint |
| 467 |
return intmask(b) |
| 468 |
|
| 469 |
def op_cast_int_to_longlong(b): |
| 470 |
assert is_valid_int(b) |
| 471 |
return r_longlong_result(b) |
| 472 |
|
| 473 |
def op_truncate_longlong_to_int(b): |
| 474 |
assert isinstance(b, r_longlong_arg) |
| 475 |
return intmask(b) |
| 476 |
|
| 477 |
def op_cast_pointer(RESTYPE, obj): |
| 478 |
checkptr(obj) |
| 479 |
return lltype.cast_pointer(RESTYPE, obj) |
| 480 |
op_cast_pointer.need_result_type = True |
| 481 |
|
| 482 |
def op_cast_adr_to_ptr(TYPE, adr): |
| 483 |
checkadr(adr) |
| 484 |
return llmemory.cast_adr_to_ptr(adr, TYPE) |
| 485 |
op_cast_adr_to_ptr.need_result_type = True |
| 486 |
|
| 487 |
def op_cast_int_to_adr(int): |
| 488 |
return llmemory.cast_int_to_adr(int) |
| 489 |
|
| 490 |
def op_convert_float_bytes_to_longlong(a): |
| 491 |
from rpython.rlib.longlong2float import float2longlong |
| 492 |
return float2longlong(a) |
| 493 |
|
| 494 |
def op_convert_longlong_bytes_to_float(a): |
| 495 |
from rpython.rlib.longlong2float import longlong2float |
| 496 |
return longlong2float(a) |
| 497 |
|
| 498 |
|
| 499 |
def op_unichar_eq(x, y): |
| 500 |
assert isinstance(x, unicode) and len(x) == 1 |
| 501 |
assert isinstance(y, unicode) and len(y) == 1 |
| 502 |
return x == y |
| 503 |
|
| 504 |
def op_unichar_ne(x, y): |
| 505 |
assert isinstance(x, unicode) and len(x) == 1 |
| 506 |
assert isinstance(y, unicode) and len(y) == 1 |
| 507 |
return x != y |
| 508 |
|
| 509 |
|
| 510 |
def op_adr_lt(addr1, addr2): |
| 511 |
checkadr(addr1) |
| 512 |
checkadr(addr2) |
| 513 |
return addr1 < addr2 |
| 514 |
|
| 515 |
def op_adr_le(addr1, addr2): |
| 516 |
checkadr(addr1) |
| 517 |
checkadr(addr2) |
| 518 |
return addr1 <= addr2 |
| 519 |
|
| 520 |
def op_adr_eq(addr1, addr2): |
| 521 |
checkadr(addr1) |
| 522 |
checkadr(addr2) |
| 523 |
return addr1 == addr2 |
| 524 |
|
| 525 |
def op_adr_ne(addr1, addr2): |
| 526 |
checkadr(addr1) |
| 527 |
checkadr(addr2) |
| 528 |
return addr1 != addr2 |
| 529 |
|
| 530 |
def op_adr_gt(addr1, addr2): |
| 531 |
checkadr(addr1) |
| 532 |
checkadr(addr2) |
| 533 |
return addr1 > addr2 |
| 534 |
|
| 535 |
def op_adr_ge(addr1, addr2): |
| 536 |
checkadr(addr1) |
| 537 |
checkadr(addr2) |
| 538 |
return addr1 >= addr2 |
| 539 |
|
| 540 |
def op_adr_add(addr, offset): |
| 541 |
checkadr(addr) |
| 542 |
assert lltype.typeOf(offset) is lltype.Signed |
| 543 |
return addr + offset |
| 544 |
|
| 545 |
def op_adr_sub(addr, offset): |
| 546 |
checkadr(addr) |
| 547 |
assert lltype.typeOf(offset) is lltype.Signed |
| 548 |
return addr - offset |
| 549 |
|
| 550 |
def op_adr_delta(addr1, addr2): |
| 551 |
checkadr(addr1) |
| 552 |
checkadr(addr2) |
| 553 |
return addr1 - addr2 |
| 554 |
|
| 555 |
def op_gc_writebarrier_before_copy(source, dest, |
| 556 |
source_start, dest_start, length): |
| 557 |
A = lltype.typeOf(source) |
| 558 |
assert A == lltype.typeOf(dest) |
| 559 |
if isinstance(A.TO, lltype.GcArray): |
| 560 |
if isinstance(A.TO.OF, lltype.Ptr): |
| 561 |
assert A.TO.OF.TO._gckind == 'gc' |
| 562 |
else: |
| 563 |
assert isinstance(A.TO.OF, lltype.Struct) |
| 564 |
else: |
| 565 |
assert isinstance(A.TO, lltype.GcStruct) |
| 566 |
assert A.TO._arrayfld is not None |
| 567 |
assert type(source_start) is int |
| 568 |
assert type(dest_start) is int |
| 569 |
assert type(length) is int |
| 570 |
return True |
| 571 |
|
| 572 |
def op_gc_writebarrier_before_move(array): |
| 573 |
A = lltype.typeOf(array) |
| 574 |
if isinstance(A.TO, lltype.GcArray): |
| 575 |
if isinstance(A.TO.OF, lltype.Ptr): |
| 576 |
assert A.TO.OF.TO._gckind == 'gc' |
| 577 |
else: |
| 578 |
assert isinstance(A.TO.OF, lltype.Struct) |
| 579 |
else: |
| 580 |
assert isinstance(A.TO, lltype.GcStruct) |
| 581 |
assert A.TO._arrayfld is not None |
| 582 |
|
| 583 |
def op_getfield(p, name): |
| 584 |
checkptr(p) |
| 585 |
TYPE = lltype.typeOf(p).TO |
| 586 |
if not TYPE._immutable_field(name): |
| 587 |
raise TypeError("cannot fold getfield on mutable struct") |
| 588 |
return getattr(p, name) |
| 589 |
|
| 590 |
def op_getarrayitem(p, index): |
| 591 |
checkptr(p) |
| 592 |
ARRAY = lltype.typeOf(p).TO |
| 593 |
if not ARRAY._immutable_field(index): |
| 594 |
raise TypeError("cannot fold getarrayitem on mutable array") |
| 595 |
return p[index] |
| 596 |
|
| 597 |
def _normalize(x): |
| 598 |
if not isinstance(x, str): |
| 599 |
TYPE = lltype.typeOf(x) |
| 600 |
if isinstance(TYPE, lltype.Ptr) and TYPE.TO._name == 'rpy_string': |
| 601 |
from rpython.rtyper.annlowlevel import hlstr |
| 602 |
return hlstr(x) |
| 603 |
return x |
| 604 |
|
| 605 |
def op_debug_flush_log(): |
| 606 |
debug.debug_flush_log() |
| 607 |
|
| 608 |
def op_debug_print(*args): |
| 609 |
debug.debug_print(*map(_normalize, args)) |
| 610 |
|
| 611 |
def op_debug_start(category, timestamp): |
| 612 |
return debug.debug_start(_normalize(category), timestamp) |
| 613 |
|
| 614 |
def op_debug_stop(category, timestamp): |
| 615 |
return debug.debug_stop(_normalize(category), timestamp) |
| 616 |
|
| 617 |
def op_debug_offset(): |
| 618 |
return debug.debug_offset() |
| 619 |
|
| 620 |
def op_debug_flush(): |
| 621 |
pass |
| 622 |
|
| 623 |
def op_have_debug_prints(): |
| 624 |
return debug.have_debug_prints() |
| 625 |
|
| 626 |
def op_have_debug_prints_for(prefix): |
| 627 |
return True |
| 628 |
|
| 629 |
def op_debug_nonnull_pointer(x): |
| 630 |
assert x |
| 631 |
|
| 632 |
def op_gc_stack_bottom(): |
| 633 |
pass |
| 634 |
|
| 635 |
def op_jit_force_virtualizable(*args): |
| 636 |
pass |
| 637 |
|
| 638 |
def op_jit_force_virtual(x): |
| 639 |
return x |
| 640 |
|
| 641 |
def op_jit_is_virtual(x): |
| 642 |
return False |
| 643 |
|
| 644 |
def op_jit_force_quasi_immutable(*args): |
| 645 |
pass |
| 646 |
|
| 647 |
def op_jit_record_exact_class(x, y): |
| 648 |
pass |
| 649 |
|
| 650 |
def op_jit_record_exact_value(x, y): |
| 651 |
pass |
| 652 |
|
| 653 |
def op_jit_ffi_save_result(*args): |
| 654 |
pass |
| 655 |
|
| 656 |
def op_jit_enter_portal_frame(x): |
| 657 |
pass |
| 658 |
|
| 659 |
def op_jit_leave_portal_frame(): |
| 660 |
pass |
| 661 |
|
| 662 |
def op_get_group_member(TYPE, grpptr, memberoffset): |
| 663 |
from rpython.rtyper.lltypesystem import llgroup |
| 664 |
assert isinstance(memberoffset, llgroup.GroupMemberOffset) |
| 665 |
member = memberoffset._get_group_member(grpptr) |
| 666 |
return lltype.cast_pointer(TYPE, member) |
| 667 |
op_get_group_member.need_result_type = True |
| 668 |
|
| 669 |
def op_get_next_group_member(TYPE, grpptr, memberoffset, skipoffset): |
| 670 |
from rpython.rtyper.lltypesystem import llgroup |
| 671 |
assert isinstance(memberoffset, llgroup.GroupMemberOffset) |
| 672 |
member = memberoffset._get_next_group_member(grpptr, skipoffset) |
| 673 |
return lltype.cast_pointer(TYPE, member) |
| 674 |
op_get_next_group_member.need_result_type = True |
| 675 |
|
| 676 |
def op_is_group_member_nonzero(memberoffset): |
| 677 |
from rpython.rtyper.lltypesystem import llgroup |
| 678 |
if isinstance(memberoffset, llgroup.GroupMemberOffset): |
| 679 |
return memberoffset.index != 0 |
| 680 |
else: |
| 681 |
assert is_valid_int(memberoffset) |
| 682 |
return memberoffset != 0 |
| 683 |
|
| 684 |
def op_extract_ushort(combinedoffset): |
| 685 |
from rpython.rtyper.lltypesystem import llgroup |
| 686 |
assert isinstance(combinedoffset, llgroup.CombinedSymbolic) |
| 687 |
return combinedoffset.lowpart |
| 688 |
|
| 689 |
def op_combine_ushort(ushort, rest): |
| 690 |
from rpython.rtyper.lltypesystem import llgroup |
| 691 |
return llgroup.CombinedSymbolic(ushort, rest) |
| 692 |
|
| 693 |
def op_gc_gettypeptr_group(TYPE, obj, grpptr, skipoffset, vtableinfo): |
| 694 |
HDR = vtableinfo[0] |
| 695 |
size_gc_header = vtableinfo[1] |
| 696 |
fieldname = vtableinfo[2] |
| 697 |
objaddr = llmemory.cast_ptr_to_adr(obj) |
| 698 |
hdraddr = objaddr - size_gc_header |
| 699 |
hdr = llmemory.cast_adr_to_ptr(hdraddr, lltype.Ptr(HDR)) |
| 700 |
typeid = getattr(hdr, fieldname) |
| 701 |
if lltype.typeOf(typeid) == lltype.Signed: |
| 702 |
typeid = op_extract_ushort(typeid) |
| 703 |
return op_get_next_group_member(TYPE, grpptr, typeid, skipoffset) |
| 704 |
op_gc_gettypeptr_group.need_result_type = True |
| 705 |
|
| 706 |
def op_get_member_index(memberoffset): |
| 707 |
raise NotImplementedError |
| 708 |
|
| 709 |
def op_gc_writebarrier(addr): |
| 710 |
pass |
| 711 |
|
| 712 |
def op_gc_bit(hdr, bitmask): |
| 713 |
if hdr.tid & bitmask: |
| 714 |
return random.randrange(1, sys.maxint) |
| 715 |
return 0 |
| 716 |
|
| 717 |
def op_shrink_array(array, smallersize): |
| 718 |
return False |
| 719 |
|
| 720 |
def op_ll_read_timestamp(): |
| 721 |
from rpython.rlib.rtimer import read_timestamp |
| 722 |
return read_timestamp() |
| 723 |
|
| 724 |
def op_ll_get_timestamp_unit(): |
| 725 |
from rpython.rlib.rtimer import get_timestamp_unit |
| 726 |
return get_timestamp_unit() |
| 727 |
|
| 728 |
def op_debug_fatalerror(ll_msg): |
| 729 |
from rpython.rtyper.lltypesystem import lltype, rstr |
| 730 |
from rpython.rtyper.llinterp import LLFatalError |
| 731 |
assert lltype.typeOf(ll_msg) == lltype.Ptr(rstr.STR) |
| 732 |
msg = ''.join(ll_msg.chars) |
| 733 |
raise LLFatalError(msg) |
| 734 |
|
| 735 |
def op_raw_store(p, ofs, newvalue): |
| 736 |
from rpython.rtyper.lltypesystem import rffi |
| 737 |
p = rffi.cast(llmemory.Address, p) |
| 738 |
TVAL = lltype.typeOf(newvalue) |
| 739 |
p = rffi.cast(rffi.CArrayPtr(TVAL), p + ofs) |
| 740 |
p[0] = newvalue |
| 741 |
|
| 742 |
def op_gc_store(p, ofs, newvalue): |
| 743 |
from rpython.rtyper.lltypesystem import rffi |
| 744 |
if lltype.typeOf(p) is not llmemory.Address: |
| 745 |
p = llmemory.cast_ptr_to_adr(p) |
| 746 |
TVAL = lltype.typeOf(newvalue) |
| 747 |
p = llmemory.cast_adr_to_ptr(p + ofs, lltype.Ptr(lltype.FixedSizeArray(TVAL, 1))) |
| 748 |
p[0] = newvalue |
| 749 |
|
| 750 |
def op_raw_load(TVAL, p, ofs): |
| 751 |
from rpython.rtyper.lltypesystem import rffi |
| 752 |
p = rffi.cast(llmemory.Address, p) |
| 753 |
p = rffi.cast(rffi.CArrayPtr(TVAL), p + ofs) |
| 754 |
return p[0] |
| 755 |
op_raw_load.need_result_type = True |
| 756 |
|
| 757 |
def op_gc_load_indexed(TVAL, p, index, scale, base_ofs): |
| 758 |
|
| 759 |
|
| 760 |
from rpython.rtyper.lltypesystem import rffi |
| 761 |
ofs = base_ofs + scale * index |
| 762 |
if isinstance(ofs, int): |
| 763 |
return op_raw_load(TVAL, p, ofs) |
| 764 |
p = rffi.cast(rffi.CArrayPtr(TVAL), llmemory.cast_ptr_to_adr(p) + ofs) |
| 765 |
return p[0] |
| 766 |
op_gc_load_indexed.need_result_type = True |
| 767 |
|
| 768 |
def op_gc_store_indexed(p, index, newvalue, scale, base_ofs): |
| 769 |
|
| 770 |
|
| 771 |
from rpython.rtyper.lltypesystem import rffi |
| 772 |
TVAL = lltype.typeOf(newvalue) |
| 773 |
ofs = base_ofs + scale * index |
| 774 |
if isinstance(ofs, int): |
| 775 |
return op_raw_store(p, ofs, newvalue) |
| 776 |
p = rffi.cast(rffi.CArrayPtr(TVAL), llmemory.cast_ptr_to_adr(p) + ofs) |
| 777 |
p[0] = newvalue |
| 778 |
|
| 779 |
def op_likely(x): |
| 780 |
assert isinstance(x, bool) |
| 781 |
return x |
| 782 |
|
| 783 |
def op_unlikely(x): |
| 784 |
assert isinstance(x, bool) |
| 785 |
return x |
| 786 |
|
| 787 |
def op_gc_ignore_finalizer(obj): |
| 788 |
pass |
| 789 |
|
| 790 |
def op_gc_move_out_of_nursery(obj): |
| 791 |
return obj |
| 792 |
|
| 793 |
def op_gc_increase_root_stack_depth(new_depth): |
| 794 |
pass |
| 795 |
|
| 796 |
def op_revdb_do_next_call(): |
| 797 |
pass |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
def get_op_impl(opname): |
| 802 |
|
| 803 |
try: |
| 804 |
return globals()['op_' + opname] |
| 805 |
except KeyError: |
| 806 |
return get_primitive_op_src(opname) |