| 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 |
/* JS bytecode generation. */ |
| 8 |
|
| 9 |
#ifndef frontend_BytecodeEmitter_h |
| 10 |
#define frontend_BytecodeEmitter_h |
| 11 |
|
| 12 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 13 |
#include "mozilla/Attributes.h" // MOZ_STACK_CLASS, MOZ_ALWAYS_INLINE, MOZ_NEVER_INLINE, MOZ_RAII |
| 14 |
#include "mozilla/Maybe.h" // mozilla::Maybe, mozilla::Some |
| 15 |
#include "mozilla/Saturate.h" // mozilla::SaturateUint8 |
| 16 |
#include "mozilla/Span.h" // mozilla::Span |
| 17 |
|
| 18 |
#include <stddef.h> // ptrdiff_t |
| 19 |
#include <stdint.h> // uint16_t, uint32_t |
| 20 |
|
| 21 |
#include "frontend/AbstractScopePtr.h" // ScopeIndex |
| 22 |
#include "frontend/BytecodeSection.h" // BytecodeSection, PerScriptData, GCThingList |
| 23 |
#include "frontend/DestructuringFlavor.h" // DestructuringFlavor |
| 24 |
#include "frontend/EitherParser.h" // EitherParser |
| 25 |
#include "frontend/IteratorKind.h" // IteratorKind |
| 26 |
#include "frontend/JumpList.h" // JumpList, JumpTarget |
| 27 |
#include "frontend/NameAnalysisTypes.h" // NameLocation |
| 28 |
#include "frontend/NameCollections.h" // AtomIndexMap |
| 29 |
#include "frontend/ParseNode.h" // ParseNode and subclasses |
| 30 |
#include "frontend/Parser.h" // Parser, PropListType |
| 31 |
#include "frontend/ParserAtom.h" // TaggedParserAtomIndex, ParserAtom |
| 32 |
#include "frontend/ScriptIndex.h" // ScriptIndex |
| 33 |
#include "frontend/SelfHostedIter.h" // SelfHostedIter |
| 34 |
#include "frontend/SourceNotes.h" // SrcNoteType |
| 35 |
#include "frontend/ValueUsage.h" // ValueUsage |
| 36 |
#include "js/AllocPolicy.h" // ReportOutOfMemory |
| 37 |
#include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin |
| 38 |
#include "js/TypeDecls.h" // jsbytecode |
| 39 |
#include "vm/BuiltinObjectKind.h" // BuiltinObjectKind |
| 40 |
#include "vm/CheckIsObjectKind.h" // CheckIsObjectKind |
| 41 |
#include "vm/CompletionKind.h" // CompletionKind |
| 42 |
#include "vm/FunctionPrefixKind.h" // FunctionPrefixKind |
| 43 |
#include "vm/GeneratorResumeKind.h" // GeneratorResumeKind |
| 44 |
#include "vm/Opcodes.h" // JSOp |
| 45 |
#include "vm/SharedStencil.h" // GCThingIndex, MemberInitializers |
| 46 |
#include "vm/StencilEnums.h" // TryNoteKind |
| 47 |
#include "vm/ThrowMsgKind.h" // ThrowMsgKind, ThrowCondition |
| 48 |
|
| 49 |
namespace js { |
| 50 |
|
| 51 |
class FrontendContext; |
| 52 |
|
| 53 |
namespace frontend { |
| 54 |
|
| 55 |
class BytecodeOffset; |
| 56 |
class CallOrNewEmitter; |
| 57 |
class ClassEmitter; |
| 58 |
class DestructuringLHSRef; |
| 59 |
class ElemOpEmitter; |
| 60 |
class EmitterScope; |
| 61 |
class ErrorReporter; |
| 62 |
class FullParseHandler; |
| 63 |
class NestableControl; |
| 64 |
class PrivateOpEmitter; |
| 65 |
class PropertyEmitter; |
| 66 |
class PropOpEmitter; |
| 67 |
class OptionalEmitter; |
| 68 |
class SharedContext; |
| 69 |
class TDZCheckCache; |
| 70 |
class TryEmitter; |
| 71 |
|
| 72 |
struct TokenPos; |
| 73 |
|
| 74 |
enum class ValueIsOnStack { Yes, No }; |
| 75 |
|
| 76 |
// [SMDOC] Bytecode emission |
| 77 |
// |
| 78 |
// Bytecode emitter class and helper classes for generating bytecode and related |
| 79 |
// stencil data from AST generated by JS parser. |
| 80 |
// |
| 81 |
// |
| 82 |
// BytecodeEmitter |
| 83 |
// --------------- |
| 84 |
// |
| 85 |
// BytecodeEmitter receives an AST, and utilizes helper classes to generate the |
| 86 |
// bytecode sequence, and related stencil data. |
| 87 |
// |
| 88 |
// BytecodeEmitter can be nested, in order to emit inner non-lazy function |
| 89 |
// scripts. |
| 90 |
// |
| 91 |
// |
| 92 |
// Bytecode structures |
| 93 |
// ------------------- |
| 94 |
// |
| 95 |
// While bytecode is being emitted, it is separated into 2 parts, the prologue |
| 96 |
// and the main part. The prologue part contains instantiation of the declared |
| 97 |
// variables, functions, and special names in function. The main part contains |
| 98 |
// the remaining part of the bytecode. |
| 99 |
// |
| 100 |
// The generated bytecode is stored into the following 2 classes, before |
| 101 |
// converting them into stencil data (See ImmutableScriptData and |
| 102 |
// BytecodeEmitter::createImmutableScriptData): |
| 103 |
// |
| 104 |
// * BytecodeSection |
| 105 |
// * PerScriptData |
| 106 |
// |
| 107 |
// BytecodeSection stores the bytecode sequence and data directly associated |
| 108 |
// with opcode or index inside the bytecode sequence. |
| 109 |
// |
| 110 |
// PerScriptData contains data referred from the bytecode, that is mostly the |
| 111 |
// list of GC things. |
| 112 |
// |
| 113 |
// |
| 114 |
// Bindings |
| 115 |
// -------- |
| 116 |
// |
| 117 |
// # Scope and bindings |
| 118 |
// |
| 119 |
// When emitting AST node that's associated with a given scope, EmitterScope is |
| 120 |
// allocated to store/cache the bindings information. |
| 121 |
// |
| 122 |
// This information is used when emitting an opcode that accesses bindings, to |
| 123 |
// determine where the binding is stored, and how the binding should be |
| 124 |
// accessed, including which opcode to use and what operand to use for it. |
| 125 |
// |
| 126 |
// |
| 127 |
// # Temporal Dead Zone (TDZ) check cache |
| 128 |
// |
| 129 |
// The spec requires TDZ check for all lexical variable access, but emitting |
| 130 |
// TDZ check for all operation increases the bytecode size and affects the |
| 131 |
// performance. TDZCheckCache is a cache to optimize away unnecessary TDZ check |
| 132 |
// operations. |
| 133 |
// |
| 134 |
// See comments for TDZCheckCache for more details. |
| 135 |
// |
| 136 |
// |
| 137 |
// Control structures |
| 138 |
// ------------------ |
| 139 |
// |
| 140 |
// # Jump list |
| 141 |
// |
| 142 |
// When emitting jump-related bytecode (if-else, break/continue, try-catch), |
| 143 |
// forward jump is tracked by JumpList class, in order to patch the jump |
| 144 |
// after the jump target is emitted. |
| 145 |
// |
| 146 |
// See the comment above JumpList class for mode details. |
| 147 |
// |
| 148 |
// |
| 149 |
// # Loop and label |
| 150 |
// |
| 151 |
// Control structure related to break/continue is handled by NestableControl and |
| 152 |
// its subclasses. Those classes handle jump with labelled and un-labelled |
| 153 |
// break/continue, stack balancing around them, TDZ check cache for the |
| 154 |
// loop's basic block, and association between the control and the scope. |
| 155 |
// |
| 156 |
// |
| 157 |
// Emitter helpers |
| 158 |
// --------------- |
| 159 |
// |
| 160 |
// Bytecode sequence or structure specific to certain syntax (e.g. if, for, try) |
| 161 |
// are handled by emitter helper classes. |
| 162 |
// |
| 163 |
// Each emitter helper is defined in *Emitter.{cpp,h} in this directory. |
| 164 |
// |
| 165 |
// Emitter helpers should meet the following requirements: |
| 166 |
// * helper classes should be ParseNode-agnostic |
| 167 |
// * helper classes shouldn't contain `JS::Rooted` field, given they can be |
| 168 |
// held in `mozilla::Maybe` in the consumer or other helper classes |
| 169 |
// * instantiation (ctor/dtor) of the emitter helper class shouldn't |
| 170 |
// modify BytecodeEmitter, except for nestable controls |
| 171 |
// * instantiation (ctor/dtor) of the emitter helper class shouldn't |
| 172 |
// read BytecodeEmitter field that can change before the first method call. |
| 173 |
// Such data should be explicitly passed as parameter, or be accessed inside |
| 174 |
// the method |
| 175 |
// * methods that emits bytecode should be named `emit*` or `prepareFor*` |
| 176 |
// * methods and their names shouldn't require the consumer knowing the |
| 177 |
// details of the bytecode sequence/structure that the helper emits |
| 178 |
// * implicit branch or scope/control handling should be hidden from the |
| 179 |
// consumer |
| 180 |
// * If there are multiple operations between bytecode that the consumer |
| 181 |
// emits, they should be wrapped into single `emit*` or `prepareFor*` |
| 182 |
// method |
| 183 |
// e.g. |
| 184 |
// // Bad! |
| 185 |
// helper.emitJumpAroundA(); |
| 186 |
// helper.allocateScopeForA(); |
| 187 |
// ... // emit bytecode for A here |
| 188 |
// helper.deallocateScopeForA(); |
| 189 |
// helper.emitJumpAroundB(); |
| 190 |
// helper.allocateScopeForB(); |
| 191 |
// ... // emit bytecode for B here |
| 192 |
// helper.deallocateScopeForB(); |
| 193 |
// helper.emitJumpTarget(); |
| 194 |
// |
| 195 |
// // Good! |
| 196 |
// helper.prepareForA(); |
| 197 |
// ... // emit bytecode for A here |
| 198 |
// helper.prepareForB(); |
| 199 |
// ... // emit bytecode for B here |
| 200 |
// helper.emitEnd(); |
| 201 |
// * helper classes should track state transition and assert it in each |
| 202 |
// method call, to avoid misuse |
| 203 |
// * it's recommended to defer receiving parameter until the parameter value |
| 204 |
// is actually used in the method, instead of receiving and storing them |
| 205 |
// into instance fields |
| 206 |
// |
| 207 |
// See comment block above each helper class for more details and example usage. |
| 208 |
|
| 209 |
struct MOZ_STACK_CLASS BytecodeEmitter { |
| 210 |
// Context shared between parsing and bytecode generation. |
| 211 |
SharedContext* const sc = nullptr; |
| 212 |
|
| 213 |
FrontendContext* const fc = nullptr; |
| 214 |
|
| 215 |
// Enclosing function or global context. |
| 216 |
BytecodeEmitter* const parent = nullptr; |
| 217 |
|
| 218 |
BytecodeSection bytecodeSection_; |
| 219 |
|
| 220 |
static constexpr unsigned LastSrcNoteIsNotLineOnly = unsigned(-1); |
| 221 |
|
| 222 |
unsigned lastLineOnlySrcNoteIndex = LastSrcNoteIsNotLineOnly; |
| 223 |
|
| 224 |
public: |
| 225 |
BytecodeSection& bytecodeSection() { return bytecodeSection_; } |
| 226 |
const BytecodeSection& bytecodeSection() const { return bytecodeSection_; } |
| 227 |
|
| 228 |
private: |
| 229 |
PerScriptData perScriptData_; |
| 230 |
|
| 231 |
public: |
| 232 |
PerScriptData& perScriptData() { return perScriptData_; } |
| 233 |
const PerScriptData& perScriptData() const { return perScriptData_; } |
| 234 |
|
| 235 |
private: |
| 236 |
// switchToMain sets this to the bytecode offset of the main section. |
| 237 |
mozilla::Maybe<uint32_t> mainOffset_ = {}; |
| 238 |
|
| 239 |
// Private storage for parser wrapper. DO NOT REFERENCE INTERNALLY. May not be |
| 240 |
// initialized. |
| 241 |
mozilla::Maybe<EitherParser> ep_ = {}; |
| 242 |
|
| 243 |
const ErrorReporter& errorReporter_; |
| 244 |
|
| 245 |
public: |
| 246 |
CompilationState& compilationState; |
| 247 |
|
| 248 |
uint32_t maxFixedSlots = 0; /* maximum number of fixed frame slots so far */ |
| 249 |
|
| 250 |
// Index into scopeList of the body scope. |
| 251 |
GCThingIndex bodyScopeIndex = ScopeNote::NoScopeIndex; |
| 252 |
|
| 253 |
EmitterScope* varEmitterScope = nullptr; |
| 254 |
NestableControl* innermostNestableControl = nullptr; |
| 255 |
EmitterScope* innermostEmitterScope_ = nullptr; |
| 256 |
TDZCheckCache* innermostTDZCheckCache = nullptr; |
| 257 |
|
| 258 |
// When compiling in self-hosted mode, we have special intrinsics that act as |
| 259 |
// decorators for exported functions. To keeps things simple, we only allow |
| 260 |
// these to target the last top-level function emitted. This field tracks that |
| 261 |
// function. |
| 262 |
FunctionBox* prevSelfHostedTopLevelFunction = nullptr; |
| 263 |
|
| 264 |
#ifdef DEBUG |
| 265 |
bool unstableEmitterScope = false; |
| 266 |
|
| 267 |
friend class AutoCheckUnstableEmitterScope; |
| 268 |
#endif |
| 269 |
|
| 270 |
const ErrorReporter& errorReporter() const { return errorReporter_; } |
| 271 |
|
| 272 |
ParserAtomsTable& parserAtoms() { return compilationState.parserAtoms; } |
| 273 |
const ParserAtomsTable& parserAtoms() const { |
| 274 |
return compilationState.parserAtoms; |
| 275 |
} |
| 276 |
|
| 277 |
EmitterScope* innermostEmitterScope() const { |
| 278 |
MOZ_ASSERT(!unstableEmitterScope); |
| 279 |
return innermostEmitterScopeNoCheck(); |
| 280 |
} |
| 281 |
EmitterScope* innermostEmitterScopeNoCheck() const { |
| 282 |
return innermostEmitterScope_; |
| 283 |
} |
| 284 |
|
| 285 |
// When parsing internal code such as self-hosted functions or synthetic |
| 286 |
// class constructors, we do not emit breakpoint and srcnote data since there |
| 287 |
// is no direcly corresponding user-visible sources. |
| 288 |
const bool suppressBreakpointsAndSourceNotes = false; |
| 289 |
|
| 290 |
// Script contains finally block. |
| 291 |
bool hasTryFinally = false; |
| 292 |
|
| 293 |
enum EmitterMode { |
| 294 |
Normal, |
| 295 |
|
| 296 |
// Emit JSOp::GetIntrinsic instead of JSOp::GetName and assert that |
| 297 |
// JSOp::GetName and JSOp::*GName don't ever get emitted. See the comment |
| 298 |
// for the field |selfHostingMode| in Parser.h for details. |
| 299 |
SelfHosting, |
| 300 |
|
| 301 |
// Check the static scope chain of the root function for resolving free |
| 302 |
// variable accesses in the script. |
| 303 |
LazyFunction |
| 304 |
}; |
| 305 |
|
| 306 |
const EmitterMode emitterMode = Normal; |
| 307 |
|
| 308 |
mozilla::Maybe<uint32_t> scriptStartOffset = {}; |
| 309 |
|
| 310 |
// The end location of a function body that is being emitted. |
| 311 |
mozilla::Maybe<uint32_t> functionBodyEndPos = {}; |
| 312 |
|
| 313 |
// Jump target just before the final CheckReturn opcode in a derived class |
| 314 |
// constructor body. |
| 315 |
JumpList endOfDerivedClassConstructorBody = {}; |
| 316 |
|
| 317 |
// Jump target just before the final yield in a generator or async function. |
| 318 |
JumpList finalYields = {}; |
| 319 |
|
| 320 |
// In order to heuristically determine the size of the allocation if this is a |
| 321 |
// constructor function, we track expressions which add properties in the |
| 322 |
// constructor. |
| 323 |
mozilla::SaturateUint8 propertyAdditionEstimate = {}; |
| 324 |
|
| 325 |
/* |
| 326 |
* Note that BytecodeEmitters are magic: they own the arena "top-of-stack" |
| 327 |
* space above their tempMark points. This means that you cannot alloc from |
| 328 |
* tempLifoAlloc and save the pointer beyond the next BytecodeEmitter |
| 329 |
* destruction. |
| 330 |
*/ |
| 331 |
private: |
| 332 |
// Internal constructor, for delegation use only. |
| 333 |
BytecodeEmitter(BytecodeEmitter* parent, FrontendContext* fc, |
| 334 |
SharedContext* sc, const ErrorReporter& errorReporter, |
| 335 |
CompilationState& compilationState, EmitterMode emitterMode); |
| 336 |
|
| 337 |
void initFromBodyPosition(TokenPos bodyPosition); |
| 338 |
|
| 339 |
public: |
| 340 |
BytecodeEmitter(FrontendContext* fc, const EitherParser& parser, |
| 341 |
SharedContext* sc, CompilationState& compilationState, |
| 342 |
EmitterMode emitterMode = Normal); |
| 343 |
|
| 344 |
template <typename Unit> |
| 345 |
BytecodeEmitter(FrontendContext* fc, Parser<FullParseHandler, Unit>* parser, |
| 346 |
SharedContext* sc, CompilationState& compilationState, |
| 347 |
EmitterMode emitterMode = Normal) |
| 348 |
: BytecodeEmitter(fc, EitherParser(parser), sc, compilationState, |
| 349 |
emitterMode) {} |
| 350 |
|
| 351 |
BytecodeEmitter(BytecodeEmitter* parent, SharedContext* sc); |
| 352 |
|
| 353 |
[[nodiscard]] bool init(); |
| 354 |
[[nodiscard]] bool init(TokenPos bodyPosition); |
| 355 |
|
| 356 |
template <typename T> |
| 357 |
T* findInnermostNestableControl() const; |
| 358 |
|
| 359 |
template <typename T, typename Predicate /* (T*) -> bool */> |
| 360 |
T* findInnermostNestableControl(Predicate predicate) const; |
| 361 |
|
| 362 |
NameLocation lookupName(TaggedParserAtomIndex name); |
| 363 |
|
| 364 |
// See EmitterScope::lookupPrivate for details around brandLoc |
| 365 |
void lookupPrivate(TaggedParserAtomIndex name, NameLocation& loc, |
| 366 |
mozilla::Maybe<NameLocation>& brandLoc); |
| 367 |
|
| 368 |
// To implement Annex B and the formal parameter defaults scope semantics |
| 369 |
// requires accessing names that would otherwise be shadowed. This method |
| 370 |
// returns the access location of a name that is known to be bound in a |
| 371 |
// target scope. |
| 372 |
mozilla::Maybe<NameLocation> locationOfNameBoundInScope( |
| 373 |
TaggedParserAtomIndex name, EmitterScope* target); |
| 374 |
|
| 375 |
// Get the location of a name known to be bound in a given scope, |
| 376 |
// starting at the source scope. |
| 377 |
template <typename T> |
| 378 |
mozilla::Maybe<NameLocation> locationOfNameBoundInScopeType( |
| 379 |
TaggedParserAtomIndex name, EmitterScope* source); |
| 380 |
|
| 381 |
// Get the location of a name known to be bound in the function scope, |
| 382 |
// starting at the source scope. |
| 383 |
mozilla::Maybe<NameLocation> locationOfNameBoundInFunctionScope( |
| 384 |
TaggedParserAtomIndex name) { |
| 385 |
return locationOfNameBoundInScopeType<FunctionScope>( |
| 386 |
name, innermostEmitterScope()); |
| 387 |
} |
| 388 |
|
| 389 |
void setVarEmitterScope(EmitterScope* emitterScope) { |
| 390 |
MOZ_ASSERT(emitterScope); |
| 391 |
MOZ_ASSERT(!varEmitterScope); |
| 392 |
varEmitterScope = emitterScope; |
| 393 |
} |
| 394 |
|
| 395 |
AbstractScopePtr outermostScope() const { |
| 396 |
return perScriptData().gcThingList().firstScope(); |
| 397 |
} |
| 398 |
AbstractScopePtr innermostScope() const; |
| 399 |
ScopeIndex innermostScopeIndex() const; |
| 400 |
|
| 401 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool makeAtomIndex( |
| 402 |
TaggedParserAtomIndex atom, ParserAtom::Atomize atomize, |
| 403 |
GCThingIndex* indexp) { |
| 404 |
MOZ_ASSERT(perScriptData().atomIndices()); |
| 405 |
AtomIndexMap::AddPtr p = perScriptData().atomIndices()->lookupForAdd(atom); |
| 406 |
if (p) { |
| 407 |
compilationState.parserAtoms.markAtomize(atom, atomize); |
| 408 |
*indexp = GCThingIndex(p->value()); |
| 409 |
return true; |
| 410 |
} |
| 411 |
|
| 412 |
GCThingIndex index; |
| 413 |
if (!perScriptData().gcThingList().append(atom, atomize, &index)) { |
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
// `atomIndices()` uses uint32_t instead of GCThingIndex, because |
| 418 |
// GCThingIndex isn't trivial type. |
| 419 |
if (!perScriptData().atomIndices()->add(p, atom, index.index)) { |
| 420 |
ReportOutOfMemory(fc); |
| 421 |
return false; |
| 422 |
} |
| 423 |
|
| 424 |
*indexp = index; |
| 425 |
return true; |
| 426 |
} |
| 427 |
|
| 428 |
bool isInLoop() const; |
| 429 |
[[nodiscard]] bool checkSingletonContext() const; |
| 430 |
|
| 431 |
bool needsImplicitThis() const; |
| 432 |
|
| 433 |
size_t countThisEnvironmentHops() const; |
| 434 |
[[nodiscard]] bool emitThisEnvironmentCallee(); |
| 435 |
[[nodiscard]] bool emitSuperBase(); |
| 436 |
|
| 437 |
uint32_t mainOffset() const { return *mainOffset_; } |
| 438 |
|
| 439 |
bool inPrologue() const { return mainOffset_.isNothing(); } |
| 440 |
|
| 441 |
void switchToMain() { |
| 442 |
MOZ_ASSERT(inPrologue()); |
| 443 |
mainOffset_.emplace(bytecodeSection().code().length()); |
| 444 |
} |
| 445 |
|
| 446 |
void setFunctionBodyEndPos(uint32_t pos) { |
| 447 |
functionBodyEndPos = mozilla::Some(pos); |
| 448 |
} |
| 449 |
|
| 450 |
void setScriptStartOffsetIfUnset(uint32_t pos) { |
| 451 |
if (scriptStartOffset.isNothing()) { |
| 452 |
scriptStartOffset = mozilla::Some(pos); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
void reportError(ParseNode* pn, unsigned errorNumber, ...) const; |
| 457 |
void reportError(uint32_t offset, unsigned errorNumber, ...) const; |
| 458 |
|
| 459 |
// Fill in a ScriptStencil using this BCE data. |
| 460 |
bool intoScriptStencil(ScriptIndex scriptIndex); |
| 461 |
|
| 462 |
// If pn contains a useful expression, return true with *answer set to true. |
| 463 |
// If pn contains a useless expression, return true with *answer set to |
| 464 |
// false. Return false on error. |
| 465 |
// |
| 466 |
// The caller should initialize *answer to false and invoke this function on |
| 467 |
// an expression statement or similar subtree to decide whether the tree |
| 468 |
// could produce code that has any side effects. For an expression |
| 469 |
// statement, we define useless code as code with no side effects, because |
| 470 |
// the main effect, the value left on the stack after the code executes, |
| 471 |
// will be discarded by a pop bytecode. |
| 472 |
[[nodiscard]] bool checkSideEffects(ParseNode* pn, bool* answer) const; |
| 473 |
|
| 474 |
#ifdef DEBUG |
| 475 |
[[nodiscard]] bool checkStrictOrSloppy(JSOp op) const; |
| 476 |
#endif |
| 477 |
|
| 478 |
// Add TryNote to the tryNoteList array. The start and end offset are |
| 479 |
// relative to current section. |
| 480 |
[[nodiscard]] bool addTryNote(TryNoteKind kind, uint32_t stackDepth, |
| 481 |
BytecodeOffset start, BytecodeOffset end); |
| 482 |
|
| 483 |
// Indicates the emitter should not generate location or debugger source |
| 484 |
// notes. This lets us avoid generating notes for non-user code. |
| 485 |
bool skipLocationSrcNotes() const { |
| 486 |
return inPrologue() || suppressBreakpointsAndSourceNotes; |
| 487 |
} |
| 488 |
bool skipBreakpointSrcNotes() const { |
| 489 |
return inPrologue() || suppressBreakpointsAndSourceNotes; |
| 490 |
} |
| 491 |
|
| 492 |
// Append a new source note of the given type (and therefore size) to the |
| 493 |
// notes dynamic array, updating noteCount. Return the new note's index |
| 494 |
// within the array pointed at by current->notes as outparam. |
| 495 |
[[nodiscard]] bool newSrcNote(SrcNoteType type, unsigned* indexp = nullptr); |
| 496 |
[[nodiscard]] bool newSrcNote2(SrcNoteType type, ptrdiff_t operand, |
| 497 |
unsigned* indexp = nullptr); |
| 498 |
[[nodiscard]] bool convertLastNewLineToNewLineColumn( |
| 499 |
JS::LimitedColumnNumberOneOrigin column); |
| 500 |
[[nodiscard]] bool convertLastSetLineToSetLineColumn( |
| 501 |
JS::LimitedColumnNumberOneOrigin column); |
| 502 |
|
| 503 |
[[nodiscard]] bool newSrcNoteOperand(ptrdiff_t operand); |
| 504 |
|
| 505 |
// Control whether emitTree emits a line number note. |
| 506 |
enum EmitLineNumberNote { EMIT_LINENOTE, SUPPRESS_LINENOTE }; |
| 507 |
|
| 508 |
// Emit code for the tree rooted at pn. |
| 509 |
[[nodiscard]] bool emitTree(ParseNode* pn, |
| 510 |
ValueUsage valueUsage = ValueUsage::WantValue, |
| 511 |
EmitLineNumberNote emitLineNote = EMIT_LINENOTE); |
| 512 |
|
| 513 |
[[nodiscard]] bool emitOptionalTree( |
| 514 |
ParseNode* pn, OptionalEmitter& oe, |
| 515 |
ValueUsage valueUsage = ValueUsage::WantValue); |
| 516 |
|
| 517 |
[[nodiscard]] bool emitDeclarationInstantiation(ParseNode* body); |
| 518 |
|
| 519 |
// Emit global, eval, or module code for tree rooted at body. Always |
| 520 |
// encompasses the entire source. |
| 521 |
[[nodiscard]] bool emitScript(ParseNode* body); |
| 522 |
|
| 523 |
// Calculate the `nslots` value for ImmutableScriptData constructor parameter. |
| 524 |
// Fails if it overflows. |
| 525 |
[[nodiscard]] bool getNslots(uint32_t* nslots) const; |
| 526 |
|
| 527 |
// Emit function code for the tree rooted at body. |
| 528 |
[[nodiscard]] bool emitFunctionScript(FunctionNode* funNode); |
| 529 |
|
| 530 |
[[nodiscard]] bool markStepBreakpoint(); |
| 531 |
[[nodiscard]] bool markSimpleBreakpoint(); |
| 532 |
[[nodiscard]] bool updateLineNumberNotes(uint32_t offset); |
| 533 |
[[nodiscard]] bool updateSourceCoordNotes(uint32_t offset); |
| 534 |
[[nodiscard]] bool updateSourceCoordNotesIfNonLiteral(ParseNode* node); |
| 535 |
|
| 536 |
[[nodiscard]] bool emitCheck(JSOp op, ptrdiff_t delta, |
| 537 |
BytecodeOffset* offset); |
| 538 |
|
| 539 |
// Emit one bytecode. |
| 540 |
[[nodiscard]] bool emit1(JSOp op); |
| 541 |
|
| 542 |
// Emit two bytecodes, an opcode (op) with a byte of immediate operand |
| 543 |
// (op1). |
| 544 |
[[nodiscard]] bool emit2(JSOp op, uint8_t op1); |
| 545 |
|
| 546 |
// Emit three bytecodes, an opcode with two bytes of immediate operands. |
| 547 |
[[nodiscard]] bool emit3(JSOp op, jsbytecode op1, jsbytecode op2); |
| 548 |
|
| 549 |
// Helper to duplicate one or more stack values. |slotFromTop| is the value's |
| 550 |
// depth on the JS stack, as measured from the top. |count| is the number of |
| 551 |
// values to duplicate, in theiro original order. |
| 552 |
[[nodiscard]] bool emitDupAt(unsigned slotFromTop, unsigned count = 1); |
| 553 |
|
| 554 |
// Helper to emit JSOp::Pop or JSOp::PopN. |
| 555 |
[[nodiscard]] bool emitPopN(unsigned n); |
| 556 |
|
| 557 |
// Helper to emit JSOp::Swap or JSOp::Pick. |
| 558 |
[[nodiscard]] bool emitPickN(uint8_t n); |
| 559 |
|
| 560 |
// Helper to emit JSOp::Swap or JSOp::Unpick. |
| 561 |
[[nodiscard]] bool emitUnpickN(uint8_t n); |
| 562 |
|
| 563 |
// Helper to emit JSOp::CheckIsObj. |
| 564 |
[[nodiscard]] bool emitCheckIsObj(CheckIsObjectKind kind); |
| 565 |
|
| 566 |
// Helper to emit JSOp::BuiltinObject. |
| 567 |
[[nodiscard]] bool emitBuiltinObject(BuiltinObjectKind kind); |
| 568 |
|
| 569 |
// Emit a bytecode followed by an uint16 immediate operand stored in |
| 570 |
// big-endian order. |
| 571 |
[[nodiscard]] bool emitUint16Operand(JSOp op, uint32_t operand); |
| 572 |
|
| 573 |
// Emit a bytecode followed by an uint32 immediate operand. |
| 574 |
[[nodiscard]] bool emitUint32Operand(JSOp op, uint32_t operand); |
| 575 |
|
| 576 |
// Emit (1 + extra) bytecodes, for N bytes of op and its immediate operand. |
| 577 |
[[nodiscard]] bool emitN(JSOp op, size_t extra, |
| 578 |
BytecodeOffset* offset = nullptr); |
| 579 |
|
| 580 |
[[nodiscard]] bool emitDouble(double dval); |
| 581 |
[[nodiscard]] bool emitNumberOp(double dval); |
| 582 |
|
| 583 |
[[nodiscard]] bool emitBigIntOp(BigIntLiteral* bigint); |
| 584 |
|
| 585 |
[[nodiscard]] bool emitThisLiteral(ThisLiteral* pn); |
| 586 |
[[nodiscard]] bool emitGetFunctionThis(NameNode* thisName); |
| 587 |
[[nodiscard]] bool emitGetThisForSuperBase(UnaryNode* superBase); |
| 588 |
[[nodiscard]] bool emitSetThis(BinaryNode* setThisNode); |
| 589 |
[[nodiscard]] bool emitCheckDerivedClassConstructorReturn(); |
| 590 |
|
| 591 |
private: |
| 592 |
[[nodiscard]] bool emitNewTarget(); |
| 593 |
|
| 594 |
public: |
| 595 |
[[nodiscard]] bool emitNewTarget(NewTargetNode* pn); |
| 596 |
[[nodiscard]] bool emitNewTarget(CallNode* pn); |
| 597 |
|
| 598 |
// Handle jump opcodes and jump targets. |
| 599 |
[[nodiscard]] bool emitJumpTargetOp(JSOp op, BytecodeOffset* off); |
| 600 |
[[nodiscard]] bool emitJumpTarget(JumpTarget* target); |
| 601 |
[[nodiscard]] bool emitJumpNoFallthrough(JSOp op, JumpList* jump); |
| 602 |
[[nodiscard]] bool emitJump(JSOp op, JumpList* jump); |
| 603 |
void patchJumpsToTarget(JumpList jump, JumpTarget target); |
| 604 |
[[nodiscard]] bool emitJumpTargetAndPatch(JumpList jump); |
| 605 |
|
| 606 |
[[nodiscard]] bool emitCall( |
| 607 |
JSOp op, uint16_t argc, |
| 608 |
const mozilla::Maybe<uint32_t>& sourceCoordOffset); |
| 609 |
[[nodiscard]] bool emitCall(JSOp op, uint16_t argc, ParseNode* pn = nullptr); |
| 610 |
[[nodiscard]] bool emitCallIncDec(UnaryNode* incDec); |
| 611 |
|
| 612 |
uint32_t getOffsetForLoop(ParseNode* nextpn) const; |
| 613 |
|
| 614 |
enum class GotoKind { Break, Continue }; |
| 615 |
[[nodiscard]] bool emitGoto(NestableControl* target, GotoKind kind); |
| 616 |
|
| 617 |
[[nodiscard]] bool emitGCIndexOp(JSOp op, GCThingIndex index); |
| 618 |
|
| 619 |
[[nodiscard]] bool emitAtomOp(JSOp op, TaggedParserAtomIndex atom); |
| 620 |
[[nodiscard]] bool emitAtomOp(JSOp op, GCThingIndex atomIndex); |
| 621 |
|
| 622 |
[[nodiscard]] bool emitStringOp(JSOp op, TaggedParserAtomIndex atom); |
| 623 |
[[nodiscard]] bool emitStringOp(JSOp op, GCThingIndex atomIndex); |
| 624 |
|
| 625 |
[[nodiscard]] bool emitArrayLiteral(ListNode* array); |
| 626 |
[[nodiscard]] bool emitArray(ListNode* array); |
| 627 |
[[nodiscard]] bool emitSpreadIntoArray(UnaryNode* elem); |
| 628 |
|
| 629 |
[[nodiscard]] bool emitInternedScopeOp(GCThingIndex index, JSOp op); |
| 630 |
[[nodiscard]] bool emitInternedObjectOp(GCThingIndex index, JSOp op); |
| 631 |
[[nodiscard]] bool emitRegExp(GCThingIndex index); |
| 632 |
|
| 633 |
[[nodiscard]] MOZ_NEVER_INLINE bool emitFunction(FunctionNode* funNode, |
| 634 |
bool needsProto = false); |
| 635 |
[[nodiscard]] MOZ_NEVER_INLINE bool emitObject(ListNode* objNode); |
| 636 |
|
| 637 |
[[nodiscard]] bool emitHoistedFunctionsInList(ListNode* stmtList); |
| 638 |
|
| 639 |
// Can we use the object-literal writer either in singleton-object mode (with |
| 640 |
// values) or in template mode (field names only, no values) for the property |
| 641 |
// list? |
| 642 |
void isPropertyListObjLiteralCompatible(ListNode* obj, bool* withValues, |
| 643 |
bool* withoutValues) const; |
| 644 |
bool isArrayObjLiteralCompatible(ListNode* array) const; |
| 645 |
|
| 646 |
[[nodiscard]] bool emitPropertyList(ListNode* obj, PropertyEmitter& pe, |
| 647 |
PropListType type); |
| 648 |
|
| 649 |
[[nodiscard]] bool emitPropertyListObjLiteral(ListNode* obj, JSOp op, |
| 650 |
bool useObjLiteralValues); |
| 651 |
|
| 652 |
[[nodiscard]] bool emitDestructuringRestExclusionSetObjLiteral( |
| 653 |
ListNode* pattern); |
| 654 |
|
| 655 |
[[nodiscard]] bool emitObjLiteralArray(ListNode* array); |
| 656 |
|
| 657 |
// Is a field value JSOp::Object-compatible? |
| 658 |
[[nodiscard]] bool isRHSObjLiteralCompatible(ParseNode* value) const; |
| 659 |
|
| 660 |
[[nodiscard]] bool emitObjLiteralValue(ObjLiteralWriter& writer, |
| 661 |
ParseNode* value); |
| 662 |
|
| 663 |
mozilla::Maybe<MemberInitializers> setupMemberInitializers( |
| 664 |
ListNode* classMembers, FieldPlacement placement) const; |
| 665 |
[[nodiscard]] bool emitCreateFieldKeys(ListNode* obj, |
| 666 |
FieldPlacement placement); |
| 667 |
[[nodiscard]] bool emitCreateMemberInitializers(ClassEmitter& ce, |
| 668 |
ListNode* obj, |
| 669 |
FieldPlacement placement |
| 670 |
#ifdef ENABLE_DECORATORS |
| 671 |
, |
| 672 |
bool hasHeritage |
| 673 |
#endif |
| 674 |
); |
| 675 |
const MemberInitializers& findMemberInitializersForCall() const; |
| 676 |
[[nodiscard]] bool emitInitializeInstanceMembers( |
| 677 |
bool isDerivedClassConstructor); |
| 678 |
[[nodiscard]] bool emitInitializeStaticFields(ListNode* classMembers); |
| 679 |
|
| 680 |
[[nodiscard]] bool emitPrivateMethodInitializers(ClassEmitter& ce, |
| 681 |
ListNode* obj); |
| 682 |
[[nodiscard]] bool emitPrivateMethodInitializer( |
| 683 |
ClassMethod* classMethod, TaggedParserAtomIndex storedMethodAtom); |
| 684 |
|
| 685 |
// To catch accidental misuse, emitUint16Operand/emit3 assert that they are |
| 686 |
// not used to unconditionally emit JSOp::GetLocal. Variable access should |
| 687 |
// instead be emitted using EmitVarOp. In special cases, when the caller |
| 688 |
// definitely knows that a given local slot is unaliased, this function may be |
| 689 |
// used as a non-asserting version of emitUint16Operand. |
| 690 |
[[nodiscard]] bool emitLocalOp(JSOp op, uint32_t slot); |
| 691 |
|
| 692 |
[[nodiscard]] bool emitArgOp(JSOp op, uint16_t slot); |
| 693 |
[[nodiscard]] bool emitEnvCoordOp(JSOp op, EnvironmentCoordinate ec); |
| 694 |
|
| 695 |
[[nodiscard]] bool emitGetNameAtLocation(TaggedParserAtomIndex name, |
| 696 |
const NameLocation& loc); |
| 697 |
[[nodiscard]] bool emitGetName(TaggedParserAtomIndex name) { |
| 698 |
return emitGetNameAtLocation(name, lookupName(name)); |
| 699 |
} |
| 700 |
[[nodiscard]] bool emitGetName(NameNode* name); |
| 701 |
[[nodiscard]] bool emitGetPrivateName(NameNode* name); |
| 702 |
[[nodiscard]] bool emitGetPrivateName(TaggedParserAtomIndex name); |
| 703 |
|
| 704 |
[[nodiscard]] bool emitTDZCheckIfNeeded(TaggedParserAtomIndex name, |
| 705 |
const NameLocation& loc, |
| 706 |
ValueIsOnStack isOnStack); |
| 707 |
|
| 708 |
[[nodiscard]] bool emitNameIncDec(UnaryNode* incDec, ValueUsage valueUsage); |
| 709 |
|
| 710 |
[[nodiscard]] bool emitDeclarationList(ListNode* declList); |
| 711 |
[[nodiscard]] bool emitSingleDeclaration(ListNode* declList, NameNode* decl, |
| 712 |
ParseNode* initializer); |
| 713 |
[[nodiscard]] bool emitAssignmentRhs(ParseNode* rhs, |
| 714 |
TaggedParserAtomIndex anonFunctionName); |
| 715 |
[[nodiscard]] bool emitAssignmentRhs(uint8_t offset); |
| 716 |
|
| 717 |
[[nodiscard]] bool emitPrepareIteratorResult(); |
| 718 |
[[nodiscard]] bool emitFinishIteratorResult(bool done); |
| 719 |
|
| 720 |
// Convert and add `writer` data to stencil. |
| 721 |
// Iff it suceeds, `outIndex` out parameter is initialized to the index of the |
| 722 |
// object in GC things vector. |
| 723 |
[[nodiscard]] bool addObjLiteralData(ObjLiteralWriter& writer, |
| 724 |
GCThingIndex* outIndex); |
| 725 |
|
| 726 |
[[nodiscard]] bool emitGetDotGeneratorInInnermostScope() { |
| 727 |
return emitGetDotGeneratorInScope(*innermostEmitterScope()); |
| 728 |
} |
| 729 |
[[nodiscard]] bool emitGetDotGeneratorInScope(EmitterScope& currentScope); |
| 730 |
|
| 731 |
[[nodiscard]] bool allocateResumeIndex(BytecodeOffset offset, |
| 732 |
uint32_t* resumeIndex); |
| 733 |
[[nodiscard]] bool allocateResumeIndexRange( |
| 734 |
mozilla::Span<BytecodeOffset> offsets, uint32_t* firstResumeIndex); |
| 735 |
|
| 736 |
[[nodiscard]] bool emitInitialYield(UnaryNode* yieldNode); |
| 737 |
[[nodiscard]] bool emitYield(UnaryNode* yieldNode); |
| 738 |
[[nodiscard]] bool emitYieldOp(JSOp op); |
| 739 |
[[nodiscard]] bool emitYieldStar(ParseNode* iter); |
| 740 |
[[nodiscard]] bool emitAwaitInInnermostScope() { |
| 741 |
return emitAwaitInScope(*innermostEmitterScope()); |
| 742 |
} |
| 743 |
[[nodiscard]] bool emitAwaitInInnermostScope(UnaryNode* awaitNode); |
| 744 |
[[nodiscard]] bool emitAwaitInScope(EmitterScope& currentScope); |
| 745 |
|
| 746 |
[[nodiscard]] bool emitPushResumeKind(GeneratorResumeKind kind); |
| 747 |
|
| 748 |
[[nodiscard]] bool emitPropLHS(PropertyAccess* prop); |
| 749 |
[[nodiscard]] bool emitPropIncDec(UnaryNode* incDec, ValueUsage valueUsage); |
| 750 |
|
| 751 |
[[nodiscard]] bool emitComputedPropertyName(UnaryNode* computedPropName); |
| 752 |
|
| 753 |
// Emit bytecode to put operands for a JSOp::GetElem/CallElem/SetElem/DelElem |
| 754 |
// opcode onto the stack in the right order. In the case of SetElem, the |
| 755 |
// value to be assigned must already be pushed. |
| 756 |
enum class EmitElemOption { Get, Call, IncDec, CompoundAssign, Ref }; |
| 757 |
[[nodiscard]] bool emitElemOperands(PropertyByValue* elem, |
| 758 |
EmitElemOption opts); |
| 759 |
|
| 760 |
[[nodiscard]] bool emitElemObjAndKey(PropertyByValue* elem, |
| 761 |
ElemOpEmitter& eoe); |
| 762 |
[[nodiscard]] bool emitElemOpBase(JSOp op); |
| 763 |
|
| 764 |
[[nodiscard]] bool emitElemIncDec(UnaryNode* incDec, ValueUsage valueUsage); |
| 765 |
[[nodiscard]] bool emitObjAndPrivateName(PrivateMemberAccess* elem, |
| 766 |
ElemOpEmitter& eoe); |
| 767 |
[[nodiscard]] bool emitPrivateIncDec(UnaryNode* incDec, |
| 768 |
ValueUsage valueUsage); |
| 769 |
|
| 770 |
[[nodiscard]] bool emitCatch(BinaryNode* catchClause); |
| 771 |
[[nodiscard]] bool emitIf(TernaryNode* ifNode); |
| 772 |
[[nodiscard]] bool emitWith(BinaryNode* withNode); |
| 773 |
|
| 774 |
[[nodiscard]] MOZ_NEVER_INLINE bool emitLabeledStatement( |
| 775 |
const LabeledStatement* labeledStmt); |
| 776 |
[[nodiscard]] MOZ_NEVER_INLINE bool emitLexicalScope( |
| 777 |
LexicalScopeNode* lexicalScope); |
| 778 |
[[nodiscard]] bool emitLexicalScopeBody( |
| 779 |
ParseNode* body, EmitLineNumberNote emitLineNote = EMIT_LINENOTE); |
| 780 |
[[nodiscard]] MOZ_NEVER_INLINE bool emitSwitch(SwitchStatement* switchStmt); |
| 781 |
[[nodiscard]] MOZ_NEVER_INLINE bool emitTry(TryNode* tryNode); |
| 782 |
|
| 783 |
[[nodiscard]] bool emitJumpToFinally(JumpList* jump, uint32_t idx); |
| 784 |
|
| 785 |
// emitDestructuringLHSRef emits the lhs expression's reference. |
| 786 |
// If the lhs expression is object property |OBJ.prop|, it emits |OBJ|. |
| 787 |
// If it's object element |OBJ[ELEM]|, it emits |OBJ| and |ELEM|. |
| 788 |
// If there's nothing to evaluate for the reference, it emits nothing. |
| 789 |
[[nodiscard]] bool emitDestructuringLHSRef(ParseNode* target, |
| 790 |
DestructuringFlavor flav, |
| 791 |
DestructuringLHSRef& lref); |
| 792 |
|
| 793 |
// emitSetOrInitializeDestructuring assumes the lhs expression's reference |
| 794 |
// and the to-be-destructured value has been pushed on the stack. It emits |
| 795 |
// code to destructure a single lhs expression (either a name or a compound |
| 796 |
// []/{} expression). |
| 797 |
[[nodiscard]] bool emitSetOrInitializeDestructuring( |
| 798 |
ParseNode* target, DestructuringFlavor flav, DestructuringLHSRef& lref); |
| 799 |
|
| 800 |
// emitDestructuringObjRestExclusionSet emits the property exclusion set |
| 801 |
// for the rest-property in an object pattern. |
| 802 |
[[nodiscard]] bool emitDestructuringObjRestExclusionSet(ListNode* pattern, |
| 803 |
uint8_t setSize); |
| 804 |
|
| 805 |
// emitDestructuringOps assumes the to-be-destructured value has been |
| 806 |
// pushed on the stack and emits code to destructure each part of a [] or |
| 807 |
// {} lhs expression. |
| 808 |
[[nodiscard]] bool emitDestructuringOps(ListNode* pattern, |
| 809 |
DestructuringFlavor flav); |
| 810 |
[[nodiscard]] bool emitDestructuringOpsArray(ListNode* pattern, |
| 811 |
DestructuringFlavor flav); |
| 812 |
[[nodiscard]] bool emitDestructuringOpsObject(ListNode* pattern, |
| 813 |
DestructuringFlavor flav); |
| 814 |
|
| 815 |
enum class CopyOption { Filtered, Unfiltered }; |
| 816 |
|
| 817 |
// Calls either the |CopyDataProperties| or the |
| 818 |
// |CopyDataPropertiesUnfiltered| intrinsic function, consumes three (or |
| 819 |
// two in the latter case) elements from the stack. |
| 820 |
[[nodiscard]] bool emitCopyDataProperties(CopyOption option); |
| 821 |
|
| 822 |
JSOp getIterCallOp(JSOp callOp, SelfHostedIter selfHostedIter) const; |
| 823 |
|
| 824 |
// Push the operands for emit(Async)Iterator onto the stack. |
| 825 |
[[nodiscard]] bool emitIterable(ParseNode* value, |
| 826 |
SelfHostedIter selfHostedIter, |
| 827 |
IteratorKind iterKind = IteratorKind::Sync); |
| 828 |
|
| 829 |
// emitIterator expects the iterable to already be on the stack. |
| 830 |
// It will replace that stack value with the corresponding iterator |
| 831 |
[[nodiscard]] bool emitIterator(SelfHostedIter selfHostedIter); |
| 832 |
|
| 833 |
[[nodiscard]] bool emitAsyncIterator(SelfHostedIter selfHostedIter); |
| 834 |
|
| 835 |
// Pops iterator from the top of the stack. Pushes the result of |.next()| |
| 836 |
// onto the stack. |
| 837 |
[[nodiscard]] bool emitIteratorNext( |
| 838 |
const mozilla::Maybe<uint32_t>& callSourceCoordOffset, |
| 839 |
IteratorKind kind = IteratorKind::Sync, |
| 840 |
SelfHostedIter selfHostedIter = SelfHostedIter::Deny); |
| 841 |
[[nodiscard]] bool emitIteratorCloseInScope( |
| 842 |
EmitterScope& currentScope, IteratorKind iterKind = IteratorKind::Sync, |
| 843 |
CompletionKind completionKind = CompletionKind::Normal, |
| 844 |
SelfHostedIter selfHostedIter = SelfHostedIter::Deny); |
| 845 |
[[nodiscard]] bool emitIteratorCloseInInnermostScope( |
| 846 |
IteratorKind iterKind = IteratorKind::Sync, |
| 847 |
CompletionKind completionKind = CompletionKind::Normal, |
| 848 |
SelfHostedIter selfHostedIter = SelfHostedIter::Deny) { |
| 849 |
return emitIteratorCloseInScope(*innermostEmitterScope(), iterKind, |
| 850 |
completionKind, selfHostedIter); |
| 851 |
} |
| 852 |
|
| 853 |
template <typename InnerEmitter> |
| 854 |
[[nodiscard]] bool wrapWithDestructuringTryNote(int32_t iterDepth, |
| 855 |
InnerEmitter emitter); |
| 856 |
|
| 857 |
[[nodiscard]] bool defineHoistedTopLevelFunctions(ParseNode* body); |
| 858 |
|
| 859 |
// Check if the value on top of the stack is "undefined". If so, replace |
| 860 |
// that value on the stack with the value defined by |defaultExpr|. |
| 861 |
// |pattern| is a lhs node of the default expression. If it's an |
| 862 |
// identifier and |defaultExpr| is an anonymous function, |SetFunctionName| |
| 863 |
// is called at compile time. |
| 864 |
[[nodiscard]] bool emitDefault(ParseNode* defaultExpr, ParseNode* pattern); |
| 865 |
|
| 866 |
[[nodiscard]] bool emitAnonymousFunctionWithName(ParseNode* node, |
| 867 |
TaggedParserAtomIndex name); |
| 868 |
|
| 869 |
[[nodiscard]] bool emitAnonymousFunctionWithComputedName( |
| 870 |
ParseNode* node, FunctionPrefixKind prefixKind); |
| 871 |
|
| 872 |
void setFunName(FunctionBox* fun, TaggedParserAtomIndex name) const; |
| 873 |
[[nodiscard]] bool emitInitializer(ParseNode* initializer, |
| 874 |
ParseNode* pattern); |
| 875 |
|
| 876 |
[[nodiscard]] bool emitCallSiteObjectArray(ObjLiteralWriter& writer, |
| 877 |
ListNode* cookedOrRaw, |
| 878 |
ParseNode* head, uint32_t count); |
| 879 |
[[nodiscard]] bool emitCallSiteObject(CallSiteNode* callSiteObj); |
| 880 |
[[nodiscard]] bool emitTemplateString(ListNode* templateString); |
| 881 |
[[nodiscard]] bool emitAssignmentOrInit(ParseNodeKind kind, ParseNode* lhs, |
| 882 |
ParseNode* rhs); |
| 883 |
[[nodiscard]] bool emitShortCircuitAssignment(AssignmentNode* node); |
| 884 |
|
| 885 |
[[nodiscard]] bool emitReturn(UnaryNode* returnNode); |
| 886 |
[[nodiscard]] bool finishReturn(BytecodeOffset setRvalOffset); |
| 887 |
|
| 888 |
[[nodiscard]] bool emitExpressionStatement(UnaryNode* exprStmt); |
| 889 |
[[nodiscard]] bool emitStatementList(ListNode* stmtList); |
| 890 |
|
| 891 |
[[nodiscard]] bool emitDeleteName(UnaryNode* deleteNode); |
| 892 |
[[nodiscard]] bool emitDeleteProperty(UnaryNode* deleteNode); |
| 893 |
[[nodiscard]] bool emitDeleteElement(UnaryNode* deleteNode); |
| 894 |
[[nodiscard]] bool emitDeleteExpression(UnaryNode* deleteNode); |
| 895 |
|
| 896 |
// Optional methods which emit Optional Jump Target |
| 897 |
[[nodiscard]] bool emitOptionalChain(UnaryNode* expr, ValueUsage valueUsage); |
| 898 |
[[nodiscard]] bool emitCalleeAndThisForOptionalChain(UnaryNode* expr, |
| 899 |
CallNode* callNode, |
| 900 |
CallOrNewEmitter& cone); |
| 901 |
[[nodiscard]] bool emitDeleteOptionalChain(UnaryNode* deleteNode); |
| 902 |
|
| 903 |
// Optional methods which emit a shortCircuit jump. They need to be called by |
| 904 |
// a method which emits an Optional Jump Target, see below. |
| 905 |
[[nodiscard]] bool emitOptionalDotExpression(PropertyAccessBase* expr, |
| 906 |
PropOpEmitter& poe, bool isSuper, |
| 907 |
OptionalEmitter& oe); |
| 908 |
[[nodiscard]] bool emitOptionalElemExpression(PropertyByValueBase* elem, |
| 909 |
ElemOpEmitter& eoe, |
| 910 |
bool isSuper, |
| 911 |
OptionalEmitter& oe); |
| 912 |
[[nodiscard]] bool emitOptionalPrivateExpression( |
| 913 |
PrivateMemberAccessBase* privateExpr, PrivateOpEmitter& xoe, |
| 914 |
OptionalEmitter& oe); |
| 915 |
[[nodiscard]] bool emitOptionalCall(CallNode* callNode, OptionalEmitter& oe, |
| 916 |
ValueUsage valueUsage); |
| 917 |
[[nodiscard]] bool emitDeletePropertyInOptChain(PropertyAccessBase* propExpr, |
| 918 |
OptionalEmitter& oe); |
| 919 |
[[nodiscard]] bool emitDeleteElementInOptChain(PropertyByValueBase* elemExpr, |
| 920 |
OptionalEmitter& oe); |
| 921 |
|
| 922 |
// |op| must be JSOp::Typeof or JSOp::TypeofExpr. |
| 923 |
[[nodiscard]] bool emitTypeof(UnaryNode* typeofNode, JSOp op); |
| 924 |
|
| 925 |
[[nodiscard]] bool tryEmitTypeofEq(ListNode* node, bool* emitted); |
| 926 |
|
| 927 |
[[nodiscard]] bool emitUnary(UnaryNode* unaryNode); |
| 928 |
[[nodiscard]] bool emitRightAssociative(ListNode* node); |
| 929 |
[[nodiscard]] bool tryEmitConstantEq(ListNode* node, JSOp op, bool* emitted); |
| 930 |
[[nodiscard]] bool emitLeftAssociative(ListNode* node); |
| 931 |
[[nodiscard]] bool emitPrivateInExpr(ListNode* node); |
| 932 |
[[nodiscard]] bool emitShortCircuit(ListNode* node, ValueUsage valueUsage); |
| 933 |
[[nodiscard]] bool emitSequenceExpr(ListNode* node, ValueUsage valueUsage); |
| 934 |
|
| 935 |
[[nodiscard]] MOZ_NEVER_INLINE bool emitIncOrDec(UnaryNode* incDec, |
| 936 |
ValueUsage valueUsage); |
| 937 |
|
| 938 |
[[nodiscard]] bool emitConditionalExpression( |
| 939 |
ConditionalExpression& conditional, ValueUsage valueUsage); |
| 940 |
|
| 941 |
[[nodiscard]] ParseNode* getCoordNode(ParseNode* callNode, |
| 942 |
ParseNode* calleeNode, JSOp op, |
| 943 |
ListNode* argsList) const; |
| 944 |
|
| 945 |
[[nodiscard]] bool emitArguments(ListNode* argsList, bool isCall, |
| 946 |
bool isSpread, CallOrNewEmitter& cone); |
| 947 |
[[nodiscard]] bool emitCallOrNew(CallNode* callNode, ValueUsage valueUsage); |
| 948 |
[[nodiscard]] bool emitDebugCheckSelfHosted(); |
| 949 |
[[nodiscard]] bool emitSelfHostedCallFunction(CallNode* callNode, JSOp op); |
| 950 |
[[nodiscard]] bool emitSelfHostedResumeGenerator(CallNode* callNode); |
| 951 |
[[nodiscard]] bool emitSelfHostedForceInterpreter(); |
| 952 |
[[nodiscard]] bool emitSelfHostedAllowContentIter(CallNode* callNode); |
| 953 |
[[nodiscard]] bool emitSelfHostedAllowContentIterWith(CallNode* callNode); |
| 954 |
[[nodiscard]] bool emitSelfHostedAllowContentIterWithNext(CallNode* callNode); |
| 955 |
[[nodiscard]] bool emitSelfHostedDefineDataProperty(CallNode* callNode); |
| 956 |
[[nodiscard]] bool emitSelfHostedGetPropertySuper(CallNode* callNode); |
| 957 |
[[nodiscard]] bool emitSelfHostedHasOwn(CallNode* callNode); |
| 958 |
[[nodiscard]] bool emitSelfHostedToNumeric(CallNode* callNode); |
| 959 |
[[nodiscard]] bool emitSelfHostedToString(CallNode* callNode); |
| 960 |
[[nodiscard]] bool emitSelfHostedIsNullOrUndefined(CallNode* callNode); |
| 961 |
[[nodiscard]] bool emitSelfHostedIteratorClose(CallNode* callNode); |
| 962 |
[[nodiscard]] bool emitSelfHostedGetBuiltinConstructor(CallNode* callNode); |
| 963 |
[[nodiscard]] bool emitSelfHostedGetBuiltinPrototype(CallNode* callNode); |
| 964 |
[[nodiscard]] bool emitSelfHostedGetBuiltinSymbol(CallNode* callNode); |
| 965 |
[[nodiscard]] bool emitSelfHostedSetIsInlinableLargeFunction( |
| 966 |
CallNode* callNode); |
| 967 |
[[nodiscard]] bool emitSelfHostedSetCanonicalName(CallNode* callNode); |
| 968 |
[[nodiscard]] bool emitSelfHostedArgumentsLength(CallNode* callNode); |
| 969 |
[[nodiscard]] bool emitSelfHostedGetArgument(CallNode* callNode); |
| 970 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 971 |
enum class DisposalKind : bool { Sync, Async }; |
| 972 |
[[nodiscard]] bool emitSelfHostedDisposeResources(CallNode* callNode, |
| 973 |
DisposalKind kind); |
| 974 |
#endif |
| 975 |
#ifdef DEBUG |
| 976 |
void assertSelfHostedExpectedTopLevel(ParseNode* node); |
| 977 |
void assertSelfHostedUnsafeGetReservedSlot(ListNode* argsList); |
| 978 |
void assertSelfHostedUnsafeSetReservedSlot(ListNode* argsList); |
| 979 |
#endif |
| 980 |
|
| 981 |
[[nodiscard]] bool emitDo(BinaryNode* doNode); |
| 982 |
[[nodiscard]] bool emitWhile(BinaryNode* whileNode); |
| 983 |
|
| 984 |
[[nodiscard]] bool emitFor( |
| 985 |
ForNode* forNode, const EmitterScope* headLexicalEmitterScope = nullptr); |
| 986 |
[[nodiscard]] bool emitCStyleFor(ForNode* forNode, |
| 987 |
const EmitterScope* headLexicalEmitterScope); |
| 988 |
[[nodiscard]] bool emitForIn(ForNode* forNode, |
| 989 |
const EmitterScope* headLexicalEmitterScope); |
| 990 |
[[nodiscard]] bool emitForOf(ForNode* forNode, |
| 991 |
const EmitterScope* headLexicalEmitterScope); |
| 992 |
|
| 993 |
[[nodiscard]] bool emitInitializeForInOrOfTarget(TernaryNode* forHead); |
| 994 |
|
| 995 |
[[nodiscard]] bool emitBreak(TaggedParserAtomIndex label); |
| 996 |
[[nodiscard]] bool emitContinue(TaggedParserAtomIndex label); |
| 997 |
|
| 998 |
[[nodiscard]] bool emitFunctionFormalParameters(ParamsBodyNode* paramsBody); |
| 999 |
[[nodiscard]] bool emitInitializeFunctionSpecialNames(); |
| 1000 |
[[nodiscard]] bool emitLexicalInitialization(NameNode* name); |
| 1001 |
[[nodiscard]] bool emitLexicalInitialization(TaggedParserAtomIndex name); |
| 1002 |
|
| 1003 |
// Emit bytecode for the spread operator. |
| 1004 |
// |
| 1005 |
// emitSpread expects some values representing the spread target (an array or |
| 1006 |
// a tuple), the iterator and it's next() method to be on the stack in that |
| 1007 |
// order (iterator's next() on the bottom). |
| 1008 |
// The number of values representing the spread target is |
| 1009 |
// `spreadeeStackItems`: it's 2 for arrays (one for the array and one for the |
| 1010 |
// index) and 1 for tuples (the tuple itself). |
| 1011 |
// Since arrays and tuples use different opcodes to initialize new elements, |
| 1012 |
// it must be specified using `storeElementOp`. |
| 1013 |
// When emitSpread() finishes, the stack only contains the values representing |
| 1014 |
// the spread target. |
| 1015 |
[[nodiscard]] bool emitSpread(SelfHostedIter selfHostedIter, |
| 1016 |
int spreadeeStackItems, JSOp storeElementOp); |
| 1017 |
// This shortcut can be used when spreading into arrays, as it assumes |
| 1018 |
// `spreadeeStackItems = 2` (|ARRAY INDEX|) and `storeElementOp = |
| 1019 |
// JSOp::InitElemInc` |
| 1020 |
[[nodiscard]] bool emitSpread(SelfHostedIter selfHostedIter); |
| 1021 |
|
| 1022 |
enum class ClassNameKind { |
| 1023 |
// The class name is defined through its BindingIdentifier, if present. |
| 1024 |
BindingName, |
| 1025 |
|
| 1026 |
// The class is anonymous and has a statically inferred name. |
| 1027 |
InferredName, |
| 1028 |
|
| 1029 |
// The class is anonymous and has a dynamically computed name. |
| 1030 |
ComputedName |
| 1031 |
}; |
| 1032 |
|
| 1033 |
[[nodiscard]] bool emitClass( |
| 1034 |
ClassNode* classNode, ClassNameKind nameKind = ClassNameKind::BindingName, |
| 1035 |
TaggedParserAtomIndex nameForAnonymousClass = |
| 1036 |
TaggedParserAtomIndex::null()); |
| 1037 |
|
| 1038 |
[[nodiscard]] bool emitSuperElemOperands( |
| 1039 |
PropertyByValue* elem, EmitElemOption opts = EmitElemOption::Get); |
| 1040 |
[[nodiscard]] bool emitSuperGetElem(PropertyByValue* elem, |
| 1041 |
bool isCall = false); |
| 1042 |
|
| 1043 |
[[nodiscard]] bool emitCalleeAndThis(ParseNode* callee, CallNode* maybeCall, |
| 1044 |
CallOrNewEmitter& cone); |
| 1045 |
|
| 1046 |
[[nodiscard]] bool emitOptionalCalleeAndThis(ParseNode* callee, |
| 1047 |
CallNode* call, |
| 1048 |
CallOrNewEmitter& cone, |
| 1049 |
OptionalEmitter& oe); |
| 1050 |
|
| 1051 |
[[nodiscard]] bool emitExportDefault(BinaryNode* exportNode); |
| 1052 |
|
| 1053 |
[[nodiscard]] bool emitReturnRval() { return emit1(JSOp::RetRval); } |
| 1054 |
|
| 1055 |
[[nodiscard]] bool emitCheckPrivateField(ThrowCondition throwCondition, |
| 1056 |
ThrowMsgKind msgKind) { |
| 1057 |
return emit3(JSOp::CheckPrivateField, uint8_t(throwCondition), |
| 1058 |
uint8_t(msgKind)); |
| 1059 |
} |
| 1060 |
|
| 1061 |
[[nodiscard]] bool emitNewPrivateName(TaggedParserAtomIndex bindingName, |
| 1062 |
TaggedParserAtomIndex symbolName); |
| 1063 |
|
| 1064 |
template <class ClassMemberType> |
| 1065 |
[[nodiscard]] bool emitNewPrivateNames(ListNode* classMembers); |
| 1066 |
|
| 1067 |
[[nodiscard]] bool emitNewPrivateNames(TaggedParserAtomIndex privateBrandName, |
| 1068 |
ListNode* classMembers); |
| 1069 |
|
| 1070 |
[[nodiscard]] js::UniquePtr<ImmutableScriptData> createImmutableScriptData(); |
| 1071 |
|
| 1072 |
#if defined(ENABLE_DECORATORS) || defined(ENABLE_EXPLICIT_RESOURCE_MANAGEMENT) |
| 1073 |
[[nodiscard]] bool emitCheckIsCallable(); |
| 1074 |
#endif |
| 1075 |
|
| 1076 |
private: |
| 1077 |
[[nodiscard]] SelfHostedIter getSelfHostedIterFor(ParseNode* parseNode) const; |
| 1078 |
|
| 1079 |
[[nodiscard]] bool emitSelfHostedGetBuiltinConstructorOrPrototype( |
| 1080 |
CallNode* callNode, bool isConstructor); |
| 1081 |
|
| 1082 |
public: |
| 1083 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1084 |
void dumpAtom(TaggedParserAtomIndex index) const; |
| 1085 |
#endif |
| 1086 |
}; |
| 1087 |
|
| 1088 |
class MOZ_RAII AutoCheckUnstableEmitterScope { |
| 1089 |
#ifdef DEBUG |
| 1090 |
bool prev_; |
| 1091 |
BytecodeEmitter* bce_; |
| 1092 |
#endif |
| 1093 |
|
| 1094 |
public: |
| 1095 |
AutoCheckUnstableEmitterScope() = delete; |
| 1096 |
explicit AutoCheckUnstableEmitterScope(BytecodeEmitter* bce) |
| 1097 |
#ifdef DEBUG |
| 1098 |
: bce_(bce) |
| 1099 |
#endif |
| 1100 |
{ |
| 1101 |
#ifdef DEBUG |
| 1102 |
prev_ = bce_->unstableEmitterScope; |
| 1103 |
bce_->unstableEmitterScope = true; |
| 1104 |
#endif |
| 1105 |
} |
| 1106 |
~AutoCheckUnstableEmitterScope() { |
| 1107 |
#ifdef DEBUG |
| 1108 |
bce_->unstableEmitterScope = prev_; |
| 1109 |
#endif |
| 1110 |
} |
| 1111 |
}; |
| 1112 |
|
| 1113 |
} /* namespace frontend */ |
| 1114 |
} /* namespace js */ |
| 1115 |
|
| 1116 |
#endif /* frontend_BytecodeEmitter_h */ |