blob: d6ab23050d181f94ccaae81d317b92c353e9980d (
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
52
53
54
55
56
57
58
59
60
61
|
#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);
}
std::shared_ptr<Label> makeLabel(const std::string& name)
{
return std::make_shared<Label>(name);
}
std::shared_ptr<Data> makeData(const std::vector<uint8_t>& data)
{
return std::make_shared<Data>(data);
}
|