| 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/BaselineFrame-inl.h" |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
|
| 11 |
#include "debugger/DebugAPI.h" |
| 12 |
#include "vm/EnvironmentObject.h" |
| 13 |
#include "vm/JSContext.h" |
| 14 |
|
| 15 |
#include "jit/JSJitFrameIter-inl.h" |
| 16 |
#include "vm/Stack-inl.h" |
| 17 |
|
| 18 |
using namespace js; |
| 19 |
using namespace js::jit; |
| 20 |
|
| 21 |
static void TraceLocals(BaselineFrame* frame, JSTracer* trc, unsigned start, |
| 22 |
unsigned end) { |
| 23 |
if (start < end) { |
| 24 |
// Stack grows down. |
| 25 |
Value* last = frame->valueSlot(end - 1); |
| 26 |
TraceRootRange(trc, end - start, last, "baseline-stack"); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
void BaselineFrame::trace(JSTracer* trc, const JSJitFrameIter& frameIterator) { |
| 31 |
replaceCalleeToken(TraceCalleeToken(trc, calleeToken())); |
| 32 |
|
| 33 |
// Trace |this|, actual and formal args. |
| 34 |
if (isFunctionFrame()) { |
| 35 |
TraceRoot(trc, &thisArgument(), "baseline-this"); |
| 36 |
|
| 37 |
unsigned numArgs = std::max(numActualArgs(), numFormalArgs()); |
| 38 |
TraceRootRange(trc, numArgs + isConstructing(), argv(), "baseline-args"); |
| 39 |
} |
| 40 |
|
| 41 |
// Trace environment chain, if it exists. |
| 42 |
if (envChain_) { |
| 43 |
TraceRoot(trc, &envChain_, "baseline-envchain"); |
| 44 |
} |
| 45 |
|
| 46 |
// Trace return value. |
| 47 |
if (hasReturnValue()) { |
| 48 |
TraceRoot(trc, returnValue().address(), "baseline-rval"); |
| 49 |
} |
| 50 |
|
| 51 |
if (hasArgsObj()) { |
| 52 |
TraceRoot(trc, &argsObj_, "baseline-args-obj"); |
| 53 |
} |
| 54 |
|
| 55 |
if (runningInInterpreter() || isRealmIndependent()) { |
| 56 |
TraceRoot(trc, &interpreterScript_, "baseline-interpreterScript"); |
| 57 |
} |
| 58 |
|
| 59 |
// Trace locals and stack values. |
| 60 |
JSScript* script = this->script(); |
| 61 |
size_t nfixed = script->nfixed(); |
| 62 |
jsbytecode* pc; |
| 63 |
frameIterator.baselineScriptAndPc(nullptr, &pc); |
| 64 |
size_t nlivefixed = script->calculateLiveFixed(pc); |
| 65 |
|
| 66 |
uint32_t numValueSlots = frameIterator.baselineFrameNumValueSlots(); |
| 67 |
|
| 68 |
// NB: It is possible that numValueSlots could be zero, even if nfixed is |
| 69 |
// nonzero. This is the case when we're initializing the environment chain or |
| 70 |
// failed the prologue stack check. |
| 71 |
if (numValueSlots > 0) { |
| 72 |
MOZ_ASSERT(nfixed <= numValueSlots); |
| 73 |
|
| 74 |
if (nfixed == nlivefixed) { |
| 75 |
// All locals are live. |
| 76 |
TraceLocals(this, trc, 0, numValueSlots); |
| 77 |
} else { |
| 78 |
// Trace operand stack. |
| 79 |
TraceLocals(this, trc, nfixed, numValueSlots); |
| 80 |
|
| 81 |
// Clear dead block-scoped locals. |
| 82 |
while (nfixed > nlivefixed) { |
| 83 |
unaliasedLocal(--nfixed).setUndefined(); |
| 84 |
} |
| 85 |
|
| 86 |
// Trace live locals. |
| 87 |
TraceLocals(this, trc, 0, nlivefixed); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (auto* debugEnvs = script->realm()->debugEnvs()) { |
| 92 |
debugEnvs->traceLiveFrame(trc, this); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
bool BaselineFrame::uninlineIsProfilerSamplingEnabled(JSContext* cx) { |
| 97 |
return cx->isProfilerSamplingEnabled(); |
| 98 |
} |
| 99 |
|
| 100 |
bool BaselineFrame::initFunctionEnvironmentObjects(JSContext* cx) { |
| 101 |
return js::InitFunctionEnvironmentObjects(cx, this); |
| 102 |
} |
| 103 |
|
| 104 |
bool BaselineFrame::pushVarEnvironment(JSContext* cx, Handle<Scope*> scope) { |
| 105 |
return js::PushVarEnvironmentObject(cx, scope, this); |
| 106 |
} |
| 107 |
|
| 108 |
void BaselineFrame::setInterpreterFields(JSScript* script, jsbytecode* pc) { |
| 109 |
uint32_t pcOffset = script->pcToOffset(pc); |
| 110 |
interpreterScript_ = script; |
| 111 |
interpreterPC_ = pc; |
| 112 |
MOZ_ASSERT(icScript_); |
| 113 |
interpreterICEntry_ = icScript_->interpreterICEntryFromPCOffset(pcOffset); |
| 114 |
} |
| 115 |
|
| 116 |
void BaselineFrame::setInterpreterFieldsForPrologue(JSScript* script) { |
| 117 |
interpreterScript_ = script; |
| 118 |
interpreterPC_ = script->code(); |
| 119 |
if (icScript_->numICEntries() > 0) { |
| 120 |
interpreterICEntry_ = &icScript_->icEntry(0); |
| 121 |
} else { |
| 122 |
// If the script does not have any ICEntries (possible for non-function |
| 123 |
// scripts) the interpreterICEntry_ field won't be used. Just set it to |
| 124 |
// nullptr. |
| 125 |
interpreterICEntry_ = nullptr; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
bool BaselineFrame::initForOsr(InterpreterFrame* fp, uint32_t numStackValues) { |
| 130 |
mozilla::PodZero(this); |
| 131 |
|
| 132 |
envChain_ = fp->environmentChain(); |
| 133 |
|
| 134 |
if (fp->hasInitialEnvironmentUnchecked()) { |
| 135 |
flags_ |= BaselineFrame::HAS_INITIAL_ENV; |
| 136 |
} |
| 137 |
|
| 138 |
if (fp->script()->needsArgsObj() && fp->hasArgsObj()) { |
| 139 |
flags_ |= BaselineFrame::HAS_ARGS_OBJ; |
| 140 |
argsObj_ = &fp->argsObj(); |
| 141 |
} |
| 142 |
|
| 143 |
if (fp->hasReturnValue()) { |
| 144 |
setReturnValue(fp->returnValue()); |
| 145 |
} |
| 146 |
|
| 147 |
icScript_ = fp->script()->jitScript()->icScript(); |
| 148 |
|
| 149 |
JSContext* cx = |
| 150 |
fp->script()->runtimeFromMainThread()->mainContextFromOwnThread(); |
| 151 |
|
| 152 |
Activation* interpActivation = cx->activation()->prev(); |
| 153 |
jsbytecode* pc = interpActivation->asInterpreter()->regs().pc; |
| 154 |
MOZ_ASSERT(fp->script()->containsPC(pc)); |
| 155 |
|
| 156 |
// We are doing OSR into the Baseline Interpreter. We can get the pc from the |
| 157 |
// C++ interpreter's activation, we just have to skip the JitActivation. |
| 158 |
flags_ |= BaselineFrame::RUNNING_IN_INTERPRETER; |
| 159 |
setInterpreterFields(pc); |
| 160 |
|
| 161 |
#ifdef DEBUG |
| 162 |
debugFrameSize_ = frameSizeForNumValueSlots(numStackValues); |
| 163 |
MOZ_ASSERT(debugNumValueSlots() == numStackValues); |
| 164 |
#endif |
| 165 |
|
| 166 |
for (uint32_t i = 0; i < numStackValues; i++) { |
| 167 |
*valueSlot(i) = fp->slots()[i]; |
| 168 |
} |
| 169 |
|
| 170 |
// The InterpreterFrame won't be used anymore, but a GC might still trace it. |
| 171 |
// Clear its stack slots to avoid keeping GC things alive. |
| 172 |
std::fill_n(fp->slots(), numStackValues, UndefinedValue()); |
| 173 |
|
| 174 |
if (fp->isDebuggee()) { |
| 175 |
// For debuggee frames, update any Debugger.Frame objects for the |
| 176 |
// InterpreterFrame to point to the BaselineFrame. |
| 177 |
if (!DebugAPI::handleBaselineOsr(cx, fp, this)) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
setIsDebuggee(); |
| 181 |
} |
| 182 |
|
| 183 |
return true; |
| 184 |
} |