| 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 "mozilla/MathAlgorithms.h" |
| 8 |
|
| 9 |
#include "jit/Bailouts.h" |
| 10 |
#include "jit/BaselineFrame.h" |
| 11 |
#include "jit/BaselineJIT.h" |
| 12 |
#include "jit/CalleeToken.h" |
| 13 |
#include "jit/JitFrames.h" |
| 14 |
#include "jit/JitRuntime.h" |
| 15 |
#include "jit/JitSpewer.h" |
| 16 |
#include "jit/PerfSpewer.h" |
| 17 |
#include "jit/VMFunctions.h" |
| 18 |
#include "jit/x86/SharedICHelpers-x86.h" |
| 19 |
#include "vm/JitActivation.h" // js::jit::JitActivation |
| 20 |
#include "vm/JSContext.h" |
| 21 |
#include "vm/Realm.h" |
| 22 |
#ifdef MOZ_VTUNE |
| 23 |
# include "vtune/VTuneWrapper.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "jit/MacroAssembler-inl.h" |
| 27 |
#include "vm/JSScript-inl.h" |
| 28 |
|
| 29 |
using mozilla::IsPowerOfTwo; |
| 30 |
|
| 31 |
using namespace js; |
| 32 |
using namespace js::jit; |
| 33 |
|
| 34 |
// All registers to save and restore. This includes the stack pointer, since we |
| 35 |
// use the ability to reference register values on the stack by index. |
| 36 |
static const LiveRegisterSet AllRegs = |
| 37 |
LiveRegisterSet(GeneralRegisterSet(Registers::AllMask), |
| 38 |
FloatRegisterSet(FloatRegisters::AllMask)); |
| 39 |
|
| 40 |
enum EnterJitEbpArgumentOffset { |
| 41 |
ARG_JITCODE = 2 * sizeof(void*), |
| 42 |
ARG_ARGC = 3 * sizeof(void*), |
| 43 |
ARG_ARGV = 4 * sizeof(void*), |
| 44 |
ARG_STACKFRAME = 5 * sizeof(void*), |
| 45 |
ARG_CALLEETOKEN = 6 * sizeof(void*), |
| 46 |
ARG_SCOPECHAIN = 7 * sizeof(void*), |
| 47 |
ARG_STACKVALUES = 8 * sizeof(void*), |
| 48 |
ARG_RESULT = 9 * sizeof(void*) |
| 49 |
}; |
| 50 |
|
| 51 |
// Generates a trampoline for calling Jit compiled code from a C++ function. |
| 52 |
// The trampoline use the EnterJitCode signature, with the standard cdecl |
| 53 |
// calling convention. |
| 54 |
void JitRuntime::generateEnterJIT(JSContext* cx, MacroAssembler& masm) { |
| 55 |
AutoCreatedBy acb(masm, "JitRuntime::generateEnterJIT"); |
| 56 |
|
| 57 |
enterJITOffset_ = startTrampolineCode(masm); |
| 58 |
|
| 59 |
masm.assertStackAlignment(ABIStackAlignment, |
| 60 |
-int32_t(sizeof(uintptr_t)) /* return address */); |
| 61 |
|
| 62 |
// Save old stack frame pointer, set new stack frame pointer. |
| 63 |
masm.push(ebp); |
| 64 |
masm.movl(esp, ebp); |
| 65 |
|
| 66 |
// Save non-volatile registers. These must be saved by the trampoline, |
| 67 |
// rather than the JIT'd code, because they are scanned by the conservative |
| 68 |
// scanner. |
| 69 |
masm.push(ebx); |
| 70 |
masm.push(esi); |
| 71 |
masm.push(edi); |
| 72 |
|
| 73 |
// Load the number of values to be copied (argc) into eax |
| 74 |
masm.loadPtr(Address(ebp, ARG_ARGC), eax); |
| 75 |
|
| 76 |
// If we are constructing, that also needs to include newTarget |
| 77 |
{ |
| 78 |
Label noNewTarget; |
| 79 |
masm.loadPtr(Address(ebp, ARG_CALLEETOKEN), edx); |
| 80 |
masm.branchTest32(Assembler::Zero, edx, |
| 81 |
Imm32(CalleeToken_FunctionConstructing), &noNewTarget); |
| 82 |
|
| 83 |
masm.addl(Imm32(1), eax); |
| 84 |
|
| 85 |
masm.bind(&noNewTarget); |
| 86 |
} |
| 87 |
|
| 88 |
// eax <- 8*numValues, eax is now the offset betwen argv and the last value. |
| 89 |
masm.shll(Imm32(3), eax); |
| 90 |
|
| 91 |
// Guarantee stack alignment of Jit frames. |
| 92 |
// |
| 93 |
// This code compensates for the offset created by the copy of the vector of |
| 94 |
// arguments, such that the jit frame will be aligned once the return |
| 95 |
// address is pushed on the stack. |
| 96 |
// |
| 97 |
// In the computation of the offset, we omit the size of the JitFrameLayout |
| 98 |
// which is pushed on the stack, as the JitFrameLayout size is a multiple of |
| 99 |
// the JitStackAlignment. |
| 100 |
masm.movl(esp, ecx); |
| 101 |
masm.subl(eax, ecx); |
| 102 |
static_assert( |
| 103 |
sizeof(JitFrameLayout) % JitStackAlignment == 0, |
| 104 |
"No need to consider the JitFrameLayout for aligning the stack"); |
| 105 |
|
| 106 |
// ecx = ecx & 15, holds alignment. |
| 107 |
masm.andl(Imm32(JitStackAlignment - 1), ecx); |
| 108 |
masm.subl(ecx, esp); |
| 109 |
|
| 110 |
/*************************************************************** |
| 111 |
Loop over argv vector, push arguments onto stack in reverse order |
| 112 |
***************************************************************/ |
| 113 |
|
| 114 |
// ebx = argv --argv pointer is in ebp + 16 |
| 115 |
masm.loadPtr(Address(ebp, ARG_ARGV), ebx); |
| 116 |
|
| 117 |
// eax = argv[8(argc)] --eax now points one value past the last argument |
| 118 |
masm.addl(ebx, eax); |
| 119 |
|
| 120 |
// while (eax > ebx) --while still looping through arguments |
| 121 |
{ |
| 122 |
Label header, footer; |
| 123 |
masm.bind(&header); |
| 124 |
|
| 125 |
masm.cmp32(eax, ebx); |
| 126 |
masm.j(Assembler::BelowOrEqual, &footer); |
| 127 |
|
| 128 |
// eax -= 8 --move to previous argument |
| 129 |
masm.subl(Imm32(8), eax); |
| 130 |
|
| 131 |
// Push what eax points to on stack, a Value is 2 words |
| 132 |
masm.push(Operand(eax, 4)); |
| 133 |
masm.push(Operand(eax, 0)); |
| 134 |
|
| 135 |
masm.jmp(&header); |
| 136 |
masm.bind(&footer); |
| 137 |
} |
| 138 |
|
| 139 |
// Load the number of actual arguments. |result| is used to store the |
| 140 |
// actual number of arguments without adding an extra argument to the enter |
| 141 |
// JIT. |
| 142 |
masm.mov(Operand(ebp, ARG_RESULT), eax); |
| 143 |
masm.unboxInt32(Address(eax, 0x0), eax); |
| 144 |
|
| 145 |
// Push the callee token. |
| 146 |
masm.push(Operand(ebp, ARG_CALLEETOKEN)); |
| 147 |
|
| 148 |
// Load the InterpreterFrame address into the OsrFrameReg. |
| 149 |
// This address is also used for setting the constructing bit on all paths. |
| 150 |
masm.loadPtr(Address(ebp, ARG_STACKFRAME), OsrFrameReg); |
| 151 |
|
| 152 |
// Push the descriptor. |
| 153 |
masm.pushFrameDescriptorForJitCall(FrameType::CppToJSJit, eax, eax); |
| 154 |
|
| 155 |
CodeLabel returnLabel; |
| 156 |
Label oomReturnLabel; |
| 157 |
{ |
| 158 |
// Handle Interpreter -> Baseline OSR. |
| 159 |
AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All()); |
| 160 |
MOZ_ASSERT(!regs.has(ebp)); |
| 161 |
regs.take(OsrFrameReg); |
| 162 |
regs.take(ReturnReg); |
| 163 |
|
| 164 |
Register scratch = regs.takeAny(); |
| 165 |
|
| 166 |
Label notOsr; |
| 167 |
masm.branchTestPtr(Assembler::Zero, OsrFrameReg, OsrFrameReg, ¬Osr); |
| 168 |
|
| 169 |
Register numStackValues = regs.takeAny(); |
| 170 |
masm.loadPtr(Address(ebp, ARG_STACKVALUES), numStackValues); |
| 171 |
|
| 172 |
Register jitcode = regs.takeAny(); |
| 173 |
masm.loadPtr(Address(ebp, ARG_JITCODE), jitcode); |
| 174 |
|
| 175 |
// Push return address. |
| 176 |
masm.mov(&returnLabel, scratch); |
| 177 |
masm.push(scratch); |
| 178 |
|
| 179 |
// Frame prologue. |
| 180 |
masm.push(ebp); |
| 181 |
masm.mov(esp, ebp); |
| 182 |
|
| 183 |
// Reserve frame. |
| 184 |
masm.subPtr(Imm32(BaselineFrame::Size()), esp); |
| 185 |
|
| 186 |
Register framePtrScratch = regs.takeAny(); |
| 187 |
masm.touchFrameValues(numStackValues, scratch, framePtrScratch); |
| 188 |
masm.mov(esp, framePtrScratch); |
| 189 |
|
| 190 |
// Reserve space for locals and stack values. |
| 191 |
masm.mov(numStackValues, scratch); |
| 192 |
masm.shll(Imm32(3), scratch); |
| 193 |
masm.subPtr(scratch, esp); |
| 194 |
|
| 195 |
// Enter exit frame. |
| 196 |
masm.pushFrameDescriptor(FrameType::BaselineJS); |
| 197 |
masm.push(Imm32(0)); // Fake return address. |
| 198 |
masm.push(FramePointer); |
| 199 |
// No GC things to mark on the stack, push a bare token. |
| 200 |
masm.loadJSContext(scratch); |
| 201 |
masm.enterFakeExitFrame(scratch, scratch, ExitFrameType::Bare); |
| 202 |
|
| 203 |
masm.push(jitcode); |
| 204 |
|
| 205 |
using Fn = bool (*)(BaselineFrame* frame, InterpreterFrame* interpFrame, |
| 206 |
uint32_t numStackValues); |
| 207 |
masm.setupUnalignedABICall(scratch); |
| 208 |
masm.passABIArg(framePtrScratch); // BaselineFrame |
| 209 |
masm.passABIArg(OsrFrameReg); // InterpreterFrame |
| 210 |
masm.passABIArg(numStackValues); |
| 211 |
masm.callWithABI<Fn, jit::InitBaselineFrameForOsr>( |
| 212 |
ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame); |
| 213 |
|
| 214 |
masm.pop(jitcode); |
| 215 |
|
| 216 |
MOZ_ASSERT(jitcode != ReturnReg); |
| 217 |
|
| 218 |
Label error; |
| 219 |
masm.addPtr(Imm32(ExitFrameLayout::SizeWithFooter()), esp); |
| 220 |
masm.branchIfFalseBool(ReturnReg, &error); |
| 221 |
|
| 222 |
// If OSR-ing, then emit instrumentation for setting lastProfilerFrame |
| 223 |
// if profiler instrumentation is enabled. |
| 224 |
{ |
| 225 |
Label skipProfilingInstrumentation; |
| 226 |
AbsoluteAddress addressOfEnabled( |
| 227 |
cx->runtime()->geckoProfiler().addressOfEnabled()); |
| 228 |
masm.branch32(Assembler::Equal, addressOfEnabled, Imm32(0), |
| 229 |
&skipProfilingInstrumentation); |
| 230 |
masm.profilerEnterFrame(ebp, scratch); |
| 231 |
masm.bind(&skipProfilingInstrumentation); |
| 232 |
} |
| 233 |
|
| 234 |
masm.jump(jitcode); |
| 235 |
|
| 236 |
// OOM: frame epilogue, load error value, discard return address and return. |
| 237 |
masm.bind(&error); |
| 238 |
masm.mov(ebp, esp); |
| 239 |
masm.pop(ebp); |
| 240 |
masm.addPtr(Imm32(sizeof(uintptr_t)), esp); // Return address. |
| 241 |
masm.moveValue(MagicValue(JS_ION_ERROR), JSReturnOperand); |
| 242 |
masm.jump(&oomReturnLabel); |
| 243 |
|
| 244 |
masm.bind(¬Osr); |
| 245 |
masm.loadPtr(Address(ebp, ARG_SCOPECHAIN), R1.scratchReg()); |
| 246 |
} |
| 247 |
|
| 248 |
// The call will push the return address and frame pointer on the stack, thus |
| 249 |
// we check that the stack would be aligned once the call is complete. |
| 250 |
masm.assertStackAlignment(JitStackAlignment, 2 * sizeof(uintptr_t)); |
| 251 |
|
| 252 |
/*************************************************************** |
| 253 |
Call passed-in code, get return value and fill in the |
| 254 |
passed in return value pointer |
| 255 |
***************************************************************/ |
| 256 |
masm.call(Address(ebp, ARG_JITCODE)); |
| 257 |
|
| 258 |
{ |
| 259 |
// Interpreter -> Baseline OSR will return here. |
| 260 |
masm.bind(&returnLabel); |
| 261 |
masm.addCodeLabel(returnLabel); |
| 262 |
masm.bind(&oomReturnLabel); |
| 263 |
} |
| 264 |
|
| 265 |
// Restore the stack pointer so the stack looks like this: |
| 266 |
// +20 ... arguments ... |
| 267 |
// +16 <return> |
| 268 |
// +12 ebp <- %ebp pointing here. |
| 269 |
// +8 ebx |
| 270 |
// +4 esi |
| 271 |
// +0 edi <- %esp pointing here. |
| 272 |
masm.lea(Operand(ebp, -int32_t(3 * sizeof(void*))), esp); |
| 273 |
|
| 274 |
// Store the return value. |
| 275 |
masm.loadPtr(Address(ebp, ARG_RESULT), eax); |
| 276 |
masm.storeValue(JSReturnOperand, Operand(eax, 0)); |
| 277 |
|
| 278 |
/************************************************************** |
| 279 |
Return stack and registers to correct state |
| 280 |
**************************************************************/ |
| 281 |
|
| 282 |
// Restore non-volatile registers |
| 283 |
masm.pop(edi); |
| 284 |
masm.pop(esi); |
| 285 |
masm.pop(ebx); |
| 286 |
|
| 287 |
// Restore old stack frame pointer |
| 288 |
masm.pop(ebp); |
| 289 |
masm.ret(); |
| 290 |
} |
| 291 |
|
| 292 |
// static |
| 293 |
mozilla::Maybe<::JS::ProfilingFrameIterator::RegisterState> |
| 294 |
JitRuntime::getCppEntryRegisters(JitFrameLayout* frameStackAddress) { |
| 295 |
// Not supported, or not implemented yet. |
| 296 |
// TODO: Implement along with the corresponding stack-walker changes, in |
| 297 |
// coordination with the Gecko Profiler, see bug 1635987 and follow-ups. |
| 298 |
return mozilla::Nothing{}; |
| 299 |
} |
| 300 |
|
| 301 |
// Push AllRegs in a way that is compatible with RegisterDump, regardless of |
| 302 |
// what PushRegsInMask might do to reduce the set size. |
| 303 |
static void DumpAllRegs(MacroAssembler& masm) { |
| 304 |
#ifdef ENABLE_WASM_SIMD |
| 305 |
masm.PushRegsInMask(AllRegs); |
| 306 |
#else |
| 307 |
// When SIMD isn't supported, PushRegsInMask reduces the set of float |
| 308 |
// registers to be double-sized, while the RegisterDump expects each of |
| 309 |
// the float registers to have the maximal possible size |
| 310 |
// (Simd128DataSize). To work around this, we just spill the double |
| 311 |
// registers by hand here, using the register dump offset directly. |
| 312 |
for (GeneralRegisterBackwardIterator iter(AllRegs.gprs()); iter.more(); |
| 313 |
++iter) { |
| 314 |
masm.Push(*iter); |
| 315 |
} |
| 316 |
|
| 317 |
masm.reserveStack(sizeof(RegisterDump::FPUArray)); |
| 318 |
for (FloatRegisterBackwardIterator iter(AllRegs.fpus()); iter.more(); |
| 319 |
++iter) { |
| 320 |
FloatRegister reg = *iter; |
| 321 |
Address spillAddress(StackPointer, reg.getRegisterDumpOffsetInBytes()); |
| 322 |
masm.storeDouble(reg, spillAddress); |
| 323 |
} |
| 324 |
#endif |
| 325 |
} |
| 326 |
|
| 327 |
void JitRuntime::generateInvalidator(MacroAssembler& masm, Label* bailoutTail) { |
| 328 |
AutoCreatedBy acb(masm, "JitRuntime::generateInvalidator"); |
| 329 |
|
| 330 |
invalidatorOffset_ = startTrampolineCode(masm); |
| 331 |
|
| 332 |
// We do the minimum amount of work in assembly and shunt the rest |
| 333 |
// off to InvalidationBailout. Assembly does: |
| 334 |
// |
| 335 |
// - Push the machine state onto the stack. |
| 336 |
// - Call the InvalidationBailout routine with the stack pointer. |
| 337 |
// - Now that the frame has been bailed out, convert the invalidated |
| 338 |
// frame into an exit frame. |
| 339 |
// - Do the normal check-return-code-and-thunk-to-the-interpreter dance. |
| 340 |
|
| 341 |
// Push registers such that we can access them from [base + code]. |
| 342 |
DumpAllRegs(masm); |
| 343 |
|
| 344 |
masm.movl(esp, eax); // Argument to jit::InvalidationBailout. |
| 345 |
|
| 346 |
// Make space for InvalidationBailout's bailoutInfo outparam. |
| 347 |
masm.reserveStack(sizeof(void*)); |
| 348 |
masm.movl(esp, ebx); |
| 349 |
|
| 350 |
using Fn = bool (*)(InvalidationBailoutStack* sp, BaselineBailoutInfo** info); |
| 351 |
masm.setupUnalignedABICall(edx); |
| 352 |
masm.passABIArg(eax); |
| 353 |
masm.passABIArg(ebx); |
| 354 |
masm.callWithABI<Fn, InvalidationBailout>( |
| 355 |
ABIType::General, CheckUnsafeCallWithABI::DontCheckOther); |
| 356 |
|
| 357 |
masm.pop(ecx); // Get bailoutInfo outparam. |
| 358 |
|
| 359 |
// Pop the machine state and the dead frame. |
| 360 |
masm.moveToStackPtr(FramePointer); |
| 361 |
|
| 362 |
// Jump to shared bailout tail. The BailoutInfo pointer has to be in ecx. |
| 363 |
masm.jmp(bailoutTail); |
| 364 |
} |
| 365 |
|
| 366 |
void JitRuntime::generateArgumentsRectifier(MacroAssembler& masm, |
| 367 |
ArgumentsRectifierKind kind) { |
| 368 |
AutoCreatedBy acb(masm, "JitRuntime::generateArgumentsRectifier"); |
| 369 |
|
| 370 |
switch (kind) { |
| 371 |
case ArgumentsRectifierKind::Normal: |
| 372 |
argumentsRectifierOffset_ = startTrampolineCode(masm); |
| 373 |
break; |
| 374 |
case ArgumentsRectifierKind::TrialInlining: |
| 375 |
trialInliningArgumentsRectifierOffset_ = startTrampolineCode(masm); |
| 376 |
break; |
| 377 |
} |
| 378 |
|
| 379 |
// Caller: |
| 380 |
// [arg2] [arg1] [this] [ [argc] [callee] [descr] [raddr] ] <- esp |
| 381 |
|
| 382 |
// Frame prologue. |
| 383 |
// |
| 384 |
// NOTE: if this changes, fix the Baseline bailout code too! |
| 385 |
// See BaselineStackBuilder::calculatePrevFramePtr and |
| 386 |
// BaselineStackBuilder::buildRectifierFrame (in BaselineBailouts.cpp). |
| 387 |
masm.push(FramePointer); |
| 388 |
masm.movl(esp, FramePointer); // Save %esp. |
| 389 |
|
| 390 |
// Load argc. |
| 391 |
masm.loadNumActualArgs(FramePointer, esi); |
| 392 |
|
| 393 |
// Load the number of |undefined|s to push into %ecx. |
| 394 |
masm.loadPtr(Address(ebp, RectifierFrameLayout::offsetOfCalleeToken()), eax); |
| 395 |
masm.mov(eax, ecx); |
| 396 |
masm.andl(Imm32(CalleeTokenMask), ecx); |
| 397 |
masm.loadFunctionArgCount(ecx, ecx); |
| 398 |
|
| 399 |
// The frame pointer and its padding are pushed on the stack. |
| 400 |
// Including |this|, there are (|nformals| + 1) arguments to push to the |
| 401 |
// stack. Then we push a JitFrameLayout. We compute the padding expressed |
| 402 |
// in the number of extra |undefined| values to push on the stack. |
| 403 |
static_assert( |
| 404 |
sizeof(JitFrameLayout) % JitStackAlignment == 0, |
| 405 |
"No need to consider the JitFrameLayout for aligning the stack"); |
| 406 |
static_assert( |
| 407 |
JitStackAlignment % sizeof(Value) == 0, |
| 408 |
"Ensure that we can pad the stack by pushing extra UndefinedValue"); |
| 409 |
static_assert(IsPowerOfTwo(JitStackValueAlignment), |
| 410 |
"must have power of two for masm.andl to do its job"); |
| 411 |
|
| 412 |
masm.addl( |
| 413 |
Imm32(JitStackValueAlignment - 1 /* for padding */ + 1 /* for |this| */), |
| 414 |
ecx); |
| 415 |
|
| 416 |
// Account for newTarget, if necessary. |
| 417 |
static_assert( |
| 418 |
CalleeToken_FunctionConstructing == 1, |
| 419 |
"Ensure that we can use the constructing bit to count an extra push"); |
| 420 |
masm.mov(eax, edx); |
| 421 |
masm.andl(Imm32(CalleeToken_FunctionConstructing), edx); |
| 422 |
masm.addl(edx, ecx); |
| 423 |
|
| 424 |
masm.andl(Imm32(~(JitStackValueAlignment - 1)), ecx); |
| 425 |
masm.subl(esi, ecx); |
| 426 |
masm.subl(Imm32(1), ecx); // For |this|. |
| 427 |
|
| 428 |
// Copy the number of actual arguments into edx. |
| 429 |
masm.mov(esi, edx); |
| 430 |
|
| 431 |
masm.moveValue(UndefinedValue(), ValueOperand(ebx, edi)); |
| 432 |
|
| 433 |
// Caller: |
| 434 |
// [arg2] [arg1] [this] [ [argc] [callee] [descr] [raddr] ] |
| 435 |
// '-- #esi ---' |
| 436 |
// |
| 437 |
// Rectifier frame: |
| 438 |
// [ebp'] <- ebp [padding] <- esp [undef] [undef] [arg2] [arg1] [this] |
| 439 |
// '--- #ecx ----' '-- #esi ---' |
| 440 |
// |
| 441 |
// [ [argc] [callee] [descr] [raddr] ] |
| 442 |
|
| 443 |
// Push undefined. |
| 444 |
{ |
| 445 |
Label undefLoopTop; |
| 446 |
masm.bind(&undefLoopTop); |
| 447 |
|
| 448 |
masm.push(ebx); // type(undefined); |
| 449 |
masm.push(edi); // payload(undefined); |
| 450 |
masm.subl(Imm32(1), ecx); |
| 451 |
masm.j(Assembler::NonZero, &undefLoopTop); |
| 452 |
} |
| 453 |
|
| 454 |
// Get the topmost argument. |
| 455 |
BaseIndex b(FramePointer, esi, TimesEight, sizeof(RectifierFrameLayout)); |
| 456 |
masm.lea(Operand(b), ecx); |
| 457 |
|
| 458 |
// Push arguments, |nargs| + 1 times (to include |this|). |
| 459 |
masm.addl(Imm32(1), esi); |
| 460 |
{ |
| 461 |
Label copyLoopTop; |
| 462 |
|
| 463 |
masm.bind(©LoopTop); |
| 464 |
masm.push(Operand(ecx, sizeof(Value) / 2)); |
| 465 |
masm.push(Operand(ecx, 0x0)); |
| 466 |
masm.subl(Imm32(sizeof(Value)), ecx); |
| 467 |
masm.subl(Imm32(1), esi); |
| 468 |
masm.j(Assembler::NonZero, ©LoopTop); |
| 469 |
} |
| 470 |
|
| 471 |
{ |
| 472 |
Label notConstructing; |
| 473 |
|
| 474 |
masm.mov(eax, ebx); |
| 475 |
masm.branchTest32(Assembler::Zero, ebx, |
| 476 |
Imm32(CalleeToken_FunctionConstructing), |
| 477 |
¬Constructing); |
| 478 |
|
| 479 |
BaseValueIndex src(FramePointer, edx, |
| 480 |
sizeof(RectifierFrameLayout) + sizeof(Value)); |
| 481 |
|
| 482 |
masm.andl(Imm32(CalleeTokenMask), ebx); |
| 483 |
masm.loadFunctionArgCount(ebx, ebx); |
| 484 |
|
| 485 |
BaseValueIndex dst(esp, ebx, sizeof(Value)); |
| 486 |
|
| 487 |
ValueOperand newTarget(ecx, edi); |
| 488 |
|
| 489 |
masm.loadValue(src, newTarget); |
| 490 |
masm.storeValue(newTarget, dst); |
| 491 |
|
| 492 |
masm.bind(¬Constructing); |
| 493 |
} |
| 494 |
|
| 495 |
// Construct JitFrameLayout. |
| 496 |
masm.push(eax); // callee token |
| 497 |
masm.pushFrameDescriptorForJitCall(FrameType::Rectifier, edx, edx); |
| 498 |
|
| 499 |
// Call the target function. |
| 500 |
masm.andl(Imm32(CalleeTokenMask), eax); |
| 501 |
switch (kind) { |
| 502 |
case ArgumentsRectifierKind::Normal: |
| 503 |
masm.loadJitCodeRaw(eax, eax); |
| 504 |
argumentsRectifierReturnOffset_ = masm.callJitNoProfiler(eax); |
| 505 |
break; |
| 506 |
case ArgumentsRectifierKind::TrialInlining: |
| 507 |
Label noBaselineScript, done; |
| 508 |
masm.loadBaselineJitCodeRaw(eax, ebx, &noBaselineScript); |
| 509 |
masm.callJitNoProfiler(ebx); |
| 510 |
masm.jump(&done); |
| 511 |
|
| 512 |
// See BaselineCacheIRCompiler::emitCallInlinedFunction. |
| 513 |
masm.bind(&noBaselineScript); |
| 514 |
masm.loadJitCodeRaw(eax, eax); |
| 515 |
masm.callJitNoProfiler(eax); |
| 516 |
masm.bind(&done); |
| 517 |
break; |
| 518 |
} |
| 519 |
|
| 520 |
masm.mov(FramePointer, StackPointer); |
| 521 |
masm.pop(FramePointer); |
| 522 |
masm.ret(); |
| 523 |
} |
| 524 |
|
| 525 |
static void PushBailoutFrame(MacroAssembler& masm, Register spArg) { |
| 526 |
// Push registers such that we can access them from [base + code]. |
| 527 |
DumpAllRegs(masm); |
| 528 |
|
| 529 |
// The current stack pointer is the first argument to jit::Bailout. |
| 530 |
masm.movl(esp, spArg); |
| 531 |
} |
| 532 |
|
| 533 |
static void GenerateBailoutThunk(MacroAssembler& masm, Label* bailoutTail) { |
| 534 |
PushBailoutFrame(masm, eax); |
| 535 |
|
| 536 |
// Make space for Bailout's bailoutInfo outparam. |
| 537 |
masm.reserveStack(sizeof(void*)); |
| 538 |
masm.movl(esp, ebx); |
| 539 |
|
| 540 |
// Call the bailout function. |
| 541 |
using Fn = bool (*)(BailoutStack* sp, BaselineBailoutInfo** info); |
| 542 |
masm.setupUnalignedABICall(ecx); |
| 543 |
masm.passABIArg(eax); |
| 544 |
masm.passABIArg(ebx); |
| 545 |
masm.callWithABI<Fn, Bailout>(ABIType::General, |
| 546 |
CheckUnsafeCallWithABI::DontCheckOther); |
| 547 |
|
| 548 |
masm.pop(ecx); // Get the bailoutInfo outparam. |
| 549 |
|
| 550 |
// Remove both the bailout frame and the topmost Ion frame's stack. |
| 551 |
masm.moveToStackPtr(FramePointer); |
| 552 |
|
| 553 |
// Jump to shared bailout tail. The BailoutInfo pointer has to be in ecx. |
| 554 |
masm.jmp(bailoutTail); |
| 555 |
} |
| 556 |
|
| 557 |
void JitRuntime::generateBailoutHandler(MacroAssembler& masm, |
| 558 |
Label* bailoutTail) { |
| 559 |
AutoCreatedBy acb(masm, "JitRuntime::generateBailoutHandler"); |
| 560 |
|
| 561 |
bailoutHandlerOffset_ = startTrampolineCode(masm); |
| 562 |
|
| 563 |
GenerateBailoutThunk(masm, bailoutTail); |
| 564 |
} |
| 565 |
|
| 566 |
bool JitRuntime::generateVMWrapper(JSContext* cx, MacroAssembler& masm, |
| 567 |
VMFunctionId id, const VMFunctionData& f, |
| 568 |
DynFn nativeFun, uint32_t* wrapperOffset) { |
| 569 |
AutoCreatedBy acb(masm, "JitRuntime::generateVMWrapper"); |
| 570 |
|
| 571 |
*wrapperOffset = startTrampolineCode(masm); |
| 572 |
|
| 573 |
// Avoid conflicts with argument registers while discarding the result after |
| 574 |
// the function call. |
| 575 |
AllocatableGeneralRegisterSet regs(Register::Codes::WrapperMask); |
| 576 |
|
| 577 |
static_assert( |
| 578 |
(Register::Codes::VolatileMask & ~Register::Codes::WrapperMask) == 0, |
| 579 |
"Wrapper register set must be a superset of Volatile register set."); |
| 580 |
|
| 581 |
// The context is the first argument. |
| 582 |
Register cxreg = regs.takeAny(); |
| 583 |
|
| 584 |
// Stack is: |
| 585 |
// ... frame ... |
| 586 |
// +8 [args] |
| 587 |
// +4 descriptor |
| 588 |
// +0 returnAddress |
| 589 |
// |
| 590 |
// Push the frame pointer to finish the exit frame, then link it up. |
| 591 |
masm.Push(FramePointer); |
| 592 |
masm.moveStackPtrTo(FramePointer); |
| 593 |
masm.loadJSContext(cxreg); |
| 594 |
masm.enterExitFrame(cxreg, regs.getAny(), id); |
| 595 |
|
| 596 |
// Reserve space for the outparameter. |
| 597 |
masm.reserveVMFunctionOutParamSpace(f); |
| 598 |
|
| 599 |
masm.setupUnalignedABICallDontSaveRestoreSP(); |
| 600 |
masm.passABIArg(cxreg); |
| 601 |
|
| 602 |
size_t argDisp = ExitFrameLayout::Size(); |
| 603 |
|
| 604 |
// Copy arguments. |
| 605 |
for (uint32_t explicitArg = 0; explicitArg < f.explicitArgs; explicitArg++) { |
| 606 |
switch (f.argProperties(explicitArg)) { |
| 607 |
case VMFunctionData::WordByValue: |
| 608 |
masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::General); |
| 609 |
argDisp += sizeof(void*); |
| 610 |
break; |
| 611 |
case VMFunctionData::DoubleByValue: |
| 612 |
// We don't pass doubles in float registers on x86, so no need |
| 613 |
// to check for argPassedInFloatReg. |
| 614 |
masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::General); |
| 615 |
argDisp += sizeof(void*); |
| 616 |
masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::General); |
| 617 |
argDisp += sizeof(void*); |
| 618 |
break; |
| 619 |
case VMFunctionData::WordByRef: |
| 620 |
masm.passABIArg(MoveOperand(FramePointer, argDisp, |
| 621 |
MoveOperand::Kind::EffectiveAddress), |
| 622 |
ABIType::General); |
| 623 |
argDisp += sizeof(void*); |
| 624 |
break; |
| 625 |
case VMFunctionData::DoubleByRef: |
| 626 |
masm.passABIArg(MoveOperand(FramePointer, argDisp, |
| 627 |
MoveOperand::Kind::EffectiveAddress), |
| 628 |
ABIType::General); |
| 629 |
argDisp += 2 * sizeof(void*); |
| 630 |
break; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
// Copy the implicit outparam, if any. |
| 635 |
const int32_t outParamOffset = |
| 636 |
-int32_t(ExitFooterFrame::Size()) - f.sizeOfOutParamStackSlot(); |
| 637 |
if (f.outParam != Type_Void) { |
| 638 |
masm.passABIArg(MoveOperand(FramePointer, outParamOffset, |
| 639 |
MoveOperand::Kind::EffectiveAddress), |
| 640 |
ABIType::General); |
| 641 |
} |
| 642 |
|
| 643 |
masm.callWithABI(nativeFun, ABIType::General, |
| 644 |
CheckUnsafeCallWithABI::DontCheckHasExitFrame); |
| 645 |
|
| 646 |
// Test for failure. |
| 647 |
switch (f.failType()) { |
| 648 |
case Type_Cell: |
| 649 |
masm.branchTestPtr(Assembler::Zero, eax, eax, masm.failureLabel()); |
| 650 |
break; |
| 651 |
case Type_Bool: |
| 652 |
masm.testb(eax, eax); |
| 653 |
masm.j(Assembler::Zero, masm.failureLabel()); |
| 654 |
break; |
| 655 |
case Type_Void: |
| 656 |
break; |
| 657 |
default: |
| 658 |
MOZ_CRASH("unknown failure kind"); |
| 659 |
} |
| 660 |
|
| 661 |
// Load the outparam. |
| 662 |
masm.loadVMFunctionOutParam(f, Address(FramePointer, outParamOffset)); |
| 663 |
|
| 664 |
// Until C++ code is instrumented against Spectre, prevent speculative |
| 665 |
// execution from returning any private data. |
| 666 |
if (f.returnsData() && JitOptions.spectreJitToCxxCalls) { |
| 667 |
masm.speculationBarrier(); |
| 668 |
} |
| 669 |
|
| 670 |
// Pop frame and restore frame pointer. |
| 671 |
masm.moveToStackPtr(FramePointer); |
| 672 |
masm.pop(FramePointer); |
| 673 |
|
| 674 |
// Return. Subtract sizeof(void*) for the frame pointer. |
| 675 |
masm.retn(Imm32(sizeof(ExitFrameLayout) - sizeof(void*) + |
| 676 |
f.explicitStackSlots() * sizeof(void*) + |
| 677 |
f.extraValuesToPop * sizeof(Value))); |
| 678 |
|
| 679 |
return true; |
| 680 |
} |
| 681 |
|
| 682 |
uint32_t JitRuntime::generatePreBarrier(JSContext* cx, MacroAssembler& masm, |
| 683 |
MIRType type) { |
| 684 |
AutoCreatedBy acb(masm, "JitRuntime::generatePreBarrier"); |
| 685 |
|
| 686 |
uint32_t offset = startTrampolineCode(masm); |
| 687 |
|
| 688 |
static_assert(PreBarrierReg == edx); |
| 689 |
Register temp1 = eax; |
| 690 |
Register temp2 = ebx; |
| 691 |
Register temp3 = ecx; |
| 692 |
masm.push(temp1); |
| 693 |
masm.push(temp2); |
| 694 |
masm.push(temp3); |
| 695 |
|
| 696 |
Label noBarrier; |
| 697 |
masm.emitPreBarrierFastPath(cx->runtime(), type, temp1, temp2, temp3, |
| 698 |
&noBarrier); |
| 699 |
|
| 700 |
// Call into C++ to mark this GC thing. |
| 701 |
masm.pop(temp3); |
| 702 |
masm.pop(temp2); |
| 703 |
masm.pop(temp1); |
| 704 |
|
| 705 |
LiveRegisterSet save; |
| 706 |
save.set() = RegisterSet(GeneralRegisterSet(Registers::VolatileMask), |
| 707 |
FloatRegisterSet(FloatRegisters::VolatileMask)); |
| 708 |
masm.PushRegsInMask(save); |
| 709 |
|
| 710 |
masm.movl(ImmPtr(cx->runtime()), ecx); |
| 711 |
|
| 712 |
masm.setupUnalignedABICall(eax); |
| 713 |
masm.passABIArg(ecx); |
| 714 |
masm.passABIArg(edx); |
| 715 |
masm.callWithABI(JitPreWriteBarrier(type)); |
| 716 |
|
| 717 |
masm.PopRegsInMask(save); |
| 718 |
masm.ret(); |
| 719 |
|
| 720 |
masm.bind(&noBarrier); |
| 721 |
masm.pop(temp3); |
| 722 |
masm.pop(temp2); |
| 723 |
masm.pop(temp1); |
| 724 |
masm.ret(); |
| 725 |
|
| 726 |
return offset; |
| 727 |
} |
| 728 |
|
| 729 |
void JitRuntime::generateBailoutTailStub(MacroAssembler& masm, |
| 730 |
Label* bailoutTail) { |
| 731 |
AutoCreatedBy acb(masm, "JitRuntime::generateBailoutTailStub"); |
| 732 |
|
| 733 |
masm.bind(bailoutTail); |
| 734 |
masm.generateBailoutTail(edx, ecx); |
| 735 |
} |