summaryrefslogtreecommitdiff
path: root/js/src/frontend/CallOrNewEmitter.cpp
blob: 51d624e38a9186bc00de3583e75fa409bc0320f0 (plain)
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: 4 -*-
 * 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/String.h"

using namespace js;
using namespace js::frontend;

using mozilla::Maybe;

AutoEmittingRunOnceLambda::AutoEmittingRunOnceLambda(BytecodeEmitter* bce)
  : bce_(bce)
{
    MOZ_ASSERT(!bce_->emittingRunOnceLambda);
    bce_->emittingRunOnceLambda = true;
}

AutoEmittingRunOnceLambda::~AutoEmittingRunOnceLambda()
{
    bce_->emittingRunOnceLambda = false;
}

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_CALL_IGNORES_RV;
    }

    MOZ_ASSERT(isCall() || isNew() || isSuperCall());
}

bool
CallOrNewEmitter::emitNameCallee(JSAtom* name)
{
    MOZ_ASSERT(state_ == State::Start);

    NameOpEmitter noe(bce_, name,
                      isCall()
                      ? NameOpEmitter::Kind::Call
                      : NameOpEmitter::Kind::Get);
    if (!noe.emitGet()) {                             // CALLEE THIS
        return false;
    }

    state_ = State::NameCallee;
    return true;
}

[[nodiscard]] 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_;
}

[[nodiscard]] ElemOpEmitter&
CallOrNewEmitter::prepareForElemCallee(bool isSuperElem)
{
    MOZ_ASSERT(state_ == State::Start);

    eoe_.emplace(bce_,
                 isCall()
                 ? ElemOpEmitter::Kind::Call
                 : ElemOpEmitter::Kind::Get,
                 isSuperElem
                 ? ElemOpEmitter::ObjKind::Super
                 : ElemOpEmitter::ObjKind::Other);

    state_ = State::ElemCallee;
    return *eoe_;
}

bool
CallOrNewEmitter::prepareForFunctionCallee()
{
    MOZ_ASSERT(state_ == State::Start);

    // Top level lambdas which are immediately invoked should be treated as
    // only running once. Every time they execute we will create new types and
    // scripts for their contents, to increase the quality of type information
    // within them and enable more backend optimizations. Note that this does
    // not depend on the lambda being invoked at most once (it may be named or
    // be accessed via foo.caller indirection), as multiple executions will
    // just cause the inner scripts to be repeatedly cloned.
    MOZ_ASSERT(!bce_->emittingRunOnceLambda);
    if (bce_->checkRunOnceContext()) {
        autoEmittingRunOnceLambda_.emplace(bce_);
    }

    state_ = State::FunctionCallee;
    return true;
}

bool
CallOrNewEmitter::emitSuperCallee()
{
    MOZ_ASSERT(state_ == State::Start);

    if (!bce_->emit1(JSOP_SUPERFUN)) {                // CALLEE
        return false;
    }
    if (!bce_->emit1(JSOP_IS_CONSTRUCTING)) {         // 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:
        autoEmittingRunOnceLambda_.reset();
        needsThis = true;
        break;
      case State::SuperCallee:
        break;
      case State::OtherCallee:
        needsThis = true;
        break;
      default:;
    }
    if (needsThis) {
        if (isNew() || isSuperCall()) {
            if (!bce_->emit1(JSOP_IS_CONSTRUCTING)) { // CALLEE THIS
                return false;
            }
        } else {
            if (!bce_->emit1(JSOP_UNDEFINED)) {       // 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.

        ifNotOptimizable_.emplace(bce_);
        //                                            // CALLEE THIS ARG0
        if (!bce_->emit1(JSOP_OPTIMIZE_SPREADCALL)) { // CALLEE THIS ARG0 OPTIMIZED
            return false;
        }
        if (!bce_->emit1(JSOP_NOT)) {                 // CALLEE THIS ARG0 !OPTIMIZED
            return false;
        }
        if (!ifNotOptimizable_->emitThen()) {         // CALLEE THIS ARG0
            return false;
        }
        if (!bce_->emit1(JSOP_POP)) {                 // 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()) {          // CALLEE THIS ARR
            return false;
        }

        ifNotOptimizable_.reset();
    }
    if (isNew() || isSuperCall()) {
        if (isSuperCall()) {
            if (!bce_->emit1(JSOP_NEWTARGET)) {       // 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)) {
                return false;                         // CALLEE THIS ARR CALLEE
            }
        }
    }
    if (!isSpread()) {
        if (!bce_->emitCall(op_, argc, beginPos)) {   // RVAL
            return false;
        }
    } else {
        if (beginPos) {
            if (!bce_->updateSourceCoordNotes(*beginPos)) {
                return false;
            }
        }
        if (!bce_->emit1(op_)) {                      // RVAL
            return false;
        }
    }

    if (isEval() && beginPos) {
        uint32_t lineNum = bce_->parser->tokenStream.srcCoords.lineNum(*beginPos);
        if (!bce_->emitUint32Operand(JSOP_LINENO, lineNum)) {
            return false;
        }
    }

    state_ = State::End;
    return true;
}