-
Notifications
You must be signed in to change notification settings - Fork 2
/
Assembler.cpp
executable file
·293 lines (272 loc) · 8.56 KB
/
Assembler.cpp
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
#include "Assembler.hpp"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include "Instructions.hpp"
using namespace std;
Assembler::Assembler(const string& fname)
{
for (int i = 0; i < sizeof(InstStrings) / sizeof(*InstStrings); i++)
{
_insts[InstStrings[i]] = char(i);
}
assemble(fname);
resolveLabels(_localLabels, _localLabelRefs);
resolveLabels(_labels, _labelRefs);
}
void Assembler::assemble(const string& fname)
{
std::ifstream file(fname.c_str());
std::stringstream ss;
string in;
int line = 0;
while (getline(file, in))
{
++line;
vector<string> tokens = tokenize(in);
if (tokens.empty())
{
continue;
}
ss.clear();
_debugInfo[_bin.size()].clear();
_debugInfo[_bin.size()][fname] = line;
if (tokens.front() == "%file")
{
if (tokens.size() != 2)
{
throw Error(fname, line, "Need 1 argument for %file directive");
}
assemble(tokens.at(1));
}
else if (find(tokens.front().begin(), tokens.front().end(), ':') != tokens.front().end())
{
// Label
if (tokens.size() != 1)
{
ss << in << " is not a valid identifier";
throw Error(fname, line, ss.str());
}
if (find(tokens.front().begin(), tokens.front().end(), ':') == tokens.front().end() - 1)
{
string label(tokens.front().begin(), tokens.front().end() - 1);
map<string, uint16_t>* labelMap;
if (label[0] == '.')
{
if (_topLabel.empty())
{
throw Error(fname, line, "You cannot declare a local label"
" without a top level label");
}
labelMap = &_localLabels;
}
else
{
resolveLabels(_localLabels, _localLabelRefs);
_topLabel = label;
_localLabels.clear();
_localLabelRefs.clear();
labelMap = &_labels;
}
map<string, uint16_t>::const_iterator it = labelMap->find(label);
if (it == labelMap->end())
{
(*labelMap)[label] = _bin.size();
}
else
{
ss << label << " is defined multiple times";
throw Error(fname, line, ss.str());
}
}
else
{
ss << in << " is not a valid identifier";
throw Error(fname, line, ss.str());
}
}
else
{
const std::map<string, char>::const_iterator it =
_insts.find(tokens.front());
if (it != _insts.end())
{
_bin.push_back(it->second);
char suffix = tokens.front().at(tokens.front().size()-1);
if (isdigit(suffix))
{
if (tokens.size() != 2)
{
throw Error(fname, line, "Argument required");
}
if (suffix == '1')
{
handleOperand(tokens.at(1), false);
}
else if (suffix == '2')
{
handleOperand(tokens.at(1), true);
}
else
{
throw Error(fname, line, "Invalid numeric suffix on instruction");
}
}
else
{
if (tokens.size() > 1)
{
throw Error(fname, line, "Too many operands");
}
}
}
else if (tokens.front() == "DS")
{
// Declare string
string::iterator it = std::find(in.begin(), in.end(), '"');
if (it == in.end())
{
ss << "Bad syntax for string declaration";
throw Error(fname, line, ss.str());
}
++it; // Step past quotation mark
const string::const_iterator end = std::find(it, in.end(), '"');
if (end == in.end())
{
ss << "Bad syntax for string declaration";
throw Error(fname, line, ss.str());
}
for (; it != end; ++it)
{
_bin.push_back(*it);
}
//_bin.push_back(0); // Terminator (should this be explicit?)
}
else if (tokens.front() == "D1" || tokens.front() == "D2")
{
std::vector<string>::const_iterator it = tokens.begin();
const std::vector<string>::const_iterator end = tokens.end();
for (++it; it != end; ++it)
{
int num = toInt(*it);
if (tokens.front() == "D2")
{
_bin.push_back(char(num >> 8));
}
_bin.push_back(char(num));
}
}
else if (tokens.front() == "R")
{
// Reserve bytes
if (tokens.size() != 2)
{
throw Error(fname, line, "(R)eserve needs one parameter");
}
int bytes = toInt(tokens.back());
if (bytes <= 0)
{
throw Error(fname, line, "(R)eserve needs a positive integer");
}
while (bytes-- > 0)
{
_bin.push_back(0);
}
}
else
{
throw Error(fname, line, "Bad instruction or directive");
}
}
}
file.close();
}
void Assembler::handleOperand(const string& operand, bool bits16)
{
const map<string, uint16_t>::const_iterator lblIt = _labels.find(operand);
if (operand[0] == '-' || isdigit(operand[0]))
{
int num = toInt(operand);
if (bits16)
{
_bin.push_back(char(num >> 8));
}
_bin.push_back(char(num));
}
else if (operand[0] == '\'' && operand.size() == 3)
{
_bin.push_back(operand[1]);
}
else
{
// This is an identifier
if (operand[0] == '.')
{
_localLabelRefs.push_back(pair<string, int>(operand, _bin.size()));
}
else
{
_labelRefs.push_back(pair<string, int>(operand, _bin.size()));
}
// Make room. We'll populate this later.
_bin.push_back(0);
_bin.push_back(0);
}
}
vector<string> Assembler::tokenize(const string& line)
{
vector<string> result;
// Strip off comments
string cleaned(line.begin(), find(line.begin(), line.end(), '#'));
char* buf = new char[cleaned.size()];
strcpy(buf, cleaned.c_str());
const char* delim = " \t";
const char* token = strtok(buf, delim);
while (token)
{
result.push_back(token);
token = strtok(NULL, delim);
}
delete[] buf;
return result;
}
void Assembler::resolveLabels(map<string, uint16_t>& labels,
vector<pair<string, int> >& labelRefs)
{
const std::vector< std::pair<std::string, int> >::const_iterator end =
labelRefs.end();
std::vector< std::pair<std::string, int> >::const_iterator it =
labelRefs.begin();
for (; it != end; ++it)
{
const map<string, uint16_t>::const_iterator lblIt = labels.find(it->first);
if (lblIt == labels.end())
{
stringstream ss;
ss << "Unable to resolve identifier " << it->first;
throw Error("", 0, ss.str());
}
_bin[it->second] = char(lblIt->second >> 8);
_bin[it->second + 1] = char(lblIt->second);
}
}
int Assembler::toInt(const string& num)
{
int result = 0;
stringstream ss;
if (!num.empty())
{
if (num.size() > 2 && num[0] == '0' && tolower(num[1]) == 'x')
{
ss << string(num.c_str() + 2);
ss >> hex >> result;
}
else
{
ss << num;
ss >> result;
}
}
return result;
}