2 * Copyright (C) 2006 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * You have a lists of states. kernel = locked, foo = NULL, ...
20 * When you hit an if {} else {} statement then you swap the list
21 * of states for a different list of states. The lists are stored
24 * At the beginning of this file there are list of the stacks that
25 * we use. Each function in this file does something to one of
28 * So the smatch_flow.c understands code but it doesn't understand states.
29 * smatch_flow calls functions in this file. This file calls functions
30 * in smatch_slist.c which just has boring generic plumbing for handling
31 * state lists. But really it's this file where all the magic happens.
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
40 struct smatch_state undefined
= { .name
= "undefined" };
41 struct smatch_state ghost
= { .name
= "ghost" };
42 struct smatch_state merged
= { .name
= "merged" };
43 struct smatch_state true_state
= { .name
= "true" };
44 struct smatch_state false_state
= { .name
= "false" };
46 static struct stree
*cur_stree
; /* current states */
47 static struct stree
*fast_overlay
;
49 static struct stree_stack
*true_stack
; /* states after a t/f branch */
50 static struct stree_stack
*false_stack
;
51 static struct stree_stack
*pre_cond_stack
; /* states before a t/f branch */
53 static struct stree_stack
*cond_true_stack
; /* states affected by a branch */
54 static struct stree_stack
*cond_false_stack
;
56 static struct stree_stack
*fake_cur_stree_stack
;
59 static struct stree_stack
*break_stack
;
60 static struct stree_stack
*fake_break_stack
;
61 static struct stree_stack
*switch_stack
;
62 static struct range_list_stack
*remaining_cases
;
63 static struct stree_stack
*default_stack
;
64 static struct stree_stack
*continue_stack
;
66 static struct named_stree_stack
*goto_stack
;
68 static struct ptr_list
*backup
;
72 void __print_cur_stree(void)
74 __print_stree(cur_stree
);
77 bool __print_states(const char *owner
)
85 FOR_EACH_SM(__get_cur_stree(), sm
) {
86 if (!strstr(check_name(sm
->owner
), owner
))
88 sm_msg("%s", show_sm(sm
));
90 } END_FOR_EACH_SM(sm
);
102 void __set_cur_stree_readonly(void)
107 void __set_cur_stree_writable(void)
112 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
114 struct sm_state
*ret
;
120 sm_perror("cur_stree is read only.");
122 if (option_debug
|| strcmp(check_name(owner
), option_debug_check
) == 0) {
123 struct smatch_state
*s
;
125 s
= __get_state(owner
, name
, sym
);
127 sm_msg("%s new [%s] '%s' %s", __func__
,
128 check_name(owner
), name
, show_state(state
));
130 sm_msg("%s change [%s] '%s' %s => %s",
131 __func__
, check_name(owner
), name
, show_state(s
),
135 if (owner
!= -1 && unreachable())
138 if (fake_cur_stree_stack
)
139 set_state_stree_stack(&fake_cur_stree_stack
, owner
, name
, sym
, state
);
141 ret
= set_state_stree(&cur_stree
, owner
, name
, sym
, state
);
146 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
)
150 struct sm_state
*ret
= NULL
;
152 expr
= strip_expr(expr
);
153 name
= expr_to_var_sym(expr
, &sym
);
156 ret
= set_state(owner
, name
, sym
, state
);
162 struct stree
*__swap_cur_stree(struct stree
*stree
)
164 struct stree
*orig
= cur_stree
;
170 void __push_fake_cur_stree(void)
172 push_stree(&fake_cur_stree_stack
, NULL
);
173 __save_pre_cond_states();
176 struct stree
*__pop_fake_cur_stree(void)
178 if (!fake_cur_stree_stack
)
179 sm_perror("popping too many fake cur strees.");
180 __use_pre_cond_states();
181 return pop_stree(&fake_cur_stree_stack
);
184 void __free_fake_cur_stree(void)
188 stree
= __pop_fake_cur_stree();
192 void __set_fake_cur_stree_fast(struct stree
*stree
)
195 sm_perror("cannot nest fast overlay");
198 fast_overlay
= stree
;
199 set_fast_math_only();
202 void __pop_fake_cur_stree_fast(void)
205 clear_fast_math_only();
208 void __merge_stree_into_cur(struct stree
*stree
)
211 struct sm_state
*orig
;
212 struct sm_state
*merged
;
214 FOR_EACH_SM(stree
, sm
) {
215 orig
= get_sm_state(sm
->owner
, sm
->name
, sm
->sym
);
217 merged
= merge_sm_states(orig
, sm
);
221 } END_FOR_EACH_SM(sm
);
224 void __set_sm(struct sm_state
*sm
)
227 sm_perror("cur_stree is read only.");
230 strcmp(check_name(sm
->owner
), option_debug_check
) == 0) {
231 struct smatch_state
*s
;
233 s
= __get_state(sm
->owner
, sm
->name
, sm
->sym
);
235 sm_msg("%s new %s", __func__
, show_sm(sm
));
237 sm_msg("%s change %s (was %s)", __func__
, show_sm(sm
),
244 if (fake_cur_stree_stack
)
245 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
247 overwrite_sm_state_stree(&cur_stree
, sm
);
250 void __set_sm_cur_stree(struct sm_state
*sm
)
253 sm_perror("cur_stree is read only.");
256 strcmp(check_name(sm
->owner
), option_debug_check
) == 0) {
257 struct smatch_state
*s
;
259 s
= __get_state(sm
->owner
, sm
->name
, sm
->sym
);
261 sm_msg("%s new %s", __func__
, show_sm(sm
));
263 sm_msg("%s change %s (was %s)",
264 __func__
, show_sm(sm
), show_state(s
));
270 overwrite_sm_state_stree(&cur_stree
, sm
);
273 void __set_sm_fake_stree(struct sm_state
*sm
)
276 sm_perror("cur_stree is read only.");
279 strcmp(check_name(sm
->owner
), option_debug_check
) == 0) {
280 struct smatch_state
*s
;
282 s
= __get_state(sm
->owner
, sm
->name
, sm
->sym
);
284 sm_msg("%s new %s", __func__
, show_sm(sm
));
286 sm_msg("%s change %s (was %s)",
287 __func__
, show_sm(sm
), show_state(s
));
293 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
297 typedef void (get_state_hook
)(int owner
, const char *name
, struct symbol
*sym
);
298 DECLARE_PTR_LIST(fn_list
, get_state_hook
*);
299 static struct fn_list
*get_state_hooks
;
301 void add_get_state_hook(get_state_hook
*fn
)
303 get_state_hook
**p
= malloc(sizeof(get_state_hook
*));
305 add_ptr_list(&get_state_hooks
, p
);
308 static void call_get_state_hooks(int owner
, const char *name
, struct symbol
*sym
)
310 static int recursion
;
317 FOR_EACH_PTR(get_state_hooks
, fn
) {
318 (*fn
)(owner
, name
, sym
);
319 } END_FOR_EACH_PTR(fn
);
324 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
)
328 sm
= get_sm_state(owner
, name
, sym
);
334 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
)
336 call_get_state_hooks(owner
, name
, sym
);
338 return __get_state(owner
, name
, sym
);
341 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
)
345 struct smatch_state
*ret
= NULL
;
347 expr
= strip_expr(expr
);
348 name
= expr_to_var_sym(expr
, &sym
);
351 ret
= get_state(owner
, name
, sym
);
357 struct state_list
*get_possible_states(int owner
, const char *name
, struct symbol
*sym
)
359 struct sm_state
*sms
;
361 sms
= get_sm_state_stree(cur_stree
, owner
, name
, sym
);
363 return sms
->possible
;
367 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
)
371 struct state_list
*ret
= NULL
;
373 expr
= strip_expr(expr
);
374 name
= expr_to_var_sym(expr
, &sym
);
377 ret
= get_possible_states(owner
, name
, sym
);
383 struct sm_state
*get_sm_state(int owner
, const char *name
, struct symbol
*sym
)
385 struct sm_state
*ret
;
387 ret
= get_sm_state_stree(fast_overlay
, owner
, name
, sym
);
391 return get_sm_state_stree(cur_stree
, owner
, name
, sym
);
394 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
)
398 struct sm_state
*ret
= NULL
;
400 expr
= strip_expr(expr
);
401 name
= expr_to_var_sym(expr
, &sym
);
404 ret
= get_sm_state(owner
, name
, sym
);
410 void delete_state(int owner
, const char *name
, struct symbol
*sym
)
412 delete_state_stree(&cur_stree
, owner
, name
, sym
);
413 if (cond_true_stack
) {
414 delete_state_stree_stack(&pre_cond_stack
, owner
, name
, sym
);
415 delete_state_stree_stack(&cond_true_stack
, owner
, name
, sym
);
416 delete_state_stree_stack(&cond_false_stack
, owner
, name
, sym
);
420 void delete_state_expr(int owner
, struct expression
*expr
)
425 expr
= strip_expr(expr
);
426 name
= expr_to_var_sym(expr
, &sym
);
429 delete_state(owner
, name
, sym
);
434 static void delete_all_states_stree_sym(struct stree
**stree
, struct symbol
*sym
)
436 struct state_list
*slist
= NULL
;
439 FOR_EACH_SM(*stree
, sm
) {
441 add_ptr_list(&slist
, sm
);
442 } END_FOR_EACH_SM(sm
);
444 FOR_EACH_PTR(slist
, sm
) {
445 delete_state_stree(stree
, sm
->owner
, sm
->name
, sm
->sym
);
446 } END_FOR_EACH_PTR(sm
);
451 static void delete_all_states_stree_stack_sym(struct stree_stack
**stack
, struct symbol
*sym
)
458 stree
= pop_stree(stack
);
459 delete_all_states_stree_sym(&stree
, sym
);
460 push_stree(stack
, stree
);
463 void __delete_all_states_sym(struct symbol
*sym
)
465 delete_all_states_stree_sym(&cur_stree
, sym
);
467 delete_all_states_stree_stack_sym(&true_stack
, sym
);
468 delete_all_states_stree_stack_sym(&true_stack
, sym
);
469 delete_all_states_stree_stack_sym(&false_stack
, sym
);
470 delete_all_states_stree_stack_sym(&pre_cond_stack
, sym
);
471 delete_all_states_stree_stack_sym(&cond_true_stack
, sym
);
472 delete_all_states_stree_stack_sym(&cond_false_stack
, sym
);
473 delete_all_states_stree_stack_sym(&fake_cur_stree_stack
, sym
);
474 delete_all_states_stree_stack_sym(&break_stack
, sym
);
475 delete_all_states_stree_stack_sym(&fake_break_stack
, sym
);
476 delete_all_states_stree_stack_sym(&switch_stack
, sym
);
477 delete_all_states_stree_stack_sym(&continue_stack
, sym
);
480 * deleting from the goto stack is problematic because we don't know
481 * if the label is in scope and also we need the value for --two-passes.
485 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
)
487 struct stree
*ret
= NULL
;
488 struct sm_state
*tmp
;
490 FOR_EACH_SM(source
, tmp
) {
491 if (tmp
->owner
== owner
)
492 avl_insert(&ret
, tmp
);
493 } END_FOR_EACH_SM(tmp
);
498 struct stree
*get_all_states_stree(int owner
)
500 return get_all_states_from_stree(owner
, cur_stree
);
503 struct stree
*__get_cur_stree(void)
508 int is_reachable(void)
515 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
516 struct smatch_state
*true_state
,
517 struct smatch_state
*false_state
)
520 sm_perror("cur_stree is read only.");
522 if (option_debug
|| strcmp(check_name(owner
), option_debug_check
) == 0) {
523 struct smatch_state
*tmp
;
525 tmp
= __get_state(owner
, name
, sym
);
526 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__
,
527 check_name(owner
), name
, show_state(tmp
),
528 show_state(true_state
), show_state(false_state
));
534 if (!cond_false_stack
|| !cond_true_stack
) {
535 sm_perror("missing true/false stacks");
540 set_state_stree_stack(&cond_true_stack
, owner
, name
, sym
, true_state
);
542 set_state_stree_stack(&cond_false_stack
, owner
, name
, sym
, false_state
);
545 void set_true_false_states_expr(int owner
, struct expression
*expr
,
546 struct smatch_state
*true_state
,
547 struct smatch_state
*false_state
)
552 expr
= strip_expr(expr
);
553 name
= expr_to_var_sym(expr
, &sym
);
556 set_true_false_states(owner
, name
, sym
, true_state
, false_state
);
561 void __set_true_false_sm(struct sm_state
*true_sm
, struct sm_state
*false_sm
)
567 if (!true_sm
&& !false_sm
)
573 owner
= true_sm
? true_sm
->owner
: false_sm
->owner
;
574 name
= true_sm
? true_sm
->name
: false_sm
->name
;
575 sym
= true_sm
? true_sm
->sym
: false_sm
->sym
;
576 if (option_debug
|| strcmp(check_name(owner
), option_debug_check
) == 0) {
577 struct smatch_state
*tmp
;
579 tmp
= __get_state(owner
, name
, sym
);
580 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__
,
581 check_name(owner
), name
, show_state(tmp
),
582 show_state(true_sm
? true_sm
->state
: NULL
),
583 show_state(false_sm
? false_sm
->state
: NULL
));
586 if (!cond_false_stack
|| !cond_true_stack
) {
587 sm_perror("missing true/false stacks");
592 overwrite_sm_state_stree_stack(&cond_true_stack
, true_sm
);
594 overwrite_sm_state_stree_stack(&cond_false_stack
, false_sm
);
597 void nullify_path(void)
599 if (fake_cur_stree_stack
) {
600 __free_fake_cur_stree();
601 __push_fake_cur_stree();
603 free_stree(&cur_stree
);
606 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
613 * At the start of every function we mark the path
614 * as unnull. That way there is always at least one state
615 * in the cur_stree until nullify_path is called. This
616 * is used in merge_slist() for the first null check.
618 void __unnullify_path(void)
621 set_state(-1, "unnull_path", NULL
, &true_state
);
624 int __path_is_null(void)
631 static void check_stree_stack_free(struct stree_stack
**stack
)
634 sm_perror("stack not empty");
635 free_stack_and_strees(stack
);
639 void save_all_states(void)
641 __add_ptr_list(&backup
, cur_stree
);
644 __add_ptr_list(&backup
, true_stack
);
646 __add_ptr_list(&backup
, false_stack
);
648 __add_ptr_list(&backup
, pre_cond_stack
);
649 pre_cond_stack
= NULL
;
651 __add_ptr_list(&backup
, cond_true_stack
);
652 cond_true_stack
= NULL
;
653 __add_ptr_list(&backup
, cond_false_stack
);
654 cond_false_stack
= NULL
;
656 __add_ptr_list(&backup
, fake_cur_stree_stack
);
657 fake_cur_stree_stack
= NULL
;
659 __add_ptr_list(&backup
, break_stack
);
661 __add_ptr_list(&backup
, fake_break_stack
);
662 fake_break_stack
= NULL
;
664 __add_ptr_list(&backup
, switch_stack
);
666 __add_ptr_list(&backup
, remaining_cases
);
667 remaining_cases
= NULL
;
668 __add_ptr_list(&backup
, default_stack
);
669 default_stack
= NULL
;
670 __add_ptr_list(&backup
, continue_stack
);
671 continue_stack
= NULL
;
673 __add_ptr_list(&backup
, goto_stack
);
677 static void *pop_backup(void)
681 ret
= last_ptr_list(backup
);
682 delete_ptr_list_last(&backup
);
686 void restore_all_states(void)
688 goto_stack
= pop_backup();
690 continue_stack
= pop_backup();
691 default_stack
= pop_backup();
692 remaining_cases
= pop_backup();
693 switch_stack
= pop_backup();
694 fake_break_stack
= pop_backup();
695 break_stack
= pop_backup();
697 fake_cur_stree_stack
= pop_backup();
699 cond_false_stack
= pop_backup();
700 cond_true_stack
= pop_backup();
702 pre_cond_stack
= pop_backup();
703 false_stack
= pop_backup();
704 true_stack
= pop_backup();
706 cur_stree
= pop_backup();
709 void free_goto_stack(void)
711 struct named_stree
*named_stree
;
713 FOR_EACH_PTR(goto_stack
, named_stree
) {
714 free_stree(&named_stree
->stree
);
715 } END_FOR_EACH_PTR(named_stree
);
716 __free_ptr_list((struct ptr_list
**)&goto_stack
);
719 void clear_all_states(void)
722 check_stree_stack_free(&true_stack
);
723 check_stree_stack_free(&false_stack
);
724 check_stree_stack_free(&pre_cond_stack
);
725 check_stree_stack_free(&cond_true_stack
);
726 check_stree_stack_free(&cond_false_stack
);
727 check_stree_stack_free(&break_stack
);
728 check_stree_stack_free(&fake_break_stack
);
729 check_stree_stack_free(&switch_stack
);
730 check_stree_stack_free(&continue_stack
);
731 check_stree_stack_free(&fake_cur_stree_stack
);
735 free_every_single_sm_state();
736 free_tmp_expressions();
739 void __push_cond_stacks(void)
741 push_stree(&cond_true_stack
, NULL
);
742 push_stree(&cond_false_stack
, NULL
);
743 __push_fake_cur_stree();
746 void __fold_in_set_states(void)
748 struct stree
*new_states
;
751 new_states
= __pop_fake_cur_stree();
752 FOR_EACH_SM(new_states
, sm
) {
754 __set_true_false_sm(sm
, sm
);
755 } END_FOR_EACH_SM(sm
);
756 free_stree(&new_states
);
759 void __free_set_states(void)
761 struct stree
*new_states
;
763 new_states
= __pop_fake_cur_stree();
764 free_stree(&new_states
);
767 struct stree
*__copy_cond_true_states(void)
771 ret
= pop_stree(&cond_true_stack
);
772 push_stree(&cond_true_stack
, clone_stree(ret
));
776 struct stree
*__copy_cond_false_states(void)
780 ret
= pop_stree(&cond_false_stack
);
781 push_stree(&cond_false_stack
, clone_stree(ret
));
785 struct stree
*__pop_cond_true_stack(void)
787 return pop_stree(&cond_true_stack
);
790 struct stree
*__pop_cond_false_stack(void)
792 return pop_stree(&cond_false_stack
);
796 * This combines the pre cond states with either the true or false states.
798 * a = kmalloc() ; if (a !! foo(a)
799 * In the pre state a is possibly null. In the true state it is non null.
800 * In the false state it is null. Combine the pre and the false to get
801 * that when we call 'foo', 'a' is null.
803 static void __use_cond_stack(struct stree_stack
**stack
)
807 free_stree(&cur_stree
);
809 cur_stree
= pop_stree(&pre_cond_stack
);
810 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
812 stree
= pop_stree(stack
);
813 overwrite_stree(stree
, &cur_stree
);
814 push_stree(stack
, stree
);
817 void __use_pre_cond_states(void)
819 free_stree(&cur_stree
);
820 cur_stree
= pop_stree(&pre_cond_stack
);
823 void __use_cond_true_states(void)
825 __use_cond_stack(&cond_true_stack
);
828 void __use_cond_false_states(void)
830 __use_cond_stack(&cond_false_stack
);
833 void __negate_cond_stacks(void)
835 struct stree
*old_false
, *old_true
;
837 old_false
= pop_stree(&cond_false_stack
);
838 old_true
= pop_stree(&cond_true_stack
);
839 push_stree(&cond_false_stack
, old_true
);
840 push_stree(&cond_true_stack
, old_false
);
843 void __and_cond_states(void)
845 and_stree_stack(&cond_true_stack
);
846 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_false_stack
);
849 void __or_cond_states(void)
851 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_true_stack
);
852 and_stree_stack(&cond_false_stack
);
855 void __save_pre_cond_states(void)
857 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
860 void __discard_pre_cond_states(void)
864 tmp
= pop_stree(&pre_cond_stack
);
868 struct stree
*__get_true_states(void)
870 return clone_stree(top_stree(cond_true_stack
));
873 struct stree
*__get_false_states(void)
875 return clone_stree(top_stree(cond_false_stack
));
878 void __use_cond_states(void)
880 struct stree
*pre
, *pre_clone
, *true_states
, *false_states
;
882 pre
= pop_stree(&pre_cond_stack
);
883 pre_clone
= clone_stree(pre
);
885 true_states
= pop_stree(&cond_true_stack
);
886 overwrite_stree(true_states
, &pre
);
887 free_stree(&true_states
);
888 /* we use the true states right away */
889 free_stree(&cur_stree
);
892 false_states
= pop_stree(&cond_false_stack
);
893 overwrite_stree(false_states
, &pre_clone
);
894 free_stree(&false_states
);
895 push_stree(&false_stack
, pre_clone
);
898 void __push_true_states(void)
900 push_stree(&true_stack
, clone_stree(cur_stree
));
903 void __use_false_states(void)
905 free_stree(&cur_stree
);
906 cur_stree
= pop_stree(&false_stack
);
909 void __discard_false_states(void)
913 stree
= pop_stree(&false_stack
);
917 void __merge_false_states(void)
921 stree
= pop_stree(&false_stack
);
922 merge_stree(&cur_stree
, stree
);
927 * This function probably seemed common sensical when I wrote it but, oh wow,
928 * does it look subtle in retrospect. Say we set a state on one side of the if
929 * else path but not on the other, then what we should record in the fake stree
930 * is the merged state.
932 * This function relies on the fact that the we always set the cur_stree as well
933 * and we already have the infrastructure to merge things correctly into the
936 * So instead of merging fake strees together which is probably a lot of work,
937 * we just use it as a list of set states and look up the actual current values
941 static void update_stree_with_merged(struct stree
**stree
)
943 struct state_list
*slist
= NULL
;
944 struct sm_state
*sm
, *new;
946 FOR_EACH_SM(*stree
, sm
) {
947 new = get_sm_state(sm
->owner
, sm
->name
, sm
->sym
);
948 if (!new) /* This can happen if we go out of scope */
950 add_ptr_list(&slist
, new);
951 } END_FOR_EACH_SM(sm
);
953 FOR_EACH_PTR(slist
, sm
) {
954 overwrite_sm_state_stree(stree
, sm
);
955 } END_FOR_EACH_PTR(sm
);
960 static void update_fake_stree_with_merged(void)
964 if (!fake_cur_stree_stack
)
966 stree
= pop_stree(&fake_cur_stree_stack
);
967 update_stree_with_merged(&stree
);
968 push_stree(&fake_cur_stree_stack
, stree
);
971 void __merge_true_states(void)
975 stree
= pop_stree(&true_stack
);
976 merge_stree(&cur_stree
, stree
);
977 update_fake_stree_with_merged();
981 void __push_continues(void)
983 push_stree(&continue_stack
, NULL
);
986 void __discard_continues(void)
990 stree
= pop_stree(&continue_stack
);
994 void __process_continues(void)
998 stree
= pop_stree(&continue_stack
);
1000 stree
= clone_stree(cur_stree
);
1002 merge_stree(&stree
, cur_stree
);
1004 push_stree(&continue_stack
, stree
);
1007 void __merge_continues(void)
1009 struct stree
*stree
;
1011 stree
= pop_stree(&continue_stack
);
1012 merge_stree(&cur_stree
, stree
);
1016 void __push_breaks(void)
1018 push_stree(&break_stack
, NULL
);
1019 if (fake_cur_stree_stack
)
1020 push_stree(&fake_break_stack
, NULL
);
1023 void __process_breaks(void)
1025 struct stree
*stree
;
1027 stree
= pop_stree(&break_stack
);
1029 stree
= clone_stree(cur_stree
);
1031 merge_stree(&stree
, cur_stree
);
1032 push_stree(&break_stack
, stree
);
1034 if (!fake_cur_stree_stack
)
1037 stree
= pop_stree(&fake_break_stack
);
1039 stree
= clone_stree(top_stree(fake_cur_stree_stack
));
1041 merge_stree(&stree
, top_stree(fake_cur_stree_stack
));
1042 push_stree(&fake_break_stack
, stree
);
1045 int __has_breaks(void)
1047 struct stree
*stree
;
1050 stree
= pop_stree(&break_stack
);
1052 push_stree(&break_stack
, stree
);
1056 void __merge_breaks(void)
1058 struct stree
*stree
;
1059 struct sm_state
*sm
;
1061 stree
= pop_stree(&break_stack
);
1062 merge_stree(&cur_stree
, stree
);
1065 if (!fake_cur_stree_stack
)
1068 stree
= pop_stree(&fake_break_stack
);
1069 update_stree_with_merged(&stree
);
1070 FOR_EACH_SM(stree
, sm
) {
1071 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
1072 } END_FOR_EACH_SM(sm
);
1076 void __use_breaks(void)
1078 struct stree
*stree
;
1079 struct sm_state
*sm
;
1081 free_stree(&cur_stree
);
1082 cur_stree
= pop_stree(&break_stack
);
1084 if (!fake_cur_stree_stack
)
1086 stree
= pop_stree(&fake_break_stack
);
1087 FOR_EACH_SM(stree
, sm
) {
1088 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
1089 } END_FOR_EACH_SM(sm
);
1095 void __save_switch_states(struct expression
*switch_expr
)
1097 struct range_list
*rl
;
1099 get_absolute_rl(switch_expr
, &rl
);
1101 push_rl(&remaining_cases
, rl
);
1102 push_stree(&switch_stack
, clone_stree(cur_stree
));
1105 int have_remaining_cases(void)
1107 return !!top_rl(remaining_cases
);
1110 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
)
1112 struct stree
*stree
;
1113 struct stree
*implied_stree
;
1115 stree
= pop_stree(&switch_stack
);
1118 * If the cur_stree was NULL before the start of the switch
1119 * statement then we don't want to unnullify it.
1122 push_stree(&switch_stack
, stree
);
1125 implied_stree
= __implied_case_stree(switch_expr
, case_rl
, &remaining_cases
, &stree
);
1126 merge_stree(&cur_stree
, implied_stree
);
1127 free_stree(&implied_stree
);
1128 push_stree(&switch_stack
, stree
);
1131 void __discard_switches(void)
1133 struct stree
*stree
;
1135 pop_rl(&remaining_cases
);
1136 stree
= pop_stree(&switch_stack
);
1140 void __push_default(void)
1142 push_stree(&default_stack
, NULL
);
1145 void __set_default(void)
1147 set_state_stree_stack(&default_stack
, 0, "has_default", NULL
, &true_state
);
1150 int __pop_default(void)
1152 struct stree
*stree
;
1154 stree
= pop_stree(&default_stack
);
1162 static struct named_stree
*alloc_named_stree(const char *name
, struct symbol
*sym
, struct stree
*stree
)
1164 struct named_stree
*named_stree
= __alloc_named_stree(0);
1166 named_stree
->name
= (char *)name
;
1167 named_stree
->stree
= stree
;
1168 named_stree
->sym
= sym
;
1172 void __save_gotos(const char *name
, struct symbol
*sym
)
1174 struct stree
**stree
;
1175 struct stree
*clone
;
1177 stree
= get_named_stree(goto_stack
, name
, sym
);
1179 merge_stree(stree
, cur_stree
);
1182 struct named_stree
*named_stree
;
1184 clone
= clone_stree(cur_stree
);
1185 named_stree
= alloc_named_stree(name
, sym
, clone
);
1186 add_ptr_list(&goto_stack
, named_stree
);
1190 void __merge_gotos(const char *name
, struct symbol
*sym
)
1192 struct stree
**stree
;
1194 stree
= get_named_stree(goto_stack
, name
, sym
);
1196 merge_stree(&cur_stree
, *stree
);