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_from_val(struct expression
*expr
, long long val
)
81 ret
= sval_blank(expr
);
83 ret
= sval_cast(get_type(expr
), ret
);
88 int sval_is_ptr(sval_t sval
)
92 return (sval
.type
->type
== SYM_PTR
|| sval
.type
->type
== SYM_ARRAY
);
95 int sval_unsigned(sval_t sval
)
97 return type_unsigned(sval
.type
);
100 int sval_signed(sval_t sval
)
102 return !type_unsigned(sval
.type
);
105 int sval_bits(sval_t sval
)
107 return type_bits(sval
.type
);
110 int sval_bits_used(sval_t sval
)
114 for (i
= 64; i
>= 1; i
--) {
115 if (sval
.uvalue
& (1ULL << (i
- 1)))
121 int sval_is_negative(sval_t sval
)
123 if (type_unsigned(sval
.type
))
130 int sval_is_positive(sval_t sval
)
132 return !sval_is_negative(sval
);
135 int sval_is_min(sval_t sval
)
137 sval_t min
= sval_type_min(sval
.type
);
139 if (sval_unsigned(sval
)) {
140 if (sval
.uvalue
== 0)
144 /* return true for less than min as well */
145 return (sval
.value
<= min
.value
);
148 int sval_is_max(sval_t sval
)
150 sval_t max
= sval_type_max(sval
.type
);
152 if (sval_unsigned(sval
))
153 return (sval
.uvalue
>= max
.value
);
154 return (sval
.value
>= max
.value
);
157 int sval_is_a_min(sval_t sval
)
159 if (sval_is_min(sval
))
161 if (sval_signed(sval
) && sval
.value
== SHRT_MIN
)
163 if (sval_signed(sval
) && sval
.value
== INT_MIN
)
165 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
170 int sval_is_a_max(sval_t sval
)
172 if (sval_is_max(sval
))
174 if (sval
.uvalue
== SHRT_MAX
)
176 if (sval
.uvalue
== INT_MAX
)
178 if (sval
.uvalue
== LLONG_MAX
)
180 if (sval
.uvalue
== USHRT_MAX
)
182 if (sval
.uvalue
== UINT_MAX
)
184 if (sval_unsigned(sval
) && sval
.uvalue
== ULLONG_MAX
)
186 if (sval
.value
> valid_ptr_max
- 1000 &&
187 sval
.value
< valid_ptr_max
+ 1000)
192 int sval_is_negative_min(sval_t sval
)
194 if (!sval_is_negative(sval
))
196 return sval_is_min(sval
);
199 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
)
201 sval_t one_cast
, two_cast
;
203 one_cast
= sval_cast(type
, one
);
204 two_cast
= sval_cast(type
, two
);
205 return sval_cmp(one_cast
, two_cast
);
208 int sval_cmp_val(sval_t one
, long long val
)
212 sval
= sval_type_val(&llong_ctype
, val
);
213 return sval_cmp(one
, sval
);
216 sval_t
sval_min(sval_t one
, sval_t two
)
218 if (sval_cmp(one
, two
) > 0)
223 sval_t
sval_max(sval_t one
, sval_t two
)
225 if (sval_cmp(one
, two
) < 0)
230 int sval_too_low(struct symbol
*type
, sval_t sval
)
232 if (sval_is_negative(sval
) && type_unsigned(type
))
234 if (type_signed(type
) && sval_unsigned(sval
))
236 if (type_signed(sval
.type
) &&
237 sval
.value
< sval_type_min(type
).value
)
239 if (sval_cmp(sval
, sval_type_min(type
)) < 0)
244 int sval_too_high(struct symbol
*type
, sval_t sval
)
246 if (sval_is_negative(sval
))
248 if (sval
.uvalue
> sval_type_max(type
).uvalue
)
253 int sval_fits(struct symbol
*type
, sval_t sval
)
255 if (sval_too_low(type
, sval
))
257 if (sval_too_high(type
, sval
))
262 sval_t
sval_cast(struct symbol
*type
, sval_t sval
)
270 switch (sval_bits(ret
)) {
272 if (sval_unsigned(ret
))
273 ret
.value
= (long long)(unsigned char)sval
.value
;
275 ret
.value
= (long long)(char)sval
.value
;
278 if (sval_unsigned(ret
))
279 ret
.value
= (long long)(unsigned short)sval
.value
;
281 ret
.value
= (long long)(short)sval
.value
;
284 if (sval_unsigned(ret
))
285 ret
.value
= (long long)(unsigned int)sval
.value
;
287 ret
.value
= (long long)(int)sval
.value
;
290 ret
.value
= sval
.value
;
296 sval_t
sval_preop(sval_t sval
, int op
)
300 sval
.value
= !sval
.value
;
303 sval
.value
= ~sval
.value
;
304 sval
= sval_cast(sval
.type
, sval
);
307 sval
.value
= -sval
.value
;
308 sval
= sval_cast(sval
.type
, sval
);
314 static sval_t
sval_binop_unsigned(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
321 ret
.uvalue
= left
.uvalue
* right
.uvalue
;
324 if (right
.uvalue
== 0) {
325 sm_msg("debug: %s: divide by zero", __func__
);
326 ret
.uvalue
= 123456789;
328 ret
.uvalue
= left
.uvalue
/ right
.uvalue
;
332 ret
.uvalue
= left
.uvalue
+ right
.uvalue
;
335 ret
.uvalue
= left
.uvalue
- right
.uvalue
;
338 if (right
.uvalue
== 0) {
339 sm_msg("internal error: %s: MOD by zero", __func__
);
340 ret
.uvalue
= 123456789;
342 ret
.uvalue
= left
.uvalue
% right
.uvalue
;
346 ret
.uvalue
= left
.uvalue
| right
.uvalue
;
349 ret
.uvalue
= left
.uvalue
& right
.uvalue
;
351 case SPECIAL_RIGHTSHIFT
:
352 ret
.uvalue
= left
.uvalue
>> right
.uvalue
;
354 case SPECIAL_LEFTSHIFT
:
355 ret
.uvalue
= left
.uvalue
<< right
.uvalue
;
358 ret
.uvalue
= left
.uvalue
^ right
.uvalue
;
361 sm_msg("internal error: %s: unhandled binop %s", __func__
,
363 ret
.uvalue
= 1234567;
369 static sval_t
sval_binop_signed(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
376 ret
.value
= left
.value
* right
.value
;
379 if (right
.value
== 0) {
380 sm_msg("debug: %s: divide by zero", __func__
);
381 ret
.value
= 123456789;
382 } else if (left
.value
== LLONG_MIN
&& right
.value
== -1) {
383 sm_msg("debug: %s: invalid divide LLONG_MIN/-1", __func__
);
384 ret
.value
= 12345678;
386 ret
.value
= left
.value
/ right
.value
;
390 ret
.value
= left
.value
+ right
.value
;
393 ret
.value
= left
.value
- right
.value
;
396 if (right
.value
== 0) {
397 sm_msg("internal error: %s: MOD by zero", __func__
);
398 ret
.value
= 123456789;
400 ret
.value
= left
.value
% right
.value
;
404 ret
.value
= left
.value
| right
.value
;
407 ret
.value
= left
.value
& right
.value
;
409 case SPECIAL_RIGHTSHIFT
:
410 ret
.value
= left
.value
>> right
.value
;
412 case SPECIAL_LEFTSHIFT
:
413 ret
.value
= left
.value
<< right
.value
;
416 ret
.value
= left
.value
^ right
.value
;
419 sm_msg("internal error: %s: unhandled binop %s", __func__
,
426 sval_t
sval_binop(sval_t left
, int op
, sval_t right
)
431 type
= get_promoted_type(left
.type
, right
.type
);
433 if (type_unsigned(type
))
434 ret
= sval_binop_unsigned(type
, left
, op
, right
);
436 ret
= sval_binop_signed(type
, left
, op
, right
);
437 return sval_cast(type
, ret
);
440 int sval_unop_overflows(sval_t sval
, int op
)
444 if (sval_positive_bits(sval
) == 32 && sval
.value
== INT_MIN
)
446 if (sval_positive_bits(sval
) == 64 && sval
.value
== LLONG_MIN
)
448 if (sval_is_negative(sval
))
450 if (sval_signed(sval
))
452 if (sval_bits(sval
) == 32 && sval
.uvalue
> INT_MAX
)
454 if (sval_bits(sval
) == 64 && sval
.uvalue
> LLONG_MAX
)
459 int sval_binop_overflows(sval_t left
, int op
, sval_t right
)
465 if (type_positive_bits(right
.type
) > type_positive_bits(left
.type
))
467 if (type_positive_bits(type
) < 31)
470 max
= sval_type_max(type
);
471 min
= sval_type_min(type
);
475 if (sval_is_negative(left
) && sval_is_negative(right
)) {
476 if (left
.value
< min
.value
+ right
.value
)
480 if (sval_is_negative(left
) || sval_is_negative(right
))
482 if (left
.uvalue
> max
.uvalue
- right
.uvalue
)
486 if (type_signed(type
)) {
487 if (left
.value
== 0 || right
.value
== 0)
489 if (left
.value
> max
.value
/ right
.value
)
491 if (left
.value
== -1 || right
.value
== -1)
493 return left
.value
!= left
.value
* right
.value
/ right
.value
;
496 return right
.uvalue
!= 0 && left
.uvalue
> max
.uvalue
/ right
.uvalue
;
498 if (type_unsigned(type
)) {
499 if (sval_cmp(left
, right
) < 0)
503 if (sval_is_negative(left
) && sval_is_negative(right
))
506 if (sval_is_negative(left
)) {
507 if (left
.value
< min
.value
+ right
.value
)
511 if (sval_is_negative(right
)) {
512 if (right
.value
== min
.value
)
514 right
= sval_preop(right
, '-');
515 if (sval_binop_overflows(left
, '+', right
))
520 case SPECIAL_LEFTSHIFT
:
521 if (sval_cmp(left
, sval_binop(max
, invert_op(op
), right
)) > 0)
528 unsigned long long fls_mask(unsigned long long uvalue
)
530 unsigned long long high_bit
= 0;
540 return ((unsigned long long)-1) >> (64 - high_bit
);
543 unsigned long long sval_fls_mask(sval_t sval
)
545 return fls_mask(sval
.uvalue
);
548 const char *sval_to_str(sval_t sval
)
552 if (sval_unsigned(sval
) && sval
.value
== ULLONG_MAX
)
554 if (sval_unsigned(sval
) && sval
.value
== UINT_MAX
)
556 if (sval
.value
== USHRT_MAX
)
559 if (sval_signed(sval
) && sval
.value
== LLONG_MAX
)
561 if (sval
.value
== INT_MAX
)
563 if (sval
.value
== SHRT_MAX
)
566 if (sval_signed(sval
) && sval
.value
== SHRT_MIN
)
568 if (sval_signed(sval
) && sval
.value
== INT_MIN
)
570 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
573 if (sval_unsigned(sval
))
574 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
575 else if (sval
.value
< 0)
576 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
578 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
580 return alloc_sname(buf
);
583 const char *sval_to_numstr(sval_t sval
)
587 if (sval_unsigned(sval
))
588 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
589 else if (sval
.value
< 0)
590 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
592 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
594 return alloc_sname(buf
);
597 sval_t
ll_to_sval(long long val
)
601 ret
.type
= &llong_ctype
;
606 static void free_svals(struct symbol
*sym
)
613 void register_sval(int my_id
)
615 add_hook(&free_svals
, AFTER_FUNC_HOOK
);