Debian Codegraph @

rpython/jit/backend/ppc/opassembler.py

raw symbols SCIP
1 from rpython.jit.backend.ppc.helper.assembler import gen_emit_cmp_op
2 from rpython.jit.backend.ppc.helper.regalloc import _check_imm_arg
3 import rpython.jit.backend.ppc.condition as c
4 import rpython.jit.backend.ppc.register as r
5 from rpython.jit.backend.ppc.locations import imm
6 from rpython.jit.backend.ppc.locations import imm as make_imm_loc
7 from rpython.jit.backend.ppc.arch import (IS_PPC_32, IS_PPC_64, WORD,
8 MAX_REG_PARAMS, MAX_FREG_PARAMS,
9 PARAM_SAVE_AREA_OFFSET,
10 THREADLOCAL_ADDR_OFFSET,
11 IS_BIG_ENDIAN)
12
13 from rpython.jit.metainterp.history import (JitCellToken, TargetToken,
14 AbstractFailDescr, FLOAT, INT, REF,
15 ConstInt, VOID)
16 from rpython.rlib.objectmodel import we_are_translated
17 from rpython.jit.backend.ppc.helper.assembler import (Saved_Volatiles)
18 from rpython.jit.backend.ppc.jump import remap_frame_layout
19 from rpython.jit.backend.ppc.codebuilder import (OverwritingBuilder, scratch_reg,
20 PPCBuilder, PPCGuardToken)
21 from rpython.jit.backend.ppc.regalloc import TempPtr, TempInt
22 from rpython.jit.backend.llsupport import symbolic, jitframe
23 from rpython.jit.backend.llsupport.descr import CallDescr
24 from rpython.jit.backend.llsupport.gcmap import allocate_gcmap
25 from rpython.rtyper.lltypesystem import rstr, rffi, lltype
26 from rpython.rtyper.annlowlevel import cast_instance_to_gcref
27 from rpython.rtyper import rclass
28 from rpython.jit.metainterp.resoperation import rop
29 from rpython.jit.codewriter.effectinfo import EffectInfo
30 from rpython.jit.backend.ppc import callbuilder
31 from rpython.rlib.rarithmetic import r_uint
32 from rpython.jit.backend.ppc.vector_ext import VectorAssembler
33 from rpython.rlib.rjitlog import rjitlog as jl
34
35 class IntOpAssembler(object):
36
37 _mixin_ = True
38
39 def emit_int_add(self, op, arglocs, regalloc):
40 l0, l1, res = arglocs
41 assert not l0.is_imm()
42 if l1.is_imm():
43 self.mc.addi(res.value, l0.value, l1.value)
44 else:
45 self.mc.add(res.value, l0.value, l1.value)
46
47 emit_nursery_ptr_increment = emit_int_add
48
49 def emit_int_sub(self, op, arglocs, regalloc):
50 l0, l1, res = arglocs
51 assert not l0.is_imm()
52 if l1.is_imm():
53 self.mc.subi(res.value, l0.value, l1.value)
54 else:
55 self.mc.sub(res.value, l0.value, l1.value)
56
57 def emit_int_mul(self, op, arglocs, regalloc):
58 l0, l1, res = arglocs
59 assert not l0.is_imm()
60 if l1.is_imm():
61 self.mc.mulli(res.value, l0.value, l1.value)
62 elif IS_PPC_32:
63 self.mc.mullw(res.value, l0.value, l1.value)
64 else:
65 self.mc.mulld(res.value, l0.value, l1.value)
66
67 def emit_uint_mul_high(self, op, arglocs, regalloc):
68 l0, l1, res = arglocs
69 assert not l0.is_imm()
70 assert not l1.is_imm()
71 self.mc.mulhdu(res.value, l0.value, l1.value)
72
73 def do_emit_int_binary_ovf(self, op, arglocs):
74 l0, l1, res = arglocs[0], arglocs[1], arglocs[2]
75 self.mc.load_imm(r.SCRATCH, 0)
76 self.mc.mtxer(r.SCRATCH.value)
77 return (res.value, l0.value, l1.value)
78
79 def emit_int_add_ovf(self, op, arglocs, regalloc):
80 self.mc.addox(*self.do_emit_int_binary_ovf(op, arglocs))
81
82 def emit_int_sub_ovf(self, op, arglocs, regalloc):
83 self.mc.subox(*self.do_emit_int_binary_ovf(op, arglocs))
84
85 def emit_int_mul_ovf(self, op, arglocs, regalloc):
86 if IS_PPC_32:
87 self.mc.mullwox(*self.do_emit_int_binary_ovf(op, arglocs))
88 else:
89 self.mc.mulldox(*self.do_emit_int_binary_ovf(op, arglocs))
90
91 def emit_int_and(self, op, arglocs, regalloc):
92 l0, l1, res = arglocs
93 self.mc.and_(res.value, l0.value, l1.value)
94
95 def emit_int_or(self, op, arglocs, regalloc):
96 l0, l1, res = arglocs
97 self.mc.or_(res.value, l0.value, l1.value)
98
99 def emit_int_xor(self, op, arglocs, regalloc):
100 l0, l1, res = arglocs
101 self.mc.xor(res.value, l0.value, l1.value)
102
103 def emit_int_lshift(self, op, arglocs, regalloc):
104 l0, l1, res = arglocs
105 if IS_PPC_32:
106 self.mc.slw(res.value, l0.value, l1.value)
107 else:
108 self.mc.sld(res.value, l0.value, l1.value)
109
110 def emit_int_rshift(self, op, arglocs, regalloc):
111 l0, l1, res = arglocs
112 if IS_PPC_32:
113 self.mc.sraw(res.value, l0.value, l1.value)
114 else:
115 self.mc.srad(res.value, l0.value, l1.value)
116
117 def emit_uint_rshift(self, op, arglocs, regalloc):
118 l0, l1, res = arglocs
119 if IS_PPC_32:
120 self.mc.srw(res.value, l0.value, l1.value)
121 else:
122 self.mc.srd(res.value, l0.value, l1.value)
123
124 emit_int_le = gen_emit_cmp_op(c.LE)
125 emit_int_lt = gen_emit_cmp_op(c.LT)
126 emit_int_gt = gen_emit_cmp_op(c.GT)
127 emit_int_ge = gen_emit_cmp_op(c.GE)
128 emit_int_eq = gen_emit_cmp_op(c.EQ)
129 emit_int_ne = gen_emit_cmp_op(c.NE)
130
131 emit_uint_lt = gen_emit_cmp_op(c.LT, signed=False)
132 emit_uint_le = gen_emit_cmp_op(c.LE, signed=False)
133 emit_uint_gt = gen_emit_cmp_op(c.GT, signed=False)
134 emit_uint_ge = gen_emit_cmp_op(c.GE, signed=False)
135
136 emit_int_is_zero = emit_int_eq # EQ to 0
137 emit_int_is_true = emit_int_ne # NE to 0
138
139 emit_ptr_eq = emit_int_eq
140 emit_ptr_ne = emit_int_ne
141
142 emit_instance_ptr_eq = emit_ptr_eq
143 emit_instance_ptr_ne = emit_ptr_ne
144
145 def emit_int_neg(self, op, arglocs, regalloc):
146 l0, res = arglocs
147 self.mc.neg(res.value, l0.value)
148
149 def emit_int_invert(self, op, arglocs, regalloc):
150 l0, res = arglocs
151 self.mc.not_(res.value, l0.value)
152
153 def emit_int_signext(self, op, arglocs, regalloc):
154 l0, res = arglocs
155 extend_from = op.getarg(1).getint()
156 if extend_from == 1:
157 self.mc.extsb(res.value, l0.value)
158 elif extend_from == 2:
159 self.mc.extsh(res.value, l0.value)
160 elif extend_from == 4:
161 self.mc.extsw(res.value, l0.value)
162 else:
163 raise AssertionError(extend_from)
164
165 def emit_int_force_ge_zero(self, op, arglocs, regalloc):
166 arg, res = arglocs
167 with scratch_reg(self.mc):
168 self.mc.nor(r.SCRATCH.value, arg.value, arg.value)
169 if IS_PPC_32:
170 self.mc.srawi(r.SCRATCH.value, r.SCRATCH.value, 31)
171 else:
172 # sradi (scratch, scratch, 63)
173 self.mc.sradi(r.SCRATCH.value, r.SCRATCH.value, 1, 31)
174 self.mc.and_(res.value, arg.value, r.SCRATCH.value)
175
176 class FloatOpAssembler(object):
177 _mixin_ = True
178
179 def emit_float_add(self, op, arglocs, regalloc):
180 l0, l1, res = arglocs
181 self.mc.fadd(res.value, l0.value, l1.value)
182
183 def emit_float_sub(self, op, arglocs, regalloc):
184 l0, l1, res = arglocs
185 self.mc.fsub(res.value, l0.value, l1.value)
186
187 def emit_float_mul(self, op, arglocs, regalloc):
188 l0, l1, res = arglocs
189 self.mc.fmul(res.value, l0.value, l1.value)
190
191 def emit_float_truediv(self, op, arglocs, regalloc):
192 l0, l1, res = arglocs
193 self.mc.fdiv(res.value, l0.value, l1.value)
194
195 def emit_float_neg(self, op, arglocs, regalloc):
196 l0, res = arglocs
197 self.mc.fneg(res.value, l0.value)
198
199 def emit_float_abs(self, op, arglocs, regalloc):
200 l0, res = arglocs
201 self.mc.fabs(res.value, l0.value)
202
203 def _emit_math_sqrt(self, op, arglocs, regalloc):
204 l0, res = arglocs
205 self.mc.fsqrt(res.value, l0.value)
206
207 def _emit_threadlocalref_get(self, op, arglocs, regalloc):
208 [resloc] = arglocs
209 offset = op.getarg(1).getint() # getarg(0) == 'threadlocalref_get'
210 calldescr = op.getdescr()
211 size = calldescr.get_result_size()
212 sign = calldescr.is_result_signed()
213 #
214 # This loads the stack location THREADLOCAL_OFS into a
215 # register, and then read the word at the given offset.
216 # It is only supported if 'translate_support_code' is
217 # true; otherwise, the execute_token() was done with a
218 # dummy value for the stack location THREADLOCAL_OFS
219 #
220 assert self.cpu.translate_support_code
221 assert resloc.is_reg()
222 assert _check_imm_arg(offset)
223 self.mc.ld(resloc.value, r.SP.value, THREADLOCAL_ADDR_OFFSET)
224 self._load_from_mem(resloc, resloc, imm(offset), imm(size), imm(sign))
225
226 emit_float_le = gen_emit_cmp_op(c.LE, fp=True)
227 emit_float_lt = gen_emit_cmp_op(c.LT, fp=True)
228 emit_float_gt = gen_emit_cmp_op(c.GT, fp=True)
229 emit_float_ge = gen_emit_cmp_op(c.GE, fp=True)
230 emit_float_eq = gen_emit_cmp_op(c.EQ, fp=True)
231 emit_float_ne = gen_emit_cmp_op(c.NE, fp=True)
232
233 def emit_cast_float_to_int(self, op, arglocs, regalloc):
234 l0, temp_loc, res = arglocs
235 self.mc.fctidz(temp_loc.value, l0.value)
236 self.mc.stfd(temp_loc.value, r.SP.value, PARAM_SAVE_AREA_OFFSET)
237 self.mc.ld(res.value, r.SP.value, PARAM_SAVE_AREA_OFFSET)
238
239 def emit_cast_int_to_float(self, op, arglocs, regalloc):
240 l0, res = arglocs
241 self.mc.std(l0.value, r.SP.value, PARAM_SAVE_AREA_OFFSET)
242 self.mc.lfd(res.value, r.SP.value, PARAM_SAVE_AREA_OFFSET)
243 self.mc.fcfid(res.value, res.value)
244
245 def emit_convert_float_bytes_to_longlong(self, op, arglocs, regalloc):
246 l0, res = arglocs
247 self.mc.stfd(l0.value, r.SP.value, PARAM_SAVE_AREA_OFFSET)
248 self.mc.ld(res.value, r.SP.value, PARAM_SAVE_AREA_OFFSET)
249
250 def emit_convert_longlong_bytes_to_float(self, op, arglocs, regalloc):
251 l0, res = arglocs
252 self.mc.std(l0.value, r.SP.value, PARAM_SAVE_AREA_OFFSET)
253 self.mc.lfd(res.value, r.SP.value, PARAM_SAVE_AREA_OFFSET)
254
255 class GuardOpAssembler(object):
256
257 _mixin_ = True
258
259 def _emit_guard(self, op, arglocs, is_guard_not_invalidated=False):
260 if is_guard_not_invalidated:
261 fcond = c.cond_none
262 else:
263 fcond = self.guard_success_cc
264 self.guard_success_cc = c.cond_none
265 assert fcond != c.cond_none
266 fcond = c.negate(fcond)
267 token = self.build_guard_token(op, arglocs[0].value, arglocs[1:], fcond)
268 token.pos_jump_offset = self.mc.currpos()
269 assert token.guard_not_invalidated() == is_guard_not_invalidated
270 if not is_guard_not_invalidated:
271 self.mc.trap() # has to be patched later on
272 self.pending_guard_tokens.append(token)
273
274 def build_guard_token(self, op, frame_depth, arglocs, fcond):
275 descr = op.getdescr()
276 gcmap = allocate_gcmap(self, frame_depth, r.JITFRAME_FIXED_SIZE)
277 faildescrindex = self.get_gcref_from_faildescr(descr)
278 token = PPCGuardToken(self.cpu, gcmap, descr, op.getfailargs(),
279 arglocs, op.getopnum(), frame_depth,
280 faildescrindex,
281 fcond)
282 return token
283
284 def emit_guard_true(self, op, arglocs, regalloc):
285 self._emit_guard(op, arglocs)
286
287 def emit_guard_false(self, op, arglocs, regalloc):
288 self.guard_success_cc = c.negate(self.guard_success_cc)
289 self._emit_guard(op, arglocs)
290
291 def emit_guard_overflow(self, op, arglocs, regalloc):
292 self.guard_success_cc = c.SO
293 self._emit_guard(op, arglocs)
294
295 def emit_guard_no_overflow(self, op, arglocs, regalloc):
296 self.guard_success_cc = c.NS
297 self._emit_guard(op, arglocs)
298
299 def emit_guard_value(self, op, arglocs, regalloc):
300 l0 = arglocs[0]
301 l1 = arglocs[1]
302 failargs = arglocs[2:]
303
304 if l0.is_reg():
305 if l1.is_imm():
306 self.mc.cmp_op(0, l0.value, l1.getint(), imm=True)
307 else:
308 self.mc.cmp_op(0, l0.value, l1.value)
309 elif l0.is_fp_reg():
310 assert l1.is_fp_reg()
311 self.mc.cmp_op(0, l0.value, l1.value, fp=True)
312 self.guard_success_cc = c.EQ
313 self._emit_guard(op, failargs)
314
315 emit_guard_nonnull = emit_guard_true
316 emit_guard_isnull = emit_guard_false
317
318 def emit_guard_class(self, op, arglocs, regalloc):
319 self._cmp_guard_class(op, arglocs, regalloc)
320 self.guard_success_cc = c.EQ
321 self._emit_guard(op, arglocs[2:])
322
323 def emit_guard_nonnull_class(self, op, arglocs, regalloc):
324 self.mc.cmp_op(0, arglocs[0].value, 1, imm=True, signed=False)
325 patch_pos = self.mc.currpos()
326 self.mc.trap()
327 self._cmp_guard_class(op, arglocs, regalloc)
328 pmc = OverwritingBuilder(self.mc, patch_pos, 1)
329 pmc.blt(self.mc.currpos() - patch_pos)
330 pmc.overwrite()
331 self.guard_success_cc = c.EQ
332 self._emit_guard(op, arglocs[2:])
333
334 def _cmp_guard_class(self, op, locs, regalloc):
335 offset = self.cpu.vtable_offset
336 if offset is not None:
337 # could be one instruction shorter, but don't care because
338 # it's not this case that is commonly translated
339 self.mc.load(r.SCRATCH.value, locs[0].value, offset)
340 self.mc.load_imm(r.SCRATCH2, locs[1].value)
341 self.mc.cmp_op(0, r.SCRATCH.value, r.SCRATCH2.value)
342 else:
343 expected_typeid = (self.cpu.gc_ll_descr
344 .get_typeid_from_classptr_if_gcremovetypeptr(locs[1].value))
345 self._cmp_guard_gc_type(locs[0], expected_typeid)
346
347 def _read_typeid(self, targetreg, loc_ptr):
348 # Note that the typeid half-word is at offset 0 on a little-endian
349 # machine; it is at offset 2 or 4 on a big-endian machine.
350 assert self.cpu.supports_guard_gc_type
351 if IS_PPC_32:
352 self.mc.lhz(targetreg.value, loc_ptr.value, 2 * IS_BIG_ENDIAN)
353 else:
354 self.mc.lwz(targetreg.value, loc_ptr.value, 4 * IS_BIG_ENDIAN)
355
356 def _cmp_guard_gc_type(self, loc_ptr, expected_typeid):
357 self._read_typeid(r.SCRATCH2, loc_ptr)
358 assert 0 <= expected_typeid <= 0x7fffffff # 4 bytes are always enough
359 if expected_typeid > 0xffff: # if 2 bytes are not enough
360 self.mc.subis(r.SCRATCH2.value, r.SCRATCH2.value,
361 expected_typeid >> 16)
362 expected_typeid = expected_typeid & 0xffff
363 self.mc.cmp_op(0, r.SCRATCH2.value, expected_typeid,
364 imm=True, signed=False)
365
366 def emit_guard_gc_type(self, op, arglocs, regalloc):
367 self._cmp_guard_gc_type(arglocs[0], arglocs[1].value)
368 self.guard_success_cc = c.EQ
369 self._emit_guard(op, arglocs[2:])
370
371 def emit_guard_is_object(self, op, arglocs, regalloc):
372 assert self.cpu.supports_guard_gc_type
373 loc_object = arglocs[0]
374 # idea: read the typeid, fetch one byte of the field 'infobits' from
375 # the big typeinfo table, and check the flag 'T_IS_RPYTHON_INSTANCE'.
376 base_type_info, shift_by, sizeof_ti = (
377 self.cpu.gc_ll_descr.get_translated_info_for_typeinfo())
378 infobits_offset, IS_OBJECT_FLAG = (
379 self.cpu.gc_ll_descr.get_translated_info_for_guard_is_object())
380
381 self._read_typeid(r.SCRATCH2, loc_object)
382 self.mc.load_imm(r.SCRATCH, base_type_info + infobits_offset)
383 assert shift_by == 0 # on PPC64; fixme for PPC32
384 self.mc.lbzx(r.SCRATCH2.value, r.SCRATCH2.value, r.SCRATCH.value)
385 self.mc.andix(r.SCRATCH2.value, r.SCRATCH2.value, IS_OBJECT_FLAG & 0xff)
386 self.guard_success_cc = c.NE
387 self._emit_guard(op, arglocs[1:])
388
389 def emit_guard_subclass(self, op, arglocs, regalloc):
390 assert self.cpu.supports_guard_gc_type
391 loc_object = arglocs[0]
392 loc_check_against_class = arglocs[1]
393 offset = self.cpu.vtable_offset
394 offset2 = self.cpu.subclassrange_min_offset
395 if offset is not None:
396 # read this field to get the vtable pointer
397 self.mc.load(r.SCRATCH2.value, loc_object.value, offset)
398 # read the vtable's subclassrange_min field
399 assert _check_imm_arg(offset2)
400 self.mc.ld(r.SCRATCH2.value, r.SCRATCH2.value, offset2)
401 else:
402 # read the typeid
403 self._read_typeid(r.SCRATCH, loc_object)
404 # read the vtable's subclassrange_min field, as a single
405 # step with the correct offset
406 base_type_info, shift_by, sizeof_ti = (
407 self.cpu.gc_ll_descr.get_translated_info_for_typeinfo())
408 self.mc.load_imm(r.SCRATCH2, base_type_info + sizeof_ti + offset2)
409 assert shift_by == 0 # on PPC64; fixme for PPC32
410 self.mc.ldx(r.SCRATCH2.value, r.SCRATCH2.value, r.SCRATCH.value)
411 # get the two bounds to check against
412 vtable_ptr = loc_check_against_class.getint()
413 vtable_ptr = rffi.cast(rclass.CLASSTYPE, vtable_ptr)
414 check_min = vtable_ptr.subclassrange_min
415 check_max = vtable_ptr.subclassrange_max
416 assert check_max > check_min
417 check_diff = check_max - check_min - 1
418 # right now, a full PyPy uses less than 6000 numbers,
419 # so we'll assert here that it always fit inside 15 bits
420 assert 0 <= check_min <= 0x7fff
421 assert 0 <= check_diff <= 0xffff
422 # check by doing the unsigned comparison (tmp - min) < (max - min)
423 self.mc.subi(r.SCRATCH2.value, r.SCRATCH2.value, check_min)
424 self.mc.cmp_op(0, r.SCRATCH2.value, check_diff, imm=True, signed=False)
425 # the guard passes if we get a result of "below or equal"
426 self.guard_success_cc = c.LE
427 self._emit_guard(op, arglocs[2:])
428
429 def emit_guard_not_invalidated(self, op, arglocs, regalloc):
430 self._emit_guard(op, arglocs, is_guard_not_invalidated=True)
431
432 def emit_guard_not_forced(self, op, arglocs, regalloc):
433 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
434 self.mc.ld(r.SCRATCH.value, r.SPP.value, ofs)
435 self.mc.cmp_op(0, r.SCRATCH.value, 0, imm=True)
436 self.guard_success_cc = c.EQ
437 self._emit_guard(op, arglocs)
438
439 def emit_guard_not_forced_2(self, op, arglocs, regalloc):
440 guard_token = self.build_guard_token(op, arglocs[0].value, arglocs[1:],
441 c.cond_none)
442 self._finish_gcmap = guard_token.gcmap
443 self._store_force_index(op)
444 self.store_info_on_descr(0, guard_token)
445
446
447 class MiscOpAssembler(object):
448
449 _mixin_ = True
450
451 def emit_label(self, op, arglocs, regalloc):
452 pass
453
454 def emit_increment_debug_counter(self, op, arglocs, regalloc):
455 [addr_loc, value_loc] = arglocs
456 self.mc.load(value_loc.value, addr_loc.value, 0)
457 self.mc.addi(value_loc.value, value_loc.value, 1) # can't use r0!
458 self.mc.store(value_loc.value, addr_loc.value, 0)
459
460 def emit_finish(self, op, arglocs, regalloc):
461 base_ofs = self.cpu.get_baseofs_of_frame_field()
462 if len(arglocs) > 0:
463 [return_val] = arglocs
464 if op.getarg(0).type == FLOAT:
465 self.mc.stfd(return_val.value, r.SPP.value, base_ofs)
466 else:
467 self.mc.std(return_val.value, r.SPP.value, base_ofs)
468
469 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
470 ofs2 = self.cpu.get_ofs_of_frame_field('jf_gcmap')
471
472 descr = op.getdescr()
473 faildescrindex = self.get_gcref_from_faildescr(descr)
474 self._load_from_gc_table(r.r5, r.r5, faildescrindex)
475
476 # gcmap logic here:
477 arglist = op.getarglist()
478 if arglist and arglist[0].type == REF:
479 if self._finish_gcmap:
480 # we're returning with a guard_not_forced_2, and
481 # additionally we need to say that the result contains
482 # a reference too:
483 self._finish_gcmap[0] |= r_uint(1)
484 gcmap = self._finish_gcmap
485 else:
486 gcmap = self.gcmap_for_finish
487 elif self._finish_gcmap:
488 # we're returning with a guard_not_forced_2
489 gcmap = self._finish_gcmap
490 else:
491 gcmap = lltype.nullptr(jitframe.GCMAP)
492 self.load_gcmap(self.mc, r.r2, gcmap)
493
494 self.mc.std(r.r5.value, r.SPP.value, ofs)
495 self.mc.store(r.r2.value, r.SPP.value, ofs2)
496
497 # exit function
498 self._call_footer()
499
500 def emit_jump(self, op, arglocs, regalloc):
501 # The backend's logic assumes that the target code is in a piece of
502 # assembler that was also called with the same number of arguments,
503 # so that the locations [ebp+8..] of the input arguments are valid
504 # stack locations both before and after the jump.
505 #
506 descr = op.getdescr()
507 assert isinstance(descr, TargetToken)
508 my_nbargs = self.current_clt._debug_nbargs
509 target_nbargs = descr._ppc_clt._debug_nbargs
510 assert my_nbargs == target_nbargs
511
512 if descr in self.target_tokens_currently_compiling:
513 self.mc.b_offset(descr._ll_loop_code)
514 else:
515 self.mc.b_abs(descr._ll_loop_code)
516
517 def _genop_same_as(self, op, arglocs, regalloc):
518 argloc, resloc = arglocs
519 if argloc is not resloc:
520 self.regalloc_mov(argloc, resloc)
521
522 emit_same_as_i = _genop_same_as
523 emit_same_as_r = _genop_same_as
524 emit_same_as_f = _genop_same_as
525 emit_cast_ptr_to_int = _genop_same_as
526 emit_cast_int_to_ptr = _genop_same_as
527
528 def emit_guard_no_exception(self, op, arglocs, regalloc):
529 self.mc.load_from_addr(r.SCRATCH2, r.SCRATCH2, self.cpu.pos_exception())
530 self.mc.cmp_op(0, r.SCRATCH2.value, 0, imm=True)
531 self.guard_success_cc = c.EQ
532 self._emit_guard(op, arglocs)
533 # If the previous operation was a COND_CALL, overwrite its conditional
534 # jump to jump over this GUARD_NO_EXCEPTION as well, if we can
535 if self._find_nearby_operation(regalloc,-1).getopnum() == rop.COND_CALL:
536 jmp_adr, BI, BO = self.previous_cond_call_jcond
537 relative_target = self.mc.currpos() - jmp_adr
538 pmc = OverwritingBuilder(self.mc, jmp_adr, 1)
539 pmc.bc(BO, BI, relative_target)
540 pmc.overwrite()
541
542 def emit_save_exc_class(self, op, arglocs, regalloc):
543 [resloc] = arglocs
544 diff = self.mc.load_imm_plus(r.r2, self.cpu.pos_exception())
545 self.mc.load(resloc.value, r.r2.value, diff)
546
547 def emit_save_exception(self, op, arglocs, regalloc):
548 [resloc] = arglocs
549 self._store_and_reset_exception(self.mc, resloc)
550
551 def emit_restore_exception(self, op, arglocs, regalloc):
552 self._restore_exception(self.mc, arglocs[1], arglocs[0])
553
554 def emit_guard_exception(self, op, arglocs, regalloc):
555 loc, resloc = arglocs[:2]
556 failargs = arglocs[2:]
557
558 mc = self.mc
559 mc.load_imm(r.SCRATCH2, self.cpu.pos_exc_value())
560 diff = self.cpu.pos_exception() - self.cpu.pos_exc_value()
561 assert _check_imm_arg(diff)
562
563 mc.load(r.SCRATCH.value, r.SCRATCH2.value, diff)
564 mc.cmp_op(0, r.SCRATCH.value, loc.value)
565 self.guard_success_cc = c.EQ
566 self._emit_guard(op, failargs)
567
568 if resloc:
569 mc.load(resloc.value, r.SCRATCH2.value, 0)
570 mc.load_imm(r.SCRATCH, 0)
571 mc.store(r.SCRATCH.value, r.SCRATCH2.value, 0)
572 mc.store(r.SCRATCH.value, r.SCRATCH2.value, diff)
573
574 def _load_from_gc_table(self, rD, rT, index):
575 # rT is a temporary, may be equal to rD, must be != r0
576 addr = self.gc_table_addr + index * WORD
577 self.mc.load_from_addr(rD, rT, addr)
578
579 def emit_load_from_gc_table(self, op, arglocs, regalloc):
580 index = op.getarg(0).getint()
581 [resloc] = arglocs
582 assert resloc.is_reg()
583 self._load_from_gc_table(resloc, resloc, index)
584
585
586 class CallOpAssembler(object):
587
588 _mixin_ = True
589
590 def _emit_call(self, op, arglocs, is_call_release_gil=False):
591 resloc = arglocs[0]
592 func_index = 1 + is_call_release_gil
593 adr = arglocs[func_index]
594 arglist = arglocs[func_index+1:]
595
596 cb = callbuilder.CallBuilder(self, adr, arglist, resloc)
597
598 descr = op.getdescr()
599 assert isinstance(descr, CallDescr)
600 cb.argtypes = descr.get_arg_types()
601 cb.restype = descr.get_result_type()
602 cb.ressize = descr.get_result_size()
603 cb.ressign = descr.is_result_signed()
604
605 if is_call_release_gil:
606 saveerrloc = arglocs[1]
607 assert saveerrloc.is_imm()
608 cb.emit_call_release_gil(saveerrloc.value)
609 else:
610 effectinfo = descr.get_extra_info()
611 if effectinfo is None or effectinfo.check_can_collect():
612 cb.emit()
613 else:
614 cb.emit_no_collect()
615
616 def _genop_call(self, op, arglocs, regalloc):
617 oopspecindex = regalloc.get_oopspecindex(op)
618 if oopspecindex == EffectInfo.OS_MATH_SQRT:
619 return self._emit_math_sqrt(op, arglocs, regalloc)
620 if oopspecindex == EffectInfo.OS_THREADLOCALREF_GET:
621 return self._emit_threadlocalref_get(op, arglocs, regalloc)
622 self._emit_call(op, arglocs)
623
624 emit_call_i = _genop_call
625 emit_call_r = _genop_call
626 emit_call_f = _genop_call
627 emit_call_n = _genop_call
628
629 def _genop_call_may_force(self, op, arglocs, regalloc):
630 self._store_force_index(self._find_nearby_operation(regalloc, +1))
631 self._emit_call(op, arglocs)
632
633 emit_call_may_force_i = _genop_call_may_force
634 emit_call_may_force_r = _genop_call_may_force
635 emit_call_may_force_f = _genop_call_may_force
636 emit_call_may_force_n = _genop_call_may_force
637
638 def _genop_call_release_gil(self, op, arglocs, regalloc):
639 self._store_force_index(self._find_nearby_operation(regalloc, +1))
640 self._emit_call(op, arglocs, is_call_release_gil=True)
641
642 emit_call_release_gil_i = _genop_call_release_gil
643 emit_call_release_gil_f = _genop_call_release_gil
644 emit_call_release_gil_n = _genop_call_release_gil
645
646 def _store_force_index(self, guard_op):
647 assert (guard_op.getopnum() == rop.GUARD_NOT_FORCED or
648 guard_op.getopnum() == rop.GUARD_NOT_FORCED_2)
649 faildescr = guard_op.getdescr()
650 ofs = self.cpu.get_ofs_of_frame_field('jf_force_descr')
651 faildescrindex = self.get_gcref_from_faildescr(faildescr)
652 self._load_from_gc_table(r.r2, r.r2, faildescrindex)
653 self.mc.store(r.r2.value, r.SPP.value, ofs)
654
655 def _find_nearby_operation(self, regalloc, delta):
656 return regalloc.operations[regalloc.rm.position + delta]
657
658 _COND_CALL_SAVE_REGS = [r.r3, r.r4, r.r5, r.r6, r.r12]
659
660 def emit_cond_call(self, op, arglocs, regalloc):
661 resloc = arglocs[0]
662 arglocs = arglocs[1:]
663
664 fcond = self.guard_success_cc
665 self.guard_success_cc = c.cond_none
666 assert fcond != c.cond_none
667
668 jmp_adr = self.mc.get_relative_pos()
669 self.mc.trap() # patched later to a 'bc'
670
671 self.load_gcmap(self.mc, r.r2, regalloc.get_gcmap([resloc]))
672
673 # save away r3, r4, r5, r6, r12 into the jitframe
674 should_be_saved = [
675 reg for reg in self._regalloc.rm.reg_bindings.values()
676 if reg in self._COND_CALL_SAVE_REGS]
677 self._push_core_regs_to_jitframe(self.mc, should_be_saved)
678 #
679 # load the 0-to-4 arguments into these registers, with the address of
680 # the function to call into r12
681 remap_frame_layout(self, arglocs,
682 [r.r12, r.r3, r.r4, r.r5, r.r6][:len(arglocs)],
683 r.SCRATCH)
684 #
685 # figure out which variant of cond_call_slowpath to call, and call it
686 callee_only = False
687 floats = False
688 for reg in regalloc.rm.reg_bindings.values():
689 if reg not in regalloc.rm.save_around_call_regs:
690 break
691 else:
692 callee_only = True
693 if len(regalloc.fprm.reg_bindings):
694 floats = True
695 cond_call_adr = self.cond_call_slowpath[floats * 2 + callee_only]
696 self.mc.bl_abs(cond_call_adr)
697 # restoring the registers saved above, and doing pop_gcmap(), is left
698 # to the cond_call_slowpath helper. If we have a result, move
699 # it from r2 to its expected location.
700 if resloc is not None:
701 self.mc.mr(resloc.value, r.SCRATCH2.value)
702 relative_target = self.mc.currpos() - jmp_adr
703 pmc = OverwritingBuilder(self.mc, jmp_adr, 1)
704 BI, BO = c.encoding[fcond]
705 pmc.bc(BO, BI, relative_target)
706 pmc.overwrite()
707 # might be overridden again to skip over the following
708 # guard_no_exception too
709 self.previous_cond_call_jcond = jmp_adr, BI, BO
710
711 emit_cond_call_value_i = emit_cond_call
712 emit_cond_call_value_r = emit_cond_call
713
714
715 class FieldOpAssembler(object):
716
717 _mixin_ = True
718
719 def _write_to_mem(self, value_loc, base_loc, ofs, size_loc):
720 assert size_loc.is_imm()
721 size = size_loc.value
722 if size == 8:
723 if value_loc.is_fp_reg():
724 if ofs.is_imm():
725 self.mc.stfd(value_loc.value, base_loc.value, ofs.value)
726 else:
727 self.mc.stfdx(value_loc.value, base_loc.value, ofs.value)
728 else:
729 if ofs.is_imm():
730 self.mc.std(value_loc.value, base_loc.value, ofs.value)
731 else:
732 self.mc.stdx(value_loc.value, base_loc.value, ofs.value)
733 elif size == 4:
734 if ofs.is_imm():
735 self.mc.stw(value_loc.value, base_loc.value, ofs.value)
736 else:
737 self.mc.stwx(value_loc.value, base_loc.value, ofs.value)
738 elif size == 2:
739 if ofs.is_imm():
740 self.mc.sth(value_loc.value, base_loc.value, ofs.value)
741 else:
742 self.mc.sthx(value_loc.value, base_loc.value, ofs.value)
743 elif size == 1:
744 if ofs.is_imm():
745 self.mc.stb(value_loc.value, base_loc.value, ofs.value)
746 else:
747 self.mc.stbx(value_loc.value, base_loc.value, ofs.value)
748 else:
749 assert 0, "size not supported"
750
751 def emit_gc_store(self, op, arglocs, regalloc):
752 value_loc, base_loc, ofs_loc, size_loc = arglocs
753 self._write_to_mem(value_loc, base_loc, ofs_loc, size_loc)
754
755 def _apply_offset(self, index_loc, ofs_loc):
756 # If offset != 0 then we have to add it here. Note that
757 # mc.addi() would not be valid with operand r0.
758 assert index_loc.is_core_reg()
759 assert index_loc is not r.SCRATCH2
760 if ofs_loc.is_imm():
761 # if it is an immediate, it must fit into 16 bits
762 assert _check_imm_arg(ofs_loc.getint())
763 # (simplified version of _apply_scale())
764 if ofs_loc.value != 0:
765 self.mc.addi(r.SCRATCH2.value, index_loc.value, ofs_loc.value)
766 index_loc = r.SCRATCH2
767 else:
768 # larger immediates are loaded into a register in regalloc.py
769 assert ofs_loc.is_core_reg()
770 self.mc.add(r.SCRATCH2.value, index_loc.value, ofs_loc.value)
771 index_loc = r.SCRATCH2
772 return index_loc
773
774 def emit_gc_store_indexed(self, op, arglocs, regalloc):
775 base_loc, index_loc, value_loc, ofs_loc, size_loc = arglocs
776 index_loc = self._apply_offset(index_loc, ofs_loc)
777 self._write_to_mem(value_loc, base_loc, index_loc, size_loc)
778
779 def _load_from_mem(self, res, base_loc, ofs, size_loc, sign_loc):
780 # res, base_loc, ofs, size and signed are all locations
781 assert base_loc is not r.SCRATCH
782 assert size_loc.is_imm()
783 size = size_loc.value
784 assert sign_loc.is_imm()
785 sign = sign_loc.value
786 if size == 8:
787 if res.is_fp_reg():
788 if ofs.is_imm():
789 self.mc.lfd(res.value, base_loc.value, ofs.value)
790 else:
791 self.mc.lfdx(res.value, base_loc.value, ofs.value)
792 else:
793 if ofs.is_imm():
794 self.mc.ld(res.value, base_loc.value, ofs.value)
795 else:
796 self.mc.ldx(res.value, base_loc.value, ofs.value)
797 elif size == 4:
798 if IS_PPC_64 and sign:
799 if ofs.is_imm():
800 self.mc.lwa(res.value, base_loc.value, ofs.value)
801 else:
802 self.mc.lwax(res.value, base_loc.value, ofs.value)
803 else:
804 if ofs.is_imm():
805 self.mc.lwz(res.value, base_loc.value, ofs.value)
806 else:
807 self.mc.lwzx(res.value, base_loc.value, ofs.value)
808 elif size == 2:
809 if sign:
810 if ofs.is_imm():
811 self.mc.lha(res.value, base_loc.value, ofs.value)
812 else:
813 self.mc.lhax(res.value, base_loc.value, ofs.value)
814 else:
815 if ofs.is_imm():
816 self.mc.lhz(res.value, base_loc.value, ofs.value)
817 else:
818 self.mc.lhzx(res.value, base_loc.value, ofs.value)
819 elif size == 1:
820 if ofs.is_imm():
821 self.mc.lbz(res.value, base_loc.value, ofs.value)
822 else:
823 self.mc.lbzx(res.value, base_loc.value, ofs.value)
824 if sign:
825 self.mc.extsb(res.value, res.value)
826 else:
827 assert 0, "size not supported"
828
829 def _genop_gc_load(self, op, arglocs, regalloc):
830 base_loc, ofs_loc, res_loc, size_loc, sign_loc = arglocs
831 self._load_from_mem(res_loc, base_loc, ofs_loc, size_loc, sign_loc)
832
833 emit_gc_load_i = _genop_gc_load
834 emit_gc_load_r = _genop_gc_load
835 emit_gc_load_f = _genop_gc_load
836
837 def _genop_gc_load_indexed(self, op, arglocs, regalloc):
838 base_loc, index_loc, res_loc, ofs_loc, size_loc, sign_loc = arglocs
839 index_loc = self._apply_offset(index_loc, ofs_loc)
840 self._load_from_mem(res_loc, base_loc, index_loc, size_loc, sign_loc)
841
842 emit_gc_load_indexed_i = _genop_gc_load_indexed
843 emit_gc_load_indexed_r = _genop_gc_load_indexed
844 emit_gc_load_indexed_f = _genop_gc_load_indexed
845
846 SIZE2SCALE = dict([(1<<_i, _i) for _i in range(32)])
847
848 def _multiply_by_constant(self, loc, multiply_by, scratch_loc):
849 # XXX should die together with _apply_scale() but can't because
850 # of emit_zero_array() and malloc_cond_varsize() at the moment
851 assert loc.is_reg()
852 if multiply_by == 1:
853 return loc
854 try:
855 scale = self.SIZE2SCALE[multiply_by]
856 except KeyError:
857 if _check_imm_arg(multiply_by):
858 self.mc.mulli(scratch_loc.value, loc.value, multiply_by)
859 else:
860 self.mc.load_imm(scratch_loc, multiply_by)
861 if IS_PPC_32:
862 self.mc.mullw(scratch_loc.value, loc.value,
863 scratch_loc.value)
864 else:
865 self.mc.mulld(scratch_loc.value, loc.value,
866 scratch_loc.value)
867 else:
868 self.mc.sldi(scratch_loc.value, loc.value, scale)
869 return scratch_loc
870
871 def _copy_in_scratch2(self, loc):
872 if loc.is_imm():
873 self.mc.li(r.SCRATCH2.value, loc.value)
874 elif loc is not r.SCRATCH2:
875 self.mc.mr(r.SCRATCH2.value, loc.value)
876 return r.SCRATCH2
877
878 # RPythonic workaround for emit_zero_array()
879 def eza_stXux(self, a, b, c, itemsize):
880 if itemsize & 1: self.mc.stbux(a, b, c)
881 elif itemsize & 2: self.mc.sthux(a, b, c)
882 elif (itemsize & 4) or IS_PPC_32: self.mc.stwux(a, b, c)
883 else: self.mc.stdux(a, b, c)
884 def eza_stXu(self, a, b, c, itemsize):
885 if itemsize & 1: self.mc.stbu(a, b, c)
886 elif itemsize & 2: self.mc.sthu(a, b, c)
887 elif (itemsize & 4) or IS_PPC_32: self.mc.stwu(a, b, c)
888 else: self.mc.stdu(a, b, c)
889
890 def emit_zero_array(self, op, arglocs, regalloc):
891 base_loc, startindex_loc, length_loc, ofs_loc = arglocs
892
893 stepsize = 8
894 shift_by = 3
895 if IS_PPC_32:
896 stepsize = 4
897 shift_by = 2
898
899 if length_loc.is_imm():
900 if length_loc.value <= 0:
901 return # nothing to do
902
903 if startindex_loc.is_imm():
904 self.mc.load_imm(r.SCRATCH2, startindex_loc.value)
905 startindex_loc = r.SCRATCH2
906 if ofs_loc.is_imm():
907 self.mc.addi(r.SCRATCH2.value, startindex_loc.value, ofs_loc.value)
908 else:
909 self.mc.add(r.SCRATCH2.value, startindex_loc.value, ofs_loc.value)
910 ofs_loc = r.SCRATCH2
911 assert base_loc.is_core_reg()
912 self.mc.add(ofs_loc.value, ofs_loc.value, base_loc.value)
913 # ofs_loc is now the real address pointing to the first
914 # byte to be zeroed
915
916 prev_length_loc = length_loc
917 if length_loc.is_imm():
918 self.mc.load_imm(r.SCRATCH, length_loc.value)
919 length_loc = r.SCRATCH
920
921 self.mc.cmp_op(0, length_loc.value, stepsize, imm=True)
922 jlt_location = self.mc.currpos()
923 self.mc.trap()
924
925 self.mc.sradi(r.SCRATCH.value, length_loc.value, 0, shift_by)
926 self.mc.mtctr(r.SCRATCH.value) # store the length in count register
927
928 self.mc.li(r.SCRATCH.value, 0)
929
930 # NOTE the following assumes that bytes have been passed to both startindex
931 # and length. Thus we zero 4/8 bytes in a loop in 1) and every remaining
932 # byte is zeroed in another loop in 2)
933
934 self.mc.subi(ofs_loc.value, ofs_loc.value, stepsize)
935
936 # first store of case 1)
937 # 1) The next loop copies WORDS into the memory chunk starting at startindex
938 # ending at startindex + length. These are bytes
939 loop_location = self.mc.currpos()
940 self.eza_stXu(r.SCRATCH.value, ofs_loc.value, stepsize, stepsize)
941 self.mc.bdnz(loop_location - self.mc.currpos())
942
943 self.mc.addi(ofs_loc.value, ofs_loc.value, stepsize)
944
945 pmc = OverwritingBuilder(self.mc, jlt_location, 1)
946 pmc.blt(self.mc.currpos() - jlt_location) # jump if length < WORD
947 pmc.overwrite()
948
949 # 2) There might be some bytes left to be written.
950 # following scenario: length_loc == 3 bytes, stepsize == 4!
951 # need to write the last bytes.
952
953 # move the last bytes to the count register
954 length_loc = prev_length_loc
955 if length_loc.is_imm():
956 self.mc.load_imm(r.SCRATCH, length_loc.value & (stepsize-1))
957 else:
958 self.mc.andix(r.SCRATCH.value, length_loc.value, (stepsize-1) & 0xff)
959
960 self.mc.cmp_op(0, r.SCRATCH.value, 0, imm=True)
961 jle_location = self.mc.currpos()
962 self.mc.trap()
963
964 self.mc.mtctr(r.SCRATCH.value)
965 self.mc.li(r.SCRATCH.value, 0)
966
967 self.mc.subi(ofs_loc.value, ofs_loc.value, 1)
968
969 loop_location = self.mc.currpos()
970 self.eza_stXu(r.SCRATCH.value, ofs_loc.value, 1, 1)
971 self.mc.bdnz(loop_location - self.mc.currpos())
972
973 pmc = OverwritingBuilder(self.mc, jle_location, 1)
974 pmc.ble(self.mc.currpos() - jle_location) # !GT
975 pmc.overwrite()
976
977
978 class AllocOpAssembler(object):
979
980 _mixin_ = True
981
982 def emit_check_memory_error(self, op, arglocs, regalloc):
983 self.propagate_memoryerror_if_reg_is_null(arglocs[0])
984
985 def emit_call_malloc_nursery(self, op, arglocs, regalloc):
986 # registers r.RES and r.RSZ are allocated for this call
987 size_box = op.getarg(0)
988 assert isinstance(size_box, ConstInt)
989 size = size_box.getint()
990 gc_ll_descr = self.cpu.gc_ll_descr
991 gcmap = regalloc.get_gcmap([r.RES, r.RSZ])
992 self.malloc_cond(
993 gc_ll_descr.get_nursery_free_addr(),
994 gc_ll_descr.get_nursery_top_addr(),
995 size, gcmap)
996
997 def emit_call_malloc_nursery_varsize_frame(self, op, arglocs, regalloc):
998 # registers r.RES and r.RSZ are allocated for this call
999 [sizeloc] = arglocs
1000 gc_ll_descr = self.cpu.gc_ll_descr
1001 gcmap = regalloc.get_gcmap([r.RES, r.RSZ])
1002 self.malloc_cond_varsize_frame(
1003 gc_ll_descr.get_nursery_free_addr(),
1004 gc_ll_descr.get_nursery_top_addr(),
1005 sizeloc, gcmap)
1006
1007 def emit_call_malloc_nursery_varsize(self, op, arglocs, regalloc):
1008 # registers r.RES and r.RSZ are allocated for this call
1009 gc_ll_descr = self.cpu.gc_ll_descr
1010 if not hasattr(gc_ll_descr, 'max_size_of_young_obj'):
1011 raise Exception("unreachable code")
1012 # for boehm, this function should never be called
1013 [lengthloc] = arglocs
1014 arraydescr = op.getdescr()
1015 itemsize = op.getarg(1).getint()
1016 maxlength = (gc_ll_descr.max_size_of_young_obj - WORD * 2) / itemsize
1017 gcmap = regalloc.get_gcmap([r.RES, r.RSZ])
1018 self.malloc_cond_varsize(
1019 op.getarg(0).getint(),
1020 gc_ll_descr.get_nursery_free_addr(),
1021 gc_ll_descr.get_nursery_top_addr(),
1022 lengthloc, itemsize, maxlength, gcmap, arraydescr)
1023
1024 def emit_debug_merge_point(self, op, arglocs, regalloc):
1025 pass
1026
1027 emit_jit_debug = emit_debug_merge_point
1028 emit_keepalive = emit_debug_merge_point
1029
1030 def emit_enter_portal_frame(self, op, arglocs, regalloc):
1031 self.enter_portal_frame(op)
1032
1033 def emit_leave_portal_frame(self, op, arglocs, regalloc):
1034 self.leave_portal_frame(op)
1035
1036 def _write_barrier_fastpath(self, mc, descr, arglocs, regalloc, array=False,
1037 is_frame=False):
1038 # Write code equivalent to write_barrier() in the GC: it checks
1039 # a flag in the object at arglocs[0], and if set, it calls a
1040 # helper piece of assembler. The latter saves registers as needed
1041 # and call the function remember_young_pointer() from the GC.
1042 if we_are_translated():
1043 cls = self.cpu.gc_ll_descr.has_write_barrier_class()
1044 assert cls is not None and isinstance(descr, cls)
1045 #
1046 card_marking_mask = 0
1047 mask = descr.jit_wb_if_flag_singlebyte
1048 if array and descr.jit_wb_cards_set != 0:
1049 # assumptions the rest of the function depends on:
1050 assert (descr.jit_wb_cards_set_byteofs ==
1051 descr.jit_wb_if_flag_byteofs)
1052 card_marking_mask = descr.jit_wb_cards_set_singlebyte
1053 #
1054 loc_base = arglocs[0]
1055 assert loc_base.is_reg()
1056 if is_frame:
1057 assert loc_base is r.SPP
1058 assert _check_imm_arg(descr.jit_wb_if_flag_byteofs)
1059 mc.lbz(r.SCRATCH2.value, loc_base.value, descr.jit_wb_if_flag_byteofs)
1060 mc.andix(r.SCRATCH.value, r.SCRATCH2.value, mask & 0xFF)
1061
1062 jz_location = mc.get_relative_pos()
1063 mc.trap() # patched later with 'beq'
1064
1065 # for cond_call_gc_wb_array, also add another fast path:
1066 # if GCFLAG_CARDS_SET, then we can just set one bit and be done
1067 if card_marking_mask:
1068 # GCFLAG_CARDS_SET is in the same byte, loaded in r2 already
1069 mc.andix(r.SCRATCH.value, r.SCRATCH2.value,
1070 card_marking_mask & 0xFF)
1071 js_location = mc.get_relative_pos()
1072 mc.trap() # patched later with 'bne'
1073 else:
1074 js_location = 0
1075
1076 # Write only a CALL to the helper prepared in advance, passing it as
1077 # argument the address of the structure we are writing into
1078 # (the first argument to COND_CALL_GC_WB).
1079 helper_num = (card_marking_mask != 0)
1080 if is_frame:
1081 helper_num = 4
1082 elif len(regalloc.fprm.reg_bindings):
1083 helper_num += 2
1084 if self.wb_slowpath[helper_num] == 0: # tests only
1085 assert not we_are_translated()
1086 assert not is_frame
1087 self.cpu.gc_ll_descr.write_barrier_descr = descr
1088 self._build_wb_slowpath(card_marking_mask != 0,
1089 bool(len(regalloc.fprm.reg_bindings)))
1090 assert self.wb_slowpath[helper_num] != 0
1091 #
1092 if not is_frame:
1093 mc.mr(r.r0.value, loc_base.value) # unusual argument location
1094 mc.load_imm(r.SCRATCH2, self.wb_slowpath[helper_num])
1095 mc.mtctr(r.SCRATCH2.value)
1096 mc.bctrl()
1097
1098 if card_marking_mask:
1099 # The helper ends again with a check of the flag in the object.
1100 # So here, we can simply write again a beq, which will be
1101 # taken if GCFLAG_CARDS_SET is still not set.
1102 jns_location = mc.get_relative_pos()
1103 mc.trap()
1104 #
1105 # patch the 'bne' above
1106 currpos = mc.currpos()
1107 pmc = OverwritingBuilder(mc, js_location, 1)
1108 pmc.bne(currpos - js_location)
1109 pmc.overwrite()
1110 #
1111 # case GCFLAG_CARDS_SET: emit a few instructions to do
1112 # directly the card flag setting
1113 loc_index = arglocs[1]
1114 if loc_index.is_reg():
1115
1116 tmp_loc = arglocs[2]
1117 n = descr.jit_wb_card_page_shift
1118
1119 # compute in tmp_loc the byte offset:
1120 # ~(index >> (card_page_shift + 3)) ('~' is 'not_' below)
1121 mc.srli_op(tmp_loc.value, loc_index.value, n + 3)
1122
1123 # compute in r2 the index of the bit inside the byte:
1124 # (index >> card_page_shift) & 7
1125 mc.rldicl(r.SCRATCH2.value, loc_index.value, 64 - n, 61)
1126 mc.li(r.SCRATCH.value, 1)
1127 mc.not_(tmp_loc.value, tmp_loc.value)
1128
1129 # set r2 to 1 << r2
1130 mc.sl_op(r.SCRATCH2.value, r.SCRATCH.value, r.SCRATCH2.value)
1131
1132 # set this bit inside the byte of interest
1133 mc.lbzx(r.SCRATCH.value, loc_base.value, tmp_loc.value)
1134 mc.or_(r.SCRATCH.value, r.SCRATCH.value, r.SCRATCH2.value)
1135 mc.stbx(r.SCRATCH.value, loc_base.value, tmp_loc.value)
1136 # done
1137
1138 else:
1139 byte_index = loc_index.value >> descr.jit_wb_card_page_shift
1140 byte_ofs = ~(byte_index >> 3)
1141 byte_val = 1 << (byte_index & 7)
1142 assert _check_imm_arg(byte_ofs)
1143
1144 mc.lbz(r.SCRATCH.value, loc_base.value, byte_ofs)
1145 mc.ori(r.SCRATCH.value, r.SCRATCH.value, byte_val)
1146 mc.stb(r.SCRATCH.value, loc_base.value, byte_ofs)
1147 #
1148 # patch the beq just above
1149 currpos = mc.currpos()
1150 pmc = OverwritingBuilder(mc, jns_location, 1)
1151 pmc.beq(currpos - jns_location)
1152 pmc.overwrite()
1153
1154 # patch the JZ above
1155 currpos = mc.currpos()
1156 pmc = OverwritingBuilder(mc, jz_location, 1)
1157 pmc.beq(currpos - jz_location)
1158 pmc.overwrite()
1159
1160 def emit_cond_call_gc_wb(self, op, arglocs, regalloc):
1161 self._write_barrier_fastpath(self.mc, op.getdescr(), arglocs, regalloc)
1162
1163 def emit_cond_call_gc_wb_array(self, op, arglocs, regalloc):
1164 self._write_barrier_fastpath(self.mc, op.getdescr(), arglocs, regalloc,
1165 array=True)
1166
1167
1168 class ForceOpAssembler(object):
1169
1170 _mixin_ = True
1171
1172 def emit_force_token(self, op, arglocs, regalloc):
1173 res_loc = arglocs[0]
1174 self.mc.mr(res_loc.value, r.SPP.value)
1175
1176 def _genop_call_assembler(self, op, arglocs, regalloc):
1177 if len(arglocs) == 3:
1178 [result_loc, argloc, vloc] = arglocs
1179 else:
1180 [result_loc, argloc] = arglocs
1181 vloc = imm(0)
1182 self._store_force_index(self._find_nearby_operation(regalloc, +1))
1183 # 'result_loc' is either r3 or f1, or None
1184 self.call_assembler(op, argloc, vloc, result_loc, r.r3)
1185
1186 emit_call_assembler_i = _genop_call_assembler
1187 emit_call_assembler_r = _genop_call_assembler
1188 emit_call_assembler_f = _genop_call_assembler
1189 emit_call_assembler_n = _genop_call_assembler
1190
1191 imm = staticmethod(imm) # for call_assembler()
1192
1193 def _call_assembler_emit_call(self, addr, argloc, _):
1194 self.regalloc_mov(argloc, r.r3)
1195 self.mc.ld(r.r4.value, r.SP.value, THREADLOCAL_ADDR_OFFSET)
1196
1197 cb = callbuilder.CallBuilder(self, addr, [r.r3, r.r4], r.r3)
1198 cb.emit()
1199
1200 def _call_assembler_emit_helper_call(self, addr, arglocs, result_loc):
1201 cb = callbuilder.CallBuilder(self, addr, arglocs, result_loc)
1202 cb.emit()
1203
1204 def _call_assembler_check_descr(self, value, tmploc):
1205 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
1206 self.mc.ld(r.r5.value, r.r3.value, ofs)
1207 if _check_imm_arg(value):
1208 self.mc.cmp_op(0, r.r5.value, value, imm=True)
1209 else:
1210 self.mc.load_imm(r.r4, value)
1211 self.mc.cmp_op(0, r.r5.value, r.r4.value, imm=False)
1212 jump_if_eq = self.mc.currpos()
1213 self.mc.trap() # patched later
1214 return jump_if_eq
1215
1216 def _call_assembler_patch_je(self, result_loc, je_location):
1217 jump_to_done = self.mc.currpos()
1218 self.mc.trap() # patched later
1219 #
1220 currpos = self.mc.currpos()
1221 pmc = OverwritingBuilder(self.mc, je_location, 1)
1222 pmc.beq(currpos - je_location)
1223 pmc.overwrite()
1224 #
1225 return jump_to_done
1226
1227 def _call_assembler_load_result(self, op, result_loc):
1228 if op.type != VOID:
1229 # load the return value from the dead frame's value index 0
1230 kind = op.type
1231 descr = self.cpu.getarraydescr_for_frame(kind)
1232 ofs = self.cpu.unpack_arraydescr(descr)
1233 if kind == FLOAT:
1234 assert result_loc is r.f1
1235 self.mc.lfd(r.f1.value, r.r3.value, ofs)
1236 else:
1237 assert result_loc is r.r3
1238 self.mc.ld(r.r3.value, r.r3.value, ofs)
1239
1240 def _call_assembler_patch_jmp(self, jmp_location):
1241 currpos = self.mc.currpos()
1242 pmc = OverwritingBuilder(self.mc, jmp_location, 1)
1243 pmc.b(currpos - jmp_location)
1244 pmc.overwrite()
1245
1246 def redirect_call_assembler(self, oldlooptoken, newlooptoken):
1247 # some minimal sanity checking
1248 old_nbargs = oldlooptoken.compiled_loop_token._debug_nbargs
1249 new_nbargs = newlooptoken.compiled_loop_token._debug_nbargs
1250 assert old_nbargs == new_nbargs
1251 oldadr = oldlooptoken._ll_function_addr
1252 target = newlooptoken._ll_function_addr
1253 # copy frame-info data
1254 baseofs = self.cpu.get_baseofs_of_frame_field()
1255 newlooptoken.compiled_loop_token.update_frame_info(
1256 oldlooptoken.compiled_loop_token, baseofs)
1257 if IS_PPC_64 and IS_BIG_ENDIAN:
1258 # PPC64 big-endian trampolines are data so overwrite the code
1259 # address in the function descriptor at the old address.
1260 # Copy the whole 3-word trampoline, even though the other
1261 # words are always zero so far. That's not enough in all
1262 # cases: if the "target" trampoline is itself redirected
1263 # later, then the "old" trampoline won't be updated; so
1264 # we still need the jump below to be safe.
1265 odata = rffi.cast(rffi.CArrayPtr(lltype.Signed), oldadr)
1266 tdata = rffi.cast(rffi.CArrayPtr(lltype.Signed), target)
1267 odata[0] = tdata[0]
1268 odata[1] = tdata[1]
1269 odata[2] = tdata[2]
1270 oldadr += 3 * WORD
1271 target += 3 * WORD
1272 # we overwrite the instructions at the old _ll_function_addr
1273 # to start with a JMP to the new _ll_function_addr.
1274 mc = PPCBuilder()
1275 mc.b_abs(target)
1276 mc.copy_to_raw_memory(oldadr)
1277 jl.redirect_assembler(oldlooptoken, newlooptoken, newlooptoken.number)
1278
1279
1280 class OpAssembler(IntOpAssembler, GuardOpAssembler,
1281 MiscOpAssembler, FieldOpAssembler,
1282 CallOpAssembler, ForceOpAssembler,
1283 AllocOpAssembler, FloatOpAssembler,
1284 VectorAssembler):
1285 _mixin_ = True
1286
1287 def nop(self):
1288 self.mc.ori(0, 0, 0)