summaryrefslogtreecommitdiff
path: root/js/src/frontend/PropOpEmitter.h
blob: 6c56a59f25e095f974510569fb0033b73d80edc0 (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
/* -*- 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_PropOpEmitter_h
#define frontend_PropOpEmitter_h

#include "mozilla/Attributes.h"

#include <stdint.h>

#include "js/TypeDecls.h"

class JSAtom;

namespace js {
namespace frontend {

struct BytecodeEmitter;

// Class for emitting bytecode for property operation.
//
// Usage: (check for the return value is omitted for simplicity)
//
//   `obj.prop;`
//     PropOpEmitter poe(this,
//                       PropOpEmitter::Kind::Get,
//                       PropOpEmitter::ObjKind::Other);
//     poe.prepareForObj();
//     emit(obj);
//     poe.emitGet(atom_of_prop);
//
//   `super.prop;`
//     PropOpEmitter poe(this,
//                       PropOpEmitter::Kind::Get,
//                       PropOpEmitter::ObjKind::Super);
//     poe.prepareForObj();
//     emit(obj);
//     poe.emitGet(atom_of_prop);
//
//   `obj.prop();`
//     PropOpEmitter poe(this,
//                       PropOpEmitter::Kind::Call,
//                       PropOpEmitter::ObjKind::Other);
//     poe.prepareForObj();
//     emit(obj);
//     poe.emitGet(atom_of_prop);
//     emit_call_here();
//
//   `new obj.prop();`
//     PropOpEmitter poe(this,
//                       PropOpEmitter::Kind::Call,
//                       PropOpEmitter::ObjKind::Other);
//     poe.prepareForObj();
//     emit(obj);
//     poe.emitGet(atom_of_prop);
//     emit_call_here();
//
//   `delete obj.prop;`
//     PropOpEmitter poe(this,
//                       PropOpEmitter::Kind::Delete,
//                       PropOpEmitter::ObjKind::Other);
//     poe.prepareForObj();
//     emit(obj);
//     poe.emitDelete(atom_of_prop);
//
//   `delete super.prop;`
//     PropOpEmitter poe(this,
//                       PropOpEmitter::Kind::Delete,
//                       PropOpEmitter::ObjKind::Other);
//     poe.emitDelete(atom_of_prop);
//
//   `obj.prop++;`
//     PropOpEmitter poe(this,
//                       PropOpEmitter::Kind::PostIncrement,
//                       PropOpEmitter::ObjKind::Other);
//     poe.prepareForObj();
//     emit(obj);
//     poe.emitIncDec(atom_of_prop);
//
//   `obj.prop = value;`
//     PropOpEmitter poe(this,
//                       PropOpEmitter::Kind::SimpleAssignment,
//                       PropOpEmitter::ObjKind::Other);
//     poe.prepareForObj();
//     emit(obj);
//     poe.prepareForRhs();
//     emit(value);
//     poe.emitAssignment(atom_of_prop);
//
//   `obj.prop += value;`
//     PropOpEmitter poe(this,
//                       PropOpEmitter::Kind::CompoundAssignment,
//                       PropOpEmitter::ObjKind::Other);
//     poe.prepareForObj();
//     emit(obj);
//     poe.emitGet(atom_of_prop);
//     poe.prepareForRhs();
//     emit(value);
//     emit_add_op_here();
//     poe.emitAssignment(nullptr); // nullptr for CompoundAssignment
//
class MOZ_STACK_CLASS PropOpEmitter
{
  public:
    enum class Kind {
        Get,
        Call,
        Set,
        Delete,
        PostIncrement,
        PreIncrement,
        PostDecrement,
        PreDecrement,
        SimpleAssignment,
        PropInit,
        CompoundAssignment
    };
    enum class ObjKind {
        Super,
        Other
    };

  private:
    BytecodeEmitter* bce_;

    Kind kind_;
    ObjKind objKind_;

    // The index for the property name's atom.
    uint32_t propAtomIndex_ = 0;

    // Whether the property name is `length` or not.
    bool isLength_ = false;

#ifdef DEBUG
    // The state of this emitter.
    //
    //             skipObjAndRhs
    //           +----------------------------+
    //           |                            |
    // +-------+ | prepareForObj +-----+      |
    // | Start |-+-------------->| Obj |-+    |
    // +-------+                 +-----+ |    |
    //                                   |    |
    // +---------------------------------+    |
    // |                                      |
    // |                                      |
    // | [Get]                                |
    // | [Call]                               |
    // |   emitGet +-----+                    |
    // +---------->| Get |                    |
    // |           +-----+                    |
    // |                                      |
    // | [Delete]                             |
    // |   emitDelete +--------+              |
    // +------------->| Delete |              |
    // |              +--------+              |
    // |                                      |
    // | [PostIncrement]                      |
    // | [PreIncrement]                       |
    // | [PostDecrement]                      |
    // | [PreDecrement]                       |
    // |   emitIncDec +--------+              |
    // +------------->| IncDec |              |
    // |              +--------+              |
    // |                                      |
    // | [SimpleAssignment]                   |
    // | [PropInit]                           |
    // |                        prepareForRhs |  +-----+
    // +--------------------->+-------------->+->| Rhs |-+
    // |                      ^                  +-----+ |
    // |                      |                          |
    // |                      |                +---------+
    // | [CompoundAssignment] |                |
    // |   emitGet +-----+    |                | emitAssignment +------------+
    // +---------->| Get |----+                + -------------->| Assignment |
    //             +-----+                                      +------------+
    enum class State {
        // The initial state.
        Start,

        // After calling prepareForObj.
        Obj,

        // After calling emitGet.
        Get,

        // After calling emitDelete.
        Delete,

        // After calling emitIncDec.
        IncDec,

        // After calling prepareForRhs or skipObjAndRhs.
        Rhs,

        // After calling emitAssignment.
        Assignment,
    };
    State state_ = State::Start;
#endif

  public:
    PropOpEmitter(BytecodeEmitter* bce, Kind kind, ObjKind objKind);

  private:
    MOZ_MUST_USE bool isCall() const {
        return kind_ == Kind::Call;
    }

    MOZ_MUST_USE bool isSuper() const {
        return objKind_ == ObjKind::Super;
    }

    MOZ_MUST_USE bool isSimpleAssignment() const {
        return kind_ == Kind::SimpleAssignment;
    }

    MOZ_MUST_USE bool isPropInit() const {
        return kind_ == Kind::PropInit;
    }

    MOZ_MUST_USE bool isDelete() const {
        return kind_ == Kind::Delete;
    }

    MOZ_MUST_USE bool isCompoundAssignment() const {
        return kind_ == Kind::CompoundAssignment;
    }

    MOZ_MUST_USE bool isIncDec() const {
        return isPostIncDec() || isPreIncDec();
    }

    MOZ_MUST_USE bool isPostIncDec() const {
        return kind_ == Kind::PostIncrement ||
               kind_ == Kind::PostDecrement;
    }

    MOZ_MUST_USE bool isPreIncDec() const {
        return kind_ == Kind::PreIncrement ||
               kind_ == Kind::PreDecrement;
    }

    MOZ_MUST_USE bool isInc() const {
        return kind_ == Kind::PostIncrement ||
               kind_ == Kind::PreIncrement;
    }

    MOZ_MUST_USE bool
    prepareAtomIndex(JSAtom* prop);

  public:
    MOZ_MUST_USE bool prepareForObj();

    MOZ_MUST_USE bool emitGet(JSAtom* prop);

    MOZ_MUST_USE bool prepareForRhs();
    MOZ_MUST_USE bool skipObjAndRhs();

    MOZ_MUST_USE bool emitDelete(JSAtom* prop);

    // `prop` can be nullptr for CompoundAssignment.
    MOZ_MUST_USE bool emitAssignment(JSAtom* prop);

    MOZ_MUST_USE bool emitIncDec(JSAtom* prop);
};

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

#endif /* frontend_PropOpEmitter_h */