/
codegen.c
1176 lines (1054 loc) · 35.1 KB
/
codegen.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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-----------------------------------------------------------------------
A code generator for Wiz programs.
For use in the COMP90045 project 2014.
Team: Bandicoot
Members: Wee Lee Tan 377990
Rui Cheng 664000
Ryan Leong 395463
-----------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "ast.h"
#include "std.h"
#include "codegen.h"
#include "symbol.h"
#include "analyze.h"
// Function declarations
Procs sort_procs(Procs);
void print_indent(int);
void print_constant(Constant, int);
void print_write_expr(Expr, int, char*);
void print_write_constant(Constant);
void print_exprs(Exprs, int, char*);
void print_stmt(Stmt, char*);
void print_stmts(Stmts, char*);
void print_procs(Procs);
void print_proc(Proc);
void print_header(Header);
void print_decls(Decls, char*);
void print_decl(Decl, char*);
void print_assign(Assign, char*);
void print_assign_array(Assign, Exprs, char*);
void print_params(Params, char*);
void print_pram(Param,char*);
void print_args(Exprs,int, char*, int,char* );
void print_write(Stmt, char*);
void print_read(Stmt, char*);
void print_fncall(Stmt, char*);
void print_intervals(Intervals);
void print_interval(Interval);
void print_relop_string(int, int, int, int, int);
void print_binop_string(int, int, int, int, int);
void print_unop_string(int, int, int);
void print_read_array(Stmt, char*);
int print_expr(Expr, int, char*);
int print_arg(Expr, int, char*,int,char*);
// Globals
int loopcount = 0;
int ifcount = 0;
void codegen_prog(FILE *fp, Program prog) {
Procs sorted_procs = sort_procs(prog->procs);
printf("call proc_main\n");
printf("halt\n");
printf("\n");
print_procs(sorted_procs);
}
/*
Print all procs
*/
void print_procs(Procs procs) {
Proc first = procs->first;
Procs rest = procs->rest;
// Print all procs
if (first) {
print_proc(first);
printf("\n");
}
if (rest) {
print_procs(rest);
}
}
/*
Print proc prologue, body, and epilogue
*/
void print_proc(Proc proc) {
// Print proc label
printf("proc_%s:", proc->header->id);
int stack_count = getStackSize(proc->header->id);
printf("\n");
// Print prologue comment in output
printf("#prologue\n");
if (proc->decls || proc->header->params) {
// Get the number of declarations.
printf("push_stack_frame %d", stack_count);
printf("\n");
// Print proc parameters
if(proc->header->params)
print_params(proc->header->params,proc->header->id);
// Declare int and real constants
printf("int_const r0, 0\n");
printf("real_const r1, 0.0\n");
printf("\n");
// Print proc declarations
if(proc->decls)
print_decls(proc->decls, proc->header->id);
}
printf("\n");
// Print proc body
if (proc->body) {
print_stmts(proc->body, proc->header->id);
}
// Print epilogue
printf("#epilogue\n");
if (stack_count != 0)
printf("pop_stack_frame %d\n", stack_count);
printf("return\n");
}
/*
Print all declarations
*/
void print_decls(Decls decls, char* proc_id) {
Decl first = decls->first;
Decls rest = decls->rest;
if (first) {
print_decl(first, proc_id);
}
if (rest) {
print_decls(rest, proc_id);
}
}
/*
Print all params of procs
*/
void print_params(Params params, char* procName) {
Params p = params;
int slot;
int i = 0;
while (p)
{
slot = getStackSlotNum(procName, p->first->id);
if (p->first->kind==VAL) {
printf("store %d, r%d\n", slot, i);
}
else if(p->first->kind==REF) {
printf("store %d, r%d\n", slot, i);
}
p = p->rest;
i++;
}
}
/*
Print declarations
*/
void print_decl(Decl decl, char* proc_id) {
int slot;
slot = getStackSlotNum(proc_id, decl->id);
// Print based on type
switch (decl->type) {
case BOOL_TYPE:
printf("store %d, r0", slot);
printf("\n");
break;
case INT_TYPE:
printf("store %d, r0", slot);
printf("\n");
break;
case FLOAT_TYPE:
printf("store %d, r1", slot);
printf("\n");
break;
case INT_ARRAY_TYPE:{
int i;
for (i = 0; i < getArraySize(proc_id, decl->id); i++){
printf("store %d, r0", slot);
printf("\n");
slot++;
}
break;
}
case FLOAT_ARRAY_TYPE:{
int i;
for (i = 0; i < getArraySize(proc_id, decl->id); i++){
printf("store %d, r1", slot);
printf("\n");
slot++;
}
break;
}
case BOOL_ARRAY_TYPE:{
int i;
for (i = 0; i < getArraySize(proc_id, decl->id); i++){
printf("store %d, r0", slot);
printf("\n");
slot++;
}
break;
}
}
}
/*
Print all intervals in array
*/
void print_intervals(Intervals intervals) {
Interval first = intervals->first;
Intervals rest = intervals->rest;
if (first) {
print_interval(first);
if (rest) {
printf(",");
print_intervals(rest);
}
}
}
/*
Print interval in a dimension of an array
*/
void print_interval(Interval interval) {
printf("%d..%d", interval->start, interval->end);
}
/*
Print all statements
*/
void print_stmts(Stmts stmts, char* proc_id) {
Stmt first = stmts->first;
Stmts rest = stmts->rest;
if (first) {
print_stmt(first, proc_id);
}
if (rest) {
print_stmts(rest, proc_id);
}
}
/*
Print Conditions of while or if statements
*/
Type print_cond(Expr expr, char* proc_id, int reg,
int stmt_type, char* label1, char* label2) {
// Print label if while loop
if (stmt_type == 1) {
// Add label to branch to at top of loop
printf("%s:\n", label1);
}
// Print based on expression kind
switch(expr->kind) {
case EXPR_ID: {
Type exprType = getType(proc_id,expr->id);
printf("load r%d, %d\n", reg, getStackSlotNum(proc_id, expr->id));
// Print branch on false based on while or if
if (stmt_type == 0) {
printf("branch_on_false r%d, %s\n", reg, label1);
}
else if (stmt_type == 1) {
// Branch away if false
printf("branch_on_false r%d, %s\n", reg, label2);
}
return exprType;
break;
}
case EXPR_CONST:
printf("int_const r%d, %d\n", reg, expr->constant.val.bool_val);
// Print branch on false based on while or if
if (stmt_type == 0) {
printf("branch_on_false r%d, %s\n", reg, label1);
}
else if (stmt_type == 1) {
// Branch away if false
printf("branch_on_false r%d, %s\n", reg, label2);
}
switch(expr->constant.type){
case INT_CONSTANT:
return INT_TYPE;
break;
case FLOAT_CONSTANT:
return FLOAT_TYPE;
break;
case BOOL_CONSTANT:
return BOOL_TYPE;
break;
}
break;
case EXPR_BINOP: {
// Print expressions in while or if conditions
reg = print_expr(expr->e1, reg, proc_id);
int reg1 = print_expr(expr->e2, reg+1, proc_id);
int ID_type = getExprType(expr->e1, proc_id);
int ID_type2 = getExprType(expr->e2, proc_id);
print_binop_string(expr->binop, reg, reg1, ID_type, ID_type2);
// Print branch on false based on while or if
if (stmt_type == 0) {
printf("branch_on_false r%d, %s\n", reg, label1);
}
else if (stmt_type == 1) {
// Branch away if false
printf("branch_on_false r%d, %s\n", reg, label2);
}
break;
}
case EXPR_RELOP: {
// Print expressions in while or if condition
int reg1 = print_expr(expr->e1, reg, proc_id);
int reg2 = print_expr(expr->e2, reg+1, proc_id);
Type e1Type = getExprType(expr->e1, proc_id);
Type e2Type = getExprType(expr->e2, proc_id);
Type evalType = -1;
// Compare types to see which to use
if (e1Type == INT_TYPE && e2Type == INT_TYPE) {
evalType = INT_TYPE;
}
else if ((e1Type == FLOAT_TYPE && e2Type == FLOAT_TYPE) ||
(e1Type == INT_TYPE && e2Type == FLOAT_TYPE) ||
(e1Type == FLOAT_TYPE && e2Type == INT_TYPE)) {
evalType = FLOAT_TYPE;
}
else if (e1Type == BOOL_TYPE && e2Type == BOOL_TYPE) {
evalType = BOOL_TYPE;
}
// Print comparision of expressions in while or if condition
print_relop_string(expr->relop, reg1, reg2, e1Type, e2Type);
// Print branch on false based on while or if
if (stmt_type == 0) {
printf("branch_on_false r%d, %s\n", reg1, label1);
}
else if (stmt_type == 1) {
// Branch away if false
printf("branch_on_false r%d, %s\n", reg1, label2);
}
break;
}
}
printf("\n");
return -1;
}
/*
Print array assignments
*/
void print_assign_array(Assign assign, Exprs expr, char* proc_id) {
// calculate offset
printf("#assignment\n");
Type ID_type;
ID_type = getExprType(assign.expr, proc_id);
int reg = print_expr(assign.expr, 0, proc_id);
int next_reg = reg+1;
if (ID_type == INT_ARRAY_TYPE || ID_type == FLOAT_ARRAY_TYPE
|| ID_type == BOOL_ARRAY_TYPE){
printf("load_indirect r%d, r%d\n", next_reg, reg);
next_reg = next_reg+1;
}
next_reg = calculate_offset(expr, next_reg, assign.id, proc_id);
printf("store_indirect r%d, r%d\n", next_reg, next_reg-1);
}
/*
Print statments in proc
*/
void print_stmt(Stmt stmt, char* proc_id) {
// Print based on statement kind
switch (stmt->kind) {
case STMT_ASSIGN:
print_assign(stmt->info.assign, proc_id);
break;
case STMT_ASSIGN_ARRAY:
print_assign_array(stmt->info.assign,
stmt->info.assign.exprs, proc_id);
break;
case STMT_COND:{
printf("#if\n");
// Form label
char ifCountStr[15];
char str[80];
if (stmt->info.cond.else_branch != NULL) {
sprintf(ifCountStr, "%d", ifcount);
strcpy (str,"else_");
strcat (str,ifCountStr);
}
else {
sprintf(ifCountStr, "%d", ifcount);
strcpy (str,"end_if_");
strcat (str,ifCountStr);
}
int currentIfCount = ifcount;
ifcount++;
// print conditions of if statment
print_cond(stmt->info.cond.cond, proc_id, 0, 0, str, "");
// Print then statements
print_stmts(stmt->info.cond.then_branch, proc_id);
// Print labels
char endIfLabel[80];
char currCountStr[15];
sprintf(currCountStr, "%d", currentIfCount);
strcpy (endIfLabel,"end_if_");
strcat (endIfLabel,currCountStr);
// Print else statements
if (stmt->info.cond.else_branch != NULL) {
// Branch unconditional
printf("branch_uncond %s\n", endIfLabel);
// Do False
printf("%s:\n", str);
print_stmts(stmt->info.cond.else_branch, proc_id);
}
// print label
printf("%s:\n", endIfLabel);
printf("\n");
break;
}
case STMT_WHILE:{
// Print start of while label
printf("#while\n");
char whileStr[15];
sprintf(whileStr, "%d", loopcount);
char top[80];
strcpy (top,"top_of_loop_");
strcat (top,whileStr);
char loopbreak[80];
strcpy (loopbreak,"loop_is_false_");
strcat (loopbreak,whileStr);
int currentLoopCount = loopcount;
loopcount++;
// print while condition
print_cond(stmt->info.loop.cond, proc_id, 0, 1, top, loopbreak);
// Print statements
print_stmts(stmt->info.loop.body, proc_id);
// Print unconditional branch
printf("branch_uncond %s\n", top);
// Print end loop label
printf("%s:\n", loopbreak);
printf("\n");
break;
}
case STMT_READ:
print_read(stmt, proc_id);
break;
case STMT_READ_ARRAY:
print_read_array(stmt, proc_id);
break;
case STMT_WRITE:
print_write(stmt, proc_id);
break;
case STMT_FNCALL:
print_fncall(stmt, proc_id);
break;
}
}
/*
Non-array assignment
*/
void print_assign(Assign assign, char* proc_id) {
int ID_type;
int expr_type;
int slot;
int reg = 0;
ID_type = getType(proc_id,assign.id);
expr_type = getExprType(assign.expr, proc_id);
if (isRef(proc_id, assign.id)==0){
if (ID_type == 2 && expr_type == 1){
slot = getStackSlotNum(proc_id, assign.id);
printf("#assignment\n");
print_expr(assign.expr, 0, proc_id);
printf("int_to_real r0, r0\n");
printf("store %d, r0\n", slot);
printf("\n");
}
else if ((ID_type == 1 && expr_type == 1) ||
(ID_type == 2 && expr_type == 2) ||
(ID_type == 0 && expr_type == 0)){
slot = getStackSlotNum(proc_id, assign.id);
printf("#assignment\n");
print_expr(assign.expr, 0, proc_id);
printf("store %d, r0\n", slot);
printf("\n");
}
}
else if(isRef(proc_id, assign.id)==1){
if (ID_type == 2 && expr_type == 1){
slot = getStackSlotNum(proc_id, assign.id);
printf("#assignment\n");
print_expr(assign.expr, reg, proc_id);
printf("int_to_real r0, r0\n");
printf("load r1, %d\n", slot);
printf("store_indirect r1, r%d\n", reg);
printf("\n");
}
else if ((ID_type == 1 && expr_type == 1) ||
(ID_type == 2 && expr_type == 2) ||
(ID_type == 0 && expr_type == 0)){
slot = getStackSlotNum(proc_id, assign.id);
printf("#assignment\n");
print_expr(assign.expr, reg, proc_id);
printf("load r1, %d\n", slot);
printf("store_indirect r1, r%d\n", reg);
printf("\n");
}
}
}
/*
Non-array read
*/
void print_read(Stmt stmt, char* proc_id) {
printf("#read");
printf("\n");
//Get the type and stack number from symbol table
Type ID_type;
int stackNo;
ID_type = getType(proc_id,stmt->info.read);
stackNo = getStackSlotNum(proc_id, stmt->info.read);
switch (ID_type){
case BOOL_TYPE:
printf("call_builtin read_bool");
printf("\n");
printf("store %d, r0", stackNo);
printf("\n");
break;
case INT_TYPE:
printf("call_builtin read_int \n"); //with a function
printf("store %d, r0 \n", stackNo);
break;
case FLOAT_TYPE:
printf("call_builtin read_real \n"); //with a function
printf("store %d, r0 \n", stackNo);
break;
}
printf("\n");
}
/* read in array */
void print_read_array(Stmt stmt, char* proc_id) {
printf("# read\n");
Type ID_type;
ID_type = getType(proc_id,stmt->info.array.id);
switch (ID_type){
case BOOL_ARRAY_TYPE:
printf("call_builtin read_bool");
printf("\n");
break;
case INT_ARRAY_TYPE:
printf("call_builtin read_int \n"); //with a function
break;
case FLOAT_ARRAY_TYPE:
printf("call_builtin read_real \n"); //with a function
break;
}
int reg = calculate_offset(stmt->info.array.exprs, 1,
stmt->info.array.id, proc_id);
printf("store_indirect r%d, r0\n", reg);
printf("\n");
}
/* write function */
void print_write(Stmt stmt, char* proc_id) {
printf("# write\n");
print_write_expr(stmt->info.write, 1, proc_id);
printf("\n");
}
void print_write_expr(Expr expr, int depth, char* proc_id) {
switch (expr->kind) {
int ID_type = 0;
int ID_type2 = 0;
int stackNo;
int reg;
int reg1;
case EXPR_ID:
//Get the type and stack number from symbol table
ID_type = getType(proc_id,expr->id);
stackNo = getStackSlotNum(proc_id, expr->id);
printf("load r0, %d\n", stackNo);
switch (ID_type){
case BOOL_TYPE:
printf("\n");
printf("call_builtin print_bool"); //with a function
printf("\n");
break;
case INT_TYPE:
printf("call_builtin print_int \n"); //with a function
break;
case FLOAT_TYPE:
printf("call_builtin print_real \n"); //with a function
break;
}
break;
case EXPR_CONST:
print_write_constant(expr->constant);
break;
case EXPR_BINOP:
reg = print_expr(expr->e1, 0, proc_id);
reg1 = print_expr(expr->e2, 1, proc_id);
ID_type = getExprType(expr->e1, proc_id);
ID_type2 = getExprType(expr->e2, proc_id);
print_binop_string(expr->binop, reg, reg1, ID_type, ID_type2);
if ((ID_type + ID_type2) >= 3){
printf("call_builtin print_real \n");
}
else if ((ID_type + ID_type2) == 2){
printf("call_builtin print_int \n");
}
else{
printf("call_builtin print_bool \n");
}
break;
case EXPR_RELOP:
reg = print_expr(expr->e1, 0, proc_id);
reg1 = print_expr(expr->e2, 1, proc_id);
ID_type = getExprType(expr->e1, proc_id);
ID_type2 = getExprType(expr->e2, proc_id);
print_relop_string(expr->relop, reg, reg1, ID_type, ID_type2);
printf("call_builtin print_bool \n");
break;
case EXPR_UNOP:
reg = print_expr(expr->e1, 0, proc_id);
ID_type = getExprType(expr->e1, proc_id);
print_unop_string(expr->unop, reg, ID_type);
if (ID_type == 2){
printf("call_builtin print_real \n");
}
else if (ID_type == 1 ){
printf("call_builtin print_int \n");
}
else{
printf("call_builtin print_bool \n");
}
break;
case EXPR_ARRAY:
ID_type = getExprType(expr, proc_id);
reg = calculate_offset(expr->es, 1, expr->id, proc_id);
printf("load_indirect r0, r%d\n", reg);
if (ID_type == FLOAT_ARRAY_TYPE){
printf("call_builtin print_real \n");
}
else if (ID_type == INT_ARRAY_TYPE){
printf("call_builtin print_int \n");
}
else{
printf("call_builtin print_bool \n");
}
break;
}
}
void print_binop_string(int binop, int curr_reg,
int next_reg, int ID1, int ID2){
int ID = ID1 + ID2; //To see if it's float or int. float = 3/4.
if (ID1 == 1 && ID2 == 2){
printf("int_to_real r%d, r%d\n", curr_reg, curr_reg);
}
else if (ID1 == 2 && ID2 == 1){
printf("int_to_real r%d, r%d\n", next_reg, next_reg);
}
switch(binop){
case BINOP_ADD:
if (ID >= 3){
printf("add_real r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
}
else{
printf("add_int r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
}
break;
case BINOP_SUB:
if (ID >= 3){
printf("sub_real r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
}
else{
printf("sub_int r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
}
break;
case BINOP_MUL:
if (ID >= 3){
printf("mul_real r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
}
else{
printf("mul_int r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
}
break;
case BINOP_DIV:
if (ID >= 3){
printf("div_real r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
}
else{
printf("div_int r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
}
break;
case BINOP_AND:
printf("and r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
break;
case BINOP_OR:
printf("or r%d, r%d, r%d \n",
curr_reg, curr_reg, next_reg);
break;
}
}
void print_relop_string(int relop, int curr_reg,
int next_reg, int ID1, int ID2){
int ID = ID1 + ID2; //To see if it's float or int. float = 3/4.
switch(relop){
case RELOP_EQ:
if (ID >= 3){
printf("cmp_eq_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
else{
printf("cmp_eq_int r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
break;
case RELOP_NE:
if (ID >= 3){
printf("cmp_ne_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
else{
printf("cmp_ne_int r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
break;
case RELOP_LT:
if (ID == 4){
printf("cmp_lt_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
else if(ID == 2){
printf("cmp_lt_int r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
else if (ID1 == 1){
printf("int_to_real r%d, r%d\n", curr_reg, curr_reg);
printf("cmp_lt_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
else
{
printf("int_to_real r%d, r%d\n", next_reg, next_reg);
printf("cmp_lt_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
break;
case RELOP_GT:
if (ID == 4){
printf("cmp_gt_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
else if(ID == 2){
printf("cmp_gt_int r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
else if (ID1 == 1){
printf("int_to_real r%d, r%d\n", curr_reg, curr_reg);
printf("cmp_gt_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
else
{
printf("int_to_real r%d, r%d\n", next_reg, next_reg);
printf("cmp_gt_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
break;
case RELOP_LE:
if (ID == 4){
printf("cmp_le_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
else if(ID == 2){
printf("cmp_le_int r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
else if (ID1 == 1){
printf("int_to_real r%d, r%d\n", curr_reg, curr_reg);
printf("cmp_le_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
else
{
printf("int_to_real r%d, r%d\n", next_reg, next_reg);
printf("cmp_le_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
break;
case RELOP_GE:
if (ID == 4){
printf("cmp_ge_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
else if(ID == 2){
printf("cmp_ge_int r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);}
else if (ID1 == 1){
printf("int_to_real r%d, r%d\n", curr_reg, curr_reg);
printf("cmp_ge_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
else
{
printf("int_to_real r%d, r%d\n", next_reg, next_reg);
printf("cmp_ge_real r%d, r%d, r%d\n",
curr_reg, curr_reg, next_reg);
}
break;
}
}
void print_unop_string(int unop, int reg, int ID){
switch(unop){
case UNOP_MINUS:
if (ID == 2){
printf("real_const r%d, -1\n", reg+1);
printf("mul_real r%d, r%d, r%d \n", reg, reg, reg+1);
}
else{
printf("int_const r%d, -1\n", reg+1);
printf("mul_int r%d, r%d, r%d \n", reg, reg, reg+1);
}
break;
case UNOP_NOT:
printf("not r%d, r%d\n", reg, reg);
break;
}
}
void print_write_constant(Constant constant) {
switch (constant.type) {
case BOOL_CONSTANT:
printf("int_const r0, %d", constant.val.bool_val);
printf("\n");
printf("call_builtin print_bool"); //with a function
printf("\n");
break;
case INT_CONSTANT:
printf("int_const r0, %d", constant.val.int_val);
printf("\n");
printf("call_builtin print_int"); //with a function
printf("\n");
break;
case FLOAT_CONSTANT:
printf("real_const r0, %s", constant.val.float_val);
printf("\n");
printf("call_builtin print_real"); //with a function
printf("\n");
break;
case STRING_CONSTANT:
printf("string_const r0, %s", constant.val.str_val);
printf("\n");
printf("call_builtin print_string"); //with a function
printf("\n");
break;
}
}
/* Generating code for expression */
int print_expr(Expr expr, int reg, char* proc_id) {
int curr_reg = reg;
int next_reg;
int ID_type;
int ID_type2;
switch (expr->kind) {
Type ID_type;
int stackNo;
case EXPR_ID:
ID_type = getType(proc_id,expr->id);
stackNo = getStackSlotNum(proc_id, expr->id);
printf("load r%d, %d\n", curr_reg,stackNo);
if(isRef(proc_id, expr->id))
printf("load_indirect r%d, r%d\n", curr_reg ,curr_reg);
break;
case EXPR_CONST:
print_constant(expr->constant, curr_reg);
break;
case EXPR_BINOP:
ID_type = getExprType(expr->e1, proc_id);
ID_type2 = getExprType(expr->e2, proc_id);
curr_reg = print_expr(expr->e1, curr_reg, proc_id);
next_reg = print_expr(expr->e2, curr_reg + 1, proc_id);
print_binop_string(expr->binop, curr_reg,
next_reg, ID_type, ID_type2);
break;
case EXPR_RELOP:
ID_type = getExprType(expr->e1, proc_id);
ID_type2 = getExprType(expr->e2, proc_id);
curr_reg = print_expr(expr->e1, curr_reg, proc_id);
next_reg = print_expr(expr->e2, curr_reg + 1, proc_id);
print_relop_string(expr->relop, curr_reg,
next_reg, ID_type, ID_type2);
break;
case EXPR_UNOP:
ID_type = getExprType(expr->e1, proc_id);
print_unop_string(expr->unop, curr_reg, ID_type);
break;
case EXPR_ARRAY:
curr_reg = calculate_offset(expr->es, curr_reg,
expr->id, proc_id);
break;
}
return curr_reg;
}
void print_constant(Constant constant, int reg) {
switch (constant.type) {
case BOOL_CONSTANT:
printf("int_const r%d, %d\n", reg, constant.val.bool_val);
break;
case INT_CONSTANT:
printf("int_const r%d, %d\n", reg,constant.val.int_val);
break;
case FLOAT_CONSTANT:
printf("real_const r%d, %s\n", reg, constant.val.float_val);
break;
case STRING_CONSTANT:
//Should not be possible?
printf("string_const r%d, %s\n", reg, constant.val.str_val);
break;
}
}
/* calculate address offset for arrays */
void print_offset(int free_reg, int high, int low){
printf("int_const r%d, %d\n", free_reg,
high-low+1);
}