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
|
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <LibJS/Bytecode/Instruction.h>
#include <LibJS/Bytecode/Label.h>
#include <LibJS/Bytecode/Register.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Runtime/Value.h>
namespace JS::Bytecode::Op {
class Load final : public Instruction {
public:
Load(Register dst, Value value)
: Instruction(Type::Load)
, m_dst(dst)
, m_value(value)
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_dst;
Value m_value;
};
class LoadRegister final : public Instruction {
public:
LoadRegister(Register dst, Register src)
: Instruction(Type::LoadRegister)
, m_dst(dst)
, m_src(src)
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_dst;
Register m_src;
};
#define JS_ENUMERATE_COMMON_BINARY_OPS(O) \
O(Add, add) \
O(Sub, sub) \
O(Mul, mul) \
O(Div, div) \
O(Exp, exp) \
O(Mod, mod) \
O(In, in) \
O(InstanceOf, instance_of) \
O(GreaterThan, greater_than) \
O(GreaterThanEquals, greater_than_equals) \
O(LessThan, less_than) \
O(LessThanEquals, less_than_equals) \
O(AbstractInequals, abstract_inequals) \
O(AbstractEquals, abstract_equals) \
O(TypedInequals, typed_inequals) \
O(TypedEquals, typed_equals) \
O(BitwiseAnd, bitwise_and) \
O(BitwiseOr, bitwise_or) \
O(BitwiseXor, bitwise_xor) \
O(LeftShift, left_shift) \
O(RightShift, right_shift) \
O(UnsignedRightShift, unsigned_right_shift)
#define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
class OpTitleCase final : public Instruction { \
public: \
OpTitleCase(Register dst, Register src1, Register src2) \
: Instruction(Type::OpTitleCase) \
, m_dst(dst) \
, m_src1(src1) \
, m_src2(src2) \
{ \
} \
\
void execute(Bytecode::Interpreter&) const; \
String to_string() const; \
\
private: \
Register m_dst; \
Register m_src1; \
Register m_src2; \
};
JS_ENUMERATE_COMMON_BINARY_OPS(JS_DECLARE_COMMON_BINARY_OP)
#undef JS_DECLARE_COMMON_BINARY_OP
#define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
O(BitwiseNot, bitwise_not) \
O(Not, not_) \
O(UnaryPlus, unary_plus) \
O(UnaryMinus, unary_minus) \
O(Typeof, typeof_)
#define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
class OpTitleCase final : public Instruction { \
public: \
OpTitleCase(Register dst, Register src) \
: Instruction(Type::OpTitleCase) \
, m_dst(dst) \
, m_src(src) \
{ \
} \
\
void execute(Bytecode::Interpreter&) const; \
String to_string() const; \
\
private: \
Register m_dst; \
Register m_src; \
};
JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
#undef JS_DECLARE_COMMON_UNARY_OP
class NewString final : public Instruction {
public:
NewString(Register dst, String string)
: Instruction(Type::NewString)
, m_dst(dst)
, m_string(move(string))
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_dst;
String m_string;
};
class NewObject final : public Instruction {
public:
explicit NewObject(Register dst)
: Instruction(Type::NewObject)
, m_dst(dst)
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_dst;
};
class SetVariable final : public Instruction {
public:
SetVariable(FlyString identifier, Register src)
: Instruction(Type::SetVariable)
, m_identifier(move(identifier))
, m_src(src)
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
FlyString m_identifier;
Register m_src;
};
class GetVariable final : public Instruction {
public:
GetVariable(Register dst, FlyString identifier)
: Instruction(Type::GetVariable)
, m_dst(dst)
, m_identifier(move(identifier))
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_dst;
FlyString m_identifier;
};
class GetById final : public Instruction {
public:
GetById(Register dst, Register base, FlyString property)
: Instruction(Type::GetById)
, m_dst(dst)
, m_base(base)
, m_property(move(property))
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_dst;
Register m_base;
FlyString m_property;
};
class PutById final : public Instruction {
public:
PutById(Register base, FlyString property, Register src)
: Instruction(Type::PutById)
, m_base(base)
, m_property(move(property))
, m_src(src)
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_base;
FlyString m_property;
Register m_src;
};
class Jump final : public Instruction {
public:
explicit Jump(Optional<Label> target = {})
: Instruction(Type::Jump)
, m_target(move(target))
{
}
void set_target(Optional<Label> target) { m_target = move(target); }
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Optional<Label> m_target;
};
class JumpIfFalse final : public Instruction {
public:
explicit JumpIfFalse(Register result, Optional<Label> target = {})
: Instruction(Type::JumpIfFalse)
, m_result(result)
, m_target(move(target))
{
}
void set_target(Optional<Label> target) { m_target = move(target); }
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_result;
Optional<Label> m_target;
};
class JumpIfTrue final : public Instruction {
public:
explicit JumpIfTrue(Register result, Optional<Label> target = {})
: Instruction(Type::JumpIfTrue)
, m_result(result)
, m_target(move(target))
{
}
void set_target(Optional<Label> target) { m_target = move(target); }
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_result;
Optional<Label> m_target;
};
class JumpIfNullish final : public Instruction {
public:
explicit JumpIfNullish(Register result, Optional<Label> target = {})
: Instruction(Type::JumpIfNullish)
, m_result(result)
, m_target(move(target))
{
}
void set_target(Optional<Label> target) { m_target = move(target); }
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_result;
Optional<Label> m_target;
};
// NOTE: This instruction is variable-width depending on the number of arguments!
class Call final : public Instruction {
public:
Call(Register dst, Register callee, Register this_value, Vector<Register> const& arguments)
: Instruction(Type::Call)
, m_dst(dst)
, m_callee(callee)
, m_this_value(this_value)
, m_argument_count(arguments.size())
{
for (size_t i = 0; i < m_argument_count; ++i)
m_arguments[i] = arguments[i];
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
size_t length() const { return sizeof(*this) + sizeof(Register) * m_argument_count; }
private:
Register m_dst;
Register m_callee;
Register m_this_value;
size_t m_argument_count { 0 };
Register m_arguments[];
};
class EnterScope final : public Instruction {
public:
explicit EnterScope(ScopeNode const& scope_node)
: Instruction(Type::EnterScope)
, m_scope_node(scope_node)
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
ScopeNode const& m_scope_node;
};
class Return final : public Instruction {
public:
explicit Return(Optional<Register> argument)
: Instruction(Type::Return)
, m_argument(move(argument))
{
}
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Optional<Register> m_argument;
};
}
|