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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/* -*- 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 "frontend/SwitchEmitter.h"
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "mozilla/Span.h" // mozilla::Span
#include <algorithm> // std::min, std::max
#include "jstypes.h" // JS_BIT
#include "frontend/BytecodeEmitter.h" // BytecodeEmitter
#include "frontend/SharedContext.h" // StatementKind
#include "js/friend/ErrorMessages.h" // JSMSG_*
#include "js/TypeDecls.h" // jsbytecode
#include "util/BitArray.h"
#include "vm/BytecodeUtil.h" // SET_JUMP_OFFSET, JUMP_OFFSET_LEN, SET_RESUMEINDEX
#include "vm/Opcodes.h" // JSOp, JSOpLength_TableSwitch
#include "vm/Runtime.h" // ReportOutOfMemory
using namespace js;
using namespace js::frontend;
using mozilla::Maybe;
bool SwitchEmitter::TableGenerator::addNumber(int32_t caseValue) {
if (isInvalid()) {
return true;
}
if (unsigned(caseValue + int(Bit(15))) >= unsigned(Bit(16))) {
setInvalid();
return true;
}
if (intmap_.isNothing()) {
intmap_.emplace();
}
low_ = std::min(low_, caseValue);
high_ = std::max(high_, caseValue);
// Check for duplicates, which are not supported in a table switch.
// We bias caseValue by 65536 if it's negative, and hope that's a rare case
// (because it requires a malloc'd bitmap).
if (caseValue < 0) {
caseValue += Bit(16);
}
if (caseValue >= intmapBitLength_) {
size_t newLength = NumWordsForBitArrayOfLength(caseValue + 1);
if (!intmap_->resize(newLength)) {
ReportOutOfMemory(bce_->cx);
return false;
}
intmapBitLength_ = newLength * BitArrayElementBits;
}
if (IsBitArrayElementSet(intmap_->begin(), intmap_->length(), caseValue)) {
// Duplicate entry is not supported in table switch.
setInvalid();
return true;
}
SetBitArrayElement(intmap_->begin(), intmap_->length(), caseValue);
return true;
}
void SwitchEmitter::TableGenerator::finish(uint32_t caseCount) {
intmap_.reset();
#ifdef DEBUG
finished_ = true;
#endif
if (isInvalid()) {
return;
}
if (caseCount == 0) {
low_ = 0;
high_ = -1;
return;
}
// Compute table length. Don't use table switch if overlarge or more than
// half-sparse.
tableLength_ = uint32_t(high_ - low_ + 1);
if (tableLength_ >= Bit(16) || tableLength_ > 2 * caseCount) {
setInvalid();
}
}
uint32_t SwitchEmitter::TableGenerator::toCaseIndex(int32_t caseValue) const {
MOZ_ASSERT(finished_);
MOZ_ASSERT(isValid());
uint32_t caseIndex = uint32_t(caseValue - low_);
MOZ_ASSERT(caseIndex < tableLength_);
return caseIndex;
}
uint32_t SwitchEmitter::TableGenerator::tableLength() const {
MOZ_ASSERT(finished_);
MOZ_ASSERT(isValid());
return tableLength_;
}
SwitchEmitter::SwitchEmitter(BytecodeEmitter* bce) : bce_(bce) {}
bool SwitchEmitter::emitDiscriminant(const Maybe<uint32_t>& switchPos) {
MOZ_ASSERT(state_ == State::Start);
switchPos_ = switchPos;
if (switchPos_) {
// Ensure that the column of the switch statement is set properly.
if (!bce_->updateSourceCoordNotes(*switchPos_)) {
return false;
}
}
state_ = State::Discriminant;
return true;
}
bool SwitchEmitter::emitLexical(LexicalScope::ParserData* bindings) {
MOZ_ASSERT(state_ == State::Discriminant);
MOZ_ASSERT(bindings);
tdzCacheLexical_.emplace(bce_);
emitterScope_.emplace(bce_);
if (!emitterScope_->enterLexical(bce_, ScopeKind::Lexical, bindings)) {
return false;
}
state_ = State::Lexical;
return true;
}
bool SwitchEmitter::validateCaseCount(uint32_t caseCount) {
MOZ_ASSERT(state_ == State::Discriminant || state_ == State::Lexical);
if (caseCount > Bit(16)) {
bce_->reportError(switchPos_, JSMSG_TOO_MANY_CASES);
return false;
}
caseCount_ = caseCount;
state_ = State::CaseCount;
return true;
}
bool SwitchEmitter::emitCond() {
MOZ_ASSERT(state_ == State::CaseCount);
kind_ = Kind::Cond;
// After entering the scope if necessary, push the switch control.
controlInfo_.emplace(bce_, StatementKind::Switch);
top_ = bce_->bytecodeSection().offset();
if (!caseOffsets_.resize(caseCount_)) {
ReportOutOfMemory(bce_->cx);
return false;
}
MOZ_ASSERT(top_ == bce_->bytecodeSection().offset());
tdzCacheCaseAndBody_.emplace(bce_);
state_ = State::Cond;
return true;
}
bool SwitchEmitter::emitTable(const TableGenerator& tableGen) {
MOZ_ASSERT(state_ == State::CaseCount);
kind_ = Kind::Table;
// After entering the scope if necessary, push the switch control.
controlInfo_.emplace(bce_, StatementKind::Switch);
top_ = bce_->bytecodeSection().offset();
if (!caseOffsets_.resize(tableGen.tableLength())) {
ReportOutOfMemory(bce_->cx);
return false;
}
MOZ_ASSERT(top_ == bce_->bytecodeSection().offset());
if (!bce_->emitN(JSOp::TableSwitch,
JSOpLength_TableSwitch - sizeof(jsbytecode))) {
return false;
}
// Skip default offset.
jsbytecode* pc =
bce_->bytecodeSection().code(top_ + BytecodeOffsetDiff(JUMP_OFFSET_LEN));
// Fill in switch bounds, which we know fit in 16-bit offsets.
SET_JUMP_OFFSET(pc, tableGen.low());
SET_JUMP_OFFSET(pc + JUMP_OFFSET_LEN, tableGen.high());
state_ = State::Table;
return true;
}
bool SwitchEmitter::emitCaseOrDefaultJump(uint32_t caseIndex, bool isDefault) {
MOZ_ASSERT(kind_ == Kind::Cond);
if (isDefault) {
if (!bce_->emitJump(JSOp::Default, &condSwitchDefaultOffset_)) {
return false;
}
return true;
}
JumpList caseJump;
if (!bce_->emitJump(JSOp::Case, &caseJump)) {
return false;
}
caseOffsets_[caseIndex] = caseJump.offset;
lastCaseOffset_ = caseJump.offset;
return true;
}
bool SwitchEmitter::prepareForCaseValue() {
MOZ_ASSERT(kind_ == Kind::Cond);
MOZ_ASSERT(state_ == State::Cond || state_ == State::Case);
if (!bce_->emit1(JSOp::Dup)) {
return false;
}
state_ = State::CaseValue;
return true;
}
bool SwitchEmitter::emitCaseJump() {
MOZ_ASSERT(kind_ == Kind::Cond);
MOZ_ASSERT(state_ == State::CaseValue);
if (!bce_->emit1(JSOp::StrictEq)) {
return false;
}
if (!emitCaseOrDefaultJump(caseIndex_, false)) {
return false;
}
caseIndex_++;
state_ = State::Case;
return true;
}
bool SwitchEmitter::emitImplicitDefault() {
MOZ_ASSERT(kind_ == Kind::Cond);
MOZ_ASSERT(state_ == State::Cond || state_ == State::Case);
if (!emitCaseOrDefaultJump(0, true)) {
return false;
}
caseIndex_ = 0;
// No internal state after emitting default jump.
return true;
}
bool SwitchEmitter::emitCaseBody() {
MOZ_ASSERT(kind_ == Kind::Cond);
MOZ_ASSERT(state_ == State::Cond || state_ == State::Case ||
state_ == State::CaseBody || state_ == State::DefaultBody);
tdzCacheCaseAndBody_.reset();
if (state_ == State::Cond || state_ == State::Case) {
// For cond switch, JSOp::Default is always emitted.
if (!emitImplicitDefault()) {
return false;
}
}
JumpList caseJump;
caseJump.offset = caseOffsets_[caseIndex_];
if (!bce_->emitJumpTargetAndPatch(caseJump)) {
return false;
}
JumpTarget here;
if (!bce_->emitJumpTarget(&here)) {
return false;
}
caseIndex_++;
tdzCacheCaseAndBody_.emplace(bce_);
state_ = State::CaseBody;
return true;
}
bool SwitchEmitter::emitCaseBody(int32_t caseValue,
const TableGenerator& tableGen) {
MOZ_ASSERT(kind_ == Kind::Table);
MOZ_ASSERT(state_ == State::Table || state_ == State::CaseBody ||
state_ == State::DefaultBody);
tdzCacheCaseAndBody_.reset();
JumpTarget here;
if (!bce_->emitJumpTarget(&here)) {
return false;
}
caseOffsets_[tableGen.toCaseIndex(caseValue)] = here.offset;
tdzCacheCaseAndBody_.emplace(bce_);
state_ = State::CaseBody;
return true;
}
bool SwitchEmitter::emitDefaultBody() {
MOZ_ASSERT(state_ == State::Cond || state_ == State::Table ||
state_ == State::Case || state_ == State::CaseBody);
MOZ_ASSERT(!hasDefault_);
tdzCacheCaseAndBody_.reset();
if (state_ == State::Cond || state_ == State::Case) {
// For cond switch, JSOp::Default is always emitted.
if (!emitImplicitDefault()) {
return false;
}
}
JumpTarget here;
if (!bce_->emitJumpTarget(&here)) {
return false;
}
defaultJumpTargetOffset_ = here;
tdzCacheCaseAndBody_.emplace(bce_);
hasDefault_ = true;
state_ = State::DefaultBody;
return true;
}
bool SwitchEmitter::emitEnd() {
MOZ_ASSERT(state_ == State::Cond || state_ == State::Table ||
state_ == State::CaseBody || state_ == State::DefaultBody);
tdzCacheCaseAndBody_.reset();
if (!hasDefault_) {
// If no default case, offset for default is to end of switch.
if (!bce_->emitJumpTarget(&defaultJumpTargetOffset_)) {
return false;
}
}
MOZ_ASSERT(defaultJumpTargetOffset_.offset.valid());
// Set the default offset (to end of switch if no default).
jsbytecode* pc;
if (kind_ == Kind::Cond) {
pc = nullptr;
bce_->patchJumpsToTarget(condSwitchDefaultOffset_,
defaultJumpTargetOffset_);
} else {
// Fill in the default jump target.
pc = bce_->bytecodeSection().code(top_);
SET_JUMP_OFFSET(pc, (defaultJumpTargetOffset_.offset - top_).value());
pc += JUMP_OFFSET_LEN;
}
if (kind_ == Kind::Table) {
// Skip over the already-initialized switch bounds.
pc += 2 * JUMP_OFFSET_LEN;
// Use the 'default' offset for missing cases.
for (uint32_t i = 0, length = caseOffsets_.length(); i < length; i++) {
if (caseOffsets_[i].value() == 0) {
caseOffsets_[i] = defaultJumpTargetOffset_.offset;
}
}
// Allocate resume index range.
uint32_t firstResumeIndex = 0;
mozilla::Span<BytecodeOffset> offsets =
mozilla::Span(caseOffsets_.begin(), caseOffsets_.end());
if (!bce_->allocateResumeIndexRange(offsets, &firstResumeIndex)) {
return false;
}
SET_RESUMEINDEX(pc, firstResumeIndex);
}
// Patch breaks before leaving the scope, as all breaks are under the
// lexical scope if it exists.
if (!controlInfo_->patchBreaks(bce_)) {
return false;
}
if (emitterScope_ && !emitterScope_->leave(bce_)) {
return false;
}
emitterScope_.reset();
tdzCacheLexical_.reset();
controlInfo_.reset();
state_ = State::End;
return true;
}
|