PageRenderTime 58ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/rpython/jit/codewriter/jtransform.py

https://bitbucket.org/pypy/pypy/
Python | 2170 lines | 2044 code | 67 blank | 59 comment | 101 complexity | fd99a2f9e14c94d81b1ce8e9274c221b 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 py
  2. from rpython.jit.codewriter import support, heaptracker, longlong
  3. from rpython.jit.codewriter.effectinfo import EffectInfo
  4. from rpython.jit.codewriter.flatten import ListOfKind, IndirectCallTargets
  5. from rpython.jit.codewriter.policy import log
  6. from rpython.jit.metainterp import quasiimmut
  7. from rpython.jit.metainterp.history import getkind
  8. from rpython.jit.metainterp.typesystem import deref, arrayItem
  9. from rpython.jit.metainterp.blackhole import BlackholeInterpreter
  10. from rpython.flowspace.model import SpaceOperation, Variable, Constant,\
  11. c_last_exception
  12. from rpython.rlib import objectmodel
  13. from rpython.rlib.jit import _we_are_jitted
  14. from rpython.rlib.rgc import lltype_is_gc
  15. from rpython.rtyper.lltypesystem import lltype, llmemory, rstr, rffi
  16. from rpython.rtyper.lltypesystem import rbytearray
  17. from rpython.rtyper import rclass
  18. from rpython.rtyper.rclass import IR_QUASIIMMUTABLE, IR_QUASIIMMUTABLE_ARRAY
  19. from rpython.translator.unsimplify import varoftype
  20. class UnsupportedMallocFlags(Exception):
  21. pass
  22. def transform_graph(graph, cpu=None, callcontrol=None, portal_jd=None):
  23. """Transform a control flow graph to make it suitable for
  24. being flattened in a JitCode.
  25. """
  26. constant_fold_ll_issubclass(graph, cpu)
  27. t = Transformer(cpu, callcontrol, portal_jd)
  28. t.transform(graph)
  29. def constant_fold_ll_issubclass(graph, cpu):
  30. # ll_issubclass can be inserted by the inliner to check exception types.
  31. # See corner case metainterp.test.test_exception:test_catch_different_class
  32. if cpu is None:
  33. return
  34. excmatch = cpu.rtyper.exceptiondata.fn_exception_match
  35. for block in list(graph.iterblocks()):
  36. for i, op in enumerate(block.operations):
  37. if (op.opname == 'direct_call' and
  38. all(isinstance(a, Constant) for a in op.args) and
  39. op.args[0].value._obj is excmatch._obj):
  40. constant_result = excmatch(*[a.value for a in op.args[1:]])
  41. block.operations[i] = SpaceOperation(
  42. 'same_as',
  43. [Constant(constant_result, lltype.Bool)],
  44. op.result)
  45. if block.exitswitch is op.result:
  46. block.exitswitch = None
  47. block.recloseblock(*[link for link in block.exits
  48. if link.exitcase == constant_result])
  49. def integer_bounds(size, unsigned):
  50. if unsigned:
  51. return 0, 1 << (8 * size)
  52. else:
  53. return -(1 << (8 * size - 1)), 1 << (8 * size - 1)
  54. class Transformer(object):
  55. vable_array_vars = None
  56. def __init__(self, cpu=None, callcontrol=None, portal_jd=None):
  57. self.cpu = cpu
  58. self.callcontrol = callcontrol
  59. self.portal_jd = portal_jd # non-None only for the portal graph(s)
  60. def transform(self, graph):
  61. self.graph = graph
  62. for block in list(graph.iterblocks()):
  63. self.optimize_block(block)
  64. def optimize_block(self, block):
  65. if block.operations == ():
  66. return
  67. self.vable_array_vars = {}
  68. self.vable_flags = {}
  69. renamings = {}
  70. renamings_constants = {} # subset of 'renamings', {Var:Const} only
  71. newoperations = []
  72. #
  73. def do_rename(var, var_or_const):
  74. if var.concretetype is lltype.Void:
  75. renamings[var] = Constant(None, lltype.Void)
  76. return
  77. renamings[var] = var_or_const
  78. if isinstance(var_or_const, Constant):
  79. value = var_or_const.value
  80. value = lltype._cast_whatever(var.concretetype, value)
  81. renamings_constants[var] = Constant(value, var.concretetype)
  82. #
  83. for op in block.operations:
  84. if renamings_constants:
  85. op = self._do_renaming(renamings_constants, op)
  86. oplist = self.rewrite_operation(op)
  87. #
  88. count_before_last_operation = len(newoperations)
  89. if not isinstance(oplist, list):
  90. oplist = [oplist]
  91. for op1 in oplist:
  92. if isinstance(op1, SpaceOperation):
  93. newoperations.append(self._do_renaming(renamings, op1))
  94. elif op1 is None:
  95. # rewrite_operation() returns None to mean "has no real
  96. # effect, the result should just be renamed to args[0]"
  97. if op.result is not None:
  98. do_rename(op.result, renamings.get(op.args[0],
  99. op.args[0]))
  100. elif isinstance(op1, Constant):
  101. do_rename(op.result, op1)
  102. else:
  103. raise TypeError(repr(op1))
  104. #
  105. if block.canraise:
  106. if len(newoperations) == count_before_last_operation:
  107. self._killed_exception_raising_operation(block)
  108. block.operations = newoperations
  109. block.exitswitch = renamings.get(block.exitswitch, block.exitswitch)
  110. self.follow_constant_exit(block)
  111. self.optimize_goto_if_not(block)
  112. if isinstance(block.exitswitch, tuple):
  113. self._check_no_vable_array(block.exitswitch)
  114. for link in block.exits:
  115. self._check_no_vable_array(link.args)
  116. self._do_renaming_on_link(renamings, link)
  117. def _do_renaming(self, rename, op):
  118. op = SpaceOperation(op.opname, op.args[:], op.result)
  119. for i, v in enumerate(op.args):
  120. if isinstance(v, Variable):
  121. if v in rename:
  122. op.args[i] = rename[v]
  123. elif isinstance(v, ListOfKind):
  124. newlst = []
  125. for x in v:
  126. if x in rename:
  127. x = rename[x]
  128. newlst.append(x)
  129. op.args[i] = ListOfKind(v.kind, newlst)
  130. return op
  131. def _check_no_vable_array(self, list):
  132. if not self.vable_array_vars:
  133. return
  134. for v in list:
  135. if v in self.vable_array_vars:
  136. vars = self.vable_array_vars[v]
  137. (v_base, arrayfielddescr, arraydescr) = vars
  138. raise AssertionError(
  139. "A virtualizable array is passed around; it should\n"
  140. "only be used immediately after being read. Note\n"
  141. "that a possible cause is indexing with an index not\n"
  142. "known non-negative, or catching IndexError, or\n"
  143. "not inlining at all (for tests: use listops=True).\n"
  144. "This is about: %r\n"
  145. "Occurred in: %r" % (arrayfielddescr, self.graph))
  146. # extra explanation: with the way things are organized in
  147. # rpython/rlist.py, the ll_getitem becomes a function call
  148. # that is typically meant to be inlined by the JIT, but
  149. # this does not work with vable arrays because
  150. # jtransform.py expects the getfield and the getarrayitem
  151. # to be in the same basic block. It works a bit as a hack
  152. # for simple cases where we performed the backendopt
  153. # inlining before (even with a very low threshold, because
  154. # there is _always_inline_ on the relevant functions).
  155. def _do_renaming_on_link(self, rename, link):
  156. for i, v in enumerate(link.args):
  157. if isinstance(v, Variable):
  158. if v in rename:
  159. link.args[i] = rename[v]
  160. def _killed_exception_raising_operation(self, block):
  161. assert block.exits[0].exitcase is None
  162. block.exits = block.exits[:1]
  163. block.exitswitch = None
  164. # ----------
  165. def follow_constant_exit(self, block):
  166. v = block.exitswitch
  167. if isinstance(v, Constant) and not block.canraise:
  168. llvalue = v.value
  169. for link in block.exits:
  170. if link.llexitcase == llvalue:
  171. break
  172. else:
  173. assert link.exitcase == 'default'
  174. block.exitswitch = None
  175. link.exitcase = link.llexitcase = None
  176. block.recloseblock(link)
  177. def optimize_goto_if_not(self, block):
  178. """Replace code like 'v = int_gt(x,y); exitswitch = v'
  179. with just 'exitswitch = ('int_gt',x,y)'."""
  180. if len(block.exits) != 2:
  181. return False
  182. v = block.exitswitch
  183. if (block.canraise or isinstance(v, tuple)
  184. or v.concretetype != lltype.Bool):
  185. return False
  186. for op in block.operations[::-1]:
  187. if v in op.args:
  188. return False # variable is also used in cur block
  189. if v is op.result:
  190. if op.opname not in ('int_lt', 'int_le', 'int_eq', 'int_ne',
  191. 'int_gt', 'int_ge',
  192. 'float_lt', 'float_le', 'float_eq',
  193. 'float_ne', 'float_gt', 'float_ge',
  194. 'int_is_zero', 'int_is_true',
  195. 'ptr_eq', 'ptr_ne',
  196. 'ptr_iszero', 'ptr_nonzero'):
  197. return False # not a supported operation
  198. # ok! optimize this case
  199. block.operations.remove(op)
  200. block.exitswitch = (op.opname,) + tuple(op.args)
  201. #if op.opname in ('ptr_iszero', 'ptr_nonzero'):
  202. block.exitswitch += ('-live-before',)
  203. # if the variable escape to the next block along a link,
  204. # replace it with a constant, because we know its value
  205. for link in block.exits:
  206. while v in link.args:
  207. index = link.args.index(v)
  208. link.args[index] = Constant(link.llexitcase,
  209. lltype.Bool)
  210. return True
  211. return False
  212. # ----------
  213. def rewrite_operation(self, op):
  214. try:
  215. rewrite = _rewrite_ops[op.opname]
  216. except KeyError:
  217. raise Exception("the JIT doesn't support the operation %r"
  218. " in %r" % (op, getattr(self, 'graph', '?')))
  219. return rewrite(self, op)
  220. def rewrite_op_same_as(self, op):
  221. if op.args[0] in self.vable_array_vars:
  222. self.vable_array_vars[op.result]= self.vable_array_vars[op.args[0]]
  223. def rewrite_op_cast_ptr_to_adr(self, op):
  224. if lltype_is_gc(op.args[0].concretetype):
  225. raise Exception("cast_ptr_to_adr for GC types unsupported")
  226. def rewrite_op_cast_pointer(self, op):
  227. newop = self.rewrite_op_same_as(op)
  228. assert newop is None
  229. return
  230. # disabled for now
  231. if (self._is_rclass_instance(op.args[0]) and
  232. self._is_rclass_instance(op.result)):
  233. FROM = op.args[0].concretetype.TO
  234. TO = op.result.concretetype.TO
  235. if lltype._castdepth(TO, FROM) > 0:
  236. vtable = heaptracker.get_vtable_for_gcstruct(self.cpu, TO)
  237. if vtable.subclassrange_max - vtable.subclassrange_min == 1:
  238. # it's a precise class check
  239. const_vtable = Constant(vtable, lltype.typeOf(vtable))
  240. return [None, # hack, do the right renaming from op.args[0] to op.result
  241. SpaceOperation("record_exact_class", [op.args[0], const_vtable], None)]
  242. def rewrite_op_likely(self, op):
  243. return None # "no real effect"
  244. def rewrite_op_unlikely(self, op):
  245. return None # "no real effect"
  246. def rewrite_op_raw_malloc_usage(self, op):
  247. if self.cpu.translate_support_code or isinstance(op.args[0], Variable):
  248. return # the operation disappears
  249. else:
  250. # only for untranslated tests: get a real integer estimate
  251. arg = op.args[0].value
  252. arg = llmemory.raw_malloc_usage(arg)
  253. return [Constant(arg, lltype.Signed)]
  254. def rewrite_op_jit_record_exact_class(self, op):
  255. return SpaceOperation("record_exact_class", [op.args[0], op.args[1]], None)
  256. def rewrite_op_cast_bool_to_int(self, op): pass
  257. def rewrite_op_cast_bool_to_uint(self, op): pass
  258. def rewrite_op_cast_char_to_int(self, op): pass
  259. def rewrite_op_cast_unichar_to_int(self, op): pass
  260. def rewrite_op_cast_int_to_char(self, op): pass
  261. def rewrite_op_cast_int_to_unichar(self, op): pass
  262. def rewrite_op_cast_int_to_uint(self, op): pass
  263. def rewrite_op_cast_uint_to_int(self, op): pass
  264. def _rewrite_symmetric(self, op):
  265. """Rewrite 'c1+v2' into 'v2+c1' in an attempt to avoid generating
  266. too many variants of the bytecode."""
  267. if (isinstance(op.args[0], Constant) and
  268. isinstance(op.args[1], Variable)):
  269. reversename = {'int_lt': 'int_gt',
  270. 'int_le': 'int_ge',
  271. 'int_gt': 'int_lt',
  272. 'int_ge': 'int_le',
  273. 'uint_lt': 'uint_gt',
  274. 'uint_le': 'uint_ge',
  275. 'uint_gt': 'uint_lt',
  276. 'uint_ge': 'uint_le',
  277. 'float_lt': 'float_gt',
  278. 'float_le': 'float_ge',
  279. 'float_gt': 'float_lt',
  280. 'float_ge': 'float_le',
  281. }.get(op.opname, op.opname)
  282. return SpaceOperation(reversename,
  283. [op.args[1], op.args[0]] + op.args[2:],
  284. op.result)
  285. else:
  286. return op
  287. rewrite_op_int_add = _rewrite_symmetric
  288. rewrite_op_int_mul = _rewrite_symmetric
  289. rewrite_op_int_and = _rewrite_symmetric
  290. rewrite_op_int_or = _rewrite_symmetric
  291. rewrite_op_int_xor = _rewrite_symmetric
  292. rewrite_op_int_lt = _rewrite_symmetric
  293. rewrite_op_int_le = _rewrite_symmetric
  294. rewrite_op_int_gt = _rewrite_symmetric
  295. rewrite_op_int_ge = _rewrite_symmetric
  296. rewrite_op_uint_lt = _rewrite_symmetric
  297. rewrite_op_uint_le = _rewrite_symmetric
  298. rewrite_op_uint_gt = _rewrite_symmetric
  299. rewrite_op_uint_ge = _rewrite_symmetric
  300. rewrite_op_float_add = _rewrite_symmetric
  301. rewrite_op_float_mul = _rewrite_symmetric
  302. rewrite_op_float_lt = _rewrite_symmetric
  303. rewrite_op_float_le = _rewrite_symmetric
  304. rewrite_op_float_gt = _rewrite_symmetric
  305. rewrite_op_float_ge = _rewrite_symmetric
  306. def rewrite_op_int_add_ovf(self, op):
  307. op0 = self._rewrite_symmetric(op)
  308. op1 = SpaceOperation('-live-', [], None)
  309. return [op1, op0]
  310. rewrite_op_int_mul_ovf = rewrite_op_int_add_ovf
  311. def rewrite_op_int_sub_ovf(self, op):
  312. op1 = SpaceOperation('-live-', [], None)
  313. return [op1, op]
  314. def _noop_rewrite(self, op):
  315. return op
  316. rewrite_op_convert_float_bytes_to_longlong = _noop_rewrite
  317. rewrite_op_convert_longlong_bytes_to_float = _noop_rewrite
  318. cast_ptr_to_weakrefptr = _noop_rewrite
  319. cast_weakrefptr_to_ptr = _noop_rewrite
  320. # ----------
  321. # Various kinds of calls
  322. def rewrite_op_direct_call(self, op):
  323. kind = self.callcontrol.guess_call_kind(op)
  324. return getattr(self, 'handle_%s_call' % kind)(op)
  325. def rewrite_op_indirect_call(self, op):
  326. kind = self.callcontrol.guess_call_kind(op)
  327. return getattr(self, 'handle_%s_indirect_call' % kind)(op)
  328. def rewrite_call(self, op, namebase, initialargs, args=None,
  329. calldescr=None):
  330. """Turn 'i0 = direct_call(fn, i1, i2, ref1, ref2)'
  331. into 'i0 = xxx_call_ir_i(fn, descr, [i1,i2], [ref1,ref2])'.
  332. The name is one of '{residual,direct}_call_{r,ir,irf}_{i,r,f,v}'."""
  333. if args is None:
  334. args = op.args[1:]
  335. self._check_no_vable_array(args)
  336. lst_i, lst_r, lst_f = self.make_three_lists(args)
  337. reskind = getkind(op.result.concretetype)[0]
  338. if lst_f or reskind == 'f': kinds = 'irf'
  339. elif lst_i: kinds = 'ir'
  340. else: kinds = 'r'
  341. sublists = []
  342. if 'i' in kinds: sublists.append(lst_i)
  343. if 'r' in kinds: sublists.append(lst_r)
  344. if 'f' in kinds: sublists.append(lst_f)
  345. if calldescr is not None:
  346. sublists.append(calldescr)
  347. return SpaceOperation('%s_%s_%s' % (namebase, kinds, reskind),
  348. initialargs + sublists, op.result)
  349. def make_three_lists(self, vars):
  350. args_i = []
  351. args_r = []
  352. args_f = []
  353. for v in vars:
  354. self.add_in_correct_list(v, args_i, args_r, args_f)
  355. return [ListOfKind('int', args_i),
  356. ListOfKind('ref', args_r),
  357. ListOfKind('float', args_f)]
  358. def add_in_correct_list(self, v, lst_i, lst_r, lst_f):
  359. kind = getkind(v.concretetype)
  360. if kind == 'void': return
  361. elif kind == 'int': lst = lst_i
  362. elif kind == 'ref': lst = lst_r
  363. elif kind == 'float': lst = lst_f
  364. else: raise AssertionError(kind)
  365. lst.append(v)
  366. def handle_residual_call(self, op, extraargs=[], may_call_jitcodes=False,
  367. oopspecindex=EffectInfo.OS_NONE,
  368. extraeffect=None,
  369. extradescr=None):
  370. """A direct_call turns into the operation 'residual_call_xxx' if it
  371. is calling a function that we don't want to JIT. The initial args
  372. of 'residual_call_xxx' are the function to call, and its calldescr."""
  373. calldescr = self.callcontrol.getcalldescr(op, oopspecindex=oopspecindex,
  374. extraeffect=extraeffect,
  375. extradescr=extradescr)
  376. op1 = self.rewrite_call(op, 'residual_call',
  377. [op.args[0]] + extraargs, calldescr=calldescr)
  378. if may_call_jitcodes or self.callcontrol.calldescr_canraise(calldescr):
  379. op1 = [op1, SpaceOperation('-live-', [], None)]
  380. return op1
  381. def handle_regular_call(self, op):
  382. """A direct_call turns into the operation 'inline_call_xxx' if it
  383. is calling a function that we want to JIT. The initial arg of
  384. 'inline_call_xxx' is the JitCode of the called function."""
  385. [targetgraph] = self.callcontrol.graphs_from(op)
  386. jitcode = self.callcontrol.get_jitcode(targetgraph,
  387. called_from=self.graph)
  388. op0 = self.rewrite_call(op, 'inline_call', [jitcode])
  389. op1 = SpaceOperation('-live-', [], None)
  390. return [op0, op1]
  391. def handle_builtin_call(self, op):
  392. oopspec_name, args = support.decode_builtin_call(op)
  393. # dispatch to various implementations depending on the oopspec_name
  394. if oopspec_name.startswith('list.') or oopspec_name.startswith('newlist'):
  395. prepare = self._handle_list_call
  396. elif oopspec_name.startswith('int.'):
  397. prepare = self._handle_int_special
  398. elif oopspec_name.startswith('stroruni.'):
  399. prepare = self._handle_stroruni_call
  400. elif oopspec_name == 'str.str2unicode':
  401. prepare = self._handle_str2unicode_call
  402. elif oopspec_name.startswith('virtual_ref'):
  403. prepare = self._handle_virtual_ref_call
  404. elif oopspec_name.startswith('jit.'):
  405. prepare = self._handle_jit_call
  406. elif oopspec_name.startswith('libffi_'):
  407. prepare = self._handle_libffi_call
  408. elif oopspec_name.startswith('math.sqrt'):
  409. prepare = self._handle_math_sqrt_call
  410. elif oopspec_name.startswith('rgc.'):
  411. prepare = self._handle_rgc_call
  412. elif oopspec_name.startswith('rvmprof.'):
  413. prepare = self._handle_rvmprof_call
  414. elif oopspec_name.endswith('dict.lookup'):
  415. # also ordereddict.lookup
  416. prepare = self._handle_dict_lookup_call
  417. else:
  418. prepare = self.prepare_builtin_call
  419. try:
  420. op1 = prepare(op, oopspec_name, args)
  421. except NotSupported:
  422. op1 = op
  423. # If the resulting op1 is still a direct_call, turn it into a
  424. # residual_call.
  425. if isinstance(op1, SpaceOperation) and op1.opname == 'direct_call':
  426. op1 = self.handle_residual_call(op1)
  427. return op1
  428. def handle_recursive_call(self, op):
  429. jitdriver_sd = self.callcontrol.jitdriver_sd_from_portal_runner_ptr(
  430. op.args[0].value)
  431. assert jitdriver_sd is not None
  432. ops = self.promote_greens(op.args[1:], jitdriver_sd.jitdriver)
  433. num_green_args = len(jitdriver_sd.jitdriver.greens)
  434. args = ([Constant(jitdriver_sd.index, lltype.Signed)] +
  435. self.make_three_lists(op.args[1:1+num_green_args]) +
  436. self.make_three_lists(op.args[1+num_green_args:]))
  437. kind = getkind(op.result.concretetype)[0]
  438. op0 = SpaceOperation('recursive_call_%s' % kind, args, op.result)
  439. op1 = SpaceOperation('-live-', [], None)
  440. return ops + [op0, op1]
  441. handle_residual_indirect_call = handle_residual_call
  442. def handle_regular_indirect_call(self, op):
  443. """An indirect call where at least one target has a JitCode."""
  444. lst = []
  445. for targetgraph in self.callcontrol.graphs_from(op):
  446. jitcode = self.callcontrol.get_jitcode(targetgraph,
  447. called_from=self.graph)
  448. lst.append(jitcode)
  449. op0 = SpaceOperation('-live-', [], None)
  450. op1 = SpaceOperation('int_guard_value', [op.args[0]], None)
  451. op2 = self.handle_residual_call(op, [IndirectCallTargets(lst)], True)
  452. result = [op0, op1]
  453. if isinstance(op2, list):
  454. result += op2
  455. else:
  456. result.append(op2)
  457. return result
  458. def prepare_builtin_call(self, op, oopspec_name, args,
  459. extra=None, extrakey=None):
  460. argtypes = [v.concretetype for v in args]
  461. resulttype = op.result.concretetype
  462. c_func, TP = support.builtin_func_for_spec(self.cpu.rtyper,
  463. oopspec_name, argtypes,
  464. resulttype, extra, extrakey)
  465. return SpaceOperation('direct_call', [c_func] + args, op.result)
  466. def _do_builtin_call(self, op, oopspec_name=None, args=None,
  467. extra=None, extrakey=None):
  468. if oopspec_name is None: oopspec_name = op.opname
  469. if args is None: args = op.args
  470. op1 = self.prepare_builtin_call(op, oopspec_name, args,
  471. extra, extrakey)
  472. return self.rewrite_op_direct_call(op1)
  473. # XXX some of the following functions should not become residual calls
  474. # but be really compiled
  475. rewrite_op_int_abs = _do_builtin_call
  476. rewrite_op_int_floordiv = _do_builtin_call
  477. rewrite_op_int_mod = _do_builtin_call
  478. rewrite_op_llong_abs = _do_builtin_call
  479. rewrite_op_llong_floordiv = _do_builtin_call
  480. rewrite_op_llong_mod = _do_builtin_call
  481. rewrite_op_ullong_floordiv = _do_builtin_call
  482. rewrite_op_ullong_mod = _do_builtin_call
  483. rewrite_op_gc_identityhash = _do_builtin_call
  484. rewrite_op_gc_id = _do_builtin_call
  485. rewrite_op_gc_pin = _do_builtin_call
  486. rewrite_op_gc_unpin = _do_builtin_call
  487. rewrite_op_cast_float_to_uint = _do_builtin_call
  488. rewrite_op_cast_uint_to_float = _do_builtin_call
  489. rewrite_op_weakref_create = _do_builtin_call
  490. rewrite_op_weakref_deref = _do_builtin_call
  491. rewrite_op_gc_add_memory_pressure = _do_builtin_call
  492. # ----------
  493. # getfield/setfield/mallocs etc.
  494. def rewrite_op_hint(self, op):
  495. hints = op.args[1].value
  496. # hack: if there are both 'promote' and 'promote_string', kill
  497. # one of them based on the type of the value
  498. if hints.get('promote_string') and hints.get('promote'):
  499. hints = hints.copy()
  500. if op.args[0].concretetype == lltype.Ptr(rstr.STR):
  501. del hints['promote']
  502. else:
  503. del hints['promote_string']
  504. if hints.get('promote') and op.args[0].concretetype is not lltype.Void:
  505. assert op.args[0].concretetype != lltype.Ptr(rstr.STR)
  506. kind = getkind(op.args[0].concretetype)
  507. op0 = SpaceOperation('-live-', [], None)
  508. op1 = SpaceOperation('%s_guard_value' % kind, [op.args[0]], None)
  509. # the special return value None forces op.result to be considered
  510. # equal to op.args[0]
  511. return [op0, op1, None]
  512. if (hints.get('promote_string') and
  513. op.args[0].concretetype is not lltype.Void):
  514. S = lltype.Ptr(rstr.STR)
  515. assert op.args[0].concretetype == S
  516. self._register_extra_helper(EffectInfo.OS_STREQ_NONNULL,
  517. "str.eq_nonnull",
  518. [S, S],
  519. lltype.Signed,
  520. EffectInfo.EF_ELIDABLE_CANNOT_RAISE)
  521. descr, p = self.callcontrol.callinfocollection.callinfo_for_oopspec(
  522. EffectInfo.OS_STREQ_NONNULL)
  523. # XXX this is fairly ugly way of creating a constant,
  524. # however, callinfocollection has no better interface
  525. c = Constant(p.adr.ptr, lltype.typeOf(p.adr.ptr))
  526. op1 = SpaceOperation('str_guard_value', [op.args[0], c, descr],
  527. op.result)
  528. return [SpaceOperation('-live-', [], None), op1, None]
  529. if hints.get('force_virtualizable'):
  530. return SpaceOperation('hint_force_virtualizable', [op.args[0]], None)
  531. if hints.get('force_no_const'): # for tests only
  532. assert getkind(op.args[0].concretetype) == 'int'
  533. return SpaceOperation('int_same_as', [op.args[0]], op.result)
  534. log.WARNING('ignoring hint %r at %r' % (hints, self.graph))
  535. def _rewrite_raw_malloc(self, op, name, args):
  536. d = op.args[1].value.copy()
  537. d.pop('flavor')
  538. add_memory_pressure = d.pop('add_memory_pressure', False)
  539. zero = d.pop('zero', False)
  540. track_allocation = d.pop('track_allocation', True)
  541. if d:
  542. raise UnsupportedMallocFlags(d)
  543. if zero:
  544. name += '_zero'
  545. if add_memory_pressure:
  546. name += '_add_memory_pressure'
  547. if not track_allocation:
  548. name += '_no_track_allocation'
  549. TYPE = op.args[0].value
  550. op1 = self.prepare_builtin_call(op, name, args, (TYPE,), TYPE)
  551. if name.startswith('raw_malloc_varsize') and TYPE.OF == lltype.Char:
  552. return self._handle_oopspec_call(op1, args,
  553. EffectInfo.OS_RAW_MALLOC_VARSIZE_CHAR,
  554. EffectInfo.EF_CAN_RAISE)
  555. return self.rewrite_op_direct_call(op1)
  556. def rewrite_op_malloc_varsize(self, op):
  557. if op.args[1].value['flavor'] == 'raw':
  558. return self._rewrite_raw_malloc(op, 'raw_malloc_varsize',
  559. [op.args[2]])
  560. if op.args[0].value == rstr.STR:
  561. return SpaceOperation('newstr', [op.args[2]], op.result)
  562. elif op.args[0].value == rstr.UNICODE:
  563. return SpaceOperation('newunicode', [op.args[2]], op.result)
  564. else:
  565. # XXX only strings or simple arrays for now
  566. ARRAY = op.args[0].value
  567. arraydescr = self.cpu.arraydescrof(ARRAY)
  568. if op.args[1].value.get('zero', False):
  569. opname = 'new_array_clear'
  570. elif ((isinstance(ARRAY.OF, lltype.Ptr) and ARRAY.OF._needsgc()) or
  571. isinstance(ARRAY.OF, lltype.Struct)):
  572. opname = 'new_array_clear'
  573. else:
  574. opname = 'new_array'
  575. return SpaceOperation(opname, [op.args[2], arraydescr], op.result)
  576. def zero_contents(self, ops, v, TYPE):
  577. if isinstance(TYPE, lltype.Struct):
  578. for name, FIELD in TYPE._flds.iteritems():
  579. if isinstance(FIELD, lltype.Struct):
  580. # substruct
  581. self.zero_contents(ops, v, FIELD)
  582. else:
  583. c_name = Constant(name, lltype.Void)
  584. c_null = Constant(FIELD._defl(), FIELD)
  585. op = SpaceOperation('setfield', [v, c_name, c_null],
  586. None)
  587. self.extend_with(ops, self.rewrite_op_setfield(op,
  588. override_type=TYPE))
  589. elif isinstance(TYPE, lltype.Array):
  590. assert False # this operation disappeared
  591. else:
  592. raise TypeError("Expected struct or array, got '%r'", (TYPE,))
  593. if len(ops) == 1:
  594. return ops[0]
  595. return ops
  596. def extend_with(self, l, ops):
  597. if ops is None:
  598. return
  599. if isinstance(ops, list):
  600. l.extend(ops)
  601. else:
  602. l.append(ops)
  603. def rewrite_op_free(self, op):
  604. d = op.args[1].value.copy()
  605. assert d['flavor'] == 'raw'
  606. d.pop('flavor')
  607. track_allocation = d.pop('track_allocation', True)
  608. if d:
  609. raise UnsupportedMallocFlags(d)
  610. STRUCT = op.args[0].concretetype.TO
  611. name = 'raw_free'
  612. if not track_allocation:
  613. name += '_no_track_allocation'
  614. op1 = self.prepare_builtin_call(op, name, [op.args[0]], (STRUCT,),
  615. STRUCT)
  616. if name.startswith('raw_free'):
  617. return self._handle_oopspec_call(op1, [op.args[0]],
  618. EffectInfo.OS_RAW_FREE,
  619. EffectInfo.EF_CANNOT_RAISE)
  620. return self.rewrite_op_direct_call(op1)
  621. def rewrite_op_getarrayitem(self, op):
  622. ARRAY = op.args[0].concretetype.TO
  623. if self._array_of_voids(ARRAY):
  624. return []
  625. if isinstance(ARRAY, lltype.FixedSizeArray):
  626. raise NotImplementedError(
  627. "%r uses %r, which is not supported by the JIT codewriter"
  628. % (self.graph, ARRAY))
  629. if op.args[0] in self.vable_array_vars: # for virtualizables
  630. vars = self.vable_array_vars[op.args[0]]
  631. (v_base, arrayfielddescr, arraydescr) = vars
  632. kind = getkind(op.result.concretetype)
  633. return [SpaceOperation('-live-', [], None),
  634. SpaceOperation('getarrayitem_vable_%s' % kind[0],
  635. [v_base, op.args[1], arrayfielddescr,
  636. arraydescr], op.result)]
  637. # normal case follows
  638. pure = ''
  639. immut = ARRAY._immutable_field(None)
  640. if immut:
  641. pure = '_pure'
  642. arraydescr = self.cpu.arraydescrof(ARRAY)
  643. kind = getkind(op.result.concretetype)
  644. if ARRAY._gckind != 'gc':
  645. assert ARRAY._gckind == 'raw'
  646. if kind == 'r':
  647. raise Exception("getarrayitem_raw_r not supported")
  648. pure = '' # always redetected from pyjitpl.py: we don't need
  649. # a '_pure' version of getarrayitem_raw
  650. return SpaceOperation('getarrayitem_%s_%s%s' % (ARRAY._gckind,
  651. kind[0], pure),
  652. [op.args[0], op.args[1], arraydescr],
  653. op.result)
  654. def rewrite_op_setarrayitem(self, op):
  655. ARRAY = op.args[0].concretetype.TO
  656. if self._array_of_voids(ARRAY):
  657. return []
  658. if isinstance(ARRAY, lltype.FixedSizeArray):
  659. raise NotImplementedError(
  660. "%r uses %r, which is not supported by the JIT codewriter"
  661. % (self.graph, ARRAY))
  662. if op.args[0] in self.vable_array_vars: # for virtualizables
  663. vars = self.vable_array_vars[op.args[0]]
  664. (v_base, arrayfielddescr, arraydescr) = vars
  665. kind = getkind(op.args[2].concretetype)
  666. return [SpaceOperation('-live-', [], None),
  667. SpaceOperation('setarrayitem_vable_%s' % kind[0],
  668. [v_base, op.args[1], op.args[2],
  669. arrayfielddescr, arraydescr], None)]
  670. arraydescr = self.cpu.arraydescrof(ARRAY)
  671. kind = getkind(op.args[2].concretetype)
  672. return SpaceOperation('setarrayitem_%s_%s' % (ARRAY._gckind, kind[0]),
  673. [op.args[0], op.args[1], op.args[2], arraydescr],
  674. None)
  675. def rewrite_op_getarraysize(self, op):
  676. ARRAY = op.args[0].concretetype.TO
  677. assert ARRAY._gckind == 'gc'
  678. if op.args[0] in self.vable_array_vars: # for virtualizables
  679. vars = self.vable_array_vars[op.args[0]]
  680. (v_base, arrayfielddescr, arraydescr) = vars
  681. return [SpaceOperation('-live-', [], None),
  682. SpaceOperation('arraylen_vable',
  683. [v_base, arrayfielddescr, arraydescr],
  684. op.result)]
  685. # normal case follows
  686. arraydescr = self.cpu.arraydescrof(ARRAY)
  687. return SpaceOperation('arraylen_gc', [op.args[0], arraydescr],
  688. op.result)
  689. def rewrite_op_getarraysubstruct(self, op):
  690. ARRAY = op.args[0].concretetype.TO
  691. assert ARRAY._gckind == 'raw'
  692. assert ARRAY._hints.get('nolength') is True
  693. return self.rewrite_op_direct_ptradd(op)
  694. def _array_of_voids(self, ARRAY):
  695. return ARRAY.OF == lltype.Void
  696. def rewrite_op_getfield(self, op):
  697. if self.is_typeptr_getset(op):
  698. return self.handle_getfield_typeptr(op)
  699. # turn the flow graph 'getfield' operation into our own version
  700. [v_inst, c_fieldname] = op.args
  701. RESULT = op.result.concretetype
  702. if RESULT is lltype.Void:
  703. return
  704. # check for virtualizable
  705. try:
  706. if self.is_virtualizable_getset(op):
  707. descr = self.get_virtualizable_field_descr(op)
  708. kind = getkind(RESULT)[0]
  709. return [SpaceOperation('-live-', [], None),
  710. SpaceOperation('getfield_vable_%s' % kind,
  711. [v_inst, descr], op.result)]
  712. except VirtualizableArrayField as e:
  713. # xxx hack hack hack
  714. vinfo = e.args[1]
  715. arrayindex = vinfo.array_field_counter[op.args[1].value]
  716. arrayfielddescr = vinfo.array_field_descrs[arrayindex]
  717. arraydescr = vinfo.array_descrs[arrayindex]
  718. self.vable_array_vars[op.result] = (op.args[0],
  719. arrayfielddescr,
  720. arraydescr)
  721. return []
  722. # check for _immutable_fields_ hints
  723. immut = v_inst.concretetype.TO._immutable_field(c_fieldname.value)
  724. need_live = False
  725. if immut:
  726. if (self.callcontrol is not None and
  727. self.callcontrol.could_be_green_field(v_inst.concretetype.TO,
  728. c_fieldname.value)):
  729. pure = '_greenfield'
  730. need_live = True
  731. else:
  732. pure = '_pure'
  733. else:
  734. pure = ''
  735. self.check_field_access(v_inst.concretetype.TO)
  736. argname = getattr(v_inst.concretetype.TO, '_gckind', 'gc')
  737. descr = self.cpu.fielddescrof(v_inst.concretetype.TO,
  738. c_fieldname.value)
  739. kind = getkind(RESULT)[0]
  740. if argname != 'gc':
  741. assert argname == 'raw'
  742. if (kind, pure) == ('r', ''):
  743. # note: a pure 'getfield_raw_r' is used e.g. to load class
  744. # attributes that are GC objects, so that one is supported.
  745. raise Exception("getfield_raw_r (without _pure) not supported")
  746. pure = '' # always redetected from pyjitpl.py: we don't need
  747. # a '_pure' version of getfield_raw
  748. #
  749. op1 = SpaceOperation('getfield_%s_%s%s' % (argname, kind, pure),
  750. [v_inst, descr], op.result)
  751. #
  752. if immut in (IR_QUASIIMMUTABLE, IR_QUASIIMMUTABLE_ARRAY):
  753. op1.opname += "_pure"
  754. descr1 = self.cpu.fielddescrof(
  755. v_inst.concretetype.TO,
  756. quasiimmut.get_mutate_field_name(c_fieldname.value))
  757. return [SpaceOperation('-live-', [], None),
  758. SpaceOperation('record_quasiimmut_field',
  759. [v_inst, descr, descr1], None),
  760. op1]
  761. if need_live:
  762. return [SpaceOperation('-live-', [], None), op1]
  763. return op1
  764. def rewrite_op_setfield(self, op, override_type=None):
  765. if self.is_typeptr_getset(op):
  766. # ignore the operation completely -- instead, it's done by 'new'
  767. return
  768. self._check_no_vable_array(op.args)
  769. # turn the flow graph 'setfield' operation into our own version
  770. [v_inst, c_fieldname, v_value] = op.args
  771. RESULT = v_value.concretetype
  772. if override_type is not None:
  773. TYPE = override_type
  774. else:
  775. TYPE = v_inst.concretetype.TO
  776. if RESULT is lltype.Void:
  777. return
  778. # check for virtualizable
  779. if self.is_virtualizable_getset(op):
  780. descr = self.get_virtualizable_field_descr(op)
  781. kind = getkind(RESULT)[0]
  782. return [SpaceOperation('-live-', [], None),
  783. SpaceOperation('setfield_vable_%s' % kind,
  784. [v_inst, v_value, descr], None)]
  785. self.check_field_access(TYPE)
  786. if override_type:
  787. argname = 'gc'
  788. else:
  789. argname = getattr(TYPE, '_gckind', 'gc')
  790. descr = self.cpu.fielddescrof(TYPE, c_fieldname.value)
  791. kind = getkind(RESULT)[0]
  792. if argname == 'raw' and kind == 'r':
  793. raise Exception("setfield_raw_r not supported")
  794. return SpaceOperation('setfield_%s_%s' % (argname, kind),
  795. [v_inst, v_value, descr],
  796. None)
  797. def rewrite_op_getsubstruct(self, op):
  798. STRUCT = op.args[0].concretetype.TO
  799. argname = getattr(STRUCT, '_gckind', 'gc')
  800. if argname != 'raw':
  801. raise Exception("%r: only supported for gckind=raw" % (op,))
  802. ofs = llmemory.offsetof(STRUCT, op.args[1].value)
  803. return SpaceOperation('int_add',
  804. [op.args[0], Constant(ofs, lltype.Signed)],
  805. op.result)
  806. def is_typeptr_getset(self, op):
  807. return (op.args[1].value == 'typeptr' and
  808. op.args[0].concretetype.TO._hints.get('typeptr'))
  809. def check_field_access(self, STRUCT):
  810. # check against a GcStruct with a nested GcStruct as a first argument
  811. # but which is not an object at all; see metainterp/test/test_loop,
  812. # test_regular_pointers_in_short_preamble.
  813. if not isinstance(STRUCT, lltype.GcStruct):
  814. return
  815. if STRUCT._first_struct() == (None, None):
  816. return
  817. PARENT = STRUCT
  818. while not PARENT._hints.get('typeptr'):
  819. _, PARENT = PARENT._first_struct()
  820. if PARENT is None:
  821. raise NotImplementedError("%r is a GcStruct using nesting but "
  822. "not inheriting from object" %
  823. (STRUCT,))
  824. def get_vinfo(self, v_virtualizable):
  825. if self.callcontrol is None: # for tests
  826. return None
  827. return self.callcontrol.get_vinfo(v_virtualizable.concretetype)
  828. def is_virtualizable_getset(self, op):
  829. # every access of an object of exactly the type VTYPEPTR is
  830. # likely to be a virtualizable access, but we still have to
  831. # check it in pyjitpl.py.
  832. vinfo = self.get_vinfo(op.args[0])
  833. if vinfo is None:
  834. return False
  835. res = False
  836. if op.args[1].value in vinfo.static_field_to_extra_box:
  837. res = True
  838. if op.args[1].value in vinfo.array_fields:
  839. res = VirtualizableArrayField(self.graph, vinfo)
  840. if res:
  841. flags = self.vable_flags[op.args[0]]
  842. if 'fresh_virtualizable' in flags:
  843. return False
  844. if isinstance(res, Exception):
  845. raise res
  846. return res
  847. def get_virtualizable_field_descr(self, op):
  848. fieldname = op.args[1].value
  849. vinfo = self.get_vinfo(op.args[0])
  850. index = vinfo.static_field_to_extra_box[fieldname]
  851. return vinfo.static_field_descrs[index]
  852. def handle_getfield_typeptr(self, op):
  853. if isinstance(op.args[0], Constant):
  854. cls = op.args[0].value.typeptr
  855. return Constant(cls, concretetype=rclass.CLASSTYPE)
  856. op0 = SpaceOperation('-live-', [], None)
  857. op1 = SpaceOperation('guard_class', [op.args[0]], op.result)
  858. return [op0, op1]
  859. def rewrite_op_malloc(self, op):
  860. d = op.args[1].value
  861. if d.get('nonmovable', False):
  862. raise UnsupportedMallocFlags(d)
  863. if d['flavor'] == 'raw':
  864. return self._rewrite_raw_malloc(op, 'raw_malloc_fixedsize', [])
  865. #
  866. if d.get('zero', False):
  867. zero = True
  868. else:
  869. zero = False
  870. STRUCT = op.args[0].value
  871. vtable = heaptracker.get_vtable_for_gcstruct(self.cpu, STRUCT)
  872. if vtable:
  873. # do we have a __del__?
  874. try:
  875. rtti = lltype.getRuntimeTypeInfo(STRUCT)
  876. except ValueError:
  877. pass
  878. else:
  879. if hasattr(rtti._obj, 'destructor_funcptr'):
  880. RESULT = lltype.Ptr(STRUCT)
  881. assert RESULT == op.result.concretetype
  882. return self._do_builtin_call(op, 'alloc_with_del', [],
  883. extra=(RESULT, vtable),
  884. extrakey=STRUCT)
  885. opname = 'new_with_vtable'
  886. else:
  887. opname = 'new'
  888. vtable = lltype.nullptr(rclass.OBJECT_VTABLE)
  889. sizedescr = self.cpu.sizeof(STRUCT, vtable)
  890. op1 = SpaceOperation(opname, [sizedescr], op.result)
  891. if zero:
  892. return self.zero_contents([op1], op.result, STRUCT)
  893. return op1
  894. def _has_gcptrs_in(self, STRUCT):
  895. if isinstance(STRUCT, lltype.Array):
  896. ITEM = STRUCT.OF
  897. if isinstance(ITEM, lltype.Struct):
  898. STRUCT = ITEM
  899. else:
  900. return isinstance(ITEM, lltype.Ptr) and ITEM._needsgc()
  901. for FIELD in STRUCT._flds.values():
  902. if isinstance(FIELD, lltype.Ptr) and FIELD._needsgc():
  903. return True
  904. elif isinstance(FIELD, lltype.Struct):
  905. if self._has_gcptrs_in(FIELD):
  906. return True
  907. return False
  908. def rewrite_op_getinteriorarraysize(self, op):
  909. # only supports strings and unicodes
  910. assert len(op.args) == 2
  911. assert op.args[1].value == 'chars'
  912. optype = op.args[0].concretetype
  913. if optype == lltype.Ptr(rstr.STR):
  914. opname = "strlen"
  915. elif optype == lltype.Ptr(rstr.UNICODE):
  916. opname = "unicodelen"
  917. elif optype == lltype.Ptr(rbytearray.BYTEARRAY):
  918. bytearraydescr = self.cpu.arraydescrof(rbytearray.BYTEARRAY)
  919. return SpaceOperation('arraylen_gc', [op.args[0], bytearraydescr],
  920. op.result)
  921. else:
  922. assert 0, "supported type %r" % (optype,)
  923. return SpaceOperation(opname, [op.args[0]], op.result)
  924. def rewrite_op_getinteriorfield(self, op):
  925. assert len(op.args) == 3
  926. optype = op.args[0].concretetype
  927. if optype == lltype.Ptr(rstr.STR):
  928. opname = "strgetitem"
  929. return SpaceOperation(opname, [op.args[0], op.args[2]], op.result)
  930. elif optype == lltype.Ptr(rstr.UNICODE):
  931. opname = "unicodegetitem"
  932. return SpaceOperation(opname, [op.args[0], op.args[2]], op.result)
  933. elif optype == lltype.Ptr(rbytearray.BYTEARRAY):
  934. bytearraydescr = self.cpu.arraydescrof(rbytearray.BYTEARRAY)
  935. v_index = op.args[2]
  936. return SpaceOperation('getarrayitem_gc_i',
  937. [op.args[0], v_index, bytearraydescr],
  938. op.result)
  939. elif op.result.concretetype is lltype.Void:
  940. return
  941. elif isinstance(op.args[0].concretetype.TO, lltype.GcArray):
  942. # special-case 1: GcArray of Struct
  943. v_inst, v_index, c_field = op.args
  944. STRUCT = v_inst.concretetype.TO.OF
  945. assert isinstance(STRUCT, lltype.Struct)
  946. descr = self.cpu.interiorfielddescrof(v_inst.concretetype.TO,
  947. c_field.value)
  948. args = [v_inst, v_index, descr]
  949. kind = getkind(op.result.concretetype)[0]
  950. return SpaceOperation('getinteriorfield_gc_%s' % kind, args,
  951. op.result)
  952. #elif isinstance(op.args[0].concretetype.TO, lltype.GcStruct):
  953. # # special-case 2: GcStruct with Array field
  954. # ---was added in the faster-rstruct branch,---
  955. # ---no longer directly supported---
  956. # v_inst, c_field, v_index = op.args
  957. # STRUCT = v_inst.concretetype.TO
  958. # ARRAY = getattr(STRUCT, c_field.value)
  959. # assert isinstance(ARRAY, lltype.Array)
  960. # arraydescr = self.cpu.arraydescrof(STRUCT)
  961. # kind = getkind(op.result.concretetype)[0]
  962. # assert kind in ('i', 'f')
  963. # return SpaceOperation('getarrayitem_gc_%s' % kind,
  964. # [op.args[0], v_index, arraydescr],
  965. # op.result)
  966. else:
  967. assert False, 'not supported'
  968. def rewrite_op_setinteriorfield(self, op):
  969. assert len(op.args) == 4
  970. optype = op.args[0].concretetype
  971. if optype == lltype.Ptr(rstr.STR):
  972. opname = "strsetitem"
  973. return SpaceOperation(opname, [op.args[0], op.args[2], op.args[3]],
  974. op.result)
  975. elif optype == lltype.Ptr(rstr.UNICODE):
  976. opname = "unicodesetitem"
  977. return SpaceOperation(opname, [op.args[0], op.args[2], op.args[3]],
  978. op.result)
  979. elif optype == lltype.Ptr(rbytearray.BYTEARRAY):
  980. bytearraydescr = self.cpu.arraydescrof(rbytearray.BYTEARRAY)
  981. opname = "setarrayitem_gc_i"
  982. return SpaceOperation(opname, [op.args[0], op.args[2], op.args[3],
  983. bytearraydescr], op.result)
  984. else:
  985. v_inst, v_index, c_field, v_value = op.args
  986. if v_value.concretetype is lltype.Void:
  987. return
  988. # only GcArray of Struct supported
  989. assert isinstance(v_inst.concretetype.TO, lltype.GcArray)
  990. STRUCT = v_inst.concretetype.TO.OF
  991. assert isinstance(STRUCT, lltype.Struct)
  992. descr = self.cpu.interiorfielddescrof(v_inst.concretetype.TO,
  993. c_field.value)
  994. kind = getkind(v_value.concretetype)[0]
  995. args = [v_inst, v_index, v_value, descr]
  996. return SpaceOperation('setinteriorfield_gc_%s' % kind, args,
  997. op.result)
  998. def rewrite_op_raw_store(self, op):
  999. T = op.args[2].concretetype
  1000. kind = getkind(T)[0]
  1001. assert kind != 'r'
  1002. descr = self.cpu.arraydescrof(rffi.CArray(T))
  1003. return SpaceOperation('raw_store_%s' % kind,
  1004. [op.args[0], op.args[1], op.args[2], descr],
  1005. None)
  1006. def rewrite_op_raw_load(self, op):
  1007. T = op.result.concretetype
  1008. kind = getkind(T)[0]
  1009. assert kind != 'r'
  1010. descr = self.cpu.arraydescrof(rffi.CArray(T))
  1011. return SpaceOperation('raw_load_%s' % kind,
  1012. [op.args[0], op.args[1], descr], op.result)
  1013. def rewrite_op_gc_load_indexed(self, op):
  1014. T = op.result.concretetype
  1015. kind = getkind(T)[0]
  1016. assert kind != 'r'
  1017. descr = self.cpu.arraydescrof(rffi.CArray(T))
  1018. if (not isinstance(op.args[2], Constant) or
  1019. not isinstance(op.args[3], Constant)):
  1020. raise NotImplementedError("gc_load_indexed: 'scale' and 'base_ofs'"
  1021. " should be constants")
  1022. # xxx hard-code the size in bytes at translation time, which is
  1023. # probably fine and avoids lots of issues later
  1024. bytes = descr.get_item_size_in_bytes()
  1025. if descr.is_item_signed():
  1026. bytes = -bytes
  1027. c_bytes = Constant(bytes, lltype.Signed)
  1028. return SpaceOperation('gc_load_indexed_%s' % kind,
  1029. [op.args[0], op.args[1],
  1030. op.args[2], op.args[3], c_bytes], op.result)
  1031. def _rewrite_equality(self, op, opname):
  1032. arg0, arg1 = op.args
  1033. if isinstance(arg0, Constant) and not arg0.value:
  1034. return SpaceOperation(opname, [arg1], op.result)

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