| 1 |
from rpython.rlib import rgc, jit, types |
| 2 |
from rpython.rtyper.debug import ll_assert |
| 3 |
from rpython.rlib.signature import signature |
| 4 |
from rpython.rtyper.error import TyperError |
| 5 |
from rpython.rtyper.lltypesystem import rstr |
| 6 |
from rpython.rtyper.lltypesystem.lltype import (GcForwardReference, Ptr, GcArray, |
| 7 |
GcStruct, Void, Signed, malloc, typeOf, nullptr, typeMethod, Char) |
| 8 |
from rpython.rtyper.rlist import (AbstractBaseListRepr, AbstractListRepr, |
| 9 |
AbstractFixedSizeListRepr, AbstractListIteratorRepr, ll_setitem_nonneg, |
| 10 |
ADTIList, ADTIFixedList, dum_nocheck) |
| 11 |
from rpython.rtyper.rmodel import Repr, inputconst, externalvsinternal |
| 12 |
from rpython.tool.pairtype import pairtype, pair |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
def LIST_OF(ITEMTYPE, cache={}): |
| 34 |
""" |
| 35 |
Return the low-level type for a resizable list of ITEMTYPE |
| 36 |
""" |
| 37 |
try: |
| 38 |
return cache[ITEMTYPE] |
| 39 |
except KeyError: |
| 40 |
pass |
| 41 |
|
| 42 |
from rpython.rtyper.lltypesystem.rstr import CharRepr |
| 43 |
assert ITEMTYPE is Char, 'only Char is supported for now' |
| 44 |
|
| 45 |
list_of_char_repr = ListRepr(None, CharRepr()) |
| 46 |
list_of_char_repr._setup_repr() |
| 47 |
LIST = list_of_char_repr.LIST |
| 48 |
cache[ITEMTYPE] = LIST |
| 49 |
return LIST |
| 50 |
|
| 51 |
|
| 52 |
class BaseListRepr(AbstractBaseListRepr): |
| 53 |
rstr_ll = rstr.LLHelpers |
| 54 |
|
| 55 |
def __init__(self, rtyper, item_repr, listitem=None): |
| 56 |
self.rtyper = rtyper |
| 57 |
self.LIST = GcForwardReference() |
| 58 |
self.lowleveltype = Ptr(self.LIST) |
| 59 |
if not isinstance(item_repr, Repr): |
| 60 |
assert callable(item_repr) |
| 61 |
self._item_repr_computer = item_repr |
| 62 |
else: |
| 63 |
self.external_item_repr, self.item_repr = externalvsinternal(rtyper, item_repr, gcref=True) |
| 64 |
self.listitem = listitem |
| 65 |
self.list_cache = {} |
| 66 |
|
| 67 |
|
| 68 |
def null_const(self): |
| 69 |
return nullptr(self.LIST) |
| 70 |
|
| 71 |
def get_eqfunc(self): |
| 72 |
return inputconst(Void, self.item_repr.get_ll_eq_function()) |
| 73 |
|
| 74 |
def make_iterator_repr(self, *variant): |
| 75 |
if not variant: |
| 76 |
return ListIteratorRepr(self) |
| 77 |
elif variant == ("reversed",): |
| 78 |
return ReversedListIteratorRepr(self) |
| 79 |
else: |
| 80 |
raise TyperError("unsupported %r iterator over a list" % (variant,)) |
| 81 |
|
| 82 |
def get_itemarray_lowleveltype(self): |
| 83 |
ITEM = self.item_repr.lowleveltype |
| 84 |
ITEMARRAY = GcArray(ITEM, |
| 85 |
adtmeths = ADTIFixedList({ |
| 86 |
"ll_newlist": ll_fixed_newlist, |
| 87 |
"ll_newemptylist": ll_fixed_newemptylist, |
| 88 |
"ll_length": ll_fixed_length, |
| 89 |
"ll_items": ll_fixed_items, |
| 90 |
"ITEM": ITEM, |
| 91 |
"ll_getitem_fast": ll_fixed_getitem_fast, |
| 92 |
"ll_setitem_fast": ll_fixed_setitem_fast, |
| 93 |
})) |
| 94 |
return ITEMARRAY |
| 95 |
|
| 96 |
|
| 97 |
class __extend__(pairtype(BaseListRepr, BaseListRepr)): |
| 98 |
def rtype_is_((r_lst1, r_lst2), hop): |
| 99 |
if r_lst1.lowleveltype != r_lst2.lowleveltype: |
| 100 |
|
| 101 |
v_lst1, v_lst2 = hop.inputargs(r_lst1, r_lst2) |
| 102 |
return hop.gendirectcall(ll_both_none, v_lst1, v_lst2) |
| 103 |
|
| 104 |
return pairtype(Repr, Repr).rtype_is_(pair(r_lst1, r_lst2), hop) |
| 105 |
|
| 106 |
|
| 107 |
class ListRepr(AbstractListRepr, BaseListRepr): |
| 108 |
|
| 109 |
def _setup_repr(self): |
| 110 |
if 'item_repr' not in self.__dict__: |
| 111 |
self.external_item_repr, self.item_repr = externalvsinternal(self.rtyper, self._item_repr_computer(), gcref=True) |
| 112 |
if isinstance(self.LIST, GcForwardReference): |
| 113 |
ITEM = self.item_repr.lowleveltype |
| 114 |
ITEMARRAY = self.get_itemarray_lowleveltype() |
| 115 |
|
| 116 |
self.LIST.become(GcStruct("list", ("length", Signed), |
| 117 |
("items", Ptr(ITEMARRAY)), |
| 118 |
adtmeths = ADTIList({ |
| 119 |
"ll_newlist": ll_newlist, |
| 120 |
"ll_newlist_hint": ll_newlist_hint, |
| 121 |
"ll_newemptylist": ll_newemptylist, |
| 122 |
"ll_length": ll_length, |
| 123 |
"ll_items": ll_items, |
| 124 |
"ITEM": ITEM, |
| 125 |
"ll_getitem_fast": ll_getitem_fast, |
| 126 |
"ll_setitem_fast": ll_setitem_fast, |
| 127 |
"_ll_resize_ge": _ll_list_resize_ge, |
| 128 |
"_ll_resize_le": _ll_list_resize_le, |
| 129 |
"_ll_resize": _ll_list_resize, |
| 130 |
"_ll_resize_hint": _ll_list_resize_hint, |
| 131 |
}), |
| 132 |
hints = {'list': True}) |
| 133 |
) |
| 134 |
|
| 135 |
def compact_repr(self): |
| 136 |
return 'ListR %s' % (self.item_repr.compact_repr(),) |
| 137 |
|
| 138 |
def prepare_const(self, n): |
| 139 |
result = malloc(self.LIST, immortal=True) |
| 140 |
result.length = n |
| 141 |
result.items = malloc(self.LIST.items.TO, n) |
| 142 |
return result |
| 143 |
|
| 144 |
def rtype_method_append(self, hop): |
| 145 |
if getattr(self.listitem, 'hint_maxlength', False): |
| 146 |
v_lst, v_value = hop.inputargs(self, self.item_repr) |
| 147 |
hop.exception_cannot_occur() |
| 148 |
hop.gendirectcall(ll_append_noresize, v_lst, v_value) |
| 149 |
else: |
| 150 |
AbstractListRepr.rtype_method_append(self, hop) |
| 151 |
|
| 152 |
def rtype_hint(self, hop): |
| 153 |
optimized = getattr(self.listitem, 'hint_maxlength', False) |
| 154 |
hints = hop.args_s[-1].const |
| 155 |
if 'maxlength' in hints: |
| 156 |
if optimized: |
| 157 |
v_list = hop.inputarg(self, arg=0) |
| 158 |
v_maxlength = self._get_v_maxlength(hop) |
| 159 |
hop.llops.gendirectcall(ll_set_maxlength, v_list, v_maxlength) |
| 160 |
return v_list |
| 161 |
if 'fence' in hints: |
| 162 |
v_list = hop.inputarg(self, arg=0) |
| 163 |
if isinstance(hop.r_result, FixedSizeListRepr): |
| 164 |
if optimized and 'exactlength' in hints: |
| 165 |
llfn = ll_list2fixed_exact |
| 166 |
else: |
| 167 |
llfn = ll_list2fixed |
| 168 |
v_list = hop.llops.gendirectcall(llfn, v_list) |
| 169 |
return v_list |
| 170 |
return AbstractListRepr.rtype_hint(self, hop) |
| 171 |
|
| 172 |
|
| 173 |
class FixedSizeListRepr(AbstractFixedSizeListRepr, BaseListRepr): |
| 174 |
|
| 175 |
def _setup_repr(self): |
| 176 |
if 'item_repr' not in self.__dict__: |
| 177 |
self.external_item_repr, self.item_repr = externalvsinternal(self.rtyper, self._item_repr_computer(), gcref=True) |
| 178 |
if isinstance(self.LIST, GcForwardReference): |
| 179 |
ITEMARRAY = self.get_itemarray_lowleveltype() |
| 180 |
self.LIST.become(ITEMARRAY) |
| 181 |
|
| 182 |
def compact_repr(self): |
| 183 |
return 'FixedSizeListR %s' % (self.item_repr.compact_repr(),) |
| 184 |
|
| 185 |
def prepare_const(self, n): |
| 186 |
result = malloc(self.LIST, n, immortal=True) |
| 187 |
return result |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
@jit.look_inside_iff(lambda l, newsize, overallocate: jit.isconstant(len(l.items)) and jit.isconstant(newsize)) |
| 199 |
@signature(types.any(), types.int(), types.bool(), returns=types.none()) |
| 200 |
def _ll_list_resize_hint_really(l, newsize, overallocate): |
| 201 |
""" |
| 202 |
Ensure l.items has room for at least newsize elements. Note that |
| 203 |
l.items may change, and even if newsize is less than l.length on |
| 204 |
entry. |
| 205 |
""" |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if newsize <= 0: |
| 213 |
ll_assert(newsize == 0, "negative list length") |
| 214 |
l.length = 0 |
| 215 |
l.items = _ll_new_empty_item_array(typeOf(l).TO) |
| 216 |
return |
| 217 |
elif overallocate: |
| 218 |
if newsize < 9: |
| 219 |
some = 3 |
| 220 |
else: |
| 221 |
some = 6 |
| 222 |
some += newsize >> 3 |
| 223 |
new_allocated = newsize + some |
| 224 |
else: |
| 225 |
new_allocated = newsize |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
items = l.items |
| 231 |
newitems = malloc(typeOf(l).TO.items.TO, new_allocated) |
| 232 |
before_len = l.length |
| 233 |
if before_len: |
| 234 |
if before_len < newsize: |
| 235 |
p = before_len |
| 236 |
else: |
| 237 |
p = newsize |
| 238 |
rgc.ll_arraycopy(items, newitems, 0, 0, p) |
| 239 |
l.items = newitems |
| 240 |
|
| 241 |
@jit.look_inside_iff(lambda l, newsize: jit.isconstant(len(l.items)) and jit.isconstant(newsize)) |
| 242 |
def _ll_list_resize_hint(l, newsize): |
| 243 |
"""Ensure l.items has room for at least newsize elements without |
| 244 |
setting l.length to newsize. |
| 245 |
|
| 246 |
Used before (and after) a batch operation that will likely grow the |
| 247 |
list to the newsize (and after the operation incase the initial |
| 248 |
guess lied). |
| 249 |
""" |
| 250 |
assert newsize >= 0, "negative list length" |
| 251 |
allocated = len(l.items) |
| 252 |
if newsize > allocated: |
| 253 |
overallocate = True |
| 254 |
elif newsize < (allocated >> 1) - 5: |
| 255 |
overallocate = False |
| 256 |
else: |
| 257 |
return |
| 258 |
_ll_list_resize_hint_really(l, newsize, overallocate) |
| 259 |
|
| 260 |
@signature(types.any(), types.int(), types.bool(), returns=types.none()) |
| 261 |
def _ll_list_resize_really(l, newsize, overallocate): |
| 262 |
""" |
| 263 |
Ensure l.items has room for at least newsize elements, and set |
| 264 |
l.length to newsize. Note that l.items may change, and even if |
| 265 |
newsize is less than l.length on entry. |
| 266 |
""" |
| 267 |
_ll_list_resize_hint_really(l, newsize, overallocate) |
| 268 |
l.length = newsize |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
@jit.dont_look_inside |
| 274 |
def _ll_list_resize(l, newsize): |
| 275 |
"""Called only in special cases. Forces the allocated and actual size |
| 276 |
of the list to be 'newsize'.""" |
| 277 |
_ll_list_resize_really(l, newsize, False) |
| 278 |
|
| 279 |
|
| 280 |
def _ll_list_resize_ge(l, newsize): |
| 281 |
"""This is called with 'newsize' larger than the current length of the |
| 282 |
list. If the list storage doesn't have enough space, then really perform |
| 283 |
a realloc(). In the common case where we already overallocated enough, |
| 284 |
then this is a very fast operation. |
| 285 |
""" |
| 286 |
cond = len(l.items) < newsize |
| 287 |
if jit.isconstant(len(l.items)) and jit.isconstant(newsize): |
| 288 |
if cond: |
| 289 |
_ll_list_resize_hint_really(l, newsize, True) |
| 290 |
else: |
| 291 |
jit.conditional_call(cond, |
| 292 |
_ll_list_resize_hint_really, l, newsize, True) |
| 293 |
l.length = newsize |
| 294 |
|
| 295 |
def _ll_list_resize_le(l, newsize): |
| 296 |
"""This is called with 'newsize' smaller than the current length of the |
| 297 |
list. If 'newsize' falls lower than half the allocated size, proceed |
| 298 |
with the realloc() to shrink the list. |
| 299 |
""" |
| 300 |
cond = newsize < (len(l.items) >> 1) - 5 |
| 301 |
|
| 302 |
if jit.isconstant(len(l.items)) and jit.isconstant(newsize): |
| 303 |
if cond: |
| 304 |
_ll_list_resize_hint_really(l, newsize, False) |
| 305 |
else: |
| 306 |
jit.conditional_call(cond, _ll_list_resize_hint_really, l, newsize, |
| 307 |
False) |
| 308 |
l.length = newsize |
| 309 |
|
| 310 |
def ll_append_noresize(l, newitem): |
| 311 |
length = l.length |
| 312 |
l.length = length + 1 |
| 313 |
l.ll_setitem_fast(length, newitem) |
| 314 |
|
| 315 |
|
| 316 |
def ll_both_none(lst1, lst2): |
| 317 |
return not lst1 and not lst2 |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
def ll_newlist(LIST, length): |
| 325 |
ll_assert(length >= 0, "negative list length") |
| 326 |
l = malloc(LIST) |
| 327 |
l.length = length |
| 328 |
l.items = malloc(LIST.items.TO, length) |
| 329 |
return l |
| 330 |
ll_newlist = typeMethod(ll_newlist) |
| 331 |
ll_newlist.oopspec = 'newlist(length)' |
| 332 |
|
| 333 |
def ll_newlist_hint(LIST, lengthhint): |
| 334 |
ll_assert(lengthhint >= 0, "negative list length") |
| 335 |
l = malloc(LIST) |
| 336 |
l.length = 0 |
| 337 |
l.items = malloc(LIST.items.TO, lengthhint) |
| 338 |
return l |
| 339 |
ll_newlist_hint = typeMethod(ll_newlist_hint) |
| 340 |
ll_newlist_hint.oopspec = 'newlist_hint(lengthhint)' |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
INITIAL_EMPTY_LIST_ALLOCATION = 0 |
| 346 |
|
| 347 |
def _ll_prebuilt_empty_array(LISTITEM): |
| 348 |
return malloc(LISTITEM, 0) |
| 349 |
_ll_prebuilt_empty_array._annspecialcase_ = 'specialize:memo' |
| 350 |
|
| 351 |
def _ll_new_empty_item_array(LIST): |
| 352 |
if INITIAL_EMPTY_LIST_ALLOCATION > 0: |
| 353 |
return malloc(LIST.items.TO, INITIAL_EMPTY_LIST_ALLOCATION) |
| 354 |
else: |
| 355 |
return _ll_prebuilt_empty_array(LIST.items.TO) |
| 356 |
|
| 357 |
def ll_newemptylist(LIST): |
| 358 |
l = malloc(LIST) |
| 359 |
l.length = 0 |
| 360 |
l.items = _ll_new_empty_item_array(LIST) |
| 361 |
return l |
| 362 |
ll_newemptylist = typeMethod(ll_newemptylist) |
| 363 |
ll_newemptylist.oopspec = 'newlist(0)' |
| 364 |
|
| 365 |
def ll_length(l): |
| 366 |
return l.length |
| 367 |
ll_length.oopspec = 'list.len(l)' |
| 368 |
|
| 369 |
def ll_items(l): |
| 370 |
return l.items |
| 371 |
|
| 372 |
def ll_getitem_fast(l, index): |
| 373 |
ll_assert(index < l.length, "getitem out of bounds") |
| 374 |
return l.ll_items()[index] |
| 375 |
ll_getitem_fast.oopspec = 'list.getitem(l, index)' |
| 376 |
|
| 377 |
def ll_setitem_fast(l, index, item): |
| 378 |
ll_assert(index < l.length, "setitem out of bounds") |
| 379 |
l.ll_items()[index] = item |
| 380 |
ll_setitem_fast.oopspec = 'list.setitem(l, index, item)' |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
@typeMethod |
| 385 |
def ll_fixed_newlist(LIST, length): |
| 386 |
ll_assert(length >= 0, "negative fixed list length") |
| 387 |
l = malloc(LIST, length) |
| 388 |
return l |
| 389 |
ll_fixed_newlist.oopspec = 'newlist(length)' |
| 390 |
|
| 391 |
@typeMethod |
| 392 |
def ll_fixed_newemptylist(LIST): |
| 393 |
return ll_fixed_newlist(LIST, 0) |
| 394 |
|
| 395 |
def ll_fixed_length(l): |
| 396 |
return len(l) |
| 397 |
ll_fixed_length.oopspec = 'list.len(l)' |
| 398 |
|
| 399 |
def ll_fixed_items(l): |
| 400 |
return l |
| 401 |
|
| 402 |
def ll_fixed_getitem_fast(l, index): |
| 403 |
ll_assert(index < len(l), "fixed getitem out of bounds") |
| 404 |
return l[index] |
| 405 |
ll_fixed_getitem_fast.oopspec = 'list.getitem(l, index)' |
| 406 |
|
| 407 |
def ll_fixed_setitem_fast(l, index, item): |
| 408 |
ll_assert(index < len(l), "fixed setitem out of bounds") |
| 409 |
l[index] = item |
| 410 |
ll_fixed_setitem_fast.oopspec = 'list.setitem(l, index, item)' |
| 411 |
|
| 412 |
def newlist(llops, r_list, items_v, v_sizehint=None): |
| 413 |
LIST = r_list.LIST |
| 414 |
if len(items_v) == 0: |
| 415 |
if v_sizehint is None: |
| 416 |
v_result = llops.gendirectcall(LIST.ll_newemptylist) |
| 417 |
else: |
| 418 |
v_result = llops.gendirectcall(LIST.ll_newlist_hint, v_sizehint) |
| 419 |
else: |
| 420 |
assert v_sizehint is None |
| 421 |
cno = inputconst(Signed, len(items_v)) |
| 422 |
v_result = llops.gendirectcall(LIST.ll_newlist, cno) |
| 423 |
v_func = inputconst(Void, dum_nocheck) |
| 424 |
for i, v_item in enumerate(items_v): |
| 425 |
ci = inputconst(Signed, i) |
| 426 |
llops.gendirectcall(ll_setitem_nonneg, v_func, v_result, ci, v_item) |
| 427 |
return v_result |
| 428 |
|
| 429 |
|
| 430 |
def ll_set_maxlength(l, n): |
| 431 |
LIST = typeOf(l).TO |
| 432 |
l.items = malloc(LIST.items.TO, n) |
| 433 |
|
| 434 |
def ll_list2fixed(l): |
| 435 |
n = l.length |
| 436 |
olditems = l.items |
| 437 |
if n == len(olditems): |
| 438 |
return olditems |
| 439 |
else: |
| 440 |
LIST = typeOf(l).TO |
| 441 |
newitems = malloc(LIST.items.TO, n) |
| 442 |
rgc.ll_arraycopy(olditems, newitems, 0, 0, n) |
| 443 |
return newitems |
| 444 |
|
| 445 |
def ll_list2fixed_exact(l): |
| 446 |
ll_assert(l.length == len(l.items), "ll_list2fixed_exact: bad length") |
| 447 |
return l.items |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
class ListIteratorRepr(AbstractListIteratorRepr): |
| 454 |
|
| 455 |
def __init__(self, r_list): |
| 456 |
self.r_list = r_list |
| 457 |
self.external_item_repr = r_list.external_item_repr |
| 458 |
self.lowleveltype = Ptr(GcStruct('listiter', |
| 459 |
('list', r_list.lowleveltype), |
| 460 |
('index', Signed))) |
| 461 |
self.ll_listiter = ll_listiter |
| 462 |
if (isinstance(r_list, FixedSizeListRepr) |
| 463 |
and not r_list.listitem.mutated): |
| 464 |
self.ll_listnext = ll_listnext_foldable |
| 465 |
else: |
| 466 |
self.ll_listnext = ll_listnext |
| 467 |
self.ll_getnextindex = ll_getnextindex |
| 468 |
|
| 469 |
|
| 470 |
def ll_listiter(ITERPTR, lst): |
| 471 |
iter = malloc(ITERPTR.TO) |
| 472 |
iter.list = lst |
| 473 |
iter.index = 0 |
| 474 |
return iter |
| 475 |
|
| 476 |
def ll_listnext(iter): |
| 477 |
l = iter.list |
| 478 |
index = iter.index |
| 479 |
if index >= l.ll_length(): |
| 480 |
raise StopIteration |
| 481 |
iter.index = index + 1 |
| 482 |
return l.ll_getitem_fast(index) |
| 483 |
|
| 484 |
def ll_listnext_foldable(iter): |
| 485 |
from rpython.rtyper.rlist import ll_getitem_foldable_nonneg |
| 486 |
l = iter.list |
| 487 |
index = iter.index |
| 488 |
if index >= l.ll_length(): |
| 489 |
raise StopIteration |
| 490 |
iter.index = index + 1 |
| 491 |
return ll_getitem_foldable_nonneg(l, index) |
| 492 |
|
| 493 |
def ll_getnextindex(iter): |
| 494 |
return iter.index |
| 495 |
|
| 496 |
|
| 497 |
class ReversedListIteratorRepr(AbstractListIteratorRepr): |
| 498 |
def __init__(self, r_list): |
| 499 |
self.r_list = r_list |
| 500 |
self.lowleveltype = Ptr(GcStruct('revlistiter', |
| 501 |
('list', r_list.lowleveltype), |
| 502 |
('index', Signed), |
| 503 |
)) |
| 504 |
self.ll_listnext = ll_revlistnext |
| 505 |
self.ll_listiter = ll_revlistiter |
| 506 |
|
| 507 |
|
| 508 |
def ll_revlistiter(ITERPTR, lst): |
| 509 |
iter = malloc(ITERPTR.TO) |
| 510 |
iter.list = lst |
| 511 |
iter.index = lst.ll_length() - 1 |
| 512 |
return iter |
| 513 |
|
| 514 |
|
| 515 |
def ll_revlistnext(iter): |
| 516 |
l = iter.list |
| 517 |
index = iter.index |
| 518 |
if index < 0: |
| 519 |
raise StopIteration |
| 520 |
iter.index -= 1 |
| 521 |
return l.ll_getitem_fast(index) |