4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
9 * Basically the point of sval is that it can hold both ULLONG_MAX and
10 * LLONG_MIN. If it is an unsigned type then we use sval.uvalue or if it is
11 * signed we use sval.value.
13 * I considered just using one bit to store whether the value was signed vs
14 * unsigned but I think it might help to have the type information so we know
15 * how to do type promotion.
20 #include "smatch_slist.h"
21 #include "smatch_extra.h"
23 __ALLOCATOR(sval_t
, "svals", sval
);
25 sval_t
*sval_alloc(sval_t sval
)
29 ret
= __alloc_sval(0);
34 sval_t
*sval_alloc_permanent(sval_t sval
)
38 ret
= malloc(sizeof(*ret
));
43 sval_t
sval_blank(struct expression
*expr
)
47 ret
.type
= get_type(expr
);
49 ret
.type
= &llong_ctype
;
50 ret
.value
= 123456789;
55 sval_t
sval_type_val(struct symbol
*type
, long long val
)
64 sval_t
sval_from_val(struct expression
*expr
, long long val
)
68 ret
= sval_blank(expr
);
70 ret
= sval_cast(get_type(expr
), ret
);
75 int sval_unsigned(sval_t sval
)
77 return type_unsigned(sval
.type
);
80 int sval_signed(sval_t sval
)
82 return !type_unsigned(sval
.type
);
85 int sval_bits(sval_t sval
)
87 return type_bits(sval
.type
);
90 int sval_positive_bits(sval_t sval
)
92 return type_positive_bits(sval
.type
);
95 int sval_bits_used(sval_t sval
)
99 for (i
= 64; i
>= 1; i
--) {
100 if (sval
.uvalue
& (1ULL << (i
- 1)))
106 int sval_is_negative(sval_t sval
)
108 if (type_unsigned(sval
.type
))
115 int sval_is_positive(sval_t sval
)
117 return !sval_is_negative(sval
);
120 int sval_is_min(sval_t sval
)
122 sval_t min
= sval_type_min(sval
.type
);
124 if (sval_unsigned(sval
)) {
125 if (sval
.uvalue
== 0)
129 /* return true for less than min as well */
130 return (sval
.value
<= min
.value
);
133 int sval_is_max(sval_t sval
)
135 sval_t max
= sval_type_max(sval
.type
);
137 if (sval_unsigned(sval
))
138 return (sval
.uvalue
>= max
.value
);
139 return (sval
.value
>= max
.value
);
142 int sval_is_a_min(sval_t sval
)
144 if (sval_signed(sval
) && sval
.value
== SHRT_MIN
)
146 if (sval_signed(sval
) && sval
.value
== INT_MIN
)
148 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
153 int sval_is_a_max(sval_t sval
)
155 if (sval
.uvalue
== SHRT_MAX
)
157 if (sval
.uvalue
== INT_MAX
)
159 if (sval
.uvalue
== LLONG_MAX
)
161 if (sval
.uvalue
== USHRT_MAX
)
163 if (sval
.uvalue
== UINT_MAX
)
165 if (sval_unsigned(sval
) && sval
.uvalue
== ULLONG_MAX
)
170 int sval_is_negative_min(sval_t sval
)
172 if (!sval_is_negative(sval
))
174 return sval_is_min(sval
);
178 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
180 int sval_cmp(sval_t one
, sval_t two
)
185 if (sval_positive_bits(two
) > sval_positive_bits(one
))
187 if (type_bits(type
) < 31)
190 one
= sval_cast(type
, one
);
191 two
= sval_cast(type
, two
);
193 if (type_unsigned(type
)) {
194 if (one
.uvalue
< two
.uvalue
)
196 if (one
.uvalue
== two
.uvalue
)
200 /* fix me handle type promotion and unsigned values */
201 if (one
.value
< two
.value
)
203 if (one
.value
== two
.value
)
208 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
)
210 sval_t one_cast
, two_cast
;
212 one_cast
= sval_cast(type
, one
);
213 two_cast
= sval_cast(type
, two
);
214 return sval_cmp(one_cast
, two_cast
);
217 int sval_cmp_val(sval_t one
, long long val
)
221 sval
= sval_type_val(&llong_ctype
, val
);
222 return sval_cmp(one
, sval
);
225 sval_t
sval_min(sval_t one
, sval_t two
)
227 if (sval_cmp(one
, two
) > 0)
232 sval_t
sval_max(sval_t one
, sval_t two
)
234 if (sval_cmp(one
, two
) < 0)
239 int sval_too_low(struct symbol
*type
, sval_t sval
)
241 if (sval_is_negative(sval
) && type_unsigned(type
))
243 if (type_signed(type
) && sval_unsigned(sval
))
245 if (sval_cmp(sval
, sval_type_min(type
)) < 0)
250 int sval_too_high(struct symbol
*type
, sval_t sval
)
252 if (sval_is_negative(sval
))
254 if (sval
.uvalue
> sval_type_max(type
).uvalue
)
259 int sval_fits(struct symbol
*type
, sval_t sval
)
261 if (sval_too_low(type
, sval
))
263 if (sval_too_high(type
, sval
))
268 sval_t
sval_cast(struct symbol
*type
, sval_t sval
)
276 switch (sval_bits(ret
)) {
278 if (sval_unsigned(ret
))
279 ret
.value
= (long long)(unsigned char)sval
.value
;
281 ret
.value
= (long long)(char)sval
.value
;
284 if (sval_unsigned(ret
))
285 ret
.value
= (long long)(unsigned short)sval
.value
;
287 ret
.value
= (long long)(short)sval
.value
;
290 if (sval_unsigned(ret
))
291 ret
.value
= (long long)(unsigned int)sval
.value
;
293 ret
.value
= (long long)(int)sval
.value
;
296 ret
.value
= sval
.value
;
302 sval_t
sval_preop(sval_t sval
, int op
)
306 sval
.value
= !sval
.value
;
309 sval
.value
= ~sval
.value
;
310 /* fixme: should probably cast this here */
313 sval
.value
= -sval
.value
;
319 static sval_t
sval_binop_unsigned(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
326 ret
.uvalue
= left
.uvalue
* right
.uvalue
;
329 if (right
.uvalue
== 0) {
330 sm_msg("debug: %s: divide by zero", __func__
);
331 ret
.uvalue
= 123456789;
333 ret
.uvalue
= left
.uvalue
/ right
.uvalue
;
337 ret
.uvalue
= left
.uvalue
+ right
.uvalue
;
340 ret
.uvalue
= left
.uvalue
- right
.uvalue
;
343 if (right
.uvalue
== 0) {
344 sm_msg("internal error: %s: MOD by zero", __func__
);
345 ret
.uvalue
= 123456789;
347 ret
.uvalue
= left
.uvalue
% right
.uvalue
;
351 ret
.uvalue
= left
.uvalue
| right
.uvalue
;
354 ret
.uvalue
= left
.uvalue
& right
.uvalue
;
356 case SPECIAL_RIGHTSHIFT
:
357 ret
.uvalue
= left
.uvalue
>> right
.uvalue
;
359 case SPECIAL_LEFTSHIFT
:
360 ret
.uvalue
= left
.uvalue
<< right
.uvalue
;
363 ret
.uvalue
= left
.uvalue
^ right
.uvalue
;
366 sm_msg("internal error: %s: unhandled binop %s", __func__
,
368 ret
.uvalue
= 1234567;
374 static sval_t
sval_binop_signed(struct symbol
*type
, sval_t left
, int op
, sval_t right
)
381 ret
.value
= left
.value
* right
.value
;
384 if (right
.value
== 0) {
385 sm_msg("debug: %s: divide by zero", __func__
);
386 ret
.value
= 123456789;
387 } else if (left
.value
== LLONG_MIN
&& right
.value
== -1) {
388 sm_msg("debug: %s: invalid divide LLONG_MIN/-1", __func__
);
389 ret
.value
= 12345678;
391 ret
.value
= left
.value
/ right
.value
;
395 ret
.value
= left
.value
+ right
.value
;
398 ret
.value
= left
.value
- right
.value
;
401 if (right
.value
== 0) {
402 sm_msg("internal error: %s: MOD by zero", __func__
);
403 ret
.value
= 123456789;
405 ret
.value
= left
.value
% right
.value
;
409 ret
.value
= left
.value
| right
.value
;
412 ret
.value
= left
.value
& right
.value
;
414 case SPECIAL_RIGHTSHIFT
:
415 ret
.value
= left
.value
>> right
.value
;
417 case SPECIAL_LEFTSHIFT
:
418 ret
.value
= left
.value
<< right
.value
;
421 ret
.value
= left
.value
^ right
.value
;
424 sm_msg("internal error: %s: unhandled binop %s", __func__
,
431 sval_t
sval_binop(sval_t left
, int op
, sval_t right
)
437 if (sval_positive_bits(right
) > sval_positive_bits(left
))
439 if (type_positive_bits(type
) < 31)
442 if (type_unsigned(type
))
443 ret
= sval_binop_unsigned(type
, left
, op
, right
);
445 ret
= sval_binop_signed(type
, left
, op
, right
);
449 int sval_binop_overflows(sval_t left
, int op
, sval_t right
)
455 if (type_positive_bits(right
.type
) > type_positive_bits(left
.type
))
457 if (type_positive_bits(type
) < 31)
459 max
= sval_type_max(type
);
460 min
= sval_type_min(type
);
464 if (left
.uvalue
> max
.uvalue
- right
.uvalue
)
468 return right
.uvalue
!= 0 && left
.uvalue
> max
.uvalue
/ right
.uvalue
;
470 return sval_cmp(right
, sval_binop(min
, '+', left
)) < 0;
475 const char *sval_to_str(sval_t sval
)
479 if (sval_unsigned(sval
) && sval
.value
== ULLONG_MAX
)
481 if (sval_unsigned(sval
) && sval
.value
== UINT_MAX
)
483 if (sval_unsigned(sval
) && sval
.value
== USHRT_MAX
)
486 if (sval_signed(sval
) && sval
.value
== LLONG_MAX
)
488 if (sval_signed(sval
) && sval
.value
== INT_MAX
)
490 if (sval_signed(sval
) && sval
.value
== SHRT_MAX
)
493 if (sval_signed(sval
) && sval
.value
== SHRT_MIN
)
495 if (sval_signed(sval
) && sval
.value
== INT_MIN
)
497 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
500 if (sval_unsigned(sval
))
501 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
502 else if (sval
.value
< 0)
503 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
505 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
507 return alloc_sname(buf
);
510 const char *sval_to_numstr(sval_t sval
)
514 if (sval_unsigned(sval
))
515 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
516 else if (sval
.value
< 0)
517 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
519 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
521 return alloc_sname(buf
);
524 sval_t
ll_to_sval(long long val
)
528 ret
.type
= &llong_ctype
;
533 static void free_svals(struct symbol
*sym
)
540 void register_sval(int my_id
)
542 add_hook(&free_svals
, END_FUNC_HOOK
);