-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodeGeneration.c
1222 lines (1181 loc) · 72.3 KB
/
codeGeneration.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
/*
* Group no. 50
* Aniruddha Mahajan -------- 2017A7PS0145P
* Ravindra Singh Shekhawat - 2017A7PS0146P
* Shreyas Srikrishna ------- 2017A7PS0162P
* Chetan Sharma ------------ 2017A7PS0182P
*/
#include "ast.h"
#include "symboltable.h"
#include "semantic.h"
#include "codeGeneration.h"
#include <string.h>
#define sc startChild
#define rs rightSibling
int utilLabel; //util%d, utilLabel; utilLabel++
int forLoopLabel;
int whileLoopLabel;
int caseLabel;
char* createTempVarName(int num, PrimitiveType type){
char* res = malloc(sizeof(char)*4);
refreshBuffer(res, 4);
if(type == integer)
sprintf(res, "TI%d", num);
else if(type == boolean)
sprintf(res, "TB%d", num);
else
sprintf(res, "TR%d", num);
return res;
}
PrimitiveType processArrayIdNode(ASTNode* node, SymbolTable* table, FILE* file, int* currTempNo){
fprintf(file, "\n;-------Processing arrayIdNode(array element)-------\n");
SymbolTableEntry* sym = lookupString(node->sc->node.idnode.lexeme, table, idEntry, true, node->sc->node.idnode.line_no);
Typeof* currType = &(sym->symbol.idEntry.type);
if(currType->type.arrayType.high>=0 && currType->type.arrayType.low>=0){ //static array
if(node->sc->rs->type == idNode){ //eg: a[b]. need to perform bound checking
//1. check bounds
fprintf(file, "\n;----code for dynamic bound checking of static array element %s[%s]----\n",node->sc->node.idnode.lexeme,node->sc->rs->node.idnode.lexeme);
SymbolTableEntry* indexEntry = lookupString(node->sc->rs->node.idnode.lexeme,table,idEntry,true,node->sc->rs->node.idnode.line_no);
fprintf(file, "\tmov ax, WORD[rbp+%d]\n", indexEntry->symbol.idEntry.offset);
fprintf(file, "\tcmp ax, %d\n\tjl runTimeError\n\tcmp ax, %d\n\tjg runTimeError\n;----index is within bounds---\n", currType->type.arrayType.low,currType->type.arrayType.high);
//2.index is within bounds. now fetch array element
if(currType->type.arrayType.t == integer){ //static integer array
//fprintf(file, "\tmov rdx,rbp\n\tmov cx,%d\n\tand rcx,000000000000FFFFh\n\tadd cx,1\n\tand rax,000000000000FFFFh\n\tsub ax,%d\n\tmov bx,2\n\tmul bx\n\tadd rcx,rax\n\tadd rdx,rcx\n\tmov ax,WORD[rdx]\n",sym->symbol.idEntry.offset, currType->type.arrayType.low);
fprintf(file, "\tmov cx,%d\n\tand rcx,000000000000FFFFh\n\tadd cx,1\n\tand rax,000000000000FFFFh\n\tsub ax,%d\n\tmov bx,2\n\tmul bx\n\tadd rcx,rax\n\tmov ax,WORD[rbp+rcx]\n",sym->symbol.idEntry.offset, currType->type.arrayType.low);
*currTempNo += 1;
char* finalTemp = createTempVarName(*currTempNo, integer);
SymbolTableEntry* sym1 = lookupString(finalTemp, table, idEntry, true, -1);
fprintf(file, "\tmov WORD[rbp+%d], ax\n;------array element fetched and stored in %s------\n", sym1->symbol.idEntry.offset, finalTemp);
return integer;
}
else if(currType->type.arrayType.t == boolean){ //static boolean array
//fprintf(file, "\tmov rdx,rbp\n\tmov cx,%d\n\tand rcx,000000000000FFFFh\n\tadd cx,1\n\tand rax,000000000000FFFFh\n\tsub ax,%d\n\tadd rcx,rax\n\tadd rdx,rcx\n\tmov al,BYTE[rdx]\n",sym->symbol.idEntry.offset, currType->type.arrayType.low);
*currTempNo += 1;
char* finalTemp = createTempVarName(*currTempNo, boolean);
SymbolTableEntry* sym1 = lookupString(finalTemp, table, idEntry, true, -1);
fprintf(file, "\tmov BYTE[rbp+%d], al\n;------array element fetched and stored in %s------\n", sym1->symbol.idEntry.offset, finalTemp);
return boolean;
}
else{
//static real array
return -1;
}
}
else{ //eg: a[3]. bound already checked at compile time
int relIndex = ((int)node->sc->rs->node.numNode.value) - currType->type.arrayType.low;
if(currType->type.arrayType.t == integer){ //static integer array
fprintf(file,"\tmov ax, WORD[rbp+%d]\n",(sym->symbol.idEntry.offset+(2*relIndex)+1));
*currTempNo += 1;
char* finalTemp = createTempVarName(*currTempNo, integer);
SymbolTableEntry* sym1 = lookupString(finalTemp, table, idEntry, true, -1);
fprintf(file, "\tmov WORD[rbp+%d], ax\n;------array element fetched and stored in %s------\n", sym1->symbol.idEntry.offset, finalTemp);
return integer;
}
else if(currType->type.arrayType.t == boolean){ //static boolean array
fprintf(file,"\tmov al, BYTE[rbp+%d]\n",(sym->symbol.idEntry.offset+(relIndex)+1));
*currTempNo += 1;
char* finalTemp = createTempVarName(*currTempNo, boolean);
SymbolTableEntry* sym1 = lookupString(finalTemp, table, idEntry, true, -1);
fprintf(file, "\tmov BYTE[rbp+%d], al\n;------array element fetched and stored in %s------\n", sym1->symbol.idEntry.offset, finalTemp);
return boolean;
}
else{
//static real array
return -1;
}
}
}
else{ //dynamic
//1.load lower bound in cx
if(currType->type.arrayType.low>=0)
fprintf(file,"\tmov cx, %d\n\tand rcx,000000000000FFFFh\n", currType->type.arrayType.low);
else{
SymbolTableEntry* leftVar = lookupString(currType->type.arrayType.left, table, idEntry, true, -1);
fprintf(file,"\tmov cx, WORD[rbp+%d]\n\tand rcx,000000000000FFFFh\n", leftVar->symbol.idEntry.offset);
}
//2.load upper bound in dx
if(currType->type.arrayType.high>=0)
fprintf(file,"\tmov dx, %d\n\tand rdx,000000000000FFFFh\n", currType->type.arrayType.high);
else{
SymbolTableEntry* rightVar = lookupString(currType->type.arrayType.right, table, idEntry, true, -1);
fprintf(file,"\tmov dx, WORD[rbp+%d]\n\tand rdx,000000000000FFFFh\n", rightVar->symbol.idEntry.offset);
}
//3. load the index in ax.
fprintf(file, "\n;----code for dynamic bound checking of dynamic array element %s[",node->sc->node.idnode.lexeme);
if(node->sc->rs->type == numNode)
fprintf(file,"%d]----\n\tmov ax, %d\n\tand rax,000000000000FFFFh\n", (int)node->sc->rs->node.numNode.value, (int)node->sc->rs->node.numNode.value);
else{
SymbolTableEntry* dynArrIndex = lookupString(node->sc->rs->node.idnode.lexeme, table, idEntry, true, node->sc->rs->node.idnode.line_no);
fprintf(file,"%s]----\n\tmov ax,WORD[rbp+%d]\n\tand rax,000000000000FFFFh\n",node->sc->rs->node.idnode.lexeme, dynArrIndex->symbol.idEntry.offset);
}
//4. check if cx<=ax<=dx
fprintf(file, "\tcmp ax,cx\n\tjl runTimeError\n\tcmp ax,dx\n\tjg runTimeError\n");
//5. bounds are ok, now retrieve the element
fprintf(file,";----loading rsp value for dynamic array %s into rdx----\n",node->sc->node.idnode.lexeme);
fprintf(file,"\tmov rdx, [rbp+%d]\t;dont need rdx(upper bound)anymore\n", sym->symbol.idEntry.offset);
fprintf(file,"\tsub ax,cx\t;ax now contains relOffset for this array element\n");
if(currType->type.arrayType.t == integer){ //integer dynamic array
fprintf(file, "\tmov bx,2\n\tpush rdx\n\tmul bx\n\tpop rdx\n\tmov cx,WORD[rdx+rax]\t;now cx contains the required array element\n");
*currTempNo += 1;
char* tempName = createTempVarName(*currTempNo, integer);
SymbolTableEntry* tempIntVar = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\tmov WORD[rbp+%d], cx\n;------array element fetched and stored in %s------\n", tempIntVar->symbol.idEntry.offset, tempName);
return integer;
}
else{ //boolean dynamic array
fprintf(file, "\tmov cl,BYTE[rdx+rax]\t;now cl contains the required array element\n");
*currTempNo += 1;
char* tempName = createTempVarName(*currTempNo, boolean);
SymbolTableEntry* tempIntVar = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\tmov BYTE[rbp+%d], cl\n;------array element fetched and stored in %s------\n", tempIntVar->symbol.idEntry.offset, tempName);
return boolean;
}
}
}
void processIntegerExpr(ASTNode* node, SymbolTable* table, FILE* file, int* currTempNo){
fprintf(file, "\n;------Processing integer expression-----\n");
//1. process left operand and load it in ax
if(node->sc->type == numNode)
fprintf(file, "\n\tmov ax, %d\n", (int)node->sc->node.numNode.value);
else if(node->sc->type == idNode){
SymbolTableEntry* sym1 = lookupString(node->sc->node.idnode.lexeme, table, idEntry, true, node->sc->node.idnode.line_no);
fprintf(file, "\n\tmov ax, WORD[rbp+%d];\tleft operand is %s\n", sym1->symbol.idEntry.offset, node->sc->node.idnode.lexeme);
}
else if(node->sc->type == opNode){
processIntegerExpr(node->sc, table, file, currTempNo);
char* tempName = createTempVarName(*currTempNo, integer);
SymbolTableEntry* sym2 = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\n\tmov ax, WORD[rbp+%d];\tleft operand is %s\n", sym2->symbol.idEntry.offset, tempName);
}
else{ //arrayIdNode
processArrayIdNode(node->sc, table, file, currTempNo);
char* tempName = createTempVarName(*currTempNo, integer);
SymbolTableEntry* sym9 = lookupString(tempName,table,idEntry,true,-1);
fprintf(file, "\n\tmov ax, WORD[rbp+%d];\tleft operand is %s\n", sym9->symbol.idEntry.offset, tempName);
}
//2. process right operand and load it in bx
fprintf(file, "\tpush rax\n"); //save ax in case it is used below
if(node->sc->rs->type == numNode)
fprintf(file, "\tmov bx, %d\n", (int)node->sc->rs->node.numNode.value);
else if(node->sc->rs->type == idNode){
SymbolTableEntry* sym3 = lookupString(node->sc->rs->node.idnode.lexeme, table, idEntry, true, node->sc->rs->node.idnode.line_no);
fprintf(file, "\tmov bx, WORD[rbp+%d];\tright operand is %s\n", sym3->symbol.idEntry.offset, node->sc->rs->node.idnode.lexeme);
}
else if(node->sc->rs->type == opNode){
processIntegerExpr(node->sc->rs, table, file, currTempNo);
char* tempName = createTempVarName(*currTempNo, integer);
SymbolTableEntry* sym4 = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\tmov bx, WORD[rbp+%d];\tright operand is %s\n", sym4->symbol.idEntry.offset, tempName);
}
else{ //arrayIdNdoe
processArrayIdNode(node->sc->rs, table, file, currTempNo);
char* tempName = createTempVarName(*currTempNo, integer);
SymbolTableEntry* sym9 = lookupString(tempName,table,idEntry,true,-1);
fprintf(file, "\n\tmov bx, WORD[rbp+%d];\tleft operand is %s\n", sym9->symbol.idEntry.offset, tempName);
}
//3. perform the calculation
fprintf(file, "\tpop rax\n");
if(strcmp(node->node.opNode.token, "PLUS")==0)
fprintf(file, "\tadd ax,bx\n");
else if(strcmp(node->node.opNode.token, "MUL")==0)
fprintf(file, "\tmul bx\n"); //assumes no overflow.. result fits in 16 bit ax register
else if(strcmp(node->node.opNode.token, "MINUS")==0)
fprintf(file, "\tsub ax,bx\n");
else{
//DIV
}
//4. store final res(inside ax) into temporary variable
*currTempNo += 1;
char* finalTemp = createTempVarName(*currTempNo, integer);
SymbolTableEntry* sym = lookupString(finalTemp, table, idEntry, true, -1);
fprintf(file, "\tmov WORD[rbp+%d], ax\n;------expression computed. result is in %s------\n", sym->symbol.idEntry.offset, finalTemp);
return;
}
void processBooleanExpr(ASTNode* node, SymbolTable* table, FILE* file, int* currTempNo, int* currUtilIntNo){
fprintf(file, "\n;------Processing boolean expression-----\n");
//1. process left operand and load it in ax/al
if(node->sc->type == boolNode){
if(strcmp(node->sc->node.boolNode.token, "TRUE")==0)
fprintf(file, "\tmov al, 1\n");
else
fprintf(file, "\tmov al, 0\n");
}
else if(node->sc->type == numNode)
fprintf(file, "\n\tmov ax, %d\n", (int)node->sc->node.numNode.value);
else if(node->sc->type == idNode){
SymbolTableEntry* sym1 = lookupString(node->sc->node.idnode.lexeme, table, idEntry, true, node->sc->node.idnode.line_no);
if(sym1->symbol.idEntry.type.type.primitiveType == boolean)
fprintf(file, "\n\tmov al, BYTE[rbp+%d];\tleft operand is %s\n", sym1->symbol.idEntry.offset, node->sc->node.idnode.lexeme);
else
fprintf(file, "\n\tmov ax, WORD[rbp+%d];\tleft operand is %s\n", sym1->symbol.idEntry.offset, node->sc->node.idnode.lexeme);
}
else if(node->sc->type == opNode){
if(strcmp(node->sc->node.opNode.token,"GE") ||
strcmp(node->sc->node.opNode.token,"LE") ||
strcmp(node->sc->node.opNode.token,"GT") ||
strcmp(node->sc->node.opNode.token,"LT") ||
strcmp(node->sc->node.opNode.token,"NE") ||
strcmp(node->sc->node.opNode.token,"EQ") ||
strcmp(node->sc->node.opNode.token,"AND") ||
strcmp(node->sc->node.opNode.token,"OR")){
processBooleanExpr(node->sc, table, file, currTempNo, currUtilIntNo);
char* tempName = createTempVarName(*currTempNo, boolean);
SymbolTableEntry* sym2 = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\n\tmov al, BYTE[rbp+%d];\tleft operand is %s\n", sym2->symbol.idEntry.offset, tempName);
}
else { //arithmetic operator
processIntegerExpr(node->sc, table, file, currUtilIntNo);
char* tempName = createTempVarName(*currUtilIntNo, integer);
SymbolTableEntry* sym2 = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\n\tmov ax, WORD[rbp+%d];\tleft operand is %s\n", sym2->symbol.idEntry.offset, tempName);
}
}
else{
PrimitiveType t = processArrayIdNode(node->sc, table, file, currTempNo);
if(t == integer){
char* tempName = createTempVarName(*currTempNo, integer);
SymbolTableEntry* sym9 = lookupString(tempName,table,idEntry,true,-1);
fprintf(file, "\n\tmov ax, WORD[rbp+%d];\tleft operand is %s\n", sym9->symbol.idEntry.offset, tempName);
}
else{
char* tempName = createTempVarName(*currTempNo, boolean);
SymbolTableEntry* sym9 = lookupString(tempName,table,idEntry,true,-1);
fprintf(file, "\n\tmov al, BYTE[rbp+%d];\tleft operand is %s\n", sym9->symbol.idEntry.offset, tempName);
}
}
//2. process right operand and load it in bx/bl
fprintf(file, "\tpush rax\n"); //save al in case it is used below
if(node->sc->rs->type == boolNode){
if(strcmp(node->sc->rs->node.boolNode.token, "TRUE")==0)
fprintf(file, "\tmov bl, 1\n");
else
fprintf(file, "\tmov bl, 0\n");
}
else if(node->sc->rs->type == numNode)
fprintf(file, "\n\tmov bx, %d\n", (int)node->sc->rs->node.numNode.value);
else if(node->sc->rs->type == idNode){
SymbolTableEntry* sym1 = lookupString(node->sc->rs->node.idnode.lexeme, table, idEntry, true, node->sc->rs->node.idnode.line_no);
if(sym1->symbol.idEntry.type.type.primitiveType == boolean)
fprintf(file, "\n\tmov bl, BYTE[rbp+%d];\tleft operand is %s\n", sym1->symbol.idEntry.offset, node->sc->rs->node.idnode.lexeme);
else
fprintf(file, "\n\tmov bx, WORD[rbp+%d];\tleft operand is %s\n", sym1->symbol.idEntry.offset, node->sc->rs->node.idnode.lexeme);
}
else if(node->sc->rs->type == opNode){
if(strcmp(node->sc->rs->node.opNode.token,"GE") ||
strcmp(node->sc->rs->node.opNode.token,"LE") ||
strcmp(node->sc->rs->node.opNode.token,"GT") ||
strcmp(node->sc->rs->node.opNode.token,"LT") ||
strcmp(node->sc->rs->node.opNode.token,"NE") ||
strcmp(node->sc->rs->node.opNode.token,"EQ") ||
strcmp(node->sc->rs->node.opNode.token,"AND") ||
strcmp(node->sc->rs->node.opNode.token,"OR")){
processBooleanExpr(node->sc->rs, table, file, currTempNo, currUtilIntNo);
char* tempName = createTempVarName(*currTempNo, boolean);
SymbolTableEntry* sym2 = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\n\tmov bl, BYTE[rbp+%d];\tright operand is %s\n", sym2->symbol.idEntry.offset, tempName);
}
else { //arithmetic operator
processIntegerExpr(node->sc->rs, table, file, currUtilIntNo);
char* tempName = createTempVarName(*currUtilIntNo, integer);
SymbolTableEntry* sym2 = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\n\tmov bx, WORD[rbp+%d];\tright operand is %s\n", sym2->symbol.idEntry.offset, tempName);
}
}
else{
PrimitiveType t = processArrayIdNode(node->sc->rs, table, file, currTempNo);
if(t == integer){
char* tempName = createTempVarName(*currTempNo, integer);
SymbolTableEntry* sym9 = lookupString(tempName,table,idEntry,true,-1);
fprintf(file, "\n\tmov bx, WORD[rbp+%d];\tleft operand is %s\n", sym9->symbol.idEntry.offset, tempName);
}
else{
char* tempName = createTempVarName(*currTempNo, boolean);
SymbolTableEntry* sym9 = lookupString(tempName,table,idEntry,true,-1);
fprintf(file, "\n\tmov bl, BYTE[rbp+%d];\tleft operand is %s\n", sym9->symbol.idEntry.offset, tempName);
}
}
//3. perform the calculation
fprintf(file, "\tpop rax\n");
//First 6 operators operate on integers stored in ax,bx
if(strcmp(node->node.opNode.token, "GT")==0){
fprintf(file, "\tcmp ax,bx\n\tjg greater%d\n",utilLabel);
fprintf(file, "\tmov al,0\n\tjmp notgreater%d\n",utilLabel);
fprintf(file, "greater%d:\n\tmov al,1\nnotgreater%d:\n",utilLabel,utilLabel);
utilLabel++;
}
else if(strcmp(node->node.opNode.token, "GE")==0){
fprintf(file, "\tcmp ax,bx\n\tjge greatereq%d\n",utilLabel);
fprintf(file, "\tmov al,0\n\tjmp notgreatereq%d\n",utilLabel);
fprintf(file, "greatereq%d:\n\tmov al,1\nnotgreatereq%d:\n",utilLabel,utilLabel);
utilLabel++;
}
else if(strcmp(node->node.opNode.token, "LT")==0){
fprintf(file, "\tcmp ax,bx\n\tjl lesser%d\n",utilLabel);
fprintf(file, "\tmov al,0\n\tjmp notlesser%d\n",utilLabel);
fprintf(file, "lesser%d:\n\tmov al,1\nnotlesser%d:\n",utilLabel,utilLabel);
utilLabel++;
}
else if(strcmp(node->node.opNode.token, "LE")==0){
fprintf(file, "\tcmp ax,bx\n\tjle lessereq%d\n",utilLabel);
fprintf(file, "\tmov al,0\n\tjmp notlessereq%d\n",utilLabel);
fprintf(file, "lessereq%d:\n\tmov al,1\nnotlessereq%d:\n",utilLabel,utilLabel);
utilLabel++;
}
else if(strcmp(node->node.opNode.token, "NE")==0){
fprintf(file, "\tcmp ax,bx\n\tjne notequal%d\n",utilLabel);
fprintf(file, "\tmov al,0\n\tjmp notnotequal%d\n",utilLabel);
fprintf(file, "notequal%d:\n\tmov al,1\nnotnotequal%d:\n",utilLabel,utilLabel);
utilLabel++;
}
else if(strcmp(node->node.opNode.token, "EQ")==0){
fprintf(file, "\tcmp ax,bx\n\tje equal%d\n",utilLabel);
fprintf(file, "\tmov al,0\n\tjmp notequal%d\n",utilLabel);
fprintf(file, "equal%d:\n\tmov al,1\nnotequal%d:\n",utilLabel,utilLabel);
utilLabel++;
}
//Next 2 operators operate on booleans stored in al,bl
else if(strcmp(node->node.opNode.token, "AND")==0)
fprintf(file, "\tand al, bl\n");
else if(strcmp(node->node.opNode.token, "OR")==0){
fprintf(file, "\tor al, bl\n");
}
//4. store final res(inside al) into temporary variable
*currTempNo += 1;
char* finalTemp = createTempVarName(*currTempNo, boolean);
SymbolTableEntry* sym = lookupString(finalTemp, table, idEntry, true, -1);
fprintf(file, "\tmov BYTE[rbp+%d], al\n;------expression computed. result is in %s------\n", sym->symbol.idEntry.offset, finalTemp);
return;
}
int processExpression(ASTNode* node, SymbolTable* table, FILE* file, PrimitiveType exprType){
int finalTempNo = 0;
int utilIntTempNo = 0;
if(exprType == integer)
processIntegerExpr(node, table, file, &finalTempNo);
else if(exprType == boolean)
processBooleanExpr(node, table, file, &finalTempNo, &utilIntTempNo);
else{}
//processRealExpr(node, table, file, &finalTempNo);
fprintf(file, "\n;-----expression processed, result is stored inside temp number %d of type %d\n", finalTempNo, exprType);
return finalTempNo;
}
void codeGen(ASTNode* node, SymbolTable* table, FILE* file){
switch(node->type){
case programNode:{
ASTNode* mod1 = node->sc->rs;
ASTNode* mod2 = node->sc->rs->rs->rs;
ASTNode* driverMod = node->sc->rs->rs;
//1. Process mod1's module list
if(mod1->type != nullNode){
while(mod1 != NULL){
SymbolTableEntry* mod1_entry = lookupString(mod1->sc->node.idnode.lexeme, table, functionEntry, false, -1);
fprintf(file, "\nmodule%d:\n", mod1_entry->symbol.functionEntry.sequenceNumber);
codeGen(mod1, mod1_entry->table, file);
}
}
//2. Process mod2's module list
if(mod2->type != nullNode){
while(mod2 != NULL){
SymbolTableEntry* mod2_entry = lookupString(mod2->sc->node.idnode.lexeme, table, functionEntry, false, -1);
fprintf(file, "\nmodule%d:\n", mod2_entry->symbol.functionEntry.sequenceNumber);
codeGen(mod2, mod2_entry->table, file);
}
}
//3. Process driver module
SymbolTableEntry* driver_entry = lookupString("driverModule", table, driverEntry, false, -1);
fprintf(file, "\nmain:\n");
codeGen(driverMod, driver_entry->table, file);
break;
}
case moduleNode:{
//1. Special case of driver module
if(node->sc->type == nullNode){
SymbolTableEntry* sym = lookupString("driverModule", table->parent, driverEntry, false, -1);
fprintf(file, "\tsub rsp, %d\n", sym->symbol.driverEntry.ARSizeWithTemp-1);
fprintf(file, "\tmov rbp, rsp\n\tsub rsp,1\n"); //for driver, the frame base is same as bottom of stack.(as the frame/activation record for the driver function is located right at the bottom of the stack)
ASTNode* stmt = node->sc->rs->rs->rs;
while(stmt != NULL){
codeGen(stmt, table, file);
stmt = stmt->next;
}
}
//2.Any other module
else{
}
break;
}
case declareNode:{
//only need to handle the case of dynamic arrays
if(node->sc->rs->type == arrayTypeNode){
if(node->sc->rs->sc->sc->type == idNode && node->sc->rs->sc->sc->rs->type == idNode || //array a[low..high]
node->sc->rs->sc->sc->type == numNode && node->sc->rs->sc->sc->rs->type == idNode || //array a[2..high]
node->sc->rs->sc->sc->type == numNode && node->sc->rs->sc->sc->rs->type == idNode){ //array a[low..4]
fprintf(file, "\n;-----Dynamic array declaration----\n");
ASTNode* currArrVar = node->sc;
ASTNode* currRangeNode = node->sc->rs->sc;
//1.load value of lower index in bx
if(currRangeNode->sc->type == numNode)
fprintf(file, "\tmov bx,%d\t;-----left index----\n",(int)currRangeNode->sc->node.numNode.value);
else{
SymbolTableEntry* leftVar = lookupString(currRangeNode->sc->node.idnode.lexeme,table,idEntry,true,currRangeNode->sc->node.idnode.line_no);
fprintf(file, "\tmov bx,WORD[rbp+%d]\t;----left index----\n",leftVar->symbol.idEntry.offset);
}
//2.load value of higher index in ax
if(currRangeNode->sc->rs->type == numNode)
fprintf(file, "\tmov ax,%d\t;---right index----\n",(int)currRangeNode->sc->rs->node.numNode.value);
else{
SymbolTableEntry* rightVar = lookupString(currRangeNode->sc->rs->node.idnode.lexeme,table,idEntry,true,currRangeNode->sc->rs->node.idnode.line_no);
fprintf(file, "\tmov ax,WORD[rbp+%d]\t;----right index----\n",rightVar->symbol.idEntry.offset);
}
//3. check if ax<bx (or else runtime error)
fprintf(file, "\tcmp ax,bx\n\tjl runTimeError\n");
int factor;
if(strcmp(node->sc->rs->sc->rs->node.typeNode.token,"INTEGER")==0)
factor=2;
else if(strcmp(node->sc->rs->sc->rs->node.typeNode.token,"BOOLEAN")==0)
factor=1;
else factor=-1; //real
//Now loop over the following step for all the declared arrays
while(currArrVar != NULL){
SymbolTableEntry* dynArr = lookupString(currArrVar->node.idnode.lexeme,table,idEntry,true,currArrVar->node.idnode.line_no);
//4. increment the stack pointer by (bx-ax)*factor and store rsp at the offset of the array variable
fprintf(file, "\tsub ax,bx\n\tinc ax\n\tmov cx,%d\n\tmul cx\n\tand rax,000000000000FFFFh\n",factor);
fprintf(file, "\tsub rsp,rax\n\tinc rsp\n\tmov QWORD[rbp+%d],rsp\n\tdec rsp\n",dynArr->symbol.idEntry.offset);
currArrVar = currArrVar->next;
}
fprintf(file, "\n;--------End of Dynamic array declaration--------\n\n\n");
}
else {break;} //static
}
break;
}
case assignmentNode:{
//1.first compute rhs and store result in eax/ax/al
int type_of_assignment = -1; //set 0 for boolean, 1 for int , 2 for real
fprintf(file, "\n;-------assignment stmt-----\n\tpush rax\n");
switch(node->sc->rs->type){
case numNode:{
if(strcmp(node->sc->rs->node.numNode.token,"NUM")==0){
fprintf(file, "\tmov ax, %d\n", (int)node->sc->rs->node.numNode.value);
type_of_assignment = 1;
}
else{
//real
type_of_assignment = 2;
}
break;
}
case boolNode:{
type_of_assignment = 0;
if(strcmp(node->sc->rs->node.boolNode.token, "TRUE")==0)
fprintf(file, "\tmov al, 1\n");
else
fprintf(file, "\tmov al, 0\n");
break;
}
case idNode:{
SymbolTableEntry* rhsSym = lookupString(node->sc->rs->node.idnode.lexeme, table, idEntry, true, node->sc->rs->node.idnode.line_no);
if(rhsSym->symbol.idEntry.type.tag == primitive){
if(rhsSym->symbol.idEntry.type.type.primitiveType == boolean){
fprintf(file, "\tmov al, BYTE[rbp+%d]\n", rhsSym->symbol.idEntry.offset);
type_of_assignment = 0;
}
else if(rhsSym->symbol.idEntry.type.type.primitiveType == integer){
fprintf(file, "\tmov ax, WORD[rbp+%d]\n", rhsSym->symbol.idEntry.offset);
type_of_assignment = 1;
}
else{
type_of_assignment = 2;
//real
}
}
else{ //case A:=B where both A and B are arrays
//array
}
break;
}
case arrayIdNode:{
SymbolTableEntry* rhsSym = lookupString(node->sc->rs->sc->node.idnode.lexeme, table, idEntry, true, node->sc->rs->node.idnode.line_no);
int tempNo = 0;
PrimitiveType t = processArrayIdNode(node,table,file,&tempNo);
if(t==boolean){
char* tempName = createTempVarName(tempNo, boolean);
SymbolTableEntry* sym = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\tmov al, BYTE[rbp+%d]\n",sym->symbol.idEntry.offset);
type_of_assignment = 0;
}
else{ //integer
char* tempName = createTempVarName(tempNo, integer);
SymbolTableEntry* sym = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\tmov ax, WORD[rbp+%d]\n",sym->symbol.idEntry.offset);
type_of_assignment = 1;
}
break;
}
case opNode:{
if(node->sc->rs->node.opNode.typeOfExpr == integer){
int tempIntVarNo = processExpression(node->sc->rs, table, file, integer);
char* tempName = createTempVarName(tempIntVarNo, integer);
SymbolTableEntry* sym = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\tmov ax, WORD[rbp+%d]\n", sym->symbol.idEntry.offset);
type_of_assignment = 1;
}
else if(node->sc->rs->node.opNode.typeOfExpr == boolean){
int tempBoolVarNo = processExpression(node->sc->rs, table, file, boolean);
char* tempName = createTempVarName(tempBoolVarNo, boolean);
SymbolTableEntry* sym = lookupString(tempName, table, idEntry, true, -1);
fprintf(file, "\tmov al, BYTE[rbp+%d]\n", sym->symbol.idEntry.offset);
type_of_assignment = 0;
}
else{
//real
}
break;
}
}
//2. now perform the assignment
if(node->sc->type == arrayIdNode){ //lhs -> a[2] or a[b]
SymbolTableEntry* arrLhs = lookupString(node->sc->sc->node.idnode.lexeme,table,idEntry,true,node->sc->sc->node.idnode.line_no);
Typeof* currType = &(arrLhs->symbol.idEntry.type);
if(currType->type.arrayType.low>=0 && currType->type.arrayType.high>=0){ //static
if(node->sc->sc->rs->type == idNode){ //a[b]
//1. check bounds
fprintf(file, "\n;----code for dynamic bound checking of static array element %s[%s]----\n",node->sc->sc->node.idnode.lexeme,node->sc->sc->rs->node.idnode.lexeme);
fprintf(file, "\tpush rax\t;saving rhs result (rax)\n");
SymbolTableEntry* indexEntry = lookupString(node->sc->sc->rs->node.idnode.lexeme,table,idEntry,true,node->sc->sc->rs->node.idnode.line_no);
fprintf(file, "\tmov ax, WORD[rbp+%d]\n", indexEntry->symbol.idEntry.offset);
fprintf(file, "\tcmp ax, %d\n\tjl runTimeError\n\tcmp ax, %d\n\tjg runTimeError\n;----index is within bounds----\n", currType->type.arrayType.low,currType->type.arrayType.high);
//2.index is within bounds. now fetch exact offset and perform assignment
if(currType->type.arrayType.t == integer){ //static integer array
fprintf(file, "\tmov cx,%d\n\tand rcx,000000000000FFFFh\n\tadd cx,1\n\tand rax,000000000000FFFFh\n\tsub ax,%d\n\tmov bx,2\n\tmul bx\n\tadd rcx,rax\n",arrLhs->symbol.idEntry.offset, currType->type.arrayType.low);
fprintf(file, "\tpop rax\t;restoring rhs result into rax\n");
fprintf(file, "\tmov WORD[rbp+rcx], ax\t;performing the assignment to %s[%s]\n", node->sc->sc->node.idnode.lexeme,node->sc->sc->rs->node.idnode.lexeme);
}
else if(currType->type.arrayType.t == boolean){ //static boolean array
fprintf(file, "\tmov cx,%d\n\tand rcx,000000000000FFFFh\n\tadd cx,1\n\tand rax,000000000000FFFFh\n\tsub ax,%d\n\tmov bx,2\n\tmul bx\n\tadd rcx,rax\n",arrLhs->symbol.idEntry.offset, currType->type.arrayType.low);
fprintf(file, "\tpop rax\t;restoring rhs result into rax\n");
fprintf(file, "\tmov BYTE[rbp+rcx], al\t;performing the assignment to %s[%s]\n", node->sc->sc->node.idnode.lexeme,node->sc->sc->rs->node.idnode.lexeme);
}
else{
//static real array
}
}
else{ //a[2]. bounds already checked at compile time
int relIndex = ((int)node->sc->rs->node.numNode.value) - currType->type.arrayType.low;
if(currType->type.arrayType.t == integer) //static integer array
fprintf(file,"\tmov WORD[rbp+%d],ax\t;performing the assignment to %s[%s]\n",(arrLhs->symbol.idEntry.offset+(2*relIndex)+1), node->sc->sc->node.idnode.lexeme,node->sc->sc->rs->node.idnode.lexeme);
else if(currType->type.arrayType.t == boolean) //static boolean array
fprintf(file,"\tmov BYTE[rbp+%d],al\t;performing the assignment to %s[%s]\n",(arrLhs->symbol.idEntry.offset+relIndex+1), node->sc->sc->node.idnode.lexeme,node->sc->sc->rs->node.idnode.lexeme);
}
}
else{ //dynamic
//save rhs result
fprintf(file, "\n;lhs is dynamic array element. saving rhs result\n\tpush rax\n");
//1.load lower bound in cx
if(currType->type.arrayType.low>=0)
fprintf(file,"\tmov cx, %d\n\tand rcx,000000000000FFFFh\n", currType->type.arrayType.low);
else{
SymbolTableEntry* leftVar = lookupString(currType->type.arrayType.left, table, idEntry, true, -1);
fprintf(file,"\tmov cx, WORD[rbp+%d]\n\tand rcx,000000000000FFFFh\n", leftVar->symbol.idEntry.offset);
}
//2.load upper bound in dx
if(currType->type.arrayType.high>=0)
fprintf(file,"\tmov dx, %d\n\tand rdx,000000000000FFFFh\n", currType->type.arrayType.high);
else{
SymbolTableEntry* rightVar = lookupString(currType->type.arrayType.right, table, idEntry, true, -1);
fprintf(file,"\tmov dx, WORD[rbp+%d]\n\tand rdx,000000000000FFFFh\n", rightVar->symbol.idEntry.offset);
}
//3. load the index in ax.
fprintf(file, "\n;----code for dynamic bound checking of dynamic array element %s[",node->sc->node.idnode.lexeme);
if(node->sc->rs->type == numNode)
fprintf(file,"%d]----\n\tmov ax, %d\n\tand rax,000000000000FFFFh\n", (int)node->sc->rs->node.numNode.value, (int)node->sc->rs->node.numNode.value);
else{
SymbolTableEntry* dynArrIndex = lookupString(node->sc->rs->node.idnode.lexeme, table, idEntry, true, node->sc->rs->node.idnode.line_no);
fprintf(file,"%s]----\n\tmov ax,WORD[rbp+%d]\n\tand rax,000000000000FFFFh\n",node->sc->rs->node.idnode.lexeme, dynArrIndex->symbol.idEntry.offset);
}
//4. check if cx<=ax<=dx
fprintf(file, "\tcmp ax,cx\n\tjl runTimeError\n\tcmp ax,dx\n\tjg runTimeError\n");
//5. bounds are ok, now perform the assignment
fprintf(file,";----loading rsp value for dynamic array %s into rdx----\n",node->sc->node.idnode.lexeme);
fprintf(file,"\tmov rdx, [rbp+%d]\t;dont need rdx(upper bound)anymore\n", arrLhs->symbol.idEntry.offset);
fprintf(file,"\tsub ax,cx\t;ax now contains relOffset for this array element\n");
if(currType->type.arrayType.t == integer){ //integer dynamic array
fprintf(file, "\tmov bx,2\n\tpush rdx\n\tmul bx\n\tpop rdx\n\tadd rdx,rax\t;now rdx contains the required array element address\n");
fprintf(file, "\tpop rax\t;--restore rhs result\n\tmov WORD[rdx],ax\n");
}
else{ //boolean dynamic array
fprintf(file, "\tadd rdx,rax\t;now rdx contains the required array element address\n");
fprintf(file, "\tpop rax\t;--restore rhs result\n\tmov BYTE[rdx],al\n");
}
}
}
else{ //idnode
SymbolTableEntry* lhsSym = lookupString(node->sc->node.idnode.lexeme, table, idEntry, true, node->sc->node.idnode.line_no);
if(type_of_assignment == 0)
fprintf(file, "\tmov BYTE[rbp+%d], al\n", lhsSym->symbol.idEntry.offset);
else if(type_of_assignment == 1)
fprintf(file, "\tmov WORD[rbp+%d], ax\n", lhsSym->symbol.idEntry.offset);
else if(type_of_assignment == 2){} //real
}
//3. restore rax
fprintf(file, "\tpop rax\n;--------------\n");
break;
}
case opNode:{
break;
}
case idNode:{
break;
}
case numNode:{
break;
}
case inputIONode:{
SymbolTableEntry* sym = lookupString(node->sc->node.idnode.lexeme,table,idEntry,true,node->sc->node.idnode.line_no);
switch(sym->symbol.idEntry.type.tag){
case array:{
if(sym->symbol.idEntry.width == 8){ //dynamic
if(sym->symbol.idEntry.type.type.arrayType.t == integer){ //dynamic integer array
Typeof* currType = &(sym->symbol.idEntry.type);
//1.load lower bound in cx
//declare A:array[low..high] of integer;
//get_value(A);
if(currType->type.arrayType.low>=0)
fprintf(file,"\tmov cx, %d\n\tand rcx,000000000000FFFFh\n", currType->type.arrayType.low);
else{
SymbolTableEntry* leftVar = lookupString(currType->type.arrayType.left, table, idEntry, true, -1);
fprintf(file,"\tmov cx, WORD[rbp+%d]\n\tand rcx,000000000000FFFFh\n", leftVar->symbol.idEntry.offset);
}
//2.load upper bound in dx
if(currType->type.arrayType.high>=0)
fprintf(file,"\tmov dx, %d\n\tand rdx,000000000000FFFFh\n", currType->type.arrayType.high);
else{
SymbolTableEntry* rightVar = lookupString(currType->type.arrayType.right, table, idEntry, true, -1);
fprintf(file,"\tmov dx, WORD[rbp+%d]\n\tand rdx,000000000000FFFFh\n", rightVar->symbol.idEntry.offset);
}
//fprintf(file,"\tpush rcx\n");
//3. print input message. (enter integer array elements from index .. to index.. etc)
fprintf(file,"\n;-------code for scanning integer array-------\n");
fprintf(file, "\tmov bx,dx\n\tsub bx,cx\n\tadd bx,1\t;bx contains high-low+1 (dx-cx+1)\n"); //bx contains high-low+1
fprintf(file,"\tpush rbp\n");
fprintf(file,"\tmov rdi, Input_Array1\n");
fprintf(file,"\tmovsx rsi, bx\n");
fprintf(file,"\txor rax, rax\n\tcall printf\n\tpop rbp\n");
//////////////////////////////
//fprintf(file,"\tpush rbp\n");
fprintf(file,"\tpush rbp\n\tmov rdi, onScreenInt\n");
fprintf(file,"\txor rax, rax\n\tcall printf\n\tpop rbp\n");
//////////////////////////////
//fprintf(file,"\tpop rcx\n");
if(currType->type.arrayType.low>=0)
fprintf(file,"\tmov cx, %d\n\tand rcx,000000000000FFFFh\n", currType->type.arrayType.low);
else{
SymbolTableEntry* leftVar = lookupString(currType->type.arrayType.left, table, idEntry, true, -1);
fprintf(file,"\tmov cx, WORD[rbp+%d]\n\tand rcx,000000000000FFFFh\n", leftVar->symbol.idEntry.offset);
}
//2.load upper bound in dx
if(currType->type.arrayType.high>=0)
fprintf(file,"\tmov dx, %d\n\tand rdx,000000000000FFFFh\n", currType->type.arrayType.high);
else{
SymbolTableEntry* rightVar = lookupString(currType->type.arrayType.right, table, idEntry, true, -1);
fprintf(file,"\tmov dx, WORD[rbp+%d]\n\tand rdx,000000000000FFFFh\n", rightVar->symbol.idEntry.offset);
}
fprintf(file,"\tpush rbp\n\tmov rdi, Input_Array2\n");
fprintf(file,"\tmov rsi, rcx\n");
// rdx already has high
fprintf(file,"\txor rax, rax\n\tcall printf\n\tpop rbp\n");
//4. Take the input for each element
//bx->high-low+1 , cx->low , dx->high
fprintf(file, "\n;---code for inputing elements of dynamic array---\n");
fprintf(file, "\tmov rdx, QWORD[rbp+%d]\n",sym->symbol.idEntry.offset); //rdx contains address of first array element
fprintf(file, "takeInput%d:\n",utilLabel++);
fprintf(file, "\tcmp bx,0\n\tje stopInput%d\n",utilLabel-1);
//code for scanf
fprintf(file,"\tpush rdx\n\tmov rdi, Input_Format\n");
fprintf(file,"\tlea rsi, [int1]\n");
fprintf(file,"\txor rax, rax\n");
fprintf(file,"\tcall scanf\n");
fprintf(file,"\tpop rdx\n");
fprintf(file, "\n\tmov ax,WORD[int1]\n\tmov WORD[rdx],ax\n");
fprintf(file,"\tadd rdx,2\n");
fprintf(file, "\tsub bx,1\n\tjmp takeInput%d\n",utilLabel-1);
fprintf(file, "stopInput%d:\n",utilLabel-1);
}
else{
//dynamic boolean array
}
}
else{ //static
switch(sym->symbol.idEntry.type.type.arrayType.t){
case integer:{ //input integer array.
int h = sym->symbol.idEntry.type.type.arrayType.high;
int l = sym->symbol.idEntry.type.type.arrayType.low;
int iteration = h-l+1;
fprintf(file,"\n;-------code for scanning integer array-------\n");
fprintf(file,"\tpush rbp\n");
fprintf(file,"\tmov rdi, Input_Array1\n");
fprintf(file,"\tmov rsi, %d\n",iteration);
fprintf(file,"\txor rax, rax\n\tcall printf\n\tpop rbp\n");
//////////////////////////////
fprintf(file,"\tpush rbp\n");
fprintf(file,"\tmov rdi, onScreenInt\n");
fprintf(file,"\txor rax, rax\n\tcall printf\n\tpop rbp\n");
/////////////////////////////
fprintf(file,"\tpush rbp\n");
fprintf(file,"\tmov rdi, Input_Array2\n");
fprintf(file,"\tmov rsi, %d\n",l);
fprintf(file,"\tmov rdx, %d\n",h);
fprintf(file,"\txor rax, rax\n\tcall printf\n\tpop rbp\n");
int i = 0;
int off = sym->symbol.idEntry.offset;
while(iteration > 0)
{
fprintf(file,"\n;-----inputting %s[%d]----\n\tpush rbp\n",sym->symbol.idEntry.node->node.idnode.lexeme,l+i);
fprintf(file,"\tmov rdi, Input_Format\n");
fprintf(file,"\tlea rsi, [int1]\n");
fprintf(file,"\txor rax, rax\n");
fprintf(file,"\tcall scanf\n");
fprintf(file,"\tpop rbp\n");
fprintf(file,"\tmov ax, WORD[int1]\n");
fprintf(file,"\tmov WORD[rbp + %d], ax\n",(1+i*2 + off));
i++;
iteration--;
}
break;
}
case real:{ //input real array
break;
}
case boolean:{ //input bool array
int h = sym->symbol.idEntry.type.type.arrayType.high;
int l = sym->symbol.idEntry.type.type.arrayType.low;
int iteration = h-l+1;
fprintf(file,"\n;-------code for scanning boolean array-------\n");
fprintf(file,"\tpush rbp\n");
fprintf(file,"\tmov rdi, Input_Array1\n");
fprintf(file,"\tmov rsi, %d\n",iteration);
fprintf(file,"\txor rax, rax\n\tcall printf\n\tpop rbp\n");
//////////////////////////////
fprintf(file,"\tpush rbp\n");
fprintf(file,"\tmov rdi, onScreenBool\n");
fprintf(file,"\txor rax, rax\n\tcall printf\n\tpop rbp\n");
/////////////////////////////
fprintf(file,"\tpush rbp\n");
fprintf(file,"\tmov rdi, Input_Array2\n");
fprintf(file,"\tmov rsi, %d\n",l);
fprintf(file,"\tmov rdx, %d\n",h);
fprintf(file,"\txor rax, rax\n\tcall printf\n\tpop rbp\n");
int i = 0;
int off = sym->symbol.idEntry.offset;
while(iteration > 0)
{
fprintf(file,"\n;-----inputting %s[%d]----\n\tpush rbp\n",sym->symbol.idEntry.node->node.idnode.lexeme,l+i);
fprintf(file,"\tmov rdi, Input_Format\n");
fprintf(file,"\tlea rsi, [bool1]\n");
fprintf(file,"\txor rax, rax\n");
fprintf(file,"\tcall scanf\n");
fprintf(file,"\tpop rbp\n");
fprintf(file,"\tmov al, BYTE[bool1]\n");
fprintf(file,"\tmov BYTE[rbp + %d], al\n",(1+i + off));
i++;
iteration--;
}
break;
}
}
}
break;// nothing
}
case primitive:{
switch(sym->symbol.idEntry.type.type.primitiveType){
case integer:{
fprintf(file, "\n;-----code for scanning integer variable----\n\tpush rbp\n\tmov rdi, inputInt\n\txor rax, rax\n\tcall printf\n\tpop rbp\n");
//write code for scanning input integer
fprintf(file, "\tpush rbp\n\tmov rdi, Input_Format\n\tlea rsi,[int1]\n\txor rax,rax\n\tcall scanf\n\tpop rbp\n");//6 = 4 + 2//lite.. sahi he na.. u sure?pretty suresill...i
fprintf(file, "\tmov ax, WORD[int1]\n");// scanned value now in ax
fprintf(file, "\tmov WORD[rbp+%d],ax\n;-----------\n", sym->symbol.idEntry.offset);
break;
}
case real:{
break;
}
case boolean:{// boolean variable
fprintf(file, "\n;-----code for scanning boolean variable----\n\tpush rbp\n\tmov rdi, inputBool\n\txor rax, rax\n\tcall printf\n\tpop rbp\n");
//write code for scanning input boolean
fprintf(file, "\tpush rbp\n\tmov rdi, Input_Format\n\tlea rsi,[bool1]\n\txor rax,rax\n\tcall scanf\n\tpop rbp\n");
fprintf(file, "\tmov al, BYTE[bool1]\n");// scanned value now in al
fprintf(file, "\tmov BYTE[rbp + %d],al\n;-----------\n", sym->symbol.idEntry.offset);
break;
}
}
break;
}
}
break;
}
case outputIONode:{
switch(node->sc->type){
case idNode:{
SymbolTableEntry* sym = lookupString(node->sc->node.idnode.lexeme, table, idEntry, true, -1);
if(sym->symbol.idEntry.type.tag == primitive){
switch(sym->symbol.idEntry.type.type.primitiveType){
case integer:{ //eg: print(z) where z is an integer
fprintf(file, "\n;------code for printing integer variable-----\n\tpush rbp\n\tmov ax, WORD[rbp + %d]\n",sym->symbol.idEntry.offset);
fprintf(file, "\tpush rbp\n\tmov rdi, output\n\tmovsx rsi, ax\n\txor rax, rax\n\tcall printf\n\tpop rbp\n;------------\n");
break;
}
case real:{
break;
}
case boolean:{
fprintf(file, "\n;------code for printing Boolean variable-----\n");
fprintf(file,"\tmov al, BYTE[rbp + %d]\n",sym->symbol.idEntry.offset);
fprintf(file,"\tcmp al, 1\n\tjne zero%d\n\tpush rbp\n\tmov rdi, trueOutput\n\txor rax, rax\n\tcall printf\n\tpop rbp\n",utilLabel++);
fprintf(file,"\tjmp empty%d\n",utilLabel++);
fprintf(file,"\tzero%d:\n\tpush rbp\n\tmov rdi, falseOutput\n\t\txor rax, rax\n\t\tcall printf\n\tpop rbp\n",utilLabel - 2);
fprintf(file,"\tempty%d:\n",utilLabel - 1);
break;
}
}
}
else{ //if the id is an array, print like this : Output: 12 4 -8 9 10 -18
switch(sym->symbol.idEntry.type.type.arrayType.t){
case integer:{ //integer array
if(sym->symbol.idEntry.type.type.arrayType.low>=0 && sym->symbol.idEntry.type.type.arrayType.high>=0){ //static array
fprintf(file,"\n;-------code for printing static integer array---------\n");
int h = sym->symbol.idEntry.type.type.arrayType.high;
int l = sym->symbol.idEntry.type.type.arrayType.low;
int iteration = h-l+1;
int ofs = sym->symbol.idEntry.offset;
int i = 0;
fprintf(file, "\tpush rbp\n\tmov rdi, output_array\n\txor rax, rax\n\tcall printf\n\tpop rbp\n;------------\n");
while(iteration > 0){
fprintf(file,";----printing %s[%d]---\n\tpush rbp\n\tmov ax, WORD[rbp + %d]\n",sym->symbol.idEntry.node->node.idnode.lexeme,l+i,(1+ofs + 2*i));
fprintf(file, "\tmov rdi, array_value\n\tmovsx rsi, ax\n\txor rax, rax\n\tcall printf\n\tpop rbp\n;------------\n");
i++;
iteration--;
}
fprintf(file, ";----printing newline character---\n\tpush rbp\n\tmov rdi, newline_char\n\txor rax,rax\n\tcall printf\n\tpop rbx\n");
}
else{
fprintf(file,";------------code for printing dynamic array-------------\n");
Typeof* currType = &(sym->symbol.idEntry.type);
//1.load lower bound in cx
//declare A:array[low..high] of integer;
//get_value(A);
fprintf(file, "\tpush rbp\n\tmov rdi, output_array\n\txor rax, rax\n\tcall printf\n\tpop rbp\n;------------\n");
if(currType->type.arrayType.low>=0)
fprintf(file,"\tmov cx, %d\n\tand rcx,000000000000FFFFh\n", currType->type.arrayType.low);
else{
SymbolTableEntry* leftVar = lookupString(currType->type.arrayType.left, table, idEntry, true, -1);
fprintf(file,"\tmov cx, WORD[rbp+%d]\n\tand rcx,000000000000FFFFh\n", leftVar->symbol.idEntry.offset);
}
//2.load upper bound in dx
if(currType->type.arrayType.high>=0)
fprintf(file,"\tmov dx, %d\n\tand rdx,000000000000FFFFh\n", currType->type.arrayType.high);
else{
SymbolTableEntry* rightVar = lookupString(currType->type.arrayType.right, table, idEntry, true, -1);
fprintf(file,"\tmov dx, WORD[rbp+%d]\n\tand rdx,000000000000FFFFh\n", rightVar->symbol.idEntry.offset);
}
fprintf(file,"\n;-------code for printing integer array-------\n");
fprintf(file, "\tmov bx,dx\n\tsub bx,cx\n\tadd bx,1\t;bx contains high-low+1 (dx-cx+1)\n");
fprintf(file, "\tmov rdx, QWORD[rbp+%d]\n",sym->symbol.idEntry.offset); //rdx contains address of first array element
fprintf(file,"showOutput%d:\n",utilLabel++);
fprintf(file,"\tcmp bx, 0\n");
fprintf(file,"\tje printOver%d\n",utilLabel-1);
fprintf(file,"\npush rdx\t");
fprintf(file,"\n\tand rsi, 000000000000FFFFh\n");
fprintf(file, "\tmov ax, WORD[rdx]\n");
fprintf(file,"\tmovsx rsi, ax\n");
fprintf(file, "\tmov rdi, array_value\n\txor rax, rax\n\tcall printf\n\tpop rdx\n;------------\n");
fprintf(file,"\tadd rdx,2\n");
fprintf(file, "\tsub bx,1\n\tjmp showOutput%d\n",utilLabel-1);
fprintf(file, "printOver%d:\n",utilLabel-1);
}
break;
}
case real:{
break;
}
case boolean:{
if(sym->symbol.idEntry.type.type.arrayType.low>=0 && sym->symbol.idEntry.type.type.arrayType.high>=0){ //static array
fprintf(file,"\n;-------code for printing static boolean array---------\n");
int h = sym->symbol.idEntry.type.type.arrayType.high;
int l = sym->symbol.idEntry.type.type.arrayType.low;
int iteration = h-l+1;
int ofs = sym->symbol.idEntry.offset;
int i = 0;
fprintf(file, "\tpush rbp\n\tmov rdi, output_array\n\txor rax, rax\n\tcall printf\n\tpop rbp\n;------------\n");
while(iteration > 0){
fprintf(file,";----printing %s[%d]---\n\tpush rbp\n\tmov al, BYTE[rbp + %d]\n",sym->symbol.idEntry.node->node.idnode.lexeme,l+i,(1+ofs + i));
fprintf(file,"\tcmp al, 1\n\tjne zero%d\n\tpush rbp\n\tmov rdi, bool_true\n\txor rax, rax\n\tcall printf\n\tpop rbp\n",utilLabel++);
fprintf(file,"\tjmp empty%d\n",utilLabel++);
fprintf(file,"\tzero%d:\n\tpush rbp\n\tmov rdi, bool_false\n\t\txor rax, rax\n\t\tcall printf\n\tpop rbp\n",utilLabel - 2);
fprintf(file,"\tempty%d:\n",utilLabel - 1);
i++;
iteration--;
}
fprintf(file, ";----printing newline character---\n\tpush rbp\n\tmov rdi, newline_char\n\txor rax,rax\n\tcall printf\n\tpop rbx\n");
}
else{
//dynamic boolean array
}
break;
}
}
}
break;
}
case numNode:{
fprintf(file,";---code for printing numNode---------------\n");
fprintf(file,"\tmov ax, %d\n",(int)node->node.numNode.value);
fprintf(file, "\tmov rdi, output\n\tmovsx rsi, ax\n\txor rax, rax\n\tcall printf\n\tpop rbp\n;------------\n");
break;
}
case boolNode:{
fprintf(file,";---code for printing boolNode---------------\n");
if(strcmp(node->node.boolNode.token,"TRUE")==0)
fprintf(file,"\tmov al, 1\n");
else
fprintf(file,"\tmov al, 0\n");
fprintf(file,"\tcmp al, 1\n\tjne zero%d\n\tpush rbp\n\tmov rdi, trueOutput\n\txor rax, rax\n\tcall printf\n\tpop rbp\n",utilLabel++);
fprintf(file,"\tjmp empty%d\n",utilLabel++);
fprintf(file,"\tzero%d:\n\tpush rbp\n\tmov rdi, falseOutput\n\t\txor rax, rax\n\t\tcall printf\n\tpop rbp\n",utilLabel - 2);
fprintf(file,"\tempty%d:\n",utilLabel - 1);
break;
}
}
break;
}
case conditionalNode:{
fprintf(file, ";---------Code for Switch-Case Statements----------\n");
SymbolTableEntry* sym = lookupString(node->sc->node.idnode.lexeme,table,idEntry,true, node->sc->node.idnode.line_no);
SymbolTableEntry* cond = lookupBlock(&node->node.conditionalNode.block,table,switchCaseEntry,false);
if(sym->symbol.idEntry.type.type.primitiveType == integer){
fprintf(file,"\tmov ax, WORD[rbp + %d]\n",sym->symbol.idEntry.offset);
ASTNode* cases = node->sc->rs; // cases is a CaseNode
while(cases != NULL)
{
fprintf(file,"\tmov dx, %d\n",(int)cases->sc->rs->node.numNode.value);
fprintf(file,"\tcmp ax, dx\n");
fprintf(file,"\tjne nextCase%d\n",caseLabel++);
codeGen(cases, cond->table, file);
fprintf(file,"\tjmp endSwitchCase%d\n",utilLabel);
fprintf(file,"\tnextCase%d:\n",caseLabel-1);
cases = cases->next;