2021-06-03 10:46:30 +02:00
|
|
|
/*
|
2025-10-10 11:16:02 +02:00
|
|
|
* Copyright (c) 2021-2025, Andreas Kling <andreas@ladybird.org>
|
2021-06-03 10:46:30 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Forward.h>
|
2024-05-06 07:51:14 +02:00
|
|
|
#include <AK/Function.h>
|
2023-09-01 16:53:55 +02:00
|
|
|
#include <LibJS/Bytecode/Executable.h>
|
2025-11-20 22:14:50 +01:00
|
|
|
#include <LibJS/Bytecode/OpCodes.h>
|
2021-06-03 10:46:30 +02:00
|
|
|
#include <LibJS/Forward.h>
|
2023-09-01 16:53:55 +02:00
|
|
|
#include <LibJS/SourceRange.h>
|
2021-06-03 10:46:30 +02:00
|
|
|
|
2025-11-20 22:14:50 +01:00
|
|
|
namespace JS::Bytecode::Op {
|
|
|
|
|
|
|
|
|
|
#define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
|
|
|
|
|
O(Exp, exp) \
|
|
|
|
|
O(Mod, mod) \
|
|
|
|
|
O(In, in) \
|
|
|
|
|
O(InstanceOf, instance_of) \
|
|
|
|
|
O(LooselyInequals, loosely_inequals) \
|
|
|
|
|
O(LooselyEquals, loosely_equals) \
|
|
|
|
|
O(StrictlyInequals, strict_inequals) \
|
|
|
|
|
O(StrictlyEquals, strict_equals)
|
|
|
|
|
|
|
|
|
|
#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_ENUMERATE_COMPARISON_OPS(X) \
|
|
|
|
|
X(LessThan, less_than, <) \
|
|
|
|
|
X(LessThanEquals, less_than_equals, <=) \
|
|
|
|
|
X(GreaterThan, greater_than, >) \
|
|
|
|
|
X(GreaterThanEquals, greater_than_equals, >=) \
|
|
|
|
|
X(LooselyEquals, loosely_equals, ==) \
|
|
|
|
|
X(LooselyInequals, loosely_inequals, !=) \
|
|
|
|
|
X(StrictlyEquals, strict_equals, ==) \
|
|
|
|
|
X(StrictlyInequals, strict_inequals, !=)
|
|
|
|
|
|
|
|
|
|
enum class EnvironmentMode {
|
|
|
|
|
Lexical,
|
|
|
|
|
Var,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class BindingInitializationMode {
|
|
|
|
|
Initialize,
|
|
|
|
|
Set,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class CallType {
|
|
|
|
|
Call,
|
|
|
|
|
Construct,
|
|
|
|
|
DirectEval,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class ArgumentsKind {
|
|
|
|
|
Mapped,
|
|
|
|
|
Unmapped,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2021-06-07 15:12:43 +02:00
|
|
|
|
2021-06-03 10:46:30 +02:00
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
|
2025-07-19 10:41:08 -07:00
|
|
|
class alignas(void*) Instruction {
|
2021-06-03 10:46:30 +02:00
|
|
|
public:
|
2021-06-09 06:49:58 +04:30
|
|
|
constexpr static bool IsTerminator = false;
|
2024-05-06 10:42:52 +02:00
|
|
|
static constexpr bool IsVariableLength = false;
|
2021-06-09 06:49:58 +04:30
|
|
|
|
2025-10-28 20:25:12 +01:00
|
|
|
enum class Type : u8 {
|
2021-06-07 15:12:43 +02:00
|
|
|
#define __BYTECODE_OP(op) \
|
|
|
|
|
op,
|
|
|
|
|
ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
|
|
|
|
|
#undef __BYTECODE_OP
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Type type() const { return m_type; }
|
2024-05-06 11:09:09 +02:00
|
|
|
size_t length() const;
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString to_byte_string(Bytecode::Executable const&) const;
|
2024-05-06 06:44:08 +02:00
|
|
|
void visit_labels(Function<void(Label&)> visitor);
|
2024-05-12 18:49:03 +02:00
|
|
|
void visit_operands(Function<void(Operand&)> visitor);
|
2021-06-07 15:12:43 +02:00
|
|
|
|
2025-10-28 20:25:12 +01:00
|
|
|
Strict strict() const { return m_strict; }
|
|
|
|
|
void set_strict(Strict strict) { m_strict = strict; }
|
|
|
|
|
|
2021-06-07 15:12:43 +02:00
|
|
|
protected:
|
2024-05-06 11:09:09 +02:00
|
|
|
explicit Instruction(Type type)
|
|
|
|
|
: m_type(type)
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-06-03 10:46:30 +02:00
|
|
|
|
2024-05-06 06:44:08 +02:00
|
|
|
void visit_labels_impl(Function<void(Label&)>) { }
|
2024-05-12 18:49:03 +02:00
|
|
|
void visit_operands_impl(Function<void(Operand&)>) { }
|
2024-05-06 06:44:08 +02:00
|
|
|
|
2021-06-07 15:12:43 +02:00
|
|
|
private:
|
|
|
|
|
Type m_type {};
|
2025-10-28 20:25:12 +01:00
|
|
|
Strict m_strict {};
|
2021-06-03 10:46:30 +02:00
|
|
|
};
|
2021-10-25 13:37:02 +02:00
|
|
|
|
2025-07-19 10:41:08 -07:00
|
|
|
class InstructionStreamIterator {
|
2021-10-25 13:37:02 +02:00
|
|
|
public:
|
2023-10-30 22:06:27 +01:00
|
|
|
InstructionStreamIterator(ReadonlyBytes bytes, Executable const* executable = nullptr, size_t offset = 0)
|
2023-09-26 16:03:42 +02:00
|
|
|
: m_begin(bytes.data())
|
|
|
|
|
, m_end(bytes.data() + bytes.size())
|
2023-10-30 22:06:27 +01:00
|
|
|
, m_ptr(bytes.data() + offset)
|
2023-09-01 16:53:55 +02:00
|
|
|
, m_executable(executable)
|
2021-10-25 13:37:02 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 16:03:42 +02:00
|
|
|
size_t offset() const { return m_ptr - m_begin; }
|
|
|
|
|
bool at_end() const { return m_ptr >= m_end; }
|
2021-10-25 13:37:02 +02:00
|
|
|
|
|
|
|
|
Instruction const& operator*() const { return dereference(); }
|
|
|
|
|
|
|
|
|
|
ALWAYS_INLINE void operator++()
|
|
|
|
|
{
|
2023-09-26 16:03:42 +02:00
|
|
|
m_ptr += dereference().length();
|
2021-10-25 13:37:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-03 08:18:10 +02:00
|
|
|
Executable const* executable() const { return m_executable; }
|
|
|
|
|
|
2021-10-25 13:37:02 +02:00
|
|
|
private:
|
2023-09-26 16:03:42 +02:00
|
|
|
Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_ptr); }
|
2021-10-25 13:37:02 +02:00
|
|
|
|
2023-09-26 16:03:42 +02:00
|
|
|
u8 const* m_begin { nullptr };
|
|
|
|
|
u8 const* m_end { nullptr };
|
|
|
|
|
u8 const* m_ptr { nullptr };
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<Executable const> m_executable;
|
2021-10-25 13:37:02 +02:00
|
|
|
};
|
|
|
|
|
|
2021-06-03 10:46:30 +02:00
|
|
|
}
|