Merge branch 'upstream_sparse' into merge
[smatch.git] / smatch_math.c
blob5781a75019dedc7203ecdf6d4a4f18e9424b2256
1 /*
2 * smatch/smatch_math.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "symbol.h"
11 #include "smatch.h"
12 #include "smatch_slist.h"
13 #include "smatch_extra.h"
15 static struct range_list *_get_rl(struct expression *expr, int implied);
16 static struct range_list *handle_variable(struct expression *expr, int implied);
18 static sval_t zero = {.type = &int_ctype, {.value = 0} };
19 static sval_t one = {.type = &int_ctype, {.value = 1} };
21 static struct range_list *rl_zero(void)
23 return alloc_rl(zero, zero);
26 static struct range_list *rl_one(void)
28 return alloc_rl(one, one);
31 enum {
32 RL_EXACT,
33 RL_HARD,
34 RL_FUZZY,
35 RL_IMPLIED,
36 RL_ABSOLUTE
39 static struct range_list *last_stmt_rl(struct statement *stmt, int implied)
41 if (!stmt)
42 return NULL;
44 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
45 if (stmt->type != STMT_EXPRESSION)
46 return NULL;
47 return _get_rl(stmt->expression, implied);
50 static struct range_list *handle_expression_statement_rl(struct expression *expr, int implied)
52 return last_stmt_rl(get_expression_statement(expr), implied);
55 static struct range_list *handle_ampersand_rl(int implied)
57 if (implied == RL_EXACT || implied == RL_HARD)
58 return NULL;
59 return alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
62 static struct range_list *handle_negate_rl(struct expression *expr, int implied)
64 if (known_condition_true(expr->unop))
65 return rl_zero();
66 if (known_condition_false(expr->unop))
67 return rl_one();
69 if (implied == RL_EXACT)
70 return NULL;
72 if (implied_condition_true(expr->unop))
73 return rl_zero();
74 if (implied_condition_false(expr->unop))
75 return rl_one();
76 return alloc_rl(zero, one);
79 static struct range_list *handle_bitwise_negate(struct expression *expr, int implied)
81 struct range_list *rl;
82 sval_t sval;
84 rl = _get_rl(expr->unop, implied);
85 if (!rl_to_sval(rl, &sval))
86 return NULL;
87 sval = sval_preop(sval, '~');
88 sval_cast(get_type(expr->unop), sval);
89 return alloc_rl(sval, sval);
92 static struct range_list *handle_minus_preop(struct expression *expr, int implied)
94 struct range_list *rl;
95 sval_t sval;
97 rl = _get_rl(expr->unop, implied);
98 if (!rl_to_sval(rl, &sval))
99 return NULL;
100 sval = sval_preop(sval, '-');
101 return alloc_rl(sval, sval);
104 static struct range_list *handle_preop_rl(struct expression *expr, int implied)
106 switch (expr->op) {
107 case '&':
108 return handle_ampersand_rl(implied);
109 case '!':
110 return handle_negate_rl(expr, implied);
111 case '~':
112 return handle_bitwise_negate(expr, implied);
113 case '-':
114 return handle_minus_preop(expr, implied);
115 case '*':
116 return handle_variable(expr, implied);
117 case '(':
118 return handle_expression_statement_rl(expr, implied);
119 default:
120 return NULL;
124 static struct range_list *handle_divide_rl(struct expression *expr, int implied)
126 struct range_list *left_rl, *right_rl;
127 struct symbol *type;
128 sval_t min, max;
130 type = get_type(expr);
132 left_rl = _get_rl(expr->left, implied);
133 left_rl = cast_rl(type, left_rl);
134 right_rl = _get_rl(expr->right, implied);
135 right_rl = cast_rl(type, right_rl);
137 if (!left_rl || !right_rl)
138 return NULL;
139 if (is_whole_rl(left_rl) || is_whole_rl(right_rl))
140 return NULL;
141 if (sval_is_negative(rl_min(left_rl)) || sval_cmp_val(rl_min(right_rl), 0) <= 0)
142 return NULL;
144 max = rl_max(left_rl);
145 if (!sval_is_max(max))
146 max = sval_binop(max, '/', rl_min(right_rl));
147 min = sval_binop(rl_min(left_rl), '/', rl_max(right_rl));
148 return alloc_rl(min, max);
151 static struct range_list *handle_subtract_rl(struct expression *expr, int implied)
153 struct symbol *type;
154 struct range_list *left_rl, *right_rl;
155 sval_t max, min, tmp;
156 int comparison;
158 type = get_type(expr);
159 comparison = get_comparison(expr->left, expr->right);
161 left_rl = _get_rl(expr->left, implied);
162 left_rl = cast_rl(type, left_rl);
163 right_rl = _get_rl(expr->right, implied);
164 right_rl = cast_rl(type, right_rl);
166 if ((!left_rl || !right_rl) &&
167 (implied == RL_EXACT || implied == RL_HARD || implied == RL_FUZZY))
168 return NULL;
170 if (!left_rl)
171 left_rl = alloc_whole_rl(type);
172 if (!right_rl)
173 right_rl = alloc_whole_rl(type);
175 /* negative values complicate everything fix this later */
176 if (sval_is_negative(rl_min(right_rl)))
177 return NULL;
178 max = rl_max(left_rl);
180 switch (comparison) {
181 case '>':
182 case SPECIAL_UNSIGNED_GT:
183 min = sval_type_val(type, 1);
184 max = rl_max(left_rl);
185 break;
186 case SPECIAL_GTE:
187 case SPECIAL_UNSIGNED_GTE:
188 min = sval_type_val(type, 0);
189 max = rl_max(left_rl);
190 break;
191 default:
192 if (sval_binop_overflows(rl_min(left_rl), '-', rl_max(right_rl)))
193 return NULL;
194 min = sval_type_min(type);
197 if (!sval_binop_overflows(rl_min(left_rl), '-', rl_max(right_rl))) {
198 tmp = sval_binop(rl_min(left_rl), '-', rl_max(right_rl));
199 if (sval_cmp(tmp, min) > 0)
200 min = tmp;
203 if (!sval_is_max(rl_max(left_rl))) {
204 tmp = sval_binop(rl_max(left_rl), '-', rl_min(right_rl));
205 if (sval_cmp(tmp, max) < 0)
206 max = tmp;
209 if (sval_is_min(min) && sval_is_max(max))
210 return NULL;
212 return cast_rl(type, alloc_rl(min, max));
215 static struct range_list *handle_mod_rl(struct expression *expr, int implied)
217 struct range_list *rl;
218 sval_t left, right, sval;
220 if (implied == RL_EXACT) {
221 if (!get_value(expr->right, &right))
222 return NULL;
223 if (!get_value(expr->left, &left))
224 return NULL;
225 sval = sval_binop(left, '%', right);
226 return alloc_rl(sval, sval);
228 /* if we can't figure out the right side it's probably hopeless */
229 if (!get_implied_value(expr->right, &right))
230 return NULL;
232 right = sval_cast(get_type(expr), right);
233 right.value--;
235 rl = _get_rl(expr->left, implied);
236 if (rl && rl_max(rl).uvalue < right.uvalue)
237 right.uvalue = rl_max(rl).uvalue;
239 return alloc_rl(zero, right);
242 static sval_t sval_lowest_set_bit(sval_t sval)
244 int i;
245 int found = 0;
247 for (i = 0; i < 64; i++) {
248 if (sval.uvalue & 1ULL << i) {
249 if (!found++)
250 continue;
251 sval.uvalue &= ~(1ULL << i);
254 return sval;
257 static struct range_list *handle_bitwise_AND(struct expression *expr, int implied)
259 struct symbol *type;
260 struct range_list *left_rl, *right_rl;
261 sval_t known;
263 if (implied != RL_IMPLIED && implied != RL_ABSOLUTE)
264 return NULL;
266 type = get_type(expr);
268 if (get_implied_value(expr->left, &known)) {
269 sval_t min;
271 min = sval_lowest_set_bit(known);
272 left_rl = alloc_rl(min, known);
273 left_rl = cast_rl(type, left_rl);
274 add_range(&left_rl, sval_type_val(type, 0), sval_type_val(type, 0));
275 } else {
276 left_rl = _get_rl(expr->left, implied);
277 if (left_rl) {
278 left_rl = cast_rl(type, left_rl);
279 left_rl = alloc_rl(sval_type_val(type, 0), rl_max(left_rl));
280 } else {
281 if (implied == RL_HARD)
282 return NULL;
283 left_rl = alloc_whole_rl(type);
287 if (get_implied_value(expr->right, &known)) {
288 sval_t min;
290 min = sval_lowest_set_bit(known);
291 right_rl = alloc_rl(min, known);
292 right_rl = cast_rl(type, right_rl);
293 add_range(&right_rl, sval_type_val(type, 0), sval_type_val(type, 0));
294 } else {
295 right_rl = _get_rl(expr->right, implied);
296 if (right_rl) {
297 right_rl = cast_rl(type, right_rl);
298 right_rl = alloc_rl(sval_type_val(type, 0), rl_max(right_rl));
299 } else {
300 if (implied == RL_HARD)
301 return NULL;
302 right_rl = alloc_whole_rl(type);
306 return rl_intersection(left_rl, right_rl);
309 static struct range_list *handle_bitwise_OR(struct expression *expr, int implied)
311 struct symbol *type;
312 struct range_list *left_rl, *right_rl;
314 if (implied != RL_IMPLIED && implied != RL_ABSOLUTE)
315 return NULL;
317 type = get_type(expr);
319 get_absolute_rl(expr->left, &left_rl);
320 get_absolute_rl(expr->right, &right_rl);
321 left_rl = cast_rl(type, left_rl);
322 right_rl = cast_rl(type, right_rl);
324 return rl_union(left_rl, right_rl);
327 static struct range_list *handle_right_shift(struct expression *expr, int implied)
329 struct range_list *left_rl;
330 sval_t right;
331 sval_t min, max;
333 if (implied == RL_EXACT || implied == RL_HARD)
334 return NULL;
335 /* this is hopeless without the right side */
336 if (!get_implied_value(expr->right, &right))
337 return NULL;
338 left_rl = _get_rl(expr->left, implied);
339 if (left_rl) {
340 max = rl_max(left_rl);
341 min = rl_min(left_rl);
342 } else {
343 if (implied == RL_FUZZY)
344 return NULL;
345 max = sval_type_max(get_type(expr->left));
346 min = sval_type_val(get_type(expr->left), 0);
349 max = sval_binop(max, SPECIAL_RIGHTSHIFT, right);
350 min = sval_binop(min, SPECIAL_RIGHTSHIFT, right);
351 return alloc_rl(min, max);
354 static struct range_list *handle_left_shift(struct expression *expr, int implied)
356 struct range_list *left_rl, *res;
357 sval_t right;
358 sval_t min, max;
359 int add_zero = 0;
361 if (implied == RL_EXACT || implied == RL_HARD)
362 return NULL;
363 /* this is hopeless without the right side */
364 if (!get_implied_value(expr->right, &right))
365 return NULL;
366 left_rl = _get_rl(expr->left, implied);
367 if (left_rl) {
368 max = rl_max(left_rl);
369 min = rl_min(left_rl);
370 if (min.value == 0) {
371 min.value = 1;
372 add_zero = 1;
374 } else {
375 if (implied == RL_FUZZY)
376 return NULL;
377 max = sval_type_max(get_type(expr->left));
378 min = sval_type_val(get_type(expr->left), 1);
379 add_zero = 1;
382 max = sval_binop(max, SPECIAL_LEFTSHIFT, right);
383 min = sval_binop(min, SPECIAL_LEFTSHIFT, right);
384 res = alloc_rl(min, max);
385 if (add_zero)
386 res = rl_union(res, rl_zero());
387 return res;
390 static struct range_list *handle_known_binop(struct expression *expr)
392 sval_t left, right;
394 if (!get_value(expr->left, &left))
395 return NULL;
396 if (!get_value(expr->right, &right))
397 return NULL;
398 left = sval_binop(left, expr->op, right);
399 return alloc_rl(left, left);
402 static struct range_list *handle_binop_rl(struct expression *expr, int implied)
404 struct symbol *type;
405 struct range_list *left_rl, *right_rl, *rl;
406 sval_t min, max;
408 rl = handle_known_binop(expr);
409 if (rl)
410 return rl;
411 if (implied == RL_EXACT)
412 return NULL;
414 switch (expr->op) {
415 case '%':
416 return handle_mod_rl(expr, implied);
417 case '&':
418 return handle_bitwise_AND(expr, implied);
419 case '|':
420 return handle_bitwise_OR(expr, implied);
421 case SPECIAL_RIGHTSHIFT:
422 return handle_right_shift(expr, implied);
423 case SPECIAL_LEFTSHIFT:
424 return handle_left_shift(expr, implied);
425 case '-':
426 return handle_subtract_rl(expr, implied);
427 case '/':
428 return handle_divide_rl(expr, implied);
431 type = get_type(expr);
432 left_rl = _get_rl(expr->left, implied);
433 left_rl = cast_rl(type, left_rl);
434 right_rl = _get_rl(expr->right, implied);
435 right_rl = cast_rl(type, right_rl);
437 if (!left_rl || !right_rl)
438 return NULL;
440 if (sval_binop_overflows(rl_min(left_rl), expr->op, rl_min(right_rl)))
441 return NULL;
442 min = sval_binop(rl_min(left_rl), expr->op, rl_min(right_rl));
444 if (sval_binop_overflows(rl_max(left_rl), expr->op, rl_max(right_rl))) {
445 switch (implied) {
446 case RL_FUZZY:
447 case RL_HARD:
448 return NULL;
449 default:
450 max = sval_type_max(get_type(expr));
452 } else {
453 max = sval_binop(rl_max(left_rl), expr->op, rl_max(right_rl));
456 return alloc_rl(min, max);
459 static int do_comparison(struct expression *expr)
461 struct range_list *left_ranges = NULL;
462 struct range_list *right_ranges = NULL;
463 int poss_true, poss_false;
464 struct symbol *type;
466 type = get_type(expr);
468 get_implied_rl(expr->left, &left_ranges);
469 get_implied_rl(expr->right, &right_ranges);
470 left_ranges = cast_rl(type, left_ranges);
471 right_ranges = cast_rl(type, right_ranges);
473 poss_true = possibly_true_rl(left_ranges, expr->op, right_ranges);
474 poss_false = possibly_false_rl(left_ranges, expr->op, right_ranges);
476 free_rl(&left_ranges);
477 free_rl(&right_ranges);
479 if (!poss_true && !poss_false)
480 return 0;
481 if (poss_true && !poss_false)
482 return 1;
483 if (!poss_true && poss_false)
484 return 2;
485 return 3;
488 static struct range_list *handle_comparison_rl(struct expression *expr, int implied)
490 sval_t left, right;
491 int res;
493 if (get_value(expr->left, &left) && get_value(expr->right, &right)) {
494 struct data_range tmp_left, tmp_right;
496 tmp_left.min = left;
497 tmp_left.max = left;
498 tmp_right.min = right;
499 tmp_right.max = right;
500 if (true_comparison_range(&tmp_left, expr->op, &tmp_right))
501 return rl_one();
502 return rl_zero();
505 if (implied == RL_EXACT)
506 return NULL;
508 res = do_comparison(expr);
509 if (res == 1)
510 return rl_one();
511 if (res == 2)
512 return rl_zero();
514 return alloc_rl(zero, one);
517 static struct range_list *handle_logical_rl(struct expression *expr, int implied)
519 sval_t left, right;
520 int left_known = 0;
521 int right_known = 0;
523 if (implied == RL_EXACT) {
524 if (get_value(expr->left, &left))
525 left_known = 1;
526 if (get_value(expr->right, &right))
527 right_known = 1;
528 } else {
529 if (get_implied_value(expr->left, &left))
530 left_known = 1;
531 if (get_implied_value(expr->right, &right))
532 right_known = 1;
535 switch (expr->op) {
536 case SPECIAL_LOGICAL_OR:
537 if (left_known && left.value)
538 return rl_one();
539 if (right_known && right.value)
540 return rl_one();
541 if (left_known && right_known)
542 return rl_zero();
543 break;
544 case SPECIAL_LOGICAL_AND:
545 if (left_known && right_known) {
546 if (left.value && right.value)
547 return rl_one();
548 return rl_zero();
550 break;
551 default:
552 return NULL;
555 if (implied == RL_EXACT)
556 return NULL;
558 return alloc_rl(zero, one);
561 static struct range_list *handle_conditional_rl(struct expression *expr, int implied)
563 struct range_list *true_rl, *false_rl;
564 struct symbol *type;
565 int final_pass_orig = final_pass;
567 if (known_condition_true(expr->conditional))
568 return _get_rl(expr->cond_true, implied);
569 if (known_condition_false(expr->conditional))
570 return _get_rl(expr->cond_false, implied);
572 if (implied == RL_EXACT)
573 return NULL;
575 if (implied_condition_true(expr->conditional))
576 return _get_rl(expr->cond_true, implied);
577 if (implied_condition_false(expr->conditional))
578 return _get_rl(expr->cond_false, implied);
581 /* this becomes a problem with deeply nested conditional statements */
582 if (low_on_memory())
583 return NULL;
585 type = get_type(expr);
587 __push_fake_cur_slist();
588 final_pass = 0;
589 __split_whole_condition(expr->conditional);
590 true_rl = _get_rl(expr->cond_true, implied);
591 __push_true_states();
592 __use_false_states();
593 false_rl = _get_rl(expr->cond_false, implied);
594 __merge_true_states();
595 __free_fake_cur_slist();
596 final_pass = final_pass_orig;
598 if (!true_rl || !false_rl)
599 return NULL;
600 true_rl = cast_rl(type, true_rl);
601 false_rl = cast_rl(type, false_rl);
603 return rl_union(true_rl, false_rl);
606 static int get_fuzzy_max_helper(struct expression *expr, sval_t *max)
608 struct smatch_state *state;
609 sval_t sval;
611 if (get_hard_max(expr, &sval)) {
612 *max = sval;
613 return 1;
616 state = get_state_expr(SMATCH_EXTRA, expr);
617 if (!state || !estate_has_fuzzy_max(state))
618 return 0;
619 *max = sval_cast(get_type(expr), estate_get_fuzzy_max(state));
620 return 1;
623 static int get_fuzzy_min_helper(struct expression *expr, sval_t *min)
625 struct smatch_state *state;
626 sval_t sval;
628 state = get_state_expr(SMATCH_EXTRA, expr);
629 if (!state || !estate_rl(state))
630 return 0;
632 sval = estate_min(state);
633 if (sval_is_negative(sval) && sval_is_min(sval))
634 return 0;
636 if (sval_is_max(sval))
637 return 0;
639 *min = sval_cast(get_type(expr), sval);
640 return 1;
643 int get_const_value(struct expression *expr, sval_t *sval)
645 struct symbol *sym;
646 sval_t right;
648 if (expr->type != EXPR_SYMBOL || !expr->symbol)
649 return 0;
650 sym = expr->symbol;
651 if (!(sym->ctype.modifiers & MOD_CONST))
652 return 0;
653 if (get_value(sym->initializer, &right)) {
654 *sval = sval_cast(get_type(expr), right);
655 return 1;
657 return 0;
660 static struct range_list *handle_variable(struct expression *expr, int implied)
662 struct smatch_state *state;
663 struct range_list *rl;
664 sval_t sval, min, max;
666 if (get_const_value(expr, &sval))
667 return alloc_rl(sval, sval);
669 switch (implied) {
670 case RL_EXACT:
671 return NULL;
672 case RL_HARD:
673 case RL_IMPLIED:
674 case RL_ABSOLUTE:
675 state = get_state_expr(SMATCH_EXTRA, expr);
676 if (!state || !state->data) {
677 if (implied == RL_HARD)
678 return NULL;
679 if (get_local_rl(expr, &rl))
680 return rl;
681 if (get_db_type_rl(expr, &rl))
682 return rl;
683 return NULL;
685 if (implied == RL_HARD && !estate_has_hard_max(state))
686 return NULL;
687 return clone_rl(estate_rl(state));
688 case RL_FUZZY:
689 if (!get_fuzzy_min_helper(expr, &min))
690 min = sval_type_min(get_type(expr));
691 if (!get_fuzzy_max_helper(expr, &max))
692 return NULL;
693 /* fuzzy ranges are often inverted */
694 if (sval_cmp(min, max) > 0) {
695 sval = min;
696 min = max;
697 max = sval;
699 return alloc_rl(min, max);
701 return NULL;
704 static sval_t handle_sizeof(struct expression *expr)
706 struct symbol *sym;
707 sval_t ret;
709 ret = sval_blank(expr);
710 sym = expr->cast_type;
711 if (!sym) {
712 sym = evaluate_expression(expr->cast_expression);
714 * Expressions of restricted types will possibly get
715 * promoted - check that here
717 if (is_restricted_type(sym)) {
718 if (sym->bit_size < bits_in_int)
719 sym = &int_ctype;
720 } else if (is_fouled_type(sym)) {
721 sym = &int_ctype;
724 examine_symbol_type(sym);
726 ret.type = size_t_ctype;
727 if (sym->bit_size <= 0) /* sizeof(void) */ {
728 if (get_real_base_type(sym) == &void_ctype)
729 ret.value = 1;
730 else
731 ret.value = 0;
732 } else
733 ret.value = bits_to_bytes(sym->bit_size);
735 return ret;
738 static struct range_list *handle_call_rl(struct expression *expr, int implied)
740 struct range_list *rl;
742 if (implied == RL_EXACT || implied == RL_HARD || implied == RL_FUZZY)
743 return NULL;
745 if (get_implied_return(expr, &rl))
746 return rl;
747 return db_return_vals(expr);
750 static struct range_list *handle_cast(struct expression *expr, int implied)
752 struct range_list *rl;
753 struct symbol *type;
755 type = get_type(expr);
756 rl = _get_rl(expr->cast_expression, implied);
757 if (rl)
758 return cast_rl(type, rl);
759 if (implied == RL_ABSOLUTE)
760 return alloc_whole_rl(type);
761 if (implied == RL_IMPLIED && type &&
762 type->bit_size > 0 && type->bit_size < 32)
763 return alloc_whole_rl(type);
764 return NULL;
767 static struct range_list *_get_rl(struct expression *expr, int implied)
769 struct range_list *rl;
770 struct symbol *type;
771 sval_t sval;
773 expr = strip_parens(expr);
774 if (!expr)
775 return NULL;
776 type = get_type(expr);
778 switch (expr->type) {
779 case EXPR_VALUE:
780 sval = sval_from_val(expr, expr->value);
781 rl = alloc_rl(sval, sval);
782 break;
783 case EXPR_PREOP:
784 rl = handle_preop_rl(expr, implied);
785 break;
786 case EXPR_POSTOP:
787 rl = _get_rl(expr->unop, implied);
788 break;
789 case EXPR_CAST:
790 case EXPR_FORCE_CAST:
791 case EXPR_IMPLIED_CAST:
792 rl = handle_cast(expr, implied);
793 break;
794 case EXPR_BINOP:
795 rl = handle_binop_rl(expr, implied);
796 break;
797 case EXPR_COMPARE:
798 rl = handle_comparison_rl(expr, implied);
799 break;
800 case EXPR_LOGICAL:
801 rl = handle_logical_rl(expr, implied);
802 break;
803 case EXPR_PTRSIZEOF:
804 case EXPR_SIZEOF:
805 sval = handle_sizeof(expr);
806 rl = alloc_rl(sval, sval);
807 break;
808 case EXPR_SELECT:
809 case EXPR_CONDITIONAL:
810 rl = handle_conditional_rl(expr, implied);
811 break;
812 case EXPR_CALL:
813 rl = handle_call_rl(expr, implied);
814 break;
815 default:
816 rl = handle_variable(expr, implied);
819 if (rl)
820 return rl;
821 if (type && implied == RL_ABSOLUTE)
822 return alloc_whole_rl(type);
823 return NULL;
826 /* returns 1 if it can get a value literal or else returns 0 */
827 int get_value(struct expression *expr, sval_t *sval)
829 struct range_list *rl;
831 rl = _get_rl(expr, RL_EXACT);
832 if (!rl_to_sval(rl, sval))
833 return 0;
834 return 1;
837 int get_implied_value(struct expression *expr, sval_t *sval)
839 struct range_list *rl;
841 rl = _get_rl(expr, RL_IMPLIED);
842 if (!rl_to_sval(rl, sval))
843 return 0;
844 return 1;
847 int get_implied_min(struct expression *expr, sval_t *sval)
849 struct range_list *rl;
851 rl = _get_rl(expr, RL_IMPLIED);
852 if (!rl)
853 return 0;
854 *sval = rl_min(rl);
855 return 1;
858 int get_implied_max(struct expression *expr, sval_t *sval)
860 struct range_list *rl;
862 rl = _get_rl(expr, RL_IMPLIED);
863 if (!rl)
864 return 0;
865 *sval = rl_max(rl);
866 return 1;
869 int get_implied_rl(struct expression *expr, struct range_list **rl)
871 *rl = _get_rl(expr, RL_IMPLIED);
872 if (*rl)
873 return 1;
874 return 0;
877 int get_absolute_rl(struct expression *expr, struct range_list **rl)
879 *rl = _get_rl(expr, RL_ABSOLUTE);
880 if (!*rl)
881 *rl = alloc_whole_rl(get_type(expr));
882 return 1;
885 int get_implied_rl_var_sym(const char *var, struct symbol *sym, struct range_list **rl)
887 struct smatch_state *state;
889 state = get_state(SMATCH_EXTRA, var, sym);
890 *rl = estate_rl(state);
891 if (*rl)
892 return 1;
893 return 0;
896 int get_hard_max(struct expression *expr, sval_t *sval)
898 struct range_list *rl;
900 rl = _get_rl(expr, RL_HARD);
901 if (!rl)
902 return 0;
903 *sval = rl_max(rl);
904 return 1;
907 int get_fuzzy_min(struct expression *expr, sval_t *sval)
909 struct range_list *rl;
910 sval_t tmp;
912 rl = _get_rl(expr, RL_FUZZY);
913 if (!rl)
914 return 0;
915 tmp = rl_min(rl);
916 if (sval_is_negative(tmp) && sval_is_min(tmp))
917 return 0;
918 *sval = tmp;
919 return 1;
922 int get_fuzzy_max(struct expression *expr, sval_t *sval)
924 struct range_list *rl;
925 sval_t max;
927 rl = _get_rl(expr, RL_FUZZY);
928 if (!rl)
929 return 0;
930 max = rl_max(rl);
931 if (max.uvalue > INT_MAX - 10000)
932 return 0;
933 *sval = max;
934 return 1;
937 int get_absolute_min(struct expression *expr, sval_t *sval)
939 struct range_list *rl;
940 struct symbol *type;
942 type = get_type(expr);
943 if (!type)
944 type = &llong_ctype; // FIXME: this is wrong but places assume get type can't fail.
945 rl = _get_rl(expr, RL_ABSOLUTE);
946 if (rl)
947 *sval = rl_min(rl);
948 else
949 *sval = sval_type_min(type);
951 if (sval_cmp(*sval, sval_type_min(type)) < 0)
952 *sval = sval_type_min(type);
953 return 1;
956 int get_absolute_max(struct expression *expr, sval_t *sval)
958 struct range_list *rl;
959 struct symbol *type;
961 type = get_type(expr);
962 if (!type)
963 type = &llong_ctype;
964 rl = _get_rl(expr, RL_ABSOLUTE);
965 if (rl)
966 *sval = rl_max(rl);
967 else
968 *sval = sval_type_max(type);
970 if (sval_cmp(sval_type_max(type), *sval) < 0)
971 *sval = sval_type_max(type);
972 return 1;
975 int known_condition_true(struct expression *expr)
977 sval_t tmp;
979 if (!expr)
980 return 0;
982 if (get_value(expr, &tmp) && tmp.value)
983 return 1;
985 return 0;
988 int known_condition_false(struct expression *expr)
990 if (!expr)
991 return 0;
993 if (is_zero(expr))
994 return 1;
996 if (expr->type == EXPR_CALL) {
997 if (sym_name_is("__builtin_constant_p", expr->fn))
998 return 1;
1000 return 0;
1003 int implied_condition_true(struct expression *expr)
1005 sval_t tmp;
1007 if (!expr)
1008 return 0;
1010 if (known_condition_true(expr))
1011 return 1;
1012 if (get_implied_value(expr, &tmp) && tmp.value)
1013 return 1;
1015 if (expr->type == EXPR_POSTOP)
1016 return implied_condition_true(expr->unop);
1018 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_DECREMENT)
1019 return implied_not_equal(expr->unop, 1);
1020 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_INCREMENT)
1021 return implied_not_equal(expr->unop, -1);
1023 expr = strip_expr(expr);
1024 switch (expr->type) {
1025 case EXPR_COMPARE:
1026 if (do_comparison(expr) == 1)
1027 return 1;
1028 break;
1029 case EXPR_PREOP:
1030 if (expr->op == '!') {
1031 if (implied_condition_false(expr->unop))
1032 return 1;
1033 break;
1035 break;
1036 default:
1037 if (implied_not_equal(expr, 0) == 1)
1038 return 1;
1039 break;
1041 return 0;
1044 int implied_condition_false(struct expression *expr)
1046 struct expression *tmp;
1047 sval_t sval;
1049 if (!expr)
1050 return 0;
1052 if (known_condition_false(expr))
1053 return 1;
1055 switch (expr->type) {
1056 case EXPR_COMPARE:
1057 if (do_comparison(expr) == 2)
1058 return 1;
1059 case EXPR_PREOP:
1060 if (expr->op == '!') {
1061 if (implied_condition_true(expr->unop))
1062 return 1;
1063 break;
1065 tmp = strip_expr(expr);
1066 if (tmp != expr)
1067 return implied_condition_false(tmp);
1068 break;
1069 default:
1070 if (get_implied_value(expr, &sval) && sval.value == 0)
1071 return 1;
1072 break;
1074 return 0;