-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeGen.c
More file actions
1394 lines (1152 loc) · 61.8 KB
/
codeGen.c
File metadata and controls
1394 lines (1152 loc) · 61.8 KB
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 Number: 31
// MADHUR PANWAR 2016B4A70933P
// TUSSANK GUPTA 2016B3A70528P
// SALMAAN SHAHID 2016B4A70580P
// APURV BAJAJ 2016B3A70549P
// HASAN NAQVI 2016B5A70452P
// nasm -felf64 code.asm && gcc code.o && ./a.out
// nasm -felf64 code.asm && gcc -no-pie code.o && ./a.out
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "codeGen.h"
#include "symbolTableDef.h"
#include "symbolHash.h"
#include "symbolTable.h"
#include "typeCheck.h"
#include "lexerDef.h"
//prereq: stack should be aligned (odd number of pushes)
void RUNTIME_EXIT_WITH_ERROR(FILE *fp, char *e) {
fprintf(fp, "\t mov rdi, %s \n", e);
fprintf(fp, "\t call printf \n");
fprintf(fp, "\t mov rax, 60 \n");
fprintf(fp, "\t xor rdi, rdi \n");
fprintf(fp, "\t syscall \n");
}
extern char *inverseMappingTable[];
//first register is used for function activation record base
//second register is used for IO List base
char *baseRegister[2] = {"RBP", "RBX"};
char *expreg[4] = {"r8","r9","r10","r11"};
int arrBaseSize = 1; //in words
int scale = 2;
int getActivationRecordSize(char *functionName, symbolTable* symT) {
symFuncInfo *finfo = stGetFuncInfo(functionName, symT);
int size = scale * (finfo->st->scopeSize);
if (size % 16)
size += (16 - (size % 16));
return size;
}
int getIOlistSize(char *functionName, symbolTable* symT) {
symFuncInfo *finfo = stGetFuncInfo(functionName, symT);
int size = (finfo->arSize) - (finfo->st->scopeSize);
size = scale * size;
if (size % 16)
size += (16 - (size % 16));
return size;
}
void setExpSize(gSymbol etype, char **expSizeStr, char **expSizeRegSuffix){
switch(etype){
case g_INTEGER:
*expSizeStr = "dword";
*expSizeRegSuffix = "d";
break;
case g_BOOLEAN:
*expSizeStr = "word";
*expSizeRegSuffix = "w";
break;
case g_REAL:
*expSizeStr = "qword";
*expSizeRegSuffix = "";
break;
default:
*expSizeStr = "word";
*expSizeRegSuffix = "w";
break;
}
}
//outcome: lower bound in expreg[0], upper bound in expreg[1]
//affects: No other register affected
void getArrBoundsInExpReg(ASTNode *arrNode, FILE *fp){
char *expSizeStr, *expSizeRegSuffix;
setExpSize(g_INTEGER,&expSizeStr,&expSizeRegSuffix);
varType arrVtype = arrNode->stNode->info.var.vtype;
if(arrVtype.vaType == DYN_L_ARR || arrVtype.vaType == DYN_ARR){
//get the left bound to expreg[0]
symTableNode *leftStn = arrVtype.si.vt_id;
bool isIOlistVar = arrNode->stNode->info.var.isIOlistVar;
int toSub;
if(isIOlistVar){
toSub = scale *(arrNode->stNode->info.var.offset + arrBaseSize + getSizeByType(g_INTEGER));
}
else{
toSub = scale * (leftStn->info.var.offset + leftStn->info.var.vtype.width);
}
fprintf(fp,"\t xor %s, %s \n",expreg[0],expreg[0]);
fprintf(fp, "\t mov %s%s, %s[%s-%d] \n", expreg[0],expSizeRegSuffix, expSizeStr, baseRegister[isIOlistVar||leftStn->info.var.isIOlistVar], toSub);
}
if(arrVtype.vaType == DYN_R_ARR || arrVtype.vaType == DYN_ARR){
//get the right bound to expreg[1]
symTableNode *rightStn = arrVtype.ei.vt_id;
bool isIOlistVar = arrNode->stNode->info.var.isIOlistVar;
int toSub;
if(isIOlistVar){
toSub = scale *(arrNode->stNode->info.var.offset + arrBaseSize + (2*getSizeByType(g_INTEGER)));
}
else{
toSub = scale * (rightStn->info.var.offset + rightStn->info.var.vtype.width);
}
fprintf(fp,"\t xor %s, %s \n",expreg[1],expreg[1]);
fprintf(fp, "\t mov %s%s, %s[%s-%d] \n", expreg[1],expSizeRegSuffix, expSizeStr, baseRegister[isIOlistVar||rightStn->info.var.isIOlistVar], toSub);
}
if(arrVtype.vaType == DYN_R_ARR || arrVtype.vaType == STAT_ARR){
//get the left bound to expreg[0]
fprintf(fp,"\t mov %s, %d \n",expreg[0],arrVtype.si.vt_num);
}
if(arrVtype.vaType == DYN_L_ARR || arrVtype.vaType == STAT_ARR){
//get the right bound to expreg[1]
fprintf(fp,"\t mov %s, %d \n",expreg[1],arrVtype.ei.vt_num);
}
}
//prereq: lower bound in expreg[0], index in expreg[2]
//outcome: address of array element at idx in expreg[1]
//affects: expreg[2] value destroyed
void getArrAddrAtIdx(ASTNode *arrNode, FILE *fp){
char *expSizeStr, *expSizeRegSuffix;
fprintf(fp,"\t sub %s, %s \n",expreg[2],expreg[0]);
fprintf(fp,"\t add %s, %s \n",expreg[2],expreg[2]);
setExpSize(arrNode->stNode->info.var.vtype.baseType, &expSizeStr, &expSizeRegSuffix);
if(arrNode->stNode->info.var.vtype.baseType == g_INTEGER){
//size of int is 2words (4 bytes)
fprintf(fp,"\t add %s, %s \n",expreg[2],expreg[2]);
}
else if(arrNode->stNode->info.var.vtype.baseType == g_REAL){
fprintf(fp,"\t add %s, %s \n",expreg[2],expreg[2]);
fprintf(fp,"\t add %s, %s \n",expreg[2],expreg[2]);
}
fprintf(fp,"\t xor %s, %s \n",expreg[1],expreg[1]);
//br[isiolvar] - 2*(offset + arrBaseSize)
int toSub = scale * (arrNode->stNode->info.var.offset + arrBaseSize);
bool isIOlistVar = arrNode->stNode->info.var.isIOlistVar;
fprintf(fp,"\t movsx %s, word[%s-%d] \n",expreg[1],baseRegister[isIOlistVar],toSub);
fprintf(fp,"\t add %s, [stack_top] \n",expreg[1]);
fprintf(fp,"\t sub %s, %s \n",expreg[1],expreg[2]);
}
//prereq: lower bound in expreg[0], index in expreg[2]
//outcome: array value at idx in expreg[0]
//affects: expreg[2] value destroyed
void getArrValueAtIdxInReg(ASTNode *arrNode, FILE *fp){
char *expSizeStr, *expSizeRegSuffix;
getArrAddrAtIdx(arrNode,fp);
setExpSize(arrNode->stNode->info.var.vtype.baseType, &expSizeStr, &expSizeRegSuffix);
fprintf(fp,"\t xor %s, %s \n",expreg[0],expreg[0]);
fprintf(fp,"\t mov %s%s, %s[%s] \n",expreg[0],expSizeRegSuffix,expSizeStr,expreg[1]);
}
//prereq: lower bound in expreg[0], upper bound in expreg[1], index in expreg[2], preExpRSP already populated
void boundCheckArrAndExit(void *someRefPtr, FILE *fp) {
//someRefPtr is any unique address
fprintf(fp,"\t cmp %s, %s \n",expreg[2],expreg[0]);
fprintf(fp,"\t jge lb_ok_%p \n",someRefPtr);
fprintf(fp,"\t mov rsp, [preExpRSP] \n\t push r8 ;just for stack alignment\n");
RUNTIME_EXIT_WITH_ERROR(fp,"OUT_OF_BOUNDS");
fprintf(fp,"lb_ok_%p: \n",someRefPtr);
fprintf(fp,"\t cmp %s, %s \n",expreg[2],expreg[1]);
fprintf(fp,"\t jle rb_ok_%p \n",someRefPtr);
fprintf(fp,"\t mov rsp, [preExpRSP] \n\t push r8 ;just for stack alignment\n");
RUNTIME_EXIT_WITH_ERROR(fp,"OUT_OF_BOUNDS");
fprintf(fp,"rb_ok_%p: \n",someRefPtr);
}
void genExpr(ASTNode *astNode, FILE *fp, bool firstCall, gSymbol expType){
char *expSizeStr = "word";
char *expSizeRegSuffix = ""; //"d","w",""
setExpSize(expType,&expSizeStr,&expSizeRegSuffix);
if(firstCall){
if(astNode == NULL)
return;
else if(astNode->gs != g_assignmentStmt){
// printf("%d \n",astNode->gs);
genExpr(astNode->next,fp,true,expType);
genExpr(astNode->child,fp,true,expType);
return;
}
//assignmentStatement Node will be passed
ASTNode *idNode = astNode->child->child->child;
expType = idNode->stNode->info.var.vtype.baseType;
setExpSize(idNode->stNode->info.var.vtype.baseType,&expSizeStr,&expSizeRegSuffix);
//array element on LHS
if(idNode->next->next != NULL){
//get the result of the RHS expression on the stack
genExpr(idNode->next->next,fp,false,expType);
getArrBoundsInExpReg(idNode,fp);
//get the index dynamic or static in the eR[2]
if(idNode->next->gs == g_ID){
//dynamic index
ASTNode *idxIdNode = idNode->next;
bool isIOlistVar = idxIdNode->stNode->info.var.isIOlistVar;
int toSub = scale * (idxIdNode->stNode->info.var.offset + idxIdNode->stNode->info.var.vtype.width);
setExpSize(g_INTEGER,&expSizeStr,&expSizeRegSuffix);
fprintf(fp,"\t xor %s,%s \n",expreg[2],expreg[2]);
fprintf(fp,"\t mov %s%s, %s[%s-%d] \n",expreg[2],expSizeRegSuffix,expSizeStr,baseRegister[isIOlistVar],toSub);
}
else{
//static index
fprintf(fp,"\t mov %s, %d \n",expreg[2],idNode->next->tkinfo->value.num);
}
//now we have left bound in expreg[0], right bound in expreg[1] and index in expreg[2]
boundCheckArrAndExit(idNode->next, fp);
getArrAddrAtIdx(idNode,fp);
//now we have the target address in expreg[1]
setExpSize(idNode->stNode->info.var.vtype.baseType,&expSizeStr,&expSizeRegSuffix);
fprintf(fp,"\t pop %s \n",expreg[0]);
fprintf(fp, "\t mov %s[%s], %s%s \n", expSizeStr, expreg[1],expreg[0], expSizeRegSuffix);
return;
}
else{
//array assignment or variable
if(idNode->stNode->info.var.vtype.vaType == VARIABLE){
//variable
//get the RHS Expression's result on stack
genExpr(idNode->next,fp,false,expType);
bool isIOlistVar = idNode->stNode->info.var.isIOlistVar;
int toSub = scale * (idNode->stNode->info.var.offset + idNode->stNode->info.var.vtype.width);
fprintf(fp,"\t xor %s, %s \n",expreg[1],expreg[1]);
fprintf(fp,"\t pop %s \n",expreg[1]);
fprintf(fp, "\t mov %s[%s-%d], %s%s \n", expSizeStr, baseRegister[isIOlistVar], toSub, expreg[1],expSizeRegSuffix);
return;
}
else{
//array
ASTNode *arr1Node = idNode;
ASTNode *arr2Node = idNode->next->child;
getArrBoundsInExpReg(arr1Node,fp);
fprintf(fp,"\t mov %s, %s \n",expreg[2],expreg[0]);
fprintf(fp,"\t mov %s, %s \n",expreg[3],expreg[1]);
getArrBoundsInExpReg(arr2Node,fp);
fprintf(fp,"\t cmp %s, %s \n",expreg[0],expreg[2]);
fprintf(fp,"\t je lb_match_%p_%p \n",arr1Node,arr2Node);
fprintf(fp,"\t mov rsp, [preExpRSP] \n\t push r8 ;just for stack alignment\n");
RUNTIME_EXIT_WITH_ERROR(fp,"ARR_TYPE_MISMATCH");
fprintf(fp,"lb_match_%p_%p:\n",arr1Node,arr2Node);
fprintf(fp,"\t cmp %s, %s \n",expreg[1],expreg[3]);
fprintf(fp,"\t je rb_match_%p_%p \n",arr1Node,arr2Node);
fprintf(fp,"\t mov rsp, [preExpRSP] \n\t push r8 ;just for stack alignment\n");
RUNTIME_EXIT_WITH_ERROR(fp,"ARR_TYPE_MISMATCH");
fprintf(fp,"rb_match_%p_%p:\n",arr1Node,arr2Node);
//match successful, now copy
//get base address of arr2
int toSub2 = scale * (arr2Node->stNode->info.var.offset + arrBaseSize);
bool isIOlistVar2 = arr2Node->stNode->info.var.isIOlistVar;
fprintf(fp,"\t mov %sw, word[%s-%d] \n",expreg[0],baseRegister[isIOlistVar2],toSub2);
//copy base address to arr1
int toSub1 = scale * (arr1Node->stNode->info.var.offset + arrBaseSize);
bool isIOlistVar1 = arr1Node->stNode->info.var.isIOlistVar;
fprintf(fp,"\t mov word[%s-%d], %sw \n",baseRegister[isIOlistVar1],toSub1,expreg[0]);
/* //ARRAY COPY IMPLEMENTATION
fprintf(fp,"\t mov [asgnLB], %s \n",expreg[0]);
fprintf(fp,"\t mov [asgnRB], %s \n",expreg[1]);
//prereq: lower bound in expreg[0], index in expreg[2]
//outcome: address of array element at idx in expreg[1]
//affects: expreg[2] value destroyed
getArrAddrAtIdx(arr2Node,fp);
fprintf(fp,"\t mov %s, %s \n",expreg[3],expreg[1]);
fprintf(fp,"\t mov %s, [asgnLB] \n",expreg[0]); //left bound
fprintf(fp,"\t mov %s, [asgnRB] \n",expreg[1]); //right bound
fprintf(fp,"\t mov %s, [asgnLB] \n",expreg[2]); //first index
//prereq: lower bound in expreg[0], index in expreg[2]
//outcome: address of array element at idx in expreg[1]
//affects: expreg[2] value destroyed
getArrAddrAtIdx(arr1Node,fp);
fprintf(fp,"\t mov %s, %s \n",expreg[2],expreg[1]);
//NOW eR[2] contains addr of first element of arr1 & eR[3] contains addr of first element of arr2
fprintf(fp,"\t mov %s, [asgnLB] \n",expreg[0]); //current index in er0
setExpSize(arr1Node->stNode->info.var.vtype.baseType,&expSizeStr,&expSizeRegSuffix);
int toSub = scale * getSizeByType(arr1Node->stNode->info.var.vtype.baseType);
fprintf(fp,"arr_asgn_%p_%p: \n",arr1Node,arr2Node);
fprintf(fp,"\t xor %s, %s \n",expreg[1],expreg[1]);
//move data from arr2[i] to arr1[i]
fprintf(fp,"\t mov %s%s, %s[%s] \n",expreg[1],expSizeRegSuffix,expSizeStr,expreg[3]);
fprintf(fp,"\t mov %s[%s], %s%s \n",expSizeStr,expreg[2],expreg[1],expSizeRegSuffix);
//move index & offsets accordingly
fprintf(fp,"\t sub %s, %d \n",expreg[3],toSub);
fprintf(fp,"\t sub %s, %d \n",expreg[2],toSub);
fprintf(fp,"\t inc %s \n",expreg[0]);
fprintf(fp,"\t cmp %s, [asgnRB] \n",expreg[0]);
fprintf(fp,"\t jle arr_asgn_%p_%p \n",arr1Node,arr2Node);
*/
}
}
}
else{
if(astNode->gs == g_u){
ASTNode *uOp = astNode->child;
genExpr(uOp->next,fp,false,expType);
if(uOp->gs == g_MINUS){
fprintf(fp,"\t pop %s \n",expreg[1]);
fprintf(fp,"\t xor %s, %s \n",expreg[0],expreg[0]);
fprintf(fp,"\t sub %s,%s \n",expreg[0],expreg[1]);
fprintf(fp,"\t push %s \n", expreg[0]);
}
}
else if(astNode->gs == g_var_id_num){
astNode = astNode->child;
switch(astNode->gs){
case g_NUM:
fprintf(fp,"\t push %d \n",astNode->tkinfo->value.num);
break;
case g_RNUM:
//DONE: Mov real constant to stack
break;
case g_ID:
{
setExpSize(astNode->stNode->info.var.vtype.baseType,&expSizeStr,&expSizeRegSuffix);
if(astNode->stNode->info.var.vtype.vaType == VARIABLE){
bool isIOlistVar = astNode->stNode->info.var.isIOlistVar;
int toSub = scale * (astNode->stNode->info.var.offset + astNode->stNode->info.var.vtype.width);
switch(astNode->stNode->info.var.vtype.baseType){
case g_REAL:
//TODO: Handle real variables
break;
case g_INTEGER:
case g_BOOLEAN:
fprintf(fp,"\t xor %s,%s \n",expreg[0],expreg[0]);
fprintf(fp,"\t mov %s%s, %s[%s-%d] \n",expreg[0],expSizeRegSuffix,expSizeStr,baseRegister[isIOlistVar],toSub);
fprintf(fp,"\t push %s \n",expreg[0]);
break;
}
}
else{
//for bounds
getArrBoundsInExpReg(astNode,fp);
//move index to eR[2]
if(astNode->next->gs == g_ID){
ASTNode *idxIdNode = astNode->next;
bool isIOlistVar = idxIdNode->stNode->info.var.isIOlistVar;
int toSub = scale * (idxIdNode->stNode->info.var.offset + idxIdNode->stNode->info.var.vtype.width);
setExpSize(g_INTEGER,&expSizeStr,&expSizeRegSuffix);
fprintf(fp,"\t xor %s,%s \n",expreg[2],expreg[2]);
fprintf(fp,"\t mov %s%s, %s[%s-%d] \n",expreg[2],expSizeRegSuffix,expSizeStr,baseRegister[isIOlistVar],toSub);
}
else{
fprintf(fp,"\t mov %s, %d \n",expreg[2],astNode->next->tkinfo->value.num);
}
//now we have left bound in expreg[0], right bound in expreg[1] and index in expreg[2]
boundCheckArrAndExit(astNode->next, fp);
//toSub from array base
getArrValueAtIdxInReg(astNode,fp);
//now we have array value at index in eR[0]
fprintf(fp,"\t push %s \n",expreg[0]);
}
}
break;
}
}
else if(astNode->gs == g_TRUE){
fprintf(fp,"\t push 1 \n");
}
else if(astNode->gs == g_FALSE){
fprintf(fp,"\t push 0 \n");
}
else{
//astnode is an operator
genExpr(astNode->child,fp,false,expType);
genExpr(astNode->child->next,fp,false,expType);
if(expType == g_REAL){
//handle real operations
}
else{
//pop the right subtree's result
fprintf(fp,"\t xor %s,%s \n",expreg[1],expreg[1]);
fprintf(fp,"\t pop %s \n",expreg[1]);
//pop the left subtree's result
fprintf(fp,"\t xor %s,%s \n",expreg[0],expreg[0]);
fprintf(fp,"\t pop %s \n",expreg[0]);
char *jCmd = NULL;
switch(astNode->gs){
case g_PLUS:
fprintf(fp,"\t add %s, %s \n",expreg[0],expreg[1]);
break;
case g_MINUS:
fprintf(fp,"\t sub %s, %s \n",expreg[0],expreg[1]);
break;
case g_MUL:
setExpSize(g_INTEGER,&expSizeStr,&expSizeRegSuffix);
fprintf(fp,"\t push rax \n\t push rdx \n\t xor rdx,rdx \n"); //to save the prev value
fprintf(fp,"\t mov rax, %s \n",expreg[0]);
fprintf(fp,"\t imul %s%s \n",expreg[1],expSizeRegSuffix);
fprintf(fp,"\t mov %s, rax \n",expreg[0]);
fprintf(fp,"\t pop rdx \n\t pop rax \n"); //to restore the prev value
break;
case g_DIV:
setExpSize(g_INTEGER,&expSizeStr,&expSizeRegSuffix);
fprintf(fp,"\t push rax \n\t push rdx \n\t xor rdx,rdx \n"); //to save the prev value
fprintf(fp,"\t mov rax, %s \n",expreg[0]);
fprintf(fp,"\t cmp %s%s, 0 \n",expreg[1],expSizeRegSuffix);
fprintf(fp,"\t jne div0_safe_%p \n",astNode);
fprintf(fp,"\t mov rsp, [preExpRSP] \n\t push r8 ;just for stack alignment\n");
RUNTIME_EXIT_WITH_ERROR(fp,"DIV_BY_ZERO");
fprintf(fp,"div0_safe_%p: \n",astNode);
fprintf(fp,"\t idiv %s%s \n",expreg[1],expSizeRegSuffix);
fprintf(fp,"\t mov %s, rax \n",expreg[0]);
fprintf(fp,"\t pop rdx \n\t pop rax \n"); //to restore the prev value
break;
case g_AND:
fprintf(fp,"\t and %s, %s \n",expreg[0],expreg[1]);
break;
case g_OR:
fprintf(fp,"\t or %s, %s \n",expreg[0],expreg[1]);
break;
case g_GT:
jCmd = "jg";
break;
case g_LT:
jCmd = "jl";
break;
case g_GE:
jCmd = "jge";
break;
case g_LE:
jCmd = "jle";
break;
case g_EQ:
jCmd = "je";
break;
case g_NE:
jCmd = "jne";
break;
}
if(jCmd != NULL){
setExpSize(g_INTEGER,&expSizeStr,&expSizeRegSuffix);
fprintf(fp,"\t cmp %s%s, %s%s \n",expreg[0],expSizeRegSuffix,expreg[1],expSizeRegSuffix);
fprintf(fp,"\t %s exp_t_%p \n",jCmd,(void *)astNode);
fprintf(fp,"\t mov %s, 0 \n",expreg[0]);
fprintf(fp,"\t jmp exp_f_%p \n",(void *)astNode);
fprintf(fp," exp_t_%p:\n",(void*)astNode);
fprintf(fp,"\t mov %s, 1 \n",expreg[0]);
fprintf(fp," exp_f_%p:\n",(void*)astNode);
}
fprintf(fp,"\t push %s \n",expreg[0]);
}
return;
}
}
}
void generateCode(ASTNode* root, symbolTable* symT, FILE* fp) {
if(root == NULL) return;
gSymbol gs = root->gs;
switch(gs) {
case g_program:
{
fprintf(fp, "section .bss \n");
fprintf(fp, "\t stack_top: resb 8 \n");
fprintf(fp, "\t inta: resb 4 \n");
fprintf(fp, "\t floatb: resb 8 \n");
fprintf(fp, "\t boolc: resb 2 \n");
fprintf(fp,"\t asgnLB: resb 8 \n");
fprintf(fp,"\t asgnRB: resb 8 \n");
fprintf(fp,"\t preExpRSP: resb 8 \n");
fprintf(fp, "section .data \n");
fprintf(fp,"\t msgBoolean: db \"Input: Enter a boolean value\", 10, 0 \n");
fprintf(fp,"\t inputBoolean: db \"%%hd\", 0 \n");
fprintf(fp,"\t msgBooleanArr: db \"Input: Enter %%d array elements of boolean type for range %%d to %%d\", 10, 0 \n");
fprintf(fp,"\t msgInt: db \"Input: Enter an integer value\", 10, 0 \n");
fprintf(fp,"\t inputInt: db \"%%d\", 0 \n");
fprintf(fp,"\t msgIntArr: db \"Input: Enter %%d array elements of integer type for range %%d to %%d\", 10, 0 \n");
fprintf(fp,"\t msgFloat: db \"Input: Enter a float value\", 10, 0 \n");
fprintf(fp,"\t inputFloat: db \"%%lf\",0 \n");
fprintf(fp,"\t outputBooleanTrue: db \"Output: true\", 10, 0, \n");
fprintf(fp,"\t outputBooleanFalse: db \"Output: false\", 10, 0, \n");
fprintf(fp,"\t outputInt: db \"Output: %%d\", 10, 0, \n");
fprintf(fp,"\t outputFloat: db \"Output: %%lf\", 10, 0, \n");
fprintf(fp,"\t output: db \"Output: \", 0 \n");
fprintf(fp,"\t intHolder: db \"%%d \", 0 \n");
fprintf(fp,"\t booleanTrue: db \"true \", 0 \n");
fprintf(fp,"\t booleanFalse: db \"false \", 0 \n");
fprintf(fp,"\t newLine: db \" \", 10, 0 \n");
fprintf(fp, "\t OUT_OF_BOUNDS: db \"RUN TIME ERROR: Array index out of bound\", 10, 0 \n");
fprintf(fp, "\t ARR_TYPE_MISMATCH: db \"RUN TIME ERROR: Bounds do not match for LHS Array and RHS Array\", 10, 0 \n");
fprintf(fp, "\t ARR_TYPE_MISMATCH2: db \"RUN TIME ERROR: Bounds do not match for formal and actual argument arrays\", 10, 0 \n");
fprintf(fp, "\t UPPER_BOUND_SMALL: db \"RUN TIME ERROR: Upper bound of dynamic array is smaller than lower bound\", 10, 0 \n");
fprintf(fp, "\t DIV_BY_ZERO: db \"RUN TIME ERROR: Division by Zero.\", 10, 0 \n");
fprintf(fp, "\n section .text \n");
fprintf(fp, "\t global main \n");
fprintf(fp, "\t extern scanf \n");
fprintf(fp, "\t extern printf \n");
ASTNode* ASTChild = root->child;
// Might need to change its position.
while(ASTChild) {
generateCode(ASTChild, symT, fp);
ASTChild = ASTChild->next;
}
return;
}
case g_moduleDeclarations:
case g_otherModules:
case g_statements:
{
// moduleDeclarations -> ID moduleDeclarations
ASTNode* ASTChild = root->child;
while(ASTChild) {
generateCode(ASTChild, symT, fp);
ASTChild = ASTChild->next;
}
return;
}
case g_module:
{
//<module> -> DEF MODULE ID ENDDEF TAKES INPUT SQBO <input_plist> SQBC SEMICOL <ret> <moduleDef>
ASTNode *functionID = root->child;
ASTNode *inputList = functionID->next;
ASTNode *outputList = inputList->next;
ASTNode *moduleDef = outputList->next;
if(outputList->gs == g_moduleDef)
moduleDef = outputList;
fprintf(fp, "\n %s: \n", functionID->tkinfo->lexeme);
fprintf(fp, "\t ; stack init starts \n");
fprintf(fp, "\t mov rbp, rsp \n");
fprintf(fp, "\t sub rsp, %d \n", getActivationRecordSize(functionID->tkinfo->lexeme, symT));
fprintf(fp, "\t ; stack init done. \n\n");
generateCode(moduleDef, symT, fp);
fprintf(fp, "\t mov rsp, rbp \n");
fprintf(fp, "\t ret \n");
return;
}
case g_DRIVER:
{
fprintf(fp, "\n main: \n");
fprintf(fp, "\t ; stack init starts \n");
fprintf(fp, "\t mov rbp, rsp \n");
fprintf(fp, "\t mov QWORD[stack_top], rsp \n");
fprintf(fp, "\t sub rsp, %d \n", getActivationRecordSize("@driver", symT));
fprintf(fp, "\t ; stack init done. \n\n");
generateCode(root->child, symT, fp);
fprintf(fp, "\t mov rsp, rbp \n");
fprintf(fp, "\t push rsp \n");
fprintf(fp, "\t mov rax, 60 \n");
fprintf(fp, "\t xor rdi, rdi \n");
fprintf(fp, "\t syscall \n");
fprintf(fp, "\t ; driver ends \n\n");
return;
}
case g_moduleDef:
case g_simpleStmt:
case g_START:
{
generateCode(root->child, symT, fp);
return;
}
case g_ID:
{
return;
}
case g_iterativeStmt:
{
generateCode(root->child, symT, fp);
return;
}
case g_FOR:
{
ASTNode* idNode = root->next; // ID
ASTNode* rangeNode = idNode->next;
ASTNode* startNode = rangeNode->child;
ASTNode* endNode = startNode->next;
ASTNode* statementsNode = rangeNode->next->child;
symVarInfo idNodeVar = idNode->stNode->info.var;
// Experimental
fprintf(fp, "\t mov dword[%s - %d], %s \n", baseRegister[idNodeVar.isIOlistVar], 2*(idNodeVar.vtype.width + idNodeVar.offset), startNode->tkinfo->lexeme);
fprintf(fp, "\t cmp dword[%s - %d], %s \n", baseRegister[idNodeVar.isIOlistVar], 2*(idNodeVar.vtype.width + idNodeVar.offset), endNode->tkinfo->lexeme);
fprintf(fp, "\t ja forLoopEnd_%p \n", endNode);
fprintf(fp, "forLoopStart_%p: \n", startNode);
generateCode(statementsNode, symT, fp);
fprintf(fp, "\t inc dword[%s - %d] \n", baseRegister[idNodeVar.isIOlistVar], 2*(idNodeVar.vtype.width + idNodeVar.offset));
fprintf(fp, "\t cmp dword[%s - %d], %s \n", baseRegister[idNodeVar.isIOlistVar], 2*(idNodeVar.vtype.width + idNodeVar.offset), endNode->tkinfo->lexeme);
fprintf(fp, "\t jna forLoopStart_%p \n", startNode);
fprintf(fp, "forLoopEnd_%p: \n", endNode);
return;
}
case g_WHILE:
{
fprintf(fp,"\t ; WHILE starts \n");
fprintf(fp," WHILE_loop_%p: \n", root->next->next); //pointer to WHILE's START ASTNode (uniqueness assured across entire code)
fprintf(fp,"\t ; evaluating while condition \n");
fprintf(fp,"\t mov [preExpRSP], rsp \n"); //store the pre expression rsp into preExpRSP
genExpr(root->next, fp, false, g_BOOLEAN); // puts 8 bytes containing result of condition evaluation on stack
fprintf(fp,"\t ; while condition evaluated \n");
fprintf(fp,"\t pop r8 \n"); // pop in r8
fprintf(fp,"\t cmp r8, 0 \n"); // cmp r8, 0
fprintf(fp,"\t ; exit the loop if condition was false \n");
fprintf(fp,"\t jz EXIT_WHILE_loop_%p \n", root);
fprintf(fp,"\t ; execute following statements if condition was true \n");
fprintf(fp,"\t ; while loop statements start \n");
generateCode(root->next->next->child, symT, fp); // recurse on statements
fprintf(fp,"\t ; while loop statements end \n");
fprintf(fp,"\t jmp WHILE_loop_%p \n", root->next->next);
fprintf(fp," EXIT_WHILE_loop_%p: \n", root); //pointer to WHILE's ASTNode (uniqueness assured across entire code)
fprintf(fp,"\t ; WHILE ends \n");
}
return;
case g_moduleReuseStmt:
{
ASTNode *idOrAssignop = root->child;
ASTNode *outputList = NULL, *functionID, *inputList;
if(idOrAssignop->gs == g_ASSIGNOP) {
outputList = idOrAssignop->child; // If present, return values
functionID = outputList->next;
inputList = functionID->next;
}
else {
functionID = idOrAssignop;
inputList = functionID->next;
}
symFuncInfo *finfo = stGetFuncInfo(functionID->tkinfo->lexeme, symT);
symTableNode *inputParam = finfo->inpPListHead;
symTableNode *outputParam = finfo->outPListHead;
ASTNode *idNode = inputList->child;
char *sizeStr, *regSuffix;
while(idNode != NULL) {
varType actualVarType = idNode->stNode->info.var.vtype;
symVarInfo actualVar = idNode->stNode->info.var;
varType formalVarType = inputParam->info.var.vtype;
symVarInfo formalVar = inputParam->info.var;
if (actualVarType.vaType == VARIABLE) {
setExpSize(actualVarType.baseType, &sizeStr, ®Suffix);
fprintf(fp, "\t mov r12%s, %s [%s - %d] \n", regSuffix, sizeStr, baseRegister[actualVar.isIOlistVar], scale * (actualVarType.width + actualVar.offset));
fprintf(fp, "\t mov %s [rsp - %d], r12%s \n", sizeStr, scale * (formalVarType.width + formalVar.offset), regSuffix);
}
else {
getArrBoundsInExpReg(idNode, fp);
fprintf(fp, "\t mov dword [rsp - %d], %sd \n", scale * (arrBaseSize + formalVar.offset + getSizeByType(g_INTEGER)), expreg[0]); // lb
fprintf(fp, "\t mov dword [rsp - %d], %sd \n", scale * (arrBaseSize + formalVar.offset + 2*getSizeByType(g_INTEGER)), expreg[1]); // ub
fprintf(fp, "\t mov r12, %s \n", expreg[0]);
fprintf(fp, "\t mov r13, %s \n", expreg[1]);
fprintf(fp, "\t mov rsi, %s \n", baseRegister[actualVar.isIOlistVar]);
fprintf(fp, "\t sub rsi, %d \n", scale * (arrBaseSize + actualVar.offset));
fprintf(fp, "\t movsx rsi, word[rsi] \n"); // move base val
fprintf(fp, "\t mov word [rsp - %d], si \n", scale * (arrBaseSize + formalVar.offset)); // base address
varType arrVtype = inputParam->info.var.vtype;
if(arrVtype.vaType == DYN_R_ARR || arrVtype.vaType == STAT_ARR){
//get the left bound to expreg[0]
fprintf(fp,"\t mov %s, %d \n",expreg[0],arrVtype.si.vt_num);
fprintf(fp, "\t cmp %s, r12 \n", expreg[0]);
fprintf(fp, "\t je lbound_match_%p \n", idNode);
fprintf(fp, "\t push rbx \n");
RUNTIME_EXIT_WITH_ERROR(fp, "ARR_TYPE_MISMATCH2");
fprintf(fp, "\t pop rbx \n");
}
fprintf(fp, "lbound_match_%p: \n", idNode);
if(arrVtype.vaType == DYN_L_ARR || arrVtype.vaType == STAT_ARR){
//get the right bound to expreg[1]
fprintf(fp,"\t mov %s, %d \n",expreg[1],arrVtype.ei.vt_num);
fprintf(fp, "\t cmp %s, r13 \n", expreg[1]);
fprintf(fp, "\t je ubound_match_%p \n", idNode);
fprintf(fp, "\t push rbx \n");
RUNTIME_EXIT_WITH_ERROR(fp, "ARR_TYPE_MISMATCH2");
fprintf(fp, "\t pop rbx \n");
}
fprintf(fp, "ubound_match_%p: \n", idNode);
}
inputParam = inputParam->next;
idNode = idNode->next;
}
fprintf(fp, "\t sub rsp, %d \n", getIOlistSize(functionID->tkinfo->lexeme, symT));
fprintf(fp, "\t push rbp \n");
fprintf(fp, "\t push rbx \n");
fprintf(fp, "\t mov rbx, rsp \n");
fprintf(fp, "\t add rbx, %d \n", getIOlistSize(functionID->tkinfo->lexeme, symT) + 16);
fprintf(fp, "\t push rsi \n"); // Odd no of register
fprintf(fp, "\t call %s \n", functionID->tkinfo->lexeme);
fprintf(fp, "\t pop rsi \n");
fprintf(fp, "\t pop rbx \n");
fprintf(fp, "\t pop rbp \n");
fprintf(fp, "\t add rsp, %d \n", getIOlistSize(functionID->tkinfo->lexeme, symT));
if(outputList) {
idNode = outputList->child;
while(idNode != NULL) {
varType actualVarType = idNode->stNode->info.var.vtype;
symVarInfo actualVar = idNode->stNode->info.var;
varType formalVarType = outputParam->info.var.vtype;
symVarInfo formalVar = outputParam->info.var;
fprintf(fp, "\t mov rsi, %s \n", baseRegister[actualVar.isIOlistVar]);
fprintf(fp, "\t sub rsi, %d \n", scale * (actualVarType.width + actualVar.offset));
setExpSize(actualVarType.baseType, &sizeStr, ®Suffix);
fprintf(fp, "\t mov r12%s, %s [rsp - %d] \n", regSuffix, sizeStr, scale * (formalVarType.width + formalVar.offset));
fprintf(fp, "\t mov %s [rsi], r12%s \n", sizeStr, regSuffix);
outputParam = outputParam->next;
idNode = idNode->next;
}
}
return;
}
case g_ioStmt:
{
generateCode(root->child, symT, fp);
return;
}
case g_GET_VALUE:
{
ASTNode* siblingId = root->next;
fprintf(fp,"\t ; GET_VALUE(%s: %s) starts\n", siblingId->tkinfo->lexeme, inverseMappingTable[siblingId->stNode->info.var.vtype.baseType]);
// <ioStmt> -> GET_VALUE BO ID BC SEMICOL
if(! siblingId->stNode) {
// Undeclared variable. Must have been already handled.
return;
}
varType idVarType = siblingId->stNode->info.var.vtype;
symVarInfo idVar = siblingId->stNode->info.var;
if(idVarType.vaType == VARIABLE) {
// More registers need to me pushed to preserve
// their values.
// BEWARE: Number of pushes here should be odd.
// push rbx
fprintf(fp, "\t push rbp \n");
if(idVarType.baseType == g_BOOLEAN) {
fprintf(fp, "\t mov rdi, msgBoolean \n ");
fprintf(fp, "\t call printf \n ");
fprintf(fp, "\t mov rdi, inputBoolean \n");
fprintf(fp, "\t mov rsi, %s \n", baseRegister[idVar.isIOlistVar]);
fprintf(fp, "\t sub rsi, %d \n", 2 * (idVarType.width + idVar.offset));
fprintf(fp, "\t call scanf \n");
}
else if(idVarType.baseType == g_INTEGER) {
fprintf(fp, "\t mov rdi, msgInt \n");
fprintf(fp, "\t call printf \n");
fprintf(fp, "\t mov rdi, inputInt \n");
fprintf(fp, "\t mov rsi, %s \n", baseRegister[idVar.isIOlistVar]);
fprintf(fp, "\t sub rsi, %d \n", 2 * (idVarType.width + idVar.offset));
fprintf(fp, "\t call scanf \n");
// Check the value being scanned
// fprintf(fp, "\t mov rdi, outputInt \n");
// fprintf(fp, "\t mov rsi, [inta] \n");
// fprintf(fp, "\t call printf \n");
}
else if(idVarType.baseType == g_REAL) {
fprintf(fp, "\t mov rdi, msgFloat \n");
fprintf(fp, "\t call printf \n");
fprintf(fp, "\t mov rdi, inputFloat \n");
fprintf(fp, "\t mov rsi, %s \n", baseRegister[idVar.isIOlistVar]);
fprintf(fp, "\t sub rsi, %d \n", 2 * (idVarType.width + idVar.offset));
fprintf(fp, "\t call scanf \n");
}
fprintf(fp, "\t pop rbp \n");
}
// Scan whole array
else {
fprintf(fp, "\t push rbp \n");
if(idVarType.baseType == g_INTEGER)
fprintf(fp, "\t mov rdi, msgIntArr \n");
else if(idVarType.baseType == g_BOOLEAN)
fprintf(fp, "\t mov rdi, msgBooleanArr \n");
else{
printf("Real array!\n");
return;
}
getArrBoundsInExpReg(siblingId, fp);
fprintf(fp, "\t mov r12, %s \n", expreg[0]); // lb
fprintf(fp, "\t mov r13, %s \n", expreg[1]); // ub
fprintf(fp, "\t sub %s, %s \n", expreg[1], expreg[0]);
fprintf(fp, "\t inc %s \n", expreg[1]);
fprintf(fp, "\t mov rsi, %s \n", expreg[1]); // no of elems
fprintf(fp, "\t mov rdx, r12 \n"); // lb
fprintf(fp, "\t mov rcx, r13 \n"); // ub
fprintf(fp, "\t call printf \n");
fprintf(fp, "\t mov rsi, %s \n", baseRegister[idVar.isIOlistVar]); // isIOlistVar may be 0 or 1
fprintf(fp, "\t sub rsi, %d \n", scale * (arrBaseSize + idVar.offset));
fprintf(fp, "\t movsx rsi, word[rsi] \n"); // move base val
fprintf(fp, "\t add rsi, [stack_top] \n"); // address of first elem!
if(idVarType.baseType == g_INTEGER)
fprintf(fp, "\t mov rdi, inputInt \n");
else if(idVarType.baseType == g_BOOLEAN)
fprintf(fp, "\t mov rdi, inputBoolean \n");
fprintf(fp, "scan_arr_%p: \n", siblingId);
fprintf(fp, "\t push rsi \n");
fprintf(fp, "\t push rdi \n");
fprintf(fp, "\t call scanf \n");
fprintf(fp, "\t pop rdi \n");
fprintf(fp, "\t pop rsi \n");
fprintf(fp, "\t cmp r12, r13 \n"); // ub
fprintf(fp, "\t jz scan_arr_exit_%p \n", siblingId);
fprintf(fp, "\t inc r12 \n");
fprintf(fp, "\t sub rsi, %d \n", scale * getSizeByType(idVarType.baseType)); // address of n-th elem
fprintf(fp, "\t jmp scan_arr_%p \n", siblingId);
fprintf(fp, "scan_arr_exit_%p: \n", siblingId);
fprintf(fp, "\t pop rbp \n");
}
fprintf(fp,"\t ; GET_VALUE(%s: %s) ends \n\n", siblingId->tkinfo->lexeme, inverseMappingTable[siblingId->stNode->info.var.vtype.baseType]);
return;
}
case g_PRINT:
{
ASTNode* sibling = root->next;
// <ioStmt> -> PRINT BO <var> BC SEMICOL
// <boolConstt> -> TRUE | FALSE
// <var_id_num> -> ID <whichId> | NUM | RNUM
// <var> -> <var_id_num> | <boolConstt>
// <whichId> -> SQBO <index> SQBC | ε
// <index> -> NUM | ID
if(sibling->gs == g_TRUE) {
fprintf(fp,"\t ; PRINT(true) starts\n");
fprintf(fp, "\t push rbp \n");
fprintf(fp, "\t mov rdi, outputBooleanTrue \n");
fprintf(fp, "\t call printf \n");
fprintf(fp, "\t pop rbp \n");
fprintf(fp,"\t ; PRINT(true) ends \n\n");
return;
}
if(sibling->gs == g_FALSE) {
fprintf(fp,"\t ; PRINT(false) starts \n");
fprintf(fp, "\t push rbp \n");
fprintf(fp, "\t mov rdi, outputBooleanFalse \n");
fprintf(fp, "\t call printf \n");
fprintf(fp, "\t pop rbp \n");
fprintf(fp,"\t ; PRINT(false) ends \n\n");
return;
}
ASTNode *siblingId = sibling->child;
if(siblingId->gs == g_NUM) {
fprintf(fp,"\t ; PRINT(%d) starts \n",siblingId->tkinfo->value.num);
fprintf(fp, "\t push rbp \n");
fprintf(fp, "\t mov rdi, outputInt \n");
fprintf(fp, "\t mov rsi, %d \n", siblingId->tkinfo->value.num);
fprintf(fp, "\t call printf \n");
fprintf(fp, "\t pop rbp \n");
fprintf(fp,"\t ; PRINT(%d) ends \n\n", siblingId->tkinfo->value.num);
return;
}
if(siblingId->gs == g_RNUM) {
fprintf(fp,"\t ; PRINT(%lf) starts \n",siblingId->tkinfo->value.rnum);
fprintf(fp, "\t push rbp \n");
fprintf(fp, "\t mov rdi, outputFloat \n");
fprintf(fp, "\t mov rsi, __float64__(%s) \n", siblingId->tkinfo->lexeme);
fprintf(fp, "\t movq xmm0, rsi \n");
fprintf(fp, "\t mov rax, 1 \n");
fprintf(fp, "\t call printf \n");
fprintf(fp, "\t pop rbp \n");
fprintf(fp,"\t ; PRINT(%lf) ends \n\n",siblingId->tkinfo->value.rnum);
return;
}
varType idVarType = siblingId->stNode->info.var.vtype;
symVarInfo idVar = siblingId->stNode->info.var;
if(siblingId->next != NULL) {
// Individual element of array is being accessed!
ASTNode *idOrNum = siblingId->next;
fprintf(fp, "\t push rbp \n");
// The only registers that the called function is required to preserve (the calle-save registers) are:
// rbp, rbx, r12, r13, r14, r15. All others are free to be changed by the called function.
if(idVarType.baseType == g_BOOLEAN) {
getArrBoundsInExpReg(siblingId, fp);
if(idOrNum->gs == g_NUM) {
fprintf(fp, "\t mov %s, %d \n", expreg[2], idOrNum->tkinfo->value.num);
}
else {
varType idVarType = idOrNum->stNode->info.var.vtype;
symVarInfo idVar = idOrNum->stNode->info.var;
// DWORD because ID will (and should) always be of type INTEGER.
fprintf(fp, "\t movsx %s, DWORD [%s - %d] \n", expreg[2], baseRegister[idVar.isIOlistVar], scale * (idVarType.width + idVar.offset));
}
fprintf(fp, "\t cmp %s, %s \n", expreg[2], expreg[1]);
fprintf(fp, "\t jbe stat_valid_ub_%p \n", idOrNum); // UB satisfied