| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
// Copyright (c) 1994-2006 Sun Microsystems Inc. |
| 8 |
// All Rights Reserved. |
| 9 |
// |
| 10 |
// Redistribution and use in source and binary forms, with or without |
| 11 |
// modification, are permitted provided that the following conditions are |
| 12 |
// met: |
| 13 |
// |
| 14 |
// - Redistributions of source code must retain the above copyright notice, |
| 15 |
// this list of conditions and the following disclaimer. |
| 16 |
// |
| 17 |
// - Redistribution in binary form must reproduce the above copyright |
| 18 |
// notice, this list of conditions and the following disclaimer in the |
| 19 |
// documentation and/or other materials provided with the distribution. |
| 20 |
// |
| 21 |
// - Neither the name of Sun Microsystems or the names of contributors may |
| 22 |
// be used to endorse or promote products derived from this software without |
| 23 |
// specific prior written permission. |
| 24 |
// |
| 25 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 26 |
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 27 |
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 28 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 29 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 30 |
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 31 |
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 32 |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 33 |
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 34 |
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 |
|
| 37 |
// The original source code covered by the above license above has been |
| 38 |
// modified significantly by Google Inc. |
| 39 |
// Copyright 2021 the V8 project authors. All rights reserved. |
| 40 |
#include "jit/riscv64/Assembler-riscv64.h" |
| 41 |
|
| 42 |
#include "mozilla/DebugOnly.h" |
| 43 |
#include "mozilla/Maybe.h" |
| 44 |
|
| 45 |
#include "gc/Marking.h" |
| 46 |
#include "jit/AutoWritableJitCode.h" |
| 47 |
#include "jit/ExecutableAllocator.h" |
| 48 |
#include "jit/riscv64/disasm/Disasm-riscv64.h" |
| 49 |
#include "vm/Realm.h" |
| 50 |
|
| 51 |
using mozilla::DebugOnly; |
| 52 |
namespace js { |
| 53 |
namespace jit { |
| 54 |
|
| 55 |
#define UNIMPLEMENTED_RISCV() MOZ_CRASH("RISC_V not implemented"); |
| 56 |
|
| 57 |
bool Assembler::FLAG_riscv_debug = false; |
| 58 |
|
| 59 |
void Assembler::nop() { addi(ToRegister(0), ToRegister(0), 0); } |
| 60 |
|
| 61 |
// Size of the instruction stream, in bytes. |
| 62 |
size_t Assembler::size() const { return m_buffer.size(); } |
| 63 |
|
| 64 |
bool Assembler::swapBuffer(wasm::Bytes& bytes) { |
| 65 |
// For now, specialize to the one use case. As long as wasm::Bytes is a |
| 66 |
// Vector, not a linked-list of chunks, there's not much we can do other |
| 67 |
// than copy. |
| 68 |
MOZ_ASSERT(bytes.empty()); |
| 69 |
if (!bytes.resize(bytesNeeded())) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
m_buffer.executableCopy(bytes.begin()); |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
// Size of the relocation table, in bytes. |
| 77 |
size_t Assembler::jumpRelocationTableBytes() const { |
| 78 |
return jumpRelocations_.length(); |
| 79 |
} |
| 80 |
|
| 81 |
size_t Assembler::dataRelocationTableBytes() const { |
| 82 |
return dataRelocations_.length(); |
| 83 |
} |
| 84 |
// Size of the data table, in bytes. |
| 85 |
size_t Assembler::bytesNeeded() const { |
| 86 |
return size() + jumpRelocationTableBytes() + dataRelocationTableBytes(); |
| 87 |
} |
| 88 |
|
| 89 |
void Assembler::executableCopy(uint8_t* buffer) { |
| 90 |
MOZ_ASSERT(isFinished); |
| 91 |
m_buffer.executableCopy(buffer); |
| 92 |
} |
| 93 |
|
| 94 |
uint32_t Assembler::AsmPoolMaxOffset = 1024; |
| 95 |
|
| 96 |
uint32_t Assembler::GetPoolMaxOffset() { |
| 97 |
static bool isSet = false; |
| 98 |
if (!isSet) { |
| 99 |
char* poolMaxOffsetStr = getenv("ASM_POOL_MAX_OFFSET"); |
| 100 |
uint32_t poolMaxOffset; |
| 101 |
if (poolMaxOffsetStr && |
| 102 |
sscanf(poolMaxOffsetStr, "%u", &poolMaxOffset) == 1) { |
| 103 |
AsmPoolMaxOffset = poolMaxOffset; |
| 104 |
} |
| 105 |
isSet = true; |
| 106 |
} |
| 107 |
return AsmPoolMaxOffset; |
| 108 |
} |
| 109 |
|
| 110 |
// Pool callbacks stuff: |
| 111 |
void Assembler::InsertIndexIntoTag(uint8_t* load_, uint32_t index) { |
| 112 |
MOZ_CRASH("Unimplement"); |
| 113 |
} |
| 114 |
|
| 115 |
void Assembler::PatchConstantPoolLoad(void* loadAddr, void* constPoolAddr) { |
| 116 |
MOZ_CRASH("Unimplement"); |
| 117 |
} |
| 118 |
|
| 119 |
void Assembler::processCodeLabels(uint8_t* rawCode) { |
| 120 |
for (const CodeLabel& label : codeLabels_) { |
| 121 |
Bind(rawCode, label); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void Assembler::WritePoolGuard(BufferOffset branch, Instruction* dest, |
| 126 |
BufferOffset afterPool) { |
| 127 |
DEBUG_PRINTF("\tWritePoolGuard\n"); |
| 128 |
int32_t off = afterPool.getOffset() - branch.getOffset(); |
| 129 |
if (!is_int21(off) || !((off & 0x1) == 0)) { |
| 130 |
printf("%d\n", off); |
| 131 |
MOZ_CRASH("imm invalid"); |
| 132 |
} |
| 133 |
// JAL encode is |
| 134 |
// 31 | 30 21 | 20 | 19 12 | 11 7 | 6 0 | |
| 135 |
// imm[20] | imm[10:1] | imm[11] | imm[19:12] | rd | opcode| |
| 136 |
// 1 10 1 8 5 7 |
| 137 |
// offset[20:1] dest JAL |
| 138 |
int32_t imm20 = (off & 0xff000) | // bits 19-12 |
| 139 |
((off & 0x800) << 9) | // bit 11 |
| 140 |
((off & 0x7fe) << 20) | // bits 10-1 |
| 141 |
((off & 0x100000) << 11); // bit 20 |
| 142 |
Instr instr = JAL | (imm20 & kImm20Mask); |
| 143 |
dest->SetInstructionBits(instr); |
| 144 |
DEBUG_PRINTF("%p(%x): ", dest, branch.getOffset()); |
| 145 |
disassembleInstr(dest->InstructionBits(), JitSpew_Codegen); |
| 146 |
} |
| 147 |
|
| 148 |
void Assembler::WritePoolHeader(uint8_t* start, Pool* p, bool isNatural) { |
| 149 |
static_assert(sizeof(PoolHeader) == 4); |
| 150 |
|
| 151 |
// Get the total size of the pool. |
| 152 |
const uintptr_t totalPoolSize = sizeof(PoolHeader) + p->getPoolSize(); |
| 153 |
const uintptr_t totalPoolInstructions = totalPoolSize / kInstrSize; |
| 154 |
|
| 155 |
MOZ_ASSERT((totalPoolSize & 0x3) == 0); |
| 156 |
MOZ_ASSERT(totalPoolInstructions < (1 << 15)); |
| 157 |
|
| 158 |
PoolHeader header(totalPoolInstructions, isNatural); |
| 159 |
*(PoolHeader*)start = header; |
| 160 |
} |
| 161 |
|
| 162 |
void Assembler::copyJumpRelocationTable(uint8_t* dest) { |
| 163 |
if (jumpRelocations_.length()) { |
| 164 |
memcpy(dest, jumpRelocations_.buffer(), jumpRelocations_.length()); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void Assembler::copyDataRelocationTable(uint8_t* dest) { |
| 169 |
if (dataRelocations_.length()) { |
| 170 |
memcpy(dest, dataRelocations_.buffer(), dataRelocations_.length()); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void Assembler::RV_li(Register rd, int64_t imm) { |
| 175 |
UseScratchRegisterScope temps(this); |
| 176 |
if (RecursiveLiCount(imm) > GeneralLiCount(imm, temps.hasAvailable())) { |
| 177 |
GeneralLi(rd, imm); |
| 178 |
} else { |
| 179 |
RecursiveLi(rd, imm); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
int Assembler::RV_li_count(int64_t imm, bool is_get_temp_reg) { |
| 184 |
if (RecursiveLiCount(imm) > GeneralLiCount(imm, is_get_temp_reg)) { |
| 185 |
return GeneralLiCount(imm, is_get_temp_reg); |
| 186 |
} else { |
| 187 |
return RecursiveLiCount(imm); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void Assembler::GeneralLi(Register rd, int64_t imm) { |
| 192 |
// 64-bit imm is put in the register rd. |
| 193 |
// In most cases the imm is 32 bit and 2 instructions are generated. If a |
| 194 |
// temporary register is available, in the worst case, 6 instructions are |
| 195 |
// generated for a full 64-bit immediate. If temporay register is not |
| 196 |
// available the maximum will be 8 instructions. If imm is more than 32 bits |
| 197 |
// and a temp register is available, imm is divided into two 32-bit parts, |
| 198 |
// low_32 and up_32. Each part is built in a separate register. low_32 is |
| 199 |
// built before up_32. If low_32 is negative (upper 32 bits are 1), 0xffffffff |
| 200 |
// is subtracted from up_32 before up_32 is built. This compensates for 32 |
| 201 |
// bits of 1's in the lower when the two registers are added. If no temp is |
| 202 |
// available, the upper 32 bit is built in rd, and the lower 32 bits are |
| 203 |
// devided to 3 parts (11, 11, and 10 bits). The parts are shifted and added |
| 204 |
// to the upper part built in rd. |
| 205 |
if (is_int32(imm + 0x800)) { |
| 206 |
// 32-bit case. Maximum of 2 instructions generated |
| 207 |
int64_t high_20 = ((imm + 0x800) >> 12); |
| 208 |
int64_t low_12 = imm << 52 >> 52; |
| 209 |
if (high_20) { |
| 210 |
lui(rd, (int32_t)high_20); |
| 211 |
if (low_12) { |
| 212 |
addi(rd, rd, low_12); |
| 213 |
} |
| 214 |
} else { |
| 215 |
addi(rd, zero_reg, low_12); |
| 216 |
} |
| 217 |
return; |
| 218 |
} else { |
| 219 |
UseScratchRegisterScope temps(this); |
| 220 |
BlockTrampolinePoolScope block_trampoline_pool(this, 8); |
| 221 |
// 64-bit case: divide imm into two 32-bit parts, upper and lower |
| 222 |
int64_t up_32 = imm >> 32; |
| 223 |
int64_t low_32 = imm & 0xffffffffull; |
| 224 |
Register temp_reg = rd; |
| 225 |
// Check if a temporary register is available |
| 226 |
if (up_32 == 0 || low_32 == 0) { |
| 227 |
// No temp register is needed |
| 228 |
} else { |
| 229 |
temp_reg = temps.hasAvailable() ? temps.Acquire() : InvalidReg; |
| 230 |
} |
| 231 |
if (temp_reg != InvalidReg) { |
| 232 |
// keep track of hardware behavior for lower part in sim_low |
| 233 |
int64_t sim_low = 0; |
| 234 |
// Build lower part |
| 235 |
if (low_32 != 0) { |
| 236 |
int64_t high_20 = ((low_32 + 0x800) >> 12); |
| 237 |
int64_t low_12 = low_32 & 0xfff; |
| 238 |
if (high_20) { |
| 239 |
// Adjust to 20 bits for the case of overflow |
| 240 |
high_20 &= 0xfffff; |
| 241 |
sim_low = ((high_20 << 12) << 32) >> 32; |
| 242 |
lui(rd, (int32_t)high_20); |
| 243 |
if (low_12) { |
| 244 |
sim_low += (low_12 << 52 >> 52) | low_12; |
| 245 |
addi(rd, rd, low_12); |
| 246 |
} |
| 247 |
} else { |
| 248 |
sim_low = low_12; |
| 249 |
ori(rd, zero_reg, low_12); |
| 250 |
} |
| 251 |
} |
| 252 |
if (sim_low & 0x100000000) { |
| 253 |
// Bit 31 is 1. Either an overflow or a negative 64 bit |
| 254 |
if (up_32 == 0) { |
| 255 |
// Positive number, but overflow because of the add 0x800 |
| 256 |
slli(rd, rd, 32); |
| 257 |
srli(rd, rd, 32); |
| 258 |
return; |
| 259 |
} |
| 260 |
// low_32 is a negative 64 bit after the build |
| 261 |
up_32 = (up_32 - 0xffffffff) & 0xffffffff; |
| 262 |
} |
| 263 |
if (up_32 == 0) { |
| 264 |
return; |
| 265 |
} |
| 266 |
// Build upper part in a temporary register |
| 267 |
if (low_32 == 0) { |
| 268 |
// Build upper part in rd |
| 269 |
temp_reg = rd; |
| 270 |
} |
| 271 |
int64_t high_20 = (up_32 + 0x800) >> 12; |
| 272 |
int64_t low_12 = up_32 & 0xfff; |
| 273 |
if (high_20) { |
| 274 |
// Adjust to 20 bits for the case of overflow |
| 275 |
high_20 &= 0xfffff; |
| 276 |
lui(temp_reg, (int32_t)high_20); |
| 277 |
if (low_12) { |
| 278 |
addi(temp_reg, temp_reg, low_12); |
| 279 |
} |
| 280 |
} else { |
| 281 |
ori(temp_reg, zero_reg, low_12); |
| 282 |
} |
| 283 |
// Put it at the bgining of register |
| 284 |
slli(temp_reg, temp_reg, 32); |
| 285 |
if (low_32 != 0) { |
| 286 |
add(rd, rd, temp_reg); |
| 287 |
} |
| 288 |
return; |
| 289 |
} |
| 290 |
// No temp register. Build imm in rd. |
| 291 |
// Build upper 32 bits first in rd. Divide lower 32 bits parts and add |
| 292 |
// parts to the upper part by doing shift and add. |
| 293 |
// First build upper part in rd. |
| 294 |
int64_t high_20 = (up_32 + 0x800) >> 12; |
| 295 |
int64_t low_12 = up_32 & 0xfff; |
| 296 |
if (high_20) { |
| 297 |
// Adjust to 20 bits for the case of overflow |
| 298 |
high_20 &= 0xfffff; |
| 299 |
lui(rd, (int32_t)high_20); |
| 300 |
if (low_12) { |
| 301 |
addi(rd, rd, low_12); |
| 302 |
} |
| 303 |
} else { |
| 304 |
ori(rd, zero_reg, low_12); |
| 305 |
} |
| 306 |
// upper part already in rd. Each part to be added to rd, has maximum of 11 |
| 307 |
// bits, and always starts with a 1. rd is shifted by the size of the part |
| 308 |
// plus the number of zeros between the parts. Each part is added after the |
| 309 |
// left shift. |
| 310 |
uint32_t mask = 0x80000000; |
| 311 |
int32_t shift_val = 0; |
| 312 |
int32_t i; |
| 313 |
for (i = 0; i < 32; i++) { |
| 314 |
if ((low_32 & mask) == 0) { |
| 315 |
mask >>= 1; |
| 316 |
shift_val++; |
| 317 |
if (i == 31) { |
| 318 |
// rest is zero |
| 319 |
slli(rd, rd, shift_val); |
| 320 |
} |
| 321 |
continue; |
| 322 |
} |
| 323 |
// The first 1 seen |
| 324 |
int32_t part; |
| 325 |
if ((i + 11) < 32) { |
| 326 |
// Pick 11 bits |
| 327 |
part = ((uint32_t)(low_32 << i) >> i) >> (32 - (i + 11)); |
| 328 |
slli(rd, rd, shift_val + 11); |
| 329 |
ori(rd, rd, part); |
| 330 |
i += 10; |
| 331 |
mask >>= 11; |
| 332 |
} else { |
| 333 |
part = (uint32_t)(low_32 << i) >> i; |
| 334 |
slli(rd, rd, shift_val + (32 - i)); |
| 335 |
ori(rd, rd, part); |
| 336 |
break; |
| 337 |
} |
| 338 |
shift_val = 0; |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
int Assembler::GeneralLiCount(int64_t imm, bool is_get_temp_reg) { |
| 344 |
int count = 0; |
| 345 |
// imitate Assembler::RV_li |
| 346 |
if (is_int32(imm + 0x800)) { |
| 347 |
// 32-bit case. Maximum of 2 instructions generated |
| 348 |
int64_t high_20 = ((imm + 0x800) >> 12); |
| 349 |
int64_t low_12 = imm << 52 >> 52; |
| 350 |
if (high_20) { |
| 351 |
count++; |
| 352 |
if (low_12) { |
| 353 |
count++; |
| 354 |
} |
| 355 |
} else { |
| 356 |
count++; |
| 357 |
} |
| 358 |
return count; |
| 359 |
} else { |
| 360 |
// 64-bit case: divide imm into two 32-bit parts, upper and lower |
| 361 |
int64_t up_32 = imm >> 32; |
| 362 |
int64_t low_32 = imm & 0xffffffffull; |
| 363 |
// Check if a temporary register is available |
| 364 |
if (is_get_temp_reg) { |
| 365 |
// keep track of hardware behavior for lower part in sim_low |
| 366 |
int64_t sim_low = 0; |
| 367 |
// Build lower part |
| 368 |
if (low_32 != 0) { |
| 369 |
int64_t high_20 = ((low_32 + 0x800) >> 12); |
| 370 |
int64_t low_12 = low_32 & 0xfff; |
| 371 |
if (high_20) { |
| 372 |
// Adjust to 20 bits for the case of overflow |
| 373 |
high_20 &= 0xfffff; |
| 374 |
sim_low = ((high_20 << 12) << 32) >> 32; |
| 375 |
count++; |
| 376 |
if (low_12) { |
| 377 |
sim_low += (low_12 << 52 >> 52) | low_12; |
| 378 |
count++; |
| 379 |
} |
| 380 |
} else { |
| 381 |
sim_low = low_12; |
| 382 |
count++; |
| 383 |
} |
| 384 |
} |
| 385 |
if (sim_low & 0x100000000) { |
| 386 |
// Bit 31 is 1. Either an overflow or a negative 64 bit |
| 387 |
if (up_32 == 0) { |
| 388 |
// Positive number, but overflow because of the add 0x800 |
| 389 |
count++; |
| 390 |
count++; |
| 391 |
return count; |
| 392 |
} |
| 393 |
// low_32 is a negative 64 bit after the build |
| 394 |
up_32 = (up_32 - 0xffffffff) & 0xffffffff; |
| 395 |
} |
| 396 |
if (up_32 == 0) { |
| 397 |
return count; |
| 398 |
} |
| 399 |
int64_t high_20 = (up_32 + 0x800) >> 12; |
| 400 |
int64_t low_12 = up_32 & 0xfff; |
| 401 |
if (high_20) { |
| 402 |
// Adjust to 20 bits for the case of overflow |
| 403 |
high_20 &= 0xfffff; |
| 404 |
count++; |
| 405 |
if (low_12) { |
| 406 |
count++; |
| 407 |
} |
| 408 |
} else { |
| 409 |
count++; |
| 410 |
} |
| 411 |
// Put it at the bgining of register |
| 412 |
count++; |
| 413 |
if (low_32 != 0) { |
| 414 |
count++; |
| 415 |
} |
| 416 |
return count; |
| 417 |
} |
| 418 |
// No temp register. Build imm in rd. |
| 419 |
// Build upper 32 bits first in rd. Divide lower 32 bits parts and add |
| 420 |
// parts to the upper part by doing shift and add. |
| 421 |
// First build upper part in rd. |
| 422 |
int64_t high_20 = (up_32 + 0x800) >> 12; |
| 423 |
int64_t low_12 = up_32 & 0xfff; |
| 424 |
if (high_20) { |
| 425 |
// Adjust to 20 bits for the case of overflow |
| 426 |
high_20 &= 0xfffff; |
| 427 |
count++; |
| 428 |
if (low_12) { |
| 429 |
count++; |
| 430 |
} |
| 431 |
} else { |
| 432 |
count++; |
| 433 |
} |
| 434 |
// upper part already in rd. Each part to be added to rd, has maximum of 11 |
| 435 |
// bits, and always starts with a 1. rd is shifted by the size of the part |
| 436 |
// plus the number of zeros between the parts. Each part is added after the |
| 437 |
// left shift. |
| 438 |
uint32_t mask = 0x80000000; |
| 439 |
int32_t i; |
| 440 |
for (i = 0; i < 32; i++) { |
| 441 |
if ((low_32 & mask) == 0) { |
| 442 |
mask >>= 1; |
| 443 |
if (i == 31) { |
| 444 |
// rest is zero |
| 445 |
count++; |
| 446 |
} |
| 447 |
continue; |
| 448 |
} |
| 449 |
// The first 1 seen |
| 450 |
if ((i + 11) < 32) { |
| 451 |
// Pick 11 bits |
| 452 |
count++; |
| 453 |
count++; |
| 454 |
i += 10; |
| 455 |
mask >>= 11; |
| 456 |
} else { |
| 457 |
count++; |
| 458 |
count++; |
| 459 |
break; |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
return count; |
| 464 |
} |
| 465 |
|
| 466 |
void Assembler::li_ptr(Register rd, int64_t imm) { |
| 467 |
m_buffer.enterNoNops(); |
| 468 |
m_buffer.assertNoPoolAndNoNops(); |
| 469 |
// Initialize rd with an address |
| 470 |
// Pointers are 48 bits |
| 471 |
// 6 fixed instructions are generated |
| 472 |
DEBUG_PRINTF("li_ptr(%d, %" PRIx64 " <%" PRId64 ">)\n", ToNumber(rd), imm, |
| 473 |
imm); |
| 474 |
MOZ_ASSERT((imm & 0xfff0000000000000ll) == 0); |
| 475 |
int64_t a6 = imm & 0x3f; // bits 0:5. 6 bits |
| 476 |
int64_t b11 = (imm >> 6) & 0x7ff; // bits 6:11. 11 bits |
| 477 |
int64_t high_31 = (imm >> 17) & 0x7fffffff; // 31 bits |
| 478 |
int64_t high_20 = ((high_31 + 0x800) >> 12); // 19 bits |
| 479 |
int64_t low_12 = high_31 & 0xfff; // 12 bits |
| 480 |
lui(rd, (int32_t)high_20); |
| 481 |
addi(rd, rd, low_12); // 31 bits in rd. |
| 482 |
slli(rd, rd, 11); // Space for next 11 bis |
| 483 |
ori(rd, rd, b11); // 11 bits are put in. 42 bit in rd |
| 484 |
slli(rd, rd, 6); // Space for next 6 bits |
| 485 |
ori(rd, rd, a6); // 6 bits are put in. 48 bis in rd |
| 486 |
m_buffer.leaveNoNops(); |
| 487 |
} |
| 488 |
|
| 489 |
void Assembler::li_constant(Register rd, int64_t imm) { |
| 490 |
m_buffer.enterNoNops(); |
| 491 |
m_buffer.assertNoPoolAndNoNops(); |
| 492 |
DEBUG_PRINTF("li_constant(%d, %" PRIx64 " <%" PRId64 ">)\n", ToNumber(rd), |
| 493 |
imm, imm); |
| 494 |
lui(rd, (imm + (1LL << 47) + (1LL << 35) + (1LL << 23) + (1LL << 11)) >> |
| 495 |
48); // Bits 63:48 |
| 496 |
addiw(rd, rd, |
| 497 |
(imm + (1LL << 35) + (1LL << 23) + (1LL << 11)) << 16 >> |
| 498 |
52); // Bits 47:36 |
| 499 |
slli(rd, rd, 12); |
| 500 |
addi(rd, rd, (imm + (1LL << 23) + (1LL << 11)) << 28 >> 52); // Bits 35:24 |
| 501 |
slli(rd, rd, 12); |
| 502 |
addi(rd, rd, (imm + (1LL << 11)) << 40 >> 52); // Bits 23:12 |
| 503 |
slli(rd, rd, 12); |
| 504 |
addi(rd, rd, imm << 52 >> 52); // Bits 11:0 |
| 505 |
m_buffer.leaveNoNops(); |
| 506 |
} |
| 507 |
|
| 508 |
ABIArg ABIArgGenerator::next(MIRType type) { |
| 509 |
switch (type) { |
| 510 |
case MIRType::Int32: |
| 511 |
case MIRType::Int64: |
| 512 |
case MIRType::Pointer: |
| 513 |
case MIRType::WasmAnyRef: |
| 514 |
case MIRType::WasmArrayData: |
| 515 |
case MIRType::StackResults: { |
| 516 |
if (intRegIndex_ == NumIntArgRegs) { |
| 517 |
current_ = ABIArg(stackOffset_); |
| 518 |
stackOffset_ += sizeof(uintptr_t); |
| 519 |
break; |
| 520 |
} |
| 521 |
current_ = ABIArg(Register::FromCode(intRegIndex_ + a0.encoding())); |
| 522 |
intRegIndex_++; |
| 523 |
break; |
| 524 |
} |
| 525 |
case MIRType::Float32: |
| 526 |
case MIRType::Double: { |
| 527 |
if (floatRegIndex_ == NumFloatArgRegs) { |
| 528 |
current_ = ABIArg(stackOffset_); |
| 529 |
stackOffset_ += sizeof(double); |
| 530 |
break; |
| 531 |
} |
| 532 |
current_ = ABIArg(FloatRegister( |
| 533 |
FloatRegisters::Encoding(floatRegIndex_ + fa0.encoding()), |
| 534 |
type == MIRType::Double ? FloatRegisters::Double |
| 535 |
: FloatRegisters::Single)); |
| 536 |
floatRegIndex_++; |
| 537 |
break; |
| 538 |
} |
| 539 |
case MIRType::Simd128: { |
| 540 |
MOZ_CRASH("RISCV64 does not support simd yet."); |
| 541 |
break; |
| 542 |
} |
| 543 |
default: |
| 544 |
MOZ_CRASH("Unexpected argument type"); |
| 545 |
} |
| 546 |
return current_; |
| 547 |
} |
| 548 |
|
| 549 |
bool Assembler::oom() const { |
| 550 |
return AssemblerShared::oom() || m_buffer.oom() || jumpRelocations_.oom() || |
| 551 |
dataRelocations_.oom() || !enoughLabelCache_; |
| 552 |
} |
| 553 |
|
| 554 |
int Assembler::disassembleInstr(Instr instr, bool enable_spew) { |
| 555 |
if (!FLAG_riscv_debug && !enable_spew) return -1; |
| 556 |
disasm::NameConverter converter; |
| 557 |
disasm::Disassembler disasm(converter); |
| 558 |
EmbeddedVector<char, 128> disasm_buffer; |
| 559 |
|
| 560 |
int size = |
| 561 |
disasm.InstructionDecode(disasm_buffer, reinterpret_cast<byte*>(&instr)); |
| 562 |
DEBUG_PRINTF("%s\n", disasm_buffer.start()); |
| 563 |
if (enable_spew) { |
| 564 |
JitSpew(JitSpew_Codegen, "%s", disasm_buffer.start()); |
| 565 |
} |
| 566 |
return size; |
| 567 |
} |
| 568 |
|
| 569 |
uint64_t Assembler::target_address_at(Instruction* pc) { |
| 570 |
Instruction* instr0 = pc; |
| 571 |
DEBUG_PRINTF("target_address_at: pc: 0x%p\t", instr0); |
| 572 |
Instruction* instr1 = pc + 1 * kInstrSize; |
| 573 |
Instruction* instr2 = pc + 2 * kInstrSize; |
| 574 |
Instruction* instr3 = pc + 3 * kInstrSize; |
| 575 |
Instruction* instr4 = pc + 4 * kInstrSize; |
| 576 |
Instruction* instr5 = pc + 5 * kInstrSize; |
| 577 |
|
| 578 |
// Interpret instructions for address generated by li: See listing in |
| 579 |
// Assembler::set_target_address_at() just below. |
| 580 |
if (IsLui(*reinterpret_cast<Instr*>(instr0)) && |
| 581 |
IsAddi(*reinterpret_cast<Instr*>(instr1)) && |
| 582 |
IsSlli(*reinterpret_cast<Instr*>(instr2)) && |
| 583 |
IsOri(*reinterpret_cast<Instr*>(instr3)) && |
| 584 |
IsSlli(*reinterpret_cast<Instr*>(instr4)) && |
| 585 |
IsOri(*reinterpret_cast<Instr*>(instr5))) { |
| 586 |
// Assemble the 64 bit value. |
| 587 |
int64_t addr = (int64_t)(instr0->Imm20UValue() << kImm20Shift) + |
| 588 |
(int64_t)instr1->Imm12Value(); |
| 589 |
MOZ_ASSERT(instr2->Imm12Value() == 11); |
| 590 |
addr <<= 11; |
| 591 |
addr |= (int64_t)instr3->Imm12Value(); |
| 592 |
MOZ_ASSERT(instr4->Imm12Value() == 6); |
| 593 |
addr <<= 6; |
| 594 |
addr |= (int64_t)instr5->Imm12Value(); |
| 595 |
|
| 596 |
DEBUG_PRINTF("addr: %" PRIx64 "\n", addr); |
| 597 |
return static_cast<uint64_t>(addr); |
| 598 |
} |
| 599 |
// We should never get here, force a bad address if we do. |
| 600 |
MOZ_CRASH("RISC-V UNREACHABLE"); |
| 601 |
} |
| 602 |
|
| 603 |
void Assembler::PatchDataWithValueCheck(CodeLocationLabel label, |
| 604 |
ImmPtr newValue, ImmPtr expectedValue) { |
| 605 |
PatchDataWithValueCheck(label, PatchedImmPtr(newValue.value), |
| 606 |
PatchedImmPtr(expectedValue.value)); |
| 607 |
} |
| 608 |
|
| 609 |
void Assembler::PatchDataWithValueCheck(CodeLocationLabel label, |
| 610 |
PatchedImmPtr newValue, |
| 611 |
PatchedImmPtr expectedValue) { |
| 612 |
Instruction* inst = (Instruction*)label.raw(); |
| 613 |
|
| 614 |
// Extract old Value |
| 615 |
DebugOnly<uint64_t> value = Assembler::ExtractLoad64Value(inst); |
| 616 |
MOZ_ASSERT(value == uint64_t(expectedValue.value)); |
| 617 |
|
| 618 |
// Replace with new value |
| 619 |
Assembler::UpdateLoad64Value(inst, uint64_t(newValue.value)); |
| 620 |
} |
| 621 |
|
| 622 |
uint64_t Assembler::ExtractLoad64Value(Instruction* inst0) { |
| 623 |
DEBUG_PRINTF("\tExtractLoad64Value: \tpc:%p ", inst0); |
| 624 |
if (IsJal(*reinterpret_cast<Instr*>(inst0))) { |
| 625 |
int offset = inst0->Imm20JValue(); |
| 626 |
inst0 = inst0 + offset; |
| 627 |
} |
| 628 |
Instruction* instr1 = inst0 + 1 * kInstrSize; |
| 629 |
if (IsAddiw(*reinterpret_cast<Instr*>(instr1))) { |
| 630 |
// Li64 |
| 631 |
Instruction* instr2 = inst0 + 2 * kInstrSize; |
| 632 |
Instruction* instr3 = inst0 + 3 * kInstrSize; |
| 633 |
Instruction* instr4 = inst0 + 4 * kInstrSize; |
| 634 |
Instruction* instr5 = inst0 + 5 * kInstrSize; |
| 635 |
Instruction* instr6 = inst0 + 6 * kInstrSize; |
| 636 |
Instruction* instr7 = inst0 + 7 * kInstrSize; |
| 637 |
if (IsLui(*reinterpret_cast<Instr*>(inst0)) && |
| 638 |
IsAddiw(*reinterpret_cast<Instr*>(instr1)) && |
| 639 |
IsSlli(*reinterpret_cast<Instr*>(instr2)) && |
| 640 |
IsAddi(*reinterpret_cast<Instr*>(instr3)) && |
| 641 |
IsSlli(*reinterpret_cast<Instr*>(instr4)) && |
| 642 |
IsAddi(*reinterpret_cast<Instr*>(instr5)) && |
| 643 |
IsSlli(*reinterpret_cast<Instr*>(instr6)) && |
| 644 |
IsAddi(*reinterpret_cast<Instr*>(instr7))) { |
| 645 |
int64_t imm = (int64_t)(inst0->Imm20UValue() << kImm20Shift) + |
| 646 |
(int64_t)instr1->Imm12Value(); |
| 647 |
MOZ_ASSERT(instr2->Imm12Value() == 12); |
| 648 |
imm <<= 12; |
| 649 |
imm += (int64_t)instr3->Imm12Value(); |
| 650 |
MOZ_ASSERT(instr4->Imm12Value() == 12); |
| 651 |
imm <<= 12; |
| 652 |
imm += (int64_t)instr5->Imm12Value(); |
| 653 |
MOZ_ASSERT(instr6->Imm12Value() == 12); |
| 654 |
imm <<= 12; |
| 655 |
imm += (int64_t)instr7->Imm12Value(); |
| 656 |
DEBUG_PRINTF("imm:%" PRIx64 "\n", imm); |
| 657 |
return imm; |
| 658 |
} else { |
| 659 |
FLAG_riscv_debug = true; |
| 660 |
disassembleInstr(inst0->InstructionBits()); |
| 661 |
disassembleInstr(instr1->InstructionBits()); |
| 662 |
disassembleInstr(instr2->InstructionBits()); |
| 663 |
disassembleInstr(instr3->InstructionBits()); |
| 664 |
disassembleInstr(instr4->InstructionBits()); |
| 665 |
disassembleInstr(instr5->InstructionBits()); |
| 666 |
disassembleInstr(instr6->InstructionBits()); |
| 667 |
disassembleInstr(instr7->InstructionBits()); |
| 668 |
MOZ_CRASH(); |
| 669 |
} |
| 670 |
} else { |
| 671 |
DEBUG_PRINTF("\n"); |
| 672 |
Instruction* instrf1 = (inst0 - 1 * kInstrSize); |
| 673 |
Instruction* instr2 = inst0 + 2 * kInstrSize; |
| 674 |
Instruction* instr3 = inst0 + 3 * kInstrSize; |
| 675 |
Instruction* instr4 = inst0 + 4 * kInstrSize; |
| 676 |
Instruction* instr5 = inst0 + 5 * kInstrSize; |
| 677 |
Instruction* instr6 = inst0 + 6 * kInstrSize; |
| 678 |
Instruction* instr7 = inst0 + 7 * kInstrSize; |
| 679 |
disassembleInstr(instrf1->InstructionBits()); |
| 680 |
disassembleInstr(inst0->InstructionBits()); |
| 681 |
disassembleInstr(instr1->InstructionBits()); |
| 682 |
disassembleInstr(instr2->InstructionBits()); |
| 683 |
disassembleInstr(instr3->InstructionBits()); |
| 684 |
disassembleInstr(instr4->InstructionBits()); |
| 685 |
disassembleInstr(instr5->InstructionBits()); |
| 686 |
disassembleInstr(instr6->InstructionBits()); |
| 687 |
disassembleInstr(instr7->InstructionBits()); |
| 688 |
MOZ_ASSERT(IsAddi(*reinterpret_cast<Instr*>(instr1))); |
| 689 |
// Li48 |
| 690 |
return target_address_at(inst0); |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
void Assembler::UpdateLoad64Value(Instruction* pc, uint64_t value) { |
| 695 |
DEBUG_PRINTF("\tUpdateLoad64Value: pc: %p\tvalue: %" PRIx64 "\n", pc, value); |
| 696 |
Instruction* instr1 = pc + 1 * kInstrSize; |
| 697 |
if (IsJal(*reinterpret_cast<Instr*>(pc))) { |
| 698 |
pc = pc + pc->Imm20JValue(); |
| 699 |
instr1 = pc + 1 * kInstrSize; |
| 700 |
} |
| 701 |
if (IsAddiw(*reinterpret_cast<Instr*>(instr1))) { |
| 702 |
Instruction* instr0 = pc; |
| 703 |
Instruction* instr2 = pc + 2 * kInstrSize; |
| 704 |
Instruction* instr3 = pc + 3 * kInstrSize; |
| 705 |
Instruction* instr4 = pc + 4 * kInstrSize; |
| 706 |
Instruction* instr5 = pc + 5 * kInstrSize; |
| 707 |
Instruction* instr6 = pc + 6 * kInstrSize; |
| 708 |
Instruction* instr7 = pc + 7 * kInstrSize; |
| 709 |
MOZ_ASSERT(IsLui(*reinterpret_cast<Instr*>(pc)) && |
| 710 |
IsAddiw(*reinterpret_cast<Instr*>(instr1)) && |
| 711 |
IsSlli(*reinterpret_cast<Instr*>(instr2)) && |
| 712 |
IsAddi(*reinterpret_cast<Instr*>(instr3)) && |
| 713 |
IsSlli(*reinterpret_cast<Instr*>(instr4)) && |
| 714 |
IsAddi(*reinterpret_cast<Instr*>(instr5)) && |
| 715 |
IsSlli(*reinterpret_cast<Instr*>(instr6)) && |
| 716 |
IsAddi(*reinterpret_cast<Instr*>(instr7))); |
| 717 |
// lui(rd, (imm + (1LL << 47) + (1LL << 35) + (1LL << 23) + (1LL << 11)) >> |
| 718 |
// 48); // Bits 63:48 |
| 719 |
// addiw(rd, rd, |
| 720 |
// (imm + (1LL << 35) + (1LL << 23) + (1LL << 11)) << 16 >> |
| 721 |
// 52); // Bits 47:36 |
| 722 |
// slli(rd, rd, 12); |
| 723 |
// addi(rd, rd, (imm + (1LL << 23) + (1LL << 11)) << 28 >> 52); // Bits |
| 724 |
// 35:24 slli(rd, rd, 12); addi(rd, rd, (imm + (1LL << 11)) << 40 >> 52); // |
| 725 |
// Bits 23:12 slli(rd, rd, 12); addi(rd, rd, imm << 52 >> 52); // Bits 11:0 |
| 726 |
*reinterpret_cast<Instr*>(instr0) &= 0xfff; |
| 727 |
*reinterpret_cast<Instr*>(instr0) |= |
| 728 |
(((value + (1LL << 47) + (1LL << 35) + (1LL << 23) + (1LL << 11)) >> 48) |
| 729 |
<< 12); |
| 730 |
*reinterpret_cast<Instr*>(instr1) &= 0xfffff; |
| 731 |
*reinterpret_cast<Instr*>(instr1) |= |
| 732 |
(((value + (1LL << 35) + (1LL << 23) + (1LL << 11)) << 16 >> 52) << 20); |
| 733 |
*reinterpret_cast<Instr*>(instr3) &= 0xfffff; |
| 734 |
*reinterpret_cast<Instr*>(instr3) |= |
| 735 |
(((value + (1LL << 23) + (1LL << 11)) << 28 >> 52) << 20); |
| 736 |
*reinterpret_cast<Instr*>(instr5) &= 0xfffff; |
| 737 |
*reinterpret_cast<Instr*>(instr5) |= |
| 738 |
(((value + (1LL << 11)) << 40 >> 52) << 20); |
| 739 |
*reinterpret_cast<Instr*>(instr7) &= 0xfffff; |
| 740 |
*reinterpret_cast<Instr*>(instr7) |= ((value << 52 >> 52) << 20); |
| 741 |
disassembleInstr(instr0->InstructionBits()); |
| 742 |
disassembleInstr(instr1->InstructionBits()); |
| 743 |
disassembleInstr(instr2->InstructionBits()); |
| 744 |
disassembleInstr(instr3->InstructionBits()); |
| 745 |
disassembleInstr(instr4->InstructionBits()); |
| 746 |
disassembleInstr(instr5->InstructionBits()); |
| 747 |
disassembleInstr(instr6->InstructionBits()); |
| 748 |
disassembleInstr(instr7->InstructionBits()); |
| 749 |
MOZ_ASSERT(ExtractLoad64Value(pc) == value); |
| 750 |
} else { |
| 751 |
Instruction* instr0 = pc; |
| 752 |
Instruction* instr2 = pc + 2 * kInstrSize; |
| 753 |
Instruction* instr3 = pc + 3 * kInstrSize; |
| 754 |
Instruction* instr4 = pc + 4 * kInstrSize; |
| 755 |
Instruction* instr5 = pc + 5 * kInstrSize; |
| 756 |
Instruction* instr6 = pc + 6 * kInstrSize; |
| 757 |
Instruction* instr7 = pc + 7 * kInstrSize; |
| 758 |
disassembleInstr(instr0->InstructionBits()); |
| 759 |
disassembleInstr(instr1->InstructionBits()); |
| 760 |
disassembleInstr(instr2->InstructionBits()); |
| 761 |
disassembleInstr(instr3->InstructionBits()); |
| 762 |
disassembleInstr(instr4->InstructionBits()); |
| 763 |
disassembleInstr(instr5->InstructionBits()); |
| 764 |
disassembleInstr(instr6->InstructionBits()); |
| 765 |
disassembleInstr(instr7->InstructionBits()); |
| 766 |
MOZ_ASSERT(IsAddi(*reinterpret_cast<Instr*>(instr1))); |
| 767 |
set_target_value_at(pc, value); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
void Assembler::set_target_value_at(Instruction* pc, uint64_t target) { |
| 772 |
DEBUG_PRINTF("\tset_target_value_at: pc: %p\ttarget: %" PRIx64 "\n", pc, |
| 773 |
target); |
| 774 |
uint32_t* p = reinterpret_cast<uint32_t*>(pc); |
| 775 |
MOZ_ASSERT((target & 0xffff000000000000ll) == 0); |
| 776 |
#ifdef DEBUG |
| 777 |
// Check we have the result from a li macro-instruction. |
| 778 |
Instruction* instr0 = pc; |
| 779 |
Instruction* instr1 = pc + 1 * kInstrSize; |
| 780 |
Instruction* instr3 = pc + 3 * kInstrSize; |
| 781 |
Instruction* instr5 = pc + 5 * kInstrSize; |
| 782 |
MOZ_ASSERT(IsLui(*reinterpret_cast<Instr*>(instr0)) && |
| 783 |
IsAddi(*reinterpret_cast<Instr*>(instr1)) && |
| 784 |
IsOri(*reinterpret_cast<Instr*>(instr3)) && |
| 785 |
IsOri(*reinterpret_cast<Instr*>(instr5))); |
| 786 |
#endif |
| 787 |
int64_t a6 = target & 0x3f; // bits 0:6. 6 bits |
| 788 |
int64_t b11 = (target >> 6) & 0x7ff; // bits 6:11. 11 bits |
| 789 |
int64_t high_31 = (target >> 17) & 0x7fffffff; // 31 bits |
| 790 |
int64_t high_20 = ((high_31 + 0x800) >> 12); // 19 bits |
| 791 |
int64_t low_12 = high_31 & 0xfff; // 12 bits |
| 792 |
*p = *p & 0xfff; |
| 793 |
*p = *p | ((int32_t)high_20 << 12); |
| 794 |
*(p + 1) = *(p + 1) & 0xfffff; |
| 795 |
*(p + 1) = *(p + 1) | ((int32_t)low_12 << 20); |
| 796 |
*(p + 2) = *(p + 2) & 0xfffff; |
| 797 |
*(p + 2) = *(p + 2) | (11 << 20); |
| 798 |
*(p + 3) = *(p + 3) & 0xfffff; |
| 799 |
*(p + 3) = *(p + 3) | ((int32_t)b11 << 20); |
| 800 |
*(p + 4) = *(p + 4) & 0xfffff; |
| 801 |
*(p + 4) = *(p + 4) | (6 << 20); |
| 802 |
*(p + 5) = *(p + 5) & 0xfffff; |
| 803 |
*(p + 5) = *(p + 5) | ((int32_t)a6 << 20); |
| 804 |
MOZ_ASSERT(target_address_at(pc) == target); |
| 805 |
} |
| 806 |
|
| 807 |
void Assembler::WriteLoad64Instructions(Instruction* inst0, Register reg, |
| 808 |
uint64_t value) { |
| 809 |
DEBUG_PRINTF("\tWriteLoad64Instructions\n"); |
| 810 |
// Initialize rd with an address |
| 811 |
// Pointers are 48 bits |
| 812 |
// 6 fixed instructions are generated |
| 813 |
MOZ_ASSERT((value & 0xfff0000000000000ll) == 0); |
| 814 |
int64_t a6 = value & 0x3f; // bits 0:5. 6 bits |
| 815 |
int64_t b11 = (value >> 6) & 0x7ff; // bits 6:11. 11 bits |
| 816 |
int64_t high_31 = (value >> 17) & 0x7fffffff; // 31 bits |
| 817 |
int64_t high_20 = ((high_31 + 0x800) >> 12); // 19 bits |
| 818 |
int64_t low_12 = high_31 & 0xfff; // 12 bits |
| 819 |
Instr lui_ = LUI | (reg.code() << kRdShift) | |
| 820 |
((int32_t)high_20 << kImm20Shift); // lui(rd, (int32_t)high_20); |
| 821 |
*reinterpret_cast<Instr*>(inst0) = lui_; |
| 822 |
|
| 823 |
Instr addi_ = |
| 824 |
OP_IMM | (reg.code() << kRdShift) | (0b000 << kFunct3Shift) | |
| 825 |
(reg.code() << kRs1Shift) | |
| 826 |
(low_12 << kImm12Shift); // addi(rd, rd, low_12); // 31 bits in rd. |
| 827 |
*reinterpret_cast<Instr*>(inst0 + 1 * kInstrSize) = addi_; |
| 828 |
|
| 829 |
Instr slli_ = |
| 830 |
OP_IMM | (reg.code() << kRdShift) | (0b001 << kFunct3Shift) | |
| 831 |
(reg.code() << kRs1Shift) | |
| 832 |
(11 << kImm12Shift); // slli(rd, rd, 11); // Space for next 11 bis |
| 833 |
*reinterpret_cast<Instr*>(inst0 + 2 * kInstrSize) = slli_; |
| 834 |
|
| 835 |
Instr ori_b11 = OP_IMM | (reg.code() << kRdShift) | (0b110 << kFunct3Shift) | |
| 836 |
(reg.code() << kRs1Shift) | |
| 837 |
(b11 << kImm12Shift); // ori(rd, rd, b11); // 11 bits |
| 838 |
// are put in. 42 bit in rd |
| 839 |
*reinterpret_cast<Instr*>(inst0 + 3 * kInstrSize) = ori_b11; |
| 840 |
|
| 841 |
slli_ = OP_IMM | (reg.code() << kRdShift) | (0b001 << kFunct3Shift) | |
| 842 |
(reg.code() << kRs1Shift) | |
| 843 |
(6 << kImm12Shift); // slli(rd, rd, 6); // Space for next 11 bis |
| 844 |
*reinterpret_cast<Instr*>(inst0 + 4 * kInstrSize) = |
| 845 |
slli_; // slli(rd, rd, 6); // Space for next 6 bits |
| 846 |
|
| 847 |
Instr ori_a6 = OP_IMM | (reg.code() << kRdShift) | (0b110 << kFunct3Shift) | |
| 848 |
(reg.code() << kRs1Shift) | |
| 849 |
(a6 << kImm12Shift); // ori(rd, rd, a6); // 6 bits are |
| 850 |
// put in. 48 bis in rd |
| 851 |
*reinterpret_cast<Instr*>(inst0 + 5 * kInstrSize) = ori_a6; |
| 852 |
disassembleInstr((inst0 + 0 * kInstrSize)->InstructionBits()); |
| 853 |
disassembleInstr((inst0 + 1 * kInstrSize)->InstructionBits()); |
| 854 |
disassembleInstr((inst0 + 2 * kInstrSize)->InstructionBits()); |
| 855 |
disassembleInstr((inst0 + 3 * kInstrSize)->InstructionBits()); |
| 856 |
disassembleInstr((inst0 + 4 * kInstrSize)->InstructionBits()); |
| 857 |
disassembleInstr((inst0 + 5 * kInstrSize)->InstructionBits()); |
| 858 |
disassembleInstr((inst0 + 6 * kInstrSize)->InstructionBits()); |
| 859 |
MOZ_ASSERT(ExtractLoad64Value(inst0) == value); |
| 860 |
} |
| 861 |
|
| 862 |
// This just stomps over memory with 32 bits of raw data. Its purpose is to |
| 863 |
// overwrite the call of JITed code with 32 bits worth of an offset. This will |
| 864 |
// is only meant to function on code that has been invalidated, so it should |
| 865 |
// be totally safe. Since that instruction will never be executed again, a |
| 866 |
// ICache flush should not be necessary |
| 867 |
void Assembler::PatchWrite_Imm32(CodeLocationLabel label, Imm32 imm) { |
| 868 |
// Raw is going to be the return address. |
| 869 |
uint32_t* raw = (uint32_t*)label.raw(); |
| 870 |
// Overwrite the 4 bytes before the return address, which will |
| 871 |
// end up being the call instruction. |
| 872 |
*(raw - 1) = imm.value; |
| 873 |
} |
| 874 |
|
| 875 |
void Assembler::target_at_put(BufferOffset pos, BufferOffset target_pos, |
| 876 |
bool trampoline) { |
| 877 |
if (m_buffer.oom()) { |
| 878 |
return; |
| 879 |
} |
| 880 |
DEBUG_PRINTF("\ttarget_at_put: %p (%d) to %p (%d)\n", |
| 881 |
reinterpret_cast<Instr*>(editSrc(pos)), pos.getOffset(), |
| 882 |
reinterpret_cast<Instr*>(editSrc(pos)) + target_pos.getOffset() - |
| 883 |
pos.getOffset(), |
| 884 |
target_pos.getOffset()); |
| 885 |
Instruction* instruction = editSrc(pos); |
| 886 |
Instr instr = instruction->InstructionBits(); |
| 887 |
switch (instruction->InstructionOpcodeType()) { |
| 888 |
case BRANCH: { |
| 889 |
instr = SetBranchOffset(pos.getOffset(), target_pos.getOffset(), instr); |
| 890 |
instr_at_put(pos, instr); |
| 891 |
} break; |
| 892 |
case JAL: { |
| 893 |
MOZ_ASSERT(IsJal(instr)); |
| 894 |
instr = SetJalOffset(pos.getOffset(), target_pos.getOffset(), instr); |
| 895 |
instr_at_put(pos, instr); |
| 896 |
} break; |
| 897 |
case LUI: { |
| 898 |
set_target_value_at(instruction, |
| 899 |
reinterpret_cast<uintptr_t>(editSrc(target_pos))); |
| 900 |
} break; |
| 901 |
case AUIPC: { |
| 902 |
Instr instr_auipc = instr; |
| 903 |
Instr instr_I = |
| 904 |
editSrc(BufferOffset(pos.getOffset() + 4))->InstructionBits(); |
| 905 |
MOZ_ASSERT(IsJalr(instr_I) || IsAddi(instr_I)); |
| 906 |
|
| 907 |
intptr_t offset = target_pos.getOffset() - pos.getOffset(); |
| 908 |
if (is_int21(offset) && IsJalr(instr_I) && trampoline) { |
| 909 |
MOZ_ASSERT(is_int21(offset) && ((offset & 1) == 0)); |
| 910 |
Instr instr = JAL; |
| 911 |
instr = SetJalOffset(pos.getOffset(), target_pos.getOffset(), instr); |
| 912 |
MOZ_ASSERT(IsJal(instr)); |
| 913 |
MOZ_ASSERT(JumpOffset(instr) == offset); |
| 914 |
instr_at_put(pos, instr); |
| 915 |
instr_at_put(BufferOffset(pos.getOffset() + 4), kNopByte); |
| 916 |
} else { |
| 917 |
MOZ_RELEASE_ASSERT(is_int32(offset + 0x800)); |
| 918 |
MOZ_ASSERT(instruction->RdValue() == |
| 919 |
editSrc(BufferOffset(pos.getOffset() + 4))->Rs1Value()); |
| 920 |
int32_t Hi20 = (((int32_t)offset + 0x800) >> 12); |
| 921 |
int32_t Lo12 = (int32_t)offset << 20 >> 20; |
| 922 |
|
| 923 |
instr_auipc = |
| 924 |
(instr_auipc & ~kImm31_12Mask) | ((Hi20 & kImm19_0Mask) << 12); |
| 925 |
instr_at_put(pos, instr_auipc); |
| 926 |
|
| 927 |
const int kImm31_20Mask = ((1 << 12) - 1) << 20; |
| 928 |
const int kImm11_0Mask = ((1 << 12) - 1); |
| 929 |
instr_I = (instr_I & ~kImm31_20Mask) | ((Lo12 & kImm11_0Mask) << 20); |
| 930 |
instr_at_put(BufferOffset(pos.getOffset() + 4), instr_I); |
| 931 |
} |
| 932 |
} break; |
| 933 |
default: |
| 934 |
UNIMPLEMENTED_RISCV(); |
| 935 |
break; |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
const int kEndOfChain = -1; |
| 940 |
const int32_t kEndOfJumpChain = 0; |
| 941 |
|
| 942 |
int Assembler::target_at(BufferOffset pos, bool is_internal) { |
| 943 |
if (oom()) { |
| 944 |
return kEndOfChain; |
| 945 |
} |
| 946 |
Instruction* instruction = editSrc(pos); |
| 947 |
Instruction* instruction2 = nullptr; |
| 948 |
if (IsAuipc(instruction->InstructionBits())) { |
| 949 |
instruction2 = editSrc(BufferOffset(pos.getOffset() + kInstrSize)); |
| 950 |
} |
| 951 |
return target_at(instruction, pos, is_internal, instruction2); |
| 952 |
} |
| 953 |
|
| 954 |
int Assembler::target_at(Instruction* instruction, BufferOffset pos, |
| 955 |
bool is_internal, Instruction* instruction2) { |
| 956 |
DEBUG_PRINTF("\t target_at: %p(%x)\n\t", |
| 957 |
reinterpret_cast<Instr*>(instruction), pos.getOffset()); |
| 958 |
disassembleInstr(instruction->InstructionBits()); |
| 959 |
Instr instr = instruction->InstructionBits(); |
| 960 |
switch (instruction->InstructionOpcodeType()) { |
| 961 |
case BRANCH: { |
| 962 |
int32_t imm13 = BranchOffset(instr); |
| 963 |
if (imm13 == kEndOfJumpChain) { |
| 964 |
// EndOfChain sentinel is returned directly, not relative to pc or pos. |
| 965 |
return kEndOfChain; |
| 966 |
} else { |
| 967 |
DEBUG_PRINTF("\t target_at: %d %d\n", imm13, pos.getOffset() + imm13); |
| 968 |
return pos.getOffset() + imm13; |
| 969 |
} |
| 970 |
} |
| 971 |
case JAL: { |
| 972 |
int32_t imm21 = JumpOffset(instr); |
| 973 |
if (imm21 == kEndOfJumpChain) { |
| 974 |
// EndOfChain sentinel is returned directly, not relative to pc or pos. |
| 975 |
return kEndOfChain; |
| 976 |
} else { |
| 977 |
DEBUG_PRINTF("\t target_at: %d %d\n", imm21, pos.getOffset() + imm21); |
| 978 |
return pos.getOffset() + imm21; |
| 979 |
} |
| 980 |
} |
| 981 |
case JALR: { |
| 982 |
int32_t imm12 = instr >> 20; |
| 983 |
if (imm12 == kEndOfJumpChain) { |
| 984 |
// EndOfChain sentinel is returned directly, not relative to pc or pos. |
| 985 |
return kEndOfChain; |
| 986 |
} else { |
| 987 |
DEBUG_PRINTF("\t target_at: %d %d\n", imm12, pos.getOffset() + imm12); |
| 988 |
return pos.getOffset() + imm12; |
| 989 |
} |
| 990 |
} |
| 991 |
case LUI: { |
| 992 |
uintptr_t imm = target_address_at(instruction); |
| 993 |
uintptr_t instr_address = reinterpret_cast<uintptr_t>(instruction); |
| 994 |
if (imm == kEndOfJumpChain) { |
| 995 |
return kEndOfChain; |
| 996 |
} else { |
| 997 |
MOZ_ASSERT(instr_address - imm < INT_MAX); |
| 998 |
int32_t delta = static_cast<int32_t>(instr_address - imm); |
| 999 |
MOZ_ASSERT(pos.getOffset() > delta); |
| 1000 |
return pos.getOffset() - delta; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
case AUIPC: { |
| 1004 |
MOZ_ASSERT(instruction2 != nullptr); |
| 1005 |
Instr instr_auipc = instr; |
| 1006 |
Instr instr_I = instruction2->InstructionBits(); |
| 1007 |
MOZ_ASSERT(IsJalr(instr_I) || IsAddi(instr_I)); |
| 1008 |
int32_t offset = BrachlongOffset(instr_auipc, instr_I); |
| 1009 |
if (offset == kEndOfJumpChain) return kEndOfChain; |
| 1010 |
DEBUG_PRINTF("\t target_at: %d %d\n", offset, pos.getOffset() + offset); |
| 1011 |
return offset + pos.getOffset(); |
| 1012 |
} |
| 1013 |
default: { |
| 1014 |
UNIMPLEMENTED_RISCV(); |
| 1015 |
} |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
uint32_t Assembler::next_link(Label* L, bool is_internal) { |
| 1020 |
MOZ_ASSERT(L->used()); |
| 1021 |
BufferOffset pos(L); |
| 1022 |
int link = target_at(pos, is_internal); |
| 1023 |
if (link == kEndOfChain) { |
| 1024 |
L->reset(); |
| 1025 |
return LabelBase::INVALID_OFFSET; |
| 1026 |
} else { |
| 1027 |
MOZ_ASSERT(link >= 0); |
| 1028 |
DEBUG_PRINTF("next: %p to offset %d\n", L, link); |
| 1029 |
L->use(link); |
| 1030 |
return link; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
void Assembler::bind(Label* label, BufferOffset boff) { |
| 1035 |
JitSpew(JitSpew_Codegen, ".set Llabel %p %d", label, currentOffset()); |
| 1036 |
DEBUG_PRINTF(".set Llabel %p\n", label); |
| 1037 |
// If our caller didn't give us an explicit target to bind to |
| 1038 |
// then we want to bind to the location of the next instruction |
| 1039 |
BufferOffset dest = boff.assigned() ? boff : nextOffset(); |
| 1040 |
if (label->used()) { |
| 1041 |
uint32_t next; |
| 1042 |
|
| 1043 |
// A used label holds a link to branch that uses it. |
| 1044 |
do { |
| 1045 |
BufferOffset b(label); |
| 1046 |
DEBUG_PRINTF("\tbind next:%d\n", b.getOffset()); |
| 1047 |
// Even a 0 offset may be invalid if we're out of memory. |
| 1048 |
if (oom()) { |
| 1049 |
return; |
| 1050 |
} |
| 1051 |
int fixup_pos = b.getOffset(); |
| 1052 |
int dist = dest.getOffset() - fixup_pos; |
| 1053 |
next = next_link(label, false); |
| 1054 |
DEBUG_PRINTF("\t%p fixup: %d next: %d\n", label, fixup_pos, next); |
| 1055 |
DEBUG_PRINTF("\t fixup: %d dest: %d dist: %d %d %d\n", fixup_pos, |
| 1056 |
dest.getOffset(), dist, nextOffset().getOffset(), |
| 1057 |
currentOffset()); |
| 1058 |
Instruction* instruction = editSrc(b); |
| 1059 |
Instr instr = instruction->InstructionBits(); |
| 1060 |
if (IsBranch(instr)) { |
| 1061 |
if (dist > kMaxBranchOffset) { |
| 1062 |
MOZ_ASSERT(next != LabelBase::INVALID_OFFSET); |
| 1063 |
MOZ_RELEASE_ASSERT((next - fixup_pos) <= kMaxBranchOffset); |
| 1064 |
MOZ_ASSERT(IsAuipc(editSrc(BufferOffset(next))->InstructionBits())); |
| 1065 |
MOZ_ASSERT( |
| 1066 |
IsJalr(editSrc(BufferOffset(next + 4))->InstructionBits())); |
| 1067 |
DEBUG_PRINTF("\t\ttrampolining: %d\n", next); |
| 1068 |
} else { |
| 1069 |
target_at_put(b, dest); |
| 1070 |
BufferOffset deadline(b.getOffset() + |
| 1071 |
ImmBranchMaxForwardOffset(CondBranchRangeType)); |
| 1072 |
m_buffer.unregisterBranchDeadline(CondBranchRangeType, deadline); |
| 1073 |
} |
| 1074 |
} else if (IsJal(instr)) { |
| 1075 |
if (dist > kMaxJumpOffset) { |
| 1076 |
MOZ_ASSERT(next != LabelBase::INVALID_OFFSET); |
| 1077 |
MOZ_RELEASE_ASSERT((next - fixup_pos) <= kMaxJumpOffset); |
| 1078 |
MOZ_ASSERT(IsAuipc(editSrc(BufferOffset(next))->InstructionBits())); |
| 1079 |
MOZ_ASSERT( |
| 1080 |
IsJalr(editSrc(BufferOffset(next + 4))->InstructionBits())); |
| 1081 |
DEBUG_PRINTF("\t\ttrampolining: %d\n", next); |
| 1082 |
} else { |
| 1083 |
target_at_put(b, dest); |
| 1084 |
BufferOffset deadline( |
| 1085 |
b.getOffset() + ImmBranchMaxForwardOffset(UncondBranchRangeType)); |
| 1086 |
m_buffer.unregisterBranchDeadline(UncondBranchRangeType, deadline); |
| 1087 |
} |
| 1088 |
} else { |
| 1089 |
MOZ_ASSERT(IsAuipc(instr)); |
| 1090 |
target_at_put(b, dest); |
| 1091 |
} |
| 1092 |
} while (next != LabelBase::INVALID_OFFSET); |
| 1093 |
} |
| 1094 |
label->bind(dest.getOffset()); |
| 1095 |
} |
| 1096 |
|
| 1097 |
void Assembler::Bind(uint8_t* rawCode, const CodeLabel& label) { |
| 1098 |
if (label.patchAt().bound()) { |
| 1099 |
auto mode = label.linkMode(); |
| 1100 |
intptr_t offset = label.patchAt().offset(); |
| 1101 |
intptr_t target = label.target().offset(); |
| 1102 |
|
| 1103 |
if (mode == CodeLabel::RawPointer) { |
| 1104 |
*reinterpret_cast<const void**>(rawCode + offset) = rawCode + target; |
| 1105 |
} else { |
| 1106 |
MOZ_ASSERT(mode == CodeLabel::MoveImmediate || |
| 1107 |
mode == CodeLabel::JumpImmediate); |
| 1108 |
Instruction* inst = (Instruction*)(rawCode + offset); |
| 1109 |
Assembler::UpdateLoad64Value(inst, (uint64_t)(rawCode + target)); |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
bool Assembler::is_near(Label* L) { |
| 1115 |
MOZ_ASSERT(L->bound()); |
| 1116 |
return is_intn((currentOffset() - L->offset()), kJumpOffsetBits); |
| 1117 |
} |
| 1118 |
|
| 1119 |
bool Assembler::is_near(Label* L, OffsetSize bits) { |
| 1120 |
if (L == nullptr || !L->bound()) return true; |
| 1121 |
return is_intn((currentOffset() - L->offset()), bits); |
| 1122 |
} |
| 1123 |
|
| 1124 |
bool Assembler::is_near_branch(Label* L) { |
| 1125 |
MOZ_ASSERT(L->bound()); |
| 1126 |
return is_intn((currentOffset() - L->offset()), kBranchOffsetBits); |
| 1127 |
} |
| 1128 |
|
| 1129 |
int32_t Assembler::branch_long_offset(Label* L) { |
| 1130 |
if (oom()) { |
| 1131 |
return kEndOfJumpChain; |
| 1132 |
} |
| 1133 |
intptr_t target_pos; |
| 1134 |
BufferOffset next_instr_offset = nextInstrOffset(2); |
| 1135 |
DEBUG_PRINTF("\tbranch_long_offset: %p to (%d)\n", L, |
| 1136 |
next_instr_offset.getOffset()); |
| 1137 |
if (L->bound()) { |
| 1138 |
JitSpew(JitSpew_Codegen, ".use Llabel %p on %d", L, |
| 1139 |
next_instr_offset.getOffset()); |
| 1140 |
target_pos = L->offset(); |
| 1141 |
} else { |
| 1142 |
if (L->used()) { |
| 1143 |
LabelCahe::Ptr p = label_cache_.lookup(L->offset()); |
| 1144 |
MOZ_ASSERT(p); |
| 1145 |
MOZ_ASSERT(p->key() == L->offset()); |
| 1146 |
target_pos = p->value().getOffset(); |
| 1147 |
target_at_put(BufferOffset(target_pos), next_instr_offset); |
| 1148 |
DEBUG_PRINTF("\tLabel %p added to link: %d\n", L, |
| 1149 |
next_instr_offset.getOffset()); |
| 1150 |
bool ok = label_cache_.put(L->offset(), next_instr_offset); |
| 1151 |
if (!ok) { |
| 1152 |
NoEnoughLabelCache(); |
| 1153 |
} |
| 1154 |
return kEndOfJumpChain; |
| 1155 |
} else { |
| 1156 |
JitSpew(JitSpew_Codegen, ".use Llabel %p on %d", L, |
| 1157 |
next_instr_offset.getOffset()); |
| 1158 |
L->use(next_instr_offset.getOffset()); |
| 1159 |
DEBUG_PRINTF("\tLabel %p added to link: %d\n", L, |
| 1160 |
next_instr_offset.getOffset()); |
| 1161 |
bool ok = label_cache_.putNew(L->offset(), next_instr_offset); |
| 1162 |
if (!ok) { |
| 1163 |
NoEnoughLabelCache(); |
| 1164 |
} |
| 1165 |
return kEndOfJumpChain; |
| 1166 |
} |
| 1167 |
} |
| 1168 |
intptr_t offset = target_pos - next_instr_offset.getOffset(); |
| 1169 |
MOZ_ASSERT((offset & 3) == 0); |
| 1170 |
MOZ_ASSERT(is_int32(offset)); |
| 1171 |
return static_cast<int32_t>(offset); |
| 1172 |
} |
| 1173 |
|
| 1174 |
int32_t Assembler::branch_offset_helper(Label* L, OffsetSize bits) { |
| 1175 |
if (oom()) { |
| 1176 |
return kEndOfJumpChain; |
| 1177 |
} |
| 1178 |
int32_t target_pos; |
| 1179 |
BufferOffset next_instr_offset = nextInstrOffset(); |
| 1180 |
DEBUG_PRINTF("\tbranch_offset_helper: %p to %d\n", L, |
| 1181 |
next_instr_offset.getOffset()); |
| 1182 |
// This is the last possible branch target. |
| 1183 |
if (L->bound()) { |
| 1184 |
JitSpew(JitSpew_Codegen, ".use Llabel %p on %d", L, |
| 1185 |
next_instr_offset.getOffset()); |
| 1186 |
target_pos = L->offset(); |
| 1187 |
} else { |
| 1188 |
BufferOffset deadline(next_instr_offset.getOffset() + |
| 1189 |
ImmBranchMaxForwardOffset(bits)); |
| 1190 |
DEBUG_PRINTF("\tregisterBranchDeadline %d type %d\n", deadline.getOffset(), |
| 1191 |
OffsetSizeToImmBranchRangeType(bits)); |
| 1192 |
m_buffer.registerBranchDeadline(OffsetSizeToImmBranchRangeType(bits), |
| 1193 |
deadline); |
| 1194 |
if (L->used()) { |
| 1195 |
LabelCahe::Ptr p = label_cache_.lookup(L->offset()); |
| 1196 |
MOZ_ASSERT(p); |
| 1197 |
MOZ_ASSERT(p->key() == L->offset()); |
| 1198 |
target_pos = p->value().getOffset(); |
| 1199 |
target_at_put(BufferOffset(target_pos), next_instr_offset); |
| 1200 |
DEBUG_PRINTF("\tLabel %p added to link: %d\n", L, |
| 1201 |
next_instr_offset.getOffset()); |
| 1202 |
bool ok = label_cache_.put(L->offset(), next_instr_offset); |
| 1203 |
if (!ok) { |
| 1204 |
NoEnoughLabelCache(); |
| 1205 |
} |
| 1206 |
return kEndOfJumpChain; |
| 1207 |
} else { |
| 1208 |
JitSpew(JitSpew_Codegen, ".use Llabel %p on %d", L, |
| 1209 |
next_instr_offset.getOffset()); |
| 1210 |
L->use(next_instr_offset.getOffset()); |
| 1211 |
bool ok = label_cache_.putNew(L->offset(), next_instr_offset); |
| 1212 |
if (!ok) { |
| 1213 |
NoEnoughLabelCache(); |
| 1214 |
} |
| 1215 |
DEBUG_PRINTF("\tLabel %p added to link: %d\n", L, |
| 1216 |
next_instr_offset.getOffset()); |
| 1217 |
return kEndOfJumpChain; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
int32_t offset = target_pos - next_instr_offset.getOffset(); |
| 1222 |
DEBUG_PRINTF("\toffset = %d\n", offset); |
| 1223 |
MOZ_ASSERT(is_intn(offset, bits)); |
| 1224 |
MOZ_ASSERT((offset & 1) == 0); |
| 1225 |
return offset; |
| 1226 |
} |
| 1227 |
|
| 1228 |
Assembler::Condition Assembler::InvertCondition(Condition cond) { |
| 1229 |
switch (cond) { |
| 1230 |
case Equal: |
| 1231 |
return NotEqual; |
| 1232 |
case NotEqual: |
| 1233 |
return Equal; |
| 1234 |
case Zero: |
| 1235 |
return NonZero; |
| 1236 |
case NonZero: |
| 1237 |
return Zero; |
| 1238 |
case LessThan: |
| 1239 |
return GreaterThanOrEqual; |
| 1240 |
case LessThanOrEqual: |
| 1241 |
return GreaterThan; |
| 1242 |
case GreaterThan: |
| 1243 |
return LessThanOrEqual; |
| 1244 |
case GreaterThanOrEqual: |
| 1245 |
return LessThan; |
| 1246 |
case Above: |
| 1247 |
return BelowOrEqual; |
| 1248 |
case AboveOrEqual: |
| 1249 |
return Below; |
| 1250 |
case Below: |
| 1251 |
return AboveOrEqual; |
| 1252 |
case BelowOrEqual: |
| 1253 |
return Above; |
| 1254 |
case Signed: |
| 1255 |
return NotSigned; |
| 1256 |
case NotSigned: |
| 1257 |
return Signed; |
| 1258 |
default: |
| 1259 |
MOZ_CRASH("unexpected condition"); |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
Assembler::DoubleCondition Assembler::InvertCondition(DoubleCondition cond) { |
| 1264 |
switch (cond) { |
| 1265 |
case DoubleOrdered: |
| 1266 |
return DoubleUnordered; |
| 1267 |
case DoubleEqual: |
| 1268 |
return DoubleNotEqualOrUnordered; |
| 1269 |
case DoubleNotEqual: |
| 1270 |
return DoubleEqualOrUnordered; |
| 1271 |
case DoubleGreaterThan: |
| 1272 |
return DoubleLessThanOrEqualOrUnordered; |
| 1273 |
case DoubleGreaterThanOrEqual: |
| 1274 |
return DoubleLessThanOrUnordered; |
| 1275 |
case DoubleLessThan: |
| 1276 |
return DoubleGreaterThanOrEqualOrUnordered; |
| 1277 |
case DoubleLessThanOrEqual: |
| 1278 |
return DoubleGreaterThanOrUnordered; |
| 1279 |
case DoubleUnordered: |
| 1280 |
return DoubleOrdered; |
| 1281 |
case DoubleEqualOrUnordered: |
| 1282 |
return DoubleNotEqual; |
| 1283 |
case DoubleNotEqualOrUnordered: |
| 1284 |
return DoubleEqual; |
| 1285 |
case DoubleGreaterThanOrUnordered: |
| 1286 |
return DoubleLessThanOrEqual; |
| 1287 |
case DoubleGreaterThanOrEqualOrUnordered: |
| 1288 |
return DoubleLessThan; |
| 1289 |
case DoubleLessThanOrUnordered: |
| 1290 |
return DoubleGreaterThanOrEqual; |
| 1291 |
case DoubleLessThanOrEqualOrUnordered: |
| 1292 |
return DoubleGreaterThan; |
| 1293 |
default: |
| 1294 |
MOZ_CRASH("unexpected condition"); |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
// Break / Trap instructions. |
| 1299 |
void Assembler::break_(uint32_t code, bool break_as_stop) { |
| 1300 |
// We need to invalidate breaks that could be stops as well because the |
| 1301 |
// simulator expects a char pointer after the stop instruction. |
| 1302 |
// See constants-mips.h for explanation. |
| 1303 |
MOZ_ASSERT( |
| 1304 |
(break_as_stop && code <= kMaxStopCode && code > kMaxTracepointCode) || |
| 1305 |
(!break_as_stop && (code > kMaxStopCode || code <= kMaxTracepointCode))); |
| 1306 |
|
| 1307 |
// since ebreak does not allow additional immediate field, we use the |
| 1308 |
// immediate field of lui instruction immediately following the ebreak to |
| 1309 |
// encode the "code" info |
| 1310 |
ebreak(); |
| 1311 |
MOZ_ASSERT(is_uint20(code)); |
| 1312 |
lui(zero_reg, code); |
| 1313 |
} |
| 1314 |
|
| 1315 |
void Assembler::ToggleToJmp(CodeLocationLabel inst_) { |
| 1316 |
Instruction* inst = (Instruction*)inst_.raw(); |
| 1317 |
MOZ_ASSERT(IsAddi(inst->InstructionBits())); |
| 1318 |
int32_t offset = inst->Imm12Value(); |
| 1319 |
MOZ_ASSERT(is_int12(offset)); |
| 1320 |
Instr jal_ = JAL | (0b000 << kFunct3Shift) | |
| 1321 |
(offset & 0xff000) | // bits 19-12 |
| 1322 |
((offset & 0x800) << 9) | // bit 11 |
| 1323 |
((offset & 0x7fe) << 20) | // bits 10-1 |
| 1324 |
((offset & 0x100000) << 11); // bit 20 |
| 1325 |
// jal(zero, offset); |
| 1326 |
*reinterpret_cast<Instr*>(inst) = jal_; |
| 1327 |
} |
| 1328 |
|
| 1329 |
void Assembler::ToggleToCmp(CodeLocationLabel inst_) { |
| 1330 |
Instruction* inst = (Instruction*)inst_.raw(); |
| 1331 |
|
| 1332 |
// toggledJump is allways used for short jumps. |
| 1333 |
MOZ_ASSERT(IsJal(inst->InstructionBits())); |
| 1334 |
// Replace "jal zero_reg, offset" with "addi $zero, $zero, offset" |
| 1335 |
int32_t offset = inst->Imm20JValue(); |
| 1336 |
MOZ_ASSERT(is_int12(offset)); |
| 1337 |
Instr addi_ = OP_IMM | (0b000 << kFunct3Shift) | |
| 1338 |
(offset << kImm12Shift); // addi(zero, zero, low_12); |
| 1339 |
*reinterpret_cast<Instr*>(inst) = addi_; |
| 1340 |
} |
| 1341 |
|
| 1342 |
bool Assembler::reserve(size_t size) { |
| 1343 |
// This buffer uses fixed-size chunks so there's no point in reserving |
| 1344 |
// now vs. on-demand. |
| 1345 |
return !oom(); |
| 1346 |
} |
| 1347 |
|
| 1348 |
static JitCode* CodeFromJump(Instruction* jump) { |
| 1349 |
uint8_t* target = (uint8_t*)Assembler::ExtractLoad64Value(jump); |
| 1350 |
return JitCode::FromExecutable(target); |
| 1351 |
} |
| 1352 |
|
| 1353 |
void Assembler::TraceJumpRelocations(JSTracer* trc, JitCode* code, |
| 1354 |
CompactBufferReader& reader) { |
| 1355 |
while (reader.more()) { |
| 1356 |
JitCode* child = |
| 1357 |
CodeFromJump((Instruction*)(code->raw() + reader.readUnsigned())); |
| 1358 |
TraceManuallyBarrieredEdge(trc, &child, "rel32"); |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
static void TraceOneDataRelocation(JSTracer* trc, |
| 1363 |
mozilla::Maybe<AutoWritableJitCode>& awjc, |
| 1364 |
JitCode* code, Instruction* inst) { |
| 1365 |
void* ptr = (void*)Assembler::ExtractLoad64Value(inst); |
| 1366 |
void* prior = ptr; |
| 1367 |
|
| 1368 |
// Data relocations can be for Values or for raw pointers. If a Value is |
| 1369 |
// zero-tagged, we can trace it as if it were a raw pointer. If a Value |
| 1370 |
// is not zero-tagged, we have to interpret it as a Value to ensure that the |
| 1371 |
// tag bits are masked off to recover the actual pointer. |
| 1372 |
uintptr_t word = reinterpret_cast<uintptr_t>(ptr); |
| 1373 |
if (word >> JSVAL_TAG_SHIFT) { |
| 1374 |
// This relocation is a Value with a non-zero tag. |
| 1375 |
Value v = Value::fromRawBits(word); |
| 1376 |
TraceManuallyBarrieredEdge(trc, &v, "jit-masm-value"); |
| 1377 |
ptr = (void*)v.bitsAsPunboxPointer(); |
| 1378 |
} else { |
| 1379 |
// This relocation is a raw pointer or a Value with a zero tag. |
| 1380 |
// No barrier needed since these are constants. |
| 1381 |
TraceManuallyBarrieredGenericPointerEdge( |
| 1382 |
trc, reinterpret_cast<gc::Cell**>(&ptr), "jit-masm-ptr"); |
| 1383 |
} |
| 1384 |
|
| 1385 |
if (ptr != prior) { |
| 1386 |
if (awjc.isNothing()) { |
| 1387 |
awjc.emplace(code); |
| 1388 |
} |
| 1389 |
Assembler::UpdateLoad64Value(inst, uint64_t(ptr)); |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
/* static */ |
| 1394 |
void Assembler::TraceDataRelocations(JSTracer* trc, JitCode* code, |
| 1395 |
CompactBufferReader& reader) { |
| 1396 |
mozilla::Maybe<AutoWritableJitCode> awjc; |
| 1397 |
while (reader.more()) { |
| 1398 |
size_t offset = reader.readUnsigned(); |
| 1399 |
Instruction* inst = (Instruction*)(code->raw() + offset); |
| 1400 |
TraceOneDataRelocation(trc, awjc, code, inst); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
UseScratchRegisterScope::UseScratchRegisterScope(Assembler* assembler) |
| 1405 |
: available_(assembler->GetScratchRegisterList()), |
| 1406 |
old_available_(*available_) {} |
| 1407 |
|
| 1408 |
UseScratchRegisterScope::~UseScratchRegisterScope() { |
| 1409 |
*available_ = old_available_; |
| 1410 |
} |
| 1411 |
|
| 1412 |
Register UseScratchRegisterScope::Acquire() { |
| 1413 |
MOZ_ASSERT(available_ != nullptr); |
| 1414 |
MOZ_ASSERT(!available_->empty()); |
| 1415 |
Register index = GeneralRegisterSet::FirstRegister(available_->bits()); |
| 1416 |
available_->takeRegisterIndex(index); |
| 1417 |
return index; |
| 1418 |
} |
| 1419 |
|
| 1420 |
bool UseScratchRegisterScope::hasAvailable() const { |
| 1421 |
return (available_->size()) != 0; |
| 1422 |
} |
| 1423 |
|
| 1424 |
void Assembler::retarget(Label* label, Label* target) { |
| 1425 |
spew("retarget %p -> %p", label, target); |
| 1426 |
if (label->used() && !oom()) { |
| 1427 |
if (target->bound()) { |
| 1428 |
bind(label, BufferOffset(target)); |
| 1429 |
} else if (target->used()) { |
| 1430 |
// The target is not bound but used. Prepend label's branch list |
| 1431 |
// onto target's. |
| 1432 |
int32_t next; |
| 1433 |
BufferOffset labelBranchOffset(label); |
| 1434 |
|
| 1435 |
// Find the head of the use chain for label. |
| 1436 |
do { |
| 1437 |
next = next_link(label, false); |
| 1438 |
labelBranchOffset = BufferOffset(next); |
| 1439 |
} while (next != LabelBase::INVALID_OFFSET); |
| 1440 |
|
| 1441 |
// Then patch the head of label's use chain to the tail of |
| 1442 |
// target's use chain, prepending the entire use chain of target. |
| 1443 |
target->use(label->offset()); |
| 1444 |
target_at_put(labelBranchOffset, BufferOffset(target)); |
| 1445 |
MOZ_CRASH("check"); |
| 1446 |
} else { |
| 1447 |
// The target is unbound and unused. We can just take the head of |
| 1448 |
// the list hanging off of label, and dump that into target. |
| 1449 |
target->use(label->offset()); |
| 1450 |
} |
| 1451 |
} |
| 1452 |
label->reset(); |
| 1453 |
} |
| 1454 |
|
| 1455 |
bool Assembler::appendRawCode(const uint8_t* code, size_t numBytes) { |
| 1456 |
if (m_buffer.oom()) { |
| 1457 |
return false; |
| 1458 |
} |
| 1459 |
while (numBytes > SliceSize) { |
| 1460 |
m_buffer.putBytes(SliceSize, code); |
| 1461 |
numBytes -= SliceSize; |
| 1462 |
code += SliceSize; |
| 1463 |
} |
| 1464 |
m_buffer.putBytes(numBytes, code); |
| 1465 |
return !m_buffer.oom(); |
| 1466 |
} |
| 1467 |
|
| 1468 |
void Assembler::ToggleCall(CodeLocationLabel inst_, bool enabled) { |
| 1469 |
Instruction* i0 = (Instruction*)inst_.raw(); |
| 1470 |
Instruction* i1 = (Instruction*)(inst_.raw() + 1 * kInstrSize); |
| 1471 |
Instruction* i2 = (Instruction*)(inst_.raw() + 2 * kInstrSize); |
| 1472 |
Instruction* i3 = (Instruction*)(inst_.raw() + 3 * kInstrSize); |
| 1473 |
Instruction* i4 = (Instruction*)(inst_.raw() + 4 * kInstrSize); |
| 1474 |
Instruction* i5 = (Instruction*)(inst_.raw() + 5 * kInstrSize); |
| 1475 |
Instruction* i6 = (Instruction*)(inst_.raw() + 6 * kInstrSize); |
| 1476 |
|
| 1477 |
MOZ_ASSERT(IsLui(i0->InstructionBits())); |
| 1478 |
MOZ_ASSERT(IsAddi(i1->InstructionBits())); |
| 1479 |
MOZ_ASSERT(IsSlli(i2->InstructionBits())); |
| 1480 |
MOZ_ASSERT(IsOri(i3->InstructionBits())); |
| 1481 |
MOZ_ASSERT(IsSlli(i4->InstructionBits())); |
| 1482 |
MOZ_ASSERT(IsOri(i5->InstructionBits())); |
| 1483 |
if (enabled) { |
| 1484 |
Instr jalr_ = JALR | (ra.code() << kRdShift) | (0x0 << kFunct3Shift) | |
| 1485 |
(i5->RdValue() << kRs1Shift) | (0x0 << kImm12Shift); |
| 1486 |
*((Instr*)i6) = jalr_; |
| 1487 |
} else { |
| 1488 |
*((Instr*)i6) = kNopByte; |
| 1489 |
} |
| 1490 |
} |
| 1491 |
|
| 1492 |
void Assembler::PatchShortRangeBranchToVeneer(Buffer* buffer, unsigned rangeIdx, |
| 1493 |
BufferOffset deadline, |
| 1494 |
BufferOffset veneer) { |
| 1495 |
if (buffer->oom()) { |
| 1496 |
return; |
| 1497 |
} |
| 1498 |
DEBUG_PRINTF("\tPatchShortRangeBranchToVeneer\n"); |
| 1499 |
// Reconstruct the position of the branch from (rangeIdx, deadline). |
| 1500 |
ImmBranchRangeType branchRange = static_cast<ImmBranchRangeType>(rangeIdx); |
| 1501 |
BufferOffset branch(deadline.getOffset() - |
| 1502 |
ImmBranchMaxForwardOffset(branchRange)); |
| 1503 |
Instruction* branchInst = buffer->getInst(branch); |
| 1504 |
Instruction* veneerInst_1 = buffer->getInst(veneer); |
| 1505 |
Instruction* veneerInst_2 = |
| 1506 |
buffer->getInst(BufferOffset(veneer.getOffset() + 4)); |
| 1507 |
// Verify that the branch range matches what's encoded. |
| 1508 |
DEBUG_PRINTF("\t%p(%x): ", branchInst, branch.getOffset()); |
| 1509 |
disassembleInstr(branchInst->InstructionBits(), JitSpew_Codegen); |
| 1510 |
DEBUG_PRINTF("\t instert veneer %x, branch:%x deadline: %x\n", |
| 1511 |
veneer.getOffset(), branch.getOffset(), deadline.getOffset()); |
| 1512 |
MOZ_ASSERT(branchRange <= UncondBranchRangeType); |
| 1513 |
MOZ_ASSERT(branchInst->GetImmBranchRangeType() == branchRange); |
| 1514 |
// emit a long jump slot |
| 1515 |
Instr auipc = AUIPC | (t6.code() << kRdShift) | (0x0 << kImm20Shift); |
| 1516 |
Instr jalr = JALR | (zero_reg.code() << kRdShift) | (0x0 << kFunct3Shift) | |
| 1517 |
(t6.code() << kRs1Shift) | (0x0 << kImm12Shift); |
| 1518 |
|
| 1519 |
// We want to insert veneer after branch in the linked list of instructions |
| 1520 |
// that use the same unbound label. |
| 1521 |
// The veneer should be an unconditional branch. |
| 1522 |
int32_t nextElemOffset = target_at(buffer->getInst(branch), branch, false); |
| 1523 |
int32_t dist; |
| 1524 |
// If offset is 0, this is the end of the linked list. |
| 1525 |
if (nextElemOffset != kEndOfChain) { |
| 1526 |
// Make the offset relative to veneer so it targets the same instruction |
| 1527 |
// as branchInst. |
| 1528 |
dist = nextElemOffset - veneer.getOffset(); |
| 1529 |
} else { |
| 1530 |
dist = 0; |
| 1531 |
} |
| 1532 |
int32_t Hi20 = (((int32_t)dist + 0x800) >> 12); |
| 1533 |
int32_t Lo12 = (int32_t)dist << 20 >> 20; |
| 1534 |
auipc = SetAuipcOffset(Hi20, auipc); |
| 1535 |
jalr = SetJalrOffset(Lo12, jalr); |
| 1536 |
// insert veneer |
| 1537 |
veneerInst_1->SetInstructionBits(auipc); |
| 1538 |
veneerInst_2->SetInstructionBits(jalr); |
| 1539 |
// Now link branchInst to veneer. |
| 1540 |
if (IsBranch(branchInst->InstructionBits())) { |
| 1541 |
branchInst->SetInstructionBits(SetBranchOffset( |
| 1542 |
branch.getOffset(), veneer.getOffset(), branchInst->InstructionBits())); |
| 1543 |
} else { |
| 1544 |
MOZ_ASSERT(IsJal(branchInst->InstructionBits())); |
| 1545 |
branchInst->SetInstructionBits(SetJalOffset( |
| 1546 |
branch.getOffset(), veneer.getOffset(), branchInst->InstructionBits())); |
| 1547 |
} |
| 1548 |
DEBUG_PRINTF("\tfix to veneer:"); |
| 1549 |
disassembleInstr(branchInst->InstructionBits()); |
| 1550 |
} |
| 1551 |
} // namespace jit |
| 1552 |
} // namespace js |