-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAssemblyCompiler.cpp
More file actions
469 lines (425 loc) · 12.6 KB
/
Copy pathAssemblyCompiler.cpp
File metadata and controls
469 lines (425 loc) · 12.6 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include "AssemblyCompiler.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <cctype>
#include "Opcode.h"
#include "SymbolTable.h"
//Constructor Destructor
AssemblyCompiler::AssemblyCompiler() = default;
AssemblyCompiler::~AssemblyCompiler()
{
m_Lines.clear();
m_Bytecode.clear();
delete m_pSymbolTable;
m_pSymbolTable = nullptr;
}
//Input
void AssemblyCompiler::SetSource(std::vector<std::string> lines)
{
m_Lines = lines;
m_State = CompState::SOURCE;
}
bool AssemblyCompiler::LoadSource(std::string filename)
{
std::ifstream file(filename);
if(!file.good())
{
std::cerr << "[ASM CMP] Input filestream could not be created" << std::endl;
m_Lines.clear();
m_State = CompState::INIT;
return false;
}
std::string line;
while (std::getline(file, line))
{
m_Lines.push_back(line);
}
if(m_Lines.empty())
{
std::cerr << "[ASM CMP] No assembly lines loaded" << std::endl;
m_Lines.clear();
m_State = CompState::INIT;
return false;
}
std::cout << "[ASM CMP] Assembly file loaded!" << std::endl;
m_State = CompState::SOURCE;
return true;
}
//Compile
bool AssemblyCompiler::Compile()
{
//Check valid input
if(m_State == CompState::INIT)
{
std::cerr << "[ASM CMP] No assembly provided!" << std::endl;
return false;
}
if(m_State == CompState::FAILED)
{
std::cerr << "[ASM CMP] Provide new source before recompiling!" << std::endl;
return false;
}
if(m_State == CompState::COMPILED)
{
std::cerr << "[ASM CMP] No new compilation required!" << std::endl;
return false;
}
if(!m_pSymbolTable)
m_pSymbolTable = new SymbolTable(m_StackSize);
else
std::cerr << "[ASM CMP] Symbol table already created!" << std::endl;
//Do compilation
if(!BuildSymbolTable())return false;
if(!CompileInstructions())return false;
if(!CompileHeader())return false;
std::cout << "[ASM CMP] Compilation Complete, no errors detected!" << std::endl;
m_State = CompState::COMPILED;
return true;
}
bool AssemblyCompiler::BuildSymbolTable()
{
for(uint32 line = 0; line < m_Lines.size(); ++line)
{
std::string opname;
std::string arguments;
if(!TokenizeLine(m_Lines[line], opname, arguments))continue;
//Jump labels
if(opname[0] == '@')
{
if(!m_pSymbolTable->AddLabel(opname))
{
std::cerr << "[ASM CMP] " << line << ": label " << opname << " already defined!" << std::endl;
return false;
}
continue;
}
if(opname[0] == '$')
{
if(!m_pSymbolTable->AddFunction(opname, arguments))
{
std::cerr << "[ASM CMP] " << line << ": error adding function: " << opname << std::endl;
return false;
}
continue;
}
if(!IsValidOpname(opname, line))return false;
Opcode code = OpcodeNames[opname];
switch(code)
{
case Opcode::LITERAL:
{
m_pSymbolTable->m_NumInstructions += 5;
if(!HasValidArgs(arguments, line, opname))return false;
CheckVar(arguments);
}
break;
case Opcode::LITERAL_ARRAY:
{
if(!HasValidArgs(arguments, line, opname))return false;
if(arguments[0] == '\"')
{
if(arguments.size()==1)
{
std::cerr << "[ASM CMP] " << line << ", " << opname << ": Incorrect argument size!" << std::endl;
PrintAbort(line);
return false;
}
uint32 j = 1;
while(arguments[j] != '\"')
{
m_pSymbolTable->m_NumInstructions+=4;
++j;
}
}
else
{
while(!arguments.empty())
{
m_pSymbolTable->m_NumInstructions+=4;
CheckVar(arguments);
}
}
m_pSymbolTable->m_NumInstructions+=5;
}
break;
default:
m_pSymbolTable->m_NumInstructions++;
break;
}
}
m_pSymbolTable->SetParsingStatic();
m_pSymbolTable->AllocateStatic();
return true;
}
bool AssemblyCompiler::CompileInstructions()
{
for(uint32 line = 0; line < m_Lines.size(); ++line)
{
std::string opname;
std::string arguments;
if(!TokenizeLine(m_Lines[line], opname, arguments))continue;
if(opname[0] == '@') continue; //Skip labels
if(opname[0] == '$') //Write num arguments and variables for function
{
WriteInt(m_pSymbolTable->GetFunctionArgCount(opname));
WriteInt(m_pSymbolTable->GetFunctionVarCount(opname));
continue;
}
if(!IsValidOpname(opname, line))return false;
Opcode code = OpcodeNames[opname];
switch(code)
{
case Opcode::LITERAL:
{
if(!HasValidArgs(arguments, line, opname))return false;
int32 parsed;
if(ParseLiteral(parsed, arguments))
{
m_Bytecode.push_back(static_cast<uint8>(code));
WriteInt(parsed);
}
else
{
PrintAbort(line);
return false;
}
}
break;
case Opcode::LITERAL_ARRAY:
{
if(!HasValidArgs(arguments, line, opname))return false;
m_Bytecode.push_back(static_cast<uint8>(code));
std::vector<int32> arr;
if(arguments[0] == '\"')
{
if(arguments.size()==1)
{
std::cerr << "[ASM CMP] " << line << ", " << opname << ": Incorrect argument size!" << std::endl;
PrintAbort(line);
return false;
}
uint32 j = 1;
while(arguments[j] != '\"')
{
if(j >= arguments.size())
{
std::cerr << "[ASM CMP] " << line << ", " << opname << R"(: Expected ' " ' !)" << std::endl;
PrintAbort(line);
return false;
}
arr.push_back(int32(arguments[j]));
++j;
}
}
else
{
while(!arguments.empty())
{
int32 parsed;
if(ParseLiteral(parsed, arguments))
{
WriteInt(parsed);
arr.push_back(parsed);
}
else
{
PrintAbort(line);
return false;
}
}
}
WriteInt(static_cast<int32>(arr.size()));
for(auto value : arr) WriteInt(value);
}
break;
default:
m_Bytecode.push_back(static_cast<uint8>(code));
break;
}
}
return true;
}
bool AssemblyCompiler::CompileHeader()
{
std::vector<uint8> header;
WriteInt(m_StackSize, header);
WriteInt(m_pSymbolTable->GetStaticVarCount(), header);
m_HeaderSize = header.size();
m_Bytecode.insert(m_Bytecode.begin(), header.begin(), header.end());
return true;
}
//Output Results
bool AssemblyCompiler::Save(std::string filename)
{
if(!(m_State == CompState::COMPILED))
{
std::cerr << "[ASM CMP] File not saved, source not compiled!" << std::endl;
return false;
}
std::ofstream output( filename, std::ios::binary );
if(!(output.good()))
{
std::cerr << "[ASM CMP] File not saved, file " << filename << " could not be created" << std::endl;
return false;
}
for(uint8 i : m_Bytecode)
{
output << i;
}
std::cout << "[ASM CMP] Executable saved!" << std::endl;
return true;
}
std::vector<uint8> AssemblyCompiler::GetBytecode()
{
if(!(m_State == CompState::COMPILED))
{
std::cerr << "[ASM CMP] Warning, non compiled bytecode accessed!" << std::endl;
}
return m_Bytecode;
}
//Parsing helpers
bool AssemblyCompiler::TokenizeLine(std::string line, std::string &opname, std::string &arguments)
{
//Comment
if(line.size() > 1 && line[0] == '/' && line[1] == '/')
return false;
//Empty Line
if(line.empty())
return false;
//get op and arguments
std::size_t firstSpace = line.find(' ');
if(firstSpace!=std::string::npos)
{
opname = line.substr(0, firstSpace);
if(firstSpace < line.size()-1)
arguments = line.substr(firstSpace+1);
}
else opname = line;
return true;
}
bool AssemblyCompiler::IsValidOpname(std::string opname, uint32 line)
{
if(!(OpcodeNames.count(opname)))
{
std::cerr << "[ASM CMP] " << line << ": Invalid Opcode '" << opname << "'!" << std::endl;
PrintAbort(line);
return false;
}
return true;
}
void AssemblyCompiler::CheckVar(std::string &arguments)
{
//Separate first argument out
std::string arg;
std::size_t nDelim = arguments.find(' ', 1);
if(nDelim == std::string::npos)
{
arg = arguments;
arguments = std::string();
}
else
{
arg = arguments.substr(0, nDelim);
arguments = arguments.substr(nDelim+1);
}
//Handle found variable
if(arg[0]=='#')
{
m_pSymbolTable->AddVariable(arg);
}
}
bool AssemblyCompiler::HasValidArgs(std::string arguments, uint32 line, std::string opname)
{
if(arguments.empty())
{
std::cerr <<"[ASM CMP] " << line << ", " << opname << ": Incorrect amount of arguments!" << std::endl;
PrintAbort(line);
return false;
}
return true;
}
bool isNumber(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
bool AssemblyCompiler::ParseLiteral(int32 &out, std::string &arguments)
{
if((arguments[0]=='#') || (arguments[0]=='@') || (arguments[0]=='$')) //Replace mnemonics (variables, lables, functions)
{
std::string arg;
std::size_t nDelim = arguments.find(' ', 1);
if(nDelim == std::string::npos)
{
arg = arguments;
arguments = std::string();
}
else
{
arg = arguments.substr(0, nDelim);
arguments = arguments.substr(nDelim+1);
}
if(m_pSymbolTable->HasSymbol(arg))
{
out = static_cast<int32>(m_pSymbolTable->GetValue(arg));
return true;
}
std::cerr << "[ASM CMP] Couldn't find symbol: " << arg << std::endl;
}
else if(arguments[0]=='\'') //char
{
out = int32(arguments[1]);
std::size_t nDelim = arguments.find('\'', 1);
if(nDelim == std::string::npos)
{
arguments = std::string();
}
else arguments = arguments.substr(nDelim+1);
return true;
}
else if(isNumber(arguments)) //int
{
std::size_t nDelim = arguments.find(' ', 1);
if(nDelim == std::string::npos)
{
out = stoi(arguments);
arguments = std::string();
}
else
{
out = stoi(arguments.substr(0, nDelim));
arguments = arguments.substr(nDelim+1);
}
return true;
}
return false;
}
void AssemblyCompiler::WriteInt(int32 value)
{
WriteInt(value, m_Bytecode);
}
void AssemblyCompiler::WriteInt(int32 value, std::vector<uint8> &target)
{
#ifdef WORD_BIG_ENDIAN
target.push_back(value & 0xFF);
target.push_back((value >> 8) & 0xFF);
target.push_back((value >> 16) & 0xFF);
target.push_back((value >> 24) & 0xFF);
#elif
target.push_back((value >> 24) & 0xFF);
target.push_back((value >> 16) & 0xFF);
target.push_back((value >> 8) & 0xFF);
target.push_back(value & 0xFF);
#endif
}
void AssemblyCompiler::PrintAbort(uint32 line)
{
std::cerr << m_Lines[line] << std::endl;
std::cerr << "[ASM CMP]" << std::endl;
std::cerr << "[ASM CMP] Aborting compilation!" << std::endl;
m_State = CompState::FAILED;
}