whitespace: remove some blank lines
[smatch.git] / smatch_states.c
blob8c180e50cf837dfc7bbb4af62ce90d4e2dccca52
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 *switch_stack;
60 static struct range_list_stack *remaining_cases;
61 static struct stree_stack *default_stack;
62 static struct stree_stack *continue_stack;
64 static struct named_stree_stack *goto_stack;
66 static struct ptr_list *backup;
68 int option_debug;
70 void __print_cur_stree(void)
72 __print_stree(cur_stree);
75 int unreachable(void)
77 if (!cur_stree)
78 return 1;
79 return 0;
82 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
84 struct sm_state *ret;
86 if (!name)
87 return NULL;
89 if (read_only)
90 sm_msg("Smatch Internal Error: cur_stree is read only.");
92 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
93 struct smatch_state *s;
95 s = get_state(owner, name, sym);
96 if (!s)
97 sm_msg("%s new [%s] '%s' %s", __func__,
98 check_name(owner), name, show_state(state));
99 else
100 sm_msg("%s change [%s] '%s' %s => %s",
101 __func__, check_name(owner), name, show_state(s),
102 show_state(state));
105 if (owner != -1 && unreachable())
106 return NULL;
108 if (fake_cur_stree_stack)
109 set_state_stree_stack(&fake_cur_stree_stack, owner, name, sym, state);
111 ret = set_state_stree(&cur_stree, owner, name, sym, state);
113 return ret;
116 struct sm_state *set_state_expr(int owner, struct expression *expr, struct smatch_state *state)
118 char *name;
119 struct symbol *sym;
120 struct sm_state *ret = NULL;
122 expr = strip_expr(expr);
123 name = expr_to_var_sym(expr, &sym);
124 if (!name || !sym)
125 goto free;
126 ret = set_state(owner, name, sym, state);
127 free:
128 free_string(name);
129 return ret;
132 void __swap_cur_stree(struct stree *stree)
134 free_stree(&cur_stree);
135 cur_stree = stree;
138 void __push_fake_cur_stree(void)
140 push_stree(&fake_cur_stree_stack, NULL);
141 __save_pre_cond_states();
144 struct stree *__pop_fake_cur_stree(void)
146 if (!fake_cur_stree_stack)
147 sm_msg("INTERNAL error: popping too many fake cur strees.");
148 __use_pre_cond_states();
149 return pop_stree(&fake_cur_stree_stack);
152 void __free_fake_cur_stree(void)
154 struct stree *stree;
156 stree = __pop_fake_cur_stree();
157 free_stree(&stree);
160 void __set_fake_cur_stree_fast(struct stree *stree)
162 push_stree(&pre_cond_stack, cur_stree);
163 cur_stree = stree;
164 read_only = 1;
167 void __pop_fake_cur_stree_fast(void)
169 cur_stree = pop_stree(&pre_cond_stack);
170 read_only = 0;
173 void __merge_stree_into_cur(struct stree *stree)
175 struct sm_state *sm;
176 struct sm_state *orig;
177 struct sm_state *merged;
179 FOR_EACH_SM(stree, sm) {
180 orig = get_sm_state(sm->owner, sm->name, sm->sym);
181 if (orig)
182 merged = merge_sm_states(orig, sm);
183 else
184 merged = sm;
185 __set_sm(merged);
186 } END_FOR_EACH_SM(sm);
189 void __set_sm(struct sm_state *sm)
191 if (read_only)
192 sm_msg("Smatch Internal Error: cur_stree is read only.");
194 if (option_debug ||
195 strcmp(check_name(sm->owner), option_debug_check) == 0) {
196 struct smatch_state *s;
198 s = get_state(sm->owner, sm->name, sm->sym);
199 if (!s)
200 sm_msg("%s new %s", __func__, show_sm(sm));
201 else
202 sm_msg("%s change %s (was %s)", __func__, show_sm(sm),
203 show_state(s));
206 if (unreachable())
207 return;
209 if (fake_cur_stree_stack)
210 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
212 overwrite_sm_state_stree(&cur_stree, sm);
215 void __set_sm_cur_stree(struct sm_state *sm)
217 if (read_only)
218 sm_msg("Smatch Internal Error: cur_stree is read only.");
220 if (option_debug ||
221 strcmp(check_name(sm->owner), option_debug_check) == 0) {
222 struct smatch_state *s;
224 s = get_state(sm->owner, sm->name, sm->sym);
225 if (!s)
226 sm_msg("%s new %s", __func__, show_sm(sm));
227 else
228 sm_msg("%s change %s (was %s)",
229 __func__, show_sm(sm), show_state(s));
232 if (unreachable())
233 return;
235 overwrite_sm_state_stree(&cur_stree, sm);
238 void __set_sm_fake_stree(struct sm_state *sm)
240 if (read_only)
241 sm_msg("Smatch Internal Error: cur_stree is read only.");
243 if (option_debug ||
244 strcmp(check_name(sm->owner), option_debug_check) == 0) {
245 struct smatch_state *s;
247 s = get_state(sm->owner, sm->name, sm->sym);
248 if (!s)
249 sm_msg("%s new %s", __func__, show_sm(sm));
250 else
251 sm_msg("%s change %s (was %s)",
252 __func__, show_sm(sm), show_state(s));
255 if (unreachable())
256 return;
258 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
261 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
263 __get_state_hook(owner, name, sym);
265 return get_state_stree(cur_stree, owner, name, sym);
268 struct smatch_state *get_state_expr(int owner, struct expression *expr)
270 char *name;
271 struct symbol *sym;
272 struct smatch_state *ret = NULL;
274 expr = strip_expr(expr);
275 name = expr_to_var_sym(expr, &sym);
276 if (!name || !sym)
277 goto free;
278 ret = get_state(owner, name, sym);
279 free:
280 free_string(name);
281 return ret;
284 struct state_list *get_possible_states(int owner, const char *name, struct symbol *sym)
286 struct sm_state *sms;
288 sms = get_sm_state_stree(cur_stree, owner, name, sym);
289 if (sms)
290 return sms->possible;
291 return NULL;
294 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
296 char *name;
297 struct symbol *sym;
298 struct state_list *ret = NULL;
300 expr = strip_expr(expr);
301 name = expr_to_var_sym(expr, &sym);
302 if (!name || !sym)
303 goto free;
304 ret = get_possible_states(owner, name, sym);
305 free:
306 free_string(name);
307 return ret;
310 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
312 return get_sm_state_stree(cur_stree, owner, name, sym);
315 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
317 char *name;
318 struct symbol *sym;
319 struct sm_state *ret = NULL;
321 expr = strip_expr(expr);
322 name = expr_to_var_sym(expr, &sym);
323 if (!name || !sym)
324 goto free;
325 ret = get_sm_state(owner, name, sym);
326 free:
327 free_string(name);
328 return ret;
331 void delete_state(int owner, const char *name, struct symbol *sym)
333 delete_state_stree(&cur_stree, owner, name, sym);
334 if (cond_true_stack) {
335 delete_state_stree_stack(&pre_cond_stack, owner, name, sym);
336 delete_state_stree_stack(&cond_true_stack, owner, name, sym);
337 delete_state_stree_stack(&cond_false_stack, owner, name, sym);
341 void delete_state_expr(int owner, struct expression *expr)
343 char *name;
344 struct symbol *sym;
346 expr = strip_expr(expr);
347 name = expr_to_var_sym(expr, &sym);
348 if (!name || !sym)
349 goto free;
350 delete_state(owner, name, sym);
351 free:
352 free_string(name);
355 static void delete_all_states_stree_sym(struct stree **stree, struct symbol *sym)
357 struct state_list *slist = NULL;
358 struct sm_state *sm;
360 FOR_EACH_SM(*stree, sm) {
361 if (sm->sym == sym)
362 add_ptr_list(&slist, sm);
363 } END_FOR_EACH_SM(sm);
365 FOR_EACH_PTR(slist, sm) {
366 delete_state_stree(stree, sm->owner, sm->name, sm->sym);
367 } END_FOR_EACH_PTR(sm);
369 free_slist(&slist);
372 static void delete_all_states_stree_stack_sym(struct stree_stack **stack, struct symbol *sym)
374 struct stree *stree;
376 if (!*stack)
377 return;
379 stree = pop_stree(stack);
380 delete_all_states_stree_sym(&stree, sym);
381 push_stree(stack, stree);
384 void __delete_all_states_sym(struct symbol *sym)
386 delete_all_states_stree_sym(&cur_stree, sym);
388 delete_all_states_stree_stack_sym(&true_stack, sym);
389 delete_all_states_stree_stack_sym(&true_stack, sym);
390 delete_all_states_stree_stack_sym(&false_stack, sym);
391 delete_all_states_stree_stack_sym(&pre_cond_stack, sym);
392 delete_all_states_stree_stack_sym(&cond_true_stack, sym);
393 delete_all_states_stree_stack_sym(&cond_false_stack, sym);
394 delete_all_states_stree_stack_sym(&fake_cur_stree_stack, sym);
395 delete_all_states_stree_stack_sym(&break_stack, sym);
396 delete_all_states_stree_stack_sym(&switch_stack, sym);
397 delete_all_states_stree_stack_sym(&continue_stack, sym);
400 * deleting from the goto stack is problematic because we don't know
401 * if the label is in scope and also we need the value for --two-passes.
405 struct stree *get_all_states_from_stree(int owner, struct stree *source)
407 struct stree *ret = NULL;
408 struct sm_state *tmp;
410 FOR_EACH_SM(source, tmp) {
411 if (tmp->owner == owner)
412 avl_insert(&ret, tmp);
413 } END_FOR_EACH_SM(tmp);
415 return ret;
418 struct stree *get_all_states_stree(int owner)
420 return get_all_states_from_stree(owner, cur_stree);
423 struct stree *__get_cur_stree(void)
425 return cur_stree;
428 int is_reachable(void)
430 if (cur_stree)
431 return 1;
432 return 0;
435 void set_true_false_states(int owner, const char *name, struct symbol *sym,
436 struct smatch_state *true_state,
437 struct smatch_state *false_state)
439 if (read_only)
440 sm_msg("Smatch Internal Error: cur_stree is read only.");
442 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
443 struct smatch_state *tmp;
445 tmp = get_state(owner, name, sym);
446 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__,
447 check_name(owner), name, show_state(tmp),
448 show_state(true_state), show_state(false_state));
451 if (unreachable())
452 return;
454 if (!cond_false_stack || !cond_true_stack) {
455 printf("Error: missing true/false stacks\n");
456 return;
459 if (true_state)
460 set_state_stree_stack(&cond_true_stack, owner, name, sym, true_state);
461 if (false_state)
462 set_state_stree_stack(&cond_false_stack, owner, name, sym, false_state);
465 void set_true_false_states_expr(int owner, struct expression *expr,
466 struct smatch_state *true_state,
467 struct smatch_state *false_state)
469 char *name;
470 struct symbol *sym;
472 expr = strip_expr(expr);
473 name = expr_to_var_sym(expr, &sym);
474 if (!name || !sym)
475 goto free;
476 set_true_false_states(owner, name, sym, true_state, false_state);
477 free:
478 free_string(name);
481 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
483 int owner;
484 const char *name;
485 struct symbol *sym;
487 if (!true_sm && !false_sm)
488 return;
490 if (unreachable())
491 return;
493 owner = true_sm ? true_sm->owner : false_sm->owner;
494 name = true_sm ? true_sm->name : false_sm->name;
495 sym = true_sm ? true_sm->sym : false_sm->sym;
496 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
497 struct smatch_state *tmp;
499 tmp = get_state(owner, name, sym);
500 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__,
501 check_name(owner), name, show_state(tmp),
502 show_state(true_sm ? true_sm->state : NULL),
503 show_state(false_sm ? false_sm->state : NULL));
506 if (!cond_false_stack || !cond_true_stack) {
507 printf("Error: missing true/false stacks\n");
508 return;
511 if (true_sm)
512 overwrite_sm_state_stree_stack(&cond_true_stack, true_sm);
513 if (false_sm)
514 overwrite_sm_state_stree_stack(&cond_false_stack, false_sm);
517 void nullify_path(void)
519 free_stree(&cur_stree);
522 void __match_nullify_path_hook(const char *fn, struct expression *expr,
523 void *unused)
525 nullify_path();
529 * At the start of every function we mark the path
530 * as unnull. That way there is always at least one state
531 * in the cur_stree until nullify_path is called. This
532 * is used in merge_slist() for the first null check.
534 void __unnullify_path(void)
536 if (!cur_stree)
537 set_state(-1, "unnull_path", NULL, &true_state);
540 int __path_is_null(void)
542 if (cur_stree)
543 return 0;
544 return 1;
547 static void check_stree_stack_free(struct stree_stack **stack)
549 if (*stack) {
550 sm_msg("smatch internal error: stack not empty");
551 free_stack_and_strees(stack);
555 void save_all_states(void)
557 __add_ptr_list(&backup, cur_stree, 0);
558 cur_stree = NULL;
560 __add_ptr_list(&backup, true_stack, 0);
561 true_stack = NULL;
562 __add_ptr_list(&backup, false_stack, 0);
563 false_stack = NULL;
564 __add_ptr_list(&backup, pre_cond_stack, 0);
565 pre_cond_stack = NULL;
567 __add_ptr_list(&backup, cond_true_stack, 0);
568 cond_true_stack = NULL;
569 __add_ptr_list(&backup, cond_false_stack, 0);
570 cond_false_stack = NULL;
572 __add_ptr_list(&backup, fake_cur_stree_stack, 0);
573 fake_cur_stree_stack = NULL;
575 __add_ptr_list(&backup, break_stack, 0);
576 break_stack = NULL;
577 __add_ptr_list(&backup, switch_stack, 0);
578 switch_stack = NULL;
579 __add_ptr_list(&backup, remaining_cases, 0);
580 remaining_cases = NULL;
581 __add_ptr_list(&backup, default_stack, 0);
582 default_stack = NULL;
583 __add_ptr_list(&backup, continue_stack, 0);
584 continue_stack = NULL;
586 __add_ptr_list(&backup, goto_stack, 0);
587 goto_stack = NULL;
590 static void *pop_backup(void)
592 void *ret;
594 ret = last_ptr_list(backup);
595 delete_ptr_list_last(&backup);
596 return ret;
599 void restore_all_states(void)
601 goto_stack = pop_backup();
603 continue_stack = pop_backup();
604 default_stack = pop_backup();
605 remaining_cases = pop_backup();
606 switch_stack = pop_backup();
607 break_stack = pop_backup();
609 fake_cur_stree_stack = pop_backup();
611 cond_false_stack = pop_backup();
612 cond_true_stack = pop_backup();
614 pre_cond_stack = pop_backup();
615 false_stack = pop_backup();
616 true_stack = pop_backup();
618 cur_stree = pop_backup();
621 void free_goto_stack(void)
623 struct named_stree *named_stree;
625 FOR_EACH_PTR(goto_stack, named_stree) {
626 free_stree(&named_stree->stree);
627 } END_FOR_EACH_PTR(named_stree);
628 __free_ptr_list((struct ptr_list **)&goto_stack);
631 void clear_all_states(void)
633 nullify_path();
634 check_stree_stack_free(&true_stack);
635 check_stree_stack_free(&false_stack);
636 check_stree_stack_free(&pre_cond_stack);
637 check_stree_stack_free(&cond_true_stack);
638 check_stree_stack_free(&cond_false_stack);
639 check_stree_stack_free(&break_stack);
640 check_stree_stack_free(&switch_stack);
641 check_stree_stack_free(&continue_stack);
642 check_stree_stack_free(&fake_cur_stree_stack);
644 free_goto_stack();
646 free_every_single_sm_state();
649 void __push_cond_stacks(void)
651 push_stree(&cond_true_stack, NULL);
652 push_stree(&cond_false_stack, NULL);
653 __push_fake_cur_stree();
656 void __fold_in_set_states(void)
658 struct stree *new_states;
659 struct sm_state *sm;
661 new_states = __pop_fake_cur_stree();
662 FOR_EACH_SM(new_states, sm) {
663 __set_sm(sm);
664 __set_true_false_sm(sm, sm);
665 } END_FOR_EACH_SM(sm);
666 free_stree(&new_states);
669 void __free_set_states(void)
671 struct stree *new_states;
673 new_states = __pop_fake_cur_stree();
674 free_stree(&new_states);
677 struct stree *__copy_cond_true_states(void)
679 struct stree *ret;
681 ret = pop_stree(&cond_true_stack);
682 push_stree(&cond_true_stack, clone_stree(ret));
683 return ret;
686 struct stree *__copy_cond_false_states(void)
688 struct stree *ret;
690 ret = pop_stree(&cond_false_stack);
691 push_stree(&cond_false_stack, clone_stree(ret));
692 return ret;
695 struct stree *__pop_cond_true_stack(void)
697 return pop_stree(&cond_true_stack);
700 struct stree *__pop_cond_false_stack(void)
702 return pop_stree(&cond_false_stack);
706 * This combines the pre cond states with either the true or false states.
707 * For example:
708 * a = kmalloc() ; if (a !! foo(a)
709 * In the pre state a is possibly null. In the true state it is non null.
710 * In the false state it is null. Combine the pre and the false to get
711 * that when we call 'foo', 'a' is null.
713 static void __use_cond_stack(struct stree_stack **stack)
715 struct stree *stree;
717 free_stree(&cur_stree);
719 cur_stree = pop_stree(&pre_cond_stack);
720 push_stree(&pre_cond_stack, clone_stree(cur_stree));
722 stree = pop_stree(stack);
723 overwrite_stree(stree, &cur_stree);
724 push_stree(stack, stree);
727 void __use_pre_cond_states(void)
729 free_stree(&cur_stree);
730 cur_stree = pop_stree(&pre_cond_stack);
733 void __use_cond_true_states(void)
735 __use_cond_stack(&cond_true_stack);
738 void __use_cond_false_states(void)
740 __use_cond_stack(&cond_false_stack);
743 void __negate_cond_stacks(void)
745 struct stree *old_false, *old_true;
747 __use_cond_stack(&cond_false_stack);
748 old_false = pop_stree(&cond_false_stack);
749 old_true = pop_stree(&cond_true_stack);
750 push_stree(&cond_false_stack, old_true);
751 push_stree(&cond_true_stack, old_false);
754 void __and_cond_states(void)
756 and_stree_stack(&cond_true_stack);
757 or_stree_stack(&pre_cond_stack, cur_stree, &cond_false_stack);
760 void __or_cond_states(void)
762 or_stree_stack(&pre_cond_stack, cur_stree, &cond_true_stack);
763 and_stree_stack(&cond_false_stack);
766 void __save_pre_cond_states(void)
768 push_stree(&pre_cond_stack, clone_stree(cur_stree));
771 void __discard_pre_cond_states(void)
773 struct stree *tmp;
775 tmp = pop_stree(&pre_cond_stack);
776 free_stree(&tmp);
779 struct stree *__get_true_states(void)
781 return clone_stree(top_stree(cond_true_stack));
784 struct stree *__get_false_states(void)
786 return clone_stree(top_stree(cond_false_stack));
789 void __use_cond_states(void)
791 struct stree *pre, *pre_clone, *true_states, *false_states;
793 pre = pop_stree(&pre_cond_stack);
794 pre_clone = clone_stree(pre);
796 true_states = pop_stree(&cond_true_stack);
797 overwrite_stree(true_states, &pre);
798 free_stree(&true_states);
799 /* we use the true states right away */
800 free_stree(&cur_stree);
801 cur_stree = pre;
803 false_states = pop_stree(&cond_false_stack);
804 overwrite_stree(false_states, &pre_clone);
805 free_stree(&false_states);
806 push_stree(&false_stack, pre_clone);
809 void __push_true_states(void)
811 push_stree(&true_stack, clone_stree(cur_stree));
814 void __use_false_states(void)
816 free_stree(&cur_stree);
817 cur_stree = pop_stree(&false_stack);
820 void __discard_false_states(void)
822 struct stree *stree;
824 stree = pop_stree(&false_stack);
825 free_stree(&stree);
828 void __merge_false_states(void)
830 struct stree *stree;
832 stree = pop_stree(&false_stack);
833 merge_stree(&cur_stree, stree);
834 free_stree(&stree);
837 static void update_fake_stree_with_merged(void)
839 struct stree *stree;
840 struct state_list *slist = NULL;
841 struct sm_state *sm, *new;
843 if (!fake_cur_stree_stack)
844 return;
845 stree = pop_stree(&fake_cur_stree_stack);
846 FOR_EACH_SM(stree, sm) {
847 new = get_sm_state(sm->owner, sm->name, sm->sym);
848 if (!new)
849 continue;
850 add_ptr_list(&slist, new);
851 } END_FOR_EACH_SM(sm);
853 FOR_EACH_PTR(slist, sm) {
854 overwrite_sm_state_stree(&stree, sm);
855 } END_FOR_EACH_PTR(sm);
857 free_slist(&slist);
858 push_stree(&fake_cur_stree_stack, stree);
861 void __merge_true_states(void)
863 struct stree *stree;
865 stree = pop_stree(&true_stack);
866 merge_stree(&cur_stree, stree);
867 update_fake_stree_with_merged();
868 free_stree(&stree);
871 void __push_continues(void)
873 push_stree(&continue_stack, NULL);
876 void __discard_continues(void)
878 struct stree *stree;
880 stree = pop_stree(&continue_stack);
881 free_stree(&stree);
884 void __process_continues(void)
886 struct stree *stree;
888 stree = pop_stree(&continue_stack);
889 if (!stree)
890 stree = clone_stree(cur_stree);
891 else
892 merge_stree(&stree, cur_stree);
894 push_stree(&continue_stack, stree);
897 void __merge_continues(void)
899 struct stree *stree;
901 stree = pop_stree(&continue_stack);
902 merge_stree(&cur_stree, stree);
903 free_stree(&stree);
906 void __push_breaks(void)
908 push_stree(&break_stack, NULL);
911 void __process_breaks(void)
913 struct stree *stree;
915 stree = pop_stree(&break_stack);
916 if (!stree)
917 stree = clone_stree(cur_stree);
918 else
919 merge_stree(&stree, cur_stree);
921 push_stree(&break_stack, stree);
924 int __has_breaks(void)
926 struct stree *stree;
927 int ret;
929 stree = pop_stree(&break_stack);
930 ret = !!stree;
931 push_stree(&break_stack, stree);
932 return ret;
935 void __merge_breaks(void)
937 struct stree *stree;
939 stree = pop_stree(&break_stack);
940 merge_stree(&cur_stree, stree);
941 free_stree(&stree);
944 void __use_breaks(void)
946 free_stree(&cur_stree);
947 cur_stree = pop_stree(&break_stack);
950 void __save_switch_states(struct expression *switch_expr)
952 struct range_list *rl;
954 get_absolute_rl(switch_expr, &rl);
956 push_rl(&remaining_cases, rl);
957 push_stree(&switch_stack, clone_stree(cur_stree));
960 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl)
962 struct stree *stree;
963 struct stree *implied_stree;
965 stree = pop_stree(&switch_stack);
966 implied_stree = __implied_case_stree(switch_expr, case_rl, &remaining_cases, &stree);
967 merge_stree(&cur_stree, implied_stree);
968 free_stree(&implied_stree);
969 push_stree(&switch_stack, stree);
972 void __discard_switches(void)
974 struct stree *stree;
976 pop_rl(&remaining_cases);
977 stree = pop_stree(&switch_stack);
978 free_stree(&stree);
981 void __push_default(void)
983 push_stree(&default_stack, NULL);
986 void __set_default(void)
988 set_state_stree_stack(&default_stack, 0, "has_default", NULL, &true_state);
991 int __pop_default(void)
993 struct stree *stree;
995 stree = pop_stree(&default_stack);
996 if (stree) {
997 free_stree(&stree);
998 return 1;
1000 return 0;
1003 static struct named_stree *alloc_named_stree(const char *name, struct stree *stree)
1005 struct named_stree *named_stree = __alloc_named_stree(0);
1007 named_stree->name = (char *)name;
1008 named_stree->stree = stree;
1009 return named_stree;
1012 void __save_gotos(const char *name)
1014 struct stree **stree;
1015 struct stree *clone;
1017 stree = get_named_stree(goto_stack, name);
1018 if (stree) {
1019 merge_stree(stree, cur_stree);
1020 return;
1021 } else {
1022 struct named_stree *named_stree;
1024 clone = clone_stree(cur_stree);
1025 named_stree = alloc_named_stree(name, clone);
1026 add_ptr_list(&goto_stack, named_stree);
1030 void __merge_gotos(const char *name)
1032 struct stree **stree;
1034 stree = get_named_stree(goto_stack, name);
1035 if (stree)
1036 merge_stree(&cur_stree, *stree);