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
, 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_is_min(sval_t sval
)
85 sval_t min
= sval_type_min(sval
.type
);
87 if (sval_unsigned(sval
)) {
92 /* return true for less than min as well */
93 return (sval
.value
<= min
.value
);
96 int sval_is_max(sval_t sval
)
98 sval_t max
= sval_type_max(sval
.type
);
100 if (sval_unsigned(sval
))
101 return (sval
.uvalue
>= max
.value
);
102 return (sval
.value
>= max
.value
);
106 * Casts the values and then does a compare. Returns -1 if one is smaller, 0 if
107 * they are the same and 1 if two is larger.
109 int sval_cmp(sval_t one
, sval_t two
)
111 /* fix me handle type promotion and unsigned values */
112 if (one
.value
< two
.value
)
114 if (one
.value
== two
.value
)
119 int sval_cmp_val(sval_t one
, long long val
)
123 if (one
.value
== val
)
128 sval_t
sval_cast(sval_t sval
, struct expression
*expr
)
132 ret
= sval_blank(expr
);
133 switch (sval_bits(ret
)) {
135 if (sval_unsigned(ret
))
136 ret
.value
= (long long)(unsigned char)sval
.value
;
138 ret
.value
= (long long)(char)sval
.value
;
141 if (sval_unsigned(ret
))
142 ret
.value
= (long long)(unsigned short)sval
.value
;
144 ret
.value
= (long long)(short)sval
.value
;
147 if (sval_unsigned(ret
))
148 ret
.value
= (long long)(unsigned int)sval
.value
;
150 ret
.value
= (long long)(int)sval
.value
;
153 ret
.value
= sval
.value
;
159 sval_t
sval_preop(sval_t sval
, int op
)
163 sval
.value
= !sval
.value
;
166 sval
.value
= ~sval
.value
;
167 /* fixme: should probably cast this here */
170 sval
.value
= -sval
.value
;
176 sval_t
sval_binop(sval_t left
, int op
, sval_t right
)
180 /* fixme: these need to have proper type promotions */
181 ret
.type
= left
.type
;
184 ret
.value
= left
.value
* right
.value
;
187 if (right
.value
== 0) {
188 sm_msg("internal error: %s: divide by zero", __func__
);
189 ret
.value
= 123456789;
191 ret
.value
= left
.value
/ right
.value
;
195 ret
.value
= left
.value
+ right
.value
;
198 ret
.value
= left
.value
- right
.value
;
201 if (right
.value
== 0) {
202 sm_msg("internal error: %s: MOD by zero", __func__
);
203 ret
.value
= 123456789;
205 ret
.value
= left
.value
% right
.value
;
209 ret
.value
= left
.value
| right
.value
;
212 ret
.value
= left
.value
& right
.value
;
214 case SPECIAL_RIGHTSHIFT
:
215 ret
.value
= left
.value
>> right
.value
;
217 case SPECIAL_LEFTSHIFT
:
218 ret
.value
= left
.value
<< right
.value
;
221 ret
.value
= left
.value
^ right
.value
;
224 sm_msg("internal error: %s: unhandled binop %s", __func__
,
231 const char *sval_to_str(sval_t sval
)
235 if (sval_unsigned(sval
) && sval
.value
== ULLONG_MAX
)
237 if (sval
.value
== LLONG_MAX
)
239 if (sval_unsigned(sval
) && sval
.value
== UINT_MAX
)
241 if (sval
.value
== INT_MAX
)
243 if (sval_unsigned(sval
) && sval
.value
== USHRT_MAX
)
246 if ((sval
.type
== &sshort_ctype
|| sval
.type
== &short_ctype
) && sval
.value
== SHRT_MIN
)
248 if ((sval
.type
== &sint_ctype
|| sval
.type
== &int_ctype
) && sval
.value
== INT_MIN
)
250 if (sval_signed(sval
) && sval
.value
== LLONG_MIN
)
253 if (sval_unsigned(sval
))
254 snprintf(buf
, sizeof(buf
), "%llu", sval
.value
);
255 else if (sval
.value
< 0)
256 snprintf(buf
, sizeof(buf
), "(%lld)", sval
.value
);
258 snprintf(buf
, sizeof(buf
), "%lld", sval
.value
);
260 return alloc_sname(buf
);
264 * This function is for compatibility. Eventually everything will use svals
265 * and we can get rid of whole_range.max.
267 long long sval_to_ll(sval_t sval
)
269 if (sval_unsigned(sval
) && sval
.value
== ULLONG_MAX
)
270 return whole_range
.max
;
274 sval_t
ll_to_sval(long long val
)
278 ret
.type
= &llong_ctype
;
283 static void free_svals(struct symbol
*sym
)
288 void register_sval(int my_id
)
290 add_hook(&free_svals
, END_FUNC_HOOK
);