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_max(sval_t one
, sval_t two
)
293 if (sval_cmp(one
, two
) < 0)
298 int sval_too_low(struct symbol
*type
, sval_t sval
)
300 if (sval_is_negative(sval
) && type_unsigned(type
))
302 if (type_signed(type
) && sval_unsigned(sval
))
304 if (type_signed(sval
.type
) &&
305 sval
.value
< sval_type_min(type
).value
)
307 if (sval_cmp(sval
, sval_type_min(type
)) < 0)
312 int sval_too_high(struct symbol
*type
, sval_t sval
)
314 if (sval_is_negative(sval
))
316 if (sval
.uvalue
> sval_type_max(type
).uvalue
)
321 int sval_fits(struct symbol
*type
, sval_t sval
)
323 /* everything fits into floating point */
324 if (type_is_fp(type
))
326 /* floating points don't fit into int */
327 if (type_is_fp(sval
.type
))
330 if (sval_too_low(type
, sval
))
332 if (sval_too_high(type
, sval
))
337 static sval_t
cast_to_fp(struct symbol
*type
, sval_t sval
)
342 if (type
== &float_ctype
) {
343 if (!sval_is_fp(sval
)) {
344 if (sval_unsigned(sval
))
345 ret
.fvalue
= sval
.uvalue
;
347 ret
.fvalue
= sval
.value
;
348 } else if (sval
.type
== &float_ctype
)
349 ret
.fvalue
= sval
.fvalue
;
350 else if (sval
.type
== &double_ctype
)
351 ret
.fvalue
= sval
.dvalue
;
353 ret
.fvalue
= sval
.ldvalue
;
354 } else if (type
== &double_ctype
) {
355 if (!sval_is_fp(sval
)) {
356 if (sval_unsigned(sval
))
357 ret
.dvalue
= sval
.uvalue
;
359 ret
.dvalue
= sval
.value
;
360 } else if (sval
.type
== &float_ctype
)
361 ret
.dvalue
= sval
.fvalue
;
362 else if (sval
.type
== &double_ctype
)
363 ret
.dvalue
= sval
.dvalue
;
365 ret
.dvalue
= sval
.ldvalue
;
366 } else if (type
== &ldouble_ctype
) {
367 if (!sval_is_fp(sval
)) {
368 if (sval_unsigned(sval
))
369 ret
.ldvalue
= (long double)sval
.uvalue
;
371 ret
.ldvalue
= (long double)sval
.value
;
372 } else if (sval
.type
== &float_ctype
)
373 ret
.ldvalue
= sval
.fvalue
;
374 else if (sval
.type
== &double_ctype
)
375 ret
.ldvalue
= sval
.dvalue
;
377 ret
.ldvalue
= sval
.ldvalue
;
379 sm_perror("%s: bad type: %s", __func__
, type_to_str(type
));
385 static sval_t
cast_from_fp(struct symbol
*type
, sval_t sval
)
389 ret
.type
= &llong_ctype
;
390 if (sval
.type
== &float_ctype
)
391 ret
.value
= sval
.fvalue
;
392 else if (sval
.type
== &double_ctype
)
393 ret
.value
= sval
.dvalue
;
394 else if (sval
.type
== &ldouble_ctype
)
395 ret
.value
= sval
.ldvalue
;
397 sm_perror("%s: bad type: %s", __func__
, type_to_str(type
));
399 return sval_cast(type
, ret
);
402 sval_t
sval_cast(struct symbol
*type
, sval_t sval
)
409 if (type_is_fp(type
))
410 return cast_to_fp(type
, sval
);
411 if (type_is_fp(sval
.type
))
412 return cast_from_fp(type
, sval
);
415 switch (sval_bits(ret
)) {
417 ret
.value
= !!sval
.value
;
420 if (sval_unsigned(ret
))
421 ret
.value
= (long long)(unsigned char)sval
.value
;
423 ret
.value
= (long long)(char)sval
.value
;
426 if (sval_unsigned(ret
))
427 ret
.value
= (long long)(unsigned short)sval
.value
;
429 ret
.value
= (long long)(short)sval
.value
;
432 if (sval_unsigned(ret
))
433 ret
.value
= (long long)(unsigned int)sval
.value
;
435 ret
.value
= (long long)(int)sval
.value
;
438 ret
.value
= sval
.value
;
444 sval_t
sval_preop(sval_t sval
, int op
)
448 sval
.value
= !sval
.value
;
451 sval
.value
= ~sval
.value
;
452 sval
= sval_cast(sval
.type
, sval
);
455 sval
.value
= -sval
.value
;
456 sval
= sval_cast(sval
.type
, sval
);
462 static sval_t
sval_binop_unsigned(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
469 ret
.uvalue
= left
.uvalue
* right
.uvalue
;
472 if (right
.uvalue
== 0) {
473 sm_debug("%s: divide by zero", __func__
);
474 ret
.uvalue
= 123456789;
476 ret
.uvalue
= left
.uvalue
/ right
.uvalue
;
480 ret
.uvalue
= left
.uvalue
+ right
.uvalue
;
483 ret
.uvalue
= left
.uvalue
- right
.uvalue
;
486 if (right
.uvalue
== 0) {
487 sm_perror(" %s: MOD by zero", __func__
);
488 ret
.uvalue
= 123456789;
490 ret
.uvalue
= left
.uvalue
% right
.uvalue
;
494 ret
.uvalue
= left
.uvalue
| right
.uvalue
;
497 ret
.uvalue
= left
.uvalue
& right
.uvalue
;
499 case SPECIAL_RIGHTSHIFT
:
500 ret
.uvalue
= left
.uvalue
>> right
.uvalue
;
502 case SPECIAL_LEFTSHIFT
:
503 ret
.uvalue
= left
.uvalue
<< right
.uvalue
;
506 ret
.uvalue
= left
.uvalue
^ right
.uvalue
;
509 sm_perror(" %s: unhandled binop %s", __func__
,
511 ret
.uvalue
= 1234567;
517 static sval_t
sval_binop_signed(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
524 ret
.value
= left
.value
* right
.value
;
527 if (right
.value
== 0) {
528 sm_debug("%s: divide by zero", __func__
);
529 ret
.value
= 123456789;
530 } else if (left
.value
== LLONG_MIN
&& right
.value
== -1) {
531 sm_debug("%s: invalid divide LLONG_MIN/-1", __func__
);
532 ret
.value
= 12345678;
534 ret
.value
= left
.value
/ right
.value
;
538 ret
.value
= left
.value
+ right
.value
;
541 ret
.value
= left
.value
- right
.value
;
544 if (right
.value
== 0) {
545 sm_perror(" %s: MOD by zero", __func__
);
546 ret
.value
= 123456789;
548 ret
.value
= left
.value
% right
.value
;
552 ret
.value
= left
.value
| right
.value
;
555 ret
.value
= left
.value
& right
.value
;
557 case SPECIAL_RIGHTSHIFT
:
558 ret
.value
= left
.value
>> right
.value
;
560 case SPECIAL_LEFTSHIFT
:
561 ret
.value
= left
.value
<< right
.value
;
564 ret
.value
= left
.value
^ right
.value
;
567 sm_perror(" %s: unhandled binop %s", __func__
,
574 static sval_t
ptr_binop(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
579 if (op
!= '+' && op
!= '-')
580 return sval_binop_unsigned(type
, left
, op
, right
);
583 if (type
->type
== SYM_PTR
)
584 type
= get_real_base_type(type
);
585 align
= type
->ctype
.alignment
;
590 if (type_is_ptr(left
.type
))
591 ret
.value
= left
.value
+ right
.value
* align
;
593 ret
.value
= left
.value
* align
+ right
.value
;
595 if (!type_is_ptr(left
.type
)) {
596 left
.value
= -left
.value
;
597 ret
= ptr_binop(type
, left
, '+', right
);
598 } else if (!type_is_ptr(right
.type
)) {
599 right
.value
= -right
.value
;
600 ret
= ptr_binop(type
, left
, '+', right
);
602 ret
.value
= (left
.value
- right
.value
) / align
;
607 ret
.type
= ssize_t_ctype
;
611 sval_t
sval_binop(sval_t left
, int op
, sval_t right
)
616 type
= get_promoted_type(left
.type
, right
.type
);
618 if (type_is_ptr(type
))
619 ret
= ptr_binop(type
, left
, op
, right
);
620 else if (type_unsigned(type
))
621 ret
= sval_binop_unsigned(type
, left
, op
, right
);
623 ret
= sval_binop_signed(type
, left
, op
, right
);
624 return sval_cast(type
, ret
);
627 int sval_unop_overflows(sval_t sval
, int op
)
631 if (sval_positive_bits(sval
) == 32 && sval
.value
== INT_MIN
)
633 if (sval_positive_bits(sval
) == 64 && sval
.value
== LLONG_MIN
)
635 if (sval_is_negative(sval
))
637 if (sval_signed(sval
))
639 if (sval_bits(sval
) == 32 && sval
.uvalue
> INT_MAX
)
641 if (sval_bits(sval
) == 64 && sval
.uvalue
> LLONG_MAX
)
646 int sval_binop_overflows(sval_t left
, int op
, sval_t right
)
652 if (type_positive_bits(right
.type
) > type_positive_bits(left
.type
))
654 if (type_positive_bits(type
) < 31)
657 max
= sval_type_max(type
);
658 min
= sval_type_min(type
);
662 if (sval_is_negative(left
) && sval_is_negative(right
)) {
663 if (left
.value
< min
.value
+ right
.value
)
667 if (sval_is_negative(left
) || sval_is_negative(right
))
669 if (left
.uvalue
> max
.uvalue
- right
.uvalue
)
673 if (type_signed(type
)) {
674 if (left
.value
== 0 || right
.value
== 0)
676 if (left
.value
> max
.value
/ right
.value
)
678 if (left
.value
== -1 || right
.value
== -1)
680 return left
.value
!= left
.value
* right
.value
/ right
.value
;
683 return right
.uvalue
!= 0 && left
.uvalue
> max
.uvalue
/ right
.uvalue
;
685 if (type_unsigned(type
)) {
686 if (sval_cmp(left
, right
) < 0)
690 if (sval_is_negative(left
) && sval_is_negative(right
))
693 if (sval_is_negative(left
)) {
694 if (left
.value
< min
.value
+ right
.value
)
698 if (sval_is_negative(right
)) {
699 if (right
.value
== min
.value
)
701 right
= sval_preop(right
, '-');
702 if (sval_binop_overflows(left
, '+', right
))
707 case SPECIAL_LEFTSHIFT
:
708 if (sval_cmp(left
, sval_binop(max
, invert_op(op
), right
)) > 0)
715 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
)
721 if (type_positive_bits(right
.type
) > type_positive_bits(left
.type
))
723 if (type_positive_bits(type
) <= 31)
726 type
= &ullong_ctype
;
728 left
= sval_cast(type
, left
);
729 right
= sval_cast(type
, right
);
730 return sval_binop_overflows(left
, op
, right
);
733 int find_first_zero_bit(unsigned long long uvalue
)
737 for (i
= 0; i
< 64; i
++) {
738 if (!(uvalue
& (1ULL << i
)))
744 int sm_fls64(unsigned long long uvalue
)
756 unsigned long long fls_mask(unsigned long long uvalue
)
760 high_bit
= sm_fls64(uvalue
);
764 return ((unsigned long long)-1) >> (64 - high_bit
);
767 unsigned long long sval_fls_mask(sval_t sval
)
769 return fls_mask(sval
.uvalue
);
772 static char *fp_to_str(sval_t sval
)
776 if (sval
.type
== &float_ctype
)
777 snprintf(buf
, sizeof(buf
), "%f", sval
.fvalue
);
778 else if (sval
.type
== &double_ctype
)
779 snprintf(buf
, sizeof(buf
), "%e", sval
.dvalue
);
780 else if (sval
.type
== &ldouble_ctype
) {
781 snprintf(buf
, sizeof(buf
), "%Lf", sval
.ldvalue
);
783 snprintf(buf
, sizeof(buf
), "nan");
785 return alloc_sname(buf
);
788 const char *sval_to_str(sval_t sval
)
792 if (sval_is_fp(sval
))
793 return fp_to_str(sval
);
795 if (sval_is_ptr(sval
) && sval
.value
== valid_ptr_max
)
797 if (sval_unsigned(sval
) && sval
.value
== ULLONG_MAX
)
799 if (sval_unsigned(sval
) && sval
.value
== UINT_MAX
)
801 if (sval
.value
== USHRT_MAX
)
804 if (sval_signed(sval
) && sval
.value
== LLONG_MAX
)
806 if (sval
.value
== INT_MAX
)
808 if (sval
.value
== SHRT_MAX
)
811 if (sval_signed(sval
) && sval
.value
== SHRT_MIN
)
813 if (sval_signed(sval
) && sval
.value
== INT_MIN
)
815 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
818 if (sval_unsigned(sval
))
819 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
820 else if (sval
.value
< 0)
821 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
823 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
825 return alloc_sname(buf
);
828 const char *sval_to_str_or_err_ptr(sval_t sval
)
832 if (option_project
!= PROJ_KERNEL
||
833 !is_ptr_type(sval
.type
))
834 return sval_to_str(sval
);
836 if (!sval_is_fp(sval
) && sval
.uvalue
>= -4905ULL) {
837 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
838 return alloc_sname(buf
);
841 return sval_to_str(sval
);
844 const char *sval_to_numstr(sval_t sval
)
848 if (type_is_fp(sval
.type
))
849 return fp_to_str(sval
);
851 if (sval_unsigned(sval
))
852 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
853 else if (sval
.value
< 0)
854 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
856 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
858 return alloc_sname(buf
);
861 sval_t
ll_to_sval(long long val
)
865 ret
.type
= &llong_ctype
;
870 static void free_svals(struct symbol
*sym
)
877 void register_sval(int my_id
)
879 add_hook(&free_svals
, AFTER_FUNC_HOOK
);