diff options
Diffstat (limited to 'asm/assembler.cpp')
-rw-r--r-- | asm/assembler.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/asm/assembler.cpp b/asm/assembler.cpp index 275bd4a..7a9f6cf 100644 --- a/asm/assembler.cpp +++ b/asm/assembler.cpp @@ -1,7 +1,13 @@ #include "assembler.h" +using namespace std::string_literals; + +namespace { + std::unordered_map<std::string, FactoryFunction> ops; +} + bool registerOp(const std::string& mnemonic, FactoryFunction f) { if (ops.contains(mnemonic)) { @@ -15,3 +21,31 @@ bool registerOp(const std::string& mnemonic, FactoryFunction f) return true; } + +std::string mangleName(const std::string& s, AsmArgs& args) +{ + std::string result {s}; + + for (const auto& arg: args) { + result += "_"s + arg.type().name(); + } + + return result; +} + +std::shared_ptr<Op> makeOp(const std::string& mnemonic, AsmArgs& args) +{ + std::string mangled{mangleName(mnemonic, args)}; + + const auto& i{ops.find(mangled)}; + if (i == ops.end()) + throw std::runtime_error("Instruction "s + mangled + " not implemented."); + + return i->second(args); +} + +std::shared_ptr<Op> makeOp(const std::string& mnemonic) +{ + AsmArgs dummy; // empty list of arguments + return makeOp(mnemonic, dummy); +} |