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>
|
2021-06-03 08:46:30 +00:00
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
|
2021-07-01 10:24:46 +00:00
|
|
|
#define ENUMERATE_BYTECODE_OPS(O) \
|
|
|
|
O(Add) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(BitwiseAnd) \
|
|
|
|
O(BitwiseNot) \
|
|
|
|
O(BitwiseOr) \
|
|
|
|
O(BitwiseXor) \
|
|
|
|
O(Call) \
|
|
|
|
O(ConcatString) \
|
|
|
|
O(ContinuePendingUnwind) \
|
|
|
|
O(CopyObjectExcludingProperties) \
|
|
|
|
O(Decrement) \
|
2021-07-01 10:24:46 +00:00
|
|
|
O(Div) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(EnterUnwindContext) \
|
2021-07-01 10:24:46 +00:00
|
|
|
O(Exp) \
|
2021-11-10 18:52:26 +00:00
|
|
|
O(FinishUnwind) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(GetById) \
|
|
|
|
O(GetByValue) \
|
|
|
|
O(GetIterator) \
|
|
|
|
O(GetVariable) \
|
2021-07-01 10:24:46 +00:00
|
|
|
O(GreaterThan) \
|
|
|
|
O(GreaterThanEquals) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(In) \
|
|
|
|
O(Increment) \
|
|
|
|
O(InstanceOf) \
|
|
|
|
O(IteratorNext) \
|
|
|
|
O(IteratorResultDone) \
|
|
|
|
O(IteratorResultValue) \
|
|
|
|
O(IteratorToArray) \
|
|
|
|
O(Jump) \
|
|
|
|
O(JumpConditional) \
|
|
|
|
O(JumpNullish) \
|
|
|
|
O(JumpUndefined) \
|
|
|
|
O(LeaveUnwindContext) \
|
|
|
|
O(LeftShift) \
|
2021-07-01 10:24:46 +00:00
|
|
|
O(LessThan) \
|
|
|
|
O(LessThanEquals) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(Load) \
|
|
|
|
O(LoadImmediate) \
|
2021-09-23 22:06:10 +00:00
|
|
|
O(LooselyEquals) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(LooselyInequals) \
|
|
|
|
O(Mod) \
|
|
|
|
O(Mul) \
|
2021-07-01 10:24:46 +00:00
|
|
|
O(NewArray) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(NewBigInt) \
|
|
|
|
O(NewClass) \
|
|
|
|
O(NewFunction) \
|
2021-07-01 10:24:46 +00:00
|
|
|
O(NewObject) \
|
|
|
|
O(NewRegExp) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(NewString) \
|
|
|
|
O(Not) \
|
|
|
|
O(PushDeclarativeEnvironment) \
|
2021-07-01 10:24:46 +00:00
|
|
|
O(PutById) \
|
|
|
|
O(PutByValue) \
|
2021-10-24 12:43:00 +00:00
|
|
|
O(ResolveThisBinding) \
|
2021-07-01 10:24:46 +00:00
|
|
|
O(Return) \
|
|
|
|
O(RightShift) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(SetVariable) \
|
|
|
|
O(Store) \
|
|
|
|
O(StrictlyEquals) \
|
|
|
|
O(StrictlyInequals) \
|
|
|
|
O(Sub) \
|
2021-07-01 10:24:46 +00:00
|
|
|
O(Throw) \
|
2021-10-24 12:38:57 +00:00
|
|
|
O(Typeof) \
|
|
|
|
O(UnaryMinus) \
|
|
|
|
O(UnaryPlus) \
|
|
|
|
O(UnsignedRightShift) \
|
|
|
|
O(Yield)
|
2021-06-07 13:12:43 +00:00
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
|
|
|
class Instruction {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2021-06-13 16:10:20 +00:00
|
|
|
bool is_terminator() const;
|
2021-06-07 13:12:43 +00:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
size_t length() const;
|
2021-06-09 08:02:01 +00:00
|
|
|
String to_string(Bytecode::Executable const&) const;
|
2021-06-07 13:12:43 +00:00
|
|
|
void execute(Bytecode::Interpreter&) const;
|
2021-06-13 16:10:20 +00:00
|
|
|
void replace_references(BasicBlock const&, BasicBlock const&);
|
2021-06-07 13:12:43 +00:00
|
|
|
static void destroy(Instruction&);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
explicit Instruction(Type type)
|
|
|
|
: m_type(type)
|
|
|
|
{
|
|
|
|
}
|
2021-06-03 08:46:30 +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:
|
|
|
|
explicit InstructionStreamIterator(ReadonlyBytes bytes)
|
|
|
|
: m_bytes(bytes)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t offset() const { return m_offset; }
|
|
|
|
bool at_end() const { return m_offset >= m_bytes.size(); }
|
|
|
|
void jump(size_t offset)
|
|
|
|
{
|
|
|
|
VERIFY(offset <= m_bytes.size());
|
|
|
|
m_offset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction const& operator*() const { return dereference(); }
|
|
|
|
|
|
|
|
ALWAYS_INLINE void operator++()
|
|
|
|
{
|
|
|
|
VERIFY(!at_end());
|
|
|
|
m_offset += dereference().length();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_bytes.data() + offset()); }
|
|
|
|
|
|
|
|
ReadonlyBytes m_bytes;
|
|
|
|
size_t m_offset { 0 };
|
|
|
|
};
|
|
|
|
|
2021-06-03 08:46:30 +00:00
|
|
|
}
|