-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.h
634 lines (593 loc) · 16.2 KB
/
node.h
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
630
631
632
633
634
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define UNDEFINED_TYPE 0
#define INT_TYPE 1
#define FLOAT_TYPE 2
#define STRING_TYPE 3
#define MAX_STRING_LENGTH 20
#define MULTI 4
#define PLUS_TYPE 5
#define MINUS_TYPE 6
#define DIVIDE_TYPE 7
#define MULTIPLY_TYPE 8
#define EQUAL_TYPE 9
#define READ_TYPE 10
#define WRITE_TYPE 11
#define FUNC_TYPE 12
#define EQ_TYPE 13
#define GT_TYPE 14
#define LT_TYPE 15
#define GE_TYPE 16
#define LE_TYPE 17
#define TRUE_TYPE 18
#define FALSE_TYPE 19
#define NE_TYPE 20
#define IF_BLOCK 0
#define WHILE_BLOCK 1
#define NODE_EXPR 1
#define NODE_VAR 2
#define NODE_LIT 3
typedef struct Sym_node {
char * name;
int fp_offset;
int int_val;
char * string_val;
float float_val;
int type;
struct Sym_node * next;
} Sym_node;
typedef struct Stack {
char * name;
Sym_node * node;
struct Stack * next;
} Stack;
typedef struct AST_node {
char * name;
Sym_node * pointer;
int asttype;
struct AST_node * left;
struct AST_node * right;
} AST_node;
Stack * stack_head = NULL;
Sym_node * sym_table = NULL;
Sym_node * curr_var_list = NULL;
Stack * curr_stack = NULL;
Stack * temp_head = NULL;
Stack * curr_label = NULL;
char * curr_name = NULL;
int max_label = 1;
int count = 0;
char * err_var = NULL;
int var_count = 0;
int fp_arg = 0;
int fp_local = 0;
Sym_node * main_arg = NULL;
Sym_node * put_string(char * var_name, char * string_val);
Sym_node * duplicate_check(Sym_node * head, char * name);
Sym_node * put_var(Sym_node * head, char* var_name, int type);
void print_var_list(Sym_node * head);
Sym_node * vartype_decl(Sym_node * head, int var_type);
void free_list(Sym_node * head);
Sym_node * new_var(char * var_name, int type);
Stack * build_stack(Stack * head, Sym_node * table, char * name);
void print_stack(Stack * head);
Stack * head_stack(Stack * head, Sym_node * table, char * name);
Stack * connect(Stack * head, Stack * temphead);
AST_node * print_post_tree(AST_node * tree);
AST_node * AST_node_make(char * name, Sym_node * ptr, int type, AST_node * left, AST_node * right);
Stack * pop_stack(Stack * head);
Sym_node * check_stack(Stack * head, char * name);
void print_var_node(Sym_node * head);
void print_cond(AST_node * tree, int type, int label_count);
int stack_local_count(Stack * head, char * name);
void print_Sym_node_back(Sym_node * ptr);
Sym_node * duplicate_check(Sym_node * head, char * name) {
Sym_node * ptr = head;
while (ptr != NULL) {
if (strcmp(ptr->name, name) == 0) {
err_var = malloc(strlen(name) + 1);
strcpy(err_var, name);
return(ptr);
}
ptr = ptr->next;
}
return (0);
}
Sym_node * put_string(char * var_name, char * string_val) {
Sym_node * ptr = (Sym_node *) malloc(sizeof(Sym_node));
ptr->name = (char *) malloc(strlen(var_name) + 1);
strcpy(ptr->name, var_name);
ptr->type = STRING_TYPE;
ptr->string_val = (char *) malloc(strlen(string_val) + 1);
strcpy(ptr->string_val, string_val);
return (ptr);
}
void print_var_list(Sym_node * head) {
Sym_node * ptr = head;
while (ptr != NULL) {
if (ptr->type == INT_TYPE) {
//printf("name %s type INT\n", ptr-> name);
printf("var %s\n", ptr-> name);
}
else if (ptr->type == FLOAT_TYPE) {
//printf("name %s type FLOAT\n", ptr->name);
printf("var %s\n", ptr-> name);
}
else if (ptr->type == STRING_TYPE){
//printf("name %s type STRING value %s\n", ptr-> name, ptr->string_val);
printf("str %s %s\n", ptr-> name, ptr->string_val);
}
else {
printf("name %s, undefined type no value\n", ptr->name);
}
ptr = ptr->next;
}
}
void free_list(Sym_node * head) {
Sym_node * ptr = head;
if (ptr != NULL) {
if (ptr->next != NULL) {
free_list(ptr->next);
}
free(ptr);
}
}
Sym_node * vartype_decl(Sym_node * head, int var_type) {
Sym_node * ptr = head;
while (ptr != NULL) {
ptr->type = var_type;
ptr = ptr->next;
}
return(head);
}
Sym_node * append_list(Sym_node * head, Sym_node * tail) {
Sym_node * ptr = head;
if (ptr == NULL) {
return(tail);
}
while (ptr != NULL) {
Sym_node * check = duplicate_check(tail, ptr->name);
if (check != NULL) {
return NULL;
}
ptr = ptr->next;
}
ptr = head;
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = tail;
return(head);
}
Sym_node * put_var(Sym_node * head, char* var_name, int type) {
Sym_node * ptr = (Sym_node*) malloc(sizeof(Sym_node));
ptr->name = (char *) malloc(strlen(var_name) + 1);
strcpy(ptr->name, var_name);
ptr->type = type;
ptr->next = head;
return(ptr);
}
Sym_node * new_var(char * var_name, int type) {
Sym_node * ptr = (Sym_node*) malloc(sizeof(Sym_node));
ptr->name = (char *) malloc(strlen(var_name) + 1);
strcpy(ptr->name, var_name);
ptr->type = type;
ptr->next = NULL;
return(ptr);
}
Stack * build_stack(Stack * head, Sym_node * table, char * name) {
Stack * ptr = head;
if (head == NULL) {
Stack * ptr = (Stack *) malloc(sizeof(Stack));
ptr->name = (char *) malloc(sizeof(name) + 1);
strcpy(ptr->name, name);
ptr->node = table;
return(ptr);
}
else {
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = build_stack(NULL, table, name);
}
return(head);
}
Stack * head_stack(Stack * head, Sym_node * table, char * name) {
Stack * ptr = (Stack *) malloc(sizeof(Stack));
ptr->name = (char *) malloc(sizeof(name) + 1);
strcpy(ptr->name, name);
ptr->node = table;
ptr->next = head;
return(ptr);
}
Stack * connect(Stack * head, Stack * temphead) {
Stack * ptr = head;
if (ptr == NULL) {
return(temphead);
}
else {
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = temphead;
return(head);
}
}
void print_stack(Stack * head) {
/*if (err_var != NULL) {
printf("DECLARATION ERROR %s\n", err_var);
return;
}*/
Stack * ptr = head;
int track = 1;
int count = 0;
while (ptr != NULL) {
print_var_list(ptr->node);
ptr = ptr->next;
}
}
Stack * pop_stack(Stack * head) {
if (head == NULL) {
return(NULL);
}
Stack * ptr = head->next;
head->next = NULL;
return (ptr);
}
AST_node * AST_node_make(char * name, Sym_node * ptr, int type, AST_node * left, AST_node * right) {
AST_node * node = (AST_node *) malloc(sizeof(AST_node));
node->name = name;
node->pointer = ptr;
node->asttype = type;
node->left = left;
node->right = right;
return(node);
}
void print_cond(AST_node * tree, int type, int label_count) {
if (tree == NULL) {
return;
}
AST_node * left = print_post_tree(tree->left);
AST_node * right = print_post_tree(tree->right);
char temp[MAX_STRING_LENGTH];
char * temp_var = NULL;
if (tree->asttype == TRUE_TYPE) {
return;
} else if (tree->asttype == FALSE_TYPE) {
if (type == WHILE_BLOCK){
printf("jmp WHILE_END_%d\n", label_count+1);
} else if (type == IF_BLOCK) {
printf("jmp ELSE_%d\n", label_count);
}
return;
}
if (left->pointer->type == INT_TYPE) {
printf("move %s r%d\n", right->name, var_count % 4);
printf("cmpi %s r%d\n", left->name, var_count % 4);
} else {
printf("move %s r%d\n", right->name, var_count % 4);
printf("cmpr %s r%d\n", left->name, var_count % 4);
}
var_count++;
switch(tree->asttype){
case GT_TYPE:
if (type == WHILE_BLOCK){
printf("jle WHILE_END_%d\n", label_count+1);
} else if (type == IF_BLOCK) {
printf("jle ELSE_%d\n", label_count);
}
break;
case LT_TYPE:
if (type == WHILE_BLOCK){
printf("jge WHILE_END_%d\n", label_count+1);
} else if (type == IF_BLOCK) {
printf("jge ELSE_%d\n", label_count);
}
break;
case GE_TYPE:
if (type == WHILE_BLOCK){
printf("jlt WHILE_END_%d\n", label_count+1);
} else if (type == IF_BLOCK) {
printf("jlt ELSE_%d\n", label_count);
}
break;
case LE_TYPE:
if (type == WHILE_BLOCK){
printf("jgt WHILE_END_%d\n", label_count+1);
} else if (type == IF_BLOCK) {
printf("jgt ELSE_%d\n", label_count);
}
break;
case EQ_TYPE:
if (type == WHILE_BLOCK){
printf("jne WHILE_END_%d\n", label_count+1);
} else if (type == IF_BLOCK) {
printf("jne ELSE_%d\n", label_count);
}
break;
case NE_TYPE:
if (type == WHILE_BLOCK){
printf("jeq WHILE_END_%d\n", label_count+1);
} else if (type == IF_BLOCK) {
printf("jeq ELSE_%d\n", label_count);
}
break;
}
}
AST_node * print_post_tree(AST_node * tree) {
if (tree == NULL) {
return (NULL);
}
AST_node * left = print_post_tree(tree->left);
AST_node * right = print_post_tree(tree->right);
char temp[MAX_STRING_LENGTH];
char * temp_var = NULL;
Sym_node * ptr = tree->pointer;
//printf("%s\n", tree->name);
switch (tree->asttype)
{
case INT_TYPE:
//printf("int type, %d\n", tree->pointer->int_val);
//print_var_node(tree->pointer);
if (!strcmp("LITERAL", ptr->name)){
sprintf(temp, "r%d", var_count++ % 4);
temp_var = strdup(temp);
//printf(";STOREI %d %s\n", ptr->int_val, temp_var);
//here is the assembly im making below---------------
printf("move %d %s\n", ptr->int_val, temp_var);
//ptr->name = temp_var;
tree->name = temp_var;
return(tree);
} else if (ptr->fp_offset) {
sprintf(temp, "$%d", ptr->fp_offset);
temp_var = strdup(temp);
//ptr->name = temp_var;
tree->name = temp_var;
return(tree);
} else {
return(tree);
}
break;
case FLOAT_TYPE:
//printf("float type, %f\n", tree->pointer->float_val);
//print_var_node(tree->pointer);
if (!strcmp("LITERAL", ptr->name)){
sprintf(temp, "r%d", var_count++ % 4);
temp_var = strdup(temp);
//printf(";STOREF %f %s\n", ptr->float_val, temp_var);
//assembly--------------------------------
printf("move %f %s\n", ptr->float_val, temp_var);
//ptr->name = temp_var;
tree->name = temp_var;
return(tree);
} else if (ptr->fp_offset) {
sprintf(temp, "$%d", ptr->fp_offset);
temp_var = strdup(temp);
//ptr->name = temp_var;
tree->name = temp_var;
return(tree);
} else {
return(tree);
}
break;
case STRING_TYPE:
printf("string type, %s\n", tree->pointer->string_val);
print_var_node(tree->pointer);
return(tree);
break;
case PLUS_TYPE:
//printf("plus type\n");
//print_var_node(tree->pointer);
sprintf(temp, "r%d", var_count++ % 4);
temp_var = strdup(temp);
if (left->pointer->type == INT_TYPE) {
ptr = new_var(temp_var, INT_TYPE);
//printf(";ADDI %s %s %s\n", left->name,right->name, temp_var);
//assembly------------------------------------------------
printf("move %s %s\n", left->name, temp_var);
printf("addi %s %s\n", right->name, temp_var);
} else {
ptr = new_var(temp_var, FLOAT_TYPE);
//printf(";ADDF %s %s %s\n", left->name,right->name, temp_var);
//assembly------------------------------------------------
printf("move %s %s\n", left->name, temp_var);
printf("addr %s %s\n", right->name, temp_var);
}
//ptr->name = temp_var;
tree->pointer = ptr;
tree->name = temp_var;
return(tree);
break;
case MINUS_TYPE:
//printf("minus type\n");
//print_var_node(tree->pointer);
sprintf(temp, "r%d", var_count++ % 4);
temp_var = strdup(temp);
if (left->pointer->type == INT_TYPE) {
ptr = new_var(temp_var, INT_TYPE);
//printf(";SUBI %s %s %s\n", left->name,right->name, temp_var);
//assembly------------------------------------------------
printf("move %s %s\n", left->name, temp_var);
printf("subi %s %s\n", right->name, temp_var);
} else {
ptr = new_var(temp_var, FLOAT_TYPE);
//printf(";SUBF %s %s %s\n", left->name,right->name, temp_var);
//assembly------------------------------------------------
printf("move %s %s\n", left->name, temp_var);
printf("subr %s %s\n", right->name, temp_var);
}
//ptr->name = temp_var;
tree->pointer = ptr;
tree->name = temp_var;
return(tree);
break;
case DIVIDE_TYPE:
//printf("divide type\n");
//print_var_node(tree->pointer);
sprintf(temp, "r%d", var_count++ % 4);
temp_var = strdup(temp);
if (left->pointer->type == INT_TYPE) {
ptr = new_var(temp_var, INT_TYPE);
//printf(";DIVI %s %s %s\n", left->name,right->name, temp_var);
//assembly------------------------------------------------
printf("move %s %s\n", left->name, temp_var);
printf("divi %s %s\n", right->name, temp_var);
} else {
ptr = new_var(temp_var, FLOAT_TYPE);
//printf(";DIVF %s %s %s\n", left->name,right->name, temp_var);
//assembly------------------------------------------------
printf("move %s %s\n", left->name, temp_var);
printf("divr %s %s\n", right->name, temp_var);
}
//ptr->name = temp_var;
tree->name = temp_var;
tree->pointer = ptr;
return(tree);
break;
case MULTIPLY_TYPE:
//printf("multiply type\n");
//print_var_node(tree->pointer);
sprintf(temp, "r%d", var_count++ % 4);
temp_var = strdup(temp);
if (left->pointer->type == INT_TYPE) {
ptr = new_var(temp_var, INT_TYPE);
//printf(";MULI %s %s %s\n", left->name,right->name, temp_var);
//assembly------------------------------------------------
printf("move %s %s\n", left->name, temp_var);
printf("muli %s %s\n", right->name, temp_var);
} else {
ptr = new_var(temp_var, FLOAT_TYPE);
//printf(";MULF %s %s %s\n", left->name,right->name, temp_var);
//assembly------------------------------------------------
printf("move %s %s\n", left->name, temp_var);
printf("mulr %s %s\n", right->name, temp_var);
}
//ptr->name = temp_var;
tree->name = temp_var;
tree->pointer = ptr;
return(tree);
break;
case EQUAL_TYPE:
//printf("equal type\n");
//print_var_node(tree->pointer);
sprintf(temp, "r%d", var_count++ % 4);
temp_var = strdup(temp);
if (left->pointer->type == INT_TYPE) {
//printf(";STOREI %s %s\n", right->name,left->name);
ptr = new_var(temp_var, INT_TYPE);
printf("move %s %s\n", right->name, temp_var);
printf("move %s %s\n", temp_var, left->name);
} else {
//printf(";STOREF %s %s\n", right->name,left->name);
ptr = new_var(temp_var, FLOAT_TYPE);
printf("move %s %s\n", right->name, temp_var);
printf("move %s %s\n", temp_var, left->name);
}
tree->pointer = ptr;
return(tree);
break;
case READ_TYPE:
//printf("read type\n");
//print_var_list(tree->pointer);
while (ptr!= NULL) {
Sym_node * node = check_stack(curr_stack, ptr->name);
if (node->fp_offset) {
sprintf(temp, "$%d", node->fp_offset);
temp_var = strdup(temp);
} else {
temp_var = ptr->name;
}
if (node->type == INT_TYPE) {
//printf(";READI %s\n", ptr->name);
printf("sys readi %s\n", temp_var);
} else if (node->type == FLOAT_TYPE){
//printf(";READF %s\n", ptr_name);
printf("sys readr %s\n", temp_var);
} else {
//printf(";READS %s\n", ptr->name);
printf("sys readr %s\n", temp_var);
}
ptr = ptr->next;
}
break;
case WRITE_TYPE:
//printf("write type\n");
//print_var_list(tree->pointer);
while (ptr!= NULL) {
Sym_node * node = check_stack(curr_stack, ptr->name);
if (node->fp_offset) {
sprintf(temp, "$%d", node->fp_offset);
temp_var = strdup(temp);
} else {
temp_var = ptr->name;
}
if (node->type == INT_TYPE) {
//printf(";WRITEI %s\n", ptr->name);
printf("sys writei %s\n", temp_var);
} else if (node->type == FLOAT_TYPE) {
//printf(";WRITEF %s\n", ptr->name);
printf("sys writer %s\n", temp_var);
} else {
//printf(";WRITES %s\n", ptr->name);
printf("sys writes %s\n", temp_var);
}
ptr = ptr->next;
}
break;
}
return(NULL);
}
Sym_node * check_stack(Stack * head, char * name) {
Stack * ptr = head;
Sym_node * track = duplicate_check(head->node, name);
if (track != 0) {
return track;
}
else if (ptr->next != NULL) {
check_stack(ptr->next, name);
}
}
void print_var_node(Sym_node * head) {
Sym_node * ptr = head;
if (ptr != NULL) {
if (ptr->type == INT_TYPE) {
//printf("name %s type INT\n", ptr-> name);
printf("var %s\n", ptr-> name);
}
else if (ptr->type == FLOAT_TYPE) {
printf("var %s\n", ptr->name);
}
else if (ptr->type == STRING_TYPE){
printf("str %s %s\n", ptr-> name, ptr->string_val);
}
else {
printf("name %s, Undefined type no value \n");
}
ptr = ptr->next;
}
}
int stack_local_count(Stack * head, char * name) {
int output = 0;
Stack * ptr = head;
while (ptr != NULL) {
Sym_node * sym_track = ptr->node;
if (!strcmp(ptr->name,name)) {
while (sym_track != NULL) {
if (sym_track->fp_offset < 0) {
output = output + 1;
}
sym_track = sym_track->next;
}
}
ptr = ptr->next;
}
return output;
}
void print_Sym_node_back(Sym_node * ptr){
if (ptr == NULL) {
return;
}
print_Sym_node_back(ptr->next);
printf("push %s\n", ptr->name);
}