| 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 |
#include "jit/mips64/Assembler-mips64.h" |
| 8 |
|
| 9 |
#include "mozilla/DebugOnly.h" |
| 10 |
#include "mozilla/Maybe.h" |
| 11 |
|
| 12 |
#include "jit/AutoWritableJitCode.h" |
| 13 |
|
| 14 |
using mozilla::DebugOnly; |
| 15 |
|
| 16 |
using namespace js; |
| 17 |
using namespace js::jit; |
| 18 |
|
| 19 |
ABIArgGenerator::ABIArgGenerator() |
| 20 |
: regIndex_(0), stackOffset_(0), current_() {} |
| 21 |
|
| 22 |
ABIArg ABIArgGenerator::next(MIRType type) { |
| 23 |
static_assert(NumIntArgRegs == NumFloatArgRegs); |
| 24 |
if (regIndex_ == NumIntArgRegs) { |
| 25 |
if (type != MIRType::Simd128) { |
| 26 |
current_ = ABIArg(stackOffset_); |
| 27 |
stackOffset_ += sizeof(uint64_t); |
| 28 |
} else { |
| 29 |
// Mips platform does not support simd yet. |
| 30 |
MOZ_CRASH("Unexpected argument type"); |
| 31 |
} |
| 32 |
return current_; |
| 33 |
} |
| 34 |
switch (type) { |
| 35 |
case MIRType::Int32: |
| 36 |
case MIRType::Int64: |
| 37 |
case MIRType::Pointer: |
| 38 |
case MIRType::WasmAnyRef: |
| 39 |
case MIRType::WasmArrayData: |
| 40 |
case MIRType::StackResults: { |
| 41 |
Register destReg; |
| 42 |
GetIntArgReg(regIndex_++, &destReg); |
| 43 |
current_ = ABIArg(destReg); |
| 44 |
break; |
| 45 |
} |
| 46 |
case MIRType::Float32: |
| 47 |
case MIRType::Double: { |
| 48 |
FloatRegister::ContentType contentType; |
| 49 |
contentType = (type == MIRType::Double) ? FloatRegisters::Double |
| 50 |
: FloatRegisters::Single; |
| 51 |
FloatRegister destFReg; |
| 52 |
GetFloatArgReg(regIndex_++, &destFReg); |
| 53 |
current_ = ABIArg(FloatRegister(destFReg.id(), contentType)); |
| 54 |
break; |
| 55 |
} |
| 56 |
default: |
| 57 |
MOZ_CRASH("Unexpected argument type"); |
| 58 |
} |
| 59 |
return current_; |
| 60 |
} |
| 61 |
|
| 62 |
uint32_t js::jit::RT(FloatRegister r) { |
| 63 |
MOZ_ASSERT(r.id() < FloatRegisters::TotalPhys); |
| 64 |
return r.id() << RTShift; |
| 65 |
} |
| 66 |
|
| 67 |
uint32_t js::jit::RD(FloatRegister r) { |
| 68 |
MOZ_ASSERT(r.id() < FloatRegisters::TotalPhys); |
| 69 |
return r.id() << RDShift; |
| 70 |
} |
| 71 |
|
| 72 |
uint32_t js::jit::RZ(FloatRegister r) { |
| 73 |
MOZ_ASSERT(r.id() < FloatRegisters::TotalPhys); |
| 74 |
return r.id() << RZShift; |
| 75 |
} |
| 76 |
|
| 77 |
uint32_t js::jit::SA(FloatRegister r) { |
| 78 |
MOZ_ASSERT(r.id() < FloatRegisters::TotalPhys); |
| 79 |
return r.id() << SAShift; |
| 80 |
} |
| 81 |
|
| 82 |
void Assembler::executableCopy(uint8_t* buffer) { |
| 83 |
MOZ_ASSERT(isFinished); |
| 84 |
m_buffer.executableCopy(buffer); |
| 85 |
} |
| 86 |
|
| 87 |
uintptr_t Assembler::GetPointer(uint8_t* instPtr) { |
| 88 |
Instruction* inst = (Instruction*)instPtr; |
| 89 |
return Assembler::ExtractLoad64Value(inst); |
| 90 |
} |
| 91 |
|
| 92 |
static JitCode* CodeFromJump(Instruction* jump) { |
| 93 |
uint8_t* target = (uint8_t*)Assembler::ExtractLoad64Value(jump); |
| 94 |
return JitCode::FromExecutable(target); |
| 95 |
} |
| 96 |
|
| 97 |
void Assembler::TraceJumpRelocations(JSTracer* trc, JitCode* code, |
| 98 |
CompactBufferReader& reader) { |
| 99 |
while (reader.more()) { |
| 100 |
JitCode* child = |
| 101 |
CodeFromJump((Instruction*)(code->raw() + reader.readUnsigned())); |
| 102 |
TraceManuallyBarrieredEdge(trc, &child, "rel32"); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static void TraceOneDataRelocation(JSTracer* trc, |
| 107 |
mozilla::Maybe<AutoWritableJitCode>& awjc, |
| 108 |
JitCode* code, Instruction* inst) { |
| 109 |
void* ptr = (void*)Assembler::ExtractLoad64Value(inst); |
| 110 |
void* prior = ptr; |
| 111 |
|
| 112 |
// Data relocations can be for Values or for raw pointers. If a Value is |
| 113 |
// zero-tagged, we can trace it as if it were a raw pointer. If a Value |
| 114 |
// is not zero-tagged, we have to interpret it as a Value to ensure that the |
| 115 |
// tag bits are masked off to recover the actual pointer. |
| 116 |
uintptr_t word = reinterpret_cast<uintptr_t>(ptr); |
| 117 |
if (word >> JSVAL_TAG_SHIFT) { |
| 118 |
// This relocation is a Value with a non-zero tag. |
| 119 |
Value v = Value::fromRawBits(word); |
| 120 |
TraceManuallyBarrieredEdge(trc, &v, "jit-masm-value"); |
| 121 |
ptr = (void*)v.bitsAsPunboxPointer(); |
| 122 |
} else { |
| 123 |
// This relocation is a raw pointer or a Value with a zero tag. |
| 124 |
// No barrier needed since these are constants. |
| 125 |
TraceManuallyBarrieredGenericPointerEdge( |
| 126 |
trc, reinterpret_cast<gc::Cell**>(&ptr), "jit-masm-ptr"); |
| 127 |
} |
| 128 |
|
| 129 |
if (ptr != prior) { |
| 130 |
if (awjc.isNothing()) { |
| 131 |
awjc.emplace(code); |
| 132 |
} |
| 133 |
Assembler::UpdateLoad64Value(inst, uint64_t(ptr)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/* static */ |
| 138 |
void Assembler::TraceDataRelocations(JSTracer* trc, JitCode* code, |
| 139 |
CompactBufferReader& reader) { |
| 140 |
mozilla::Maybe<AutoWritableJitCode> awjc; |
| 141 |
while (reader.more()) { |
| 142 |
size_t offset = reader.readUnsigned(); |
| 143 |
Instruction* inst = (Instruction*)(code->raw() + offset); |
| 144 |
TraceOneDataRelocation(trc, awjc, code, inst); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void Assembler::Bind(uint8_t* rawCode, const CodeLabel& label) { |
| 149 |
if (label.patchAt().bound()) { |
| 150 |
auto mode = label.linkMode(); |
| 151 |
intptr_t offset = label.patchAt().offset(); |
| 152 |
intptr_t target = label.target().offset(); |
| 153 |
|
| 154 |
if (mode == CodeLabel::RawPointer) { |
| 155 |
*reinterpret_cast<const void**>(rawCode + offset) = rawCode + target; |
| 156 |
} else { |
| 157 |
MOZ_ASSERT(mode == CodeLabel::MoveImmediate || |
| 158 |
mode == CodeLabel::JumpImmediate); |
| 159 |
Instruction* inst = (Instruction*)(rawCode + offset); |
| 160 |
Assembler::UpdateLoad64Value(inst, (uint64_t)(rawCode + target)); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
void Assembler::bind(InstImm* inst, uintptr_t branch, uintptr_t target) { |
| 166 |
int64_t offset = target - branch; |
| 167 |
InstImm inst_bgezal = InstImm(op_regimm, zero, rt_bgezal, BOffImm16(0)); |
| 168 |
InstImm inst_beq = InstImm(op_beq, zero, zero, BOffImm16(0)); |
| 169 |
|
| 170 |
// If encoded offset is 4, then the jump must be short |
| 171 |
if (BOffImm16(inst[0]).decode() == 4) { |
| 172 |
MOZ_ASSERT(BOffImm16::IsInRange(offset)); |
| 173 |
inst[0].setBOffImm16(BOffImm16(offset)); |
| 174 |
inst[1].makeNop(); |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
// Generate the long jump for calls because return address has to be the |
| 179 |
// address after the reserved block. |
| 180 |
if (inst[0].encode() == inst_bgezal.encode()) { |
| 181 |
addLongJump(BufferOffset(branch), BufferOffset(target)); |
| 182 |
Assembler::WriteLoad64Instructions(inst, ScratchRegister, |
| 183 |
LabelBase::INVALID_OFFSET); |
| 184 |
inst[4] = InstReg(op_special, ScratchRegister, zero, ra, ff_jalr).encode(); |
| 185 |
// There is 1 nop after this. |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
if (BOffImm16::IsInRange(offset)) { |
| 190 |
// Don't skip trailing nops can improve performance |
| 191 |
// on Loongson3 platform. |
| 192 |
bool skipNops = |
| 193 |
!isLoongson() && (inst[0].encode() != inst_bgezal.encode() && |
| 194 |
inst[0].encode() != inst_beq.encode()); |
| 195 |
|
| 196 |
inst[0].setBOffImm16(BOffImm16(offset)); |
| 197 |
inst[1].makeNop(); |
| 198 |
|
| 199 |
if (skipNops) { |
| 200 |
inst[2] = |
| 201 |
InstImm(op_regimm, zero, rt_bgez, BOffImm16(5 * sizeof(uint32_t))) |
| 202 |
.encode(); |
| 203 |
// There are 4 nops after this |
| 204 |
} |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
if (inst[0].encode() == inst_beq.encode()) { |
| 209 |
// Handle long unconditional jump. |
| 210 |
addLongJump(BufferOffset(branch), BufferOffset(target)); |
| 211 |
Assembler::WriteLoad64Instructions(inst, ScratchRegister, |
| 212 |
LabelBase::INVALID_OFFSET); |
| 213 |
#ifdef MIPSR6 |
| 214 |
inst[4] = |
| 215 |
InstReg(op_special, ScratchRegister, zero, zero, ff_jalr).encode(); |
| 216 |
#else |
| 217 |
inst[4] = InstReg(op_special, ScratchRegister, zero, zero, ff_jr).encode(); |
| 218 |
#endif |
| 219 |
// There is 1 nop after this. |
| 220 |
} else { |
| 221 |
// Handle long conditional jump. |
| 222 |
inst[0] = invertBranch(inst[0], BOffImm16(7 * sizeof(uint32_t))); |
| 223 |
// No need for a "nop" here because we can clobber scratch. |
| 224 |
addLongJump(BufferOffset(branch + sizeof(uint32_t)), BufferOffset(target)); |
| 225 |
Assembler::WriteLoad64Instructions(&inst[1], ScratchRegister, |
| 226 |
LabelBase::INVALID_OFFSET); |
| 227 |
#ifdef MIPSR6 |
| 228 |
inst[5] = |
| 229 |
InstReg(op_special, ScratchRegister, zero, zero, ff_jalr).encode(); |
| 230 |
#else |
| 231 |
inst[5] = InstReg(op_special, ScratchRegister, zero, zero, ff_jr).encode(); |
| 232 |
#endif |
| 233 |
// There is 1 nop after this. |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void Assembler::processCodeLabels(uint8_t* rawCode) { |
| 238 |
for (const CodeLabel& label : codeLabels_) { |
| 239 |
Bind(rawCode, label); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
uint32_t Assembler::PatchWrite_NearCallSize() { |
| 244 |
// Load an address needs 4 instructions, and a jump with a delay slot. |
| 245 |
return (4 + 2) * sizeof(uint32_t); |
| 246 |
} |
| 247 |
|
| 248 |
void Assembler::PatchWrite_NearCall(CodeLocationLabel start, |
| 249 |
CodeLocationLabel toCall) { |
| 250 |
Instruction* inst = (Instruction*)start.raw(); |
| 251 |
uint8_t* dest = toCall.raw(); |
| 252 |
|
| 253 |
// Overwrite whatever instruction used to be here with a call. |
| 254 |
// Always use long jump for two reasons: |
| 255 |
// - Jump has to be the same size because of PatchWrite_NearCallSize. |
| 256 |
// - Return address has to be at the end of replaced block. |
| 257 |
// Short jump wouldn't be more efficient. |
| 258 |
Assembler::WriteLoad64Instructions(inst, ScratchRegister, (uint64_t)dest); |
| 259 |
inst[4] = InstReg(op_special, ScratchRegister, zero, ra, ff_jalr); |
| 260 |
inst[5] = InstNOP(); |
| 261 |
} |
| 262 |
|
| 263 |
uint64_t Assembler::ExtractLoad64Value(Instruction* inst0) { |
| 264 |
InstImm* i0 = (InstImm*)inst0; |
| 265 |
InstImm* i1 = (InstImm*)i0->next(); |
| 266 |
InstReg* i2 = (InstReg*)i1->next(); |
| 267 |
InstImm* i3 = (InstImm*)i2->next(); |
| 268 |
InstImm* i5 = (InstImm*)i3->next()->next(); |
| 269 |
|
| 270 |
MOZ_ASSERT(i0->extractOpcode() == ((uint32_t)op_lui >> OpcodeShift)); |
| 271 |
MOZ_ASSERT(i1->extractOpcode() == ((uint32_t)op_ori >> OpcodeShift)); |
| 272 |
MOZ_ASSERT(i3->extractOpcode() == ((uint32_t)op_ori >> OpcodeShift)); |
| 273 |
|
| 274 |
if ((i2->extractOpcode() == ((uint32_t)op_special >> OpcodeShift)) && |
| 275 |
(i2->extractFunctionField() == ff_dsrl32)) { |
| 276 |
uint64_t value = (uint64_t(i0->extractImm16Value()) << 32) | |
| 277 |
(uint64_t(i1->extractImm16Value()) << 16) | |
| 278 |
uint64_t(i3->extractImm16Value()); |
| 279 |
return uint64_t((int64_t(value) << 16) >> 16); |
| 280 |
} |
| 281 |
|
| 282 |
MOZ_ASSERT(i5->extractOpcode() == ((uint32_t)op_ori >> OpcodeShift)); |
| 283 |
uint64_t value = (uint64_t(i0->extractImm16Value()) << 48) | |
| 284 |
(uint64_t(i1->extractImm16Value()) << 32) | |
| 285 |
(uint64_t(i3->extractImm16Value()) << 16) | |
| 286 |
uint64_t(i5->extractImm16Value()); |
| 287 |
return value; |
| 288 |
} |
| 289 |
|
| 290 |
void Assembler::UpdateLoad64Value(Instruction* inst0, uint64_t value) { |
| 291 |
InstImm* i0 = (InstImm*)inst0; |
| 292 |
InstImm* i1 = (InstImm*)i0->next(); |
| 293 |
InstReg* i2 = (InstReg*)i1->next(); |
| 294 |
InstImm* i3 = (InstImm*)i2->next(); |
| 295 |
InstImm* i5 = (InstImm*)i3->next()->next(); |
| 296 |
|
| 297 |
MOZ_ASSERT(i0->extractOpcode() == ((uint32_t)op_lui >> OpcodeShift)); |
| 298 |
MOZ_ASSERT(i1->extractOpcode() == ((uint32_t)op_ori >> OpcodeShift)); |
| 299 |
MOZ_ASSERT(i3->extractOpcode() == ((uint32_t)op_ori >> OpcodeShift)); |
| 300 |
|
| 301 |
if ((i2->extractOpcode() == ((uint32_t)op_special >> OpcodeShift)) && |
| 302 |
(i2->extractFunctionField() == ff_dsrl32)) { |
| 303 |
i0->setImm16(Imm16::Lower(Imm32(value >> 32))); |
| 304 |
i1->setImm16(Imm16::Upper(Imm32(value))); |
| 305 |
i3->setImm16(Imm16::Lower(Imm32(value))); |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
MOZ_ASSERT(i5->extractOpcode() == ((uint32_t)op_ori >> OpcodeShift)); |
| 310 |
|
| 311 |
i0->setImm16(Imm16::Upper(Imm32(value >> 32))); |
| 312 |
i1->setImm16(Imm16::Lower(Imm32(value >> 32))); |
| 313 |
i3->setImm16(Imm16::Upper(Imm32(value))); |
| 314 |
i5->setImm16(Imm16::Lower(Imm32(value))); |
| 315 |
} |
| 316 |
|
| 317 |
void Assembler::WriteLoad64Instructions(Instruction* inst0, Register reg, |
| 318 |
uint64_t value) { |
| 319 |
Instruction* inst1 = inst0->next(); |
| 320 |
Instruction* inst2 = inst1->next(); |
| 321 |
Instruction* inst3 = inst2->next(); |
| 322 |
|
| 323 |
*inst0 = InstImm(op_lui, zero, reg, Imm16::Lower(Imm32(value >> 32))); |
| 324 |
*inst1 = InstImm(op_ori, reg, reg, Imm16::Upper(Imm32(value))); |
| 325 |
*inst2 = InstReg(op_special, rs_one, reg, reg, 48 - 32, ff_dsrl32); |
| 326 |
*inst3 = InstImm(op_ori, reg, reg, Imm16::Lower(Imm32(value))); |
| 327 |
} |
| 328 |
|
| 329 |
void Assembler::PatchDataWithValueCheck(CodeLocationLabel label, |
| 330 |
ImmPtr newValue, ImmPtr expectedValue) { |
| 331 |
PatchDataWithValueCheck(label, PatchedImmPtr(newValue.value), |
| 332 |
PatchedImmPtr(expectedValue.value)); |
| 333 |
} |
| 334 |
|
| 335 |
void Assembler::PatchDataWithValueCheck(CodeLocationLabel label, |
| 336 |
PatchedImmPtr newValue, |
| 337 |
PatchedImmPtr expectedValue) { |
| 338 |
Instruction* inst = (Instruction*)label.raw(); |
| 339 |
|
| 340 |
// Extract old Value |
| 341 |
DebugOnly<uint64_t> value = Assembler::ExtractLoad64Value(inst); |
| 342 |
MOZ_ASSERT(value == uint64_t(expectedValue.value)); |
| 343 |
|
| 344 |
// Replace with new value |
| 345 |
Assembler::UpdateLoad64Value(inst, uint64_t(newValue.value)); |
| 346 |
} |
| 347 |
|
| 348 |
uint64_t Assembler::ExtractInstructionImmediate(uint8_t* code) { |
| 349 |
InstImm* inst = (InstImm*)code; |
| 350 |
return Assembler::ExtractLoad64Value(inst); |
| 351 |
} |
| 352 |
|
| 353 |
void Assembler::ToggleCall(CodeLocationLabel inst_, bool enabled) { |
| 354 |
Instruction* inst = (Instruction*)inst_.raw(); |
| 355 |
InstImm* i0 = (InstImm*)inst; |
| 356 |
InstImm* i1 = (InstImm*)i0->next(); |
| 357 |
InstImm* i3 = (InstImm*)i1->next()->next(); |
| 358 |
Instruction* i4 = (Instruction*)i3->next(); |
| 359 |
|
| 360 |
MOZ_ASSERT(i0->extractOpcode() == ((uint32_t)op_lui >> OpcodeShift)); |
| 361 |
MOZ_ASSERT(i1->extractOpcode() == ((uint32_t)op_ori >> OpcodeShift)); |
| 362 |
MOZ_ASSERT(i3->extractOpcode() == ((uint32_t)op_ori >> OpcodeShift)); |
| 363 |
|
| 364 |
if (enabled) { |
| 365 |
MOZ_ASSERT(i4->extractOpcode() != ((uint32_t)op_lui >> OpcodeShift)); |
| 366 |
InstReg jalr = InstReg(op_special, ScratchRegister, zero, ra, ff_jalr); |
| 367 |
*i4 = jalr; |
| 368 |
} else { |
| 369 |
InstNOP nop; |
| 370 |
*i4 = nop; |
| 371 |
} |
| 372 |
} |