forked from vojone/IFJ21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.c
1648 lines (1273 loc) · 45.8 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
/******************************************************************************
* IFJ21
* generator.c
*
* Authors: Juraj Dědič (xdedic07), Tomáš Dvořák (xdvora3r)
* Purpose: Implementation of code generating functions
* For function description see correspoding .h file
*
* Last change: 8. 12. 2021
*****************************************************************************/
/**
* @file generator.c
* @brief Source file with implementation of code generator
* @note For function description see correspoding .h file
*
* @authors Juraj Dědič (xdedic07), Tomáš Dvořák (xdvora3r)
*/
#include "generator.h"
#define VAR_FORMAT "TF@&VAR&"
/*** Functions for internal represenation ***/
DSTACK(instr_t *, instr,)
DSTACK(prog_t, prog,)
instr_t *new_instruction() {
instr_t *instr = NULL;
instr = (instr_t *)malloc(sizeof(instr_t));
if(!instr) {
fprintf(stderr,"Allocation error!\n");
return NULL;
}
instr->next = NULL;
instr->prev = NULL;
if(str_init(&instr->content) != STR_SUCCESS) {
free(instr);
return NULL;
}
return instr;
}
int set_instruction(instr_t *instr, const char *const _Format, va_list args) {
size_t written = 0;
string_t *cont = &(instr->content);
va_list args_tmp;
va_copy(args_tmp, args); //Saving args for multiple use
written = vsnprintf(cont->str, cont->alloc_size, _Format, args); //First attemt to store
while(written >= cont->alloc_size) { //Capacity of dynamic string is too small
str_clear(cont);
if(extend_string(cont) != STR_SUCCESS) {
return INTERNAL_ERROR;
}
va_copy(args, args_tmp); //Restore args (its undefined)
written = vsnprintf(cont->str, cont->alloc_size, _Format, args); //Next attempt
}
return EXIT_SUCCESS;
}
void instr_dtor(instr_t *instr) {
instr->next = NULL;
instr->prev = NULL;
str_dtor(&instr->content);
free(instr);
}
void init_new_prog(prog_t *program) {
program->first_instr = NULL;
program->last_instr = NULL;
program->cycle_nest_lvl = 0;
}
void program_dtor(prog_t *program) {
instr_t *current = program->last_instr;
instr_t *next_deletion;
while(current) { //Deletion of every instruction in given program from its end
next_deletion = current->prev;
instr_dtor(current);
current = next_deletion;
}
program->first_instr = NULL;
program->last_instr = NULL;
}
instr_t *get_last(prog_t *program) {
return program->last_instr;
}
instr_t *get_first(prog_t *program) {
return program->last_instr;
}
instr_t *get_next(instr_t *instr) {
return instr->next;
}
instr_t *get_prev(instr_t *instr) {
return instr->prev;
}
void app_instr(prog_t *dst, const char *const _Format, ...) {
//Creation of new instruction
instr_t *new_instr = new_instruction();
if(new_instr == NULL) {
exit(INTERNAL_ERROR);
}
va_list args;
va_start(args, _Format);
if(set_instruction(new_instr, _Format, args) != EXIT_SUCCESS) {
exit(INTERNAL_ERROR);
}
va_end(args);
//Putting instruction to the end of program
if(dst->first_instr == NULL) {
dst->first_instr = new_instr;
}
else {
new_instr->prev = dst->last_instr;
dst->last_instr->next = new_instr;
}
dst->last_instr = new_instr;
}
void app_prog(prog_t *dst, prog_t *prog) {
if(get_first(dst) != NULL) { //Destination program can be empty
if(get_first(prog) != NULL) { //Apended program can be empty
prog->first_instr->prev = dst->last_instr;
}
dst->last_instr->next = prog->first_instr;
}
else {
dst->first_instr = prog->first_instr;
}
dst->last_instr = prog->last_instr;
}
void ins_after(prog_t *dst, instr_t *instr, const char *const _Format, ...) {
if(instr == NULL) {
return;
}
//Creation of new instruction
instr_t *new_instr = new_instruction();
if(new_instr == NULL) {
exit(INTERNAL_ERROR);
}
va_list args;
va_start(args, _Format);
if(set_instruction(new_instr, _Format, args) != EXIT_SUCCESS) {
exit(INTERNAL_ERROR);
}
va_end(args);
instr_t * instr_after = instr->next;
//Putting instruction inside list of instructions (program)
instr->next = new_instr;
new_instr->prev = instr;
if(instr_after != NULL) {
instr_after->prev = new_instr;
new_instr->next = instr_after;
}
else {
dst->last_instr = new_instr;
new_instr->next = NULL;
}
}
void ins_before(prog_t *dst, instr_t *instr, const char *const _Format, ...) {
if(instr == NULL) {
return;
}
instr_t *new_instr = new_instruction();
if(new_instr == NULL) {
exit(INTERNAL_ERROR);
}
va_list args;
va_start(args, _Format);
if(set_instruction(new_instr, _Format, args) != EXIT_SUCCESS) {
exit(INTERNAL_ERROR);
}
va_end(args);
instr_t * instr_before = instr->prev;
//Putting instruction inside list of instructions (program)
instr->prev = new_instr;
new_instr->next = instr;
if(instr_before != NULL) {
instr_before->next = new_instr;
new_instr->prev = instr_before;
}
else {
dst->first_instr = new_instr;
new_instr->prev = NULL;
}
}
instr_t * fill_stack(instr_stack_t *to_be_filled, prog_t *dst,
instr_t *from, instr_t *to) {
instr_t * current = from;
while(current) {
if(!instr_push(to_be_filled, current)) {
return NULL;
}
if(current == to) {
break;
}
current = current->next;
}
return current;
}
void revert(prog_t *dst, instr_t *from, instr_t *to) {
if(from == NULL || to == NULL) {
return;
}
instr_stack_t stack;
if(!instr_stack_init(&stack)) {
return;
}
instr_t * instr_before = from->prev;
instr_t * instr_after = to->next;
if(!fill_stack(&stack, dst, from, to)) { //Fill stack with instructions that will be reverted
instr_stack_dtor(&stack);
return;
}
instr_t *last = NULL;
instr_t *current = NULL;
if(!instr_is_empty(&stack)) { //Handling instruction before
current = instr_top(&stack);
if(instr_before) {
instr_before->next = instr_pop(&stack);
current->prev = instr_before;
}
else {
dst->first_instr = instr_pop(&stack);
current->prev = NULL;
}
}
while(!instr_is_empty(&stack)) //Inserts instructions from stack (in reversed order)
{
current->next = instr_top(&stack);
last = current;
current = instr_pop(&stack);
current->prev = last;
}
if(current) { //Handling instruction after
if(instr_after) {
current->next = instr_after;
instr_after->prev = current;
}
else {
dst->last_instr = current;
current->next = NULL;
}
}
instr_stack_dtor(&stack);
}
void print_program(prog_t *source) {
instr_t *current_instr = source->first_instr;
while(current_instr) {
fprintf(stdout, "%s\n", to_str(¤t_instr->content));
current_instr = current_instr->next;
}
}
/*** End of functions for internal representation ***/
void generate_init(prog_t *dst){
app_instr(dst,".IFJcode21");
app_instr(dst,"CREATEFRAME");
//builtin
// generate_write_function(dst);
// generate_reads_function(dst);
// generate_readi_function(dst);
// generate_readn_function(dst);
// generate_tointeger_function(dst);
// generate_chr_function(dst);
// generate_ord_function(dst);
// generate_substr_function(dst);
//operation functions
generate_unaryminus_function(dst);
//custom builtin
generate_checkzero_function_float(dst);
generate_checkzero_function_int(dst);
generate_checknil_function_single(dst);
generate_checknil_function_double(dst);
generate_same_types(dst);
generate_force_floats(dst);
generate_force_ints(dst);
generate_tobool(dst);
generate_int2num(dst);
generate_operation_function_pow(dst);
generate_operation_function_mod(dst);
}
function_name_t *get_builtin_by_name(char *name) {
static function_name_t builtin[] = {
{"chr",generate_chr_function},
{"ord",generate_ord_function},
{"readi",generate_readi_function},
{"readn",generate_readn_function},
{"reads",generate_reads_function},
{"substr",generate_substr_function},
{"tointeger",generate_tointeger_function},
{"write",generate_write_function},
};
for (size_t i = 0; i < sizeof(builtin)/sizeof(function_name_t); i++)
{
function_name_t item = builtin[i];
if(str_cmp(item.name, name) == 0){
return &builtin[i];
}
}
return NULL;
}
int generate_builtin(prog_t *dst, symtab_t *symtab){
//symtab should be global
// bool is_builtin = check_builtin(get_attr(&id_fc, parser->scanner), &parser->sym.global);
for (size_t i = 0; i < BUILTIN_TABLE_SIZE; i++)
{
/* code */
sym_data_t *data = builtin_functions(i);
if(search(symtab, data->name.str) != NULL) {
//if used we generate it
function_name_t *func = get_builtin_by_name(data->name.str);
if(func == NULL){
return INTERNAL_ERROR;
}else{
func->function_ptr(dst);
}
}
}
return 0;
}
/**
* *---------FUNCTIONS---------
*/
void generate_start_function(prog_t *dst, const char * name){
app_instr(dst,"\n");
app_instr(dst,"JUMP $END_FUN$%s",name);
app_instr(dst,"#function %s ()",name);
app_instr(dst,"LABEL $FUN$%s", name);
app_instr(dst,"CREATEFRAME");
}
void generate_parameters( prog_t *dst, void *sym_stack,symtab_t *symtab , void *param_names, scanner_t * scanner){
tok_stack_t *params = param_names;
while (!tok_is_empty(params))
{
//get it from the token name
token_t name_token = tok_pop(params);
string_t name_unique = get_unique_name(sym_stack, symtab,&name_token,scanner);
generate_parameter(dst,name_unique.str);
}
}
void generate_parameter(prog_t *dst, const char * name){
app_instr(dst,"#define param %s",name);
app_instr(dst,"DEFVAR %s%s",VAR_FORMAT,name); //creates temporary variable
app_instr(dst,"POPS %s%s",VAR_FORMAT,name); //assigns one argument from stack to temporary variable
}
void generate_end_function(prog_t *dst, const char * name){
app_instr(dst,"\n");
app_instr(dst,"RETURN");
app_instr(dst,"LABEL $END_FUN$%s",name);
app_instr(dst,"");
}
void generate_call_function(prog_t *dst, const char *name){
app_instr(dst,"# %s()",name);
app_instr(dst,"PUSHFRAME");
app_instr(dst,"CALL $FUN$%s",name);
app_instr(dst,"POPFRAME");
}
void generate_additional_returns(prog_t *dst, size_t n){
for (size_t i = 0; i < n; i++)
{
app_instr(dst,"PUSHS nil@nil");
}
}
void generate_return(prog_t *dst){
app_instr(dst,"RETURN");
}
void generate_dump_values(prog_t *dst, size_t save_n, size_t delete_n){
app_instr(dst, "PUSHFRAME");
app_instr(dst, "CREATEFRAME");
for (size_t i = 0; i < save_n; i++) //Storing save_n values from top
{
app_instr(dst,"DEFVAR TF@TMP_STORAGE$%ld", i);
app_instr(dst, "POPS TF@TMP_STORAGE$%ld", i);
}
app_instr(dst, "DEFVAR TF@TMP_DUMP");
for (size_t i = 0; i < delete_n; i++) //Deleting delete_n values
{
app_instr(dst, "POPS TF@TMP_DUMP");
}
for (long int i = save_n - 1; i >= 0; i--) //Pushing values back in to achieve original order
{
app_instr(dst, "PUSHS TF@TMP_STORAGE$%ld", i);
}
app_instr(dst,"POPFRAME");
}
void generate_reverse_stack(prog_t *dst, size_t n_values) {
app_instr(dst, "PUSHFRAME");
app_instr(dst, "CREATEFRAME");
for (size_t i = 0; i < n_values; i++) //Storing save_n values from top
{
app_instr(dst,"DEFVAR TF@TMP_STORAGE$%ld", i);
app_instr(dst, "POPS TF@TMP_STORAGE$%ld", i);
}
for (long int i = 0; i < n_values; i++) //Pushing values back in reversed order
{
app_instr(dst, "PUSHS TF@TMP_STORAGE$%ld", i);
}
app_instr(dst,"POPFRAME");
}
/**
* *---------VARIABLES---------
*/
void generate_declare_variable(prog_t *dst, void *sym_stack,symtab_t *symtab , token_t *var_id, scanner_t * scanner){
string_t name = get_unique_name(sym_stack,symtab,var_id,scanner);
if(dst->cycle_nest_lvl == 0) {
//we are not in a while loop, we append it
app_instr(dst,"DEFVAR %s%s",VAR_FORMAT,name.str);
app_instr(dst,"MOVE %s%s nil@nil",VAR_FORMAT,name.str);
}
else{
//if we are in a while loop we insert it before the while loops start
ins_before(dst,instr_top(&dst->cycle_stack),"DEFVAR %s%s",VAR_FORMAT,name.str);
ins_before(dst,instr_top(&dst->cycle_stack),"MOVE %s%s nil@nil",VAR_FORMAT,name.str);
//will actually print them in reverse order because we are using ins_before
//BUT it will keep MOVE after DEFVAR
}
}
void generate_multiple_assignment(prog_t *dst, void *sym_stack, symtab_t *symtab , void *param_names, scanner_t * scanner){
tok_stack_t *params = param_names;
while (!tok_is_empty(params))
{
//get it from the token name
token_t name_token = tok_pop(params);
string_t name_unique = get_unique_name(sym_stack,symtab,&name_token,scanner);
generate_assign_value(dst,name_unique.str);
}
}
void generate_assign_value(prog_t *dst, const char * name){
app_instr(dst,"#assign value to %s",name);
app_instr(dst,"POPS %s%s",VAR_FORMAT,name); //assigns one argument from stack to temporary variable
}
void generate_value_push(prog_t *dst, sym_type_t type, sym_dtype_t dtype, const char * token){
if(type == VAR){
app_instr(dst,"PUSHS %s%s",VAR_FORMAT,token);
}else if(type == VAL){
if(dtype == STR){
string_t token_s;
str_init(&token_s);
to_ascii(token, &token_s);
app_instr(dst,"#here");
app_instr(dst,"PUSHS %s@%s",convert_type(dtype), token_s.str);
str_dtor(&token_s);
}else if(dtype == NUM){
char *rest = NULL;
double num = strtod(token, &rest);
if(errno == ERANGE) { //Prevent inf
if(GEN_WARNING) {
fprintf(stderr, "\t|\033[1;33m Warning: \033[0m");
fprintf(stderr, "Numeric literal '\033[1;33m%s\033[0m' is out of compilers range. It will be truncated to %e\n", token, DBL_MAX);
fprintf(stderr, "\t| Check your interpret if it supports as big numbers as this!\n");
}
num = DBL_MAX;
}
app_instr(dst,"PUSHS %s@%a",convert_type(dtype), num);
}else{
app_instr(dst,"PUSHS %s@%s",convert_type(dtype), token);
}
}else{
fprintf(stderr,"Code generation: Error not supported yet\n");
}
}
/**
* *--------CONDIONS--------
*/
void generate_if_condition(prog_t *dst, size_t n){
app_instr(dst,"#if %i",n);
//convert other types to bool
generate_call_function(dst,"$BUILTIN$tobool");
app_instr(dst,"PUSHS bool@true");
app_instr(dst,"JUMPIFNEQS $ELSE$START$%i",n);
}
void generate_if_end(prog_t *dst, size_t n){
app_instr(dst,"#end of if %i",n);
app_instr(dst,"JUMP $ELSE$END$%i",n);
app_instr(dst,"LABEL $ELSE$START$%i",n);
}
void generate_else_end(prog_t *dst, size_t n){
app_instr(dst,"#end of else and the whole if %i statement",n);
app_instr(dst,"LABEL $ELSE$END$%i",n);
}
/**
* *LOOPS
*/
void generate_while_condition_beginning(prog_t *dst, size_t n){
app_instr(dst,"#while %i, nest level %i",n,dst->cycle_nest_lvl);
app_instr(dst,"LABEL $WHILE$COND$%i",n);
if(dst->cycle_nest_lvl == 0){
instr_push(&dst->cycle_stack, get_last(dst));
}
dst->cycle_nest_lvl++;
}
void generate_while_condition_evaluate(prog_t *dst, size_t n){
//convert other types to bool
generate_call_function(dst,"$BUILTIN$tobool");
app_instr(dst,"PUSHS bool@true");
app_instr(dst,"JUMPIFNEQS $WHILE$END$%i",n);
}
void generate_while_end(prog_t *dst, size_t n){
app_instr(dst,"#end of while %i loop",n);
app_instr(dst,"JUMP $WHILE$COND$%i",n);
app_instr(dst,"LABEL $WHILE$END$%i",n);
dst->cycle_nest_lvl--;
if(!instr_is_empty(&dst->cycle_stack)) {
instr_pop(&dst->cycle_stack);
}
}
/**
* *---------OPERATIONS---------
*/
void generate_operation_add(prog_t *dst){
generate_call_function(dst,"$OP$checknil_double");
generate_call_function(dst,"$BUILTIN$sametypes");
app_instr(dst,"ADDS");
}
void generate_operation_sub(prog_t *dst){
generate_call_function(dst,"$OP$checknil_double");
generate_call_function(dst,"$BUILTIN$sametypes");
app_instr(dst,"SUBS");
}
void generate_operation_mul(prog_t *dst){
generate_call_function(dst,"$OP$checknil_double");
generate_call_function(dst,"$BUILTIN$sametypes");
app_instr(dst,"MULS");
}
void generate_operation_div(prog_t *dst){
generate_call_function(dst,"$OP$checknil_double");
generate_call_function(dst,"$BUILTIN$forcefloats");
generate_call_function(dst,"$OP$checkzero_float");
app_instr(dst,"DIVS");
}
void generate_operation_idiv(prog_t *dst){
generate_call_function(dst,"$OP$checknil_double");
generate_call_function(dst,"$OP$checkzero_int");
app_instr(dst,"IDIVS");
}
void generate_operation_unary_minus(prog_t *dst){
generate_call_function(dst,"$OP$checknil_single");
generate_call_function(dst,"$OP$unaryminus");
}
void generate_operation_eq(prog_t *dst){
generate_call_function(dst,"$BUILTIN$sametypes");
app_instr(dst,"# start operator A==B");
app_instr(dst,"EQS");
app_instr(dst,"# end operator A==B");
}
void generate_operation_neq(prog_t *dst){
generate_call_function(dst,"$BUILTIN$sametypes");
app_instr(dst,"# start operator A~=B");
app_instr(dst,"EQS");
app_instr(dst,"NOTS");
app_instr(dst,"# end operator A~=B");
}
void generate_operation_gt(prog_t *dst){
generate_call_function(dst,"$OP$checknil_double");
generate_call_function(dst,"$BUILTIN$sametypes");
app_instr(dst,"# start operator A>B");
app_instr(dst,"GTS");
app_instr(dst,"# end operator A>B");
}
void generate_operation_lt(prog_t *dst){
generate_call_function(dst,"$OP$checknil_double");
generate_call_function(dst,"$BUILTIN$sametypes");
app_instr(dst,"# start operator A<B");
app_instr(dst,"LTS");
app_instr(dst,"# end operator A<B");
}
void generate_operation_gte(prog_t *dst){
generate_call_function(dst,"$OP$checknil_double");
generate_call_function(dst,"$BUILTIN$sametypes");
app_instr(dst,"# start operator A>=B");
app_instr(dst,"PUSHFRAME");
app_instr(dst,"CREATEFRAME");
//define temp operands A & B
app_instr(dst,"DEFVAR TF@!TMP!A");
app_instr(dst,"DEFVAR TF@!TMP!B");
app_instr(dst,"POPS TF@!TMP!B");
app_instr(dst,"POPS TF@!TMP!A");
//make the stack to this form ->[B,A,B,A]
app_instr(dst,"PUSHS TF@!TMP!A");
app_instr(dst,"PUSHS TF@!TMP!B");
app_instr(dst,"PUSHS TF@!TMP!A");
app_instr(dst,"PUSHS TF@!TMP!B");
//evaluate
app_instr(dst,"DEFVAR TF@!TMP!LTRES");
app_instr(dst,"GTS");
app_instr(dst,"POPS TF@!TMP!LTRES");
app_instr(dst,"EQS");
app_instr(dst,"PUSHS TF@!TMP!LTRES");
app_instr(dst,"ORS");
//the result will be on top of the stack
app_instr(dst,"POPFRAME");
app_instr(dst,"# end operator A>=B");
}
void generate_operation_lte(prog_t *dst){
generate_call_function(dst,"$OP$checknil_double");
generate_call_function(dst,"$BUILTIN$sametypes");
app_instr(dst,"# start operator A<=B");
app_instr(dst,"PUSHFRAME");
app_instr(dst,"CREATEFRAME");
//define temp operands A & B
app_instr(dst,"DEFVAR TF@!TMP!A");
app_instr(dst,"DEFVAR TF@!TMP!B");
app_instr(dst,"POPS TF@!TMP!B");
app_instr(dst,"POPS TF@!TMP!A");
app_instr(dst,"DEFVAR TF@!TYPE!A");
app_instr(dst,"TYPE TF@!TYPE!A TF@!TMP!A");
//make the stack to this form ->[B,A,B,A]
app_instr(dst,"PUSHS TF@!TMP!A");
app_instr(dst,"PUSHS TF@!TMP!B");
app_instr(dst,"PUSHS TF@!TMP!A");
app_instr(dst,"PUSHS TF@!TMP!B");
//evaluate
app_instr(dst,"DEFVAR TF@!TMP!LTRES");
app_instr(dst,"LTS");
app_instr(dst,"POPS TF@!TMP!LTRES");
app_instr(dst,"EQS");
app_instr(dst,"PUSHS TF@!TMP!LTRES");
app_instr(dst,"ORS");
//the result will be on top of the stack
app_instr(dst,"POPFRAME");
app_instr(dst,"# end operator A<=B");
}
void generate_operation_strlen(prog_t *dst){
app_instr(dst,"# start operator #A");
app_instr(dst,"PUSHFRAME");
app_instr(dst,"CREATEFRAME");
generate_call_function(dst,"$OP$checknil_single");
//define temp operands A
app_instr(dst,"DEFVAR TF@!TMP!A");
app_instr(dst,"DEFVAR TF@!TMP!RESULT");
app_instr(dst,"POPS TF@!TMP!A");
app_instr(dst,"STRLEN TF@!TMP!RESULT TF@!TMP!A");
app_instr(dst,"PUSHS TF@!TMP!RESULT");
app_instr(dst,"POPFRAME");
app_instr(dst,"# end operator #A");
}
void generate_operation_concat(prog_t *dst){
app_instr(dst,"# start operator A..B");
app_instr(dst,"PUSHFRAME");
app_instr(dst,"CREATEFRAME");
generate_call_function(dst,"$OP$checknil_double");
//define temp operands A & B
app_instr(dst,"DEFVAR TF@!TMP!A");
app_instr(dst,"DEFVAR TF@!TMP!B");
app_instr(dst,"DEFVAR TF@!TMP!RESULT");
app_instr(dst,"POPS TF@!TMP!B");
app_instr(dst,"POPS TF@!TMP!A");
app_instr(dst,"CONCAT TF@!TMP!RESULT TF@!TMP!A TF@!TMP!B");
app_instr(dst,"PUSHS TF@!TMP!RESULT");
app_instr(dst,"POPFRAME");
app_instr(dst,"# end operator A..B");
}
void generate_operation_pow(prog_t *dst){
app_instr(dst,"# start operator A^B");
generate_call_function(dst,"$BUILTIN_POW$");
}
void generate_operation_function_pow(prog_t *dst){
generate_start_function(dst,"$BUILTIN_POW$");
generate_call_function(dst,"$OP$checknil_double");
generate_parameter(dst, "exponent");
generate_parameter(dst, "base");
app_instr(dst,"DEFVAR %sbase_type",VAR_FORMAT);
app_instr(dst,"TYPE %sbase_type %sbase",VAR_FORMAT,VAR_FORMAT);
//check if base is int => tofloat
app_instr(dst,"JUMPIFEQ $POW_SKIP_CONVERT$ string@float %sbase_type",VAR_FORMAT);
app_instr(dst,"INT2FLOAT %sbase %sbase",VAR_FORMAT,VAR_FORMAT);
app_instr(dst,"LABEL $POW_SKIP_CONVERT$");
//check if exponent == 0
app_instr(dst,"JUMPIFEQ $POW_ZERO$ int@0 %sexponent",VAR_FORMAT);
//check if exponent < 0
app_instr(dst,"PUSHS %sexponent",VAR_FORMAT);
app_instr(dst,"PUSHS int@0");
generate_operation_lt(dst);
app_instr(dst,"PUSHS bool@true");
app_instr(dst,"JUMPIFNEQS $POW_FLIP_SKIP$");
app_instr(dst,"MUL %sexponent int@-1 %sexponent",VAR_FORMAT,VAR_FORMAT);
app_instr(dst,"DIV %sbase float@%a %sbase",VAR_FORMAT,1.0f,VAR_FORMAT);
app_instr(dst,"LABEL $POW_FLIP_SKIP$");
app_instr(dst,"PUSHS %sbase",VAR_FORMAT);
//cycle
app_instr(dst,"LABEL $POW_START_CYCLE$");
app_instr(dst,"JUMPIFEQ $POW_END_CYCLE$ int@1 %sexponent",VAR_FORMAT);
app_instr(dst,"PUSHS %sbase",VAR_FORMAT);
app_instr(dst,"MULS");
app_instr(dst,"SUB %sexponent %sexponent int@1",VAR_FORMAT,VAR_FORMAT);
app_instr(dst,"JUMP $POW_START_CYCLE$");
app_instr(dst,"LABEL $POW_END_CYCLE$");
app_instr(dst,"JUMP $POW_END$");
app_instr(dst,"LABEL $POW_ZERO$",VAR_FORMAT);
app_instr(dst,"PUSHS float@%a",1.0f);
app_instr(dst,"LABEL $POW_END$");
generate_end_function(dst,"$BUILTIN_POW$");
}
void generate_operation_mod(prog_t *dst){
app_instr(dst,"# start operator AmodB");
generate_call_function(dst,"$BUILTIN_MOD$");
}
void generate_operation_function_mod(prog_t *dst){
generate_start_function(dst,"$BUILTIN_MOD$");
generate_call_function(dst,"$BUILTIN$forcefloats");
generate_call_function(dst,"$OP$checkzero_float");
generate_parameter(dst,"B");
generate_parameter(dst,"A");
app_instr(dst,"DEFVAR %snegative",VAR_FORMAT);
app_instr(dst,"MOVE %snegative bool@false",VAR_FORMAT);
app_instr(dst,"DEFVAR %sA_TYPE",VAR_FORMAT);
app_instr(dst,"DEFVAR %sB_TYPE",VAR_FORMAT);
//check for type of A
app_instr(dst,"TYPE %sA_TYPE %sA",VAR_FORMAT,VAR_FORMAT);
//check for type of B
app_instr(dst,"TYPE %sB_TYPE %sB",VAR_FORMAT,VAR_FORMAT);
;
//if A < 0.0f => A*-1.0f
app_instr(dst,"PUSHS %sA",VAR_FORMAT);
app_instr(dst,"PUSHS float@%a",0.0f);
generate_operation_lt(dst);
app_instr(dst,"PUSHS bool@true");
app_instr(dst,"JUMPIFNEQS $SKIP_A_FLOAT$");
app_instr(dst,"MUL %sA %sA float@%a",VAR_FORMAT,VAR_FORMAT,-1.0f);
app_instr(dst,"LABEL $SKIP_A_FLOAT$");
//if B < 0.0f => B*-1.0f
app_instr(dst,"PUSHS %sB",VAR_FORMAT);
app_instr(dst,"PUSHS float@%a",0.0f);
generate_operation_lt(dst);
app_instr(dst,"PUSHS bool@true");
app_instr(dst,"JUMPIFNEQS $SKIP_B_FLOAT$");
app_instr(dst,"MOVE %snegative bool@true",VAR_FORMAT);
app_instr(dst,"MUL %sB %sB float@%a",VAR_FORMAT,VAR_FORMAT,-1.0f);
app_instr(dst,"LABEL $SKIP_B_FLOAT$");
//cycle
app_instr(dst,"LABEL $MOD_CYCLE$");
app_instr(dst,"PUSHS %sA",VAR_FORMAT);
app_instr(dst,"PUSHS %sB",VAR_FORMAT);
generate_operation_lt(dst);
app_instr(dst,"PUSHS bool@true");
app_instr(dst,"JUMPIFEQS $MOD_CYCLE_END$");
app_instr(dst,"SUB %sA %sA %sB",VAR_FORMAT,VAR_FORMAT,VAR_FORMAT);
app_instr(dst,"JUMP $MOD_CYCLE$");
app_instr(dst,"LABEL $MOD_CYCLE_END$");
app_instr(dst,"PUSHS %sA",VAR_FORMAT);
//if negative == true
app_instr(dst,"JUMPIFNEQ $SKIP_NEGATIVE$ bool@true %snegative",VAR_FORMAT);
//MULS -1*(stack top)
app_instr(dst,"PUSHS float@%a",-1.0f);
app_instr(dst,"MULS");
app_instr(dst,"LABEL $SKIP_NEGATIVE$");
generate_end_function(dst,"$BUILTIN_MOD$");
}
/**
* *---------BUILTIN---------
*/
void generate_write_function(prog_t *dst){
generate_start_function(dst,"write"); //function write()
generate_parameter(dst,"str");
app_instr(dst,"PUSHS %s%s",VAR_FORMAT,"str");
app_instr(dst,"PUSHS nil@nil");
app_instr(dst,"JUMPIFNEQS $CHECKNIL_WRITE$");
app_instr(dst,"WRITE string@nil");
app_instr(dst,"JUMP $WRITESKIP$");
app_instr(dst,"LABEL $CHECKNIL_WRITE$");
app_instr(dst,"WRITE %s%s",VAR_FORMAT,"str"); //writes it
app_instr(dst,"LABEL $WRITESKIP$");
generate_end_function(dst,"write"); //end
}
void generate_reads_function(prog_t *dst){
generate_start_function(dst,"reads");
app_instr(dst,"DEFVAR TF@$TEMP$");
app_instr(dst,"READ TF@$TEMP$ string");
app_instr(dst,"PUSHS TF@$TEMP$");
generate_end_function(dst,"reads");
}
void generate_readi_function(prog_t *dst){
generate_start_function(dst,"readi");
app_instr(dst,"DEFVAR TF@$TEMP$");
app_instr(dst,"READ TF@$TEMP$ int");
app_instr(dst,"PUSHS TF@$TEMP$");
generate_end_function(dst,"readi");
}
void generate_readn_function(prog_t *dst){
generate_start_function(dst,"readn");
app_instr(dst,"DEFVAR TF@$TEMP$");