Debian Codegraph @

rpython/jit/backend/arm/opassembler.py

raw symbols SCIP
1 from __future__ import with_statement
2 from rpython.jit.backend.arm import conditions as c
3 from rpython.jit.backend.arm import registers as r
4 from rpython.jit.backend.arm import shift
5 from rpython.jit.backend.arm.arch import WORD, DOUBLE_WORD, JITFRAME_FIXED_SIZE
6 from rpython.jit.backend.arm.helper.assembler import (
7 gen_emit_op_unary_cmp,
8 gen_emit_op_ri,
9 gen_emit_cmp_op,
10 gen_emit_float_op,
11 gen_emit_float_cmp_op,
12 gen_emit_unary_float_op,
13 saved_registers)
14 from rpython.jit.backend.arm.helper.regalloc import check_imm_arg
15 from rpython.jit.backend.arm.helper.regalloc import VMEM_imm_size
16 from rpython.jit.backend.arm.codebuilder import InstrBuilder, OverwritingBuilder
17 from rpython.jit.backend.arm.jump import remap_frame_layout
18 from rpython.jit.backend.arm.regalloc import TempVar
19 from rpython.jit.backend.arm.locations import imm, RawSPStackLocation
20 from rpython.jit.backend.llsupport import symbolic
21 from rpython.jit.backend.llsupport.gcmap import allocate_gcmap
22 from rpython.jit.backend.llsupport.assembler import GuardToken, BaseAssembler
23 from rpython.jit.backend.llsupport.regalloc import get_scale
24 from rpython.jit.metainterp.history import (AbstractFailDescr, ConstInt,
25 INT, FLOAT, REF)
26 from rpython.jit.metainterp.history import TargetToken
27 from rpython.jit.metainterp.resoperation import rop
28 from rpython.rlib.objectmodel import we_are_translated
29 from rpython.rtyper.lltypesystem import rstr, rffi, lltype
30 from rpython.rtyper.annlowlevel import cast_instance_to_gcref
31 from rpython.rtyper import rclass
32 from rpython.jit.backend.arm import callbuilder
33 from rpython.rlib.rarithmetic import r_uint
34 from rpython.rlib.rjitlog import rjitlog as jl
35
36
37 class ArmGuardToken(GuardToken):
38 def __init__(self, cpu, gcmap, faildescr, failargs, fail_locs,
39 offset, guard_opnum, frame_depth, faildescrindex, fcond=c.AL):
40 GuardToken.__init__(self, cpu, gcmap, faildescr, failargs, fail_locs,
41 guard_opnum, frame_depth, faildescrindex)
42 self.fcond = fcond
43 self.offset = offset
44
45
46 class ResOpAssembler(BaseAssembler):
47
48 def emit_op_int_add(self, op, arglocs, regalloc, fcond):
49 return self.int_add_impl(op, arglocs, regalloc, fcond)
50
51 emit_op_nursery_ptr_increment = emit_op_int_add
52
53 def int_add_impl(self, op, arglocs, regalloc, fcond, flags=False):
54 l0, l1, res = arglocs
55 if flags:
56 s = 1
57 else:
58 s = 0
59 if l0.is_imm():
60 self.mc.ADD_ri(res.value, l1.value, imm=l0.value, s=s)
61 elif l1.is_imm():
62 self.mc.ADD_ri(res.value, l0.value, imm=l1.value, s=s)
63 else:
64 self.mc.ADD_rr(res.value, l0.value, l1.value, s=1)
65
66 return fcond
67
68 def emit_op_int_sub(self, op, arglocs, regalloc, fcond, flags=False):
69 return self.int_sub_impl(op, arglocs, regalloc, fcond)
70
71 def int_sub_impl(self, op, arglocs, regalloc, fcond, flags=False):
72 l0, l1, res = arglocs
73 if flags:
74 s = 1
75 else:
76 s = 0
77 if l0.is_imm():
78 value = l0.getint()
79 assert value >= 0
80 # reverse substract ftw
81 self.mc.RSB_ri(res.value, l1.value, value, s=s)
82 elif l1.is_imm():
83 value = l1.getint()
84 assert value >= 0
85 self.mc.SUB_ri(res.value, l0.value, value, s=s)
86 else:
87 self.mc.SUB_rr(res.value, l0.value, l1.value, s=s)
88
89 return fcond
90
91 def emit_op_int_mul(self, op, arglocs, regalloc, fcond):
92 reg1, reg2, res = arglocs
93 self.mc.MUL(res.value, reg1.value, reg2.value)
94 return fcond
95
96 def emit_op_uint_mul_high(self, op, arglocs, regalloc, fcond):
97 reg1, reg2, res = arglocs
98 self.mc.UMULL(r.ip.value, res.value, reg1.value, reg2.value)
99 return fcond
100
101 def emit_op_int_force_ge_zero(self, op, arglocs, regalloc, fcond):
102 arg, res = arglocs
103 self.mc.CMP_ri(arg.value, 0)
104 self.mc.MOV_ri(res.value, 0, cond=c.LT)
105 self.mc.MOV_rr(res.value, arg.value, cond=c.GE)
106 return fcond
107
108 def emit_op_int_signext(self, op, arglocs, regalloc, fcond):
109 arg, numbytes, res = arglocs
110 assert numbytes.is_imm()
111 if numbytes.value == 1:
112 self.mc.SXTB_rr(res.value, arg.value)
113 elif numbytes.value == 2:
114 self.mc.SXTH_rr(res.value, arg.value)
115 else:
116 raise AssertionError("bad number of bytes")
117 return fcond
118
119 #ref: http://blogs.arm.com/software-enablement/detecting-overflow-from-mul/
120 def emit_op_int_mul_ovf(self, op, arglocs, regalloc, fcond):
121 reg1 = arglocs[0]
122 reg2 = arglocs[1]
123 res = arglocs[2]
124 self.mc.SMULL(res.value, r.ip.value, reg1.value, reg2.value,
125 cond=fcond)
126 self.mc.CMP_rr(r.ip.value, res.value, shifttype=shift.ASR,
127 imm=31, cond=fcond)
128 self.guard_success_cc = c.EQ
129 return fcond
130
131 def emit_op_int_add_ovf(self, op, arglocs, regalloc, fcond):
132 fcond = self.int_add_impl(op, arglocs, regalloc, fcond, flags=True)
133 self.guard_success_cc = c.VC
134 return fcond
135
136 def emit_op_int_sub_ovf(self, op, arglocs, regalloc, fcond):
137 fcond = self.int_sub_impl(op, arglocs, regalloc, fcond, flags=True)
138 self.guard_success_cc = c.VC
139 return fcond
140
141 emit_op_int_and = gen_emit_op_ri('int_and', 'AND')
142 emit_op_int_or = gen_emit_op_ri('int_or', 'ORR')
143 emit_op_int_xor = gen_emit_op_ri('int_xor', 'EOR')
144 emit_op_int_lshift = gen_emit_op_ri('int_lshift', 'LSL')
145 emit_op_int_rshift = gen_emit_op_ri('int_rshift', 'ASR')
146 emit_op_uint_rshift = gen_emit_op_ri('uint_rshift', 'LSR')
147
148 emit_op_int_lt = gen_emit_cmp_op('int_lt', c.LT)
149 emit_op_int_le = gen_emit_cmp_op('int_le', c.LE)
150 emit_op_int_eq = gen_emit_cmp_op('int_eq', c.EQ)
151 emit_op_int_ne = gen_emit_cmp_op('int_ne', c.NE)
152 emit_op_int_gt = gen_emit_cmp_op('int_gt', c.GT)
153 emit_op_int_ge = gen_emit_cmp_op('int_ge', c.GE)
154
155 emit_op_uint_le = gen_emit_cmp_op('uint_le', c.LS)
156 emit_op_uint_gt = gen_emit_cmp_op('uint_gt', c.HI)
157 emit_op_uint_lt = gen_emit_cmp_op('uint_lt', c.LO)
158 emit_op_uint_ge = gen_emit_cmp_op('uint_ge', c.HS)
159
160 emit_op_ptr_eq = emit_op_instance_ptr_eq = emit_op_int_eq
161 emit_op_ptr_ne = emit_op_instance_ptr_ne = emit_op_int_ne
162
163 emit_op_int_is_true = gen_emit_op_unary_cmp('int_is_true', c.NE)
164 emit_op_int_is_zero = gen_emit_op_unary_cmp('int_is_zero', c.EQ)
165
166 def emit_op_int_invert(self, op, arglocs, regalloc, fcond):
167 reg, res = arglocs
168
169 self.mc.MVN_rr(res.value, reg.value)
170 return fcond
171
172 def emit_op_int_neg(self, op, arglocs, regalloc, fcond):
173 l0, resloc = arglocs
174 self.mc.RSB_ri(resloc.value, l0.value, imm=0)
175 return fcond
176
177 def build_guard_token(self, op, frame_depth, arglocs, offset, fcond):
178 assert isinstance(fcond, int)
179 descr = op.getdescr()
180 assert isinstance(descr, AbstractFailDescr)
181
182 gcmap = allocate_gcmap(self, frame_depth, JITFRAME_FIXED_SIZE)
183 faildescrindex = self.get_gcref_from_faildescr(descr)
184 token = ArmGuardToken(self.cpu, gcmap,
185 descr,
186 failargs=op.getfailargs(),
187 fail_locs=arglocs,
188 offset=offset,
189 guard_opnum=op.getopnum(),
190 frame_depth=frame_depth,
191 faildescrindex=faildescrindex,
192 fcond=fcond)
193 return token
194
195 def _emit_guard(self, op, arglocs, is_guard_not_invalidated=False):
196 if is_guard_not_invalidated:
197 fcond = c.cond_none
198 else:
199 fcond = self.guard_success_cc
200 self.guard_success_cc = c.cond_none
201 assert fcond != c.cond_none
202 pos = self.mc.currpos()
203 token = self.build_guard_token(op, arglocs[0].value, arglocs[1:], pos, fcond)
204 self.pending_guards.append(token)
205 assert token.guard_not_invalidated() == is_guard_not_invalidated
206 # For all guards that are not GUARD_NOT_INVALIDATED we emit a
207 # breakpoint to ensure the location is patched correctly. In the case
208 # of GUARD_NOT_INVALIDATED we use just a NOP, because it is only
209 # eventually patched at a later point.
210 if is_guard_not_invalidated:
211 self.mc.NOP()
212 else:
213 self.mc.BKPT()
214 return c.AL
215
216 def emit_op_guard_true(self, op, arglocs, regalloc, fcond):
217 fcond = self._emit_guard(op, arglocs)
218 return fcond
219
220 def emit_op_guard_false(self, op, arglocs, regalloc, fcond):
221 self.guard_success_cc = c.get_opposite_of(self.guard_success_cc)
222 fcond = self._emit_guard(op, arglocs)
223 return fcond
224
225 def emit_op_guard_value(self, op, arglocs, regalloc, fcond):
226 l0 = arglocs[0]
227 l1 = arglocs[1]
228 failargs = arglocs[2:]
229
230 if l0.is_core_reg():
231 if l1.is_imm():
232 self.mc.CMP_ri(l0.value, l1.getint())
233 else:
234 self.mc.CMP_rr(l0.value, l1.value)
235 elif l0.is_vfp_reg():
236 assert l1.is_vfp_reg()
237 self.mc.VCMP(l0.value, l1.value)
238 self.mc.VMRS(cond=fcond)
239 self.guard_success_cc = c.EQ
240 fcond = self._emit_guard(op, failargs)
241 return fcond
242
243 emit_op_guard_nonnull = emit_op_guard_true
244 emit_op_guard_isnull = emit_op_guard_false
245
246 emit_op_guard_no_overflow = emit_op_guard_true
247 emit_op_guard_overflow = emit_op_guard_false
248
249 def emit_op_guard_class(self, op, arglocs, regalloc, fcond):
250 self._cmp_guard_class(op, arglocs, regalloc, fcond)
251 self.guard_success_cc = c.EQ
252 self._emit_guard(op, arglocs[2:])
253 return fcond
254
255 def emit_op_guard_nonnull_class(self, op, arglocs, regalloc, fcond):
256 self.mc.CMP_ri(arglocs[0].value, 1)
257 self._cmp_guard_class(op, arglocs, regalloc, c.HS)
258 self.guard_success_cc = c.EQ
259 self._emit_guard(op, arglocs[2:])
260 return fcond
261
262 def _cmp_guard_class(self, op, locs, regalloc, fcond):
263 offset = self.cpu.vtable_offset
264 if offset is not None:
265 self.mc.LDR_ri(r.ip.value, locs[0].value, offset, cond=fcond)
266 self.mc.gen_load_int(r.lr.value, locs[1].value, cond=fcond)
267 self.mc.CMP_rr(r.ip.value, r.lr.value, cond=fcond)
268 else:
269 expected_typeid = (self.cpu.gc_ll_descr
270 .get_typeid_from_classptr_if_gcremovetypeptr(locs[1].value))
271 self._cmp_guard_gc_type(locs[0], expected_typeid, fcond)
272
273 def _cmp_guard_gc_type(self, loc_ptr, expected_typeid, fcond=c.AL):
274 # Note that the typeid half-word is at offset 0 on a little-endian
275 # machine; it would be at offset 2 or 4 on a big-endian machine.
276 assert self.cpu.supports_guard_gc_type
277 self.mc.LDRH_ri(r.ip.value, loc_ptr.value, cond=fcond)
278 self.mc.gen_load_int(r.lr.value, expected_typeid, cond=fcond)
279 self.mc.CMP_rr(r.ip.value, r.lr.value, cond=fcond)
280
281 def emit_op_guard_gc_type(self, op, arglocs, regalloc, fcond):
282 self._cmp_guard_gc_type(arglocs[0], arglocs[1].value, fcond)
283 self.guard_success_cc = c.EQ
284 self._emit_guard(op, arglocs[2:])
285 return fcond
286
287 def emit_op_guard_is_object(self, op, arglocs, regalloc, fcond):
288 assert self.cpu.supports_guard_gc_type
289 loc_object = arglocs[0]
290 # idea: read the typeid, fetch one byte of the field 'infobits' from
291 # the big typeinfo table, and check the flag 'T_IS_RPYTHON_INSTANCE'.
292 self.mc.LDRH_ri(r.ip.value, loc_object.value)
293 #
294 base_type_info, shift_by, sizeof_ti = (
295 self.cpu.gc_ll_descr.get_translated_info_for_typeinfo())
296 infobits_offset, IS_OBJECT_FLAG = (
297 self.cpu.gc_ll_descr.get_translated_info_for_guard_is_object())
298
299 self.mc.gen_load_int(r.lr.value, base_type_info + infobits_offset)
300 if shift_by > 0:
301 self.mc.LSL_ri(r.ip.value, r.ip.value, shift_by)
302 self.mc.LDRB_rr(r.ip.value, r.ip.value, r.lr.value)
303 self.mc.TST_ri(r.ip.value, imm=(IS_OBJECT_FLAG & 0xff))
304 self.guard_success_cc = c.NE
305 self._emit_guard(op, arglocs[1:])
306 return fcond
307
308 def emit_op_guard_subclass(self, op, arglocs, regalloc, fcond):
309 assert self.cpu.supports_guard_gc_type
310 loc_object = arglocs[0]
311 loc_check_against_class = arglocs[1]
312 offset = self.cpu.vtable_offset
313 offset2 = self.cpu.subclassrange_min_offset
314 if offset is not None:
315 # read this field to get the vtable pointer
316 self.mc.LDR_ri(r.ip.value, loc_object.value, offset)
317 # read the vtable's subclassrange_min field
318 self.mc.LDR_ri(r.ip.value, r.ip.value, offset2)
319 else:
320 # read the typeid
321 self.mc.LDRH_ri(r.ip.value, loc_object.value)
322 # read the vtable's subclassrange_min field, as a single
323 # step with the correct offset
324 base_type_info, shift_by, sizeof_ti = (
325 self.cpu.gc_ll_descr.get_translated_info_for_typeinfo())
326
327 self.mc.gen_load_int(r.lr.value,
328 base_type_info + sizeof_ti + offset2)
329 if shift_by > 0:
330 self.mc.LSL_ri(r.ip.value, r.ip.value, shift_by)
331 self.mc.LDR_rr(r.ip.value, r.ip.value, r.lr.value)
332 # get the two bounds to check against
333 vtable_ptr = loc_check_against_class.getint()
334 vtable_ptr = rffi.cast(rclass.CLASSTYPE, vtable_ptr)
335 check_min = vtable_ptr.subclassrange_min
336 check_max = vtable_ptr.subclassrange_max
337 assert check_max > check_min
338 check_diff = check_max - check_min - 1
339 # check by doing the unsigned comparison (tmp - min) < (max - min)
340 self.mc.gen_load_int(r.lr.value, check_min)
341 self.mc.SUB_rr(r.ip.value, r.ip.value, r.lr.value)
342 if check_diff <= 0xff:
343 self.mc.CMP_ri(r.ip.value, check_diff)
344 else:
345 self.mc.gen_load_int(r.lr.value, check_diff)
346 self.mc.CMP_rr(r.ip.value, r.lr.value)
347 # the guard passes if we get a result of "below or equal"
348 self.guard_success_cc = c.LS
349 self._emit_guard(op, arglocs[2:])
350 return fcond
351
352 def emit_op_guard_not_invalidated(self, op, locs, regalloc, fcond):
353 return self._emit_guard(op, locs, is_guard_not_invalidated=True)
354
355 def emit_op_label(self, op, arglocs, regalloc, fcond):
356 self._check_frame_depth_debug(self.mc)
357 return fcond
358
359 def emit_op_cond_call(self, op, arglocs, regalloc, fcond):
360 call_loc = arglocs[0]
361 if len(arglocs) == 2:
362 res_loc = arglocs[1] # cond_call_value
363 else:
364 res_loc = None # cond_call
365 # see x86.regalloc for why we skip res_loc in the gcmap
366 gcmap = regalloc.get_gcmap([res_loc])
367
368 assert call_loc is r.r4
369 jmp_adr = self.mc.currpos()
370 self.mc.BKPT() # patched later: the conditional jump
371 #
372 self.push_gcmap(self.mc, gcmap, store=True)
373 #
374 callee_only = False
375 floats = False
376 if self._regalloc is not None:
377 for reg in self._regalloc.rm.reg_bindings.values():
378 if reg not in self._regalloc.rm.save_around_call_regs:
379 break
380 else:
381 callee_only = True
382 if len(self._regalloc.vfprm.reg_bindings):
383 floats = True
384 cond_call_adr = self.cond_call_slowpath[floats * 2 + callee_only]
385 self.mc.BL(cond_call_adr)
386 # if this is a COND_CALL_VALUE, we need to move the result in place
387 # from its current location (which is, unusually, in r4: see
388 # cond_call_slowpath)
389 if res_loc is not None and res_loc is not r.r4:
390 self.mc.MOV_rr(res_loc.value, r.r4.value)
391 #
392 self.pop_gcmap(self.mc)
393 cond = c.get_opposite_of(self.guard_success_cc)
394 self.guard_success_cc = c.cond_none
395 pmc = OverwritingBuilder(self.mc, jmp_adr, WORD)
396 pmc.B_offs(self.mc.currpos(), cond)
397 # might be overridden again to skip over the following
398 # guard_no_exception too
399 self.previous_cond_call_jcond = jmp_adr, cond
400 return fcond
401
402 emit_op_cond_call_value_i = emit_op_cond_call
403 emit_op_cond_call_value_r = emit_op_cond_call
404
405 def emit_op_jump(self, op, arglocs, regalloc, fcond):
406 target_token = op.getdescr()
407 assert isinstance(target_token, TargetToken)
408 target = target_token._ll_loop_code
409 assert fcond == c.AL
410 if target_token in self.target_tokens_currently_compiling:
411 self.mc.B_offs(target, fcond)
412 else:
413 self.mc.B(target, fcond)
414 return fcond
415
416 def emit_op_finish(self, op, arglocs, regalloc, fcond):
417 base_ofs = self.cpu.get_baseofs_of_frame_field()
418 if len(arglocs) > 0:
419 [return_val] = arglocs
420 self.store_reg(self.mc, return_val, r.fp, base_ofs)
421 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
422
423 faildescrindex = self.get_gcref_from_faildescr(op.getdescr())
424 self.load_from_gc_table(r.ip.value, faildescrindex)
425 # XXX self.mov(fail_descr_loc, RawStackLoc(ofs))
426 self.store_reg(self.mc, r.ip, r.fp, ofs, helper=r.lr)
427 if op.numargs() > 0 and op.getarg(0).type == REF:
428 if self._finish_gcmap:
429 # we're returning with a guard_not_forced_2, and
430 # additionally we need to say that r0 contains
431 # a reference too:
432 self._finish_gcmap[0] |= r_uint(1)
433 gcmap = self._finish_gcmap
434 else:
435 gcmap = self.gcmap_for_finish
436 self.push_gcmap(self.mc, gcmap, store=True)
437 elif self._finish_gcmap:
438 # we're returning with a guard_not_forced_2
439 gcmap = self._finish_gcmap
440 self.push_gcmap(self.mc, gcmap, store=True)
441 else:
442 # note that the 0 here is redundant, but I would rather
443 # keep that one and kill all the others
444 ofs = self.cpu.get_ofs_of_frame_field('jf_gcmap')
445 self.mc.gen_load_int(r.ip.value, 0)
446 self.store_reg(self.mc, r.ip, r.fp, ofs)
447 self.mc.MOV_rr(r.r0.value, r.fp.value)
448 # exit function
449 self.gen_func_epilog()
450 return fcond
451
452 def _genop_call(self, op, arglocs, regalloc, fcond):
453 return self._emit_call(op, arglocs, fcond=fcond)
454 emit_op_call_i = _genop_call
455 emit_op_call_r = _genop_call
456 emit_op_call_f = _genop_call
457 emit_op_call_n = _genop_call
458
459 def _emit_call(self, op, arglocs, is_call_release_gil=False, fcond=c.AL):
460 # args = [resloc, size, sign, args...]
461 from rpython.jit.backend.llsupport.descr import CallDescr
462
463 func_index = 3 + is_call_release_gil
464 cb = callbuilder.get_callbuilder(self.cpu, self, arglocs[func_index],
465 arglocs[func_index+1:], arglocs[0])
466
467 descr = op.getdescr()
468 assert isinstance(descr, CallDescr)
469 cb.callconv = descr.get_call_conv()
470 cb.argtypes = descr.get_arg_types()
471 cb.restype = descr.get_result_type()
472 sizeloc = arglocs[1]
473 assert sizeloc.is_imm()
474 cb.ressize = sizeloc.value
475 signloc = arglocs[2]
476 assert signloc.is_imm()
477 cb.ressign = signloc.value
478
479 if is_call_release_gil:
480 saveerrloc = arglocs[3]
481 assert saveerrloc.is_imm()
482 cb.emit_call_release_gil(saveerrloc.value)
483 else:
484 effectinfo = descr.get_extra_info()
485 if effectinfo is None or effectinfo.check_can_collect():
486 cb.emit()
487 else:
488 cb.emit_no_collect()
489 return fcond
490
491 def _genop_same_as(self, op, arglocs, regalloc, fcond):
492 argloc, resloc = arglocs
493 if argloc is not resloc:
494 self.mov_loc_loc(argloc, resloc)
495 return fcond
496
497 emit_op_same_as_i = _genop_same_as
498 emit_op_same_as_r = _genop_same_as
499 emit_op_same_as_f = _genop_same_as
500 emit_op_cast_ptr_to_int = _genop_same_as
501 emit_op_cast_int_to_ptr = _genop_same_as
502
503 def emit_op_guard_no_exception(self, op, arglocs, regalloc, fcond):
504 loc = arglocs[0]
505 failargs = arglocs[1:]
506 self.mc.LDR_ri(loc.value, loc.value)
507 self.mc.CMP_ri(loc.value, 0)
508 self.guard_success_cc = c.EQ
509 fcond = self._emit_guard(op, failargs)
510 # If the previous operation was a COND_CALL, overwrite its conditional
511 # jump to jump over this GUARD_NO_EXCEPTION as well, if we can
512 if self._find_nearby_operation(-1).getopnum() == rop.COND_CALL:
513 jmp_adr, prev_cond = self.previous_cond_call_jcond
514 pmc = OverwritingBuilder(self.mc, jmp_adr, WORD)
515 pmc.B_offs(self.mc.currpos(), prev_cond)
516 return fcond
517
518 def emit_op_guard_exception(self, op, arglocs, regalloc, fcond):
519 loc, loc1, resloc, pos_exc_value, pos_exception = arglocs[:5]
520 failargs = arglocs[5:]
521 self.mc.gen_load_int(loc1.value, pos_exception.value)
522 self.mc.LDR_ri(r.ip.value, loc1.value)
523
524 self.mc.CMP_rr(r.ip.value, loc.value)
525 self.guard_success_cc = c.EQ
526 self._emit_guard(op, failargs)
527 self._store_and_reset_exception(self.mc, resloc)
528 return fcond
529
530 def emit_op_save_exc_class(self, op, arglocs, regalloc, fcond):
531 resloc = arglocs[0]
532 self.mc.gen_load_int(r.ip.value, self.cpu.pos_exception())
533 self.load_reg(self.mc, resloc, r.ip)
534 return fcond
535
536 def emit_op_save_exception(self, op, arglocs, regalloc, fcond):
537 resloc = arglocs[0]
538 self._store_and_reset_exception(self.mc, resloc)
539 return fcond
540
541 def emit_op_restore_exception(self, op, arglocs, regalloc, fcond):
542 self._restore_exception(self.mc, arglocs[1], arglocs[0])
543 return fcond
544
545 def emit_op_debug_merge_point(self, op, arglocs, regalloc, fcond):
546 return fcond
547 emit_op_jit_debug = emit_op_debug_merge_point
548 emit_op_keepalive = emit_op_debug_merge_point
549 emit_op_enter_portal_frame = emit_op_debug_merge_point
550 emit_op_leave_portal_frame = emit_op_debug_merge_point
551
552 def emit_op_cond_call_gc_wb(self, op, arglocs, regalloc, fcond):
553 self._write_barrier_fastpath(self.mc, op.getdescr(), arglocs, fcond)
554 return fcond
555
556 def emit_op_cond_call_gc_wb_array(self, op, arglocs, regalloc, fcond):
557 self._write_barrier_fastpath(self.mc, op.getdescr(), arglocs,
558 fcond, array=True)
559 return fcond
560
561 def _write_barrier_fastpath(self, mc, descr, arglocs, fcond=c.AL, array=False,
562 is_frame=False):
563 # Write code equivalent to write_barrier() in the GC: it checks
564 # a flag in the object at arglocs[0], and if set, it calls a
565 # helper piece of assembler. The latter saves registers as needed
566 # and call the function remember_young_pointer() from the GC.
567 if we_are_translated():
568 cls = self.cpu.gc_ll_descr.has_write_barrier_class()
569 assert cls is not None and isinstance(descr, cls)
570 #
571 card_marking = False
572 mask = descr.jit_wb_if_flag_singlebyte
573 if array and descr.jit_wb_cards_set != 0:
574 # assumptions the rest of the function depends on:
575 assert (descr.jit_wb_cards_set_byteofs ==
576 descr.jit_wb_if_flag_byteofs)
577 assert descr.jit_wb_cards_set_singlebyte == -0x80
578 card_marking = True
579 mask = descr.jit_wb_if_flag_singlebyte | -0x80
580 #
581 loc_base = arglocs[0]
582 if is_frame:
583 assert loc_base is r.fp
584 mc.LDRB_ri(r.ip.value, loc_base.value,
585 imm=descr.jit_wb_if_flag_byteofs)
586 mask &= 0xFF
587 mc.TST_ri(r.ip.value, imm=mask)
588 jz_location = mc.currpos()
589 mc.BKPT()
590
591 # for cond_call_gc_wb_array, also add another fast path:
592 # if GCFLAG_CARDS_SET, then we can just set one bit and be done
593 if card_marking:
594 # GCFLAG_CARDS_SET is in this byte at 0x80
595 mc.TST_ri(r.ip.value, imm=0x80)
596
597 js_location = mc.currpos()
598 mc.BKPT()
599 else:
600 js_location = 0
601
602 # Write only a CALL to the helper prepared in advance, passing it as
603 # argument the address of the structure we are writing into
604 # (the first argument to COND_CALL_GC_WB).
605 helper_num = card_marking
606 if is_frame:
607 helper_num = 4
608 elif self._regalloc is not None and len(self._regalloc.vfprm.reg_bindings):
609 helper_num += 2
610 if self.wb_slowpath[helper_num] == 0: # tests only
611 assert not we_are_translated()
612 self.cpu.gc_ll_descr.write_barrier_descr = descr
613 self._build_wb_slowpath(card_marking,
614 bool(len(self._regalloc.vfprm.reg_bindings)))
615 assert self.wb_slowpath[helper_num] != 0
616 #
617 if loc_base is not r.r0:
618 # push two registers to keep stack aligned
619 mc.PUSH([r.r0.value, loc_base.value])
620 mc.MOV_rr(r.r0.value, loc_base.value)
621 if is_frame:
622 assert loc_base is r.fp
623 mc.BL(self.wb_slowpath[helper_num])
624 if loc_base is not r.r0:
625 mc.POP([r.r0.value, loc_base.value])
626
627 if card_marking:
628 # The helper ends again with a check of the flag in the object. So
629 # here, we can simply write again a conditional jump, which will be
630 # taken if GCFLAG_CARDS_SET is still not set.
631 jns_location = mc.currpos()
632 mc.BKPT()
633 #
634 # patch the JS above
635 offset = mc.currpos()
636 pmc = OverwritingBuilder(mc, js_location, WORD)
637 pmc.B_offs(offset, c.NE) # We want to jump if the z flag isn't set
638 #
639 # case GCFLAG_CARDS_SET: emit a few instructions to do
640 # directly the card flag setting
641 loc_index = arglocs[1]
642 assert loc_index.is_core_reg()
643 # must save the register loc_index before it is mutated
644 mc.PUSH([loc_index.value])
645 tmp1 = loc_index
646 tmp2 = arglocs[-1] # the last item is a preallocated tmp
647 # lr = byteofs
648 s = 3 + descr.jit_wb_card_page_shift
649 mc.MVN_rr(r.lr.value, loc_index.value,
650 imm=s, shifttype=shift.LSR)
651
652 # tmp1 = byte_index
653 mc.MOV_ri(r.ip.value, imm=7)
654 mc.AND_rr(tmp1.value, r.ip.value, loc_index.value,
655 imm=descr.jit_wb_card_page_shift, shifttype=shift.LSR)
656
657 # set the bit
658 mc.MOV_ri(tmp2.value, imm=1)
659 mc.LDRB_rr(r.ip.value, loc_base.value, r.lr.value)
660 mc.ORR_rr_sr(r.ip.value, r.ip.value, tmp2.value,
661 tmp1.value, shifttype=shift.LSL)
662 mc.STRB_rr(r.ip.value, loc_base.value, r.lr.value)
663 # done
664 mc.POP([loc_index.value])
665 #
666 #
667 # patch the JNS above
668 offset = mc.currpos()
669 pmc = OverwritingBuilder(mc, jns_location, WORD)
670 pmc.B_offs(offset, c.EQ) # We want to jump if the z flag is set
671
672 offset = mc.currpos()
673 pmc = OverwritingBuilder(mc, jz_location, WORD)
674 pmc.B_offs(offset, c.EQ)
675 return fcond
676
677 def emit_op_gc_store(self, op, arglocs, regalloc, fcond):
678 value_loc, base_loc, ofs_loc, size_loc = arglocs
679 scale = get_scale(size_loc.value)
680 self._write_to_mem(value_loc, base_loc, ofs_loc, imm(scale), fcond)
681 return fcond
682
683 def _emit_op_gc_load(self, op, arglocs, regalloc, fcond):
684 base_loc, ofs_loc, res_loc, nsize_loc = arglocs
685 nsize = nsize_loc.value
686 signed = (nsize < 0)
687 scale = get_scale(abs(nsize))
688 self._load_from_mem(res_loc, base_loc, ofs_loc, imm(scale),
689 signed, fcond)
690 return fcond
691
692 emit_op_gc_load_i = _emit_op_gc_load
693 emit_op_gc_load_r = _emit_op_gc_load
694 emit_op_gc_load_f = _emit_op_gc_load
695
696 def emit_op_increment_debug_counter(self, op, arglocs, regalloc, fcond):
697 base_loc, value_loc = arglocs
698 self.mc.LDR_ri(value_loc.value, base_loc.value, 0, cond=fcond)
699 self.mc.ADD_ri(value_loc.value, value_loc.value, 1, cond=fcond)
700 self.mc.STR_ri(value_loc.value, base_loc.value, 0, cond=fcond)
701 return fcond
702
703 def emit_op_gc_store_indexed(self, op, arglocs, regalloc, fcond):
704 value_loc, base_loc, index_loc, size_loc, ofs_loc = arglocs
705 assert index_loc.is_core_reg()
706 # add the base offset
707 if ofs_loc.value != 0:
708 if check_imm_arg(ofs_loc.value):
709 self.mc.ADD_ri(r.ip.value, index_loc.value, imm=ofs_loc.value)
710 else:
711 # ofs_loc.value is too large for an ADD_ri
712 self.load(r.ip, ofs_loc)
713 self.mc.ADD_rr(r.ip.value, r.ip.value, index_loc.value)
714 index_loc = r.ip
715 scale = get_scale(size_loc.value)
716 self._write_to_mem(value_loc, base_loc, index_loc, imm(scale), fcond)
717 return fcond
718
719 def _write_to_mem(self, value_loc, base_loc, ofs_loc, scale, fcond=c.AL):
720 # Write a value of size '1 << scale' at the address
721 # 'base_ofs + ofs_loc'. Note that 'scale' is not used to scale
722 # the offset!
723 if scale.value == 3:
724 assert value_loc.is_vfp_reg()
725 # vstr only supports imm offsets
726 # so if the ofset is too large we add it to the base and use an
727 # offset of 0
728 if ofs_loc.is_core_reg():
729 tmploc, save = self.get_tmp_reg([value_loc, base_loc, ofs_loc])
730 assert not save
731 self.mc.ADD_rr(tmploc.value, base_loc.value, ofs_loc.value)
732 base_loc = tmploc
733 ofs_loc = imm(0)
734 else:
735 assert ofs_loc.is_imm()
736 assert ofs_loc.value % 4 == 0
737 self.mc.VSTR(value_loc.value, base_loc.value, ofs_loc.value)
738 elif scale.value == 2:
739 if ofs_loc.is_imm():
740 self.mc.STR_ri(value_loc.value, base_loc.value,
741 ofs_loc.value, cond=fcond)
742 else:
743 self.mc.STR_rr(value_loc.value, base_loc.value,
744 ofs_loc.value, cond=fcond)
745 elif scale.value == 1:
746 if ofs_loc.is_imm():
747 self.mc.STRH_ri(value_loc.value, base_loc.value,
748 ofs_loc.value, cond=fcond)
749 else:
750 self.mc.STRH_rr(value_loc.value, base_loc.value,
751 ofs_loc.value, cond=fcond)
752 elif scale.value == 0:
753 if ofs_loc.is_imm():
754 self.mc.STRB_ri(value_loc.value, base_loc.value,
755 ofs_loc.value, cond=fcond)
756 else:
757 self.mc.STRB_rr(value_loc.value, base_loc.value,
758 ofs_loc.value, cond=fcond)
759 else:
760 assert 0
761
762 def _emit_op_gc_load_indexed(self, op, arglocs, regalloc, fcond):
763 res_loc, base_loc, index_loc, nsize_loc, ofs_loc = arglocs
764 assert index_loc.is_core_reg()
765 nsize = nsize_loc.value
766 signed = (nsize < 0)
767 # add the base offset
768 if ofs_loc.value != 0:
769 if check_imm_arg(ofs_loc.value):
770 self.mc.ADD_ri(r.ip.value, index_loc.value, imm=ofs_loc.value)
771 else:
772 # ofs_loc.value is too large for an ADD_ri
773 self.load(r.ip, ofs_loc)
774 self.mc.ADD_rr(r.ip.value, r.ip.value, index_loc.value)
775 index_loc = r.ip
776 #
777 scale = get_scale(abs(nsize))
778 self._load_from_mem(res_loc, base_loc, index_loc, imm(scale),
779 signed, fcond)
780 return fcond
781
782 emit_op_gc_load_indexed_i = _emit_op_gc_load_indexed
783 emit_op_gc_load_indexed_r = _emit_op_gc_load_indexed
784 emit_op_gc_load_indexed_f = _emit_op_gc_load_indexed
785
786 def _load_from_mem(self, res_loc, base_loc, ofs_loc, scale,
787 signed=False, fcond=c.AL):
788 # Load a value of '1 << scale' bytes, from the memory location
789 # 'base_loc + ofs_loc'. Note that 'scale' is not used to scale
790 # the offset!
791 #
792 if scale.value == 3:
793 assert res_loc.is_vfp_reg()
794 # vldr only supports imm offsets
795 # if the offset is in a register we add it to the base and use a
796 # tmp reg
797 if ofs_loc.is_core_reg():
798 tmploc, save = self.get_tmp_reg([base_loc, ofs_loc])
799 assert not save
800 self.mc.ADD_rr(tmploc.value, base_loc.value, ofs_loc.value)
801 base_loc = tmploc
802 ofs_loc = imm(0)
803 else:
804 assert ofs_loc.is_imm()
805 assert ofs_loc.value % 4 == 0
806 self.mc.VLDR(res_loc.value, base_loc.value, ofs_loc.value, cond=fcond)
807 elif scale.value == 2:
808 if ofs_loc.is_imm():
809 self.mc.LDR_ri(res_loc.value, base_loc.value,
810 ofs_loc.value, cond=fcond)
811 else:
812 self.mc.LDR_rr(res_loc.value, base_loc.value,
813 ofs_loc.value, cond=fcond)
814 elif scale.value == 1:
815 if ofs_loc.is_imm():
816 if signed:
817 self.mc.LDRSH_ri(res_loc.value, base_loc.value,
818 ofs_loc.value, cond=fcond)
819 else:
820 self.mc.LDRH_ri(res_loc.value, base_loc.value,
821 ofs_loc.value, cond=fcond)
822 else:
823 if signed:
824 self.mc.LDRSH_rr(res_loc.value, base_loc.value,
825 ofs_loc.value, cond=fcond)
826 else:
827 self.mc.LDRH_rr(res_loc.value, base_loc.value,
828 ofs_loc.value, cond=fcond)
829 elif scale.value == 0:
830 if ofs_loc.is_imm():
831 if signed:
832 self.mc.LDRSB_ri(res_loc.value, base_loc.value,
833 ofs_loc.value, cond=fcond)
834 else:
835 self.mc.LDRB_ri(res_loc.value, base_loc.value,
836 ofs_loc.value, cond=fcond)
837 else:
838 if signed:
839 self.mc.LDRSB_rr(res_loc.value, base_loc.value,
840 ofs_loc.value, cond=fcond)
841 else:
842 self.mc.LDRB_rr(res_loc.value, base_loc.value,
843 ofs_loc.value, cond=fcond)
844 else:
845 assert 0
846
847 def emit_op_load_effective_address(self, op, arglocs, regalloc, fcond):
848 static_ofs = op.getarg(2).getint()
849 scale = op.getarg(3).getint()
850 self._gen_address(arglocs[2], arglocs[0], arglocs[1], scale, static_ofs)
851 return fcond
852
853 # result = base_loc + (scaled_loc << scale) + static_offset
854 def _gen_address(self, result, base_loc, scaled_loc, scale=0, static_offset=0):
855 assert scaled_loc.is_core_reg()
856 assert base_loc.is_core_reg()
857 assert check_imm_arg(scale)
858 assert check_imm_arg(static_offset)
859 if scale > 0:
860 self.mc.LSL_ri(r.ip.value, scaled_loc.value, scale)
861 scaled_loc = r.ip
862 else:
863 scaled_loc = scaled_loc
864 self.mc.ADD_rr(result.value, base_loc.value, scaled_loc.value)
865 self.mc.ADD_ri(result.value, result.value, static_offset)
866
867 def store_force_descr(self, op, fail_locs, frame_depth):
868 pos = self.mc.currpos()
869 guard_token = self.build_guard_token(op, frame_depth, fail_locs, pos, c.AL)
870 #self.pending_guards.append(guard_token)
871 self._finish_gcmap = guard_token.gcmap
872 self._store_force_index(op)
873 self.store_info_on_descr(pos, guard_token)
874
875 def emit_op_force_token(self, op, arglocs, regalloc, fcond):
876 # XXX kill me
877 res_loc = arglocs[0]
878 self.mc.MOV_rr(res_loc.value, r.fp.value)
879 return fcond
880
881 def imm(self, v):
882 return imm(v)
883
884 def _genop_call_assembler(self, op, arglocs, regalloc, fcond):
885 if len(arglocs) == 4:
886 [argloc, vloc, result_loc, tmploc] = arglocs
887 else:
888 [argloc, result_loc, tmploc] = arglocs
889 vloc = imm(0)
890 self._store_force_index(self._find_nearby_operation(+1))
891 self.call_assembler(op, argloc, vloc, result_loc, tmploc)
892 return fcond
893 emit_op_call_assembler_i = _genop_call_assembler
894 emit_op_call_assembler_r = _genop_call_assembler
895 emit_op_call_assembler_f = _genop_call_assembler
896 emit_op_call_assembler_n = _genop_call_assembler
897
898 def _call_assembler_emit_call(self, addr, argloc, resloc):
899 ofs = self.saved_threadlocal_addr
900 threadlocal_loc = RawSPStackLocation(ofs, INT)
901 self.simple_call(addr, [argloc, threadlocal_loc], result_loc=resloc)
902
903 def _call_assembler_emit_helper_call(self, addr, arglocs, resloc):
904 self.simple_call(addr, arglocs, result_loc=resloc)
905
906 def _call_assembler_check_descr(self, value, tmploc):
907 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
908 self.mc.LDR_ri(r.ip.value, tmploc.value, imm=ofs)
909 if check_imm_arg(value):
910 self.mc.CMP_ri(r.ip.value, imm=value)
911 else:
912 self.mc.gen_load_int(r.lr.value, value)
913 self.mc.CMP_rr(r.ip.value, r.lr.value)
914 pos = self.mc.currpos()
915 self.mc.BKPT()
916 return pos
917
918 def _call_assembler_patch_je(self, result_loc, jmp_location):
919 pos = self.mc.currpos()
920 self.mc.BKPT()
921 #
922 pmc = OverwritingBuilder(self.mc, jmp_location, WORD)
923 pmc.B_offs(self.mc.currpos(), c.EQ)
924 return pos
925
926 def _call_assembler_load_result(self, op, result_loc):
927 if op.type != 'v':
928 # load the return value from (tmploc, 0)
929 kind = op.type
930 descr = self.cpu.getarraydescr_for_frame(kind)
931 if kind == FLOAT:
932 ofs = self.cpu.unpack_arraydescr(descr)
933 assert check_imm_arg(ofs)
934 assert result_loc.is_vfp_reg()
935 # we always have a register here, since we have to sync them
936 # before call_assembler
937 self.load_reg(self.mc, result_loc, r.r0, ofs=ofs)
938 else:
939 assert result_loc is r.r0
940 ofs = self.cpu.unpack_arraydescr(descr)
941 assert check_imm_arg(ofs)
942 self.mc.LDR_ri(result_loc.value, result_loc.value, imm=ofs)
943
944 def _call_assembler_patch_jmp(self, jmp_location):
945 # merge point
946 currpos = self.mc.currpos()
947 pmc = OverwritingBuilder(self.mc, jmp_location, WORD)
948 pmc.B_offs(currpos)
949
950 # ../x86/assembler.py:668
951 def redirect_call_assembler(self, oldlooptoken, newlooptoken):
952 # some minimal sanity checking
953 old_nbargs = oldlooptoken.compiled_loop_token._debug_nbargs
954 new_nbargs = newlooptoken.compiled_loop_token._debug_nbargs
955 assert old_nbargs == new_nbargs
956 # we overwrite the instructions at the old _ll_function_addr
957 # to start with a JMP to the new _ll_function_addr.
958 # Ideally we should rather patch all existing CALLs, but well.
959 oldadr = oldlooptoken._ll_function_addr
960 target = newlooptoken._ll_function_addr
961 # copy frame-info data
962 baseofs = self.cpu.get_baseofs_of_frame_field()
963 newlooptoken.compiled_loop_token.update_frame_info(
964 oldlooptoken.compiled_loop_token, baseofs)
965 mc = InstrBuilder(self.cpu.cpuinfo.arch_version)
966 mc.B(target)
967 mc.copy_to_raw_memory(oldadr)
968 #
969 jl.redirect_assembler(oldlooptoken, newlooptoken, newlooptoken.number)
970
971 def emit_op_guard_not_forced(self, op, arglocs, regalloc, fcond):
972 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
973 self.mc.LDR_ri(r.ip.value, r.fp.value, imm=ofs)
974 self.mc.CMP_ri(r.ip.value, 0)
975 self.guard_success_cc = c.EQ
976 self._emit_guard(op, arglocs)
977 return fcond
978
979 def _genop_call_may_force(self, op, arglocs, regalloc, fcond):
980 self._store_force_index(self._find_nearby_operation(+1))
981 self._emit_call(op, arglocs, fcond=fcond)
982 return fcond
983 emit_op_call_may_force_i = _genop_call_may_force
984 emit_op_call_may_force_r = _genop_call_may_force
985 emit_op_call_may_force_f = _genop_call_may_force
986 emit_op_call_may_force_n = _genop_call_may_force
987
988 def _genop_call_release_gil(self, op, arglocs, regalloc, fcond):
989 self._store_force_index(self._find_nearby_operation(+1))
990 self._emit_call(op, arglocs, is_call_release_gil=True)
991 return fcond
992 emit_op_call_release_gil_i = _genop_call_release_gil
993 emit_op_call_release_gil_f = _genop_call_release_gil
994 emit_op_call_release_gil_n = _genop_call_release_gil
995
996 def _store_force_index(self, guard_op):
997 assert (guard_op.getopnum() == rop.GUARD_NOT_FORCED or
998 guard_op.getopnum() == rop.GUARD_NOT_FORCED_2)
999 faildescr = guard_op.getdescr()
1000 faildescrindex = self.get_gcref_from_faildescr(faildescr)
1001 ofs = self.cpu.get_ofs_of_frame_field('jf_force_descr')
1002 self.load_from_gc_table(r.ip.value, faildescrindex)
1003 self.store_reg(self.mc, r.ip, r.fp, ofs)
1004
1005 def _find_nearby_operation(self, delta):
1006 regalloc = self._regalloc
1007 return regalloc.operations[regalloc.rm.position + delta]
1008
1009 def emit_op_check_memory_error(self, op, arglocs, regalloc, fcond):
1010 self.propagate_memoryerror_if_reg_is_null(arglocs[0])
1011 self._alignment_check()
1012 return fcond
1013
1014 def _alignment_check(self):
1015 if not self.debug:
1016 return
1017 self.mc.MOV_rr(r.ip.value, r.r0.value)
1018 self.mc.AND_ri(r.ip.value, r.ip.value, 3)
1019 self.mc.CMP_ri(r.ip.value, 0)
1020 self.mc.MOV_rr(r.pc.value, r.pc.value, cond=c.EQ)
1021 self.mc.BKPT()
1022 self.mc.NOP()
1023
1024 emit_op_float_add = gen_emit_float_op('float_add', 'VADD')
1025 emit_op_float_sub = gen_emit_float_op('float_sub', 'VSUB')
1026 emit_op_float_mul = gen_emit_float_op('float_mul', 'VMUL')
1027 emit_op_float_truediv = gen_emit_float_op('float_truediv', 'VDIV')
1028
1029 emit_op_float_neg = gen_emit_unary_float_op('float_neg', 'VNEG')
1030 emit_op_float_abs = gen_emit_unary_float_op('float_abs', 'VABS')
1031 emit_opx_math_sqrt = gen_emit_unary_float_op('math_sqrt', 'VSQRT')
1032
1033 emit_op_float_lt = gen_emit_float_cmp_op('float_lt', c.VFP_LT)
1034 emit_op_float_le = gen_emit_float_cmp_op('float_le', c.VFP_LE)
1035 emit_op_float_eq = gen_emit_float_cmp_op('float_eq', c.EQ)
1036 emit_op_float_ne = gen_emit_float_cmp_op('float_ne', c.NE)
1037 emit_op_float_gt = gen_emit_float_cmp_op('float_gt', c.GT)
1038 emit_op_float_ge = gen_emit_float_cmp_op('float_ge', c.GE)
1039
1040 def emit_op_cast_float_to_int(self, op, arglocs, regalloc, fcond):
1041 arg, res = arglocs
1042 assert arg.is_vfp_reg()
1043 assert res.is_core_reg()
1044 self.mc.VCVT_float_to_int(r.svfp_ip.value, arg.value)
1045 self.mc.VMOV_sc(res.value, r.svfp_ip.value)
1046 return fcond
1047
1048 def emit_op_cast_int_to_float(self, op, arglocs, regalloc, fcond):
1049 arg, res = arglocs
1050 assert res.is_vfp_reg()
1051 assert arg.is_core_reg()
1052 self.mc.VMOV_cs(r.svfp_ip.value, arg.value)
1053 self.mc.VCVT_int_to_float(res.value, r.svfp_ip.value)
1054 return fcond
1055
1056 # the following five instructions are only ARMv7 with NEON;
1057 # regalloc.py won't call them at all, in other cases
1058 emit_opx_llong_add = gen_emit_float_op('llong_add', 'VADD_i64')
1059 emit_opx_llong_sub = gen_emit_float_op('llong_sub', 'VSUB_i64')
1060 emit_opx_llong_and = gen_emit_float_op('llong_and', 'VAND_i64')
1061 emit_opx_llong_or = gen_emit_float_op('llong_or', 'VORR_i64')
1062 emit_opx_llong_xor = gen_emit_float_op('llong_xor', 'VEOR_i64')
1063
1064 def emit_opx_llong_to_int(self, op, arglocs, regalloc, fcond):
1065 loc = arglocs[0]
1066 res = arglocs[1]
1067 assert loc.is_vfp_reg()
1068 assert res.is_core_reg()
1069 self.mc.VMOV_rc(res.value, r.ip.value, loc.value)
1070 return fcond
1071
1072 emit_op_convert_float_bytes_to_longlong = gen_emit_unary_float_op(
1073 'float_bytes_to_longlong', 'VMOV_cc')
1074 emit_op_convert_longlong_bytes_to_float = gen_emit_unary_float_op(
1075 'longlong_bytes_to_float', 'VMOV_cc')
1076
1077 """ disabled: missing an implementation that works in user mode
1078 def ..._read_timestamp(...):
1079 tmp = arglocs[0]
1080 res = arglocs[1]
1081 self.mc.MRC(15, 0, tmp.value, 15, 12, 1)
1082 self.mc.MOV_ri(r.ip.value, 0)
1083 self.mc.VMOV_cr(res.value, tmp.value, r.ip.value)
1084 return fcond
1085 """
1086
1087 def emit_op_cast_float_to_singlefloat(self, op, arglocs, regalloc, fcond):
1088 arg, res = arglocs
1089 assert arg.is_vfp_reg()
1090 assert res.is_core_reg()
1091 self.mc.VCVT_f64_f32(r.svfp_ip.value, arg.value)
1092 self.mc.VMOV_sc(res.value, r.svfp_ip.value)
1093 return fcond
1094
1095 def emit_op_cast_singlefloat_to_float(self, op, arglocs, regalloc, fcond):
1096 arg, res = arglocs
1097 assert res.is_vfp_reg()
1098 assert arg.is_core_reg()
1099 self.mc.VMOV_cs(r.svfp_ip.value, arg.value)
1100 self.mc.VCVT_f32_f64(res.value, r.svfp_ip.value)
1101 return fcond
1102
1103 #from ../x86/regalloc.py:1388
1104 def emit_op_zero_array(self, op, arglocs, regalloc, fcond):
1105 from rpython.jit.backend.llsupport.descr import unpack_arraydescr
1106 assert len(arglocs) == 0
1107 size_box = op.getarg(2)
1108 if isinstance(size_box, ConstInt) and size_box.getint() == 0:
1109 return fcond # nothing to do
1110 itemsize, baseofs, _ = unpack_arraydescr(op.getdescr())
1111 args = op.getarglist()
1112 #
1113 # ZERO_ARRAY(base_loc, start, size, 1, 1)
1114 # 'start' and 'size' are both expressed in bytes,
1115 # and the two scaling arguments should always be ConstInt(1) on ARM.
1116 assert args[3].getint() == 1
1117 assert args[4].getint() == 1
1118 #
1119 base_loc = regalloc.rm.make_sure_var_in_reg(args[0], args)
1120 startbyte_box = args[1]
1121 if isinstance(startbyte_box, ConstInt):
1122 startbyte_loc = None
1123 startbyte = startbyte_box.getint()
1124 assert startbyte >= 0
1125 else:
1126 startbyte_loc = regalloc.rm.make_sure_var_in_reg(startbyte_box,
1127 args)
1128 startbyte = -1
1129
1130 # base_loc and startbyte_loc are in two regs here (or startbyte_loc
1131 # is an immediate). Compute the dstaddr_loc, which is the raw
1132 # address that we will pass as first argument to memset().
1133 # It can be in the same register as either one, but not in
1134 # args[2], because we're still needing the latter.
1135 dstaddr_box = TempVar()
1136 dstaddr_loc = regalloc.rm.force_allocate_reg(dstaddr_box, [args[2]])
1137 if startbyte >= 0: # a constant
1138 ofs = baseofs + startbyte
1139 reg = base_loc.value
1140 else:
1141 self.mc.ADD_rr(dstaddr_loc.value,
1142 base_loc.value, startbyte_loc.value)
1143 ofs = baseofs
1144 reg = dstaddr_loc.value
1145 if check_imm_arg(ofs):
1146 self.mc.ADD_ri(dstaddr_loc.value, reg, imm=ofs)
1147 else:
1148 self.mc.gen_load_int(r.ip.value, ofs)
1149 self.mc.ADD_rr(dstaddr_loc.value, reg, r.ip.value)
1150
1151 # We use STRB, STRH or STR based on whether we know the array
1152 # item size is a multiple of 1, 2 or 4.
1153 if itemsize & 1: itemsize = 1
1154 elif itemsize & 2: itemsize = 2
1155 else: itemsize = 4
1156 limit = itemsize
1157 next_group = -1
1158 if itemsize < 4 and startbyte >= 0:
1159 # we optimize STRB/STRH into STR, but this needs care:
1160 # it only works if startindex_loc is a constant, otherwise
1161 # we'd be doing unaligned accesses.
1162 next_group = (-startbyte) & 3
1163 limit = 4
1164
1165 if (isinstance(size_box, ConstInt) and
1166 size_box.getint() <= 14 * limit): # same limit as GCC
1167 # Inline a series of STR operations, starting at 'dstaddr_loc'.
1168 #
1169 self.mc.gen_load_int(r.ip.value, 0)
1170 i = 0
1171 total_size = size_box.getint()
1172 while i < total_size:
1173 sz = itemsize
1174 if i == next_group:
1175 next_group += 4
1176 if next_group <= total_size:
1177 sz = 4
1178 if sz == 4:
1179 self.mc.STR_ri(r.ip.value, dstaddr_loc.value, imm=i)
1180 elif sz == 2:
1181 self.mc.STRH_ri(r.ip.value, dstaddr_loc.value, imm=i)
1182 else:
1183 self.mc.STRB_ri(r.ip.value, dstaddr_loc.value, imm=i)
1184 i += sz
1185
1186 else:
1187 if isinstance(size_box, ConstInt):
1188 size_loc = imm(size_box.getint())
1189 else:
1190 # load size_loc in a register different than dstaddr_loc
1191 size_loc = regalloc.rm.make_sure_var_in_reg(size_box,
1192 [dstaddr_box])
1193 #
1194 # call memset()
1195 regalloc.before_call()
1196 self.simple_call_no_collect(imm(self.memset_addr),
1197 [dstaddr_loc, imm(0), size_loc])
1198 regalloc.rm.possibly_free_var(size_box)
1199 regalloc.rm.possibly_free_var(dstaddr_box)
1200 return fcond
1201
1202 def emit_opx_threadlocalref_get(self, op, arglocs, regalloc, fcond):
1203 ofs_loc, size_loc, sign_loc, res_loc = arglocs
1204 assert ofs_loc.is_imm()
1205 assert size_loc.is_imm()
1206 assert sign_loc.is_imm()
1207 ofs = self.saved_threadlocal_addr
1208 self.load_reg(self.mc, res_loc, r.sp, ofs)
1209 scale = get_scale(size_loc.value)
1210 signed = (sign_loc.value != 0)
1211 self._load_from_mem(res_loc, res_loc, ofs_loc, imm(scale), signed,
1212 fcond)
1213 return fcond
1214
1215 def emit_op_load_from_gc_table(self, op, arglocs, regalloc, fcond):
1216 res_loc, = arglocs
1217 index = op.getarg(0).getint()
1218 self.load_from_gc_table(res_loc.value, index)
1219 return fcond