| 1 |
|
| 2 |
|
| 3 |
from rpython.jit.backend.llsupport.asmmemmgr import BlockBuilderMixin |
| 4 |
from rpython.jit.backend.riscv import registers as r |
| 5 |
from rpython.jit.backend.riscv.arch import ( |
| 6 |
INST_SIZE, PC_REL_MAX, PC_REL_MIN, SINT12_IMM_MAX, SINT12_IMM_MIN, XLEN) |
| 7 |
from rpython.jit.backend.riscv.instruction_builder import ( |
| 8 |
gen_all_instr_assemblers) |
| 9 |
from rpython.jit.backend.riscv.instruction_util import ( |
| 10 |
COND_BEQ, COND_BGE, COND_BGEU, COND_BLT, COND_BLTU, COND_BNE, |
| 11 |
check_imm_arg) |
| 12 |
from rpython.rlib.objectmodel import we_are_translated |
| 13 |
from rpython.rtyper.lltypesystem import rffi |
| 14 |
from rpython.tool.udir import udir |
| 15 |
|
| 16 |
|
| 17 |
_SINT32_MIN = -2**31 |
| 18 |
_SINT32_MAX = 2**31 - 1 |
| 19 |
_SINT64_MIN = -2**63 |
| 20 |
_SINT64_MAX = 2**63 - 1 |
| 21 |
|
| 22 |
|
| 23 |
MAX_NUM_INSTS_FOR_LOAD_INT_IMM = 2 + (2 if XLEN == 8 else 0) |
| 24 |
|
| 25 |
|
| 26 |
class AbstractRISCVBuilder(object): |
| 27 |
def write32(self, word): |
| 28 |
self.writechar(chr(word & 0xff)) |
| 29 |
self.writechar(chr((word >> 8) & 0xff)) |
| 30 |
self.writechar(chr((word >> 16) & 0xff)) |
| 31 |
self.writechar(chr((word >> 24) & 0xff)) |
| 32 |
|
| 33 |
def write64(self, word): |
| 34 |
self.writechar(chr(word & 0xff)) |
| 35 |
self.writechar(chr((word >> 8) & 0xff)) |
| 36 |
self.writechar(chr((word >> 16) & 0xff)) |
| 37 |
self.writechar(chr((word >> 24) & 0xff)) |
| 38 |
self.writechar(chr((word >> 32) & 0xff)) |
| 39 |
self.writechar(chr((word >> 40) & 0xff)) |
| 40 |
self.writechar(chr((word >> 48) & 0xff)) |
| 41 |
self.writechar(chr((word >> 56) & 0xff)) |
| 42 |
|
| 43 |
|
| 44 |
def NOP(self): |
| 45 |
self.ADDI(r.zero.value, r.zero.value, 0) |
| 46 |
|
| 47 |
|
| 48 |
def MV(self, rd, rs1): |
| 49 |
self.ADDI(rd, rs1, 0) |
| 50 |
|
| 51 |
|
| 52 |
def FMV_D(self, rd, rs1): |
| 53 |
self.FSGNJ_D(rd, rs1, rs1) |
| 54 |
|
| 55 |
|
| 56 |
def J(self, imm): |
| 57 |
self.JAL(r.zero.value, imm) |
| 58 |
|
| 59 |
|
| 60 |
def JR(self, rs1): |
| 61 |
self.JALR(r.zero.value, rs1, 0) |
| 62 |
|
| 63 |
|
| 64 |
def RET(self): |
| 65 |
self.JALR(r.zero.value, r.ra.value, 0) |
| 66 |
|
| 67 |
|
| 68 |
def NOT(self, rd, rs1): |
| 69 |
self.XORI(rd, rs1, -1) |
| 70 |
|
| 71 |
|
| 72 |
def NEG(self, rd, rs1): |
| 73 |
self.SUB(rd, r.zero.value, rs1) |
| 74 |
|
| 75 |
|
| 76 |
def SEQZ(self, rd, rs1): |
| 77 |
self.SLTIU(rd, rs1, 1) |
| 78 |
|
| 79 |
|
| 80 |
def SNEZ(self, rd, rs1): |
| 81 |
self.SLTU(rd, r.zero.value, rs1) |
| 82 |
|
| 83 |
|
| 84 |
def SLTZ(self, rd, rs1): |
| 85 |
self.SLT(rd, rs1, r.zero.value) |
| 86 |
|
| 87 |
|
| 88 |
def SGTZ(self, rd, rs1): |
| 89 |
self.SLT(rd, r.zero.value, rs1) |
| 90 |
|
| 91 |
|
| 92 |
def FNEG_D(self, rd, rs1): |
| 93 |
self.FSGNJN_D(rd, rs1, rs1) |
| 94 |
|
| 95 |
|
| 96 |
def FABS_D(self, rd, rs1): |
| 97 |
self.FSGNJX_D(rd, rs1, rs1) |
| 98 |
|
| 99 |
|
| 100 |
def BEQZ(self, rs1, offset): |
| 101 |
self.BEQ(rs1, r.zero.value, offset) |
| 102 |
|
| 103 |
|
| 104 |
def BNEZ(self, rs1, offset): |
| 105 |
self.BNE(rs1, r.zero.value, offset) |
| 106 |
|
| 107 |
|
| 108 |
def BLEZ(self, rs1, offset): |
| 109 |
self.BGE(r.zero.value, rs1, offset) |
| 110 |
|
| 111 |
|
| 112 |
def BGEZ(self, rs1, offset): |
| 113 |
self.BGE(rs1, r.zero.value, offset) |
| 114 |
|
| 115 |
|
| 116 |
def BLTZ(self, rs1, offset): |
| 117 |
self.BLT(rs1, r.zero.value, offset) |
| 118 |
|
| 119 |
|
| 120 |
def BGTZ(self, rs1, offset): |
| 121 |
self.BLT(r.zero.value, rs1, offset) |
| 122 |
|
| 123 |
|
| 124 |
def load_int(self, rd, rs1, imm): |
| 125 |
self.LD(rd, rs1, imm) |
| 126 |
|
| 127 |
|
| 128 |
def store_int(self, rs2, rs1, imm): |
| 129 |
self.SD(rs2, rs1, imm) |
| 130 |
|
| 131 |
|
| 132 |
def load_int_from_base_plus_offset(self, rd, rs1, imm, tmp=-1): |
| 133 |
if tmp == -1: |
| 134 |
tmp = rd |
| 135 |
assert tmp != rs1 |
| 136 |
if check_imm_arg(imm): |
| 137 |
self.load_int(rd, rs1, imm) |
| 138 |
else: |
| 139 |
self.load_int_imm(tmp, imm) |
| 140 |
self.ADD(tmp, tmp, rs1) |
| 141 |
self.load_int(rd, tmp, 0) |
| 142 |
|
| 143 |
|
| 144 |
def store_int_to_base_plus_offset(self, rs2, rs1, imm, tmp): |
| 145 |
assert tmp != rs2 and tmp != rs1 |
| 146 |
if check_imm_arg(imm): |
| 147 |
self.store_int(rs2, rs1, imm) |
| 148 |
else: |
| 149 |
self.load_int_imm(tmp, imm) |
| 150 |
self.ADD(tmp, tmp, rs1) |
| 151 |
self.store_int(rs2, tmp, 0) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
def atomic_swap_int(self, rd, rs2, rs1, acrl): |
| 156 |
self.AMOSWAP_D(rd, rs2, rs1, acrl) |
| 157 |
|
| 158 |
|
| 159 |
def load_reserve_int(self, rd, rs1, acrl): |
| 160 |
self.LR_D(rd, rs1, acrl) |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
def store_conditional_int(self, rd, rs2, rs1, acrl): |
| 165 |
self.SC_D(rd, rs2, rs1, acrl) |
| 166 |
|
| 167 |
|
| 168 |
def load_float(self, rd, rs1, imm): |
| 169 |
self.FLD(rd, rs1, imm) |
| 170 |
|
| 171 |
|
| 172 |
def store_float(self, rs2, rs1, imm): |
| 173 |
self.FSD(rs2, rs1, imm) |
| 174 |
|
| 175 |
|
| 176 |
def load_float_from_base_plus_offset(self, rd, rs1, imm, tmp): |
| 177 |
assert tmp != rs1 |
| 178 |
if check_imm_arg(imm): |
| 179 |
self.load_float(rd, rs1, imm) |
| 180 |
else: |
| 181 |
self.load_int_imm(tmp, imm) |
| 182 |
self.ADD(tmp, tmp, rs1) |
| 183 |
self.load_float(rd, tmp, 0) |
| 184 |
|
| 185 |
|
| 186 |
def store_float_to_base_plus_offset(self, rs2, rs1, imm, tmp): |
| 187 |
assert tmp != rs1 |
| 188 |
if check_imm_arg(imm): |
| 189 |
self.store_float(rs2, rs1, imm) |
| 190 |
else: |
| 191 |
self.load_int_imm(tmp, imm) |
| 192 |
self.ADD(tmp, tmp, rs1) |
| 193 |
self.store_float(rs2, tmp, 0) |
| 194 |
|
| 195 |
|
| 196 |
def load_rffi_int(self, rd, rs1, imm): |
| 197 |
|
| 198 |
self.LW(rd, rs1, imm) |
| 199 |
|
| 200 |
|
| 201 |
def store_rffi_int(self, rs2, rs1, imm): |
| 202 |
self.SW(rs2, rs1, imm) |
| 203 |
|
| 204 |
|
| 205 |
def load_rffi_int_from_base_plus_offset(self, rd, rs1, imm, tmp=-1): |
| 206 |
if tmp == -1: |
| 207 |
tmp = rd |
| 208 |
assert tmp != rs1 |
| 209 |
if check_imm_arg(imm): |
| 210 |
self.load_rffi_int(rd, rs1, imm) |
| 211 |
else: |
| 212 |
self.load_int_imm(tmp, imm) |
| 213 |
self.ADD(tmp, tmp, rs1) |
| 214 |
self.load_rffi_int(rd, tmp, 0) |
| 215 |
|
| 216 |
|
| 217 |
def store_rffi_int_to_base_plus_offset(self, rs2, rs1, imm, tmp): |
| 218 |
assert tmp != rs2 and tmp != rs1 |
| 219 |
if check_imm_arg(imm): |
| 220 |
self.store_rffi_int(rs2, rs1, imm) |
| 221 |
else: |
| 222 |
self.load_int_imm(tmp, imm) |
| 223 |
self.ADD(tmp, tmp, rs1) |
| 224 |
self.store_rffi_int(rs2, tmp, 0) |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@staticmethod |
| 230 |
def split_imm32(offset): |
| 231 |
lower = offset & 0xfff |
| 232 |
if lower >= 0x800: |
| 233 |
lower -= 0x1000 |
| 234 |
offset += 0x1000 |
| 235 |
upper = ((offset >> 12) & 0xfffff) |
| 236 |
return (upper, lower) |
| 237 |
|
| 238 |
|
| 239 |
def load_int_pc_rel(self, rd, offset): |
| 240 |
assert PC_REL_MIN <= offset <= PC_REL_MAX |
| 241 |
upper, lower = self.split_imm32(offset) |
| 242 |
self.AUIPC(rd, upper) |
| 243 |
self.load_int(rd, rd, lower) |
| 244 |
|
| 245 |
|
| 246 |
def load_float_pc_rel(self, rd, offset, scratch_reg=r.x31.value): |
| 247 |
assert PC_REL_MIN <= offset <= PC_REL_MAX |
| 248 |
upper, lower = self.split_imm32(offset) |
| 249 |
self.AUIPC(scratch_reg, upper) |
| 250 |
self.load_float(rd, scratch_reg, lower) |
| 251 |
|
| 252 |
|
| 253 |
def jalr_pc_rel(self, rd, offset): |
| 254 |
assert int(rd) != 0 |
| 255 |
assert PC_REL_MIN <= offset <= PC_REL_MAX |
| 256 |
upper, lower = self.split_imm32(offset) |
| 257 |
self.AUIPC(rd, upper) |
| 258 |
self.JALR(rd, rd, lower) |
| 259 |
|
| 260 |
|
| 261 |
def jal_abs(self, rd, abs_addr): |
| 262 |
scratch_reg = r.x31 |
| 263 |
self.load_int_imm(scratch_reg.value, abs_addr) |
| 264 |
self.JALR(rd, scratch_reg.value, 0) |
| 265 |
|
| 266 |
|
| 267 |
def load_int_imm(self, rd, imm): |
| 268 |
if SINT12_IMM_MIN <= imm <= SINT12_IMM_MAX: |
| 269 |
self.ADDI(rd, r.zero.value, imm) |
| 270 |
return |
| 271 |
elif _SINT32_MIN <= imm <= _SINT32_MAX: |
| 272 |
upper, lower = self.split_imm32(imm) |
| 273 |
self.LUI(rd, upper) |
| 274 |
if lower: |
| 275 |
self.ADDIW(rd, rd, lower) |
| 276 |
return |
| 277 |
|
| 278 |
|
| 279 |
assert _SINT64_MIN <= imm <= _SINT64_MAX |
| 280 |
load_inst_pos = self._get_relative_pos_for_load_imm() |
| 281 |
self.append_pending_int_constant(load_inst_pos, rd, imm) |
| 282 |
self.EBREAK() |
| 283 |
self.NOP() |
| 284 |
|
| 285 |
|
| 286 |
def load_float_imm(self, rd, imm): |
| 287 |
load_inst_pos = self._get_relative_pos_for_load_imm() |
| 288 |
self.append_pending_float_constant(load_inst_pos, rd, imm) |
| 289 |
self.EBREAK() |
| 290 |
self.NOP() |
| 291 |
|
| 292 |
def _get_relative_pos_for_load_imm(self): |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
raise NotImplementedError() |
| 300 |
|
| 301 |
gen_all_instr_assemblers(AbstractRISCVBuilder) |
| 302 |
|
| 303 |
BRANCH_BUILDER = { |
| 304 |
COND_BEQ: AbstractRISCVBuilder.BEQ, |
| 305 |
COND_BNE: AbstractRISCVBuilder.BNE, |
| 306 |
|
| 307 |
COND_BGE: AbstractRISCVBuilder.BGE, |
| 308 |
COND_BLT: AbstractRISCVBuilder.BLT, |
| 309 |
|
| 310 |
COND_BGEU: AbstractRISCVBuilder.BGEU, |
| 311 |
COND_BLTU: AbstractRISCVBuilder.BLTU, |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
class OverwritingBuilder(AbstractRISCVBuilder): |
| 316 |
def __init__(self, cb, start, size): |
| 317 |
AbstractRISCVBuilder.__init__(self) |
| 318 |
self.cb = cb |
| 319 |
self.index = start |
| 320 |
self.start = start |
| 321 |
self.end = start + size |
| 322 |
|
| 323 |
def currpos(self): |
| 324 |
return self.index |
| 325 |
|
| 326 |
def writechar(self, char): |
| 327 |
assert self.index <= self.end |
| 328 |
self.cb.overwrite(self.index, char) |
| 329 |
self.index += 1 |
| 330 |
|
| 331 |
def append_pending_int_constant(self, load_inst_pos, reg, const_value): |
| 332 |
self.cb.append_pending_int_constant(load_inst_pos, reg, const_value) |
| 333 |
|
| 334 |
def append_pending_float_constant(self, load_inst_pos, reg, const_value): |
| 335 |
self.cb.append_pending_float_constant(load_inst_pos, reg, const_value) |
| 336 |
|
| 337 |
def _get_relative_pos_for_load_imm(self): |
| 338 |
return self.index |
| 339 |
|
| 340 |
|
| 341 |
class InstrBuilder(BlockBuilderMixin, AbstractRISCVBuilder): |
| 342 |
def __init__(self): |
| 343 |
AbstractRISCVBuilder.__init__(self) |
| 344 |
self.init_block_builder() |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
self.ops_offset = {} |
| 349 |
|
| 350 |
|
| 351 |
self._int_const_pool = {} |
| 352 |
self._float_const_pool = {} |
| 353 |
|
| 354 |
def mark_op(self, op): |
| 355 |
self.ops_offset[op] = self.get_relative_pos() |
| 356 |
|
| 357 |
def _dump_trace(self, addr, name, formatter=-1): |
| 358 |
if not we_are_translated(): |
| 359 |
if formatter != -1: |
| 360 |
name = name % formatter |
| 361 |
dir = udir.ensure('asm', dir=True) |
| 362 |
f = dir.join(name).open('wb') |
| 363 |
data = rffi.cast(rffi.CCHARP, addr) |
| 364 |
for i in range(self.get_relative_pos()): |
| 365 |
f.write(data[i]) |
| 366 |
f.close() |
| 367 |
|
| 368 |
def materialize(self, cpu, allblocks, gcrootmap=None): |
| 369 |
assert not self._int_const_pool and not self._float_const_pool |
| 370 |
return BlockBuilderMixin.materialize(self, cpu, allblocks, gcrootmap) |
| 371 |
|
| 372 |
def append_pending_int_constant(self, load_inst_pos, reg, const_value): |
| 373 |
self._int_const_pool[load_inst_pos] = (reg, const_value) |
| 374 |
|
| 375 |
def append_pending_float_constant(self, load_inst_pos, reg, const_value): |
| 376 |
self._float_const_pool[load_inst_pos] = (reg, const_value) |
| 377 |
|
| 378 |
def _get_relative_pos_for_load_imm(self): |
| 379 |
return self.get_relative_pos() |
| 380 |
|
| 381 |
|
| 382 |
def _emit_pending_constants(self): |
| 383 |
int_const_pool = self._int_const_pool |
| 384 |
if int_const_pool: |
| 385 |
self._int_const_pool = {} |
| 386 |
|
| 387 |
float_const_pool = self._float_const_pool |
| 388 |
if float_const_pool: |
| 389 |
self._float_const_pool = {} |
| 390 |
|
| 391 |
if not int_const_pool and not float_const_pool: |
| 392 |
return |
| 393 |
|
| 394 |
|
| 395 |
for i in range(self._get_relative_pos_for_load_imm() % 8): |
| 396 |
self.writechar(chr(0)) |
| 397 |
|
| 398 |
const_value_pos_dict = {} |
| 399 |
|
| 400 |
for is_float, const_pool in [(True, float_const_pool), |
| 401 |
(False, int_const_pool)]: |
| 402 |
for inst_pos, pair in const_pool.iteritems(): |
| 403 |
reg, const_value = pair |
| 404 |
|
| 405 |
|
| 406 |
try: |
| 407 |
|
| 408 |
const_pos = const_value_pos_dict[const_value] |
| 409 |
except KeyError: |
| 410 |
const_pos = self._get_relative_pos_for_load_imm() |
| 411 |
self.write64(const_value) |
| 412 |
const_value_pos_dict[const_value] = const_pos |
| 413 |
|
| 414 |
offset = const_pos - inst_pos |
| 415 |
|
| 416 |
|
| 417 |
pmc = OverwritingBuilder(self, inst_pos, INST_SIZE * 2) |
| 418 |
if is_float: |
| 419 |
pmc.load_float_pc_rel(reg, offset) |
| 420 |
else: |
| 421 |
pmc.load_int_pc_rel(reg, offset) |
| 422 |
|
| 423 |
InstrBuilder.emit_pending_constants = _emit_pending_constants |