-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssembler.cpp
More file actions
342 lines (303 loc) · 9.98 KB
/
Assembler.cpp
File metadata and controls
342 lines (303 loc) · 9.98 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "Assembler.hpp"
#include "LabelTable.hpp"
#include "Segment.hpp"
#include "Argument.hpp"
#include "errorChecking.hpp"
#include "InstructionsHost.hpp"
#include "DelayedAddresses.hpp"
#include "Error.hpp"
#include "Segments.hpp"
#include <map>
#include <string>
#include <functional>
#include <cassert>
#include <iostream>
#include <cstring>
namespace {
std::string replaced(std::string s, const std::string& from, const std::string& to) {
size_t startIndex = s.find(from);
while (startIndex != std::string::npos) {
s = s.replace(startIndex, from.length(), to);
startIndex = s.find(from);
}
return s;
}
std::string replacedEscapes(std::string s) {
s = replaced(s, "\\n", "\n");
s = replaced(s, "\\t", "\t");
s = replaced(s, "\\0", std::string("\0", 1)); // "\0" is difficult to pass!
// s = replaced(s, "\\0", std::string(1, '!')); // "\0" is difficult to pass!
return s;
}
}
class RelocationsTable {
public:
void add(int offset);
void print(std::ostream&) const;
private:
std::vector<int> _offsets;
};
void RelocationsTable::add(int offset) {
_offsets.push_back(offset);
}
void RelocationsTable::print(std::ostream& stream) const {
for (int offset : _offsets) {
stream << std::hex << offset << std::endl;
}
}
struct Assembler::Impl : public InstructionsHost {
typedef std::function<void (InstructionsHost&)> NullaryInstruction;
typedef std::function<void (InstructionsHost&, const Argument&)> UnaryInstruction;
typedef std::function<void (InstructionsHost&, const Argument&, const Argument&)> BinaryInstruction;
NullaryInstruction findNullaryInstruction(const char* mnemonic) {
auto f = nullaryInstructions[std::string(mnemonic)];
if (!f) {
throw Error(std::string("No nullary instruction ") + mnemonic);
}
return f;
}
UnaryInstruction findUnaryInstruction(const char* mnemonic) {
auto f = unaryInstructions[std::string(mnemonic)];
if (!f) {
throw Error(std::string("No unary instruction ") + mnemonic);
}
return f;
}
BinaryInstruction findBinaryInstruction(const char* mnemonic) {
auto f = binaryInstructions[std::string(mnemonic)];
if (!f) {
throw Error(std::string("No binary instruction ") + mnemonic);
}
return f;
}
Argument resolveArgument(const RawArgument& arg) {
RawArgument argument = arg;
if (argument.type == IDENTIFIER_ARGUMENT) {
auto t = eqTable.find(std::string(argument.identifier));
if (t != eqTable.end()) {
return t->second;
}
}
else if (argument.type == DEREFERENCED_IDENTIFIER_ARGUMENT) {
auto t = eqTable.find(std::string(argument.identifier));
if (t != eqTable.end()) {
return t->second.asAddressValue();
}
}
else if (argument.type == DEREFERENCED_INDEXED_IDENTIFIER_ARGUMENT) {
if (argument.indexIdentifier) {
auto t = eqTable.find(std::string(argument.indexIdentifier));
if (t != eqTable.end()) {
argument.indexValue = t->second.byteValue();
}
else {
std::cerr << "Did not find " << argument.indexIdentifier << std::endl;
assert(0);
}
}
if (argument.indexOperation == '-')
argument.indexValue = -argument.indexValue;
}
return Argument::createWithRawArgument(argument);
}
void addEq(const char* identifier, const Argument& argument) {
if (eqTable.count(identifier)) {
warning(std::string("Redefining ") + identifier);
}
eqTable[identifier] = argument;
}
void addString(const Argument& argument) {
for (char c : replacedEscapes(argument.string())) {
addCode(c);
}
}
void addCode(Byte byte) {
currentSegment().add(byte);
}
void add16BitValue(int value) {
assert(value >= 0);
assert(value <= 0xffff);
Byte low = (Byte)value & 0xff;
Byte high = (Byte)((value >> 8) & 0xff);
addCode(low);
addCode(high);
}
void add16BitValue(const Argument& arg) {
int address;
if (arg.hasValue()) {
address = arg.value();
}
else {
if (labelTable.contains(arg.identifier())) {
address = labelTable.addressForLabel(arg.identifier());
}
else {
address = 0; // Write zero for now
delayedAddresses.add16Bit(arg.identifier(), currentSegment().currentOffset());
}
}
relocationsTable.add(currentSegment().currentOffset());
add16BitValue(address);
}
void add16BitAddress(const Argument& arg) {
add16BitValue(arg);
}
void add8BitRelativeAddress(const Argument& arg) {
const int currentPC = currentSegment().currentOffset() - 1;
int address;
int delta;
if (arg.hasValue()) {
address = arg.value();
delta = address - currentPC - 2;
}
else {
if (labelTable.contains(arg.identifier())) {
const int address = labelTable.addressForLabel(arg.identifier());
delta = address - currentPC - 2;
}
else {
delta = 0; // Write zero for now
delayedAddresses.add8BitRelative(arg.identifier(), currentSegment().currentOffset());
}
}
if (delta < -128)
error("Delta <-128");
if (delta > 127)
error("Delta >127");
addCode(delta);
}
Segment& currentSegment() {
return segments.index(segments.numberOfSegments()-1);
}
RelocationsTable relocationsTable;
LabelTable labelTable;
Segments segments;
DelayedAddresses delayedAddresses;
std::map<std::string, NullaryInstruction> nullaryInstructions;
std::map<std::string, UnaryInstruction> unaryInstructions;
std::map<std::string, BinaryInstruction> binaryInstructions;
std::map<std::string, Argument> eqTable;
};
#define ASM_NULLARY_INSTRUCTION(X) extern void X ## Instruction(InstructionsHost&); \
_pimpl->nullaryInstructions[std::string(#X)] = X ## Instruction;
#define ASM_UNARY_INSTRUCTION(X) extern void X ## Instruction(InstructionsHost&, const Argument&); \
_pimpl->unaryInstructions[std::string(#X)] = X ## Instruction;
#define ASM_BINARY_INSTRUCTION(X) extern void X ## Instruction(InstructionsHost&, const Argument&, const Argument&); \
_pimpl->binaryInstructions[std::string(#X)] = X ## Instruction;
Assembler::Assembler()
: _pimpl(new Impl) {
ASM_NULLARY_INSTRUCTION(nop);
ASM_NULLARY_INSTRUCTION(cpir);
ASM_NULLARY_INSTRUCTION(di);
ASM_NULLARY_INSTRUCTION(ei);
ASM_NULLARY_INSTRUCTION(halt);
ASM_NULLARY_INSTRUCTION(neg);
ASM_NULLARY_INSTRUCTION(cpl);
ASM_NULLARY_INSTRUCTION(ret);
ASM_NULLARY_INSTRUCTION(reti);
ASM_BINARY_INSTRUCTION(ld);
ASM_BINARY_INSTRUCTION(add);
ASM_BINARY_INSTRUCTION(sub);
ASM_BINARY_INSTRUCTION(sbc);
ASM_BINARY_INSTRUCTION(ex);
ASM_UNARY_INSTRUCTION(push);
ASM_UNARY_INSTRUCTION(pop);
ASM_UNARY_INSTRUCTION(or);
ASM_UNARY_INSTRUCTION(and);
ASM_UNARY_INSTRUCTION(out);
ASM_UNARY_INSTRUCTION(in);
ASM_UNARY_INSTRUCTION(im);
ASM_UNARY_INSTRUCTION(cp);
ASM_UNARY_INSTRUCTION(inc);
ASM_UNARY_INSTRUCTION(dec);
ASM_UNARY_INSTRUCTION(call);
ASM_UNARY_INSTRUCTION(srl);
// Special handling of instructions which can have different arity
extern void jpUnaryInstruction(InstructionsHost&, const Argument&);
extern void jpBinaryInstruction(InstructionsHost&, const Argument&, const Argument&);
extern void jrUnaryInstruction(InstructionsHost&, const Argument&);
extern void jrBinaryInstruction(InstructionsHost&, const Argument&, const Argument&);
_pimpl->unaryInstructions[std::string("jp")] = jpUnaryInstruction;
_pimpl->binaryInstructions[std::string("jp")] = jpBinaryInstruction;
_pimpl->unaryInstructions[std::string("jr")] = jrUnaryInstruction;
_pimpl->binaryInstructions[std::string("jr")] = jrBinaryInstruction;
}
Assembler::~Assembler() {}
void Assembler::command0(const char* mnemonic) {
try {
auto f = _pimpl->findNullaryInstruction(mnemonic);
f(*_pimpl);
}
catch (const Error& e) {
error(e.message());
}
}
void Assembler::command1(const char* mnemonic, const RawArgument& arg) {
try {
auto f = _pimpl->findUnaryInstruction(mnemonic);
Argument resolvedArg = _pimpl->resolveArgument(arg);
f(*_pimpl, resolvedArg);
}
catch (const Error& e) {
error(e.message());
}
}
void Assembler::command2(const char* mnemonic,
const RawArgument& arg1,
const RawArgument& arg2) {
try {
auto f = _pimpl->findBinaryInstruction(mnemonic);
Argument resolvedArg1 = _pimpl->resolveArgument(arg1);
Argument resolvedArg2 = _pimpl->resolveArgument(arg2);
f(*_pimpl, resolvedArg1, resolvedArg2);
}
catch (const Error& e) {
error(e.message());
}
}
void Assembler::label(const char* label) {
int address = _pimpl->currentSegment().currentOffset();
if (_pimpl->labelTable.contains(label)) {
error(std::string("Label ") + label + std::string(" already defined"));
}
_pimpl->labelTable.addLabel(label, address);
}
void Assembler::metaCommand1(const char* command,
const RawArgument& argument) {
if (strcmp(command, "org") == 0) {
size_t origin = _pimpl->resolveArgument(argument).value();
_pimpl->segments.addSegment(origin);
}
else if (strcmp(command, "string") == 0) {
_pimpl->addString(_pimpl->resolveArgument(argument));
}
else if (strcmp(command, "int16") == 0) {
_pimpl->add16BitAddress(_pimpl->resolveArgument(argument));
}
else if (strcmp(command, "int8") == 0) {
_pimpl->addCode(_pimpl->resolveArgument(argument).value());
}
else
error(std::string("Unknown single argument .command ") + command);
}
void Assembler::metaCommand2(const char* command,
const char* identifier,
const RawArgument& argument) {
if (strcmp(command, "eq") == 0) {
_pimpl->addEq(identifier, _pimpl->resolveArgument(argument));
}
else
error(std::string("Unknown double argument .command ") + command);
}
const Segments& Assembler::segments() const {
return _pimpl->segments;
}
void Assembler::resolveRemaining() {
_pimpl->delayedAddresses.resolve(_pimpl->currentSegment(), _pimpl->labelTable);
}
void Assembler::printSymbolTable(std::ostream& stream) {
_pimpl->labelTable.print(stream);
}
void Assembler::printRelocations(std::ostream& stream) {
_pimpl->relocationsTable.print(stream);
}