-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeGen.c
1710 lines (1501 loc) · 53.9 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
/* GROUP 48:
PUNEET ANAND 2016B4A70487P
MAYANK JASORIA 2016B1A70703P
SHUBHAM TIWARI 2016B4A70935P
VIBHAV OSWAL 2016B4A70594P */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "data_structures/st.h"
#include "data_structures/ast.h"
#include "codeGen.h"
#include "codeGenDef.h"
// void rte(/* char* errorMsg */) {
// fprintf(fp, "; --- START: rte() --- \n");
// /**
// * TODO: Print error message
// * RUN TIME ERROR: Array index out of bound
// */
// fprintf(fp, "\tmov ebx, 0 ;return 0 status on exit - 'No errors'\n");
// fprintf(fp, "\tmov eax, 1 ;invoke SYS_EXIT system call (kernel opcode 1)\n");
// fprintf(fp, "\tint 80h ;generate interrupt\n");
// fprintf(fp, "; --- END: rte() --- \n");
// }
void emitCodeInit(const char* fname) {
/* Open fname */
fp = fopen(fname, "w");
}
void pushRegs() {
fprintf(fp, "\tpush r10\n");
fprintf(fp, "\tpush r11\n");
fprintf(fp, "\tpush rsi\n");
fprintf(fp, "\tpush rdx\n");
fprintf(fp, "\tpush rcx\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tpush r9\n");
fprintf(fp, "\tpush rax\n");
}
void popRegs() {
fprintf(fp, "\tpop rax\n");
fprintf(fp, "\tpop r9\n");
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tpop rcx\n");
fprintf(fp, "\tpop rdx\n");
fprintf(fp, "\tpop rsi\n");
fprintf(fp, "\tpop r11\n");
fprintf(fp, "\tpop r10\n");
}
void alignStack() {
pushRegs();
fprintf(fp, "; --- START: ALIGN STACK---\n");
fprintf(fp, "\tmov qword [rspreserve], rsp\n");
fprintf(fp, "\tand rsp, 0xfffffffffffffff0\n");
fprintf(fp, "\tsub rsp, 10000B\n");
fprintf(fp, "; --- END: ALIGN STACK ---\n");
}
void getBackStack() {
fprintf(fp, "\tmov rsp, qword [rspreserve]\n");
popRegs();
}
void emitCodeFinalize() {
fprintf(fp, "rte:\n");
fprintf(fp, "\tmov ebx, 0 ;return 0 status on exit - 'No errors'\n");
fprintf(fp, "\tmov eax, 1 ;invoke SYS_EXIT system call (kernel opcode 1)\n");
fprintf(fp, "\tint 80h ;generate interrupt\n");
fprintf(fp, "\noob:\n");
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, rte_oob\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tjmp rte\n");
fprintf(fp, "\ntypeError:\n");
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, rte_type\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tjmp rte\n");
fprintf(fp, "\nparam:\n");
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, rte_param\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tjmp rte\n");
fprintf(fp, "\ninvalidbounds:\n");
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, rte_invalidbounds\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tjmp rte\n");
fprintf(fp, "\ndivisionby0:\n");
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, rte_divisionby0\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tjmp rte\n");
if (fp) {
fclose(fp);
fp = NULL;
}
}
void emitCodeChildren(ASTNode * head, char * fname) {
ASTNode* ch = head;
while(ch != NULL) {
emitCodeAST(ch, fname);
ch = ch -> next;
}
}
void asmComment(char * str) {
fprintf(fp, "\n; ### %s ### \n", str);
}
int getLastCaseLabel(ASTNode * curr) {
ASTNode * tmp = curr -> child -> next;
while(tmp -> child -> next -> next != NULL) {
tmp = tmp -> child -> next -> next;
}
return tmp -> nodeData.caseStmt -> lastLabel;
}
// void rte(char * str, int line_num) {
// fprintf(fp, "\tpush rbp\n");
// fprintf(fp, "\tmov rdi, %s\n", str);
// fprintf(fp, "\tmov si, %dd\n", line_num);
// alignStack();
// fprintf(fp, "\tcall printf\n");
// getBackStack();
// fprintf(fp, "\tpop rbp\n");
// fprintf(fp, "\tjmp rte\n");
// }
int getIDOffset(ASTNode * idNode) {
SymTableVar * tmp = fetchVarData(idNode -> parent -> localST, idNode -> nodeData.leaf -> tn -> lex, idNode -> nodeData.leaf -> tn -> line_num);
return tmp -> offset;
}
/**
* Returns temporary offset of the node in the expression
*/
int getExprOffset(ASTNode * expr) {
if(expr -> type == AST_NODE_LEAF)
return expr -> nodeData.leaf -> temporaryOffset;
else if(expr -> type == AST_NODE_VARIDNUM)
return expr -> nodeData.var -> temporaryOffset;
else if(expr -> type == AST_NODE_AOBEXPR)
return expr -> nodeData.AOBExpr -> temporaryOffset;
else if(expr -> type == AST_NODE_UNARY)
return expr -> nodeData.unary -> temporaryOffset;
}
/**
* Populates r10w : left index
* r11w : right index
*/
void getLeftRightIndex(SymTableVar * id) {
SymTableFunc * local = id -> table;
SymTableFunc * func = getParentFunc(local);
if(id -> offset < func -> inputSize) {
int lft, right;
int offset = id -> offset;
if(strcmp(id -> sdt.r -> lowId, "") == 0) {
lft = id -> sdt.r -> low;
fprintf(fp, "\tmov r10w, %dd\n", lft);
}
else {
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", offset + typeSize[AST_TYPE_POINTER] + typeSize[AST_TYPE_INT]);
fprintf(fp, "\tmov r10w, word [rax]\n");
}
if(strcmp(id -> sdt.r -> highId, "") == 0) {
right = id -> sdt.r -> high;
fprintf(fp, "\tmov r11w, %dd\n", right);
}
else {
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", offset + typeSize[AST_TYPE_POINTER] + 2*typeSize[AST_TYPE_INT]);
fprintf(fp, "\tmov r11w, word [rax]\n");
}
return;
}
fprintf(fp, "; --- START: get left and right index of %s ---\n", id->name);
int lft, right;
if(strcmp(id -> sdt.r -> lowId, "") == 0) {
lft = id -> sdt.r -> low;
fprintf(fp, "\tmov r10w, %dd\n", lft);
}
else {
SymTableVar * l = fetchVarData(id -> table, id -> sdt.r -> lowId, id -> declarationLine);
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", typeSize[AST_TYPE_INT] + l -> offset);
fprintf(fp, "\tmov r10w, word[rax]\n");
}
if(strcmp(id -> sdt.r -> highId, "") == 0) {
right = id -> sdt.r -> high;
fprintf(fp, "\tmov r11w, %dd\n", right);
}
else {
SymTableVar * r = fetchVarData(id -> table, id -> sdt.r -> highId, id -> declarationLine);
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", typeSize[AST_TYPE_INT] + r -> offset);
fprintf(fp, "\tmov r11w, word[rax]\n");
}
fprintf(fp, "; --- END: got left and right index of %s in r10w and r11w --- \n", id->name);
}
void getInputElement(astDataType t) {
fprintf(fp, "; START: --- getInputElement() ---\n");
fprintf(fp, "\tpush rbp\n");
if(t == AST_TYPE_INT)
fprintf(fp, "\tmov rdi, fmt_int\n");
else if(t == AST_TYPE_REAL)
fprintf(fp, "\tmov rdi, fmt_float\n");
else if(t == AST_TYPE_BOOLEAN)
fprintf(fp, "\tmov rdi, fmt_bool\n");
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, r9\n");
fprintf(fp, "\tmov rsi, rax\n");
alignStack();
fprintf(fp, "\tcall scanf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "; --- END: getInputElement() --- \n");
}
/**
* r9 <- offset relative to base address of array, the offset, base address rdx
* @param arr AST node of the array
* @param index AST Node specifying index
*/
void fetchArraybyIndex(ASTNode * arr, ASTNode * index) {
fprintf(fp, "; --- START: fetchArraybyIndex() for array %s: base: rdx, offset: r9 --- \n", arr -> nodeData.leaf -> tn -> lex);
/* Array index */
ASTNode * i = index;
/* Fetch array from symbol table */
SymTableVar * id = fetchVarData(arr -> parent -> localST, arr -> nodeData.leaf -> tn -> lex, arr -> nodeData.leaf -> tn -> line_num);
/* DataType of elements of array */
astDataType type = id -> sdt.r -> dataType;
int offset = id -> offset;
int idx;
/* store the value of the index in r8w */
if(i -> nodeData.leaf -> type == AST_LEAF_IDXNUM) {
/* index is of type NUM */
idx = i -> nodeData.leaf -> tn -> value.val_int;
fprintf(fp, "\tmov r8w, %dd\n", idx);
}
else {
/* index is of type ID */
SymTableVar * tmp = fetchVarData(arr -> parent -> localST, i -> nodeData.leaf -> tn -> lex, i -> nodeData.leaf -> tn -> line_num);
// if(tmp -> isAssigned == 0) {
// rte();
// }
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", typeSize[AST_TYPE_INT] + tmp -> offset);
fprintf(fp, "\tmov r8w, word [rax]\n");
}
fprintf(fp, "\tmov rsi, %dd\n", i -> nodeData.leaf -> tn -> line_num);
fprintf(fp, "\tcmp r8w, r10w\n");
fprintf(fp, "\tjl oob\n");
fprintf(fp, "\tcmp r8w, r11w\n");
fprintf(fp, "\tjg oob\n");
/* TODO: write this in assembly */
// if(idx < lft || idx > right) {
// rte();
// }
/* Move base address of array to r8x */
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", offset + typeSize[AST_TYPE_POINTER]);
fprintf(fp, "\tmov rdx, qword [rax]\n");
/* compute element address as: [rdx - (r8w - r10w + 1) * width] */
fprintf(fp, "\tmov r9w, 0\n");
fprintf(fp, "\tsub r8w, r10w\n");
fprintf(fp, "\tinc r8w\n");
fprintf(fp, "\tmov rcx, %dd\n", typeSize[type]);
fprintf(fp, "label_%d:\n", label_num++);
fprintf(fp, "\tadd r9w, r8w\n");
fprintf(fp, "\tdec rcx\n");
fprintf(fp, "\tjnz label_%d\n", label_num - 1);
fprintf(fp, "\tmovsx r9, r9w\n");
fprintf(fp, "; --- END: fetchArraybyIndex() for array %s: base: rdx, offset: r9 --- \n", arr -> nodeData.leaf -> tn -> lex);
}
/**
* NOTE: Function was initially designed to modularize arrays,
* but later found out that this can be reused for non array types as well.
* don't go by the name
*/
void outputArrayElement(SymTableVar * id, int op) {
fprintf(fp, "; --- START: outputArrayElement() for %s --- \n", id -> name);
fprintf(fp, "; Function is used for both Arrays and non-Array types, don't go by the name! \n");
astDataType type = id -> dataType;
if(type == AST_TYPE_ARRAY)
type = id -> sdt.r -> dataType;
fprintf(fp, "\tpush rbp\n");
if(type == AST_TYPE_INT) {
if(op == 1)
fprintf(fp, "\tmov rdi, fmt_int\n");
else
fprintf(fp, "\tmov rdi, output_fmt_int\n");
fprintf(fp, "\tmov rax, rdx\n");
fprintf(fp, "\tsub rax, r9\n");
fprintf(fp, "\tmov si, word[rax]\n");
}
else if(type == AST_TYPE_REAL) {
if(op == 1)
fprintf(fp, "\tmov rdi, fmt_float\n");
else
fprintf(fp, "\tmov rdi, output_fmt_float\n");
fprintf(fp, "\tmov rax, rdx\n");
fprintf(fp, "\tsub rax, r9\n");
fprintf(fp, "\tcvtss2sd xmm0, dword[rax]\n");
fprintf(fp, "\tmov rax, 1\n");
}
else if(type == AST_TYPE_BOOLEAN) {
fprintf(fp, "\tmov rax, rdx\n");
fprintf(fp, "\tsub rax, r9\n");
fprintf(fp, "\tmov al, byte[rax]\n");
fprintf(fp, "\tcmp al, 0\n");
fprintf(fp, "\tjz label_%d\n", label_num++);
if(op == 1)
fprintf(fp, "\tmov rdi, fmt_string\n");
else
fprintf(fp, "\tmov rdi, output_fmt_string\n");
fprintf(fp, "\tmov rsi, bool_true\n");
fprintf(fp, "\tjmp label_%d\n", label_num++);
fprintf(fp, "label_%d:\n", label_num - 2);
if(op == 1)
fprintf(fp, "\tmov rdi, fmt_string\n");
else
fprintf(fp, "\tmov rdi, output_fmt_string\n");
fprintf(fp, "\tmov rsi, bool_false\n");
fprintf(fp, "label_%d:\n", label_num - 1);
}
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "; --- END: outputArrayElement() for %s--- \n", id -> name);
}
/**
* Emits code for copying rhs to lhs
* If lhsOff is -1, left hand side is treated as an array and assumed
* that rdx is base and r9 is offset, already emitted.
* @param lshOff
* @param rhsOff
* @param type
*/
void moveOffsetToOffset(int lhsOff, int rhsOff, astDataType type) {
fprintf(fp, "; --- START: moveOffsetToOffset(): lhsoff = %d, rhsoff = %d, type = %s ---\n", lhsOff, rhsOff, typeName[type]);
/* offset of rhs is in rax */
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", rhsOff + typeSize[type]);
/* */
if(type == AST_TYPE_INT) {
fprintf(fp, "\tmov r8w, word [rax]\n");
if(lhsOff == -1) {
fprintf(fp, "\tmov rax, rdx\n");
fprintf(fp, "\tsub rax, r9\n");
}
else {
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", typeSize[type] + lhsOff);
}
fprintf(fp, "\tmov word [rax], r8w\n");
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tmov r8d, dword [rax]\n");
if(lhsOff == -1) {
fprintf(fp, "\tmov rax, rdx\n");
fprintf(fp, "\tsub rax, r9\n");
}
else {
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", typeSize[type] + lhsOff);
}
fprintf(fp, "\tmov dword [rax], r8d\n");
}
if(type == AST_TYPE_BOOLEAN) {
fprintf(fp, "\tmov r8b, byte [rax]\n");
if(lhsOff == -1) {
fprintf(fp, "\tmov rax, rdx\n");
fprintf(fp, "\tsub rax, r9\n");
}
else {
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", typeSize[type] + lhsOff);
}
fprintf(fp, "\tmov byte [rax], r8b\n");
}
if(type == AST_TYPE_ARRAY) {
fprintf(fp, "\tmov r8, qword [rax]\n");
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", typeSize[type] + lhsOff);
fprintf(fp, "\tmov qword [rax], r8\n");
}
fprintf(fp, "; --- END: moveOffsetToOffset(): lhsoff = %d, rhsoff = %d, type = %s ---\n", lhsOff, rhsOff, typeName[type]);
}
void if0else1() {
fprintf(fp, "; --- START: if0else1() --- \n");
fprintf(fp, "\tmov r8b, 0\n");
fprintf(fp, "\tjmp label_%d\n", label_num++);
fprintf(fp, "label_%d:\n", label_num - 2);
fprintf(fp, "\tmov r8b, 1\n");
fprintf(fp, "label_%d:\n", label_num - 1);
fprintf(fp, "; --- END: if0else1() --- \n");
}
void pushTemporary(astDataType type, SymTableFunc* par) {
fprintf(fp, "; --- START: pushTemporary(): type = %s ---\n", typeName[type]);
if(type == AST_TYPE_INT) {
fprintf(fp, "\tmov dx, word [rax]\n");
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
fprintf(fp, "\tmov word [rax], dx\n");
}
else if(type == AST_TYPE_REAL) {
fprintf(fp, "\tmov edx, dword [rax]\n");
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
fprintf(fp, "\tmov dword [rax], edx\n");
}
else if(type == AST_TYPE_BOOLEAN) {
fprintf(fp, "\tmov dl, byte [rax]\n");
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
fprintf(fp, "\tmov byte [rax], dl\n");
}
else if(type == AST_TYPE_ARRAY) {
fprintf(fp, "\tmov rdx, qword [rax]\n");
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
fprintf(fp, "\tmov qword [rax], rdx\n");
}
fprintf(fp, "; --- END: pushTemporary(): type = %s ---\n", typeName[type]);
}
/**
* Floating point operations source: https://gist.github.com/nikAizuddin/0e307cac142792dcdeba
* cmp eax, 0000000000000000B ;is st0 > source ?
* je .example_11_greater
cmp eax, 0000000100000000B ;is st0 < source ?
je .example_11_less
cmp eax, 0100000000000000B ;is st0 = source ?
je .example_11_equal
*/
void applyOperator(int leftOp, int rightOp, ASTNode * operator, astDataType type) {
fprintf(fp, "; --- START: applyOperator(): leftOp: %d, rightOp: %d, operator: %s, type: %s --- \n", leftOp, rightOp, operator->nodeData.leaf->tn->lex, typeName[type]);
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", leftOp + typeSize[type]);
fprintf(fp, "\tmov r10, rsp\n");
fprintf(fp, "\tsub r10, %dd\n", rightOp + typeSize[type]);
SymTableFunc * par = getParentFunc(operator -> parent -> localST);
operator -> parent -> nodeData.AOBExpr -> temporaryOffset = par -> dynamicRecSize;
par -> dynamicRecSize += typeSize[operator -> parent -> nodeData.AOBExpr -> dataType];
if(type == AST_TYPE_INT) {
fprintf(fp, "\tmov r8w, word [rax]\n");
fprintf(fp, "\tmov r9w, word [r10]\n");
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfinit\n");
if(operator -> parent -> nodeData.AOBExpr -> dataType == AST_TYPE_BOOLEAN) {
fprintf(fp, "\tfld dword [r10]\n");
fprintf(fp, "\tfld dword [rax]\n");
}
else {
fprintf(fp, "\tfld dword [rax]\n");
fprintf(fp, "\tfld dword [r10]\n");
}
}
if(type == AST_TYPE_BOOLEAN) {
fprintf(fp, "\tmov r8b, byte [rax]\n");
fprintf(fp, "\tmov r9b, byte [r10]\n");
}
switch(operator -> nodeData.leaf -> type) {
case AST_LEAF_AND:
fprintf(fp, "\tand r8b, r9b\n");
break;
case AST_LEAF_OR:
fprintf(fp, "\tor r8b, r9b\n");
break;
case AST_LEAF_LT:
if(type == AST_TYPE_INT) {
fprintf(fp, "\tcmp r8w, r9w\n");
fprintf(fp, "\tjl label_%d\n", label_num++);
if0else1();
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfcom st0, st1\n");
fprintf(fp, "\tfstsw ax\n");
fprintf(fp, "\tand eax, 0100011100000000B\n");
fprintf(fp, "\tcmp eax, 0000000100000000B\n");
fprintf(fp, "\tje label_%d\n", label_num++);
if0else1();
}
break;
case AST_LEAF_LE:
if(type == AST_TYPE_INT) {
fprintf(fp, "\tcmp r8w, r9w\n");
fprintf(fp, "\tjle label_%d\n", label_num++);
if0else1();
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfcom st0, st1\n");
fprintf(fp, "\tfstsw ax\n");
fprintf(fp, "\tand eax, 0100011100000000B\n");
fprintf(fp, "\tcmp eax, 0000000100000000B\n");
fprintf(fp, "\tje label_%d\n", label_num++);
fprintf(fp, "\tcmp eax, 0100000000000000B\n");
fprintf(fp, "\tje label_%d\n", label_num - 1);
if0else1();
}
break;
case AST_LEAF_GT:
if(type == AST_TYPE_INT) {
fprintf(fp, "\tcmp r8w, r9w\n");
fprintf(fp, "\tjg label_%d\n", label_num++);
if0else1();
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfcom st0, st1\n");
fprintf(fp, "\tfstsw ax\n");
fprintf(fp, "\tand eax, 0100011100000000B\n");
fprintf(fp, "\tcmp eax, 0000000000000000B\n");
fprintf(fp, "\tje label_%d\n", label_num++);
if0else1();
}
break;
case AST_LEAF_GE:
if(type == AST_TYPE_INT) {
fprintf(fp, "\tcmp r8w, r9w\n");
fprintf(fp, "\tjge label_%d\n", label_num++);
if0else1();
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfcom st0, st1\n");
fprintf(fp, "\tfstsw ax\n");
fprintf(fp, "\tand eax, 0100011100000000B\n");
fprintf(fp, "\tcmp eax, 0000000000000000B\n");
fprintf(fp, "\tje label_%d\n", label_num++);
fprintf(fp, "\tcmp eax, 0100000000000000B\n");
fprintf(fp, "\tje label_%d\n", label_num - 1);
if0else1();
}
break;
case AST_LEAF_EQ:
if(type == AST_TYPE_INT) {
fprintf(fp, "\tcmp r8w, r9w\n");
fprintf(fp, "\tje label_%d\n", label_num++);
if0else1();
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfcom st0, st1\n");
fprintf(fp, "\tfstsw ax\n");
fprintf(fp, "\tand eax, 0100011100000000B\n");
fprintf(fp, "\tcmp eax, 0100000000000000B\n");
fprintf(fp, "\tje label_%d\n", label_num++);
if0else1();
}
break;
case AST_LEAF_NE:
if(type == AST_TYPE_INT) {
fprintf(fp, "\tcmp r8w, r9w\n");
fprintf(fp, "\tjne label_%d\n", label_num++);
if0else1();
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfcom st0, st1\n");
fprintf(fp, "\tfstsw ax\n");
fprintf(fp, "\tand eax, 0100011100000000B\n");
fprintf(fp, "\tcmp eax, 0100000000000000B\n");
fprintf(fp, "\tjne label_%d\n", label_num++);
if0else1();
}
break;
case AST_LEAF_PLUS:
if(type == AST_TYPE_INT)
fprintf(fp, "\tadd r8w, r9w\n");
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfadd\n");
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
// fprintf(fp, "\tsub rsp, 4\n");
fprintf(fp, "\tfstp dword [rax]\n");
}
break;
case AST_LEAF_MINUS:
if(type == AST_TYPE_INT)
fprintf(fp, "\tsub r8w, r9w\n");
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfsub\n");
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
// fprintf(fp, "\tsub rsp, 4\n");
fprintf(fp, "\tfstp dword [rax]\n");
}
break;
case AST_LEAF_MUL:
if(type == AST_TYPE_INT) {
fprintf(fp, "\tmov ax, r8w\n");
fprintf(fp, "\tmul r9w\n");
fprintf(fp, "\tmov r8w, ax\n");
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfmul\n");
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
// fprintf(fp, "\tsub rsp, 4\n");
fprintf(fp, "\tfstp dword [rax]\n");
}
break;
case AST_LEAF_DIV:
if(type == AST_TYPE_INT) {
fprintf(fp, "\tmov rsi, %dd\n", operator -> nodeData.leaf -> tn -> line_num);
fprintf(fp, "\tmov dx, 0\n");
fprintf(fp, "\tmov ax, r8w\n");
fprintf(fp, "\tcmp r9w, 0\n");
fprintf(fp, "\tjz divisionby0\n");
fprintf(fp, "\tdiv r9w\n");
fprintf(fp, "\tmov r8w, ax\n");
}
if(type == AST_TYPE_REAL) {
fprintf(fp, "\tfdiv\n");
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
// fprintf(fp, "\tsub rsp, 4\n");
fprintf(fp, "\tfstp dword [rax]\n");
fprintf(fp, "\tfstsw ax\n");
fprintf(fp, "\tmov rsi, %dd\n", operator -> nodeData.leaf -> tn -> line_num);
fprintf(fp, "\tand eax, 0000000000000100B\n");
fprintf(fp, "\tcmp eax, 0000000000000100B\n");
fprintf(fp, "\tje divisionby0\n");
}
break;
default:
break;
}
if(operator -> parent -> nodeData.AOBExpr -> dataType == AST_TYPE_INT) {
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
fprintf(fp, "\tmov word [rax], r8w\n");
}
else if(operator -> parent -> nodeData.AOBExpr -> dataType == AST_TYPE_BOOLEAN) {
fprintf(fp, "\tmov rax, rsp\n");
fprintf(fp, "\tsub rax, %dd\n", par -> dynamicRecSize);
fprintf(fp, "\tmov byte [rax], r8b\n");
}
fprintf(fp, "; --- START: applyOperator(): leftOp: %d, rightOp: %d, operator: %s, type: %s --- \n", leftOp, rightOp, operator->nodeData.leaf->tn->lex, typeName[type]);
}
void scopeBegin() {
fprintf(fp, "; --- START: scopeBegin() --- \n");
fprintf(fp, "\tsub rsp, 2d\n");
fprintf(fp, "\tmov ax, word [dynamic]\n");
fprintf(fp, "\tmov word [rsp], ax\n");
fprintf(fp, "\tmov ax, 0\n");
fprintf(fp, "\tmov word [dynamic], ax\n");
fprintf(fp, "; --- END: scopeBegin() --- \n");
}
void scopeEnd() {
fprintf(fp, "; --- START: scopeEnd() --- \n");
fprintf(fp, "\tmovsx rax, word [dynamic]\n");
fprintf(fp, "\tadd rsp, rax\n");
fprintf(fp, "\tmov ax, word [rsp]\n");
fprintf(fp, "\tmov word [dynamic], ax\n");
fprintf(fp, "\tadd rsp, 2d\n");
fprintf(fp, "; --- END: scopeEnd() --- \n");
}
void takeInput(astDataType t, SymTableVar * idNode) {
fprintf(fp, "; --- START: takeInput(): type: %s, Name: %s --- \n", typeName[t], idNode->name);
fprintf(fp, "\tpush rbp\n");
int offset = idNode -> offset;
// unsigned long long int addr =
switch(t) {
case AST_TYPE_INT: {
fprintf(fp, "\tmov rdi, op1\n");
fprintf(fp, "\tmov rsi, type_int\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tmov r9, %dd\n", offset + typeSize[t]);
getInputElement(t);
}
break;
case AST_TYPE_REAL: {
fprintf(fp, "\tmov rdi, op1\n");
fprintf(fp, "\tmov rsi, type_float\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tmov r9, %dd\n", offset + typeSize[t]);
getInputElement(t);
}
break;
case AST_TYPE_BOOLEAN: {
fprintf(fp, "\tmov rdi, op1\n");
fprintf(fp, "\tmov rsi, type_bool\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tmov r9, %dd\n", offset + typeSize[t]);
getInputElement(t);
}
break;
case AST_TYPE_ARRAY: {
getLeftRightIndex(idNode);
/* TODO write it in register format */
// if(lft > right) {
// rte();
// }
astDataType type = idNode -> sdt.r -> dataType;
fprintf(fp, "\n; --- Asking for user input for Array ---\n");
fprintf(fp, "\tmov rdi, op2\n");
fprintf(fp, "\tmov si, r11w\n");
fprintf(fp, "\tsub si, r10w\n");
fprintf(fp, "\tmovsx rsi, si\n");
fprintf(fp, "\tinc rsi\n");
if(type == AST_TYPE_INT)
fprintf(fp, "\tmov rdx, type_int\n");
else if(type == AST_TYPE_REAL)
fprintf(fp, "\tmov rdx, type_float\n");
else if(type == AST_TYPE_BOOLEAN)
fprintf(fp, "\tmov rdx, type_bool\n");
fprintf(fp, "\tmovsx rcx, r10w\n");
fprintf(fp, "\tmovsx r8, r11w\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\n; --- rdx will be the address of the first element of the array ---\n");
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", offset + typeSize[AST_TYPE_POINTER]);
fprintf(fp, "\tmov rdx, qword [rax]\n");
fprintf(fp, "\n; --- Loop for scanning each element of the array --- \n");
getLeftRightIndex(idNode);
fprintf(fp, "\tmov cx, r11w\n");
fprintf(fp, "\tsub cx, r10w\n");
fprintf(fp, "\tmovsx rcx, cx\n");
fprintf(fp, "\tinc rcx\n");
fprintf(fp, "label_%d:\n", label_num++);
fprintf(fp, "\tpush rdx\n");
fprintf(fp, "\tpush rcx\n");
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\n; --- Scanning input ---\n");
if(type == AST_TYPE_INT)
fprintf(fp, "\tmov rdi, fmt_int\n");
else if(type == AST_TYPE_REAL)
fprintf(fp, "\tmov rdi, fmt_float\n");
else if(type == AST_TYPE_BOOLEAN)
fprintf(fp, "\tmov rdi, fmt_bool\n");
fprintf(fp, "\tsub rdx, %dd\n", typeSize[type]);
fprintf(fp, "\tmov rsi, rdx\n");
alignStack();
fprintf(fp, "\tcall scanf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tpop rcx\n");
fprintf(fp, "\tpop rdx\n");
fprintf(fp, "\tsub rdx, %dd\n", typeSize[type]);
fprintf(fp, "\tdec rcx\n");
fprintf(fp, "\tcmp rcx, 0x0\n");
fprintf(fp, "\tjnz label_%d\n", label_num - 1);
// fprintf(fp, "\tpop rbp\n");
}
break;
}
fprintf(fp, "; --- END: takeInput(): type: %s, Name: %s --- \n", typeName[t], idNode->name);
}
void giveOutput(ASTNode * curr) {
fprintf(fp, "; --- START: giveInput()");
ASTNode * ch = curr;
if(ch -> type == AST_NODE_LEAF) {
fprintf(fp, " type: AST_NODE_LEAF, ");
switch(ch -> nodeData.leaf -> type) {
case AST_LEAF_VARIDNUM_NUM: {
fprintf(fp, " leaf-type: AST_LEAF_VARIDNUM_NUM --- \n");
int val = ch -> nodeData.leaf -> tn -> value.val_int;
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, output_fmt_int\n");
fprintf(fp, "\tmov rsi, %d\n", val);
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
}
break;
case AST_LEAF_VARIDNUM_RNUM: {
float val = ch -> nodeData.leaf -> tn -> value.val_float;
fprintf(fp, " leaf-type: AST_LEAF_VARIDNUM_RNUM, value: %f --- \n", val);
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, output_fmt_float\n");
fprintf(fp, "\tsub rsp, 4\n");
fprintf(fp, "\tmov dword [rsp], __float32__(%f)\n", val);
fprintf(fp, "\tcvtss2sd xmm0, [rsp]\n");
fprintf(fp, "\tadd rsp, 4\n");
fprintf(fp, "\tmov rax, 1\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
}
break;
case AST_LEAF_BOOLTRUE: {
fprintf(fp, " leaf-type: AST_LEAF_BOOLTRUE --- \n");
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, output_fmt_string\n");
fprintf(fp, "\tmov rsi, bool_true\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
}
break;
case AST_LEAF_BOOLFALSE: {
fprintf(fp, " leaf-type: AST_LEAF_BOOLFALSE --- \n");
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, output_fmt_string\n");
fprintf(fp, "\tmov rsi, bool_false\n");
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
}
break;
default: {
fprintf(fp, " leaf-type: default case hit --- \n");
/* do nothing */
}
break;
}
}
if(ch -> type == AST_NODE_VARIDNUM) {
fprintf(fp, " type: AST_NODE_VARIDNUM --- \n");
/* non array variable */
ASTNode * idNode = ch -> child;
SymTableVar * id = fetchVarData(curr -> localST, idNode -> nodeData.leaf -> tn -> lex, idNode -> nodeData.leaf -> tn -> line_num);
if(id -> dataType != AST_TYPE_ARRAY) {
// if(id -> isAssigned == 0) {
// rte();
// }
fprintf(fp, "\tmov r9, %dd\n", (id -> offset) + typeSize[id -> dataType]);
fprintf(fp, "\tmov rdx, rbp\n");
outputArrayElement(id, 0);
return;
}
getLeftRightIndex(id);
// if(lft > right) {
// rte();
// }
astDataType type = id -> sdt.r -> dataType;
if(idNode -> next == NULL) {
fprintf(fp, "; --- idNode->next is NULL --- \n");
int offset = id -> offset;
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, output_fmt_plain\n" );
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tmov rax, rbp\n");
fprintf(fp, "\tsub rax, %dd\n", offset + typeSize[AST_TYPE_POINTER]);
fprintf(fp, "\tmov rdx, qword [rax]\n");
fprintf(fp, "\tmov cx, r11w\n");
fprintf(fp, "\tsub cx, r10w\n");
fprintf(fp, "\tmovsx rcx, cx\n");
fprintf(fp, "\tinc rcx\n");
fprintf(fp, "\tmov r9, %dd\n", typeSize[type]);
fprintf(fp, "label_%d:\n", label_num++);
int loopLabel = label_num - 1;
fprintf(fp, "\tpush rdx\n");
fprintf(fp, "\tpush rcx\n");
outputArrayElement(id, 1);
fprintf(fp, "\tpop rcx\n");
fprintf(fp, "\tpop rdx\n");
fprintf(fp, "\tpush rdx\n");
fprintf(fp, "\tpush rcx\n");
fprintf(fp, "\tpush rbp\n");
fprintf(fp, "\tmov rdi, single_space\n" );
alignStack();
fprintf(fp, "\tcall printf\n");
getBackStack();
fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tpop rcx\n");
fprintf(fp, "\tpop rdx\n");
fprintf(fp, "\tsub rdx, %dd\n", typeSize[type]);
fprintf(fp, "\tdec rcx\n");
fprintf(fp, "\tjnz label_%d\n", loopLabel);
// fprintf(fp, "\tpop rbp\n");
fprintf(fp, "\tpush rbp\n");