Debian Codegraph @

rpython/jit/backend/arm/callbuilder.py

raw symbols SCIP
1 from rpython.rlib.clibffi import FFI_DEFAULT_ABI
2 from rpython.rlib.objectmodel import we_are_translated
3 from rpython.jit.metainterp.history import INT, FLOAT, REF
4 from rpython.jit.backend.arm.arch import WORD
5 from rpython.jit.backend.arm import registers as r
6 from rpython.jit.backend.arm import conditions as c
7 from rpython.jit.backend.arm.locations import RawSPStackLocation
8 from rpython.jit.backend.arm.jump import remap_frame_layout
9 from rpython.jit.backend.llsupport.callbuilder import AbstractCallBuilder
10 from rpython.jit.backend.arm.helper.assembler import count_reg_args
11 from rpython.jit.backend.arm.helper.assembler import saved_registers
12 from rpython.jit.backend.arm.helper.regalloc import check_imm_arg
13 from rpython.jit.backend.arm.codebuilder import OverwritingBuilder
14 from rpython.jit.backend.llsupport import llerrno
15 from rpython.rtyper.lltypesystem import rffi
16
17
18 class ARMCallbuilder(AbstractCallBuilder):
19 def __init__(self, assembler, fnloc, arglocs,
20 resloc=r.r0, restype=INT, ressize=WORD, ressigned=True):
21 AbstractCallBuilder.__init__(self, assembler, fnloc, arglocs,
22 resloc, restype, ressize)
23 self.current_sp = 0
24
25 def push_gcmap(self):
26 assert not self.is_call_release_gil
27 # we push *now* the gcmap, describing the status of GC registers
28 # after the rearrangements done just above, ignoring the return
29 # value eax, if necessary
30 noregs = self.asm.cpu.gc_ll_descr.is_shadow_stack()
31 gcmap = self.asm._regalloc.get_gcmap([r.r0], noregs=noregs)
32 self.asm.push_gcmap(self.mc, gcmap, store=True)
33
34 def pop_gcmap(self):
35 self.asm._reload_frame_if_necessary(self.mc)
36 self.asm.pop_gcmap(self.mc)
37
38 def emit_raw_call(self):
39 #the actual call
40 if self.fnloc.is_imm():
41 self.mc.BL(self.fnloc.value)
42 return
43 # --self.fnloc.is_stack() is always remapped to r4 here
44 assert self.fnloc.is_core_reg()
45 self.mc.BLX(self.fnloc.value)
46
47 def restore_stack_pointer(self):
48 # readjust the sp in case we passed some args on the stack
49 assert self.current_sp % 8 == 0 # sanity check
50 if self.current_sp != 0:
51 self._adjust_sp(self.current_sp)
52 self.current_sp = 0
53
54 def _push_stack_args(self, stack_args, on_stack):
55 assert on_stack % 8 == 0
56 if on_stack == 0:
57 return
58 self._adjust_sp(-on_stack)
59 self.current_sp = on_stack
60 ofs = 0
61 for i, arg in enumerate(stack_args):
62 if arg is not None:
63 sp_loc = RawSPStackLocation(ofs, arg.type)
64 self.asm.regalloc_mov(arg, sp_loc)
65 ofs += sp_loc.width
66 else: # alignment word
67 ofs += WORD
68
69 def _adjust_sp(self, n):
70 # adjust the current stack pointer by n bytes
71 if n > 0:
72 if check_imm_arg(n):
73 self.mc.ADD_ri(r.sp.value, r.sp.value, n)
74 else:
75 self.mc.gen_load_int(r.ip.value, n)
76 self.mc.ADD_rr(r.sp.value, r.sp.value, r.ip.value)
77 elif n < 0:
78 n = abs(n)
79 if check_imm_arg(n):
80 self.mc.SUB_ri(r.sp.value, r.sp.value, n)
81 else:
82 self.mc.gen_load_int(r.ip.value, n)
83 self.mc.SUB_rr(r.sp.value, r.sp.value, r.ip.value)
84
85 def call_releasegil_addr_and_move_real_arguments(self, fastgil):
86 assert self.is_call_release_gil
87 assert not self.asm._is_asmgcc()
88
89 # Save this thread's shadowstack pointer into r7, for later comparison
90 gcrootmap = self.asm.cpu.gc_ll_descr.gcrootmap
91 if gcrootmap:
92 rst = gcrootmap.get_root_stack_top_addr()
93 self.mc.gen_load_int(r.r5.value, rst)
94 self.mc.LDR_ri(r.r7.value, r.r5.value)
95
96 # change 'rpy_fastgil' to 0 (it should be non-zero right now)
97 if self.asm.cpu.cpuinfo.arch_version >= 7:
98 self.mc.DMB()
99 self.mc.gen_load_int(r.r6.value, fastgil)
100 self.mc.LDR_ri(r.r8.value, r.r6.value) # => our thread ident
101 self.mc.MOV_ri(r.ip.value, 0)
102 self.mc.STR_ri(r.ip.value, r.r6.value)
103
104 if not we_are_translated(): # for testing: we should not access
105 self.mc.ADD_ri(r.fp.value, r.fp.value, 1) # fp any more
106
107 def move_real_result_and_call_reacqgil_addr(self, fastgil):
108 # try to reacquire the lock. The registers r5 to r7 are still
109 # valid from before the call:
110 # r5 == &root_stack_top
111 # r6 == fastgil
112 # r7 == previous value of root_stack_top
113 # r8 == our thread ident
114 self.mc.LDREX(r.r3.value, r.r6.value) # load the lock value
115 self.mc.CMP_ri(r.r3.value, 0) # is the lock free?
116 self.mc.STREX(r.r3.value, r.r8.value, r.r6.value, c=c.EQ)
117 # try to claim the lock
118 self.mc.CMP_ri(r.r3.value, 0, cond=c.EQ) # did this succeed?
119 if self.asm.cpu.cpuinfo.arch_version >= 7:
120 self.mc.DMB()
121 # the success of the lock acquisition is defined by
122 # 'EQ is true', or equivalently by 'r3 == 0'.
123 #
124 if self.asm.cpu.gc_ll_descr.gcrootmap:
125 # When doing a call_release_gil with shadowstack, there
126 # is the risk that the 'rpy_fastgil' was free but the
127 # current shadowstack can be the one of a different
128 # thread. So here we check if the shadowstack pointer
129 # is still the same as before we released the GIL (saved
130 # in 'r7'), and if not, we fall back to 'reacqgil_addr'.
131 self.mc.LDR_ri(r.ip.value, r.r5.value, cond=c.EQ)
132 self.mc.CMP_rr(r.ip.value, r.r7.value, cond=c.EQ)
133 b1_location = self.mc.currpos()
134 self.mc.BKPT() # BEQ below
135 # there are two cases here: either EQ was false from
136 # the beginning, or EQ was true at first but the CMP
137 # made it false. In the second case we need to
138 # release the fastgil here. We know which case it is
139 # by checking again r3.
140 self.mc.CMP_ri(r.r3.value, 0)
141 self.mc.STR_ri(r.r3.value, r.r6.value, cond=c.EQ)
142 else:
143 b1_location = self.mc.currpos()
144 self.mc.BKPT() # BEQ below
145 #
146 # save the result we just got
147 gpr_to_save, vfp_to_save = self.get_result_locs()
148 with saved_registers(self.mc, gpr_to_save, vfp_to_save):
149 self.mc.BL(self.asm.reacqgil_addr)
150
151 # replace b1_location with B(here, c.EQ)
152 pmc = OverwritingBuilder(self.mc, b1_location, WORD)
153 pmc.B_offs(self.mc.currpos(), c.EQ)
154
155 if not we_are_translated(): # for testing: now we can access
156 self.mc.SUB_ri(r.fp.value, r.fp.value, 1) # fp again
157
158 def get_result_locs(self):
159 raise NotImplementedError
160
161 def _ensure_result_bit_extension(self, resloc, size, signed):
162 if size == 4:
163 return
164 if size == 1:
165 if not signed: # unsigned char
166 self.mc.AND_ri(resloc.value, resloc.value, 0xFF)
167 else:
168 self.mc.LSL_ri(resloc.value, resloc.value, 24)
169 self.mc.ASR_ri(resloc.value, resloc.value, 24)
170 elif size == 2:
171 if not signed:
172 self.mc.LSL_ri(resloc.value, resloc.value, 16)
173 self.mc.LSR_ri(resloc.value, resloc.value, 16)
174 else:
175 self.mc.LSL_ri(resloc.value, resloc.value, 16)
176 self.mc.ASR_ri(resloc.value, resloc.value, 16)
177
178 def write_real_errno(self, save_err):
179 if save_err & rffi.RFFI_READSAVED_ERRNO:
180 # Just before a call, read '*_errno' and write it into the
181 # real 'errno'. The r0-r3 registers contain arguments to the
182 # future call; the r5-r8 registers contain various stuff.
183 # We still have r9-r12.
184 if save_err & rffi.RFFI_ALT_ERRNO:
185 rpy_errno = llerrno.get_alt_errno_offset(self.asm.cpu)
186 else:
187 rpy_errno = llerrno.get_rpy_errno_offset(self.asm.cpu)
188 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
189 self.mc.LDR_ri(r.r9.value, r.sp.value,
190 self.asm.saved_threadlocal_addr + self.current_sp)
191 self.mc.LDR_ri(r.ip.value, r.r9.value, p_errno)
192 self.mc.LDR_ri(r.r9.value, r.r9.value, rpy_errno)
193 self.mc.STR_ri(r.r9.value, r.ip.value)
194 elif save_err & rffi.RFFI_ZERO_ERRNO_BEFORE:
195 # Same, but write zero.
196 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
197 self.mc.LDR_ri(r.r9.value, r.sp.value,
198 self.asm.saved_threadlocal_addr + self.current_sp)
199 self.mc.LDR_ri(r.ip.value, r.r9.value, p_errno)
200 self.mc.MOV_ri(r.r9.value, 0)
201 self.mc.STR_ri(r.r9.value, r.ip.value)
202
203 def read_real_errno(self, save_err):
204 if save_err & rffi.RFFI_SAVE_ERRNO:
205 # Just after a call, read the real 'errno' and save a copy of
206 # it inside our thread-local '*_errno'. Registers r9-r12
207 # are unused here, and registers r2-r3 never contain anything
208 # after the call.
209 if save_err & rffi.RFFI_ALT_ERRNO:
210 rpy_errno = llerrno.get_alt_errno_offset(self.asm.cpu)
211 else:
212 rpy_errno = llerrno.get_rpy_errno_offset(self.asm.cpu)
213 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
214 self.mc.LDR_ri(r.r3.value, r.sp.value,
215 self.asm.saved_threadlocal_addr)
216 self.mc.LDR_ri(r.ip.value, r.r3.value, p_errno)
217 self.mc.LDR_ri(r.ip.value, r.ip.value, 0)
218 self.mc.STR_ri(r.ip.value, r.r3.value, rpy_errno)
219
220
221 class SoftFloatCallBuilder(ARMCallbuilder):
222 # XXX Maybe we could kill this class and unify the remaining two
223 # XXX classes, by carefully checking if all methods here are doing
224 # XXX the exact same thing as the methods from HardFloatCallBuilder,
225 # XXX but simply forcing all BoxFloat arguments to be longlongs
226 # XXX (i.e. ignoring 'f' in favour of 'L'), and the same with
227 # XXX single-float arguments (ignoring 'S' in favour of 'i');
228 # XXX and the same for the return value.
229
230 def get_result_locs(self):
231 if self.resloc is None:
232 return [], []
233 if self.resloc.is_vfp_reg():
234 return [r.r0, r.r1], []
235 assert self.resloc.is_core_reg()
236 return [r.r0], []
237
238 def load_result(self):
239 # ensure the result is wellformed and stored in the correct location
240 resloc = self.resloc
241 if resloc is None:
242 return
243 if resloc.is_vfp_reg():
244 # move result to the allocated register
245 self.asm.mov_to_vfp_loc(r.r0, r.r1, resloc)
246 elif resloc.is_core_reg():
247 # move result to the allocated register
248 if resloc is not r.r0:
249 self.asm.mov_loc_loc(r.r0, resloc)
250 self._ensure_result_bit_extension(resloc,
251 self.ressize, self.ressign)
252
253
254 def _collect_and_push_stack_args(self, arglocs):
255 n_args = len(arglocs)
256 reg_args = count_reg_args(arglocs)
257 # all arguments past the 4th go on the stack
258 # first we need to prepare the list so it stays aligned
259 stack_args = []
260 count = 0
261 on_stack = 0
262 if n_args > reg_args:
263 for i in range(reg_args, n_args):
264 arg = arglocs[i]
265 if arg.type != FLOAT:
266 count += 1
267 on_stack += 1
268 else:
269 on_stack += 2
270 if count % 2 != 0:
271 stack_args.append(None)
272 count = 0
273 on_stack += 1
274 stack_args.append(arg)
275 if count % 2 != 0:
276 on_stack += 1
277 stack_args.append(None)
278 if on_stack > 0:
279 self._push_stack_args(stack_args, on_stack*WORD)
280
281 def prepare_arguments(self):
282 arglocs = self.arglocs
283 reg_args = count_reg_args(arglocs)
284 self._collect_and_push_stack_args(arglocs)
285 # collect variables that need to go in registers and the registers they
286 # will be stored in
287 num = 0
288 count = 0
289 non_float_locs = []
290 non_float_regs = []
291 float_locs = []
292 for i in range(reg_args):
293 arg = arglocs[i]
294 if arg.type == FLOAT and count % 2 != 0:
295 num += 1
296 count = 0
297 reg = r.caller_resp[num]
298
299 if arg.type == FLOAT:
300 float_locs.append((arg, reg))
301 else:
302 non_float_locs.append(arg)
303 non_float_regs.append(reg)
304
305 if arg.type == FLOAT:
306 num += 2
307 else:
308 num += 1
309 count += 1
310 # Check that the address of the function we want to call is not
311 # currently stored in one of the registers used to pass the arguments
312 # or on the stack, which we can not access later
313 # If this happens to be the case we remap the register to r4 and use r4
314 # to call the function
315 if not self.fnloc.is_imm():
316 non_float_locs.append(self.fnloc)
317 non_float_regs.append(r.r4)
318 self.fnloc = r.r4
319 # remap values stored in core registers
320 remap_frame_layout(self.asm, non_float_locs, non_float_regs, r.ip)
321
322 for loc, reg in float_locs:
323 self.asm.mov_from_vfp_loc(loc, reg, r.all_regs[reg.value + 1])
324
325 class HardFloatCallBuilder(ARMCallbuilder):
326
327 next_arg_vfp = 0
328 next_arg_svfp = 0
329
330 def get_next_vfp(self, tp):
331 assert tp in 'fS'
332 if tp == 'f':
333 # 64bit double
334 i = max(self.next_arg_vfp, (self.next_arg_svfp + 1) >> 1)
335 if i >= len(r.vfp_argument_regs):
336 self.next_arg_svfp = 1000 # stop that sequence too
337 return None
338 self.next_arg_vfp = i + 1
339 return r.vfp_argument_regs[i]
340 else:
341 # 32bit float
342 i = self.next_arg_svfp
343 if not (i & 1): # if i is even
344 i = max(i, self.next_arg_vfp << 1)
345 if i >= len(r.svfp_argument_regs):
346 return None
347 self.next_arg_svfp = i + 1
348 return r.svfp_argument_regs[i]
349
350 def prepare_arguments(self):
351 non_float_locs = []
352 non_float_regs = []
353 float_locs = []
354 float_regs = []
355 stack_args = []
356 singlefloats = None
357 longlong_mask = 0
358
359 arglocs = self.arglocs
360 argtypes = self.argtypes
361
362 r_register_count = 0
363 on_stack = 0
364
365 for i in range(len(arglocs)):
366 argtype = INT
367 if i < len(argtypes) and argtypes[i] == 'S':
368 argtype = argtypes[i]
369 arg = arglocs[i]
370
371 if arg.is_float():
372 if i < len(argtypes) and argtypes[i] == 'L':
373 # A longlong argument. It uses two regular argument
374 # positions, but aligned to an even number. This is
375 # a bit strange, but it is the case even for registers:
376 # it can be in r0-r1 or in r2-r3 but not in r1-r2.
377 assert arg.is_float()
378 if r_register_count == 0:
379 # will temporarily load the register into d8
380 float_locs.append(arg)
381 float_regs.append(r.d8)
382 longlong_mask |= 1
383 r_register_count = 2
384 continue
385 elif r_register_count <= 2:
386 # will temporarily load the register into d9
387 float_locs.append(arg)
388 float_regs.append(r.d9)
389 longlong_mask |= 2
390 r_register_count = 4
391 continue
392 elif r_register_count == 3:
393 r_register_count = 4
394 else:
395 # A 64-bit float argument. Goes into the next free v#
396 # register, or if none, to the stack aligned to an
397 # even number of words.
398 argtype = FLOAT
399 reg = self.get_next_vfp(argtype)
400 if reg:
401 float_locs.append(arg)
402 assert reg not in float_regs
403 float_regs.append(reg)
404 continue
405 # float or longlong argument that needs to go on the stack
406 if on_stack & 1: # odd: realign
407 stack_args.append(None)
408 on_stack += 1
409 stack_args.append(arg)
410 on_stack += 2
411
412 elif argtype == 'S':
413 # Singlefloat (32-bit) argument. Goes into the next free
414 # v# register, or if none, to the stack in a single word.
415 if singlefloats is None:
416 singlefloats = []
417 tgt = self.get_next_vfp(argtype)
418 if tgt:
419 singlefloats.append((arg, tgt))
420 else: # Singlefloat argument that needs to go on the stack
421 # treated the same as a regular core register argument
422 stack_args.append(arg)
423 on_stack += 1
424 else:
425 # Regular one-word argument. Goes into the next register
426 # free from the list r0, r1, r2, r3, or to the stack.
427 if r_register_count < len(r.argument_regs):
428 reg = r.argument_regs[r_register_count]
429 r_register_count += 1
430 non_float_locs.append(arg)
431 non_float_regs.append(reg)
432 else: # non-float argument that needs to go on the stack
433 stack_args.append(arg)
434 on_stack += 1
435
436 # align the stack
437 if on_stack & 1: # odd: realign
438 stack_args.append(None)
439 on_stack += 1
440 self._push_stack_args(stack_args, on_stack*WORD)
441
442 # Check that the address of the function we want to call is not
443 # currently stored in one of the registers used to pass the arguments
444 # or on the stack, which we can not access later
445 # If this happens to be the case we remap the register to r4 and use r4
446 # to call the function
447 if not self.fnloc.is_imm():
448 non_float_locs.append(self.fnloc)
449 non_float_regs.append(r.r4)
450 self.fnloc = r.r4
451
452 # remap values stored in vfp registers
453 remap_frame_layout(self.asm, float_locs, float_regs, r.vfp_ip)
454 if singlefloats:
455 for src, dest in singlefloats:
456 if src.is_float():
457 assert 0, 'unsupported case'
458 if src.is_stack():
459 # use special VLDR for 32bit
460 self.asm.regalloc_mov(src, r.ip)
461 src = r.ip
462 if src.is_imm():
463 self.mc.gen_load_int(r.ip.value, src.value)
464 src = r.ip
465 if src.is_core_reg():
466 self.mc.VMOV_cs(dest.value, src.value)
467
468 # remap values stored in core registers
469 remap_frame_layout(self.asm, non_float_locs, non_float_regs, r.ip)
470 if longlong_mask & 1:
471 self.mc.FMRRD(r.r0.value, r.r1.value, r.d8.value)
472 if longlong_mask & 2:
473 self.mc.FMRRD(r.r2.value, r.r3.value, r.d9.value)
474
475
476 def load_result(self):
477 resloc = self.resloc
478 if self.restype == 'S':
479 self.mc.VMOV_sc(resloc.value, r.s0.value)
480 elif self.restype == 'L':
481 assert resloc.is_vfp_reg()
482 self.mc.FMDRR(resloc.value, r.r0.value, r.r1.value)
483 # ensure the result is wellformed and stored in the correct location
484 if resloc is not None and resloc.is_core_reg():
485 self._ensure_result_bit_extension(resloc,
486 self.ressize, self.ressign)
487
488 def get_result_locs(self):
489 if self.resloc is None:
490 return [], []
491 if self.resloc.is_vfp_reg():
492 if self.restype == 'L': # long long
493 return [r.r0, r.r1], []
494 else:
495 return [], [r.d0]
496 assert self.resloc.is_core_reg()
497 return [r.r0], []
498
499
500 def get_callbuilder(cpu, assembler, fnloc, arglocs,
501 resloc=r.r0, restype=INT, ressize=WORD, ressigned=True):
502 if cpu.cpuinfo.hf_abi:
503 return HardFloatCallBuilder(assembler, fnloc, arglocs, resloc,
504 restype, ressize, ressigned)
505 else:
506 return SoftFloatCallBuilder(assembler, fnloc, arglocs, resloc,
507 restype, ressize, ressigned)