flow: hooks: introduce GLOBAL_ASSIGNMENT_HOOK
[smatch.git] / smatch_sval.c
blob007746837773e71c7472ebf5841bb0c87b51eeab
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 sval_t tmp;
169 tmp = one;
170 if (sval_positive_bits(two) > sval_positive_bits(one))
171 tmp = two;
172 if (sval_bits(tmp) >= 32 && sval_unsigned(tmp)) {
173 if (one.uvalue < two.uvalue)
174 return -1;
175 if (one.uvalue == two.uvalue)
176 return 0;
177 return 1;
179 /* fix me handle type promotion and unsigned values */
180 if (one.value < two.value)
181 return -1;
182 if (one.value == two.value)
183 return 0;
184 return 1;
187 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two)
189 sval_t one_cast, two_cast;
191 one_cast = sval_cast(type, one);
192 two_cast = sval_cast(type, two);
193 return sval_cmp(one_cast, two_cast);
196 int sval_cmp_val(sval_t one, long long val)
198 if (one.value < val)
199 return -1;
200 if (one.value == val)
201 return 0;
202 return 1;
205 sval_t sval_cast(struct symbol *type, sval_t sval)
207 sval_t ret;
209 if (!type)
210 type = &llong_ctype;
212 ret.type = type;
213 switch (sval_bits(ret)) {
214 case 8:
215 if (sval_unsigned(ret))
216 ret.value = (long long)(unsigned char)sval.value;
217 else
218 ret.value = (long long)(char)sval.value;
219 break;
220 case 16:
221 if (sval_unsigned(ret))
222 ret.value = (long long)(unsigned short)sval.value;
223 else
224 ret.value = (long long)(short)sval.value;
225 break;
226 case 32:
227 if (sval_unsigned(ret))
228 ret.value = (long long)(unsigned int)sval.value;
229 else
230 ret.value = (long long)(int)sval.value;
231 break;
232 default:
233 ret.value = sval.value;
235 return ret;
239 sval_t sval_preop(sval_t sval, int op)
241 switch (op) {
242 case '!':
243 sval.value = !sval.value;
244 break;
245 case '~':
246 sval.value = ~sval.value;
247 /* fixme: should probably cast this here */
248 break;
249 case '-':
250 sval.value = -sval.value;
251 break;
253 return sval;
256 static sval_t sval_binop_unsigned(struct symbol *type, sval_t left, int op, sval_t right)
258 sval_t ret;
260 ret.type = type;
261 switch (op) {
262 case '*':
263 ret.uvalue = left.uvalue * right.uvalue;
264 break;
265 case '/':
266 if (right.uvalue == 0) {
267 sm_msg("debug: %s: divide by zero", __func__);
268 ret.uvalue = 123456789;
269 } else {
270 ret.uvalue = left.uvalue / right.uvalue;
272 break;
273 case '+':
274 ret.uvalue = left.uvalue + right.uvalue;
275 break;
276 case '-':
277 ret.uvalue = left.uvalue - right.uvalue;
278 break;
279 case '%':
280 if (right.uvalue == 0) {
281 sm_msg("internal error: %s: MOD by zero", __func__);
282 ret.uvalue = 123456789;
283 } else {
284 ret.uvalue = left.uvalue % right.uvalue;
286 break;
287 case '|':
288 ret.uvalue = left.uvalue | right.uvalue;
289 break;
290 case '&':
291 ret.uvalue = left.uvalue & right.uvalue;
292 break;
293 case SPECIAL_RIGHTSHIFT:
294 ret.uvalue = left.uvalue >> right.uvalue;
295 break;
296 case SPECIAL_LEFTSHIFT:
297 ret.uvalue = left.uvalue << right.uvalue;
298 break;
299 case '^':
300 ret.uvalue = left.uvalue ^ right.uvalue;
301 break;
302 default:
303 sm_msg("internal error: %s: unhandled binop %s", __func__,
304 show_special(op));
305 ret.uvalue = 1234567;
307 return ret;
311 static sval_t sval_binop_signed(struct symbol *type, sval_t left, int op, sval_t right)
313 sval_t ret;
315 ret.type = type;
316 switch (op) {
317 case '*':
318 ret.value = left.value * right.value;
319 break;
320 case '/':
321 if (right.value == 0) {
322 sm_msg("debug: %s: divide by zero", __func__);
323 ret.value = 123456789;
324 } else if (left.value == LLONG_MIN && right.value == -1) {
325 sm_msg("debug: %s: invalid divide LLONG_MIN/-1", __func__);
326 ret.value = 12345678;
327 } else {
328 ret.value = left.value / right.value;
330 break;
331 case '+':
332 ret.value = left.value + right.value;
333 break;
334 case '-':
335 ret.value = left.value - right.value;
336 break;
337 case '%':
338 if (right.value == 0) {
339 sm_msg("internal error: %s: MOD by zero", __func__);
340 ret.value = 123456789;
341 } else {
342 ret.value = left.value % right.value;
344 break;
345 case '|':
346 ret.value = left.value | right.value;
347 break;
348 case '&':
349 ret.value = left.value & right.value;
350 break;
351 case SPECIAL_RIGHTSHIFT:
352 ret.value = left.value >> right.value;
353 break;
354 case SPECIAL_LEFTSHIFT:
355 ret.value = left.value << right.value;
356 break;
357 case '^':
358 ret.value = left.value ^ right.value;
359 break;
360 default:
361 sm_msg("internal error: %s: unhandled binop %s", __func__,
362 show_special(op));
363 ret.value = 1234567;
365 return ret;
368 sval_t sval_binop(sval_t left, int op, sval_t right)
370 struct symbol *type;
371 sval_t ret;
373 type = left.type;
374 if (sval_positive_bits(right) > sval_positive_bits(left))
375 type = right.type;
376 if (type_positive_bits(type) < 31)
377 type = &int_ctype;
379 if (type_unsigned(type))
380 ret = sval_binop_unsigned(type, left, op, right);
381 else
382 ret = sval_binop_signed(type, left, op, right);
383 return ret;
386 const char *sval_to_str(sval_t sval)
388 char buf[30];
390 if (sval_unsigned(sval) && sval.value == ULLONG_MAX)
391 return "u64max";
392 if (sval.value == LLONG_MAX)
393 return "s64max";
394 if (sval_unsigned(sval) && sval.value == UINT_MAX)
395 return "u32max";
396 if (sval.value == INT_MAX)
397 return "s32max";
398 if (sval_unsigned(sval) && sval.value == USHRT_MAX)
399 return "u16max";
401 if (sval_signed(sval) && sval.value == SHRT_MIN)
402 return "s16min";
403 if (sval_signed(sval) && sval.value == INT_MIN)
404 return "s32min";
405 if (sval_signed(sval) && sval.value == LLONG_MIN)
406 return "s64min";
408 if (sval_unsigned(sval))
409 snprintf(buf, sizeof(buf), "%llu", sval.value);
410 else if (sval.value < 0)
411 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
412 else
413 snprintf(buf, sizeof(buf), "%lld", sval.value);
415 return alloc_sname(buf);
418 const char *sval_to_numstr(sval_t sval)
420 char buf[30];
422 if (sval_unsigned(sval))
423 snprintf(buf, sizeof(buf), "%llu", sval.value);
424 else if (sval.value < 0)
425 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
426 else
427 snprintf(buf, sizeof(buf), "%lld", sval.value);
429 return alloc_sname(buf);
432 sval_t ll_to_sval(long long val)
434 sval_t ret;
436 ret.type = &llong_ctype;
437 ret.value = val;
438 return ret;
441 static void free_svals(struct symbol *sym)
443 clear_sval_alloc();
446 void register_sval(int my_id)
448 add_hook(&free_svals, END_FUNC_HOOK);