diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-12-04 18:00:30 +0330 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-12-21 05:03:44 +0330 |
commit | afa3d06ea64b5e1cc7d144d39672726f5c9d3014 (patch) | |
tree | 0ad6fde9daf585e31b22d0e9cb5a03bcbc8a1687 /Userland | |
parent | d471405caf3cce74d9ca5b30e425e996cb0cd8eb (diff) | |
download | serenity-afa3d06ea64b5e1cc7d144d39672726f5c9d3014.zip |
LibWasm: Add a instruction_from_name getter
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWasm/Printer/Printer.cpp | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibWasm/Printer/Printer.h | 1 |
2 files changed, 13 insertions, 0 deletions
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<OpCode, String> instruction_names; + static HashMap<String, OpCode> 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("<unknown>"); } +Optional<OpCode> 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::OpCode, String> Wasm::Names::instruction_names { { Instructions::structured_else, "synthetic:else" }, { Instructions::structured_end, "synthetic:end" }, }; +HashMap<String, Wasm::OpCode> 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<OpCode> instruction_from_name(StringView name); struct Printer { explicit Printer(OutputStream& stream, size_t initial_indent = 0) |