PageRenderTime 61ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/jit/metainterp/resume.py

https://bitbucket.org/pypy/pypy/
Python | 1317 lines | 1027 code | 184 blank | 106 comment | 173 complexity | a7acd0463da83d7a3cf0a0135d9e3198 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. import sys, os
  2. from pypy.jit.metainterp.history import Box, Const, ConstInt, getkind
  3. from pypy.jit.metainterp.history import BoxInt, BoxPtr, BoxFloat
  4. from pypy.jit.metainterp.history import INT, REF, FLOAT, HOLE
  5. from pypy.jit.metainterp.history import AbstractDescr
  6. from pypy.jit.metainterp.resoperation import rop
  7. from pypy.jit.metainterp import jitprof
  8. from pypy.jit.codewriter.effectinfo import EffectInfo
  9. from pypy.rpython.lltypesystem import lltype, llmemory, rffi, rstr
  10. from pypy.rpython import annlowlevel
  11. from pypy.rlib import rarithmetic, rstack
  12. from pypy.rlib.objectmodel import we_are_translated, specialize
  13. from pypy.rlib.debug import have_debug_prints, ll_assert
  14. from pypy.rlib.debug import debug_start, debug_stop, debug_print
  15. from pypy.jit.metainterp.optimize import InvalidLoop
  16. # Logic to encode the chain of frames and the state of the boxes at a
  17. # guard operation, and to decode it again. This is a bit advanced,
  18. # because it needs to support optimize.py which encodes virtuals with
  19. # arbitrary cycles and also to compress the information
  20. class Snapshot(object):
  21. __slots__ = ('prev', 'boxes')
  22. def __init__(self, prev, boxes):
  23. self.prev = prev
  24. self.boxes = boxes
  25. class FrameInfo(object):
  26. __slots__ = ('prev', 'jitcode', 'pc')
  27. def __init__(self, prev, jitcode, pc):
  28. self.prev = prev
  29. self.jitcode = jitcode
  30. self.pc = pc
  31. def _ensure_parent_resumedata(framestack, n):
  32. target = framestack[n]
  33. if n == 0:
  34. return
  35. back = framestack[n-1]
  36. if target.parent_resumedata_frame_info_list is not None:
  37. assert target.parent_resumedata_frame_info_list.pc == back.pc
  38. return
  39. _ensure_parent_resumedata(framestack, n-1)
  40. target.parent_resumedata_frame_info_list = FrameInfo(
  41. back.parent_resumedata_frame_info_list,
  42. back.jitcode,
  43. back.pc)
  44. target.parent_resumedata_snapshot = Snapshot(
  45. back.parent_resumedata_snapshot,
  46. back.get_list_of_active_boxes(True))
  47. def capture_resumedata(framestack, virtualizable_boxes, virtualref_boxes,
  48. storage):
  49. n = len(framestack)-1
  50. top = framestack[n]
  51. _ensure_parent_resumedata(framestack, n)
  52. frame_info_list = FrameInfo(top.parent_resumedata_frame_info_list,
  53. top.jitcode, top.pc)
  54. storage.rd_frame_info_list = frame_info_list
  55. snapshot = Snapshot(top.parent_resumedata_snapshot,
  56. top.get_list_of_active_boxes(False))
  57. if virtualizable_boxes is not None:
  58. boxes = virtualref_boxes + virtualizable_boxes
  59. else:
  60. boxes = virtualref_boxes[:]
  61. snapshot = Snapshot(snapshot, boxes)
  62. storage.rd_snapshot = snapshot
  63. #
  64. # The following is equivalent to the RPython-level declaration:
  65. #
  66. # class Numbering: __slots__ = ['prev', 'nums']
  67. #
  68. # except that it is more compact in translated programs, because the
  69. # array 'nums' is inlined in the single NUMBERING object. This is
  70. # important because this is often the biggest single consumer of memory
  71. # in a pypy-c-jit.
  72. #
  73. NUMBERINGP = lltype.Ptr(lltype.GcForwardReference())
  74. NUMBERING = lltype.GcStruct('Numbering',
  75. ('prev', NUMBERINGP),
  76. ('nums', lltype.Array(rffi.SHORT)))
  77. NUMBERINGP.TO.become(NUMBERING)
  78. PENDINGFIELDSTRUCT = lltype.Struct('PendingField',
  79. ('lldescr', annlowlevel.base_ptr_lltype()),
  80. ('num', rffi.SHORT),
  81. ('fieldnum', rffi.SHORT),
  82. ('itemindex', rffi.INT))
  83. PENDINGFIELDSP = lltype.Ptr(lltype.GcArray(PENDINGFIELDSTRUCT))
  84. TAGMASK = 3
  85. class TagOverflow(Exception):
  86. pass
  87. def tag(value, tagbits):
  88. assert 0 <= tagbits <= 3
  89. sx = value >> 13
  90. if sx != 0 and sx != -1:
  91. raise TagOverflow
  92. return rffi.r_short(value<<2|tagbits)
  93. def untag(value):
  94. value = rarithmetic.widen(value)
  95. tagbits = value&TAGMASK
  96. return value>>2, tagbits
  97. def tagged_eq(x, y):
  98. # please rpython :(
  99. return rarithmetic.widen(x) == rarithmetic.widen(y)
  100. def tagged_list_eq(tl1, tl2):
  101. if len(tl1) != len(tl2):
  102. return False
  103. for i in range(len(tl1)):
  104. if not tagged_eq(tl1[i], tl2[i]):
  105. return False
  106. return True
  107. TAGCONST = 0
  108. TAGINT = 1
  109. TAGBOX = 2
  110. TAGVIRTUAL = 3
  111. UNASSIGNED = tag(-1<<13, TAGBOX)
  112. UNASSIGNEDVIRTUAL = tag(-1<<13, TAGVIRTUAL)
  113. NULLREF = tag(-1, TAGCONST)
  114. UNINITIALIZED = tag(-2, TAGCONST) # used for uninitialized string characters
  115. class ResumeDataLoopMemo(object):
  116. def __init__(self, metainterp_sd):
  117. self.metainterp_sd = metainterp_sd
  118. self.cpu = metainterp_sd.cpu
  119. self.consts = []
  120. self.large_ints = {}
  121. self.refs = self.cpu.ts.new_ref_dict_2()
  122. self.numberings = {}
  123. self.cached_boxes = {}
  124. self.cached_virtuals = {}
  125. self.nvirtuals = 0
  126. self.nvholes = 0
  127. self.nvreused = 0
  128. def getconst(self, const):
  129. if const.type == INT:
  130. val = const.getint()
  131. if not we_are_translated() and not isinstance(val, int):
  132. # unhappiness, probably a symbolic
  133. return self._newconst(const)
  134. try:
  135. return tag(val, TAGINT)
  136. except TagOverflow:
  137. pass
  138. tagged = self.large_ints.get(val, UNASSIGNED)
  139. if not tagged_eq(tagged, UNASSIGNED):
  140. return tagged
  141. tagged = self._newconst(const)
  142. self.large_ints[val] = tagged
  143. return tagged
  144. elif const.type == REF:
  145. val = const.getref_base()
  146. if not val:
  147. return NULLREF
  148. tagged = self.refs.get(val, UNASSIGNED)
  149. if not tagged_eq(tagged, UNASSIGNED):
  150. return tagged
  151. tagged = self._newconst(const)
  152. self.refs[val] = tagged
  153. return tagged
  154. return self._newconst(const)
  155. def _newconst(self, const):
  156. result = tag(len(self.consts), TAGCONST)
  157. self.consts.append(const)
  158. return result
  159. # env numbering
  160. def number(self, values, snapshot):
  161. if snapshot is None:
  162. return lltype.nullptr(NUMBERING), {}, 0
  163. if snapshot in self.numberings:
  164. numb, liveboxes, v = self.numberings[snapshot]
  165. return numb, liveboxes.copy(), v
  166. numb1, liveboxes, v = self.number(values, snapshot.prev)
  167. n = len(liveboxes)-v
  168. boxes = snapshot.boxes
  169. length = len(boxes)
  170. numb = lltype.malloc(NUMBERING, length)
  171. for i in range(length):
  172. box = boxes[i]
  173. value = values.get(box, None)
  174. if value is not None:
  175. box = value.get_key_box()
  176. if isinstance(box, Const):
  177. tagged = self.getconst(box)
  178. elif box in liveboxes:
  179. tagged = liveboxes[box]
  180. else:
  181. if value is not None and value.is_virtual():
  182. tagged = tag(v, TAGVIRTUAL)
  183. v += 1
  184. else:
  185. tagged = tag(n, TAGBOX)
  186. n += 1
  187. liveboxes[box] = tagged
  188. numb.nums[i] = tagged
  189. #
  190. numb.prev = numb1
  191. self.numberings[snapshot] = numb, liveboxes, v
  192. return numb, liveboxes.copy(), v
  193. def forget_numberings(self, virtualbox):
  194. # XXX ideally clear only the affected numberings
  195. self.numberings.clear()
  196. self.clear_box_virtual_numbers()
  197. # caching for virtuals and boxes inside them
  198. def num_cached_boxes(self):
  199. return len(self.cached_boxes)
  200. def assign_number_to_box(self, box, boxes):
  201. # returns a negative number
  202. if box in self.cached_boxes:
  203. num = self.cached_boxes[box]
  204. boxes[-num-1] = box
  205. else:
  206. boxes.append(box)
  207. num = -len(boxes)
  208. self.cached_boxes[box] = num
  209. return num
  210. def num_cached_virtuals(self):
  211. return len(self.cached_virtuals)
  212. def assign_number_to_virtual(self, box):
  213. # returns a negative number
  214. if box in self.cached_virtuals:
  215. num = self.cached_virtuals[box]
  216. else:
  217. num = self.cached_virtuals[box] = -len(self.cached_virtuals) - 1
  218. return num
  219. def clear_box_virtual_numbers(self):
  220. self.cached_boxes.clear()
  221. self.cached_virtuals.clear()
  222. def update_counters(self, profiler):
  223. profiler.count(jitprof.NVIRTUALS, self.nvirtuals)
  224. profiler.count(jitprof.NVHOLES, self.nvholes)
  225. profiler.count(jitprof.NVREUSED, self.nvreused)
  226. _frame_info_placeholder = (None, 0, 0)
  227. class ResumeDataVirtualAdder(object):
  228. def __init__(self, storage, memo):
  229. self.storage = storage
  230. self.memo = memo
  231. def make_virtual(self, known_class, fielddescrs):
  232. return VirtualInfo(known_class, fielddescrs)
  233. def make_vstruct(self, typedescr, fielddescrs):
  234. return VStructInfo(typedescr, fielddescrs)
  235. def make_varray(self, arraydescr):
  236. return VArrayInfo(arraydescr)
  237. def make_varraystruct(self, arraydescr, fielddescrs):
  238. return VArrayStructInfo(arraydescr, fielddescrs)
  239. def make_vstrplain(self, is_unicode=False):
  240. if is_unicode:
  241. return VUniPlainInfo()
  242. return VStrPlainInfo()
  243. def make_vstrconcat(self, is_unicode=False):
  244. if is_unicode:
  245. return VUniConcatInfo()
  246. return VStrConcatInfo()
  247. def make_vstrslice(self, is_unicode=False):
  248. if is_unicode:
  249. return VUniSliceInfo()
  250. return VStrSliceInfo()
  251. def register_virtual_fields(self, virtualbox, fieldboxes):
  252. tagged = self.liveboxes_from_env.get(virtualbox, UNASSIGNEDVIRTUAL)
  253. self.liveboxes[virtualbox] = tagged
  254. self.vfieldboxes[virtualbox] = fieldboxes
  255. self._register_boxes(fieldboxes)
  256. def register_box(self, box):
  257. if (isinstance(box, Box) and box not in self.liveboxes_from_env
  258. and box not in self.liveboxes):
  259. self.liveboxes[box] = UNASSIGNED
  260. def _register_boxes(self, boxes):
  261. for box in boxes:
  262. self.register_box(box)
  263. def already_seen_virtual(self, virtualbox):
  264. if virtualbox not in self.liveboxes:
  265. assert virtualbox in self.liveboxes_from_env
  266. assert untag(self.liveboxes_from_env[virtualbox])[1] == TAGVIRTUAL
  267. return False
  268. tagged = self.liveboxes[virtualbox]
  269. _, tagbits = untag(tagged)
  270. return tagbits == TAGVIRTUAL
  271. def finish(self, values, pending_setfields=[]):
  272. # compute the numbering
  273. storage = self.storage
  274. # make sure that nobody attached resume data to this guard yet
  275. assert not storage.rd_numb
  276. snapshot = storage.rd_snapshot
  277. assert snapshot is not None # is that true?
  278. numb, liveboxes_from_env, v = self.memo.number(values, snapshot)
  279. self.liveboxes_from_env = liveboxes_from_env
  280. self.liveboxes = {}
  281. storage.rd_numb = numb
  282. storage.rd_snapshot = None
  283. # collect liveboxes and virtuals
  284. n = len(liveboxes_from_env) - v
  285. liveboxes = [None]*n
  286. self.vfieldboxes = {}
  287. for box, tagged in liveboxes_from_env.iteritems():
  288. i, tagbits = untag(tagged)
  289. if tagbits == TAGBOX:
  290. liveboxes[i] = box
  291. else:
  292. assert tagbits == TAGVIRTUAL
  293. value = values[box]
  294. value.get_args_for_fail(self)
  295. for _, box, fieldbox, _ in pending_setfields:
  296. self.register_box(box)
  297. self.register_box(fieldbox)
  298. value = values[fieldbox]
  299. value.get_args_for_fail(self)
  300. self._number_virtuals(liveboxes, values, v)
  301. self._add_pending_fields(pending_setfields)
  302. storage.rd_consts = self.memo.consts
  303. dump_storage(storage, liveboxes)
  304. return liveboxes[:]
  305. def _number_virtuals(self, liveboxes, values, num_env_virtuals):
  306. # !! 'liveboxes' is a list that is extend()ed in-place !!
  307. memo = self.memo
  308. new_liveboxes = [None] * memo.num_cached_boxes()
  309. count = 0
  310. # So far, self.liveboxes should contain 'tagged' values that are
  311. # either UNASSIGNED, UNASSIGNEDVIRTUAL, or a *non-negative* value
  312. # with the TAGVIRTUAL. The following loop removes the UNASSIGNED
  313. # and UNASSIGNEDVIRTUAL entries, and replaces them with real
  314. # negative values.
  315. for box, tagged in self.liveboxes.iteritems():
  316. i, tagbits = untag(tagged)
  317. if tagbits == TAGBOX:
  318. assert box not in self.liveboxes_from_env
  319. assert tagged_eq(tagged, UNASSIGNED)
  320. index = memo.assign_number_to_box(box, new_liveboxes)
  321. self.liveboxes[box] = tag(index, TAGBOX)
  322. count += 1
  323. else:
  324. assert tagbits == TAGVIRTUAL
  325. if tagged_eq(tagged, UNASSIGNEDVIRTUAL):
  326. assert box not in self.liveboxes_from_env
  327. index = memo.assign_number_to_virtual(box)
  328. self.liveboxes[box] = tag(index, TAGVIRTUAL)
  329. else:
  330. assert i >= 0
  331. new_liveboxes.reverse()
  332. liveboxes.extend(new_liveboxes)
  333. nholes = len(new_liveboxes) - count
  334. storage = self.storage
  335. storage.rd_virtuals = None
  336. vfieldboxes = self.vfieldboxes
  337. if vfieldboxes:
  338. length = num_env_virtuals + memo.num_cached_virtuals()
  339. virtuals = storage.rd_virtuals = [None] * length
  340. memo.nvirtuals += length
  341. memo.nvholes += length - len(vfieldboxes)
  342. for virtualbox, fieldboxes in vfieldboxes.iteritems():
  343. num, _ = untag(self.liveboxes[virtualbox])
  344. value = values[virtualbox]
  345. fieldnums = [self._gettagged(box)
  346. for box in fieldboxes]
  347. vinfo = value.make_virtual_info(self, fieldnums)
  348. # if a new vinfo instance is made, we get the fieldnums list we
  349. # pass in as an attribute. hackish.
  350. if vinfo.fieldnums is not fieldnums:
  351. memo.nvreused += 1
  352. virtuals[num] = vinfo
  353. if self._invalidation_needed(len(liveboxes), nholes):
  354. memo.clear_box_virtual_numbers()
  355. def _invalidation_needed(self, nliveboxes, nholes):
  356. memo = self.memo
  357. # xxx heuristic a bit out of thin air
  358. failargs_limit = memo.metainterp_sd.options.failargs_limit
  359. if nliveboxes > (failargs_limit // 2):
  360. if nholes > nliveboxes//3:
  361. return True
  362. return False
  363. def _add_pending_fields(self, pending_setfields):
  364. rd_pendingfields = lltype.nullptr(PENDINGFIELDSP.TO)
  365. if pending_setfields:
  366. n = len(pending_setfields)
  367. rd_pendingfields = lltype.malloc(PENDINGFIELDSP.TO, n)
  368. for i in range(n):
  369. descr, box, fieldbox, itemindex = pending_setfields[i]
  370. lldescr = annlowlevel.cast_instance_to_base_ptr(descr)
  371. num = self._gettagged(box)
  372. fieldnum = self._gettagged(fieldbox)
  373. # the index is limited to 2147483647 (64-bit machines only)
  374. if itemindex > 2147483647:
  375. raise TagOverflow
  376. itemindex = rffi.cast(rffi.INT, itemindex)
  377. #
  378. rd_pendingfields[i].lldescr = lldescr
  379. rd_pendingfields[i].num = num
  380. rd_pendingfields[i].fieldnum = fieldnum
  381. rd_pendingfields[i].itemindex= itemindex
  382. self.storage.rd_pendingfields = rd_pendingfields
  383. def _gettagged(self, box):
  384. if box is None:
  385. return UNINITIALIZED
  386. if isinstance(box, Const):
  387. return self.memo.getconst(box)
  388. else:
  389. if box in self.liveboxes_from_env:
  390. return self.liveboxes_from_env[box]
  391. return self.liveboxes[box]
  392. class AbstractVirtualInfo(object):
  393. #def allocate(self, decoder, index):
  394. # raise NotImplementedError
  395. def equals(self, fieldnums):
  396. return tagged_list_eq(self.fieldnums, fieldnums)
  397. def set_content(self, fieldnums):
  398. self.fieldnums = fieldnums
  399. def debug_prints(self):
  400. raise NotImplementedError
  401. class AbstractVirtualStructInfo(AbstractVirtualInfo):
  402. def __init__(self, fielddescrs):
  403. self.fielddescrs = fielddescrs
  404. #self.fieldnums = ...
  405. @specialize.argtype(1)
  406. def setfields(self, decoder, struct):
  407. for i in range(len(self.fielddescrs)):
  408. descr = self.fielddescrs[i]
  409. decoder.setfield(descr, struct, self.fieldnums[i])
  410. return struct
  411. def debug_prints(self):
  412. assert len(self.fielddescrs) == len(self.fieldnums)
  413. for i in range(len(self.fielddescrs)):
  414. debug_print("\t\t",
  415. str(self.fielddescrs[i]),
  416. str(untag(self.fieldnums[i])))
  417. class VirtualInfo(AbstractVirtualStructInfo):
  418. def __init__(self, known_class, fielddescrs):
  419. AbstractVirtualStructInfo.__init__(self, fielddescrs)
  420. self.known_class = known_class
  421. @specialize.argtype(1)
  422. def allocate(self, decoder, index):
  423. struct = decoder.allocate_with_vtable(self.known_class)
  424. decoder.virtuals_cache[index] = struct
  425. return self.setfields(decoder, struct)
  426. def debug_prints(self):
  427. debug_print("\tvirtualinfo", self.known_class.repr_rpython())
  428. AbstractVirtualStructInfo.debug_prints(self)
  429. class VStructInfo(AbstractVirtualStructInfo):
  430. def __init__(self, typedescr, fielddescrs):
  431. AbstractVirtualStructInfo.__init__(self, fielddescrs)
  432. self.typedescr = typedescr
  433. @specialize.argtype(1)
  434. def allocate(self, decoder, index):
  435. struct = decoder.allocate_struct(self.typedescr)
  436. decoder.virtuals_cache[index] = struct
  437. return self.setfields(decoder, struct)
  438. def debug_prints(self):
  439. debug_print("\tvstructinfo", self.typedescr.repr_rpython())
  440. AbstractVirtualStructInfo.debug_prints(self)
  441. class VArrayInfo(AbstractVirtualInfo):
  442. def __init__(self, arraydescr):
  443. self.arraydescr = arraydescr
  444. #self.fieldnums = ...
  445. @specialize.argtype(1)
  446. def allocate(self, decoder, index):
  447. length = len(self.fieldnums)
  448. arraydescr = self.arraydescr
  449. array = decoder.allocate_array(arraydescr, length)
  450. decoder.virtuals_cache[index] = array
  451. # NB. the check for the kind of array elements is moved out of the loop
  452. if arraydescr.is_array_of_pointers():
  453. for i in range(length):
  454. decoder.setarrayitem_ref(arraydescr, array, i,
  455. self.fieldnums[i])
  456. elif arraydescr.is_array_of_floats():
  457. for i in range(length):
  458. decoder.setarrayitem_float(arraydescr, array, i,
  459. self.fieldnums[i])
  460. else:
  461. for i in range(length):
  462. decoder.setarrayitem_int(arraydescr, array, i,
  463. self.fieldnums[i])
  464. return array
  465. def debug_prints(self):
  466. debug_print("\tvarrayinfo", self.arraydescr)
  467. for i in self.fieldnums:
  468. debug_print("\t\t", str(untag(i)))
  469. class VArrayStructInfo(AbstractVirtualInfo):
  470. def __init__(self, arraydescr, fielddescrs):
  471. self.arraydescr = arraydescr
  472. self.fielddescrs = fielddescrs
  473. def debug_prints(self):
  474. debug_print("\tvarraystructinfo", self.arraydescr)
  475. for i in self.fieldnums:
  476. debug_print("\t\t", str(untag(i)))
  477. @specialize.argtype(1)
  478. def allocate(self, decoder, index):
  479. array = decoder.allocate_array(self.arraydescr, len(self.fielddescrs))
  480. decoder.virtuals_cache[index] = array
  481. p = 0
  482. for i in range(len(self.fielddescrs)):
  483. for j in range(len(self.fielddescrs[i])):
  484. decoder.setinteriorfield(i, self.fielddescrs[i][j], array, self.fieldnums[p])
  485. p += 1
  486. return array
  487. class VStrPlainInfo(AbstractVirtualInfo):
  488. """Stands for the string made out of the characters of all fieldnums."""
  489. @specialize.argtype(1)
  490. def allocate(self, decoder, index):
  491. length = len(self.fieldnums)
  492. string = decoder.allocate_string(length)
  493. decoder.virtuals_cache[index] = string
  494. for i in range(length):
  495. charnum = self.fieldnums[i]
  496. if not tagged_eq(charnum, UNINITIALIZED):
  497. decoder.string_setitem(string, i, charnum)
  498. return string
  499. def debug_prints(self):
  500. debug_print("\tvstrplaininfo length", len(self.fieldnums))
  501. class VStrConcatInfo(AbstractVirtualInfo):
  502. """Stands for the string made out of the concatenation of two
  503. other strings."""
  504. @specialize.argtype(1)
  505. def allocate(self, decoder, index):
  506. # xxx for blackhole resuming, this will build all intermediate
  507. # strings and throw them away immediately, which is a bit sub-
  508. # efficient. Not sure we care.
  509. left, right = self.fieldnums
  510. string = decoder.concat_strings(left, right)
  511. decoder.virtuals_cache[index] = string
  512. return string
  513. def debug_prints(self):
  514. debug_print("\tvstrconcatinfo")
  515. for i in self.fieldnums:
  516. debug_print("\t\t", str(untag(i)))
  517. class VStrSliceInfo(AbstractVirtualInfo):
  518. """Stands for the string made out of slicing another string."""
  519. @specialize.argtype(1)
  520. def allocate(self, decoder, index):
  521. largerstr, start, length = self.fieldnums
  522. string = decoder.slice_string(largerstr, start, length)
  523. decoder.virtuals_cache[index] = string
  524. return string
  525. def debug_prints(self):
  526. debug_print("\tvstrsliceinfo")
  527. for i in self.fieldnums:
  528. debug_print("\t\t", str(untag(i)))
  529. class VUniPlainInfo(AbstractVirtualInfo):
  530. """Stands for the unicode string made out of the characters of all
  531. fieldnums."""
  532. @specialize.argtype(1)
  533. def allocate(self, decoder, index):
  534. length = len(self.fieldnums)
  535. string = decoder.allocate_unicode(length)
  536. decoder.virtuals_cache[index] = string
  537. for i in range(length):
  538. charnum = self.fieldnums[i]
  539. if not tagged_eq(charnum, UNINITIALIZED):
  540. decoder.unicode_setitem(string, i, charnum)
  541. return string
  542. def debug_prints(self):
  543. debug_print("\tvuniplaininfo length", len(self.fieldnums))
  544. class VUniConcatInfo(AbstractVirtualInfo):
  545. """Stands for the unicode string made out of the concatenation of two
  546. other unicode strings."""
  547. @specialize.argtype(1)
  548. def allocate(self, decoder, index):
  549. # xxx for blackhole resuming, this will build all intermediate
  550. # strings and throw them away immediately, which is a bit sub-
  551. # efficient. Not sure we care.
  552. left, right = self.fieldnums
  553. string = decoder.concat_unicodes(left, right)
  554. decoder.virtuals_cache[index] = string
  555. return string
  556. def debug_prints(self):
  557. debug_print("\tvuniconcatinfo")
  558. for i in self.fieldnums:
  559. debug_print("\t\t", str(untag(i)))
  560. class VUniSliceInfo(AbstractVirtualInfo):
  561. """Stands for the unicode string made out of slicing another
  562. unicode string."""
  563. @specialize.argtype(1)
  564. def allocate(self, decoder, index):
  565. largerstr, start, length = self.fieldnums
  566. string = decoder.slice_unicode(largerstr, start, length)
  567. decoder.virtuals_cache[index] = string
  568. return string
  569. def debug_prints(self):
  570. debug_print("\tvunisliceinfo")
  571. for i in self.fieldnums:
  572. debug_print("\t\t", str(untag(i)))
  573. # ____________________________________________________________
  574. class AbstractResumeDataReader(object):
  575. """A base mixin containing the logic to reconstruct virtuals out of
  576. guard failure. There are two implementations of this mixin:
  577. ResumeDataBoxReader for when we are compiling (i.e. when we have a
  578. metainterp), and ResumeDataDirectReader for when we are merely
  579. blackholing and want the best performance.
  580. """
  581. _mixin_ = True
  582. rd_virtuals = None
  583. virtuals_cache = None
  584. virtual_default = None
  585. def _init(self, cpu, storage):
  586. self.cpu = cpu
  587. self.cur_numb = storage.rd_numb
  588. self.consts = storage.rd_consts
  589. def _prepare(self, storage):
  590. self._prepare_virtuals(storage.rd_virtuals)
  591. self._prepare_pendingfields(storage.rd_pendingfields)
  592. def getvirtual(self, index):
  593. # Returns the index'th virtual, building it lazily if needed.
  594. # Note that this may be called recursively; that's why the
  595. # allocate() methods must fill in the cache as soon as they
  596. # have the object, before they fill its fields.
  597. assert self.virtuals_cache is not None
  598. v = self.virtuals_cache[index]
  599. if not v:
  600. v = self.rd_virtuals[index].allocate(self, index)
  601. ll_assert(v == self.virtuals_cache[index], "resume.py: bad cache")
  602. return v
  603. def force_all_virtuals(self):
  604. rd_virtuals = self.rd_virtuals
  605. if rd_virtuals:
  606. for i in range(len(rd_virtuals)):
  607. if rd_virtuals[i] is not None:
  608. self.getvirtual(i)
  609. return self.virtuals_cache
  610. def _prepare_virtuals(self, virtuals):
  611. if virtuals:
  612. self.rd_virtuals = virtuals
  613. self.virtuals_cache = [self.virtual_default] * len(virtuals)
  614. def _prepare_pendingfields(self, pendingfields):
  615. if pendingfields:
  616. for i in range(len(pendingfields)):
  617. lldescr = pendingfields[i].lldescr
  618. num = pendingfields[i].num
  619. fieldnum = pendingfields[i].fieldnum
  620. itemindex= pendingfields[i].itemindex
  621. descr = annlowlevel.cast_base_ptr_to_instance(AbstractDescr,
  622. lldescr)
  623. struct = self.decode_ref(num)
  624. itemindex = rffi.cast(lltype.Signed, itemindex)
  625. if itemindex < 0:
  626. self.setfield(descr, struct, fieldnum)
  627. else:
  628. self.setarrayitem(descr, struct, itemindex, fieldnum)
  629. def setarrayitem(self, arraydescr, array, index, fieldnum):
  630. if arraydescr.is_array_of_pointers():
  631. self.setarrayitem_ref(arraydescr, array, index, fieldnum)
  632. elif arraydescr.is_array_of_floats():
  633. self.setarrayitem_float(arraydescr, array, index, fieldnum)
  634. else:
  635. self.setarrayitem_int(arraydescr, array, index, fieldnum)
  636. def _prepare_next_section(self, info):
  637. # Use info.enumerate_vars(), normally dispatching to
  638. # pypy.jit.codewriter.jitcode. Some tests give a different 'info'.
  639. info.enumerate_vars(self._callback_i,
  640. self._callback_r,
  641. self._callback_f,
  642. self.unique_id) # <-- annotation hack
  643. self.cur_numb = self.cur_numb.prev
  644. def _callback_i(self, index, register_index):
  645. value = self.decode_int(self.cur_numb.nums[index])
  646. self.write_an_int(register_index, value)
  647. def _callback_r(self, index, register_index):
  648. value = self.decode_ref(self.cur_numb.nums[index])
  649. self.write_a_ref(register_index, value)
  650. def _callback_f(self, index, register_index):
  651. value = self.decode_float(self.cur_numb.nums[index])
  652. self.write_a_float(register_index, value)
  653. def done(self):
  654. self.cpu.clear_latest_values(self.cpu.get_latest_value_count())
  655. # ---------- when resuming for pyjitpl.py, make boxes ----------
  656. def rebuild_from_resumedata(metainterp, storage, virtualizable_info,
  657. greenfield_info):
  658. resumereader = ResumeDataBoxReader(storage, metainterp)
  659. boxes = resumereader.consume_vref_and_vable_boxes(virtualizable_info,
  660. greenfield_info)
  661. virtualizable_boxes, virtualref_boxes = boxes
  662. frameinfo = storage.rd_frame_info_list
  663. while True:
  664. f = metainterp.newframe(frameinfo.jitcode)
  665. f.setup_resume_at_op(frameinfo.pc)
  666. resumereader.consume_boxes(f.get_current_position_info(),
  667. f.registers_i, f.registers_r, f.registers_f)
  668. frameinfo = frameinfo.prev
  669. if frameinfo is None:
  670. break
  671. metainterp.framestack.reverse()
  672. resumereader.done()
  673. return resumereader.liveboxes, virtualizable_boxes, virtualref_boxes
  674. class ResumeDataBoxReader(AbstractResumeDataReader):
  675. unique_id = lambda: None
  676. def __init__(self, storage, metainterp):
  677. self._init(metainterp.cpu, storage)
  678. self.metainterp = metainterp
  679. self.liveboxes = [None] * metainterp.cpu.get_latest_value_count()
  680. self._prepare(storage)
  681. def consume_boxes(self, info, boxes_i, boxes_r, boxes_f):
  682. self.boxes_i = boxes_i
  683. self.boxes_r = boxes_r
  684. self.boxes_f = boxes_f
  685. self._prepare_next_section(info)
  686. def consume_virtualizable_boxes(self, vinfo, numb):
  687. # we have to ignore the initial part of 'nums' (containing vrefs),
  688. # find the virtualizable from nums[-1], and use it to know how many
  689. # boxes of which type we have to return. This does not write
  690. # anything into the virtualizable.
  691. index = len(numb.nums) - 1
  692. virtualizablebox = self.decode_ref(numb.nums[index])
  693. virtualizable = vinfo.unwrap_virtualizable_box(virtualizablebox)
  694. return vinfo.load_list_of_boxes(virtualizable, self, numb)
  695. def consume_virtualref_boxes(self, numb, end):
  696. # Returns a list of boxes, assumed to be all BoxPtrs.
  697. # We leave up to the caller to call vrefinfo.continue_tracing().
  698. assert (end & 1) == 0
  699. return [self.decode_ref(numb.nums[i]) for i in range(end)]
  700. def consume_vref_and_vable_boxes(self, vinfo, ginfo):
  701. numb = self.cur_numb
  702. self.cur_numb = numb.prev
  703. if vinfo is not None:
  704. virtualizable_boxes = self.consume_virtualizable_boxes(vinfo, numb)
  705. end = len(numb.nums) - len(virtualizable_boxes)
  706. elif ginfo is not None:
  707. index = len(numb.nums) - 1
  708. virtualizable_boxes = [self.decode_ref(numb.nums[index])]
  709. end = len(numb.nums) - 1
  710. else:
  711. virtualizable_boxes = None
  712. end = len(numb.nums)
  713. virtualref_boxes = self.consume_virtualref_boxes(numb, end)
  714. return virtualizable_boxes, virtualref_boxes
  715. def allocate_with_vtable(self, known_class):
  716. return self.metainterp.execute_and_record(rop.NEW_WITH_VTABLE,
  717. None, known_class)
  718. def allocate_struct(self, typedescr):
  719. return self.metainterp.execute_and_record(rop.NEW, typedescr)
  720. def allocate_array(self, arraydescr, length):
  721. return self.metainterp.execute_and_record(rop.NEW_ARRAY,
  722. arraydescr, ConstInt(length))
  723. def allocate_string(self, length):
  724. return self.metainterp.execute_and_record(rop.NEWSTR,
  725. None, ConstInt(length))
  726. def string_setitem(self, strbox, index, charnum):
  727. charbox = self.decode_box(charnum, INT)
  728. self.metainterp.execute_and_record(rop.STRSETITEM, None,
  729. strbox, ConstInt(index), charbox)
  730. def concat_strings(self, str1num, str2num):
  731. cic = self.metainterp.staticdata.callinfocollection
  732. calldescr, func = cic.callinfo_for_oopspec(EffectInfo.OS_STR_CONCAT)
  733. str1box = self.decode_box(str1num, REF)
  734. str2box = self.decode_box(str2num, REF)
  735. return self.metainterp.execute_and_record_varargs(
  736. rop.CALL, [ConstInt(func), str1box, str2box], calldescr)
  737. def slice_string(self, strnum, startnum, lengthnum):
  738. cic = self.metainterp.staticdata.callinfocollection
  739. calldescr, func = cic.callinfo_for_oopspec(EffectInfo.OS_STR_SLICE)
  740. strbox = self.decode_box(strnum, REF)
  741. startbox = self.decode_box(startnum, INT)
  742. lengthbox = self.decode_box(lengthnum, INT)
  743. stopbox = self.metainterp.execute_and_record(rop.INT_ADD, None,
  744. startbox, lengthbox)
  745. return self.metainterp.execute_and_record_varargs(
  746. rop.CALL, [ConstInt(func), strbox, startbox, stopbox], calldescr)
  747. def allocate_unicode(self, length):
  748. return self.metainterp.execute_and_record(rop.NEWUNICODE,
  749. None, ConstInt(length))
  750. def unicode_setitem(self, strbox, index, charnum):
  751. charbox = self.decode_box(charnum, INT)
  752. self.metainterp.execute_and_record(rop.UNICODESETITEM, None,
  753. strbox, ConstInt(index), charbox)
  754. def concat_unicodes(self, str1num, str2num):
  755. cic = self.metainterp.staticdata.callinfocollection
  756. calldescr, func = cic.callinfo_for_oopspec(EffectInfo.OS_UNI_CONCAT)
  757. str1box = self.decode_box(str1num, REF)
  758. str2box = self.decode_box(str2num, REF)
  759. return self.metainterp.execute_and_record_varargs(
  760. rop.CALL, [ConstInt(func), str1box, str2box], calldescr)
  761. def slice_unicode(self, strnum, startnum, lengthnum):
  762. cic = self.metainterp.staticdata.callinfocollection
  763. calldescr, func = cic.callinfo_for_oopspec(EffectInfo.OS_UNI_SLICE)
  764. strbox = self.decode_box(strnum, REF)
  765. startbox = self.decode_box(startnum, INT)
  766. lengthbox = self.decode_box(lengthnum, INT)
  767. stopbox = self.metainterp.execute_and_record(rop.INT_ADD, None,
  768. startbox, lengthbox)
  769. return self.metainterp.execute_and_record_varargs(
  770. rop.CALL, [ConstInt(func), strbox, startbox, stopbox], calldescr)
  771. def setfield(self, descr, structbox, fieldnum):
  772. if descr.is_pointer_field():
  773. kind = REF
  774. elif descr.is_float_field():
  775. kind = FLOAT
  776. else:
  777. kind = INT
  778. fieldbox = self.decode_box(fieldnum, kind)
  779. self.metainterp.execute_and_record(rop.SETFIELD_GC, descr,
  780. structbox, fieldbox)
  781. def setinteriorfield(self, index, descr, array, fieldnum):
  782. if descr.is_pointer_field():
  783. kind = REF
  784. elif descr.is_float_field():
  785. kind = FLOAT
  786. else:
  787. kind = INT
  788. fieldbox = self.decode_box(fieldnum, kind)
  789. self.metainterp.execute_and_record(rop.SETINTERIORFIELD_GC, descr,
  790. array, ConstInt(index), fieldbox)
  791. def setarrayitem_int(self, arraydescr, arraybox, index, fieldnum):
  792. self._setarrayitem(arraydescr, arraybox, index, fieldnum, INT)
  793. def setarrayitem_ref(self, arraydescr, arraybox, index, fieldnum):
  794. self._setarrayitem(arraydescr, arraybox, index, fieldnum, REF)
  795. def setarrayitem_float(self, arraydescr, arraybox, index, fieldnum):
  796. self._setarrayitem(arraydescr, arraybox, index, fieldnum, FLOAT)
  797. def _setarrayitem(self, arraydescr, arraybox, index, fieldnum, kind):
  798. itembox = self.decode_box(fieldnum, kind)
  799. self.metainterp.execute_and_record(rop.SETARRAYITEM_GC,
  800. arraydescr, arraybox,
  801. ConstInt(index), itembox)
  802. def decode_int(self, tagged):
  803. return self.decode_box(tagged, INT)
  804. def decode_ref(self, tagged):
  805. return self.decode_box(tagged, REF)
  806. def decode_float(self, tagged):
  807. return self.decode_box(tagged, FLOAT)
  808. def decode_box(self, tagged, kind):
  809. num, tag = untag(tagged)
  810. if tag == TAGCONST:
  811. if tagged_eq(tagged, NULLREF):
  812. box = self.cpu.ts.CONST_NULL
  813. else:
  814. box = self.consts[num]
  815. elif tag == TAGVIRTUAL:
  816. box = self.getvirtual(num)
  817. elif tag == TAGINT:
  818. box = ConstInt(num)
  819. else:
  820. assert tag == TAGBOX
  821. box = self.liveboxes[num]
  822. if box is None:
  823. box = self.load_box_from_cpu(num, kind)
  824. assert box.type == kind
  825. return box
  826. def load_box_from_cpu(self, num, kind):
  827. if num < 0:
  828. num += len(self.liveboxes)
  829. assert num >= 0
  830. if kind == INT:
  831. box = BoxInt(self.cpu.get_latest_value_int(num))
  832. elif kind == REF:
  833. box = BoxPtr(self.cpu.get_latest_value_ref(num))
  834. elif kind == FLOAT:
  835. box = BoxFloat(self.cpu.get_latest_value_float(num))
  836. else:
  837. assert 0, "bad kind: %d" % ord(kind)
  838. self.liveboxes[num] = box
  839. return box
  840. def decode_box_of_type(self, TYPE, tagged):
  841. kind = getkind(TYPE)
  842. if kind == 'int': kind = INT
  843. elif kind == 'ref': kind = REF
  844. elif kind == 'float': kind = FLOAT
  845. else: raise AssertionError(kind)
  846. return self.decode_box(tagged, kind)
  847. decode_box_of_type._annspecialcase_ = 'specialize:arg(1)'
  848. def write_an_int(self, index, box):
  849. self.boxes_i[index] = box
  850. def write_a_ref(self, index, box):
  851. self.boxes_r[index] = box
  852. def write_a_float(self, index, box):
  853. self.boxes_f[index] = box
  854. # ---------- when resuming for blackholing, get direct values ----------
  855. def blackhole_from_resumedata(blackholeinterpbuilder, jitdriver_sd, storage,
  856. all_virtuals=None):
  857. # The initialization is stack-critical code: it must not be interrupted by
  858. # StackOverflow, otherwise the jit_virtual_refs are left in a dangling state.
  859. rstack._stack_criticalcode_start()
  860. try:
  861. resumereader = ResumeDataDirectReader(blackholeinterpbuilder.metainterp_sd,
  862. storage, all_virtuals)
  863. vinfo = jitdriver_sd.virtualizable_info
  864. ginfo = jitdriver_sd.greenfield_info
  865. vrefinfo = blackholeinterpbuilder.metainterp_sd.virtualref_info
  866. resumereader.consume_vref_and_vable(vrefinfo, vinfo, ginfo)
  867. finally:
  868. rstack._stack_criticalcode_stop()
  869. #
  870. # First get a chain of blackhole interpreters whose length is given
  871. # by the depth of rd_frame_info_list. The first one we get must be
  872. # the bottom one, i.e. the last one in the chain, in order to make
  873. # the comment in BlackholeInterpreter.setposition() valid.
  874. nextbh = None
  875. frameinfo = storage.rd_frame_info_list
  876. while True:
  877. curbh = blackholeinterpbuilder.acquire_interp()
  878. curbh.nextblackholeinterp = nextbh
  879. nextbh = curbh
  880. frameinfo = frameinfo.prev
  881. if frameinfo is None:
  882. break
  883. firstbh = nextbh
  884. #
  885. # Now fill the blackhole interpreters with resume data.
  886. curbh = firstbh
  887. frameinfo = storage.rd_frame_info_list
  888. while True:
  889. curbh.setposition(frameinfo.jitcode, frameinfo.pc)
  890. resumereader.consume_one_section(curbh)
  891. curbh = curbh.nextblackholeinterp
  892. frameinfo = frameinfo.prev
  893. if frameinfo is None:
  894. break
  895. resumereader.done()
  896. return firstbh
  897. def force_from_resumedata(metainterp_sd, storage, vinfo, ginfo):
  898. resumereader = ResumeDataDirectReader(metainterp_sd, storage)
  899. resumereader.handling_async_forcing()
  900. vrefinfo = metainterp_sd.virtualref_info
  901. resumereader.consume_vref_and_vable(vrefinfo, vinfo, ginfo)
  902. return resumereader.force_all_virtuals()
  903. class ResumeDataDirectReader(AbstractResumeDataReader):
  904. unique_id = lambda: None
  905. virtual_default = lltype.nullptr(llmemory.GCREF.TO)
  906. resume_after_guard_not_forced = 0
  907. # 0: not a GUARD_NOT_FORCED
  908. # 1: in handle_async_forcing
  909. # 2: resuming from the GUARD_NOT_FORCED
  910. def __init__(self, metainterp_sd, storage, all_virtuals=None):
  911. self._init(metainterp_sd.cpu, storage)
  912. self.callinfocollection = metainterp_sd.callinfocollection
  913. if all_virtuals is None: # common case
  914. self._prepare(storage)
  915. else:
  916. # special case for resuming after a GUARD_NOT_FORCED: we already
  917. # have the virtuals
  918. self.resume_after_guard_not_forced = 2
  919. self.virtuals_cache = all_virtuals
  920. # self.rd_virtuals can remain None, because virtuals_cache is
  921. # already filled
  922. def handling_async_forcing(self):
  923. self.resume_after_guard_not_forced = 1
  924. def consume_one_section(self, blackholeinterp):
  925. self.blackholeinterp = blackholeinterp
  926. info = blackholeinterp.get_current_position_info()
  927. self._prepare_next_section(info)
  928. def consume_virtualref_info(self, vrefinfo, numb, end):
  929. # we have to decode a list of references containing pairs
  930. # [..., virtual, vref, ...] stopping at 'end'
  931. assert (end & 1) == 0
  932. for i in range(0, end, 2):
  933. virtual = self.decode_ref(numb.nums[i])
  934. vref = self.decode_ref(numb.nums[i+1])
  935. # For each pair, we store the virtual inside the vref.
  936. vrefinfo.continue_tracing(vref, virtual)
  937. def consume_vable_info(self, vinfo, numb):
  938. # we have to ignore the initial part of 'nums' (containing vrefs),
  939. # find the virtualizable from nums[-1], load all other values
  940. # from the CPU stack, and copy them into the virtualizable
  941. if vinfo is None:
  942. return len(numb.nums)
  943. index = len(numb.nums) - 1
  944. virtualizable = self.decode_ref(numb.nums[index])
  945. if self.resume_after_guard_not_forced == 1:
  946. # in the middle of handle_async_forcing()
  947. assert vinfo.gettoken(virtualizable)
  948. vinfo.settoken(virtualizable, vinfo.TOKEN_NONE)
  949. else:
  950. # just jumped away from assembler (case 4 in the comment in
  951. # virtualizable.py) into tracing (case 2); check that vable_token
  952. # is and stays 0. Note the call to reset_vable_token() in
  953. # warmstate.py.
  954. assert not vinfo.gettoken(virtualizable)
  955. return vinfo.write_from_resume_data_partial(virtualizable, self, numb)
  956. def load_value_of_type(self, TYPE, tagged):
  957. from pypy.jit.metainterp.warmstate import specialize_value
  958. kind = getkind(TYPE)
  959. if kind == 'int':
  960. x = self.decode_int(tagged)
  961. elif kind == 'ref':
  962. x = self.decode_ref(tagged)
  963. elif kind == 'float':
  964. x = self.decode_float(tagged)
  965. else:
  966. raise AssertionError(kind)
  967. return specialize_value(TYPE, x)
  968. load_value_of_type._annspecialcase_ = 'specialize:arg(1)'
  969. def consume_vref_and_vable(self, vrefinfo, vinfo, ginfo):
  970. numb = self.cur_numb
  971. self.cur_numb = numb.prev
  972. if self.resume_after_guard_not_forced != 2:
  973. end_vref = self.consume_vable_info(vinfo, numb)
  974. if ginfo is not None: end_vref -= 1
  975. self.consume_virtualref_info(vrefinfo, numb, end_vref)
  976. def allocate_with_vtable(self, known_class):
  977. from pypy.jit.metainterp.executor import exec_new_with_vtable
  978. return exec_new_with_vtable(self.cpu, known_class)
  979. def allocate_struct(self, typedescr):
  980. return self.cpu.bh_new(typedescr)
  981. def allocate_array(self, arraydescr, length):
  982. return self.cpu.bh_new_array(arraydescr, length)
  983. def allocate_string(self, length):
  984. return self.cpu.bh_newstr(length)
  985. def string_setitem(self, str, index, charnum):
  986. char = self.decode_int(charnum)
  987. self.cpu.bh_strsetitem(str, index, char)
  988. def concat_strings(self, str1num, str2num):
  989. str1 = self.decode_ref(str1num)
  990. str2 = self.decode_ref(str2num)
  991. str1 = lltype.cast_opaque_ptr(lltype.Ptr(rstr.STR), str1)
  992. str2 = lltype.cast_opaque_ptr(lltype.Ptr(rstr.STR), str2)
  993. cic = self.callinfocollection
  994. funcptr = cic.funcptr_for_oopspec(EffectInfo.OS_STR_CONCAT)
  995. result = funcptr(str1, str2)
  996. return lltype.cast_opaque_ptr(llmemory.GCREF, result)
  997. def slice_string(self, strnum, startnum, lengthnum):
  998. str = self.decode_ref(strnum)
  999. start = self.decode_int(startnum)
  1000. length = self.decode_int(lengthnum)
  1001. str = lltype.cast_opaque_ptr(lltype.Ptr(rstr.STR), str)
  1002. cic = self.callinfocollection
  1003. funcptr = cic.funcptr_for_oopspec(EffectInfo.OS_STR_SLICE)
  1004. result = funcptr(str, start, start + length)
  1005. return lltype.cast_opaque_ptr(llmemory.GCREF, result)
  1006. def allocate_unicode(self, length):
  1007. return self.cpu.bh_newunicode(length)
  1008. def unicode_setitem(self, str, index, charnum):
  1009. char = self.decode_int(charnum)
  1010. self.cpu.bh_unicodesetitem(str, index, char)
  1011. def concat_unicodes(self, str1num, str2num):
  1012. str1 = self.decode_ref(str1num)
  1013. str2 = self.decode_ref(str2num)
  1014. str1 = lltype.cast_opaque_ptr(lltype.Ptr(rstr.UNICODE), str1)
  1015. str2 = lltype.cast_opaque_ptr(lltype.Ptr(rstr.UNICODE), str2)
  1016. cic = self.callinfocollection
  1017. funcptr = cic.funcptr_for_oopspec(EffectInfo.OS_UNI_CONCAT)
  1018. result = funcptr(str1, str2)
  1019. return lltype.cast_opaque_ptr(llmemory.GCREF, result)
  1020. def slice_unicode(self, strnum, startnum, lengthnum):
  1021. str = self.decode_ref(strnum)
  1022. start = self.decode_int(startnum)
  1023. length = self.decode_int(lengthnum)
  1024. str = lltype.cast_opaque_ptr(lltype.Ptr(rstr.UNICODE), str)
  1025. cic = self.callinfocollection
  1026. funcptr = cic.funcptr_for_oopspec(EffectInfo.OS_UNI_SLICE)
  1027. result = funcptr(str, start, start + length)
  1028. return lltype.cast_opaque_ptr(llmemory.GCREF, result)
  1029. def setfield(self, descr, struct, fieldnum):
  1030. if descr.is_pointer_field():
  1031. newvalue = self.decode_ref(fieldnum)
  1032. self.cpu.bh_setfield_gc_r(struct, descr, newvalue)
  1033. elif descr.is_float_field():
  1034. newvalue = self.decode_float(fieldnum)
  1035. self.cpu.bh_setfield_gc_f(struct, descr, newvalue)
  1036. else:
  1037. newvalue = self.decode_int(fieldnum)
  1038. self.cpu.bh_setfield_gc_i(struct, descr, newvalue)
  1039. def setinteriorfield(self, index, descr, array, fieldnum):
  1040. if descr.is_pointer_field():
  1041. newvalue = self.decode_ref(fieldnum)
  1042. self.cpu.bh_setinteriorfield_gc_r(array, index, descr, newvalue)
  1043. elif descr.is_float_field():
  1044. newvalue = self.decode_float(fieldnum)
  1045. self.cpu.bh_setinteriorfield_gc_f(array, index, descr, newvalue)
  1046. else:
  1047. newvalue = self.decode_int(fieldnum)
  1048. self.cpu.bh_setinteriorfield_gc_i(array, index, descr, newvalue)
  1049. def setarrayitem_int(self, arraydescr, array, index, fieldnum):
  1050. newvalue = self.decode_int(fieldnum)
  1051. self.cpu.bh_setarrayitem_gc_i(arraydescr, array, index, newvalue)
  1052. def setarrayitem_ref(self, arraydescr, array, index, fieldnum):
  1053. newvalue = self.decode_ref(fieldnum)
  1054. self.cpu.bh_setarrayitem_gc_r(arraydescr, array, index, newvalue)
  1055. def setarrayitem_float(self, arraydescr, array, index, fieldnum):
  1056. newvalue = self.decode_float(fieldnum)
  1057. self.cpu.bh_setarrayitem_gc_f(arraydescr, array, index, newvalue)
  1058. def decode_int(self, tagged):
  1059. num, tag = untag(tagged)
  1060. if tag == TAGCONST:
  1061. return self.consts[num].getint()
  1062. elif tag == TAGINT:
  1063. return num
  1064. else:
  1065. assert tag == TAGBOX
  1066. if num < 0:
  1067. num += self.cpu.get_latest_value_count()
  1068. return self.cpu.get_latest_value_int(num)
  1069. def decode_ref(self, tagged):
  1070. num, tag = untag(tagged)
  1071. if tag == TAGCONST:
  1072. if tagged_eq(tagged, NULLREF):
  1073. return self.cpu.ts.NULLREF
  1074. return self.consts[num].getref_base()
  1075. elif tag == TAGVIRTUAL:
  1076. return self.getvirtual(num)
  1077. else:
  1078. assert tag == TAGBOX
  1079. if num < 0:
  1080. num += self.cpu.get_latest_value_count()
  1081. return self.cpu.get_latest_value_ref(num)
  1082. def decode_float(self, tagged):
  1083. num, tag = untag(tagged)
  1084. if tag == TAGCONST:
  1085. return self.consts[num].getfloatstorage()
  1086. else:
  1087. assert tag == TAGBOX
  1088. if num < 0:
  1089. num += self.cpu.get_latest_value_count()
  1090. return self.cpu.get_latest_value_float(num)
  1091. def write_an_int(self, index, int):
  1092. self.blackholeinterp.setarg_i(index, int)
  1093. def write_a_ref(self, index, ref):
  1094. self.blackholeinterp.setarg_r(index, ref)
  1095. def write_a_float(self, index, float):
  1096. self.blackholeinterp.setarg_f(index, float)
  1097. # ____________________________________________________________
  1098. def dump_storage(storage, liveboxes):
  1099. "For profiling only."
  1100. from pypy.rlib.objectmodel import compute_unique_id
  1101. debug_start("jit-resume")
  1102. if have_debug_prints():
  1103. debug_print('Log storage', compute_uni

Large files files are truncated, but you can click here to view the full file