forked from mashiox/idaho_sicxe_assembler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sicxe_asm.cpp
483 lines (419 loc) · 12.1 KB
/
sicxe_asm.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* Phillip Domann, Shad Aziz, Melanie Reed, Matt Walther
masc0832
Team Idaho
prog3
CS530, Spring 2016
*/
#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <algorithm>
#include "sicxe_asm.h"
#include "file_parse_exception.h"
#include "opcode_error_exception.h"
#include "symtab_exception.h"
using namespace std;
int hextoi(string str) {
int integer;
stringstream ss;
ss << hex << str;
ss >> integer;
return integer;
}
bool isdecimal(string& str, size_t start, size_t end) {
for (size_t i = start; i < end; ++i) {
if (!isdigit(str[i])) {
return false;
}
}
return true;
}
bool ishexadecimal(string& str, size_t start, size_t end) {
string upper = str;
transform(upper.begin(),upper.end(), upper.begin(), ::toupper);
for (size_t i = start; i < end; ++i) {
if (!(isdigit(upper[i]) || (65 <= upper[i] && upper[i] <= 70))) {
return false;
}
}
return true;
}
bool isconstant(string& str) {
if (str.empty())
return false;
return (str[0] == '$') ? ishexadecimal(str, 1, str.length())
: isdecimal(str, 0, str.length());
}
int ctoi(string& str) {
return (str[0] == '$') ? hextoi(str.substr(1)) : hextoi(str);
}
bool isquoted(string& str) {
return ((str[1] == '\'') && (str[str.length()-1] == '\'')) ? true : false;
}
bool isliteral(string& str) {
if (str.empty() || str.length() < 3)
return false;
if ((str[0] == 'c') || (str[0] == 'C'))
return isquoted(str);
else if (((str[0] == 'x') || (str[0] == 'X')) && (str.length()%2 == 1)) {
return (isquoted(str)) ? ishexadecimal(str, 2, str.length()-1): false;
}
return false;
}
unsigned int size_for_literal(string& str) {
return ((str[0] == 'c') || (str[0] == 'C')) ? (unsigned int)str.length()-3
: (unsigned int)(str.length()-3)>>1;
}
string itos(int integer) {
stringstream itoss;
itoss << integer;
return itoss.str();
}
bool is_start( string opcode ){
transform(opcode.begin(), opcode.end(), opcode.begin(), ::toupper);
return ( opcode.compare("START") == 0 ? true : false );
}
bool is_end( string opcode ){
transform(opcode.begin(), opcode.end(), opcode.begin(), ::toupper);
return ( opcode.compare("END") == 0 ? true : false );
}
const struct sicxe_asm::dhpair sicxe_asm::dhpairs[9] = {
{"START", &sicxe_asm::handle_start},
{"END", &sicxe_asm::handle_directive},
{"BYTE", &sicxe_asm::handle_byte_word},
{"WORD", &sicxe_asm::handle_byte_word},
{"RESB", &sicxe_asm::handle_resb},
{"RESW", &sicxe_asm::handle_resw},
{"BASE", &sicxe_asm::handle_directive},
{"NOBASE", &sicxe_asm::handle_directive},
{"EQU", &sicxe_asm::handle_equ}
};
sicxe_asm::sicxe_asm(string file) {
intermed_filen = file;
parser = new file_parser(file);
locctr = 0;
setup_handler_map();
string listingFile = file.substr(0, file.rfind("."));
listingFile = listingFile + ".lis";
listing.open(listingFile.c_str());
listing_head(file);
}
sicxe_asm::~sicxe_asm() {
listing.close();
delete parser;
}
void sicxe_asm::error_str(string msg) {
string str = "***Error***\n";
str.append(msg);
throw str;
}
void sicxe_asm::error_ln_str(string msg) {
string str = "***Error***\n";
string lnmsg = "Line "+itos(index+1)+": "+label+" "+opcode+" "+operand+"\n";
str.append(lnmsg);
str.append(msg);
throw str;
}
void sicxe_asm::pass1() {
parser->read_file();
unsigned int nlines = (unsigned int)parser->size();
for (index = 0; index < nlines; ++index) {
get_tokens();
if (is_start(opcode)) {
handle_start();
listing_lnout();
break;
} else if (!opcode.empty()) {
error_ln_str("There must be no operations before start directive.");
} else {
listing_lnout();
line_addrs.push_back(locctr);
}
}
if (index == nlines) {
error_str("There is no start directive in sourcefile.");
}
sym_handler handle_symbol;
for (++index; index < nlines; ++index) {
line_addrs.push_back(locctr);
get_tokens();
listing_lnout();
if (is_end(opcode)) {
handle_directive();
break;
}
else {
handle_symbol = handler_for_symbol();
(this->*handle_symbol)();
}
}
if (index == nlines) {
error_str("There is no end directive in sourcefile.");
}
for (++index; index < nlines; ++index) {
line_addrs.push_back(locctr);
get_tokens();
listing_lnout();
if (!opcode.empty()) {
error_ln_str("Additional operations cannot exist after end directive.");
}
}
}
void sicxe_asm::pass2() {
parser = new file_parser(file);
unsigned int nlines = (unsigned int)parser->size();
for ( index = 0 ; index < nlines ; index++ ){
get_tokens();
if ( is_start(opcode) ){
}
}
}
void sicxe_asm::get_tokens() {
label = parser->get_token(index, 0);
opcode = parser->get_token(index, 1);
operand = parser->get_token(index, 2);
comment = parser->get_token(index, 3);
}
void sicxe_asm::listing_head(string filename) {
int width = 38-(int)(filename.length()>>1);
listing << setw(width) << "**" << filename << "**" << "\n\n";
listing << setw(5) << "Line#";
listing << setw(8) << " Address";
listing << left << setw(16) << " Label";
listing << setw(16) << "Opcode";
listing << setw(36) << "Operand";
listing << endl;
listing << setw(5) << "=====" << setw(8) << " =======" << setw(16);
listing << " =====" << setw(16) << "======" << setw(36) << "=======";
listing << endl;
}
void sicxe_asm::listing_lnout() {
listing << right << setw(5) << dec << index+1;
listing << " " << setw(5) << hex << uppercase << setfill('0') << locctr;
listing << " " << left << setw(15) << setfill(' ') << label;
listing << setw(16) << opcode;
listing << setw(36) << operand;
listing << endl;
}
void sicxe_asm::setup_handler_map() {
unsigned int i;
for (i = 0; i < sizeof(opcodetab::instrs)/sizeof(opcodetab::instr); ++i) {
string instr = opcodetab::instrs[i].menmonic;
transform(instr.begin(), instr.end(), instr.begin(), ::toupper);
hmap.insert(make_pair(instr, &sicxe_asm::handle_instruction));
}
for (i = 0; i < sizeof(sicxe_asm::dhpairs)/sizeof(sicxe_asm::dhpair); ++i) {
hmap.insert(make_pair(sicxe_asm::dhpairs[i].directive,
sicxe_asm::dhpairs[i].handler));
}
}
sicxe_asm::sym_handler sicxe_asm::handler_for_symbol() {
if (opcode.empty()) {
return &sicxe_asm::handle_empty;
}
string symbol = (opcode[0] == '+') ? opcode.substr(1, string::npos) : opcode;
transform(symbol.begin(), symbol.end(), symbol.begin(), ::toupper);
map<string, sym_handler>::iterator iter = hmap.find(symbol);
if (iter == hmap.end()) {
error_ln_str("Invalid opcode.");
}
return iter->second;
}
void sicxe_asm::handle_instruction() {
try {
locctr += optab.get_instruction_size(opcode);
}
catch (opcode_error_exception e) {
error_ln_str(e.getMessage());
}
add_symbol_for_label();
}
void sicxe_asm::handle_start() {
if (isconstant(operand))
locctr = ctoi(operand);
else
error_ln_str("Invalid constant.");
try {
symbols.add(opcode, label);
}
catch (symtab_exception e) {
error_ln_str(e.getMessage());
}
}
void sicxe_asm::handle_byte_word() {
if (isliteral(operand))
locctr += size_for_literal(operand);
else if (isconstant(operand))
locctr += (opcode[0] == 'b' || opcode[0] == 'B') ? 1: 3;
else
error_ln_str("Invalid quoted literal.");
add_symbol_for_label();
}
void sicxe_asm::handle_resb() {
if (isconstant(operand))
locctr += ctoi(operand);
else
error_ln_str("Invalid constant.");
add_symbol_for_label();
}
void sicxe_asm::handle_resw() {
if (isconstant(operand))
locctr += ctoi(operand)*3;
else
error_ln_str("Invalid constant.");
add_symbol_for_label();
}
void sicxe_asm::handle_equ() {
try {
symbols.add(label, operand);
}
catch (symtab_exception e) {
error_ln_str(e.getMessage());
}
}
void sicxe_asm::handle_directive() {
try {
symbols.add(opcode, operand);
}
catch (symtab_exception e) {
error_ln_str(e.getMessage());
}
}
void sicxe_asm::handle_empty() {
add_symbol_for_label();
}
void sicxe_asm::add_symbol_for_label() {
if (!label.empty()) {
try {
symbols.add(label, itos(locctr));
}
catch (symtab_exception e) {
error_ln_str(e.getMessage());
}
}
}
int sicxe_asm::getDisplacement( int addr1, int addr2 ){
int disp = addr1 - addr2;
if ( disp >= -2048 && disp <= 2047 ){
nixbpe[3] = 0;
nixbpe[4] = 1;
return disp;
}
if ( disp >= 0 && disp <= 4095 ){
nixbpe[3] = 1;
nixbpe[4] = 0;
return disp;
}
error_ln_str("Addressing displacement out of bounds, use format 4");
}
// Pass 2 Format 1 & 2
string sicxe_asm::get_reg_val(string r){ // Return register number
int i = 0;
while(r[i]) {
r[i] = toupper(r[i]);
i++;
}
if ( r == "A" ) return "0";
else if ( r == "X" ) return "1";
else if ( r == "L" ) return "2";
else if ( r == "B" ) return "3";
else if ( r == "S" ) return "4";
else if ( r == "T" ) return "5";
else if ( r == "F" ) return "6";
else if ( r == "PC" ) return "8";
else if ( r == "SW" ) return "9";
else return "";
}
int sicxe_asm::str_toint(string r){ // turns string into int
int tempint;
istringstream(r) >> tempint;
return tempint;
}
string sicxe_asm::int_tohex_tostr(int r){ //converts int into hex, then into string
stringstream tempstr;
tempstr << hex << r;
return tempstr.str();
}
string sicxe_asm::format_1(string opc) { // Format 1 machine code
return optab.get_machine_code(opc);
}
string sicxe_asm::format_2(string opc, string oper) { // Format 2 Machine code
string op_machine_code = optab.get_machine_code(opc);
string machine_code, r1, r2 = "";
stringstream str(oper);
getline(str, r1, ',');
getline(str, r2);
string r1_value = get_reg_val(r1);
string r2_value = get_reg_val(r2);
int i = 0;
while(opc[i]) {
opc[i] = tolower(opc[i]);
i++;
}
if(r1 == ""){ // register 1 must exist or else error
error_str("Opcode " + opc + " has incorrect argument");
}
if(opc == "clear" || opc == "tixr") {
if(r1_value == "") { // register 1 must have a value
error_str("Opcode " + opc + " has incorrect argument");
}
machine_code = op_machine_code + r1_value + "0";
}
else if (opc == "shiftl" || opc == "shiftr") {
if(r2 == "" || r1_value == ""){
error_str("Opcode " + opc + " has incorrect argument");
}
else {
int tempint = str_toint(r2);
tempint--;
string tempR2 = int_tohex_tostr(tempint);
machine_code = op_machine_code + r1_value + tempR2;
}
}
else if ( opc == "svc") {
int tempint = str_toint(r1);
string tempR1 = int_tohex_tostr(tempint);
machine_code = op_machine_code + tempR1 + "0";
}
else {
if(r2_value == "" || r1_value == "") { //register value nust exist
error_str("Opcode " + opc + " has incorrect argument");
}
else
machine_code = op_machine_code + r1_value + r2_value;
}
if (machine_code == "") {
error_str("Opcode " + opc + " does not exist");
}
return machine_code;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
cout << "Proper usage is " << argv[0] << " filename. " << endl;
return 1;
}
sicxe_asm assembler(argv[1]);
try {
assembler.pass1();
}
catch (file_parse_exception fpe) {
cout << fpe.getMessage();
}
catch (opcode_error_exception oee) {
cout << oee.getMessage();
}
catch (symtab_exception se) {
cout << se.getMessage();
}
catch (string str) {
cout << str;
}
try {
assembler.pass2();
return 0;
}
return 1;
}