| 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 |
|
| 41 |
#ifndef jit_riscv64_Assembler_riscv64_h |
| 42 |
#define jit_riscv64_Assembler_riscv64_h |
| 43 |
|
| 44 |
#include "mozilla/Assertions.h" |
| 45 |
#include "mozilla/Sprintf.h" |
| 46 |
|
| 47 |
#include <stdint.h> |
| 48 |
|
| 49 |
#include "jit/CompactBuffer.h" |
| 50 |
#include "jit/JitCode.h" |
| 51 |
#include "jit/JitSpewer.h" |
| 52 |
#include "jit/Registers.h" |
| 53 |
#include "jit/RegisterSets.h" |
| 54 |
#include "jit/riscv64/Architecture-riscv64.h" |
| 55 |
#include "jit/riscv64/constant/Constant-riscv64.h" |
| 56 |
#include "jit/riscv64/extension/base-assembler-riscv.h" |
| 57 |
#include "jit/riscv64/extension/base-riscv-i.h" |
| 58 |
#include "jit/riscv64/extension/extension-riscv-a.h" |
| 59 |
#include "jit/riscv64/extension/extension-riscv-c.h" |
| 60 |
#include "jit/riscv64/extension/extension-riscv-d.h" |
| 61 |
#include "jit/riscv64/extension/extension-riscv-f.h" |
| 62 |
#include "jit/riscv64/extension/extension-riscv-m.h" |
| 63 |
#include "jit/riscv64/extension/extension-riscv-v.h" |
| 64 |
#include "jit/riscv64/extension/extension-riscv-zicsr.h" |
| 65 |
#include "jit/riscv64/extension/extension-riscv-zifencei.h" |
| 66 |
#include "jit/riscv64/Register-riscv64.h" |
| 67 |
#include "jit/shared/Assembler-shared.h" |
| 68 |
#include "jit/shared/Disassembler-shared.h" |
| 69 |
#include "jit/shared/IonAssemblerBufferWithConstantPools.h" |
| 70 |
#include "js/HashTable.h" |
| 71 |
#include "wasm/WasmTypeDecls.h" |
| 72 |
namespace js { |
| 73 |
namespace jit { |
| 74 |
|
| 75 |
struct ScratchFloat32Scope : public AutoFloatRegisterScope { |
| 76 |
explicit ScratchFloat32Scope(MacroAssembler& masm) |
| 77 |
: AutoFloatRegisterScope(masm, ScratchFloat32Reg) {} |
| 78 |
}; |
| 79 |
|
| 80 |
struct ScratchDoubleScope : public AutoFloatRegisterScope { |
| 81 |
explicit ScratchDoubleScope(MacroAssembler& masm) |
| 82 |
: AutoFloatRegisterScope(masm, ScratchDoubleReg) {} |
| 83 |
}; |
| 84 |
|
| 85 |
struct ScratchRegisterScope : public AutoRegisterScope { |
| 86 |
explicit ScratchRegisterScope(MacroAssembler& masm) |
| 87 |
: AutoRegisterScope(masm, ScratchRegister) {} |
| 88 |
}; |
| 89 |
|
| 90 |
class MacroAssembler; |
| 91 |
|
| 92 |
static constexpr uint32_t ABIStackAlignment = 8; |
| 93 |
static constexpr uint32_t CodeAlignment = 16; |
| 94 |
static constexpr uint32_t JitStackAlignment = 8; |
| 95 |
static constexpr uint32_t JitStackValueAlignment = |
| 96 |
JitStackAlignment / sizeof(Value); |
| 97 |
static const uint32_t WasmStackAlignment = 16; |
| 98 |
static const uint32_t WasmTrapInstructionLength = 2 * sizeof(uint32_t); |
| 99 |
// See comments in wasm::GenerateFunctionPrologue. The difference between these |
| 100 |
// is the size of the largest callable prologue on the platform. |
| 101 |
static constexpr uint32_t WasmCheckedCallEntryOffset = 0u; |
| 102 |
static constexpr uint32_t WasmCheckedTailEntryOffset = 20u; |
| 103 |
|
| 104 |
static const Scale ScalePointer = TimesEight; |
| 105 |
|
| 106 |
class Assembler; |
| 107 |
|
| 108 |
static constexpr int32_t SliceSize = 1024; |
| 109 |
|
| 110 |
typedef js::jit::AssemblerBufferWithConstantPools< |
| 111 |
SliceSize, 4, Instruction, Assembler, NumShortBranchRangeTypes> |
| 112 |
Buffer; |
| 113 |
|
| 114 |
class Assembler : public AssemblerShared, |
| 115 |
public AssemblerRISCVI, |
| 116 |
public AssemblerRISCVA, |
| 117 |
public AssemblerRISCVF, |
| 118 |
public AssemblerRISCVD, |
| 119 |
public AssemblerRISCVM, |
| 120 |
public AssemblerRISCVC, |
| 121 |
public AssemblerRISCVZicsr, |
| 122 |
public AssemblerRISCVZifencei { |
| 123 |
GeneralRegisterSet scratch_register_list_; |
| 124 |
|
| 125 |
static constexpr int kInvalidSlotPos = -1; |
| 126 |
|
| 127 |
#ifdef JS_JITSPEW |
| 128 |
Sprinter* printer; |
| 129 |
#endif |
| 130 |
bool enoughLabelCache_ = true; |
| 131 |
|
| 132 |
protected: |
| 133 |
using LabelOffset = int32_t; |
| 134 |
using LabelCahe = |
| 135 |
HashMap<LabelOffset, BufferOffset, js::DefaultHasher<LabelOffset>, |
| 136 |
js::SystemAllocPolicy>; |
| 137 |
LabelCahe label_cache_; |
| 138 |
void NoEnoughLabelCache() { enoughLabelCache_ = false; } |
| 139 |
CompactBufferWriter jumpRelocations_; |
| 140 |
CompactBufferWriter dataRelocations_; |
| 141 |
Buffer m_buffer; |
| 142 |
bool isFinished = false; |
| 143 |
Instruction* editSrc(BufferOffset bo) { return m_buffer.getInst(bo); } |
| 144 |
|
| 145 |
struct RelativePatch { |
| 146 |
// the offset within the code buffer where the value is loaded that |
| 147 |
// we want to fix-up |
| 148 |
BufferOffset offset; |
| 149 |
void* target; |
| 150 |
RelocationKind kind; |
| 151 |
|
| 152 |
RelativePatch(BufferOffset offset, void* target, RelocationKind kind) |
| 153 |
: offset(offset), target(target), kind(kind) {} |
| 154 |
}; |
| 155 |
|
| 156 |
js::Vector<RelativePatch, 8, SystemAllocPolicy> jumps_; |
| 157 |
|
| 158 |
void addPendingJump(BufferOffset src, ImmPtr target, RelocationKind kind) { |
| 159 |
enoughMemory_ &= jumps_.append(RelativePatch(src, target.value, kind)); |
| 160 |
if (kind == RelocationKind::JITCODE) { |
| 161 |
jumpRelocations_.writeUnsigned(src.getOffset()); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
void addLongJump(BufferOffset src, BufferOffset dst) { |
| 166 |
CodeLabel cl; |
| 167 |
cl.patchAt()->bind(src.getOffset()); |
| 168 |
cl.target()->bind(dst.getOffset()); |
| 169 |
cl.setLinkMode(CodeLabel::JumpImmediate); |
| 170 |
addCodeLabel(std::move(cl)); |
| 171 |
} |
| 172 |
|
| 173 |
public: |
| 174 |
static bool FLAG_riscv_debug; |
| 175 |
|
| 176 |
Assembler() |
| 177 |
: scratch_register_list_((1 << t5.code()) | (1 << t4.code()) | |
| 178 |
(1 << t6.code())), |
| 179 |
#ifdef JS_JITSPEW |
| 180 |
printer(nullptr), |
| 181 |
#endif |
| 182 |
m_buffer(/*guardSize*/ 2, /*headerSize*/ 2, /*instBufferAlign*/ 8, |
| 183 |
/*poolMaxOffset*/ GetPoolMaxOffset(), /*pcBias*/ 8, |
| 184 |
/*alignFillInst*/ kNopByte, /*nopFillInst*/ kNopByte), |
| 185 |
isFinished(false) { |
| 186 |
} |
| 187 |
static uint32_t NopFill; |
| 188 |
static uint32_t AsmPoolMaxOffset; |
| 189 |
static uint32_t GetPoolMaxOffset(); |
| 190 |
bool reserve(size_t size); |
| 191 |
bool oom() const; |
| 192 |
void setPrinter(Sprinter* sp) { |
| 193 |
#ifdef JS_JITSPEW |
| 194 |
printer = sp; |
| 195 |
#endif |
| 196 |
} |
| 197 |
void finish() { |
| 198 |
MOZ_ASSERT(!isFinished); |
| 199 |
isFinished = true; |
| 200 |
} |
| 201 |
void enterNoPool(size_t maxInst) { m_buffer.enterNoPool(maxInst); } |
| 202 |
void leaveNoPool() { m_buffer.leaveNoPool(); } |
| 203 |
bool swapBuffer(wasm::Bytes& bytes); |
| 204 |
// Size of the instruction stream, in bytes. |
| 205 |
size_t size() const; |
| 206 |
// Size of the data table, in bytes. |
| 207 |
size_t bytesNeeded() const; |
| 208 |
// Size of the jump relocation table, in bytes. |
| 209 |
size_t jumpRelocationTableBytes() const; |
| 210 |
size_t dataRelocationTableBytes() const; |
| 211 |
void copyJumpRelocationTable(uint8_t* dest); |
| 212 |
void copyDataRelocationTable(uint8_t* dest); |
| 213 |
// Copy the assembly code to the given buffer, and perform any pending |
| 214 |
// relocations relying on the target address. |
| 215 |
void executableCopy(uint8_t* buffer); |
| 216 |
// API for speaking with the IonAssemblerBufferWithConstantPools generate an |
| 217 |
// initial placeholder instruction that we want to later fix up. |
| 218 |
static void InsertIndexIntoTag(uint8_t* load, uint32_t index); |
| 219 |
static void PatchConstantPoolLoad(void* loadAddr, void* constPoolAddr); |
| 220 |
// We're not tracking short-range branches for ARM for now. |
| 221 |
static void PatchShortRangeBranchToVeneer(Buffer*, unsigned rangeIdx, |
| 222 |
BufferOffset deadline, |
| 223 |
BufferOffset veneer); |
| 224 |
struct PoolHeader { |
| 225 |
uint32_t data; |
| 226 |
|
| 227 |
struct Header { |
| 228 |
// The size should take into account the pool header. |
| 229 |
// The size is in units of Instruction (4bytes), not byte. |
| 230 |
union { |
| 231 |
struct { |
| 232 |
uint32_t size : 15; |
| 233 |
|
| 234 |
// "Natural" guards are part of the normal instruction stream, |
| 235 |
// while "non-natural" guards are inserted for the sole purpose |
| 236 |
// of skipping around a pool. |
| 237 |
uint32_t isNatural : 1; |
| 238 |
uint32_t ONES : 16; |
| 239 |
}; |
| 240 |
uint32_t data; |
| 241 |
}; |
| 242 |
|
| 243 |
Header(int size_, bool isNatural_) |
| 244 |
: size(size_), isNatural(isNatural_), ONES(0xffff) {} |
| 245 |
|
| 246 |
Header(uint32_t data) : data(data) { |
| 247 |
static_assert(sizeof(Header) == sizeof(uint32_t)); |
| 248 |
MOZ_ASSERT(ONES == 0xffff); |
| 249 |
} |
| 250 |
|
| 251 |
uint32_t raw() const { |
| 252 |
static_assert(sizeof(Header) == sizeof(uint32_t)); |
| 253 |
return data; |
| 254 |
} |
| 255 |
}; |
| 256 |
|
| 257 |
PoolHeader(int size_, bool isNatural_) |
| 258 |
: data(Header(size_, isNatural_).raw()) {} |
| 259 |
|
| 260 |
uint32_t size() const { |
| 261 |
Header tmp(data); |
| 262 |
return tmp.size; |
| 263 |
} |
| 264 |
|
| 265 |
uint32_t isNatural() const { |
| 266 |
Header tmp(data); |
| 267 |
return tmp.isNatural; |
| 268 |
} |
| 269 |
}; |
| 270 |
|
| 271 |
static void WritePoolHeader(uint8_t* start, Pool* p, bool isNatural); |
| 272 |
static void WritePoolGuard(BufferOffset branch, Instruction* inst, |
| 273 |
BufferOffset dest); |
| 274 |
void processCodeLabels(uint8_t* rawCode); |
| 275 |
BufferOffset nextOffset() { return m_buffer.nextOffset(); } |
| 276 |
// Get the buffer offset of the next inserted instruction. This may flush |
| 277 |
// constant pools. |
| 278 |
BufferOffset nextInstrOffset(int numInstr = 1) { |
| 279 |
return m_buffer.nextInstrOffset(numInstr); |
| 280 |
} |
| 281 |
void comment(const char* msg) { spew("; %s", msg); } |
| 282 |
|
| 283 |
#ifdef JS_JITSPEW |
| 284 |
inline void spew(const char* fmt, ...) MOZ_FORMAT_PRINTF(2, 3) { |
| 285 |
if (MOZ_UNLIKELY(printer || JitSpewEnabled(JitSpew_Codegen))) { |
| 286 |
va_list va; |
| 287 |
va_start(va, fmt); |
| 288 |
spew(fmt, va); |
| 289 |
va_end(va); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
#else |
| 294 |
MOZ_ALWAYS_INLINE void spew(const char* fmt, ...) MOZ_FORMAT_PRINTF(2, 3) {} |
| 295 |
#endif |
| 296 |
|
| 297 |
#ifdef JS_JITSPEW |
| 298 |
MOZ_COLD void spew(const char* fmt, va_list va) MOZ_FORMAT_PRINTF(2, 0) { |
| 299 |
// Buffer to hold the formatted string. Note that this may contain |
| 300 |
// '%' characters, so do not pass it directly to printf functions. |
| 301 |
char buf[200]; |
| 302 |
|
| 303 |
int i = VsprintfLiteral(buf, fmt, va); |
| 304 |
if (i > -1) { |
| 305 |
if (printer) { |
| 306 |
printer->printf("%s\n", buf); |
| 307 |
} |
| 308 |
js::jit::JitSpew(js::jit::JitSpew_Codegen, "%s", buf); |
| 309 |
} |
| 310 |
} |
| 311 |
#endif |
| 312 |
|
| 313 |
enum Condition { |
| 314 |
Overflow = overflow, |
| 315 |
Below = Uless, |
| 316 |
BelowOrEqual = Uless_equal, |
| 317 |
Above = Ugreater, |
| 318 |
AboveOrEqual = Ugreater_equal, |
| 319 |
Equal = equal, |
| 320 |
NotEqual = not_equal, |
| 321 |
GreaterThan = greater, |
| 322 |
GreaterThanOrEqual = greater_equal, |
| 323 |
LessThan = less, |
| 324 |
LessThanOrEqual = less_equal, |
| 325 |
Always = cc_always, |
| 326 |
CarrySet, |
| 327 |
CarryClear, |
| 328 |
Signed, |
| 329 |
NotSigned, |
| 330 |
Zero, |
| 331 |
NonZero, |
| 332 |
}; |
| 333 |
|
| 334 |
enum DoubleCondition { |
| 335 |
// These conditions will only evaluate to true if the comparison is ordered |
| 336 |
// - i.e. neither operand is NaN. |
| 337 |
DoubleOrdered, |
| 338 |
DoubleEqual, |
| 339 |
DoubleNotEqual, |
| 340 |
DoubleGreaterThan, |
| 341 |
DoubleGreaterThanOrEqual, |
| 342 |
DoubleLessThan, |
| 343 |
DoubleLessThanOrEqual, |
| 344 |
// If either operand is NaN, these conditions always evaluate to true. |
| 345 |
DoubleUnordered, |
| 346 |
DoubleEqualOrUnordered, |
| 347 |
DoubleNotEqualOrUnordered, |
| 348 |
DoubleGreaterThanOrUnordered, |
| 349 |
DoubleGreaterThanOrEqualOrUnordered, |
| 350 |
DoubleLessThanOrUnordered, |
| 351 |
DoubleLessThanOrEqualOrUnordered, |
| 352 |
FIRST_UNORDERED = DoubleUnordered, |
| 353 |
LAST_UNORDERED = DoubleLessThanOrEqualOrUnordered |
| 354 |
}; |
| 355 |
|
| 356 |
Register getStackPointer() const { return StackPointer; } |
| 357 |
void flushBuffer() {} |
| 358 |
static int disassembleInstr(Instr instr, bool enable_spew = false); |
| 359 |
int target_at(BufferOffset pos, bool is_internal); |
| 360 |
static int target_at(Instruction* instruction, BufferOffset pos, |
| 361 |
bool is_internal, Instruction* instruction2 = nullptr); |
| 362 |
uint32_t next_link(Label* label, bool is_internal); |
| 363 |
static uint64_t target_address_at(Instruction* pos); |
| 364 |
static void set_target_value_at(Instruction* pc, uint64_t target); |
| 365 |
void target_at_put(BufferOffset pos, BufferOffset target_pos, |
| 366 |
bool trampoline = false); |
| 367 |
virtual int32_t branch_offset_helper(Label* L, OffsetSize bits); |
| 368 |
int32_t branch_long_offset(Label* L); |
| 369 |
|
| 370 |
// Determines if Label is bound and near enough so that branch instruction |
| 371 |
// can be used to reach it, instead of jump instruction. |
| 372 |
bool is_near(Label* L); |
| 373 |
bool is_near(Label* L, OffsetSize bits); |
| 374 |
bool is_near_branch(Label* L); |
| 375 |
|
| 376 |
void nopAlign(int m) { |
| 377 |
MOZ_ASSERT(m >= 4 && (m & (m - 1)) == 0); |
| 378 |
while ((currentOffset() & (m - 1)) != 0) { |
| 379 |
nop(); |
| 380 |
} |
| 381 |
} |
| 382 |
virtual void emit(Instr x) { |
| 383 |
MOZ_ASSERT(hasCreator()); |
| 384 |
m_buffer.putInt(x); |
| 385 |
#ifdef DEBUG |
| 386 |
if (!oom()) { |
| 387 |
DEBUG_PRINTF( |
| 388 |
"0x%" PRIx64 "(%" PRIxPTR "):", |
| 389 |
(uint64_t)editSrc(BufferOffset(currentOffset() - sizeof(Instr))), |
| 390 |
currentOffset() - sizeof(Instr)); |
| 391 |
disassembleInstr(x, JitSpewEnabled(JitSpew_Codegen)); |
| 392 |
} |
| 393 |
#endif |
| 394 |
} |
| 395 |
virtual void emit(ShortInstr x) { MOZ_CRASH(); } |
| 396 |
virtual void emit(uint64_t x) { MOZ_CRASH(); } |
| 397 |
virtual void emit(uint32_t x) { |
| 398 |
DEBUG_PRINTF( |
| 399 |
"0x%" PRIx64 "(%" PRIxPTR "): uint32_t: %" PRId32 "\n", |
| 400 |
(uint64_t)editSrc(BufferOffset(currentOffset() - sizeof(Instr))), |
| 401 |
currentOffset() - sizeof(Instr), x); |
| 402 |
m_buffer.putInt(x); |
| 403 |
} |
| 404 |
|
| 405 |
void instr_at_put(BufferOffset offset, Instr instr) { |
| 406 |
DEBUG_PRINTF("\t[instr_at_put\n"); |
| 407 |
DEBUG_PRINTF("\t%p %d \n\t", editSrc(offset), offset.getOffset()); |
| 408 |
disassembleInstr(editSrc(offset)->InstructionBits()); |
| 409 |
DEBUG_PRINTF("\t"); |
| 410 |
*reinterpret_cast<Instr*>(editSrc(offset)) = instr; |
| 411 |
disassembleInstr(editSrc(offset)->InstructionBits()); |
| 412 |
DEBUG_PRINTF("\t]\n"); |
| 413 |
} |
| 414 |
|
| 415 |
static Condition InvertCondition(Condition); |
| 416 |
|
| 417 |
static DoubleCondition InvertCondition(DoubleCondition); |
| 418 |
|
| 419 |
static uint64_t ExtractLoad64Value(Instruction* inst0); |
| 420 |
static void UpdateLoad64Value(Instruction* inst0, uint64_t value); |
| 421 |
static void PatchDataWithValueCheck(CodeLocationLabel label, ImmPtr newValue, |
| 422 |
ImmPtr expectedValue); |
| 423 |
static void PatchDataWithValueCheck(CodeLocationLabel label, |
| 424 |
PatchedImmPtr newValue, |
| 425 |
PatchedImmPtr expectedValue); |
| 426 |
static void PatchWrite_Imm32(CodeLocationLabel label, Imm32 imm); |
| 427 |
|
| 428 |
static void PatchWrite_NearCall(CodeLocationLabel start, |
| 429 |
CodeLocationLabel toCall) { |
| 430 |
Instruction* inst = (Instruction*)start.raw(); |
| 431 |
uint8_t* dest = toCall.raw(); |
| 432 |
|
| 433 |
// Overwrite whatever instruction used to be here with a call. |
| 434 |
// Always use long jump for two reasons: |
| 435 |
// - Jump has to be the same size because of PatchWrite_NearCallSize. |
| 436 |
// - Return address has to be at the end of replaced block. |
| 437 |
// Short jump wouldn't be more efficient. |
| 438 |
// WriteLoad64Instructions will emit 6 instrs to load a addr. |
| 439 |
Assembler::WriteLoad64Instructions(inst, ScratchRegister, (uint64_t)dest); |
| 440 |
Instr jalr_ = JALR | (ra.code() << kRdShift) | (0x0 << kFunct3Shift) | |
| 441 |
(ScratchRegister.code() << kRs1Shift) | (0x0 << kImm12Shift); |
| 442 |
*reinterpret_cast<Instr*>(inst + 6 * kInstrSize) = jalr_; |
| 443 |
} |
| 444 |
static void WriteLoad64Instructions(Instruction* inst0, Register reg, |
| 445 |
uint64_t value); |
| 446 |
|
| 447 |
static uint32_t PatchWrite_NearCallSize() { return 7 * sizeof(uint32_t); } |
| 448 |
|
| 449 |
static void TraceJumpRelocations(JSTracer* trc, JitCode* code, |
| 450 |
CompactBufferReader& reader); |
| 451 |
static void TraceDataRelocations(JSTracer* trc, JitCode* code, |
| 452 |
CompactBufferReader& reader); |
| 453 |
|
| 454 |
static void ToggleToJmp(CodeLocationLabel inst_); |
| 455 |
static void ToggleToCmp(CodeLocationLabel inst_); |
| 456 |
static void ToggleCall(CodeLocationLabel inst_, bool enable); |
| 457 |
|
| 458 |
static void Bind(uint8_t* rawCode, const CodeLabel& label); |
| 459 |
// label operations |
| 460 |
void bind(Label* label, BufferOffset boff = BufferOffset()); |
| 461 |
void bind(CodeLabel* label) { label->target()->bind(currentOffset()); } |
| 462 |
uint32_t currentOffset() { return nextOffset().getOffset(); } |
| 463 |
void retarget(Label* label, Label* target); |
| 464 |
static uint32_t NopSize() { return 4; } |
| 465 |
|
| 466 |
static uint64_t GetPointer(uint8_t* instPtr) { |
| 467 |
Instruction* inst = (Instruction*)instPtr; |
| 468 |
return Assembler::ExtractLoad64Value(inst); |
| 469 |
} |
| 470 |
|
| 471 |
static bool HasRoundInstruction(RoundingMode) { return false; } |
| 472 |
|
| 473 |
void verifyHeapAccessDisassembly(uint32_t begin, uint32_t end, |
| 474 |
const Disassembler::HeapAccess& heapAccess) { |
| 475 |
MOZ_CRASH(); |
| 476 |
} |
| 477 |
|
| 478 |
void setUnlimitedBuffer() { m_buffer.setUnlimited(); } |
| 479 |
|
| 480 |
GeneralRegisterSet* GetScratchRegisterList() { |
| 481 |
return &scratch_register_list_; |
| 482 |
} |
| 483 |
|
| 484 |
void EmitConstPoolWithJumpIfNeeded(size_t margin = 0) {} |
| 485 |
|
| 486 |
// As opposed to x86/x64 version, the data relocation has to be executed |
| 487 |
// before to recover the pointer, and not after. |
| 488 |
void writeDataRelocation(ImmGCPtr ptr) { |
| 489 |
// Raw GC pointer relocations and Value relocations both end up in |
| 490 |
// TraceOneDataRelocation. |
| 491 |
if (ptr.value) { |
| 492 |
if (gc::IsInsideNursery(ptr.value)) { |
| 493 |
embedsNurseryPointers_ = true; |
| 494 |
} |
| 495 |
dataRelocations_.writeUnsigned(nextOffset().getOffset()); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
bool appendRawCode(const uint8_t* code, size_t numBytes); |
| 500 |
|
| 501 |
void assertNoGCThings() const { |
| 502 |
#ifdef DEBUG |
| 503 |
MOZ_ASSERT(dataRelocations_.length() == 0); |
| 504 |
for (auto& j : jumps_) { |
| 505 |
MOZ_ASSERT(j.kind == RelocationKind::HARDCODED); |
| 506 |
} |
| 507 |
#endif |
| 508 |
} |
| 509 |
|
| 510 |
// Assembler Pseudo Instructions (Tables 25.2, 25.3, RISC-V Unprivileged ISA) |
| 511 |
void break_(uint32_t code, bool break_as_stop = false); |
| 512 |
void nop(); |
| 513 |
void RV_li(Register rd, int64_t imm); |
| 514 |
static int RV_li_count(int64_t imm, bool is_get_temp_reg = false); |
| 515 |
void GeneralLi(Register rd, int64_t imm); |
| 516 |
static int GeneralLiCount(int64_t imm, bool is_get_temp_reg = false); |
| 517 |
void RecursiveLiImpl(Register rd, int64_t imm); |
| 518 |
void RecursiveLi(Register rd, int64_t imm); |
| 519 |
static int RecursiveLiCount(int64_t imm); |
| 520 |
static int RecursiveLiImplCount(int64_t imm); |
| 521 |
// Returns the number of instructions required to load the immediate |
| 522 |
static int li_estimate(int64_t imm, bool is_get_temp_reg = false); |
| 523 |
// Loads an immediate, always using 8 instructions, regardless of the value, |
| 524 |
// so that it can be modified later. |
| 525 |
void li_constant(Register rd, int64_t imm); |
| 526 |
void li_ptr(Register rd, int64_t imm); |
| 527 |
}; |
| 528 |
|
| 529 |
class ABIArgGenerator { |
| 530 |
public: |
| 531 |
ABIArgGenerator() |
| 532 |
: intRegIndex_(0), floatRegIndex_(0), stackOffset_(0), current_() {} |
| 533 |
ABIArg next(MIRType); |
| 534 |
ABIArg& current() { return current_; } |
| 535 |
uint32_t stackBytesConsumedSoFar() const { return stackOffset_; } |
| 536 |
void increaseStackOffset(uint32_t bytes) { stackOffset_ += bytes; } |
| 537 |
|
| 538 |
protected: |
| 539 |
unsigned intRegIndex_; |
| 540 |
unsigned floatRegIndex_; |
| 541 |
uint32_t stackOffset_; |
| 542 |
ABIArg current_; |
| 543 |
}; |
| 544 |
|
| 545 |
// Note that nested uses of these are allowed, but the inner calls must imply |
| 546 |
// an area of code which exists only inside the area of code implied by the |
| 547 |
// outermost call. Otherwise AssemblerBufferWithConstantPools::enterNoPool |
| 548 |
// will assert. |
| 549 |
class BlockTrampolinePoolScope { |
| 550 |
public: |
| 551 |
explicit BlockTrampolinePoolScope(Assembler* assem, int margin) |
| 552 |
: assem_(assem) { |
| 553 |
assem_->enterNoPool(margin); |
| 554 |
} |
| 555 |
~BlockTrampolinePoolScope() { assem_->leaveNoPool(); } |
| 556 |
|
| 557 |
private: |
| 558 |
Assembler* assem_; |
| 559 |
BlockTrampolinePoolScope() = delete; |
| 560 |
BlockTrampolinePoolScope(const BlockTrampolinePoolScope&) = delete; |
| 561 |
BlockTrampolinePoolScope& operator=(const BlockTrampolinePoolScope&) = delete; |
| 562 |
}; |
| 563 |
|
| 564 |
class UseScratchRegisterScope { |
| 565 |
public: |
| 566 |
explicit UseScratchRegisterScope(Assembler* assembler); |
| 567 |
~UseScratchRegisterScope(); |
| 568 |
|
| 569 |
Register Acquire(); |
| 570 |
bool hasAvailable() const; |
| 571 |
void Include(const GeneralRegisterSet& list) { |
| 572 |
*available_ = GeneralRegisterSet::Intersect(*available_, list); |
| 573 |
} |
| 574 |
void Exclude(const GeneralRegisterSet& list) { |
| 575 |
*available_ = GeneralRegisterSet::Subtract(*available_, list); |
| 576 |
} |
| 577 |
|
| 578 |
private: |
| 579 |
GeneralRegisterSet* available_; |
| 580 |
GeneralRegisterSet old_available_; |
| 581 |
}; |
| 582 |
|
| 583 |
// Class Operand represents a shifter operand in data processing instructions. |
| 584 |
class Operand { |
| 585 |
public: |
| 586 |
enum Tag { REG, FREG, MEM, IMM }; |
| 587 |
Operand(FloatRegister freg) : tag(FREG), rm_(freg.code()) {} |
| 588 |
|
| 589 |
explicit Operand(Register base, Imm32 off) |
| 590 |
: tag(MEM), rm_(base.code()), offset_(off.value) {} |
| 591 |
|
| 592 |
explicit Operand(Register base, int32_t off) |
| 593 |
: tag(MEM), rm_(base.code()), offset_(off) {} |
| 594 |
|
| 595 |
explicit Operand(const Address& addr) |
| 596 |
: tag(MEM), rm_(addr.base.code()), offset_(addr.offset) {} |
| 597 |
|
| 598 |
explicit Operand(int64_t immediate) : tag(IMM), rm_() { value_ = immediate; } |
| 599 |
// Register. |
| 600 |
Operand(const Register rm) : tag(REG), rm_(rm.code()) {} |
| 601 |
// Return true if this is a register operand. |
| 602 |
bool is_reg() const { return tag == REG; } |
| 603 |
bool is_freg() const { return tag == FREG; } |
| 604 |
bool is_mem() const { return tag == MEM; } |
| 605 |
bool is_imm() const { return tag == IMM; } |
| 606 |
inline int64_t immediate() const { |
| 607 |
MOZ_ASSERT(is_imm()); |
| 608 |
return value_; |
| 609 |
} |
| 610 |
bool IsImmediate() const { return !is_reg(); } |
| 611 |
Register rm() const { return Register::FromCode(rm_); } |
| 612 |
int32_t offset() const { |
| 613 |
MOZ_ASSERT(is_mem()); |
| 614 |
return offset_; |
| 615 |
} |
| 616 |
|
| 617 |
FloatRegister toFReg() const { |
| 618 |
MOZ_ASSERT(tag == FREG); |
| 619 |
return FloatRegister::FromCode(rm_); |
| 620 |
} |
| 621 |
|
| 622 |
Register toReg() const { |
| 623 |
MOZ_ASSERT(tag == REG); |
| 624 |
return Register::FromCode(rm_); |
| 625 |
} |
| 626 |
|
| 627 |
Address toAddress() const { |
| 628 |
MOZ_ASSERT(tag == MEM); |
| 629 |
return Address(Register::FromCode(rm_), offset()); |
| 630 |
} |
| 631 |
|
| 632 |
private: |
| 633 |
Tag tag; |
| 634 |
uint32_t rm_; |
| 635 |
int32_t offset_; |
| 636 |
int64_t value_; // valid if rm_ == no_reg |
| 637 |
|
| 638 |
friend class Assembler; |
| 639 |
friend class MacroAssembler; |
| 640 |
}; |
| 641 |
|
| 642 |
static const uint32_t NumIntArgRegs = 8; |
| 643 |
static const uint32_t NumFloatArgRegs = 8; |
| 644 |
static inline bool GetIntArgReg(uint32_t usedIntArgs, Register* out) { |
| 645 |
if (usedIntArgs < NumIntArgRegs) { |
| 646 |
*out = Register::FromCode(a0.code() + usedIntArgs); |
| 647 |
return true; |
| 648 |
} |
| 649 |
return false; |
| 650 |
} |
| 651 |
|
| 652 |
static inline bool GetFloatArgReg(uint32_t usedFloatArgs, FloatRegister* out) { |
| 653 |
if (usedFloatArgs < NumFloatArgRegs) { |
| 654 |
*out = FloatRegister::FromCode(fa0.code() + usedFloatArgs); |
| 655 |
return true; |
| 656 |
} |
| 657 |
return false; |
| 658 |
} |
| 659 |
|
| 660 |
// Get a register in which we plan to put a quantity that will be used as an |
| 661 |
// integer argument. This differs from GetIntArgReg in that if we have no more |
| 662 |
// actual argument registers to use we will fall back on using whatever |
| 663 |
// CallTempReg* don't overlap the argument registers, and only fail once those |
| 664 |
// run out too. |
| 665 |
static inline bool GetTempRegForIntArg(uint32_t usedIntArgs, |
| 666 |
uint32_t usedFloatArgs, Register* out) { |
| 667 |
// NOTE: We can't properly determine which regs are used if there are |
| 668 |
// float arguments. If this is needed, we will have to guess. |
| 669 |
MOZ_ASSERT(usedFloatArgs == 0); |
| 670 |
|
| 671 |
if (GetIntArgReg(usedIntArgs, out)) { |
| 672 |
return true; |
| 673 |
} |
| 674 |
// Unfortunately, we have to assume things about the point at which |
| 675 |
// GetIntArgReg returns false, because we need to know how many registers it |
| 676 |
// can allocate. |
| 677 |
usedIntArgs -= NumIntArgRegs; |
| 678 |
if (usedIntArgs >= NumCallTempNonArgRegs) { |
| 679 |
return false; |
| 680 |
} |
| 681 |
*out = CallTempNonArgRegs[usedIntArgs]; |
| 682 |
return true; |
| 683 |
} |
| 684 |
|
| 685 |
} // namespace jit |
| 686 |
} // namespace js |
| 687 |
#endif /* jit_riscv64_Assembler_riscv64_h */ |