/
parser.c
629 lines (568 loc) · 15.1 KB
/
parser.c
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
619
620
621
622
623
624
625
626
627
628
629
//#include "parser.h"
#include "definitions.h"
#include "dictionary.h"
#include <math.h>
static char *previouslabel = NULL; // global variable, useful for instructions after a (valid) label.
static node *node_mod;
static
void add_node_parser(list *L, ASMinstr value, dictionary *D) // a hack due to bad design!
{
node_mod = add_node_and_get_address(L, value);
fprintf(stderr, "%d ", node_mod);
/*using a global variable; the following line will do nothing if previouslabel == NULL */
add_to_dict(D, hash(previouslabel), node_mod);
/* free(previouslabel); */ // DO NOT free previouslabel's space; dict only has a
// pointer to that space, not a copy!
previouslabel = NULL; // add it only once!
}
/*
static
bool strIncludedInArray(char * string, char ** strArray)
{
for(int i=0; i < strlen(strArray); i++)
{
if (strcmp(string, strArray[i]) == 0)
return true;
}
return false;
}
*/
static
bool charIncludedInString(char c, char * string)
{
for(uint_fast16_t i=0; i<strlen(string); i++)
{
if(c == string[i])
return true;
}
return false;
}
char *xstrtok(char *line, char *delims)
{
//Custom strtok function. Improvements:
//1. Identifies consecutive delimiters
//2. Does not modify the original string
//3.
static char *saveline = NULL;
int n;
if(line != NULL)
{
saveline = xmalloc(sizeof(char*));
strcpy(saveline, line);
int i = 0;
while(charIncludedInString(saveline[i++], delims));
saveline += i-1;
}
/*
*see if we have reached the end of the line
*/
if(saveline == NULL || *saveline == '\0')
return(NULL);
/*
*return the number of characters that aren't delims
*/
n = strcspn(saveline, delims);
//p = saveline; /*save start of this token*/
char p[n+1];
strncpy(p, saveline, n);
p[n] = '\0';
saveline += n; /*bump past the delim*/
if(*saveline != '\0') /*trash the delim if necessary*/
*saveline++ = '\0';
return(p);
}
int xatoi(char token[])
{
int i = 1;
int zerosInBegin = 0;
int result = 0;
while(token[zerosInBegin] == '0')
{
zerosInBegin++;
}
i += zerosInBegin;
while(token[i]>='0' && token[i]<='9')
{
result += (token[i] - '0') * ((int) pow(10, i-1-zerosInBegin));
i++;
}
if(token[i] != '\0' && (token[i]<'0' || token[i]>'9'))
result = -1;
return result;
}
/*
static
long xatoiImm(char* token)
{
long returnLong;
if(token[0] == '-')
{
returnLong = - atoi(token + 1);
}
else
returnLong = atoi(token);
return returnLong;
}
*/
int strToRegNum(char* token)
{
int regNum = -1;
char * sentToken = malloc(sizeof(char*));
strcpy(sentToken, token);
int regType = token[0];
switch (regType)
{
case 'r':
case 'R':
regNum = xatoi(token);
break;
case 'f':
case 'F':
regNum = num_of_int_registers + xatoi(token);
break;
default:
//error(E_CMD_REG_INV, ACTION_PRINTMSG);
break;
}
return regNum;
}
static
int strToRRegNum(char* token)
{
int returnReg = strToRegNum(token);
//if(returnReg >= num_of_int_registers)
// returnReg -= num_of_int_registers;
return returnReg;
}
static
int strToFRegNum(char* token)
{
int returnReg = strToRegNum(token);
if(returnReg <= num_of_int_registers)
returnReg += num_of_int_registers;
return returnReg;
}
static
void parseRinstr(char* commandLine, dictionary *D)
{
Rinstr cmd;
cmd.cmd = getCmdEnum(xstrtok(commandLine, cmd_delim));
char * token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.dstReg = strToFRegNum(token);
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.src1Reg = strToFRegNum(token);
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.src2Reg = strToFRegNum(token);
if(cmd.src1Reg < num_of_int_registers || cmd.src2Reg < num_of_int_registers)
{
fprintf(stderr, "%s: ", commandLine);
fprintf(stderr, "Register invalid, not included in the FPR\n");
}
else
{
//TODO Put cmd in list
ASMinstr a;
a.type = RTYPE;
a.isBreakpoint = false;
a.instr.r = cmd;
add_node_parser(L1, a, D);
fprintf(stdout, "R-type command: %-6.6s %6d %6d %6d\n", getCmdStr(cmd.cmd), cmd.dstReg, cmd.src1Reg, cmd.src2Reg);
}
}
static
void parseRRinstr(char* commandLine, dictionary *D)
{
Rinstr cmd;
cmd.cmd = getCmdEnum(xstrtok(commandLine, cmd_delim));
char * token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.dstReg = strToRRegNum(token);
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.src1Reg = strToRRegNum(token);
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.src2Reg = strToRRegNum(token);
if(cmd.src1Reg == -1 || cmd.src2Reg == -1 || cmd.dstReg == -1)
{
fprintf(stderr, "%s: ", commandLine);
fprintf(stderr, "Register invalid, not included in the FPR\n");
}
else
{
//TODO Put cmd in list
ASMinstr a;
a.type = RTYPE;
a.isBreakpoint = false;
a.instr.r = cmd;
add_node_parser(L1, a, D);
fprintf(stdout, "R-type command: %-6.6s %6d %6d %6d\n", getCmdStr(cmd.cmd), cmd.dstReg, cmd.src1Reg, cmd.src2Reg);
}
}
static
void parseIinstr(char* commandLine, dictionary *D)
{
Iinstr cmd;
cmd.cmd = getCmdEnum(xstrtok(commandLine, cmd_delim));
char * token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.dstReg = strToRRegNum(token);
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.srcReg = strToRRegNum(token);
if(cmd.srcReg == -1 || cmd.dstReg == -1)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_REG_INV, ACTION_PRINTMSG);
return;
}
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.immediate = atoi(token);
if(cmd.immediate == 0)
{
fprintf(stderr, "%s: ", commandLine);
fprintf(stderr, "Cannot parse the given string to int value\n");
return;
}
ASMinstr a;
a.type = ITYPE;
a.isBreakpoint = false;
a.instr.i = cmd;
add_node_parser(L1, a, D);
fprintf(stdout, "I-type command: %-6.6s %6d %6d %6d\n", getCmdStr(cmd.cmd), cmd.dstReg, cmd.srcReg, cmd.immediate);
}
static
void parseBinstr(char* commandLine, dictionary *D)
{
Binstr cmd;
cmd.cmd = getCmdEnum(xstrtok(commandLine, cmd_delim));
char * token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.dstReg = strToRRegNum(token);
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
//cmd.br.label = malloc(sizeof(uint64_t));
cmd.br.label = hash(strcat(token, ":"));
//xstrcpy(cmd.br.label, token);
//for(uint_fast16_t i=0; i<strlen(token); i++)
// cmd.br.label[i] = token[i];
//strncpy(cmd.br.label, token, strlen(token));
//TODO Put cmd in list
ASMinstr a;
a.type = BTYPE;
a.isBreakpoint = false;
a.instr.b = cmd;
add_node_parser(L1, a, D);
fprintf(stdout, "B-type command: %-6.6s %6d %lu\n", getCmdStr(cmd.cmd), cmd.dstReg, cmd.br.label);
add_node(L2, a); // also adding to the list of the 2nd pass
}
static
void parseSDinstr(char * commandLine, dictionary *D)
{
Iinstr cmd;
cmd.cmd = getCmdEnum(xstrtok(commandLine, cmd_delim));
char * token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.immediate = atoi(token);
if(cmd.immediate == 0)
{
fprintf(stderr, "%s: ", commandLine);
error(E_ATOI, ACTION_PRINTMSG);
return;
}
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.srcReg = strToRRegNum(token);
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.dstReg = strToFRegNum(token);
ASMinstr a;
a.type = ITYPE;
a.isBreakpoint = false;
a.instr.i = cmd;
add_node_parser(L1, a, D);
fprintf(stdout, "I-type command: %-6.6s %6d %6d %6d\n", getCmdStr(cmd.cmd), cmd.immediate, cmd.srcReg, cmd.dstReg);
}
static
void parseLDinstr(char * commandLine, dictionary *D)
{
Iinstr cmd;
cmd.cmd = getCmdEnum(xstrtok(commandLine, cmd_delim));
char * token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.dstReg = strToFRegNum(token);
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.immediate = atoi(token);
if(cmd.immediate == 0)
{
fprintf(stderr, "%s: ", commandLine);
error(E_ATOI, ACTION_PRINTMSG);
return;
}
token = xstrtok(NULL, cmd_delim);
if(token == NULL)
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
return;
}
cmd.srcReg = strToRRegNum(token);
ASMinstr a;
a.type = ITYPE;
a.isBreakpoint = false;
a.instr.i = cmd;
add_node_parser(L1, a, D);
fprintf(stdout, "I-type command: %-6.6s %6d %6d %6d\n", getCmdStr(cmd.cmd), cmd.dstReg, cmd.immediate, cmd.srcReg);
}
static
void splitCommandLine(char* commandLine, int currentLine, dictionary *D)
{
//ASMinstr cmd;
static consecutive_labels = 0;
char * token = xstrtok(commandLine, cmd_delim);
//if (strIncludedInArray(token, RtypeCommands))
if (!strcmp(token, "breakpoint"))
{
ASMinstr a;
a.type = BREAKPOINT;
a.isBreakpoint = true;
add_node(L1, a);
/* ?!?!?! MAYBE add_node(L1, a); is better
than add_node_parser(L1, a, D) for breakpoints; */
//add_node_parser(L1, a, D);
fprintf(stderr, "Found breakpoint: %s\n", commandLine);
//When there is a breakpoint in a line, anything following will be considered as comment
return;
//token = xstrtok(NULL, cmd_delim);
//if (!xstrcmp(token, "arloumpes"))
// return;
}
if (isRFinstr(token))
{
//cmd.type = RTYPE;
//cmd.instr.r = parseRinstr(commandLine);
parseRinstr(commandLine, D);
}
//else if (strIncludedInArray(token, ItypeCommands))
else if (isIinstr(token))
{
//cmd.type = ITYPE;
//cmd.instr.i = parseIinstr(commandLine);
parseIinstr(commandLine, D);
}
else if (isBinstr(token))
{
parseBinstr(commandLine, D);
}
else if (isSDinstr(token))
parseSDinstr(commandLine, D);
else if (isLDinstr(token))
parseLDinstr(commandLine, D);
else if (isRRinstr(token))
parseRRinstr(commandLine, D);
else
{
if(token[strlen(token)-1] == ':')
{
//token = strncpy(token, token, strlen(token)-2);
//token[strlen(token)+1] = '\0';
fprintf(stdout, "New Label: %s\n", token); //TODO Put label in dict
/*
if (previouslabel != NULL) //|| isFirstLabel
{
//isFirstLabel = false;
add_to_dict(D, hash(token), node_mod);
consecutive_labels++;
}
else
while(consecutive_labels > 0)
{
D->e[D->entries-consecutive_labels].goto_node = D->e[D->entries].goto_node;
consecutive_labels--;
}
*/
previouslabel = (char *)malloc(sizeof(char *));
strcpy(previouslabel, token);
token = xstrtok(commandLine, cmd_delim);
char lampel[MAX_LABEL_LEN];
strcpy(lampel, token);
if(token[0] == '0' && token[1] == 'x')
{
long hexLabel = 0;
hexLabel = strtol(lampel, NULL, 16);
if(hexLabel > 0 && hexLabel < 65535)
fprintf(stderr, "The label is in hexadecimal format and within the byte addressing range.\n");
}
strcpy(lampel, token);
}
else
{
fprintf(stderr, "%s: ", commandLine);
error(E_CMD_INVALID, ACTION_PRINTMSG);
}
}
}
static
list *first_pass(FILE *asmfile, dictionary *D)
{
char commandLine[COMMAND_LINE_BUFFER];
int c;
int charsRead;
int currentLine = 0;
do
{
charsRead = 0;
currentLine++;
c = fgetc(asmfile);
commandLine[0] = '\0';
while(c != '\n' && c != '#' && c != EOF)
{
if (charsRead++ > COMMAND_LINE_BUFFER)
{
fprintf(stderr, "%s : ", commandLine);
error(E_CMDLINE_BUF, ACTION_PRINTMSG);
break;
}
commandLine[charsRead-1] = c;
commandLine[charsRead] = '\0';
c = fgetc(asmfile);
}
if (commandLine[0] != '\0')
{
splitCommandLine(commandLine, currentLine, D);
}
if (c == '#')
while ((c = fgetc(asmfile))!= '\n' && c != EOF);
}while(c != EOF);
return L1;
}
static
void second_pass(list *L, dictionary *Dict) /** DEFINITELY NOT READY! **/
{
if(L->head == NULL){
error(E_NO_BRANCHES, ACTION_PRINTMSG);
return;
}
//assert(); // I forgot what I wanted to assert here, but it was important!
/**/
for(node *n = L2->head; n != NULL; n = n->next){
assert (n->instr.type == BTYPE);
/* which one of the following 2 lines will we implement? (I guess the first one.)
For now, assign both: */
node * retrieved_node = retrieve_from(Dict, n->instr.instr.b.br.label);
if (retrieved_node == NULL)
error(E_INV_BR, ACTION_PRINTMSG);
n->branch = retrieved_node;
n->instr.instr.b.br.branchp = retrieved_node;
}
/**/
}
list *parse_file_to_list(FILE *file)
{
L1 = create_list();
dictionary *D = create_dict(MAX_LABEL_NUM);
/*
list *L2 = first_pass(L1, D, file); // 1.parse all instructions, remove junk, keep labels to
// a dictionary and save branching instructions to L2
second_pass(L2, D); // 2.parse branching instructions to fill their pointers
assert(L1->size == sublist_size(L1->head));
*/
/* Much uglier alternative due to static list declarations (It even ignores the list
"returned" by the first_pass() function and obscures the control flow): */
L2 = create_list();
first_pass(file, D);
second_pass(L2, D);
//Print dictionary
fprintf(stdout, "\n\nPrinting Labels from the dictionary.\n");
fprintf(stdout, "There are %d entries:\n", D->entries);
for(int i = 0; i < D->entries; ++i)
fprintf(stdout, "Label: Hash Code %20lu. Next instruction @%d %5s %2d\n", D->e[i].hashcode, D->e[i].goto_node, getCmdStr(D->e[i].goto_node->instr.instr.r.cmd), D->e[i].goto_node->instr.instr.r.dstReg);
free(D);
fprintf(stderr,"\n\nStarting the issuing of commands:");
return L1;
}