2021-06-03 08:46:30 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org>
|
2021-06-03 08:46:30 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Forward.h>
|
2024-05-06 05:51:14 +00:00
|
|
|
#include <AK/Function.h>
|
2021-10-25 11:37:02 +00:00
|
|
|
#include <AK/Span.h>
|
2023-09-01 14:53:55 +00:00
|
|
|
#include <LibJS/Bytecode/Executable.h>
|
2021-06-03 08:46:30 +00:00
|
|
|
#include <LibJS/Forward.h>
|
2023-09-01 14:53:55 +00:00
|
|
|
#include <LibJS/SourceRange.h>
|
2021-06-03 08:46:30 +00:00
|
|
|
|
2023-08-01 12:33:58 +00:00
|
|
|
#define ENUMERATE_BYTECODE_OPS(O) \
|
|
|
|
O(Add) \
|
2024-05-11 22:54:41 +00:00
|
|
|
O(AddPrivateName) \
|
2024-02-04 07:00:54 +00:00
|
|
|
O(ArrayAppend) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(AsyncIteratorClose) \
|
|
|
|
O(Await) \
|
|
|
|
O(BitwiseAnd) \
|
|
|
|
O(BitwiseNot) \
|
|
|
|
O(BitwiseOr) \
|
|
|
|
O(BitwiseXor) \
|
|
|
|
O(BlockDeclarationInstantiation) \
|
|
|
|
O(Call) \
|
2024-10-31 21:47:30 +00:00
|
|
|
O(CallBuiltin) \
|
|
|
|
O(CallConstruct) \
|
|
|
|
O(CallDirectEval) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(CallWithArgumentArray) \
|
2023-11-11 22:19:46 +00:00
|
|
|
O(Catch) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(ConcatString) \
|
|
|
|
O(ContinuePendingUnwind) \
|
|
|
|
O(CopyObjectExcludingProperties) \
|
2024-05-14 09:06:12 +00:00
|
|
|
O(CreateArguments) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(CreateLexicalEnvironment) \
|
2024-05-11 22:54:41 +00:00
|
|
|
O(CreatePrivateEnvironment) \
|
2024-05-05 20:06:55 +00:00
|
|
|
O(CreateRestParams) \
|
2024-05-14 09:06:12 +00:00
|
|
|
O(CreateVariable) \
|
|
|
|
O(CreateVariableEnvironment) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(Decrement) \
|
|
|
|
O(DeleteById) \
|
|
|
|
O(DeleteByIdWithThis) \
|
|
|
|
O(DeleteByValue) \
|
|
|
|
O(DeleteByValueWithThis) \
|
|
|
|
O(DeleteVariable) \
|
|
|
|
O(Div) \
|
2024-02-04 07:00:54 +00:00
|
|
|
O(Dump) \
|
|
|
|
O(End) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(EnterObjectEnvironment) \
|
2024-04-11 09:58:18 +00:00
|
|
|
O(EnterUnwindContext) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(Exp) \
|
2024-05-05 20:06:55 +00:00
|
|
|
O(GetArgument) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(GetById) \
|
|
|
|
O(GetByIdWithThis) \
|
|
|
|
O(GetByValue) \
|
|
|
|
O(GetByValueWithThis) \
|
|
|
|
O(GetCalleeAndThisFromEnvironment) \
|
2024-05-14 09:06:12 +00:00
|
|
|
O(GetGlobal) \
|
|
|
|
O(GetImportMeta) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(GetIterator) \
|
2024-05-20 09:53:28 +00:00
|
|
|
O(GetLength) \
|
|
|
|
O(GetLengthWithThis) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(GetMethod) \
|
|
|
|
O(GetNewTarget) \
|
2023-12-07 09:44:41 +00:00
|
|
|
O(GetNextMethodFromIteratorRecord) \
|
2024-04-11 09:58:18 +00:00
|
|
|
O(GetObjectFromIteratorRecord) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(GetObjectPropertyIterator) \
|
|
|
|
O(GetPrivateById) \
|
2024-05-14 09:32:04 +00:00
|
|
|
O(GetBinding) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(GreaterThan) \
|
|
|
|
O(GreaterThanEquals) \
|
|
|
|
O(HasPrivateId) \
|
|
|
|
O(ImportCall) \
|
|
|
|
O(In) \
|
|
|
|
O(Increment) \
|
2024-05-14 09:30:30 +00:00
|
|
|
O(InitializeLexicalBinding) \
|
|
|
|
O(InitializeVariableBinding) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(InstanceOf) \
|
|
|
|
O(IteratorClose) \
|
|
|
|
O(IteratorNext) \
|
|
|
|
O(IteratorToArray) \
|
|
|
|
O(Jump) \
|
2024-05-06 08:15:17 +00:00
|
|
|
O(JumpFalse) \
|
2024-05-09 13:13:31 +00:00
|
|
|
O(JumpGreaterThan) \
|
|
|
|
O(JumpGreaterThanEquals) \
|
2024-02-04 07:00:54 +00:00
|
|
|
O(JumpIf) \
|
2024-05-09 13:13:31 +00:00
|
|
|
O(JumpLessThan) \
|
|
|
|
O(JumpLessThanEquals) \
|
|
|
|
O(JumpLooselyEquals) \
|
|
|
|
O(JumpLooselyInequals) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(JumpNullish) \
|
2024-05-09 13:13:31 +00:00
|
|
|
O(JumpStrictlyEquals) \
|
|
|
|
O(JumpStrictlyInequals) \
|
2024-05-06 08:15:17 +00:00
|
|
|
O(JumpTrue) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(JumpUndefined) \
|
2024-04-11 09:58:18 +00:00
|
|
|
O(LeaveFinally) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(LeaveLexicalEnvironment) \
|
2024-05-11 22:54:41 +00:00
|
|
|
O(LeavePrivateEnvironment) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(LeaveUnwindContext) \
|
|
|
|
O(LeftShift) \
|
|
|
|
O(LessThan) \
|
|
|
|
O(LessThanEquals) \
|
|
|
|
O(LooselyEquals) \
|
|
|
|
O(LooselyInequals) \
|
|
|
|
O(Mod) \
|
2024-02-04 07:00:54 +00:00
|
|
|
O(Mov) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(Mul) \
|
|
|
|
O(NewArray) \
|
|
|
|
O(NewClass) \
|
|
|
|
O(NewFunction) \
|
|
|
|
O(NewObject) \
|
2023-11-17 20:07:23 +00:00
|
|
|
O(NewPrimitiveArray) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(NewRegExp) \
|
|
|
|
O(NewTypeError) \
|
|
|
|
O(Not) \
|
2024-05-18 15:25:43 +00:00
|
|
|
O(PrepareYield) \
|
2024-02-20 10:45:01 +00:00
|
|
|
O(PostfixDecrement) \
|
|
|
|
O(PostfixIncrement) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(PutById) \
|
|
|
|
O(PutByIdWithThis) \
|
2024-11-01 21:00:32 +00:00
|
|
|
O(PutBySpread) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(PutByValue) \
|
|
|
|
O(PutByValueWithThis) \
|
|
|
|
O(PutPrivateById) \
|
|
|
|
O(ResolveSuperBase) \
|
2024-05-14 09:06:12 +00:00
|
|
|
O(ResolveThisBinding) \
|
2024-04-11 09:07:35 +00:00
|
|
|
O(RestoreScheduledJump) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(Return) \
|
|
|
|
O(RightShift) \
|
|
|
|
O(ScheduleJump) \
|
2024-05-05 20:06:55 +00:00
|
|
|
O(SetArgument) \
|
2024-05-14 09:30:30 +00:00
|
|
|
O(SetLexicalBinding) \
|
|
|
|
O(SetVariableBinding) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(StrictlyEquals) \
|
|
|
|
O(StrictlyInequals) \
|
|
|
|
O(Sub) \
|
|
|
|
O(SuperCallWithArgumentArray) \
|
|
|
|
O(Throw) \
|
|
|
|
O(ThrowIfNotObject) \
|
|
|
|
O(ThrowIfNullish) \
|
2024-02-04 07:00:54 +00:00
|
|
|
O(ThrowIfTDZ) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(Typeof) \
|
2024-06-14 07:37:26 +00:00
|
|
|
O(TypeofBinding) \
|
2023-08-01 12:33:58 +00:00
|
|
|
O(UnaryMinus) \
|
|
|
|
O(UnaryPlus) \
|
|
|
|
O(UnsignedRightShift) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(Yield)
|
2021-06-07 13:12:43 +00:00
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
2022-09-09 14:47:42 +00:00
|
|
|
class alignas(void*) Instruction {
|
2021-06-03 08:46:30 +00:00
|
|
|
public:
|
2021-06-09 02:19:58 +00:00
|
|
|
constexpr static bool IsTerminator = false;
|
2024-05-06 08:42:52 +00:00
|
|
|
static constexpr bool IsVariableLength = false;
|
2021-06-09 02:19:58 +00:00
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
enum class Type {
|
|
|
|
#define __BYTECODE_OP(op) \
|
|
|
|
op,
|
|
|
|
ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
|
|
|
|
#undef __BYTECODE_OP
|
|
|
|
};
|
|
|
|
|
|
|
|
Type type() const { return m_type; }
|
2024-05-06 09:09:09 +00:00
|
|
|
size_t length() const;
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString to_byte_string(Bytecode::Executable const&) const;
|
2024-05-06 04:44:08 +00:00
|
|
|
void visit_labels(Function<void(Label&)> visitor);
|
2024-05-12 16:49:03 +00:00
|
|
|
void visit_operands(Function<void(Operand&)> visitor);
|
2021-06-07 13:12:43 +00:00
|
|
|
static void destroy(Instruction&);
|
|
|
|
|
|
|
|
protected:
|
2024-05-06 09:09:09 +00:00
|
|
|
explicit Instruction(Type type)
|
|
|
|
: m_type(type)
|
|
|
|
{
|
|
|
|
}
|
2021-06-03 08:46:30 +00:00
|
|
|
|
2024-05-06 04:44:08 +00:00
|
|
|
void visit_labels_impl(Function<void(Label&)>) { }
|
2024-05-12 16:49:03 +00:00
|
|
|
void visit_operands_impl(Function<void(Operand&)>) { }
|
2024-05-06 04:44:08 +00:00
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
private:
|
|
|
|
Type m_type {};
|
2021-06-03 08:46:30 +00:00
|
|
|
};
|
2021-10-25 11:37:02 +00:00
|
|
|
|
|
|
|
class InstructionStreamIterator {
|
|
|
|
public:
|
2023-10-30 21:06:27 +00:00
|
|
|
InstructionStreamIterator(ReadonlyBytes bytes, Executable const* executable = nullptr, size_t offset = 0)
|
2023-09-26 14:03:42 +00:00
|
|
|
: m_begin(bytes.data())
|
|
|
|
, m_end(bytes.data() + bytes.size())
|
2023-10-30 21:06:27 +00:00
|
|
|
, m_ptr(bytes.data() + offset)
|
2023-09-01 14:53:55 +00:00
|
|
|
, m_executable(executable)
|
2021-10-25 11:37:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:03:42 +00:00
|
|
|
size_t offset() const { return m_ptr - m_begin; }
|
|
|
|
bool at_end() const { return m_ptr >= m_end; }
|
2021-10-25 11:37:02 +00:00
|
|
|
|
|
|
|
Instruction const& operator*() const { return dereference(); }
|
|
|
|
|
|
|
|
ALWAYS_INLINE void operator++()
|
|
|
|
{
|
2023-09-26 14:03:42 +00:00
|
|
|
m_ptr += dereference().length();
|
2021-10-25 11:37:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 14:53:55 +00:00
|
|
|
UnrealizedSourceRange source_range() const;
|
|
|
|
RefPtr<SourceCode> source_code() const;
|
|
|
|
|
2023-10-03 06:18:10 +00:00
|
|
|
Executable const* executable() const { return m_executable; }
|
|
|
|
|
2021-10-25 11:37:02 +00:00
|
|
|
private:
|
2023-09-26 14:03:42 +00:00
|
|
|
Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_ptr); }
|
2021-10-25 11:37:02 +00:00
|
|
|
|
2023-09-26 14:03:42 +00:00
|
|
|
u8 const* m_begin { nullptr };
|
|
|
|
u8 const* m_end { nullptr };
|
|
|
|
u8 const* m_ptr { nullptr };
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<Executable const> m_executable;
|
2021-10-25 11:37:02 +00:00
|
|
|
};
|
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
}
|