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
|
// Helper Functions for assembling
#pragma once
#include "chunk.h"
#include "../minicc.h"
#include <boost/endian/conversion.hpp>
#include <any>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
namespace Asm {
class Args: public std::vector<std::any>
{
public:
Args(){}
Args(const std::vector<std::any>& args): std::vector<std::any>(args){}
class Immediate8
{
public:
Immediate8(uint8_t value): m_value(value) {}
uint8_t value() const {return m_value;}
std::vector<uint8_t> getCode() {return {m_value};};
private:
uint8_t m_value;
};
class Immediate64;
class Immediate32
{
public:
Immediate32(uint32_t value): m_value(value) {}
Immediate32(const Immediate64&); ///< Convert from Immediate64 if data is small enough
uint32_t value() const { return m_value; }
std::vector<uint8_t> getCode() {
std::vector<uint8_t> result(size_t(4));
*(reinterpret_cast<uint32_t*>(result.data())) = boost::endian::native_to_little(m_value);
return result;
};
private:
uint32_t m_value;
};
class Immediate64
{
public:
Immediate64(uint64_t value): m_value(value) {}
uint64_t value() const { return m_value; }
std::vector<uint8_t> getCode() {
std::vector<uint8_t> result(size_t(8));
*(reinterpret_cast<uint64_t*>(result.data())) = boost::endian::native_to_little(m_value);
return result;
};
private:
uint64_t m_value;
};
class Register8
{
public:
Register8(const std::string& name): m_name(name) {}
std::string name() const { return m_name; }
private:
std::string m_name;
};
class Register32
{
public:
Register32(const std::string& name): m_name(name) {}
std::string name() const { return m_name; }
private:
std::string m_name;
};
class Register64
{
public:
Register64(const std::string& name): m_name(name) {}
std::string name() const { return m_name; }
private:
std::string m_name;
};
// 64 bit Ptr to 8 bit Memory
class Mem8Ptr64
{
public:
Mem8Ptr64(const std::string& reg, int32_t offs = 0): m_reg(reg), m_offs(offs) {}
Mem8Ptr64(const std::string& reg, const std::string& reg2, int32_t offs = 0): m_reg(reg), m_reg2(reg2), m_offs(offs) {}
std::string reg() const { return m_reg; }
std::string reg2() const { return m_reg2; }
int32_t offs() const { return m_offs; }
private:
std::string m_reg;
std::string m_reg2;
int32_t m_offs;
};
// 64 bit Ptr to 32 bit Memory
class Mem32Ptr64
{
public:
Mem32Ptr64(const std::string& reg, int32_t offs = 0): m_reg(reg), m_offs(offs) {}
Mem32Ptr64(const std::string& reg, const std::string& reg2, int32_t offs = 0): m_reg(reg), m_reg2(reg2), m_offs(offs) {}
std::string reg() const { return m_reg; }
std::string reg2() const { return m_reg2; }
int32_t offs() const { return m_offs; }
private:
std::string m_reg;
std::string m_reg2;
int32_t m_offs;
};
// 64 bit Ptr to 64 bit Memory
class Mem64Ptr64
{
public:
Mem64Ptr64(const std::string& reg, int32_t offs = 0): m_reg(reg), m_offs(offs) {}
Mem64Ptr64(const std::string& reg, const std::string& reg2, int32_t offs = 0): m_reg(reg), m_reg2(reg2), m_offs(offs) {}
std::string reg() const { return m_reg; }
std::string reg2() const { return m_reg2; }
int32_t offs() const { return m_offs; }
private:
std::string m_reg;
std::string m_reg2;
int32_t m_offs;
};
class Label
{
public:
Label(const std::string& name): m_name(name) {}
std::string name() const { return m_name; }
private:
std::string m_name;
};
}; // class Args
} // namespace Asm
using FactoryFunction = std::function<std::shared_ptr<Op>(const Asm::Args&)>;
// mnemonic: mnemonic including argument types
bool registerOp(const std::string& mnemonic, FactoryFunction f);
// Create Op from a registered mnemonic
// mnemonic: just the mnemonic name
std::shared_ptr<Op> makeOp(const std::string& mnemonic, const Asm::Args& args);
// overload for empty list of arguments
std::shared_ptr<Op> makeOp(const std::string& mnemonic);
std::shared_ptr<Label> makeLabel(const std::string& name);
std::shared_ptr<Data> makeData(const std::vector<uint8_t>& data);
template<typename T>
std::string mangleNameOne(const std::string& s)
{
return s + "_" + typeid(T).name();
}
template<typename T, typename... Targs>
std::string mangleName(const std::string& s)
{
if constexpr (sizeof...(Targs) == 0)
return mangleNameOne<T>(s);
else
return mangleName<Targs...>(s + "_" + typeid(T).name());
}
std::string mangleName(const std::string& s, const Asm::Args& args);
|