summaryrefslogtreecommitdiff
path: root/js/src/frontend/CallOrNewEmitter.h
blob: f1f9ce5e5a5ac8192ad2d4cf2c15f6ee98668daa (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* -*- 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/. */

#ifndef frontend_CallOrNewEmitter_h
#define frontend_CallOrNewEmitter_h

#include "mozilla/Attributes.h"
#include "mozilla/Maybe.h"

#include <stdint.h>

#include "frontend/ElemOpEmitter.h"
#include "frontend/IfEmitter.h"
#include "frontend/PropOpEmitter.h"
#include "frontend/ValueUsage.h"
#include "js/TypeDecls.h"
#include "vm/Opcodes.h"
#include "jsopcode.h"

namespace js {
namespace frontend {

struct BytecodeEmitter;

class MOZ_RAII AutoEmittingRunOnceLambda
{
    BytecodeEmitter* bce_;

  public:
    explicit AutoEmittingRunOnceLambda(BytecodeEmitter* bce);
    ~AutoEmittingRunOnceLambda();
};

// Class for emitting bytecode for call or new expression.
//
// Usage: (check for the return value is omitted for simplicity)
//
//   `print(arg);`
//     CallOrNewEmitter cone(this, JSOP_CALL,
//                           CallOrNewEmitter::ArgumentsKind::Other,
//                           ValueUsage::WantValue);
//     cone.emitNameCallee();
//     emit(print);
//     cone.emitThis();
//     cone.prepareForNonSpreadArguments();
//     emit(arg);
//     cone.emitEnd(1, Some(offset_of_callee));
//
//   `callee.prop(arg1, arg2);`
//     CallOrNewEmitter cone(this, JSOP_CALL,
//                           CallOrNewEmitter::ArgumentsKind::Other,
//                           ValueUsage::WantValue);
//     PropOpEmitter& poe = cone.prepareForPropCallee(false);
//     ... emit `callee.prop` with `poe` here...
//     cone.emitThis();
//     cone.prepareForNonSpreadArguments();
//     emit(arg1);
//     emit(arg2);
//     cone.emitEnd(2, Some(offset_of_callee));
//
//   `callee[key](arg);`
//     CallOrNewEmitter cone(this, JSOP_CALL,
//                           CallOrNewEmitter::ArgumentsKind::Other,
//                           ValueUsage::WantValue);
//     ElemOpEmitter& eoe = cone.prepareForElemCallee(false);
//     ... emit `callee[key]` with `eoe` here...
//     cone.emitThis();
//     cone.prepareForNonSpreadArguments();
//     emit(arg);
//     cone.emitEnd(1, Some(offset_of_callee));
//
//   `(function() { ... })(arg);`
//     CallOrNewEmitter cone(this, JSOP_CALL,
//                           CallOrNewEmitter::ArgumentsKind::Other,
//                           ValueUsage::WantValue);
//     cone.prepareForFunctionCallee();
//     emit(function);
//     cone.emitThis();
//     cone.prepareForNonSpreadArguments();
//     emit(arg);
//     cone.emitEnd(1, Some(offset_of_callee));
//
//   `super(arg);`
//     CallOrNewEmitter cone(this, JSOP_CALL,
//                           CallOrNewEmitter::ArgumentsKind::Other,
//                           ValueUsage::WantValue);
//     cone.emitSuperCallee();
//     cone.emitThis();
//     cone.prepareForNonSpreadArguments();
//     emit(arg);
//     cone.emitEnd(1, Some(offset_of_callee));
//
//   `(some_other_expression)(arg);`
//     CallOrNewEmitter cone(this, JSOP_CALL,
//                           CallOrNewEmitter::ArgumentsKind::Other,
//                           ValueUsage::WantValue);
//     cone.prepareForOtherCallee();
//     emit(some_other_expression);
//     cone.emitThis();
//     cone.prepareForNonSpreadArguments();
//     emit(arg);
//     cone.emitEnd(1, Some(offset_of_callee));
//
//   `print(...arg);`
//     CallOrNewEmitter cone(this, JSOP_SPREADCALL,
//                           CallOrNewEmitter::ArgumentsKind::Other,
//                           ValueUsage::WantValue);
//     cone.emitNameCallee();
//     emit(print);
//     cone.emitThis();
//     if (cone.wantSpreadOperand())
//       emit(arg)
//     cone.emitSpreadArgumentsTest();
//     emit([...arg]);
//     cone.emitEnd(1, Some(offset_of_callee));
//
//   `print(...rest);`
//   where `rest` is rest parameter
//     CallOrNewEmitter cone(this, JSOP_SPREADCALL,
//                           CallOrNewEmitter::ArgumentsKind::SingleSpreadRest,
//                           ValueUsage::WantValue);
//     cone.emitNameCallee();
//     emit(print);
//     cone.emitThis();
//     if (cone.wantSpreadOperand())
//       emit(arg)
//     cone.emitSpreadArgumentsTest();
//     emit([...arg]);
//     cone.emitEnd(1, Some(offset_of_callee));
//
//   `new f(arg);`
//     CallOrNewEmitter cone(this, JSOP_NEW,
//                           CallOrNewEmitter::ArgumentsKind::Other,
//                           ValueUsage::WantValue);
//     cone.emitNameCallee();
//     emit(f);
//     cone.emitThis();
//     cone.prepareForNonSpreadArguments();
//     emit(arg);
//     cone.emitEnd(1, Some(offset_of_callee));
//
class MOZ_STACK_CLASS CallOrNewEmitter
{
  public:
    enum class ArgumentsKind {
        Other,

        // Specify this for the following case:
        //
        //   function f(...rest) {
        //     g(...rest);
        //   }
        //
        // This enables optimization to avoid allocating an intermediate array
        // for spread operation.
        //
        // wantSpreadOperand() returns true when this is specified.
        SingleSpreadRest
    };

  private:
    BytecodeEmitter* bce_;

    // The opcode for the call or new.
    JSOp op_;

    // Whether the call is a spread call with single rest parameter or not.
    // See the comment in emitSpreadArgumentsTest for more details.
    ArgumentsKind argumentsKind_;

    // The branch for spread call optimization.
    mozilla::Maybe<InternalIfEmitter> ifNotOptimizable_;

    mozilla::Maybe<AutoEmittingRunOnceLambda> autoEmittingRunOnceLambda_;

    mozilla::Maybe<PropOpEmitter> poe_;
    mozilla::Maybe<ElemOpEmitter> eoe_;

    // The state of this emitter.
    //
    // +-------+   emitNameCallee           +------------+
    // | Start |-+------------------------->| NameCallee |------+
    // +-------+ |                          +------------+      |
    //           |                                              |
    //           | prepareForPropCallee     +------------+      v
    //           +------------------------->| PropCallee |----->+
    //           |                          +------------+      |
    //           |                                              |
    //           | prepareForElemCallee     +------------+      v
    //           +------------------------->| ElemCallee |----->+
    //           |                          +------------+      |
    //           |                                              |
    //           | prepareForFunctionCallee +----------------+  v
    //           +------------------------->| FunctionCallee |->+
    //           |                          +----------------+  |
    //           |                                              |
    //           | emitSuperCallee          +-------------+     v
    //           +------------------------->| SuperCallee |---->+
    //           |                          +-------------+     |
    //           |                                              |
    //           | prepareForOtherCallee    +-------------+     v
    //           +------------------------->| OtherCallee |---->+
    //                                      +-------------+     |
    //                                                          |
    // +--------------------------------------------------------+
    // |
    // | emitThis +------+
    // +--------->| This |-+
    //            +------+ |
    //                     |
    // +-------------------+
    // |
    // | [!isSpread]
    // |   prepareForNonSpreadArguments    +-----------+ emitEnd +-----+
    // +------------------------------->+->| Arguments |-------->| End |
    // |                                ^  +-----------+         +-----+
    // |                                |
    // |                                +----------------------------------+
    // |                                                                   |
    // | [isSpread]                                                        |
    // |   wantSpreadOperand +-------------------+ emitSpreadArgumentsTest |
    // +-------------------->| WantSpreadOperand |-------------------------+
    //                       +-------------------+
    enum class State {
        // The initial state.
        Start,

        // After calling emitNameCallee.
        NameCallee,

        // After calling prepareForPropCallee.
        PropCallee,

        // After calling prepareForElemCallee.
        ElemCallee,

        // After calling prepareForFunctionCallee.
        FunctionCallee,

        // After calling emitSuperCallee.
        SuperCallee,

        // After calling prepareForOtherCallee.
        OtherCallee,

        // After calling emitThis.
        This,

        // After calling wantSpreadOperand.
        WantSpreadOperand,

        // After calling prepareForNonSpreadArguments.
        Arguments,

        // After calling emitEnd.
        End
    };
    State state_ = State::Start;

  public:
    CallOrNewEmitter(BytecodeEmitter* bce, JSOp op,
                     ArgumentsKind argumentsKind,
                     ValueUsage valueUsage);

  private:
    MOZ_MUST_USE bool isCall() const {
        return op_ == JSOP_CALL || op_ == JSOP_CALL_IGNORES_RV ||
               op_ == JSOP_SPREADCALL ||
               isEval() || isFunApply() || isFunCall();
    }

    MOZ_MUST_USE bool isNew() const {
        return op_ == JSOP_NEW || op_ == JSOP_SPREADNEW;
    }

    MOZ_MUST_USE bool isSuperCall() const {
        return op_ == JSOP_SUPERCALL || op_ == JSOP_SPREADSUPERCALL;
    }

    MOZ_MUST_USE bool isEval() const {
        return op_ == JSOP_EVAL || op_ == JSOP_STRICTEVAL ||
               op_ == JSOP_SPREADEVAL || op_ == JSOP_STRICTSPREADEVAL;
    }

    MOZ_MUST_USE bool isFunApply() const {
        return op_ == JSOP_FUNAPPLY;
    }

    MOZ_MUST_USE bool isFunCall() const {
        return op_ == JSOP_FUNCALL;
    }

    MOZ_MUST_USE bool isSpread() const {
        return JOF_OPTYPE(op_) == JOF_BYTE;
    }

    MOZ_MUST_USE bool isSingleSpreadRest() const {
        return argumentsKind_ == ArgumentsKind::SingleSpreadRest;
    }

  public:
    MOZ_MUST_USE bool emitNameCallee(JSAtom* name);
    MOZ_MUST_USE PropOpEmitter& prepareForPropCallee(bool isSuperProp);
    MOZ_MUST_USE ElemOpEmitter& prepareForElemCallee(bool isSuperElem);
    MOZ_MUST_USE bool prepareForFunctionCallee();
    MOZ_MUST_USE bool emitSuperCallee();
    MOZ_MUST_USE bool prepareForOtherCallee();

    MOZ_MUST_USE bool emitThis();

    // Used by BytecodeEmitter::emitPipeline to reuse CallOrNewEmitter instance
    // across multiple chained calls.
    void reset();

    MOZ_MUST_USE bool prepareForNonSpreadArguments();

    // See the usage in the comment at the top of the class.
    MOZ_MUST_USE bool wantSpreadOperand();
    MOZ_MUST_USE bool emitSpreadArgumentsTest();

    // Parameters are the offset in the source code for each character below:
    //
    //   callee(arg);
    //   ^
    //   |
    //   beginPos
    //
    // Can be Nothing() if not available.
    MOZ_MUST_USE bool emitEnd(uint32_t argc, const mozilla::Maybe<uint32_t>& beginPos);
};

} /* namespace frontend */
} /* namespace js */

#endif /* frontend_CallOrNewEmitter_h */