| 1 |
|
| 2 |
|
| 3 |
from rpython.jit.backend.llsupport import rewrite |
| 4 |
from rpython.jit.backend.llsupport.descr import CallDescr |
| 5 |
from rpython.jit.backend.llsupport.gcmap import allocate_gcmap |
| 6 |
from rpython.jit.backend.llsupport.jump import remap_frame_layout_mixed |
| 7 |
from rpython.jit.backend.llsupport.regalloc import ( |
| 8 |
BaseRegalloc, FrameManager, RegisterManager, TempVar, |
| 9 |
compute_vars_longevity) |
| 10 |
from rpython.jit.backend.riscv import registers as r |
| 11 |
from rpython.jit.backend.riscv.arch import ( |
| 12 |
JITFRAME_FIXED_SIZE, SHAMT_MAX, SINT12_IMM_MAX, SINT12_IMM_MIN, XLEN) |
| 13 |
from rpython.jit.backend.riscv.instruction_util import ( |
| 14 |
COND_BEQ, COND_BGE, COND_BGEU, COND_BLT, COND_BLTU, COND_BNE, COND_INVALID, |
| 15 |
check_imm_arg, get_negated_branch_inst) |
| 16 |
from rpython.jit.backend.riscv.locations import ( |
| 17 |
FloatImmLocation, ImmLocation, StackLocation, get_fp_offset) |
| 18 |
from rpython.jit.backend.riscv.opassembler import asm_comp_operations |
| 19 |
from rpython.jit.codewriter import longlong |
| 20 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 21 |
from rpython.jit.metainterp.history import ( |
| 22 |
Const, ConstFloat, ConstInt, ConstPtr, FLOAT, INT, REF, TargetToken) |
| 23 |
from rpython.jit.metainterp.resoperation import rop |
| 24 |
from rpython.rlib.rarithmetic import r_uint |
| 25 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 26 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 27 |
|
| 28 |
|
| 29 |
class TempInt(TempVar): |
| 30 |
type = INT |
| 31 |
|
| 32 |
def __repr__(self): |
| 33 |
return "<TempInt at %s>" % (id(self),) |
| 34 |
|
| 35 |
|
| 36 |
class TempPtr(TempVar): |
| 37 |
type = REF |
| 38 |
|
| 39 |
def __repr__(self): |
| 40 |
return "<TempPtr at %s>" % (id(self),) |
| 41 |
|
| 42 |
|
| 43 |
class TempFloat(TempVar): |
| 44 |
type = FLOAT |
| 45 |
|
| 46 |
def __repr__(self): |
| 47 |
return "<TempFloat at %s>" % (id(self),) |
| 48 |
|
| 49 |
|
| 50 |
class RISCVFrameManager(FrameManager): |
| 51 |
def __init__(self, base_ofs): |
| 52 |
FrameManager.__init__(self) |
| 53 |
self.base_ofs = base_ofs |
| 54 |
|
| 55 |
def frame_pos(self, i, box_type): |
| 56 |
return StackLocation(i, get_fp_offset(self.base_ofs, i), box_type) |
| 57 |
|
| 58 |
@staticmethod |
| 59 |
def frame_size(type): |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
return 1 |
| 76 |
|
| 77 |
@staticmethod |
| 78 |
def get_loc_index(loc): |
| 79 |
assert loc.is_stack() |
| 80 |
return loc.position |
| 81 |
|
| 82 |
|
| 83 |
class RISCVRegisterManager(RegisterManager): |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
FORBID_TEMP_BOXES = True |
| 94 |
|
| 95 |
|
| 96 |
class CoreRegisterManager(RISCVRegisterManager): |
| 97 |
all_regs = r.allocatable_registers |
| 98 |
box_types = None |
| 99 |
no_lower_byte_regs = all_regs |
| 100 |
save_around_call_regs = r.caller_saved_registers |
| 101 |
frame_reg = r.jfp |
| 102 |
|
| 103 |
def __init__(self, longevity, frame_manager=None, assembler=None): |
| 104 |
RISCVRegisterManager.__init__(self, longevity, frame_manager, |
| 105 |
assembler) |
| 106 |
|
| 107 |
def call_result_location(self, v): |
| 108 |
return r.x10 |
| 109 |
|
| 110 |
def convert_to_imm(self, c): |
| 111 |
|
| 112 |
if isinstance(c, ConstInt): |
| 113 |
val = rffi.cast(lltype.Signed, c.value) |
| 114 |
else: |
| 115 |
assert isinstance(c, ConstPtr) |
| 116 |
val = rffi.cast(lltype.Signed, c.value) |
| 117 |
return ImmLocation(val) |
| 118 |
|
| 119 |
def return_constant(self, v, forbidden_vars=[], selected_reg=None): |
| 120 |
self._check_type(v) |
| 121 |
if isinstance(v, Const): |
| 122 |
if isinstance(v, ConstInt): |
| 123 |
reg_type = INT |
| 124 |
else: |
| 125 |
assert isinstance(v, ConstPtr) |
| 126 |
reg_type = REF |
| 127 |
|
| 128 |
if not v.nonnull() and selected_reg is None: |
| 129 |
return r.zero |
| 130 |
|
| 131 |
loc = self.get_scratch_reg(reg_type, forbidden_vars, |
| 132 |
selected_reg=selected_reg) |
| 133 |
immvalue = self.convert_to_imm(v) |
| 134 |
self.assembler.load_imm(loc, immvalue) |
| 135 |
return loc |
| 136 |
return RISCVRegisterManager.return_constant(self, v, forbidden_vars, |
| 137 |
selected_reg) |
| 138 |
|
| 139 |
def get_scratch_reg(self, type=INT, forbidden_vars=[], selected_reg=None): |
| 140 |
assert type == INT or type == REF |
| 141 |
box = None |
| 142 |
if type == INT: |
| 143 |
box = TempInt() |
| 144 |
else: |
| 145 |
box = TempPtr() |
| 146 |
reg = self.force_allocate_reg(box, forbidden_vars=forbidden_vars, |
| 147 |
selected_reg=selected_reg) |
| 148 |
self.temp_boxes.append(box) |
| 149 |
return reg |
| 150 |
|
| 151 |
|
| 152 |
class FloatRegisterManager(RISCVRegisterManager): |
| 153 |
all_regs = r.allocatable_fp_registers |
| 154 |
box_types = [FLOAT] |
| 155 |
save_around_call_regs = r.caller_saved_fp_registers |
| 156 |
|
| 157 |
def __init__(self, longevity, frame_manager=None, assembler=None): |
| 158 |
RISCVRegisterManager.__init__(self, longevity, frame_manager, |
| 159 |
assembler) |
| 160 |
|
| 161 |
def call_result_location(self, v): |
| 162 |
return r.f10 |
| 163 |
|
| 164 |
def convert_to_imm(self, c): |
| 165 |
imm_bits = longlong.extract_bits(c.getfloatstorage()) |
| 166 |
return FloatImmLocation(imm_bits) |
| 167 |
|
| 168 |
def return_constant(self, v, forbidden_vars=[], selected_reg=None): |
| 169 |
self._check_type(v) |
| 170 |
if isinstance(v, Const): |
| 171 |
assert isinstance(v, ConstFloat) |
| 172 |
loc = self.get_scratch_reg(FLOAT, forbidden_vars, |
| 173 |
selected_reg=selected_reg) |
| 174 |
immvalue = self.convert_to_imm(v) |
| 175 |
self.assembler.load_imm(loc, immvalue) |
| 176 |
return loc |
| 177 |
return RISCVRegisterManager.return_constant(self, v, forbidden_vars, |
| 178 |
selected_reg) |
| 179 |
|
| 180 |
def get_scratch_reg(self, type=FLOAT, forbidden_vars=[], selected_reg=None): |
| 181 |
assert type == FLOAT |
| 182 |
box = TempFloat() |
| 183 |
reg = self.force_allocate_reg(box, forbidden_vars=forbidden_vars, |
| 184 |
selected_reg=selected_reg) |
| 185 |
self.temp_boxes.append(box) |
| 186 |
return reg |
| 187 |
|
| 188 |
|
| 189 |
def check_imm_box(arg): |
| 190 |
if isinstance(arg, ConstInt): |
| 191 |
return check_imm_arg(arg.getint()) |
| 192 |
return False |
| 193 |
|
| 194 |
def check_negative_imm_box(arg): |
| 195 |
if isinstance(arg, ConstInt): |
| 196 |
return check_imm_arg(-arg.getint()) |
| 197 |
return False |
| 198 |
|
| 199 |
def check_plus_one_imm_box(arg): |
| 200 |
if isinstance(arg, ConstInt): |
| 201 |
return check_imm_arg(arg.getint() + 1) |
| 202 |
return False |
| 203 |
|
| 204 |
def check_uint_le_rhs_imm_arg(imm): |
| 205 |
return imm >= SINT12_IMM_MIN - 1 and imm <= SINT12_IMM_MAX - 1 |
| 206 |
|
| 207 |
def check_uint_le_rhs_imm_box(arg): |
| 208 |
if isinstance(arg, ConstInt): |
| 209 |
return check_uint_le_rhs_imm_arg(arg.getint()) |
| 210 |
return False |
| 211 |
|
| 212 |
def check_shamt_imm_arg(imm): |
| 213 |
return imm >= 0 and imm <= SHAMT_MAX |
| 214 |
|
| 215 |
def check_shamt_imm_box(arg): |
| 216 |
if isinstance(arg, ConstInt): |
| 217 |
return check_shamt_imm_arg(arg.getint()) |
| 218 |
return False |
| 219 |
|
| 220 |
|
| 221 |
class Regalloc(BaseRegalloc): |
| 222 |
def __init__(self, assembler): |
| 223 |
self.cpu = assembler.cpu |
| 224 |
self.assembler = assembler |
| 225 |
self.frame_manager = None |
| 226 |
self.rm = None |
| 227 |
self.fprm = None |
| 228 |
self.jump_target_descr = None |
| 229 |
self.final_jump_op = None |
| 230 |
|
| 231 |
def _prepare(self, inputargs, operations, allgcrefs): |
| 232 |
cpu = self.cpu |
| 233 |
|
| 234 |
self.fm = RISCVFrameManager(cpu.get_baseofs_of_frame_field()) |
| 235 |
self.frame_manager = self.fm |
| 236 |
operations = cpu.gc_ll_descr.rewrite_assembler(cpu, operations, |
| 237 |
allgcrefs) |
| 238 |
|
| 239 |
longevity = compute_vars_longevity(inputargs, operations) |
| 240 |
self.longevity = longevity |
| 241 |
|
| 242 |
self.rm = CoreRegisterManager(longevity, self.fm, self.assembler) |
| 243 |
self.fprm = FloatRegisterManager(longevity, self.fm, self.assembler) |
| 244 |
return operations |
| 245 |
|
| 246 |
def prepare_loop(self, inputargs, operations, looptoken, allgcrefs): |
| 247 |
operations = self._prepare(inputargs, operations, allgcrefs) |
| 248 |
self._set_initial_bindings(inputargs, looptoken) |
| 249 |
self.possibly_free_vars(list(inputargs)) |
| 250 |
return operations |
| 251 |
|
| 252 |
def prepare_bridge(self, inputargs, arglocs, operations, allgcrefs, |
| 253 |
frame_info): |
| 254 |
operations = self._prepare(inputargs, operations, allgcrefs) |
| 255 |
self._update_bindings(arglocs, inputargs) |
| 256 |
return operations |
| 257 |
|
| 258 |
def _update_bindings(self, locs, inputargs): |
| 259 |
|
| 260 |
used = {} |
| 261 |
i = 0 |
| 262 |
for loc in locs: |
| 263 |
if loc is None: |
| 264 |
loc = r.jfp |
| 265 |
arg = inputargs[i] |
| 266 |
i += 1 |
| 267 |
if loc.is_core_reg(): |
| 268 |
self.rm.reg_bindings[arg] = loc |
| 269 |
used[loc] = None |
| 270 |
elif loc.is_fp_reg(): |
| 271 |
self.fprm.reg_bindings[arg] = loc |
| 272 |
used[loc] = None |
| 273 |
else: |
| 274 |
assert loc.is_stack() |
| 275 |
self.frame_manager.bind(arg, loc) |
| 276 |
|
| 277 |
|
| 278 |
self.rm.free_regs = [] |
| 279 |
for reg in self.rm.all_regs: |
| 280 |
if reg not in used: |
| 281 |
self.rm.free_regs.append(reg) |
| 282 |
self.fprm.free_regs = [] |
| 283 |
for reg in self.fprm.all_regs: |
| 284 |
if reg not in used: |
| 285 |
self.fprm.free_regs.append(reg) |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
self.possibly_free_vars(list(inputargs)) |
| 290 |
|
| 291 |
self.frame_manager.finish_binding() |
| 292 |
self._check_invariants() |
| 293 |
|
| 294 |
def _prepare_op_int_commutative_binary_op(self, op): |
| 295 |
boxes = op.getarglist() |
| 296 |
a0, a1 = boxes |
| 297 |
imm_a0 = check_imm_box(a0) |
| 298 |
imm_a1 = check_imm_box(a1) |
| 299 |
if not imm_a0 and imm_a1: |
| 300 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 301 |
l1 = self.convert_to_imm(a1) |
| 302 |
elif imm_a0 and not imm_a1: |
| 303 |
l1 = self.convert_to_imm(a0) |
| 304 |
l0 = self.make_sure_var_in_reg(a1, boxes) |
| 305 |
else: |
| 306 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 307 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 308 |
self.possibly_free_vars_for_op(op) |
| 309 |
self.free_temp_vars() |
| 310 |
res = self.force_allocate_reg(op) |
| 311 |
return [l0, l1, res] |
| 312 |
|
| 313 |
prepare_op_int_add = _prepare_op_int_commutative_binary_op |
| 314 |
prepare_op_int_and = _prepare_op_int_commutative_binary_op |
| 315 |
prepare_op_int_or = _prepare_op_int_commutative_binary_op |
| 316 |
prepare_op_int_xor = _prepare_op_int_commutative_binary_op |
| 317 |
prepare_op_nursery_ptr_increment = _prepare_op_int_commutative_binary_op |
| 318 |
|
| 319 |
def prepare_op_int_sub(self, op): |
| 320 |
boxes = op.getarglist() |
| 321 |
a0, a1 = boxes |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
if check_negative_imm_box(a1): |
| 326 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 327 |
l1 = self.convert_to_imm(a1) |
| 328 |
else: |
| 329 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 330 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 331 |
self.possibly_free_vars_for_op(op) |
| 332 |
self.free_temp_vars() |
| 333 |
res = self.force_allocate_reg(op) |
| 334 |
return [l0, l1, res] |
| 335 |
|
| 336 |
def _prepare_op_int_mul_op(self, op): |
| 337 |
boxes = op.getarglist() |
| 338 |
a0, a1 = boxes |
| 339 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 340 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 341 |
self.possibly_free_vars_for_op(op) |
| 342 |
self.free_temp_vars() |
| 343 |
res = self.force_allocate_reg(op) |
| 344 |
return [l0, l1, res] |
| 345 |
|
| 346 |
prepare_op_int_mul = _prepare_op_int_mul_op |
| 347 |
prepare_op_uint_mul_high = _prepare_op_int_mul_op |
| 348 |
|
| 349 |
def _prepare_op_int_shift_op(self, op): |
| 350 |
boxes = op.getarglist() |
| 351 |
a0, a1 = boxes |
| 352 |
if check_shamt_imm_box(a1): |
| 353 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 354 |
l1 = self.convert_to_imm(a1) |
| 355 |
else: |
| 356 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 357 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 358 |
self.possibly_free_vars_for_op(op) |
| 359 |
self.free_temp_vars() |
| 360 |
res = self.force_allocate_reg(op) |
| 361 |
return [l0, l1, res] |
| 362 |
|
| 363 |
prepare_op_int_lshift = _prepare_op_int_shift_op |
| 364 |
prepare_op_int_rshift = _prepare_op_int_shift_op |
| 365 |
prepare_op_uint_rshift = _prepare_op_int_shift_op |
| 366 |
|
| 367 |
def _gen_prepare_op_int_lt(swap_operands): |
| 368 |
def _prepare_op_int_lt(self, op): |
| 369 |
boxes = op.getarglist() |
| 370 |
|
| 371 |
if swap_operands: |
| 372 |
a1, a0 = boxes |
| 373 |
else: |
| 374 |
a0, a1 = boxes |
| 375 |
|
| 376 |
if check_imm_box(a1): |
| 377 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 378 |
l1 = self.convert_to_imm(a1) |
| 379 |
else: |
| 380 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 381 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 382 |
|
| 383 |
self.possibly_free_vars_for_op(op) |
| 384 |
self.free_temp_vars() |
| 385 |
res = self.force_allocate_reg(op) |
| 386 |
return [l0, l1, res] |
| 387 |
return _prepare_op_int_lt |
| 388 |
|
| 389 |
prepare_op_int_lt = _gen_prepare_op_int_lt(swap_operands=False) |
| 390 |
prepare_op_int_gt = _gen_prepare_op_int_lt(swap_operands=True) |
| 391 |
|
| 392 |
prepare_op_uint_lt = prepare_op_int_lt |
| 393 |
prepare_op_uint_gt = prepare_op_int_gt |
| 394 |
|
| 395 |
prepare_comp_op_int_lt = prepare_op_int_lt |
| 396 |
prepare_comp_op_int_gt = prepare_op_int_gt |
| 397 |
|
| 398 |
prepare_comp_op_uint_lt = prepare_op_uint_lt |
| 399 |
prepare_comp_op_uint_gt = prepare_op_uint_gt |
| 400 |
|
| 401 |
def _gen_prepare_op_int_le(swap_operands): |
| 402 |
def _prepare_op_int_le(self, op): |
| 403 |
boxes = op.getarglist() |
| 404 |
|
| 405 |
if swap_operands: |
| 406 |
a1, a0 = boxes |
| 407 |
else: |
| 408 |
a0, a1 = boxes |
| 409 |
|
| 410 |
if check_plus_one_imm_box(a1): |
| 411 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 412 |
l1 = self.convert_to_imm(a1) |
| 413 |
elif check_imm_box(a0): |
| 414 |
l0 = self.convert_to_imm(a0) |
| 415 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 416 |
else: |
| 417 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 418 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 419 |
|
| 420 |
self.possibly_free_vars_for_op(op) |
| 421 |
self.free_temp_vars() |
| 422 |
res = self.force_allocate_reg(op) |
| 423 |
return [l0, l1, res] |
| 424 |
return _prepare_op_int_le |
| 425 |
|
| 426 |
prepare_op_int_le = _gen_prepare_op_int_le(swap_operands=False) |
| 427 |
prepare_op_int_ge = _gen_prepare_op_int_le(swap_operands=True) |
| 428 |
|
| 429 |
prepare_comp_op_int_le = prepare_op_int_le |
| 430 |
prepare_comp_op_int_ge = prepare_op_int_ge |
| 431 |
|
| 432 |
def _gen_prepare_op_uint_le(swap_operands): |
| 433 |
def _prepare_op_uint_le(self, op): |
| 434 |
boxes = op.getarglist() |
| 435 |
|
| 436 |
if swap_operands: |
| 437 |
a1, a0 = boxes |
| 438 |
else: |
| 439 |
a0, a1 = boxes |
| 440 |
|
| 441 |
if check_uint_le_rhs_imm_box(a1): |
| 442 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 443 |
l1 = self.convert_to_imm(a1) |
| 444 |
elif check_imm_box(a0): |
| 445 |
l0 = self.convert_to_imm(a0) |
| 446 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 447 |
else: |
| 448 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 449 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 450 |
|
| 451 |
self.possibly_free_vars_for_op(op) |
| 452 |
self.free_temp_vars() |
| 453 |
res = self.force_allocate_reg(op) |
| 454 |
return [l0, l1, res] |
| 455 |
return _prepare_op_uint_le |
| 456 |
|
| 457 |
prepare_op_uint_le = _gen_prepare_op_uint_le(swap_operands=False) |
| 458 |
prepare_op_uint_ge = _gen_prepare_op_uint_le(swap_operands=True) |
| 459 |
|
| 460 |
prepare_comp_op_uint_le = prepare_op_uint_le |
| 461 |
prepare_comp_op_uint_ge = prepare_op_uint_ge |
| 462 |
|
| 463 |
def _prepare_op_int_commutative_compare_op(self, op): |
| 464 |
boxes = op.getarglist() |
| 465 |
a0, a1 = boxes |
| 466 |
imm_a0 = check_imm_box(a0) |
| 467 |
imm_a1 = check_imm_box(a1) |
| 468 |
if not imm_a0 and imm_a1: |
| 469 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 470 |
l1 = self.convert_to_imm(a1) |
| 471 |
elif imm_a0 and not imm_a1: |
| 472 |
l1 = self.convert_to_imm(a0) |
| 473 |
l0 = self.make_sure_var_in_reg(a1, boxes) |
| 474 |
else: |
| 475 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 476 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 477 |
self.possibly_free_vars_for_op(op) |
| 478 |
self.free_temp_vars() |
| 479 |
res = self.force_allocate_reg(op) |
| 480 |
return [l0, l1, res] |
| 481 |
|
| 482 |
prepare_op_int_eq = _prepare_op_int_commutative_compare_op |
| 483 |
prepare_op_int_ne = _prepare_op_int_commutative_compare_op |
| 484 |
|
| 485 |
prepare_comp_op_int_eq = prepare_op_int_eq |
| 486 |
prepare_comp_op_int_ne = prepare_op_int_ne |
| 487 |
|
| 488 |
prepare_op_ptr_eq = prepare_op_instance_ptr_eq = prepare_op_int_eq |
| 489 |
prepare_op_ptr_ne = prepare_op_instance_ptr_ne = prepare_op_int_ne |
| 490 |
|
| 491 |
prepare_comp_op_ptr_eq = prepare_op_int_eq |
| 492 |
prepare_comp_op_ptr_ne = prepare_op_int_ne |
| 493 |
|
| 494 |
def _prepare_op_unary_op(self, op): |
| 495 |
boxes = op.getarglist() |
| 496 |
a0 = boxes[0] |
| 497 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 498 |
self.possibly_free_vars_for_op(op) |
| 499 |
self.free_temp_vars() |
| 500 |
res = self.force_allocate_reg(op) |
| 501 |
return [l0, res] |
| 502 |
|
| 503 |
prepare_op_int_is_true = _prepare_op_unary_op |
| 504 |
prepare_op_int_neg = _prepare_op_unary_op |
| 505 |
prepare_op_int_invert = _prepare_op_unary_op |
| 506 |
prepare_op_int_is_zero = _prepare_op_unary_op |
| 507 |
prepare_op_int_force_ge_zero = _prepare_op_unary_op |
| 508 |
|
| 509 |
prepare_comp_op_int_is_true = prepare_op_int_is_true |
| 510 |
prepare_comp_op_int_is_zero = prepare_op_int_is_zero |
| 511 |
|
| 512 |
def prepare_op_int_signext(self, op): |
| 513 |
a0, a1 = op.getarglist() |
| 514 |
l0 = self.loc(a0) |
| 515 |
if l0.is_stack(): |
| 516 |
|
| 517 |
|
| 518 |
pass |
| 519 |
else: |
| 520 |
|
| 521 |
|
| 522 |
l0 = self.make_sure_var_in_reg(a0) |
| 523 |
l1 = self.convert_to_imm(a1) |
| 524 |
self.possibly_free_vars_for_op(op) |
| 525 |
self.free_temp_vars() |
| 526 |
res = self.force_allocate_reg(op) |
| 527 |
return [l0, l1, res] |
| 528 |
|
| 529 |
def _gen_prepare_op_float_binary_op(swap_operands): |
| 530 |
def _prepare_op_float_binary_op(self, op): |
| 531 |
boxes = op.getarglist() |
| 532 |
if swap_operands: |
| 533 |
a1, a0 = boxes |
| 534 |
else: |
| 535 |
a0, a1 = boxes |
| 536 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 537 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 538 |
self.possibly_free_vars_for_op(op) |
| 539 |
self.free_temp_vars() |
| 540 |
res = self.force_allocate_reg(op) |
| 541 |
return [l0, l1, res] |
| 542 |
return _prepare_op_float_binary_op |
| 543 |
|
| 544 |
_prepare_op_float_binary_op = _gen_prepare_op_float_binary_op(False) |
| 545 |
_prepare_op_float_binary_op_swapped = _gen_prepare_op_float_binary_op(True) |
| 546 |
|
| 547 |
prepare_op_float_add = _prepare_op_float_binary_op |
| 548 |
prepare_op_float_sub = _prepare_op_float_binary_op |
| 549 |
prepare_op_float_mul = _prepare_op_float_binary_op |
| 550 |
prepare_op_float_truediv = _prepare_op_float_binary_op |
| 551 |
|
| 552 |
prepare_op_float_lt = _prepare_op_float_binary_op |
| 553 |
prepare_op_float_le = _prepare_op_float_binary_op |
| 554 |
prepare_op_float_gt = _prepare_op_float_binary_op_swapped |
| 555 |
prepare_op_float_ge = _prepare_op_float_binary_op_swapped |
| 556 |
prepare_op_float_eq = _prepare_op_float_binary_op |
| 557 |
prepare_op_float_ne = _prepare_op_float_binary_op |
| 558 |
|
| 559 |
prepare_comp_op_float_lt = prepare_op_float_lt |
| 560 |
prepare_comp_op_float_le = prepare_op_float_le |
| 561 |
prepare_comp_op_float_gt = prepare_op_float_gt |
| 562 |
prepare_comp_op_float_ge = prepare_op_float_ge |
| 563 |
prepare_comp_op_float_eq = prepare_op_float_eq |
| 564 |
prepare_comp_op_float_ne = prepare_op_float_ne |
| 565 |
|
| 566 |
prepare_op_float_neg = _prepare_op_unary_op |
| 567 |
prepare_op_float_abs = _prepare_op_unary_op |
| 568 |
prepare_op_cast_float_to_int = _prepare_op_unary_op |
| 569 |
prepare_op_cast_int_to_float = _prepare_op_unary_op |
| 570 |
prepare_op_convert_float_bytes_to_longlong = _prepare_op_unary_op |
| 571 |
prepare_op_convert_longlong_bytes_to_float= _prepare_op_unary_op |
| 572 |
|
| 573 |
def prepare_op_gc_store(self, op): |
| 574 |
boxes = op.getarglist() |
| 575 |
base_loc = self.make_sure_var_in_reg(boxes[0], boxes) |
| 576 |
ofs = boxes[1] |
| 577 |
if check_imm_box(ofs): |
| 578 |
ofs_loc = self.convert_to_imm(ofs) |
| 579 |
else: |
| 580 |
ofs_loc = self.make_sure_var_in_reg(ofs, boxes) |
| 581 |
value_loc = self.make_sure_var_in_reg(boxes[2], boxes) |
| 582 |
size_loc = self.convert_to_imm(boxes[3]) |
| 583 |
return [value_loc, base_loc, ofs_loc, size_loc] |
| 584 |
|
| 585 |
def _prepare_op_gc_load(self, op): |
| 586 |
boxes = op.getarglist() |
| 587 |
base_loc = self.make_sure_var_in_reg(boxes[0], boxes) |
| 588 |
ofs = boxes[1] |
| 589 |
if check_imm_box(ofs): |
| 590 |
ofs_loc = self.convert_to_imm(ofs) |
| 591 |
else: |
| 592 |
ofs_loc = self.make_sure_var_in_reg(ofs, boxes) |
| 593 |
nsize_loc = self.convert_to_imm(boxes[2]) |
| 594 |
self.possibly_free_vars_for_op(op) |
| 595 |
res_loc = self.force_allocate_reg(op) |
| 596 |
return [base_loc, ofs_loc, res_loc, nsize_loc] |
| 597 |
|
| 598 |
prepare_op_gc_load_i = _prepare_op_gc_load |
| 599 |
prepare_op_gc_load_r = _prepare_op_gc_load |
| 600 |
prepare_op_gc_load_f = _prepare_op_gc_load |
| 601 |
|
| 602 |
def prepare_op_gc_store_indexed(self, op): |
| 603 |
boxes = op.getarglist() |
| 604 |
base_loc = self.make_sure_var_in_reg(boxes[0], boxes) |
| 605 |
index_loc = self.make_sure_var_in_reg(boxes[1], boxes) |
| 606 |
value_loc = self.make_sure_var_in_reg(boxes[2], boxes) |
| 607 |
assert boxes[3].getint() == 1 |
| 608 |
ofs = boxes[4] |
| 609 |
if check_imm_box(ofs): |
| 610 |
ofs_loc = self.convert_to_imm(ofs) |
| 611 |
else: |
| 612 |
ofs_loc = self.make_sure_var_in_reg(ofs, boxes) |
| 613 |
size_loc = self.convert_to_imm(boxes[5]) |
| 614 |
return [value_loc, base_loc, index_loc, ofs_loc, size_loc] |
| 615 |
|
| 616 |
def _prepare_op_gc_load_indexed(self, op): |
| 617 |
boxes = op.getarglist() |
| 618 |
base_loc = self.make_sure_var_in_reg(boxes[0], boxes) |
| 619 |
index_loc = self.make_sure_var_in_reg(boxes[1], boxes) |
| 620 |
assert boxes[2].getint() == 1 |
| 621 |
ofs = boxes[3] |
| 622 |
if check_imm_box(ofs): |
| 623 |
ofs_loc = self.convert_to_imm(ofs) |
| 624 |
else: |
| 625 |
ofs_loc = self.make_sure_var_in_reg(ofs, boxes) |
| 626 |
nsize_loc = self.convert_to_imm(boxes[4]) |
| 627 |
self.possibly_free_vars_for_op(op) |
| 628 |
res_loc = self.force_allocate_reg(op) |
| 629 |
return [base_loc, index_loc, ofs_loc, res_loc, nsize_loc] |
| 630 |
|
| 631 |
prepare_op_gc_load_indexed_i = _prepare_op_gc_load_indexed |
| 632 |
prepare_op_gc_load_indexed_r = _prepare_op_gc_load_indexed |
| 633 |
prepare_op_gc_load_indexed_f = _prepare_op_gc_load_indexed |
| 634 |
|
| 635 |
def _prepare_guard_arglocs(self, op): |
| 636 |
arglocs = [None] * (len(op.getfailargs()) + 1) |
| 637 |
arglocs[0] = ImmLocation(self.frame_manager.get_frame_depth()) |
| 638 |
failargs = op.getfailargs() |
| 639 |
for i in range(len(failargs)): |
| 640 |
if failargs[i]: |
| 641 |
arglocs[i + 1] = self.loc(failargs[i]) |
| 642 |
return arglocs |
| 643 |
|
| 644 |
def _prepare_op_guard_unary_op(self, op): |
| 645 |
boxes = op.getarglist() |
| 646 |
a0 = boxes[0] |
| 647 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
guard_arglocs = self._prepare_guard_arglocs(op) |
| 669 |
return [l0] + guard_arglocs |
| 670 |
|
| 671 |
prepare_op_guard_true = _prepare_op_guard_unary_op |
| 672 |
prepare_op_guard_false = _prepare_op_guard_unary_op |
| 673 |
prepare_op_guard_nonnull = _prepare_op_guard_unary_op |
| 674 |
prepare_op_guard_isnull = _prepare_op_guard_unary_op |
| 675 |
|
| 676 |
def prepare_op_guard_value(self, op): |
| 677 |
boxes = op.getarglist() |
| 678 |
a0, a1 = boxes |
| 679 |
l0 = self.make_sure_var_in_reg(a0, boxes) |
| 680 |
l1 = self.make_sure_var_in_reg(a1, boxes) |
| 681 |
|
| 682 |
|
| 683 |
return [l0, l1] + self._prepare_guard_arglocs(op) |
| 684 |
|
| 685 |
def _gen_prepare_guard_op_guard_bool_op(guard_true): |
| 686 |
def _prepare_guard_op_guard_bool_op(self, op, guard_op): |
| 687 |
boxes = op.getarglist() |
| 688 |
|
| 689 |
opnum = op.getopnum() |
| 690 |
if (opnum == rop.INT_EQ or opnum == rop.PTR_EQ or |
| 691 |
opnum == rop.INSTANCE_PTR_EQ): |
| 692 |
guard_branch_inst = COND_BEQ |
| 693 |
elif (opnum == rop.INT_NE or opnum == rop.PTR_NE or |
| 694 |
opnum == rop.INSTANCE_PTR_NE): |
| 695 |
guard_branch_inst = COND_BNE |
| 696 |
elif opnum == rop.INT_LT: |
| 697 |
guard_branch_inst = COND_BLT |
| 698 |
elif opnum == rop.INT_LE: |
| 699 |
boxes = [boxes[1], boxes[0]] |
| 700 |
guard_branch_inst = COND_BGE |
| 701 |
elif opnum == rop.INT_GT: |
| 702 |
boxes = [boxes[1], boxes[0]] |
| 703 |
guard_branch_inst = COND_BLT |
| 704 |
elif opnum == rop.INT_GE: |
| 705 |
guard_branch_inst = COND_BGE |
| 706 |
elif opnum == rop.UINT_LT: |
| 707 |
guard_branch_inst = COND_BLTU |
| 708 |
elif opnum == rop.UINT_LE: |
| 709 |
if check_plus_one_imm_box(boxes[1]): |
| 710 |
boxes = [boxes[0], ConstInt(boxes[1].getint() + 1)] |
| 711 |
guard_branch_inst = COND_BLTU |
| 712 |
else: |
| 713 |
boxes = [boxes[1], boxes[0]] |
| 714 |
guard_branch_inst = COND_BGEU |
| 715 |
elif opnum == rop.UINT_GT: |
| 716 |
boxes = [boxes[1], boxes[0]] |
| 717 |
guard_branch_inst = COND_BLTU |
| 718 |
elif opnum == rop.UINT_GE: |
| 719 |
if check_plus_one_imm_box(boxes[0]): |
| 720 |
boxes = [boxes[1], ConstInt(boxes[0].getint() + 1)] |
| 721 |
guard_branch_inst = COND_BLTU |
| 722 |
else: |
| 723 |
guard_branch_inst = COND_BGEU |
| 724 |
elif opnum == rop.INT_IS_ZERO: |
| 725 |
boxes = [boxes[0], ConstInt(0)] |
| 726 |
guard_branch_inst = COND_BEQ |
| 727 |
elif opnum == rop.INT_IS_TRUE: |
| 728 |
boxes = [boxes[0], ConstInt(0)] |
| 729 |
guard_branch_inst = COND_BNE |
| 730 |
else: |
| 731 |
assert False, 'unexpected case' |
| 732 |
|
| 733 |
locs = [self.make_sure_var_in_reg(v, boxes) for v in boxes] |
| 734 |
|
| 735 |
if not guard_true: |
| 736 |
guard_branch_inst = get_negated_branch_inst(guard_branch_inst) |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
guard_arglocs = self._prepare_guard_arglocs(guard_op) |
| 741 |
|
| 742 |
arglocs = locs + guard_arglocs |
| 743 |
return arglocs, guard_branch_inst |
| 744 |
return _prepare_guard_op_guard_bool_op |
| 745 |
|
| 746 |
prepare_guard_op_guard_true = _gen_prepare_guard_op_guard_bool_op(True) |
| 747 |
prepare_guard_op_guard_false = _gen_prepare_guard_op_guard_bool_op(False) |
| 748 |
|
| 749 |
def _gen_prepare_guard_op_guard_overflow_op(guard_overflow): |
| 750 |
def _prepare_guard_op_guard_overflow_op(self, op, guard_op): |
| 751 |
boxes = op.getarglist() |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
locs = [self.make_sure_var_in_reg(v, boxes) for v in boxes] |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
locs.append(self.force_allocate_reg(op, boxes)) |
| 788 |
locs.append(self.rm.get_scratch_reg(INT, boxes + [op])) |
| 789 |
locs.append(self.rm.get_scratch_reg(INT, boxes + [op])) |
| 790 |
|
| 791 |
guard_branch_inst = COND_BNE if guard_overflow else COND_BEQ |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
arglocs = locs + self._prepare_guard_arglocs(guard_op) |
| 796 |
return arglocs, guard_branch_inst |
| 797 |
return _prepare_guard_op_guard_overflow_op |
| 798 |
|
| 799 |
prepare_guard_op_guard_overflow = \ |
| 800 |
_gen_prepare_guard_op_guard_overflow_op(True) |
| 801 |
prepare_guard_op_guard_no_overflow = \ |
| 802 |
_gen_prepare_guard_op_guard_overflow_op(False) |
| 803 |
|
| 804 |
def prepare_op_guard_class(self, op): |
| 805 |
obj_loc = self.make_sure_var_in_reg(op.getarg(0)) |
| 806 |
cls_loc = ImmLocation(rffi.cast(lltype.Signed, op.getarg(1).getint())) |
| 807 |
|
| 808 |
|
| 809 |
return [obj_loc, cls_loc] + self._prepare_guard_arglocs(op) |
| 810 |
|
| 811 |
prepare_op_guard_nonnull_class = prepare_op_guard_class |
| 812 |
|
| 813 |
prepare_op_guard_gc_type = prepare_op_guard_class |
| 814 |
prepare_op_guard_subclass = prepare_op_guard_class |
| 815 |
|
| 816 |
def prepare_op_guard_is_object(self, op): |
| 817 |
obj_loc = self.make_sure_var_in_reg(op.getarg(0)) |
| 818 |
|
| 819 |
|
| 820 |
return [obj_loc] + self._prepare_guard_arglocs(op) |
| 821 |
|
| 822 |
def prepare_op_guard_not_invalidated(self, op): |
| 823 |
return self._prepare_guard_arglocs(op) |
| 824 |
|
| 825 |
def prepare_op_guard_exception(self, op): |
| 826 |
boxes = op.getarglist() |
| 827 |
|
| 828 |
expected_exc_tp_loc = self.make_sure_var_in_reg(boxes[0], boxes) |
| 829 |
if op in self.longevity and self.rm.stays_alive(op): |
| 830 |
res_exc_val_loc = self.force_allocate_reg(op, boxes) |
| 831 |
else: |
| 832 |
res_exc_val_loc = None |
| 833 |
|
| 834 |
arglocs = [expected_exc_tp_loc, res_exc_val_loc] |
| 835 |
return arglocs + self._prepare_guard_arglocs(op) |
| 836 |
|
| 837 |
def prepare_op_guard_no_exception(self, op): |
| 838 |
return self._prepare_guard_arglocs(op) |
| 839 |
|
| 840 |
def prepare_op_save_exception(self, op): |
| 841 |
res = self.force_allocate_reg(op) |
| 842 |
return [res] |
| 843 |
|
| 844 |
prepare_op_save_exc_class = prepare_op_save_exception |
| 845 |
|
| 846 |
def prepare_op_restore_exception(self, op): |
| 847 |
boxes = op.getarglist() |
| 848 |
exc_tp_loc = self.make_sure_var_in_reg(boxes[0], boxes) |
| 849 |
exc_val_loc = self.make_sure_var_in_reg(boxes[1], boxes) |
| 850 |
return [exc_tp_loc, exc_val_loc] |
| 851 |
|
| 852 |
def prepare_op_check_memory_error(self, op): |
| 853 |
l0 = self.make_sure_var_in_reg(op.getarg(0)) |
| 854 |
return [l0] |
| 855 |
|
| 856 |
def _prepare_op_same_as(self, op): |
| 857 |
boxes = op.getarglist() |
| 858 |
a0 = boxes[0] |
| 859 |
if check_imm_box(a0): |
| 860 |
l0 = self.convert_to_imm(a0) |
| 861 |
else: |
| 862 |
l0 = self.make_sure_var_in_reg(a0) |
| 863 |
self.possibly_free_vars_for_op(op) |
| 864 |
self.free_temp_vars() |
| 865 |
res = self.force_allocate_reg(op) |
| 866 |
return [l0, res] |
| 867 |
|
| 868 |
prepare_op_same_as_i = _prepare_op_same_as |
| 869 |
prepare_op_same_as_r = _prepare_op_same_as |
| 870 |
prepare_op_same_as_f = _prepare_op_same_as |
| 871 |
prepare_op_cast_ptr_to_int = _prepare_op_same_as |
| 872 |
prepare_op_cast_int_to_ptr = _prepare_op_same_as |
| 873 |
|
| 874 |
def _prepare_op_math_sqrt(self, op): |
| 875 |
|
| 876 |
l1 = self.make_sure_var_in_reg(op.getarg(1)) |
| 877 |
self.possibly_free_vars_for_op(op) |
| 878 |
self.free_temp_vars() |
| 879 |
res = self.force_allocate_reg(op) |
| 880 |
return [l1, res] |
| 881 |
|
| 882 |
def _prepare_op_threadlocalref_get(self, op): |
| 883 |
|
| 884 |
res = self.force_allocate_reg(op) |
| 885 |
return [res] |
| 886 |
|
| 887 |
def before_call(self, force_store=[], save_all_regs=False): |
| 888 |
|
| 889 |
self.rm.before_call(force_store=force_store, |
| 890 |
save_all_regs=save_all_regs) |
| 891 |
self.fprm.before_call(force_store=force_store, |
| 892 |
save_all_regs=save_all_regs) |
| 893 |
|
| 894 |
def after_call(self, v): |
| 895 |
|
| 896 |
|
| 897 |
if v.type == 'v': |
| 898 |
return |
| 899 |
if v.type == FLOAT: |
| 900 |
return self.fprm.after_call(v) |
| 901 |
else: |
| 902 |
return self.rm.after_call(v) |
| 903 |
|
| 904 |
def _call(self, op, gc_level): |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
save_all_regs = gc_level == 2 |
| 910 |
self.fprm.before_call(save_all_regs=save_all_regs) |
| 911 |
if gc_level == 1 and self.cpu.gc_ll_descr.gcrootmap: |
| 912 |
save_all_regs = 2 |
| 913 |
self.rm.before_call(save_all_regs=save_all_regs) |
| 914 |
resloc = self.after_call(op) |
| 915 |
return resloc |
| 916 |
|
| 917 |
def _prepare_call(self, op, save_all_regs=False, first_arg_index=1): |
| 918 |
locs = [None] * (op.numargs() + 3) |
| 919 |
calldescr = op.getdescr() |
| 920 |
assert isinstance(calldescr, CallDescr) |
| 921 |
assert len(calldescr.arg_classes) == op.numargs() - first_arg_index |
| 922 |
|
| 923 |
for i in range(op.numargs()): |
| 924 |
locs[i + 3] = self.loc(op.getarg(i)) |
| 925 |
|
| 926 |
size = calldescr.get_result_size() |
| 927 |
sign = calldescr.is_result_signed() |
| 928 |
sign_loc = ImmLocation(sign) |
| 929 |
locs[1] = ImmLocation(size) |
| 930 |
locs[2] = sign_loc |
| 931 |
|
| 932 |
effectinfo = calldescr.get_extra_info() |
| 933 |
if save_all_regs: |
| 934 |
gc_level = 2 |
| 935 |
elif effectinfo is None or effectinfo.check_can_collect(): |
| 936 |
gc_level = 1 |
| 937 |
else: |
| 938 |
gc_level = 0 |
| 939 |
|
| 940 |
locs[0] = self._call(op, gc_level) |
| 941 |
return locs |
| 942 |
|
| 943 |
def _prepare_op_call(self, op): |
| 944 |
calldescr = op.getdescr() |
| 945 |
assert calldescr is not None |
| 946 |
effectinfo = calldescr.get_extra_info() |
| 947 |
if effectinfo is not None: |
| 948 |
oopspecindex = effectinfo.oopspecindex |
| 949 |
if oopspecindex == EffectInfo.OS_MATH_SQRT: |
| 950 |
args = self._prepare_op_math_sqrt(op) |
| 951 |
self.assembler.math_sqrt(op, args) |
| 952 |
return |
| 953 |
elif oopspecindex == EffectInfo.OS_THREADLOCALREF_GET: |
| 954 |
args = self._prepare_op_threadlocalref_get(op) |
| 955 |
self.assembler.threadlocalref_get(op, args) |
| 956 |
return |
| 957 |
return self._prepare_call(op) |
| 958 |
|
| 959 |
prepare_op_call_i = _prepare_op_call |
| 960 |
prepare_op_call_f = _prepare_op_call |
| 961 |
prepare_op_call_r = _prepare_op_call |
| 962 |
prepare_op_call_n = _prepare_op_call |
| 963 |
|
| 964 |
def _prepare_op_cond_call(self, op): |
| 965 |
assert 2 <= op.numargs() <= 4 + 2 |
| 966 |
|
| 967 |
func_addr = op.getarg(1) |
| 968 |
assert isinstance(func_addr, Const) |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
self.rm.get_scratch_reg(INT, selected_reg=r.x30) |
| 974 |
|
| 975 |
|
| 976 |
_FUNC_ARGS = [r.x10, r.x11, r.x12, r.x13, r.x14, r.x15, r.x16, r.x17] |
| 977 |
allocated_arg_vars = [] |
| 978 |
for i in range(2, op.numargs()): |
| 979 |
reg = _FUNC_ARGS[i - 2] |
| 980 |
arg = op.getarg(i) |
| 981 |
assert arg.type != FLOAT |
| 982 |
self.make_sure_var_in_reg(arg, allocated_arg_vars, selected_reg=reg) |
| 983 |
allocated_arg_vars.append(arg) |
| 984 |
|
| 985 |
|
| 986 |
argloc = self.make_sure_var_in_reg(op.getarg(0), allocated_arg_vars) |
| 987 |
|
| 988 |
if op.type == 'v': |
| 989 |
|
| 990 |
|
| 991 |
return [argloc] |
| 992 |
else: |
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
args = op.getarglist() |
| 1003 |
resloc = self.rm.force_result_in_reg(op, args[0], |
| 1004 |
forbidden_vars=args[2:]) |
| 1005 |
return [argloc, resloc] |
| 1006 |
|
| 1007 |
prepare_op_cond_call = _prepare_op_cond_call |
| 1008 |
prepare_op_cond_call_value_i = _prepare_op_cond_call |
| 1009 |
prepare_op_cond_call_value_r = _prepare_op_cond_call |
| 1010 |
|
| 1011 |
def prepare_guard_op_cond_call(self, cond_op, cond_call_op): |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
prepare_cond_op = regalloc_comp_operations[cond_op.getopnum()] |
| 1017 |
cond_op_arglocs = prepare_cond_op(self, cond_op) |
| 1018 |
asm_comp_operations[cond_op.getopnum()](self.assembler, cond_op, |
| 1019 |
cond_op_arglocs) |
| 1020 |
|
| 1021 |
|
| 1022 |
op_arglocs = self._prepare_op_cond_call(cond_call_op) |
| 1023 |
return (op_arglocs, COND_INVALID) |
| 1024 |
|
| 1025 |
def _prepare_op_cond_call_gc_wb(self, op): |
| 1026 |
|
| 1027 |
tmplocs = [self.rm.get_scratch_reg(INT)] |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
args = op.getarglist() |
| 1032 |
arglocs = [self.make_sure_var_in_reg(op.getarg(i), args) |
| 1033 |
for i in range(op.numargs())] |
| 1034 |
return tmplocs + arglocs |
| 1035 |
|
| 1036 |
prepare_op_cond_call_gc_wb = _prepare_op_cond_call_gc_wb |
| 1037 |
prepare_op_cond_call_gc_wb_array = _prepare_op_cond_call_gc_wb |
| 1038 |
|
| 1039 |
def prepare_guard_op_guard_not_forced(self, op, guard_op): |
| 1040 |
if rop.is_call_release_gil(op.getopnum()): |
| 1041 |
arglocs = self._prepare_call(op, save_all_regs=True, |
| 1042 |
first_arg_index=2) |
| 1043 |
elif rop.is_call_assembler(op.getopnum()): |
| 1044 |
locs = self.locs_for_call_assembler(op) |
| 1045 |
resloc = self._call(op, gc_level=2) |
| 1046 |
arglocs = locs + [resloc] |
| 1047 |
else: |
| 1048 |
assert rop.is_call_may_force(op.getopnum()) |
| 1049 |
arglocs = self._prepare_call(op, save_all_regs=True) |
| 1050 |
guard_arglocs = self._prepare_guard_arglocs(guard_op) |
| 1051 |
return arglocs + guard_arglocs, len(arglocs) |
| 1052 |
|
| 1053 |
def prepare_op_guard_not_forced_2(self, op): |
| 1054 |
self.rm.before_call(force_store=op.getfailargs(), save_all_regs=True) |
| 1055 |
self.fprm.before_call(force_store=op.getfailargs(), save_all_regs=True) |
| 1056 |
arglocs = self._prepare_guard_arglocs(op) |
| 1057 |
return arglocs |
| 1058 |
|
| 1059 |
def prepare_op_call_malloc_nursery(self, op): |
| 1060 |
size_box = op.getarg(0) |
| 1061 |
assert isinstance(size_box, ConstInt) |
| 1062 |
|
| 1063 |
|
| 1064 |
self.rm.get_scratch_reg(INT, selected_reg=r.x10) |
| 1065 |
self.rm.get_scratch_reg(INT, selected_reg=r.x11) |
| 1066 |
|
| 1067 |
gcmap = self.get_gcmap([r.x10, r.x11]) |
| 1068 |
|
| 1069 |
self.rm.free_temp_vars() |
| 1070 |
self.rm.force_allocate_reg(op, selected_reg=r.x10) |
| 1071 |
|
| 1072 |
|
| 1073 |
gc_ll_descr = self.cpu.gc_ll_descr |
| 1074 |
self.assembler.malloc_cond( |
| 1075 |
gc_ll_descr.get_nursery_free_addr(), |
| 1076 |
gc_ll_descr.get_nursery_top_addr(), size_box.getint(), gcmap) |
| 1077 |
|
| 1078 |
def prepare_op_call_malloc_nursery_varsize_frame(self, op): |
| 1079 |
size_box = op.getarg(0) |
| 1080 |
assert not isinstance(size_box, ConstInt) |
| 1081 |
|
| 1082 |
|
| 1083 |
self.rm.get_scratch_reg(INT, selected_reg=r.x10) |
| 1084 |
self.rm.get_scratch_reg(INT, selected_reg=r.x11) |
| 1085 |
size_loc = self.rm.make_sure_var_in_reg(size_box) |
| 1086 |
|
| 1087 |
gcmap = self.get_gcmap([r.x10, r.x11]) |
| 1088 |
|
| 1089 |
self.rm.possibly_free_vars_for_op(op) |
| 1090 |
self.rm.free_temp_vars() |
| 1091 |
self.rm.force_allocate_reg(op, selected_reg=r.x10) |
| 1092 |
|
| 1093 |
|
| 1094 |
gc_ll_descr = self.cpu.gc_ll_descr |
| 1095 |
self.assembler.malloc_cond_varsize_frame( |
| 1096 |
gc_ll_descr.get_nursery_free_addr(), |
| 1097 |
gc_ll_descr.get_nursery_top_addr(), size_loc, gcmap) |
| 1098 |
|
| 1099 |
def prepare_op_call_malloc_nursery_varsize(self, op): |
| 1100 |
|
| 1101 |
|
| 1102 |
gc_ll_descr = self.cpu.gc_ll_descr |
| 1103 |
if not hasattr(gc_ll_descr, 'max_size_of_young_obj'): |
| 1104 |
raise Exception('unreachable code') |
| 1105 |
|
| 1106 |
|
| 1107 |
arraydescr = op.getdescr() |
| 1108 |
kind = op.getarg(0).getint() |
| 1109 |
itemsize = op.getarg(1).getint() |
| 1110 |
length_box = op.getarg(2) |
| 1111 |
assert not isinstance(length_box, Const) |
| 1112 |
|
| 1113 |
|
| 1114 |
self.rm.get_scratch_reg(INT, selected_reg=r.x10) |
| 1115 |
self.rm.get_scratch_reg(INT, selected_reg=r.x11) |
| 1116 |
if kind == rewrite.FLAG_ARRAY: |
| 1117 |
self.rm.get_scratch_reg(INT, selected_reg=r.x12) |
| 1118 |
|
| 1119 |
length_loc = self.rm.make_sure_var_in_reg(length_box) |
| 1120 |
|
| 1121 |
if kind == rewrite.FLAG_ARRAY: |
| 1122 |
gcmap = self.get_gcmap([r.x10, r.x11, r.x12]) |
| 1123 |
else: |
| 1124 |
gcmap = self.get_gcmap([r.x10, r.x11]) |
| 1125 |
|
| 1126 |
self.rm.possibly_free_vars_for_op(op) |
| 1127 |
self.rm.free_temp_vars() |
| 1128 |
self.rm.force_allocate_reg(op, selected_reg=r.x10) |
| 1129 |
|
| 1130 |
|
| 1131 |
maxlength = (gc_ll_descr.max_size_of_young_obj - XLEN * 2) / itemsize |
| 1132 |
self.assembler.malloc_cond_varsize( |
| 1133 |
kind, gc_ll_descr.get_nursery_free_addr(), |
| 1134 |
gc_ll_descr.get_nursery_top_addr(), length_loc, itemsize, |
| 1135 |
maxlength, gcmap, arraydescr) |
| 1136 |
|
| 1137 |
def prepare_op_zero_array(self, op): |
| 1138 |
|
| 1139 |
|
| 1140 |
return [] |
| 1141 |
|
| 1142 |
def prepare_op_force_token(self, op): |
| 1143 |
res = self.force_allocate_reg(op) |
| 1144 |
return [res] |
| 1145 |
|
| 1146 |
def prepare_op_load_from_gc_table(self, op): |
| 1147 |
res = self.force_allocate_reg(op) |
| 1148 |
return [res] |
| 1149 |
|
| 1150 |
def prepare_op_enter_portal_frame(self, op): |
| 1151 |
return [] |
| 1152 |
|
| 1153 |
def prepare_op_leave_portal_frame(self, op): |
| 1154 |
return [] |
| 1155 |
|
| 1156 |
def prepare_op_keepalive(self, op): |
| 1157 |
return [] |
| 1158 |
|
| 1159 |
def prepare_op_jit_debug(self, op): |
| 1160 |
return [] |
| 1161 |
|
| 1162 |
def prepare_op_increment_debug_counter(self, op): |
| 1163 |
boxes = op.getarglist() |
| 1164 |
a0 = boxes[0] |
| 1165 |
base_loc = self.make_sure_var_in_reg(a0, boxes) |
| 1166 |
return [base_loc] |
| 1167 |
|
| 1168 |
def compute_hint_frame_locations(self, operations): |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
op = operations[-1] |
| 1176 |
if op.getopnum() != rop.JUMP: |
| 1177 |
return |
| 1178 |
|
| 1179 |
self.final_jump_op = op |
| 1180 |
descr = op.getdescr() |
| 1181 |
assert isinstance(descr, TargetToken) |
| 1182 |
if descr._ll_loop_code != 0: |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
self._compute_hint_frame_locations_from_descr(descr) |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
def _compute_hint_frame_locations_from_descr(self, descr): |
| 1194 |
arglocs = descr._riscv_arglocs |
| 1195 |
jump_op = self.final_jump_op |
| 1196 |
assert len(arglocs) == jump_op.numargs() |
| 1197 |
for i in range(jump_op.numargs()): |
| 1198 |
box = jump_op.getarg(i) |
| 1199 |
if not isinstance(box, Const): |
| 1200 |
loc = arglocs[i] |
| 1201 |
if loc is not None and loc.is_stack(): |
| 1202 |
self.frame_manager.add_frame_pos_hint(box, loc) |
| 1203 |
|
| 1204 |
def prepare_op_label(self, op): |
| 1205 |
descr = op.getdescr() |
| 1206 |
assert isinstance(descr, TargetToken) |
| 1207 |
inputargs = op.getarglist() |
| 1208 |
arglocs = [None] * len(inputargs) |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
position = self.rm.position |
| 1214 |
for arg in inputargs: |
| 1215 |
assert not isinstance(arg, Const) |
| 1216 |
if self.longevity[arg].is_last_real_use_before(position): |
| 1217 |
self.force_spill_var(arg) |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
for i in range(len(inputargs)): |
| 1225 |
arg = inputargs[i] |
| 1226 |
assert not isinstance(arg, Const) |
| 1227 |
loc = self.loc(arg) |
| 1228 |
arglocs[i] = loc |
| 1229 |
if loc.is_core_reg() or loc.is_fp_reg(): |
| 1230 |
self.frame_manager.mark_as_free(arg) |
| 1231 |
|
| 1232 |
descr._riscv_arglocs = arglocs |
| 1233 |
descr._riscv_clt = self.assembler.current_clt |
| 1234 |
descr._ll_loop_code = self.assembler.mc.get_relative_pos() |
| 1235 |
self.assembler.target_tokens_currently_compiling[descr] = None |
| 1236 |
self.possibly_free_vars_for_op(op) |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
jump_op = self.final_jump_op |
| 1243 |
if jump_op is not None and jump_op.getdescr() is descr: |
| 1244 |
self._compute_hint_frame_locations_from_descr(descr) |
| 1245 |
return [] |
| 1246 |
|
| 1247 |
def prepare_op_jump(self, op): |
| 1248 |
assert self.jump_target_descr is None |
| 1249 |
descr = op.getdescr() |
| 1250 |
assert isinstance(descr, TargetToken) |
| 1251 |
self.jump_target_descr = descr |
| 1252 |
arglocs = descr._riscv_arglocs |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
self.assembler.check_frame_depth_before_jump(self.jump_target_descr) |
| 1257 |
|
| 1258 |
|
| 1259 |
src_core_locs = [] |
| 1260 |
dst_core_locs = [] |
| 1261 |
scratch_core_reg = r.x31 |
| 1262 |
|
| 1263 |
|
| 1264 |
src_fp_locs = [] |
| 1265 |
dst_fp_locs = [] |
| 1266 |
scratch_fp_reg = r.f31 |
| 1267 |
|
| 1268 |
|
| 1269 |
for i in range(op.numargs()): |
| 1270 |
box = op.getarg(i) |
| 1271 |
src_loc = self.loc(box) |
| 1272 |
dst_loc = arglocs[i] |
| 1273 |
if box.type != FLOAT: |
| 1274 |
src_core_locs.append(src_loc) |
| 1275 |
dst_core_locs.append(dst_loc) |
| 1276 |
else: |
| 1277 |
src_fp_locs.append(src_loc) |
| 1278 |
dst_fp_locs.append(dst_loc) |
| 1279 |
|
| 1280 |
remap_frame_layout_mixed(self.assembler, src_core_locs, dst_core_locs, |
| 1281 |
scratch_core_reg, src_fp_locs, dst_fp_locs, |
| 1282 |
scratch_fp_reg, XLEN) |
| 1283 |
return [] |
| 1284 |
|
| 1285 |
def prepare_op_finish(self, op): |
| 1286 |
if op.numargs() == 1: |
| 1287 |
loc = self.make_sure_var_in_reg(op.getarg(0)) |
| 1288 |
locs = [loc] |
| 1289 |
else: |
| 1290 |
locs = [] |
| 1291 |
return locs |
| 1292 |
|
| 1293 |
def loc(self, var): |
| 1294 |
if var.type == FLOAT: |
| 1295 |
return self.fprm.loc(var) |
| 1296 |
else: |
| 1297 |
return self.rm.loc(var) |
| 1298 |
|
| 1299 |
def make_sure_var_in_reg(self, var, forbidden_vars=[], |
| 1300 |
selected_reg=None, need_lower_byte=False): |
| 1301 |
if var.type == FLOAT: |
| 1302 |
return self.fprm.make_sure_var_in_reg(var, forbidden_vars, |
| 1303 |
selected_reg, need_lower_byte) |
| 1304 |
else: |
| 1305 |
return self.rm.make_sure_var_in_reg(var, forbidden_vars, |
| 1306 |
selected_reg, need_lower_byte) |
| 1307 |
|
| 1308 |
def force_allocate_reg(self, var, forbidden_vars=[], selected_reg=None): |
| 1309 |
if var.type == FLOAT: |
| 1310 |
return self.fprm.force_allocate_reg(var, forbidden_vars, |
| 1311 |
selected_reg) |
| 1312 |
else: |
| 1313 |
return self.rm.force_allocate_reg(var, forbidden_vars, selected_reg) |
| 1314 |
|
| 1315 |
def possibly_free_var(self, var): |
| 1316 |
if var.type == FLOAT: |
| 1317 |
self.fprm.possibly_free_var(var) |
| 1318 |
else: |
| 1319 |
self.rm.possibly_free_var(var) |
| 1320 |
|
| 1321 |
def possibly_free_vars_for_op(self, op): |
| 1322 |
for i in range(op.numargs()): |
| 1323 |
var = op.getarg(i) |
| 1324 |
if var is not None: |
| 1325 |
self.possibly_free_var(var) |
| 1326 |
if op.is_guard(): |
| 1327 |
self.possibly_free_vars(op.getfailargs()) |
| 1328 |
|
| 1329 |
def possibly_free_vars(self, vars): |
| 1330 |
for var in vars: |
| 1331 |
if var is not None: |
| 1332 |
self.possibly_free_var(var) |
| 1333 |
|
| 1334 |
def force_spill_var(self, var): |
| 1335 |
if var.type == FLOAT: |
| 1336 |
self.fprm.force_spill_var(var) |
| 1337 |
else: |
| 1338 |
self.rm.force_spill_var(var) |
| 1339 |
|
| 1340 |
def free_temp_vars(self): |
| 1341 |
self.rm.free_temp_vars() |
| 1342 |
self.fprm.free_temp_vars() |
| 1343 |
|
| 1344 |
def _check_invariants(self): |
| 1345 |
self.rm._check_invariants() |
| 1346 |
self.fprm._check_invariants() |
| 1347 |
|
| 1348 |
def convert_to_imm(self, value): |
| 1349 |
if isinstance(value, ConstInt): |
| 1350 |
return self.rm.convert_to_imm(value) |
| 1351 |
else: |
| 1352 |
assert isinstance(value, ConstFloat) |
| 1353 |
return self.fprm.convert_to_imm(value) |
| 1354 |
|
| 1355 |
def position(self): |
| 1356 |
return self.rm.position |
| 1357 |
|
| 1358 |
def next_instruction(self): |
| 1359 |
self.rm.next_instruction() |
| 1360 |
self.fprm.next_instruction() |
| 1361 |
|
| 1362 |
def get_gcmap(self, forbidden_regs=[], noregs=False): |
| 1363 |
frame_depth = self.frame_manager.get_frame_depth() |
| 1364 |
gcmap = allocate_gcmap(self.assembler, |
| 1365 |
frame_depth, JITFRAME_FIXED_SIZE) |
| 1366 |
for box, loc in self.rm.reg_bindings_iteritems(): |
| 1367 |
if loc in forbidden_regs: |
| 1368 |
continue |
| 1369 |
if box.type == REF and self.rm.is_still_alive(box): |
| 1370 |
assert not noregs |
| 1371 |
assert loc.is_core_reg() |
| 1372 |
val = self.cpu.all_reg_indexes[loc.value] |
| 1373 |
gcmap[val // XLEN // 8] |= r_uint(1) << (val % (XLEN * 8)) |
| 1374 |
for box, loc in self.frame_manager.bindings_iteritems(): |
| 1375 |
if box.type == REF and self.rm.is_still_alive(box): |
| 1376 |
assert loc.is_stack() |
| 1377 |
val = loc.position + JITFRAME_FIXED_SIZE |
| 1378 |
gcmap[val // XLEN // 8] |= r_uint(1) << (val % (XLEN * 8)) |
| 1379 |
return gcmap |
| 1380 |
|
| 1381 |
def get_final_frame_depth(self): |
| 1382 |
return self.frame_manager.get_frame_depth() |
| 1383 |
|
| 1384 |
|
| 1385 |
def not_implemented(self, op): |
| 1386 |
llop.debug_print(lltype.Void, |
| 1387 |
'[riscv/regalloc] %s not implemented' % op.getopname()) |
| 1388 |
raise NotImplementedError(op) |
| 1389 |
|
| 1390 |
def not_implemented_guard_op(self, op, prevop): |
| 1391 |
llop.debug_print(lltype.Void, |
| 1392 |
'[riscv/regalloc] %s not implemented' % op.getopname()) |
| 1393 |
raise NotImplementedError(op) |
| 1394 |
|
| 1395 |
def not_implemented_comp_op(self, op): |
| 1396 |
llop.debug_print(lltype.Void, |
| 1397 |
'[riscv/regalloc] %s not implemented' % op.getopname()) |
| 1398 |
raise NotImplementedError(op) |
| 1399 |
|
| 1400 |
regalloc_operations = [not_implemented] * (rop._LAST + 1) |
| 1401 |
regalloc_guard_operations = [not_implemented_guard_op] * (rop._LAST + 1) |
| 1402 |
regalloc_comp_operations = [not_implemented_comp_op] * (rop._LAST + 1) |
| 1403 |
|
| 1404 |
for key, value in rop.__dict__.items(): |
| 1405 |
key = key.lower() |
| 1406 |
if key.startswith('_'): |
| 1407 |
continue |
| 1408 |
method_name = 'prepare_op_%s' % key |
| 1409 |
if hasattr(Regalloc, method_name): |
| 1410 |
func = getattr(Regalloc, method_name).im_func |
| 1411 |
regalloc_operations[value] = func |
| 1412 |
method_name = 'prepare_guard_op_%s' % key |
| 1413 |
if hasattr(Regalloc, method_name): |
| 1414 |
func = getattr(Regalloc, method_name).im_func |
| 1415 |
regalloc_guard_operations[value] = func |
| 1416 |
method_name = 'prepare_comp_op_%s' % key |
| 1417 |
if hasattr(Regalloc, method_name): |
| 1418 |
func = getattr(Regalloc, method_name).im_func |
| 1419 |
regalloc_comp_operations[value] = func |