2021-06-03 08:46:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Forward.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) \
|
|
|
|
O(Append) \
|
|
|
|
O(AsyncIteratorClose) \
|
|
|
|
O(Await) \
|
|
|
|
O(BitwiseAnd) \
|
|
|
|
O(BitwiseNot) \
|
|
|
|
O(BitwiseOr) \
|
|
|
|
O(BitwiseXor) \
|
|
|
|
O(BlockDeclarationInstantiation) \
|
|
|
|
O(Call) \
|
|
|
|
O(CallWithArgumentArray) \
|
|
|
|
O(ConcatString) \
|
|
|
|
O(ContinuePendingUnwind) \
|
|
|
|
O(CopyObjectExcludingProperties) \
|
|
|
|
O(CreateLexicalEnvironment) \
|
|
|
|
O(CreateVariable) \
|
|
|
|
O(Decrement) \
|
|
|
|
O(DeleteById) \
|
|
|
|
O(DeleteByIdWithThis) \
|
|
|
|
O(DeleteByValue) \
|
|
|
|
O(DeleteByValueWithThis) \
|
|
|
|
O(DeleteVariable) \
|
|
|
|
O(Div) \
|
|
|
|
O(EnterUnwindContext) \
|
|
|
|
O(EnterObjectEnvironment) \
|
|
|
|
O(Exp) \
|
|
|
|
O(GetById) \
|
|
|
|
O(GetByIdWithThis) \
|
|
|
|
O(GetByValue) \
|
|
|
|
O(GetByValueWithThis) \
|
|
|
|
O(GetCalleeAndThisFromEnvironment) \
|
|
|
|
O(GetIterator) \
|
|
|
|
O(GetMethod) \
|
|
|
|
O(GetNewTarget) \
|
|
|
|
O(GetImportMeta) \
|
|
|
|
O(GetObjectPropertyIterator) \
|
|
|
|
O(GetPrivateById) \
|
|
|
|
O(GetVariable) \
|
|
|
|
O(GetGlobal) \
|
|
|
|
O(GetLocal) \
|
|
|
|
O(GreaterThan) \
|
|
|
|
O(GreaterThanEquals) \
|
|
|
|
O(HasPrivateId) \
|
|
|
|
O(ImportCall) \
|
|
|
|
O(In) \
|
|
|
|
O(Increment) \
|
|
|
|
O(InstanceOf) \
|
|
|
|
O(IteratorClose) \
|
|
|
|
O(IteratorNext) \
|
|
|
|
O(IteratorResultDone) \
|
|
|
|
O(IteratorResultValue) \
|
|
|
|
O(IteratorToArray) \
|
|
|
|
O(Jump) \
|
|
|
|
O(JumpConditional) \
|
|
|
|
O(JumpNullish) \
|
|
|
|
O(JumpUndefined) \
|
|
|
|
O(LeaveLexicalEnvironment) \
|
|
|
|
O(LeaveUnwindContext) \
|
|
|
|
O(LeftShift) \
|
|
|
|
O(LessThan) \
|
|
|
|
O(LessThanEquals) \
|
|
|
|
O(Load) \
|
|
|
|
O(LoadImmediate) \
|
|
|
|
O(LooselyEquals) \
|
|
|
|
O(LooselyInequals) \
|
|
|
|
O(Mod) \
|
|
|
|
O(Mul) \
|
|
|
|
O(NewArray) \
|
|
|
|
O(NewBigInt) \
|
|
|
|
O(NewClass) \
|
|
|
|
O(NewFunction) \
|
|
|
|
O(NewObject) \
|
|
|
|
O(NewRegExp) \
|
|
|
|
O(NewString) \
|
|
|
|
O(NewTypeError) \
|
|
|
|
O(Not) \
|
|
|
|
O(PushDeclarativeEnvironment) \
|
|
|
|
O(PutById) \
|
|
|
|
O(PutByIdWithThis) \
|
|
|
|
O(PutByValue) \
|
|
|
|
O(PutByValueWithThis) \
|
|
|
|
O(PutPrivateById) \
|
|
|
|
O(ResolveThisBinding) \
|
|
|
|
O(ResolveSuperBase) \
|
|
|
|
O(Return) \
|
|
|
|
O(RightShift) \
|
|
|
|
O(ScheduleJump) \
|
|
|
|
O(SetVariable) \
|
|
|
|
O(SetLocal) \
|
|
|
|
O(Store) \
|
|
|
|
O(StrictlyEquals) \
|
|
|
|
O(StrictlyInequals) \
|
|
|
|
O(Sub) \
|
|
|
|
O(SuperCallWithArgumentArray) \
|
|
|
|
O(Throw) \
|
|
|
|
O(ThrowIfNotObject) \
|
|
|
|
O(ThrowIfNullish) \
|
|
|
|
O(ToNumeric) \
|
|
|
|
O(Typeof) \
|
|
|
|
O(TypeofVariable) \
|
|
|
|
O(TypeofLocal) \
|
|
|
|
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;
|
|
|
|
|
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; }
|
2023-09-13 19:32:51 +00:00
|
|
|
size_t length() const { return m_length; }
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString to_deprecated_string(Bytecode::Executable const&) const;
|
2022-02-07 13:36:45 +00:00
|
|
|
ThrowCompletionOr<void> execute(Bytecode::Interpreter&) const;
|
2021-06-07 13:12:43 +00:00
|
|
|
static void destroy(Instruction&);
|
|
|
|
|
2023-09-01 14:53:55 +00:00
|
|
|
// FIXME: Find a better way to organize this information
|
|
|
|
void set_source_record(SourceRecord rec) { m_source_record = rec; }
|
|
|
|
SourceRecord source_record() const { return m_source_record; }
|
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
protected:
|
2023-09-13 19:32:51 +00:00
|
|
|
Instruction(Type type, size_t length)
|
2021-06-07 13:12:43 +00:00
|
|
|
: m_type(type)
|
2023-09-13 19:32:51 +00:00
|
|
|
, m_length(length)
|
2021-06-07 13:12:43 +00:00
|
|
|
{
|
|
|
|
}
|
2021-06-03 08:46:30 +00:00
|
|
|
|
2021-06-07 13:12:43 +00:00
|
|
|
private:
|
2023-09-01 14:53:55 +00:00
|
|
|
SourceRecord m_source_record {};
|
2021-06-07 13:12:43 +00:00
|
|
|
Type m_type {};
|
2023-09-13 19:32:51 +00:00
|
|
|
size_t m_length {};
|
2021-06-03 08:46:30 +00:00
|
|
|
};
|
2021-10-25 11:37:02 +00:00
|
|
|
|
|
|
|
class InstructionStreamIterator {
|
|
|
|
public:
|
2023-09-01 14:53:55 +00:00
|
|
|
InstructionStreamIterator(ReadonlyBytes bytes, Executable const* executable = nullptr)
|
2023-09-26 14:03:42 +00:00
|
|
|
: m_begin(bytes.data())
|
|
|
|
, m_end(bytes.data() + bytes.size())
|
|
|
|
, m_ptr(bytes.data())
|
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 };
|
2023-10-03 06:18:10 +00:00
|
|
|
RefPtr<Executable const> m_executable;
|
2021-10-25 11:37:02 +00:00
|
|
|
};
|
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
}
|