PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/pypy/jit/metainterp/resume.py

http://github.com/pypy/pypy
Python | 1316 lines | 1026 code | 184 blank | 106 comment | 172 complexity | 4a4f50fad1fed7ca24bf14832a779e15 MD5 | raw file

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

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