1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:57:35 +00:00

LibWasm: Add a instruction_from_name getter

This commit is contained in:
Ali Mohammad Pur 2021-12-04 18:00:30 +03:30 committed by Ali Mohammad Pur
parent d471405caf
commit afa3d06ea6
2 changed files with 13 additions and 0 deletions

View file

@ -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;

View file

@ -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)