2 * Copyright (C) 2009 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
20 #include "smatch_extra.h"
21 #include "smatch_slist.h"
23 ALLOCATOR(data_info
, "smatch extra data");
24 ALLOCATOR(data_range
, "data range");
25 __DO_ALLOCATOR(struct data_range
, sizeof(struct data_range
), __alignof__(struct data_range
),
26 "permanent ranges", perm_data_range
);
27 __DECLARE_ALLOCATOR(struct ptr_list
, rl_ptrlist
);
29 char *show_rl(struct range_list
*list
)
31 struct data_range
*tmp
;
36 full
[sizeof(full
) - 1] = '\0';
37 FOR_EACH_PTR(list
, tmp
) {
39 strncat(full
, ",", 254 - strlen(full
));
40 if (sval_cmp(tmp
->min
, tmp
->max
) == 0) {
41 strncat(full
, sval_to_str(tmp
->min
), 254 - strlen(full
));
44 strncat(full
, sval_to_str(tmp
->min
), 254 - strlen(full
));
45 strncat(full
, "-", 254 - strlen(full
));
46 strncat(full
, sval_to_str(tmp
->max
), 254 - strlen(full
));
47 } END_FOR_EACH_PTR(tmp
);
48 if (strlen(full
) == sizeof(full
) - 1)
49 full
[sizeof(full
) - 2] = '+';
50 return alloc_sname(full
);
53 void free_all_rl(void)
55 clear_rl_ptrlist_alloc();
58 static int sval_too_big(struct symbol
*type
, sval_t sval
)
60 if (type_bits(type
) >= 32 &&
61 type_bits(sval
.type
) <= type_bits(type
))
63 if (sval
.uvalue
<= ((1ULL << type_bits(type
)) - 1))
65 if (type_signed(sval
.type
)) {
66 if (type_unsigned(type
)) {
67 unsigned long long neg
= ~sval
.uvalue
;
68 if (neg
<= sval_type_max(type
).uvalue
)
71 if (sval
.value
< sval_type_min(type
).value
)
73 if (sval
.value
> sval_type_max(type
).value
)
77 if (sval
.uvalue
> sval_type_max(type
).uvalue
)
82 static void add_range_t(struct symbol
*type
, struct range_list
**rl
, sval_t min
, sval_t max
)
84 /* If we're just adding a number, cast it and add it */
85 if (sval_cmp(min
, max
) == 0) {
86 add_range(rl
, sval_cast(type
, min
), sval_cast(type
, max
));
90 /* If the range is within the type range then add it */
91 if (sval_fits(type
, min
) && sval_fits(type
, max
)) {
92 add_range(rl
, sval_cast(type
, min
), sval_cast(type
, max
));
97 * If the range we are adding has more bits than the range type then
98 * add the whole range type. Eg:
99 * 0x8000000000000000 - 0xf000000000000000 -> cast to int
100 * This isn't totally the right thing to do. We could be more granular.
102 if (sval_too_big(type
, min
) || sval_too_big(type
, max
)) {
103 add_range(rl
, sval_type_min(type
), sval_type_max(type
));
107 /* Cast negative values to high positive values */
108 if (sval_is_negative(min
) && type_unsigned(type
)) {
109 if (sval_is_positive(max
)) {
110 if (sval_too_high(type
, max
)) {
111 add_range(rl
, sval_type_min(type
), sval_type_max(type
));
114 add_range(rl
, sval_type_val(type
, 0), sval_cast(type
, max
));
115 max
= sval_type_max(type
);
117 max
= sval_cast(type
, max
);
119 min
= sval_cast(type
, min
);
120 add_range(rl
, min
, max
);
123 /* Cast high positive numbers to negative */
124 if (sval_unsigned(max
) && sval_is_negative(sval_cast(type
, max
))) {
125 if (!sval_is_negative(sval_cast(type
, min
))) {
126 add_range(rl
, sval_cast(type
, min
), sval_type_max(type
));
127 min
= sval_type_min(type
);
129 min
= sval_cast(type
, min
);
131 max
= sval_cast(type
, max
);
132 add_range(rl
, min
, max
);
135 add_range(rl
, sval_cast(type
, min
), sval_cast(type
, max
));
139 static int str_to_comparison_arg_helper(const char *str
,
140 struct expression
*call
, int *comparison
,
141 struct expression
**arg
, char **endp
)
144 char *c
= (char *)str
;
153 *comparison
= SPECIAL_LTE
;
158 } else if (*c
== '=') {
161 *comparison
= SPECIAL_EQUAL
;
162 } else if (*c
== '>') {
165 *comparison
= SPECIAL_GTE
;
170 } else if (*c
== '!') {
173 *comparison
= SPECIAL_NOTEQUAL
;
182 param
= strtoll(c
, &c
, 10);
184 c
++; /* skip the ']' character */
190 *arg
= get_argument_from_call_expr(call
->args
, param
);
193 if (*c
== '-' && *(c
+ 1) == '>') {
197 n
= snprintf(buf
, sizeof(buf
), "$%s", c
);
198 if (n
>= sizeof(buf
))
200 if (buf
[n
- 1] == ']')
202 *arg
= gen_expression_from_key(*arg
, buf
);
203 while (*c
&& *c
!= ']')
209 int str_to_comparison_arg(const char *str
, struct expression
*call
, int *comparison
, struct expression
**arg
)
218 return str_to_comparison_arg_helper(str
, call
, comparison
, arg
, NULL
);
221 static int get_val_from_key(int use_max
, struct symbol
*type
, char *c
, struct expression
*call
, char **endp
, sval_t
*sval
)
223 struct expression
*arg
;
228 ret
= sval_type_max(type
);
230 ret
= sval_type_min(type
);
232 if (!str_to_comparison_arg_helper(c
, call
, &comparison
, &arg
, endp
)) {
237 if (use_max
&& get_implied_max(arg
, &tmp
)) {
239 if (comparison
== '<') {
241 ret
= sval_binop(ret
, '-', tmp
);
244 if (!use_max
&& get_implied_min(arg
, &tmp
)) {
246 if (comparison
== '>') {
248 ret
= sval_binop(ret
, '+', tmp
);
256 static sval_t
add_one(sval_t sval
)
262 static sval_t
sub_one(sval_t sval
)
268 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
)
270 struct range_list
*left_orig
= *rl
;
271 struct range_list
*right_orig
= right
;
272 struct range_list
*ret_rl
= *rl
;
273 struct symbol
*cast_type
;
276 cast_type
= rl_type(left_orig
);
277 if (sval_type_max(rl_type(left_orig
)).uvalue
< sval_type_max(rl_type(right_orig
)).uvalue
)
278 cast_type
= rl_type(right_orig
);
279 if (sval_type_max(cast_type
).uvalue
< INT_MAX
)
280 cast_type
= &int_ctype
;
282 min
= sval_type_min(cast_type
);
283 max
= sval_type_max(cast_type
);
284 left_orig
= cast_rl(cast_type
, left_orig
);
285 right_orig
= cast_rl(cast_type
, right_orig
);
287 switch (comparison
) {
289 case SPECIAL_UNSIGNED_LT
:
290 ret_rl
= remove_range(left_orig
, rl_max(right_orig
), max
);
293 case SPECIAL_UNSIGNED_LTE
:
294 if (!sval_is_max(rl_max(right_orig
)))
295 ret_rl
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
298 ret_rl
= rl_intersection(left_orig
, right_orig
);
301 case SPECIAL_UNSIGNED_GTE
:
302 if (!sval_is_min(rl_min(right_orig
)))
303 ret_rl
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
306 case SPECIAL_UNSIGNED_GT
:
307 ret_rl
= remove_range(left_orig
, min
, rl_min(right_orig
));
309 case SPECIAL_NOTEQUAL
:
310 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
311 ret_rl
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
314 sm_msg("internal error: unhandled comparison %s", show_special(comparison
));
318 *rl
= cast_rl(rl_type(*rl
), ret_rl
);
321 static struct range_list
*filter_by_comparison_call(char *c
, struct expression
*call
, char **endp
, struct range_list
*start_rl
)
324 struct expression
*arg
;
325 struct range_list
*casted_start
, *right_orig
;
328 if (!str_to_comparison_arg_helper(c
, call
, &comparison
, &arg
, endp
))
331 if (!get_implied_rl(arg
, &right_orig
))
335 if (type_positive_bits(rl_type(start_rl
)) > type_positive_bits(type
))
336 type
= rl_type(start_rl
);
337 if (type_positive_bits(rl_type(right_orig
)) > type_positive_bits(type
))
338 type
= rl_type(right_orig
);
340 casted_start
= cast_rl(type
, start_rl
);
341 right_orig
= cast_rl(type
, right_orig
);
343 filter_by_comparison(&casted_start
, comparison
, right_orig
);
344 return cast_rl(rl_type(start_rl
), casted_start
);
347 static sval_t
parse_val(int use_max
, struct expression
*call
, struct symbol
*type
, char *c
, char **endp
)
352 if (!strncmp(start
, "max", 3)) {
353 ret
= sval_type_max(type
);
355 } else if (!strncmp(start
, "u64max", 6)) {
356 ret
= sval_type_val(type
, ULLONG_MAX
);
358 } else if (!strncmp(start
, "s64max", 6)) {
359 ret
= sval_type_val(type
, LLONG_MAX
);
361 } else if (!strncmp(start
, "u32max", 6)) {
362 ret
= sval_type_val(type
, UINT_MAX
);
364 } else if (!strncmp(start
, "s32max", 6)) {
365 ret
= sval_type_val(type
, INT_MAX
);
367 } else if (!strncmp(start
, "u16max", 6)) {
368 ret
= sval_type_val(type
, USHRT_MAX
);
370 } else if (!strncmp(start
, "s16max", 6)) {
371 ret
= sval_type_val(type
, SHRT_MAX
);
373 } else if (!strncmp(start
, "min", 3)) {
374 ret
= sval_type_min(type
);
376 } else if (!strncmp(start
, "s64min", 6)) {
377 ret
= sval_type_val(type
, LLONG_MIN
);
379 } else if (!strncmp(start
, "s32min", 6)) {
380 ret
= sval_type_val(type
, INT_MIN
);
382 } else if (!strncmp(start
, "s16min", 6)) {
383 ret
= sval_type_val(type
, SHRT_MIN
);
385 } else if (!strncmp(start
, "long_min", 8)) {
386 ret
= sval_type_val(type
, LONG_MIN
);
388 } else if (!strncmp(start
, "long_max", 8)) {
389 ret
= sval_type_val(type
, LONG_MAX
);
391 } else if (!strncmp(start
, "ulong_max", 9)) {
392 ret
= sval_type_val(type
, ULONG_MAX
);
394 } else if (!strncmp(start
, "ptr_max", 7)) {
395 ret
= sval_type_val(type
, valid_ptr_max
);
397 } else if (start
[0] == '[') {
398 /* this parses [==p0] comparisons */
399 get_val_from_key(1, type
, start
, call
, &c
, &ret
);
400 } else if (type_positive_bits(type
) == 64) {
401 ret
= sval_type_val(type
, strtoull(start
, &c
, 0));
403 ret
= sval_type_val(type
, strtoll(start
, &c
, 0));
409 static char *jump_to_call_math(char *value
)
413 while (*c
&& *c
!= '[')
419 if (*c
== '<' || *c
== '=' || *c
== '>' || *c
== '!')
425 static void str_to_rl_helper(struct expression
*call
, struct symbol
*type
, char *str
, char **endp
, struct range_list
**rl
)
427 struct range_list
*rl_tmp
= NULL
;
431 min
= sval_type_min(type
);
432 max
= sval_type_max(type
);
434 while (*c
!= '\0' && *c
!= '[') {
436 if (sval_cmp(min
, sval_type_min(type
)) != 0)
438 max
= sval_type_max(type
);
439 add_range_t(type
, &rl_tmp
, min
, max
);
444 min
= parse_val(0, call
, type
, c
, &c
);
445 if (!sval_fits(type
, min
))
446 min
= sval_type_min(type
);
450 if (*c
== '\0' || *c
== '[') {
451 add_range_t(type
, &rl_tmp
, min
, min
);
455 add_range_t(type
, &rl_tmp
, min
, min
);
460 min
= sval_type_max(type
);
464 sm_msg("debug XXX: trouble parsing %s c = %s", str
, c
);
470 max
= parse_val(1, call
, type
, c
, &c
);
471 if (!sval_fits(type
, max
))
472 max
= sval_type_max(type
);
474 max
= sval_type_max(type
);
477 add_range_t(type
, &rl_tmp
, min
, max
);
488 static void str_to_dinfo(struct expression
*call
, struct symbol
*type
, char *value
, struct data_info
*dinfo
)
490 struct range_list
*math_rl
;
493 struct range_list
*rl
= NULL
;
498 if (strcmp(value
, "empty") == 0)
501 if (strncmp(value
, "[==$", 4) == 0) {
502 struct expression
*arg
;
505 if (!str_to_comparison_arg(value
, call
, &comparison
, &arg
))
507 if (!get_implied_rl(arg
, &rl
))
512 str_to_rl_helper(call
, type
, value
, &c
, &rl
);
516 call_math
= jump_to_call_math(value
);
517 if (call_math
&& parse_call_math_rl(call
, call_math
, &math_rl
)) {
518 rl
= rl_intersection(rl
, math_rl
);
523 * For now if we already tried to handle the call math and couldn't
524 * figure it out then bail.
526 if (jump_to_call_math(c
) == c
+ 1)
529 rl
= filter_by_comparison_call(c
, call
, &c
, rl
);
532 rl
= cast_rl(type
, rl
);
533 dinfo
->value_ranges
= rl
;
536 void str_to_rl(struct symbol
*type
, char *value
, struct range_list
**rl
)
538 struct data_info dinfo
= {};
540 str_to_dinfo(NULL
, type
, value
, &dinfo
);
541 *rl
= dinfo
.value_ranges
;
544 void call_results_to_rl(struct expression
*expr
, struct symbol
*type
, char *value
, struct range_list
**rl
)
546 struct data_info dinfo
= {};
548 str_to_dinfo(strip_expr(expr
), type
, value
, &dinfo
);
549 *rl
= dinfo
.value_ranges
;
552 int is_whole_rl(struct range_list
*rl
)
554 struct data_range
*drange
;
556 if (ptr_list_empty(rl
))
558 drange
= first_ptr_list((struct ptr_list
*)rl
);
559 if (sval_is_min(drange
->min
) && sval_is_max(drange
->max
))
564 int is_whole_rl_non_zero(struct range_list
*rl
)
566 struct data_range
*drange
;
568 if (ptr_list_empty(rl
))
570 drange
= first_ptr_list((struct ptr_list
*)rl
);
571 if (sval_unsigned(drange
->min
) &&
572 drange
->min
.value
== 1 &&
573 sval_is_max(drange
->max
))
575 if (!sval_is_min(drange
->min
) || drange
->max
.value
!= -1)
577 drange
= last_ptr_list((struct ptr_list
*)rl
);
578 if (drange
->min
.value
!= 1 || !sval_is_max(drange
->max
))
583 sval_t
rl_min(struct range_list
*rl
)
585 struct data_range
*drange
;
588 ret
.type
= &llong_ctype
;
589 ret
.value
= LLONG_MIN
;
590 if (ptr_list_empty(rl
))
592 drange
= first_ptr_list((struct ptr_list
*)rl
);
596 sval_t
rl_max(struct range_list
*rl
)
598 struct data_range
*drange
;
601 ret
.type
= &llong_ctype
;
602 ret
.value
= LLONG_MAX
;
603 if (ptr_list_empty(rl
))
605 drange
= last_ptr_list((struct ptr_list
*)rl
);
609 int rl_to_sval(struct range_list
*rl
, sval_t
*sval
)
618 if (sval_cmp(min
, max
) != 0)
624 struct symbol
*rl_type(struct range_list
*rl
)
628 return rl_min(rl
).type
;
631 static struct data_range
*alloc_range_helper_sval(sval_t min
, sval_t max
, int perm
)
633 struct data_range
*ret
;
636 ret
= __alloc_perm_data_range(0);
638 ret
= __alloc_data_range(0);
644 struct data_range
*alloc_range(sval_t min
, sval_t max
)
646 return alloc_range_helper_sval(min
, max
, 0);
649 struct data_range
*alloc_range_perm(sval_t min
, sval_t max
)
651 return alloc_range_helper_sval(min
, max
, 1);
654 struct range_list
*alloc_rl(sval_t min
, sval_t max
)
656 struct range_list
*rl
= NULL
;
658 if (sval_cmp(min
, max
) > 0)
659 return alloc_whole_rl(min
.type
);
661 add_range(&rl
, min
, max
);
665 struct range_list
*alloc_whole_rl(struct symbol
*type
)
667 if (!type
|| type_positive_bits(type
) < 0)
669 if (type
->type
== SYM_ARRAY
)
672 return alloc_rl(sval_type_min(type
), sval_type_max(type
));
675 extern int rl_ptrlist_hack
;
676 void add_range(struct range_list
**list
, sval_t min
, sval_t max
)
678 struct data_range
*tmp
;
679 struct data_range
*new = NULL
;
683 * There is at least on valid reason why the types might be confusing
684 * and that's when you have a void pointer and on some paths you treat
685 * it as a u8 pointer and on other paths you treat it as a u16 pointer.
686 * This case is hard to deal with.
688 * There are other cases where we probably should be more specific about
689 * the types than we are. For example, we end up merging a lot of ulong
690 * with pointers and I have not figured out why we do that.
692 * But this hack works for both cases, I think. We cast it to pointers
693 * or we use the bigger size.
696 if (*list
&& rl_type(*list
) != min
.type
) {
697 if (rl_type(*list
)->type
== SYM_PTR
) {
698 min
= sval_cast(rl_type(*list
), min
);
699 max
= sval_cast(rl_type(*list
), max
);
700 } else if (min
.type
->type
== SYM_PTR
) {
701 *list
= cast_rl(min
.type
, *list
);
702 } else if (type_bits(rl_type(*list
)) >= type_bits(min
.type
)) {
703 min
= sval_cast(rl_type(*list
), min
);
704 max
= sval_cast(rl_type(*list
), max
);
706 *list
= cast_rl(min
.type
, *list
);
710 if (sval_cmp(min
, max
) > 0) {
711 min
= sval_type_min(min
.type
);
712 max
= sval_type_max(min
.type
);
716 * FIXME: This has a problem merging a range_list like: min-0,3-max
717 * with a range like 1-2. You end up with min-2,3-max instead of
720 FOR_EACH_PTR(*list
, tmp
) {
722 /* Sometimes we overlap with more than one range
723 so we have to delete or modify the next range. */
724 if (!sval_is_max(max
) && max
.value
+ 1 == tmp
->min
.value
) {
725 /* join 2 ranges here */
727 DELETE_CURRENT_PTR(tmp
);
731 /* Doesn't overlap with the next one. */
732 if (sval_cmp(max
, tmp
->min
) < 0)
735 if (sval_cmp(max
, tmp
->max
) <= 0) {
736 /* Partially overlaps the next one. */
738 DELETE_CURRENT_PTR(tmp
);
741 /* Completely overlaps the next one. */
742 DELETE_CURRENT_PTR(tmp
);
743 /* there could be more ranges to delete */
747 if (!sval_is_max(max
) && max
.value
+ 1 == tmp
->min
.value
) {
748 /* join 2 ranges into a big range */
749 new = alloc_range(min
, tmp
->max
);
750 REPLACE_CURRENT_PTR(tmp
, new);
753 if (sval_cmp(max
, tmp
->min
) < 0) { /* new range entirely below */
754 new = alloc_range(min
, max
);
755 INSERT_CURRENT(new, tmp
);
758 if (sval_cmp(min
, tmp
->min
) < 0) { /* new range partially below */
759 if (sval_cmp(max
, tmp
->max
) < 0)
763 new = alloc_range(min
, max
);
764 REPLACE_CURRENT_PTR(tmp
, new);
769 if (sval_cmp(max
, tmp
->max
) <= 0) /* new range already included */
771 if (sval_cmp(min
, tmp
->max
) <= 0) { /* new range partially above */
773 new = alloc_range(min
, max
);
774 REPLACE_CURRENT_PTR(tmp
, new);
778 if (!sval_is_min(min
) && min
.value
- 1 == tmp
->max
.value
) {
779 /* join 2 ranges into a big range */
780 new = alloc_range(tmp
->min
, max
);
781 REPLACE_CURRENT_PTR(tmp
, new);
785 /* the new range is entirely above the existing ranges */
786 } END_FOR_EACH_PTR(tmp
);
789 new = alloc_range(min
, max
);
792 add_ptr_list(list
, new);
796 struct range_list
*clone_rl(struct range_list
*list
)
798 struct data_range
*tmp
;
799 struct range_list
*ret
= NULL
;
801 FOR_EACH_PTR(list
, tmp
) {
802 add_ptr_list(&ret
, tmp
);
803 } END_FOR_EACH_PTR(tmp
);
807 struct range_list
*clone_rl_permanent(struct range_list
*list
)
809 struct data_range
*tmp
;
810 struct data_range
*new;
811 struct range_list
*ret
= NULL
;
813 FOR_EACH_PTR(list
, tmp
) {
814 new = alloc_range_perm(tmp
->min
, tmp
->max
);
815 add_ptr_list(&ret
, new);
816 } END_FOR_EACH_PTR(tmp
);
820 struct range_list
*rl_union(struct range_list
*one
, struct range_list
*two
)
822 struct data_range
*tmp
;
823 struct range_list
*ret
= NULL
;
825 FOR_EACH_PTR(one
, tmp
) {
826 add_range(&ret
, tmp
->min
, tmp
->max
);
827 } END_FOR_EACH_PTR(tmp
);
828 FOR_EACH_PTR(two
, tmp
) {
829 add_range(&ret
, tmp
->min
, tmp
->max
);
830 } END_FOR_EACH_PTR(tmp
);
834 struct range_list
*remove_range(struct range_list
*list
, sval_t min
, sval_t max
)
836 struct data_range
*tmp
;
837 struct range_list
*ret
= NULL
;
842 min
= sval_cast(rl_type(list
), min
);
843 max
= sval_cast(rl_type(list
), max
);
844 if (sval_cmp(min
, max
) > 0) {
850 FOR_EACH_PTR(list
, tmp
) {
851 if (sval_cmp(tmp
->max
, min
) < 0) {
852 add_range(&ret
, tmp
->min
, tmp
->max
);
855 if (sval_cmp(tmp
->min
, max
) > 0) {
856 add_range(&ret
, tmp
->min
, tmp
->max
);
859 if (sval_cmp(tmp
->min
, min
) >= 0 && sval_cmp(tmp
->max
, max
) <= 0)
861 if (sval_cmp(tmp
->min
, min
) >= 0) {
863 add_range(&ret
, max
, tmp
->max
);
864 } else if (sval_cmp(tmp
->max
, max
) <= 0) {
866 add_range(&ret
, tmp
->min
, min
);
870 add_range(&ret
, tmp
->min
, min
);
871 add_range(&ret
, max
, tmp
->max
);
873 } END_FOR_EACH_PTR(tmp
);
877 int ranges_equiv(struct data_range
*one
, struct data_range
*two
)
883 if (sval_cmp(one
->min
, two
->min
) != 0)
885 if (sval_cmp(one
->max
, two
->max
) != 0)
890 int rl_equiv(struct range_list
*one
, struct range_list
*two
)
892 struct data_range
*one_range
;
893 struct data_range
*two_range
;
898 PREPARE_PTR_LIST(one
, one_range
);
899 PREPARE_PTR_LIST(two
, two_range
);
901 if (!one_range
&& !two_range
)
903 if (!ranges_equiv(one_range
, two_range
))
905 NEXT_PTR_LIST(one_range
);
906 NEXT_PTR_LIST(two_range
);
908 FINISH_PTR_LIST(two_range
);
909 FINISH_PTR_LIST(one_range
);
914 int true_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
916 switch (comparison
) {
918 case SPECIAL_UNSIGNED_LT
:
919 if (sval_cmp(left
->min
, right
->max
) < 0)
922 case SPECIAL_UNSIGNED_LTE
:
924 if (sval_cmp(left
->min
, right
->max
) <= 0)
928 if (sval_cmp(left
->max
, right
->min
) < 0)
930 if (sval_cmp(left
->min
, right
->max
) > 0)
933 case SPECIAL_UNSIGNED_GTE
:
935 if (sval_cmp(left
->max
, right
->min
) >= 0)
939 case SPECIAL_UNSIGNED_GT
:
940 if (sval_cmp(left
->max
, right
->min
) > 0)
943 case SPECIAL_NOTEQUAL
:
944 if (sval_cmp(left
->min
, left
->max
) != 0)
946 if (sval_cmp(right
->min
, right
->max
) != 0)
948 if (sval_cmp(left
->min
, right
->min
) != 0)
952 sm_msg("unhandled comparison %d\n", comparison
);
958 int true_comparison_range_LR(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
961 return true_comparison_range(var
, comparison
, val
);
963 return true_comparison_range(val
, comparison
, var
);
966 static int false_comparison_range_sval(struct data_range
*left
, int comparison
, struct data_range
*right
)
968 switch (comparison
) {
970 case SPECIAL_UNSIGNED_LT
:
971 if (sval_cmp(left
->max
, right
->min
) >= 0)
974 case SPECIAL_UNSIGNED_LTE
:
976 if (sval_cmp(left
->max
, right
->min
) > 0)
980 if (sval_cmp(left
->min
, left
->max
) != 0)
982 if (sval_cmp(right
->min
, right
->max
) != 0)
984 if (sval_cmp(left
->min
, right
->min
) != 0)
987 case SPECIAL_UNSIGNED_GTE
:
989 if (sval_cmp(left
->min
, right
->max
) < 0)
993 case SPECIAL_UNSIGNED_GT
:
994 if (sval_cmp(left
->min
, right
->max
) <= 0)
997 case SPECIAL_NOTEQUAL
:
998 if (sval_cmp(left
->max
, right
->min
) < 0)
1000 if (sval_cmp(left
->min
, right
->max
) > 0)
1004 sm_msg("unhandled comparison %d\n", comparison
);
1010 int false_comparison_range_LR(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
1013 return false_comparison_range_sval(var
, comparison
, val
);
1015 return false_comparison_range_sval(val
, comparison
, var
);
1018 int possibly_true(struct expression
*left
, int comparison
, struct expression
*right
)
1020 struct range_list
*rl_left
, *rl_right
;
1021 struct data_range
*tmp_left
, *tmp_right
;
1022 struct symbol
*type
;
1024 if (!get_implied_rl(left
, &rl_left
))
1026 if (!get_implied_rl(right
, &rl_right
))
1029 type
= rl_type(rl_left
);
1030 if (type_positive_bits(type
) < type_positive_bits(rl_type(rl_right
)))
1031 type
= rl_type(rl_right
);
1032 if (type_positive_bits(type
) < 31)
1035 rl_left
= cast_rl(type
, rl_left
);
1036 rl_right
= cast_rl(type
, rl_right
);
1038 FOR_EACH_PTR(rl_left
, tmp_left
) {
1039 FOR_EACH_PTR(rl_right
, tmp_right
) {
1040 if (true_comparison_range(tmp_left
, comparison
, tmp_right
))
1042 } END_FOR_EACH_PTR(tmp_right
);
1043 } END_FOR_EACH_PTR(tmp_left
);
1047 int possibly_false(struct expression
*left
, int comparison
, struct expression
*right
)
1049 struct range_list
*rl_left
, *rl_right
;
1050 struct data_range
*tmp_left
, *tmp_right
;
1051 struct symbol
*type
;
1053 if (!get_implied_rl(left
, &rl_left
))
1055 if (!get_implied_rl(right
, &rl_right
))
1058 type
= rl_type(rl_left
);
1059 if (type_positive_bits(type
) < type_positive_bits(rl_type(rl_right
)))
1060 type
= rl_type(rl_right
);
1061 if (type_positive_bits(type
) < 31)
1064 rl_left
= cast_rl(type
, rl_left
);
1065 rl_right
= cast_rl(type
, rl_right
);
1067 FOR_EACH_PTR(rl_left
, tmp_left
) {
1068 FOR_EACH_PTR(rl_right
, tmp_right
) {
1069 if (false_comparison_range_sval(tmp_left
, comparison
, tmp_right
))
1071 } END_FOR_EACH_PTR(tmp_right
);
1072 } END_FOR_EACH_PTR(tmp_left
);
1076 int possibly_true_rl(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
1078 struct data_range
*left_tmp
, *right_tmp
;
1079 struct symbol
*type
;
1081 if (!left_ranges
|| !right_ranges
)
1084 type
= rl_type(left_ranges
);
1085 if (type_positive_bits(type
) < type_positive_bits(rl_type(right_ranges
)))
1086 type
= rl_type(right_ranges
);
1087 if (type_positive_bits(type
) < 31)
1090 left_ranges
= cast_rl(type
, left_ranges
);
1091 right_ranges
= cast_rl(type
, right_ranges
);
1093 FOR_EACH_PTR(left_ranges
, left_tmp
) {
1094 FOR_EACH_PTR(right_ranges
, right_tmp
) {
1095 if (true_comparison_range(left_tmp
, comparison
, right_tmp
))
1097 } END_FOR_EACH_PTR(right_tmp
);
1098 } END_FOR_EACH_PTR(left_tmp
);
1102 int possibly_false_rl(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
1104 struct data_range
*left_tmp
, *right_tmp
;
1105 struct symbol
*type
;
1107 if (!left_ranges
|| !right_ranges
)
1110 type
= rl_type(left_ranges
);
1111 if (type_positive_bits(type
) < type_positive_bits(rl_type(right_ranges
)))
1112 type
= rl_type(right_ranges
);
1113 if (type_positive_bits(type
) < 31)
1116 left_ranges
= cast_rl(type
, left_ranges
);
1117 right_ranges
= cast_rl(type
, right_ranges
);
1119 FOR_EACH_PTR(left_ranges
, left_tmp
) {
1120 FOR_EACH_PTR(right_ranges
, right_tmp
) {
1121 if (false_comparison_range_sval(left_tmp
, comparison
, right_tmp
))
1123 } END_FOR_EACH_PTR(right_tmp
);
1124 } END_FOR_EACH_PTR(left_tmp
);
1128 /* FIXME: the _rl here stands for right left so really it should be _lr */
1129 int possibly_true_rl_LR(int comparison
, struct range_list
*a
, struct range_list
*b
, int left
)
1132 return possibly_true_rl(a
, comparison
, b
);
1134 return possibly_true_rl(b
, comparison
, a
);
1137 int possibly_false_rl_LR(int comparison
, struct range_list
*a
, struct range_list
*b
, int left
)
1140 return possibly_false_rl(a
, comparison
, b
);
1142 return possibly_false_rl(b
, comparison
, a
);
1145 int rl_has_sval(struct range_list
*rl
, sval_t sval
)
1147 struct data_range
*tmp
;
1149 FOR_EACH_PTR(rl
, tmp
) {
1150 if (sval_cmp(tmp
->min
, sval
) <= 0 &&
1151 sval_cmp(tmp
->max
, sval
) >= 0)
1153 } END_FOR_EACH_PTR(tmp
);
1157 void tack_on(struct range_list
**list
, struct data_range
*drange
)
1159 add_ptr_list(list
, drange
);
1162 void push_rl(struct range_list_stack
**rl_stack
, struct range_list
*rl
)
1164 add_ptr_list(rl_stack
, rl
);
1167 struct range_list
*pop_rl(struct range_list_stack
**rl_stack
)
1169 struct range_list
*rl
;
1171 rl
= last_ptr_list((struct ptr_list
*)*rl_stack
);
1172 delete_ptr_list_last((struct ptr_list
**)rl_stack
);
1176 struct range_list
*top_rl(struct range_list_stack
*rl_stack
)
1178 struct range_list
*rl
;
1180 rl
= last_ptr_list((struct ptr_list
*)rl_stack
);
1184 void filter_top_rl(struct range_list_stack
**rl_stack
, struct range_list
*filter
)
1186 struct range_list
*rl
;
1188 rl
= pop_rl(rl_stack
);
1189 rl
= rl_filter(rl
, filter
);
1190 push_rl(rl_stack
, rl
);
1193 struct range_list
*rl_truncate_cast(struct symbol
*type
, struct range_list
*rl
)
1195 struct data_range
*tmp
;
1196 struct range_list
*ret
= NULL
;
1202 if (!type
|| type
== rl_type(rl
))
1205 FOR_EACH_PTR(rl
, tmp
) {
1208 if (type_bits(type
) < type_bits(rl_type(rl
))) {
1209 min
.uvalue
= tmp
->min
.uvalue
& ((1ULL << type_bits(type
)) - 1);
1210 max
.uvalue
= tmp
->max
.uvalue
& ((1ULL << type_bits(type
)) - 1);
1212 if (sval_cmp(min
, max
) > 0) {
1213 min
= sval_cast(type
, min
);
1214 max
= sval_cast(type
, max
);
1216 add_range_t(type
, &ret
, min
, max
);
1217 } END_FOR_EACH_PTR(tmp
);
1222 static int rl_is_sane(struct range_list
*rl
)
1224 struct data_range
*tmp
;
1225 struct symbol
*type
;
1228 FOR_EACH_PTR(rl
, tmp
) {
1229 if (!sval_fits(type
, tmp
->min
))
1231 if (!sval_fits(type
, tmp
->max
))
1233 if (sval_cmp(tmp
->min
, tmp
->max
) > 0)
1235 } END_FOR_EACH_PTR(tmp
);
1240 static int rl_type_consistent(struct range_list
*rl
)
1242 struct data_range
*tmp
;
1243 struct symbol
*type
;
1246 FOR_EACH_PTR(rl
, tmp
) {
1247 if (type
!= tmp
->min
.type
|| type
!= tmp
->max
.type
)
1249 } END_FOR_EACH_PTR(tmp
);
1253 static struct range_list
*cast_to_bool(struct range_list
*rl
)
1255 struct data_range
*tmp
;
1256 struct range_list
*ret
= NULL
;
1259 sval_t min
= { .type
= &bool_ctype
};
1260 sval_t max
= { .type
= &bool_ctype
};
1262 FOR_EACH_PTR(rl
, tmp
) {
1263 if (tmp
->min
.value
|| tmp
->max
.value
)
1265 if (sval_is_negative(tmp
->min
) &&
1266 sval_is_negative(tmp
->max
))
1268 if (tmp
->min
.value
== 0 ||
1269 tmp
->max
.value
== 0)
1271 if (sval_is_negative(tmp
->min
) &&
1274 } END_FOR_EACH_PTR(tmp
);
1281 add_range(&ret
, min
, max
);
1285 struct range_list
*cast_rl(struct symbol
*type
, struct range_list
*rl
)
1287 struct data_range
*tmp
;
1288 struct range_list
*ret
= NULL
;
1295 if (!rl_is_sane(rl
))
1296 return alloc_whole_rl(type
);
1297 if (type
== rl_type(rl
) && rl_type_consistent(rl
))
1300 if (type
== &bool_ctype
)
1301 return cast_to_bool(rl
);
1303 FOR_EACH_PTR(rl
, tmp
) {
1304 add_range_t(type
, &ret
, tmp
->min
, tmp
->max
);
1305 } END_FOR_EACH_PTR(tmp
);
1308 return alloc_whole_rl(type
);
1313 struct range_list
*rl_invert(struct range_list
*orig
)
1315 struct range_list
*ret
= NULL
;
1316 struct data_range
*tmp
;
1317 sval_t gap_min
, abs_max
, sval
;
1321 if (type_bits(rl_type(orig
)) < 0) /* void type mostly */
1324 gap_min
= sval_type_min(rl_min(orig
).type
);
1325 abs_max
= sval_type_max(rl_max(orig
).type
);
1327 FOR_EACH_PTR(orig
, tmp
) {
1328 if (sval_cmp(tmp
->min
, gap_min
) > 0) {
1329 sval
= sval_type_val(tmp
->min
.type
, tmp
->min
.value
- 1);
1330 add_range(&ret
, gap_min
, sval
);
1332 if (sval_cmp(tmp
->max
, abs_max
) == 0)
1334 gap_min
= sval_type_val(tmp
->max
.type
, tmp
->max
.value
+ 1);
1335 } END_FOR_EACH_PTR(tmp
);
1337 if (sval_cmp(gap_min
, abs_max
) <= 0)
1338 add_range(&ret
, gap_min
, abs_max
);
1343 struct range_list
*rl_filter(struct range_list
*rl
, struct range_list
*filter
)
1345 struct data_range
*tmp
;
1347 FOR_EACH_PTR(filter
, tmp
) {
1348 rl
= remove_range(rl
, tmp
->min
, tmp
->max
);
1349 } END_FOR_EACH_PTR(tmp
);
1354 struct range_list
*rl_intersection(struct range_list
*one
, struct range_list
*two
)
1356 struct range_list
*one_orig
;
1357 struct range_list
*two_orig
;
1358 struct range_list
*ret
;
1359 struct symbol
*ret_type
;
1360 struct symbol
*small_type
;
1361 struct symbol
*large_type
;
1371 ret_type
= rl_type(one
);
1372 small_type
= rl_type(one
);
1373 large_type
= rl_type(two
);
1375 if (type_bits(rl_type(two
)) < type_bits(small_type
)) {
1376 small_type
= rl_type(two
);
1377 large_type
= rl_type(one
);
1380 one
= cast_rl(large_type
, one
);
1381 two
= cast_rl(large_type
, two
);
1384 one
= rl_invert(one
);
1385 two
= rl_invert(two
);
1387 ret
= rl_filter(ret
, one
);
1388 ret
= rl_filter(ret
, two
);
1390 one
= cast_rl(small_type
, one_orig
);
1391 two
= cast_rl(small_type
, two_orig
);
1393 one
= rl_invert(one
);
1394 two
= rl_invert(two
);
1396 ret
= cast_rl(small_type
, ret
);
1397 ret
= rl_filter(ret
, one
);
1398 ret
= rl_filter(ret
, two
);
1400 return cast_rl(ret_type
, ret
);
1403 static struct range_list
*handle_mod_rl(struct range_list
*left
, struct range_list
*right
)
1408 max
= rl_max(right
);
1409 if (sval_is_max(max
))
1414 if (sval_is_negative(max
))
1416 if (sval_cmp(rl_max(left
), max
) < 0)
1420 return alloc_rl(zero
, max
);
1423 static struct range_list
*get_neg_rl(struct range_list
*rl
)
1425 struct data_range
*tmp
;
1426 struct data_range
*new;
1427 struct range_list
*ret
= NULL
;
1431 if (sval_is_positive(rl_min(rl
)))
1434 FOR_EACH_PTR(rl
, tmp
) {
1435 if (sval_is_positive(tmp
->min
))
1437 if (sval_is_positive(tmp
->max
)) {
1438 new = alloc_range(tmp
->min
, tmp
->max
);
1439 new->max
.value
= -1;
1440 add_range(&ret
, new->min
, new->max
);
1443 add_range(&ret
, tmp
->min
, tmp
->max
);
1444 } END_FOR_EACH_PTR(tmp
);
1449 static struct range_list
*get_pos_rl(struct range_list
*rl
)
1451 struct data_range
*tmp
;
1452 struct data_range
*new;
1453 struct range_list
*ret
= NULL
;
1457 if (sval_is_negative(rl_max(rl
)))
1460 FOR_EACH_PTR(rl
, tmp
) {
1461 if (sval_is_negative(tmp
->max
))
1463 if (sval_is_positive(tmp
->min
)) {
1464 add_range(&ret
, tmp
->min
, tmp
->max
);
1467 new = alloc_range(tmp
->min
, tmp
->max
);
1469 add_range(&ret
, new->min
, new->max
);
1470 } END_FOR_EACH_PTR(tmp
);
1475 static struct range_list
*divide_rl_helper(struct range_list
*left
, struct range_list
*right
)
1477 sval_t right_min
, right_max
;
1480 if (!left
|| !right
)
1483 /* let's assume we never divide by zero */
1484 right_min
= rl_min(right
);
1485 right_max
= rl_max(right
);
1486 if (right_min
.value
== 0 && right_max
.value
== 0)
1488 if (right_min
.value
== 0)
1489 right_min
.value
= 1;
1490 if (right_max
.value
== 0)
1491 right_max
.value
= -1;
1493 max
= sval_binop(rl_max(left
), '/', right_min
);
1494 min
= sval_binop(rl_min(left
), '/', right_max
);
1496 return alloc_rl(min
, max
);
1499 static struct range_list
*handle_divide_rl(struct range_list
*left
, struct range_list
*right
)
1501 struct range_list
*left_neg
, *left_pos
, *right_neg
, *right_pos
;
1502 struct range_list
*neg_neg
, *neg_pos
, *pos_neg
, *pos_pos
;
1503 struct range_list
*ret
;
1505 if (is_whole_rl(right
))
1508 left_neg
= get_neg_rl(left
);
1509 left_pos
= get_pos_rl(left
);
1510 right_neg
= get_neg_rl(right
);
1511 right_pos
= get_pos_rl(right
);
1513 neg_neg
= divide_rl_helper(left_neg
, right_neg
);
1514 neg_pos
= divide_rl_helper(left_neg
, right_pos
);
1515 pos_neg
= divide_rl_helper(left_pos
, right_neg
);
1516 pos_pos
= divide_rl_helper(left_pos
, right_pos
);
1518 ret
= rl_union(neg_neg
, neg_pos
);
1519 ret
= rl_union(ret
, pos_neg
);
1520 return rl_union(ret
, pos_pos
);
1523 static struct range_list
*handle_add_mult_rl(struct range_list
*left
, int op
, struct range_list
*right
)
1527 if (sval_binop_overflows(rl_min(left
), op
, rl_min(right
)))
1529 min
= sval_binop(rl_min(left
), op
, rl_min(right
));
1531 if (sval_binop_overflows(rl_max(left
), op
, rl_max(right
)))
1533 max
= sval_binop(rl_max(left
), op
, rl_max(right
));
1535 return alloc_rl(min
, max
);
1538 static struct range_list
*handle_sub_rl(struct range_list
*left_orig
, struct range_list
*right_orig
)
1540 struct range_list
*left_rl
, *right_rl
;
1541 struct symbol
*type
;
1543 sval_t min_ll
, max_ll
, res_ll
;
1546 /* TODO: These things should totally be using dranges where possible */
1548 if (!left_orig
|| !right_orig
)
1552 if (type_positive_bits(rl_type(left_orig
)) > type_positive_bits(type
))
1553 type
= rl_type(left_orig
);
1554 if (type_positive_bits(rl_type(right_orig
)) > type_positive_bits(type
))
1555 type
= rl_type(right_orig
);
1557 left_rl
= cast_rl(type
, left_orig
);
1558 right_rl
= cast_rl(type
, right_orig
);
1560 max
= rl_max(left_rl
);
1561 min
= sval_type_min(type
);
1563 min_ll
= rl_min(left_rl
);
1564 min_ll
.type
= &llong_ctype
;
1565 max_ll
= rl_max(right_rl
);
1566 max_ll
.type
= &llong_ctype
;
1568 res_ll
.value
= min_ll
.value
- max_ll
.value
;
1570 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
1571 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
1572 if (sval_cmp(tmp
, min
) > 0)
1574 } else if (type_positive_bits(type
) < 63 &&
1575 !sval_binop_overflows(min_ll
, '-', max_ll
) &&
1576 (min
.value
!= 0 && sval_cmp(res_ll
, min
) >= 0)) {
1577 struct range_list
*left_casted
, *right_casted
, *result
;
1579 left_casted
= cast_rl(&llong_ctype
, left_orig
);
1580 right_casted
= cast_rl(&llong_ctype
, right_orig
);
1581 result
= handle_sub_rl(left_casted
, right_casted
);
1582 return cast_rl(type
, result
);
1585 if (!sval_is_max(rl_max(left_rl
))) {
1586 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
1587 if (sval_cmp(tmp
, max
) < 0)
1591 if (sval_is_min(min
) && sval_is_max(max
))
1594 return alloc_rl(min
, max
);
1597 static unsigned long long rl_bits_always_set(struct range_list
*rl
)
1599 return sval_fls_mask(rl_min(rl
));
1602 static unsigned long long rl_bits_maybe_set(struct range_list
*rl
)
1604 return sval_fls_mask(rl_max(rl
));
1607 static struct range_list
*handle_OR_rl(struct range_list
*left
, struct range_list
*right
)
1609 unsigned long long left_min
, left_max
, right_min
, right_max
;
1613 if ((rl_to_sval(left
, &sval
) || rl_to_sval(right
, &sval
)) &&
1614 !sval_binop_overflows(rl_max(left
), '+', rl_max(right
)))
1615 return rl_binop(left
, '+', right
);
1617 left_min
= rl_bits_always_set(left
);
1618 left_max
= rl_bits_maybe_set(left
);
1619 right_min
= rl_bits_always_set(right
);
1620 right_max
= rl_bits_maybe_set(right
);
1622 min
.type
= max
.type
= &ullong_ctype
;
1623 min
.uvalue
= left_min
| right_min
;
1624 max
.uvalue
= left_max
| right_max
;
1626 return cast_rl(rl_type(left
), alloc_rl(min
, max
));
1629 static struct range_list
*handle_XOR_rl(struct range_list
*left
, struct range_list
*right
)
1631 unsigned long long left_set
, left_maybe
;
1632 unsigned long long right_set
, right_maybe
;
1635 left_set
= rl_bits_always_set(left
);
1636 left_maybe
= rl_bits_maybe_set(left
);
1638 right_set
= rl_bits_always_set(right
);
1639 right_maybe
= rl_bits_maybe_set(right
);
1641 zero
= max
= rl_min(left
);
1643 max
.uvalue
= fls_mask((left_maybe
| right_maybe
) ^ (left_set
& right_set
));
1645 return cast_rl(rl_type(left
), alloc_rl(zero
, max
));
1648 static struct range_list
*handle_AND_rl(struct range_list
*left
, struct range_list
*right
)
1650 unsigned long long left_set
, left_maybe
;
1651 unsigned long long right_set
, right_maybe
;
1656 left_set
= rl_bits_always_set(left
);
1657 left_maybe
= rl_bits_maybe_set(left
);
1659 right_set
= rl_bits_always_set(right
);
1660 right_maybe
= rl_bits_maybe_set(right
);
1662 zero
= max
= rl_min(left
);
1664 max
.uvalue
= fls_mask((left_maybe
| right_maybe
) ^ (left_set
& right_set
));
1666 return cast_rl(rl_type(left
), alloc_rl(zero
, max
));
1669 struct range_list
*rl_binop(struct range_list
*left
, int op
, struct range_list
*right
)
1671 struct symbol
*cast_type
;
1672 sval_t left_sval
, right_sval
;
1673 struct range_list
*ret
= NULL
;
1675 cast_type
= rl_type(left
);
1676 if (sval_type_max(rl_type(left
)).uvalue
< sval_type_max(rl_type(right
)).uvalue
)
1677 cast_type
= rl_type(right
);
1678 if (sval_type_max(cast_type
).uvalue
< INT_MAX
)
1679 cast_type
= &int_ctype
;
1681 left
= cast_rl(cast_type
, left
);
1682 right
= cast_rl(cast_type
, right
);
1684 if (!left
&& !right
)
1687 if (rl_to_sval(left
, &left_sval
) && rl_to_sval(right
, &right_sval
)) {
1688 sval_t val
= sval_binop(left_sval
, op
, right_sval
);
1689 return alloc_rl(val
, val
);
1694 ret
= handle_mod_rl(left
, right
);
1697 ret
= handle_divide_rl(left
, right
);
1701 ret
= handle_add_mult_rl(left
, op
, right
);
1704 ret
= handle_OR_rl(left
, right
);
1707 ret
= handle_XOR_rl(left
, right
);
1710 ret
= handle_AND_rl(left
, right
);
1713 ret
= handle_sub_rl(left
, right
);
1715 /* FIXME: Do the rest as well */
1716 case SPECIAL_RIGHTSHIFT
:
1717 case SPECIAL_LEFTSHIFT
:
1724 void free_data_info_allocs(void)
1726 struct allocator_struct
*desc
= &data_info_allocator
;
1727 struct allocation_blob
*blob
= desc
->blobs
;
1733 desc
->allocations
= 0;
1734 desc
->total_bytes
= 0;
1735 desc
->useful_bytes
= 0;
1736 desc
->freelist
= NULL
;
1738 struct allocation_blob
*next
= blob
->next
;
1739 blob_free(blob
, desc
->chunking
);
1742 clear_data_range_alloc();
1745 void split_comparison_rl(struct range_list
*left_orig
, int op
, struct range_list
*right_orig
,
1746 struct range_list
**left_true_rl
, struct range_list
**left_false_rl
,
1747 struct range_list
**right_true_rl
, struct range_list
**right_false_rl
)
1749 struct range_list
*left_true
, *left_false
;
1750 struct range_list
*right_true
, *right_false
;
1753 min
= sval_type_min(rl_type(left_orig
));
1754 max
= sval_type_max(rl_type(left_orig
));
1756 left_true
= clone_rl(left_orig
);
1757 left_false
= clone_rl(left_orig
);
1758 right_true
= clone_rl(right_orig
);
1759 right_false
= clone_rl(right_orig
);
1763 case SPECIAL_UNSIGNED_LT
:
1764 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
1765 if (!sval_is_min(rl_min(right_orig
))) {
1766 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1769 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
1770 if (!sval_is_max(rl_max(left_orig
)))
1771 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1773 case SPECIAL_UNSIGNED_LTE
:
1775 if (!sval_is_max(rl_max(right_orig
)))
1776 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1777 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
1779 if (!sval_is_min(rl_min(left_orig
)))
1780 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1781 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
1783 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1784 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
1785 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1786 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
1789 left_true
= rl_intersection(left_orig
, right_orig
);
1790 right_true
= clone_rl(left_true
);
1792 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1793 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1794 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1795 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1797 case SPECIAL_UNSIGNED_GTE
:
1799 if (!sval_is_min(rl_min(right_orig
)))
1800 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
1801 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
1803 if (!sval_is_max(rl_max(left_orig
)))
1804 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
1805 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
1807 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
1808 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
1809 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
1810 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
1813 case SPECIAL_UNSIGNED_GT
:
1814 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
1815 if (!sval_is_max(rl_max(right_orig
)))
1816 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
1818 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
1819 if (!sval_is_min(rl_min(left_orig
)))
1820 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
1822 case SPECIAL_NOTEQUAL
:
1823 left_false
= rl_intersection(left_orig
, right_orig
);
1824 right_false
= clone_rl(left_false
);
1826 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
1827 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
1828 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
1829 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
1832 sm_msg("internal error: unhandled comparison %d", op
);
1837 *left_true_rl
= left_true
;
1838 *left_false_rl
= left_false
;
1840 if (right_true_rl
) {
1841 *right_true_rl
= right_true
;
1842 *right_false_rl
= right_false
;