DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/BaselineFrame-inl.h"

#include "debugger/DebugAPI.h"
#include "jit/BaselineJIT.h"
#include "jit/Ion.h"
#include "vm/EnvironmentObject.h"

#include "jit/JitFrames-inl.h"
#include "vm/Stack-inl.h"

using namespace js;
using namespace js::jit;

static void TraceLocals(BaselineFrame* frame, JSTracer* trc, unsigned start,
                        unsigned end) {
  if (start < end) {
    // Stack grows down.
    Value* last = frame->valueSlot(end - 1);
    TraceRootRange(trc, end - start, last, "baseline-stack");
  }
}

void BaselineFrame::trace(JSTracer* trc, const JSJitFrameIter& frameIterator) {
  replaceCalleeToken(TraceCalleeToken(trc, calleeToken()));

  // Trace |this|, actual and formal args.
  if (isFunctionFrame()) {
    TraceRoot(trc, &thisArgument(), "baseline-this");

    unsigned numArgs = js::Max(numActualArgs(), numFormalArgs());
    TraceRootRange(trc, numArgs + isConstructing(), argv(), "baseline-args");
  }

  // Trace environment chain, if it exists.
  if (envChain_) {
    TraceRoot(trc, &envChain_, "baseline-envchain");
  }

  // Trace return value.
  if (hasReturnValue()) {
    TraceRoot(trc, returnValue().address(), "baseline-rval");
  }

  if (isEvalFrame() && script()->isDirectEvalInFunction()) {
    TraceRoot(trc, evalNewTargetAddress(), "baseline-evalNewTarget");
  }

  if (hasArgsObj()) {
    TraceRoot(trc, &argsObj_, "baseline-args-obj");
  }

  if (runningInInterpreter()) {
    TraceRoot(trc, &interpreterScript_, "baseline-interpreterScript");
  }

  // Trace locals and stack values.
  JSScript* script = this->script();
  size_t nfixed = script->nfixed();
  jsbytecode* pc;
  frameIterator.baselineScriptAndPc(nullptr, &pc);
  size_t nlivefixed = script->calculateLiveFixed(pc);

  // NB: It is possible that numValueSlots() could be zero, even if nfixed is
  // nonzero.  This is the case if the function has an early stack check.
  if (numValueSlots() == 0) {
    return;
  }

  MOZ_ASSERT(nfixed <= numValueSlots());

  if (nfixed == nlivefixed) {
    // All locals are live.
    TraceLocals(this, trc, 0, numValueSlots());
  } else {
    // Trace operand stack.
    TraceLocals(this, trc, nfixed, numValueSlots());

    // Clear dead block-scoped locals.
    while (nfixed > nlivefixed) {
      unaliasedLocal(--nfixed).setUndefined();
    }

    // Trace live locals.
    TraceLocals(this, trc, 0, nlivefixed);
  }

  if (auto* debugEnvs = script->realm()->debugEnvs()) {
    debugEnvs->traceLiveFrame(trc, this);
  }
}

bool BaselineFrame::isNonGlobalEvalFrame() const {
  return isEvalFrame() &&
         script()->enclosingScope()->as<EvalScope>().isNonGlobal();
}

bool BaselineFrame::initFunctionEnvironmentObjects(JSContext* cx) {
  return js::InitFunctionEnvironmentObjects(cx, this);
}

bool BaselineFrame::pushVarEnvironment(JSContext* cx, HandleScope scope) {
  return js::PushVarEnvironmentObject(cx, scope, this);
}

void BaselineFrame::setInterpreterFields(JSScript* script, jsbytecode* pc) {
  uint32_t pcOffset = script->pcToOffset(pc);
  JitScript* jitScript = script->jitScript();
  interpreterScript_ = script;
  interpreterPC_ = pc;
  interpreterICEntry_ = jitScript->interpreterICEntryFromPCOffset(pcOffset);
}

void BaselineFrame::setInterpreterFieldsForPrologueBailout(JSScript* script) {
  JitScript* jitScript = script->jitScript();
  interpreterScript_ = script;
  interpreterPC_ = script->code();
  if (jitScript->numICEntries() > 0) {
    interpreterICEntry_ = &jitScript->icEntry(0);
  } else {
    // If the script does not have any ICEntries (possible for non-function
    // scripts) the interpreterICEntry_ field won't be used. Just set it to
    // nullptr.
    interpreterICEntry_ = nullptr;
  }
}

bool BaselineFrame::initForOsr(InterpreterFrame* fp, uint32_t numStackValues) {
  mozilla::PodZero(this);

  envChain_ = fp->environmentChain();

  if (fp->hasInitialEnvironmentUnchecked()) {
    flags_ |= BaselineFrame::HAS_INITIAL_ENV;
  }

  if (fp->script()->needsArgsObj() && fp->hasArgsObj()) {
    flags_ |= BaselineFrame::HAS_ARGS_OBJ;
    argsObj_ = &fp->argsObj();
  }

  if (fp->hasReturnValue()) {
    setReturnValue(fp->returnValue());
  }

  JSContext* cx =
      fp->script()->runtimeFromMainThread()->mainContextFromOwnThread();

  Activation* interpActivation = cx->activation()->prev();
  jsbytecode* pc = interpActivation->asInterpreter()->regs().pc;
  MOZ_ASSERT(fp->script()->containsPC(pc));

  // We are doing OSR into the Baseline Interpreter. We can get the pc from the
  // C++ interpreter's activation, we just have to skip the JitActivation.
  flags_ |= BaselineFrame::RUNNING_IN_INTERPRETER;
  setInterpreterFields(pc);

  frameSize_ = BaselineFrame::FramePointerOffset + BaselineFrame::Size() +
               numStackValues * sizeof(Value);

  MOZ_ASSERT(numValueSlots() == numStackValues);

  for (uint32_t i = 0; i < numStackValues; i++) {
    *valueSlot(i) = fp->slots()[i];
  }

  if (fp->isDebuggee()) {
    // For debuggee frames, update any Debugger.Frame objects for the
    // InterpreterFrame to point to the BaselineFrame.
    if (!DebugAPI::handleBaselineOsr(cx, fp, this)) {
      return false;
    }
    setIsDebuggee();
  }

  return true;
}