summaryrefslogtreecommitdiffhomepage
path: root/asm/assembler.cpp
blob: 7a9f6cf7cde9584981cb36826a49a197caac1140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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)) {
  std::cout << "Warning: mnemonic |" << mnemonic << "| already registered." << std::endl;
  return false;
 }

 std::cout << "Registering mnemonic |" << mnemonic << "|." << std::endl;

 ops[mnemonic] = 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);
}