2 * Copyright (C) 2012 Oracle.
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 * Basically the point of sval is that it can hold both ULLONG_MAX and
20 * LLONG_MIN. If it is an unsigned type then we use sval.uvalue or if it is
21 * signed we use sval.value.
23 * I considered just using one bit to store whether the value was signed vs
24 * unsigned but I think it might help to have the type information so we know
25 * how to do type promotion.
30 #include "smatch_slist.h"
31 #include "smatch_extra.h"
33 __ALLOCATOR(sval_t
, "svals", sval
);
35 sval_t
*sval_alloc(sval_t sval
)
39 ret
= __alloc_sval(0);
44 sval_t
*sval_alloc_permanent(sval_t sval
)
48 ret
= malloc(sizeof(*ret
));
53 sval_t
sval_blank(struct expression
*expr
)
57 ret
.type
= get_type(expr
);
59 ret
.type
= &int_ctype
;
60 ret
.value
= 123456789;
65 sval_t
sval_type_val(struct symbol
*type
, long long val
)
77 sval_t
sval_type_fval(struct symbol
*type
, long double fval
)
81 ret
.type
= &ldouble_ctype
;
83 return sval_cast(type
, ret
);
86 sval_t
sval_from_val(struct expression
*expr
, long long val
)
90 ret
= sval_blank(expr
);
92 ret
= sval_cast(get_type(expr
), ret
);
97 sval_t
sval_from_fval(struct expression
*expr
, long double fval
)
101 ret
.type
= &ldouble_ctype
;
103 ret
= sval_cast(get_type(expr
), ret
);
108 int sval_is_ptr(sval_t sval
)
112 return (sval
.type
->type
== SYM_PTR
|| sval
.type
->type
== SYM_ARRAY
);
115 bool sval_is_fp(sval_t sval
)
117 return type_is_fp(sval
.type
);
120 int sval_unsigned(sval_t sval
)
122 if (is_ptr_type(sval
.type
))
124 return type_unsigned(sval
.type
);
127 int sval_signed(sval_t sval
)
129 return !type_unsigned(sval
.type
);
132 int sval_bits(sval_t sval
)
134 return type_bits(sval
.type
);
137 int sval_bits_used(sval_t sval
)
141 for (i
= 64; i
>= 1; i
--) {
142 if (sval
.uvalue
& (1ULL << (i
- 1)))
148 int sval_is_negative(sval_t sval
)
150 if (type_unsigned(sval
.type
))
157 int sval_is_positive(sval_t sval
)
159 return !sval_is_negative(sval
);
162 static bool fp_is_min(sval_t sval
)
164 if (sval
.type
== &float_ctype
)
165 return sval
.fvalue
== -FLT_MAX
;
166 if (sval
.type
== &double_ctype
)
167 return sval
.dvalue
== -DBL_MAX
;
168 if (sval
.type
== &ldouble_ctype
)
169 return sval
.ldvalue
== -LDBL_MAX
;
170 sm_perror("%s: bad type: '%s'", __func__
, type_to_str(sval
.type
));
174 int sval_is_min(sval_t sval
)
176 sval_t min
= sval_type_min(sval
.type
);
178 if (sval_is_fp(sval
))
179 return fp_is_min(sval
);
181 if (sval_unsigned(sval
)) {
182 if (sval
.uvalue
== 0)
186 /* return true for less than min as well */
187 return (sval
.value
<= min
.value
);
190 static bool fp_is_max(sval_t sval
)
192 if (sval
.type
== &float_ctype
)
193 return sval
.fvalue
== FLT_MAX
;
194 if (sval
.type
== &double_ctype
)
195 return sval
.dvalue
== DBL_MAX
;
196 if (sval
.type
== &ldouble_ctype
)
197 return sval
.ldvalue
== LDBL_MAX
;
198 sm_perror("%s: bad type: '%s'", __func__
, type_to_str(sval
.type
));
202 int sval_is_max(sval_t sval
)
204 sval_t max
= sval_type_max(sval
.type
);
206 if (sval_is_fp(sval
))
207 return fp_is_max(sval
);
209 if (sval_unsigned(sval
))
210 return (sval
.uvalue
>= max
.value
);
211 return (sval
.value
>= max
.value
);
214 int sval_is_a_min(sval_t sval
)
216 if (sval_is_min(sval
))
219 if (sval_is_fp(sval
))
222 if (sval_signed(sval
) && sval
.value
== SHRT_MIN
)
224 if (sval_signed(sval
) && sval
.value
== INT_MIN
)
226 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
231 int sval_is_a_max(sval_t sval
)
233 if (sval_is_max(sval
))
236 if (sval_is_fp(sval
))
239 if (sval
.uvalue
== SHRT_MAX
)
241 if (sval
.uvalue
== INT_MAX
)
243 if (sval
.uvalue
== LLONG_MAX
)
245 if (sval
.uvalue
== USHRT_MAX
)
247 if (sval
.uvalue
== UINT_MAX
)
249 if (sval_unsigned(sval
) && sval
.uvalue
== ULLONG_MAX
)
251 if (sval
.value
> valid_ptr_max
- 1000 &&
252 sval
.value
< valid_ptr_max
+ 1000)
257 int sval_is_negative_min(sval_t sval
)
259 if (sval_is_fp(sval
))
262 if (!sval_is_negative(sval
))
264 return sval_is_min(sval
);
267 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
)
269 sval_t one_cast
, two_cast
;
271 one_cast
= sval_cast(type
, one
);
272 two_cast
= sval_cast(type
, two
);
273 return sval_cmp(one_cast
, two_cast
);
276 int sval_cmp_val(sval_t one
, long long val
)
280 sval
= sval_type_val(&llong_ctype
, val
);
281 return sval_cmp(one
, sval
);
284 sval_t
sval_min(sval_t one
, sval_t two
)
286 if (sval_cmp(one
, two
) > 0)
291 sval_t
sval_min_nonneg(sval_t one
, sval_t two
)
295 if (sval_is_negative(one
) && sval_is_negative(two
)) {
299 if (sval_is_negative(one
))
301 if (sval_is_negative(two
))
304 return sval_min(one
, two
);
307 sval_t
sval_max(sval_t one
, sval_t two
)
309 if (sval_cmp(one
, two
) < 0)
314 int sval_too_low(struct symbol
*type
, sval_t sval
)
316 if (sval_is_negative(sval
) && type_unsigned(type
))
318 if (type_signed(type
) && sval_unsigned(sval
))
320 if (type_signed(sval
.type
) &&
321 sval
.value
< sval_type_min(type
).value
)
323 if (sval_cmp(sval
, sval_type_min(type
)) < 0)
328 int sval_too_high(struct symbol
*type
, sval_t sval
)
330 if (sval_is_negative(sval
))
332 if (sval
.uvalue
> sval_type_max(type
).uvalue
)
337 int sval_fits(struct symbol
*type
, sval_t sval
)
339 /* everything fits into floating point */
340 if (type_is_fp(type
))
342 /* floating points don't fit into int */
343 if (type_is_fp(sval
.type
))
346 if (sval_too_low(type
, sval
))
348 if (sval_too_high(type
, sval
))
353 static sval_t
cast_to_fp(struct symbol
*type
, sval_t sval
)
358 if (type
== &float_ctype
) {
359 if (!sval_is_fp(sval
)) {
360 if (sval_unsigned(sval
))
361 ret
.fvalue
= sval
.uvalue
;
363 ret
.fvalue
= sval
.value
;
364 } else if (sval
.type
== &float_ctype
)
365 ret
.fvalue
= sval
.fvalue
;
366 else if (sval
.type
== &double_ctype
)
367 ret
.fvalue
= sval
.dvalue
;
369 ret
.fvalue
= sval
.ldvalue
;
370 } else if (type
== &double_ctype
) {
371 if (!sval_is_fp(sval
)) {
372 if (sval_unsigned(sval
))
373 ret
.dvalue
= sval
.uvalue
;
375 ret
.dvalue
= sval
.value
;
376 } else if (sval
.type
== &float_ctype
)
377 ret
.dvalue
= sval
.fvalue
;
378 else if (sval
.type
== &double_ctype
)
379 ret
.dvalue
= sval
.dvalue
;
381 ret
.dvalue
= sval
.ldvalue
;
382 } else if (type
== &ldouble_ctype
) {
383 if (!sval_is_fp(sval
)) {
384 if (sval_unsigned(sval
))
385 ret
.ldvalue
= (long double)sval
.uvalue
;
387 ret
.ldvalue
= (long double)sval
.value
;
388 } else if (sval
.type
== &float_ctype
)
389 ret
.ldvalue
= sval
.fvalue
;
390 else if (sval
.type
== &double_ctype
)
391 ret
.ldvalue
= sval
.dvalue
;
393 ret
.ldvalue
= sval
.ldvalue
;
395 sm_perror("%s: bad type: %s", __func__
, type_to_str(type
));
401 static sval_t
cast_from_fp(struct symbol
*type
, sval_t sval
)
405 ret
.type
= &llong_ctype
;
406 if (sval
.type
== &float_ctype
)
407 ret
.value
= sval
.fvalue
;
408 else if (sval
.type
== &double_ctype
)
409 ret
.value
= sval
.dvalue
;
410 else if (sval
.type
== &ldouble_ctype
)
411 ret
.value
= sval
.ldvalue
;
413 sm_perror("%s: bad type: %s", __func__
, type_to_str(type
));
415 return sval_cast(type
, ret
);
418 sval_t
sval_cast(struct symbol
*type
, sval_t sval
)
425 if (type_is_fp(type
))
426 return cast_to_fp(type
, sval
);
427 if (type_is_fp(sval
.type
))
428 return cast_from_fp(type
, sval
);
431 switch (sval_bits(ret
)) {
433 ret
.value
= !!sval
.value
;
436 if (sval_unsigned(ret
))
437 ret
.value
= (long long)(unsigned char)sval
.value
;
439 ret
.value
= (long long)(char)sval
.value
;
442 if (sval_unsigned(ret
))
443 ret
.value
= (long long)(unsigned short)sval
.value
;
445 ret
.value
= (long long)(short)sval
.value
;
448 if (sval_unsigned(ret
))
449 ret
.value
= (long long)(unsigned int)sval
.value
;
451 ret
.value
= (long long)(int)sval
.value
;
454 ret
.value
= sval
.value
;
460 sval_t
sval_preop(sval_t sval
, int op
)
464 sval
.value
= !sval
.value
;
467 sval
.value
= ~sval
.value
;
468 sval
= sval_cast(sval
.type
, sval
);
471 sval
.value
= -sval
.value
;
472 sval
= sval_cast(sval
.type
, sval
);
478 static sval_t
sval_binop_unsigned(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
485 ret
.uvalue
= left
.uvalue
* right
.uvalue
;
488 if (right
.uvalue
== 0) {
489 sm_debug("%s: divide by zero", __func__
);
490 ret
.uvalue
= 123456789;
492 ret
.uvalue
= left
.uvalue
/ right
.uvalue
;
496 ret
.uvalue
= left
.uvalue
+ right
.uvalue
;
499 ret
.uvalue
= left
.uvalue
- right
.uvalue
;
502 if (right
.uvalue
== 0) {
503 sm_perror(" %s: MOD by zero", __func__
);
504 ret
.uvalue
= 123456789;
506 ret
.uvalue
= left
.uvalue
% right
.uvalue
;
510 ret
.uvalue
= left
.uvalue
| right
.uvalue
;
513 ret
.uvalue
= left
.uvalue
& right
.uvalue
;
515 case SPECIAL_RIGHTSHIFT
:
516 ret
.uvalue
= left
.uvalue
>> right
.uvalue
;
518 case SPECIAL_LEFTSHIFT
:
519 ret
.uvalue
= left
.uvalue
<< right
.uvalue
;
522 ret
.uvalue
= left
.uvalue
^ right
.uvalue
;
525 sm_perror(" %s: unhandled binop %s", __func__
,
527 ret
.uvalue
= 1234567;
533 static sval_t
sval_binop_signed(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
540 ret
.value
= left
.value
* right
.value
;
543 if (right
.value
== 0) {
544 sm_debug("%s: divide by zero", __func__
);
545 ret
.value
= 123456789;
546 } else if (left
.value
== LLONG_MIN
&& right
.value
== -1) {
547 sm_debug("%s: invalid divide LLONG_MIN/-1", __func__
);
548 ret
.value
= 12345678;
550 ret
.value
= left
.value
/ right
.value
;
554 ret
.value
= left
.value
+ right
.value
;
557 ret
.value
= left
.value
- right
.value
;
560 if (right
.value
== 0) {
561 sm_perror(" %s: MOD by zero", __func__
);
562 ret
.value
= 123456789;
564 ret
.value
= left
.value
% right
.value
;
568 ret
.value
= left
.value
| right
.value
;
571 ret
.value
= left
.value
& right
.value
;
573 case SPECIAL_RIGHTSHIFT
:
574 ret
.value
= left
.value
>> right
.value
;
576 case SPECIAL_LEFTSHIFT
:
577 ret
.value
= left
.value
<< right
.value
;
580 ret
.value
= left
.value
^ right
.value
;
583 sm_perror(" %s: unhandled binop %s", __func__
,
590 static sval_t
ptr_binop(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
595 if (op
!= '+' && op
!= '-')
596 return sval_binop_unsigned(type
, left
, op
, right
);
599 if (type
->type
== SYM_PTR
)
600 type
= get_real_base_type(type
);
601 align
= type
->ctype
.alignment
;
606 if (type_is_ptr(left
.type
))
607 ret
.value
= left
.value
+ right
.value
* align
;
609 ret
.value
= left
.value
* align
+ right
.value
;
611 if (!type_is_ptr(left
.type
)) {
612 left
.value
= -left
.value
;
613 ret
= ptr_binop(type
, left
, '+', right
);
614 } else if (!type_is_ptr(right
.type
)) {
615 right
.value
= -right
.value
;
616 ret
= ptr_binop(type
, left
, '+', right
);
618 ret
.value
= (left
.value
- right
.value
) / align
;
623 ret
.type
= ssize_t_ctype
;
627 sval_t
sval_binop(sval_t left
, int op
, sval_t right
)
632 type
= get_promoted_type(left
.type
, right
.type
);
634 if (type_is_ptr(type
))
635 ret
= ptr_binop(type
, left
, op
, right
);
636 else if (type_unsigned(type
))
637 ret
= sval_binop_unsigned(type
, left
, op
, right
);
639 ret
= sval_binop_signed(type
, left
, op
, right
);
640 return sval_cast(type
, ret
);
643 int sval_unop_overflows(sval_t sval
, int op
)
647 if (sval_positive_bits(sval
) == 32 && sval
.value
== INT_MIN
)
649 if (sval_positive_bits(sval
) == 64 && sval
.value
== LLONG_MIN
)
651 if (sval_is_negative(sval
))
653 if (sval_signed(sval
))
655 if (sval_bits(sval
) == 32 && sval
.uvalue
> INT_MAX
)
657 if (sval_bits(sval
) == 64 && sval
.uvalue
> LLONG_MAX
)
662 int sval_binop_overflows(sval_t left
, int op
, sval_t right
)
668 if (type_positive_bits(right
.type
) > type_positive_bits(left
.type
))
670 if (type_positive_bits(type
) < 31)
673 max
= sval_type_max(type
);
674 min
= sval_type_min(type
);
678 if (sval_is_negative(left
) && sval_is_negative(right
)) {
679 if (left
.value
< min
.value
+ right
.value
)
683 if (sval_is_negative(left
) || sval_is_negative(right
))
685 if (left
.uvalue
> max
.uvalue
- right
.uvalue
)
689 if (type_signed(type
)) {
690 if (left
.value
== 0 || right
.value
== 0)
692 if (left
.value
> max
.value
/ right
.value
)
694 if (left
.value
== -1 || right
.value
== -1)
696 return left
.value
!= left
.value
* right
.value
/ right
.value
;
699 return right
.uvalue
!= 0 && left
.uvalue
> max
.uvalue
/ right
.uvalue
;
701 if (type_unsigned(type
)) {
702 if (sval_cmp(left
, right
) < 0)
706 if (sval_is_negative(left
) && sval_is_negative(right
))
709 if (sval_is_negative(left
)) {
710 if (left
.value
< min
.value
+ right
.value
)
714 if (sval_is_negative(right
)) {
715 if (right
.value
== min
.value
)
717 right
= sval_preop(right
, '-');
718 if (sval_binop_overflows(left
, '+', right
))
723 case SPECIAL_LEFTSHIFT
:
724 if (sval_cmp(left
, sval_binop(max
, invert_op(op
), right
)) > 0)
731 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
)
737 if (type_positive_bits(right
.type
) > type_positive_bits(left
.type
))
739 if (type_positive_bits(type
) <= 31)
742 type
= &ullong_ctype
;
744 left
= sval_cast(type
, left
);
745 right
= sval_cast(type
, right
);
746 return sval_binop_overflows(left
, op
, right
);
749 int find_first_zero_bit(unsigned long long uvalue
)
753 for (i
= 0; i
< 64; i
++) {
754 if (!(uvalue
& (1ULL << i
)))
760 int sm_fls64(unsigned long long uvalue
)
772 unsigned long long fls_mask(unsigned long long uvalue
)
776 high_bit
= sm_fls64(uvalue
);
780 return ((unsigned long long)-1) >> (64 - high_bit
);
783 unsigned long long sval_fls_mask(sval_t sval
)
785 return fls_mask(sval
.uvalue
);
788 static char *fp_to_str(sval_t sval
)
792 if (sval
.type
== &float_ctype
)
793 snprintf(buf
, sizeof(buf
), "%f", sval
.fvalue
);
794 else if (sval
.type
== &double_ctype
)
795 snprintf(buf
, sizeof(buf
), "%e", sval
.dvalue
);
796 else if (sval
.type
== &ldouble_ctype
) {
797 snprintf(buf
, sizeof(buf
), "%Lf", sval
.ldvalue
);
799 snprintf(buf
, sizeof(buf
), "nan");
801 return alloc_sname(buf
);
804 const char *sval_to_str(sval_t sval
)
808 if (sval_is_fp(sval
))
809 return fp_to_str(sval
);
811 if (sval_is_ptr(sval
) && sval
.value
== valid_ptr_max
)
813 if (sval_unsigned(sval
) && sval
.value
== ULLONG_MAX
)
815 if (sval_unsigned(sval
) && sval
.value
== UINT_MAX
)
817 if (sval
.value
== USHRT_MAX
)
820 if (sval_signed(sval
) && sval
.value
== LLONG_MAX
)
822 if (sval
.value
== INT_MAX
)
824 if (sval
.value
== SHRT_MAX
)
827 if (sval_signed(sval
) && sval
.value
== SHRT_MIN
)
829 if (sval_signed(sval
) && sval
.value
== INT_MIN
)
831 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
834 if (sval_unsigned(sval
))
835 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
836 else if (sval
.value
< 0)
837 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
839 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
841 return alloc_sname(buf
);
844 const char *sval_to_str_or_err_ptr(sval_t sval
)
848 if (option_project
!= PROJ_KERNEL
||
849 !is_ptr_type(sval
.type
))
850 return sval_to_str(sval
);
852 if (!sval_is_fp(sval
) &&
853 sval_cmp(sval
, valid_ptr_max_sval
) > 0) {
854 snprintf(buf
, sizeof(buf
), "(%lld)", sign_extend_err_ptr(sval
.value
));
855 return alloc_sname(buf
);
858 return sval_to_str(sval
);
861 const char *sval_to_numstr(sval_t sval
)
865 if (type_is_fp(sval
.type
))
866 return fp_to_str(sval
);
868 if (sval_unsigned(sval
))
869 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
870 else if (sval
.value
< 0)
871 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
873 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
875 return alloc_sname(buf
);
878 sval_t
ll_to_sval(long long val
)
882 ret
.type
= &llong_ctype
;
887 static void free_svals(struct symbol
*sym
)
894 void register_sval(int my_id
)
896 add_hook(&free_svals
, AFTER_FUNC_HOOK
);