-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.c
1462 lines (1296 loc) · 37.6 KB
/
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
/**
* @file generator.c
* @author Tomáš Hobza ([email protected])
* @brief Generator of IFJcode20.
* @version 0.1
* @date 2023-11-24
*
* @copyright Copyright (c) 2023
* Project: IFJ compiler
*
*/
#include "generator.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/// GLOBAL COUNTERS
int if_counter = 0;
int while_counter = 0;
int tmp_counter = 0;
int label_counter = 0;
int_stack *else_label_st = NULL;
DEFINE_STACK_FUNCTIONS(int);
/// OPERAND FUNCTIONS
char *label(char *name)
{
char *label = malloc(sizeof(char) * (strlen(name) + 1));
sprintf(label, "%s", name);
return label;
}
char *type(Expression_type type)
{
char *type_str = malloc(sizeof(char) * 10);
switch (type)
{
case TYPE_INT:
case TYPE_INT_NIL:
sprintf(type_str, "int");
break;
case TYPE_DOUBLE:
case TYPE_DOUBLE_NIL:
sprintf(type_str, "float");
break;
case TYPE_STRING:
case TYPE_STRING_NIL:
sprintf(type_str, "string");
break;
case TYPE_BOOL:
case TYPE_BOOL_NIL:
sprintf(type_str, "bool");
break;
case TYPE_NIL:
sprintf(type_str, "nil");
break;
case TYPE_INVALID:
sprintf(type_str, "invalid");
break;
default:
sprintf(type_str, "unknown");
break;
}
return type_str;
}
char *variable(char *id, int scope, bool has_suffix)
{
char *var_name = malloc(sizeof(char) * (10 + strlen(id)));
char *scope_prefix = scope == 0 ? "GF" : (scope < 0 ? "TF" : "LF"); // scope determies the frame prefix
char *suffix = malloc(sizeof(char) * 10);
sprintf(suffix, "%d", scope);
sprintf(var_name, "%s@$%s%s", scope_prefix, id, has_suffix ? suffix : "");
return var_name;
}
char *literal(Token token)
{
return format_token(token);
}
char *symbol(Token symbol)
{
return symb_resolve(symbol);
}
/// INSTRUCTION FUNCTIONS
void handle_0_operand_instructions(Instruction inst)
{
char *instruction = instructionToString(inst);
fprintf(out_code_file, "%s\n", instruction);
free(instruction);
}
void handle_1_operand_instructions(Instruction inst, char *op1)
{
char *instruction = instructionToString(inst);
fprintf(out_code_file, "%s %s\n", instruction, op1);
free(instruction);
}
void handle_2_operand_instructions(Instruction inst, char *op1, char *op2)
{
char *instruction = instructionToString(inst);
fprintf(out_code_file, "%s %s %s\n", instruction, op1, op2);
free(instruction);
}
void handle_3_operand_instructions(Instruction inst, char *op1, char *op2, char *op3)
{
char *instruction = instructionToString(inst);
fprintf(out_code_file, "%s %s %s %s\n", instruction, op1, op2, op3);
free(instruction);
}
void processInstruction(Instruction inst, char **operands, int operands_count)
{
switch (operands_count)
{
case 0:
handle_0_operand_instructions(inst);
break;
case 1:
handle_1_operand_instructions(inst, operands[0]);
break;
case 2:
handle_2_operand_instructions(inst, operands[0], operands[1]);
break;
case 3:
handle_3_operand_instructions(inst, operands[0], operands[1], operands[2]);
break;
default:
throw_error(INTERNAL_ERR, -1, "Invalid number of tokens\n");
break;
}
}
/// FORMATTING FUNCTIONS
char *symb_resolve(Token token)
{
char *var_name = malloc(sizeof(char) * (10 + strlen(token.token_value)));
switch (token.type)
{
case TOKEN_IDENTIFICATOR:
{
// for identificators, we need to find the variable in the symtable
symtable_item *found = symtable_find_in_stack(token.token_value, sym_st, false);
if (found == NULL)
{
// if the symbol has not been found, we we will presume that it's just temporary
sprintf(var_name, "%s@$%s", "TF", token.token_value);
break;
}
symtable_item item = *found;
if (item.type == VARIABLE)
{
// check if the variable is initialized
if (item.data.var_data->type == TYPE_INVALID)
{
throw_error(INTERNAL_ERR, -1, "Variable '%s' is invalid\n", token.token_value);
}
sprintf(var_name, "%s@$%s%d", item.scope == 0 ? "GF" : (item.scope < 0 ? "TF" : "LF"), item.id,
item.scope);
break;
}
else if (item.type == FUNCTION)
{
// functions are just labels and function names are already uniques
sprintf(var_name, "%s", item.id);
break;
}
else
{
// this state should never occur
sprintf(var_name, "%s", token.token_value);
break;
}
sprintf(var_name, "%s@$%s%d", item.scope == 0 ? "GF" : (item.scope < 0 ? "TF" : "LF"), item.id, (int)item.data.var_data->gen_id_idx);
break;
}
case TOKEN_STRING:
case TOKEN_INT:
case TOKEN_DOUBLE:
case TOKEN_NIL:
case TOKEN_BOOL:
{
// literals need to be formatted based on their type
free(var_name);
var_name = format_token(token);
break;
}
default:
sprintf(var_name, "%s", token.token_value);
break;
}
return var_name;
}
char *format_token(Token token)
{
char *formatted_value = NULL;
switch (token.type)
{
case TOKEN_IDENTIFICATOR:
throw_error(INTERNAL_ERR, -1, "Identificator\n");
break;
case TOKEN_INT:
{
// Format integer literals with "int@"
formatted_value = malloc(strlen(token.token_value) + 5); //"int@" and '\0'
sprintf(formatted_value, "int@%s", token.token_value);
break;
}
case TOKEN_DOUBLE:
{
// Format floating-point literals with "float@"
double double_value = atof(token.token_value); // Convert to double
formatted_value = malloc(sizeof(char) * 60); // Allocating enough space
sprintf(formatted_value, "float@%a", double_value);
break;
}
case TOKEN_STRING:
{
// Format string literals with "string@"
formatted_value = malloc((strlen(token.token_value) * 10) + 8); //"string@" and '\0'
sprintf(formatted_value, "string@%s", escapeString(token.token_value));
break;
}
case TOKEN_BOOL:
{
// Format bool literals with "bool@"
formatted_value = malloc(strlen(token.token_value) + 6); //"bool@" and '\0'
sprintf(formatted_value, "bool@%s", token.token_value);
break;
}
case TOKEN_NIL:
{
// Format nil with "nil@"
formatted_value = malloc(strlen("nil@nil") + 1); //"nil@" and '\0'
sprintf(formatted_value, "nil@nil");
break;
}
default:
{
// Format other tokens with their value
formatted_value = malloc(strlen(token.token_value) + 1);
sprintf(formatted_value, "%s", token.token_value);
break;
}
}
return formatted_value;
}
void print_out_code()
{
// Check if out_code_file is NULL
if (out_code_file == NULL)
{
throw_error(INTERNAL_ERR, -1, "Error: out_code_file is not initialized.\n");
return;
}
// Reset the file position to the beginning of the file
if (fseek(out_code_file, 0, SEEK_SET) != 0)
{
throw_error(INTERNAL_ERR, -1, "Error: Failed to seek in out_code_file.\n");
return;
}
int c; // fgetc returns int, not char
while ((c = fgetc(out_code_file)) != EOF)
{
printf("%c", (char)c);
}
}
/// GENERATION FUNCTIONS
void generate_func_header(symtable_item func_item)
{
// CREATE OPERANDS
char *func_lbl = malloc(sizeof(char) * (strlen(func_item.id) + 5));
strcpy(func_lbl, func_item.id);
char *func_end_lbl = malloc(sizeof(char) * (strlen(func_item.id) + 5));
sprintf(func_end_lbl, "%s_end", func_item.id);
generate_instruction(JUMP, label(func_end_lbl)); // jump over the function body
generate_instruction(LABEL, label(func_lbl)); // function label
fprintf(out_code_file, "\n");
// frame initialization
generate_instruction(PUSHFRAME);
generate_instruction(CREATEFRAME);
// initialize function params (in reverse order, because of stack)
fprintf(out_code_file, "# function params\n");
for (int i = func_item.data.func_data->params_count - 1; i >= 0; i--)
{
Token param = (Token){
.type = TOKEN_IDENTIFICATOR,
.token_value = malloc(sizeof(char) * (strlen(func_item.data.func_data->params[i].id) + 1)),
};
strcpy(param.token_value, func_item.data.func_data->params[i].id);
char *var_name = symbol(param);
HANDLE_DEFVAR(generate_instruction(DEFVAR, var_name););
generate_instruction(POPS, var_name);
free(var_name);
}
fprintf(out_code_file, "# function params end\n\n");
free(func_end_lbl);
free(func_lbl);
}
void generate_func_end(symtable_item func_item)
{
char *func_lbl;
func_lbl = malloc(sizeof(char) * (strlen(func_item.id) + 5));
sprintf(func_lbl, "%s_end", func_item.id);
generate_instruction(LABEL, label(func_lbl)); // function end label
fprintf(out_code_file, "\n");
free(func_lbl);
}
void generate_builtin_func_call(Token func, int param_cnt)
{
char *tmp_token = malloc(sizeof(char) * 20);
sprintf(tmp_token, "tmp%d", tmp_counter);
char *tmp_token_name = variable(tmp_token, -1, false);
BuiltinFunc builting_inst = getBuiltInFunctionName(func);
switch (builting_inst)
{
case B_WRITE:
fprintf(out_code_file, "# WRITE\n");
// all the parameters separately in reverse order (because of stack)
for (int i = 0; i < param_cnt; i++)
{
sprintf(tmp_token, "tmp%d", tmp_counter + param_cnt - i - 1);
char *tmp_token_name = variable(tmp_token, -1, false);
HANDLE_DEFVAR(generate_instruction(DEFVAR, tmp_token_name););
generate_instruction(POPS, tmp_token_name);
// tmp_counter++;
sprintf(tmp_token, "tmp%d", tmp_counter);
tmp_token_name = variable(tmp_token, -1, false);
}
// write all the parameters separately
for (int i = 0; i < param_cnt; i++)
{
sprintf(tmp_token, "tmp%d", tmp_counter);
char *tmp_token_name = variable(tmp_token, -1, false);
generate_instruction(WRITE, tmp_token_name);
tmp_counter++;
sprintf(tmp_token, "tmp%d", tmp_counter);
tmp_token_name = variable(tmp_token, -1, false);
}
tmp_counter--;
fprintf(out_code_file, "# WRITE END\n");
fprintf(out_code_file, "\n");
break;
case B_READ:
// read the input and push it on the stack
fprintf(out_code_file, "# READ\n");
HANDLE_DEFVAR(generate_instruction(DEFVAR, tmp_token_name););
generate_instruction(READ, tmp_token_name, type(getReadType(func))); // the type is determined by the function name
generate_instruction(PUSHS, tmp_token_name);
fprintf(out_code_file, "# READ END\n");
fprintf(out_code_file, "\n");
break;
case B_INT2DOUBLE:
// just a stack instruction
fprintf(out_code_file, "# INT2DOUBLE\n");
generate_instruction(INT2FLOATS);
fprintf(out_code_file, "# INT2DOUBLE END\n");
fprintf(out_code_file, "\n");
break;
case B_DOUBLE2INT:
// just a stack instruction
fprintf(out_code_file, "# FLOAT2INT\n");
generate_instruction(FLOAT2INTS);
fprintf(out_code_file, "# FLOAT2INT END\n");
fprintf(out_code_file, "\n");
break;
case B_LENGTH:
{
fprintf(out_code_file, "# LENGTH\n");
sprintf(tmp_token, "tmp%d", tmp_counter);
tmp_counter++;
sprintf(tmp_token, "tmp%d", tmp_counter);
char *tmp_token_name_2 = variable(tmp_token, -1, false);
tmp_counter++;
// get the string to a temporary variable
HANDLE_DEFVAR(generate_instruction(DEFVAR, tmp_token_name););
HANDLE_DEFVAR(generate_instruction(DEFVAR, tmp_token_name_2););
generate_instruction(POPS, tmp_token_name_2);
// get the length of the string
generate_instruction(STRLEN, tmp_token_name, tmp_token_name_2);
// push the length on the stack
generate_instruction(PUSHS, tmp_token_name);
fprintf(out_code_file, "# LENGTH END\n");
fprintf(out_code_file, "\n");
break;
}
case B_SUBSTRING:
{
fprintf(out_code_file, "# SUBSTRING\n");
// end index variable
char *end_name = malloc(sizeof(char) * 20);
sprintf(end_name, "tmp%d", tmp_counter);
char *end = variable(end_name, -1, false);
tmp_counter++;
// start index variable
char *start_name = malloc(sizeof(char) * 20);
sprintf(start_name, "tmp%d", tmp_counter);
char *start = variable(start_name, -1, false);
tmp_counter++;
// string variable
char *string_name = malloc(sizeof(char) * 20);
sprintf(string_name, "tmp%d", tmp_counter);
char *string = variable(string_name, -1, false);
tmp_counter++;
// internal variable (used for storing the characters)
char *internal_name = malloc(sizeof(char) * 20);
sprintf(internal_name, "tmp%d", tmp_counter);
char *internal = variable(internal_name, -1, false);
tmp_counter++;
// result variable (used for storing the resulting string)
char *result_name = malloc(sizeof(char) * 20);
sprintf(result_name, "tmp%d", tmp_counter);
char *result = variable(result_name, -1, false);
tmp_counter++;
HANDLE_DEFVAR(generate_instruction(DEFVAR, end););
HANDLE_DEFVAR(generate_instruction(DEFVAR, start););
HANDLE_DEFVAR(generate_instruction(DEFVAR, string););
HANDLE_DEFVAR(generate_instruction(DEFVAR, internal););
HANDLE_DEFVAR(generate_instruction(DEFVAR, result););
generate_instruction(POPS, end);
generate_instruction(POPS, start);
generate_instruction(POPS, string);
generate_instruction(MOVE, result, literal((Token){
.type = TOKEN_STRING,
.token_value = "",
}));
char *loop_label = malloc(sizeof(char) * 20);
sprintf(loop_label, "loop_substring_%d", tmp_counter);
char *end_label = malloc(sizeof(char) * 20);
sprintf(end_label, "end_substring_%d", tmp_counter);
// loop until start != end
generate_instruction(LABEL, label(loop_label));
generate_instruction(JUMPIFEQ, label(end_label), start, end);
// concat the character to the result
generate_instruction(GETCHAR, internal, string, start);
generate_instruction(CONCAT, result, result, internal);
generate_instruction(ADD, start, start, literal((Token){
.type = TOKEN_INT,
.token_value = "1",
}));
generate_instruction(JUMP, label(loop_label));
generate_instruction(LABEL, label(end_label));
// push the resulting string on the stack
generate_instruction(PUSHS, result);
fprintf(out_code_file, "# SUBSTRING END\n");
fprintf(out_code_file, "\n");
tmp_counter++;
break;
}
case B_ORD:
{
fprintf(out_code_file, "# STRI2INT\n");
// push 0 as the index for the STRI2INTS instruction
generate_instruction(PUSHS, literal((Token){
.type = TOKEN_INT,
.token_value = "0",
}));
generate_instruction(STRI2INTS);
fprintf(out_code_file, "# STRI2INT END\n");
fprintf(out_code_file, "\n");
break;
}
case B_CHR:
{
// just a stack instruction
fprintf(out_code_file, "# CHR\n");
generate_instruction(INT2CHARS);
fprintf(out_code_file, "# CHR END\n");
fprintf(out_code_file, "\n");
break;
}
default:
throw_error(INTERNAL_ERR, -1, "Invalid built-in function.\n");
break;
}
tmp_counter++;
}
void generate_if_start()
{
if (else_label_st == NULL)
{
else_label_st = int_stack_init();
int_stack_push(else_label_st, 0);
}
int elif_counter = int_stack_top(else_label_st);
char *true_op = literal((Token){
.type = TOKEN_BOOL,
.token_value = "true",
});
char *if_lbl = malloc(sizeof(char) * 20);
sprintf(if_lbl, "else%d_%d", if_counter, elif_counter);
fprintf(out_code_file, "# if%d start\n", if_counter);
generate_instruction(PUSHS, true_op); // push true to check the condition
generate_instruction(JUMPIFNEQS, label(if_lbl));
fprintf(out_code_file, "\n");
free(if_lbl);
if_counter++;
int_stack_push(else_label_st, 0);
}
void generate_elseif_else()
{
if_counter--;
int elif_counter = 0;
if (!int_stack_empty(else_label_st))
{
elif_counter = int_stack_pop(else_label_st);
}
char *endif_lbl = malloc(sizeof(char) * 10);
sprintf(endif_lbl, "else%d_%d", if_counter, elif_counter);
char *elsif_else_lbl = malloc(sizeof(char) * 20);
sprintf(elsif_else_lbl, "else%d_%d", if_counter, elif_counter);
generate_instruction(JUMP, label(endif_lbl)); // jump to end of if block
generate_instruction(LABEL, label(elsif_else_lbl));
fprintf(out_code_file, "\n");
elif_counter++;
free(endif_lbl);
free(elsif_else_lbl);
int_stack_push(else_label_st, elif_counter);
if_counter++;
}
void generate_elseif_if()
{
if_counter--;
int elif_counter = 0;
if (!int_stack_empty(else_label_st))
{
elif_counter = int_stack_pop(else_label_st);
}
char *true_op = literal((Token){
.type = TOKEN_BOOL,
.token_value = "true",
});
char *elsif_if_lbl = malloc(sizeof(char) * 20);
sprintf(elsif_if_lbl, "else%d_%d", if_counter, elif_counter);
fprintf(out_code_file, "# elseif%d start\n", elif_counter);
generate_instruction(PUSHS, true_op); // push true to check the condition
generate_instruction(JUMPIFNEQS, label(elsif_if_lbl));
fprintf(out_code_file, "\n");
free(elsif_if_lbl);
int_stack_push(else_label_st, elif_counter);
if_counter++;
}
void generate_else()
{
if_counter--;
int elif_counter = 0;
if (!int_stack_empty(else_label_st))
{
elif_counter = int_stack_pop(else_label_st);
}
char *else_lbl = malloc(sizeof(char) * 10);
sprintf(else_lbl, "else%d_%d", if_counter, elif_counter);
elif_counter++;
char *endif_lbl = malloc(sizeof(char) * 10);
sprintf(endif_lbl, "else%d_%d", if_counter, elif_counter);
fprintf(out_code_file, "# if%d else\n", if_counter);
generate_instruction(JUMP, label(endif_lbl)); // jump to end of if block
generate_instruction(LABEL, label(else_lbl));
fprintf(out_code_file, "\n");
free(else_lbl);
free(endif_lbl);
int_stack_push(else_label_st, elif_counter);
if_counter++;
}
void generate_if_end()
{
if_counter--;
int elif_counter = 0;
if (!int_stack_empty(else_label_st))
{
elif_counter = int_stack_pop(else_label_st);
}
fprintf(out_code_file, "# if%d end\n", if_counter);
char *endif_lbl = malloc(sizeof(char) * 10);
sprintf(endif_lbl, "else%d_%d", if_counter, elif_counter);
generate_instruction(LABEL, label(endif_lbl)); // ending label of the if block
fprintf(out_code_file, "\n");
free(endif_lbl);
}
void generate_while_start()
{
// just the label to jump to
char *while_lbl = malloc(sizeof(char) * 10);
sprintf(while_lbl, "while%d", while_counter);
fprintf(out_code_file, "# while%d start\n", while_counter);
// all the variable definitions should be above the looping while body, so we need to create another file just for the while body, save only the variable defenitions of the body into the original file and then copy the body file back to the original file
while_def_out_code_file = out_code_file;
out_code_file = tmpfile();
if (out_code_file == NULL)
{
throw_error(INTERNAL_ERR, -1, "Error: out_code_file is not initialized.\n");
return;
}
is_in_loop = true;
generate_instruction(LABEL, label(while_lbl));
fprintf(out_code_file, "\n");
free(while_lbl);
while_counter++;
}
void generate_while_condition()
{
while_counter--;
char *endwhile_lbl = malloc(sizeof(char) * 10);
sprintf(endwhile_lbl, "endwhile%d", while_counter);
char *true_op = literal((Token){
.type = TOKEN_BOOL,
.token_value = "true",
});
fprintf(out_code_file, "# while%d condition\n", while_counter);
// check condition and jump accordingly
generate_instruction(PUSHS, true_op);
generate_instruction(JUMPIFNEQS, label(endwhile_lbl));
fprintf(out_code_file, "\n");
while_counter++;
}
void generate_while_end()
{
while_counter--;
// copy the while body back to the original file, swap the pointers back and delete the temporary file
copyFileContents(out_code_file, while_def_out_code_file);
out_code_file = while_def_out_code_file;
is_in_loop = false;
fprintf(out_code_file, "# while%d end\n", if_counter);
char *endwhile_lbl = malloc(sizeof(char) * 10);
sprintf(endwhile_lbl, "endwhile%d", while_counter);
char *while_lbl = malloc(sizeof(char) * 10);
sprintf(while_lbl, "while%d", while_counter);
generate_instruction(JUMP, label(while_lbl)); // jump back to condition
generate_instruction(LABEL, label(endwhile_lbl)); // while ending label
fprintf(out_code_file, "\n");
free(endwhile_lbl);
}
void generate_implicit_init(symtable_item var_item)
{
if (var_item.data.var_data->is_initialized == false)
{
Expression_type type = var_item.data.var_data->type;
if (type == TYPE_INT_NIL || type == TYPE_DOUBLE_NIL || type == TYPE_STRING_NIL || type == TYPE_BOOL_NIL)
{
char *nil = literal((Token){
.type = TOKEN_NIL,
.token_value = "nil",
});
generate_instruction(MOVE, variable(var_item.id, sym_st->size - 1, true), nil);
}
}
}
void generate_temp_pop()
{
// create a temporary variable and pop the stack into it
char *tmp_token = malloc(sizeof(char) * 20);
sprintf(tmp_token, "tmp%d", tmp_counter);
char *tmp_token_name = variable(tmp_token, -1, false);
HANDLE_DEFVAR(generate_instruction(DEFVAR, tmp_token_name););
generate_instruction(POPS, tmp_token_name);
tmp_counter++;
free(tmp_token);
}
void generate_temp_push()
{
// push the value of the latest temporary value back onto the stack
tmp_counter--;
char *tmp_token = malloc(sizeof(char) * 20);
sprintf(tmp_token, "tmp%d", tmp_counter);
char *tmp_token_name = variable(tmp_token, -1, false);
generate_instruction(PUSHS, tmp_token_name);
tmp_counter++;
free(tmp_token);
}
void generate_nil_coelacing()
{
generate_temp_pop();
generate_temp_pop();
// operand deinitions
char *tmp_token1 = malloc(sizeof(char) * 20);
sprintf(tmp_token1, "tmp%d", tmp_counter - 2);
char *tmp_token_name1 = variable(tmp_token1, -1, false);
char *tmp_token2 = malloc(sizeof(char) * 20);
sprintf(tmp_token2, "tmp%d", tmp_counter - 1);
char *tmp_token_name2 = variable(tmp_token2, -1, false);
char *not_nil_label = malloc(sizeof(char) * 20);
sprintf(not_nil_label, "not_nil_%d", tmp_counter);
char *was_nil_label = malloc(sizeof(char) * 20);
sprintf(was_nil_label, "was_nil_%d", tmp_counter);
// instruction generation
// push left operand and nil
generate_instruction(PUSHS, tmp_token_name2);
generate_instruction(PUSHS, literal((Token){
.type = TOKEN_NIL,
.token_value = "nil",
}));
// if left operand is nil, push right operand
generate_instruction(JUMPIFNEQS, label(not_nil_label));
// push right operand
generate_instruction(PUSHS, tmp_token_name1);
generate_instruction(JUMP, label(was_nil_label));
generate_instruction(LABEL, label(not_nil_label));
// else push left operand
generate_instruction(PUSHS, tmp_token_name2);
generate_instruction(LABEL, label(was_nil_label));
}
void generate_string_concat()
{
generate_temp_pop();
generate_temp_pop();
// operand deinitions
char *tmp_token1 = malloc(sizeof(char) * 20);
sprintf(tmp_token1, "tmp%d", tmp_counter - 2);
char *tmp_token_name1 = variable(tmp_token1, -1, false);
char *tmp_token2 = malloc(sizeof(char) * 20);
sprintf(tmp_token2, "tmp%d", tmp_counter - 1);
char *tmp_token_name2 = variable(tmp_token2, -1, false);
// concant the two strings and push the result
generate_instruction(CONCAT, tmp_token_name1, tmp_token_name2, tmp_token_name1);
generate_instruction(PUSHS, tmp_token_name1);
}
/// UTILITY FUNCTIONS
char *instructionToString(Instruction in)
{
char *instruction = malloc(sizeof(char) * 100);
switch (in)
{
case CREATEFRAME:
sprintf(instruction, "CREATEFRAME");
break;
case PUSHFRAME:
sprintf(instruction, "PUSHFRAME");
break;
case POPFRAME:
sprintf(instruction, "POPFRAME");
break;
case RETURN:
sprintf(instruction, "RETURN");
break;
case CLEARS:
sprintf(instruction, "CLEARS");
break;
case ADDS:
sprintf(instruction, "ADDS");
break;
case SUBS:
sprintf(instruction, "SUBS");
break;
case DIVS:
sprintf(instruction, "DIVS");
break;
case IDIVS:
sprintf(instruction, "IDIVS");
break;
case MULS:
sprintf(instruction, "MULS");
break;
case LTS:
sprintf(instruction, "LTS");
break;
case EQS:
sprintf(instruction, "EQS");
break;
case GTS:
sprintf(instruction, "GTS");
break;
case ANDS:
sprintf(instruction, "ANDS");
break;
case ORS:
sprintf(instruction, "ORS");
break;
case NOTS:
sprintf(instruction, "NOTS");
break;
case INT2FLOATS:
sprintf(instruction, "INT2FLOATS");
break;
case FLOAT2INTS:
sprintf(instruction, "FLOAT2INTS");
break;
case INT2CHARS:
sprintf(instruction, "INT2CHARS");
break;
case STRI2INTS:
sprintf(instruction, "STRI2INTS");
break;
case BREAK:
sprintf(instruction, "BREAK");
break;
case CALL:
sprintf(instruction, "CALL");
break;
case LABEL:
sprintf(instruction, "LABEL");
break;
case JUMP:
sprintf(instruction, "JUMP");
break;
case JUMPIFEQS:
sprintf(instruction, "JUMPIFEQS");
break;
case JUMPIFNEQS:
sprintf(instruction, "JUMPIFNEQS");
break;
case DEFVAR:
sprintf(instruction, "DEFVAR");
break;
case POPS:
sprintf(instruction, "POPS");
break;
case PUSHS:
sprintf(instruction, "PUSHS");
break;
case WRITE:
sprintf(instruction, "WRITE");
break;
case EXIT:
sprintf(instruction, "EXIT");
break;
case DPRINT:
sprintf(instruction, "DPRINT");
break;
case READ:
sprintf(instruction, "READ");
break;
case MOVE:
sprintf(instruction, "MOVE");
break;
case INT2FLOAT:
sprintf(instruction, "INT2FLOAT");
break;
case FLOAT2INT:
sprintf(instruction, "FLOAT2INT");
break;
case INT2CHAR:
sprintf(instruction, "INT2CHAR");
break;
case STRI2INT:
sprintf(instruction, "STRI2INT");
break;
case STRLEN:
sprintf(instruction, "STRLEN");
break;
case TYPE:
sprintf(instruction, "TYPE");
break;
case ADD:
sprintf(instruction, "ADD");
break;
case SUB:
sprintf(instruction, "SUB");
break;
case DIV:
sprintf(instruction, "DIV");
break;
case IDIV:
sprintf(instruction, "IDIV");
break;
case MUL:
sprintf(instruction, "MUL");
break;