| 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 |
#ifndef frontend_BytecodeControlStructures_h |
| 8 |
#define frontend_BytecodeControlStructures_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 11 |
#include "mozilla/Maybe.h" // mozilla::Maybe |
| 12 |
|
| 13 |
#include <stdint.h> // int32_t, uint32_t |
| 14 |
|
| 15 |
#include "ds/Nestable.h" // Nestable |
| 16 |
#include "frontend/BytecodeOffset.h" // BytecodeOffset |
| 17 |
#include "frontend/JumpList.h" // JumpList, JumpTarget |
| 18 |
#include "frontend/ParserAtom.h" // TaggedParserAtomIndex |
| 19 |
#include "frontend/SharedContext.h" // StatementKind, StatementKindIsLoop, StatementKindIsUnlabeledBreakTarget |
| 20 |
#include "frontend/TDZCheckCache.h" // TDZCheckCache |
| 21 |
#include "vm/StencilEnums.h" // TryNoteKind |
| 22 |
|
| 23 |
namespace js { |
| 24 |
namespace frontend { |
| 25 |
|
| 26 |
struct BytecodeEmitter; |
| 27 |
class EmitterScope; |
| 28 |
|
| 29 |
class NestableControl : public Nestable<NestableControl> { |
| 30 |
StatementKind kind_; |
| 31 |
|
| 32 |
// The innermost scope when this was pushed. |
| 33 |
EmitterScope* emitterScope_; |
| 34 |
|
| 35 |
protected: |
| 36 |
NestableControl(BytecodeEmitter* bce, StatementKind kind); |
| 37 |
|
| 38 |
public: |
| 39 |
using Nestable<NestableControl>::enclosing; |
| 40 |
using Nestable<NestableControl>::findNearest; |
| 41 |
|
| 42 |
StatementKind kind() const { return kind_; } |
| 43 |
|
| 44 |
EmitterScope* emitterScope() const { return emitterScope_; } |
| 45 |
|
| 46 |
template <typename T> |
| 47 |
bool is() const; |
| 48 |
|
| 49 |
template <typename T> |
| 50 |
T& as() { |
| 51 |
MOZ_ASSERT(this->is<T>()); |
| 52 |
return static_cast<T&>(*this); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
class BreakableControl : public NestableControl { |
| 57 |
public: |
| 58 |
// Offset of the last break. |
| 59 |
JumpList breaks; |
| 60 |
|
| 61 |
BreakableControl(BytecodeEmitter* bce, StatementKind kind); |
| 62 |
|
| 63 |
[[nodiscard]] bool patchBreaks(BytecodeEmitter* bce); |
| 64 |
}; |
| 65 |
template <> |
| 66 |
inline bool NestableControl::is<BreakableControl>() const { |
| 67 |
return StatementKindIsUnlabeledBreakTarget(kind_) || |
| 68 |
kind_ == StatementKind::Label; |
| 69 |
} |
| 70 |
|
| 71 |
class LabelControl : public BreakableControl { |
| 72 |
TaggedParserAtomIndex label_; |
| 73 |
|
| 74 |
// The code offset when this was pushed. Used for effectfulness checking. |
| 75 |
BytecodeOffset startOffset_; |
| 76 |
|
| 77 |
public: |
| 78 |
LabelControl(BytecodeEmitter* bce, TaggedParserAtomIndex label, |
| 79 |
BytecodeOffset startOffset); |
| 80 |
|
| 81 |
TaggedParserAtomIndex label() const { return label_; } |
| 82 |
|
| 83 |
BytecodeOffset startOffset() const { return startOffset_; } |
| 84 |
}; |
| 85 |
template <> |
| 86 |
inline bool NestableControl::is<LabelControl>() const { |
| 87 |
return kind_ == StatementKind::Label; |
| 88 |
} |
| 89 |
|
| 90 |
class LoopControl : public BreakableControl { |
| 91 |
// Loops' children are emitted in dominance order, so they can always |
| 92 |
// have a TDZCheckCache. |
| 93 |
TDZCheckCache tdzCache_; |
| 94 |
|
| 95 |
// Here's the basic structure of a loop: |
| 96 |
// |
| 97 |
// head: |
| 98 |
// JSOp::LoopHead |
| 99 |
// {loop condition/body} |
| 100 |
// |
| 101 |
// continueTarget: |
| 102 |
// {loop update if present} |
| 103 |
// |
| 104 |
// # Loop end, backward jump |
| 105 |
// JSOp::Goto/JSOp::JumpIfTrue head |
| 106 |
// |
| 107 |
// breakTarget: |
| 108 |
|
| 109 |
// The bytecode offset of JSOp::LoopHead. |
| 110 |
JumpTarget head_; |
| 111 |
|
| 112 |
// Stack depth when this loop was pushed on the control stack. |
| 113 |
int32_t stackDepth_; |
| 114 |
|
| 115 |
// The loop nesting depth. Used as a hint to Ion. |
| 116 |
uint32_t loopDepth_; |
| 117 |
|
| 118 |
public: |
| 119 |
// Offset of the last continue in the loop. |
| 120 |
JumpList continues; |
| 121 |
|
| 122 |
LoopControl(BytecodeEmitter* bce, StatementKind loopKind); |
| 123 |
|
| 124 |
BytecodeOffset headOffset() const { return head_.offset; } |
| 125 |
|
| 126 |
[[nodiscard]] bool emitContinueTarget(BytecodeEmitter* bce); |
| 127 |
|
| 128 |
// `nextPos` is the offset in the source code for the character that |
| 129 |
// corresponds to the next instruction after JSOp::LoopHead. |
| 130 |
// Can be Nothing() if not available. |
| 131 |
[[nodiscard]] bool emitLoopHead(BytecodeEmitter* bce, |
| 132 |
const mozilla::Maybe<uint32_t>& nextPos); |
| 133 |
|
| 134 |
[[nodiscard]] bool emitLoopEnd(BytecodeEmitter* bce, JSOp op, |
| 135 |
TryNoteKind tryNoteKind); |
| 136 |
}; |
| 137 |
template <> |
| 138 |
inline bool NestableControl::is<LoopControl>() const { |
| 139 |
return StatementKindIsLoop(kind_); |
| 140 |
} |
| 141 |
|
| 142 |
enum class NonLocalExitKind { Continue, Break, Return }; |
| 143 |
|
| 144 |
class TryFinallyContinuation { |
| 145 |
public: |
| 146 |
TryFinallyContinuation(NestableControl* target, NonLocalExitKind kind) |
| 147 |
: target_(target), kind_(kind) {} |
| 148 |
|
| 149 |
NestableControl* target_; |
| 150 |
NonLocalExitKind kind_; |
| 151 |
}; |
| 152 |
|
| 153 |
class TryFinallyControl : public NestableControl { |
| 154 |
bool emittingSubroutine_ = false; |
| 155 |
|
| 156 |
public: |
| 157 |
// Offset of the last jump to this `finally`. |
| 158 |
JumpList finallyJumps_; |
| 159 |
|
| 160 |
js::Vector<TryFinallyContinuation, 4, SystemAllocPolicy> continuations_; |
| 161 |
|
| 162 |
TryFinallyControl(BytecodeEmitter* bce, StatementKind kind); |
| 163 |
|
| 164 |
void setEmittingSubroutine() { emittingSubroutine_ = true; } |
| 165 |
|
| 166 |
bool emittingSubroutine() const { return emittingSubroutine_; } |
| 167 |
|
| 168 |
enum SpecialContinuations { Fallthrough, Count }; |
| 169 |
bool allocateContinuation(NestableControl* target, NonLocalExitKind kind, |
| 170 |
uint32_t* idx); |
| 171 |
bool emitContinuations(BytecodeEmitter* bce); |
| 172 |
}; |
| 173 |
template <> |
| 174 |
inline bool NestableControl::is<TryFinallyControl>() const { |
| 175 |
return kind_ == StatementKind::Try || kind_ == StatementKind::Finally; |
| 176 |
} |
| 177 |
|
| 178 |
class NonLocalExitControl { |
| 179 |
BytecodeEmitter* bce_; |
| 180 |
const uint32_t savedScopeNoteIndex_; |
| 181 |
const int savedDepth_; |
| 182 |
uint32_t openScopeNoteIndex_; |
| 183 |
NonLocalExitKind kind_; |
| 184 |
|
| 185 |
// The offset of a `JSOp::SetRval` that can be rewritten as a |
| 186 |
// `JSOp::Return` if we don't generate any code for this |
| 187 |
// NonLocalExitControl. |
| 188 |
BytecodeOffset setRvalOffset_ = BytecodeOffset::invalidOffset(); |
| 189 |
|
| 190 |
[[nodiscard]] bool leaveScope(EmitterScope* es); |
| 191 |
|
| 192 |
public: |
| 193 |
NonLocalExitControl(const NonLocalExitControl&) = delete; |
| 194 |
NonLocalExitControl(BytecodeEmitter* bce, NonLocalExitKind kind); |
| 195 |
~NonLocalExitControl(); |
| 196 |
|
| 197 |
[[nodiscard]] bool emitNonLocalJump(NestableControl* target, |
| 198 |
NestableControl* startingAfter = nullptr); |
| 199 |
[[nodiscard]] bool emitReturn(BytecodeOffset setRvalOffset); |
| 200 |
}; |
| 201 |
|
| 202 |
} /* namespace frontend */ |
| 203 |
} /* namespace js */ |
| 204 |
|
| 205 |
#endif /* frontend_BytecodeControlStructures_h */ |