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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/* -*- 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/. */
#ifndef frontend_BytecodeSection_h
#define frontend_BytecodeSection_h
#include "mozilla/Attributes.h" // MOZ_STACK_CLASS
#include "mozilla/Maybe.h" // mozilla::Maybe
#include "mozilla/Span.h" // mozilla::Span
#include <stddef.h> // ptrdiff_t, size_t
#include <stdint.h> // uint16_t, int32_t, uint32_t
#include "frontend/AbstractScopePtr.h" // AbstractScopePtr, ScopeIndex
#include "frontend/BytecodeOffset.h" // BytecodeOffset
#include "frontend/CompilationStencil.h" // CompilationStencil, CompilationGCOutput, CompilationAtomCache
#include "frontend/FrontendContext.h" // FrontendContext
#include "frontend/JumpList.h" // JumpTarget
#include "frontend/NameCollections.h" // AtomIndexMap, PooledMapPtr
#include "frontend/ParseNode.h" // BigIntLiteral
#include "frontend/ParserAtom.h" // ParserAtomsTable, TaggedParserAtomIndex, ParserAtom
#include "frontend/SourceNotes.h" // SrcNote
#include "frontend/Stencil.h" // Stencils
#include "js/TypeDecls.h" // jsbytecode, JSContext
#include "js/Vector.h" // Vector
#include "vm/SharedStencil.h" // TryNote, ScopeNote, GCThingIndex
#include "vm/StencilEnums.h" // TryNoteKind
namespace js {
namespace frontend {
class FunctionBox;
struct MOZ_STACK_CLASS GCThingList {
// The BCE accumulates TaggedScriptThingIndex items so use a vector type. We
// reserve some stack slots to avoid allocating for most small scripts.
using ScriptThingsStackVector = Vector<TaggedScriptThingIndex, 8>;
CompilationState& compilationState;
ScriptThingsStackVector vector;
// Index of the first scope in the vector.
mozilla::Maybe<GCThingIndex> firstScopeIndex;
explicit GCThingList(FrontendContext* fc, CompilationState& compilationState)
: compilationState(compilationState), vector(fc) {}
[[nodiscard]] bool append(TaggedParserAtomIndex atom,
ParserAtom::Atomize atomize, GCThingIndex* index) {
*index = GCThingIndex(vector.length());
compilationState.parserAtoms.markUsedByStencil(atom, atomize);
if (!vector.emplaceBack(atom)) {
return false;
}
return true;
}
[[nodiscard]] bool append(ScopeIndex scope, GCThingIndex* index) {
*index = GCThingIndex(vector.length());
if (!vector.emplaceBack(scope)) {
return false;
}
if (!firstScopeIndex) {
firstScopeIndex.emplace(*index);
}
return true;
}
[[nodiscard]] bool append(BigIntLiteral* literal, GCThingIndex* index) {
*index = GCThingIndex(vector.length());
if (!vector.emplaceBack(literal->index())) {
return false;
}
return true;
}
[[nodiscard]] bool append(RegExpLiteral* literal, GCThingIndex* index) {
*index = GCThingIndex(vector.length());
if (!vector.emplaceBack(literal->index())) {
return false;
}
return true;
}
[[nodiscard]] bool append(ObjLiteralIndex objlit, GCThingIndex* index) {
*index = GCThingIndex(vector.length());
if (!vector.emplaceBack(objlit)) {
return false;
}
return true;
}
[[nodiscard]] bool append(FunctionBox* funbox, GCThingIndex* index);
[[nodiscard]] bool appendEmptyGlobalScope(GCThingIndex* index) {
*index = GCThingIndex(vector.length());
EmptyGlobalScopeType emptyGlobalScope;
if (!vector.emplaceBack(emptyGlobalScope)) {
return false;
}
if (!firstScopeIndex) {
firstScopeIndex.emplace(*index);
}
return true;
}
uint32_t length() const { return vector.length(); }
const ScriptThingsStackVector& objects() { return vector; }
AbstractScopePtr getScope(size_t index) const;
// Index of scope within CompilationStencil or Nothing is the scope is
// EmptyGlobalScopeType.
mozilla::Maybe<ScopeIndex> getScopeIndex(size_t index) const;
TaggedParserAtomIndex getAtom(size_t index) const;
AbstractScopePtr firstScope() const {
MOZ_ASSERT(firstScopeIndex.isSome());
return getScope(*firstScopeIndex);
}
};
[[nodiscard]] bool EmitScriptThingsVector(
JSContext* cx, const CompilationAtomCache& atomCache,
const CompilationStencil& stencil, CompilationGCOutput& gcOutput,
mozilla::Span<const TaggedScriptThingIndex> things,
mozilla::Span<JS::GCCellPtr> output);
struct CGTryNoteList {
Vector<TryNote, 0> list;
explicit CGTryNoteList(FrontendContext* fc) : list(fc) {}
[[nodiscard]] bool append(TryNoteKind kind, uint32_t stackDepth,
BytecodeOffset start, BytecodeOffset end);
mozilla::Span<const TryNote> span() const {
return {list.begin(), list.length()};
}
size_t length() const { return list.length(); }
};
struct CGScopeNoteList {
Vector<ScopeNote, 0> list;
explicit CGScopeNoteList(FrontendContext* fc) : list(fc) {}
[[nodiscard]] bool append(GCThingIndex scopeIndex, BytecodeOffset offset,
uint32_t parent);
void recordEnd(uint32_t index, BytecodeOffset offset);
void recordEndFunctionBodyVar(uint32_t index);
mozilla::Span<const ScopeNote> span() const {
return {list.begin(), list.length()};
}
size_t length() const { return list.length(); }
private:
void recordEndImpl(uint32_t index, uint32_t offset);
};
struct CGResumeOffsetList {
Vector<uint32_t, 0> list;
explicit CGResumeOffsetList(FrontendContext* fc) : list(fc) {}
[[nodiscard]] bool append(uint32_t offset) { return list.append(offset); }
mozilla::Span<const uint32_t> span() const {
return {list.begin(), list.length()};
}
size_t length() const { return list.length(); }
};
static constexpr size_t MaxBytecodeLength = INT32_MAX;
static constexpr size_t MaxSrcNotesLength = INT32_MAX;
// Have a few inline elements, so as to avoid heap allocation for tiny
// sequences. See bug 1390526.
typedef Vector<jsbytecode, 64> BytecodeVector;
typedef Vector<js::SrcNote, 64> SrcNotesVector;
// Bytecode and all data directly associated with specific opcode/index inside
// bytecode is stored in this class.
class BytecodeSection {
public:
BytecodeSection(FrontendContext* fc, uint32_t lineNum, uint32_t column);
// ---- Bytecode ----
BytecodeVector& code() { return code_; }
const BytecodeVector& code() const { return code_; }
jsbytecode* code(BytecodeOffset offset) {
return code_.begin() + offset.value();
}
BytecodeOffset offset() const {
return BytecodeOffset(code_.end() - code_.begin());
}
// ---- Source notes ----
SrcNotesVector& notes() { return notes_; }
const SrcNotesVector& notes() const { return notes_; }
BytecodeOffset lastNoteOffset() const { return lastNoteOffset_; }
void setLastNoteOffset(BytecodeOffset offset) { lastNoteOffset_ = offset; }
// ---- Jump ----
BytecodeOffset lastTargetOffset() const { return lastTarget_.offset; }
void setLastTargetOffset(BytecodeOffset offset) {
lastTarget_.offset = offset;
}
// ---- Stack ----
int32_t stackDepth() const { return stackDepth_; }
void setStackDepth(int32_t depth) { stackDepth_ = depth; }
uint32_t maxStackDepth() const { return maxStackDepth_; }
void updateDepth(JSOp op, BytecodeOffset target);
// ---- Try notes ----
CGTryNoteList& tryNoteList() { return tryNoteList_; };
const CGTryNoteList& tryNoteList() const { return tryNoteList_; };
// ---- Scope ----
CGScopeNoteList& scopeNoteList() { return scopeNoteList_; };
const CGScopeNoteList& scopeNoteList() const { return scopeNoteList_; };
// ---- Generator ----
CGResumeOffsetList& resumeOffsetList() { return resumeOffsetList_; }
const CGResumeOffsetList& resumeOffsetList() const {
return resumeOffsetList_;
}
uint32_t numYields() const { return numYields_; }
void addNumYields() { numYields_++; }
// ---- Line and column ----
uint32_t currentLine() const { return currentLine_; }
uint32_t lastColumn() const { return lastColumn_; }
void setCurrentLine(uint32_t line, uint32_t sourceOffset) {
currentLine_ = line;
lastColumn_ = 0;
lastSourceOffset_ = sourceOffset;
}
void setLastColumn(uint32_t column, uint32_t offset) {
lastColumn_ = column;
lastSourceOffset_ = offset;
}
void updateSeparatorPosition() {
lastSeparatorCodeOffset_ = code().length();
lastSeparatorSourceOffset_ = lastSourceOffset_;
lastSeparatorLine_ = currentLine_;
lastSeparatorColumn_ = lastColumn_;
}
void updateSeparatorPositionIfPresent() {
if (lastSeparatorCodeOffset_ == code().length()) {
lastSeparatorSourceOffset_ = lastSourceOffset_;
lastSeparatorLine_ = currentLine_;
lastSeparatorColumn_ = lastColumn_;
}
}
bool isDuplicateLocation() const {
return lastSeparatorLine_ == currentLine_ &&
lastSeparatorColumn_ == lastColumn_;
}
bool atSeparator(uint32_t offset) const {
return lastSeparatorSourceOffset_ == offset;
}
// ---- JIT ----
uint32_t numICEntries() const { return numICEntries_; }
void incrementNumICEntries() {
MOZ_ASSERT(numICEntries_ != UINT32_MAX, "Shouldn't overflow");
numICEntries_++;
}
void setNumICEntries(uint32_t entries) { numICEntries_ = entries; }
private:
// ---- Bytecode ----
// Bytecode.
BytecodeVector code_;
// ---- Source notes ----
// Source notes
SrcNotesVector notes_;
// Code offset for last source note
BytecodeOffset lastNoteOffset_;
// ---- Jump ----
// Last jump target emitted.
JumpTarget lastTarget_;
// ---- Stack ----
// Maximum number of expression stack slots so far.
uint32_t maxStackDepth_ = 0;
// Current stack depth in script frame.
int32_t stackDepth_ = 0;
// ---- Try notes ----
// List of emitted try notes.
CGTryNoteList tryNoteList_;
// ---- Scope ----
// List of emitted block scope notes.
CGScopeNoteList scopeNoteList_;
// ---- Generator ----
// Certain ops (yield, await) have an entry in the script's resumeOffsets
// list. This can be used to map from the op's resumeIndex to the bytecode
// offset of the next pc. This indirection makes it easy to resume in the JIT
// (because BaselineScript stores a resumeIndex => native code array).
CGResumeOffsetList resumeOffsetList_;
// Number of yield instructions emitted. Does not include JSOp::Await.
uint32_t numYields_ = 0;
// ---- Line and column ----
// Line number for srcnotes.
//
// WARNING: If this becomes out of sync with already-emitted srcnotes,
// we can get undefined behavior.
uint32_t currentLine_;
// Zero-based column index on currentLine_ of last
// SrcNoteType::ColSpan-annotated opcode.
//
// WARNING: If this becomes out of sync with already-emitted srcnotes,
// we can get undefined behavior.
uint32_t lastColumn_ = 0;
// The last code unit used for srcnotes.
uint32_t lastSourceOffset_ = 0;
// The offset, line and column numbers of the last opcode for the
// breakpoint for step execution.
uint32_t lastSeparatorCodeOffset_ = 0;
uint32_t lastSeparatorSourceOffset_ = 0;
uint32_t lastSeparatorLine_ = 0;
uint32_t lastSeparatorColumn_ = 0;
// ---- JIT ----
// Number of ICEntries in the script. There's one ICEntry for each JOF_IC op
// and, if the script is a function, for |this| and each formal argument.
uint32_t numICEntries_ = 0;
};
// Data that is not directly associated with specific opcode/index inside
// bytecode, but referred from bytecode is stored in this class.
class PerScriptData {
public:
PerScriptData(FrontendContext* fc,
frontend::CompilationState& compilationState);
[[nodiscard]] bool init(FrontendContext* fc);
GCThingList& gcThingList() { return gcThingList_; }
const GCThingList& gcThingList() const { return gcThingList_; }
PooledMapPtr<AtomIndexMap>& atomIndices() { return atomIndices_; }
const PooledMapPtr<AtomIndexMap>& atomIndices() const { return atomIndices_; }
private:
// List of emitted scopes/objects/bigints.
GCThingList gcThingList_;
// Map from atom to index.
PooledMapPtr<AtomIndexMap> atomIndices_;
};
} /* namespace frontend */
} /* namespace js */
#endif /* frontend_BytecodeSection_h */
|