shift_to_zero: do a small clean up
[smatch.git] / smatch_implied.c
blob66bd77a4227ced7b8dcd4d97ce2349d9047f04d8
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 || option_debug) 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 * add_pool() adds a slist to *pools. If the slist has already been
177 * added earlier then it doesn't get added a second time.
179 void add_pool(struct state_list **pools, struct sm_state *new)
181 struct sm_state *tmp;
183 FOR_EACH_PTR(*pools, tmp) {
184 if (tmp->pool < new->pool)
185 continue;
186 else if (tmp->pool == new->pool) {
187 return;
188 } else {
189 INSERT_CURRENT(new, tmp);
190 return;
192 } END_FOR_EACH_PTR(tmp);
193 add_ptr_list(pools, new);
196 static int pool_in_pools(struct stree *pool,
197 const struct state_list *slist)
199 struct sm_state *tmp;
201 FOR_EACH_PTR(slist, tmp) {
202 if (!tmp->pool)
203 continue;
204 if (tmp->pool == pool)
205 return 1;
206 if (tmp->pool > pool)
207 return 0;
208 } END_FOR_EACH_PTR(tmp);
209 return 0;
212 static int remove_pool(struct state_list **pools, struct stree *remove)
214 struct sm_state *tmp;
215 int ret = 0;
217 FOR_EACH_PTR(*pools, tmp) {
218 if (tmp->pool == remove) {
219 DELETE_CURRENT_PTR(tmp);
220 ret = 1;
222 } END_FOR_EACH_PTR(tmp);
224 return ret;
228 * If 'foo' == 99 add it that pool to the true pools. If it's false, add it to
229 * the false pools. If we're not sure, then we don't add it to either.
231 static void do_compare(struct sm_state *sm, int comparison, struct range_list *rl,
232 struct state_list **true_stack,
233 struct state_list **maybe_stack,
234 struct state_list **false_stack,
235 int *mixed, struct sm_state *gate_sm)
237 int istrue;
238 int isfalse;
239 struct range_list *var_rl;
241 if (!sm->pool)
242 return;
244 var_rl = cast_rl(rl_type(rl), estate_rl(sm->state));
246 istrue = !possibly_false_rl(var_rl, comparison, rl);
247 isfalse = !possibly_true_rl(var_rl, comparison, rl);
249 print_debug_tf(sm, istrue, isfalse);
251 /* give up if we have borrowed implications (smatch_equiv.c) */
252 if (sm->sym != gate_sm->sym ||
253 strcmp(sm->name, gate_sm->name) != 0) {
254 if (mixed)
255 *mixed = 1;
258 if (mixed && !*mixed && !is_merged(sm) && !istrue && !isfalse) {
259 if (!create_fake_history(sm, comparison, rl)) {
260 if (mixed)
261 *mixed = 1;
265 if (istrue)
266 add_pool(true_stack, sm);
267 else if (isfalse)
268 add_pool(false_stack, sm);
269 else
270 add_pool(maybe_stack, sm);
274 static int is_checked(struct state_list *checked, struct sm_state *sm)
276 struct sm_state *tmp;
278 FOR_EACH_PTR(checked, tmp) {
279 if (tmp == sm)
280 return 1;
281 } END_FOR_EACH_PTR(tmp);
282 return 0;
286 * separate_pools():
287 * Example code: if (foo == 99) {
289 * Say 'foo' is a merged state that has many possible values. It is the combination
290 * of merges. separate_pools() iterates through the pools recursively and calls
291 * do_compare() for each time 'foo' was set.
293 static void __separate_pools(struct sm_state *sm, int comparison, struct range_list *rl,
294 struct state_list **true_stack,
295 struct state_list **maybe_stack,
296 struct state_list **false_stack,
297 struct state_list **checked, int *mixed, struct sm_state *gate_sm)
299 int free_checked = 0;
300 struct state_list *checked_states = NULL;
302 if (!sm)
303 return;
306 * If it looks like this is going to take too long as-is, then don't
307 * create even more fake history.
309 if (mixed && sm->nr_children > 100)
310 *mixed = 1;
313 Sometimes the implications are just too big to deal with
314 so we bail. Theoretically, bailing out here can cause more false
315 positives but won't hide actual bugs.
317 if (sm->nr_children > 4000) {
318 if (option_debug || option_debug_implied) {
319 static char buf[1028];
320 snprintf(buf, sizeof(buf), "debug: %s: nr_children over 4000 (%d). (%s %s)",
321 __func__, sm->nr_children, sm->name, show_state(sm->state));
322 implied_debug_msg = buf;
324 return;
327 if (checked == NULL) {
328 checked = &checked_states;
329 free_checked = 1;
331 if (is_checked(*checked, sm))
332 return;
333 add_ptr_list(checked, sm);
335 do_compare(sm, comparison, rl, true_stack, maybe_stack, false_stack, mixed, gate_sm);
337 __separate_pools(sm->left, comparison, rl, true_stack, maybe_stack, false_stack, checked, mixed, gate_sm);
338 __separate_pools(sm->right, comparison, rl, true_stack, maybe_stack, false_stack, checked, mixed, gate_sm);
339 if (free_checked)
340 free_slist(checked);
343 static void separate_pools(struct sm_state *sm, int comparison, struct range_list *rl,
344 struct state_list **true_stack,
345 struct state_list **false_stack,
346 struct state_list **checked, int *mixed)
348 struct state_list *maybe_stack = NULL;
349 struct sm_state *tmp;
351 __separate_pools(sm, comparison, rl, true_stack, &maybe_stack, false_stack, checked, mixed, sm);
353 if (option_debug) {
354 struct sm_state *sm;
356 FOR_EACH_PTR(*true_stack, sm) {
357 sm_msg("TRUE %s [stree %d]", show_sm(sm), get_stree_id(sm->pool));
358 } END_FOR_EACH_PTR(sm);
360 FOR_EACH_PTR(maybe_stack, sm) {
361 sm_msg("MAYBE %s [stree %d]", show_sm(sm), get_stree_id(sm->pool));
362 } END_FOR_EACH_PTR(sm);
364 FOR_EACH_PTR(*false_stack, sm) {
365 sm_msg("FALSE %s [stree %d]", show_sm(sm), get_stree_id(sm->pool));
366 } END_FOR_EACH_PTR(sm);
368 /* if it's a maybe then remove it */
369 FOR_EACH_PTR(maybe_stack, tmp) {
370 remove_pool(false_stack, tmp->pool);
371 remove_pool(true_stack, tmp->pool);
372 } END_FOR_EACH_PTR(tmp);
374 /* if it's both true and false remove it from both */
375 FOR_EACH_PTR(*true_stack, tmp) {
376 if (remove_pool(false_stack, tmp->pool))
377 DELETE_CURRENT_PTR(tmp);
378 } END_FOR_EACH_PTR(tmp);
381 static int sm_in_keep_leafs(struct sm_state *sm, const struct state_list *keep_gates)
383 struct sm_state *tmp, *old;
385 FOR_EACH_PTR(keep_gates, tmp) {
386 if (is_merged(tmp))
387 continue;
388 old = get_sm_state_stree(tmp->pool, sm->owner, sm->name, sm->sym);
389 if (!old)
390 continue;
391 if (old == sm)
392 return 1;
393 } END_FOR_EACH_PTR(tmp);
394 return 0;
398 * NOTE: If a state is in both the keep stack and the remove stack then it is
399 * removed. If that happens it means you have a bug. Only add states which are
400 * definitely true or definitely false. If you have a leaf state that could be
401 * both true and false, then create a fake split history where one side is true
402 * and one side is false. Otherwise, if you can't do that, then don't add it to
403 * either list, and it will be treated as true.
405 struct sm_state *filter_pools(struct sm_state *sm,
406 const struct state_list *remove_stack,
407 const struct state_list *keep_stack,
408 int *modified)
410 struct sm_state *ret = NULL;
411 struct sm_state *left;
412 struct sm_state *right;
413 int removed = 0;
415 if (!sm)
416 return NULL;
418 if (sm->nr_children > 4000) {
419 if (option_debug || option_debug_implied) {
420 static char buf[1028];
421 snprintf(buf, sizeof(buf), "debug: %s: nr_children over 4000 (%d). (%s %s)",
422 __func__, sm->nr_children, sm->name, show_state(sm->state));
423 implied_debug_msg = buf;
425 return NULL;
428 if (pool_in_pools(sm->pool, remove_stack)) {
429 DIMPLIED("removed [stree %d] %s from %d\n", get_stree_id(sm->pool), show_sm(sm), sm->line);
430 *modified = 1;
431 return NULL;
434 if (!is_merged(sm) || pool_in_pools(sm->pool, keep_stack) || sm_in_keep_leafs(sm, keep_stack)) {
435 DIMPLIED("kept [stree %d] %s from %d. %s. %s. %s.\n", get_stree_id(sm->pool), show_sm(sm), sm->line,
436 is_merged(sm) ? "merged" : "not merged",
437 pool_in_pools(sm->pool, keep_stack) ? "not in keep pools" : "in keep pools",
438 sm_in_keep_leafs(sm, keep_stack) ? "reachable keep leaf" : "no keep leaf");
439 return sm;
442 DIMPLIED("checking [stree %d] %s from %d (%d) left = %s [stree %d] right = %s [stree %d]\n",
443 get_stree_id(sm->pool),
444 show_sm(sm), sm->line, sm->nr_children,
445 sm->left ? sm->left->state->name : "<none>", sm->left ? get_stree_id(sm->left->pool) : -1,
446 sm->right ? sm->right->state->name : "<none>", sm->right ? get_stree_id(sm->right->pool) : -1);
447 left = filter_pools(sm->left, remove_stack, keep_stack, &removed);
448 right = filter_pools(sm->right, remove_stack, keep_stack, &removed);
449 if (!removed) {
450 DIMPLIED("kept [stree %d] %s from %d\n", get_stree_id(sm->pool), show_sm(sm), sm->line);
451 return sm;
453 *modified = 1;
454 if (!left && !right) {
455 DIMPLIED("removed [stree %d] %s from %d <none>\n", get_stree_id(sm->pool), show_sm(sm), sm->line);
456 return NULL;
459 if (!left) {
460 ret = clone_sm(right);
461 ret->merged = 1;
462 ret->right = right;
463 ret->left = NULL;
464 } else if (!right) {
465 ret = clone_sm(left);
466 ret->merged = 1;
467 ret->left = left;
468 ret->right = NULL;
469 } else {
470 if (left->sym != sm->sym || strcmp(left->name, sm->name) != 0) {
471 left = clone_sm(left);
472 left->sym = sm->sym;
473 left->name = sm->name;
475 if (right->sym != sm->sym || strcmp(right->name, sm->name) != 0) {
476 right = clone_sm(right);
477 right->sym = sm->sym;
478 right->name = sm->name;
480 ret = merge_sm_states(left, right);
483 ret->pool = sm->pool;
485 DIMPLIED("partial %s => ", show_sm(sm));
486 DIMPLIED("%s from %d [stree %d]\n", show_sm(ret), sm->line, get_stree_id(sm->pool));
487 return ret;
490 static struct stree *filter_stack(struct sm_state *gate_sm,
491 struct stree *pre_stree,
492 const struct state_list *remove_stack,
493 const struct state_list *keep_stack)
495 struct stree *ret = NULL;
496 struct sm_state *tmp;
497 struct sm_state *filtered_sm;
498 int modified;
500 if (!remove_stack)
501 return NULL;
503 FOR_EACH_SM(pre_stree, tmp) {
504 if (option_debug)
505 sm_msg("%s: %s", __func__, show_sm(tmp));
506 if (!tmp->merged)
507 continue;
508 if (sm_in_keep_leafs(tmp, keep_stack))
509 continue;
510 modified = 0;
511 filtered_sm = filter_pools(tmp, remove_stack, keep_stack, &modified);
512 if (!filtered_sm || !modified)
513 continue;
514 /* the assignments here are for borrowed implications */
515 filtered_sm->name = tmp->name;
516 filtered_sm->sym = tmp->sym;
517 avl_insert(&ret, filtered_sm);
518 if (out_of_memory())
519 return NULL;
521 } END_FOR_EACH_SM(tmp);
522 return ret;
525 static void separate_and_filter(struct sm_state *sm, int comparison, struct range_list *rl,
526 struct stree *pre_stree,
527 struct stree **true_states,
528 struct stree **false_states,
529 int *mixed)
531 struct state_list *true_stack = NULL;
532 struct state_list *false_stack = NULL;
533 struct timeval time_before;
534 struct timeval time_after;
536 gettimeofday(&time_before, NULL);
538 if (!is_merged(sm)) {
539 DIMPLIED("%d '%s' is not merged.\n", get_lineno(), sm->name);
540 return;
543 if (option_debug_implied || option_debug) {
544 sm_msg("checking implications: (%s %s %s)",
545 sm->name, show_special(comparison), show_rl(rl));
548 separate_pools(sm, comparison, rl, &true_stack, &false_stack, NULL, mixed);
550 DIMPLIED("filtering true stack.\n");
551 *true_states = filter_stack(sm, pre_stree, false_stack, true_stack);
552 DIMPLIED("filtering false stack.\n");
553 *false_states = filter_stack(sm, pre_stree, true_stack, false_stack);
554 free_slist(&true_stack);
555 free_slist(&false_stack);
556 if (option_debug_implied || option_debug) {
557 printf("These are the implied states for the true path: (%s %s %s)\n",
558 sm->name, show_special(comparison), show_rl(rl));
559 __print_stree(*true_states);
560 printf("These are the implied states for the false path: (%s %s %s)\n",
561 sm->name, show_special(comparison), show_rl(rl));
562 __print_stree(*false_states);
565 gettimeofday(&time_after, NULL);
566 if (time_after.tv_sec - time_before.tv_sec > 20) {
567 sm->nr_children = 4000;
568 sm_msg("Function too hairy. Giving up.");
572 static struct expression *get_last_expr(struct statement *stmt)
574 struct statement *last;
576 last = last_ptr_list((struct ptr_list *)stmt->stmts);
577 if (last->type == STMT_EXPRESSION)
578 return last->expression;
580 if (last->type == STMT_LABEL) {
581 if (last->label_statement &&
582 last->label_statement->type == STMT_EXPRESSION)
583 return last->label_statement->expression;
586 return NULL;
589 static struct expression *get_left_most_expr(struct expression *expr)
591 struct statement *compound;
593 compound = get_expression_statement(expr);
594 if (compound)
595 return get_last_expr(compound);
597 expr = strip_parens(expr);
598 if (expr->type == EXPR_ASSIGNMENT)
599 return get_left_most_expr(expr->left);
600 return expr;
603 static int is_merged_expr(struct expression *expr)
605 struct sm_state *sm;
606 sval_t dummy;
608 if (get_value(expr, &dummy))
609 return 0;
610 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
611 if (!sm)
612 return 0;
613 if (is_merged(sm))
614 return 1;
615 return 0;
618 static void delete_gate_sm_equiv(struct stree **stree, const char *name, struct symbol *sym)
620 struct smatch_state *state;
621 struct relation *rel;
623 state = get_state(SMATCH_EXTRA, name, sym);
624 if (!state)
625 return;
626 FOR_EACH_PTR(estate_related(state), rel) {
627 delete_state_stree(stree, SMATCH_EXTRA, rel->name, rel->sym);
628 } END_FOR_EACH_PTR(rel);
631 static void delete_gate_sm(struct stree **stree, const char *name, struct symbol *sym)
633 delete_state_stree(stree, SMATCH_EXTRA, name, sym);
636 static int handle_comparison(struct expression *expr,
637 struct stree **implied_true,
638 struct stree **implied_false)
640 struct sm_state *sm = NULL;
641 struct range_list *rl = NULL;
642 struct expression *left;
643 struct expression *right;
644 struct symbol *type;
645 int comparison = expr->op;
646 int mixed = 0;
648 left = get_left_most_expr(expr->left);
649 right = get_left_most_expr(expr->right);
651 if (is_merged_expr(left)) {
652 sm = get_sm_state_expr(SMATCH_EXTRA, left);
653 get_implied_rl(right, &rl);
654 } else if (is_merged_expr(right)) {
655 sm = get_sm_state_expr(SMATCH_EXTRA, right);
656 get_implied_rl(left, &rl);
657 comparison = flip_comparison(comparison);
660 if (!rl || !sm) {
661 free_rl(&rl);
662 return 0;
665 type = get_type(expr);
666 if (!type)
667 return 0;
668 if (type_positive_bits(rl_type(rl)) > type_positive_bits(type))
669 type = rl_type(rl);
670 if (type_positive_bits(type) < 31)
671 type = &int_ctype;
672 rl = cast_rl(type, rl);
674 separate_and_filter(sm, comparison, rl, __get_cur_stree(), implied_true, implied_false, &mixed);
675 free_rl(&rl);
677 delete_gate_sm_equiv(implied_true, sm->name, sm->sym);
678 delete_gate_sm_equiv(implied_false, sm->name, sm->sym);
679 if (mixed) {
680 delete_gate_sm(implied_true, sm->name, sm->sym);
681 delete_gate_sm(implied_false, sm->name, sm->sym);
684 return 1;
687 static int handle_zero_comparison(struct expression *expr,
688 struct stree **implied_true,
689 struct stree **implied_false)
691 struct symbol *sym;
692 char *name;
693 struct sm_state *sm;
694 int mixed = 0;
695 int ret = 0;
697 if (expr->type == EXPR_POSTOP)
698 expr = strip_expr(expr->unop);
700 if (expr->type == EXPR_ASSIGNMENT) {
701 /* most of the time ->pools will be empty here because we
702 just set the state, but if have assigned a conditional
703 function there are implications. */
704 expr = expr->left;
707 name = expr_to_var_sym(expr, &sym);
708 if (!name || !sym)
709 goto free;
710 sm = get_sm_state(SMATCH_EXTRA, name, sym);
711 if (!sm)
712 goto free;
714 separate_and_filter(sm, SPECIAL_NOTEQUAL, tmp_range_list(estate_type(sm->state), 0), __get_cur_stree(), implied_true, implied_false, &mixed);
715 delete_gate_sm_equiv(implied_true, sm->name, sm->sym);
716 delete_gate_sm_equiv(implied_false, sm->name, sm->sym);
717 if (mixed) {
718 delete_gate_sm(implied_true, sm->name, sm->sym);
719 delete_gate_sm(implied_false, sm->name, sm->sym);
722 ret = 1;
723 free:
724 free_string(name);
725 return ret;
728 static int handled_by_comparison_hook(struct expression *expr,
729 struct stree **implied_true,
730 struct stree **implied_false)
732 struct state_list *true_stack = NULL;
733 struct state_list *false_stack = NULL;
734 struct stree *pre_stree;
735 struct sm_state *sm;
737 sm = comparison_implication_hook(expr, &true_stack, &false_stack);
738 if (!sm)
739 return 0;
741 pre_stree = clone_stree(__get_cur_stree());
743 *implied_true = filter_stack(sm, pre_stree, false_stack, true_stack);
744 *implied_false = filter_stack(sm, pre_stree, true_stack, false_stack);
746 free_stree(&pre_stree);
747 free_slist(&true_stack);
748 free_slist(&false_stack);
750 return 1;
753 static int handled_by_extra_states(struct expression *expr,
754 struct stree **implied_true,
755 struct stree **implied_false)
757 if (expr->type == EXPR_COMPARE)
758 return handle_comparison(expr, implied_true, implied_false);
759 else
760 return handle_zero_comparison(expr, implied_true, implied_false);
763 static int handled_by_stored_conditions(struct expression *expr,
764 struct stree **implied_true,
765 struct stree **implied_false)
767 struct state_list *true_stack = NULL;
768 struct state_list *false_stack = NULL;
769 struct stree *pre_stree;
770 struct sm_state *sm;
772 sm = stored_condition_implication_hook(expr, &true_stack, &false_stack);
773 if (!sm)
774 return 0;
776 pre_stree = clone_stree(__get_cur_stree());
778 *implied_true = filter_stack(sm, pre_stree, false_stack, true_stack);
779 *implied_false = filter_stack(sm, pre_stree, true_stack, false_stack);
781 free_stree(&pre_stree);
782 free_slist(&true_stack);
783 free_slist(&false_stack);
785 return 1;
788 static int found_implications;
789 static struct stree *saved_implied_true;
790 static struct stree *saved_implied_false;
791 static struct stree *extra_saved_implied_true;
792 static struct stree *extra_saved_implied_false;
794 static void get_tf_states(struct expression *expr,
795 struct stree **implied_true,
796 struct stree **implied_false)
798 if (handled_by_comparison_hook(expr, implied_true, implied_false))
799 goto found;
801 if (handled_by_extra_states(expr, implied_true, implied_false)) {
802 /* We process these later. */
803 extra_saved_implied_true = *implied_true;
804 extra_saved_implied_false = *implied_false;
805 *implied_true = NULL;
806 *implied_false = NULL;
807 goto found;
810 if (handled_by_stored_conditions(expr, implied_true, implied_false))
811 goto found;
813 return;
814 found:
815 found_implications = 1;
818 static void save_implications_hook(struct expression *expr)
820 get_tf_states(expr, &saved_implied_true, &saved_implied_false);
823 static void set_implied_states(struct expression *expr)
825 struct sm_state *sm;
827 FOR_EACH_SM(saved_implied_true, sm) {
828 __set_true_false_sm(sm, NULL);
829 } END_FOR_EACH_SM(sm);
830 free_stree(&saved_implied_true);
832 FOR_EACH_SM(saved_implied_false, sm) {
833 __set_true_false_sm(NULL, sm);
834 } END_FOR_EACH_SM(sm);
835 free_stree(&saved_implied_false);
838 static void set_extra_implied_states(struct expression *expr)
840 saved_implied_true = extra_saved_implied_true;
841 saved_implied_false = extra_saved_implied_false;
842 extra_saved_implied_true = NULL;
843 extra_saved_implied_false = NULL;
844 set_implied_states(NULL);
847 void param_limit_implications(struct expression *expr, int param, char *key, char *value)
849 struct expression *arg;
850 struct symbol *compare_type;
851 char *name;
852 struct symbol *sym;
853 struct sm_state *sm;
854 struct sm_state *tmp;
855 struct stree *implied_true = NULL;
856 struct stree *implied_false = NULL;
857 struct range_list *orig, *limit;
859 while (expr->type == EXPR_ASSIGNMENT)
860 expr = strip_expr(expr->right);
861 if (expr->type != EXPR_CALL)
862 return;
864 arg = get_argument_from_call_expr(expr->args, param);
865 if (!arg)
866 return;
868 name = get_variable_from_key(arg, key, &sym);
869 if (!name || !sym)
870 goto free;
872 sm = get_sm_state(SMATCH_EXTRA, name, sym);
873 if (!sm || !sm->merged)
874 goto free;
876 if (strcmp(key, "$") == 0)
877 compare_type = get_arg_type(expr->fn, param);
878 else
879 compare_type = get_member_type_from_key(arg, key);
881 orig = estate_rl(sm->state);
882 orig = cast_rl(compare_type, orig);
884 call_results_to_rl(expr, compare_type, value, &limit);
886 separate_and_filter(sm, SPECIAL_EQUAL, limit, __get_cur_stree(), &implied_true, &implied_false, NULL);
888 FOR_EACH_SM(implied_true, tmp) {
889 __set_sm_fake_stree(tmp);
890 } END_FOR_EACH_SM(tmp);
892 free_stree(&implied_true);
893 free_stree(&implied_false);
894 free:
895 free_string(name);
898 struct stree *__implied_case_stree(struct expression *switch_expr,
899 struct range_list *rl,
900 struct range_list_stack **remaining_cases,
901 struct stree **raw_stree)
903 char *name;
904 struct symbol *sym;
905 struct var_sym_list *vsl;
906 struct sm_state *sm;
907 struct stree *true_states = NULL;
908 struct stree *false_states = NULL;
909 struct stree *extra_states;
910 struct stree *ret = clone_stree(*raw_stree);
912 name = expr_to_chunk_sym_vsl(switch_expr, &sym, &vsl);
914 if (rl)
915 filter_top_rl(remaining_cases, rl);
916 else
917 rl = clone_rl(top_rl(*remaining_cases));
919 if (name) {
920 sm = get_sm_state_stree(*raw_stree, SMATCH_EXTRA, name, sym);
921 if (sm)
922 separate_and_filter(sm, SPECIAL_EQUAL, rl, *raw_stree, &true_states, &false_states, NULL);
925 __push_fake_cur_stree();
926 __unnullify_path();
927 if (name)
928 set_extra_nomod_vsl(name, sym, vsl, alloc_estate_rl(rl));
929 __pass_case_to_client(switch_expr, rl);
930 extra_states = __pop_fake_cur_stree();
931 overwrite_stree(extra_states, &true_states);
932 overwrite_stree(true_states, &ret);
933 free_stree(&extra_states);
934 free_stree(&true_states);
935 free_stree(&false_states);
937 free_string(name);
938 return ret;
941 static void match_end_func(struct symbol *sym)
943 if (__inline_fn)
944 return;
945 implied_debug_msg = NULL;
948 static void get_tf_stacks_from_pool(struct sm_state *gate_sm,
949 struct sm_state *pool_sm,
950 struct state_list **true_stack,
951 struct state_list **false_stack)
953 struct sm_state *tmp;
954 int possibly_true = 0;
956 if (!gate_sm)
957 return;
959 if (strcmp(gate_sm->state->name, pool_sm->state->name) == 0) {
960 add_ptr_list(true_stack, pool_sm);
961 return;
964 FOR_EACH_PTR(gate_sm->possible, tmp) {
965 if (strcmp(tmp->state->name, pool_sm->state->name) == 0) {
966 possibly_true = 1;
967 break;
969 } END_FOR_EACH_PTR(tmp);
971 if (!possibly_true) {
972 add_ptr_list(false_stack, gate_sm);
973 return;
976 get_tf_stacks_from_pool(gate_sm->left, pool_sm, true_stack, false_stack);
977 get_tf_stacks_from_pool(gate_sm->right, pool_sm, true_stack, false_stack);
981 * The situation is we have a SMATCH_EXTRA state and we want to break it into
982 * each of the ->possible states and find the implications of each. The caller
983 * has to use __push_fake_cur_stree() to preserve the correct states so they
984 * can be restored later.
986 void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm)
988 struct state_list *true_stack = NULL;
989 struct state_list *false_stack = NULL;
990 struct stree *pre_stree;
991 struct stree *implied_true;
992 struct sm_state *tmp;
994 if (!pool_sm->pool)
995 return;
997 get_tf_stacks_from_pool(gate_sm, pool_sm, &true_stack, &false_stack);
999 pre_stree = clone_stree(__get_cur_stree());
1001 implied_true = filter_stack(gate_sm, pre_stree, false_stack, true_stack);
1003 free_stree(&pre_stree);
1004 free_slist(&true_stack);
1005 free_slist(&false_stack);
1007 FOR_EACH_SM(implied_true, tmp) {
1008 set_state(tmp->owner, tmp->name, tmp->sym, tmp->state);
1009 } END_FOR_EACH_SM(tmp);
1011 free_stree(&implied_true);
1014 int assume(struct expression *expr)
1016 int orig_final_pass = final_pass;
1018 final_pass = 0;
1019 __push_fake_cur_stree();
1020 found_implications = 0;
1021 __split_whole_condition(expr);
1022 final_pass = orig_final_pass;
1024 if (!found_implications) {
1025 __discard_false_states();
1026 __free_fake_cur_stree();
1027 return 0;
1030 return 1;
1033 void end_assume(void)
1035 __discard_false_states();
1036 __free_fake_cur_stree();
1039 void __extra_match_condition(struct expression *expr);
1040 void __comparison_match_condition(struct expression *expr);
1041 void __stored_condition(struct expression *expr);
1042 void register_implications(int id)
1044 add_hook(&save_implications_hook, CONDITION_HOOK);
1045 add_hook(&set_implied_states, CONDITION_HOOK);
1046 add_hook(&__extra_match_condition, CONDITION_HOOK);
1047 add_hook(&set_extra_implied_states, CONDITION_HOOK);
1048 add_hook(&__comparison_match_condition, CONDITION_HOOK);
1049 add_hook(&__stored_condition, CONDITION_HOOK);
1050 add_hook(&match_end_func, END_FUNC_HOOK);