param_set: introduce param_was_set_var_sym()
[smatch.git] / smatch_implied.c
blobb9ab16ec97bcdec08138c02292fe739615acf1a7
1 /*
2 * Copyright (C) 2008 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 * Imagine we have this code:
20 * foo = 1;
21 * if (bar)
22 * foo = 99;
23 * else
24 * frob();
25 * // <-- point #1
26 * if (foo == 99) // <-- point #2
27 * bar->baz; // <-- point #3
30 * At point #3 bar is non null and can be dereferenced.
32 * It's smatch_implied.c which sets bar to non null at point #2.
34 * At point #1 merge_slist() stores the list of states from both
35 * the true and false paths. On the true path foo == 99 and on
36 * the false path foo == 1. merge_slist() sets their pool
37 * list to show the other states which were there when foo == 99.
39 * When it comes to the if (foo == 99) the smatch implied hook
40 * looks for all the pools where foo was not 99. It makes a list
41 * of those.
43 * Then for bar (and all the other states) it says, ok bar is a
44 * merged state that came from these previous states. We'll
45 * chop out all the states where it came from a pool where
46 * foo != 99 and merge it all back together.
48 * That is the implied state of bar.
50 * merge_slist() sets up ->pool. An sm_state only has one ->pool and
51 * that is the pool where it was first set. The my pool gets set when
52 * code paths merge. States that have been set since the last merge do
53 * not have a ->pool.
54 * merge_sm_state() sets ->left and ->right. (These are the states which were
55 * merged to form the current state.)
56 * a pool: a pool is an slist that has been merged with another slist.
59 #include <sys/time.h>
60 #include <time.h>
61 #include "smatch.h"
62 #include "smatch_slist.h"
63 #include "smatch_extra.h"
65 char *implied_debug_msg;
66 #define DIMPLIED(msg...) do { if (option_debug_implied) printf(msg); } while (0)
68 int option_debug_implied = 0;
71 * tmp_range_list():
72 * It messes things up to free range list allocations. This helper fuction
73 * lets us reuse memory instead of doing new allocations.
75 static struct range_list *tmp_range_list(struct symbol *type, long long num)
77 static struct range_list *my_list = NULL;
78 static struct data_range *my_range;
80 __free_ptr_list((struct ptr_list **)&my_list);
81 my_range = alloc_range(ll_to_sval(num), ll_to_sval(num));
82 add_ptr_list(&my_list, my_range);
83 return my_list;
86 static void print_debug_tf(struct sm_state *sm, int istrue, int isfalse)
88 if (!option_debug_implied && !option_debug)
89 return;
91 if (istrue && isfalse) {
92 printf("%s: %d: does not exist.\n", show_sm(sm), sm->line);
93 } else if (istrue) {
94 printf("'%s = %s' from %d is true. %s[stree %d]\n", sm->name, show_state(sm->state),
95 sm->line, sm->merged ? "[merged]" : "[leaf]",
96 get_stree_id(sm->pool));
97 } else if (isfalse) {
98 printf("'%s = %s' from %d is false. %s[stree %d]\n", sm->name, show_state(sm->state),
99 sm->line,
100 sm->merged ? "[merged]" : "[leaf]",
101 get_stree_id(sm->pool));
102 } else {
103 printf("'%s = %s' from %d could be true or false. %s[stree %d]\n", sm->name,
104 show_state(sm->state), sm->line,
105 sm->merged ? "[merged]" : "[leaf]",
106 get_stree_id(sm->pool));
110 static int create_fake_history(struct sm_state *sm, int comparison, struct range_list *rl)
112 struct range_list *orig_rl;
113 struct range_list *true_rl, *false_rl;
114 struct stree *true_stree, *false_stree;
115 struct sm_state *true_sm, *false_sm;
116 sval_t sval;
118 if (is_merged(sm) || sm->left || sm->right)
119 return 0;
120 if (!rl_to_sval(rl, &sval))
121 return 0;
122 if (!estate_rl(sm->state))
123 return 0;
125 orig_rl = cast_rl(rl_type(rl), estate_rl(sm->state));
126 split_comparison_rl(orig_rl, comparison, rl, &true_rl, &false_rl, NULL, NULL);
128 true_rl = rl_truncate_cast(estate_type(sm->state), true_rl);
129 false_rl = rl_truncate_cast(estate_type(sm->state), false_rl);
130 if (is_whole_rl(true_rl) || is_whole_rl(false_rl) ||
131 !true_rl || !false_rl ||
132 rl_equiv(orig_rl, true_rl) || rl_equiv(orig_rl, false_rl) ||
133 rl_equiv(estate_rl(sm->state), true_rl) || rl_equiv(estate_rl(sm->state), false_rl))
134 return 0;
136 if (rl_intersection(true_rl, false_rl)) {
137 sm_msg("internal error parsing (%s (%s) %s %s)",
138 sm->name, sm->state->name, show_special(comparison), show_rl(rl));
139 sm_msg("true_rl = %s false_rl = %s intersection = %s",
140 show_rl(true_rl), show_rl(false_rl), show_rl(rl_intersection(true_rl, false_rl)));
141 return 0;
144 if (option_debug)
145 sm_msg("fake_history: %s vs %s. %s %s %s. --> T: %s F: %s",
146 sm->name, show_rl(rl), sm->state->name, show_special(comparison), show_rl(rl),
147 show_rl(true_rl), show_rl(false_rl));
149 true_sm = clone_sm(sm);
150 false_sm = clone_sm(sm);
152 true_sm->state = alloc_estate_rl(cast_rl(estate_type(sm->state), true_rl));
153 free_slist(&true_sm->possible);
154 add_possible_sm(true_sm, true_sm);
155 false_sm->state = alloc_estate_rl(cast_rl(estate_type(sm->state), false_rl));
156 free_slist(&false_sm->possible);
157 add_possible_sm(false_sm, false_sm);
159 true_stree = clone_stree(sm->pool);
160 false_stree = clone_stree(sm->pool);
162 overwrite_sm_state_stree(&true_stree, true_sm);
163 overwrite_sm_state_stree(&false_stree, false_sm);
165 true_sm->pool = true_stree;
166 false_sm->pool = false_stree;
168 sm->merged = 1;
169 sm->left = true_sm;
170 sm->right = false_sm;
172 return 1;
176 * If 'foo' == 99 add it that pool to the true pools. If it's false, add it to
177 * the false pools. If we're not sure, then we don't add it to either.
179 static void do_compare(struct sm_state *sm, int comparison, struct range_list *rl,
180 struct state_list **true_stack,
181 struct state_list **false_stack, int *mixed, struct sm_state *gate_sm)
183 struct sm_state *s;
184 int istrue;
185 int isfalse;
186 struct range_list *var_rl;
188 if (!sm->pool)
189 return;
191 if (is_implied(sm)) {
192 s = get_sm_state_stree(sm->pool,
193 sm->owner, sm->name,
194 sm->sym);
195 } else {
196 s = sm;
199 if (!s) {
200 if (option_debug_implied || option_debug)
201 sm_msg("%s from %d, has borrowed implications.",
202 sm->name, sm->line);
203 return;
206 var_rl = cast_rl(rl_type(rl), estate_rl(s->state));
208 istrue = !possibly_false_rl(var_rl, comparison, rl);
209 isfalse = !possibly_true_rl(var_rl, comparison, rl);
211 print_debug_tf(s, istrue, isfalse);
213 /* give up if we have borrowed implications (smatch_equiv.c) */
214 if (sm->sym != gate_sm->sym ||
215 strcmp(sm->name, gate_sm->name) != 0) {
216 if (mixed)
217 *mixed = 1;
220 if (mixed && !*mixed && !is_merged(sm) && !istrue && !isfalse) {
221 if (!create_fake_history(sm, comparison, rl)) {
222 if (mixed)
223 *mixed = 1;
227 if (istrue)
228 add_ptr_list(true_stack, s);
230 if (isfalse)
231 add_ptr_list(false_stack, s);
234 static int pool_in_pools(struct stree *pool,
235 const struct state_list *slist)
237 struct sm_state *tmp;
239 FOR_EACH_PTR(slist, tmp) {
240 if (!tmp->pool)
241 continue;
242 if (tmp->pool == pool)
243 return 1;
244 } END_FOR_EACH_PTR(tmp);
245 return 0;
248 static int is_checked(struct state_list *checked, struct sm_state *sm)
250 struct sm_state *tmp;
252 FOR_EACH_PTR(checked, tmp) {
253 if (tmp == sm)
254 return 1;
255 } END_FOR_EACH_PTR(tmp);
256 return 0;
260 * separate_pools():
261 * Example code: if (foo == 99) {
263 * Say 'foo' is a merged state that has many possible values. It is the combination
264 * of merges. separate_pools() iterates through the pools recursively and calls
265 * do_compare() for each time 'foo' was set.
267 static void __separate_pools(struct sm_state *sm, int comparison, struct range_list *rl,
268 struct state_list **true_stack,
269 struct state_list **false_stack,
270 struct state_list **checked, int *mixed, struct sm_state *gate_sm)
272 int free_checked = 0;
273 struct state_list *checked_states = NULL;
275 if (!sm)
276 return;
279 * If it looks like this is going to take too long as-is, then don't
280 * create even more fake history.
282 if (mixed && sm->nr_children > 100)
283 *mixed = 1;
286 Sometimes the implications are just too big to deal with
287 so we bail. Theoretically, bailing out here can cause more false
288 positives but won't hide actual bugs.
290 if (sm->nr_children > 4000) {
291 if (option_debug || option_debug_implied) {
292 static char buf[1028];
293 snprintf(buf, sizeof(buf), "debug: %s: nr_children over 4000 (%d). (%s %s)",
294 __func__, sm->nr_children, sm->name, show_state(sm->state));
295 implied_debug_msg = buf;
297 return;
300 if (checked == NULL) {
301 checked = &checked_states;
302 free_checked = 1;
304 if (is_checked(*checked, sm))
305 return;
306 add_ptr_list(checked, sm);
308 do_compare(sm, comparison, rl, true_stack, false_stack, mixed, gate_sm);
310 __separate_pools(sm->left, comparison, rl, true_stack, false_stack, checked, mixed, gate_sm);
311 __separate_pools(sm->right, comparison, rl, true_stack, false_stack, checked, mixed, gate_sm);
312 if (free_checked)
313 free_slist(checked);
316 static void separate_pools(struct sm_state *sm, int comparison, struct range_list *rl,
317 struct state_list **true_stack,
318 struct state_list **false_stack,
319 struct state_list **checked, int *mixed)
321 __separate_pools(sm, comparison, rl, true_stack, false_stack, checked, mixed, sm);
324 struct sm_state *filter_pools(struct sm_state *sm,
325 const struct state_list *remove_stack,
326 const struct state_list *keep_stack,
327 int *modified)
329 struct sm_state *ret = NULL;
330 struct sm_state *left;
331 struct sm_state *right;
332 int removed = 0;
334 if (!sm)
335 return NULL;
337 if (sm->nr_children > 4000) {
338 if (option_debug || option_debug_implied) {
339 static char buf[1028];
340 snprintf(buf, sizeof(buf), "debug: %s: nr_children over 4000 (%d). (%s %s)",
341 __func__, sm->nr_children, sm->name, show_state(sm->state));
342 implied_debug_msg = buf;
344 return NULL;
347 if (pool_in_pools(sm->pool, remove_stack)) {
348 DIMPLIED("removed %s from %d [stree %d]\n", show_sm(sm), sm->line, get_stree_id(sm->pool));
349 *modified = 1;
350 return NULL;
353 if (!is_merged(sm) || pool_in_pools(sm->pool, keep_stack)) {
354 DIMPLIED("kept %s from %d [stree %d]\n", show_sm(sm), sm->line, get_stree_id(sm->pool));
355 return sm;
358 DIMPLIED("checking %s from %d (%d) [stree %d] left = %s [stree %d] right = %s [stree %d]\n",
359 show_sm(sm), sm->line, sm->nr_children, get_stree_id(sm->pool),
360 sm->left ? show_sm(sm->left) : "<none>", sm->left ? get_stree_id(sm->left->pool) : -1,
361 sm->right ? show_sm(sm->right) : "<none>", sm->right ? get_stree_id(sm->right->pool) : -1);
362 left = filter_pools(sm->left, remove_stack, keep_stack, &removed);
363 right = filter_pools(sm->right, remove_stack, keep_stack, &removed);
364 if (!removed) {
365 DIMPLIED("kept %s from %d [stree %d]\n", show_sm(sm), sm->line, get_stree_id(sm->pool));
366 return sm;
368 *modified = 1;
369 if (!left && !right) {
370 DIMPLIED("removed %s from %d <none> [stree %d]\n", show_sm(sm), sm->line, get_stree_id(sm->pool));
371 return NULL;
374 if (!left) {
375 ret = clone_sm(right);
376 ret->merged = 1;
377 ret->right = right;
378 ret->left = NULL;
379 } else if (!right) {
380 ret = clone_sm(left);
381 ret->merged = 1;
382 ret->left = left;
383 ret->right = NULL;
384 } else {
385 ret = merge_sm_states(left, right);
388 ret->pool = sm->pool;
390 ret->implied = 1;
391 DIMPLIED("partial %s => ", show_sm(sm));
392 DIMPLIED("%s from %d [stree %d]\n", show_sm(ret), sm->line, get_stree_id(sm->pool));
393 return ret;
396 static int sm_in_keep_leafs(struct sm_state *sm, const struct state_list *keep_gates)
398 struct sm_state *tmp, *old;
400 FOR_EACH_PTR(keep_gates, tmp) {
401 if (is_merged(tmp))
402 continue;
403 old = get_sm_state_stree(tmp->pool, sm->owner, sm->name, sm->sym);
404 if (old == sm)
405 return 1;
406 } END_FOR_EACH_PTR(tmp);
407 return 0;
410 static struct stree *filter_stack(struct sm_state *gate_sm,
411 struct stree *pre_stree,
412 const struct state_list *remove_stack,
413 const struct state_list *keep_stack)
415 struct stree *ret = NULL;
416 struct sm_state *tmp;
417 struct sm_state *filtered_sm;
418 int modified;
420 if (!remove_stack)
421 return NULL;
423 FOR_EACH_SM(pre_stree, tmp) {
424 if (!tmp->merged)
425 continue;
426 if (sm_in_keep_leafs(tmp, keep_stack))
427 continue;
428 modified = 0;
429 filtered_sm = filter_pools(tmp, remove_stack, keep_stack, &modified);
430 if (filtered_sm && modified) {
431 /* the assignments here are for borrowed implications */
432 filtered_sm->name = tmp->name;
433 filtered_sm->sym = tmp->sym;
434 avl_insert(&ret, filtered_sm);
435 if (out_of_memory())
436 return NULL;
439 } END_FOR_EACH_SM(tmp);
440 return ret;
443 static void separate_and_filter(struct sm_state *sm, int comparison, struct range_list *rl,
444 struct stree *pre_stree,
445 struct stree **true_states,
446 struct stree **false_states,
447 int *mixed)
449 struct state_list *true_stack = NULL;
450 struct state_list *false_stack = NULL;
451 struct timeval time_before;
452 struct timeval time_after;
454 gettimeofday(&time_before, NULL);
456 if (!is_merged(sm)) {
457 DIMPLIED("%d '%s' is not merged.\n", get_lineno(), sm->name);
458 return;
461 if (option_debug_implied || option_debug) {
462 sm_msg("checking implications: (%s %s %s)",
463 sm->name, show_special(comparison), show_rl(rl));
466 separate_pools(sm, comparison, rl, &true_stack, &false_stack, NULL, mixed);
468 DIMPLIED("filtering true stack.\n");
469 *true_states = filter_stack(sm, pre_stree, false_stack, true_stack);
470 DIMPLIED("filtering false stack.\n");
471 *false_states = filter_stack(sm, pre_stree, true_stack, false_stack);
472 free_slist(&true_stack);
473 free_slist(&false_stack);
474 if (option_debug_implied || option_debug) {
475 printf("These are the implied states for the true path:\n");
476 __print_stree(*true_states);
477 printf("These are the implied states for the false path:\n");
478 __print_stree(*false_states);
481 gettimeofday(&time_after, NULL);
482 if (time_after.tv_sec - time_before.tv_sec > 7)
483 __bail_on_rest_of_function = 1;
486 static struct expression *get_last_expr(struct statement *stmt)
488 struct statement *last;
490 last = last_ptr_list((struct ptr_list *)stmt->stmts);
491 if (last->type == STMT_EXPRESSION)
492 return last->expression;
494 if (last->type == STMT_LABEL) {
495 if (last->label_statement &&
496 last->label_statement->type == STMT_EXPRESSION)
497 return last->label_statement->expression;
500 return NULL;
503 static struct expression *get_left_most_expr(struct expression *expr)
505 struct statement *compound;
507 compound = get_expression_statement(expr);
508 if (compound)
509 return get_last_expr(compound);
511 expr = strip_parens(expr);
512 if (expr->type == EXPR_ASSIGNMENT)
513 return get_left_most_expr(expr->left);
514 return expr;
517 static int is_merged_expr(struct expression *expr)
519 struct sm_state *sm;
520 sval_t dummy;
522 if (get_value(expr, &dummy))
523 return 0;
524 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
525 if (!sm)
526 return 0;
527 if (is_merged(sm))
528 return 1;
529 return 0;
532 static void delete_gate_sm_equiv(struct stree **stree, const char *name, struct symbol *sym)
534 struct smatch_state *state;
535 struct relation *rel;
537 state = get_state(SMATCH_EXTRA, name, sym);
538 if (!state)
539 return;
540 FOR_EACH_PTR(estate_related(state), rel) {
541 delete_state_stree(stree, SMATCH_EXTRA, rel->name, rel->sym);
542 } END_FOR_EACH_PTR(rel);
545 static void delete_gate_sm(struct stree **stree, const char *name, struct symbol *sym)
547 delete_state_stree(stree, SMATCH_EXTRA, name, sym);
550 static int handle_comparison(struct expression *expr,
551 struct stree **implied_true,
552 struct stree **implied_false)
554 struct sm_state *sm = NULL;
555 struct range_list *rl = NULL;
556 struct expression *left;
557 struct expression *right;
558 struct symbol *type;
559 int comparison = expr->op;
560 int mixed = 0;
562 left = get_left_most_expr(expr->left);
563 right = get_left_most_expr(expr->right);
565 if (is_merged_expr(left)) {
566 sm = get_sm_state_expr(SMATCH_EXTRA, left);
567 get_implied_rl(right, &rl);
568 } else if (is_merged_expr(right)) {
569 sm = get_sm_state_expr(SMATCH_EXTRA, right);
570 get_implied_rl(left, &rl);
571 comparison = flip_comparison(comparison);
574 if (!rl || !sm) {
575 free_rl(&rl);
576 return 0;
579 type = get_type(expr);
580 if (!type)
581 return 0;
582 if (type_positive_bits(rl_type(rl)) > type_positive_bits(type))
583 type = rl_type(rl);
584 if (type_positive_bits(type) < 31)
585 type = &int_ctype;
586 rl = cast_rl(type, rl);
588 separate_and_filter(sm, comparison, rl, __get_cur_stree(), implied_true, implied_false, &mixed);
589 free_rl(&rl);
591 delete_gate_sm_equiv(implied_true, sm->name, sm->sym);
592 delete_gate_sm_equiv(implied_false, sm->name, sm->sym);
593 if (mixed) {
594 delete_gate_sm(implied_true, sm->name, sm->sym);
595 delete_gate_sm(implied_false, sm->name, sm->sym);
598 return 1;
601 static int handle_zero_comparison(struct expression *expr,
602 struct stree **implied_true,
603 struct stree **implied_false)
605 struct symbol *sym;
606 char *name;
607 struct sm_state *sm;
608 int mixed = 0;
609 int ret = 0;
611 if (expr->type == EXPR_POSTOP)
612 expr = strip_expr(expr->unop);
614 if (expr->type == EXPR_ASSIGNMENT) {
615 /* most of the time ->pools will be empty here because we
616 just set the state, but if have assigned a conditional
617 function there are implications. */
618 expr = expr->left;
621 name = expr_to_var_sym(expr, &sym);
622 if (!name || !sym)
623 goto free;
624 sm = get_sm_state(SMATCH_EXTRA, name, sym);
625 if (!sm)
626 goto free;
628 separate_and_filter(sm, SPECIAL_NOTEQUAL, tmp_range_list(estate_type(sm->state), 0), __get_cur_stree(), implied_true, implied_false, &mixed);
629 delete_gate_sm_equiv(implied_true, sm->name, sm->sym);
630 delete_gate_sm_equiv(implied_false, sm->name, sm->sym);
631 if (mixed) {
632 delete_gate_sm(implied_true, sm->name, sm->sym);
633 delete_gate_sm(implied_false, sm->name, sm->sym);
636 ret = 1;
637 free:
638 free_string(name);
639 return ret;
642 static int handled_by_implied_hook(struct expression *expr,
643 struct stree **implied_true,
644 struct stree **implied_false)
646 struct state_list *true_stack = NULL;
647 struct state_list *false_stack = NULL;
648 struct stree *pre_stree;
649 struct sm_state *sm;
651 sm = comparison_implication_hook(expr, &true_stack, &false_stack);
652 if (!sm)
653 return 0;
655 pre_stree = clone_stree(__get_cur_stree());
657 *implied_true = filter_stack(sm, pre_stree, false_stack, true_stack);
658 *implied_false = filter_stack(sm, pre_stree, true_stack, false_stack);
660 free_stree(&pre_stree);
661 free_slist(&true_stack);
662 free_slist(&false_stack);
664 return 1;
667 static int handled_by_extra_states(struct expression *expr,
668 struct stree **implied_true,
669 struct stree **implied_false)
671 if (expr->type == EXPR_COMPARE)
672 return handle_comparison(expr, implied_true, implied_false);
673 else
674 return handle_zero_comparison(expr, implied_true, implied_false);
677 static int handled_by_stored_conditions(struct expression *expr,
678 struct stree **implied_true,
679 struct stree **implied_false)
681 struct state_list *true_stack = NULL;
682 struct state_list *false_stack = NULL;
683 struct stree *pre_stree;
684 struct sm_state *sm;
686 sm = stored_condition_implication_hook(expr, &true_stack, &false_stack);
687 if (!sm)
688 return 0;
690 pre_stree = clone_stree(__get_cur_stree());
692 *implied_true = filter_stack(sm, pre_stree, false_stack, true_stack);
693 *implied_false = filter_stack(sm, pre_stree, true_stack, false_stack);
695 free_stree(&pre_stree);
696 free_slist(&true_stack);
697 free_slist(&false_stack);
699 return 1;
702 static int found_implications;
703 static struct stree *saved_implied_true;
704 static struct stree *saved_implied_false;
705 static struct stree *extra_saved_implied_true;
706 static struct stree *extra_saved_implied_false;
708 static void get_tf_states(struct expression *expr,
709 struct stree **implied_true,
710 struct stree **implied_false)
712 if (handled_by_implied_hook(expr, implied_true, implied_false))
713 goto found;
715 if (handled_by_extra_states(expr, implied_true, implied_false)) {
716 /* We process these later. */
717 extra_saved_implied_true = *implied_true;
718 extra_saved_implied_false = *implied_false;
719 *implied_true = NULL;
720 *implied_false = NULL;
721 goto found;
724 if (handled_by_stored_conditions(expr, implied_true, implied_false))
725 goto found;
727 return;
728 found:
729 found_implications = 1;
732 static void save_implications_hook(struct expression *expr)
734 get_tf_states(expr, &saved_implied_true, &saved_implied_false);
737 static void set_implied_states(struct expression *expr)
739 struct sm_state *sm;
741 FOR_EACH_SM(saved_implied_true, sm) {
742 __set_true_false_sm(sm, NULL);
743 } END_FOR_EACH_SM(sm);
744 free_stree(&saved_implied_true);
746 FOR_EACH_SM(saved_implied_false, sm) {
747 __set_true_false_sm(NULL, sm);
748 } END_FOR_EACH_SM(sm);
749 free_stree(&saved_implied_false);
752 static void set_extra_implied_states(struct expression *expr)
754 saved_implied_true = extra_saved_implied_true;
755 saved_implied_false = extra_saved_implied_false;
756 extra_saved_implied_true = NULL;
757 extra_saved_implied_false = NULL;
758 set_implied_states(NULL);
761 void param_limit_implications(struct expression *expr, int param, char *key, char *value)
763 struct expression *arg;
764 struct symbol *compare_type;
765 char *name;
766 struct symbol *sym;
767 struct sm_state *sm;
768 struct sm_state *tmp;
769 struct stree *implied_true = NULL;
770 struct stree *implied_false = NULL;
771 struct range_list *orig, *limit, *rl;
773 while (expr->type == EXPR_ASSIGNMENT)
774 expr = strip_expr(expr->right);
775 if (expr->type != EXPR_CALL)
776 return;
778 arg = get_argument_from_call_expr(expr->args, param);
779 if (!arg)
780 return;
782 name = get_variable_from_key(arg, key, &sym);
783 if (!name || !sym)
784 goto free;
786 sm = get_sm_state(SMATCH_EXTRA, name, sym);
787 if (!sm || !sm->merged)
788 goto free;
790 if (strcmp(key, "$") == 0)
791 compare_type = get_arg_type(expr->fn, param);
792 else
793 compare_type = get_member_type_from_key(arg, key);
795 orig = estate_rl(sm->state);
796 orig = cast_rl(compare_type, orig);
798 call_results_to_rl(expr, compare_type, value, &limit);
799 rl = rl_intersection(orig, limit);
801 separate_and_filter(sm, SPECIAL_EQUAL, rl, __get_cur_stree(), &implied_true, &implied_false, NULL);
803 FOR_EACH_SM(implied_true, tmp) {
804 __set_sm_fake_stree(tmp);
805 } END_FOR_EACH_SM(tmp);
807 free_stree(&implied_true);
808 free_stree(&implied_false);
809 free:
810 free_string(name);
813 struct stree *__implied_case_stree(struct expression *switch_expr,
814 struct range_list *rl,
815 struct range_list_stack **remaining_cases,
816 struct stree **raw_stree)
818 char *name;
819 struct symbol *sym;
820 struct sm_state *sm;
821 struct stree *true_states = NULL;
822 struct stree *false_states = NULL;
823 struct stree *extra_states;
824 struct stree *ret = clone_stree(*raw_stree);
826 name = expr_to_var_sym(switch_expr, &sym);
827 if (!name || !sym)
828 goto free;
830 if (rl)
831 filter_top_rl(remaining_cases, rl);
832 else
833 rl = clone_rl(top_rl(*remaining_cases));
835 sm = get_sm_state_stree(*raw_stree, SMATCH_EXTRA, name, sym);
836 if (sm)
837 separate_and_filter(sm, SPECIAL_EQUAL, rl, *raw_stree, &true_states, &false_states, NULL);
839 __push_fake_cur_stree();
840 __unnullify_path();
841 set_extra_nomod(name, sym, alloc_estate_rl(rl));
842 __pass_case_to_client(switch_expr, rl);
843 extra_states = __pop_fake_cur_stree();
844 overwrite_stree(extra_states, &true_states);
845 overwrite_stree(true_states, &ret);
846 free_stree(&extra_states);
847 free_stree(&true_states);
848 free_stree(&false_states);
849 free:
850 free_string(name);
851 return ret;
854 static void match_end_func(struct symbol *sym)
856 if (__inline_fn)
857 return;
858 implied_debug_msg = NULL;
861 static int sm_state_in_slist(struct sm_state *sm, struct state_list *slist)
863 struct sm_state *tmp;
865 FOR_EACH_PTR(slist, tmp) {
866 if (tmp == sm)
867 return 1;
868 } END_FOR_EACH_PTR(tmp);
869 return 0;
873 * The situation is we have a SMATCH_EXTRA state and we want to break it into
874 * each of the ->possible states and find the implications of each. The caller
875 * has to use __push_fake_cur_stree() to preserve the correct states so they
876 * can be restored later.
878 void overwrite_states_using_pool(struct sm_state *sm)
880 struct sm_state *old;
881 struct sm_state *new;
883 if (!sm->pool)
884 return;
886 FOR_EACH_SM(sm->pool, old) {
887 new = get_sm_state(old->owner, old->name, old->sym);
888 if (!new) /* the variable went out of scope */
889 continue;
890 if (sm_state_in_slist(old, new->possible))
891 set_state(old->owner, old->name, old->sym, old->state);
892 } END_FOR_EACH_SM(old);
895 int assume(struct expression *expr)
897 int orig_final_pass = final_pass;
899 final_pass = 0;
900 __push_fake_cur_stree();
901 found_implications = 0;
902 __split_whole_condition(expr);
903 final_pass = orig_final_pass;
905 if (!found_implications) {
906 __discard_false_states();
907 __free_fake_cur_stree();
908 return 0;
911 return 1;
914 void end_assume(void)
916 __discard_false_states();
917 __free_fake_cur_stree();
920 void __extra_match_condition(struct expression *expr);
921 void __comparison_match_condition(struct expression *expr);
922 void __stored_condition(struct expression *expr);
923 void register_implications(int id)
925 add_hook(&save_implications_hook, CONDITION_HOOK);
926 add_hook(&set_implied_states, CONDITION_HOOK);
927 add_hook(&__extra_match_condition, CONDITION_HOOK);
928 add_hook(&set_extra_implied_states, CONDITION_HOOK);
929 add_hook(&__comparison_match_condition, CONDITION_HOOK);
930 add_hook(&__stored_condition, CONDITION_HOOK);
931 add_hook(&match_end_func, END_FUNC_HOOK);