| 1 |
import sys |
| 2 |
|
| 3 |
from rpython.annotator import model as annmodel |
| 4 |
from rpython.rtyper.llannotation import lltype_to_annotation |
| 5 |
from rpython.annotator.policy import AnnotatorPolicy |
| 6 |
from rpython.flowspace.model import Variable, Constant |
| 7 |
from rpython.rlib import rgc |
| 8 |
from rpython.rlib.jit import elidable, oopspec |
| 9 |
from rpython.rlib.rarithmetic import r_longlong, r_ulonglong, r_uint, intmask |
| 10 |
from rpython.rlib.rarithmetic import LONG_BIT |
| 11 |
from rpython.rtyper import rlist |
| 12 |
from rpython.rtyper.lltypesystem import rlist as rlist_ll |
| 13 |
from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator |
| 14 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 15 |
from rpython.rtyper.llinterp import LLInterpreter |
| 16 |
from rpython.rtyper.lltypesystem import lltype, rffi, llmemory, rstr as ll_rstr, rdict as ll_rdict |
| 17 |
from rpython.rtyper import rclass |
| 18 |
from rpython.rtyper.lltypesystem import rordereddict |
| 19 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 20 |
from rpython.rtyper.lltypesystem.module import ll_math |
| 21 |
from rpython.translator.translator import TranslationContext |
| 22 |
from rpython.translator.unsimplify import split_block |
| 23 |
|
| 24 |
|
| 25 |
def getargtypes(annotator, values): |
| 26 |
if values is None: |
| 27 |
from rpython.annotator.listdef import s_list_of_strings |
| 28 |
return [s_list_of_strings] |
| 29 |
return [_annotation(annotator, x) for x in values] |
| 30 |
|
| 31 |
def _annotation(a, x): |
| 32 |
T = lltype.typeOf(x) |
| 33 |
if T == lltype.Ptr(ll_rstr.STR): |
| 34 |
t = str |
| 35 |
else: |
| 36 |
t = lltype_to_annotation(T) |
| 37 |
return a.typeannotation(t) |
| 38 |
|
| 39 |
def annotate(func, values, inline=None, backendoptimize=True, |
| 40 |
translationoptions={}): |
| 41 |
|
| 42 |
t = TranslationContext() |
| 43 |
for key, value in translationoptions.items(): |
| 44 |
setattr(t.config.translation, key, value) |
| 45 |
annpolicy = AnnotatorPolicy() |
| 46 |
a = t.buildannotator(policy=annpolicy) |
| 47 |
argtypes = getargtypes(a, values) |
| 48 |
a.build_types(func, argtypes, main_entry_point=True) |
| 49 |
rtyper = t.buildrtyper() |
| 50 |
rtyper.specialize() |
| 51 |
|
| 52 |
|
| 53 |
if backendoptimize: |
| 54 |
from rpython.translator.backendopt.all import backend_optimizations |
| 55 |
backend_optimizations(t, inline_threshold=inline or 0, |
| 56 |
remove_asserts=True, really_remove_asserts=True) |
| 57 |
|
| 58 |
return rtyper |
| 59 |
|
| 60 |
def getgraph(func, values): |
| 61 |
rtyper = annotate(func, values) |
| 62 |
return rtyper.annotator.translator.graphs[0] |
| 63 |
|
| 64 |
def autodetect_jit_markers_redvars(graph): |
| 65 |
|
| 66 |
|
| 67 |
for block, op in graph.iterblockops(): |
| 68 |
if op.opname == 'jit_marker': |
| 69 |
jitdriver = op.args[1].value |
| 70 |
if not jitdriver.autoreds: |
| 71 |
continue |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
methname = op.args[0].value |
| 78 |
assert methname == 'jit_merge_point', ( |
| 79 |
"reds='auto' is supported only for jit drivers which " |
| 80 |
"calls only jit_merge_point. Found a call to %s" % methname) |
| 81 |
|
| 82 |
|
| 83 |
alive_v = set() |
| 84 |
for link in block.exits: |
| 85 |
alive_v.update(link.args) |
| 86 |
alive_v.difference_update(link.getextravars()) |
| 87 |
for op1 in block.operations[::-1]: |
| 88 |
if op1 is op: |
| 89 |
break |
| 90 |
alive_v.discard(op1.result) |
| 91 |
alive_v.update(op1.args) |
| 92 |
greens_v = op.args[2:] |
| 93 |
reds_v = alive_v - set(greens_v) |
| 94 |
reds_v = [v for v in reds_v if isinstance(v, Variable) and |
| 95 |
v.concretetype is not lltype.Void] |
| 96 |
reds_v = sort_vars(reds_v) |
| 97 |
op.args.extend(reds_v) |
| 98 |
if jitdriver.numreds is None: |
| 99 |
jitdriver.numreds = len(reds_v) |
| 100 |
elif jitdriver.numreds != len(reds_v): |
| 101 |
raise AssertionError("there are multiple jit_merge_points " |
| 102 |
"with the same jitdriver") |
| 103 |
|
| 104 |
def split_before_jit_merge_point(graph, portalblock, portalopindex): |
| 105 |
"""Split the block just before the 'jit_merge_point', |
| 106 |
making sure the input args are in the canonical order. |
| 107 |
""" |
| 108 |
|
| 109 |
if portalopindex > 0: |
| 110 |
link = split_block(portalblock, portalopindex) |
| 111 |
portalblock = link.target |
| 112 |
portalop = portalblock.operations[0] |
| 113 |
|
| 114 |
|
| 115 |
assert portalop.opname == 'jit_marker' |
| 116 |
assert portalop.args[0].value == 'jit_merge_point' |
| 117 |
greens_v, reds_v = decode_hp_hint_args(portalop) |
| 118 |
link = split_block(portalblock, 0, greens_v + reds_v) |
| 119 |
return link.target |
| 120 |
|
| 121 |
def sort_vars(args_v): |
| 122 |
from rpython.jit.metainterp.history import getkind |
| 123 |
_kind2count = {'int': 1, 'ref': 2, 'float': 3} |
| 124 |
return sorted(args_v, key=lambda v: _kind2count[getkind(v.concretetype)]) |
| 125 |
|
| 126 |
def decode_hp_hint_args(op): |
| 127 |
|
| 128 |
|
| 129 |
assert op.opname == 'jit_marker' |
| 130 |
jitdriver = op.args[1].value |
| 131 |
numgreens = len(jitdriver.greens) |
| 132 |
assert jitdriver.numreds is not None |
| 133 |
numreds = jitdriver.numreds |
| 134 |
greens_v = op.args[2:2+numgreens] |
| 135 |
reds_v = op.args[2+numgreens:] |
| 136 |
assert len(reds_v) == numreds |
| 137 |
|
| 138 |
def _sort(args_v, is_green): |
| 139 |
lst = [v for v in args_v if v.concretetype is not lltype.Void] |
| 140 |
if is_green: |
| 141 |
assert len(lst) == len(args_v), ( |
| 142 |
"not supported so far: 'greens' variables contain Void") |
| 143 |
|
| 144 |
|
| 145 |
lst2 = sort_vars(lst) |
| 146 |
assert lst == lst2, ("You have to reorder the variables named in " |
| 147 |
"the JitDriver (both the 'greens' and 'reds' independently). " |
| 148 |
"They must be sorted like this: first all the integer-like, " |
| 149 |
"then all the pointer-like, and finally the floats.\n" |
| 150 |
"Got: %r\n" |
| 151 |
"Expected: %r" % (lst, lst2)) |
| 152 |
return lst |
| 153 |
|
| 154 |
return (_sort(greens_v, True), _sort(reds_v, False)) |
| 155 |
|
| 156 |
def maybe_on_top_of_llinterp(rtyper, fnptr): |
| 157 |
|
| 158 |
|
| 159 |
funcobj = fnptr._obj |
| 160 |
if hasattr(funcobj, 'graph'): |
| 161 |
llinterp = LLInterpreter(rtyper) |
| 162 |
def on_top_of_llinterp(*args): |
| 163 |
return llinterp.eval_graph(funcobj.graph, list(args)) |
| 164 |
else: |
| 165 |
assert hasattr(funcobj, '_callable') |
| 166 |
def on_top_of_llinterp(*args): |
| 167 |
return funcobj._callable(*args) |
| 168 |
return on_top_of_llinterp |
| 169 |
|
| 170 |
class Entry(ExtRegistryEntry): |
| 171 |
_about_ = maybe_on_top_of_llinterp |
| 172 |
def compute_result_annotation(self, s_rtyper, s_fnptr): |
| 173 |
return s_fnptr |
| 174 |
def specialize_call(self, hop): |
| 175 |
hop.exception_cannot_occur() |
| 176 |
return hop.inputarg(hop.args_r[1], arg=1) |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
def _ll_0_newlist(LIST): |
| 185 |
return LIST.ll_newlist(0) |
| 186 |
def _ll_1_newlist(LIST, count): |
| 187 |
return LIST.ll_newlist(count) |
| 188 |
_ll_0_newlist.need_result_type = True |
| 189 |
_ll_1_newlist.need_result_type = True |
| 190 |
|
| 191 |
_ll_1_newlist_clear = rlist._ll_alloc_and_clear |
| 192 |
_ll_1_newlist_clear.need_result_type = True |
| 193 |
|
| 194 |
def _ll_1_newlist_hint(LIST, hint): |
| 195 |
return LIST.ll_newlist_hint(hint) |
| 196 |
_ll_1_newlist_hint.need_result_type = True |
| 197 |
|
| 198 |
def _ll_1_list_len(l): |
| 199 |
return l.ll_length() |
| 200 |
def _ll_2_list_getitem(l, index): |
| 201 |
return rlist.ll_getitem(rlist.dum_checkidx, l, index) |
| 202 |
def _ll_3_list_setitem(l, index, newitem): |
| 203 |
rlist.ll_setitem(rlist.dum_checkidx, l, index, newitem) |
| 204 |
def _ll_2_list_delitem(l, index): |
| 205 |
rlist.ll_delitem(rlist.dum_checkidx, l, index) |
| 206 |
def _ll_1_list_pop(l): |
| 207 |
return rlist.ll_pop_default(rlist.dum_checkidx, l) |
| 208 |
def _ll_2_list_pop(l, index): |
| 209 |
return rlist.ll_pop(rlist.dum_checkidx, l, index) |
| 210 |
_ll_2_list_append = rlist.ll_append |
| 211 |
_ll_2_list_extend = rlist.ll_extend |
| 212 |
_ll_2_list_delslice_startonly = rlist.ll_listdelslice_startonly |
| 213 |
_ll_3_list_delslice_startstop = rlist.ll_listdelslice_startstop |
| 214 |
_ll_2_list_inplace_mul = rlist.ll_inplace_mul |
| 215 |
|
| 216 |
_ll_2_list_getitem_foldable = _ll_2_list_getitem |
| 217 |
_ll_1_list_len_foldable = _ll_1_list_len |
| 218 |
|
| 219 |
_ll_5_list_ll_arraycopy = rgc.ll_arraycopy |
| 220 |
_ll_4_list_ll_arraymove = rgc.ll_arraymove |
| 221 |
|
| 222 |
_ll_3_list_resize_hint_really = rlist_ll._ll_list_resize_hint_really |
| 223 |
|
| 224 |
@elidable |
| 225 |
def _ll_1_gc_identityhash(x): |
| 226 |
return lltype.identityhash(x) |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
def _ll_1_gc_id(ptr): |
| 234 |
return llop.gc_id(lltype.Signed, ptr) |
| 235 |
|
| 236 |
|
| 237 |
def _ll_1_gc_pin(ptr): |
| 238 |
return llop.gc_pin(lltype.Bool, ptr) |
| 239 |
|
| 240 |
def _ll_1_gc_unpin(ptr): |
| 241 |
llop.gc_unpin(lltype.Void, ptr) |
| 242 |
|
| 243 |
|
| 244 |
@oopspec("jit.force_virtual(inst)") |
| 245 |
def _ll_1_jit_force_virtual(inst): |
| 246 |
return llop.jit_force_virtual(lltype.typeOf(inst), inst) |
| 247 |
|
| 248 |
|
| 249 |
def _ll_1_int_abs(x): |
| 250 |
|
| 251 |
mask = x >> (LONG_BIT - 1) |
| 252 |
return (x ^ mask) - mask |
| 253 |
|
| 254 |
|
| 255 |
def _ll_2_int_floordiv(x, y): |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
r = x // y |
| 262 |
p = r * y |
| 263 |
|
| 264 |
return r + (((x ^ y) >> (LONG_BIT - 1)) & (p != x)) |
| 265 |
|
| 266 |
def _ll_2_int_mod(x, y): |
| 267 |
|
| 268 |
r = x % y |
| 269 |
|
| 270 |
r -= y & (((x ^ y) & (r | -r)) >> (LONG_BIT - 1)) |
| 271 |
return r |
| 272 |
|
| 273 |
|
| 274 |
def _ll_1_cast_uint_to_float(x): |
| 275 |
|
| 276 |
|
| 277 |
return llop.cast_uint_to_float(lltype.Float, x) |
| 278 |
|
| 279 |
def _ll_1_cast_float_to_uint(x): |
| 280 |
|
| 281 |
|
| 282 |
return llop.cast_float_to_uint(lltype.Unsigned, x) |
| 283 |
|
| 284 |
def _ll_0_ll_read_timestamp(): |
| 285 |
from rpython.rlib import rtimer |
| 286 |
return rtimer.read_timestamp() |
| 287 |
|
| 288 |
def _ll_0_ll_get_timestamp_unit(): |
| 289 |
from rpython.rlib import rtimer |
| 290 |
return rtimer.get_timestamp_unit() |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
_ll_1_ll_math_ll_math_sqrt = ll_math.ll_math_sqrt |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
def u_to_longlong(x): |
| 302 |
return rffi.cast(lltype.SignedLongLong, x) |
| 303 |
|
| 304 |
def _ll_1_llong_invert(xll): |
| 305 |
y = ~r_ulonglong(xll) |
| 306 |
return u_to_longlong(y) |
| 307 |
|
| 308 |
def _ll_1_ullong_invert(xull): |
| 309 |
return ~xull |
| 310 |
|
| 311 |
def _ll_2_llong_lt(xll, yll): |
| 312 |
return xll < yll |
| 313 |
|
| 314 |
def _ll_2_llong_le(xll, yll): |
| 315 |
return xll <= yll |
| 316 |
|
| 317 |
def _ll_2_llong_eq(xll, yll): |
| 318 |
return xll == yll |
| 319 |
|
| 320 |
def _ll_2_llong_ne(xll, yll): |
| 321 |
return xll != yll |
| 322 |
|
| 323 |
def _ll_2_llong_gt(xll, yll): |
| 324 |
return xll > yll |
| 325 |
|
| 326 |
def _ll_2_llong_ge(xll, yll): |
| 327 |
return xll >= yll |
| 328 |
|
| 329 |
def _ll_2_ullong_eq(xull, yull): |
| 330 |
return xull == yull |
| 331 |
|
| 332 |
def _ll_2_ullong_ne(xull, yull): |
| 333 |
return xull != yull |
| 334 |
|
| 335 |
def _ll_2_ullong_ult(xull, yull): |
| 336 |
return xull < yull |
| 337 |
|
| 338 |
def _ll_2_ullong_ule(xull, yull): |
| 339 |
return xull <= yull |
| 340 |
|
| 341 |
def _ll_2_ullong_ugt(xull, yull): |
| 342 |
return xull > yull |
| 343 |
|
| 344 |
def _ll_2_ullong_uge(xull, yull): |
| 345 |
return xull >= yull |
| 346 |
|
| 347 |
def _ll_2_llong_add(xll, yll): |
| 348 |
z = r_ulonglong(xll) + r_ulonglong(yll) |
| 349 |
return u_to_longlong(z) |
| 350 |
|
| 351 |
def _ll_2_llong_sub(xll, yll): |
| 352 |
z = r_ulonglong(xll) - r_ulonglong(yll) |
| 353 |
return u_to_longlong(z) |
| 354 |
|
| 355 |
def _ll_2_llong_mul(xll, yll): |
| 356 |
z = r_ulonglong(xll) * r_ulonglong(yll) |
| 357 |
return u_to_longlong(z) |
| 358 |
|
| 359 |
def _ll_2_llong_and(xll, yll): |
| 360 |
z = r_ulonglong(xll) & r_ulonglong(yll) |
| 361 |
return u_to_longlong(z) |
| 362 |
|
| 363 |
def _ll_2_llong_or(xll, yll): |
| 364 |
z = r_ulonglong(xll) | r_ulonglong(yll) |
| 365 |
return u_to_longlong(z) |
| 366 |
|
| 367 |
def _ll_2_llong_xor(xll, yll): |
| 368 |
z = r_ulonglong(xll) ^ r_ulonglong(yll) |
| 369 |
return u_to_longlong(z) |
| 370 |
|
| 371 |
def _ll_2_ullong_add(xull, yull): |
| 372 |
z = (xull) + (yull) |
| 373 |
return (z) |
| 374 |
|
| 375 |
def _ll_2_ullong_sub(xull, yull): |
| 376 |
z = (xull) - (yull) |
| 377 |
return (z) |
| 378 |
|
| 379 |
def _ll_2_ullong_mul(xull, yull): |
| 380 |
z = (xull) * (yull) |
| 381 |
return (z) |
| 382 |
|
| 383 |
def _ll_2_ullong_and(xull, yull): |
| 384 |
z = (xull) & (yull) |
| 385 |
return (z) |
| 386 |
|
| 387 |
def _ll_2_ullong_or(xull, yull): |
| 388 |
z = (xull) | (yull) |
| 389 |
return (z) |
| 390 |
|
| 391 |
def _ll_2_ullong_xor(xull, yull): |
| 392 |
z = (xull) ^ (yull) |
| 393 |
return (z) |
| 394 |
|
| 395 |
def _ll_2_llong_lshift(xll, y): |
| 396 |
z = r_ulonglong(xll) << y |
| 397 |
return u_to_longlong(z) |
| 398 |
|
| 399 |
def _ll_2_ullong_lshift(xull, y): |
| 400 |
return xull << y |
| 401 |
|
| 402 |
def _ll_2_llong_rshift(xll, y): |
| 403 |
return xll >> y |
| 404 |
|
| 405 |
def _ll_2_ullong_urshift(xull, y): |
| 406 |
return xull >> y |
| 407 |
|
| 408 |
def _ll_1_llong_from_int(x): |
| 409 |
return r_longlong(intmask(x)) |
| 410 |
|
| 411 |
def _ll_1_ullong_from_int(x): |
| 412 |
return r_ulonglong(intmask(x)) |
| 413 |
|
| 414 |
def _ll_1_llong_from_uint(x): |
| 415 |
return r_longlong(r_uint(x)) |
| 416 |
|
| 417 |
def _ll_1_ullong_from_uint(x): |
| 418 |
return r_ulonglong(r_uint(x)) |
| 419 |
|
| 420 |
def _ll_1_llong_to_int(xll): |
| 421 |
return intmask(xll) |
| 422 |
|
| 423 |
def _ll_1_llong_from_float(xf): |
| 424 |
return r_longlong(xf) |
| 425 |
|
| 426 |
def _ll_1_ullong_from_float(xf): |
| 427 |
return r_ulonglong(xf) |
| 428 |
|
| 429 |
def _ll_1_llong_to_float(xll): |
| 430 |
return float(rffi.cast(lltype.SignedLongLong, xll)) |
| 431 |
|
| 432 |
def _ll_1_ullong_u_to_float(xull): |
| 433 |
return float(rffi.cast(lltype.UnsignedLongLong, xull)) |
| 434 |
|
| 435 |
|
| 436 |
def _ll_1_llong_abs(xll): |
| 437 |
if xll < 0: |
| 438 |
return -xll |
| 439 |
else: |
| 440 |
return xll |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
inline_calls_to = [ |
| 445 |
('int_abs', [lltype.Signed], lltype.Signed), |
| 446 |
('int_floordiv', [lltype.Signed, lltype.Signed], lltype.Signed), |
| 447 |
('int_mod', [lltype.Signed, lltype.Signed], lltype.Signed), |
| 448 |
('ll_math.ll_math_sqrt', [lltype.Float], lltype.Float), |
| 449 |
] |
| 450 |
|
| 451 |
|
| 452 |
class LLtypeHelpers: |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
_ll_1_dict_copy = ll_rdict.ll_copy |
| 457 |
_ll_1_dict_clear = ll_rdict.ll_clear |
| 458 |
_ll_2_dict_update = ll_rdict.ll_update |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
_ll_1_dict_keys = ll_rdict.ll_dict_keys |
| 463 |
_ll_1_dict_values = ll_rdict.ll_dict_values |
| 464 |
_ll_1_dict_items = ll_rdict.ll_dict_items |
| 465 |
_ll_1_dict_keys .need_result_type = True |
| 466 |
_ll_1_dict_values.need_result_type = True |
| 467 |
_ll_1_dict_items .need_result_type = True |
| 468 |
|
| 469 |
_ll_1_dictiter_next = ll_rdict._ll_dictnext |
| 470 |
_ll_1_dict_resize = ll_rdict.ll_dict_resize |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
_ll_1_odict_copy = rordereddict.ll_dict_copy |
| 475 |
_ll_1_odict_clear = rordereddict.ll_dict_clear |
| 476 |
_ll_2_odict_update = rordereddict.ll_dict_update |
| 477 |
|
| 478 |
_ll_1_odict_keys = rordereddict.ll_dict_keys |
| 479 |
_ll_1_odict_values = rordereddict.ll_dict_values |
| 480 |
_ll_1_odict_keys .need_result_type = True |
| 481 |
_ll_1_odict_values.need_result_type = True |
| 482 |
|
| 483 |
_ll_1_odictiter_next = rordereddict._ll_dictnext |
| 484 |
_ll_1_odict_resize = rordereddict.ll_dict_resize |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
_ll_1_str_str2unicode = ll_rstr.LLHelpers.ll_str2unicode |
| 489 |
|
| 490 |
def _ll_4_str_eq_slice_checknull(s1, start, length, s2): |
| 491 |
"""str1[start : start + length] == str2.""" |
| 492 |
if not s2: |
| 493 |
return 0 |
| 494 |
chars2 = s2.chars |
| 495 |
if len(chars2) != length: |
| 496 |
return 0 |
| 497 |
j = 0 |
| 498 |
chars1 = s1.chars |
| 499 |
while j < length: |
| 500 |
if chars1[start + j] != chars2[j]: |
| 501 |
return 0 |
| 502 |
j += 1 |
| 503 |
return 1 |
| 504 |
|
| 505 |
def _ll_4_str_eq_slice_nonnull(s1, start, length, s2): |
| 506 |
"""str1[start : start + length] == str2, assuming str2 != NULL.""" |
| 507 |
chars2 = s2.chars |
| 508 |
if len(chars2) != length: |
| 509 |
return 0 |
| 510 |
j = 0 |
| 511 |
chars1 = s1.chars |
| 512 |
while j < length: |
| 513 |
if chars1[start + j] != chars2[j]: |
| 514 |
return 0 |
| 515 |
j += 1 |
| 516 |
return 1 |
| 517 |
|
| 518 |
def _ll_4_str_eq_slice_char(s1, start, length, c2): |
| 519 |
"""str1[start : start + length] == c2.""" |
| 520 |
if length != 1: |
| 521 |
return 0 |
| 522 |
if s1.chars[start] != c2: |
| 523 |
return 0 |
| 524 |
return 1 |
| 525 |
|
| 526 |
def _ll_2_str_eq_nonnull(s1, s2): |
| 527 |
len1 = len(s1.chars) |
| 528 |
len2 = len(s2.chars) |
| 529 |
if len1 != len2: |
| 530 |
return 0 |
| 531 |
j = 0 |
| 532 |
chars1 = s1.chars |
| 533 |
chars2 = s2.chars |
| 534 |
while j < len1: |
| 535 |
if chars1[j] != chars2[j]: |
| 536 |
return 0 |
| 537 |
j += 1 |
| 538 |
return 1 |
| 539 |
|
| 540 |
def _ll_2_str_eq_nonnull_char(s1, c2): |
| 541 |
chars = s1.chars |
| 542 |
if len(chars) != 1: |
| 543 |
return 0 |
| 544 |
if chars[0] != c2: |
| 545 |
return 0 |
| 546 |
return 1 |
| 547 |
|
| 548 |
def _ll_2_str_eq_checknull_char(s1, c2): |
| 549 |
if not s1: |
| 550 |
return 0 |
| 551 |
chars = s1.chars |
| 552 |
if len(chars) != 1: |
| 553 |
return 0 |
| 554 |
if chars[0] != c2: |
| 555 |
return 0 |
| 556 |
return 1 |
| 557 |
|
| 558 |
def _ll_2_str_eq_lengthok(s1, s2): |
| 559 |
j = 0 |
| 560 |
chars1 = s1.chars |
| 561 |
chars2 = s2.chars |
| 562 |
len1 = len(chars1) |
| 563 |
while j < len1: |
| 564 |
if chars1[j] != chars2[j]: |
| 565 |
return 0 |
| 566 |
j += 1 |
| 567 |
return 1 |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
def _ll_2_raw_malloc(TP, size): |
| 572 |
return lltype.malloc(TP, size, flavor='raw') |
| 573 |
|
| 574 |
def build_ll_0_alloc_with_del(RESULT, vtable): |
| 575 |
def _ll_0_alloc_with_del(): |
| 576 |
p = lltype.malloc(RESULT.TO) |
| 577 |
lltype.cast_pointer(rclass.OBJECTPTR, p).typeptr = vtable |
| 578 |
return p |
| 579 |
return _ll_0_alloc_with_del |
| 580 |
|
| 581 |
def build_raw_malloc_varsize_builder(zero=False, |
| 582 |
add_memory_pressure=False, |
| 583 |
track_allocation=True): |
| 584 |
def build_ll_1_raw_malloc_varsize(ARRAY): |
| 585 |
def _ll_1_raw_malloc_varsize(n): |
| 586 |
return lltype.malloc(ARRAY, n, flavor='raw', zero=zero, |
| 587 |
add_memory_pressure=add_memory_pressure, |
| 588 |
track_allocation=track_allocation) |
| 589 |
name = '_ll_1_raw_malloc_varsize' |
| 590 |
if zero: |
| 591 |
name += '_zero' |
| 592 |
if add_memory_pressure: |
| 593 |
name += '_mpressure' |
| 594 |
if not track_allocation: |
| 595 |
name += '_notrack' |
| 596 |
_ll_1_raw_malloc_varsize.__name__ = name |
| 597 |
return _ll_1_raw_malloc_varsize |
| 598 |
return build_ll_1_raw_malloc_varsize |
| 599 |
|
| 600 |
build_ll_1_raw_malloc_varsize = ( |
| 601 |
build_raw_malloc_varsize_builder()) |
| 602 |
build_ll_1_raw_malloc_varsize_zero = ( |
| 603 |
build_raw_malloc_varsize_builder(zero=True)) |
| 604 |
build_ll_1_raw_malloc_varsize_zero_add_memory_pressure = ( |
| 605 |
build_raw_malloc_varsize_builder(zero=True, add_memory_pressure=True)) |
| 606 |
build_ll_1_raw_malloc_varsize_add_memory_pressure = ( |
| 607 |
build_raw_malloc_varsize_builder(add_memory_pressure=True)) |
| 608 |
build_ll_1_raw_malloc_varsize_no_track_allocation = ( |
| 609 |
build_raw_malloc_varsize_builder(track_allocation=False)) |
| 610 |
build_ll_1_raw_malloc_varsize_zero_no_track_allocation = ( |
| 611 |
build_raw_malloc_varsize_builder(zero=True, track_allocation=False)) |
| 612 |
build_ll_1_raw_malloc_varsize_zero_add_memory_pressure_no_track_allocation = ( |
| 613 |
build_raw_malloc_varsize_builder(zero=True, add_memory_pressure=True, track_allocation=False)) |
| 614 |
build_ll_1_raw_malloc_varsize_add_memory_pressure_no_track_allocation = ( |
| 615 |
build_raw_malloc_varsize_builder(add_memory_pressure=True, track_allocation=False)) |
| 616 |
|
| 617 |
def build_raw_malloc_fixedsize_builder(zero=False, |
| 618 |
add_memory_pressure=False, |
| 619 |
track_allocation=True): |
| 620 |
def build_ll_0_raw_malloc_fixedsize(STRUCT): |
| 621 |
def _ll_0_raw_malloc_fixedsize(): |
| 622 |
return lltype.malloc(STRUCT, flavor='raw', zero=zero, |
| 623 |
add_memory_pressure=add_memory_pressure, |
| 624 |
track_allocation=track_allocation) |
| 625 |
name = '_ll_0_raw_malloc_fixedsize' |
| 626 |
if zero: |
| 627 |
name += '_zero' |
| 628 |
if add_memory_pressure: |
| 629 |
name += '_mpressure' |
| 630 |
if not track_allocation: |
| 631 |
name += '_notrack' |
| 632 |
_ll_0_raw_malloc_fixedsize.__name__ = name |
| 633 |
return _ll_0_raw_malloc_fixedsize |
| 634 |
return build_ll_0_raw_malloc_fixedsize |
| 635 |
|
| 636 |
build_ll_0_raw_malloc_fixedsize = ( |
| 637 |
build_raw_malloc_fixedsize_builder()) |
| 638 |
build_ll_0_raw_malloc_fixedsize_zero = ( |
| 639 |
build_raw_malloc_fixedsize_builder(zero=True)) |
| 640 |
build_ll_0_raw_malloc_fixedsize_zero_add_memory_pressure = ( |
| 641 |
build_raw_malloc_fixedsize_builder(zero=True, add_memory_pressure=True)) |
| 642 |
build_ll_0_raw_malloc_fixedsize_add_memory_pressure = ( |
| 643 |
build_raw_malloc_fixedsize_builder(add_memory_pressure=True)) |
| 644 |
build_ll_0_raw_malloc_fixedsize_no_track_allocation = ( |
| 645 |
build_raw_malloc_fixedsize_builder(track_allocation=False)) |
| 646 |
build_ll_0_raw_malloc_fixedsize_zero_no_track_allocation = ( |
| 647 |
build_raw_malloc_fixedsize_builder(zero=True, track_allocation=False)) |
| 648 |
build_ll_0_raw_malloc_fixedsize_zero_add_memory_pressure_no_track_allocation = ( |
| 649 |
build_raw_malloc_fixedsize_builder(zero=True, add_memory_pressure=True, track_allocation=False)) |
| 650 |
build_ll_0_raw_malloc_fixedsize_add_memory_pressure_no_track_allocation = ( |
| 651 |
build_raw_malloc_fixedsize_builder(add_memory_pressure=True, track_allocation=False)) |
| 652 |
|
| 653 |
def build_raw_free_builder(track_allocation=True): |
| 654 |
def build_ll_1_raw_free(ARRAY): |
| 655 |
def _ll_1_raw_free(p): |
| 656 |
lltype.free(p, flavor='raw', |
| 657 |
track_allocation=track_allocation) |
| 658 |
return _ll_1_raw_free |
| 659 |
return build_ll_1_raw_free |
| 660 |
|
| 661 |
build_ll_1_raw_free = ( |
| 662 |
build_raw_free_builder()) |
| 663 |
build_ll_1_raw_free_no_track_allocation = ( |
| 664 |
build_raw_free_builder(track_allocation=False)) |
| 665 |
|
| 666 |
def _ll_1_threadlocalref_get(TP, offset): |
| 667 |
return llop.threadlocalref_get(TP, offset) |
| 668 |
_ll_1_threadlocalref_get.need_result_type = 'exact' |
| 669 |
|
| 670 |
def _ll_1_weakref_create(obj): |
| 671 |
return llop.weakref_create(llmemory.WeakRefPtr, obj) |
| 672 |
|
| 673 |
def _ll_1_weakref_deref(TP, obj): |
| 674 |
return llop.weakref_deref(lltype.Ptr(TP), obj) |
| 675 |
_ll_1_weakref_deref.need_result_type = True |
| 676 |
|
| 677 |
def _ll_1_gc_add_memory_pressure(num): |
| 678 |
llop.gc_add_memory_pressure(lltype.Void, num) |
| 679 |
def _ll_2_gc_add_memory_pressure(num, obj): |
| 680 |
llop.gc_add_memory_pressure(lltype.Void, num, obj) |
| 681 |
|
| 682 |
|
| 683 |
def setup_extra_builtin(rtyper, oopspec_name, nb_args, extra=None): |
| 684 |
name = '_ll_%d_%s' % (nb_args, oopspec_name.replace('.', '_')) |
| 685 |
if extra is not None: |
| 686 |
name = 'build' + name |
| 687 |
try: |
| 688 |
wrapper = globals()[name] |
| 689 |
except KeyError: |
| 690 |
wrapper = getattr(LLtypeHelpers, name).im_func |
| 691 |
if extra is not None: |
| 692 |
wrapper = wrapper(*extra) |
| 693 |
return wrapper |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
class Index: |
| 698 |
def __init__(self, n): |
| 699 |
self.n = n |
| 700 |
|
| 701 |
def parse_oopspec(fnobj): |
| 702 |
FUNCTYPE = lltype.typeOf(fnobj) |
| 703 |
ll_func = fnobj._callable |
| 704 |
nb_args = len(FUNCTYPE.ARGS) |
| 705 |
argnames = ll_func.__code__.co_varnames[:nb_args] |
| 706 |
|
| 707 |
operation_name, args = ll_func.oopspec.split('(', 1) |
| 708 |
assert args.endswith(')') |
| 709 |
args = args[:-1] + ',' |
| 710 |
if args.strip() == ',': |
| 711 |
args = '()' |
| 712 |
nb_args = len(argnames) |
| 713 |
argname2index = dict(zip(argnames, [Index(n) for n in range(nb_args)])) |
| 714 |
argtuple = eval(args, argname2index) |
| 715 |
return operation_name, argtuple |
| 716 |
|
| 717 |
def normalize_opargs(argtuple, opargs): |
| 718 |
result = [] |
| 719 |
for obj in argtuple: |
| 720 |
if isinstance(obj, Index): |
| 721 |
result.append(opargs[obj.n]) |
| 722 |
else: |
| 723 |
result.append(Constant(obj, lltype.typeOf(obj))) |
| 724 |
return result |
| 725 |
|
| 726 |
def get_call_oopspec_opargs(fnobj, opargs): |
| 727 |
oopspec, argtuple = parse_oopspec(fnobj) |
| 728 |
normalized_opargs = normalize_opargs(argtuple, opargs) |
| 729 |
return oopspec, normalized_opargs |
| 730 |
|
| 731 |
def get_identityhash_oopspec(op): |
| 732 |
return 'gc_identityhash', op.args |
| 733 |
|
| 734 |
def get_gcid_oopspec(op): |
| 735 |
return 'gc_id', op.args |
| 736 |
|
| 737 |
|
| 738 |
RENAMED_ADT_NAME = { |
| 739 |
'list': { |
| 740 |
'll_getitem_fast': 'getitem', |
| 741 |
'll_setitem_fast': 'setitem', |
| 742 |
'll_length': 'len', |
| 743 |
}, |
| 744 |
} |
| 745 |
|
| 746 |
def get_send_oopspec(SELFTYPE, name): |
| 747 |
oopspec_name = SELFTYPE.oopspec_name |
| 748 |
assert oopspec_name is not None |
| 749 |
renamed = RENAMED_ADT_NAME.get(oopspec_name, {}) |
| 750 |
pubname = renamed.get(name, name) |
| 751 |
oopspec = '%s.%s' % (oopspec_name, pubname) |
| 752 |
return oopspec |
| 753 |
|
| 754 |
|
| 755 |
def decode_builtin_call(op): |
| 756 |
if op.opname == 'direct_call': |
| 757 |
fnobj = op.args[0].value._obj |
| 758 |
opargs = op.args[1:] |
| 759 |
return get_call_oopspec_opargs(fnobj, opargs) |
| 760 |
elif op.opname == 'gc_identityhash': |
| 761 |
return get_identityhash_oopspec(op) |
| 762 |
elif op.opname == 'gc_id': |
| 763 |
return get_gcid_oopspec(op) |
| 764 |
else: |
| 765 |
raise ValueError(op.opname) |
| 766 |
|
| 767 |
def builtin_func_for_spec(rtyper, oopspec_name, ll_args, ll_res, |
| 768 |
extra=None, extrakey=None): |
| 769 |
assert (extra is None) == (extrakey is None) |
| 770 |
key = (oopspec_name, tuple(ll_args), ll_res, extrakey) |
| 771 |
try: |
| 772 |
return rtyper._builtin_func_for_spec_cache[key] |
| 773 |
except (KeyError, AttributeError): |
| 774 |
pass |
| 775 |
args_s = [lltype_to_annotation(v) for v in ll_args] |
| 776 |
if '.' not in oopspec_name: |
| 777 |
LIST_OR_DICT = ll_res |
| 778 |
else: |
| 779 |
LIST_OR_DICT = ll_args[0] |
| 780 |
s_result = lltype_to_annotation(ll_res) |
| 781 |
impl = setup_extra_builtin(rtyper, oopspec_name, len(args_s), extra) |
| 782 |
if getattr(impl, 'need_result_type', False): |
| 783 |
if hasattr(rtyper, 'annotator'): |
| 784 |
bk = rtyper.annotator.bookkeeper |
| 785 |
ll_restype = ll_res |
| 786 |
if impl.need_result_type != 'exact': |
| 787 |
ll_restype = ll_restype.TO |
| 788 |
desc = bk.getdesc(ll_restype) |
| 789 |
else: |
| 790 |
class TestingDesc(object): |
| 791 |
knowntype = int |
| 792 |
pyobj = None |
| 793 |
desc = TestingDesc() |
| 794 |
args_s.insert(0, annmodel.SomePBC([desc])) |
| 795 |
|
| 796 |
if hasattr(rtyper, 'annotator'): |
| 797 |
mixlevelann = MixLevelHelperAnnotator(rtyper) |
| 798 |
c_func = mixlevelann.constfunc(impl, args_s, s_result) |
| 799 |
mixlevelann.finish() |
| 800 |
else: |
| 801 |
|
| 802 |
c_func = Constant(oopspec_name, |
| 803 |
lltype.Ptr(lltype.FuncType(ll_args, ll_res))) |
| 804 |
|
| 805 |
if not hasattr(rtyper, '_builtin_func_for_spec_cache'): |
| 806 |
rtyper._builtin_func_for_spec_cache = {} |
| 807 |
rtyper._builtin_func_for_spec_cache[key] = (c_func, LIST_OR_DICT) |
| 808 |
|
| 809 |
return c_func, LIST_OR_DICT |