| 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/Bailouts.h" |
| 8 |
#include "jit/BaselineFrame.h" |
| 9 |
#include "jit/CalleeToken.h" |
| 10 |
#include "jit/JitFrames.h" |
| 11 |
#include "jit/JitRuntime.h" |
| 12 |
#ifdef JS_ION_PERF |
| 13 |
# include "jit/PerfSpewer.h" |
| 14 |
#endif |
| 15 |
#include "jit/riscv64/SharedICRegisters-riscv64.h" |
| 16 |
#include "jit/VMFunctions.h" |
| 17 |
#include "vm/JitActivation.h" // js::jit::JitActivation |
| 18 |
#include "vm/JSContext.h" |
| 19 |
|
| 20 |
#include "jit/MacroAssembler-inl.h" |
| 21 |
|
| 22 |
using namespace js; |
| 23 |
using namespace js::jit; |
| 24 |
|
| 25 |
// This file includes stubs for generating the JIT trampolines when there is no |
| 26 |
// JIT backend, and also includes implementations for assorted random things |
| 27 |
// which can't be implemented in headers. |
| 28 |
|
| 29 |
// All registers to save and restore. This includes the stack pointer, since we |
| 30 |
// use the ability to reference register values on the stack by index. |
| 31 |
static const LiveRegisterSet AllRegs = |
| 32 |
LiveRegisterSet(GeneralRegisterSet(Registers::AllMask), |
| 33 |
FloatRegisterSet(FloatRegisters::AllMask)); |
| 34 |
|
| 35 |
static void PushBailoutFrame(MacroAssembler& masm, Register spArg) { |
| 36 |
// Push the frameSize_ stored in ra |
| 37 |
// See: CodeGeneratorRiscv64::generateOutOfLineCode() |
| 38 |
masm.push(ra); |
| 39 |
|
| 40 |
// Push registers such that we can access them from [base + code]. |
| 41 |
masm.PushRegsInMask(AllRegs); |
| 42 |
|
| 43 |
// Put pointer to BailoutStack as first argument to the Bailout() |
| 44 |
masm.movePtr(StackPointer, spArg); |
| 45 |
} |
| 46 |
|
| 47 |
struct EnterJITRegs { |
| 48 |
double fs11; |
| 49 |
double fs10; |
| 50 |
double fs9; |
| 51 |
double fs8; |
| 52 |
double fs7; |
| 53 |
double fs6; |
| 54 |
double fs5; |
| 55 |
double fs4; |
| 56 |
double fs3; |
| 57 |
double fs2; |
| 58 |
double fs1; |
| 59 |
double fs0; |
| 60 |
|
| 61 |
// uintptr_t align; |
| 62 |
|
| 63 |
// non-volatile registers. |
| 64 |
uint64_t ra; |
| 65 |
uint64_t sp; |
| 66 |
uint64_t fp; |
| 67 |
uint64_t gp; |
| 68 |
uint64_t s11; |
| 69 |
uint64_t s10; |
| 70 |
uint64_t s9; |
| 71 |
uint64_t s8; |
| 72 |
uint64_t s7; |
| 73 |
uint64_t s6; |
| 74 |
uint64_t s5; |
| 75 |
uint64_t s4; |
| 76 |
uint64_t s3; |
| 77 |
uint64_t s2; |
| 78 |
uint64_t s1; |
| 79 |
// Save reg_vp(a7) on stack, use it after call jit code. |
| 80 |
uint64_t a7; |
| 81 |
}; |
| 82 |
|
| 83 |
static void GenerateReturn(MacroAssembler& masm, int returnCode) { |
| 84 |
MOZ_ASSERT(masm.framePushed() == sizeof(EnterJITRegs)); |
| 85 |
|
| 86 |
// Restore non-volatile registers |
| 87 |
masm.ld(s1, StackPointer, offsetof(EnterJITRegs, s1)); |
| 88 |
masm.ld(s2, StackPointer, offsetof(EnterJITRegs, s2)); |
| 89 |
masm.ld(s3, StackPointer, offsetof(EnterJITRegs, s3)); |
| 90 |
masm.ld(s4, StackPointer, offsetof(EnterJITRegs, s4)); |
| 91 |
masm.ld(s5, StackPointer, offsetof(EnterJITRegs, s5)); |
| 92 |
masm.ld(s6, StackPointer, offsetof(EnterJITRegs, s6)); |
| 93 |
masm.ld(s7, StackPointer, offsetof(EnterJITRegs, s7)); |
| 94 |
masm.ld(s8, StackPointer, offsetof(EnterJITRegs, s8)); |
| 95 |
masm.ld(s9, StackPointer, offsetof(EnterJITRegs, s9)); |
| 96 |
masm.ld(s10, StackPointer, offsetof(EnterJITRegs, s10)); |
| 97 |
masm.ld(s11, StackPointer, offsetof(EnterJITRegs, s11)); |
| 98 |
masm.ld(gp, StackPointer, offsetof(EnterJITRegs, gp)); |
| 99 |
masm.ld(fp, StackPointer, offsetof(EnterJITRegs, fp)); |
| 100 |
masm.ld(sp, StackPointer, offsetof(EnterJITRegs, sp)); |
| 101 |
masm.ld(ra, StackPointer, offsetof(EnterJITRegs, ra)); |
| 102 |
|
| 103 |
// Restore non-volatile floating point registers |
| 104 |
masm.fld(fs11, StackPointer, offsetof(EnterJITRegs, fs11)); |
| 105 |
masm.fld(fs10, StackPointer, offsetof(EnterJITRegs, fs10)); |
| 106 |
masm.fld(fs9, StackPointer, offsetof(EnterJITRegs, fs9)); |
| 107 |
masm.fld(fs8, StackPointer, offsetof(EnterJITRegs, fs8)); |
| 108 |
masm.fld(fs7, StackPointer, offsetof(EnterJITRegs, fs7)); |
| 109 |
masm.fld(fs6, StackPointer, offsetof(EnterJITRegs, fs6)); |
| 110 |
masm.fld(fs5, StackPointer, offsetof(EnterJITRegs, fs5)); |
| 111 |
masm.fld(fs4, StackPointer, offsetof(EnterJITRegs, fs4)); |
| 112 |
masm.fld(fs3, StackPointer, offsetof(EnterJITRegs, fs3)); |
| 113 |
masm.fld(fs2, StackPointer, offsetof(EnterJITRegs, fs2)); |
| 114 |
masm.fld(fs1, StackPointer, offsetof(EnterJITRegs, fs1)); |
| 115 |
masm.fld(fs0, StackPointer, offsetof(EnterJITRegs, fs0)); |
| 116 |
|
| 117 |
masm.freeStack(sizeof(EnterJITRegs)); |
| 118 |
|
| 119 |
masm.branch(ra); |
| 120 |
} |
| 121 |
|
| 122 |
static void GeneratePrologue(MacroAssembler& masm) { |
| 123 |
masm.reserveStack(sizeof(EnterJITRegs)); |
| 124 |
|
| 125 |
masm.sd(s1, StackPointer, offsetof(EnterJITRegs, s1)); |
| 126 |
masm.sd(s2, StackPointer, offsetof(EnterJITRegs, s2)); |
| 127 |
masm.sd(s3, StackPointer, offsetof(EnterJITRegs, s3)); |
| 128 |
masm.sd(s4, StackPointer, offsetof(EnterJITRegs, s4)); |
| 129 |
masm.sd(s5, StackPointer, offsetof(EnterJITRegs, s5)); |
| 130 |
masm.sd(s6, StackPointer, offsetof(EnterJITRegs, s6)); |
| 131 |
masm.sd(s7, StackPointer, offsetof(EnterJITRegs, s7)); |
| 132 |
masm.sd(s8, StackPointer, offsetof(EnterJITRegs, s8)); |
| 133 |
masm.sd(s9, StackPointer, offsetof(EnterJITRegs, s9)); |
| 134 |
masm.sd(s10, StackPointer, offsetof(EnterJITRegs, s10)); |
| 135 |
masm.sd(s11, StackPointer, offsetof(EnterJITRegs, s11)); |
| 136 |
masm.sd(gp, StackPointer, offsetof(EnterJITRegs, gp)); |
| 137 |
masm.sd(fp, StackPointer, offsetof(EnterJITRegs, fp)); |
| 138 |
masm.sd(sp, StackPointer, offsetof(EnterJITRegs, sp)); |
| 139 |
masm.sd(ra, StackPointer, offsetof(EnterJITRegs, ra)); |
| 140 |
masm.sd(a7, StackPointer, offsetof(EnterJITRegs, a7)); |
| 141 |
|
| 142 |
masm.fsd(fs11, StackPointer, offsetof(EnterJITRegs, fs11)); |
| 143 |
masm.fsd(fs10, StackPointer, offsetof(EnterJITRegs, fs10)); |
| 144 |
masm.fsd(fs9, StackPointer, offsetof(EnterJITRegs, fs9)); |
| 145 |
masm.fsd(fs8, StackPointer, offsetof(EnterJITRegs, fs8)); |
| 146 |
masm.fsd(fs7, StackPointer, offsetof(EnterJITRegs, fs7)); |
| 147 |
masm.fsd(fs6, StackPointer, offsetof(EnterJITRegs, fs6)); |
| 148 |
masm.fsd(fs5, StackPointer, offsetof(EnterJITRegs, fs5)); |
| 149 |
masm.fsd(fs4, StackPointer, offsetof(EnterJITRegs, fs4)); |
| 150 |
masm.fsd(fs3, StackPointer, offsetof(EnterJITRegs, fs3)); |
| 151 |
masm.fsd(fs2, StackPointer, offsetof(EnterJITRegs, fs2)); |
| 152 |
masm.fsd(fs1, StackPointer, offsetof(EnterJITRegs, fs1)); |
| 153 |
masm.fsd(fs0, StackPointer, offsetof(EnterJITRegs, fs0)); |
| 154 |
} |
| 155 |
|
| 156 |
static void GenerateBailoutThunk(MacroAssembler& masm, Label* bailoutTail) { |
| 157 |
PushBailoutFrame(masm, a0); |
| 158 |
|
| 159 |
// Make space for Bailout's bailoutInfo outparam. |
| 160 |
masm.reserveStack(sizeof(void*)); |
| 161 |
masm.movePtr(StackPointer, a1); |
| 162 |
|
| 163 |
// Call the bailout function. |
| 164 |
using Fn = bool (*)(BailoutStack* sp, BaselineBailoutInfo** info); |
| 165 |
masm.setupUnalignedABICall(a2); |
| 166 |
masm.passABIArg(a0); |
| 167 |
masm.passABIArg(a1); |
| 168 |
masm.callWithABI<Fn, Bailout>(ABIType::General, |
| 169 |
CheckUnsafeCallWithABI::DontCheckOther); |
| 170 |
|
| 171 |
// Get the bailoutInfo outparam. |
| 172 |
masm.pop(a2); |
| 173 |
|
| 174 |
// Remove both the bailout frame and the topmost Ion frame's stack. |
| 175 |
masm.moveToStackPtr(FramePointer); |
| 176 |
|
| 177 |
// Jump to shared bailout tail. The BailoutInfo pointer has to be in a2. |
| 178 |
masm.jump(bailoutTail); |
| 179 |
} |
| 180 |
|
| 181 |
// Generates a trampoline for calling Jit compiled code from a C++ function. |
| 182 |
// The trampoline use the EnterJitCode signature, with the standard x64 fastcall |
| 183 |
// calling convention. |
| 184 |
void JitRuntime::generateEnterJIT(JSContext* cx, MacroAssembler& masm) { |
| 185 |
AutoCreatedBy acb(masm, "JitRuntime::generateEnterJIT"); |
| 186 |
|
| 187 |
enterJITOffset_ = startTrampolineCode(masm); |
| 188 |
|
| 189 |
const Register reg_code = IntArgReg0; |
| 190 |
const Register reg_argc = IntArgReg1; |
| 191 |
const Register reg_argv = IntArgReg2; |
| 192 |
const mozilla::DebugOnly<Register> reg_frame = IntArgReg3; |
| 193 |
const Register reg_token = IntArgReg4; |
| 194 |
const Register reg_chain = IntArgReg5; |
| 195 |
const Register reg_values = IntArgReg6; |
| 196 |
const Register reg_vp = IntArgReg7; |
| 197 |
|
| 198 |
MOZ_ASSERT(OsrFrameReg == reg_frame); |
| 199 |
|
| 200 |
GeneratePrologue(masm); |
| 201 |
|
| 202 |
// Save stack pointer as baseline frame. |
| 203 |
masm.movePtr(StackPointer, FramePointer); |
| 204 |
|
| 205 |
// Load the number of actual arguments into s3. |
| 206 |
masm.unboxInt32(Address(reg_vp, 0), s3); |
| 207 |
|
| 208 |
/*************************************************************** |
| 209 |
Loop over argv vector, push arguments onto stack in reverse order |
| 210 |
***************************************************************/ |
| 211 |
|
| 212 |
// if we are constructing, that also needs to include newTarget |
| 213 |
JitSpew(JitSpew_Codegen, "__Line__: %d", __LINE__); |
| 214 |
{ |
| 215 |
Label noNewTarget; |
| 216 |
masm.branchTest32(Assembler::Zero, reg_token, |
| 217 |
Imm32(CalleeToken_FunctionConstructing), &noNewTarget); |
| 218 |
|
| 219 |
masm.add32(Imm32(1), reg_argc); |
| 220 |
|
| 221 |
masm.bind(&noNewTarget); |
| 222 |
} |
| 223 |
JitSpew(JitSpew_Codegen, "__Line__: %d", __LINE__); |
| 224 |
// Make stack algined |
| 225 |
masm.ma_and(s2, reg_argc, Imm32(1)); |
| 226 |
masm.ma_sub64(s1, zero, Imm32(sizeof(Value))); |
| 227 |
Label no_zero; |
| 228 |
masm.ma_branch(&no_zero, Assembler::Condition::Equal, s2, Operand(0)); |
| 229 |
masm.mv(s1, zero); |
| 230 |
masm.bind(&no_zero); |
| 231 |
masm.ma_add64(StackPointer, StackPointer, s1); |
| 232 |
|
| 233 |
masm.slli(s2, reg_argc, 3); // Value* argv |
| 234 |
masm.addPtr(reg_argv, s2); // s2 = &argv[argc] |
| 235 |
JitSpew(JitSpew_Codegen, "__Line__: %d", __LINE__); |
| 236 |
// Loop over arguments, copying them from an unknown buffer onto the Ion |
| 237 |
// stack so they can be accessed from JIT'ed code. |
| 238 |
Label header, footer; |
| 239 |
// If there aren't any arguments, don't do anything |
| 240 |
masm.ma_b(s2, reg_argv, &footer, Assembler::BelowOrEqual, ShortJump); |
| 241 |
{ |
| 242 |
masm.bind(&header); |
| 243 |
|
| 244 |
masm.subPtr(Imm32(sizeof(Value)), s2); |
| 245 |
masm.subPtr(Imm32(sizeof(Value)), StackPointer); |
| 246 |
|
| 247 |
ValueOperand value = ValueOperand(s6); |
| 248 |
masm.loadValue(Address(s2, 0), value); |
| 249 |
masm.storeValue(value, Address(StackPointer, 0)); |
| 250 |
|
| 251 |
masm.ma_b(s2, reg_argv, &header, Assembler::Above, ShortJump); |
| 252 |
} |
| 253 |
masm.bind(&footer); |
| 254 |
JitSpew(JitSpew_Codegen, "__Line__: %d", __LINE__); |
| 255 |
masm.push(reg_token); |
| 256 |
masm.pushFrameDescriptorForJitCall(FrameType::CppToJSJit, s3, s3); |
| 257 |
|
| 258 |
CodeLabel returnLabel; |
| 259 |
Label oomReturnLabel; |
| 260 |
{ |
| 261 |
// Handle Interpreter -> Baseline OSR. |
| 262 |
AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All()); |
| 263 |
MOZ_ASSERT(!regs.has(FramePointer)); |
| 264 |
regs.take(OsrFrameReg); |
| 265 |
regs.take(reg_code); |
| 266 |
MOZ_ASSERT(!regs.has(ReturnReg), "ReturnReg matches reg_code"); |
| 267 |
|
| 268 |
Label notOsr; |
| 269 |
masm.ma_b(OsrFrameReg, OsrFrameReg, ¬Osr, Assembler::Zero, ShortJump); |
| 270 |
|
| 271 |
Register numStackValues = reg_values; |
| 272 |
regs.take(numStackValues); |
| 273 |
Register scratch = regs.takeAny(); |
| 274 |
|
| 275 |
// Push return address. |
| 276 |
masm.subPtr(Imm32(sizeof(uintptr_t)), StackPointer); |
| 277 |
masm.ma_li(scratch, &returnLabel); |
| 278 |
masm.storePtr(scratch, Address(StackPointer, 0)); |
| 279 |
|
| 280 |
// Push previous frame pointer. |
| 281 |
masm.subPtr(Imm32(sizeof(uintptr_t)), StackPointer); |
| 282 |
masm.storePtr(FramePointer, Address(StackPointer, 0)); |
| 283 |
|
| 284 |
// Reserve frame. |
| 285 |
Register framePtr = FramePointer; |
| 286 |
masm.movePtr(StackPointer, framePtr); |
| 287 |
masm.subPtr(Imm32(BaselineFrame::Size()), StackPointer); |
| 288 |
|
| 289 |
Register framePtrScratch = regs.takeAny(); |
| 290 |
masm.movePtr(sp, framePtrScratch); |
| 291 |
|
| 292 |
// Reserve space for locals and stack values. |
| 293 |
masm.slli(scratch, numStackValues, 3); |
| 294 |
masm.subPtr(scratch, StackPointer); |
| 295 |
|
| 296 |
// Enter exit frame. |
| 297 |
masm.reserveStack(3 * sizeof(uintptr_t)); |
| 298 |
masm.storePtr( |
| 299 |
ImmWord(MakeFrameDescriptor(FrameType::BaselineJS)), |
| 300 |
Address(StackPointer, 2 * sizeof(uintptr_t))); // Frame descriptor |
| 301 |
masm.storePtr( |
| 302 |
zero, Address(StackPointer, sizeof(uintptr_t))); // fake return address |
| 303 |
masm.storePtr(FramePointer, Address(StackPointer, 0)); |
| 304 |
|
| 305 |
// No GC things to mark, push a bare token. |
| 306 |
masm.loadJSContext(scratch); |
| 307 |
masm.enterFakeExitFrame(scratch, scratch, ExitFrameType::Bare); |
| 308 |
|
| 309 |
masm.reserveStack(2 * sizeof(uintptr_t)); |
| 310 |
masm.storePtr(framePtr, |
| 311 |
Address(StackPointer, sizeof(uintptr_t))); // BaselineFrame |
| 312 |
masm.storePtr(reg_code, Address(StackPointer, 0)); // jitcode |
| 313 |
|
| 314 |
using Fn = bool (*)(BaselineFrame* frame, InterpreterFrame* interpFrame, |
| 315 |
uint32_t numStackValues); |
| 316 |
masm.setupUnalignedABICall(scratch); |
| 317 |
masm.passABIArg(framePtrScratch); // BaselineFrame |
| 318 |
masm.passABIArg(OsrFrameReg); // InterpreterFrame |
| 319 |
masm.passABIArg(numStackValues); |
| 320 |
masm.callWithABI<Fn, jit::InitBaselineFrameForOsr>( |
| 321 |
ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame); |
| 322 |
|
| 323 |
regs.add(OsrFrameReg); |
| 324 |
Register jitcode = regs.takeAny(); |
| 325 |
masm.loadPtr(Address(StackPointer, 0), jitcode); |
| 326 |
masm.loadPtr(Address(StackPointer, sizeof(uintptr_t)), framePtr); |
| 327 |
masm.freeStack(2 * sizeof(uintptr_t)); |
| 328 |
|
| 329 |
Label error; |
| 330 |
masm.freeStack(ExitFrameLayout::SizeWithFooter()); |
| 331 |
masm.branchIfFalseBool(ReturnReg, &error); |
| 332 |
|
| 333 |
// If OSR-ing, then emit instrumentation for setting lastProfilerFrame |
| 334 |
// if profiler instrumentation is enabled. |
| 335 |
{ |
| 336 |
Label skipProfilingInstrumentation; |
| 337 |
AbsoluteAddress addressOfEnabled( |
| 338 |
cx->runtime()->geckoProfiler().addressOfEnabled()); |
| 339 |
masm.branch32(Assembler::Equal, addressOfEnabled, Imm32(0), |
| 340 |
&skipProfilingInstrumentation); |
| 341 |
masm.profilerEnterFrame(framePtr, scratch); |
| 342 |
masm.bind(&skipProfilingInstrumentation); |
| 343 |
} |
| 344 |
|
| 345 |
masm.jump(jitcode); |
| 346 |
|
| 347 |
// OOM: load error value, discard return address and previous frame |
| 348 |
// pointer and return. |
| 349 |
masm.bind(&error); |
| 350 |
masm.movePtr(framePtr, StackPointer); |
| 351 |
masm.addPtr(Imm32(2 * sizeof(uintptr_t)), StackPointer); |
| 352 |
masm.moveValue(MagicValue(JS_ION_ERROR), JSReturnOperand); |
| 353 |
masm.jump(&oomReturnLabel); |
| 354 |
|
| 355 |
masm.bind(¬Osr); |
| 356 |
// Load the scope chain in R1. |
| 357 |
MOZ_ASSERT(R1.scratchReg() != reg_code); |
| 358 |
masm.ma_or(R1.scratchReg(), reg_chain, zero); |
| 359 |
} |
| 360 |
JitSpew(JitSpew_Codegen, "__Line__: %d", __LINE__); |
| 361 |
// The call will push the return address and frame pointer on the stack, thus |
| 362 |
// we check that the stack would be aligned once the call is complete. |
| 363 |
masm.assertStackAlignment(JitStackAlignment, 2 * sizeof(uintptr_t)); |
| 364 |
|
| 365 |
// Call the function with pushing return address to stack. |
| 366 |
masm.callJitNoProfiler(reg_code); |
| 367 |
|
| 368 |
{ |
| 369 |
// Interpreter -> Baseline OSR will return here. |
| 370 |
masm.bind(&returnLabel); |
| 371 |
masm.addCodeLabel(returnLabel); |
| 372 |
masm.bind(&oomReturnLabel); |
| 373 |
} |
| 374 |
|
| 375 |
// Discard arguments and padding. Set sp to the address of the EnterJITRegs |
| 376 |
// on the stack. |
| 377 |
masm.mov(FramePointer, StackPointer); |
| 378 |
|
| 379 |
// Store the returned value into the vp |
| 380 |
masm.ld(reg_vp, StackPointer, offsetof(EnterJITRegs, a7)); |
| 381 |
masm.storeValue(JSReturnOperand, Address(reg_vp, 0)); |
| 382 |
JitSpew(JitSpew_Codegen, "__Line__: %d", __LINE__); |
| 383 |
// Restore non-volatile registers and return. |
| 384 |
GenerateReturn(masm, ShortJump); |
| 385 |
} |
| 386 |
|
| 387 |
// static |
| 388 |
mozilla::Maybe<::JS::ProfilingFrameIterator::RegisterState> |
| 389 |
JitRuntime::getCppEntryRegisters(JitFrameLayout* frameStackAddress) { |
| 390 |
return mozilla::Nothing{}; |
| 391 |
} |
| 392 |
|
| 393 |
void JitRuntime::generateInvalidator(MacroAssembler& masm, Label* bailoutTail) { |
| 394 |
AutoCreatedBy acb(masm, "JitRuntime::generateInvalidator"); |
| 395 |
|
| 396 |
invalidatorOffset_ = startTrampolineCode(masm); |
| 397 |
|
| 398 |
// Stack has to be alligned here. If not, we will have to fix it. |
| 399 |
masm.checkStackAlignment(); |
| 400 |
|
| 401 |
// Push registers such that we can access them from [base + code]. |
| 402 |
masm.PushRegsInMask(AllRegs); |
| 403 |
|
| 404 |
// Pass pointer to InvalidationBailoutStack structure. |
| 405 |
masm.movePtr(StackPointer, a0); |
| 406 |
|
| 407 |
// Reserve place for BailoutInfo pointer. Two words to ensure alignment for |
| 408 |
// setupAlignedABICall. |
| 409 |
masm.subPtr(Imm32(2 * sizeof(uintptr_t)), StackPointer); |
| 410 |
// Pass pointer to BailoutInfo |
| 411 |
masm.movePtr(StackPointer, a1); |
| 412 |
|
| 413 |
using Fn = bool (*)(InvalidationBailoutStack* sp, BaselineBailoutInfo** info); |
| 414 |
masm.setupAlignedABICall(); |
| 415 |
masm.passABIArg(a0); |
| 416 |
masm.passABIArg(a1); |
| 417 |
masm.callWithABI<Fn, InvalidationBailout>( |
| 418 |
ABIType::General, CheckUnsafeCallWithABI::DontCheckOther); |
| 419 |
|
| 420 |
masm.pop(a2); |
| 421 |
|
| 422 |
// Pop the machine state and the dead frame. |
| 423 |
masm.moveToStackPtr(FramePointer); |
| 424 |
|
| 425 |
// Jump to shared bailout tail. The BailoutInfo pointer has to be in r2. |
| 426 |
masm.jump(bailoutTail); |
| 427 |
} |
| 428 |
|
| 429 |
void JitRuntime::generateArgumentsRectifier(MacroAssembler& masm, |
| 430 |
ArgumentsRectifierKind kind) { |
| 431 |
// Do not erase the frame pointer in this function. |
| 432 |
|
| 433 |
AutoCreatedBy acb(masm, "JitRuntime::generateArgumentsRectifier"); |
| 434 |
|
| 435 |
switch (kind) { |
| 436 |
case ArgumentsRectifierKind::Normal: |
| 437 |
argumentsRectifierOffset_ = startTrampolineCode(masm); |
| 438 |
break; |
| 439 |
case ArgumentsRectifierKind::TrialInlining: |
| 440 |
trialInliningArgumentsRectifierOffset_ = startTrampolineCode(masm); |
| 441 |
break; |
| 442 |
} |
| 443 |
masm.pushReturnAddress(); |
| 444 |
// Caller: |
| 445 |
// [arg2] [arg1] [this] [[argc] [callee] [descr] [raddr]] <- sp |
| 446 |
|
| 447 |
// Frame prologue. |
| 448 |
// |
| 449 |
// NOTE: if this changes, fix the Baseline bailout code too! |
| 450 |
// See BaselineStackBuilder::calculatePrevFramePtr and |
| 451 |
// BaselineStackBuilder::buildRectifierFrame (in BaselineBailouts.cpp). |
| 452 |
masm.push(FramePointer); |
| 453 |
masm.mov(StackPointer, FramePointer); |
| 454 |
|
| 455 |
// Load argc. |
| 456 |
masm.loadNumActualArgs(FramePointer, s3); |
| 457 |
|
| 458 |
Register numActArgsReg = a6; |
| 459 |
Register calleeTokenReg = a7; |
| 460 |
Register numArgsReg = a5; |
| 461 |
|
| 462 |
// Load |nformals| into numArgsReg. |
| 463 |
masm.loadPtr( |
| 464 |
Address(FramePointer, RectifierFrameLayout::offsetOfCalleeToken()), |
| 465 |
calleeTokenReg); |
| 466 |
masm.mov(calleeTokenReg, numArgsReg); |
| 467 |
masm.andPtr(Imm32(uint32_t(CalleeTokenMask)), numArgsReg); |
| 468 |
masm.loadFunctionArgCount(numArgsReg, numArgsReg); |
| 469 |
|
| 470 |
// Stash another copy in t3, since we are going to do destructive operations |
| 471 |
// on numArgsReg |
| 472 |
masm.mov(numArgsReg, t3); |
| 473 |
|
| 474 |
static_assert( |
| 475 |
CalleeToken_FunctionConstructing == 1, |
| 476 |
"Ensure that we can use the constructing bit to count the value"); |
| 477 |
masm.mov(calleeTokenReg, t2); |
| 478 |
masm.ma_and(t2, t2, Imm32(uint32_t(CalleeToken_FunctionConstructing))); |
| 479 |
|
| 480 |
// Including |this|, and |new.target|, there are (|nformals| + 1 + |
| 481 |
// isConstructing) arguments to push to the stack. Then we push a |
| 482 |
// JitFrameLayout. We compute the padding expressed in the number of extra |
| 483 |
// |undefined| values to push on the stack. |
| 484 |
static_assert( |
| 485 |
sizeof(JitFrameLayout) % JitStackAlignment == 0, |
| 486 |
"No need to consider the JitFrameLayout for aligning the stack"); |
| 487 |
static_assert( |
| 488 |
JitStackAlignment % sizeof(Value) == 0, |
| 489 |
"Ensure that we can pad the stack by pushing extra UndefinedValue"); |
| 490 |
|
| 491 |
MOZ_ASSERT(mozilla::IsPowerOfTwo(JitStackValueAlignment)); |
| 492 |
masm.add32( |
| 493 |
Imm32(JitStackValueAlignment - 1 /* for padding */ + 1 /* for |this| */), |
| 494 |
numArgsReg); |
| 495 |
masm.add32(t2, numArgsReg); |
| 496 |
masm.and32(Imm32(~(JitStackValueAlignment - 1)), numArgsReg); |
| 497 |
|
| 498 |
// Load the number of |undefined|s to push into t1. Subtract 1 for |this|. |
| 499 |
masm.ma_sub64(t1, numArgsReg, s3); |
| 500 |
masm.sub32(Imm32(1), t1); |
| 501 |
|
| 502 |
// Caller: |
| 503 |
// [arg2] [arg1] [this] [ [argc] [callee] [descr] [raddr] ] <- sp |
| 504 |
// '--- s3 ----' |
| 505 |
// |
| 506 |
// Rectifier frame: |
| 507 |
// [fp'] [undef] [undef] [undef] [arg2] [arg1] [this] [ [argc] [callee] |
| 508 |
// [descr] [raddr] ] |
| 509 |
// '-------- t1 ---------' '--- s3 ----' |
| 510 |
|
| 511 |
// Copy number of actual arguments into numActArgsReg. |
| 512 |
masm.mov(s3, numActArgsReg); |
| 513 |
|
| 514 |
masm.moveValue(UndefinedValue(), ValueOperand(t0)); |
| 515 |
|
| 516 |
// Push undefined. (including the padding) |
| 517 |
{ |
| 518 |
Label undefLoopTop; |
| 519 |
|
| 520 |
masm.bind(&undefLoopTop); |
| 521 |
masm.sub32(Imm32(1), t1); |
| 522 |
masm.subPtr(Imm32(sizeof(Value)), StackPointer); |
| 523 |
masm.storeValue(ValueOperand(t0), Address(StackPointer, 0)); |
| 524 |
|
| 525 |
masm.ma_b(t1, t1, &undefLoopTop, Assembler::NonZero, ShortJump); |
| 526 |
} |
| 527 |
|
| 528 |
// Get the topmost argument. |
| 529 |
static_assert(sizeof(Value) == 8, "TimesEight is used to skip arguments"); |
| 530 |
|
| 531 |
// Get the topmost argument. |
| 532 |
masm.slli(t0, s3, 3); // t0 <- nargs * 8 |
| 533 |
masm.ma_add64(t1, FramePointer, t0); // t1 <- fp(saved sp) + nargs * 8 |
| 534 |
masm.addPtr(Imm32(sizeof(RectifierFrameLayout)), t1); |
| 535 |
|
| 536 |
// Push arguments, |nargs| + 1 times (to include |this|). |
| 537 |
masm.addPtr(Imm32(1), s3); |
| 538 |
{ |
| 539 |
Label copyLoopTop; |
| 540 |
|
| 541 |
masm.bind(©LoopTop); |
| 542 |
masm.sub32(Imm32(1), s3); |
| 543 |
masm.subPtr(Imm32(sizeof(Value)), StackPointer); |
| 544 |
masm.loadValue(Address(t1, 0), ValueOperand(t0)); |
| 545 |
masm.storeValue(ValueOperand(t0), Address(StackPointer, 0)); |
| 546 |
masm.subPtr(Imm32(sizeof(Value)), t1); |
| 547 |
|
| 548 |
masm.ma_b(s3, s3, ©LoopTop, Assembler::NonZero, ShortJump); |
| 549 |
} |
| 550 |
|
| 551 |
// if constructing, copy newTarget |
| 552 |
{ |
| 553 |
Label notConstructing; |
| 554 |
|
| 555 |
masm.branchTest32(Assembler::Zero, calleeTokenReg, |
| 556 |
Imm32(CalleeToken_FunctionConstructing), |
| 557 |
¬Constructing); |
| 558 |
|
| 559 |
// thisFrame[numFormals] = prevFrame[argc] |
| 560 |
ValueOperand newTarget(t0); |
| 561 |
|
| 562 |
// Load vp[argc]. Add sizeof(Value) for |this|. |
| 563 |
BaseIndex newTargetSrc(FramePointer, numActArgsReg, TimesEight, |
| 564 |
sizeof(RectifierFrameLayout) + sizeof(Value)); |
| 565 |
masm.loadValue(newTargetSrc, newTarget); |
| 566 |
|
| 567 |
// Again, 1 for |this| |
| 568 |
BaseIndex newTargetDest(StackPointer, t3, TimesEight, sizeof(Value)); |
| 569 |
masm.storeValue(newTarget, newTargetDest); |
| 570 |
|
| 571 |
masm.bind(¬Constructing); |
| 572 |
} |
| 573 |
|
| 574 |
// Caller: |
| 575 |
// [arg2] [arg1] [this] [ [argc] [callee] [descr] [raddr] ] |
| 576 |
// |
| 577 |
// |
| 578 |
// Rectifier frame: |
| 579 |
// [fp'] <- fp [undef] [undef] [undef] [arg2] [arg1] [this] <- sp [ [argc] |
| 580 |
// [callee] [descr] [raddr] ] |
| 581 |
// |
| 582 |
|
| 583 |
// Construct JitFrameLayout. |
| 584 |
masm.push(calleeTokenReg); |
| 585 |
masm.pushFrameDescriptorForJitCall(FrameType::Rectifier, numActArgsReg, |
| 586 |
numActArgsReg); |
| 587 |
|
| 588 |
// Call the target function. |
| 589 |
masm.andPtr(Imm32(uint32_t(CalleeTokenMask)), calleeTokenReg); |
| 590 |
switch (kind) { |
| 591 |
case ArgumentsRectifierKind::Normal: |
| 592 |
masm.loadJitCodeRaw(calleeTokenReg, t1); |
| 593 |
argumentsRectifierReturnOffset_ = masm.callJitNoProfiler(t1); |
| 594 |
break; |
| 595 |
case ArgumentsRectifierKind::TrialInlining: |
| 596 |
Label noBaselineScript, done; |
| 597 |
masm.loadBaselineJitCodeRaw(calleeTokenReg, t1, &noBaselineScript); |
| 598 |
masm.callJitNoProfiler(t1); |
| 599 |
masm.jump(&done); |
| 600 |
|
| 601 |
// See BaselineCacheIRCompiler::emitCallInlinedFunction. |
| 602 |
masm.bind(&noBaselineScript); |
| 603 |
masm.loadJitCodeRaw(calleeTokenReg, t1); |
| 604 |
masm.callJitNoProfiler(t1); |
| 605 |
masm.bind(&done); |
| 606 |
break; |
| 607 |
} |
| 608 |
|
| 609 |
masm.mov(FramePointer, StackPointer); |
| 610 |
masm.pop(FramePointer); |
| 611 |
masm.ret(); |
| 612 |
} |
| 613 |
|
| 614 |
void JitRuntime::generateBailoutHandler(MacroAssembler& masm, |
| 615 |
Label* bailoutTail) { |
| 616 |
AutoCreatedBy acb(masm, "JitRuntime::generateBailoutHandler"); |
| 617 |
|
| 618 |
bailoutHandlerOffset_ = startTrampolineCode(masm); |
| 619 |
|
| 620 |
GenerateBailoutThunk(masm, bailoutTail); |
| 621 |
} |
| 622 |
|
| 623 |
uint32_t JitRuntime::generatePreBarrier(JSContext* cx, MacroAssembler& masm, |
| 624 |
MIRType type) { |
| 625 |
AutoCreatedBy acb(masm, "JitRuntime::generatePreBarrier"); |
| 626 |
|
| 627 |
uint32_t offset = startTrampolineCode(masm); |
| 628 |
|
| 629 |
MOZ_ASSERT(PreBarrierReg == a1); |
| 630 |
Register temp1 = a0; |
| 631 |
Register temp2 = a2; |
| 632 |
Register temp3 = a3; |
| 633 |
masm.push(temp1); |
| 634 |
masm.push(temp2); |
| 635 |
masm.push(temp3); |
| 636 |
|
| 637 |
Label noBarrier; |
| 638 |
masm.emitPreBarrierFastPath(cx->runtime(), type, temp1, temp2, temp3, |
| 639 |
&noBarrier); |
| 640 |
|
| 641 |
// Call into C++ to mark this GC thing. |
| 642 |
masm.pop(temp3); |
| 643 |
masm.pop(temp2); |
| 644 |
masm.pop(temp1); |
| 645 |
|
| 646 |
LiveRegisterSet save; |
| 647 |
save.set() = RegisterSet(GeneralRegisterSet(Registers::VolatileMask), |
| 648 |
FloatRegisterSet(FloatRegisters::VolatileMask)); |
| 649 |
masm.push(ra); |
| 650 |
masm.PushRegsInMask(save); |
| 651 |
|
| 652 |
masm.movePtr(ImmPtr(cx->runtime()), a0); |
| 653 |
|
| 654 |
masm.setupUnalignedABICall(a2); |
| 655 |
masm.passABIArg(a0); |
| 656 |
masm.passABIArg(a1); |
| 657 |
masm.callWithABI(JitPreWriteBarrier(type)); |
| 658 |
|
| 659 |
masm.PopRegsInMask(save); |
| 660 |
masm.ret(); |
| 661 |
|
| 662 |
masm.bind(&noBarrier); |
| 663 |
masm.pop(temp3); |
| 664 |
masm.pop(temp2); |
| 665 |
masm.pop(temp1); |
| 666 |
masm.abiret(); |
| 667 |
|
| 668 |
return offset; |
| 669 |
} |
| 670 |
|
| 671 |
void JitRuntime::generateBailoutTailStub(MacroAssembler& masm, |
| 672 |
Label* bailoutTail) { |
| 673 |
AutoCreatedBy acb(masm, "JitRuntime::generateBailoutTailStub"); |
| 674 |
|
| 675 |
masm.bind(bailoutTail); |
| 676 |
masm.generateBailoutTail(a1, a2); |
| 677 |
} |
| 678 |
|
| 679 |
bool JitRuntime::generateVMWrapper(JSContext* cx, MacroAssembler& masm, |
| 680 |
VMFunctionId id, const VMFunctionData& f, |
| 681 |
DynFn nativeFun, uint32_t* wrapperOffset) { |
| 682 |
AutoCreatedBy acb(masm, "JitRuntime::generateVMWrapper"); |
| 683 |
|
| 684 |
*wrapperOffset = startTrampolineCode(masm); |
| 685 |
|
| 686 |
// Avoid conflicts with argument registers while discarding the result after |
| 687 |
// the function call. |
| 688 |
AllocatableGeneralRegisterSet regs(Register::Codes::WrapperMask); |
| 689 |
|
| 690 |
static_assert( |
| 691 |
(Register::Codes::VolatileMask & ~Register::Codes::WrapperMask) == 0, |
| 692 |
"Wrapper register set should be a superset of Volatile register set."); |
| 693 |
|
| 694 |
// The context is the first argument; a0 is the first argument register. |
| 695 |
Register cxreg = a0; |
| 696 |
regs.take(cxreg); |
| 697 |
|
| 698 |
// On link-register platforms, it is the responsibility of the VM *callee* to |
| 699 |
// push the return address, while the caller must ensure that the address |
| 700 |
// is stored in ra on entry. This allows the VM wrapper to work with both |
| 701 |
// direct calls and tail calls. |
| 702 |
masm.pushReturnAddress(); |
| 703 |
|
| 704 |
// Push the frame pointer to finish the exit frame, then link it up. |
| 705 |
masm.Push(FramePointer); |
| 706 |
masm.moveStackPtrTo(FramePointer); |
| 707 |
masm.loadJSContext(cxreg); |
| 708 |
masm.enterExitFrame(cxreg, regs.getAny(), id); |
| 709 |
|
| 710 |
// Reserve space for the outparameter. |
| 711 |
masm.reserveVMFunctionOutParamSpace(f); |
| 712 |
|
| 713 |
masm.setupUnalignedABICallDontSaveRestoreSP(); |
| 714 |
masm.passABIArg(cxreg); |
| 715 |
|
| 716 |
size_t argDisp = ExitFrameLayout::Size(); |
| 717 |
|
| 718 |
// Copy any arguments. |
| 719 |
for (uint32_t explicitArg = 0; explicitArg < f.explicitArgs; explicitArg++) { |
| 720 |
switch (f.argProperties(explicitArg)) { |
| 721 |
case VMFunctionData::WordByValue: |
| 722 |
if (f.argPassedInFloatReg(explicitArg)) { |
| 723 |
masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::Float64); |
| 724 |
} else { |
| 725 |
masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::General); |
| 726 |
} |
| 727 |
argDisp += sizeof(void*); |
| 728 |
break; |
| 729 |
case VMFunctionData::WordByRef: |
| 730 |
masm.passABIArg(MoveOperand(FramePointer, argDisp, |
| 731 |
MoveOperand::Kind::EffectiveAddress), |
| 732 |
ABIType::General); |
| 733 |
argDisp += sizeof(void*); |
| 734 |
break; |
| 735 |
case VMFunctionData::DoubleByValue: |
| 736 |
case VMFunctionData::DoubleByRef: |
| 737 |
MOZ_CRASH("NYI: riscv callVM should not be used with 128bits values."); |
| 738 |
break; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
// Copy the semi-implicit outparam, if any. |
| 743 |
// It is not a C++-abi outparam, which would get passed in the |
| 744 |
// outparam register, but a real parameter to the function, which |
| 745 |
// was stack-allocated above. |
| 746 |
const int32_t outParamOffset = |
| 747 |
-int32_t(ExitFooterFrame::Size()) - f.sizeOfOutParamStackSlot(); |
| 748 |
if (f.outParam != Type_Void) { |
| 749 |
masm.passABIArg(MoveOperand(FramePointer, outParamOffset, |
| 750 |
MoveOperand::Kind::EffectiveAddress), |
| 751 |
ABIType::General); |
| 752 |
} |
| 753 |
|
| 754 |
masm.callWithABI(nativeFun, ABIType::General, |
| 755 |
CheckUnsafeCallWithABI::DontCheckHasExitFrame); |
| 756 |
|
| 757 |
// Test for failure. |
| 758 |
switch (f.failType()) { |
| 759 |
case Type_Cell: |
| 760 |
masm.branchTestPtr(Assembler::Zero, a0, a0, masm.failureLabel()); |
| 761 |
break; |
| 762 |
case Type_Bool: |
| 763 |
// Called functions return bools, which are 0/false and non-zero/true |
| 764 |
masm.branchIfFalseBool(a0, masm.failureLabel()); |
| 765 |
break; |
| 766 |
case Type_Void: |
| 767 |
break; |
| 768 |
default: |
| 769 |
MOZ_CRASH("unknown failure kind"); |
| 770 |
} |
| 771 |
|
| 772 |
// Load the outparam. |
| 773 |
masm.loadVMFunctionOutParam(f, Address(FramePointer, outParamOffset)); |
| 774 |
|
| 775 |
// Pop frame and restore frame pointer. |
| 776 |
masm.moveToStackPtr(FramePointer); |
| 777 |
masm.pop(FramePointer); |
| 778 |
|
| 779 |
// Return. Subtract sizeof(void*) for the frame pointer. |
| 780 |
masm.retn(Imm32(sizeof(ExitFrameLayout) - sizeof(void*) + |
| 781 |
f.explicitStackSlots() * sizeof(void*) + |
| 782 |
f.extraValuesToPop * sizeof(Value))); |
| 783 |
|
| 784 |
return true; |
| 785 |
} |