-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_generator.c
1025 lines (943 loc) · 31 KB
/
code_generator.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
/**
* Project: IFJ21 imperative language compiler
*
* Brief: Code Generator for IFJ21 compiler
*
* Author: Adam Kankovsky <xkanko00>
* Author: Stepan Bakaj <xbakaj00>
* Author: Radek Serejch <xserej00>
*
*/
#include <ctype.h>
#include <stdlib.h>
#include <limits.h>
#include "code_generator.h"
#define DEF 2
/*
* ----------------------USEFUL FUNCTIONS-----------------------
*/
int numPlaces (int n) {
int r = 1;
if (n < 0) n = (n == INT_MIN) ? INT_MAX: -n;
while (n > 9) {
n /= 10;
r++;
}
return r;
}
char* convert_string(char* str_toconvert){
if (str_toconvert == NULL)
{
return NULL;
}
char* string = str_toconvert;
if (string[0] == '\0')
{
return "";
}
string_ptr_t string_res = string_init();
while(*string != '\0'){
if(*string == 92){
string_append_character(string_res, '\\');
if (!isdigit(*(string+1)))
{
string++;
switch (*string)
{
case 'a':
string_append_character(string_res, '0');
string_append_character(string_res, '0');
string_append_character(string_res, '7');
break;
case 'b':
string_append_character(string_res, '0');
string_append_character(string_res, '0');
string_append_character(string_res, '8');
break;
case 'f':
string_append_character(string_res, '0');
string_append_character(string_res, '1');
string_append_character(string_res, '2');
break;
case 'n':
string_append_character(string_res, '0');
string_append_character(string_res, '1');
string_append_character(string_res, '0');
break;
case 'r':
string_append_character(string_res, '0');
string_append_character(string_res, '1');
string_append_character(string_res, '3');
break;
case 't':
string_append_character(string_res, '0');
string_append_character(string_res, '0');
string_append_character(string_res, '9');
break;
case 'v':
string_append_character(string_res, '0');
string_append_character(string_res, '1');
string_append_character(string_res, '1');
break;
case '\\':
string_append_character(string_res, '0');
string_append_character(string_res, '9');
string_append_character(string_res, '2');
break;
case '"':
string_append_character(string_res, '0');
string_append_character(string_res, '3');
string_append_character(string_res, '4');
break;
case '\'':
string_append_character(string_res, '0');
string_append_character(string_res, '3');
string_append_character(string_res, '9');
break;
default:
break;
}
}
}else if(*string == 35){
string_append_character(string_res, '\\');
string_append_character(string_res, '0');
string_append_character(string_res, '3');
string_append_character(string_res, '5');
}else if(*string <= 32){
string_append_character(string_res, '\\');
string_append_character(string_res, '0');
string_append_character(string_res, ((*string / 10) + 48));
string_append_character(string_res, ((*string % 10) + 48));
}else{
string_append_character(string_res, *string);
}
string++;
}
string_append_character(string_res, '\0');
return string_res->string;
}
shadowStack_t* shStackPush(shadowStack_t* shade, char* name, int scale, int function){
shadowStack_t* newNode = malloc(sizeof(struct shadowStack));
if(newNode == NULL){
err = E_INTERNAL;
return NULL;
}
newNode->name = malloc(strlen(name)+1);
if(newNode->name == NULL){
err = E_INTERNAL;
return NULL;
}
strcpy(newNode->name, name);
newNode->scale = scale;
newNode->inicialized = 0;
newNode->nameScale = malloc(strlen(name) + numPlaces(scale) + 1);
if(newNode->nameScale == NULL){
err = E_INTERNAL;
return NULL;
}
char funCnt[function+1];
for(int i = 0; i < function; i++){
funCnt[i] = '$';
}
funCnt[function] = '\0';
sprintf(newNode->nameScale, "%s%s$%d", funCnt, name, scale);
newNode->next = shade;
return shade = newNode;
}
shadowStack_t * shStackNameScaleByName(shadowStack_t* shade, char* name){
shadowStack_t* current = shade;
while(current != NULL){
if(strcmp(current->name, name) == 0){
return current;
}else{
current = current->next;
}
}
return NULL;
}
shadowStack_t * shStackNameScaleByNameInitialized(shadowStack_t* shade, char* name){
shadowStack_t* current = shade;
while(current != NULL){
if((strcmp(current->name, name) == 0) && current->inicialized == 1){
return current;
}else{
current = current->next;
}
}
return NULL;
}
shadowStack_t* shStackDelByScale(shadowStack_t* shade, int scale){
shadowStack_t* del;
while(shade != NULL){
if(shade->scale == scale){
del = shade;
shade = shade->next;
free(del->name);
del->name = NULL;
free(del->nameScale);
del->nameScale = NULL;
free(del);
del = NULL;
}else{
return shade;
}
}
return NULL;
}
/*
* ----------------------BUILD IN FUNCTIONS-----------------------
*/
void codeGen_write(){
printf("#----FUN-write----\n");
printf("JUMP write$end\n");
printf("LABEL write\n");
printf("PUSHFRAME\n");
printf("CREATEFRAME\n");
printf("DEFVAR TF@cnt_of_parameter\n");
printf("DEFVAR TF@to_print\n");
printf("DEFVAR TF@cnt\n");
printf("POPS TF@cnt_of_parameter\n");
printf("MOVE TF@cnt int@0\n");
printf("LABEL _print_while_start\n");
printf("LT GF@expr TF@cnt TF@cnt_of_parameter\n");
printf("JUMPIFNEQ _print_while_end GF@expr bool@true\n");
printf("POPS TF@to_print\n");
printf("JUMPIFNEQ exprint TF@to_print nil@nil \n");
printf("PUSHS string@nil\n");
printf("POPS TF@to_print\n");
printf("LABEL exprint\n");
printf("WRITE TF@to_print\n");
printf("ADD TF@cnt TF@cnt int@1\n");
printf("JUMP _print_while_start\n");
printf("LABEL _print_while_end\n");
printf("POPFRAME\n");
printf("RETURN\n");
printf("LABEL write$end\n");
}
void codeGen_reads(){
printf("#----FUN-reads----\n");
printf("JUMP reads$end\n");
printf("LABEL reads\n");
printf("PUSHFRAME\n");
printf("CREATEFRAME\n");
printf("POPS GF@trash\n");
printf("DEFVAR TF@out\n");
printf("READ TF@out string\n");
printf("PUSHS TF@out\n");
printf("POPFRAME\n");
printf("RETURN\n");
printf("LABEL reads$end\n");
}
void codeGen_readi(){
printf("#----FUN-readi----\n");
printf("JUMP readi$end\n");
printf("LABEL readi\n");
printf("PUSHFRAME\n");
printf("CREATEFRAME\n");
printf("POPS GF@trash\n");
printf("DEFVAR TF@out\n");
printf("READ TF@out int\n");
printf("PUSHS TF@out\n");
printf("POPFRAME\n");
printf("RETURN\n");
printf("LABEL readi$end\n");
}
void codeGen_readn(){
printf("#----FUN-readn----\n");
printf("JUMP readn$end\n");
printf("LABEL readn\n");
printf("PUSHFRAME\n");
printf("CREATEFRAME\n");
printf("POPS GF@trash\n");
printf("DEFVAR TF@out\n");
printf("READ TF@out float\n");
printf("PUSHS TF@out\n");
printf("POPFRAME\n");
printf("RETURN\n");
printf("LABEL readn$end\n");
}
void codeGen_tointeger(){
printf("#----FUN-tointeger----\n");
printf("JUMP tointeger$end\n");
printf("LABEL tointeger\n");
printf("POPS GF@trash\n");
printf("FLOAT2INTS\n");
printf("RETURN\n");
printf("LABEL tointeger$end\n");
}
void codeGen_substr(){
printf("#----FUN-substr----\n");
printf("JUMP substr$end\n");
printf("LABEL substr\n");
printf("PUSHFRAME\n");
printf("CREATEFRAME\n");
printf("POPS GF@trash\n");
printf("DEFVAR TF@ret_str\n");
printf("MOVE TF@ret_str string@\n");
printf("DEFVAR TF@s\n");
printf("POPS TF@s\n");
printf("DEFVAR TF@i\n");
printf("POPS TF@i\n");
printf("SUB TF@i TF@i int@1\n");
printf("DEFVAR TF@n\n");
printf("POPS TF@n\n");
printf("DEFVAR TF@char\n");
printf("MOVE TF@char string@\n");
printf("DEFVAR TF@str_len\n");
printf("DEFVAR TF@l_limit\n");
printf("DEFVAR TF@r_limit\n");
printf("STRLEN TF@str_len TF@s\n");
printf("LT TF@l_limit TF@i int@0\n");
printf("NOT TF@l_limit TF@l_limit\n");
printf("GT TF@r_limit TF@i TF@str_len\n");
printf("NOT TF@r_limit TF@r_limit\n");
printf("AND TF@l_limit TF@l_limit TF@r_limit\n");
printf("LT TF@r_limit TF@n int@0\n");
printf("NOT TF@r_limit TF@r_limit\n");
printf("AND TF@l_limit TF@l_limit TF@r_limit\n");
printf("JUMPIFNEQ _sub_end TF@l_limit bool@true\n");
printf("DEFVAR TF@cnt_of_loaded\n");
printf("MOVE TF@cnt_of_loaded TF@i\n");
printf("LABEL _sub_while\n");
printf("LT TF@l_limit TF@i TF@str_len\n");
printf("LT TF@r_limit TF@cnt_of_loaded TF@n\n");
printf("AND TF@l_limit TF@l_limit TF@r_limit\n");
printf("JUMPIFNEQ _sub_end TF@l_limit bool@true\n");
printf("GETCHAR TF@char TF@s TF@i\n");
printf("CONCAT TF@ret_str TF@ret_str TF@char\n");
printf("ADD TF@cnt_of_loaded TF@cnt_of_loaded int@1\n");
printf("ADD TF@i TF@i int@1\n");
printf("JUMP _sub_while\n");
printf("LABEL _sub_end\n");
printf("PUSHS TF@ret_str\n");
printf("POPFRAME\n");
printf("RETURN\n");
printf("LABEL substr$end\n");
}
void codeGen_ord(){
printf("#----FUN-ord----\n");
printf("JUMP ord$end\n");
printf("LABEL ord\n");
printf("PUSHFRAME\n");
printf("CREATEFRAME\n");
printf("POPS GF@trash\n");
printf("DEFVAR TF@ascii\n");
printf("DEFVAR TF@err\n");
printf("MOVE TF@ascii string@\n");
printf("MOVE TF@err int@1\n");
printf("DEFVAR TF@l_limit\n");
printf("DEFVAR TF@r_limit\n");
printf("DEFVAR TF@string\n");
printf("DEFVAR TF@i\n");
printf("POPS TF@string\n");
printf("POPS TF@i\n");
printf("DEFVAR TF@str_len\n");
printf("MOVE TF@str_len int@0\n");
printf("STRLEN TF@str_len TF@string\n");
printf("SUB TF@str_len TF@str_len int@1\n");
printf("LT TF@l_limit TF@i int@0\n");
printf("NOT TF@l_limit TF@l_limit\n");
printf("GT TF@r_limit TF@i TF@str_len\n");
printf("NOT TF@r_limit TF@r_limit\n");
printf("AND TF@l_limit TF@l_limit TF@r_limit\n");
printf("JUMPIFNEQ _ord_end TF@l_limit bool@true\n");
printf("MOVE TF@err int@0\n");
printf("STRI2INT TF@ascii TF@string TF@i\n");
printf("LABEL _ord_end\n");
printf("PUSHS TF@ascii\n");
printf("PUSHS TF@err\n");
printf("POPFRAME\n");
printf("RETURN\n");
printf("LABEL ord$end\n");
}
void codeGen_chr(){
printf("#----FUN-chr----\n");
printf("JUMP chr$end\n");
printf("LABEL chr\n");
printf("PUSHFRAME\n");
printf("CREATEFRAME\n");
printf("POPS GF@trash\n");
printf("DEFVAR TF@ret_str\n");
printf("MOVE TF@ret_str string@\n");
printf("DEFVAR TF@err\n");
printf("MOVE TF@err int@1\n");
printf("DEFVAR TF@l_limit\n");
printf("DEFVAR TF@r_limit\n");
printf("DEFVAR TF@i\n");
printf("POPS TF@i\n");
printf("LT TF@l_limit TF@i int@0\n");
printf("NOT TF@l_limit TF@l_limit\n");
printf("GT TF@r_limit TF@i int@255\n");
printf("NOT TF@r_limit TF@r_limit\n");
printf("AND TF@l_limit TF@l_limit TF@r_limit\n");
printf("JUMPIFNEQ _chr_end TF@l_limit bool@true\n");
printf("MOVE TF@err int@0\n");
printf("INT2CHAR TF@ret_str TF@i\n");
printf("LABEL _chr_end\n");
printf("PUSHS TF@ret_str\n");
printf("PUSHS TF@err\n");
printf("POPFRAME\n");
printf("RETURN\n");
printf("LABEL chr$end\n");
}
/*
* ----------------------GENERATOR START-----------------------
*/
static int ifCounter = 0;
static int whileCounter = 0;
static int stackTop = -1;
static int stackSize = TABLE_SIZE;
static int* stack;
static int intToFloat1 = -1;
static int intToFloat2 = -1;
static int scale = -1;
static int function = 0;
static int isWhile = 0;
static int isNil = 0;
static int toBool = -1;
DLList* list = NULL;
shadowStack_t* shStack = NULL;
void codeGen_init(){
stack = malloc(sizeof(int) * stackSize);
if(stack == NULL){
err = E_INTERNAL;
return;
}
list = malloc(sizeof(DLList));
if(list == NULL){
err = E_INTERNAL;
return;
}
DLL_Init(list);
shStack = NULL;
printf(".IFJcode21\n");
printf("DEFVAR GF@expr\n");
printf("DEFVAR GF@tmp1\n");
printf("DEFVAR GF@tmp2\n");
printf("DEFVAR GF@tmp3\n");
printf("DEFVAR GF@tmp4\n");
printf("DEFVAR GF@trash\n");
printf("CREATEFRAME\n");
}
void codeGen_built_in_function(){
codeGen_write();
codeGen_reads();
codeGen_readi();
codeGen_readn();
codeGen_tointeger();
codeGen_substr();
codeGen_ord();
codeGen_chr();
}
/*
* ----------------------VAR-----------------------
*/
void codeGen_push_var(char* name){
shadowStack_t* current = shStackNameScaleByNameInitialized(shStack, name);
if(current == NULL){
err = E_INTERNAL;
return;
}
if(isWhile == 0){
printf("PUSHS TF@%s\n", current->nameScale);
}else{
char* str = (char*)malloc(12 + strlen(current->nameScale) + 1);
sprintf(str, "PUSHS TF@%s\n", current->nameScale);
DLL_InsertLast(list, str, 12 + strlen(current->nameScale) + 1);
free(str);
str = NULL;
}
}
void codeGen_push_string(char* value){
if(isWhile == 0){
printf("PUSHS string@%s\n", convert_string(value));
}else{
char* str = (char*)malloc(15 + strlen(convert_string(value)) + 1);
sprintf(str, "PUSHS string@%s\n", convert_string(value));
DLL_InsertLast(list, str, 15 + strlen(convert_string(value)) + 1);
free(str);
str = NULL;
}
}
void codeGen_push_int(int value){
if(isWhile == 0){
printf("PUSHS int@%d\n", value);
}else{
char* str = (char*)malloc(INST_LEN + numPlaces(value) + 1);
sprintf(str, "PUSHS int@%d\n", value);
DLL_InsertLast(list, str, INST_LEN + numPlaces(value) + 1);
free(str);
str = NULL;
}
}
void codeGen_push_float(double value){
if(isWhile == 0){
printf("PUSHS float@%a\n", value);
}else{
char* str = (char*)malloc(INST_LEN + 30 + 1);
sprintf(str, "PUSHS float@%a\n", value);
DLL_InsertLast(list, str, INST_LEN + 30 + 1);
free(str);
str = NULL;
}
}
void codeGen_push_nil(){
isNil = 1;
if(isWhile == 0){
printf("PUSHS nil@nil\n");
}else{
char* str = (char*)malloc(INST_LEN + 1);
sprintf(str, "PUSHS nil@nil\n");
DLL_InsertLast(list, str, INST_LEN + 1);
free(str);
str = NULL;
}
}
void codeGen_new_var(char* name){
shStack = shStackPush(shStack, name, scale, function);
if(shStack == NULL){
err = E_INTERNAL;
return;
}
printf("DEFVAR TF@%s\n", shStack->nameScale);
}
void codeGen_assign_var(char* name, unsigned nil){
shadowStack_t* current = shStackNameScaleByName(shStack, name);
if (nil == DEF)
{
current->inicialized = 1;
}
else
{
if(current == NULL){
err = E_INTERNAL;
return;
}
if(nil == 0){
current->inicialized = 1;
}
if(isWhile == 0){
printf("POPS TF@%s\n", current->nameScale);
}else{
char* str = (char*)malloc(INST_LEN + strlen(current->nameScale) + 1);
sprintf(str, "POPS TF@%s\n", current->nameScale);
DLL_InsertLast(list, str, INST_LEN + strlen(current->nameScale) + 1);
free(str);
str = NULL;
}
}
}
/*
* ----------------------IF-----------------------
*/
void codeGen_if_start(){
stackTop++;
scale++;
if(stackTop >= stackSize){
stackSize += stackSize;
stack = realloc(stack, sizeof(int) * stackSize);
}
stack[stackTop] = ifCounter;
ifCounter++;
if(isWhile == 0){
printf("POPS GF@expr\n");
printf("JUMPIFNEQ if$%d$else GF@expr bool@true\n", stack[stackTop]);
}else{
char* str = (char*)malloc(INST_LEN + 1);
sprintf(str, "POPS GF@expr\n");
DLL_InsertLast(list, str, INST_LEN + 1);
free(str);
str = NULL;
char* str2 = (char*)malloc(INST_LEN + numPlaces(stack[stackTop]) + 1);
sprintf(str2, "JUMPIFNEQ if$%d$else GF@expr bool@true\n", stack[stackTop]);
DLL_InsertLast(list, str2, INST_LEN + numPlaces(stack[stackTop]) + 1);
free(str2);
str2 = NULL;
}
}
void codeGen_if_else(){
if(isWhile == 0) {
printf("JUMP if$%d$end\n", stack[stackTop]);
printf("LABEL if$%d$else\n", stack[stackTop]);
}else{
char* str = (char*)malloc(INST_LEN + numPlaces(stack[stackTop]) + 1);
sprintf(str, "JUMP if$%d$end\n", stack[stackTop]);
DLL_InsertLast(list, str, INST_LEN + numPlaces(stack[stackTop]) + 1);
free(str);
str = NULL;
char* str2 = (char*)malloc(INST_LEN + numPlaces(stack[stackTop]) + 1);
sprintf(str2, "LABEL if$%d$else\n", stack[stackTop]);
DLL_InsertLast(list, str2, INST_LEN + numPlaces(stack[stackTop]) + 1);
free(str2);
str2 = NULL;
}
}
void codeGen_if_end(){
if(isWhile == 0){
printf("LABEL if$%d$end\n", stack[stackTop]);
}else{
char* str = (char*)malloc(INST_LEN + numPlaces(stack[stackTop]) + 1);
sprintf(str, "LABEL if$%d$end\n", stack[stackTop]);
DLL_InsertLast(list, str, INST_LEN + numPlaces(stack[stackTop]) + 1);
free(str);
str = NULL;
}
shStack = shStackDelByScale(shStack, scale);
stackTop--;
scale--;
}
/*
* ----------------------WHILE-----------------------
*/
void codeGen_while_body_start(){
stackTop++;
scale++;
isWhile = 1;
if(stackTop >= stackSize){
stackSize += stackSize;
stack = realloc(stack, sizeof(int) * stackSize);
}
stack[stackTop] = whileCounter;
whileCounter++;
char* str = (char*)malloc(INST_LEN + numPlaces(stack[stackTop]) + 1);
sprintf(str, "LABEL while$%d$start\n", stack[stackTop]);
DLL_InsertLast(list, str, INST_LEN + numPlaces(stack[stackTop]) + 1);
free(str);
str = NULL;
}
void codeGen_while_start(){
if(isWhile == 0) {
printf("POPS GF@expr\n");
printf("JUMPIFNEQ while$%d$end GF@expr bool@true\n", stack[stackTop]);
}else{
DLL_InsertLast(list, "POPS GF@expr\n", 14);
char* str2 = (char*)malloc(INST_LEN + numPlaces(stack[stackTop]) + 1);
sprintf(str2, "JUMPIFNEQ while$%d$end GF@expr bool@true\n", stack[stackTop]);
DLL_InsertLast(list, str2, INST_LEN + numPlaces(stack[stackTop]) + 1);
free(str2);
str2 = NULL;
}
}
void codeGen_while_end(){
DLL_PrintAll(list);
DLL_Dispose(list);
printf("JUMP while$%d$start\n", stack[stackTop]);
printf("LABEL while$%d$end\n", stack[stackTop]);
isWhile = 0;
stackTop--;
scale--;
}
/*
* ----------------------FUNC-----------------------
*/
void codeGen_function_start(char* name){
scale++;
function++;
printf("#----FUN-%s----\n", name);
printf("JUMP %s$end\nLABEL %s\nPUSHFRAME\nCREATEFRAME\n", name, name);
printf("POPS GF@trash\n");
}
void codeGen_function_return(){
if(isWhile == 0){
printf("POPFRAME\nRETURN\n");
}else{
DLL_InsertLast(list, "POPFRAME\nRETURN\n", 17);
}
}
void codeGen_function_end(char* name){
printf("POPFRAME\nRETURN\nLABEL %s$end\n", name);
shStack = shStackDelByScale(shStack, scale);
scale--;
}
void codeGen_function_call(char* name, unsigned parameters){
if(isWhile == 0){
printf("PUSHS int@%i\n", parameters);
printf("CALL %s\n", name);
}else{
char* str = (char*)malloc(INST_LEN + numPlaces(parameters) + 1);
sprintf(str, "PUSHS int@%i\n", parameters);
DLL_InsertLast(list, str, INST_LEN + numPlaces(parameters) + 1);
free(str);
str = NULL;
char* str2 = (char*)malloc(INST_LEN + strlen(name) + 1);
sprintf(str2, "CALL %s\n", name);
DLL_InsertLast(list, str2, INST_LEN + strlen(name) + 1);
free(str2);
str2 = NULL;
}
}
/*
* ----------------------STACK OPERATION-----------------------
*/
void generate_IntToFloat1(){
if(isWhile == 0){
printf("POPS GF@tmp1\n");
printf("JUMPIFEQ nope%d GF@tmp1 nil@nil\n",++intToFloat1);
printf("PUSHS GF@tmp1\n");
printf("INT2FLOATS\n");
printf("LABEL nope%d\n",intToFloat1);
}else{
DLL_InsertLast(list, "POPS GF@tmp1\n", 14);
char* str = (char*)malloc(INST_LEN + numPlaces(++intToFloat1) + 1);
sprintf(str, "JUMPIFEQ nope%d GF@tmp1 nil@nil\n", intToFloat1);
DLL_InsertLast(list, str, INST_LEN + numPlaces(++intToFloat1) + 1);
free(str);
str = NULL;
DLL_InsertLast(list, "PUSHS GF@tmp1\n", 15);
DLL_InsertLast(list, "INT2FLOATS\n", 12);
char* str2 = (char*)malloc(INST_LEN + numPlaces(intToFloat1) + 1);
sprintf(str2, "LABEL nope%d\n", intToFloat1);
DLL_InsertLast(list, str2, INST_LEN + numPlaces(intToFloat1) + 1);
free(str2);
str2 = NULL;
}
}
void generate_IntToFloat2(){
if(isWhile == 0){
printf("POPS GF@tmp3\n");
printf("POPS GF@tmp2\n");
printf("JUMPIFEQ no%d GF@tmp2 nil@nil\n",++intToFloat2);
printf("INT2FLOAT GF@tmp2 GF@tmp2\n");
printf("LABEL no%d\n",intToFloat2);
printf("PUSHS GF@tmp2\n");
printf("PUSHS GF@tmp3\n");
}else{
DLL_InsertLast(list, "POPS GF@tmp3\n", 14);
DLL_InsertLast(list, "POPS GF@tmp2\n", 14);
char* str = (char*)malloc(INST_LEN + numPlaces(++intToFloat2) + 1);
sprintf(str, "JUMPIFEQ no%d GF@tmp2 nil@nil\n", intToFloat2);
DLL_InsertLast(list, str, INST_LEN + numPlaces(++intToFloat2) + 1);
free(str);
str = NULL;
DLL_InsertLast(list, "INT2FLOAT GF@tmp2 GF@tmp2\n", 27);
char* str2 = (char*)malloc(INST_LEN + numPlaces(intToFloat2) + 1);
sprintf(str2, "LABEL no%d\n", intToFloat2);
DLL_InsertLast(list, str2, INST_LEN + numPlaces(intToFloat2) + 1);
free(str2);
str2 = NULL;
DLL_InsertLast(list, "PUSHS GF@tmp2\n", 15);
DLL_InsertLast(list, "PUSHS GF@tmp3\n", 15);
}
}
void generate_checkifNIL2ops(){
if(isWhile == 0){
printf("POPS GF@tmp1\n");
printf("POPS GF@tmp2\n");
printf("JUMPIFEQ ERR8 GF@tmp1 nil@nil\n");
printf("JUMPIFEQ ERR8 GF@tmp2 nil@nil\n");
printf("PUSHS GF@tmp2\n");
printf("PUSHS GF@tmp1\n");
}else{
DLL_InsertLast(list, "POPS GF@tmp1\n", 14);
DLL_InsertLast(list, "POPS GF@tmp2\n", 14);
DLL_InsertLast(list, "JUMPIFEQ ERR8 GF@tmp1 nil@nil\n", 31);
DLL_InsertLast(list, "JUMPIFEQ ERR8 GF@tmp2 nil@nil\n", 31);
DLL_InsertLast(list, "PUSHS GF@tmp2\n", 15);
DLL_InsertLast(list, "PUSHS GF@tmp1\n", 15);
}
}
void generate_checkifNIL1op(){
if(isWhile == 0){
printf("POPS GF@tmp1\n");
printf("JUMPIFEQ ERR8 GF@tmp1 nil@nil\n");
printf("PUSHS GF@tmp1\n");
}else{
DLL_InsertLast(list, "POPS GF@tmp1\n", 14);
DLL_InsertLast(list, "JUMPIFEQ ERR8 GF@tmp1 nil@nil\n", 31);
DLL_InsertLast(list, "PUSHS GF@tmp1\n", 15);
}
}
void generate_errorOp(){
printf("JUMP errorOp_End\n");
printf("LABEL ERR9\n");
printf("EXIT int@9\n");
printf("JUMP errorOp_End\n");
printf("LABEL ERR8\n");
printf("EXIT int@8\n");
printf("LABEL errorOp_End\n");
free(list);
list = NULL;
free(stack);
stack = NULL;
}
void generate_operation(psa_rules_enum operation){
switch (operation){
case NT_PLUS_NT:
// rule E -> E + E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("ADDS\n");
}else{
DLL_InsertLast(list, "ADDS\n", 6);
}
break;
case NT_MINUS_NT:
// rule E -> E - E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("SUBS\n");
}else{
DLL_InsertLast(list, "SUBS\n", 6);
}
break;
case NT_MUL_NT:
// rule E -> E * E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("MULS\n");
}else{
DLL_InsertLast(list, "MULS\n", 6);
}
break;
case NT_DIV_NT:
// rule E -> E / E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("POPS GF@tmp1\n");
printf("POPS GF@tmp2\n");
printf("JUMPIFEQ ERR9 GF@tmp1 float@0x0p+0\n");
printf("DIV GF@tmp1 GF@tmp2 GF@tmp1\n");
printf("PUSHS GF@tmp1\n");
}else{
DLL_InsertLast(list, "POPS GF@tmp1\n", 14);
DLL_InsertLast(list, "POPS GF@tmp2\n", 14);
DLL_InsertLast(list, "JUMPIFEQ ERR9 GF@tmp1 float@0x0p+0\n", 36);
DLL_InsertLast(list, "DIV GF@tmp1 GF@tmp2 GF@tmp1\n", 29);
DLL_InsertLast(list, "PUSHS GF@tmp1\n", 15);
}
break;
case NT_IDIV_NT:
// rule E -> E // E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("POPS GF@tmp1\n");
printf("POPS GF@tmp2\n");
printf("JUMPIFNEQ ERR9 GF@tmp1 int@0\n");
printf("IDIV GF@tmp1 GF@tmp2 GF@tmp1\n");
printf("PUSHS GF@tmp1\n");
}else{
DLL_InsertLast(list, "POPS GF@tmp1\n", 14);
DLL_InsertLast(list, "POPS GF@tmp2\n", 14);
DLL_InsertLast(list, "JUMPIFNEQ ERR9 GF@tmp1 int@0\n", 30);
DLL_InsertLast(list, "IDIV GF@tmp1 GF@tmp2 GF@tmp1\n", 30);
DLL_InsertLast(list, "PUSHS GF@tmp1\n", 15);
}
break;
case NT_CONCAT_NT:
// rule E -> E .. E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("POPS GF@tmp1\n");
printf("POPS GF@tmp2\n");
printf("CONCAT GF@tmp1 GF@tmp2 GF@tmp1\n");
printf("PUSHS GF@tmp1\n");
}else{
DLL_InsertLast(list, "POPS GF@tmp1\n", 14);
DLL_InsertLast(list, "POPS GF@tmp2\n", 14);
DLL_InsertLast(list, "CONCAT GF@tmp1 GF@tmp2 GF@tmp1\n", 32);
DLL_InsertLast(list, "PUSHS GF@tmp1\n", 15);
}
break;
case NT_EQ_NT:
// rule E -> E == E
if(isWhile == 0){
printf("EQS\n");
}else{
DLL_InsertLast(list, "EQS\n", 5);
}
break;
case NT_NEQ_NT:
// rule E -> E ~= E
if(isWhile == 0){
printf("EQS\nNOTS\n");
}else{
DLL_InsertLast(list, "EQS\nNOTS\n", 10);
}
break;
case NT_LEQ_NT:
// rule E -> E <= E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("GTS\nNOTS\n");
}else{
DLL_InsertLast(list, "GTS\nNOTS\n", 10);
}
break;
case NT_GEQ_NT:
// rule E -> E >= E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("LTS\nNOTS\n");
}else{
DLL_InsertLast(list, "LTS\nNOTS\n", 10);
}
break;
case NT_LTN_NT:
// rule E -> E < E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("LTS\n");
}else{
DLL_InsertLast(list, "LTS\n", 5);
}
break;
case NT_GTN_NT:
// rule E -> E > E
generate_checkifNIL2ops();
if(isWhile == 0){
printf("GTS\n");
}else{
DLL_InsertLast(list, "GTS\n", 5);
}
break;
case NT_HASHTAG:
// rule E -> #E
generate_checkifNIL1op();
if(isWhile == 0){
printf("POPS GF@tmp1\n");
printf("STRLEN GF@tmp4 GF@tmp1\n");
printf("PUSHS GF@tmp4\n");
}else{
DLL_InsertLast(list, "POPS GF@tmp1\n", 14);
DLL_InsertLast(list, "STRLEN GF@tmp4 GF@tmp1\n", 24);
DLL_InsertLast(list, "PUSHS GF@tmp4\n", 15);
}
break;
default:break;
}
}
void generate_toBool() {
if(isWhile == 0){
printf("POPS GF@tmp1\n");
printf("JUMPIFEQ toBoolTru%d GF@tmp1 nil@nil\n",++toBool);
printf("PUSHS bool@true\n");
printf("JUMP toBoolFalse%d\n",toBool);
printf("LABEL toBoolTru%d\n",toBool);
printf("PUSHS bool@false\n");
printf("LABEL toBoolFalse%d\n",toBool);
}else{