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
|
/* -*- 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/CallOrNewEmitter.h"
#include "frontend/BytecodeEmitter.h"
#include "frontend/NameOpEmitter.h"
#include "vm/Opcodes.h"
using namespace js;
using namespace js::frontend;
CallOrNewEmitter::CallOrNewEmitter(BytecodeEmitter* bce, JSOp op,
ArgumentsKind argumentsKind,
ValueUsage valueUsage)
: bce_(bce), op_(op), argumentsKind_(argumentsKind) {
if (op_ == JSOp::Call && valueUsage == ValueUsage::IgnoreValue) {
op_ = JSOp::CallIgnoresRv;
}
MOZ_ASSERT(isCall() || isNew() || isSuperCall());
}
bool CallOrNewEmitter::emitNameCallee(TaggedParserAtomIndex name) {
MOZ_ASSERT(state_ == State::Start);
// [stack]
NameOpEmitter noe(
bce_, name,
isCall() ? NameOpEmitter::Kind::Call : NameOpEmitter::Kind::Get);
if (!noe.emitGet()) {
// [stack] # if isCall()
// [stack] CALLEE THIS
// [stack] # if isNew() or isSuperCall()
// [stack] CALLEE
return false;
}
state_ = State::NameCallee;
return true;
}
[[nodiscard]] PropOpEmitter& CallOrNewEmitter::prepareForPropCallee(
bool isSuperProp) {
MOZ_ASSERT(state_ == State::Start);
MOZ_ASSERT(bce_->emitterMode != BytecodeEmitter::SelfHosting);
// [stack]
poe_.emplace(bce_,
isCall() ? PropOpEmitter::Kind::Call : PropOpEmitter::Kind::Get,
isSuperProp ? PropOpEmitter::ObjKind::Super
: PropOpEmitter::ObjKind::Other);
state_ = State::PropCallee;
return *poe_;
}
[[nodiscard]] ElemOpEmitter& CallOrNewEmitter::prepareForElemCallee(
bool isSuperElem) {
MOZ_ASSERT(state_ == State::Start);
MOZ_ASSERT(bce_->emitterMode != BytecodeEmitter::SelfHosting);
// [stack]
eoe_.emplace(bce_,
isCall() ? ElemOpEmitter::Kind::Call : ElemOpEmitter::Kind::Get,
isSuperElem ? ElemOpEmitter::ObjKind::Super
: ElemOpEmitter::ObjKind::Other);
state_ = State::ElemCallee;
return *eoe_;
}
PrivateOpEmitter& CallOrNewEmitter::prepareForPrivateCallee(
TaggedParserAtomIndex privateName) {
MOZ_ASSERT(state_ == State::Start);
MOZ_ASSERT(bce_->emitterMode != BytecodeEmitter::SelfHosting);
// [stack]
xoe_.emplace(
bce_,
isCall() ? PrivateOpEmitter::Kind::Call : PrivateOpEmitter::Kind::Get,
privateName);
state_ = State::PrivateCallee;
return *xoe_;
}
bool CallOrNewEmitter::prepareForFunctionCallee() {
MOZ_ASSERT(state_ == State::Start);
MOZ_ASSERT(bce_->emitterMode != BytecodeEmitter::SelfHosting);
// [stack]
state_ = State::FunctionCallee;
return true;
}
bool CallOrNewEmitter::emitSuperCallee() {
MOZ_ASSERT(state_ == State::Start);
MOZ_ASSERT(bce_->emitterMode != BytecodeEmitter::SelfHosting);
// [stack]
if (!bce_->emitThisEnvironmentCallee()) {
// [stack] CALLEE
return false;
}
if (!bce_->emit1(JSOp::SuperFun)) {
// [stack] SUPER_FUN
return false;
}
if (!bce_->emit1(JSOp::IsConstructing)) {
// [stack] SUPER_FUN IS_CONSTRUCTING
return false;
}
state_ = State::SuperCallee;
return true;
}
bool CallOrNewEmitter::prepareForOtherCallee() {
MOZ_ASSERT(state_ == State::Start);
MOZ_ASSERT(bce_->emitterMode != BytecodeEmitter::SelfHosting);
// [stack]
state_ = State::OtherCallee;
return true;
}
bool CallOrNewEmitter::emitThis() {
MOZ_ASSERT(state_ == State::NameCallee || state_ == State::PropCallee ||
state_ == State::ElemCallee || state_ == State::PrivateCallee ||
state_ == State::FunctionCallee || state_ == State::SuperCallee ||
state_ == State::OtherCallee);
// [stack] # if isCall()
// [stack] CALLEE THIS?
// [stack] # if isNew() or isSuperCall()
// [stack] CALLEE
bool needsThis = false;
switch (state_) {
case State::NameCallee:
if (!isCall()) {
needsThis = true;
}
break;
case State::PropCallee:
poe_.reset();
if (!isCall()) {
needsThis = true;
}
break;
case State::ElemCallee:
eoe_.reset();
if (!isCall()) {
needsThis = true;
}
break;
case State::PrivateCallee:
xoe_.reset();
if (!isCall()) {
needsThis = true;
}
break;
case State::FunctionCallee:
needsThis = true;
break;
case State::SuperCallee:
break;
case State::OtherCallee:
needsThis = true;
break;
default:;
}
if (needsThis) {
if (isNew() || isSuperCall()) {
if (!bce_->emit1(JSOp::IsConstructing)) {
// [stack] CALLEE IS_CONSTRUCTING
return false;
}
} else {
if (!bce_->emit1(JSOp::Undefined)) {
// [stack] CALLEE THIS
return false;
}
}
}
// [stack] CALLEE THIS
state_ = State::This;
return true;
}
bool CallOrNewEmitter::prepareForNonSpreadArguments() {
MOZ_ASSERT(state_ == State::This);
MOZ_ASSERT(!isSpread());
// [stack] CALLEE THIS
state_ = State::Arguments;
return true;
}
// See the usage in the comment at the top of the class.
bool CallOrNewEmitter::wantSpreadOperand() {
MOZ_ASSERT(state_ == State::This);
MOZ_ASSERT(isSpread());
// [stack] CALLEE THIS
state_ = State::WantSpreadOperand;
return isSingleSpread() || isPassthroughRest();
}
bool CallOrNewEmitter::prepareForSpreadArguments() {
MOZ_ASSERT(state_ == State::WantSpreadOperand);
MOZ_ASSERT(isSpread());
MOZ_ASSERT(!isSingleSpread() && !isPassthroughRest());
// [stack] CALLEE THIS
state_ = State::Arguments;
return true;
}
bool CallOrNewEmitter::emitSpreadArgumentsTest() {
// Caller should check wantSpreadOperand before this.
MOZ_ASSERT(state_ == State::WantSpreadOperand);
MOZ_ASSERT(isSpread());
MOZ_ASSERT(isSingleSpread() || isPassthroughRest());
// [stack] CALLEE THIS ARG0
if (isSingleSpread()) {
// Emit a preparation code to optimize the spread call:
//
// g(...args);
//
// If the spread operand is a packed array, skip the spread
// operation and pass it directly to spread call operation.
// See the comment in OptimizeSpreadCall in Interpreter.cpp
// for the optimizable conditions.
// [stack] CALLEE THIS ARG0
ifNotOptimizable_.emplace(bce_);
if (!bce_->emit1(JSOp::Dup)) {
// [stack] CALLEE THIS ARG0 ARG0
return false;
}
if (!bce_->emit1(JSOp::OptimizeSpreadCall)) {
// [stack] CALLEE THIS ARG0 ARRAY_OR_UNDEF
return false;
}
if (!bce_->emit1(JSOp::Dup)) {
// [stack] CALLEE THIS ARG0 ARRAY_OR_UNDEF ARRAY_OR_UNDEF
return false;
}
if (!bce_->emit1(JSOp::Undefined)) {
// [stack] CALLEE THIS ARG0 ARRAY_OR_UNDEF ARRAY_OR_UNDEF UNDEF
return false;
}
if (!bce_->emit1(JSOp::StrictEq)) {
// [stack] CALLEE THIS ARG0 ARRAY_OR_UNDEF EQ
return false;
}
if (!ifNotOptimizable_->emitThenElse()) {
// [stack] CALLEE THIS ARG0 ARRAY_OR_UNDEF
return false;
}
if (!bce_->emit1(JSOp::Pop)) {
// [stack] CALLEE THIS ARG0
return false;
}
}
state_ = State::SpreadArgumentsTest;
return true;
}
bool CallOrNewEmitter::wantSpreadIteration() {
MOZ_ASSERT(state_ == State::SpreadArgumentsTest);
MOZ_ASSERT(isSpread());
state_ = State::SpreadIteration;
return !isPassthroughRest();
}
bool CallOrNewEmitter::emitSpreadArgumentsTestEnd() {
MOZ_ASSERT(state_ == State::SpreadIteration);
MOZ_ASSERT(isSpread());
if (isSingleSpread()) {
if (!ifNotOptimizable_->emitElse()) {
// [stack] CALLEE THIS ARG0 ARRAY_OR_UNDEF
return false;
}
if (!bce_->emit1(JSOp::Swap)) {
// [stack] CALLEE THIS ARRAY_OR_UNDEF ARG0
return false;
}
if (!bce_->emit1(JSOp::Pop)) {
// [stack] CALLEE THIS ARRAY_OR_UNDEF
return false;
}
if (!ifNotOptimizable_->emitEnd()) {
// [stack] CALLEE THIS ARR
return false;
}
ifNotOptimizable_.reset();
}
state_ = State::Arguments;
return true;
}
bool CallOrNewEmitter::emitEnd(uint32_t argc, uint32_t beginPos) {
MOZ_ASSERT(state_ == State::Arguments);
// [stack] # if isCall()
// [stack] CALLEE THIS ARG0 ... ARGN
// [stack] # if isNew() or isSuperCall()
// [stack] CALLEE IS_CONSTRUCTING ARG0 ... ARGN NEW.TARGET?
if (!bce_->updateSourceCoordNotes(beginPos)) {
return false;
}
if (!bce_->markSimpleBreakpoint()) {
return false;
}
if (!isSpread()) {
if (!bce_->emitCall(op_, argc)) {
// [stack] RVAL
return false;
}
} else {
if (!bce_->emit1(op_)) {
// [stack] RVAL
return false;
}
}
if (isEval()) {
uint32_t lineNum = bce_->errorReporter().lineAt(beginPos);
if (!bce_->emitUint32Operand(JSOp::Lineno, lineNum)) {
// [stack] RVAL
return false;
}
}
state_ = State::End;
return true;
}
|