From afa3d06ea64b5e1cc7d144d39672726f5c9d3014 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sat, 4 Dec 2021 18:00:30 +0330 Subject: [PATCH] LibWasm: Add a instruction_from_name getter --- Userland/Libraries/LibWasm/Printer/Printer.cpp | 12 ++++++++++++ Userland/Libraries/LibWasm/Printer/Printer.h | 1 + 2 files changed, 13 insertions(+) diff --git a/Userland/Libraries/LibWasm/Printer/Printer.cpp b/Userland/Libraries/LibWasm/Printer/Printer.cpp index e9214ac953..ebbc9ae724 100644 --- a/Userland/Libraries/LibWasm/Printer/Printer.cpp +++ b/Userland/Libraries/LibWasm/Printer/Printer.cpp @@ -13,6 +13,7 @@ namespace Wasm { struct Names { static HashMap instruction_names; + static HashMap instructions_by_name; }; String instruction_name(OpCode const& opcode) @@ -20,6 +21,16 @@ String instruction_name(OpCode const& opcode) return Names::instruction_names.get(opcode).value_or(""); } +Optional instruction_from_name(StringView name) +{ + if (Names::instructions_by_name.is_empty()) { + for (auto& entry : Names::instruction_names) + Names::instructions_by_name.set(entry.value, entry.key); + } + + return Names::instructions_by_name.get(name); +} + void Printer::print_indent() { for (size_t i = 0; i < m_indent; ++i) @@ -866,3 +877,4 @@ HashMap Wasm::Names::instruction_names { { Instructions::structured_else, "synthetic:else" }, { Instructions::structured_end, "synthetic:end" }, }; +HashMap Wasm::Names::instructions_by_name; diff --git a/Userland/Libraries/LibWasm/Printer/Printer.h b/Userland/Libraries/LibWasm/Printer/Printer.h index 06913beb54..bf1f973192 100644 --- a/Userland/Libraries/LibWasm/Printer/Printer.h +++ b/Userland/Libraries/LibWasm/Printer/Printer.h @@ -14,6 +14,7 @@ class Reference; class Value; String instruction_name(OpCode const& opcode); +Optional instruction_from_name(StringView name); struct Printer { explicit Printer(OutputStream& stream, size_t initial_indent = 0)