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
= &llong_ctype
;
60 ret
.value
= 123456789;
65 sval_t
sval_type_val(struct symbol
*type
, long long val
)
74 sval_t
sval_from_val(struct expression
*expr
, long long val
)
78 ret
= sval_blank(expr
);
80 ret
= sval_cast(get_type(expr
), ret
);
85 int sval_unsigned(sval_t sval
)
87 return type_unsigned(sval
.type
);
90 int sval_signed(sval_t sval
)
92 return !type_unsigned(sval
.type
);
95 int sval_bits(sval_t sval
)
97 return type_bits(sval
.type
);
100 int sval_positive_bits(sval_t sval
)
102 return type_positive_bits(sval
.type
);
105 int sval_bits_used(sval_t sval
)
109 for (i
= 64; i
>= 1; i
--) {
110 if (sval
.uvalue
& (1ULL << (i
- 1)))
116 int sval_is_negative(sval_t sval
)
118 if (type_unsigned(sval
.type
))
125 int sval_is_positive(sval_t sval
)
127 return !sval_is_negative(sval
);
130 int sval_is_min(sval_t sval
)
132 sval_t min
= sval_type_min(sval
.type
);
134 if (sval_unsigned(sval
)) {
135 if (sval
.uvalue
== 0)
139 /* return true for less than min as well */
140 return (sval
.value
<= min
.value
);
143 int sval_is_max(sval_t sval
)
145 sval_t max
= sval_type_max(sval
.type
);
147 if (sval_unsigned(sval
))
148 return (sval
.uvalue
>= max
.value
);
149 return (sval
.value
>= max
.value
);
152 int sval_is_a_min(sval_t sval
)
154 if (sval_signed(sval
) && sval
.value
== SHRT_MIN
)
156 if (sval_signed(sval
) && sval
.value
== INT_MIN
)
158 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
163 int sval_is_a_max(sval_t sval
)
165 if (sval
.uvalue
== SHRT_MAX
)
167 if (sval
.uvalue
== INT_MAX
)
169 if (sval
.uvalue
== LLONG_MAX
)
171 if (sval
.uvalue
== USHRT_MAX
)
173 if (sval
.uvalue
== UINT_MAX
)
175 if (sval_unsigned(sval
) && sval
.uvalue
== ULLONG_MAX
)
180 int sval_is_negative_min(sval_t sval
)
182 if (!sval_is_negative(sval
))
184 return sval_is_min(sval
);
188 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
190 int sval_cmp(sval_t one
, sval_t two
)
195 if (sval_positive_bits(two
) > sval_positive_bits(one
))
197 if (type_bits(type
) < 31)
200 one
= sval_cast(type
, one
);
201 two
= sval_cast(type
, two
);
203 if (type_unsigned(type
)) {
204 if (one
.uvalue
< two
.uvalue
)
206 if (one
.uvalue
== two
.uvalue
)
210 /* fix me handle type promotion and unsigned values */
211 if (one
.value
< two
.value
)
213 if (one
.value
== two
.value
)
218 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
)
220 sval_t one_cast
, two_cast
;
222 one_cast
= sval_cast(type
, one
);
223 two_cast
= sval_cast(type
, two
);
224 return sval_cmp(one_cast
, two_cast
);
227 int sval_cmp_val(sval_t one
, long long val
)
231 sval
= sval_type_val(&llong_ctype
, val
);
232 return sval_cmp(one
, sval
);
235 sval_t
sval_min(sval_t one
, sval_t two
)
237 if (sval_cmp(one
, two
) > 0)
242 sval_t
sval_max(sval_t one
, sval_t two
)
244 if (sval_cmp(one
, two
) < 0)
249 int sval_too_low(struct symbol
*type
, sval_t sval
)
251 if (sval_is_negative(sval
) && type_unsigned(type
))
253 if (type_signed(type
) && sval_unsigned(sval
))
255 if (sval_cmp(sval
, sval_type_min(type
)) < 0)
260 int sval_too_high(struct symbol
*type
, sval_t sval
)
262 if (sval_is_negative(sval
))
264 if (sval
.uvalue
> sval_type_max(type
).uvalue
)
269 int sval_fits(struct symbol
*type
, sval_t sval
)
271 if (sval_too_low(type
, sval
))
273 if (sval_too_high(type
, sval
))
278 sval_t
sval_cast(struct symbol
*type
, sval_t sval
)
286 switch (sval_bits(ret
)) {
288 if (sval_unsigned(ret
))
289 ret
.value
= (long long)(unsigned char)sval
.value
;
291 ret
.value
= (long long)(char)sval
.value
;
294 if (sval_unsigned(ret
))
295 ret
.value
= (long long)(unsigned short)sval
.value
;
297 ret
.value
= (long long)(short)sval
.value
;
300 if (sval_unsigned(ret
))
301 ret
.value
= (long long)(unsigned int)sval
.value
;
303 ret
.value
= (long long)(int)sval
.value
;
306 ret
.value
= sval
.value
;
312 sval_t
sval_preop(sval_t sval
, int op
)
316 sval
.value
= !sval
.value
;
319 sval
.value
= ~sval
.value
;
320 sval
= sval_cast(sval
.type
, sval
);
323 sval
.value
= -sval
.value
;
324 sval
= sval_cast(sval
.type
, sval
);
330 static sval_t
sval_binop_unsigned(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
337 ret
.uvalue
= left
.uvalue
* right
.uvalue
;
340 if (right
.uvalue
== 0) {
341 sm_msg("debug: %s: divide by zero", __func__
);
342 ret
.uvalue
= 123456789;
344 ret
.uvalue
= left
.uvalue
/ right
.uvalue
;
348 ret
.uvalue
= left
.uvalue
+ right
.uvalue
;
351 ret
.uvalue
= left
.uvalue
- right
.uvalue
;
354 if (right
.uvalue
== 0) {
355 sm_msg("internal error: %s: MOD by zero", __func__
);
356 ret
.uvalue
= 123456789;
358 ret
.uvalue
= left
.uvalue
% right
.uvalue
;
362 ret
.uvalue
= left
.uvalue
| right
.uvalue
;
365 ret
.uvalue
= left
.uvalue
& right
.uvalue
;
367 case SPECIAL_RIGHTSHIFT
:
368 ret
.uvalue
= left
.uvalue
>> right
.uvalue
;
370 case SPECIAL_LEFTSHIFT
:
371 ret
.uvalue
= left
.uvalue
<< right
.uvalue
;
374 ret
.uvalue
= left
.uvalue
^ right
.uvalue
;
377 sm_msg("internal error: %s: unhandled binop %s", __func__
,
379 ret
.uvalue
= 1234567;
385 static sval_t
sval_binop_signed(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
392 ret
.value
= left
.value
* right
.value
;
395 if (right
.value
== 0) {
396 sm_msg("debug: %s: divide by zero", __func__
);
397 ret
.value
= 123456789;
398 } else if (left
.value
== LLONG_MIN
&& right
.value
== -1) {
399 sm_msg("debug: %s: invalid divide LLONG_MIN/-1", __func__
);
400 ret
.value
= 12345678;
402 ret
.value
= left
.value
/ right
.value
;
406 ret
.value
= left
.value
+ right
.value
;
409 ret
.value
= left
.value
- right
.value
;
412 if (right
.value
== 0) {
413 sm_msg("internal error: %s: MOD by zero", __func__
);
414 ret
.value
= 123456789;
416 ret
.value
= left
.value
% right
.value
;
420 ret
.value
= left
.value
| right
.value
;
423 ret
.value
= left
.value
& right
.value
;
425 case SPECIAL_RIGHTSHIFT
:
426 ret
.value
= left
.value
>> right
.value
;
428 case SPECIAL_LEFTSHIFT
:
429 ret
.value
= left
.value
<< right
.value
;
432 ret
.value
= left
.value
^ right
.value
;
435 sm_msg("internal error: %s: unhandled binop %s", __func__
,
442 sval_t
sval_binop(sval_t left
, int op
, sval_t right
)
448 if (sval_positive_bits(right
) > sval_positive_bits(left
))
450 if (type_positive_bits(type
) < 31)
453 if (type_unsigned(type
))
454 ret
= sval_binop_unsigned(type
, left
, op
, right
);
456 ret
= sval_binop_signed(type
, left
, op
, right
);
460 int sval_unop_overflows(sval_t sval
, int op
)
464 if (sval_positive_bits(sval
) == 32 && sval
.value
== INT_MIN
)
466 if (sval_positive_bits(sval
) == 64 && sval
.value
== LLONG_MIN
)
468 if (sval_is_negative(sval
))
470 if (sval_signed(sval
))
472 if (sval_bits(sval
) == 32 && sval
.uvalue
> INT_MAX
)
474 if (sval_bits(sval
) == 64 && sval
.uvalue
> LLONG_MAX
)
479 int sval_binop_overflows(sval_t left
, int op
, sval_t right
)
485 if (type_positive_bits(right
.type
) > type_positive_bits(left
.type
))
487 if (type_positive_bits(type
) < 31)
489 max
= sval_type_max(type
);
490 min
= sval_type_min(type
);
494 if (sval_is_negative(right
)) {
495 if (left
.value
< min
.value
- right
.value
)
498 if (left
.uvalue
> max
.uvalue
- right
.uvalue
)
503 return right
.uvalue
!= 0 && left
.uvalue
> max
.uvalue
/ right
.uvalue
;
505 if (type_unsigned(type
)) {
506 if (sval_cmp(left
, right
) < 0)
511 if (sval_unop_overflows(right
, '-'))
513 right
= sval_preop(right
, '-');
514 if (sval_binop_overflows(left
, '+', right
))
521 const char *sval_to_str(sval_t sval
)
525 if (sval_unsigned(sval
) && sval
.value
== ULLONG_MAX
)
527 if (sval_unsigned(sval
) && sval
.value
== UINT_MAX
)
529 if (sval_unsigned(sval
) && sval
.value
== USHRT_MAX
)
532 if (sval_signed(sval
) && sval
.value
== LLONG_MAX
)
534 if (sval_signed(sval
) && sval
.value
== INT_MAX
)
536 if (sval_signed(sval
) && sval
.value
== SHRT_MAX
)
539 if (sval_signed(sval
) && sval
.value
== SHRT_MIN
)
541 if (sval_signed(sval
) && sval
.value
== INT_MIN
)
543 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
546 if (sval_unsigned(sval
))
547 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
548 else if (sval
.value
< 0)
549 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
551 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
553 return alloc_sname(buf
);
556 const char *sval_to_numstr(sval_t sval
)
560 if (sval_unsigned(sval
))
561 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
562 else if (sval
.value
< 0)
563 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
565 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
567 return alloc_sname(buf
);
570 sval_t
ll_to_sval(long long val
)
574 ret
.type
= &llong_ctype
;
579 static void free_svals(struct symbol
*sym
)
586 void register_sval(int my_id
)
588 add_hook(&free_svals
, END_FUNC_HOOK
);