2021-05-01 01:08:51 +04:30
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2025-06-06 14:13:28 +02:00
|
|
|
#include <AK/DoublyLinkedList.h>
|
2021-05-01 01:08:51 +04:30
|
|
|
#include <LibWasm/AbstractMachine/AbstractMachine.h>
|
2025-08-02 20:30:44 +02:00
|
|
|
#include <LibWasm/Types.h>
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
namespace Wasm {
|
|
|
|
|
|
|
|
class Configuration {
|
|
|
|
public:
|
|
|
|
explicit Configuration(Store& store)
|
|
|
|
: m_store(store)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-09-22 11:38:44 +02:00
|
|
|
void set_frame(Frame frame, bool is_tailcall = false)
|
2021-08-09 02:55:01 +04:30
|
|
|
{
|
2025-09-19 23:06:08 +02:00
|
|
|
auto continuation = frame.expression().instructions().size() - 1;
|
|
|
|
if (auto size = frame.expression().compiled_instructions.dispatches.size(); size > 0)
|
|
|
|
continuation = size - 1;
|
|
|
|
Label label(frame.arity(), continuation, m_value_stack.size());
|
2024-08-02 20:51:40 -07:00
|
|
|
frame.label_index() = m_label_stack.size();
|
2025-06-06 14:12:28 +02:00
|
|
|
if (auto hint = frame.expression().stack_usage_hint(); hint.has_value())
|
2025-08-02 20:30:44 +02:00
|
|
|
m_value_stack.ensure_capacity(*hint + m_value_stack.size());
|
2025-09-22 11:38:44 +02:00
|
|
|
if (!is_tailcall) {
|
|
|
|
if (auto hint = frame.expression().frame_usage_hint(); hint.has_value())
|
|
|
|
m_label_stack.ensure_capacity(*hint + m_label_stack.size());
|
|
|
|
}
|
2024-08-02 20:51:40 -07:00
|
|
|
m_frame_stack.append(move(frame));
|
|
|
|
m_label_stack.append(label);
|
2025-08-02 20:30:44 +02:00
|
|
|
m_locals_base = m_frame_stack.unchecked_last().locals().data();
|
2021-08-09 02:55:01 +04:30
|
|
|
}
|
2025-08-02 20:30:44 +02:00
|
|
|
ALWAYS_INLINE auto& frame() const { return m_frame_stack.unchecked_last(); }
|
|
|
|
ALWAYS_INLINE auto& frame() { return m_frame_stack.unchecked_last(); }
|
2021-06-09 20:31:07 +04:30
|
|
|
ALWAYS_INLINE auto& ip() const { return m_ip; }
|
|
|
|
ALWAYS_INLINE auto& ip() { return m_ip; }
|
|
|
|
ALWAYS_INLINE auto& depth() const { return m_depth; }
|
|
|
|
ALWAYS_INLINE auto& depth() { return m_depth; }
|
2024-08-02 20:51:40 -07:00
|
|
|
ALWAYS_INLINE auto& value_stack() const { return m_value_stack; }
|
|
|
|
ALWAYS_INLINE auto& value_stack() { return m_value_stack; }
|
|
|
|
ALWAYS_INLINE auto& label_stack() const { return m_label_stack; }
|
|
|
|
ALWAYS_INLINE auto& label_stack() { return m_label_stack; }
|
2021-06-09 20:31:07 +04:30
|
|
|
ALWAYS_INLINE auto& store() const { return m_store; }
|
|
|
|
ALWAYS_INLINE auto& store() { return m_store; }
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2025-08-02 20:30:44 +02:00
|
|
|
ALWAYS_INLINE Value const* raw_locals() const { return m_locals_base; }
|
|
|
|
ALWAYS_INLINE Value const& local(LocalIndex index) const { return m_locals_base[index.value()]; }
|
|
|
|
ALWAYS_INLINE Value& local(LocalIndex index) { return m_locals_base[index.value()]; }
|
|
|
|
|
2021-05-24 01:28:02 +04:30
|
|
|
struct CallFrameHandle {
|
|
|
|
explicit CallFrameHandle(Configuration& configuration)
|
2025-08-22 17:11:25 +02:00
|
|
|
: configuration(configuration)
|
2021-05-24 01:28:02 +04:30
|
|
|
{
|
|
|
|
configuration.depth()++;
|
|
|
|
}
|
|
|
|
|
|
|
|
~CallFrameHandle()
|
|
|
|
{
|
|
|
|
configuration.unwind({}, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Configuration& configuration;
|
|
|
|
};
|
|
|
|
|
2025-09-22 11:38:44 +02:00
|
|
|
void unwind(Badge<CallFrameHandle>, CallFrameHandle const&) { unwind_impl(); }
|
|
|
|
ErrorOr<Optional<HostFunction&>, Trap> prepare_call(FunctionAddress, Vector<Value>& arguments, bool is_tailcall = false);
|
2021-05-24 02:04:58 +04:30
|
|
|
Result call(Interpreter&, FunctionAddress, Vector<Value> arguments);
|
|
|
|
Result execute(Interpreter&);
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2021-07-15 00:00:45 +04:30
|
|
|
void enable_instruction_count_limit() { m_should_limit_instruction_count = true; }
|
|
|
|
bool should_limit_instruction_count() const { return m_should_limit_instruction_count; }
|
|
|
|
|
2021-05-01 03:19:01 +04:30
|
|
|
void dump_stack();
|
|
|
|
|
2025-08-22 17:11:25 +02:00
|
|
|
ALWAYS_INLINE FLATTEN void push_to_destination(Value value, Dispatch::RegisterOrStack destination)
|
2025-08-02 20:30:44 +02:00
|
|
|
{
|
|
|
|
if (destination == Dispatch::RegisterOrStack::Stack) {
|
|
|
|
value_stack().unchecked_append(value);
|
|
|
|
return;
|
|
|
|
}
|
2025-08-20 13:55:42 +02:00
|
|
|
regs.data()[to_underlying(destination)] = value;
|
2025-08-02 20:30:44 +02:00
|
|
|
}
|
|
|
|
|
2025-08-22 17:11:25 +02:00
|
|
|
ALWAYS_INLINE FLATTEN Value& source_value(u8 index, Dispatch::RegisterOrStack const* sources)
|
2025-08-02 20:30:44 +02:00
|
|
|
{
|
|
|
|
// Note: The last source in a dispatch *must* be equal to the destination for this to be valid.
|
|
|
|
auto const source = sources[index];
|
|
|
|
if (source == Dispatch::RegisterOrStack::Stack)
|
|
|
|
return value_stack().unsafe_last();
|
2025-08-20 13:55:42 +02:00
|
|
|
return regs.data()[to_underlying(source)];
|
2025-08-02 20:30:44 +02:00
|
|
|
}
|
|
|
|
|
2025-08-22 17:11:25 +02:00
|
|
|
ALWAYS_INLINE FLATTEN Value take_source(u8 index, Dispatch::RegisterOrStack const* sources)
|
2025-08-02 20:30:44 +02:00
|
|
|
{
|
|
|
|
auto const source = sources[index];
|
|
|
|
if (source == Dispatch::RegisterOrStack::Stack)
|
|
|
|
return value_stack().unsafe_take_last();
|
2025-08-20 13:55:42 +02:00
|
|
|
return regs.data()[to_underlying(source)];
|
2025-08-02 20:30:44 +02:00
|
|
|
}
|
|
|
|
|
2025-08-09 06:02:38 +02:00
|
|
|
Array<Value, Dispatch::RegisterOrStack::CountRegisters> regs = {
|
2025-08-02 20:30:44 +02:00
|
|
|
Value(0),
|
|
|
|
Value(0),
|
|
|
|
Value(0),
|
|
|
|
Value(0),
|
|
|
|
Value(0),
|
|
|
|
Value(0),
|
|
|
|
Value(0),
|
|
|
|
Value(0),
|
|
|
|
};
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
private:
|
2025-09-22 11:38:44 +02:00
|
|
|
void unwind_impl();
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
Store& m_store;
|
2025-08-02 20:30:44 +02:00
|
|
|
Vector<Value, 64, FastLastAccess::Yes> m_value_stack;
|
2025-08-20 13:54:14 +02:00
|
|
|
Vector<Label, 64> m_label_stack;
|
2025-09-10 17:05:09 +02:00
|
|
|
DoublyLinkedList<Frame, 512> m_frame_stack;
|
2021-05-01 01:08:51 +04:30
|
|
|
size_t m_depth { 0 };
|
2025-08-02 20:30:44 +02:00
|
|
|
u64 m_ip { 0 };
|
2021-07-15 00:00:45 +04:30
|
|
|
bool m_should_limit_instruction_count { false };
|
2025-08-02 20:30:44 +02:00
|
|
|
Value* m_locals_base { nullptr };
|
2021-05-01 01:08:51 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
}
|