/
quad.c
877 lines (831 loc) · 23.1 KB
/
quad.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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
#include "quad.h"
int functionCount = 0;
int blockCount = 1;
int tempCount = 1;
int stringCount = 1;
block* currentBlock = NULL;
qnode* loop_break = NULL;
qnode* loop_continue = NULL;
char* opcodeText[] = { //opcode strings used by print function
"MOV",
"LOAD",
"LEA",
"STOR",
"CMP",
"BR",
"BRGT",
"BRGE",
"BRLT",
"BRLE",
"BREQ",
"BRNE",
"INC",
"DEC",
"MUL",
"DIV",
"MOD",
"ADD",
"SUB",
"SHL",
"SHR",
"AND",
"XOR",
"IOR",
"NOT",
"LOGAND",
"LOGOR",
"LOGNOT",
"CALL",
"ARGNUM",
"ARGDEF",
"RETURN"
};
//create a new basic block
block* bb_newBlock(int funcID, int id, block* prev) {
block* b = malloc(sizeof(block));
if(!b) {
fprintf(stderr, "Unable to allocate memory for basic block\n");
return NULL;
}
b->prev = prev;
if(prev) {
prev->next = b;
}
b->funcID = funcID;
b->blockID = id;
b->top = NULL;
b->bottom = NULL;
b->branch1 = NULL;
b->branch2 = NULL;
b->br_type = BR_DEFAULT;
b->name = malloc(10);
sprintf(b->name, ".BB.%i.%i", b->funcID, b->blockID);
return b;
}
//Add the quad to the specified basic block
block* bb_appendQuad(block* b, quad* q) {
if(b->top && b->bottom) {
quad* temp = b->bottom;
temp->next = q;
b->bottom = q;
} else if(!b->top && !b->bottom) {
b->top = q;
b->bottom = q;
} else {
fprintf(stderr, "Error (bb_append): Inconsistent Basic Block\n");
}
}
//Create new quad node
qnode* qnode_new(qnodeType type) {
qnode* q = malloc(sizeof(qnode));
if(!q){
fprintf(stderr, "Unable to allocate memory for quad node\n");
return NULL;
}
q->type = type;
q->name = malloc(256);
q->pos = NULL;
return q;
}
quad* emit(opcode op, qnode* dest, qnode* src1, qnode* src2) {
//printf("EMIT\n");
quad *q = malloc(sizeof(quad));
if(!q){
fprintf(stderr, "Unable to allocate memory for quad\n");
return NULL;
}
q->op = op;
q->dest = dest;
q->source1 = src1;
q->source2 = src2;
bb_appendQuad(currentBlock, q);
return q;
}
block* function_block(node* body, int print) {
blockCount = 1;
currentBlock = bb_newBlock(++functionCount, blockCount, currentBlock);
block* tmp = currentBlock;
stmt_list_parse(body);
if(!currentBlock->bottom || currentBlock->bottom->op != O_RETURN) { //default return
emit(O_RETURN, NULL, NULL, NULL);
}
if(print)
print_blocks(tmp);
return tmp;
}
void stmt_list_parse(node* list) {
while(list) {
if(list && list->type == LIST_NODE) {
node* n = list->u.list.start;
if(n->type == ASSIGN_NODE) {
//printf("generating assignment quad in BB %s\n", currentBlock->name);
gen_assign(n);
} else if(n->type == BINOP_NODE) {
gen_rvalue(n, NULL);
} else if(n->type == UNOP_NODE) {
gen_rvalue(n, NULL);
} else if(n->type == IF_NODE || n->type == IFELSE_NODE) {
gen_if(n);
} else if(n->type == FOR_NODE) {
gen_for(n);
} else if(n->type == WHILE_NODE) {
gen_while(n);
} else if(n->type == JUMP_NODE) {
gen_jmp(n);
} else if(n->type == CALL_NODE) { //Function Call
int argnum = n->u.call.argnum;
qnode* num = qnode_new(Q_CONSTANT);
num->name = malloc(100);
sprintf(num->name, "%i", argnum);
num->u.value = argnum;
emit(O_ARGNUM, NULL, num, NULL);
if(argnum) { //Generate argument quad
node* argptr = n->u.call.args;
while(argptr) {
emit(O_ARGDEF, NULL, gen_rvalue(argptr->u.list.start, NULL), NULL);
if(argptr->next) {
argptr = argptr->next;
} else {
break;
}
}
}
emit(O_CALL, NULL, gen_rvalue(n->u.call.function, NULL), NULL);
} else if(n->type == IDENT_NODE || n->type == DEFAULT_NODE) {
//do nothing
} else {
fprintf(stderr, "QUAD ERROR: Unrecognized Node Type %i\n", n->type);
exit(1);
}
if(list->next) {
list = list->next;
} else {
break;
}
} else {
fprintf(stderr, "QUAD ERROR: Invalid Statement List\n");
break;
}
}
}
qnode* gen_rvalue(node* n, qnode* target) {
switch(n->type) {
case IDENT_NODE: {
qnode* q = qnode_new(Q_IDENT);
q->name = n->u.ident.id;
q->u.ast = n;
q->pos = &(n->u.ident.pos);
if(n->next->type == SCALAR_NODE || n->next->type == POINTER_NODE) {
if(target && target->type == Q_IDENT) {
if(target->u.ast->next->type == SCALAR_NODE) {
emit(O_MOV, target, q, NULL);
}
}
return q;
} else if(n->next->type == ARRAY_NODE) {
//qnode* dest = new_temp();
//emit(O_LEA, dest, q, NULL);
if(!target) target = new_temp();
emit(O_LEA, target, q, NULL);
return target;
} else if(n->next->type == FUNCTION_NODE) {
return q;
} else {
fprintf(stderr, "RVAL Unimplemented IDENT\n");
}
break;
}
case NUMBER_NODE: {
qnode* q = qnode_new(Q_CONSTANT);
sprintf(q->name, "%lli", n->u.number.value);
q->u.value = n->u.number.value;
if(target && target->type == Q_IDENT) {
if(target->u.ast->next->type == SCALAR_NODE) {
emit(O_MOV, target, q, NULL);
}
}
return q;
break;
}
case UNOP_NODE: {
switch(n->u.unop.type) {
case DEREF_OP: {
//because of the AST structure, this is a bit messy
// if(n->u.unop.operand->type == BINOP_NODE && n->u.unop.operand->u.binop.type == PLUS_OP) {
// node* l = n->u.unop.operand->u.binop.left;
// if(l->type == IDENT_NODE && l->next && l->next->type == ARRAY_NODE) {
// return gen_rvalue(n->u.unop.operand, target);
// }
// }
qnode* addr = gen_rvalue(n->u.unop.operand, NULL);
if(!target) target = new_temp();
emit(O_LOAD, target, addr, NULL);
return target;
}
case POSTINC_OP: {
qnode* q = gen_rvalue(n->u.unop.operand, NULL);
if(!target) target = new_temp();
emit(O_MOV, target, q, NULL);
emit(O_INC, NULL, q, NULL);
return target;
}
case POSTDEC_OP: {
qnode* q = gen_rvalue(n->u.unop.operand, NULL);
if(!target) target = new_temp();
emit(O_MOV, target, q, NULL);
emit(O_DEC, NULL, q, NULL);
return target;
}
case PREINC_OP: {
qnode* q = gen_rvalue(n->u.unop.operand, NULL);
emit(O_INC, NULL, q, NULL);
return q;
}
case PREDEC_OP: {
qnode* q = gen_rvalue(n->u.unop.operand, NULL);
emit(O_DEC, NULL, q, NULL);
return q;
}
case LOGNOT_OP: {
block* bt = bb_newBlock(functionCount, ++blockCount, currentBlock);
block* bf = bb_newBlock(functionCount, ++blockCount, bt);
block* bn = bb_newBlock(functionCount, ++blockCount, bf);
if(!target) target = new_temp();
qnode* zero = qnode_new(Q_CONSTANT);
zero->name = strdup("0");
zero->u.value = 0;
qnode* one = qnode_new(Q_CONSTANT);
one->name = strdup("1");
one->u.value = 1;
gen_cond(n->u.unop.operand, blockToQnode(bt), blockToQnode(bf));
currentBlock->next = bt; //reset next block in case it has changed
bt->prev = currentBlock;
currentBlock = bt;
emit(O_MOV, target, zero, NULL);
emit(O_BR, NULL, blockToQnode(bn), NULL);
currentBlock = bf;
emit(O_MOV, target, one, NULL);
emit(O_BR, NULL, blockToQnode(bn), NULL);
currentBlock = bn;
return target;
}
case ADDR_OP: {
if(!target) target = new_temp();
emit(O_LEA, target, gen_rvalue(n->u.unop.operand, NULL), NULL);
return target;
}
case NEG_OP: {
if(!target) target = new_temp();
qnode* val = gen_rvalue(n->u.unop.operand, NULL);
qnode* one = qnode_new(Q_CONSTANT);
one->name = strdup("-1");
one->u.value = -1;
emit(O_MUL, target, one, val);
return target;
}
case POS_OP: {
return gen_rvalue(n->u.unop.operand, NULL); //not much to do...
}
case SIZEOF_OP: {
int size = 0;
if(n->u.unop.operand->type == IDENT_NODE) {
if(n->u.unop.operand->next->type == ARRAY_NODE) { //compute size
node* a = n->u.unop.operand->next;
size = a->u.array.length;
while(a->next && a->next->type == ARRAY_NODE) {//multidimensional
a = a->next;
size *= a->u.array.length;
}
size *= sizeof(int);
} else { //assume its an int or ptr (same size)
size = sizeof(int);
}
} else if(n->u.unop.operand->type == POINTER_NODE) {
size = sizeof(void*);
} else if(n->u.unop.operand->type == SCALAR_NODE) {
size = sizeof(int); //only supporting ints
} else {
fprintf(stderr, "RVAL unimplemented SIZEOF (type %i)\n", n->u.unop.operand->type);
exit(1);
}
qnode* q = qnode_new(Q_CONSTANT);
sprintf(q->name, "%i", size);
q->u.value = size;
if(target && target->type == Q_IDENT) {
if(target->u.ast->next->type == SCALAR_NODE) {
emit(O_MOV, target, q, NULL);
}
}
return q;
}
default:
fprintf(stderr, "RVAL Unimplemented UNOP %i\n", n->u.unop.type);
}
break;
}
case BINOP_NODE: {
//boolean expression
if((n->u.binop.type >= LT_OP && n->u.binop.type <= NOTEQ_OP) || n->u.binop.type >= LOGOR_OP) {
if(!target) target = new_temp();
block* temp = currentBlock;
block* bt = bb_newBlock(functionCount, ++blockCount, currentBlock);
block* bf = bb_newBlock(functionCount, ++blockCount, bt);
block* bn = bb_newBlock(functionCount, ++blockCount, bf);
//create qnodes for the above blocks
qnode* trueBlock = blockToQnode(bt);
qnode* falseBlock = blockToQnode(bf);
qnode* nextBlock = blockToQnode(bn);
//qnodes for zero and one
qnode* zero = qnode_new(Q_CONSTANT);
zero->name = strdup("0");
zero->u.value = 0;
qnode* one = qnode_new(Q_CONSTANT);
one->name = strdup("1");
one->u.value = 1;
gen_cond(n, trueBlock, falseBlock); //evaluate expression
currentBlock = bt; //true
emit(O_MOV, target, one, NULL);
emit(O_BR, NULL, nextBlock, NULL);
currentBlock = bf; //false
emit(O_MOV, target, zero, NULL);
emit(O_BR, NULL, nextBlock, NULL);
currentBlock = bn;
return target;
} else {
node* bLeft = n->u.binop.left;
qnode* left = gen_rvalue(n->u.binop.left, NULL);
qnode* right = gen_rvalue(n->u.binop.right, NULL);
if(bLeft->type == IDENT_NODE && bLeft->next->type == ARRAY_NODE) {
qnode* size = qnode_new(Q_CONSTANT);
int nextType = bLeft->next->next->type;
if(nextType == SCALAR_NODE) {
size->u.value = (long long) sizeof(int);
} else if(nextType == POINTER_NODE) {
size->u.value = (long long) sizeof(int*);
} else if(nextType == ARRAY_NODE) {
node* arr = bLeft->next->next;
size->u.value = 1;
while(arr) {
size->u.value *= (long long) sizeof(int*)*arr->u.array.length;
if(arr->next && arr->next->type == ARRAY_NODE) {
arr = arr->next;
} else {
break;
}
}
} else {
fprintf(stderr, "QUAD ERROR, unimplemented sizeof\n");
}
qnode* temp = new_temp();
sprintf(size->name, "%li", size->u.value);
emit(O_MUL, temp, right, size);
right = temp;
}
if(!target) target = new_temp();
emit(getBinop(n->u.binop.type), target, left, right);
return target;
}
}
case CALL_NODE: {
if(n->u.call.function) {
int argnum = n->u.call.argnum;
qnode* num = qnode_new(Q_CONSTANT);
num->name = malloc(64);
sprintf(num->name, "%i", argnum);
num->u.value = argnum;
emit(O_ARGNUM, NULL, num, NULL);
if(argnum) { //Generate argument quad
node* argptr = n->u.call.args;
while(argptr) {
emit(O_ARGDEF, NULL, gen_rvalue(argptr->u.list.start, NULL), NULL);
if(argptr->next) {
argptr = argptr->next;
} else {
break;
}
}
}
if(!target) target = new_temp();
emit(O_CALL, target, gen_rvalue(n->u.call.function, NULL), NULL);
return target;
} else {
fprintf(stderr, "Error: Call Node RVALUE\n");
}
break;
}
case STRING_NODE: {
qnode* str = qnode_new(Q_STRING);
str->name = malloc(64);
sprintf(str->name, ".S%i", stringCount++);
str->u.ast = n;
if(target && target->type == Q_IDENT) {
if(target->u.ast->next->type == POINTER_NODE) {
emit(O_MOV, target, str, NULL);
return target;
}
} else {
return str;
}
}
default:
fprintf(stderr, "Error: Cannot generate RVALUE for AST node type %i\n", n->type);
exit(1);
}
return NULL;
}
qnode* gen_lvalue(node* node, int* flag) {
switch(node->type) {
case IDENT_NODE:
if(node->next->type == SCALAR_NODE || node->next->type == POINTER_NODE) {
qnode* q = qnode_new(Q_IDENT);
q->name = node->u.ident.id;
q->u.ast = node;
q->pos = &(node->u.ident.pos);
*flag = 0; //direct assignment
return q;
} else {
fprintf(stderr, "LVAL Unimplemented IDENT->%i \n", node->next->type);
}
break;
case NUMBER_NODE: {
fprintf(stderr, "Error: Scalar type invalid lvalue\n");
return NULL;
}
case UNOP_NODE: {
if(node->u.unop.type == DEREF_OP) {
*flag = 1; //indirect assignment via ptr
return gen_rvalue(node->u.unop.operand, NULL);
} else {
fprintf(stderr,"LVAL Unimplemented UNOP\n");
}
}
default:
fprintf(stderr, "Error: Cannot generate LVALUE for AST node type %i\n", node->type);
exit(1);
}
}
//Generate quads for assignment
qnode* gen_assign(node* a) {
if(a->type == ASSIGN_NODE) {
node* right = a->u.assign.right;
if(a->u.assign.type != EQ_OP) { //convert assignment
node* b = ast_newNode(BINOP_NODE);
b->u.binop.type = assignToBinop(a->u.assign.type);
b->u.binop.left = a->u.assign.left;
b->u.binop.right = right;
right = b;
}
int flag;
qnode* dest = gen_lvalue(a->u.assign.left, &flag);
if(flag == 0) { //direct
//printf("direct assign flag\n");
return gen_rvalue(right, dest);
} else if(flag == 1) { //indirect
qnode* rval = gen_rvalue(right, NULL);
emit(O_STOR, NULL, rval, dest);
return dest;
}
} else {
fprintf(stderr, "QUAD ERROR: Expected assignment node\n");
}
}
//Generate quads for IF statement
void gen_if(node* start) {
//printf("%s\n", "processing IF NODE");
block* bt = bb_newBlock(functionCount, ++blockCount, currentBlock); //true bb
block* bf = bb_newBlock(functionCount, ++blockCount, bt); //false bb
block* bn; //next bb
qnode* trueBlock = blockToQnode(bt);
qnode* falseBlock = blockToQnode(bf);
qnode* nextBlock;
block* temp = currentBlock;
if(start->type == IF_NODE) {
bn = bf;
gen_cond(start->u.if_stmt.condition, trueBlock, falseBlock);
} else if(start->type == IFELSE_NODE) {
bn = bb_newBlock(functionCount, ++blockCount, bf);
gen_cond(start->u.ifelse_stmt.condition, trueBlock, falseBlock);
} else {
fprintf(stderr, "QUAD ERROR: Not an If/else node\n");
return;
}
nextBlock = blockToQnode(bn);
if(currentBlock != temp) {
currentBlock->next = bt;
}
currentBlock = bt; //begin true block
stmt_list_parse(start->u.if_stmt.if_block); //same for both if and ifelse
emit(O_BR, NULL, nextBlock, NULL); //unconditional jump
if(start->type == IFELSE_NODE) {
if(currentBlock != bt) { //additional blocks were added
currentBlock->next = bf;
}
currentBlock = bf; //false (else) block
stmt_list_parse(start->u.ifelse_stmt.else_block);
emit(O_BR, NULL, nextBlock, NULL);
if(currentBlock != bf) { //additional blocks were added
currentBlock->next = bn;
}
} if(currentBlock != bt) { //additional blocks were added
currentBlock->next = bn;
}
//printf("current block %s, setting to bn (%s)\n", currentBlock->name, bn->name);
currentBlock = bn;
}
//Generate quads for WHILE and DO-WHILE loop
void gen_while(node* start) {
//printf("processing WHILE\n");
if(start->type == WHILE_NODE) {
block *cond, *body, *next;
if(start->u.while_stmt.do_stmt) { //do-while loop
body = bb_newBlock(functionCount, ++blockCount, currentBlock);
cond = bb_newBlock(functionCount, ++blockCount, body);
next = bb_newBlock(functionCount, ++blockCount, cond);
} else { //regular while loop
cond = bb_newBlock(functionCount, ++blockCount, currentBlock);
body = bb_newBlock(functionCount, ++blockCount, cond);
next = bb_newBlock(functionCount, ++blockCount, body);
}
qnode* b = blockToQnode(body);
qnode* c = blockToQnode(cond);
qnode* n = blockToQnode(next);
qnode* last_break = loop_break;
loop_break = n;
currentBlock = cond;
gen_cond(start->u.while_stmt.condition, b, n);
if(currentBlock != cond) { //reset block pts if more blocks were added
if(start->u.while_stmt.do_stmt) { //do-while loop
currentBlock->next = next;
next->prev = currentBlock;
} else {
currentBlock->next = body; //while loop
body->prev = currentBlock;
}
}
currentBlock = body;
stmt_list_parse(start->u.while_stmt.body);
if(currentBlock != body) { //more blocks were added
if(start->u.while_stmt.do_stmt) { //do-while loop
currentBlock->next = cond;
} else {
currentBlock->next = next; //while loop
}
}
emit(O_BR, NULL, c, NULL);
currentBlock = next;
loop_break = last_break;
} else {
fprintf(stderr, "QUAD ERROR: Not a while loop\n");
}
}
//Generate quads for FOR loop
void gen_for(node* start) {
if(start->type == FOR_NODE) {
block* init = bb_newBlock(functionCount, ++blockCount, currentBlock);
block* body = bb_newBlock(functionCount, ++blockCount, init);
block* after = bb_newBlock(functionCount, ++blockCount, body);
block* next = bb_newBlock(functionCount, ++blockCount, after);
qnode* b = blockToQnode(body);
qnode* a = blockToQnode(after);
qnode* n = blockToQnode(next);
qnode* last_break = loop_break;
loop_break = n;
currentBlock = init;
stmt_list_parse(start->u.for_stmt.init);
gen_cond(start->u.for_stmt.condition, b, n);
if(currentBlock != init) { //additional blocks added
currentBlock->next = body;
}
currentBlock = body;
stmt_list_parse(start->u.for_stmt.body);
emit(O_BR, NULL, a, NULL);
if(currentBlock != body) { //additional blocks added
currentBlock->next = after;
}
currentBlock = after;
if(start->u.for_stmt.afterthought)
stmt_list_parse(start->u.for_stmt.afterthought);
gen_cond(start->u.for_stmt.condition, b, n);
if(currentBlock != after) { //additional blocks added
currentBlock->next = next;
}
currentBlock = next;
loop_break = last_break;
} else {
fprintf(stderr, "QUAD ERROR: Not a for loop\n");
}
}
//Generate compare and branch quads
void gen_cond(node* expr, qnode* t, qnode* f) {
//printf("Processing conditional\n");
if(expr->type == BINOP_NODE) {
switch(expr->u.binop.type) {
case LT_OP:
emit(O_CMP, NULL, gen_rvalue(expr->u.binop.left, NULL), gen_rvalue(expr->u.binop.right, NULL));
emit(O_BRLT, NULL, t, f);
return;
case GT_OP:
emit(O_CMP, NULL, gen_rvalue(expr->u.binop.left, NULL), gen_rvalue(expr->u.binop.right, NULL));
emit(O_BRGT, NULL, t, f);
return;
case LTEQ_OP:
emit(O_CMP, NULL, gen_rvalue(expr->u.binop.left, NULL), gen_rvalue(expr->u.binop.right, NULL));
emit(O_BRLE, NULL, t, f);
return;
case GTEQ_OP:
emit(O_CMP, NULL, gen_rvalue(expr->u.binop.left, NULL), gen_rvalue(expr->u.binop.right, NULL));
emit(O_BRGE, NULL, t, f);
return;
case EQEQ_OP:
emit(O_CMP, NULL, gen_rvalue(expr->u.binop.left, NULL), gen_rvalue(expr->u.binop.right, NULL));
emit(O_BREQ, NULL, t, f);
return;
case NOTEQ_OP:
emit(O_CMP, NULL, gen_rvalue(expr->u.binop.left, NULL), gen_rvalue(expr->u.binop.right, NULL));
emit(O_BRNE, NULL, t, f);
return;
case LOGOR_OP: { //short circuit OR
block* temp = currentBlock;
block* bi = bb_newBlock(functionCount, ++blockCount, currentBlock); //intermediate block
currentBlock->next = bi;
t->u.block->prev = bi;
bi->next = t->u.block;
bi->prev = currentBlock;
gen_cond(expr->u.binop.left, t, blockToQnode(bi)); //test left expr
if(currentBlock != temp) {
currentBlock->next = bi;
}
currentBlock = bi;
gen_cond(expr->u.binop.right, t, f); //test right expr
//currentBlock = temp;
return;
}
case LOGAND_OP: { //short circuit AND
block* temp = currentBlock;
block* bi = bb_newBlock(functionCount, ++blockCount, currentBlock); //intermediate block
currentBlock->next = bi;
t->u.block->prev = bi;
bi->next = t->u.block;
bi->prev = currentBlock;
gen_cond(expr->u.binop.left, blockToQnode(bi), f); //test left expr
if(currentBlock != temp) {
currentBlock->next = bi;
}
currentBlock = bi;
gen_cond(expr->u.binop.right, t, f); //test right expr
//currentBlock = temp;
return;
}
default: {
break;
}
}
}
//gen_cond for non-conditional expr
qnode* val = gen_rvalue(expr, NULL);
qnode* zero = qnode_new(Q_CONSTANT);
zero->name = strdup("0");
zero->u.value = 0;
emit(O_CMP, NULL, val, zero);
emit(O_BRNE, NULL, t, f);
}
void gen_jmp(node* start) {
if(start->type == JUMP_NODE) {
if(start->u.jump.type == RETURN_JP) {
qnode* val = NULL;
if(start->u.jump.target) {
val = gen_rvalue(start->u.jump.target, NULL);
}
emit(O_RETURN, NULL, val, NULL); //emit return quad
} else if(start->u.jump.type == BREAK_JP) {
if(loop_break) {
emit(O_BR, NULL, loop_break, NULL);
} else {
fprintf(stderr,"Unexpected BREAK\n");
}
} else {
fprintf(stderr, "GOTO and CONTINUE not currently implemented...\n");
}
} else {
fprintf(stderr, "QUAD ERROR: Not a jump statement\n");
}
}
//Traverse BB linked list and print
void print_blocks(block* b) {
while(b) {
printf("\n%s\n", b->name);
quad* q = b->top;
while(q) {
printf("\t");
if(q->dest) {
printf("%s", q->dest->name);
if(q->dest->type == Q_IDENT) {
if(q->dest->u.ast->type == IDENT_NODE) {
if(q->dest->u.ast->u.ident.stor == SG_STATIC) {
printf("<gl>");
} else if(q->dest->u.ast->u.ident.stor == SG_AUTO) {
printf("<at>");
}
} else {
fprintf(stderr, "IDENT quad not linked to IDENT NODE!\n");
}
}
printf(" =\t");
} else {
printf("\t");
}
printf("%s\t", opcodeText[q->op]);
if(q->source1) {
printf("%s\t", q->source1->name);
} else {
printf("\t");
}
if(q->source2) {
printf("%s\n", q->source2->name);
} else {
printf("\n");
}
if(q->next && q != b->bottom) {
q = q->next;
} else {
//printf("end of BB %s\n", b->name);
break;
}
}
if(b->next) {
b = b->next;
} else {
break;
}
}
}
//New temporary register qnode
qnode* new_temp() {
qnode* q = qnode_new(Q_TEMPORARY);
sprintf(q->name, "%%T%i", tempCount);
q->u.tempID = tempCount++;
q->pos = malloc(sizeof(int));
*(q->pos) = -1;
return q;
}
//Convert basic block to quad node
qnode* blockToQnode(block* b) {
qnode* q = qnode_new(Q_LABEL);
q->name = strdup(b->name);
q->u.block = b;
return q;
}
opcode getBinop(binopType binop) {
switch(binop) {
case MULT_OP:
return O_MUL;
case DIV_OP:
return O_DIV;
case MOD_OP:
return O_MOD;
case PLUS_OP:
return O_ADD;
case MINUS_OP:
return O_SUB;
case SHL_OP:
return O_SHL;
case SHR_OP:
return O_SHR;
case AND_OP:
return O_AND;
case XOR_OP:
return O_XOR;
case IOR_OP:
return O_IOR;
case LOGOR_OP:
return O_LOGOR;
case LOGAND_OP:
return O_LOGAND;
}
}
opcode assignToBinop(assignType assign) {
switch(assign) {
case TIMESEQ_OP:
return MULT_OP;
case DIVEQ_OP:
return DIV_OP;
case MODEQ_OP:
return MOD_OP;
case PLUSEQ_OP:
return PLUS_OP;
case MINUSEQ_OP:
return MINUS_OP;
case SHLEQ_OP:
return SHL_OP;
case SHREQ_OP:
return SHR_OP;
case ANDEQ_OP:
return AND_OP;
case OREQ_OP:
return IOR_OP;
case XOREQ_OP:
return O_XOR;
}
}
// int main() {
// }