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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
#include "assembler.h"
#include "byteorder.h"
using namespace std::string_literals;
Asm::Args::Immediate8::Immediate8(uint8_t value): m_value(value)
{}
uint8_t Asm::Args::Immediate8::value() const
{
return m_value;
}
std::vector<uint8_t> Asm::Args::Immediate8::getCode() const
{
return {m_value};
}
Asm::Args::Immediate16::Immediate16(uint16_t value): m_value(value)
{}
uint16_t Asm::Args::Immediate16::value() const
{
return m_value;
}
std::vector<uint8_t> Asm::Args::Immediate16::getCode() const
{
return endian::to_little(m_value);
}
Asm::Args::Immediate32::Immediate32(uint32_t value): m_value(value)
{}
Asm::Args::Immediate32::Immediate32(const Asm::Args::Immediate64& imm64)
{
if (imm64.value() < 0x100000000)
m_value = static_cast<uint32_t>(imm64.value());
else
throw std::runtime_error("Immediate32: Constructed from too big Immediate64");
}
uint32_t Asm::Args::Immediate32::value() const
{
return m_value;
}
std::vector<uint8_t> Asm::Args::Immediate32::getCode() const
{
return endian::to_little(m_value);
}
Asm::Args::Immediate64::Immediate64(uint64_t value): m_value(value)
{}
uint64_t Asm::Args::Immediate64::value() const
{
return m_value;
}
std::vector<uint8_t> Asm::Args::Immediate64::getCode() const
{
return endian::to_little(m_value);
}
Asm::Args::Register8::Register8(const std::string& name): m_name(name)
{}
std::string Asm::Args::Register8::name() const
{
return m_name;
}
Asm::Args::Register16::Register16(const std::string& name): m_name(name)
{}
std::string Asm::Args::Register16::name() const
{
return m_name;
}
Asm::Args::Register32::Register32(const std::string& name): m_name(name)
{}
std::string Asm::Args::Register32::name() const
{
return m_name;
}
Asm::Args::Register64::Register64(const std::string& name): m_name(name)
{}
std::string Asm::Args::Register64::name() const
{
return m_name;
}
Asm::Args::Mem8Ptr64::Mem8Ptr64(const std::string& reg, int32_t offs): m_reg(reg), m_offs(offs)
{}
Asm::Args::Mem8Ptr64::Mem8Ptr64(const std::string& reg, const std::string& reg2, int32_t offs): m_reg(reg), m_reg2(reg2), m_offs(offs)
{}
std::string Asm::Args::Mem8Ptr64::reg() const
{
return "["s + m_reg + "]"s;
}
std::string Asm::Args::Mem8Ptr64::reg2() const
{
return "["s + m_reg2 + "]"s;
}
int32_t Asm::Args::Mem8Ptr64::offs() const
{
return m_offs;
}
Asm::Args::Mem16Ptr64::Mem16Ptr64(const std::string& reg, int32_t offs): m_reg(reg), m_offs(offs)
{}
Asm::Args::Mem16Ptr64::Mem16Ptr64(const std::string& reg, const std::string& reg2, int32_t offs): m_reg(reg), m_reg2(reg2), m_offs(offs)
{}
std::string Asm::Args::Mem16Ptr64::reg() const
{
return "["s + m_reg + "]"s;
}
std::string Asm::Args::Mem16Ptr64::reg2() const
{
return "["s + m_reg2 + "]"s;
}
int32_t Asm::Args::Mem16Ptr64::offs() const
{
return m_offs;
}
Asm::Args::Mem32Ptr64::Mem32Ptr64(const std::string& reg, int32_t offs): m_reg(reg), m_offs(offs)
{}
Asm::Args::Mem32Ptr64::Mem32Ptr64(const std::string& reg, const std::string& reg2, int32_t offs): m_reg(reg), m_reg2(reg2), m_offs(offs)
{}
std::string Asm::Args::Mem32Ptr64::reg() const
{
return "["s + m_reg + "]"s;
}
std::string Asm::Args::Mem32Ptr64::reg2() const
{
return "["s + m_reg2 + "]"s;
}
int32_t Asm::Args::Mem32Ptr64::offs() const
{
return m_offs;
}
Asm::Args::Mem64Ptr64::Mem64Ptr64(const std::string& reg, int32_t offs): m_reg(reg), m_offs(offs)
{}
Asm::Args::Mem64Ptr64::Mem64Ptr64(const std::string& reg, const std::string& reg2, int32_t offs): m_reg(reg), m_reg2(reg2), m_offs(offs)
{}
std::string Asm::Args::Mem64Ptr64::reg() const
{
return "["s + m_reg + "]"s;
}
std::string Asm::Args::Mem64Ptr64::reg2() const
{
return "["s + m_reg2 + "]"s;
}
int32_t Asm::Args::Mem64Ptr64::offs() const
{
return m_offs;
}
namespace {
std::unordered_map<std::string, FactoryFunction> ops;
}
bool registerOp(const std::string& mnemonic, FactoryFunction f)
{
if (ops.contains(mnemonic)) {
std::cerr << "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, const Asm::Args& 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, const Asm::Args& 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)
{
Asm::Args 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);
}
|