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
|
/* -*- 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_TryEmitter_h
#define frontend_TryEmitter_h
#include "mozilla/Attributes.h" // MOZ_STACK_CLASS
#include "mozilla/Maybe.h" // mozilla::Maybe, mozilla::Nothing
#include <stdint.h> // uint32_t
#include "frontend/BytecodeControlStructures.h" // TryFinallyControl
#include "frontend/BytecodeOffset.h" // BytecodeOffset
#include "frontend/JumpList.h" // JumpList, JumpTarget
namespace js {
namespace frontend {
struct BytecodeEmitter;
// Class for emitting bytecode for blocks like try-catch-finally.
//
// Usage: (check for the return value is omitted for simplicity)
//
// `try { try_block } catch (ex) { catch_block }`
// TryEmitter tryCatch(this, TryEmitter::Kind::TryCatch,
// TryEmitter::ControlKind::Syntactic);
// tryCatch.emitTry();
// emit(try_block);
// tryCatch.emitCatch();
// emit(catch_block); // Pending exception is on stack
// tryCatch.emitEnd();
//
// `try { try_block } finally { finally_block }`
// TryEmitter tryCatch(this, TryEmitter::Kind::TryFinally,
// TryEmitter::ControlKind::Syntactic);
// tryCatch.emitTry();
// emit(try_block);
// // finally_pos: The "{" character's position in the source code text.
// tryCatch.emitFinally(Some(finally_pos));
// emit(finally_block);
// tryCatch.emitEnd();
//
// `try { try_block } catch (ex) {catch_block} finally { finally_block }`
// TryEmitter tryCatch(this, TryEmitter::Kind::TryCatchFinally,
// TryEmitter::ControlKind::Syntactic);
// tryCatch.emitTry();
// emit(try_block);
// tryCatch.emitCatch();
// emit(catch_block);
// tryCatch.emitFinally(Some(finally_pos));
// emit(finally_block);
// tryCatch.emitEnd();
//
class MOZ_STACK_CLASS TryEmitter {
public:
enum class Kind { TryCatch, TryCatchFinally, TryFinally };
// Syntactic try-catch-finally and internally used non-syntactic
// try-catch-finally behave differently for 2 points.
//
// The first one is whether TryFinallyControl is used or not.
// See the comment for `controlInfo_`.
//
// The second one is whether the catch and finally blocks handle the frame's
// return value. For syntactic try-catch-finally, the bytecode marked with
// "*" are emitted to clear return value with `undefined` before the catch
// block and the finally block, and also to save/restore the return value
// before/after the finally block. Note that these instructions are not
// emitted for noScriptRval scripts that don't track the return value.
//
// JSOp::Try offsetOf(jumpToEnd)
//
// try_body...
//
// JSOp::Goto finally
// JSOp::JumpTarget
// jumpToEnd:
// JSOp::Goto end:
//
// catch:
// JSOp::JumpTarget
// * JSOp::Undefined
// * JSOp::SetRval
//
// catch_body...
//
// JSOp::Goto finally
// JSOp::JumpTarget
// JSOp::Goto end
//
// finally:
// JSOp::JumpTarget
// * JSOp::GetRval
// * JSOp::Undefined
// * JSOp::SetRval
//
// finally_body...
//
// * JSOp::SetRval
// JSOp::Nop
//
// end:
// JSOp::JumpTarget
//
// For syntactic try-catch-finally, Syntactic should be used.
// For non-syntactic try-catch-finally, NonSyntactic should be used.
enum class ControlKind { Syntactic, NonSyntactic };
private:
BytecodeEmitter* bce_;
Kind kind_;
ControlKind controlKind_;
// Tracks jumps to the finally block for later fixup.
//
// When a finally block is active, non-local jumps (including
// jumps-over-catches) result in a goto being written into the bytecode
// stream and fixed-up later.
//
// For non-syntactic try-catch-finally, all that handling is skipped.
// The non-syntactic try-catch-finally must:
// * have only one catch block
// * have JSOp::Goto at the end of catch-block
// * have no non-local-jump
// * don't use finally block for normal completion of try-block and
// catch-block
//
// Additionally, a finally block may be emitted for non-syntactic
// try-catch-finally, even if the kind is TryCatch, because JSOp::Goto is
// not emitted.
mozilla::Maybe<TryFinallyControl> controlInfo_;
// The stack depth before emitting JSOp::Try.
int depth_;
// The offset of the JSOp::Try op.
BytecodeOffset tryOpOffset_;
// JSOp::JumpTarget after the entire try-catch-finally block.
JumpList catchAndFinallyJump_;
// The offset of JSOp::Goto at the end of the try block.
JumpTarget tryEnd_;
// The offset of JSOp::JumpTarget at the beginning of the finally block.
JumpTarget finallyStart_;
#ifdef DEBUG
// The state of this emitter.
//
// +-------+ emitTry +-----+ emitCatch +-------+ emitEnd +-----+
// | Start |-------->| Try |-+---------->| Catch |-+->+--------->| End |
// +-------+ +-----+ | +-------+ | ^ +-----+
// | | |
// | +------------------+ +----+
// | | |
// | v emitFinally +---------+ |
// +->+------------>| Finally |--+
// +---------+
enum class State {
// The initial state.
Start,
// After calling emitTry.
Try,
// After calling emitCatch.
Catch,
// After calling emitFinally.
Finally,
// After calling emitEnd.
End
};
State state_;
#endif
bool hasCatch() const {
return kind_ == Kind::TryCatch || kind_ == Kind::TryCatchFinally;
}
bool hasFinally() const {
return kind_ == Kind::TryCatchFinally || kind_ == Kind::TryFinally;
}
BytecodeOffset offsetAfterTryOp() const {
return tryOpOffset_ + BytecodeOffsetDiff(JSOpLength_Try);
}
// Returns true if catch and finally blocks should handle the frame's
// return value.
bool shouldUpdateRval() const;
// Jump to the finally block. After the finally block executes,
// fall through to the code following the finally block.
[[nodiscard]] bool emitJumpToFinallyWithFallthrough();
public:
TryEmitter(BytecodeEmitter* bce, Kind kind, ControlKind controlKind);
[[nodiscard]] bool emitTry();
enum class ExceptionStack : bool {
/**
* Push only the pending exception value.
*/
No,
/**
* Push the pending exception value and its stack.
*/
Yes,
};
[[nodiscard]] bool emitCatch(ExceptionStack stack = ExceptionStack::No);
// If `finallyPos` is specified, it's an offset of the finally block's
// "{" character in the source code text, to improve line:column number in
// the error reporting.
// For non-syntactic try-catch-finally, `finallyPos` can be omitted.
[[nodiscard]] bool emitFinally(
const mozilla::Maybe<uint32_t>& finallyPos = mozilla::Nothing());
[[nodiscard]] bool emitEnd();
private:
[[nodiscard]] bool emitTryEnd();
[[nodiscard]] bool emitCatchEnd();
[[nodiscard]] bool emitFinallyEnd();
};
} /* namespace frontend */
} /* namespace js */
#endif /* frontend_TryEmitter_h */
|