-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
536 lines (492 loc) · 13.7 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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "parser.h"
char *jump_str2bin(char *jmp_str) {
if (!jmp_str || strlen(jmp_str) == 0) {
return NULL_J;
}
else if (strcmp(jmp_str, "JGT") == 0) {
return JGT;
}
else if (strcmp(jmp_str, "JEQ") == 0) {
return JEQ;
}
else if (strcmp(jmp_str, "JGE") == 0) {
return JGE;
}
else if (strcmp(jmp_str, "JLT") == 0) {
return JLT;
}
else if (strcmp(jmp_str, "JNE") == 0) {
return JNE;
}
else if (strcmp(jmp_str, "JLE") == 0) {
return JLE;
}
else if (strcmp(jmp_str, "JMP") == 0) {
return JMP;
}
return NULL;
}
char *dest_str2bin(char *dest_str) {
if (!dest_str || strlen(dest_str) == 0) {
return NULL_DEST;
}
else if (strcmp(dest_str, "M") == 0) {
return M_DEST;
}
else if (strcmp(dest_str, "D") == 0) {
return D_DEST;
}
else if (strcmp(dest_str, "MD") == 0) {
return MD_DEST;
}
else if (strcmp(dest_str, "A") == 0) {
return A_DEST;
}
else if (strcmp(dest_str, "AM") == 0) {
return AM_DEST;
}
else if (strcmp(dest_str, "AD") == 0) {
return AD_DEST;
}
else if (strcmp(dest_str, "AMD") == 0) {
return AMD_DEST;
}
return NULL;
}
char *comp_str2bin(char *comp_str) {
if (!comp_str) {
return NULL;
}
else if (strcmp(comp_str, "0") == 0) {
return ZERO;
}
else if (strcmp(comp_str, "1") == 0) {
return ONE;
}
else if (strcmp(comp_str, "-1") == 0) {
return NEG_ONE;
}
else if (strcmp(comp_str, "D") == 0) {
return D;
}
else if (strcmp(comp_str, "A") == 0) {
return A;
}
else if (strcmp(comp_str, "!D") == 0) {
return NOT_D;
}
else if (strcmp(comp_str, "!A") == 0) {
return NOT_A;
}
else if (strcmp(comp_str, "-D") == 0) {
return MINUS_D;
}
else if (strcmp(comp_str, "-A") == 0) {
return MINUS_A;
}
else if (strcmp(comp_str, "D+1") == 0) {
return D_PLUS_1;
}
else if (strcmp(comp_str, "A+1") == 0) {
return A_PLUS_1;
}
else if (strcmp(comp_str, "D-1") == 0) {
return D_MINUS_1;
}
else if (strcmp(comp_str, "A-1") == 0) {
return A_MINUS_1;
}
else if (strcmp(comp_str, "D+A") == 0) {
return D_PLUS_A;
}
else if (strcmp(comp_str, "D-A") == 0) {
return D_MINUS_A;
}
else if (strcmp(comp_str, "A-D") == 0) {
return A_MINUS_D;
}
else if (strcmp(comp_str, "D&A") == 0) {
return D_AND_A;
}
else if (strcmp(comp_str, "D|A") == 0) {
return D_OR_A;
}
//a = 1
else if (strcmp(comp_str, "M") == 0) {
return M;
}
else if (strcmp(comp_str, "!M") == 0) {
return NOT_M;
}
else if (strcmp(comp_str, "-M") == 0) {
return MINUS_M;
}
else if (strcmp(comp_str, "M+1") == 0) {
return M_PLUS_1;
}
else if (strcmp(comp_str, "M-1") == 0) {
return M_MINUS_1;
}
else if (strcmp(comp_str, "D+M") == 0) {
return D_PLUS_M;
}
else if (strcmp(comp_str, "D-M") == 0) {
return D_MINUS_M;
}
else if (strcmp(comp_str, "M-D") == 0) {
return M_MINUS_D;
}
else if (strcmp(comp_str, "D&M") == 0) {
return D_AND_M;
}
else if (strcmp(comp_str, "D|M") == 0) {
return D_OR_M;
}
return NULL;
}
char *get_addr_bin(struct symbol_entry *table, char *str, const int new_val) {
int i;
int val;
if (!str) {
fprintf(stderr, "get_addr_bin: address is NULL\n");
return NULL;
}
if (isdigit(str[0])) {
//check if valid decimal address
for (i = 0; i < strlen(str); i++) {
if (!isdigit(str[i])) {
fprintf(stderr, "In line: %s\nNot a valid numeric address\n",
str);
return NULL;
}
}
val = strtol(str, NULL, 10);
}
else if (isalpha(str[0])) {
//check if symbol exists in the symbol table
if ((val = find_symbol(table, str)) < 0) {
//enter symbol to the table
if (!insert_to_table(table, str, new_val)) {
return NULL;
}
val = find_symbol(table, str);
}
}
if (val >= 0) {
return dec_to_bin(val);
}
return NULL;
}
int parse_address_asm(const char *instr_str, char *symbol, int *is_label) {
if (!instr_str || symbol == NULL) {
return 0;
}
int i, len = 0;
for (i = 0; i < strlen(instr_str); i++) {
if (instr_str[i] == '\r' || instr_str[i] == '\n')
{
break;
}
else if (instr_str[i] == ')') {
*is_label = 1;
break;
}
else if (instr_str[i] == '(' || instr_str[i] == ' ') {
continue;
}
//printf("char: %c\n", instr_str[i]);
symbol[len] = instr_str[i];
len++;
}
symbol[len] = '\0';
if (len == 0) {
fprintf(stderr, "Error: Need a valid symbol or address name\n");
}
return len;
}
struct instruct_st *parse_instruction_a(struct symbol_entry *table,
struct instruct_st *instruct,
const char *instr_str)
{
char symbol[SYMBOL_SIZE];
char *addr;
int is_label = 0;
if (parse_address_asm(instr_str, symbol, &is_label) == 0) {
return NULL;
}
if ( ! (addr = get_addr_bin(table, symbol, -1)) ) {
return NULL;
}
instruct->type = A_INSTR;
instruct->header_bin = A_HEADER;
instruct->blob->blob_a.is_label = is_label;
strncpy(instruct->blob->blob_a.address, addr, 15);
instruct->blob->blob_a.address[15] = '\0';
return instruct;
}
char *tokenize_instruction(char *instr_str,
int dest_pos,
int comp_pos,
int jump_pos)
{
if (!instr_str || !(*instr_str)) {
fprintf(stderr, "tokenize_instruction: Failed to tokenize. " \
"Instruction is NULL\n");
return NULL;
}
instr_str[dest_pos] = '\0';
instr_str[comp_pos] = '\0';
instr_str[jump_pos] = '\0';
return instr_str;
}
struct instruct_st *parse_instruction_c(struct symbol_entry *table,
struct instruct_st *instruct,
const char *instr_str_orig)
{
int i = 0;
int dest_end = 0, comp_end = 0, jump_end = 0;
int dest_start = -1, comp_start = -1, jump_start = -1, tmp_start = -1;
int state = 0; //0: dest 1: comp 2: jump
char instr_str[INSTR_SIZE];
char *jump_bin;
char *dest_bin;
char *comp_bin;
if (!instr_str_orig) {
return NULL;
}
strncpy(instr_str, instr_str_orig, strlen(instr_str_orig) % INSTR_SIZE);
instr_str[strlen(instr_str_orig)] = '\0';
for (i = 0; i < strlen(instr_str); i++) {
switch(state) {
case 0:
if (tmp_start < 0 && isalnum(instr_str[i])) {
tmp_start = i;
break;
}
else if (instr_str[i] == '=') {
dest_end = i;
dest_start = tmp_start;
comp_start = i + 1;
state++; //search for computation instruction
break;
}
else if (instr_str[i] == '/' || instr_str[i] == ' ') {
return NULL;
}
//no break
case 1:
if (instr_str[i] == ';') {
comp_end = i;
state = 2; //search for JUMP instruction
jump_start = i + 1;
if (dest_start < 0) {
comp_start = tmp_start;
}
break;
}
else if (instr_str[i] == '\r' || instr_str[i] == '\n' ||
instr_str[i] == '/' || instr_str[i] == ' ')
{
comp_end = i;
i = strlen(instr_str);
break;
}
case 2:
if (instr_str[i] == '\r' || instr_str[i] == '\n' ||
instr_str[i] == ' ')
{
jump_end = i;
i = strlen(instr_str);
}
break;
default:
//do nothing
break;
}
}
if (comp_end == 0) {
return NULL;
}
if (dest_end < 1) {
dest_end = strlen(instr_str);
dest_start = dest_end;
}
if (jump_start < 1) {
jump_end = strlen(instr_str);
jump_start = jump_end;
}
tokenize_instruction(instr_str, dest_end, comp_end, jump_end);
if ( !(dest_bin = dest_str2bin(instr_str+dest_start)) ) {
fprintf(stderr, "parse_instruction_c: destination token is invalid\n");
return NULL;
}
if ( !(comp_bin = comp_str2bin(instr_str+comp_start)) ) {
fprintf(stderr, "parse_instruction_c: computation token is invalid\n");
return NULL;
}
if ( !(jump_bin = jump_str2bin(instr_str+jump_start)) ) {
fprintf(stderr, "parse_instruction_c: jump token is invalid\n");
return NULL;
}
instruct->type = C_INSTR;
instruct->header_bin = C_HEADER;
strncpy(instruct->blob->blob_c.dest_bin, dest_bin, DEST_SIZE);
instruct->blob->blob_c.dest_bin[DEST_SIZE] = '\0';
//+1 for the a-bit
strncpy(instruct->blob->blob_c.comp_bin, comp_bin, COMP_SIZE + 1);
instruct->blob->blob_c.comp_bin[COMP_SIZE + 1] = '\0';
strncpy(instruct->blob->blob_c.jump_bin, jump_bin, JUMP_SIZE);
instruct->blob->blob_c.jump_bin[COMP_SIZE] = '\0';
return instruct;
}
struct instruct_st *parse_instruction(struct symbol_entry *table,
struct instruct_st *instruct,
const char *instr_str) {
int i = 0;
int start = 0;//keeps track of the first position of the token being parsed
enum instr_type type = INVALID;
//find what type of instruction
for (i = 0; i < strlen(instr_str); i++) {
if (instr_str[i] == ' ') {
continue;
}
else if (instr_str[i] == '@' || instr_str[i] == '(') {
start = i + 1;
type = A_INSTR;
break;
}
else if (isalnum(instr_str[i])) {
start = i;
type = C_INSTR;
break;
}
else if (instr_str[i] == '/') {
return NULL;
}
}
/*
move pointer to the first non-blank or the first character of the
instruction/symbol
*/
instr_str += start;
if (type == A_INSTR) {
instruct = parse_instruction_a(table, instruct, instr_str);
}
else if (type == C_INSTR) {
instruct = parse_instruction_c(table, instruct, instr_str);
}
else {
return NULL;
}
return instruct;
}
void print_instruct_st(struct instruct_st *instruct) {
if (!instruct) {
fprintf(stderr, "print_instruct_st: NULL instruct\n");
return;
}
//printf("Instruction type : %d\n", instruct->type);
printf("Instruction header : %s\n", instruct->header_bin);
if (instruct->type == A_INSTR && instruct->blob) {
printf("Instruction Address: %s\n", instruct->blob->blob_a.address);
}
else if (instruct->type == C_INSTR && instruct->blob) {
printf("COMP: %s\n", instruct->blob->blob_c.comp_bin);
printf("DEST: %s\n", instruct->blob->blob_c.dest_bin);
printf("JUMP: %s\n", instruct->blob->blob_c.jump_bin);
}
}
void destroy_instruct_st(struct instruct_st *instruct) {
if (!instruct) {
return;
}
if (instruct->blob) {
free(instruct->blob);
}
free(instruct);
}
char *assemble_instruct(struct instruct_st *instruct, char *instruct_bin) {
if (!instruct || !instruct_bin) {
return NULL;
}
if (instruct->type == A_INSTR && instruct->blob &&
instruct->blob->blob_a.is_label == 0)
{
snprintf(instruct_bin, BUS_SIZE + 1,
"%s%s", instruct->header_bin, instruct->blob->blob_a.address
);
}
else if (instruct->type == C_INSTR) {
snprintf(instruct_bin,
C_HEADER_SIZE + DEST_SIZE + COMP_SIZE + 1 + JUMP_SIZE + 1,
"%s%s%s%s",
instruct->header_bin, instruct->blob->blob_c.comp_bin,
instruct->blob->blob_c.dest_bin, instruct->blob->blob_c.jump_bin
);
}
else {
return NULL;
}
return instruct_bin;
}
void destroy_instructions(struct instruct_bin_entry *instr) {
if (!instr) {
return;
}
struct instruct_bin_entry *next = instr->next;
free(instr);
destroy_instructions(next);
}
struct instruct_bin_entry *parse_instructions(struct symbol_entry *table,
FILE *fp)
{
struct instruct_bin_entry * instr_list = NULL;
struct instruct_bin_entry * curr_instr = NULL;
struct instruct_st instruct_info;
char instruct_bin[BUS_SIZE + 1];
char line[INSTR_SIZE];
if ( ! ( instruct_info.blob = malloc(sizeof(union instruct_blob)) ) ) {
free(instr_list);
perror("malloc");
return NULL;
}
while (fgets(line, sizeof(line), fp)) {
if ((parse_instruction(table, &instruct_info, line))) {
if (! (assemble_instruct(&instruct_info, instruct_bin)) ) {
continue;
}
if (!instr_list) {
if (! (
instr_list = malloc(sizeof(struct instruct_bin_entry))
) )
{
perror("malloc");
return NULL;
}
curr_instr = instr_list;
strncpy(curr_instr->instr, instruct_bin, BUS_SIZE);
curr_instr->instr[BUS_SIZE] = '\0';
continue;
}
if ( ! (
curr_instr->next = malloc(sizeof(struct instruct_bin_entry))
) )
{
perror("malloc");
destroy_instructions(instr_list);
free(instruct_info.blob);
return NULL;
}
strncpy(curr_instr->next->instr, instruct_bin, BUS_SIZE);
curr_instr->next->instr[BUS_SIZE] = '\0';
curr_instr = curr_instr->next;
curr_instr->next = NULL;
}
}
free(instruct_info.blob);
return instr_list;
}