-
Notifications
You must be signed in to change notification settings - Fork 2
/
ycf_yield_fun.c
1833 lines (1754 loc) · 89.5 KB
/
ycf_yield_fun.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
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB and Kjell Winblad 2019-2021. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
/*
* Author: Kjell Winblad
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ycf_utils.h"
#include "ycf_yield_fun.h"
#include "ycf_node.h"
#include "ycf_symbol.h"
#include "ycf_string.h"
#include "ycf_printers.h"
#include "ycf_parser.h"
static int ycf_yield_location_id_counter = 0;
static
ycf_node* mk_typedef_struct_node(ycf_node_list definitions, char* name){
ycf_node* res = ycf_malloc(sizeof(ycf_node));
res->next = NULL;
res->type = ycf_node_type_gen_typedef_struct;
res->u.gen_typedef_struct.definition_nodes = definitions;
res->u.gen_typedef_struct.name = name;
return res;
}
static
ycf_node_list mk_ycf_trap_state_params(){
return ycf_node_definition_list_from_string(ycf_string_new(
"long* ycf_nr_of_reductions_param;\n"
"void** ycf_trap_state;\n"
"void* ycf_extra_context;\n"));
}
static
ycf_node_list mk_saved_ycf_trap_state_params(){
return ycf_node_definition_list_from_string(
"ycf_yield_alloc_type ycf_yield_alloc;\n"
"ycf_yield_free_type ycf_yield_free;\n"
"void* ycf_yield_alloc_free_context;\n"
"size_t ycf_stack_alloc_size_or_max_size;\n"
"void* ycf_stack_alloc_data;\n");
}
static
ycf_node_list mk_trap_extra_state(){
return ycf_node_definition_list_from_string(
"int ycf_trap_location;\n"
"long ycf_nr_of_reductions;\n"
"struct ycf_alloc_data ycf_frame_alloc_data;\n");;
}
static
char* get_ycf_trap_state_assignments(ycf_node_list ycf_trap_state_defines){
ycf_node* current = ycf_trap_state_defines.head;
char* str = "";
while(current != NULL){
char* ident = ycf_symbol_get_text(current->u.definition.identifier);
if(current->u.definition.array_brackets.head != NULL && !current->u.definition.array_brackets.head->u.array_bracket.empty){
ycf_string_printable_buffer* array_size_exp = ycf_string_printable_buffer_new();
ycf_node* bracket = current->u.definition.array_brackets.head;
while(bracket != NULL){
print_node_code_expression(bracket->u.array_bracket.content,array_size_exp);
ycf_string_printable_buffer_printf(array_size_exp, " * ");
bracket = bracket->next;
}
ycf_string_printable_buffer_printf(array_size_exp, " sizeof(");
print_symbol_list(¤t->u.definition.type_specifiers, array_size_exp);
ycf_string_printable_buffer_printf(array_size_exp, ")");
str = ycf_string_new("%s"
" memcpy(ycf_my_trap_state->%s, %s, %s);\n",
str,
ident,
ident,
array_size_exp->buffer);
}else{
str = ycf_string_new("%s"
" ycf_my_trap_state->%s = %s;\n",
str,
ident,
ident);
}
current = current->next;
}
return str;
}
static
char* mk_code_from_special_code_list(ycf_node_list special_code_list){
ycf_string_printable_buffer* b = ycf_string_printable_buffer_new();
ycf_node* current = special_code_list.head;
ycf_string_printable_buffer_printf(b, "\n/* YCF SPECIAL CUSTOM CODE START */\n");
while(current != NULL) {
ycf_node_print(current->u.special_code_block.code.if_statement, b);
ycf_string_printable_buffer_printf(b, "\n");
current = current->next;
}
ycf_string_printable_buffer_printf(b, "\n/* YCF SPECIAL CUSTOM CODE END */\n");
return b->buffer;
}
static
ycf_node* mk_print_offsets_for_struct_fields(char* ycf_trap_state_name,
ycf_node_list ycf_trap_state_defines) {
ycf_string_printable_buffer* buffer = ycf_string_printable_buffer_new();
ycf_node* iterator = ycf_trap_state_defines.head;
while (iterator != NULL) {
char * fieldName = ycf_symbol_get_text(iterator->u.definition.identifier);
ycf_string_printable_buffer_printf(buffer,
"fprintf(stderr, \" %s: %%zu\\n\","
" (size_t)&((struct %s*)0)->%s );\n",
fieldName,
ycf_trap_state_name,
fieldName);
iterator = iterator->next;
}
char* ret =
ycf_string_new(("static void %s_print_offsets_for_struct_fields(void);\n"
"static void %s_print_offsets_for_struct_fields() {\n"
"fprintf(stderr, \"Field offsets for struct %s:\\n\");\n"
"%s\n"
"}\n"),
ycf_trap_state_name,
ycf_trap_state_name,
ycf_trap_state_name,
buffer->buffer);
return ycf_node_new_text_node(ret);
}
static
ycf_node* mk_yield_code(ycf_node* f_node,
char* ycf_trap_state_name,
ycf_node_list ycf_trap_state_defines,
ycf_node_list on_save_yield_state_code_list,
bool debug){
char* ret_code;
if(ycf_node_is_void_ret_fun(f_node)) {
ret_code = "return;\n";
} else {
ycf_symbol_list ret_type = ycf_node_get_return_type(f_node);
ret_code =
ycf_string_new(" {\n"
" static %s const ycf_unused_ret_value;\n"
" return ycf_unused_ret_value;\n"
" }\n",
ycf_symbol_list_to_str(&ret_type));
}
char *debug_check_for_stack_ptrs = "";
char *debug_zero_my_trap_state = "";
if(debug){
debug_check_for_stack_ptrs =
ycf_string_new("ycf_debug_check_block(\"%s\",\n"
" ycf_find_stack_bottom_conservative(),\n"
" ycf_trap_state,\n"
" ycf_my_trap_state,\n"
" sizeof(struct %s),\n"
" %s_print_offsets_for_struct_fields);\n",
ycf_trap_state_name,
ycf_trap_state_name,
ycf_trap_state_name);
debug_zero_my_trap_state =
ycf_string_new(" for (int i = 0; i < sizeof(struct %s); i++) {\n"
" ((char*)ycf_my_trap_state)[i] = 0;\n"
" }\n", ycf_trap_state_name);
}
char* code = ycf_string_new("\n"
"{\n"
" struct %s* ycf_my_trap_state;\n"
" ycf_do_yield_label_%s:;\n"
" %s"
" if (*ycf_trap_state == NULL) {\n"
" ycf_my_trap_state = ycf_yield_alloc(sizeof(struct %s), ycf_yield_alloc_free_context);\n"
" %s"
" } else {\n"
" ycf_my_trap_state = *ycf_trap_state;\n"
" }\n"
" %s\n"
" *ycf_nr_of_reductions_param = ycf_nr_of_reductions;\n"
" *ycf_trap_state = ycf_my_trap_state;\n"
" %s"
" %s\n"
"}\n"
"\n",
ycf_trap_state_name,
ycf_symbol_get_text(f_node->u.function.definition.definition.identifier),
mk_code_from_special_code_list(on_save_yield_state_code_list),
ycf_trap_state_name,
debug_zero_my_trap_state,
get_ycf_trap_state_assignments(ycf_trap_state_defines),
debug_check_for_stack_ptrs,
ret_code
);
return ycf_node_get_from_code_scope_text(code);
}
ycf_node* mk_goto_yield_code(ycf_node* f_node,
char* ycf_trap_state_name,
ycf_node_list ycf_trap_state_defines){
ycf_yield_location_id_counter++;
char* code = ycf_string_new("\n"
"{\n"
" ycf_nr_of_reductions = 0;\n"
" ycf_trap_location = %d;\n"
" goto ycf_do_yield_label_%s;\n"
" ycf_yield_location_label_%d:;\n"
"}\n",
ycf_yield_location_id_counter,
ycf_symbol_get_text(f_node->u.function.definition.definition.identifier),
ycf_yield_location_id_counter
);
return ycf_node_get_from_code_scope_text(code);
}
static
ycf_node* mk_goto_yield_no_reds_code(ycf_node* f_node,
char* ycf_trap_state_name,
ycf_node_list ycf_trap_state_defines){
ycf_yield_location_id_counter++;
char* code = ycf_string_new("\n"
"{\n"
" ycf_trap_location = %d;\n"
" goto ycf_do_yield_label_%s;\n"
" ycf_yield_location_label_%d:;\n"
"}\n",
ycf_yield_location_id_counter,
ycf_symbol_get_text(f_node->u.function.definition.definition.identifier),
ycf_yield_location_id_counter
);
return ycf_node_get_from_code_scope_text(code);
}
static
ycf_node* mk_yield_fun_call_state_var(ycf_node* f){
ycf_node_list code = ycf_node_list_empty();
char* fun_name = NULL;
if(f->u.statement.expression->type == ycf_node_type_function_call){
fun_name = ycf_symbol_get_text(f->u.statement.expression->u.function_call.identifier);
}else{
fun_name = ycf_symbol_get_text(f->u.statement.expression->u.function_call_assignment.fun_call.identifier);
}
ycf_node * scope_with_dec =
ycf_node_get_from_code_scope_text(ycf_string_new("void* ycf_sub_fun_trap_state_wb = NULL;\n"
"/*special_code_start:ON_DESTROY_STATE*/\n"
"if(0){\n"
" if(ycf_sub_fun_trap_state_wb != NULL)\n{"
" %s_ycf_gen_destroy(ycf_sub_fun_trap_state_wb);\n"
" }\n"
"}\n"
"/*special_code_end*/\n", fun_name));
ycf_node_list_append(&code, ycf_node_shallow_copy(f));
ycf_node_list_append(&code, scope_with_dec->u.code_scope.other_nodes.head);
ycf_node* ret = ycf_node_scope_new(ycf_symbol_new_open_curly_brace(),
scope_with_dec->u.code_scope.definition_nodes,
code,
ycf_symbol_new_end_curly_brace());
return ret;
}
static
ycf_node* mk_yield_fun_call(ycf_node* c_file_node,
ycf_node* f,
char* ycf_trap_state_var_name,
char* calling_fun_name,
bool auto_yield){
ycf_yield_location_id_counter++;
ycf_node_function_call f_node;
char* assignment_code = "";
char* tmp_assignment_var_name = "ycf_tmp_fun_call_tmp_var";
char* tmp_assignment_var_declaration = "";
char* parameters = "";
char* finalize_call_code = "";
if(f->u.statement.expression->type == ycf_node_type_function_call){
f_node = f->u.statement.expression->u.function_call;
}else{
ycf_symbol_list called_fun_ret_type;
ycf_string_printable_buffer* assign_to_b = ycf_string_printable_buffer_new();
f_node = f->u.statement.expression->u.function_call_assignment.fun_call;
called_fun_ret_type =
ycf_node_find_function_return_type(c_file_node,
ycf_symbol_get_text(f_node.identifier));
tmp_assignment_var_declaration =
ycf_string_new("%s %s;\n",
ycf_symbol_list_to_str(&called_fun_ret_type),
tmp_assignment_var_name);
print_node_code_expression(f->u.statement.expression->u.function_call_assignment.left_side, assign_to_b);
assignment_code = ycf_string_new("%s = ", tmp_assignment_var_name);
finalize_call_code = ycf_string_new("%s = %s;\n",
assign_to_b->buffer,
tmp_assignment_var_name);
}
if (f_node.parameter_expressions.head != NULL){
ycf_string_printable_buffer* b = ycf_string_printable_buffer_new();
print_node_list_code(f_node.parameter_expressions.head, b);
parameters = ycf_string_new(",%s", b->buffer);
}
char* code = ycf_string_new("%s"
"%s"
"%s %s%s_ycf_gen_yielding(&ycf_nr_of_reductions,\n"
" &%s,ycf_extra_context,\n"
" ycf_yield_alloc,ycf_yield_free,\n"
" ycf_yield_alloc_free_context,\n"
" YCF_ALLOC_NEXT_MAX_SIZE(),\n"
" YCF_ALLOC_NEXT_BLOCK()\n"
" %s);\n"
"while(YCF_IS_YIELDED(%s)){\n"
" ycf_trap_location = %d;\n"
" goto ycf_do_yield_label_%s;\n"
" ycf_yield_location_label_%d:;\n"
" %s %s%s_ycf_gen_continue(&ycf_nr_of_reductions,\n"
" &%s,\n"
" ycf_extra_context);\n"
"}\n"
"%s\n",
tmp_assignment_var_declaration,
auto_yield ? "YCF_CONSUME_REDS(1);" : "",
assignment_code,
ycf_symbol_list_to_str(&f_node.neg_symbols),
ycf_symbol_get_text(f_node.identifier),
ycf_trap_state_var_name,
parameters,
ycf_trap_state_var_name,
ycf_yield_location_id_counter,
calling_fun_name,
ycf_yield_location_id_counter,
assignment_code,
ycf_symbol_list_to_str(&f_node.neg_symbols),
ycf_symbol_get_text(f_node.identifier),
ycf_trap_state_var_name,
finalize_call_code
);
return ycf_node_get_from_code_scope_text(code);
}
static
char* get_trap_restore_assignments(ycf_node_list ycf_trap_state_defines){
ycf_node* current = ycf_trap_state_defines.head;
char* str = "\n";
while(current != NULL){
char* ident = ycf_symbol_get_text(current->u.definition.identifier);
if(current->u.definition.array_brackets.head != NULL && !current->u.definition.array_brackets.head->u.array_bracket.empty){
ycf_string_printable_buffer* array_size_exp = ycf_string_printable_buffer_new();
ycf_node* bracket = current->u.definition.array_brackets.head;
while(bracket != NULL){
print_node_code_expression(bracket->u.array_bracket.content,array_size_exp);
ycf_string_printable_buffer_printf(array_size_exp, " * ");
bracket = bracket->next;
}
ycf_string_printable_buffer_printf(array_size_exp, " sizeof(");
print_symbol_list(¤t->u.definition.type_specifiers, array_size_exp);
ycf_string_printable_buffer_printf(array_size_exp, ")");
str = ycf_string_new("%s"
" memcpy(%s, ycf_my_trap_state->%s, %s);\n",
str,
ident,
ident,
array_size_exp->buffer);
}else{
str = ycf_string_new("%s"
" %s = ycf_my_trap_state->%s;\n",
str,
ident,
ident);
}
current = current->next;
}
return str;
}
static
char* get_debug_trap_zero_assignments(ycf_node_list ycf_trap_state_defines){
ycf_node* current = ycf_trap_state_defines.head;
char* str = "\n";
while(current != NULL){
char* ident = ycf_symbol_get_text(current->u.definition.identifier);
if(current->u.definition.array_brackets.head != NULL &&
!current->u.definition.array_brackets.head->u.array_bracket.empty){
ycf_string_printable_buffer* array_size_exp = ycf_string_printable_buffer_new();
ycf_node* bracket = current->u.definition.array_brackets.head;
while(bracket != NULL){
print_node_code_expression(bracket->u.array_bracket.content,array_size_exp);
ycf_string_printable_buffer_printf(array_size_exp, " * ");
bracket = bracket->next;
}
ycf_string_printable_buffer_printf(array_size_exp, " sizeof(");
print_symbol_list(¤t->u.definition.type_specifiers, array_size_exp);
ycf_string_printable_buffer_printf(array_size_exp, ")");
str = ycf_string_new("%s"
" memset((char*)(void*)%s, 0, %s);\n",
str,
ident,
array_size_exp->buffer);
}else{
ycf_string_printable_buffer* type = ycf_string_printable_buffer_new();
print_symbol_list(¤t->u.definition.type_specifiers, type);
str = ycf_string_new("%s"
" memset((char*)(void*)&%s, 0, sizeof(%s));\n",
str,
ident,
type->buffer);
}
current = current->next;
}
return str;
}
static
char* get_goto_ycf_trap_location_switch(int nr_of_ycf_trap_locations){
int i;
char* str = "switch(ycf_trap_location){\n";
for(i = 1; i <= nr_of_ycf_trap_locations; i++){
str = ycf_string_new("%s"
"case %d: goto ycf_yield_location_label_%d;\n", str, i, i);
}
str = ycf_string_new("%s\n}", str, i, i);
return str;
}
typedef struct {
ycf_node* f_node;
char* ycf_trap_state_name;
ycf_node_list ycf_trap_state_defines;
} yield_code_replacer_context;
ycf_node* yield_code_replacer(ycf_node* candidate, ycf_node_code_scope* s,void* context){
yield_code_replacer_context* c = context;
(void)s;
if(candidate->type == ycf_node_type_statement &&
candidate->u.statement.expression != NULL &&
candidate->u.statement.expression->type == ycf_node_type_function_call &&
ycf_symbol_is_text_eq(candidate->u.statement.expression->u.function_call.identifier,
"YCF_YIELD") &&
candidate->u.statement.expression->u.function_call.parameter_expressions.head == NULL){
ycf_node* yield_code = mk_goto_yield_code(c->f_node, c->ycf_trap_state_name, c->ycf_trap_state_defines);
return yield_code;
} else {
return candidate;
}
}
static
void insert_yield_code(ycf_node* f_node, char* ycf_trap_state_name, ycf_node_list ycf_trap_state_defines, ycf_node_code_scope* s){
ycf_node_search_and_replace_statements_in_scope(s,
yield_code_replacer,
&(yield_code_replacer_context) {f_node, ycf_trap_state_name,
ycf_trap_state_defines});
}
static
ycf_node* special_code_section_replacer(ycf_node* candidate, ycf_node_code_scope* s,void* context){
(void)context;
(void)s;
if(candidate->type == ycf_node_type_statement &&
candidate->u.statement.expression != NULL &&
candidate->u.statement.expression->type == ycf_node_type_function_call) {
ycf_node_function_call fun_call = candidate->u.statement.expression->u.function_call;
ycf_symbol* fun_name = fun_call.identifier;
if(ycf_symbol_is_text_eq(fun_name, "YCF_SPECIAL_CODE_START") &&
ycf_node_list_length(fun_call.parameter_expressions) == 1) {
char* parameter = ycf_node_to_string(fun_call.parameter_expressions.head);
ycf_node* special_code_start =
ycf_node_new_text_node(ycf_string_new("/*special_code_start:%s*/\n"
"if(0){\n",
parameter));
return special_code_start;
} else if (ycf_symbol_is_text_eq(candidate->u.statement.expression->u.function_call.identifier,
"YCF_SPECIAL_CODE_END") &&
ycf_node_list_length(fun_call.parameter_expressions) == 0) {
return ycf_node_new_text_node("}/*special_code_end*/\n");
}
}
return candidate;
}
static
ycf_node* replace_alt_syntax_special_code_section_code(ycf_node* f_node){
ycf_node_search_and_replace_statements_in_scope(&f_node->u.function.body,
special_code_section_replacer,
NULL);
return ycf_node_get_function_from_text(ycf_node_to_string(f_node));
}
static
ycf_node* yield_no_reds_code_replacer(ycf_node* candidate, ycf_node_code_scope* s,void* context){
yield_code_replacer_context* c = context;
(void)s;
if(candidate->type == ycf_node_type_statement &&
candidate->u.statement.expression != NULL &&
candidate->u.statement.expression->type == ycf_node_type_function_call &&
ycf_symbol_is_text_eq(candidate->u.statement.expression->u.function_call.identifier,
"YCF_YIELD_NO_REDS") &&
candidate->u.statement.expression->u.function_call.parameter_expressions.head == NULL){
ycf_node* yield_code = mk_goto_yield_no_reds_code(c->f_node, c->ycf_trap_state_name, c->ycf_trap_state_defines);
return yield_code;
} else {
return candidate;
}
}
static
void insert_yield_no_reds_code(ycf_node* f_node, char* ycf_trap_state_name, ycf_node_list ycf_trap_state_defines, ycf_node_code_scope* s){
ycf_node_search_and_replace_statements_in_scope(s,
yield_no_reds_code_replacer,
&(yield_code_replacer_context){f_node, ycf_trap_state_name,
ycf_trap_state_defines});
}
typedef struct {
ycf_node_list on_return_code_list;
ycf_node_list on_return_or_destroy_code_list;
bool auto_yield;
} replace_return_ctx;
static
ycf_node* save_nr_of_reductions_before_return_replacer(ycf_node* candidate,
ycf_node_code_scope* s,
void* context){
replace_return_ctx* ctx = context;
(void)s;
char* consume_reds_code = ctx->auto_yield ? "YCF_CONSUME_REDS(1);\n" : "";
if(candidate->type == ycf_node_type_return_statement){
char* code =
ycf_string_new("\n"
"%s"
"if (*ycf_trap_state != NULL) {\n"
" ycf_yield_free(*ycf_trap_state, ycf_yield_alloc_free_context);\n"
" *ycf_trap_state = NULL;\n"
"}\n"
"ycf_destroy_stack_allocator(&ycf_frame_alloc_data,\n"
" ycf_yield_free,\n"
" ycf_yield_alloc_free_context);\n"
"*ycf_nr_of_reductions_param = ycf_nr_of_reductions;"
"%s"
"%s"
"%s",
consume_reds_code,
mk_code_from_special_code_list(
ctx->on_return_or_destroy_code_list),
mk_code_from_special_code_list(ctx->on_return_code_list),
ycf_node_to_string(candidate));
return ycf_node_get_from_code_scope_text(code);
} else {
return candidate;
}
}
static
void save_nr_of_reductions_before_return(ycf_node_code_scope* s,
ycf_node_list on_return_code_list,
ycf_node_list on_return_or_destroy_code_list,
bool auto_yield){
replace_return_ctx ctx;
ctx.on_return_code_list = on_return_code_list;
ctx.on_return_or_destroy_code_list = on_return_or_destroy_code_list;
ctx.auto_yield = auto_yield;
ycf_node_search_and_replace_statements_in_scope(s,
save_nr_of_reductions_before_return_replacer,
&ctx);
}
ycf_node* mk_consume_reds_code(char* function_name, ycf_node* consume_reds_node){
ycf_node_list parameters = consume_reds_node->u.statement.expression->u.function_call.parameter_expressions;
ycf_string_printable_buffer* b = ycf_string_printable_buffer_new();
ycf_yield_location_id_counter++;
ycf_string_printable_buffer_printf(b, "(");
ycf_node* exp = ycf_node_list_get_item_at_position(¶meters, 0);
print_node_code_expression(exp->u.expression, b);
ycf_string_printable_buffer_printf(b, ")");
return ycf_node_get_from_code_scope_text(ycf_string_new("ycf_nr_of_reductions = ycf_nr_of_reductions - %s;\n"
"if (ycf_nr_of_reductions <= 0) {\n"
" ycf_trap_location = %d;\n"
" goto ycf_do_yield_label_%s;\n"
" ycf_yield_location_label_%d:;\n"
"}\n",
b->buffer,
ycf_yield_location_id_counter,
function_name,
ycf_yield_location_id_counter));
}
static
ycf_node* consume_reds_replacer(ycf_node* candidate, ycf_node_code_scope* s, void* context){
char* function_name = context;
(void)s;
if(candidate->type == ycf_node_type_statement &&
candidate->u.statement.expression != NULL &&
candidate->u.statement.expression->type == ycf_node_type_function_call &&
ycf_symbol_is_text_eq(candidate->u.statement.expression->u.function_call.identifier,
"YCF_CONSUME_REDS")){
ycf_node_list parameters = candidate->u.statement.expression->u.function_call.parameter_expressions;
if (ycf_node_list_length(parameters) != 1){
fprintf(stderr, "Error: YCF_CONSUME_REDS call needs exactly one parameter\n");
exit(1);
}
return mk_consume_reds_code(function_name, candidate);
} else {
return candidate;
}
}
static
void insert_consume_reds_code(char* function_name, ycf_node_code_scope* s){
ycf_node_search_and_replace_statements_in_scope(s,
consume_reds_replacer,
function_name);
}
static
ycf_node* mk_consume_reds_wrapped_statement(ycf_node* statement){
return ycf_node_get_from_code_scope_text(ycf_string_new("YCF_CONSUME_REDS(1);\n"
"%s\n", ycf_node_to_string(statement)));
}
static
void insert_consume_reds_calls(ycf_node_code_scope* s);
static
ycf_node* consume_reds_calls_inserter(ycf_node* candidate, ycf_node_code_scope* s, void* context){
if(candidate->type == ycf_node_type_goto){
return mk_consume_reds_wrapped_statement(candidate);
} else if (candidate->type == ycf_node_type_while){
if(candidate->u.while_n.statement->type == ycf_node_type_code_scope){
insert_consume_reds_calls(&candidate->u.while_n.statement->u.code_scope);
}
candidate->u.while_n.statement = mk_consume_reds_wrapped_statement(candidate->u.while_n.statement);
return candidate;
} else if (candidate->type == ycf_node_type_do_while){
if(candidate->u.do_while.statement->type == ycf_node_type_code_scope){
insert_consume_reds_calls(&candidate->u.do_while.statement->u.code_scope);
}
candidate->u.do_while.statement = mk_consume_reds_wrapped_statement(candidate->u.do_while.statement);
return candidate;
} else if (candidate->type == ycf_node_type_for){
if(candidate->u.for_n.statement->type == ycf_node_type_code_scope){
insert_consume_reds_calls(&candidate->u.for_n.statement->u.code_scope);
}
candidate->u.for_n.statement = mk_consume_reds_wrapped_statement(candidate->u.for_n.statement);
return candidate;
} else {
return candidate;
}
}
static void insert_consume_reds_calls(ycf_node_code_scope* s){
ycf_node_search_and_replace_statements_in_scope(s,
consume_reds_calls_inserter,
NULL);
}
typedef struct {
ycf_node_list code;
ycf_node_type code_type;
} special_code_context;
ycf_node* do_special_code_replace(ycf_node* candidate, ycf_node_code_scope* s, void* context){
special_code_context* c = context;
(void)s;
if(candidate->type == c->code_type){
ycf_node_list_append(&c->code, ycf_node_shallow_copy(candidate));
return ycf_node_new_text_node("\n/* YCF Replaced special code */\n");
} else {
return candidate;
}
}
static
ycf_node_list save_and_replace_special_code(ycf_node_code_scope* s, ycf_node_type code_type){
special_code_context context;
context.code = ycf_node_list_empty();
context.code_type = code_type;
ycf_node_search_and_replace_statements_in_scope(s,
do_special_code_replace,
&context);
return context.code;
}
static
ycf_node* insert_fun_call_state_var_replacer(ycf_node* candidate, ycf_node_code_scope* s, void* context){
ycf_string_item_list* yielding_funs = context;
(void)s;
if(candidate->type == ycf_node_type_statement &&
(candidate->u.statement.expression->type == ycf_node_type_function_call ||
candidate->u.statement.expression->type == ycf_node_type_assignment_function_call)){
ycf_string_item* current = yielding_funs->head;
while(current != NULL){
char* fun_name = NULL;
if(candidate->u.statement.expression->type == ycf_node_type_function_call){
fun_name = ycf_symbol_get_text(candidate->u.statement.expression->u.function_call.identifier);
} else if(candidate->u.statement.expression->type == ycf_node_type_assignment_function_call){
fun_name = ycf_symbol_get_text(
candidate->u.statement.expression->u.function_call_assignment.fun_call.identifier);
} else {
current = current->next;
continue;
}
if(ycf_string_is_equal(current->str, fun_name)){
return mk_yield_fun_call_state_var(candidate);
}
current = current->next;
}
}
return candidate;
}
static
void insert_fun_call_state_var(ycf_node_code_scope* s, ycf_string_item_list* yielding_funs){
ycf_node_search_and_replace_statements_in_scope(s,
insert_fun_call_state_var_replacer,
yielding_funs);
}
typedef struct {
ycf_node* c_file_node;
char* calling_fun_name;
ycf_string_item_list* yielding_funs;
ycf_string_item_list* starte_vars_wb;
bool auto_yield;
} yielding_fun_call_code_context;
static
ycf_node* insert_yielding_fun_call_code_replacer(ycf_node* candidate,
ycf_node_code_scope* s,
void* context){
yielding_fun_call_code_context* c = context;
ycf_node* c_file_node = c->c_file_node;
ycf_string_item_list* yielding_funs = c->yielding_funs;
(void)s;
if (candidate->type == ycf_node_type_code_scope &&
candidate->u.code_scope.other_nodes.head != candidate->u.code_scope.other_nodes.last){
ycf_node* call_candidate = candidate->u.code_scope.other_nodes.head->next;
if (call_candidate->type == ycf_node_type_statement &&
(call_candidate->u.statement.expression->type == ycf_node_type_function_call ||
call_candidate->u.statement.expression->type == ycf_node_type_assignment_function_call)) {
ycf_string_item* current = yielding_funs->head;
while(current != NULL){
char* fun_name = NULL;
if (call_candidate->u.statement.expression->type == ycf_node_type_function_call) {
fun_name = ycf_symbol_get_text(call_candidate->u.statement.expression->u.function_call.identifier);
} else if (call_candidate->u.statement.expression->type == ycf_node_type_assignment_function_call) {
fun_name = ycf_symbol_get_text(
call_candidate->u.statement.expression->u.function_call_assignment.fun_call.identifier);
} else {
current = current->next;
continue;
}
if(ycf_string_is_equal(current->str, fun_name)){
char* ycf_trap_state_var_name =
ycf_symbol_get_text(ycf_node_get_assignment(candidate->u.code_scope.other_nodes.head)->left_side.content.head->u.other.what);
ycf_string_item_list_append(c->starte_vars_wb, ycf_string_item_new(ycf_trap_state_var_name));
ycf_node* new_call_code = mk_yield_fun_call(c_file_node,
call_candidate,
ycf_trap_state_var_name,
c->calling_fun_name,
c->auto_yield);
ycf_node_list_replace(&candidate->u.code_scope.other_nodes, call_candidate, new_call_code);
return ycf_node_scope_new(ycf_symbol_new_open_curly_brace(),
candidate->u.code_scope.definition_nodes,
candidate->u.code_scope.other_nodes,
ycf_symbol_new_end_curly_brace());;
}
current = current->next;
}
}
}
return candidate;
}
ycf_string_item_list insert_yielding_fun_call_code(ycf_node* c_file_node,
ycf_node_code_scope* s,
ycf_string_item_list* yielding_funs,
char* calling_fun_name,
bool auto_yield){
yielding_fun_call_code_context context;
ycf_string_item_list state_vars = ycf_string_item_list_empty();
context.c_file_node = c_file_node;
context.calling_fun_name = calling_fun_name;
context.yielding_funs = yielding_funs;
context.starte_vars_wb = &state_vars;
context.auto_yield = auto_yield;
ycf_node_search_and_replace_statements_in_scope(s,
insert_yielding_fun_call_code_replacer,
&context);
return state_vars;
}
static
ycf_node* mk_yield_init_code(char* ycf_trap_state_name,
ycf_node_list ycf_trap_state_defines,
ycf_node_list on_restore_yield_state_code_list,
bool auto_yield,
char *function_name,
ycf_node_list declarations_in_function_body,
bool is_debug_mode){
char* code = ycf_string_new("\n"
"{\n"
" %s\n"
" ycf_nr_of_reductions = *ycf_nr_of_reductions_param;\n"
" ycf_frame_alloc_data.size = 0;\n"
" ycf_frame_alloc_data.max_size = ycf_stack_alloc_size_or_max_size;\n"
" ycf_frame_alloc_data.data = ycf_stack_alloc_data;\n"
" ycf_frame_alloc_data.needs_freeing = 0;\n"
" if(*ycf_trap_state != NULL){\n"
" struct %s* ycf_my_trap_state = *ycf_trap_state;\n"
" %s\n"
" %s\n"
" ycf_nr_of_reductions = *ycf_nr_of_reductions_param;\n"
" %s\n"
" }\n"
"}\n",
(is_debug_mode ? get_debug_trap_zero_assignments(declarations_in_function_body): ""),
ycf_trap_state_name,
get_trap_restore_assignments(ycf_trap_state_defines),
mk_code_from_special_code_list(on_restore_yield_state_code_list),
get_goto_ycf_trap_location_switch(ycf_yield_location_id_counter));
return ycf_node_get_from_code_scope_text(code);
}
static
ycf_node* mk_destroy_state_function_node(char* yielding_function_name,
ycf_node_list defs,
char* ycf_trap_state_struct_name,
ycf_node_list destroy_code,
ycf_node_list on_destroy_state_or_return_code_list,
bool static_aux_funs){
ycf_node_list my_defs = ycf_node_list_shallow_copy(defs);
ycf_node* current = my_defs.head;
while(current != NULL){
current->u.definition.end = ycf_symbol_new_semicolon();
int empty_array_brackets = 0;
{
current->u.definition.array_brackets = ycf_node_list_shallow_copy(current->u.definition.array_brackets);
ycf_node* b = current->u.definition.array_brackets.head;
while(b != NULL && b->u.array_bracket.empty){
empty_array_brackets++;
ycf_node_list_remove(¤t->u.definition.array_brackets, b);
b = b->next;
}
}
current->u.definition.type_specifiers = ycf_symbol_list_shallow_copy(current->u.definition.type_specifiers);
if(empty_array_brackets > 0){
for(int i = 0; i < empty_array_brackets; i++){
ycf_symbol_list_append(¤t->u.definition.type_specifiers, ycf_symbol_new_star());
}
}
current = current->next;
}
char* code =
ycf_string_new("\n"
"%s void %s_ycf_gen_destroy(void* ycf_my_trap_state_param){\n"
" {\n"
" struct %s* ycf_my_trap_state = ycf_my_trap_state_param;\n"
" %s\n"
" %s\n"
" %s\n"
" %s\n"
" ycf_destroy_stack_allocator(&ycf_frame_alloc_data, ycf_yield_free, ycf_yield_alloc_free_context);\n"
" ycf_yield_free(ycf_my_trap_state, ycf_yield_alloc_free_context);\n"
" }\n"
"}\n",
static_aux_funs ? "static" : "",
yielding_function_name,
ycf_trap_state_struct_name,
ycf_node_list_to_string(&my_defs),
get_trap_restore_assignments(my_defs),
mk_code_from_special_code_list(on_destroy_state_or_return_code_list),
mk_code_from_special_code_list(destroy_code));
return ycf_node_get_function_from_text(code);
}
void ast_add_yield_code_generated_define(ycf_node* source_out_tree/*Will be changed*/, bool debug_mode)
{
char *debug_mode_code =
(debug_mode ?
"\n"
"#include <setjmp.h>\n"
"#include <stdint.h>\n"
"#include <string.h>\n"
"#include <inttypes.h>\n"
"static void* ycf_find_stack_bottom_conservative_helper(void);\n"
"static void* ycf_find_stack_bottom_conservative(void);\n"
"static void* ycf_find_stack_bottom_conservative_helper() {\n"
" void* p = NULL;\n"
" volatile intptr_t tmp = (intptr_t)&p;\n"
" /* codechecker_intentional [StackAddrEscapeBase] */\n"
" return (void*)tmp;\n"
"}\n"
"static void* ycf_find_stack_bottom_conservative() {\n"
" jmp_buf env;\n"
" setjmp(env);\n"
"\n"
" {\n"
" volatile int noinline = 1;\n"
" void* (*bottom)(void) = noinline\n"
" ? ycf_find_stack_bottom_conservative_helper\n"
" : (void*(*)(void))(NULL);\n"
"\n"
" return bottom();\n"
" }\n"
"}\n"
"void* ycf_debug_get_stack_start(void);\n"
"#include <signal.h>\n"
"static void ycf_debug_check_block(char*, void*, void*, void*, size_t, void (*)(void));\n"
"static void ycf_debug_check_block(char* struct_name, void* stack_start, void* stack_end, void* block, size_t block_size, void (*print_offsets_function)(void)) {\n"
" char* p;\n"
" if (ycf_debug_get_stack_start() != NULL) {\n"
" stack_end = ycf_debug_get_stack_start();\n"
" }\n"
" for (p = block; p < (((char*)block) + block_size); p += sizeof(void*)) {\n"
" if(*((char**)p) > (char*)stack_start && *((char**)p) <= (char*)stack_end){\n"
" fprintf(stderr, \"\\n\\nPointer to stack in yielded functions state!!!!! (pointer_address=%p, pointer_value=%p, struct %s, offset=%zu, struct size=%zu)\\n\\n\", (void*)p, *((void**)p), struct_name, (size_t)((uintptr_t)p-(uintptr_t)block), block_size);\n"
" print_offsets_function();\n"
" raise(SIGABRT);\n"
" }\n"
" }\n"
"}\n"
"\n"
:
"");
char *ycf_stack_alloc_code = (
"YCF_GCC_ATTRIBUTE_UNUSED\n"
"static void* ycf_stack_alloc(size_t size,\n"
" struct ycf_alloc_data* data,\n"
" ycf_yield_alloc_type allocator,\n"
" void* ycf_yield_alloc_free_context,\n"
" size_t default_stack_size){\n"
" void * ret = NULL;\n"
" if (data->data == NULL) {\n"
" if (default_stack_size == 0) {\n"
" fprintf(stderr, \"ycf_alloc: not enough stack!! (max size = 0)\\n\");\n"
" exit(1);\n"
" }\n"
" data->data = allocator(default_stack_size, ycf_yield_alloc_free_context);\n"
" data->needs_freeing = 1;\n"
" data->max_size = default_stack_size;\n"
" data->size = 0;\n"
" }\n"
" if (data->size + size > data->max_size) {\n"
" fprintf(stderr, \"ycf_alloc: not enough stack! (max size = %zu)\\n\", default_stack_size);\n"
" exit(1);\n"
" }\n"
" ret = &data->data[data->size];\n"
" data->size = data->size + size + (sizeof(void * ) - (size % sizeof(void * ))) % sizeof(void * );\n"
" return ret;\n"
"}\n"
"YCF_GCC_ATTRIBUTE_UNUSED\n"
"static void ycf_destroy_stack_allocator(struct ycf_alloc_data* data,\n"
" ycf_yield_free_type freer,\n"
" void* ycf_yield_alloc_free_context){\n"
" if(data->needs_freeing){\n"
" freer(data->data, ycf_yield_alloc_free_context);\n"
" }\n"
"}\n");
char* ycf_yielding_c_fun_helpers_code =
ycf_string_new("#ifndef YCF_YIELDING_C_FUN_HELPERS\n"
"#define YCF_YIELDING_C_FUN_HELPERS 1\n"
"#include <string.h>\n"
"#include <stdio.h>\n"
"#include <stdlib.h>\n"
"\n"
"/*\n"
" * YCF_GCC_DIAG_ON and YCF_GCC_DIAG_OFF can be used to temporarly\n"
" * disable a gcc or clang warning in a file.\n"
" *\n"
" * Example:\n"
" * YCF_GCC_DIAG_OFF(unused-function)\n"
" * static int test(){ return 0;}\n"
" * YCF_GCC_DIAG_ON(unused-function)\n"
" *\n"
" * These macros were originally authored by Jonathan Wakely and has\n"
" * been modified by Patrick Horgan.\n"
" *\n"
" * Source: http://dbp-consulting.com/tutorials/SuppressingGCCWarnings.html\n"