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
|
/* -*- 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/. */
#ifndef frontend_ElemOpEmitter_h
#define frontend_ElemOpEmitter_h
#include "mozilla/Attributes.h"
#include "frontend/Token.h"
namespace js {
namespace frontend {
struct BytecodeEmitter;
// Class for emitting bytecode for element operation.
//
// Usage: (check for the return value is omitted for simplicity)
//
// `obj[key];`
// ElemOpEmitter eoe(this,
// ElemOpEmitter::Kind::Get,
// ElemOpEmitter::ObjKind::Other);
// eoe.prepareForObj();
// emit(obj);
// eoe.prepareForKey();
// emit(key);
// eoe.emitGet();
//
// `super[key];`
// ElemOpEmitter eoe(this,
// ElemOpEmitter::Kind::Get,
// ElemOpEmitter::ObjKind::Super);
// eoe.prepareForObj();
// emit(this_for_super);
// eoe.prepareForKey();
// emit(key);
// eoe.emitGet();
//
// `obj[key]();`
// ElemOpEmitter eoe(this,
// ElemOpEmitter::Kind::Call,
// ElemOpEmitter::ObjKind::Other);
// eoe.prepareForObj();
// emit(obj);
// eoe.prepareForKey();
// emit(key);
// eoe.emitGet();
// emit_call_here();
//
// `new obj[key]();`
// ElemOpEmitter eoe(this,
// ElemOpEmitter::Kind::Call,
// ElemOpEmitter::ObjKind::Other);
// eoe.prepareForObj();
// emit(obj);
// eoe.prepareForKey();
// emit(key);
// eoe.emitGet();
// emit_call_here();
//
// `delete obj[key];`
// ElemOpEmitter eoe(this,
// ElemOpEmitter::Kind::Delete,
// ElemOpEmitter::ObjKind::Other);
// eoe.prepareForObj();
// emit(obj);
// eoe.prepareForKey();
// emit(key);
// eoe.emitDelete();
//
// `delete super[key];`
// ElemOpEmitter eoe(this,
// ElemOpEmitter::Kind::Delete,
// ElemOpEmitter::ObjKind::Super);
// eoe.prepareForObj();
// emit(this_for_super);
// eoe.prepareForKey();
// emit(key);
// eoe.emitDelete();
//
// `obj[key]++;`
// ElemOpEmitter eoe(this,
// ElemOpEmitter::Kind::PostIncrement,
// ElemOpEmitter::ObjKind::Other);
// eoe.prepareForObj();
// emit(obj);
// eoe.prepareForKey();
// emit(key);
// eoe.emitIncDec();
//
// `obj[key] = value;`
// ElemOpEmitter eoe(this,
// ElemOpEmitter::Kind::SimpleAssignment,
// ElemOpEmitter::ObjKind::Other);
// eoe.prepareForObj();
// emit(obj);
// eoe.prepareForKey();
// emit(key);
// eoe.prepareForRhs();
// emit(value);
// eoe.emitAssignment();
//
// `obj[key] += value;`
// ElemOpEmitter eoe(this,
// ElemOpEmitter::Kind::CompoundAssignment,
// ElemOpEmitter::ObjKind::Other);
// eoe.prepareForObj();
// emit(obj);
// eoe.prepareForKey();
// emit(key);
// eoe.emitGet();
// eoe.prepareForRhs();
// emit(value);
// emit_add_op_here();
// eoe.emitAssignment();
//
class MOZ_STACK_CLASS ElemOpEmitter {
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_;
NameVisibility visibility_ = NameVisibility::Public;
#ifdef DEBUG
// The state of this emitter.
//
// skipObjAndKeyAndRhs
// +------------------------------------------------+
// | |
// +-------+ | prepareForObj +-----+ prepareForKey +-----+ |
// | Start |-+-------------->| Obj |-------------->| Key |-+ |
// +-------+ +-----+ +-----+ | |
// | |
// +-------------------------------------------------------+ |
// | |
// | [Get] |
// | [Call] |
// | emitGet +-----+ |
// +---------->| Get | |
// | +-----+ |
// | |
// | [Delete] |
// | emitDelete +--------+ |
// +------------->| Delete | |
// | +--------+ |
// | |
// | [PostIncrement] |
// | [PreIncrement] |
// | [PostDecrement] |
// | [PreDecrement] |
// | emitIncDec +--------+ |
// +------------->| IncDec | |
// | +--------+ |
// | +-------------------+
// | [SimpleAssignment] |
// | [PropInit] |
// | prepareForRhs v +-----+
// +--------------------->+-------------->+->| Rhs |-+
// | ^ +-----+ |
// | | |
// | | +-------------+
// | [CompoundAssignment] | |
// | emitGet +-----+ | | emitAssignment +------------+
// +---------->| Get |----+ +--------------->| Assignment |
// +-----+ +------------+
enum class State {
// The initial state.
Start,
// After calling prepareForObj.
Obj,
// After calling emitKey.
Key,
// After calling emitGet.
Get,
// After calling emitDelete.
Delete,
// After calling emitIncDec.
IncDec,
// After calling prepareForRhs or skipObjAndKeyAndRhs.
Rhs,
// After calling emitAssignment.
Assignment,
};
State state_ = State::Start;
#endif
public:
ElemOpEmitter(BytecodeEmitter* bce, Kind kind, ObjKind objKind,
NameVisibility visibility);
private:
MOZ_MUST_USE bool isCall() const { return kind_ == Kind::Call; }
MOZ_MUST_USE bool isSimpleAssignment() const {
return kind_ == Kind::SimpleAssignment;
}
bool isPrivate() { return visibility_ == NameVisibility::Private; }
MOZ_MUST_USE bool isPropInit() const { return kind_ == Kind::PropInit; }
MOZ_MUST_USE bool isPrivateGet() const {
return visibility_ == NameVisibility::Private && kind_ == Kind::Get;
}
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 isSuper() const { return objKind_ == ObjKind::Super; }
public:
MOZ_MUST_USE bool prepareForObj();
MOZ_MUST_USE bool prepareForKey();
MOZ_MUST_USE bool emitGet();
MOZ_MUST_USE bool prepareForRhs();
MOZ_MUST_USE bool skipObjAndKeyAndRhs();
MOZ_MUST_USE bool emitDelete();
MOZ_MUST_USE bool emitAssignment();
MOZ_MUST_USE bool emitIncDec();
private:
// When we have private names, we may need to emit a CheckPrivateField
// op to potentially throw errors where required.
MOZ_MUST_USE bool emitPrivateGuard();
};
} /* namespace frontend */
} // namespace js
#endif /* frontend_ElemOpEmitter_h */
|