2021-06-04 11:54:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-11 08:32:42 +00:00
|
|
|
#include <AK/StackInfo.h>
|
2021-06-04 11:54:20 +00:00
|
|
|
#include <LibWasm/AbstractMachine/Configuration.h>
|
|
|
|
#include <LibWasm/AbstractMachine/Interpreter.h>
|
|
|
|
|
|
|
|
namespace Wasm {
|
|
|
|
|
|
|
|
struct BytecodeInterpreter : public Interpreter {
|
|
|
|
virtual void interpret(Configuration&) override;
|
|
|
|
virtual ~BytecodeInterpreter() override = default;
|
2021-07-01 12:33:17 +00:00
|
|
|
virtual bool did_trap() const override { return m_trap.has_value(); }
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString trap_reason() const override { return m_trap.value().reason; }
|
2021-07-01 12:33:17 +00:00
|
|
|
virtual void clear_trap() override { m_trap.clear(); }
|
2021-06-04 11:54:20 +00: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;
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void interpret(Configuration&, InstructionPointer&, Instruction const&);
|
|
|
|
void branch_to_label(Configuration&, LabelIndex);
|
|
|
|
template<typename ReadT, typename PushT>
|
|
|
|
void load_and_push(Configuration&, Instruction const&);
|
2021-08-11 17:46:05 +00:00
|
|
|
template<typename PopT, typename StoreT>
|
|
|
|
void pop_and_store(Configuration&, Instruction const&);
|
2021-12-04 14:26:58 +00:00
|
|
|
void store_to_memory(Configuration&, Instruction const&, ReadonlyBytes data, i32 base);
|
2021-06-04 11:54:20 +00:00
|
|
|
void call_address(Configuration&, FunctionAddress);
|
|
|
|
|
2021-08-08 22:25:01 +00:00
|
|
|
template<typename PopType, typename PushType, typename Operator>
|
|
|
|
void binary_numeric_operation(Configuration&);
|
|
|
|
|
|
|
|
template<typename PopType, typename PushType, typename Operator>
|
|
|
|
void unary_operation(Configuration&);
|
|
|
|
|
2021-06-04 11:54:20 +00:00
|
|
|
template<typename V, typename T>
|
|
|
|
MakeUnsigned<T> checked_unsigned_truncate(V);
|
|
|
|
|
|
|
|
template<typename V, typename T>
|
|
|
|
MakeSigned<T> checked_signed_truncate(V);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T read_value(ReadonlyBytes data);
|
|
|
|
|
|
|
|
Vector<Value> pop_values(Configuration& configuration, size_t count);
|
2021-07-12 20:08:21 +00:00
|
|
|
ALWAYS_INLINE bool trap_if_not(bool value, StringView reason)
|
2021-06-04 11:54:20 +00:00
|
|
|
{
|
|
|
|
if (!value)
|
2021-07-06 09:58:55 +00:00
|
|
|
m_trap = Trap { reason };
|
2021-07-01 12:33:17 +00:00
|
|
|
return m_trap.has_value();
|
2021-06-04 11:54:20 +00:00
|
|
|
}
|
2021-07-01 12:33:17 +00:00
|
|
|
|
|
|
|
Optional<Trap> m_trap;
|
2021-07-11 08:32:42 +00:00
|
|
|
StackInfo m_stack_info;
|
2021-06-04 11:54:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DebuggerBytecodeInterpreter : public BytecodeInterpreter {
|
|
|
|
virtual ~DebuggerBytecodeInterpreter() override = default;
|
|
|
|
|
|
|
|
Function<bool(Configuration&, InstructionPointer&, Instruction const&)> pre_interpret_hook;
|
|
|
|
Function<bool(Configuration&, InstructionPointer&, Instruction const&, Interpreter const&)> post_interpret_hook;
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void interpret(Configuration&, InstructionPointer&, Instruction const&) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|