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
|
/* -*- 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/IfEmitter.h"
#include "frontend/BytecodeEmitter.h"
#include "vm/Opcodes.h"
using namespace js;
using namespace js::frontend;
using mozilla::Maybe;
BranchEmitterBase::BranchEmitterBase(BytecodeEmitter* bce,
LexicalKind lexicalKind)
: bce_(bce), lexicalKind_(lexicalKind) {}
IfEmitter::IfEmitter(BytecodeEmitter* bce, LexicalKind lexicalKind)
: BranchEmitterBase(bce, lexicalKind) {}
IfEmitter::IfEmitter(BytecodeEmitter* bce)
: IfEmitter(bce, LexicalKind::MayContainLexicalAccessInBranch) {}
bool BranchEmitterBase::emitThenInternal(ConditionKind conditionKind) {
// The end of TDZCheckCache for cond for else-if.
if (lexicalKind_ == LexicalKind::MayContainLexicalAccessInBranch) {
tdzCache_.reset();
}
// Emit a branch-if-false around the then part.
JSOp op = conditionKind == ConditionKind::Positive ? JSOp::IfEq : JSOp::IfNe;
if (!bce_->emitJump(op, &jumpAroundThen_)) {
return false;
}
// To restore stack depth in else part (if present), save depth of the then
// part.
thenDepth_ = bce_->bytecodeSection().stackDepth();
// Enclose then-branch with TDZCheckCache.
if (lexicalKind_ == LexicalKind::MayContainLexicalAccessInBranch) {
tdzCache_.emplace(bce_);
}
return true;
}
void BranchEmitterBase::calculateOrCheckPushed() {
#ifdef DEBUG
if (!calculatedPushed_) {
pushed_ = bce_->bytecodeSection().stackDepth() - thenDepth_;
calculatedPushed_ = true;
} else {
MOZ_ASSERT(pushed_ == bce_->bytecodeSection().stackDepth() - thenDepth_);
}
#endif
}
bool BranchEmitterBase::emitElseInternal() {
calculateOrCheckPushed();
// The end of TDZCheckCache for then-clause.
if (lexicalKind_ == LexicalKind::MayContainLexicalAccessInBranch) {
MOZ_ASSERT(tdzCache_.isSome());
tdzCache_.reset();
}
// Emit a jump from the end of our then part around the else part. The
// patchJumpsToTarget call at the bottom of this function will fix up
// the offset with jumpsAroundElse value.
if (!bce_->emitJump(JSOp::Goto, &jumpsAroundElse_)) {
return false;
}
// Ensure the branch-if-false comes here, then emit the else.
if (!bce_->emitJumpTargetAndPatch(jumpAroundThen_)) {
return false;
}
// Clear jumpAroundThen_ offset, to tell emitEnd there was an else part.
jumpAroundThen_ = JumpList();
// Restore stack depth of the then part.
bce_->bytecodeSection().setStackDepth(thenDepth_);
// Enclose else-branch with TDZCheckCache.
if (lexicalKind_ == LexicalKind::MayContainLexicalAccessInBranch) {
tdzCache_.emplace(bce_);
}
return true;
}
bool BranchEmitterBase::emitEndInternal() {
// The end of TDZCheckCache for then or else-clause.
if (lexicalKind_ == LexicalKind::MayContainLexicalAccessInBranch) {
MOZ_ASSERT(tdzCache_.isSome());
tdzCache_.reset();
}
calculateOrCheckPushed();
if (jumpAroundThen_.offset.valid()) {
// No else part for the last branch, fixup the branch-if-false to
// come here.
if (!bce_->emitJumpTargetAndPatch(jumpAroundThen_)) {
return false;
}
}
// Patch all the jumps around else parts.
if (!bce_->emitJumpTargetAndPatch(jumpsAroundElse_)) {
return false;
}
return true;
}
bool IfEmitter::emitIf(const Maybe<uint32_t>& ifPos) {
MOZ_ASSERT(state_ == State::Start);
if (ifPos) {
// Make sure this code is attributed to the "if" so that it gets a
// useful column number, instead of the default 0 value.
if (!bce_->updateSourceCoordNotes(*ifPos)) {
return false;
}
}
#ifdef DEBUG
state_ = State::If;
#endif
return true;
}
bool IfEmitter::emitThen(
ConditionKind conditionKind /* = ConditionKind::Positive */) {
MOZ_ASSERT(state_ == State::If || state_ == State::ElseIf);
if (lexicalKind_ == LexicalKind::MayContainLexicalAccessInBranch) {
MOZ_ASSERT_IF(state_ == State::ElseIf, tdzCache_.isSome());
MOZ_ASSERT_IF(state_ != State::ElseIf, tdzCache_.isNothing());
}
if (!emitThenInternal(conditionKind)) {
return false;
}
#ifdef DEBUG
state_ = State::Then;
#endif
return true;
}
bool IfEmitter::emitThenElse(
ConditionKind conditionKind /* = ConditionKind::Positive */) {
MOZ_ASSERT(state_ == State::If || state_ == State::ElseIf);
if (lexicalKind_ == LexicalKind::MayContainLexicalAccessInBranch) {
MOZ_ASSERT_IF(state_ == State::ElseIf, tdzCache_.isSome());
MOZ_ASSERT_IF(state_ != State::ElseIf, tdzCache_.isNothing());
}
if (!emitThenInternal(conditionKind)) {
return false;
}
#ifdef DEBUG
state_ = State::ThenElse;
#endif
return true;
}
bool IfEmitter::emitElseIf(const Maybe<uint32_t>& ifPos) {
MOZ_ASSERT(state_ == State::ThenElse);
if (!emitElseInternal()) {
return false;
}
if (ifPos) {
// Make sure this code is attributed to the "if" so that it gets a
// useful column number, instead of the default 0 value.
if (!bce_->updateSourceCoordNotes(*ifPos)) {
return false;
}
}
#ifdef DEBUG
state_ = State::ElseIf;
#endif
return true;
}
bool IfEmitter::emitElse() {
MOZ_ASSERT(state_ == State::ThenElse);
if (!emitElseInternal()) {
return false;
}
#ifdef DEBUG
state_ = State::Else;
#endif
return true;
}
bool IfEmitter::emitEnd() {
MOZ_ASSERT(state_ == State::Then || state_ == State::Else);
// If there was an else part for the last branch, jumpAroundThen_ is
// already fixed up when emitting the else part.
MOZ_ASSERT_IF(state_ == State::Then, jumpAroundThen_.offset.valid());
MOZ_ASSERT_IF(state_ == State::Else, !jumpAroundThen_.offset.valid());
if (!emitEndInternal()) {
return false;
}
#ifdef DEBUG
state_ = State::End;
#endif
return true;
}
InternalIfEmitter::InternalIfEmitter(BytecodeEmitter* bce)
: IfEmitter(bce, LexicalKind::NoLexicalAccessInBranch) {
#ifdef DEBUG
// Skip emitIf (see the comment above InternalIfEmitter declaration).
state_ = State::If;
#endif
}
CondEmitter::CondEmitter(BytecodeEmitter* bce)
: BranchEmitterBase(bce, LexicalKind::MayContainLexicalAccessInBranch) {}
bool CondEmitter::emitCond() {
MOZ_ASSERT(state_ == State::Start);
#ifdef DEBUG
state_ = State::Cond;
#endif
return true;
}
bool CondEmitter::emitThenElse(
ConditionKind conditionKind /* = ConditionKind::Positive */) {
MOZ_ASSERT(state_ == State::Cond);
if (!emitThenInternal(conditionKind)) {
return false;
}
#ifdef DEBUG
state_ = State::ThenElse;
#endif
return true;
}
bool CondEmitter::emitElse() {
MOZ_ASSERT(state_ == State::ThenElse);
if (!emitElseInternal()) {
return false;
}
#ifdef DEBUG
state_ = State::Else;
#endif
return true;
}
bool CondEmitter::emitEnd() {
MOZ_ASSERT(state_ == State::Else);
MOZ_ASSERT(!jumpAroundThen_.offset.valid());
if (!emitEndInternal()) {
return false;
}
#ifdef DEBUG
state_ = State::End;
#endif
return true;
}
|