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_from_val(struct expression
*expr
, long long val
)
59 ret
= sval_blank(expr
);
61 ret
= sval_cast(ret
, get_type(expr
));
66 int sval_unsigned(sval_t sval
)
68 return type_unsigned(sval
.type
);
71 int sval_signed(sval_t sval
)
73 return !type_unsigned(sval
.type
);
76 int sval_bits(sval_t sval
)
80 return sval
.type
->bit_size
;
83 int sval_positive_bits(sval_t sval
)
87 if (sval_signed(sval
))
88 return sval
.type
->bit_size
- 1;
89 return sval
.type
->bit_size
;
92 int sval_is_min(sval_t sval
)
94 sval_t min
= sval_type_min(sval
.type
);
96 if (sval_unsigned(sval
)) {
101 /* return true for less than min as well */
102 return (sval
.value
<= min
.value
);
105 int sval_is_max(sval_t sval
)
107 sval_t max
= sval_type_max(sval
.type
);
109 if (sval_unsigned(sval
))
110 return (sval
.uvalue
>= max
.value
);
111 return (sval
.value
>= max
.value
);
115 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
117 int sval_cmp(sval_t one
, sval_t two
)
122 if (sval_positive_bits(two
) > sval_positive_bits(one
))
124 if (sval_bits(tmp
) >= 32 && sval_unsigned(tmp
)) {
125 if (one
.uvalue
< two
.uvalue
)
127 if (one
.uvalue
== two
.uvalue
)
131 /* fix me handle type promotion and unsigned values */
132 if (one
.value
< two
.value
)
134 if (one
.value
== two
.value
)
139 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
)
141 sval_t one_cast
, two_cast
;
143 one_cast
= sval_cast(one
, type
);
144 two_cast
= sval_cast(two
, type
);
145 return sval_cmp(one_cast
, two_cast
);
148 int sval_cmp_val(sval_t one
, long long val
)
152 if (one
.value
== val
)
157 sval_t
sval_cast(sval_t sval
, struct symbol
*type
)
165 switch (sval_bits(ret
)) {
167 if (sval_unsigned(ret
))
168 ret
.value
= (long long)(unsigned char)sval
.value
;
170 ret
.value
= (long long)(char)sval
.value
;
173 if (sval_unsigned(ret
))
174 ret
.value
= (long long)(unsigned short)sval
.value
;
176 ret
.value
= (long long)(short)sval
.value
;
179 if (sval_unsigned(ret
))
180 ret
.value
= (long long)(unsigned int)sval
.value
;
182 ret
.value
= (long long)(int)sval
.value
;
185 ret
.value
= sval
.value
;
191 sval_t
sval_preop(sval_t sval
, int op
)
195 sval
.value
= !sval
.value
;
198 sval
.value
= ~sval
.value
;
199 /* fixme: should probably cast this here */
202 sval
.value
= -sval
.value
;
208 sval_t
sval_binop(sval_t left
, int op
, sval_t right
)
212 /* fixme: these need to have proper type promotions */
213 ret
.type
= left
.type
;
216 ret
.value
= left
.value
* right
.value
;
219 if (right
.value
== 0) {
220 sm_msg("internal error: %s: divide by zero", __func__
);
221 ret
.value
= 123456789;
223 ret
.value
= left
.value
/ right
.value
;
227 ret
.value
= left
.value
+ right
.value
;
230 ret
.value
= left
.value
- right
.value
;
233 if (right
.value
== 0) {
234 sm_msg("internal error: %s: MOD by zero", __func__
);
235 ret
.value
= 123456789;
237 ret
.value
= left
.value
% right
.value
;
241 ret
.value
= left
.value
| right
.value
;
244 ret
.value
= left
.value
& right
.value
;
246 case SPECIAL_RIGHTSHIFT
:
247 ret
.value
= left
.value
>> right
.value
;
249 case SPECIAL_LEFTSHIFT
:
250 ret
.value
= left
.value
<< right
.value
;
253 ret
.value
= left
.value
^ right
.value
;
256 sm_msg("internal error: %s: unhandled binop %s", __func__
,
263 const char *sval_to_str(sval_t sval
)
267 if (sval
.value
&& sval_bits(sval
) > 8 && sval_is_min(sval
))
269 if (sval_bits(sval
) > 8 && sval_is_max(sval
))
272 if (sval_unsigned(sval
) && sval
.value
== ULLONG_MAX
)
274 if (sval
.value
== LLONG_MAX
)
275 return "max"; // FIXME: should be s64max
276 if (sval_unsigned(sval
) && sval
.value
== UINT_MAX
)
278 if (sval
.value
== INT_MAX
)
280 if (sval_unsigned(sval
) && sval
.value
== USHRT_MAX
)
283 if ((sval
.type
== &sshort_ctype
|| sval
.type
== &short_ctype
) && sval
.value
== SHRT_MIN
)
285 if ((sval
.type
== &sint_ctype
|| sval
.type
== &int_ctype
) && sval
.value
== INT_MIN
)
287 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
288 return "min"; // FIXME: should be s64min
290 if (sval_unsigned(sval
))
291 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
292 else if (sval
.value
< 0)
293 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
295 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
297 return alloc_sname(buf
);
300 const char *sval_to_numstr(sval_t sval
)
304 if (sval_unsigned(sval
))
305 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
306 else if (sval
.value
< 0)
307 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
309 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
311 return alloc_sname(buf
);
314 sval_t
ll_to_sval(long long val
)
318 ret
.type
= &llong_ctype
;
323 static void free_svals(struct symbol
*sym
)
328 void register_sval(int my_id
)
330 add_hook(&free_svals
, END_FUNC_HOOK
);