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
|
/* -*- 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 "frontend/SharedContext.h"
#include "vm/Opcodes.h"
#include "vm/StringType.h"
using namespace js;
using namespace js::frontend;
using mozilla::Maybe;
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(const ParserAtom* name) {
MOZ_ASSERT(state_ == State::Start);
NameOpEmitter noe(
bce_, name,
isCall() ? NameOpEmitter::Kind::Call : NameOpEmitter::Kind::Get);
if (!noe.emitGet()) {
// [stack] CALLEE THIS
return false;
}
state_ = State::NameCallee;
return true;
}
MOZ_MUST_USE PropOpEmitter& CallOrNewEmitter::prepareForPropCallee(
bool isSuperProp) {
MOZ_ASSERT(state_ == State::Start);
poe_.emplace(bce_,
isCall() ? PropOpEmitter::Kind::Call : PropOpEmitter::Kind::Get,
isSuperProp ? PropOpEmitter::ObjKind::Super
: PropOpEmitter::ObjKind::Other);
state_ = State::PropCallee;
return *poe_;
}
MOZ_MUST_USE ElemOpEmitter& CallOrNewEmitter::prepareForElemCallee(
bool isSuperElem, bool isPrivate) {
MOZ_ASSERT(state_ == State::Start);
eoe_.emplace(bce_,
isCall() ? ElemOpEmitter::Kind::Call : ElemOpEmitter::Kind::Get,
isSuperElem ? ElemOpEmitter::ObjKind::Super
: ElemOpEmitter::ObjKind::Other,
isPrivate ? NameVisibility::Private : NameVisibility::Public);
state_ = State::ElemCallee;
return *eoe_;
}
bool CallOrNewEmitter::prepareForFunctionCallee() {
MOZ_ASSERT(state_ == State::Start);
state_ = State::FunctionCallee;
return true;
}
bool CallOrNewEmitter::emitSuperCallee() {
MOZ_ASSERT(state_ == State::Start);
if (!bce_->emitThisEnvironmentCallee()) {
// [stack] CALLEE
return false;
}
if (!bce_->emit1(JSOp::SuperFun)) {
// [stack] CALLEE
return false;
}
if (!bce_->emit1(JSOp::IsConstructing)) {
// [stack] CALLEE THIS
return false;
}
state_ = State::SuperCallee;
return true;
}
bool CallOrNewEmitter::prepareForOtherCallee() {
MOZ_ASSERT(state_ == State::Start);
state_ = State::OtherCallee;
return true;
}
bool CallOrNewEmitter::emitThis() {
MOZ_ASSERT(state_ == State::NameCallee || state_ == State::PropCallee ||
state_ == State::ElemCallee || state_ == State::FunctionCallee ||
state_ == State::SuperCallee || state_ == State::OtherCallee);
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::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 THIS
return false;
}
} else {
if (!bce_->emit1(JSOp::Undefined)) {
// [stack] CALLEE THIS
return false;
}
}
}
state_ = State::This;
return true;
}
// Used by BytecodeEmitter::emitPipeline to reuse CallOrNewEmitter instance
// across multiple chained calls.
void CallOrNewEmitter::reset() {
MOZ_ASSERT(state_ == State::End);
state_ = State::Start;
}
bool CallOrNewEmitter::prepareForNonSpreadArguments() {
MOZ_ASSERT(state_ == State::This);
MOZ_ASSERT(!isSpread());
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());
state_ = State::WantSpreadOperand;
return isSingleSpreadRest();
}
bool CallOrNewEmitter::emitSpreadArgumentsTest() {
// Caller should check wantSpreadOperand before this.
MOZ_ASSERT(state_ == State::WantSpreadOperand);
MOZ_ASSERT(isSpread());
if (isSingleSpreadRest()) {
// Emit a preparation code to optimize the spread call with a rest
// parameter:
//
// function f(...args) {
// g(...args);
// }
//
// If the spread operand is a rest parameter and it's optimizable
// array, skip spread operation and pass it directly to spread call
// operation. See the comment in OptimizeSpreadCall in
// Interpreter.cpp for the optimizable conditons.
// [stack] CALLEE THIS ARG0
ifNotOptimizable_.emplace(bce_);
if (!bce_->emit1(JSOp::OptimizeSpreadCall)) {
// [stack] CALLEE THIS ARG0 OPTIMIZED
return false;
}
if (!ifNotOptimizable_->emitThen(IfEmitter::ConditionKind::Negative)) {
// [stack] CALLEE THIS ARG0
return false;
}
if (!bce_->emit1(JSOp::Pop)) {
// [stack] CALLEE THIS
return false;
}
}
state_ = State::Arguments;
return true;
}
bool CallOrNewEmitter::emitEnd(uint32_t argc, const Maybe<uint32_t>& beginPos) {
MOZ_ASSERT(state_ == State::Arguments);
if (isSingleSpreadRest()) {
if (!ifNotOptimizable_->emitEnd()) {
// [stack] CALLEE THIS ARR
return false;
}
ifNotOptimizable_.reset();
}
if (isNew() || isSuperCall()) {
if (isSuperCall()) {
if (!bce_->emit1(JSOp::NewTarget)) {
// [stack] CALLEE THIS ARG.. NEW.TARGET
return false;
}
} else {
// Repush the callee as new.target
uint32_t effectiveArgc = isSpread() ? 1 : argc;
if (!bce_->emitDupAt(effectiveArgc + 1)) {
// [stack] CALLEE THIS ARR CALLEE
return false;
}
}
}
if (beginPos) {
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() && beginPos) {
uint32_t lineNum = bce_->parser->errorReporter().lineAt(*beginPos);
if (!bce_->emitUint32Operand(JSOp::Lineno, lineNum)) {
return false;
}
}
state_ = State::End;
return true;
}
|