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
|
/* -*- 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_IfEmitter_h
#define frontend_IfEmitter_h
#include "mozilla/Attributes.h"
#include "mozilla/Maybe.h"
#include <stdint.h>
#include "frontend/JumpList.h"
#include "frontend/TDZCheckCache.h"
namespace js {
namespace frontend {
struct BytecodeEmitter;
class MOZ_STACK_CLASS BranchEmitterBase {
public:
// Whether the then-clause, the else-clause, or else-if condition may
// contain declaration or access to lexical variables, which means they
// should have their own TDZCheckCache. Basically TDZCheckCache should be
// created for each basic block, which then-clause, else-clause, and
// else-if condition are, but for internally used branches which are
// known not to touch lexical variables we can skip creating TDZCheckCache
// for them.
//
// See the comment for TDZCheckCache class for more details.
enum class LexicalKind {
// For syntactic branches (if, if-else, and conditional expression),
// which basically may contain declaration or accesses to lexical
// variables inside then-clause, else-clause, and else-if condition.
MayContainLexicalAccessInBranch,
// For internally used branches which don't touch lexical variables
// inside then-clause, else-clause, nor else-if condition.
NoLexicalAccessInBranch
};
protected:
BytecodeEmitter* bce_;
// Jump around the then clause, to the beginning of the else clause.
JumpList jumpAroundThen_;
// Jump around the else clause, to the end of the entire branch.
JumpList jumpsAroundElse_;
// The stack depth before emitting the then block.
// Used for restoring stack depth before emitting the else block.
// Also used for assertion to make sure then and else blocks pushed the
// same number of values.
int32_t thenDepth_ = 0;
enum class ConditionKind { Positive, Negative };
LexicalKind lexicalKind_;
mozilla::Maybe<TDZCheckCache> tdzCache_;
#ifdef DEBUG
// The number of values pushed in the then and else blocks.
int32_t pushed_ = 0;
bool calculatedPushed_ = false;
#endif
protected:
BranchEmitterBase(BytecodeEmitter* bce, LexicalKind lexicalKind);
[[nodiscard]] bool emitThenInternal(ConditionKind conditionKind);
void calculateOrCheckPushed();
[[nodiscard]] bool emitElseInternal();
[[nodiscard]] bool emitEndInternal();
public:
#ifdef DEBUG
// Returns the number of values pushed onto the value stack inside
// `then_block` and `else_block`.
// Can be used in assertion after emitting if-then-else.
int32_t pushed() const { return pushed_; }
// Returns the number of values popped onto the value stack inside
// `then_block` and `else_block`.
// Can be used in assertion after emitting if-then-else.
int32_t popped() const { return -pushed_; }
#endif
};
// Class for emitting bytecode for blocks like if-then-else.
//
// This class can be used to emit single if-then-else block, or cascading
// else-if blocks.
//
// Usage: (check for the return value is omitted for simplicity)
//
// `if (cond) then_block`
// IfEmitter ifThen(this);
// ifThen.emitIf(Some(offset_of_if));
// emit(cond);
// ifThen.emitThen();
// emit(then_block);
// ifThen.emitEnd();
//
// `if (!cond) then_block`
// IfEmitter ifThen(this);
// ifThen.emitIf(Some(offset_of_if));
// emit(cond);
// ifThen.emitThen(IfEmitter::ConditionKind::Negative);
// emit(then_block);
// ifThen.emitEnd();
//
// `if (cond) then_block else else_block`
// IfEmitter ifThenElse(this);
// ifThen.emitIf(Some(offset_of_if));
// emit(cond);
// ifThenElse.emitThenElse();
// emit(then_block);
// ifThenElse.emitElse();
// emit(else_block);
// ifThenElse.emitEnd();
//
// `if (c1) b1 else if (c2) b2 else if (c3) b3 else b4`
// IfEmitter ifThenElse(this);
// ifThen.emitIf(Some(offset_of_if));
// emit(c1);
// ifThenElse.emitThenElse();
// emit(b1);
// ifThenElse.emitElseIf(Some(offset_of_if));
// emit(c2);
// ifThenElse.emitThenElse();
// emit(b2);
// ifThenElse.emitElseIf(Some(offset_of_if));
// emit(c3);
// ifThenElse.emitThenElse();
// emit(b3);
// ifThenElse.emitElse();
// emit(b4);
// ifThenElse.emitEnd();
//
class MOZ_STACK_CLASS IfEmitter : public BranchEmitterBase {
public:
using ConditionKind = BranchEmitterBase::ConditionKind;
protected:
#ifdef DEBUG
// The state of this emitter.
//
// +-------+ emitIf +----+
// | Start |------->| If |-+
// +-------+ +----+ |
// |
// +--------------------+
// |
// v emitThen +------+ emitEnd +-----+
// +->+--------->| Then |---------------------------->+-------->| End |
// ^ | +------+ ^ +-----+
// | | |
// | | |
// | | |
// | | emitThenElse +----------+ emitElse +------+ |
// | +------------->| ThenElse |-+--------->| Else |-+
// | +----------+ | +------+
// | |
// | | emitElseIf +--------+
// | +----------->| ElseIf |-+
// | +--------+ |
// | |
// +------------------------------------------------------+
enum class State {
// The initial state.
Start,
// After calling emitIf.
If,
// After calling emitThen.
Then,
// After calling emitThenElse.
ThenElse,
// After calling emitElse.
Else,
// After calling emitElseIf.
ElseIf,
// After calling emitEnd.
End
};
State state_ = State::Start;
#endif
protected:
// For InternalIfEmitter.
IfEmitter(BytecodeEmitter* bce, LexicalKind lexicalKind);
public:
explicit IfEmitter(BytecodeEmitter* bce);
// `ifPos` is the offset in the source code for the character below:
//
// if ( cond ) { ... } else if ( cond2 ) { ... }
// ^ ^
// | |
// | ifPos for emitElseIf
// |
// ifPos for emitIf
//
// Can be Nothing() if not available.
[[nodiscard]] bool emitIf(const mozilla::Maybe<uint32_t>& ifPos);
[[nodiscard]] bool emitThen(
ConditionKind conditionKind = ConditionKind::Positive);
[[nodiscard]] bool emitThenElse(
ConditionKind conditionKind = ConditionKind::Positive);
[[nodiscard]] bool emitElseIf(const mozilla::Maybe<uint32_t>& ifPos);
[[nodiscard]] bool emitElse();
[[nodiscard]] bool emitEnd();
};
// Class for emitting bytecode for blocks like if-then-else which doesn't touch
// lexical variables.
//
// See the comments above NoLexicalAccessInBranch for more details when to use
// this instead of IfEmitter.
// Compared to IfEmitter, this class doesn't have emitIf method, given that
// it doesn't have syntactic `if`, and also the `cond` value can be already
// on the stack.
//
// Usage: (check for the return value is omitted for simplicity)
//
// `if (cond) then_block else else_block` (effectively)
// emit(cond);
// InternalIfEmitter ifThenElse(this);
// ifThenElse.emitThenElse();
// emit(then_block);
// ifThenElse.emitElse();
// emit(else_block);
// ifThenElse.emitEnd();
//
class MOZ_STACK_CLASS InternalIfEmitter : public IfEmitter {
public:
explicit InternalIfEmitter(
BytecodeEmitter* bce,
LexicalKind lexicalKind =
BranchEmitterBase::LexicalKind::NoLexicalAccessInBranch);
};
// Class for emitting bytecode for conditional expression.
//
// Usage: (check for the return value is omitted for simplicity)
//
// `cond ? then_expr : else_expr`
// CondEmitter condElse(this);
// condElse.emitCond();
// emit(cond);
// condElse.emitThenElse();
// emit(then_expr);
// condElse.emitElse();
// emit(else_expr);
// condElse.emitEnd();
//
class MOZ_STACK_CLASS CondEmitter : public BranchEmitterBase {
#ifdef DEBUG
// The state of this emitter.
//
// +-------+ emitCond +------+ emitThenElse +----------+
// | Start |--------->| Cond |------------->| ThenElse |-+
// +-------+ +------+ +----------+ |
// |
// +-----------------+
// |
// | emitElse +------+ emitEnd +-----+
// +--------->| Else |-------->| End |
// +------+ +-----+
enum class State {
// The initial state.
Start,
// After calling emitCond.
Cond,
// After calling emitThenElse.
ThenElse,
// After calling emitElse.
Else,
// After calling emitEnd.
End
};
State state_ = State::Start;
#endif
public:
explicit CondEmitter(BytecodeEmitter* bce);
[[nodiscard]] bool emitCond();
[[nodiscard]] bool emitThenElse(
ConditionKind conditionKind = ConditionKind::Positive);
[[nodiscard]] bool emitElse();
[[nodiscard]] bool emitEnd();
};
} /* namespace frontend */
} /* namespace js */
#endif /* frontend_IfEmitter_h */
|