buf_size: split up the get_real_array_size() function
[smatch.git] / smatch_states.c
blob49844c963b7ce8f975fe84b997629c88097dc2c3
1 /*
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
22 * on stacks.
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
26 * of the stacks.
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.
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include "smatch.h"
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 */
48 static struct stree_stack *true_stack; /* states after a t/f branch */
49 static struct stree_stack *false_stack;
50 static struct stree_stack *pre_cond_stack; /* states before a t/f branch */
52 static struct stree_stack *cond_true_stack; /* states affected by a branch */
53 static struct stree_stack *cond_false_stack;
55 static struct stree_stack *fake_cur_stree_stack;
56 static int read_only;
58 static struct stree_stack *break_stack;
59 static struct stree_stack *fake_break_stack;
60 static struct stree_stack *switch_stack;
61 static struct range_list_stack *remaining_cases;
62 static struct stree_stack *default_stack;
63 static struct stree_stack *continue_stack;
65 static struct named_stree_stack *goto_stack;
67 static struct ptr_list *backup;
69 int option_debug;
71 void __print_cur_stree(void)
73 __print_stree(cur_stree);
76 int unreachable(void)
78 if (!cur_stree)
79 return 1;
80 return 0;
83 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
85 struct sm_state *ret;
87 if (!name)
88 return NULL;
90 if (read_only)
91 sm_msg("Smatch Internal Error: cur_stree is read only.");
93 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
94 struct smatch_state *s;
96 s = get_state(owner, name, sym);
97 if (!s)
98 sm_msg("%s new [%s] '%s' %s", __func__,
99 check_name(owner), name, show_state(state));
100 else
101 sm_msg("%s change [%s] '%s' %s => %s",
102 __func__, check_name(owner), name, show_state(s),
103 show_state(state));
106 if (owner != -1 && unreachable())
107 return NULL;
109 if (fake_cur_stree_stack)
110 set_state_stree_stack(&fake_cur_stree_stack, owner, name, sym, state);
112 ret = set_state_stree(&cur_stree, owner, name, sym, state);
114 return ret;
117 struct sm_state *set_state_expr(int owner, struct expression *expr, struct smatch_state *state)
119 char *name;
120 struct symbol *sym;
121 struct sm_state *ret = NULL;
123 expr = strip_expr(expr);
124 name = expr_to_var_sym(expr, &sym);
125 if (!name || !sym)
126 goto free;
127 ret = set_state(owner, name, sym, state);
128 free:
129 free_string(name);
130 return ret;
133 void __swap_cur_stree(struct stree *stree)
135 free_stree(&cur_stree);
136 cur_stree = stree;
139 void __push_fake_cur_stree(void)
141 push_stree(&fake_cur_stree_stack, NULL);
142 __save_pre_cond_states();
145 struct stree *__pop_fake_cur_stree(void)
147 if (!fake_cur_stree_stack)
148 sm_msg("INTERNAL error: popping too many fake cur strees.");
149 __use_pre_cond_states();
150 return pop_stree(&fake_cur_stree_stack);
153 void __free_fake_cur_stree(void)
155 struct stree *stree;
157 stree = __pop_fake_cur_stree();
158 free_stree(&stree);
161 void __set_fake_cur_stree_fast(struct stree *stree)
163 push_stree(&pre_cond_stack, cur_stree);
164 cur_stree = stree;
165 read_only = 1;
168 void __pop_fake_cur_stree_fast(void)
170 cur_stree = pop_stree(&pre_cond_stack);
171 read_only = 0;
174 void __merge_stree_into_cur(struct stree *stree)
176 struct sm_state *sm;
177 struct sm_state *orig;
178 struct sm_state *merged;
180 FOR_EACH_SM(stree, sm) {
181 orig = get_sm_state(sm->owner, sm->name, sm->sym);
182 if (orig)
183 merged = merge_sm_states(orig, sm);
184 else
185 merged = sm;
186 __set_sm(merged);
187 } END_FOR_EACH_SM(sm);
190 void __set_sm(struct sm_state *sm)
192 if (read_only)
193 sm_msg("Smatch Internal Error: cur_stree is read only.");
195 if (option_debug ||
196 strcmp(check_name(sm->owner), option_debug_check) == 0) {
197 struct smatch_state *s;
199 s = get_state(sm->owner, sm->name, sm->sym);
200 if (!s)
201 sm_msg("%s new %s", __func__, show_sm(sm));
202 else
203 sm_msg("%s change %s (was %s)", __func__, show_sm(sm),
204 show_state(s));
207 if (unreachable())
208 return;
210 if (fake_cur_stree_stack)
211 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
213 overwrite_sm_state_stree(&cur_stree, sm);
216 void __set_sm_cur_stree(struct sm_state *sm)
218 if (read_only)
219 sm_msg("Smatch Internal Error: cur_stree is read only.");
221 if (option_debug ||
222 strcmp(check_name(sm->owner), option_debug_check) == 0) {
223 struct smatch_state *s;
225 s = get_state(sm->owner, sm->name, sm->sym);
226 if (!s)
227 sm_msg("%s new %s", __func__, show_sm(sm));
228 else
229 sm_msg("%s change %s (was %s)",
230 __func__, show_sm(sm), show_state(s));
233 if (unreachable())
234 return;
236 overwrite_sm_state_stree(&cur_stree, sm);
239 void __set_sm_fake_stree(struct sm_state *sm)
241 if (read_only)
242 sm_msg("Smatch Internal Error: cur_stree is read only.");
244 if (option_debug ||
245 strcmp(check_name(sm->owner), option_debug_check) == 0) {
246 struct smatch_state *s;
248 s = get_state(sm->owner, sm->name, sm->sym);
249 if (!s)
250 sm_msg("%s new %s", __func__, show_sm(sm));
251 else
252 sm_msg("%s change %s (was %s)",
253 __func__, show_sm(sm), show_state(s));
256 if (unreachable())
257 return;
259 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
262 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
264 __get_state_hook(owner, name, sym);
266 return get_state_stree(cur_stree, owner, name, sym);
269 struct smatch_state *get_state_expr(int owner, struct expression *expr)
271 char *name;
272 struct symbol *sym;
273 struct smatch_state *ret = NULL;
275 expr = strip_expr(expr);
276 name = expr_to_var_sym(expr, &sym);
277 if (!name || !sym)
278 goto free;
279 ret = get_state(owner, name, sym);
280 free:
281 free_string(name);
282 return ret;
285 struct state_list *get_possible_states(int owner, const char *name, struct symbol *sym)
287 struct sm_state *sms;
289 sms = get_sm_state_stree(cur_stree, owner, name, sym);
290 if (sms)
291 return sms->possible;
292 return NULL;
295 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
297 char *name;
298 struct symbol *sym;
299 struct state_list *ret = NULL;
301 expr = strip_expr(expr);
302 name = expr_to_var_sym(expr, &sym);
303 if (!name || !sym)
304 goto free;
305 ret = get_possible_states(owner, name, sym);
306 free:
307 free_string(name);
308 return ret;
311 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
313 return get_sm_state_stree(cur_stree, owner, name, sym);
316 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
318 char *name;
319 struct symbol *sym;
320 struct sm_state *ret = NULL;
322 expr = strip_expr(expr);
323 name = expr_to_var_sym(expr, &sym);
324 if (!name || !sym)
325 goto free;
326 ret = get_sm_state(owner, name, sym);
327 free:
328 free_string(name);
329 return ret;
332 void delete_state(int owner, const char *name, struct symbol *sym)
334 delete_state_stree(&cur_stree, owner, name, sym);
335 if (cond_true_stack) {
336 delete_state_stree_stack(&pre_cond_stack, owner, name, sym);
337 delete_state_stree_stack(&cond_true_stack, owner, name, sym);
338 delete_state_stree_stack(&cond_false_stack, owner, name, sym);
342 void delete_state_expr(int owner, struct expression *expr)
344 char *name;
345 struct symbol *sym;
347 expr = strip_expr(expr);
348 name = expr_to_var_sym(expr, &sym);
349 if (!name || !sym)
350 goto free;
351 delete_state(owner, name, sym);
352 free:
353 free_string(name);
356 static void delete_all_states_stree_sym(struct stree **stree, struct symbol *sym)
358 struct state_list *slist = NULL;
359 struct sm_state *sm;
361 FOR_EACH_SM(*stree, sm) {
362 if (sm->sym == sym)
363 add_ptr_list(&slist, sm);
364 } END_FOR_EACH_SM(sm);
366 FOR_EACH_PTR(slist, sm) {
367 delete_state_stree(stree, sm->owner, sm->name, sm->sym);
368 } END_FOR_EACH_PTR(sm);
370 free_slist(&slist);
373 static void delete_all_states_stree_stack_sym(struct stree_stack **stack, struct symbol *sym)
375 struct stree *stree;
377 if (!*stack)
378 return;
380 stree = pop_stree(stack);
381 delete_all_states_stree_sym(&stree, sym);
382 push_stree(stack, stree);
385 void __delete_all_states_sym(struct symbol *sym)
387 delete_all_states_stree_sym(&cur_stree, sym);
389 delete_all_states_stree_stack_sym(&true_stack, sym);
390 delete_all_states_stree_stack_sym(&true_stack, sym);
391 delete_all_states_stree_stack_sym(&false_stack, sym);
392 delete_all_states_stree_stack_sym(&pre_cond_stack, sym);
393 delete_all_states_stree_stack_sym(&cond_true_stack, sym);
394 delete_all_states_stree_stack_sym(&cond_false_stack, sym);
395 delete_all_states_stree_stack_sym(&fake_cur_stree_stack, sym);
396 delete_all_states_stree_stack_sym(&break_stack, sym);
397 delete_all_states_stree_stack_sym(&fake_break_stack, sym);
398 delete_all_states_stree_stack_sym(&switch_stack, sym);
399 delete_all_states_stree_stack_sym(&continue_stack, sym);
402 * deleting from the goto stack is problematic because we don't know
403 * if the label is in scope and also we need the value for --two-passes.
407 struct stree *get_all_states_from_stree(int owner, struct stree *source)
409 struct stree *ret = NULL;
410 struct sm_state *tmp;
412 FOR_EACH_SM(source, tmp) {
413 if (tmp->owner == owner)
414 avl_insert(&ret, tmp);
415 } END_FOR_EACH_SM(tmp);
417 return ret;
420 struct stree *get_all_states_stree(int owner)
422 return get_all_states_from_stree(owner, cur_stree);
425 struct stree *__get_cur_stree(void)
427 return cur_stree;
430 int is_reachable(void)
432 if (cur_stree)
433 return 1;
434 return 0;
437 void set_true_false_states(int owner, const char *name, struct symbol *sym,
438 struct smatch_state *true_state,
439 struct smatch_state *false_state)
441 if (read_only)
442 sm_msg("Smatch Internal Error: cur_stree is read only.");
444 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
445 struct smatch_state *tmp;
447 tmp = get_state(owner, name, sym);
448 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__,
449 check_name(owner), name, show_state(tmp),
450 show_state(true_state), show_state(false_state));
453 if (unreachable())
454 return;
456 if (!cond_false_stack || !cond_true_stack) {
457 printf("Error: missing true/false stacks\n");
458 return;
461 if (true_state)
462 set_state_stree_stack(&cond_true_stack, owner, name, sym, true_state);
463 if (false_state)
464 set_state_stree_stack(&cond_false_stack, owner, name, sym, false_state);
467 void set_true_false_states_expr(int owner, struct expression *expr,
468 struct smatch_state *true_state,
469 struct smatch_state *false_state)
471 char *name;
472 struct symbol *sym;
474 expr = strip_expr(expr);
475 name = expr_to_var_sym(expr, &sym);
476 if (!name || !sym)
477 goto free;
478 set_true_false_states(owner, name, sym, true_state, false_state);
479 free:
480 free_string(name);
483 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
485 int owner;
486 const char *name;
487 struct symbol *sym;
489 if (!true_sm && !false_sm)
490 return;
492 if (unreachable())
493 return;
495 owner = true_sm ? true_sm->owner : false_sm->owner;
496 name = true_sm ? true_sm->name : false_sm->name;
497 sym = true_sm ? true_sm->sym : false_sm->sym;
498 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
499 struct smatch_state *tmp;
501 tmp = get_state(owner, name, sym);
502 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__,
503 check_name(owner), name, show_state(tmp),
504 show_state(true_sm ? true_sm->state : NULL),
505 show_state(false_sm ? false_sm->state : NULL));
508 if (!cond_false_stack || !cond_true_stack) {
509 printf("Error: missing true/false stacks\n");
510 return;
513 if (true_sm)
514 overwrite_sm_state_stree_stack(&cond_true_stack, true_sm);
515 if (false_sm)
516 overwrite_sm_state_stree_stack(&cond_false_stack, false_sm);
519 void nullify_path(void)
521 if (fake_cur_stree_stack) {
522 __free_fake_cur_stree();
523 __push_fake_cur_stree();
525 free_stree(&cur_stree);
528 void __match_nullify_path_hook(const char *fn, struct expression *expr,
529 void *unused)
531 nullify_path();
535 * At the start of every function we mark the path
536 * as unnull. That way there is always at least one state
537 * in the cur_stree until nullify_path is called. This
538 * is used in merge_slist() for the first null check.
540 void __unnullify_path(void)
542 if (!cur_stree)
543 set_state(-1, "unnull_path", NULL, &true_state);
546 int __path_is_null(void)
548 if (cur_stree)
549 return 0;
550 return 1;
553 static void check_stree_stack_free(struct stree_stack **stack)
555 if (*stack) {
556 sm_msg("smatch internal error: stack not empty");
557 free_stack_and_strees(stack);
561 void save_all_states(void)
563 __add_ptr_list(&backup, cur_stree, 0);
564 cur_stree = NULL;
566 __add_ptr_list(&backup, true_stack, 0);
567 true_stack = NULL;
568 __add_ptr_list(&backup, false_stack, 0);
569 false_stack = NULL;
570 __add_ptr_list(&backup, pre_cond_stack, 0);
571 pre_cond_stack = NULL;
573 __add_ptr_list(&backup, cond_true_stack, 0);
574 cond_true_stack = NULL;
575 __add_ptr_list(&backup, cond_false_stack, 0);
576 cond_false_stack = NULL;
578 __add_ptr_list(&backup, fake_cur_stree_stack, 0);
579 fake_cur_stree_stack = NULL;
581 __add_ptr_list(&backup, break_stack, 0);
582 break_stack = NULL;
583 __add_ptr_list(&backup, fake_break_stack, 0);
584 fake_break_stack = NULL;
586 __add_ptr_list(&backup, switch_stack, 0);
587 switch_stack = NULL;
588 __add_ptr_list(&backup, remaining_cases, 0);
589 remaining_cases = NULL;
590 __add_ptr_list(&backup, default_stack, 0);
591 default_stack = NULL;
592 __add_ptr_list(&backup, continue_stack, 0);
593 continue_stack = NULL;
595 __add_ptr_list(&backup, goto_stack, 0);
596 goto_stack = NULL;
599 static void *pop_backup(void)
601 void *ret;
603 ret = last_ptr_list(backup);
604 delete_ptr_list_last(&backup);
605 return ret;
608 void restore_all_states(void)
610 goto_stack = pop_backup();
612 continue_stack = pop_backup();
613 default_stack = pop_backup();
614 remaining_cases = pop_backup();
615 switch_stack = pop_backup();
616 fake_break_stack = pop_backup();
617 break_stack = pop_backup();
619 fake_cur_stree_stack = pop_backup();
621 cond_false_stack = pop_backup();
622 cond_true_stack = pop_backup();
624 pre_cond_stack = pop_backup();
625 false_stack = pop_backup();
626 true_stack = pop_backup();
628 cur_stree = pop_backup();
631 void free_goto_stack(void)
633 struct named_stree *named_stree;
635 FOR_EACH_PTR(goto_stack, named_stree) {
636 free_stree(&named_stree->stree);
637 } END_FOR_EACH_PTR(named_stree);
638 __free_ptr_list((struct ptr_list **)&goto_stack);
641 void clear_all_states(void)
643 nullify_path();
644 check_stree_stack_free(&true_stack);
645 check_stree_stack_free(&false_stack);
646 check_stree_stack_free(&pre_cond_stack);
647 check_stree_stack_free(&cond_true_stack);
648 check_stree_stack_free(&cond_false_stack);
649 check_stree_stack_free(&break_stack);
650 check_stree_stack_free(&fake_break_stack);
651 check_stree_stack_free(&switch_stack);
652 check_stree_stack_free(&continue_stack);
653 check_stree_stack_free(&fake_cur_stree_stack);
655 free_goto_stack();
657 free_every_single_sm_state();
660 void __push_cond_stacks(void)
662 push_stree(&cond_true_stack, NULL);
663 push_stree(&cond_false_stack, NULL);
664 __push_fake_cur_stree();
667 void __fold_in_set_states(void)
669 struct stree *new_states;
670 struct sm_state *sm;
672 new_states = __pop_fake_cur_stree();
673 FOR_EACH_SM(new_states, sm) {
674 __set_sm(sm);
675 __set_true_false_sm(sm, sm);
676 } END_FOR_EACH_SM(sm);
677 free_stree(&new_states);
680 void __free_set_states(void)
682 struct stree *new_states;
684 new_states = __pop_fake_cur_stree();
685 free_stree(&new_states);
688 struct stree *__copy_cond_true_states(void)
690 struct stree *ret;
692 ret = pop_stree(&cond_true_stack);
693 push_stree(&cond_true_stack, clone_stree(ret));
694 return ret;
697 struct stree *__copy_cond_false_states(void)
699 struct stree *ret;
701 ret = pop_stree(&cond_false_stack);
702 push_stree(&cond_false_stack, clone_stree(ret));
703 return ret;
706 struct stree *__pop_cond_true_stack(void)
708 return pop_stree(&cond_true_stack);
711 struct stree *__pop_cond_false_stack(void)
713 return pop_stree(&cond_false_stack);
717 * This combines the pre cond states with either the true or false states.
718 * For example:
719 * a = kmalloc() ; if (a !! foo(a)
720 * In the pre state a is possibly null. In the true state it is non null.
721 * In the false state it is null. Combine the pre and the false to get
722 * that when we call 'foo', 'a' is null.
724 static void __use_cond_stack(struct stree_stack **stack)
726 struct stree *stree;
728 free_stree(&cur_stree);
730 cur_stree = pop_stree(&pre_cond_stack);
731 push_stree(&pre_cond_stack, clone_stree(cur_stree));
733 stree = pop_stree(stack);
734 overwrite_stree(stree, &cur_stree);
735 push_stree(stack, stree);
738 void __use_pre_cond_states(void)
740 free_stree(&cur_stree);
741 cur_stree = pop_stree(&pre_cond_stack);
744 void __use_cond_true_states(void)
746 __use_cond_stack(&cond_true_stack);
749 void __use_cond_false_states(void)
751 __use_cond_stack(&cond_false_stack);
754 void __negate_cond_stacks(void)
756 struct stree *old_false, *old_true;
758 __use_cond_stack(&cond_false_stack);
759 old_false = pop_stree(&cond_false_stack);
760 old_true = pop_stree(&cond_true_stack);
761 push_stree(&cond_false_stack, old_true);
762 push_stree(&cond_true_stack, old_false);
765 void __and_cond_states(void)
767 and_stree_stack(&cond_true_stack);
768 or_stree_stack(&pre_cond_stack, cur_stree, &cond_false_stack);
771 void __or_cond_states(void)
773 or_stree_stack(&pre_cond_stack, cur_stree, &cond_true_stack);
774 and_stree_stack(&cond_false_stack);
777 void __save_pre_cond_states(void)
779 push_stree(&pre_cond_stack, clone_stree(cur_stree));
782 void __discard_pre_cond_states(void)
784 struct stree *tmp;
786 tmp = pop_stree(&pre_cond_stack);
787 free_stree(&tmp);
790 struct stree *__get_true_states(void)
792 return clone_stree(top_stree(cond_true_stack));
795 struct stree *__get_false_states(void)
797 return clone_stree(top_stree(cond_false_stack));
800 void __use_cond_states(void)
802 struct stree *pre, *pre_clone, *true_states, *false_states;
804 pre = pop_stree(&pre_cond_stack);
805 pre_clone = clone_stree(pre);
807 true_states = pop_stree(&cond_true_stack);
808 overwrite_stree(true_states, &pre);
809 free_stree(&true_states);
810 /* we use the true states right away */
811 free_stree(&cur_stree);
812 cur_stree = pre;
814 false_states = pop_stree(&cond_false_stack);
815 overwrite_stree(false_states, &pre_clone);
816 free_stree(&false_states);
817 push_stree(&false_stack, pre_clone);
820 void __push_true_states(void)
822 push_stree(&true_stack, clone_stree(cur_stree));
825 void __use_false_states(void)
827 free_stree(&cur_stree);
828 cur_stree = pop_stree(&false_stack);
831 void __discard_false_states(void)
833 struct stree *stree;
835 stree = pop_stree(&false_stack);
836 free_stree(&stree);
839 void __merge_false_states(void)
841 struct stree *stree;
843 stree = pop_stree(&false_stack);
844 merge_stree(&cur_stree, stree);
845 free_stree(&stree);
849 * This function probably seemed common sensical when I wrote it but, oh wow,
850 * does it look subtle in retrospect. Say we set a state on one side of the if
851 * else path but not on the other, then what we should record in the fake stree
852 * is the merged state.
854 * This function relies on the fact that the we always set the cur_stree as well
855 * and we already have the infrastructure to merge things correctly into the
856 * cur_stree.
858 * So instead of merging fake strees together which is probably a lot of work,
859 * we just use it as a list of set states and look up the actual current values
860 * in the cur_stree.
863 static void update_stree_with_merged(struct stree **stree)
865 struct state_list *slist = NULL;
866 struct sm_state *sm, *new;
868 FOR_EACH_SM(*stree, sm) {
869 new = get_sm_state(sm->owner, sm->name, sm->sym);
870 if (!new) /* This can happen if we go out of scope */
871 continue;
872 add_ptr_list(&slist, new);
873 } END_FOR_EACH_SM(sm);
875 FOR_EACH_PTR(slist, sm) {
876 overwrite_sm_state_stree(stree, sm);
877 } END_FOR_EACH_PTR(sm);
879 free_slist(&slist);
882 static void update_fake_stree_with_merged(void)
884 struct stree *stree;
886 if (!fake_cur_stree_stack)
887 return;
888 stree = pop_stree(&fake_cur_stree_stack);
889 update_stree_with_merged(&stree);
890 push_stree(&fake_cur_stree_stack, stree);
893 void __merge_true_states(void)
895 struct stree *stree;
897 stree = pop_stree(&true_stack);
898 merge_stree(&cur_stree, stree);
899 update_fake_stree_with_merged();
900 free_stree(&stree);
903 void __push_continues(void)
905 push_stree(&continue_stack, NULL);
908 void __discard_continues(void)
910 struct stree *stree;
912 stree = pop_stree(&continue_stack);
913 free_stree(&stree);
916 void __process_continues(void)
918 struct stree *stree;
920 stree = pop_stree(&continue_stack);
921 if (!stree)
922 stree = clone_stree(cur_stree);
923 else
924 merge_stree(&stree, cur_stree);
926 push_stree(&continue_stack, stree);
929 void __merge_continues(void)
931 struct stree *stree;
933 stree = pop_stree(&continue_stack);
934 merge_stree(&cur_stree, stree);
935 free_stree(&stree);
938 void __push_breaks(void)
940 push_stree(&break_stack, NULL);
941 if (fake_cur_stree_stack)
942 push_stree(&fake_break_stack, NULL);
945 void __process_breaks(void)
947 struct stree *stree;
949 stree = pop_stree(&break_stack);
950 if (!stree)
951 stree = clone_stree(cur_stree);
952 else
953 merge_stree(&stree, cur_stree);
954 push_stree(&break_stack, stree);
956 if (!fake_cur_stree_stack)
957 return;
959 stree = pop_stree(&fake_break_stack);
960 if (!stree)
961 stree = clone_stree(top_stree(fake_cur_stree_stack));
962 else
963 merge_stree(&stree, top_stree(fake_cur_stree_stack));
964 push_stree(&fake_break_stack, stree);
967 int __has_breaks(void)
969 struct stree *stree;
970 int ret;
972 stree = pop_stree(&break_stack);
973 ret = !!stree;
974 push_stree(&break_stack, stree);
975 return ret;
978 void __merge_breaks(void)
980 struct stree *stree;
981 struct sm_state *sm;
983 stree = pop_stree(&break_stack);
984 merge_stree(&cur_stree, stree);
985 free_stree(&stree);
987 if (!fake_cur_stree_stack)
988 return;
990 stree = pop_stree(&fake_break_stack);
991 update_stree_with_merged(&stree);
992 FOR_EACH_SM(stree, sm) {
993 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
994 } END_FOR_EACH_SM(sm);
995 free_stree(&stree);
998 void __use_breaks(void)
1000 struct stree *stree;
1001 struct sm_state *sm;
1003 free_stree(&cur_stree);
1004 cur_stree = pop_stree(&break_stack);
1006 if (!fake_cur_stree_stack)
1007 return;
1008 stree = pop_stree(&fake_break_stack);
1009 FOR_EACH_SM(stree, sm) {
1010 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
1011 } END_FOR_EACH_SM(sm);
1012 free_stree(&stree);
1017 void __save_switch_states(struct expression *switch_expr)
1019 struct range_list *rl;
1021 get_absolute_rl(switch_expr, &rl);
1023 push_rl(&remaining_cases, rl);
1024 push_stree(&switch_stack, clone_stree(cur_stree));
1027 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl)
1029 struct stree *stree;
1030 struct stree *implied_stree;
1032 stree = pop_stree(&switch_stack);
1033 implied_stree = __implied_case_stree(switch_expr, case_rl, &remaining_cases, &stree);
1034 merge_stree(&cur_stree, implied_stree);
1035 free_stree(&implied_stree);
1036 push_stree(&switch_stack, stree);
1039 void __discard_switches(void)
1041 struct stree *stree;
1043 pop_rl(&remaining_cases);
1044 stree = pop_stree(&switch_stack);
1045 free_stree(&stree);
1048 void __push_default(void)
1050 push_stree(&default_stack, NULL);
1053 void __set_default(void)
1055 set_state_stree_stack(&default_stack, 0, "has_default", NULL, &true_state);
1058 int __pop_default(void)
1060 struct stree *stree;
1062 stree = pop_stree(&default_stack);
1063 if (stree) {
1064 free_stree(&stree);
1065 return 1;
1067 return 0;
1070 static struct named_stree *alloc_named_stree(const char *name, struct symbol *sym, struct stree *stree)
1072 struct named_stree *named_stree = __alloc_named_stree(0);
1074 named_stree->name = (char *)name;
1075 named_stree->stree = stree;
1076 named_stree->sym = sym;
1077 return named_stree;
1080 void __save_gotos(const char *name, struct symbol *sym)
1082 struct stree **stree;
1083 struct stree *clone;
1085 stree = get_named_stree(goto_stack, name, sym);
1086 if (stree) {
1087 merge_stree(stree, cur_stree);
1088 return;
1089 } else {
1090 struct named_stree *named_stree;
1092 clone = clone_stree(cur_stree);
1093 named_stree = alloc_named_stree(name, sym, clone);
1094 add_ptr_list(&goto_stack, named_stree);
1098 void __merge_gotos(const char *name, struct symbol *sym)
1100 struct stree **stree;
1102 stree = get_named_stree(goto_stack, name, sym);
1103 if (stree)
1104 merge_stree(&cur_stree, *stree);