| 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 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 40 |
if self.fnloc.is_imm(): |
| 41 |
self.mc.BL(self.fnloc.value) |
| 42 |
return |
| 43 |
|
| 44 |
assert self.fnloc.is_core_reg() |
| 45 |
self.mc.BLX(self.fnloc.value) |
| 46 |
|
| 47 |
def restore_stack_pointer(self): |
| 48 |
|
| 49 |
assert self.current_sp % 8 == 0 |
| 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: |
| 67 |
ofs += WORD |
| 68 |
|
| 69 |
def _adjust_sp(self, n): |
| 70 |
|
| 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 |
|
| 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 |
|
| 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) |
| 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(): |
| 105 |
self.mc.ADD_ri(r.fp.value, r.fp.value, 1) |
| 106 |
|
| 107 |
def move_real_result_and_call_reacqgil_addr(self, fastgil): |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
self.mc.LDREX(r.r3.value, r.r6.value) |
| 115 |
self.mc.CMP_ri(r.r3.value, 0) |
| 116 |
self.mc.STREX(r.r3.value, r.r8.value, r.r6.value, c=c.EQ) |
| 117 |
|
| 118 |
self.mc.CMP_ri(r.r3.value, 0, cond=c.EQ) |
| 119 |
if self.asm.cpu.cpuinfo.arch_version >= 7: |
| 120 |
self.mc.DMB() |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
if self.asm.cpu.gc_ll_descr.gcrootmap: |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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() |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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() |
| 145 |
|
| 146 |
|
| 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 |
|
| 152 |
pmc = OverwritingBuilder(self.mc, b1_location, WORD) |
| 153 |
pmc.B_offs(self.mc.currpos(), c.EQ) |
| 154 |
|
| 155 |
if not we_are_translated(): |
| 156 |
self.mc.SUB_ri(r.fp.value, r.fp.value, 1) |
| 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: |
| 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 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 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 |
|
| 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 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 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 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 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 |
|
| 240 |
resloc = self.resloc |
| 241 |
if resloc is None: |
| 242 |
return |
| 243 |
if resloc.is_vfp_reg(): |
| 244 |
|
| 245 |
self.asm.mov_to_vfp_loc(r.r0, r.r1, resloc) |
| 246 |
elif resloc.is_core_reg(): |
| 247 |
|
| 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 |
|
| 258 |
|
| 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 |
|
| 286 |
|
| 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 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 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 |
|
| 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 |
|
| 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 |
| 337 |
return None |
| 338 |
self.next_arg_vfp = i + 1 |
| 339 |
return r.vfp_argument_regs[i] |
| 340 |
else: |
| 341 |
|
| 342 |
i = self.next_arg_svfp |
| 343 |
if not (i & 1): |
| 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 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
assert arg.is_float() |
| 378 |
if r_register_count == 0: |
| 379 |
|
| 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 |
|
| 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 |
|
| 396 |
|
| 397 |
|
| 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 |
|
| 406 |
if on_stack & 1: |
| 407 |
stack_args.append(None) |
| 408 |
on_stack += 1 |
| 409 |
stack_args.append(arg) |
| 410 |
on_stack += 2 |
| 411 |
|
| 412 |
elif argtype == 'S': |
| 413 |
|
| 414 |
|
| 415 |
if singlefloats is None: |
| 416 |
singlefloats = [] |
| 417 |
tgt = self.get_next_vfp(argtype) |
| 418 |
if tgt: |
| 419 |
singlefloats.append((arg, tgt)) |
| 420 |
else: |
| 421 |
|
| 422 |
stack_args.append(arg) |
| 423 |
on_stack += 1 |
| 424 |
else: |
| 425 |
|
| 426 |
|
| 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: |
| 433 |
stack_args.append(arg) |
| 434 |
on_stack += 1 |
| 435 |
|
| 436 |
|
| 437 |
if on_stack & 1: |
| 438 |
stack_args.append(None) |
| 439 |
on_stack += 1 |
| 440 |
self._push_stack_args(stack_args, on_stack*WORD) |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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': |
| 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) |