2021-06-04 21:54:20 +10:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-11 13:02:42 +04:30
|
|
|
#include <AK/StackInfo.h>
|
2021-06-04 21:54:20 +10:00
|
|
|
#include <LibWasm/AbstractMachine/Configuration.h>
|
|
|
|
#include <LibWasm/AbstractMachine/Interpreter.h>
|
|
|
|
|
|
|
|
namespace Wasm {
|
|
|
|
|
2025-08-02 20:23:26 +02:00
|
|
|
struct BytecodeInterpreter final : public Interpreter {
|
2023-05-28 12:13:43 +02:00
|
|
|
explicit BytecodeInterpreter(StackInfo const& stack_info)
|
|
|
|
: m_stack_info(stack_info)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-08-20 10:27:41 +02:00
|
|
|
virtual void interpret(Configuration&) final;
|
2024-08-02 20:58:15 -07:00
|
|
|
|
2021-06-04 21:54:20 +10:00
|
|
|
virtual ~BytecodeInterpreter() override = default;
|
2024-08-20 10:27:41 +02:00
|
|
|
virtual bool did_trap() const final { return !m_trap.has<Empty>(); }
|
2025-04-22 09:48:26 +02:00
|
|
|
virtual Trap trap() const final
|
2023-02-25 11:15:11 +03:30
|
|
|
{
|
2025-04-22 09:48:26 +02:00
|
|
|
return m_trap.get<Trap>();
|
2023-02-25 11:15:11 +03:30
|
|
|
}
|
2024-08-20 10:27:41 +02:00
|
|
|
virtual void clear_trap() final { m_trap = Empty {}; }
|
2025-04-22 09:48:26 +02:00
|
|
|
virtual void visit_external_resources(HostVisitOps const& host) override
|
|
|
|
{
|
|
|
|
if (auto ptr = m_trap.get_pointer<Trap>())
|
|
|
|
if (auto data = ptr->data.get_pointer<ExternallyManagedTrap>())
|
|
|
|
host.visit_trap(*data);
|
|
|
|
}
|
2021-06-04 21:54:20 +10:00
|
|
|
|
|
|
|
struct CallFrameHandle {
|
|
|
|
explicit CallFrameHandle(BytecodeInterpreter& interpreter, Configuration& configuration)
|
|
|
|
: m_configuration_handle(configuration)
|
|
|
|
, m_interpreter(interpreter)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~CallFrameHandle() = default;
|
|
|
|
|
|
|
|
Configuration::CallFrameHandle m_configuration_handle;
|
|
|
|
BytecodeInterpreter& m_interpreter;
|
|
|
|
};
|
|
|
|
|
2025-05-21 16:16:28 +02:00
|
|
|
enum class CallAddressSource {
|
|
|
|
DirectCall,
|
|
|
|
IndirectCall,
|
|
|
|
};
|
|
|
|
|
2025-08-02 20:30:44 +02:00
|
|
|
template<bool HasCompiledList, bool HasDynamicInsnLimit>
|
|
|
|
void interpret_impl(Configuration&, Expression const&);
|
|
|
|
|
2021-06-04 21:54:20 +10:00
|
|
|
protected:
|
|
|
|
void branch_to_label(Configuration&, LabelIndex);
|
|
|
|
template<typename ReadT, typename PushT>
|
2025-08-02 20:30:44 +02:00
|
|
|
bool load_and_push(Configuration&, Instruction const&);
|
2021-08-11 22:16:05 +04:30
|
|
|
template<typename PopT, typename StoreT>
|
2025-08-02 20:30:44 +02:00
|
|
|
bool pop_and_store(Configuration&, Instruction const&);
|
|
|
|
template<typename StoreT>
|
|
|
|
bool store_value(Configuration&, Instruction const&, StoreT, size_t address_source);
|
2024-07-14 21:09:09 -07:00
|
|
|
template<size_t N>
|
2025-08-02 20:30:44 +02:00
|
|
|
bool pop_and_store_lane_n(Configuration&, Instruction const&);
|
2023-06-12 13:38:22 +03:30
|
|
|
template<size_t M, size_t N, template<typename> typename SetSign>
|
2025-08-02 20:30:44 +02:00
|
|
|
bool load_and_push_mxn(Configuration&, Instruction const&);
|
2024-07-14 21:09:09 -07:00
|
|
|
template<size_t N>
|
2025-08-02 20:30:44 +02:00
|
|
|
bool load_and_push_lane_n(Configuration&, Instruction const&);
|
2024-07-14 21:09:09 -07:00
|
|
|
template<size_t N>
|
2025-08-02 20:30:44 +02:00
|
|
|
bool load_and_push_zero_n(Configuration&, Instruction const&);
|
2023-06-12 13:38:22 +03:30
|
|
|
template<size_t M>
|
2025-08-02 20:30:44 +02:00
|
|
|
bool load_and_push_m_splat(Configuration&, Instruction const&);
|
2023-06-12 13:38:22 +03:30
|
|
|
template<size_t M, template<size_t> typename NativeType>
|
|
|
|
void set_top_m_splat(Configuration&, NativeType<M>);
|
|
|
|
template<size_t M, template<size_t> typename NativeType>
|
|
|
|
void pop_and_push_m_splat(Configuration&, Instruction const&);
|
|
|
|
template<typename M, template<typename> typename SetSign, typename VectorType = Native128ByteVectorOf<M, SetSign>>
|
2025-08-02 20:30:44 +02:00
|
|
|
VectorType pop_vector(Configuration&, size_t source);
|
|
|
|
bool store_to_memory(Configuration&, Instruction::MemoryArgument const&, ReadonlyBytes data, u32 base);
|
|
|
|
bool call_address(Configuration&, FunctionAddress, CallAddressSource = CallAddressSource::DirectCall);
|
2021-06-04 21:54:20 +10:00
|
|
|
|
2024-02-07 17:31:38 +03:30
|
|
|
template<typename PopTypeLHS, typename PushType, typename Operator, typename PopTypeRHS = PopTypeLHS, typename... Args>
|
2025-08-02 20:30:44 +02:00
|
|
|
bool binary_numeric_operation(Configuration&, Args&&...);
|
2021-08-09 02:55:01 +04:30
|
|
|
|
2024-02-07 17:31:38 +03:30
|
|
|
template<typename PopType, typename PushType, typename Operator, typename... Args>
|
2025-08-02 20:30:44 +02:00
|
|
|
bool unary_operation(Configuration&, Args&&...);
|
2021-08-09 02:55:01 +04:30
|
|
|
|
2021-06-04 21:54:20 +10:00
|
|
|
template<typename T>
|
|
|
|
T read_value(ReadonlyBytes data);
|
|
|
|
|
2021-07-13 00:38:21 +04:30
|
|
|
ALWAYS_INLINE bool trap_if_not(bool value, StringView reason)
|
2021-06-04 21:54:20 +10:00
|
|
|
{
|
2025-08-02 20:30:44 +02:00
|
|
|
if (!value) [[unlikely]] {
|
2025-04-22 09:48:26 +02:00
|
|
|
m_trap = Trap { ByteString(reason) };
|
2025-08-02 20:30:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-06-04 21:54:20 +10:00
|
|
|
}
|
2021-07-01 17:03:17 +04:30
|
|
|
|
2025-05-21 16:15:34 +02:00
|
|
|
template<typename... Rest>
|
|
|
|
ALWAYS_INLINE bool trap_if_not(bool value, StringView reason, CheckedFormatString<StringView, Rest...> format, Rest const&... args)
|
|
|
|
{
|
2025-08-02 20:30:44 +02:00
|
|
|
if (!value) [[unlikely]] {
|
2025-05-21 16:15:34 +02:00
|
|
|
m_trap = Trap { ByteString::formatted(move(format), reason, args...) };
|
2025-08-02 20:30:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2025-05-21 16:15:34 +02:00
|
|
|
}
|
|
|
|
|
2025-04-22 09:48:26 +02:00
|
|
|
Variant<Trap, Empty> m_trap;
|
2023-05-28 12:13:43 +02:00
|
|
|
StackInfo const& m_stack_info;
|
2021-06-04 21:54:20 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|