Files
palemoon27/js/src/jit/BytecodeAnalysis.cpp
T
roytam1 351263e4a5 import changes from `dev' branch of rmottola/Arctic-Fox:
- missing bits of Bug 1207245 - part 1 - move RefCounted<T> (dccd21b327)
- Bug 1193583 - Fix out-of-date reftests. (r=jorendorff) (66ee3c50d5)
- Bug 1193583 - Fix misc XPConnect and devtools tests. (r=jorendorff) (9ed7a460d1)
- Bug 589199 - Make a global lexical scope and hook it up to JS entry points. (r=efaust) (1fde4fba9b)
- Bug 589199 - Parse and emit bytecode for global lexicals. (r=efaust) (86d168b94d)
- Bug 589199 - Support global lexicals in the interpreter. (r=efaust) (e5fa8ae995)
- Bug 589199 - Support global lexicals in Baseline. (r=jandem) (7b744015fe)
- Bug 589199 - Support global lexicals in Ion. (r=jandem) (446f05ce97)
- Bug 589199 - Fix eval static scope to play with the global lexical scope. (r=efaust) (4a7e4face1)
- Bug 589199 - Fix up the global lexical scope when merging off-thread compiled scripts. (r=bhackett) (30ff41230d)
- Bug 589199 - Fix jit-tests and js reftests. (r=efaust) (6171fa2c62)
- Bug 1202902 - Support non-syntactic extensible lexical scopes. (r=billm) (a2f553d464)
- No bug - Rename Definition::CONST to Definition::CONSTANT to avoid macro name collision on Windows. (r=Waldo) (74dfc52b28)
- Bug 589199 - Implement all-or-nothing redeclaration checks for global and eval scripts. (r=efaust) (33684af400)
- fix misspatch of 589199 (0dbeca332b)
- var->let and some misspatches (f2af7240b3)
- Bug 1212183 - Fix DOM getter optimizations in the JITs. (r=jandem) (df74d3e88d)
- Bug 1212605 - Emit global name conflicts check for Ion scripts regardless of scope chain usage. (r=efaust) (a370f28465)
- Bug 1213552 - Fix typo in using TI to guard against introducing shadowing global lexical bindings. (r=efaust) (188f583410)
- Bug 1213552 - Followup: add test. (b0ca61190b)
- let-var + XP backport (40abaf773c)
- Bug 1188290 - Remove an incomplete assertion about store buffer state; r=jandem (7344dd4819)
- Bug 1209754 - Assert that all post-barriers happen on the main thread; r=jonco (9a7431aa6d)
- comment of 854037 (c026b72e69)
- fix definitions (9b140aaafb)
2022-08-19 09:49:49 +08:00

227 lines
7.5 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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/BytecodeAnalysis.h"
#include "jsopcode.h"
#include "jit/JitSpewer.h"
#include "jsopcodeinlines.h"
#include "jsscriptinlines.h"
using namespace js;
using namespace js::jit;
BytecodeAnalysis::BytecodeAnalysis(TempAllocator& alloc, JSScript* script)
: script_(script),
infos_(alloc),
usesScopeChain_(false),
hasTryFinally_(false),
hasSetArg_(false)
{
}
// Bytecode range containing only catch or finally code.
struct CatchFinallyRange
{
uint32_t start; // Inclusive.
uint32_t end; // Exclusive.
CatchFinallyRange(uint32_t start, uint32_t end)
: start(start), end(end)
{
MOZ_ASSERT(end > start);
}
bool contains(uint32_t offset) const {
return start <= offset && offset < end;
}
};
bool
BytecodeAnalysis::init(TempAllocator& alloc, GSNCache& gsn)
{
if (!infos_.growByUninitialized(script_->length()))
return false;
// Initialize the scope chain slot if either the function needs a CallObject
// or the script uses the scope chain. The latter case is handled below.
usesScopeChain_ = script_->module() ||
(script_->functionDelazifying() &&
script_->functionDelazifying()->needsCallObject());
MOZ_ASSERT_IF(script_->hasAnyAliasedBindings(), usesScopeChain_);
jsbytecode* end = script_->codeEnd();
// Clear all BytecodeInfo.
mozilla::PodZero(infos_.begin(), infos_.length());
infos_[0].init(/*stackDepth=*/0);
Vector<CatchFinallyRange, 0, JitAllocPolicy> catchFinallyRanges(alloc);
jsbytecode* nextpc;
for (jsbytecode* pc = script_->code(); pc < end; pc = nextpc) {
JSOp op = JSOp(*pc);
nextpc = pc + GetBytecodeLength(pc);
unsigned offset = script_->pcToOffset(pc);
JitSpew(JitSpew_BaselineOp, "Analyzing op @ %d (end=%d): %s",
int(script_->pcToOffset(pc)), int(script_->length()), js_CodeName[op]);
// If this bytecode info has not yet been initialized, it's not reachable.
if (!infos_[offset].initialized)
continue;
unsigned stackDepth = infos_[offset].stackDepth;
#ifdef DEBUG
for (jsbytecode* chkpc = pc + 1; chkpc < (pc + GetBytecodeLength(pc)); chkpc++)
MOZ_ASSERT(!infos_[script_->pcToOffset(chkpc)].initialized);
#endif
unsigned nuses = GetUseCount(script_, offset);
unsigned ndefs = GetDefCount(script_, offset);
MOZ_ASSERT(stackDepth >= nuses);
stackDepth -= nuses;
stackDepth += ndefs;
// If stack depth exceeds max allowed by analysis, fail fast.
MOZ_ASSERT(stackDepth <= BytecodeInfo::MAX_STACK_DEPTH);
switch (op) {
case JSOP_TABLESWITCH: {
unsigned defaultOffset = offset + GET_JUMP_OFFSET(pc);
jsbytecode* pc2 = pc + JUMP_OFFSET_LEN;
int32_t low = GET_JUMP_OFFSET(pc2);
pc2 += JUMP_OFFSET_LEN;
int32_t high = GET_JUMP_OFFSET(pc2);
pc2 += JUMP_OFFSET_LEN;
infos_[defaultOffset].init(stackDepth);
infos_[defaultOffset].jumpTarget = true;
for (int32_t i = low; i <= high; i++) {
unsigned targetOffset = offset + GET_JUMP_OFFSET(pc2);
if (targetOffset != offset) {
infos_[targetOffset].init(stackDepth);
infos_[targetOffset].jumpTarget = true;
}
pc2 += JUMP_OFFSET_LEN;
}
break;
}
case JSOP_TRY: {
JSTryNote* tn = script_->trynotes()->vector;
JSTryNote* tnlimit = tn + script_->trynotes()->length;
for (; tn < tnlimit; tn++) {
unsigned startOffset = script_->mainOffset() + tn->start;
if (startOffset == offset + 1) {
unsigned catchOffset = startOffset + tn->length;
if (tn->kind != JSTRY_FOR_IN) {
infos_[catchOffset].init(stackDepth);
infos_[catchOffset].jumpTarget = true;
}
}
}
// Get the pc of the last instruction in the try block. It's a JSOP_GOTO to
// jump over the catch/finally blocks.
jssrcnote* sn = GetSrcNote(gsn, script_, pc);
MOZ_ASSERT(SN_TYPE(sn) == SRC_TRY);
jsbytecode* endOfTry = pc + GetSrcNoteOffset(sn, 0);
MOZ_ASSERT(JSOp(*endOfTry) == JSOP_GOTO);
jsbytecode* afterTry = endOfTry + GET_JUMP_OFFSET(endOfTry);
MOZ_ASSERT(afterTry > endOfTry);
// Pop CatchFinallyRanges that are no longer needed.
while (!catchFinallyRanges.empty() && catchFinallyRanges.back().end <= offset)
catchFinallyRanges.popBack();
CatchFinallyRange range(script_->pcToOffset(endOfTry), script_->pcToOffset(afterTry));
if (!catchFinallyRanges.append(range))
return false;
break;
}
case JSOP_LOOPENTRY:
for (size_t i = 0; i < catchFinallyRanges.length(); i++) {
if (catchFinallyRanges[i].contains(offset))
infos_[offset].loopEntryInCatchOrFinally = true;
}
break;
case JSOP_GETNAME:
case JSOP_BINDNAME:
case JSOP_SETNAME:
case JSOP_STRICTSETNAME:
case JSOP_DELNAME:
case JSOP_GETALIASEDVAR:
case JSOP_SETALIASEDVAR:
case JSOP_LAMBDA:
case JSOP_LAMBDA_ARROW:
case JSOP_DEFFUN:
case JSOP_DEFVAR:
usesScopeChain_ = true;
break;
case JSOP_GETGNAME:
case JSOP_SETGNAME:
case JSOP_STRICTSETGNAME:
if (script_->hasNonSyntacticScope())
usesScopeChain_ = true;
break;
case JSOP_FINALLY:
hasTryFinally_ = true;
break;
case JSOP_SETARG:
hasSetArg_ = true;
break;
default:
break;
}
bool jump = IsJumpOpcode(op);
if (jump) {
// Case instructions do not push the lvalue back when branching.
unsigned newStackDepth = stackDepth;
if (op == JSOP_CASE)
newStackDepth--;
unsigned targetOffset = offset + GET_JUMP_OFFSET(pc);
// If this is a a backedge to an un-analyzed segment, analyze from there.
bool jumpBack = (targetOffset < offset) && !infos_[targetOffset].initialized;
infos_[targetOffset].init(newStackDepth);
infos_[targetOffset].jumpTarget = true;
if (jumpBack)
nextpc = script_->offsetToPC(targetOffset);
}
// Handle any fallthrough from this opcode.
if (BytecodeFallsThrough(op)) {
jsbytecode* fallthrough = pc + GetBytecodeLength(pc);
MOZ_ASSERT(fallthrough < end);
unsigned fallthroughOffset = script_->pcToOffset(fallthrough);
infos_[fallthroughOffset].init(stackDepth);
// Treat the fallthrough of a branch instruction as a jump target.
if (jump)
infos_[fallthroughOffset].jumpTarget = true;
}
}
return true;
}