diff --git a/AK/Debug.h.in b/AK/Debug.h.in index cdbbb622db..a037bbdf9b 100644 --- a/AK/Debug.h.in +++ b/AK/Debug.h.in @@ -422,3 +422,6 @@ #cmakedefine01 WSSCREEN_DEBUG #endif +#ifndef WASM_TRACE_DEBUG +#cmakedefine01 WASM_TRACE_DEBUG +#endif diff --git a/Meta/CMake/all_the_debug_macros.cmake b/Meta/CMake/all_the_debug_macros.cmake index cb8297a193..e89e4d1962 100644 --- a/Meta/CMake/all_the_debug_macros.cmake +++ b/Meta/CMake/all_the_debug_macros.cmake @@ -179,6 +179,7 @@ set(LINE_EDITOR_DEBUG ON) set(LANGUAGE_SERVER_DEBUG ON) set(GL_DEBUG ON) set(WASM_BINPARSER_DEBUG ON) +set(WASM_TRACE_DEBUG ON) set(PDF_DEBUG ON) set(SOLITAIRE_DEBUG ON) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h index f97b747a1f..badd342289 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h @@ -49,7 +49,7 @@ public: } template - requires(sizeof(T) <= sizeof(u64)) explicit Value(ValueType type, T raw_value) + requires(sizeof(T) == sizeof(u64)) explicit Value(ValueType type, T raw_value) : m_value(0) , m_type(type) { @@ -89,6 +89,33 @@ public: { } + Value& operator=(Value&& value) + { + m_value = move(value.m_value); + m_type = move(value.m_type); + return *this; + } + + template + Optional to() + { + Optional result; + m_value.visit( + [&](auto value) { + if constexpr (!IsSame && !IsSame) + result = value; + }, + [&](const FunctionAddress& address) { + if constexpr (IsSame) + result = address; + }, + [&](const ExternAddress& address) { + if constexpr (IsSame) + result = address; + }); + return result; + } + auto& type() const { return m_type; } auto& value() const { return m_value; } @@ -317,14 +344,17 @@ private: class Label { public: - explicit Label(InstructionPointer continuation) - : m_continuation(continuation) + explicit Label(size_t arity, InstructionPointer continuation) + : m_arity(arity) + , m_continuation(continuation) { } auto continuation() const { return m_continuation; } + auto arity() const { return m_arity; } private: + size_t m_arity { 0 }; InstructionPointer m_continuation; }; @@ -342,6 +372,7 @@ public: auto& module() const { return m_module; } auto& locals() const { return m_locals; } + auto& locals() { return m_locals; } auto& expression() const { return m_expression; } auto arity() const { return m_arity; } diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Configuration.cpp b/Userland/Libraries/LibWasm/AbstractMachine/Configuration.cpp index 6666f8598f..2da24f0022 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Configuration.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/Configuration.cpp @@ -74,4 +74,33 @@ Result Configuration::execute() return Result { move(results_moved) }; } +void Configuration::dump_stack() +{ + for (const auto& entry : stack().entries()) { + entry.visit( + [](const NonnullOwnPtr& v) { + v->value().visit([](const T& v) { + if constexpr (IsIntegral || IsFloatingPoint) + dbgln(" {}", v); + else + dbgln(" *{}", v.value()); + }); + }, + [](const NonnullOwnPtr& f) { + dbgln(" frame({})", f->arity()); + for (auto& local : f->locals()) { + local.value().visit([](const T& v) { + if constexpr (IsIntegral || IsFloatingPoint) + dbgln(" {}", v); + else + dbgln(" *{}", v.value()); + }); + } + }, + [](const NonnullOwnPtr