-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parser.cpp
563 lines (461 loc) · 11.7 KB
/
Parser.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#include "Parser.h"
// -----
// Parser
// Constructs a Parser instance based on a specific input file
// and reads in the files named in the config file.
//
// PARAMS
// filename - a string of the filename of the configuration
// file to be used.
Parser::Parser(std::string filename)
{
read_config_file(filename);
register_file.resize(32);
read_memory_contents();
read_register_file();
read_program();
}
// -----
// ~Parser
// Destructs a Parser instance.
Parser::~Parser()
{
}
// Define an enumeration of the opcodes that will be
// 6-bit opcode field and 6-bit func field set
enum Opcode
{
UNDEF = 0,
ADD = (0x00 << 26) | (32),
SUB = (0x00 << 26) | (34),
ADDI = (0x08 << 26) | (0),
SLT = (0x00 << 26) | (42),
LW = (0x23 << 26) | (0),
SW = (0x2b << 26) | (0),
BEQ = (0x04 << 26) | (0),
J = (0x02 << 26) | (0)
};
// -----
// get_register
// Reads a decimal value from a string if a '$' is present, meaning it
// is a register. Otherwise, reads as hex and sets a flag.
u32 get_register(char *f)
{
for(unsigned int i = 0; i < strlen(f); ++i)
if(f[i] == '$')
return strtoul(f + 1, NULL, 10);
// Set a flag in MSB for a value returned that did not contain
// a '$' -- meaning the value is actually a shift amount
return strtoul(f, NULL, 16) | 0x80000000;
}
// -----
// handle_RType
// Returns the three 5-bit registers and the shift amount between
// the Opcode and func fields of a MIPS instruction.
u32 handle_RType(char *fields)
{
char *rd, *rs, *rt;
int rd_n = 0, rs_n, rt_n, sh_n = 0;
// All R-Type instructions we are handling will have 3 register fields
if((rd = strtok(fields, ", ")) == NULL)
return(0);
if((rs = strtok(NULL, ", ")) == NULL)
return(0);
if((rt = strtok(NULL, ", ")) == NULL)
return(0);
u32 t = get_register(rd);
// Determine whether the 'this is a shift amount' flag is set or not
if(t >> 31)
sh_n = t & 0x7FFFFFFF;
else
rd_n = t;
rs_n = get_register(rs);
rt_n = get_register(rt);
return (rs_n << 21)
| (rt_n << 16)
| (rd_n << 11)
| (sh_n << 6);
}
// -----
// handle_IType
// Returns the two, 5-bit register values in proper order and
// the 16-bit immediate field; all in a 32-bit word.
u32 handle_IType(char *fields)
{
char *rs, *rt, *imm;
bool offset = true;
int rs_n, rt_n, imm_n;
// Check if we have the immediate field 2nd (true) or 3rd (false)
int commas = 0;
for(unsigned int i = 0; i < strlen(fields); ++i)
if(fields[i] == ',')
commas++;
if(commas == 2)
offset = false;
if((rt = strtok(fields, ", ")) == NULL)
return(0);
rt_n = get_register(rt);
if(!offset) // $rt, $rs, imm
{
if((rs = strtok(NULL, ", ")) == NULL)
return(0);
if((imm = strtok(NULL, ", ")) == NULL)
return(0);
rs_n = get_register(rs);
// The offset (immediate) value in ADDI can be expressed either
// in hexadecimal or decimal, strtol with 0 to determine which
imm_n = (u16) strtol(imm, NULL, 0);
}
else // $rt, imm($rs) -> $rt
{
if((imm = strtok(NULL, ",()")) == NULL) // imm($rs) -> imm
return(0);
if((rs = strtok(NULL, ",()")) == NULL) // $rs) -> $rs
return(0);
rs_n = get_register(rs);
// The offset field in LW, and SW instructions is represented in
// decimal, and may be either positive or negative
imm_n = (u16) strtol(imm, NULL, 10);
}
return (rs_n << 21)
| (rt_n << 16)
| (imm_n);
}
// -----
// handle_JType
// Returns the formatted 26-bit address field of a J-Type MIPS instruction.
u32 handle_JType(char *fields)
{
// Fill last 26 bits with value
return 0x03FFFFFF & (strtoul(fields, NULL, 16) >> 2);
}
// -----
// match_case
// Returns whether two words are equal to eachother, case insensitive.
//
// PARAMS
// a - a null-terminated byte string which we'll check is equal to b
// b - a null-terminated byte string which we'll check is equal to a
//
// RETURN
// true if the strings are lexicographically, but not integrally, equal
bool match_case(const char *a, const char *b)
{
int a_len = strlen(a);
bool result = true;
// Check if the char values are equal, or
// just the same letter regardless of case
for(int i = 0; i < a_len; ++i)
result &= (a[i] - b[i] == 0
|| a[i] - b[i] == ('a' - 'A')
|| a[i] - b[i] == ('A' - 'a'));
return result;
}
// -----
// translate_to_machine
// Translates each of the MIPS instruction lines in
// the instruction vector field string_instructions
// into 32-bit machine code.
//
// PARAMS
// line - an instruction line to convert
//
// RETURN
// the MIPS assembly instruction's machine code
// representation
u32 Parser::translate_to_machine(std::string line)
{
u32 instruction = 0;
// Take the line as a null-terminated byte string
char *buf = strdup(line.c_str());
char *buf_s = buf; // Actual string start so we can free
// Replace tabs with spaces to make parsing easier
for(unsigned int i = 0; i < strlen(buf); ++i)
if(buf[i] == '\t')
buf[i] = ' ';
// Get the Opcode from the instruction line
char *opcode = strtok(buf, " ");
char *fields = strtok(NULL, ""); // Don't null the next delimiter
Opcode op = UNDEF;
if(match_case("add", opcode)) op = ADD;
if(match_case("sub", opcode)) op = SUB;
if(match_case("addi", opcode)) op = ADDI;
if(match_case("slt", opcode)) op = SLT;
if(match_case("lw", opcode)) op = LW;
if(match_case("sw", opcode)) op = SW;
if(match_case("beq", opcode)) op = BEQ;
if(match_case("j", opcode)) op = J;
// Handle the fields we'd read in differently based upon the type of instruction
switch(op)
{
case ADD:
instruction |= ADD;
instruction |= handle_RType(fields);
break;
case SUB:
instruction |= SUB;
instruction |= handle_RType(fields);
break;
case ADDI:
instruction |= ADDI;
instruction |= handle_IType(fields);
break;
case SLT:
instruction |= SLT;
instruction |= handle_RType(fields);
break;
case LW:
instruction |= LW;
instruction |= handle_IType(fields);
break;
case SW:
instruction |= SW;
instruction |= handle_IType(fields);
break;
case BEQ:
instruction |= BEQ;
instruction |= handle_IType(fields);
break;
case J:
instruction |= J;
instruction |= handle_JType(fields);
break;
default:
free(buf_s);
return(0);
}
free(buf_s);
return instruction;
}
// -----
// stripLine
// Strips comments and any trailing or leading
// whitespace from an input line.
//
// PARAMS
// line - an input line to strip
//
// RETURN
// a string with no trailing/leading whitespace
// or comments
std::string stripLine(std::string line)
{
int size = line.size();
char *buf = strdup(line.c_str()); // Duplicated const char *
char *buf_s = buf; // Actual string start so we can free
// Cut off any comments
for(int i = 0; i < size; ++i)
if(buf[i] == '#')
buf[i] = '\0';
// Strip any leading whitespace
while(buf[0] != '\0' && (buf[0] == ' ' || buf[0] == '\t'))
buf++;
int end = strlen(buf) - 1;
// If nothing remains, ignore the line
if(end < 0)
return "";
// Strip any trailing whitespace
while(end != 0 && buf[end] != '\0'
&& (buf[end] == ' ' || buf[end] == '\t'))
{
buf[end] = '\0';
end--;
}
// Replace tabs with spaces to make parsing easier
for(unsigned int i = 0; i < strlen(buf); ++i)
if(buf[i] == '\t')
buf[i] = ' ';
// Store the line
std::string temp = buf;
// Free all of the allocated memory from the original start
free(buf_s);
return temp;
}
// -----
// Parser::read_register_file
// Reads in and parses lines from the register file input
// containing <register:value> pairs.
void Parser::read_register_file()
{
std::ifstream input;
input.open(register_file_input.c_str());
if(input.bad() || !input)
{
printf("Could not open file \"%s\"!\n", register_file_input.c_str());
}
else
{
std::string line;
int lineNum = 0;
while(getline(input, line))
{
lineNum++;
std::string str = stripLine(line);
if(str.size() == 0)
continue;
char *buf = strdup(str.c_str());
char *buf_s = buf;
char *reg;
char *value;
reg = strtok(buf, ":");
if((value = strtok(NULL,":")) == NULL)
{
printf("Malformed input on line %d!\n", lineNum);
free(buf_s);
break;
}
else
{
// Take chars and convert each into its int value;
// decimal for register number and hex for the value
u32 r = std::strtoul(reg, NULL, 10);
u32 val = std::strtoul(value, NULL, 16);
// Ignore invalid-valued registers
if(r > 31)
continue;
register_file[r] = val;
}
free(buf_s);
}
}
}
// -----
// Parser::read_memory_contents
// Reads in and parses lines from the initial memory contents
// file containing <address:value> pairs.
void Parser::read_memory_contents()
{
std::ifstream input;
input.open(memory_contents_input.c_str());
if(input.bad() || !input)
{
printf("Could not open file \"%s\"!\n", memory_contents_input.c_str());
}
else
{
std::string line;
int lineNum = 0;
while(getline(input, line))
{
lineNum++;
std::string str = stripLine(line);
if(str.size() == 0)
continue;
char *buf = strdup(str.c_str());
char *buf_s = buf;
char *address;
char *value;
address = strtok(buf, ":");
if((value = strtok(NULL,":")) == NULL)
{
printf("Malformed input on line %d!\n", lineNum);
free(buf_s);
break;
}
else
{
// Take string and convert it into a hexadecimal number
u32 addr = std::strtoul(address, NULL, 16);
u32 val = std::strtoul(value, NULL, 16);
memory_module[addr] = val;
}
free(buf_s);
}
}
}
// -----
// Parser::read_program
// Reads in and parses lines from the input program file
// and stores them in the member vector field.
void Parser::read_program()
{
std::ifstream input;
input.open(program_input.c_str());
if(input.bad() || !input)
{
printf("Could not open file \"%s\"!\n", program_input.c_str());
}
else
{
std::string line;
int lineNum = 0;
while(getline(input, line))
{
std::string str = stripLine(line);
if(str.size() == 0)
continue;
// Store the line in the instruction vector
string_instructions.push_back(str);
lineNum++;
// Convert the line to machine code and store
u32 instruction = translate_to_machine(str);
instruction_memory.push_back(instruction);
}
instruction_mem_size = lineNum;
}
}
// -----
// Parser::read_config_file
// Reads in and parses lines from the input configuration
// file and assigns the values to Parser's fields.
//
// PARAMS
// filename - string name of the input config file
void Parser::read_config_file(std::string filename)
{
std::ifstream input;
input.open(filename.c_str());
if(input.bad() || !input)
{
printf("Could not open file \"%s\"!\n", filename.c_str());
}
else
{
std::string line;
int lineNum = 0;
while(getline(input, line))
{
lineNum++;
std::string str = stripLine(line);
if(str.size() == 0)
continue;
char *buf = strdup(str.c_str());
char *buf_s = buf;
char *parameter;
char *value;
parameter = strtok(buf, "=");
if((value = strtok(NULL,"=")) == NULL)
{
printf("Malformed input on line %d!\n", lineNum);
free(buf_s);
break;
}
else
{
if (!strcmp(parameter, "program_input"))
program_input = value;
else if (!strcmp(parameter, "memory_contents_input"))
memory_contents_input = value;
else if (!strcmp(parameter, "register_file_input"))
register_file_input = value;
else if (!strcmp(parameter, "output_mode"))
output_mode = value;
else if (!strcmp(parameter, "debug_mode"))
debug_mode = value;
else if (!strcmp(parameter, "print_memory_contents"))
print_memory_contents = value;
else if (!strcmp(parameter, "output_file"))
output_file = value;
else if (!strcmp(parameter, "write_to_file"))
write_to_file = value;
else
{
printf("Malformed input on line %d!\n", lineNum);
free(buf_s);
break;
}
}
free(buf_s);
}
}
}