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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/* -*- 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/NameOpEmitter.h"
#include "frontend/AbstractScopePtr.h"
#include "frontend/BytecodeEmitter.h"
#include "frontend/SharedContext.h"
#include "frontend/TDZCheckCache.h"
#include "vm/Opcodes.h"
#include "vm/Scope.h"
#include "vm/StringType.h"
using namespace js;
using namespace js::frontend;
NameOpEmitter::NameOpEmitter(BytecodeEmitter* bce, const ParserAtom* name,
Kind kind)
: bce_(bce), kind_(kind), name_(name), loc_(bce_->lookupName(name_)) {}
NameOpEmitter::NameOpEmitter(BytecodeEmitter* bce, const ParserAtom* name,
const NameLocation& loc, Kind kind)
: bce_(bce), kind_(kind), name_(name), loc_(loc) {}
bool NameOpEmitter::emitGet() {
MOZ_ASSERT(state_ == State::Start);
switch (loc_.kind()) {
case NameLocation::Kind::Dynamic:
if (!bce_->emitAtomOp(JSOp::GetName, name_)) {
// [stack] VAL
return false;
}
break;
case NameLocation::Kind::Global:
if (!bce_->emitAtomOp(JSOp::GetGName, name_)) {
// [stack] VAL
return false;
}
break;
case NameLocation::Kind::Intrinsic:
if (!bce_->emitAtomOp(JSOp::GetIntrinsic, name_)) {
// [stack] VAL
return false;
}
break;
case NameLocation::Kind::NamedLambdaCallee:
if (!bce_->emit1(JSOp::Callee)) {
// [stack] VAL
return false;
}
break;
case NameLocation::Kind::Import:
if (!bce_->emitAtomOp(JSOp::GetImport, name_)) {
// [stack] VAL
return false;
}
break;
case NameLocation::Kind::ArgumentSlot:
if (!bce_->emitArgOp(JSOp::GetArg, loc_.argumentSlot())) {
// [stack] VAL
return false;
}
break;
case NameLocation::Kind::FrameSlot:
if (!bce_->emitLocalOp(JSOp::GetLocal, loc_.frameSlot())) {
// [stack] VAL
return false;
}
if (loc_.isLexical()) {
if (!bce_->emitTDZCheckIfNeeded(name_, loc_, ValueIsOnStack::Yes)) {
// [stack] VAL
return false;
}
}
break;
case NameLocation::Kind::EnvironmentCoordinate:
if (!bce_->emitEnvCoordOp(JSOp::GetAliasedVar,
loc_.environmentCoordinate())) {
// [stack] VAL
return false;
}
if (loc_.isLexical()) {
if (!bce_->emitTDZCheckIfNeeded(name_, loc_, ValueIsOnStack::Yes)) {
// [stack] VAL
return false;
}
}
break;
case NameLocation::Kind::DynamicAnnexBVar:
MOZ_CRASH(
"Synthesized vars for Annex B.3.3 should only be used in "
"initialization");
}
if (isCall()) {
switch (loc_.kind()) {
case NameLocation::Kind::Dynamic: {
JSOp thisOp = bce_->needsImplicitThis() ? JSOp::ImplicitThis
: JSOp::GImplicitThis;
if (!bce_->emitAtomOp(thisOp, name_)) {
// [stack] CALLEE THIS
return false;
}
break;
}
case NameLocation::Kind::Global:
if (!bce_->emitAtomOp(JSOp::GImplicitThis, name_)) {
// [stack] CALLEE THIS
return false;
}
break;
case NameLocation::Kind::Intrinsic:
case NameLocation::Kind::NamedLambdaCallee:
case NameLocation::Kind::Import:
case NameLocation::Kind::ArgumentSlot:
case NameLocation::Kind::FrameSlot:
case NameLocation::Kind::EnvironmentCoordinate:
if (!bce_->emit1(JSOp::Undefined)) {
// [stack] CALLEE UNDEF
return false;
}
break;
case NameLocation::Kind::DynamicAnnexBVar:
MOZ_CRASH(
"Synthesized vars for Annex B.3.3 should only be used in "
"initialization");
}
}
#ifdef DEBUG
state_ = State::Get;
#endif
return true;
}
bool NameOpEmitter::prepareForRhs() {
MOZ_ASSERT(state_ == State::Start);
switch (loc_.kind()) {
case NameLocation::Kind::Dynamic:
case NameLocation::Kind::Import:
case NameLocation::Kind::DynamicAnnexBVar:
if (!bce_->makeAtomIndex(name_, &atomIndex_)) {
return false;
}
if (loc_.kind() == NameLocation::Kind::DynamicAnnexBVar) {
// Annex B vars always go on the nearest variable environment,
// even if lexical environments in between contain same-named
// bindings.
if (!bce_->emit1(JSOp::BindVar)) {
// [stack] ENV
return false;
}
} else {
if (!bce_->emitAtomOp(JSOp::BindName, atomIndex_)) {
// [stack] ENV
return false;
}
}
emittedBindOp_ = true;
break;
case NameLocation::Kind::Global:
if (!bce_->makeAtomIndex(name_, &atomIndex_)) {
return false;
}
if (loc_.isLexical() && isInitialize()) {
// InitGLexical always gets the global lexical scope. It doesn't
// need a BindGName.
MOZ_ASSERT(bce_->innermostScope().is<GlobalScope>());
} else {
if (!bce_->emitAtomOp(JSOp::BindGName, atomIndex_)) {
// [stack] ENV
return false;
}
emittedBindOp_ = true;
}
break;
case NameLocation::Kind::Intrinsic:
break;
case NameLocation::Kind::NamedLambdaCallee:
break;
case NameLocation::Kind::ArgumentSlot:
break;
case NameLocation::Kind::FrameSlot:
break;
case NameLocation::Kind::EnvironmentCoordinate:
break;
}
// For compound assignments, first get the LHS value, then emit
// the RHS and the op.
if (isCompoundAssignment() || isIncDec()) {
if (loc_.kind() == NameLocation::Kind::Dynamic) {
// For dynamic accesses we need to emit GetBoundName instead of
// GetName for correctness: looking up @@unscopables on the
// environment chain (due to 'with' environments) must only happen
// once.
//
// GetBoundName uses the environment already pushed on the stack
// from the earlier BindName.
if (!bce_->emit1(JSOp::Dup)) {
// [stack] ENV ENV
return false;
}
if (!bce_->emitAtomOp(JSOp::GetBoundName, name_)) {
// [stack] ENV V
return false;
}
} else {
if (!emitGet()) {
// [stack] ENV? V
return false;
}
}
}
#ifdef DEBUG
state_ = State::Rhs;
#endif
return true;
}
#if defined(__clang__) && defined(XP_WIN) && \
(defined(_M_X64) || defined(__x86_64__))
// Work around a CPU bug. See bug 1524257.
__attribute__((__aligned__(32)))
#endif
bool NameOpEmitter::emitAssignment() {
MOZ_ASSERT(state_ == State::Rhs);
switch (loc_.kind()) {
case NameLocation::Kind::Dynamic:
case NameLocation::Kind::Import:
case NameLocation::Kind::DynamicAnnexBVar:
if (!bce_->emitAtomOp(bce_->strictifySetNameOp(JSOp::SetName),
atomIndex_)) {
return false;
}
break;
case NameLocation::Kind::Global: {
JSOp op;
if (emittedBindOp_) {
op = bce_->strictifySetNameOp(JSOp::SetGName);
} else {
op = JSOp::InitGLexical;
}
if (!bce_->emitAtomOp(op, atomIndex_)) {
return false;
}
break;
}
case NameLocation::Kind::Intrinsic:
if (!bce_->emitAtomOp(JSOp::SetIntrinsic, name_)) {
return false;
}
break;
case NameLocation::Kind::NamedLambdaCallee:
// Assigning to the named lambda is a no-op in sloppy mode but
// throws in strict mode.
if (bce_->sc->strict()) {
if (!bce_->emitAtomOp(JSOp::ThrowSetConst, name_)) {
return false;
}
}
break;
case NameLocation::Kind::ArgumentSlot:
if (!bce_->emitArgOp(JSOp::SetArg, loc_.argumentSlot())) {
return false;
}
break;
case NameLocation::Kind::FrameSlot: {
JSOp op = JSOp::SetLocal;
if (loc_.isLexical()) {
if (isInitialize()) {
op = JSOp::InitLexical;
} else {
if (loc_.isConst()) {
op = JSOp::ThrowSetConst;
}
if (!bce_->emitTDZCheckIfNeeded(name_, loc_, ValueIsOnStack::No)) {
return false;
}
}
}
if (op == JSOp::ThrowSetConst) {
if (!bce_->emitAtomOp(op, name_)) {
return false;
}
} else {
if (!bce_->emitLocalOp(op, loc_.frameSlot())) {
return false;
}
}
if (op == JSOp::InitLexical) {
if (!bce_->innermostTDZCheckCache->noteTDZCheck(bce_, name_,
DontCheckTDZ)) {
return false;
}
}
break;
}
case NameLocation::Kind::EnvironmentCoordinate: {
JSOp op = JSOp::SetAliasedVar;
if (loc_.isLexical()) {
if (isInitialize()) {
op = JSOp::InitAliasedLexical;
} else {
if (loc_.isConst()) {
op = JSOp::ThrowSetConst;
}
if (!bce_->emitTDZCheckIfNeeded(name_, loc_, ValueIsOnStack::No)) {
return false;
}
}
}
if (loc_.bindingKind() == BindingKind::NamedLambdaCallee) {
// Assigning to the named lambda is a no-op in sloppy mode and throws
// in strict mode.
op = JSOp::ThrowSetConst;
if (bce_->sc->strict()) {
if (!bce_->emitAtomOp(op, name_)) {
return false;
}
}
} else {
if (op == JSOp::ThrowSetConst) {
if (!bce_->emitAtomOp(op, name_)) {
return false;
}
} else {
if (!bce_->emitEnvCoordOp(op, loc_.environmentCoordinate())) {
return false;
}
}
}
if (op == JSOp::InitAliasedLexical) {
if (!bce_->innermostTDZCheckCache->noteTDZCheck(bce_, name_,
DontCheckTDZ)) {
return false;
}
}
break;
}
}
#ifdef DEBUG
state_ = State::Assignment;
#endif
return true;
}
bool NameOpEmitter::emitIncDec() {
MOZ_ASSERT(state_ == State::Start);
JSOp incOp = isInc() ? JSOp::Inc : JSOp::Dec;
if (!prepareForRhs()) {
// [stack] ENV? V
return false;
}
if (!bce_->emit1(JSOp::ToNumeric)) {
// [stack] ENV? N
return false;
}
if (isPostIncDec()) {
if (!bce_->emit1(JSOp::Dup)) {
// [stack] ENV? N? N
return false;
}
}
if (!bce_->emit1(incOp)) {
// [stack] ENV? N? N+1
return false;
}
if (isPostIncDec() && emittedBindOp()) {
if (!bce_->emit2(JSOp::Pick, 2)) {
// [stack] N? N+1 ENV?
return false;
}
if (!bce_->emit1(JSOp::Swap)) {
// [stack] N? ENV? N+1
return false;
}
}
if (!emitAssignment()) {
// [stack] N? N+1
return false;
}
if (isPostIncDec()) {
if (!bce_->emit1(JSOp::Pop)) {
// [stack] N
return false;
}
}
#ifdef DEBUG
state_ = State::IncDec;
#endif
return true;
}
|