-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
618 lines (599 loc) · 19.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include "lexer.h"
#include "vasm.h"
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <bitset>
#include <fstream>
using namespace std;
//File
ofstream outfile;
//For generating error codes
static int curr_line = 1;
//Token list
static vector<Token> token_list;
static size_t curr_token = 0;
int peek_token(){
if(curr_token < token_list.size())
return token_list[curr_token].type;
return -1;
}
Token get_token(){
if(curr_token < token_list.size())
return token_list[curr_token++];
else{
printf("Consumed too much token\n");
exit(1);
}
}
//Symbol table
map<string,int> symtab;
//Parse result
vector<Inst_node> prog;
//Reserved word
map<string, int> reserved_ops;
void build_ops_table(){
reserved_ops.insert(pair<string, int>("NOP", 0));
reserved_ops.insert(pair<string, int>("BRA", 2));
reserved_ops.insert(pair<string, int>("LD", 2));
reserved_ops.insert(pair<string, int>("STR", 2));
reserved_ops.insert(pair<string, int>("ADD", 2));
reserved_ops.insert(pair<string, int>("MUL", 2));
reserved_ops.insert(pair<string, int>("CMP", 2));
reserved_ops.insert(pair<string, int>("SHRA", 2));
reserved_ops.insert(pair<string, int>("SHL", 2));
reserved_ops.insert(pair<string, int>("SHR", 2));
reserved_ops.insert(pair<string, int>("AND", 2));
reserved_ops.insert(pair<string, int>("OR", 2));
reserved_ops.insert(pair<string, int>("XOR", 2));
reserved_ops.insert(pair<string, int>("HLT", 0));
reserved_ops.insert(pair<string, int>("SUB", 2));
}
//Matching function, return required operand number, -1 for not found
int search_ops(string s){
map<string, int>::iterator it = reserved_ops.find(s);
if(it == reserved_ops.end())
return -1;
else
return it->second;
}
//Parser function, no semantic check done here
void parse(){
build_ops_table();
//Checks maybe required
token_list = get_token_list();
//Parse
while(peek_token() != -1 && peek_token() != EOF_T){
Inst_node n;
Token t;
//Parse empty line
if(peek_token() == NEWLINE_T){
get_token();
continue;
}
//Parse hex memory value
if(peek_token() == HEX_T){
t = get_token();
n.label.type = EMPTY;
n.op.type = EMPTY;
n.dst = t;
n.src.type = EMPTY;
if(t.hex.size() != 8){
printf("Line %d, hex value must be 8 characters long\n", curr_line + 1);
exit(1);
}
//parse new line
if(peek_token() != EOF_T && peek_token() != NEWLINE_T){
printf("Line %d, expect new line\n", curr_line);
exit(1);
}
//Consume newline
get_token();
curr_line++;
prog.push_back(n);
continue;
}
if(peek_token() == IDENT_T){
t = get_token();
//Check is label
if(peek_token() == COLON_T){
n.label = t;
//Consume colon
get_token();
//Check for labeled memory variable need to be added
/*****NEED TO ADD FUNCTION HERE******/
if(peek_token() == HEX_T){ //Labeled memory
t = get_token();
n.op.type = EMPTY;
n.dst = t;
n.src.type = EMPTY;
if(t.hex.size() != 8){
printf("Line %d, hex value must be 8 characters long\n", curr_line + 1);
exit(1);
}
//Parse new line
if(peek_token() != EOF_T && peek_token() != NEWLINE_T){
printf("Line %d, expect new line\n", curr_line);
exit(1);
}
get_token();
curr_line++;
prog.push_back(n);
continue;
}
if(peek_token() != IDENT_T){
printf("Line %d, Expect identifier\n", curr_line);
exit(1);
}
//Renew token
t = get_token();
}else{
n.label.type = EMPTY;
}
}
//Match operation
int op_num = search_ops(t.ident);
if(op_num == -1){
printf("Line %d, Error : no such instruction\n", curr_line);
exit(1);
}
n.op = t;
if(op_num == 0){
if(peek_token() != EOF_T && peek_token() != NEWLINE_T){
printf("Line %d, Too many operands\n", curr_line);
exit(1);
}
//Consume newline
get_token();
curr_line++;
n.src.type = EMPTY;
n.dst.type = EMPTY;
prog.push_back(n);
continue;
}
if(op_num == 2){
if(peek_token() != IMM_T && peek_token() != IDENT_T){
printf("Line %d, error destination operand\n", curr_line);
exit(1);
}
t = get_token();
n.dst = t;
//parse comma
if(peek_token() != COMMA_T){
printf("Line %d, expect ,\n", curr_line);
exit(1);
}
get_token();
//parse second operand
if(peek_token() != IMM_T && peek_token() != IDENT_T){
printf("Line %d, error source operand\n", curr_line);
exit(1);
}
t = get_token();
n.src = t;
//parse new line
if(peek_token() != EOF_T && peek_token() != NEWLINE_T){
printf("Line %d, expect new line\n", curr_line);
exit(1);
}
//Consume newline
get_token();
curr_line++;
prog.push_back(n);
continue;
}
}
return ;
}
void print_prog_line(size_t i, int label){
//Print label
if(label){
if(prog[i].label.type == EMPTY)
printf("\t\t\t");
else
printf("%s:\t\t\t", prog[i].label.ident.c_str());
}
//Print inst
if(prog[i].op.type != EMPTY)
printf("%s ", prog[i].op.ident.c_str());
//Print dst
if(prog[i].dst.type == IDENT_T)
printf("%s, ", prog[i].dst.ident.c_str());
else if(prog[i].dst.type == IMM_T)
printf("%d, ", prog[i].dst.imm);
else if(prog[i].dst.type == HEX_T)
printf("0x%s ", prog[i].dst.hex.c_str());
//Print src
if(prog[i].src.type == IDENT_T)
printf("%s ", prog[i].src.ident.c_str());
else if(prog[i].src.type == IMM_T)
printf("#%d ", prog[i].src.imm);
printf("\n");
}
void print_prog(){
for(size_t i = 0; i < prog.size(); i++){
print_prog_line(i, 1);
}
}
//Semantic check
void build_symtab(){
for(size_t i = 0; i < prog.size(); i++){
if(prog[i].label.type != EMPTY){
if(symtab.find(prog[i].label.ident) != symtab.end()){
printf("Line %d, redifinition of label %s\n",i + 1, prog[i].label.ident.c_str());
exit(1);
}
symtab.insert(pair<string, int>(prog[i].label.ident, i));
}
}
}
string int_to_bin(int n){
string res;
while(n!=0) {res=(n%2==0 ?"0":"1")+res; n/=2;}
return res;
}
string hex_to_bin(string s){
string res = "";
for(size_t i = 0; i < s.size(); i++){
switch(s[i]){
case '0':
res += "0000";
break;
case '1':
res += "0001";
break;
case '2':
res += "0010";
break;
case '3':
res += "0011";
break;
case '4':
res += "0100";
break;
case '5':
res += "0101";
break;
case '6':
res += "0110";
break;
case '7':
res += "0111";
break;
case '8':
res += "1000";
break;
case '9':
res += "1001";
break;
case 'a':
res += "1010";
break;
case 'b':
res += "1011";
break;
case 'c':
res += "1100";
break;
case 'd':
res += "1101";
break;
case 'e':
res += "1110";
break;
case 'f':
res += "1111";
break;
default:
printf("Error hex value,got %s\n", s.c_str());
exit(1);
}
}
return res;
}
//build instruction table
map<string,string> insttab;
void build_insttab(){
insttab.insert(pair<string, string>("NOP", "0000"));
insttab.insert(pair<string, string>("BRA", "0001"));
insttab.insert(pair<string, string>("LD", "0010"));
insttab.insert(pair<string, string>("STR", "0011"));
insttab.insert(pair<string, string>("ADD", "0100"));
insttab.insert(pair<string, string>("MUL", "0101"));
insttab.insert(pair<string, string>("CMP", "0110"));
insttab.insert(pair<string, string>("SHRA", "0111"));
insttab.insert(pair<string, string>("SHL", "1000"));
insttab.insert(pair<string, string>("SHR", "1001"));
insttab.insert(pair<string, string>("AND", "1010"));
insttab.insert(pair<string, string>("OR", "1011"));
insttab.insert(pair<string, string>("XOR", "1100"));
insttab.insert(pair<string, string>("HLT", "1101"));
insttab.insert(pair<string, string>("SUB", "1110"));
}
//build condition table
map<string, string> condtab;
void build_condtab(){
//insert condition mapping here
condtab.insert(pair<string, string>("ALW", "0000"));
condtab.insert(pair<string, string>("CCC", "0001"));
condtab.insert(pair<string, string>("CCE", "0010"));
condtab.insert(pair<string, string>("CCP", "0011"));
condtab.insert(pair<string, string>("CCZ", "0100"));
condtab.insert(pair<string, string>("CCN", "0101"));
}
//parse reg identifier
//Return register number if success, -1 otherwise
int parse_reg_id(string s) {
if(s.size() <= 1 || s[0] != 'R')
return -1;
int res = 0;
for(size_t i = 1; i < s.size(); i++){
res *= 10;
res += s[i] - '0';
}
if(res > 15)
return -1;
return res;
}
//Map program to machine code
//Branching condition have special reserved word
//ZERO, EVEN, PARITY, NEGATIVE, ALWAYS
//These five condition check must be specified in the specs
//More clarifications needed
vector<string> machine_code;
void asm_to_machine(){
build_symtab();
build_condtab();
build_insttab();
for(size_t i = 0; i < prog.size(); i++){
string res;
//Labeled memory
if(prog[i].op.type == EMPTY && prog[i].dst.type == HEX_T){
res = hex_to_bin(prog[i].dst.hex.c_str());
machine_code.push_back(res);
continue;
}
//Translate opcode
map<string, string>::iterator ops = insttab.find(prog[i].op.ident);
if(ops != insttab.end()){
res += ops->second;
}else{
goto NO_OP;
}
//NOP HLT
if(prog[i].op.ident == "NOP" || prog[i].op.ident == "HLT"){
res += "1111111111111111111111111111";
machine_code.push_back(res);
continue;
}
//BRA
if(prog[i].op.ident == "BRA"){
map<string, string>::iterator conds = condtab.find(prog[i].src.ident);
if(conds == condtab.end()){
printf("Line %d, Error condition code\n", i + 1);
exit(1);
}
if(prog[i].dst.type != IDENT_T){
printf("Line %d, Branch destination must be a memory location\n", i + 1);
exit(1);
}
map<string, int>::iterator dst = symtab.find(prog[i].dst.ident);
if(dst == symtab.end()){
printf("Line %d, Unknown label\n", i + 1);
exit(1);
}
//27~24
res += conds->second;
//src
res += "000000000000";
//dst
res += bitset<12>(dst->second).to_string();
machine_code.push_back(res);
continue;
}
//ADD, MUL, CMP, AND, OR, XOR ==> reg, src
//Source can be immediate or register
if (prog[i].op.ident == "ADD" ||
prog[i].op.ident == "MUL" ||
prog[i].op.ident == "CMP" ||
prog[i].op.ident == "AND" ||
prog[i].op.ident == "OR" ||
prog[i].op.ident == "XOR" ||
prog[i].op.ident == "SUB") {
//27 ~ 24
//determine source type
if(prog[i].src.type == IMM_T){
res += "1000";
}else if(prog[i].src.type == IDENT_T){
res += "0000";
}else{
printf("Line %d, Source must be immediate or register\n", i + 1);
exit(1);
}
//src
if(prog[i].src.type == IMM_T){
//Immediate
res += bitset<12>(prog[i].src.imm).to_string();
}else{
int reg = parse_reg_id(prog[i].src.ident);
if(reg == -1){
printf("Line %d, Error source register\n", i + 1);
exit(1);
}
res += bitset<12>(reg).to_string();
}
//dst
if(prog[i].dst.type != IDENT_T){
printf("Line %d, Error destination, must be a resigter\n", i + 1);
exit(1);
}else{
int reg = parse_reg_id(prog[i].dst.ident);
if(reg == -1){
printf("Line %d, Error destination register\n", i + 1);
exit(1);
}
res += bitset<12>(reg).to_string();
}
machine_code.push_back(res);
continue;
}
//SHL, SHRA, SHR
if (prog[i].op.ident == "SHRA" ||
prog[i].op.ident == "SHL" ||
prog[i].op.ident == "SHR"){
//27 ~ 24
if(prog[i].src.type == IMM_T){
res += "1000";
}else if(prog[i].src.type == IDENT_T){
res += "0000";
}else{
printf("Line %d, cnt must be immediate or register\n", i + 1);
exit(1);
}
//src
if(prog[i].src.type == IMM_T){
if(prog[i].src.imm >= 32)
printf("Warning: shift count larger than 32 is unreasonable\n");
//Immediate
res += bitset<12>(prog[i].src.imm).to_string();
}else{
int reg = parse_reg_id(prog[i].src.ident);
if(reg == -1){
printf("Line %d, Error source register\n", i + 1);
exit(1);
}
res += bitset<12>(reg).to_string();
}
//dst
if(prog[i].dst.type != IDENT_T){
printf("Line %d, Error destination, must be a resigter\n", i + 1);
exit(1);
}else{
int reg = parse_reg_id(prog[i].dst.ident);
if(reg == -1){
printf("Line %d, Error destination register\n", i + 1);
exit(1);
}
res += bitset<12>(reg).to_string();
}
machine_code.push_back(res);
continue;
}
//Load source of LD must be immediate or memory
if(prog[i].op.ident == "LD"){
//27 ~ 24
if(prog[i].src.type == IMM_T){
res += "1000";
}else if(prog[i].src.type == IDENT_T){
res += "0000";
}else{
printf("Line %d, src must be immediate or memory\n", i + 1);
exit(1);
}
//Semantic check
if(prog[i].src.type != IDENT_T && prog[i].src.type != IMM_T){
printf("Line %d, Load destination must be a memory location or immediate\n", i + 1);
exit(1);
}
map<string, int>::iterator src_loc = symtab.find(prog[i].src.ident);
if(prog[i].src.type == IDENT_T && src_loc == symtab.end()){
printf("Line %d, Load: Unknown memory label\n", i + 1);
exit(1);
}
//src
if(prog[i].src.type == IMM_T){
//Immediate
res += bitset<12>(prog[i].src.imm).to_string();
}else{
res += bitset<12>(src_loc->second).to_string();
}
//dst
if(prog[i].dst.type != IDENT_T){
printf("Line %d, Error destination, must be a resigter\n", i + 1);
exit(1);
}else{
int reg = parse_reg_id(prog[i].dst.ident);
if(reg == -1){
printf("Line %d, Error destination register\n", i + 1);
exit(1);
}
res += bitset<12>(reg).to_string();
}
machine_code.push_back(res);
continue;
}
if(prog[i].op.ident == "STR"){
//27 ~ 24
int dst_flag = -1; //0 for direct, 1 for indirect
int indirect_reg_num = -1;
map<string, int>::iterator dst_loc = symtab.find(prog[i].dst.ident);
if(prog[i].src.type == IDENT_T){
res += "0";
}else{
printf("Line %d, STR source error\n", i + 1);
exit(1);
}
//Semantic check
if(prog[i].dst.type == IDENT_T && (indirect_reg_num = parse_reg_id(prog[i].dst.ident)) >= 0){
res += "100";
dst_flag = 1;
}else if(dst_loc != symtab.end()){
res += "000";
dst_flag = 0;
}else{
printf("Line %d, STR destination error\n", i + 1);
}
//src
if(prog[i].src.type != IDENT_T){
printf("Line %d, Error source, must be a resigter\n", i + 1);
exit(1);
}else{
int reg = parse_reg_id(prog[i].src.ident);
if(reg == -1){
printf("Line %d, Error soruce register\n", i + 1);
exit(1);
}
res += bitset<12>(reg).to_string();
}
//dst
if(dst_flag){
res += bitset<12>(indirect_reg_num).to_string();
}else
res += bitset<12>(dst_loc->second).to_string();
machine_code.push_back(res);
continue;
}
//Store Like load but opposite
NO_OP:
res = "Instruction not yet implemented";
machine_code.push_back(res);
}
}
void print_machine_code(){
for(size_t i = 0; i < machine_code.size(); i++){
printf("%.4d\t", i);
string s = machine_code[i];
string res = s.substr(0, 4) + "_" + s.substr(4, 4) + "_" + s.substr(8, 4) + "_" + s.substr(12, 4);
res += "_" + s.substr(16, 4) + "_" + s.substr(20, 4) + "_" + s.substr(24, 4) + "_" + s.substr(28, 4);
printf("%s\t\t//", res.c_str());
print_prog_line(i, 0);
}
cout << endl;
//print label location
for(auto it: symtab){
printf("%s: %.4d\n", it.first.c_str(), it.second);
}
}
bool init_outfile(string s){
outfile.open(s);
if(!outfile.is_open()){
return false;
}
return true;
}
void write_out(){
for(size_t i = 0; i < machine_code.size(); i++){
outfile << machine_code[i] << endl;
}
}