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
|
/* -*- 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/TryEmitter.h"
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "frontend/BytecodeEmitter.h" // BytecodeEmitter
#include "frontend/IfEmitter.h" // BytecodeEmitter
#include "frontend/SharedContext.h" // StatementKind
#include "vm/Opcodes.h" // JSOp
using namespace js;
using namespace js::frontend;
using mozilla::Maybe;
TryEmitter::TryEmitter(BytecodeEmitter* bce, Kind kind, ControlKind controlKind)
: bce_(bce),
kind_(kind),
controlKind_(controlKind),
depth_(0),
tryOpOffset_(0)
#ifdef DEBUG
,
state_(State::Start)
#endif
{
if (controlKind_ == ControlKind::Syntactic) {
controlInfo_.emplace(
bce_, hasFinally() ? StatementKind::Finally : StatementKind::Try);
}
}
bool TryEmitter::emitTry() {
MOZ_ASSERT(state_ == State::Start);
// Since an exception can be thrown at any place inside the try block,
// we need to restore the stack and the scope chain before we transfer
// the control to the exception handler.
//
// For that we store in a try note associated with the catch or
// finally block the stack depth upon the try entry. The interpreter
// uses this depth to properly unwind the stack and the scope chain.
depth_ = bce_->bytecodeSection().stackDepth();
tryOpOffset_ = bce_->bytecodeSection().offset();
if (!bce_->emit1(JSOp::Try)) {
return false;
}
#ifdef DEBUG
state_ = State::Try;
#endif
return true;
}
bool TryEmitter::emitJumpToFinallyWithFallthrough() {
uint32_t stackDepthForNextBlock = bce_->bytecodeSection().stackDepth();
// The fallthrough continuation is special-cased with index 0.
uint32_t idx = TryFinallyControl::SpecialContinuations::Fallthrough;
if (!bce_->emitJumpToFinally(&controlInfo_->finallyJumps_, idx)) {
return false;
}
// Reset the stack depth for the following catch or finally block.
bce_->bytecodeSection().setStackDepth(stackDepthForNextBlock);
return true;
}
bool TryEmitter::emitTryEnd() {
MOZ_ASSERT(state_ == State::Try);
MOZ_ASSERT(depth_ == bce_->bytecodeSection().stackDepth());
if (hasFinally() && controlInfo_) {
if (!emitJumpToFinallyWithFallthrough()) {
return false;
}
} else {
// Emit jump over catch
if (!bce_->emitJump(JSOp::Goto, &catchAndFinallyJump_)) {
return false;
}
}
if (!bce_->emitJumpTarget(&tryEnd_)) {
return false;
}
return true;
}
bool TryEmitter::emitCatch() {
MOZ_ASSERT(state_ == State::Try);
if (!emitTryEnd()) {
return false;
}
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_);
if (shouldUpdateRval()) {
// Clear the frame's return value that might have been set by the
// try block:
//
// eval("try { 1; throw 2 } catch(e) {}"); // undefined, not 1
if (!bce_->emit1(JSOp::Undefined)) {
return false;
}
if (!bce_->emit1(JSOp::SetRval)) {
return false;
}
}
if (!bce_->emit1(JSOp::Exception)) {
return false;
}
#ifdef DEBUG
state_ = State::Catch;
#endif
return true;
}
bool TryEmitter::emitCatchEnd() {
MOZ_ASSERT(state_ == State::Catch);
if (!controlInfo_) {
return true;
}
// Jump to <finally>, if required.
if (hasFinally()) {
if (!emitJumpToFinallyWithFallthrough()) {
return false;
}
}
return true;
}
bool TryEmitter::emitFinally(
const Maybe<uint32_t>& finallyPos /* = Nothing() */) {
// If we are using controlInfo_ (i.e., emitting a syntactic try
// blocks), we must have specified up front if there will be a finally
// close. For internal non-syntactic try blocks, like those emitted for
// yield* and IteratorClose inside for-of loops, we can emitFinally even
// without specifying up front, since the internal non-syntactic try
// blocks emit no GOSUBs.
if (!controlInfo_) {
if (kind_ == Kind::TryCatch) {
kind_ = Kind::TryCatchFinally;
}
} else {
MOZ_ASSERT(hasFinally());
}
if (!hasCatch()) {
MOZ_ASSERT(state_ == State::Try);
if (!emitTryEnd()) {
return false;
}
} else {
MOZ_ASSERT(state_ == State::Catch);
if (!emitCatchEnd()) {
return false;
}
}
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_);
// Upon entry to the finally, there are two additional values on the stack:
// a boolean value to indicate whether we're throwing an exception, and
// either that exception (if we're throwing) or a resume index to which we
// will return (if we're not throwing).
bce_->bytecodeSection().setStackDepth(depth_ + 2);
if (!bce_->emitJumpTarget(&finallyStart_)) {
return false;
}
if (controlInfo_) {
// Fix up the jumps to the finally code.
bce_->patchJumpsToTarget(controlInfo_->finallyJumps_, finallyStart_);
// Indicate that we're emitting a subroutine body.
controlInfo_->setEmittingSubroutine();
}
if (finallyPos) {
if (!bce_->updateSourceCoordNotes(finallyPos.value())) {
return false;
}
}
if (!bce_->emit1(JSOp::Finally)) {
return false;
}
if (shouldUpdateRval()) {
if (!bce_->emit1(JSOp::GetRval)) {
return false;
}
// Clear the frame's return value to make break/continue return
// correct value even if there's no other statement before them:
//
// eval("x: try { 1 } finally { break x; }"); // undefined, not 1
if (!bce_->emit1(JSOp::Undefined)) {
return false;
}
if (!bce_->emit1(JSOp::SetRval)) {
return false;
}
}
#ifdef DEBUG
state_ = State::Finally;
#endif
return true;
}
bool TryEmitter::emitFinallyEnd() {
MOZ_ASSERT(state_ == State::Finally);
if (shouldUpdateRval()) {
if (!bce_->emit1(JSOp::SetRval)) {
return false;
}
}
InternalIfEmitter ifThrowing(bce_);
if (!ifThrowing.emitThenElse()) {
return false;
}
if (!bce_->emit1(JSOp::Throw)) {
return false;
}
if (!ifThrowing.emitElse()) {
return false;
}
if (controlInfo_ && !controlInfo_->continuations_.empty()) {
if (!controlInfo_->emitContinuations(bce_)) {
return false;
}
} else {
// If there are no non-local jumps, then the only possible jump target
// is the code immediately following this finally block. Instead of
// emitting a tableswitch, we can simply pop the continuation index
// and fall through.
if (!bce_->emit1(JSOp::Pop)) {
return false;
}
}
if (!ifThrowing.emitEnd()) {
return false;
}
bce_->hasTryFinally = true;
return true;
}
bool TryEmitter::emitEnd() {
if (!hasFinally()) {
MOZ_ASSERT(state_ == State::Catch);
if (!emitCatchEnd()) {
return false;
}
} else {
MOZ_ASSERT(state_ == State::Finally);
if (!emitFinallyEnd()) {
return false;
}
}
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_);
if (catchAndFinallyJump_.offset.valid()) {
// Fix up the end-of-try/catch jumps to come here.
if (!bce_->emitJumpTargetAndPatch(catchAndFinallyJump_)) {
return false;
}
}
// Add the try note last, to let post-order give us the right ordering
// (first to last for a given nesting level, inner to outer by level).
if (hasCatch()) {
if (!bce_->addTryNote(TryNoteKind::Catch, depth_, offsetAfterTryOp(),
tryEnd_.offset)) {
return false;
}
}
// If we've got a finally, mark try+catch region with additional
// trynote to catch exceptions (re)thrown from a catch block or
// for the try{}finally{} case.
if (hasFinally()) {
if (!bce_->addTryNote(TryNoteKind::Finally, depth_, offsetAfterTryOp(),
finallyStart_.offset)) {
return false;
}
}
#ifdef DEBUG
state_ = State::End;
#endif
return true;
}
bool TryEmitter::shouldUpdateRval() const {
return controlKind_ == ControlKind::Syntactic && !bce_->sc->noScriptRval();
}
|