sval: cast things correctly in sval_cmp()
[smatch.git] / smatch_sval.c
blob054c82bb00c64b17e7ec69f0c444166f34ce5b2c
1 /*
2 * smatch/smatch_sval.c
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.
19 #include "smatch.h"
20 #include "smatch_slist.h"
21 #include "smatch_extra.h"
23 __ALLOCATOR(sval_t, "svals", sval);
25 sval_t *sval_alloc(sval_t sval)
27 sval_t *ret;
29 ret = __alloc_sval(0);
30 *ret = sval;
31 return ret;
34 sval_t *sval_alloc_permanent(sval_t sval)
36 sval_t *ret;
38 ret = malloc(sizeof(*ret));
39 *ret = sval;
40 return ret;
43 sval_t sval_blank(struct expression *expr)
45 sval_t ret;
47 ret.type = get_type(expr);
48 if (!ret.type)
49 ret.type = &llong_ctype;
50 ret.value = 123456789;
52 return ret;
55 sval_t sval_type_val(struct symbol *type, long long val)
57 sval_t ret;
59 ret.type = type;
60 ret.value = val;
61 return ret;
64 sval_t sval_from_val(struct expression *expr, long long val)
66 sval_t ret;
68 ret = sval_blank(expr);
69 ret.value = val;
70 ret = sval_cast(get_type(expr), ret);
72 return 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 if (!sval.type)
88 return 32;
89 return sval.type->bit_size;
92 int sval_positive_bits(sval_t sval)
94 if (!sval.type)
95 return 31;
96 if (sval_signed(sval))
97 return sval.type->bit_size - 1;
98 return sval.type->bit_size;
101 int sval_bits_used(sval_t sval)
103 int i;
105 for (i = 64; i >= 1; i--) {
106 if (sval.uvalue & (1ULL << (i - 1)))
107 return i;
109 return 0;
112 int sval_is_min(sval_t sval)
114 sval_t min = sval_type_min(sval.type);
116 if (sval_unsigned(sval)) {
117 if (sval.uvalue == 0)
118 return 1;
119 return 0;
121 /* return true for less than min as well */
122 return (sval.value <= min.value);
125 int sval_is_max(sval_t sval)
127 sval_t max = sval_type_max(sval.type);
129 if (sval_unsigned(sval))
130 return (sval.uvalue >= max.value);
131 return (sval.value >= max.value);
134 int sval_is_a_min(sval_t sval)
136 if (sval_signed(sval) && sval.value == SHRT_MIN)
137 return 1;
138 if (sval_signed(sval) && sval.value == INT_MIN)
139 return 1;
140 if (sval_signed(sval) && sval.value == LLONG_MIN)
141 return 1;
142 return 0;
145 int sval_is_a_max(sval_t sval)
147 if (sval.uvalue == SHRT_MAX)
148 return 1;
149 if (sval.uvalue == INT_MAX)
150 return 1;
151 if (sval.uvalue == LLONG_MAX)
152 return 1;
153 if (sval.uvalue == USHRT_MAX)
154 return 1;
155 if (sval.uvalue == UINT_MAX)
156 return 1;
157 if (sval_unsigned(sval) && sval.uvalue == ULLONG_MAX)
158 return 1;
159 return 0;
163 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
165 int sval_cmp(sval_t one, sval_t two)
167 struct symbol *type;
169 type = one.type;
170 if (sval_positive_bits(two) > sval_positive_bits(one))
171 type = two.type;
172 if (type_bits(type) < 31)
173 type = &int_ctype;
175 one = sval_cast(type, one);
176 two = sval_cast(type, two);
178 if (type_unsigned(type)) {
179 if (one.uvalue < two.uvalue)
180 return -1;
181 if (one.uvalue == two.uvalue)
182 return 0;
183 return 1;
185 /* fix me handle type promotion and unsigned values */
186 if (one.value < two.value)
187 return -1;
188 if (one.value == two.value)
189 return 0;
190 return 1;
193 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two)
195 sval_t one_cast, two_cast;
197 one_cast = sval_cast(type, one);
198 two_cast = sval_cast(type, two);
199 return sval_cmp(one_cast, two_cast);
202 int sval_cmp_val(sval_t one, long long val)
204 if (one.value < val)
205 return -1;
206 if (one.value == val)
207 return 0;
208 return 1;
211 sval_t sval_cast(struct symbol *type, sval_t sval)
213 sval_t ret;
215 if (!type)
216 type = &llong_ctype;
218 ret.type = type;
219 switch (sval_bits(ret)) {
220 case 8:
221 if (sval_unsigned(ret))
222 ret.value = (long long)(unsigned char)sval.value;
223 else
224 ret.value = (long long)(char)sval.value;
225 break;
226 case 16:
227 if (sval_unsigned(ret))
228 ret.value = (long long)(unsigned short)sval.value;
229 else
230 ret.value = (long long)(short)sval.value;
231 break;
232 case 32:
233 if (sval_unsigned(ret))
234 ret.value = (long long)(unsigned int)sval.value;
235 else
236 ret.value = (long long)(int)sval.value;
237 break;
238 default:
239 ret.value = sval.value;
241 return ret;
245 sval_t sval_preop(sval_t sval, int op)
247 switch (op) {
248 case '!':
249 sval.value = !sval.value;
250 break;
251 case '~':
252 sval.value = ~sval.value;
253 /* fixme: should probably cast this here */
254 break;
255 case '-':
256 sval.value = -sval.value;
257 break;
259 return sval;
262 static sval_t sval_binop_unsigned(struct symbol *type, sval_t left, int op, sval_t right)
264 sval_t ret;
266 ret.type = type;
267 switch (op) {
268 case '*':
269 ret.uvalue = left.uvalue * right.uvalue;
270 break;
271 case '/':
272 if (right.uvalue == 0) {
273 sm_msg("debug: %s: divide by zero", __func__);
274 ret.uvalue = 123456789;
275 } else {
276 ret.uvalue = left.uvalue / right.uvalue;
278 break;
279 case '+':
280 ret.uvalue = left.uvalue + right.uvalue;
281 break;
282 case '-':
283 ret.uvalue = left.uvalue - right.uvalue;
284 break;
285 case '%':
286 if (right.uvalue == 0) {
287 sm_msg("internal error: %s: MOD by zero", __func__);
288 ret.uvalue = 123456789;
289 } else {
290 ret.uvalue = left.uvalue % right.uvalue;
292 break;
293 case '|':
294 ret.uvalue = left.uvalue | right.uvalue;
295 break;
296 case '&':
297 ret.uvalue = left.uvalue & right.uvalue;
298 break;
299 case SPECIAL_RIGHTSHIFT:
300 ret.uvalue = left.uvalue >> right.uvalue;
301 break;
302 case SPECIAL_LEFTSHIFT:
303 ret.uvalue = left.uvalue << right.uvalue;
304 break;
305 case '^':
306 ret.uvalue = left.uvalue ^ right.uvalue;
307 break;
308 default:
309 sm_msg("internal error: %s: unhandled binop %s", __func__,
310 show_special(op));
311 ret.uvalue = 1234567;
313 return ret;
317 static sval_t sval_binop_signed(struct symbol *type, sval_t left, int op, sval_t right)
319 sval_t ret;
321 ret.type = type;
322 switch (op) {
323 case '*':
324 ret.value = left.value * right.value;
325 break;
326 case '/':
327 if (right.value == 0) {
328 sm_msg("debug: %s: divide by zero", __func__);
329 ret.value = 123456789;
330 } else if (left.value == LLONG_MIN && right.value == -1) {
331 sm_msg("debug: %s: invalid divide LLONG_MIN/-1", __func__);
332 ret.value = 12345678;
333 } else {
334 ret.value = left.value / right.value;
336 break;
337 case '+':
338 ret.value = left.value + right.value;
339 break;
340 case '-':
341 ret.value = left.value - right.value;
342 break;
343 case '%':
344 if (right.value == 0) {
345 sm_msg("internal error: %s: MOD by zero", __func__);
346 ret.value = 123456789;
347 } else {
348 ret.value = left.value % right.value;
350 break;
351 case '|':
352 ret.value = left.value | right.value;
353 break;
354 case '&':
355 ret.value = left.value & right.value;
356 break;
357 case SPECIAL_RIGHTSHIFT:
358 ret.value = left.value >> right.value;
359 break;
360 case SPECIAL_LEFTSHIFT:
361 ret.value = left.value << right.value;
362 break;
363 case '^':
364 ret.value = left.value ^ right.value;
365 break;
366 default:
367 sm_msg("internal error: %s: unhandled binop %s", __func__,
368 show_special(op));
369 ret.value = 1234567;
371 return ret;
374 sval_t sval_binop(sval_t left, int op, sval_t right)
376 struct symbol *type;
377 sval_t ret;
379 type = left.type;
380 if (sval_positive_bits(right) > sval_positive_bits(left))
381 type = right.type;
382 if (type_positive_bits(type) < 31)
383 type = &int_ctype;
385 if (type_unsigned(type))
386 ret = sval_binop_unsigned(type, left, op, right);
387 else
388 ret = sval_binop_signed(type, left, op, right);
389 return ret;
392 const char *sval_to_str(sval_t sval)
394 char buf[30];
396 if (sval_unsigned(sval) && sval.value == ULLONG_MAX)
397 return "u64max";
398 if (sval.value == LLONG_MAX)
399 return "s64max";
400 if (sval_unsigned(sval) && sval.value == UINT_MAX)
401 return "u32max";
402 if (sval.value == INT_MAX)
403 return "s32max";
404 if (sval_unsigned(sval) && sval.value == USHRT_MAX)
405 return "u16max";
407 if (sval_signed(sval) && sval.value == SHRT_MIN)
408 return "s16min";
409 if (sval_signed(sval) && sval.value == INT_MIN)
410 return "s32min";
411 if (sval_signed(sval) && sval.value == LLONG_MIN)
412 return "s64min";
414 if (sval_unsigned(sval))
415 snprintf(buf, sizeof(buf), "%llu", sval.value);
416 else if (sval.value < 0)
417 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
418 else
419 snprintf(buf, sizeof(buf), "%lld", sval.value);
421 return alloc_sname(buf);
424 const char *sval_to_numstr(sval_t sval)
426 char buf[30];
428 if (sval_unsigned(sval))
429 snprintf(buf, sizeof(buf), "%llu", sval.value);
430 else if (sval.value < 0)
431 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
432 else
433 snprintf(buf, sizeof(buf), "%lld", sval.value);
435 return alloc_sname(buf);
438 sval_t ll_to_sval(long long val)
440 sval_t ret;
442 ret.type = &llong_ctype;
443 ret.value = val;
444 return ret;
447 static void free_svals(struct symbol *sym)
449 clear_sval_alloc();
452 void register_sval(int my_id)
454 add_hook(&free_svals, END_FUNC_HOOK);