Debian Codegraph @

rpython/jit/backend/zarch/opassembler.py

raw symbols SCIP
1 from rpython.jit.backend.llsupport.jump import remap_frame_layout
2 from rpython.jit.backend.zarch.arch import (WORD,
3 STD_FRAME_SIZE_IN_BYTES)
4 from rpython.jit.backend.zarch.arch import THREADLOCAL_ADDR_OFFSET
5 from rpython.jit.backend.zarch.helper.assembler import (gen_emit_cmp_op,
6 gen_emit_rr_rp, gen_emit_shift, gen_emit_rr_rh_ri_rp, gen_emit_div_mod)
7 from rpython.jit.backend.zarch.helper.regalloc import (check_imm,
8 check_imm_value)
9 from rpython.jit.metainterp.history import (ConstInt)
10 from rpython.jit.backend.zarch.codebuilder import ZARCHGuardToken, InstrBuilder
11 from rpython.jit.backend.llsupport import symbolic, jitframe
12 from rpython.rlib.rjitlog import rjitlog as jl
13 import rpython.jit.backend.zarch.conditions as c
14 import rpython.jit.backend.zarch.registers as r
15 import rpython.jit.backend.zarch.locations as l
16 from rpython.jit.backend.zarch.locations import imm
17 from rpython.jit.backend.zarch import callbuilder
18 from rpython.jit.backend.zarch.codebuilder import OverwritingBuilder
19 from rpython.jit.backend.llsupport.descr import CallDescr
20 from rpython.jit.backend.llsupport.gcmap import allocate_gcmap
21 from rpython.jit.codewriter.effectinfo import EffectInfo
22 from rpython.jit.metainterp.history import (FLOAT, INT, REF, VOID)
23 from rpython.jit.metainterp.resoperation import rop
24 from rpython.rtyper import rclass
25 from rpython.rtyper.lltypesystem import rstr, rffi, lltype
26 from rpython.rtyper.annlowlevel import cast_instance_to_gcref
27 from rpython.rlib.objectmodel import we_are_translated
28
29 class IntOpAssembler(object):
30 _mixin_ = True
31
32 emit_int_add = gen_emit_rr_rh_ri_rp('AGR', 'AGHI', 'AGFI', 'AG')
33 emit_int_add_ovf = emit_int_add
34
35 emit_nursery_ptr_increment = emit_int_add
36
37 def emit_int_sub(self, op, arglocs, regalloc):
38 res, l0, l1 = arglocs
39 self.mc.SGRK(res, l0, l1)
40
41 emit_int_sub_ovf = emit_int_sub
42
43 emit_int_mul = gen_emit_rr_rh_ri_rp('MSGR', 'MGHI', 'MSGFI', 'MSG')
44 def emit_int_mul_ovf(self, op, arglocs, regalloc):
45 lr, lq, l1 = arglocs
46 if l1.is_in_pool():
47 self.mc.LG(r.SCRATCH, l1)
48 l1 = r.SCRATCH
49 elif l1.is_imm():
50 self.mc.LGFI(r.SCRATCH, l1)
51 l1 = r.SCRATCH
52 else:
53 # we are not allowed to modify l1 if it is not a scratch
54 # register, thus copy it here!
55 self.mc.LGR(r.SCRATCH, l1)
56 l1 = r.SCRATCH
57
58 mc = self.mc
59
60 # check left neg
61 jmp_lq_lt_0 = mc.get_relative_pos()
62 mc.reserve_cond_jump() # CGIJ lq < 0 +-----------+
63 jmp_l1_ge_0 = mc.get_relative_pos() # |
64 mc.reserve_cond_jump() # CGIJ l1 >= 0 -----------|-> (both same sign)
65 jmp_lq_pos_l1_neg = mc.get_relative_pos() # |
66 mc.reserve_cond_jump(short=True) # BCR any -----|-> (xor negative)
67 jmp_l1_neg_lq_neg = mc.get_relative_pos() # |
68 mc.reserve_cond_jump() # <-----------------------+
69 # CGIJ l1 < 0 -> (both same_sign)
70 # (xor negative)
71 label_xor_neg = mc.get_relative_pos()
72 mc.LPGR(lq, lq)
73 mc.LPGR(l1, l1)
74 mc.MLGR(lr, l1)
75 mc.LGHI(r.SCRATCH, l.imm(-1))
76 mc.RISBG(r.SCRATCH, r.SCRATCH, l.imm(0), l.imm(0x80 | 0), l.imm(0))
77 # is the value greater than 2**63 ? then an overflow occurred
78 jmp_xor_lq_overflow = mc.get_relative_pos()
79 mc.reserve_cond_jump() # CLGRJ lq > 0x8000 ... 00 -> (label_overflow)
80 jmp_xor_lr_overflow = mc.get_relative_pos()
81 mc.reserve_cond_jump() # CLGIJ lr > 0 -> (label_overflow)
82 mc.LCGR(lq, lq) # complement the value
83 mc.XGR(r.SCRATCH, r.SCRATCH)
84 mc.SPM(r.SCRATCH) # 0x80 ... 00 clears the condition code and program mask
85 jmp_no_overflow_xor_neg = mc.get_relative_pos()
86 mc.reserve_cond_jump(short=True)
87
88 # both are positive/negative
89 label_both_same_sign = mc.get_relative_pos()
90 mc.LPGR(lq, lq)
91 mc.LPGR(l1, l1)
92 mc.MLGR(lr, l1)
93 mc.LGHI(r.SCRATCH, l.imm(-1))
94 # 0xff -> shift 0 -> 0xff set MSB on pos 0 to zero -> 7f
95 mc.RISBG(r.SCRATCH, r.SCRATCH, l.imm(1), l.imm(0x80 | 63), l.imm(0))
96 jmp_lq_overflow = mc.get_relative_pos()
97 mc.reserve_cond_jump() # CLGRJ lq > 0x7fff ... ff -> (label_overflow)
98 jmp_lr_overflow = mc.get_relative_pos()
99 mc.reserve_cond_jump() # CLGIJ lr > 0 -> (label_overflow)
100 jmp_neither_lqlr_overflow = mc.get_relative_pos()
101 mc.reserve_cond_jump(short=True) # BRC any -> (label_end)
102
103
104 # set overflow!
105 label_overflow = mc.get_relative_pos()
106 # set bit 34 & 35 -> indicates overflow
107 mc.XGR(r.SCRATCH, r.SCRATCH)
108 mc.OILH(r.SCRATCH, l.imm(0x3000)) # sets OF
109 mc.SPM(r.SCRATCH)
110
111 # no overflow happended
112 label_end = mc.get_relative_pos()
113
114 # patch patch patch!!!
115
116 # jmp_lq_lt_0
117 pos = jmp_lq_lt_0
118 omc = OverwritingBuilder(self.mc, pos, 1)
119 omc.CGIJ(lq, l.imm(0), c.LT, l.imm(jmp_l1_neg_lq_neg - pos))
120 omc.overwrite()
121 # jmp_l1_ge_0
122 pos = jmp_l1_ge_0
123 omc = OverwritingBuilder(self.mc, pos, 1)
124 omc.CGIJ(l1, l.imm(0), c.GE, l.imm(label_both_same_sign - pos))
125 omc.overwrite()
126 # jmp_lq_pos_l1_neg
127 pos = jmp_lq_pos_l1_neg
128 omc = OverwritingBuilder(self.mc, pos, 1)
129 omc.BRC(c.ANY, l.imm(label_xor_neg - pos))
130 omc.overwrite()
131 # jmp_l1_neg_lq_neg
132 pos = jmp_l1_neg_lq_neg
133 omc = OverwritingBuilder(self.mc, pos, 1)
134 omc.CGIJ(l1, l.imm(0), c.LT, l.imm(label_both_same_sign - pos))
135 omc.overwrite()
136
137 # patch jmp_xor_lq_overflow
138 pos = jmp_xor_lq_overflow
139 omc = OverwritingBuilder(self.mc, pos, 1)
140 omc.CLGRJ(lq, r.SCRATCH, c.GT, l.imm(label_overflow - pos))
141 omc.overwrite()
142 # patch jmp_xor_lr_overflow
143 pos = jmp_xor_lr_overflow
144 omc = OverwritingBuilder(self.mc, pos, 1)
145 omc.CLGIJ(lr, l.imm(0), c.GT, l.imm(label_overflow - pos))
146 omc.overwrite()
147 # patch jmp_no_overflow_xor_neg
148 omc = OverwritingBuilder(self.mc, jmp_no_overflow_xor_neg, 1)
149 omc.BRC(c.ANY, l.imm(label_end - jmp_no_overflow_xor_neg))
150 omc.overwrite()
151 # patch jmp_lq_overflow
152 omc = OverwritingBuilder(self.mc, jmp_lq_overflow, 1)
153 omc.CLGRJ(lq, r.SCRATCH, c.GT, l.imm(label_overflow - jmp_lq_overflow))
154 omc.overwrite()
155 # patch jmp_lr_overflow
156 omc = OverwritingBuilder(self.mc, jmp_lr_overflow, 1)
157 omc.CLGIJ(lr, l.imm(0), c.GT, l.imm(label_overflow - jmp_lr_overflow))
158 omc.overwrite()
159 # patch jmp_neither_lqlr_overflow
160 omc = OverwritingBuilder(self.mc, jmp_neither_lqlr_overflow, 1)
161 omc.BRC(c.ANY, l.imm(label_end - jmp_neither_lqlr_overflow))
162 omc.overwrite()
163
164 def emit_uint_mul_high(self, op, arglocs, regalloc):
165 r0, _, a1 = arglocs
166 # _ carries the value, contents of r0 are ignored
167 assert not r0.is_imm()
168 assert not a1.is_imm()
169 if a1.is_core_reg():
170 self.mc.MLGR(r0, a1)
171 else:
172 self.mc.MLG(r0, a1)
173
174 def emit_int_invert(self, op, arglocs, regalloc):
175 l0, = arglocs
176 assert not l0.is_imm()
177 self.mc.LGHI(r.SCRATCH, l.imm(-1))
178 self.mc.XGR(l0, r.SCRATCH)
179
180 def emit_int_neg(self, op, arglocs, regalloc):
181 l0, = arglocs
182 self.mc.LCGR(l0, l0)
183
184 def emit_int_signext(self, op, arglocs, regalloc):
185 l0, = arglocs
186 extend_from = op.getarg(1).getint()
187 if extend_from == 1:
188 self.mc.LGBR(l0, l0)
189 elif extend_from == 2:
190 self.mc.LGHR(l0, l0)
191 elif extend_from == 4:
192 self.mc.LGFR(l0, l0)
193 else:
194 raise AssertionError(extend_from)
195
196 def emit_int_force_ge_zero(self, op, arglocs, resloc):
197 l0, = arglocs
198 off = self.mc.CGIJ_byte_count + self.mc.LGHI_byte_count
199 self.mc.CGIJ(l0, l.imm(0), c.GE, l.imm(off))
200 self.mc.LGHI(l0, l.imm(0))
201
202 def emit_int_is_zero(self, op, arglocs, regalloc):
203 l0, res = arglocs
204 self.mc.CGHI(l0, l.imm(0))
205 self.flush_cc(c.EQ, res)
206
207 def emit_int_is_true(self, op, arglocs, regalloc):
208 l0, res = arglocs
209 self.mc.CGHI(l0, l.imm(0))
210 self.flush_cc(c.NE, res)
211
212 emit_int_and = gen_emit_rr_rp("NGR", "NG")
213 emit_int_or = gen_emit_rr_rp("OGR", "OG")
214 emit_int_xor = gen_emit_rr_rp("XGR", "XG")
215
216 emit_int_rshift = gen_emit_shift("SRAG")
217 emit_int_lshift = gen_emit_shift("SLLG")
218 emit_uint_rshift = gen_emit_shift("SRLG")
219
220 emit_int_le = gen_emit_cmp_op(c.LE)
221 emit_int_lt = gen_emit_cmp_op(c.LT)
222 emit_int_gt = gen_emit_cmp_op(c.GT)
223 emit_int_ge = gen_emit_cmp_op(c.GE)
224 emit_int_eq = gen_emit_cmp_op(c.EQ)
225 emit_int_ne = gen_emit_cmp_op(c.NE)
226
227 emit_ptr_eq = emit_int_eq
228 emit_ptr_ne = emit_int_ne
229
230 emit_instance_ptr_eq = emit_ptr_eq
231 emit_instance_ptr_ne = emit_ptr_ne
232
233 emit_uint_le = gen_emit_cmp_op(c.LE, signed=False)
234 emit_uint_lt = gen_emit_cmp_op(c.LT, signed=False)
235 emit_uint_gt = gen_emit_cmp_op(c.GT, signed=False)
236 emit_uint_ge = gen_emit_cmp_op(c.GE, signed=False)
237
238 class FloatOpAssembler(object):
239 _mixin_ = True
240
241 emit_float_add = gen_emit_rr_rp('ADBR', 'ADB')
242 emit_float_sub = gen_emit_rr_rp('SDBR', 'SDB')
243 emit_float_mul = gen_emit_rr_rp('MDBR', 'MDB')
244 emit_float_truediv = gen_emit_rr_rp('DDBR', 'DDB')
245
246 # Support for NaNs: S390X sets condition code to 0x3 (unordered)
247 # whenever any operand is nan.
248 # in the case float_le,float_ge the overflow bit is not set of
249 # the initial condition!
250 # e.g. guard_true(nan <= x): jumps 1100 inv => 0011, bit 3 set
251 # e.g. guard_false(nan <= x): does not jump 1100, bit 3 not set
252 # e.g. guard_true(nan >= nan): jumps 1010 inv => 0101, bit 3 set
253 emit_float_lt = gen_emit_cmp_op(c.LT, fp=True)
254 emit_float_le = gen_emit_cmp_op(c.FLE, fp=True)
255 emit_float_eq = gen_emit_cmp_op(c.EQ, fp=True)
256 emit_float_ne = gen_emit_cmp_op(c.NE, fp=True)
257 emit_float_gt = gen_emit_cmp_op(c.GT, fp=True)
258 emit_float_ge = gen_emit_cmp_op(c.FGE, fp=True)
259
260 def emit_float_neg(self, op, arglocs, regalloc):
261 l0, = arglocs
262 self.mc.LCDBR(l0, l0)
263
264 def emit_float_abs(self, op, arglocs, regalloc):
265 l0, = arglocs
266 self.mc.LPDBR(l0, l0)
267
268 def emit_cast_float_to_int(self, op, arglocs, regalloc):
269 f0, r0 = arglocs
270 self.mc.CGDBR(r0, c.FP_TOWARDS_ZERO, f0)
271
272 def emit_cast_int_to_float(self, op, arglocs, regalloc):
273 r0, f0 = arglocs
274 self.mc.CDGBR(f0, r0)
275
276 def emit_convert_float_bytes_to_longlong(self, op, arglocs, regalloc):
277 l0, res = arglocs
278 self.mc.LGDR(res, l0)
279
280 def emit_convert_longlong_bytes_to_float(self, op, arglocs, regalloc):
281 l0, res = arglocs
282 self.mc.LDGR(res, l0)
283
284 class CallOpAssembler(object):
285
286 _mixin_ = True
287
288 def _emit_call(self, op, arglocs, is_call_release_gil=False):
289 resloc = arglocs[0]
290 func_index = 1 + is_call_release_gil
291 adr = arglocs[func_index]
292 arglist = arglocs[func_index+1:]
293
294 cb = callbuilder.CallBuilder(self, adr, arglist, resloc, op.getdescr())
295
296 descr = op.getdescr()
297 assert isinstance(descr, CallDescr)
298 cb.argtypes = descr.get_arg_types()
299 cb.restype = descr.get_result_type()
300
301 if is_call_release_gil:
302 saveerrloc = arglocs[1]
303 assert saveerrloc.is_imm()
304 cb.emit_call_release_gil(saveerrloc.value)
305 else:
306 cb.emit()
307
308 def _genop_call(self, op, arglocs, regalloc):
309 oopspecindex = regalloc.get_oopspecindex(op)
310 if oopspecindex == EffectInfo.OS_MATH_SQRT:
311 return self._emit_math_sqrt(op, arglocs, regalloc)
312 if oopspecindex == EffectInfo.OS_THREADLOCALREF_GET:
313 return self._emit_threadlocalref_get(op, arglocs, regalloc)
314 self._emit_call(op, arglocs)
315
316 emit_call_i = _genop_call
317 emit_call_r = _genop_call
318 emit_call_f = _genop_call
319 emit_call_n = _genop_call
320
321 def _emit_threadlocalref_get(self, op, arglocs, regalloc):
322 [resloc] = arglocs
323 offset = op.getarg(1).getint() # getarg(0) == 'threadlocalref_get'
324 calldescr = op.getdescr()
325 size = calldescr.get_result_size()
326 sign = calldescr.is_result_signed()
327 #
328 # This loads the stack location THREADLOCAL_OFS into a
329 # register, and then read the word at the given offset.
330 # It is only supported if 'translate_support_code' is
331 # true; otherwise, the execute_token() was done with a
332 # dummy value for the stack location THREADLOCAL_OFS
333 #
334 assert self.cpu.translate_support_code
335 assert resloc.is_reg()
336 assert check_imm_value(offset)
337 self.mc.LG(resloc, l.addr(THREADLOCAL_ADDR_OFFSET, r.SP))
338 self._memory_read(resloc, l.addr(offset, resloc), size, sign)
339
340 def _emit_math_sqrt(self, op, arglocs, regalloc):
341 l0, res = arglocs
342 self.mc.SQDBR(res, l0)
343
344 def _genop_call_may_force(self, op, arglocs, regalloc):
345 self._store_force_index(self._find_nearby_operation(regalloc, +1))
346 self._emit_call(op, arglocs)
347
348 emit_call_may_force_i = _genop_call_may_force
349 emit_call_may_force_r = _genop_call_may_force
350 emit_call_may_force_f = _genop_call_may_force
351 emit_call_may_force_n = _genop_call_may_force
352
353 def _genop_call_release_gil(self, op, arglocs, regalloc):
354 self._store_force_index(self._find_nearby_operation(regalloc, +1))
355 self._emit_call(op, arglocs, is_call_release_gil=True)
356
357 emit_call_release_gil_i = _genop_call_release_gil
358 emit_call_release_gil_f = _genop_call_release_gil
359 emit_call_release_gil_n = _genop_call_release_gil
360
361 def _store_force_index(self, guard_op):
362 assert (guard_op.getopnum() == rop.GUARD_NOT_FORCED or
363 guard_op.getopnum() == rop.GUARD_NOT_FORCED_2)
364 faildescr = guard_op.getdescr()
365 ofs = self.cpu.get_ofs_of_frame_field('jf_force_descr')
366 #
367 faildescrindex = self.get_gcref_from_faildescr(faildescr)
368 self.load_gcref_into(r.SCRATCH, faildescrindex)
369 self.mc.STG(r.SCRATCH, l.addr(ofs, r.SPP))
370
371 def _find_nearby_operation(self, regalloc, delta):
372 return regalloc.operations[regalloc.rm.position + delta]
373
374 _COND_CALL_SAVE_REGS = [r.r11, r.r2, r.r3, r.r4, r.r5]
375
376 def emit_cond_call(self, op, arglocs, regalloc):
377 resloc = arglocs[0]
378 arglocs = arglocs[1:]
379
380 fcond = self.guard_success_cc
381 self.guard_success_cc = c.cond_none
382 assert fcond.value != c.cond_none.value
383
384 jmp_adr = self.mc.get_relative_pos()
385 self.mc.reserve_cond_jump() # patched later to a relative branch
386
387 # save away r2, r3, r4, r5, r11 into the jitframe
388 should_be_saved = [
389 reg for reg in self._regalloc.rm.reg_bindings.values()
390 if reg in self._COND_CALL_SAVE_REGS]
391 self._push_core_regs_to_jitframe(self.mc, should_be_saved)
392
393 self.push_gcmap(self.mc, regalloc.get_gcmap([resloc]))
394 #
395 # load the 0-to-4 arguments into these registers, with the address of
396 # the function to call into r11
397 remap_frame_layout(self, arglocs,
398 [r.r11, r.r2, r.r3, r.r4, r.r5][:len(arglocs)],
399 r.SCRATCH)
400 #
401 # figure out which variant of cond_call_slowpath to call, and call it
402 callee_only = False
403 floats = False
404 for reg in regalloc.rm.reg_bindings.values():
405 if reg not in regalloc.rm.save_around_call_regs:
406 break
407 else:
408 callee_only = True
409 if len(regalloc.fprm.reg_bindings):
410 floats = True
411 cond_call_adr = self.cond_call_slowpath[floats * 2 + callee_only]
412 self.mc.load_imm(r.r14, cond_call_adr)
413 self.mc.BASR(r.r14, r.r14)
414 # restoring the registers saved above, and doing pop_gcmap(), is left
415 # to the cond_call_slowpath helper. We never have any result value.
416 if resloc is not None:
417 self.mc.LGR(resloc, r.SCRATCH2)
418 relative_target = self.mc.currpos() - jmp_adr
419 pmc = OverwritingBuilder(self.mc, jmp_adr, 1)
420 pmc.BRCL(fcond, l.imm(relative_target))
421 pmc.overwrite()
422 # might be overridden again to skip over the following
423 # guard_no_exception too
424 self.previous_cond_call_jcond = jmp_adr, fcond
425
426 emit_cond_call_value_i = emit_cond_call
427 emit_cond_call_value_r = emit_cond_call
428
429 class AllocOpAssembler(object):
430 _mixin_ = True
431
432 def emit_check_memory_error(self, op, arglocs, regalloc):
433 self.propagate_memoryerror_if_reg_is_null(arglocs[0])
434
435 def emit_call_malloc_nursery(self, op, arglocs, regalloc):
436 # registers r.RES and r.RSZ are allocated for this call
437 size_box = op.getarg(0)
438 assert isinstance(size_box, ConstInt)
439 size = size_box.getint()
440 gc_ll_descr = self.cpu.gc_ll_descr
441 gcmap = regalloc.get_gcmap([r.RES, r.RSZ])
442 self.malloc_cond(
443 gc_ll_descr.get_nursery_free_addr(),
444 gc_ll_descr.get_nursery_top_addr(),
445 size, gcmap)
446
447 def emit_call_malloc_nursery_varsize_frame(self, op, arglocs, regalloc):
448 # registers r.RES and r.RSZ are allocated for this call
449 [sizeloc] = arglocs
450 gc_ll_descr = self.cpu.gc_ll_descr
451 gcmap = regalloc.get_gcmap([r.RES, r.RSZ])
452 self.malloc_cond_varsize_frame(
453 gc_ll_descr.get_nursery_free_addr(),
454 gc_ll_descr.get_nursery_top_addr(),
455 sizeloc, gcmap)
456
457 def emit_call_malloc_nursery_varsize(self, op, arglocs, regalloc):
458 # registers r.RES and r.RSZ are allocated for this call
459 gc_ll_descr = self.cpu.gc_ll_descr
460 if not hasattr(gc_ll_descr, 'max_size_of_young_obj'):
461 raise Exception("unreachable code")
462 # for boehm, this function should never be called
463 [lengthloc] = arglocs
464 arraydescr = op.getdescr()
465 itemsize = op.getarg(1).getint()
466 maxlength = (gc_ll_descr.max_size_of_young_obj - WORD * 2) // itemsize
467 gcmap = regalloc.get_gcmap([r.RES, r.RSZ])
468 self.malloc_cond_varsize(
469 op.getarg(0).getint(),
470 gc_ll_descr.get_nursery_free_addr(),
471 gc_ll_descr.get_nursery_top_addr(),
472 lengthloc, itemsize, maxlength, gcmap, arraydescr)
473
474 def emit_debug_merge_point(self, op, arglocs, regalloc):
475 pass
476
477 emit_jit_debug = emit_debug_merge_point
478 emit_keepalive = emit_debug_merge_point
479
480 def emit_enter_portal_frame(self, op, arglocs, regalloc):
481 self.enter_portal_frame(op)
482
483 def emit_leave_portal_frame(self, op, arglocs, regalloc):
484 self.leave_portal_frame(op)
485
486 def _write_barrier_fastpath(self, mc, descr, arglocs, regalloc, array=False,
487 is_frame=False):
488 # Write code equivalent to write_barrier() in the GC: it checks
489 # a flag in the object at arglocs[0], and if set, it calls a
490 # helper piece of assembler. The latter saves registers as needed
491 # and call the function remember_young_pointer() from the GC.
492 if we_are_translated():
493 cls = self.cpu.gc_ll_descr.has_write_barrier_class()
494 assert cls is not None and isinstance(descr, cls)
495 #
496 card_marking_mask = 0
497 mask = descr.jit_wb_if_flag_singlebyte
498 if array and descr.jit_wb_cards_set != 0:
499 # assumptions the rest of the function depends on:
500 assert (descr.jit_wb_cards_set_byteofs ==
501 descr.jit_wb_if_flag_byteofs)
502 card_marking_mask = descr.jit_wb_cards_set_singlebyte
503 #
504 loc_base = arglocs[0]
505 assert loc_base.is_reg()
506 if is_frame:
507 assert loc_base is r.SPP
508 assert check_imm_value(descr.jit_wb_if_flag_byteofs)
509 mc.LLGC(r.SCRATCH2, l.addr(descr.jit_wb_if_flag_byteofs, loc_base))
510 mc.LGR(r.SCRATCH, r.SCRATCH2)
511 mc.NILL(r.SCRATCH, l.imm(mask & 0xFF))
512
513 jz_location = mc.get_relative_pos()
514 mc.reserve_cond_jump(short=True) # patched later with 'EQ'
515
516 # for cond_call_gc_wb_array, also add another fast path:
517 # if GCFLAG_CARDS_SET, then we can just set one bit and be done
518 if card_marking_mask:
519 # GCFLAG_CARDS_SET is in the same byte, loaded in r2 already
520 mc.LGR(r.SCRATCH, r.SCRATCH2)
521 mc.NILL(r.SCRATCH, l.imm(card_marking_mask & 0xFF))
522 js_location = mc.get_relative_pos()
523 mc.reserve_cond_jump() # patched later with 'NE'
524 else:
525 js_location = 0
526
527 # Write only a CALL to the helper prepared in advance, passing it as
528 # argument the address of the structure we are writing into
529 # (the first argument to COND_CALL_GC_WB).
530 helper_num = (card_marking_mask != 0)
531 if is_frame:
532 helper_num = 4
533 elif len(regalloc.fprm.reg_bindings):
534 helper_num += 2
535 if self.wb_slowpath[helper_num] == 0: # tests only
536 assert not we_are_translated()
537 assert not is_frame
538 self.cpu.gc_ll_descr.write_barrier_descr = descr
539 self._build_wb_slowpath(card_marking_mask != 0,
540 bool(len(regalloc.fprm.reg_bindings)))
541 assert self.wb_slowpath[helper_num] != 0
542 #
543 if not is_frame:
544 mc.LGR(r.r0, loc_base) # unusual argument location
545
546 mc.load_imm(r.r14, self.wb_slowpath[helper_num])
547 mc.BASR(r.r14, r.r14)
548
549 if card_marking_mask:
550 # The helper ends again with a check of the flag in the object.
551 # So here, we can simply write again a beq, which will be
552 # taken if GCFLAG_CARDS_SET is still not set.
553 jns_location = mc.get_relative_pos()
554 mc.reserve_cond_jump(short=True)
555 #
556 # patch the 'NE' above
557 currpos = mc.currpos()
558 pmc = OverwritingBuilder(mc, js_location, 1)
559 pmc.BRCL(c.NE, l.imm(currpos - js_location))
560 pmc.overwrite()
561 #
562 # case GCFLAG_CARDS_SET: emit a few instructions to do
563 # directly the card flag setting
564 loc_index = arglocs[1]
565 if loc_index.is_reg():
566 tmp_loc = arglocs[2]
567 n = descr.jit_wb_card_page_shift
568
569 assert tmp_loc is not loc_index
570
571 # compute in tmp_loc the byte offset:
572 # tmp_loc = ~(index >> (card_page_shift + 3))
573 mc.SRLG(tmp_loc, loc_index, l.addr(n+3))
574 # invert the bits of tmp_loc
575
576 # compute in SCRATCH the index of the bit inside the byte:
577 # scratch = (index >> card_page_shift) & 7
578 # 0x80 sets zero flag. will store 0 into all not selected bits
579 mc.RISBG(r.SCRATCH, loc_index, l.imm(61), l.imm(0x80 | 63), l.imm(64-n))
580 mc.LGHI(r.SCRATCH2, l.imm(-1))
581 mc.XGR(tmp_loc, r.SCRATCH2)
582
583 # set SCRATCH2 to 1 << r1
584 mc.LGHI(r.SCRATCH2, l.imm(1))
585 mc.SLLG(r.SCRATCH2, r.SCRATCH2, l.addr(0,r.SCRATCH))
586
587 # set this bit inside the byte of interest
588 addr = l.addr(0, loc_base, tmp_loc)
589 mc.LLGC(r.SCRATCH, addr)
590 mc.OGRK(r.SCRATCH, r.SCRATCH, r.SCRATCH2)
591 mc.STCY(r.SCRATCH, addr)
592 # done
593 else:
594 byte_index = loc_index.value >> descr.jit_wb_card_page_shift
595 byte_ofs = ~(byte_index >> 3)
596 byte_val = 1 << (byte_index & 7)
597 assert check_imm_value(byte_ofs, lower_bound=-2**19, upper_bound=2**19-1)
598
599 addr = l.addr(byte_ofs, loc_base)
600 mc.LLGC(r.SCRATCH, addr)
601 mc.OILL(r.SCRATCH, l.imm(byte_val))
602 mc.STCY(r.SCRATCH, addr)
603 #
604 # patch the beq just above
605 currpos = mc.currpos()
606 pmc = OverwritingBuilder(mc, jns_location, 1)
607 pmc.BRC(c.EQ, l.imm(currpos - jns_location))
608 pmc.overwrite()
609
610 # patch the JZ above
611 currpos = mc.currpos()
612 pmc = OverwritingBuilder(mc, jz_location, 1)
613 pmc.BRC(c.EQ, l.imm(currpos - jz_location))
614 pmc.overwrite()
615
616 def emit_cond_call_gc_wb(self, op, arglocs, regalloc):
617 self._write_barrier_fastpath(self.mc, op.getdescr(), arglocs, regalloc)
618
619 def emit_cond_call_gc_wb_array(self, op, arglocs, regalloc):
620 self._write_barrier_fastpath(self.mc, op.getdescr(), arglocs, regalloc,
621 array=True)
622
623
624 class GuardOpAssembler(object):
625 _mixin_ = True
626
627 def _emit_guard(self, op, arglocs, is_guard_not_invalidated=False):
628 if is_guard_not_invalidated:
629 fcond = c.cond_none
630 else:
631 fcond = self.guard_success_cc
632 self.guard_success_cc = c.cond_none
633 assert fcond.value != c.cond_none.value
634 fcond = c.negate(fcond)
635
636 token = self.build_guard_token(op, arglocs[0].value, arglocs[1:], fcond)
637 token.pos_jump_offset = self.mc.currpos()
638 assert token.guard_not_invalidated() == is_guard_not_invalidated
639 if not is_guard_not_invalidated:
640 self.mc.reserve_guard_branch() # has to be patched later on
641 self.pending_guard_tokens.append(token)
642
643 def build_guard_token(self, op, frame_depth, arglocs, fcond):
644 descr = op.getdescr()
645 gcmap = allocate_gcmap(self, frame_depth, r.JITFRAME_FIXED_SIZE)
646 faildescrindex = self.get_gcref_from_faildescr(descr)
647 token = ZARCHGuardToken(self.cpu, gcmap, descr, op.getfailargs(),
648 arglocs, op.getopnum(), frame_depth,
649 faildescrindex, fcond)
650 #token._pool_offset = self.pool.get_descr_offset(descr)
651 return token
652
653 def emit_load_from_gc_table(self, op, arglocs, regalloc):
654 resloc, = arglocs
655 index = op.getarg(0).getint()
656 assert resloc.is_reg()
657 self.load_gcref_into(resloc, index)
658
659 def emit_guard_true(self, op, arglocs, regalloc):
660 self._emit_guard(op, arglocs)
661
662 def emit_guard_false(self, op, arglocs, regalloc):
663 self.guard_success_cc = c.negate(self.guard_success_cc)
664 self._emit_guard(op, arglocs)
665
666 def emit_guard_overflow(self, op, arglocs, regalloc):
667 self.guard_success_cc = c.OF
668 self._emit_guard(op, arglocs)
669
670 def emit_guard_no_overflow(self, op, arglocs, regalloc):
671 self.guard_success_cc = c.NO
672 self._emit_guard(op, arglocs)
673
674 def emit_guard_value(self, op, arglocs, regalloc):
675 l0 = arglocs[0]
676 l1 = arglocs[1]
677 failargs = arglocs[2:]
678
679 if l0.is_reg():
680 if l1.is_imm():
681 self.mc.cmp_op(l0, l1, imm=True)
682 else:
683 self.mc.cmp_op(l0, l1)
684 elif l0.is_fp_reg():
685 assert l1.is_fp_reg()
686 self.mc.cmp_op(l0, l1, fp=True)
687 self.guard_success_cc = c.EQ
688 self._emit_guard(op, failargs)
689
690 emit_guard_nonnull = emit_guard_true
691 emit_guard_isnull = emit_guard_false
692
693 def emit_guard_class(self, op, arglocs, regalloc):
694 self._cmp_guard_class(op, arglocs, regalloc)
695 self.guard_success_cc = c.EQ
696 self._emit_guard(op, arglocs[2:])
697
698 def emit_guard_nonnull_class(self, op, arglocs, regalloc):
699 self.mc.cmp_op(arglocs[0], l.imm(1), imm=True, signed=False)
700
701 patch_pos = self.mc.currpos()
702 self.mc.reserve_cond_jump(short=True)
703
704 self._cmp_guard_class(op, arglocs, regalloc)
705 #self.mc.CGRT(r.SCRATCH, r.SCRATCH2, c.NE)
706
707 pmc = OverwritingBuilder(self.mc, patch_pos, 1)
708 pmc.BRC(c.LT, l.imm(self.mc.currpos() - patch_pos))
709 pmc.overwrite()
710
711 self.guard_success_cc = c.EQ
712 self._emit_guard(op, arglocs[2:])
713
714 def _cmp_guard_class(self, op, locs, regalloc):
715 offset = self.cpu.vtable_offset
716 loc_ptr = locs[0]
717 loc_classptr = locs[1]
718 if offset is not None:
719 # could be one instruction shorter, but don't care because
720 # it's not this case that is commonly translated
721 self.mc.LG(r.SCRATCH, l.addr(offset, loc_ptr))
722 self.mc.load_imm(r.SCRATCH2, locs[1].value)
723 self.mc.cmp_op(r.SCRATCH, r.SCRATCH2)
724 else:
725 classptr = loc_classptr.value
726 expected_typeid = (self.cpu.gc_ll_descr
727 .get_typeid_from_classptr_if_gcremovetypeptr(classptr))
728 self._cmp_guard_gc_type(loc_ptr, expected_typeid)
729
730 def _read_typeid(self, targetreg, loc_ptr):
731 # Note that the typeid half-word is at offset 0 on a little-endian
732 # machine; it is at offset 2 or 4 on a big-endian machine.
733 assert self.cpu.supports_guard_gc_type
734 self.mc.LGF(targetreg, l.addr(4, loc_ptr))
735
736 def _cmp_guard_gc_type(self, loc_ptr, expected_typeid):
737 self._read_typeid(r.SCRATCH2, loc_ptr)
738 assert 0 <= expected_typeid <= 0x7fffffff # 4 bytes are always enough
739 # we can handle 4 byte compare immediate
740 self.mc.cmp_op(r.SCRATCH2, l.imm(expected_typeid),
741 imm=True, signed=False)
742
743 def emit_guard_gc_type(self, op, arglocs, regalloc):
744 self._cmp_guard_gc_type(arglocs[0], arglocs[1].value)
745 self.guard_success_cc = c.EQ
746 self._emit_guard(op, arglocs[2:])
747
748 def emit_guard_is_object(self, op, arglocs, regalloc):
749 assert self.cpu.supports_guard_gc_type
750 loc_object = arglocs[0]
751 # idea: read the typeid, fetch one byte of the field 'infobits' from
752 # the big typeinfo table, and check the flag 'T_IS_RPYTHON_INSTANCE'.
753 base_type_info, shift_by, sizeof_ti = (
754 self.cpu.gc_ll_descr.get_translated_info_for_typeinfo())
755 infobits_offset, IS_OBJECT_FLAG = (
756 self.cpu.gc_ll_descr.get_translated_info_for_guard_is_object())
757
758 self._read_typeid(r.SCRATCH2, loc_object)
759 self.mc.load_imm(r.SCRATCH, base_type_info + infobits_offset)
760 assert shift_by == 0
761 self.mc.AGR(r.SCRATCH, r.SCRATCH2)
762 self.mc.LLGC(r.SCRATCH2, l.addr(0, r.SCRATCH)) # cannot use r.r0 as index reg
763 self.mc.NILL(r.SCRATCH2, l.imm(IS_OBJECT_FLAG & 0xff))
764 self.guard_success_cc = c.NE
765 self._emit_guard(op, arglocs[1:])
766
767 def emit_guard_subclass(self, op, arglocs, regalloc):
768 assert self.cpu.supports_guard_gc_type
769 loc_object = arglocs[0]
770 loc_check_against_class = arglocs[1]
771 offset = self.cpu.vtable_offset
772 offset2 = self.cpu.subclassrange_min_offset
773 if offset is not None:
774 # read this field to get the vtable pointer
775 self.mc.LG(r.SCRATCH, l.addr(offset, loc_object))
776 # read the vtable's subclassrange_min field
777 assert check_imm_value(offset2)
778 self.mc.load(r.SCRATCH2, r.SCRATCH, offset2)
779 else:
780 # read the typeid
781 self._read_typeid(r.SCRATCH, loc_object)
782 # read the vtable's subclassrange_min field, as a single
783 # step with the correct offset
784 base_type_info, shift_by, sizeof_ti = (
785 self.cpu.gc_ll_descr.get_translated_info_for_typeinfo())
786 self.mc.load_imm(r.SCRATCH2, base_type_info + sizeof_ti + offset2)
787 assert shift_by == 0
788 # add index manually
789 # we cannot use r0 in l.addr(...)
790 self.mc.AGR(r.SCRATCH, r.SCRATCH2)
791 self.mc.load(r.SCRATCH2, r.SCRATCH, 0)
792 # get the two bounds to check against
793 vtable_ptr = loc_check_against_class.getint()
794 vtable_ptr = rffi.cast(rclass.CLASSTYPE, vtable_ptr)
795 check_min = vtable_ptr.subclassrange_min
796 check_max = vtable_ptr.subclassrange_max
797 assert check_max > check_min
798 check_diff = check_max - check_min - 1
799 # right now, a full PyPy uses less than 6000 numbers,
800 # so we'll assert here that it always fit inside 15 bits
801 assert 0 <= check_min <= 0x7fff
802 assert 0 <= check_diff <= 0xffff
803 # check by doing the unsigned comparison (tmp - min) < (max - min)
804 self.mc.AGHI(r.SCRATCH2, l.imm(-check_min))
805 self.mc.cmp_op(r.SCRATCH2, l.imm(check_diff), imm=True, signed=False)
806 # the guard passes if we get a result of "below or equal"
807 self.guard_success_cc = c.LE
808 self._emit_guard(op, arglocs[2:])
809
810 def emit_guard_not_invalidated(self, op, arglocs, regalloc):
811 self._emit_guard(op, arglocs, is_guard_not_invalidated=True)
812
813 def emit_guard_not_forced(self, op, arglocs, regalloc):
814 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
815 self.mc.LG(r.SCRATCH, l.addr(ofs, r.SPP))
816 self.mc.cmp_op(r.SCRATCH, l.imm(0), imm=True)
817 self.guard_success_cc = c.EQ
818 self._emit_guard(op, arglocs)
819
820 def emit_guard_not_forced_2(self, op, arglocs, regalloc):
821 guard_token = self.build_guard_token(op, arglocs[0].value, arglocs[1:],
822 c.cond_none)
823 self._finish_gcmap = guard_token.gcmap
824 self._store_force_index(op)
825 self.store_info_on_descr(0, guard_token)
826
827 def emit_guard_exception(self, op, arglocs, regalloc):
828 loc, resloc = arglocs[:2]
829 failargs = arglocs[2:]
830
831 mc = self.mc
832 mc.load_imm(r.SCRATCH, self.cpu.pos_exc_value())
833 diff = self.cpu.pos_exception() - self.cpu.pos_exc_value()
834 assert check_imm_value(diff)
835
836 mc.LG(r.SCRATCH2, l.addr(diff, r.SCRATCH))
837 mc.cmp_op(r.SCRATCH2, loc)
838 self.guard_success_cc = c.EQ
839 self._emit_guard(op, failargs)
840
841 if resloc:
842 mc.load(resloc, r.SCRATCH, 0)
843 mc.LGHI(r.SCRATCH2, l.imm(0))
844 mc.STG(r.SCRATCH2, l.addr(0, r.SCRATCH))
845 mc.STG(r.SCRATCH2, l.addr(diff, r.SCRATCH))
846
847 def emit_save_exc_class(self, op, arglocs, regalloc):
848 [resloc] = arglocs
849 diff = self.mc.load_imm_plus(r.SCRATCH, self.cpu.pos_exception())
850 self.mc.load(resloc, r.SCRATCH, diff)
851
852 def emit_save_exception(self, op, arglocs, regalloc):
853 [resloc] = arglocs
854 self._store_and_reset_exception(self.mc, resloc)
855
856 def emit_restore_exception(self, op, arglocs, regalloc):
857 self._restore_exception(self.mc, arglocs[1], arglocs[0])
858
859 def emit_guard_no_exception(self, op, arglocs, regalloc):
860 self.mc.load_imm(r.SCRATCH, self.cpu.pos_exception())
861 self.mc.LG(r.SCRATCH2, l.addr(0,r.SCRATCH))
862 self.mc.cmp_op(r.SCRATCH2, l.imm(0), imm=True)
863 self.guard_success_cc = c.EQ
864 self._emit_guard(op, arglocs)
865 # If the previous operation was a COND_CALL, overwrite its conditional
866 # jump to jump over this GUARD_NO_EXCEPTION as well, if we can
867 if self._find_nearby_operation(regalloc,-1).getopnum() == rop.COND_CALL:
868 jmp_adr, fcond = self.previous_cond_call_jcond
869 relative_target = self.mc.currpos() - jmp_adr
870 pmc = OverwritingBuilder(self.mc, jmp_adr, 1)
871 pmc.BRCL(fcond, l.imm(relative_target))
872 pmc.overwrite()
873
874
875 class MemoryOpAssembler(object):
876 _mixin_ = True
877
878 def _memory_read(self, result_loc, source_loc, size, sign):
879 # res, base_loc, ofs, size and signed are all locations
880 if size == 8:
881 if result_loc.is_fp_reg():
882 self.mc.LDY(result_loc, source_loc)
883 else:
884 self.mc.LG(result_loc, source_loc)
885 elif size == 4:
886 if sign:
887 self.mc.LGF(result_loc, source_loc)
888 else:
889 self.mc.LLGF(result_loc, source_loc)
890 elif size == 2:
891 if sign:
892 self.mc.LGH(result_loc, source_loc)
893 else:
894 self.mc.LLGH(result_loc, source_loc)
895 elif size == 1:
896 if sign:
897 self.mc.LGB(result_loc, source_loc)
898 else:
899 self.mc.LLGC(result_loc, source_loc)
900 else:
901 assert 0, "size not supported"
902
903 def _memory_store(self, value_loc, addr_loc, size):
904 if size.value == 8:
905 if value_loc.is_fp_reg():
906 self.mc.STDY(value_loc, addr_loc)
907 else:
908 self.mc.STG(value_loc, addr_loc)
909 elif size.value == 4:
910 self.mc.STY(value_loc, addr_loc)
911 elif size.value == 2:
912 self.mc.STHY(value_loc, addr_loc)
913 elif size.value == 1:
914 self.mc.STCY(value_loc, addr_loc)
915 else:
916 assert 0, "size not supported"
917
918
919 def _emit_gc_load(self, op, arglocs, regalloc):
920 result_loc, base_loc, index_loc, size_loc, sign_loc = arglocs
921 addr_loc = self._load_address(base_loc, index_loc, l.imm0)
922 self._memory_read(result_loc, addr_loc, size_loc.value, sign_loc.value)
923
924 emit_gc_load_i = _emit_gc_load
925 emit_gc_load_f = _emit_gc_load
926 emit_gc_load_r = _emit_gc_load
927
928 def _emit_gc_load_indexed(self, op, arglocs, regalloc):
929 result_loc, base_loc, index_loc, offset_loc, size_loc, sign_loc=arglocs
930 addr_loc = self._load_address(base_loc, index_loc, offset_loc)
931 self._memory_read(result_loc, addr_loc, size_loc.value, sign_loc.value)
932
933 emit_gc_load_indexed_i = _emit_gc_load_indexed
934 emit_gc_load_indexed_f = _emit_gc_load_indexed
935 emit_gc_load_indexed_r = _emit_gc_load_indexed
936
937 def emit_gc_store(self, op, arglocs, regalloc):
938 (base_loc, index_loc, value_loc, size_loc) = arglocs
939 addr_loc = self._load_address(base_loc, index_loc, l.imm0)
940 self._memory_store(value_loc, addr_loc, size_loc)
941
942 def emit_gc_store_indexed(self, op, arglocs, regalloc):
943 (base_loc, index_loc, value_loc, offset_loc, size_loc) = arglocs
944 addr_loc = self._load_address(base_loc, index_loc, offset_loc)
945 self._memory_store(value_loc, addr_loc, size_loc)
946
947 def _load_address(self, base_loc, index_loc, offset_imm):
948 assert offset_imm.is_imm()
949 offset = offset_imm.value
950 if index_loc.is_imm():
951 offset = index_loc.value + offset
952 if self._mem_offset_supported(offset):
953 addr_loc = l.addr(offset, base_loc)
954 else:
955 self.mc.load_imm(r.SCRATCH, offset)
956 addr_loc = l.addr(0, base_loc, r.SCRATCH)
957 else:
958 assert self._mem_offset_supported(offset)
959 addr_loc = l.addr(offset, base_loc, index_loc)
960 return addr_loc
961
962
963 def _mem_offset_supported(self, value):
964 return -2**19 <= value < 2**19
965
966 # ...copystrcontent logic was removed, but note that
967 # if we want to reintroduce support for that:
968 # s390x has memset directly as a hardware instruction!!
969 # 0xB8 means we might reference dst later
970 #self.mc.MVCLE(dst, src, l.addr(0xB8))
971 # NOTE this instruction can (determined by the cpu), just
972 # quit the movement any time, thus it is looped until all bytes
973 # are copied!
974 #self.mc.BRC(c.OF, l.imm(-self.mc.MVCLE_byte_count))
975
976 def emit_zero_array(self, op, arglocs, regalloc):
977 base_loc, startindex_loc, length_loc, \
978 ofs_loc, itemsize_loc = arglocs
979
980 if ofs_loc.is_imm():
981 assert check_imm_value(ofs_loc.value)
982 self.mc.AGHI(base_loc, ofs_loc)
983 else:
984 self.mc.AGR(base_loc, ofs_loc)
985 if startindex_loc.is_imm():
986 assert check_imm_value(startindex_loc.value)
987 self.mc.AGHI(base_loc, startindex_loc)
988 else:
989 self.mc.AGR(base_loc, startindex_loc)
990 assert not length_loc.is_imm()
991 # contents of r0 do not matter because r1 is zero, so
992 # no copying takes place
993 self.mc.XGR(r.r1, r.r1)
994
995 assert base_loc.is_even()
996 assert length_loc.value == base_loc.value + 1
997
998 # s390x has memset directly as a hardware instruction!!
999 # it needs 5 registers allocated
1000 # dst = rX, dst len = rX+1 (ensured by the regalloc)
1001 # src = r0, src len = r1
1002 self.mc.MVCLE(base_loc, r.r0, l.addr(0))
1003 # NOTE this instruction can (determined by the cpu), just
1004 # quit the movement any time, thus it is looped until all bytes
1005 # are copied!
1006 self.mc.BRC(c.OF, l.imm(-self.mc.MVCLE_byte_count))
1007
1008
1009 class ForceOpAssembler(object):
1010 _mixin_ = True
1011
1012 def emit_force_token(self, op, arglocs, regalloc):
1013 res_loc = arglocs[0]
1014 self.mc.LGR(res_loc, r.SPP)
1015
1016 def _genop_call_assembler(self, op, arglocs, regalloc):
1017 if len(arglocs) == 3:
1018 [result_loc, argloc, vloc] = arglocs
1019 else:
1020 [result_loc, argloc] = arglocs
1021 vloc = imm(0)
1022 self._store_force_index(self._find_nearby_operation(regalloc, +1))
1023 # 'result_loc' is either r2, f0 or None
1024 self.call_assembler(op, argloc, vloc, result_loc, r.r2)
1025 self.mc.LARL(r.POOL, l.halfword(self.pool.pool_start - self.mc.get_relative_pos()))
1026
1027 emit_call_assembler_i = _genop_call_assembler
1028 emit_call_assembler_r = _genop_call_assembler
1029 emit_call_assembler_f = _genop_call_assembler
1030 emit_call_assembler_n = _genop_call_assembler
1031
1032 imm = staticmethod(imm) # for call_assembler()
1033
1034 def _call_assembler_emit_call(self, addr, argloc, _):
1035 self.regalloc_mov(argloc, r.r2)
1036 self.mc.LG(r.r3, l.addr(THREADLOCAL_ADDR_OFFSET, r.SP))
1037
1038 cb = callbuilder.CallBuilder(self, addr, [r.r2, r.r3], r.r2, None)
1039 cb.emit()
1040
1041 def _call_assembler_emit_helper_call(self, addr, arglocs, result_loc):
1042 cb = callbuilder.CallBuilder(self, addr, arglocs, result_loc, None)
1043 cb.emit()
1044
1045 def _call_assembler_check_descr(self, value, tmploc):
1046 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
1047 self.mc.LG(r.SCRATCH, l.addr(ofs, r.r2))
1048 if check_imm_value(value):
1049 self.mc.cmp_op(r.SCRATCH, l.imm(value), imm=True)
1050 else:
1051 self.mc.load_imm(r.SCRATCH2, value)
1052 self.mc.cmp_op(r.SCRATCH, r.SCRATCH2, imm=False)
1053 jump_if_eq = self.mc.currpos()
1054 self.mc.trap() # patched later
1055 self.mc.write('\x00' * 4) # patched later
1056 return jump_if_eq
1057
1058 def _call_assembler_patch_je(self, result_loc, je_location):
1059 jump_to_done = self.mc.currpos()
1060 self.mc.trap() # patched later
1061 self.mc.write('\x00' * 4) # patched later
1062 #
1063 currpos = self.mc.currpos()
1064 pmc = OverwritingBuilder(self.mc, je_location, 1)
1065 pmc.BRCL(c.EQ, l.imm(currpos - je_location))
1066 pmc.overwrite()
1067 #
1068 return jump_to_done
1069
1070 def _call_assembler_load_result(self, op, result_loc):
1071 if op.type != VOID:
1072 # load the return value from the dead frame's value index 0
1073 kind = op.type
1074 descr = self.cpu.getarraydescr_for_frame(kind)
1075 ofs = self.cpu.unpack_arraydescr(descr)
1076 if kind == FLOAT:
1077 assert result_loc is r.f0
1078 self.mc.LD(r.f0, l.addr(ofs, r.r2))
1079 else:
1080 assert result_loc is r.r2
1081 self.mc.LG(r.r2, l.addr(ofs, r.r2))
1082
1083 def _call_assembler_patch_jmp(self, jmp_location):
1084 currpos = self.mc.currpos()
1085 pmc = OverwritingBuilder(self.mc, jmp_location, 1)
1086 pmc.BRCL(c.ANY, l.imm(currpos - jmp_location))
1087 pmc.overwrite()
1088
1089 def redirect_call_assembler(self, oldlooptoken, newlooptoken):
1090 # some minimal sanity checking
1091 old_nbargs = oldlooptoken.compiled_loop_token._debug_nbargs
1092 new_nbargs = newlooptoken.compiled_loop_token._debug_nbargs
1093 assert old_nbargs == new_nbargs
1094 oldadr = oldlooptoken._ll_function_addr
1095 target = newlooptoken._ll_function_addr
1096 # copy frame-info data
1097 baseofs = self.cpu.get_baseofs_of_frame_field()
1098 newlooptoken.compiled_loop_token.update_frame_info(
1099 oldlooptoken.compiled_loop_token, baseofs)
1100 # we overwrite the instructions at the old _ll_function_addr
1101 # to start with a JMP to the new _ll_function_addr.
1102 mc = InstrBuilder()
1103 mc.load_imm(r.SCRATCH, target)
1104 mc.BCR(c.ANY, r.SCRATCH)
1105 mc.copy_to_raw_memory(oldadr)
1106 #
1107 jl.redirect_assembler(oldlooptoken, newlooptoken, newlooptoken.number)
1108
1109
1110 class MiscOpAssembler(object):
1111 _mixin_ = True
1112
1113 def _genop_same_as(self, op, arglocs, regalloc):
1114 argloc, resloc = arglocs
1115 if argloc is not resloc:
1116 self.regalloc_mov(argloc, resloc)
1117
1118 emit_same_as_i = _genop_same_as
1119 emit_same_as_r = _genop_same_as
1120 emit_same_as_f = _genop_same_as
1121 emit_cast_ptr_to_int = _genop_same_as
1122 emit_cast_int_to_ptr = _genop_same_as
1123
1124 def emit_increment_debug_counter(self, op, arglocs, regalloc):
1125 addr, scratch = arglocs
1126 self.mc.LG(scratch, l.addr(0,addr))
1127 self.mc.AGHI(scratch, l.imm(1))
1128 self.mc.STG(scratch, l.addr(0,addr))
1129
1130 def emit_debug_merge_point(self, op, arglocs, regalloc):
1131 pass
1132
1133 emit_jit_debug = emit_debug_merge_point
1134 emit_keepalive = emit_debug_merge_point
1135
1136 def emit_enter_portal_frame(self, op, arglocs, regalloc):
1137 self.enter_portal_frame(op)
1138
1139 def emit_leave_portal_frame(self, op, arglocs, regalloc):
1140 self.leave_portal_frame(op)
1141
1142 class OpAssembler(IntOpAssembler, FloatOpAssembler,
1143 GuardOpAssembler, CallOpAssembler,
1144 AllocOpAssembler, MemoryOpAssembler,
1145 MiscOpAssembler, ForceOpAssembler):
1146 _mixin_ = True
1147