2021-06-03 08:46:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2021-06-07 20:13:37 +00:00
|
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
2021-06-03 08:46:30 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-06-03 16:26:32 +00:00
|
|
|
#include <AK/FlyString.h>
|
2021-06-08 05:59:25 +00:00
|
|
|
#include <LibCrypto/BigInt/SignedBigInteger.h>
|
2021-06-03 08:46:30 +00:00
|
|
|
#include <LibJS/Bytecode/Instruction.h>
|
2021-06-04 10:07:38 +00:00
|
|
|
#include <LibJS/Bytecode/Label.h>
|
2021-06-03 08:46:30 +00:00
|
|
|
#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)
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::Load)
|
|
|
|
, m_dst(dst)
|
2021-06-03 08:46:30 +00:00
|
|
|
, m_value(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-03 08:46:30 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_dst;
|
|
|
|
Value m_value;
|
|
|
|
};
|
|
|
|
|
2021-06-07 20:05:09 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2021-06-07 21:08:35 +00:00
|
|
|
#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
|
2021-06-07 18:53:47 +00:00
|
|
|
|
2021-06-03 16:26:32 +00:00
|
|
|
class NewString final : public Instruction {
|
|
|
|
public:
|
|
|
|
NewString(Register dst, String string)
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::NewString)
|
|
|
|
, m_dst(dst)
|
2021-06-03 16:26:32 +00:00
|
|
|
, m_string(move(string))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-03 16:26:32 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_dst;
|
|
|
|
String m_string;
|
|
|
|
};
|
|
|
|
|
2021-06-04 18:30:23 +00:00
|
|
|
class NewObject final : public Instruction {
|
|
|
|
public:
|
|
|
|
explicit NewObject(Register dst)
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::NewObject)
|
|
|
|
, m_dst(dst)
|
2021-06-04 18:30:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-04 18:30:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_dst;
|
|
|
|
};
|
|
|
|
|
2021-06-08 05:59:25 +00:00
|
|
|
class NewBigInt final : public Instruction {
|
|
|
|
public:
|
|
|
|
explicit NewBigInt(Register dst, Crypto::SignedBigInteger bigint)
|
|
|
|
: Instruction(Type::NewBigInt)
|
|
|
|
, m_dst(dst)
|
|
|
|
, m_bigint(move(bigint))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_dst;
|
|
|
|
Crypto::SignedBigInteger m_bigint;
|
|
|
|
};
|
|
|
|
|
2021-06-03 16:26:32 +00:00
|
|
|
class SetVariable final : public Instruction {
|
|
|
|
public:
|
|
|
|
SetVariable(FlyString identifier, Register src)
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::SetVariable)
|
|
|
|
, m_identifier(move(identifier))
|
2021-06-03 16:26:32 +00:00
|
|
|
, m_src(src)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-03 16:26:32 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
FlyString m_identifier;
|
|
|
|
Register m_src;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GetVariable final : public Instruction {
|
|
|
|
public:
|
|
|
|
GetVariable(Register dst, FlyString identifier)
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::GetVariable)
|
|
|
|
, m_dst(dst)
|
2021-06-03 16:26:32 +00:00
|
|
|
, m_identifier(move(identifier))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-03 16:26:32 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_dst;
|
|
|
|
FlyString m_identifier;
|
|
|
|
};
|
|
|
|
|
2021-06-04 19:03:53 +00:00
|
|
|
class GetById final : public Instruction {
|
|
|
|
public:
|
|
|
|
GetById(Register dst, Register base, FlyString property)
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::GetById)
|
|
|
|
, m_dst(dst)
|
2021-06-04 19:03:53 +00:00
|
|
|
, m_base(base)
|
|
|
|
, m_property(move(property))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-04 19:03:53 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_dst;
|
|
|
|
Register m_base;
|
|
|
|
FlyString m_property;
|
|
|
|
};
|
|
|
|
|
2021-06-04 18:47:07 +00:00
|
|
|
class PutById final : public Instruction {
|
|
|
|
public:
|
|
|
|
PutById(Register base, FlyString property, Register src)
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::PutById)
|
|
|
|
, m_base(base)
|
2021-06-04 18:47:07 +00:00
|
|
|
, m_property(move(property))
|
|
|
|
, m_src(src)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-04 18:47:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_base;
|
|
|
|
FlyString m_property;
|
|
|
|
Register m_src;
|
|
|
|
};
|
|
|
|
|
2021-06-08 09:28:27 +00:00
|
|
|
class Jump : public Instruction {
|
2021-06-04 10:07:38 +00:00
|
|
|
public:
|
2021-06-08 09:28:27 +00:00
|
|
|
explicit Jump(Type type, Optional<Label> target = {})
|
|
|
|
: Instruction(type)
|
|
|
|
, m_target(move(target))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-06 11:26:50 +00:00
|
|
|
explicit Jump(Optional<Label> target = {})
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::Jump)
|
|
|
|
, m_target(move(target))
|
2021-06-04 10:07:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-06 11:26:50 +00:00
|
|
|
void set_target(Optional<Label> target) { m_target = move(target); }
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-04 10:07:38 +00:00
|
|
|
|
2021-06-08 09:28:27 +00:00
|
|
|
protected:
|
2021-06-06 11:26:50 +00:00
|
|
|
Optional<Label> m_target;
|
2021-06-04 10:07:38 +00:00
|
|
|
};
|
|
|
|
|
2021-06-08 09:28:27 +00:00
|
|
|
class JumpIfFalse final : public Jump {
|
2021-06-04 10:07:38 +00:00
|
|
|
public:
|
|
|
|
explicit JumpIfFalse(Register result, Optional<Label> target = {})
|
2021-06-08 09:28:27 +00:00
|
|
|
: Jump(Type::JumpIfFalse, move(target))
|
2021-06-07 13:12:43 +00:00
|
|
|
, m_result(result)
|
2021-06-04 10:07:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-04 10:07:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_result;
|
|
|
|
};
|
|
|
|
|
2021-06-08 09:28:27 +00:00
|
|
|
class JumpIfTrue : public Jump {
|
2021-06-04 10:20:44 +00:00
|
|
|
public:
|
|
|
|
explicit JumpIfTrue(Register result, Optional<Label> target = {})
|
2021-06-08 09:28:27 +00:00
|
|
|
: Jump(Type::JumpIfTrue, move(target))
|
2021-06-07 13:12:43 +00:00
|
|
|
, m_result(result)
|
2021-06-04 10:20:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-04 10:20:44 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_result;
|
|
|
|
};
|
|
|
|
|
2021-06-08 09:28:27 +00:00
|
|
|
class JumpIfNullish final : public Jump {
|
2021-06-08 00:18:47 +00:00
|
|
|
public:
|
|
|
|
explicit JumpIfNullish(Register result, Optional<Label> target = {})
|
2021-06-08 09:28:27 +00:00
|
|
|
: Jump(Type::JumpIfNullish, move(target))
|
2021-06-08 00:18:47 +00:00
|
|
|
, m_result(result)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_result;
|
|
|
|
};
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
// NOTE: This instruction is variable-width depending on the number of arguments!
|
2021-06-05 13:15:30 +00:00
|
|
|
class Call final : public Instruction {
|
|
|
|
public:
|
2021-06-07 13:12:43 +00:00
|
|
|
Call(Register dst, Register callee, Register this_value, Vector<Register> const& arguments)
|
|
|
|
: Instruction(Type::Call)
|
|
|
|
, m_dst(dst)
|
2021-06-05 13:15:30 +00:00
|
|
|
, m_callee(callee)
|
|
|
|
, m_this_value(this_value)
|
2021-06-07 13:12:43 +00:00
|
|
|
, m_argument_count(arguments.size())
|
2021-06-05 13:15:30 +00:00
|
|
|
{
|
2021-06-07 13:12:43 +00:00
|
|
|
for (size_t i = 0; i < m_argument_count; ++i)
|
|
|
|
m_arguments[i] = arguments[i];
|
2021-06-05 13:15:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
|
|
|
|
|
|
|
size_t length() const { return sizeof(*this) + sizeof(Register) * m_argument_count; }
|
2021-06-05 13:15:30 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Register m_dst;
|
|
|
|
Register m_callee;
|
|
|
|
Register m_this_value;
|
2021-06-07 13:12:43 +00:00
|
|
|
size_t m_argument_count { 0 };
|
|
|
|
Register m_arguments[];
|
2021-06-05 13:15:30 +00:00
|
|
|
};
|
|
|
|
|
2021-06-05 13:14:09 +00:00
|
|
|
class EnterScope final : public Instruction {
|
|
|
|
public:
|
|
|
|
explicit EnterScope(ScopeNode const& scope_node)
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::EnterScope)
|
|
|
|
, m_scope_node(scope_node)
|
2021-06-05 13:14:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-05 13:14:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ScopeNode const& m_scope_node;
|
|
|
|
};
|
|
|
|
|
2021-06-05 13:53:36 +00:00
|
|
|
class Return final : public Instruction {
|
|
|
|
public:
|
|
|
|
explicit Return(Optional<Register> argument)
|
2021-06-07 13:12:43 +00:00
|
|
|
: Instruction(Type::Return)
|
|
|
|
, m_argument(move(argument))
|
2021-06-05 13:53:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
|
|
|
String to_string() const;
|
2021-06-05 13:53:36 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Optional<Register> m_argument;
|
|
|
|
};
|
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
}
|