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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/* -*- 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_SwitchEmitter_h
#define frontend_SwitchEmitter_h
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "mozilla/Attributes.h" // MOZ_STACK_CLASS
#include "mozilla/Maybe.h" // mozilla::Maybe
#include <stddef.h> // size_t
#include <stdint.h> // int32_t, uint32_t
#include "frontend/BytecodeControlStructures.h" // BreakableControl
#include "frontend/EmitterScope.h" // EmitterScope
#include "frontend/JumpList.h" // JumpList, JumpTarget
#include "frontend/TDZCheckCache.h" // TDZCheckCache
#include "js/AllocPolicy.h" // SystemAllocPolicy
#include "js/Value.h" // JSVAL_INT_MAX, JSVAL_INT_MIN
#include "js/Vector.h" // Vector
#include "vm/Scope.h" // LexicalScope
namespace js {
namespace frontend {
struct BytecodeEmitter;
// Class for emitting bytecode for switch-case-default block.
//
// Usage: (check for the return value is omitted for simplicity)
//
// `switch (discriminant) { case c1_expr: c1_body; }`
// SwitchEmitter se(this);
// se.emitDiscriminant(offset_of_switch);
// emit(discriminant);
//
// se.validateCaseCount(1);
// se.emitCond();
//
// se.prepareForCaseValue();
// emit(c1_expr);
// se.emitCaseJump();
//
// se.emitCaseBody();
// emit(c1_body);
//
// se.emitEnd();
//
// `switch (discriminant) { case c1_expr: c1_body; case c2_expr: c2_body;
// default: def_body; }`
// SwitchEmitter se(this);
// se.emitDiscriminant(offset_of_switch);
// emit(discriminant);
//
// se.validateCaseCount(2);
// se.emitCond();
//
// se.prepareForCaseValue();
// emit(c1_expr);
// se.emitCaseJump();
//
// se.prepareForCaseValue();
// emit(c2_expr);
// se.emitCaseJump();
//
// se.emitCaseBody();
// emit(c1_body);
//
// se.emitCaseBody();
// emit(c2_body);
//
// se.emitDefaultBody();
// emit(def_body);
//
// se.emitEnd();
//
// `switch (discriminant) { case c1_expr: c1_body; case c2_expr: c2_body; }`
// with Table Switch
// SwitchEmitter::TableGenerator tableGen(this);
// tableGen.addNumber(c1_expr_value);
// tableGen.addNumber(c2_expr_value);
// tableGen.finish(2);
//
// // If `!tableGen.isValid()` here, `emitCond` should be used instead.
//
// SwitchEmitter se(this);
// se.emitDiscriminant(offset_of_switch);
// emit(discriminant);
// se.validateCaseCount(2);
// se.emitTable(tableGen);
//
// se.emitCaseBody(c1_expr_value, tableGen);
// emit(c1_body);
//
// se.emitCaseBody(c2_expr_value, tableGen);
// emit(c2_body);
//
// se.emitEnd();
//
// `switch (discriminant) { case c1_expr: c1_body; case c2_expr: c2_body;
// default: def_body; }`
// with Table Switch
// SwitchEmitter::TableGenerator tableGen(bce);
// tableGen.addNumber(c1_expr_value);
// tableGen.addNumber(c2_expr_value);
// tableGen.finish(2);
//
// // If `!tableGen.isValid()` here, `emitCond` should be used instead.
//
// SwitchEmitter se(this);
// se.emitDiscriminant(offset_of_switch);
// emit(discriminant);
// se.validateCaseCount(2);
// se.emitTable(tableGen);
//
// se.emitCaseBody(c1_expr_value, tableGen);
// emit(c1_body);
//
// se.emitCaseBody(c2_expr_value, tableGen);
// emit(c2_body);
//
// se.emitDefaultBody();
// emit(def_body);
//
// se.emitEnd();
//
// `switch (discriminant) { case c1_expr: c1_body; }`
// in case c1_body contains lexical bindings
// SwitchEmitter se(this);
// se.emitDiscriminant(offset_of_switch);
// emit(discriminant);
//
// se.validateCaseCount(1);
//
// se.emitLexical(bindings);
//
// se.emitCond();
//
// se.prepareForCaseValue();
// emit(c1_expr);
// se.emitCaseJump();
//
// se.emitCaseBody();
// emit(c1_body);
//
// se.emitEnd();
//
// `switch (discriminant) { case c1_expr: c1_body; }`
// in case c1_body contains hosted functions
// SwitchEmitter se(this);
// se.emitDiscriminant(offset_of_switch);
// emit(discriminant);
//
// se.validateCaseCount(1);
//
// se.emitLexical(bindings);
// emit(hosted functions);
//
// se.emitCond();
//
// se.prepareForCaseValue();
// emit(c1_expr);
// se.emitCaseJump();
//
// se.emitCaseBody();
// emit(c1_body);
//
// se.emitEnd();
//
class MOZ_STACK_CLASS SwitchEmitter {
// Bytecode for each case.
//
// Cond Switch (uses an equality comparison for each case)
// {discriminant}
//
// {c1_expr}
// JSOp::Case c1
//
// JSOp::JumpTarget
// {c2_expr}
// JSOp::Case c2
//
// ...
//
// JSOp::JumpTarget
// JSOp::Default default
//
// c1:
// JSOp::JumpTarget
// {c1_body}
// JSOp::Goto end
//
// c2:
// JSOp::JumpTarget
// {c2_body}
// JSOp::Goto end
//
// default:
// end:
// JSOp::JumpTarget
//
// Table Switch
// {discriminant}
// JSOp::TableSwitch c1, c2, ...
//
// c1:
// JSOp::JumpTarget
// {c1_body}
// JSOp::Goto end
//
// c2:
// JSOp::JumpTarget
// {c2_body}
// JSOp::Goto end
//
// ...
//
// end:
// JSOp::JumpTarget
public:
enum class Kind { Table, Cond };
// Class for generating optimized table switch data.
class MOZ_STACK_CLASS TableGenerator {
BytecodeEmitter* bce_;
// Bit array for given numbers.
mozilla::Maybe<js::Vector<size_t, 128, SystemAllocPolicy>> intmap_;
// The length of the intmap_.
int32_t intmapBitLength_ = 0;
// The length of the table.
uint32_t tableLength_ = 0;
// The lower and higher bounds of the table.
int32_t low_ = JSVAL_INT_MAX, high_ = JSVAL_INT_MIN;
// Whether the table is still valid.
bool valid_ = true;
#ifdef DEBUG
bool finished_ = false;
#endif
public:
explicit TableGenerator(BytecodeEmitter* bce) : bce_(bce) {}
void setInvalid() { valid_ = false; }
[[nodiscard]] bool isValid() const { return valid_; }
[[nodiscard]] bool isInvalid() const { return !valid_; }
// Add the given number to the table. The number is the value of
// `expr` for `case expr:` syntax.
[[nodiscard]] bool addNumber(int32_t caseValue);
// Finish generating the table.
// `caseCount` should be the number of cases in the switch statement,
// excluding the default case.
void finish(uint32_t caseCount);
private:
friend SwitchEmitter;
// The following methods can be used only after calling `finish`.
// Returns the lower bound of the added numbers.
int32_t low() const {
MOZ_ASSERT(finished_);
return low_;
}
// Returns the higher bound of the numbers.
int32_t high() const {
MOZ_ASSERT(finished_);
return high_;
}
// Returns the index in SwitchEmitter.caseOffsets_ for table switch.
uint32_t toCaseIndex(int32_t caseValue) const;
// Returns the length of the table.
// This method can be called only if `isValid()` is true.
uint32_t tableLength() const;
};
private:
BytecodeEmitter* bce_;
// `kind_` should be set to the correct value in emitCond/emitTable.
Kind kind_ = Kind::Cond;
// True if there's explicit default case.
bool hasDefault_ = false;
// The number of cases in the switch statement, excluding the default case.
uint32_t caseCount_ = 0;
// Internal index for case jump and case body, used by cond switch.
uint32_t caseIndex_ = 0;
// Bytecode offset after emitting `discriminant`.
BytecodeOffset top_;
// Bytecode offset of the previous JSOp::Case.
BytecodeOffset lastCaseOffset_;
// Bytecode offset of the JSOp::JumpTarget for default body.
JumpTarget defaultJumpTargetOffset_;
// Bytecode offset of the JSOp::Default.
JumpList condSwitchDefaultOffset_;
// Instantiated when there's lexical scope for entire switch.
mozilla::Maybe<TDZCheckCache> tdzCacheLexical_;
mozilla::Maybe<EmitterScope> emitterScope_;
// Instantiated while emitting case expression and case/default body.
mozilla::Maybe<TDZCheckCache> tdzCacheCaseAndBody_;
// Control for switch.
mozilla::Maybe<BreakableControl> controlInfo_;
uint32_t switchPos_ = 0;
// Cond Switch:
// Offset of each JSOp::Case.
// Table Switch:
// Offset of each JSOp::JumpTarget for case.
js::Vector<BytecodeOffset, 32, SystemAllocPolicy> caseOffsets_;
// The state of this emitter.
//
// +-------+ emitDiscriminant +--------------+
// | Start |----------------->| Discriminant |-+
// +-------+ +--------------+ |
// |
// +-------------------------------------------+
// |
// | validateCaseCount +-----------+
// +->+------------------------>+------------------>| CaseCount |-+
// | ^ +-----------+ |
// | emitLexical +---------+ | |
// +------------>| Lexical |-+ |
// +---------+ |
// |
// +--------------------------------------------------------------+
// |
// | emitTable +-------+
// +---------->| Table |----------------------------------->+-+
// | +-------+ ^ |
// | | |
// | emitCond +------+ | |
// +---------->| Cond |-+------------------------------->+->+ |
// +------+ | ^ |
// | | |
// +------------------+ | |
// | | |
// |prepareForCaseValue +-----------+ | |
// +----------+--------->| CaseValue | | |
// ^ +-----------+ | |
// | | | |
// | | emitCaseJump +------+ | |
// | +------------->| Case |->+-+ |
// | +------+ | |
// | | |
// +--------------------------------------+ |
// |
// +----------------------------------------------------------+
// |
// | emitEnd +-----+
// +-+----------------------------------------->+-------->| End |
// | ^ +-----+
// | emitCaseBody +----------+ |
// +->+-+---------------->| CaseBody |--->+-+-+
// ^ | +----------+ ^ |
// | | | |
// | | emitDefaultBody +-------------+ | |
// | +---------------->| DefaultBody |-+ |
// | +-------------+ |
// | |
// +-------------------------------------+
//
protected:
enum class State {
// The initial state.
Start,
// After calling emitDiscriminant.
Discriminant,
// After calling validateCaseCount.
CaseCount,
// After calling emitLexical.
Lexical,
// After calling emitCond.
Cond,
// After calling emitTable.
Table,
// After calling prepareForCaseValue.
CaseValue,
// After calling emitCaseJump.
Case,
// After calling emitCaseBody.
CaseBody,
// After calling emitDefaultBody.
DefaultBody,
// After calling emitEnd.
End
};
State state_ = State::Start;
public:
explicit SwitchEmitter(BytecodeEmitter* bce);
// `switchPos` is the offset in the source code for the character below:
//
// switch ( cond ) { ... }
// ^
// |
// switchPos
[[nodiscard]] bool emitDiscriminant(uint32_t switchPos);
// `caseCount` should be the number of cases in the switch statement,
// excluding the default case.
[[nodiscard]] bool validateCaseCount(uint32_t caseCount);
// `bindings` is a lexical scope for the entire switch, in case there's
// let/const effectively directly under case or default blocks.
[[nodiscard]] bool emitLexical(LexicalScope::ParserData* bindings);
[[nodiscard]] bool emitCond();
[[nodiscard]] bool emitTable(const TableGenerator& tableGen);
[[nodiscard]] bool prepareForCaseValue();
[[nodiscard]] bool emitCaseJump();
[[nodiscard]] bool emitCaseBody();
[[nodiscard]] bool emitCaseBody(int32_t caseValue,
const TableGenerator& tableGen);
[[nodiscard]] bool emitDefaultBody();
[[nodiscard]] bool emitEnd();
private:
[[nodiscard]] bool emitCaseOrDefaultJump(uint32_t caseIndex, bool isDefault);
[[nodiscard]] bool emitImplicitDefault();
};
// Class for emitting bytecode for switch-case-default block that doesn't
// correspond to a syntactic `switch`.
// Compared to SwitchEmitter, this class doesn't require `emitDiscriminant`,
// and the discriminant can already be on the stack. Usage is otherwise
// the same as SwitchEmitter.
class MOZ_STACK_CLASS InternalSwitchEmitter : public SwitchEmitter {
public:
explicit InternalSwitchEmitter(BytecodeEmitter* bce);
};
} /* namespace frontend */
} /* namespace js */
#endif /* frontend_SwitchEmitter_h */
|