-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_generator.c
1810 lines (1717 loc) · 63.4 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
/************************************************************************
*
* Compiler implementation for imperative programming language IFJ18
*
* Autors:
* Sasák Tomáš - xsasak01
* Venkrbec Tomáš - xvenkr01
* Krajči Martin - xkrajc21
* Natália Dižová - xdizov00
*
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "code_generator.h"
#include "error.h"
/**
* Function initalizes instruction list working structure data.
*/
void init_cgData()
{
cgData.ilist = NULL;
cgData.chrRequest = false;
cgData.inExpression = false;
cgData.lengthRequest = false;
cgData.ordRequest = false;
cgData.substrRequest = false;
cgData.uniqueCounter = 0;
}
/**
* Function initializes instruction list for first use.
* @param instrs Pointer to the instruction list.
*/
void init_ilist(tIList *instrs)
{
instrs->active = NULL;
instrs->head = NULL;
}
/**
* Function inserts instruction code (macro) to the instruction list, creates instruction node for it.
* @param instrs Pointer to the instruction list.
* @param instr Number of instruction to insert (macro).
*/
void insert_instr(tIList *instrs, int instr)
{
// This situation cannot occur, but just in case there is no SEGFAULT
if(instrs == NULL)
{
return;
}
// If the list is empty, insert new instruction and make it active because it is last instruction in the list
if(instrs->head == NULL)
{
instrs->head = (tInstr *)malloc(sizeof(struct instructionNode));
instrs->head->instr = instr;
instrs->head->params = NULL;
instrs->head->next = NULL;
instrs->active = instrs->head;
return;
}
// If the list is not empty, active member is always the last member, so just allocate next member (active->next) and save data
instrs->active->next = (tInstr *)malloc(sizeof(struct instructionNode));
instrs->active = instrs->active->next;
instrs->active->instr = instr;
instrs->active->params = NULL;
instrs->active->next = NULL;
}
/**
* Function appends parameter inside one instruction.
* @param instrs Pointer to the instruction list.
* @param param Parameter of function which is represented by token.
*/
void insert_param(tIList *instrs, tToken param)
{
// This situation cannot occur, but just in case there is no SEGFAULT
if(instrs == NULL)
{
return;
}
else if(instrs->active == NULL)
{
return;
}
// If there are no parameters, create new one
if(instrs->active->params == NULL)
{
instrs->active->params = (tTList *)malloc(sizeof(struct tokenList));
instrs->active->params->param = param;
instrs->active->params->next = NULL;
return;
}
// If there are parameters, find the last one and create new one after it
tTList *temp = instrs->active->params;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = (tTList *)malloc(sizeof(struct tokenList));
temp->next->param = param;
temp->next->next = NULL;
}
/**
* Function free's up the whole instruction list, instructions, tokens.
* @param instrs Instruction list with instructions which is supposed to get free'd.
*/
void free_ilist(tIList *instrs)
{
if(instrs == NULL)
{
return;
}
// Initialize list for freeing pointers which are used by multiple tokens
tPList *freeList = (tPList *)malloc(sizeof(struct pointerList));
init_plist(freeList);
tInstr *temp;
tTList *tmp;
// Freeing the one instruction and parameters
while(instrs->head != NULL)
{
// Freeing the parameters inside one instruction
while(instrs->head->params != NULL)
{
tmp = instrs->head->params->next;
if(instrs->head->params->param.type == STRING || instrs->head->params->param.type == ID || instrs->head->params->param.type == IDF)
{
if(search_ptr(freeList, instrs->head->params->param.attr.str) == false)
{
insert_ptr(freeList, instrs->head->params->param.attr.str);
}
}
free(instrs->head->params);
instrs->head->params = tmp;
}
temp = instrs->head->next;
free(instrs->head);
instrs->head = temp;
}
free(instrs);
extern tToken noretval;
if(search_ptr(freeList, noretval.attr.str) == false)
{
insert_ptr(freeList, noretval.attr.str);
}
free_plist(freeList);
}
/**
* Function initialize pointer list, which prevents compiler from double free.
* @param plist Pointer to the pointer list.
*/
void init_plist(tPList *plist)
{
plist->active = NULL;
plist->head = NULL;
}
/**
* Function appends pointer, at the end of list.
* @param plist Pointer to the pointer list.
* @param freed Pointer to the freed string.
*/
void insert_ptr(tPList *plist, string freed)
{
if(plist == NULL)
{
return;
}
if(plist->head == NULL)
{
plist->head = (tPtr *)malloc(sizeof(struct pointerNode));
plist->head->freed = freed;
plist->active = plist->head;
plist->head->next = NULL;
}
else
{
plist->active->next = (tPtr *)malloc(sizeof(struct pointerNode));
plist->active = plist->active->next;
plist->active->freed = freed;
plist->active->next = NULL;
}
}
/**
* Function searches allocated pointers and returns if pointer was free'd or not.
* @param plist Pointer to the pointer list.
* @param freed Pointer to the freed string.
* @return Function returns true pointer was found, if not false.
*/
bool search_ptr(tPList *plist, string freed)
{
if(plist == NULL)
{
return false;
}
tPtr *tmp = plist->head;
while(tmp != NULL)
{
if(tmp->freed.str == freed.str)
{
return true;
}
tmp = tmp->next;
}
return false;
}
/**
* Function free's up whole list of pointers.
* @param plist Pointer to the pointer list.
*/
void free_plist(tPList *plist)
{
if(plist == NULL)
{
return;
}
if(plist->head != NULL)
{
tPtr *tmp = plist->head;
while(tmp != NULL)
{
tmp = plist->head->next;
str_free(&plist->head->freed);
free(plist->head);
plist->head = tmp;
}
}
free(plist);
}
/**
* Function creates output file, insert needed header for ifjcode18 and creates temporary frame for working with "main".
* @return Function returns pointer to the output file.
*/
FILE* generate_head()
{
FILE *f = stdout;
fprintf(f,".IFJcode18\n");
fprintf(f,"CREATEFRAME\n\n");
fprintf(f, "DEFVAR GF@$pops$type$\n");
return f;
}
/**
* Function generates add operation inside code.
* @param f Pointer to the IFJcode2018 source code.
* @param instruction Pointer to the single instruction from inside code.
*/
void generate_adds(FILE *f, tInstr *instruction)
{
fprintf(f, "POPS TF@op1\n");
fprintf(f, "POPS TF@op2\n");
fprintf(f, "TYPE TF@op1$type TF@op1\n");
fprintf(f, "TYPE TF@op2$type TF@op2\n");
fprintf(f, "JUMPIFNEQ $add_nil$op1$%d TF@op1$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $add_nil$op1$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $add_nil$op2$%d TF@op2$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $add_nil$op2$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $add_ok%d TF@op1$type TF@op2$type\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $checktype1%d TF@op1$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $checktype1%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $checktype2%d TF@op2$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $checktype2%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $add-retype1%d TF@op1$type string@int\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op1 TF@op1\n");
fprintf(f, "LABEL $add-retype1%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $add_ok%d TF@op2$type string@int\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op2 TF@op2\n");
fprintf(f, "LABEL $add_ok%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $concat%d TF@op1$type string@string\n", cgData.uniqueCounter);
fprintf(f, "PUSHS TF@op2\n");
fprintf(f, "PUSHS TF@op1\n");
fprintf(f, "ADDS\n");
fprintf(f, "JUMP $add_end%d\n", cgData.uniqueCounter);
fprintf(f, "LABEL $concat%d\n", cgData.uniqueCounter);
fprintf(f, "CONCAT TF@op2 TF@op2 TF@op1\n");
fprintf(f, "PUSHS TF@op2\n");
fprintf(f, "LABEL $add_end%d\n", cgData.uniqueCounter);
instruction->instr = NOP;
cgData.uniqueCounter++;
}
/**
* Function generates sub operation inside code.
* @param f Pointer to the IFJcode2018 source code.
* @param instruction Pointer to the single instruction from inside code.
*/
void generate_subs(FILE *f, tInstr *instruction)
{
fprintf(f, "POPS TF@op1\n");
fprintf(f, "POPS TF@op2\n");
fprintf(f, "TYPE TF@op1$type TF@op1\n");
fprintf(f, "TYPE TF@op2$type TF@op2\n");
fprintf(f, "JUMPIFNEQ $sub_nil$op1$%d TF@op1$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $sub_nil$op1$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $sub_nil$op2$%d TF@op2$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $sub_nil$op2$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $checktype1%d TF@op1$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $checktype1%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $checktype2%d TF@op2$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $checktype2%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $sub_ok%d TF@op1$type TF@op2$type\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $sub-retype1%d TF@op1$type string@int\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op1 TF@op1\n");
fprintf(f, "LABEL $sub-retype1%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $sub_ok%d TF@op2$type string@int\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op2 TF@op2\n");
fprintf(f, "LABEL $sub_ok%d\n", cgData.uniqueCounter);
fprintf(f, "PUSHS TF@op2\n");
fprintf(f, "PUSHS TF@op1\n");
fprintf(f, "SUBS\n");
instruction->instr = NOP;
cgData.uniqueCounter++;
}
/**
* Function generates mul operation inside code.
* @param f Pointer to the IFJcode2018 source code.
* @param instruction Pointer to the single instruction from inside code.
*/
void generate_muls(FILE *f, tInstr *instruction)
{
fprintf(f, "POPS TF@op1\n");
fprintf(f, "POPS TF@op2\n");
fprintf(f, "TYPE TF@op1$type TF@op1\n");
fprintf(f, "TYPE TF@op2$type TF@op2\n");
fprintf(f, "JUMPIFNEQ $mul_nil$op1$%d TF@op1$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $mul_nil$op1$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $mul_nil$op2$%d TF@op2$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $mul_nil$op2$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $checktype1%d TF@op1$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $checktype1%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $checktype2%d TF@op2$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $checktype2%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $mul_ok%d TF@op1$type TF@op2$type\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $mul-retype1%d TF@op1$type string@int\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op1 TF@op1\n");
fprintf(f, "LABEL $mul-retype1%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $mul_ok%d TF@op2$type string@int\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op2 TF@op2\n");
fprintf(f, "LABEL $mul_ok%d\n", cgData.uniqueCounter);
fprintf(f, "PUSHS TF@op2\n");
fprintf(f, "PUSHS TF@op1\n");
fprintf(f, "MULS\n");
instruction->instr = NOP;
cgData.uniqueCounter++;
}
/**
* Function generates div operation inside code.
* @param f Pointer to the IFJcode2018 source code.
* @param instruction Pointer to the single instruction from inside code.
*/
void generate_divs(FILE *f, tInstr *instruction)
{
fprintf(f, "POPS TF@op2\n");
fprintf(f, "POPS TF@op1\n\n");
fprintf(f, "TYPE TF@op1$type TF@op1\n");
fprintf(f, "TYPE TF@op2$type TF@op2\n\n");
fprintf(f, "JUMPIFNEQ $div_nil$op1$%d TF@op1$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $div_nil$op1$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $div_nil$op2$%d TF@op2$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $div_nil$op2$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $div$op1$ok$%d TF@op1$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n\n");
fprintf(f, "LABEL $div$op1$ok$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $div$ok$type$%d TF@op2$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n\n");
fprintf(f, "LABEL $div$ok$type$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $div$retype$%d TF@op1$type TF@op2$type\n\n", cgData.uniqueCounter);
fprintf(f, "LABEL $div$ok$%d\n", cgData.uniqueCounter);
fprintf(f, "TYPE TF@op1$type TF@op1\n");
fprintf(f, "JUMPIFEQ $idivs$%d TF@op1$type string@float\n\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $noti$op20$%d TF@op2 int@0\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@9\n\n");
fprintf(f, "LABEL $noti$op20$%d\n", cgData.uniqueCounter);
fprintf(f, "PUSHS TF@op1\n");
fprintf(f, "PUSHS TF@op2\n");
fprintf(f, "IDIVS\n");
fprintf(f, "JUMP $end$div$%d\n\n", cgData.uniqueCounter);
fprintf(f, "LABEL $idivs$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $notf$op20$%d TF@op2 float@0x0p+0\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@9\n\n");
fprintf(f, "LABEL $notf$op20$%d\n", cgData.uniqueCounter);
fprintf(f, "PUSHS TF@op1\n");
fprintf(f, "PUSHS TF@op2\n");
fprintf(f, "DIVS\n");
fprintf(f, "JUMP $end$div$%d\n", cgData.uniqueCounter);
fprintf(f, "LABEL $div$retype$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $div$retype1$%d TF@op1$type string@int\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op2 TF@op2\n");
fprintf(f, "JUMP $div$ok$%d\n\n", cgData.uniqueCounter);
fprintf(f, "LABEL $div$retype1$%d\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op1 TF@op1\n");
fprintf(f, "JUMP $div$ok$%d\n\n", cgData.uniqueCounter);
fprintf(f, "LABEL $end$div$%d\n", cgData.uniqueCounter);
cgData.uniqueCounter++;
instruction->instr = NOP;
}
void generate_nots(FILE *f, tInstr *instruction)
{
fprintf(f, "NOTS\n");
instruction->instr = NOP;
}
void generate_lts(FILE *f, tInstr *instruction)
{
fprintf(f, "POPS TF@op2\n");
fprintf(f, "POPS TF@op1\n\n");
fprintf(f, "TYPE TF@op1$type TF@op1\n");
fprintf(f, "TYPE TF@op2$type TF@op2\n\n");
fprintf(f, "JUMPIFNEQ $lt_nil$op1$%d TF@op1$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $lt_nil$op1$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $lt_nil$op2$%d TF@op2$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $lt_nil$op2$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $lt$string$%d TF@op1$type string@string\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $lt$retype$%d TF@op1$type TF@op2$type\n\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $lt$ok$%d TF@op1$type TF@op2$type\n", cgData.uniqueCounter);
fprintf(f, "LABEL $lt$string$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $lt$ok$%d TF@op2$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n\n");
fprintf(f, "LABEL $lt$ok$%d\n", cgData.uniqueCounter);
fprintf(f, "PUSHS TF@op1\n");
fprintf(f, "PUSHS TF@op2\n");
fprintf(f, "LTS\n");
fprintf(f, "JUMP $end$lt$%d\n\n", cgData.uniqueCounter);
fprintf(f, "LABEL $lt$retype$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $lt$notstring$%d TF@op2$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n\n");
fprintf(f, "LABEL $lt$notstring$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $lt$retype1$%d TF@op1$type string@int\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op2 TF@op2\n");
fprintf(f, "JUMP $lt$ok$%d\n", cgData.uniqueCounter);
fprintf(f, "LABEL $lt$retype1$%d\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op1 TF@op1\n");
fprintf(f, "JUMP $lt$ok$%d\n\n", cgData.uniqueCounter);
fprintf(f, "LABEL $end$lt$%d\n", cgData.uniqueCounter);
cgData.uniqueCounter++;
instruction->instr = NOP;
}
void generate_gts(FILE *f, tInstr *instruction)
{
fprintf(f, "POPS TF@op2\n");
fprintf(f, "POPS TF@op1\n\n");
fprintf(f, "TYPE TF@op1$type TF@op1\n");
fprintf(f, "TYPE TF@op2$type TF@op2\n\n");
fprintf(f, "JUMPIFNEQ $gt_nil$op1$%d TF@op1$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $gt_nil$op1$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $gt_nil$op2$%d TF@op2$type string@nil\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $gt_nil$op2$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $gt$string$%d TF@op1$type string@string\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $gt$retype$%d TF@op1$type TF@op2$type\n\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $gt$ok$%d TF@op1$type TF@op2$type\n", cgData.uniqueCounter);
fprintf(f, "LABEL $gt$string$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $gt$ok$%d TF@op2$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n\n");
fprintf(f, "LABEL $gt$ok$%d\n", cgData.uniqueCounter);
fprintf(f, "PUSHS TF@op1\n");
fprintf(f, "PUSHS TF@op2\n");
fprintf(f, "GTS\n");
fprintf(f, "JUMP $end$gt$%d\n\n", cgData.uniqueCounter);
fprintf(f, "LABEL $gt$retype$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFNEQ $gt$notstring$%d TF@op2$type string@string\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n\n");
fprintf(f, "LABEL $gt$notstring$%d\n", cgData.uniqueCounter);
fprintf(f, "JUMPIFEQ $gt$retype1$%d TF@op1$type string@int\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op2 TF@op2\n");
fprintf(f, "JUMP $gt$ok$%d\n", cgData.uniqueCounter);
fprintf(f, "LABEL $gt$retype1$%d\n", cgData.uniqueCounter);
fprintf(f, "INT2FLOAT TF@op1 TF@op1\n");
fprintf(f, "JUMP $gt$ok$%d\n\n", cgData.uniqueCounter);
fprintf(f, "LABEL $end$gt$%d\n", cgData.uniqueCounter);
cgData.uniqueCounter++;
instruction->instr = NOP;
}
void generate_eqs(FILE *f, tInstr *instruction)
{
fprintf(f, "POPS TF@op2\n");
fprintf(f, "POPS TF@op1\n\n");
fprintf(f, "TYPE TF@op1$type TF@op1\n");
fprintf(f, "TYPE TF@op2$type TF@op2\n\n");
fprintf(f, "JUMPIFEQ $eq$ok$%d TF@op1$type TF@op2$type\n", cgData.uniqueCounter);
fprintf(f, "PUSHS bool@false\n\n");
fprintf(f, "JUMP $end$eq$%d\n", cgData.uniqueCounter);
fprintf(f, "LABEL $eq$ok$%d\n", cgData.uniqueCounter);
fprintf(f, "PUSHS TF@op1\n");
fprintf(f, "PUSHS TF@op2\n");
fprintf(f, "EQS\n");
fprintf(f, "LABEL $end$eq$%d\n", cgData.uniqueCounter);
cgData.uniqueCounter++;
instruction->instr = NOP;
}
void generate_expression(FILE *f, tInstr *instruction)
{
// Initialize internal variables
fprintf(f, "# MATHEMATICAL EXPRESSION START\n");
fprintf(f, "PUSHFRAME\n");
fprintf(f, "CREATEFRAME\n\n");
fprintf(f, "PUSHS nil@nil\n");
fprintf(f, "DEFVAR TF@op1\n");
fprintf(f, "DEFVAR TF@op2\n");
fprintf(f, "DEFVAR TF@op1$type\n");
fprintf(f, "DEFVAR TF@op2$type\n");
instruction = instruction->next;
while(instruction->instr != EXPRESSION_END)
{
generate_instruction(f, instruction);
instruction = instruction->next;
}
// Expression is completed and result is on stack
fprintf(f, "POPFRAME\n");
fprintf(f, "# MATHEMATHICAL EXPRESSION FINISH\n");
}
void generate_concatenation(FILE *f, tInstr *instruction)
{
int defvarnum = cgData.uniqueCounter;
tToken where = instruction->params->param;
fprintf(f, "DEFVAR TF@$tmpconcat$type$%d\n", defvarnum);
fprintf(f, "DEFVAR TF@$tmpconcat$%d\n", defvarnum);
fprintf(f, "MOVE TF@$tmpconcat$%d string@\n", defvarnum);
while(instruction->instr != CONCAT_END)
{
if(instruction->instr == CONCAT)
{
// if its constant string, no need to control type because its string
if(instruction->params->param.type == STRING)
{
fprintf(f, "CONCAT TF@$tmpconcat$%d TF@$tmpconcat$%d string@%s\n\n", defvarnum, defvarnum, instruction->params->param.attr.str.str);
}
// It is ID need to semantic check
else if(instruction->params->param.type == ID)
{
fprintf(f, "TYPE TF@$tmpconcat$type$%d TF@%s\n", defvarnum, instruction->params->param.attr.str.str);
fprintf(f, "JUMPIFEQ $concat$%d TF@$tmpconcat$type$%d string@string\n", cgData.uniqueCounter, defvarnum);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $concat$%d\n", cgData.uniqueCounter);
fprintf(f, "CONCAT TF@$tmpconcat$%d TF@$tmpconcat$%d TF@%s\n\n", defvarnum, defvarnum, instruction->params->param.attr.str.str);
}
}
cgData.uniqueCounter++;
if(instruction->next->instr == CONCAT_END)
{
break;
}
instruction = instruction->next;
}
// Here instruction is move, if NULL the value is not saved, need to save it just to retval
fprintf(f, "MOVE TF@%s TF@$tmpconcat$%d\n", where.attr.str.str, defvarnum);
}
/**
* Function generates push instruction inside code.
* @param f Pointer to the IFJcode2018 source code.
* @param instruction Pointer to the single instruction from inside code.
*/
void generate_pushs(FILE *f, tInstr *instruction)
{
switch(instruction->params->param.type)
{
case INTEGER:
fprintf(f, "PUSHS int@%d\n", instruction->params->param.attr.i);
break;
case FLOAT:
fprintf(f, "PUSHS float@%a\n", instruction->params->param.attr.f);
break;
case STRING:
fprintf(f, "PUSHS string@%s\n", instruction->params->param.attr.str.str);
break;
case ID:
fprintf(f, "PUSHS LF@%s\n", instruction->params->param.attr.str.str);
break;
case NIL:
fprintf(f, "PUSHS nil@nil\n");
break;
}
instruction->instr = NOP;
}
/**
* Function generates pop instruction inside code.
* @param f Pointer to the IFJcode2018 source code.
* @param instruction Pointer to the single instruction from inside code.
*/
void generate_pops(FILE *f, tInstr *instruction)
{
if(instruction->params->param.type == ID)
{
fprintf(f, "POPS TF@%s\n", instruction->params->param.attr.str.str);
fprintf(f, "TYPE GF@$pops$type$ TF@%s\n", instruction->params->param.attr.str.str);
fprintf(f, "JUMPIFNEQ $pops$notBOOL$%d GF@$pops$type$ string@bool\n", cgData.uniqueCounter);
fprintf(f, "EXIT int@4\n");
fprintf(f, "LABEL $pops$notBOOL$%d\n", cgData.uniqueCounter);
cgData.uniqueCounter++;
}
}
/**
* Function generates if operation inside code.
* @param f Pointer to the IFJcode2018 source code.
* @param instruction Pointer to the single instruction from inside code.
*/
int generate_if(FILE *f, tInstr *instruction, bool scoped, int uniqueIf)
{
if(scoped)
{
uniqueIf++;
int ownIf = uniqueIf;
instruction->instr = NOP;
instruction = instruction->next;
while(instruction->instr != IF_COND_END)
{
generate_instruction(f, instruction);
instruction->instr = NOP;
instruction = instruction->next;
}
fprintf(f, "POPS TF@condition%d\n", ownIf);
fprintf(f, "TYPE TF@condition$type%d TF@condition%d\n", ownIf, ownIf);
fprintf(f, "JUMPIFEQ $else%d TF@condition$type%d string@nil\n", ownIf, ownIf);
fprintf(f, "JUMPIFNEQ $if_ok%d TF@condition$type%d string@bool\n", ownIf, ownIf);
fprintf(f, "JUMPIFEQ $else%d TF@condition%d bool@false\n", ownIf, ownIf);
fprintf(f, "LABEL $if_ok%d\n", ownIf);
instruction->instr = NOP;
instruction = instruction->next;
cgData.uniqueCounter++;
// If IF founds another scoped while or if, it must be generated first, but with increased unique number because the first "boss" while generated compiler variables for them already
while(instruction->instr != ELSE_CALL)
{
if(instruction->instr == WHILE_CALL)
{
uniqueIf = generate_while(f, instruction, true, uniqueIf);
}
else if(instruction->instr == IF_CALL)
{
uniqueIf = generate_if(f, instruction, true, uniqueIf);
}
else
{
generate_instruction(f, instruction);
}
instruction->instr = NOP;
instruction = instruction->next;
}
fprintf(f, "JUMP $else_end%d\n", ownIf);
fprintf(f, "LABEL $else%d\n", ownIf);
cgData.uniqueCounter++;
instruction->instr = NOP;
instruction = instruction->next;
while(instruction->instr != IF_END)
{
if(instruction->instr == WHILE_CALL)
{
uniqueIf = generate_while(f, instruction, true, uniqueIf);
cgData.uniqueCounter++;
}
else if(instruction->instr == IF_CALL)
{
uniqueIf = generate_if(f, instruction, true, uniqueIf);
cgData.uniqueCounter++;
}
else
{
generate_instruction(f, instruction);
}
instruction->instr = NOP;
instruction = instruction->next;
}
if(instruction->instr == IF_END)
{
instruction->instr = NOP;
}
fprintf(f, "LABEL $else_end%d\n", ownIf);
cgData.uniqueCounter++;
return uniqueIf;
}
else
{
tInstr *actualInstr = instruction;
int scopeIf = 0;
int tempUniqueCounter = cgData.uniqueCounter;
do
{
if(actualInstr->instr == DEFVAR)
{
fprintf(f, "DEFVAR TF@%s\n", instruction->params->param.attr.str.str);
fprintf(f, "DEFVAR TF@%s nil@nil\n", instruction->params->param.attr.str.str);
actualInstr->instr = NOP;
}
else if(actualInstr->instr == IF_END)
{
scopeIf--;
}
else if(actualInstr->instr == IF_CALL)
{
scopeIf++;
if(scopeIf == 1)
{
actualInstr->instr = NOP;
}
}
actualInstr = actualInstr->next;
} while(scopeIf != 0);
instruction = instruction->next;
while(instruction->instr != IF_COND_END)
{
generate_instruction(f, instruction);
instruction->instr = NOP;
instruction = instruction->next;
}
fprintf(f, "DEFVAR TF@condition%d\n", tempUniqueCounter);
fprintf(f, "DEFVAR TF@condition$type%d\n", tempUniqueCounter);
fprintf(f, "POPS TF@condition%d\n", tempUniqueCounter);
fprintf(f, "TYPE TF@condition$type%d TF@condition%d\n", tempUniqueCounter, tempUniqueCounter);
fprintf(f, "JUMPIFEQ $else%d TF@condition$type%d string@nil\n", tempUniqueCounter, tempUniqueCounter);
fprintf(f, "JUMPIFNEQ $if_ok%d TF@condition$type%d string@bool\n", tempUniqueCounter, tempUniqueCounter);
fprintf(f, "JUMPIFEQ $else%d TF@condition%d bool@false\n", tempUniqueCounter, tempUniqueCounter);
fprintf(f, "LABEL $if_ok%d\n", tempUniqueCounter);
instruction->instr = NOP;
cgData.uniqueCounter++;
while(instruction->next->instr != ELSE_CALL)
{
generate_instruction(f, instruction);
instruction->instr = NOP;
instruction = instruction->next;
}
generate_instruction(f, instruction);
tToken ret = choose_return(instruction);
if(ret.type != NOP)
{
fprintf(f, "MOVE TF@$noretval TF@%s\n",ret.attr.str.str);
}
if(ret.type == NORETVAL)
{
str_free(&ret.attr.str);
}
instruction->instr = NOP;
instruction = instruction->next;
fprintf(f, "JUMP $else_end%d\n", tempUniqueCounter);
fprintf(f, "LABEL $else%d\n", tempUniqueCounter);
cgData.uniqueCounter++;
instruction->instr = NOP;
while(instruction->next->instr != IF_END)
{
generate_instruction(f, instruction);
instruction->instr = NOP;
instruction = instruction->next;
}
generate_instruction(f, instruction);
ret = choose_return(instruction);
if(ret.type != NOP)
{
fprintf(f, "MOVE TF@$noretval TF@%s\n",ret.attr.str.str);
}
if(ret.type == NORETVAL)
{
str_free(&ret.attr.str);
}
instruction->instr = NOP;
instruction = instruction->next;
if(instruction->instr == IF_END)
{
instruction->instr = NOP;
}
fprintf(f, "LABEL $else_end%d\n", tempUniqueCounter);
cgData.uniqueCounter++;
return 0;
}
}
/**
* Function generates while operation inside code.
* @param f Pointer to the IFJcode2018 source code.
* @param instruction Pointer to the single instruction from inside code.
*/
int generate_while(FILE *f, tInstr *instruction, bool scoped, int uniqueWhile)
{
if(scoped)
{
uniqueWhile++;
int ownWhile = uniqueWhile;
instruction = instruction->next;
fprintf(f, "LABEL $while_start%d\n", ownWhile);
while(instruction->instr != WHILE_COND_END)
{
generate_instruction(f, instruction);
instruction->instr = NOP;
instruction = instruction->next;
}
fprintf(f, "POPS TF@condition%d\n", ownWhile);
fprintf(f, "TYPE TF@condition$type%d TF@condition%d\n", ownWhile, ownWhile);
fprintf(f, "JUMPIFEQ $while%d TF@condition$type%d string@nil\n", ownWhile, ownWhile);
fprintf(f, "JUMPIFNEQ $while_ok%d TF@condition$type%d string@bool\n", ownWhile, ownWhile);
fprintf(f, "JUMPIFEQ $while%d TF@condition%d bool@false\n", ownWhile, ownWhile);
fprintf(f, "LABEL $while_ok%d\n", ownWhile);
instruction->instr = NOP;
instruction = instruction->next;
cgData.uniqueCounter++;
// If WHILE founds another scoped while or if, it must be generated first, but with increased unique number because the first "boss" while generated compiler variables for them already
while(instruction->instr != WHILE_END)
{
if(instruction->instr == IF_CALL)
{
uniqueWhile = generate_if(f, instruction, true, uniqueWhile);
cgData.uniqueCounter++;
}
else if(instruction->instr == WHILE_CALL)
{
uniqueWhile = generate_while(f, instruction, true, uniqueWhile);
cgData.uniqueCounter++;
}
else
{
generate_instruction(f, instruction);
}
instruction->instr = NOP;
instruction = instruction->next;
}
if(instruction->instr == WHILE_END)
{
instruction->instr = NOP;
}
fprintf(f, "JUMP $while_start%d\n", ownWhile);
fprintf(f, "LABEL $while%d\n", ownWhile);
cgData.uniqueCounter++;
return uniqueWhile;
}
else
{
tInstr *actualInstr = instruction;
int scopeWhile = 0;
int tempUniqueCounter = cgData.uniqueCounter;
int tempIfWhileUnique = cgData.uniqueCounter;
int temp = tempIfWhileUnique;
do
{
if(actualInstr->instr == DEFVAR)
{
fprintf(f, "DEFVAR TF@%s\n", instruction->params->param.attr.str.str);
fprintf(f, "DEFVAR TF@%s nil@nil\n", instruction->params->param.attr.str.str);
actualInstr->instr = NOP;
}
else if(actualInstr->instr == IF_CALL)
{
tempIfWhileUnique++;
fprintf(f, "DEFVAR TF@condition%d\n", tempIfWhileUnique);
fprintf(f, "DEFVAR TF@condition$type%d\n", tempIfWhileUnique);
cgData.uniqueCounter++;
}
else if(actualInstr->instr == WHILE_END)
{
scopeWhile--;
}
else if(actualInstr->instr == WHILE_CALL)
{
tempIfWhileUnique++;
fprintf(f, "DEFVAR TF@condition%d\n", tempIfWhileUnique);
fprintf(f, "DEFVAR TF@condition$type%d\n", tempIfWhileUnique);
scopeWhile++;
if(scopeWhile == 1)
{
instruction->instr = NOP;
}
}
actualInstr = actualInstr->next;
} while(scopeWhile != 0);
fprintf(f, "DEFVAR TF@condition%d\n", tempUniqueCounter);
fprintf(f, "DEFVAR TF@condition$type%d\n", tempUniqueCounter);
fprintf(f, "LABEL $while_start%d\n", tempUniqueCounter);
instruction = instruction->next;
while(instruction->instr != WHILE_COND_END)
{
generate_instruction(f, instruction);
instruction->instr = NOP;
instruction = instruction->next;
}
fprintf(f, "POPS TF@condition%d\n", tempUniqueCounter);
fprintf(f, "TYPE TF@condition$type%d TF@condition%d\n", tempUniqueCounter, tempUniqueCounter);
fprintf(f, "JUMPIFEQ $while%d TF@condition$type%d string@nil\n", tempUniqueCounter, tempUniqueCounter);
fprintf(f, "JUMPIFNEQ $while_ok%d TF@condition$type%d string@bool\n", tempUniqueCounter, tempUniqueCounter);
fprintf(f, "JUMPIFEQ $while%d TF@condition%d bool@false\n", tempUniqueCounter, tempUniqueCounter);
fprintf(f, "LABEL $while_ok%d\n", tempUniqueCounter);
instruction->instr = NOP;
instruction = instruction->next;
cgData.uniqueCounter++;
tempIfWhileUnique = temp;
// This "boss" WHILE generated compiler variables for scoped if or while already, so interpreter will not fail, if "boss" founds them, they will be generated, with their correct unique number
while(instruction->instr != WHILE_END)
{
if(instruction->instr == IF_CALL)
{
tempIfWhileUnique = generate_if(f, instruction, true, tempIfWhileUnique);
tempIfWhileUnique++;
}
else if(instruction->instr == WHILE_CALL)
{
tempIfWhileUnique = generate_while(f, instruction, true, tempIfWhileUnique);
tempIfWhileUnique++;
}
else
{
generate_instruction(f, instruction);
}
instruction->instr = NOP;
instruction = instruction->next;
}
if(instruction->instr == WHILE_END)
{
instruction->instr = NOP;
}
fprintf(f, "JUMP $while_start%d\n", tempUniqueCounter);
fprintf(f, "LABEL $while%d\n", tempUniqueCounter);
cgData.uniqueCounter++;
return 0;
}
}
/**
* Function generates function call with return value which is going to be saved, inside source code.
* @param f Pointer to the IFJcode2018 source code.
* @param instruction Pointer to the single instruction from inside code.
* @param builtin Signifies if called function is builtin or user-defined.
*/