Print this page
12166 resync smatch to 0.6.1-rc1-il-3
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_sval.c
+++ new/usr/src/tools/smatch/src/smatch_sval.c
1 1 /*
2 2 * Copyright (C) 2012 Oracle.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 /*
19 19 * Basically the point of sval is that it can hold both ULLONG_MAX and
20 20 * LLONG_MIN. If it is an unsigned type then we use sval.uvalue or if it is
21 21 * signed we use sval.value.
22 22 *
23 23 * I considered just using one bit to store whether the value was signed vs
24 24 * unsigned but I think it might help to have the type information so we know
25 25 * how to do type promotion.
26 26 *
27 27 */
28 28
29 29 #include "smatch.h"
30 30 #include "smatch_slist.h"
31 31 #include "smatch_extra.h"
32 32
33 33 __ALLOCATOR(sval_t, "svals", sval);
34 34
35 35 sval_t *sval_alloc(sval_t sval)
36 36 {
37 37 sval_t *ret;
38 38
39 39 ret = __alloc_sval(0);
40 40 *ret = sval;
41 41 return ret;
42 42 }
43 43
44 44 sval_t *sval_alloc_permanent(sval_t sval)
45 45 {
46 46 sval_t *ret;
47 47
48 48 ret = malloc(sizeof(*ret));
49 49 *ret = sval;
50 50 return ret;
51 51 }
52 52
53 53 sval_t sval_blank(struct expression *expr)
54 54 {
55 55 sval_t ret;
56 56
57 57 ret.type = get_type(expr);
58 58 if (!ret.type)
59 59 ret.type = &int_ctype;
60 60 ret.value = 123456789;
61 61
62 62 return ret;
63 63 }
64 64
65 65 sval_t sval_type_val(struct symbol *type, long long val)
66 66 {
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
67 67 sval_t ret;
68 68
69 69 if (!type)
70 70 type = &llong_ctype;
71 71
72 72 ret.type = type;
73 73 ret.value = val;
74 74 return ret;
75 75 }
76 76
77 +sval_t sval_type_fval(struct symbol *type, long double fval)
78 +{
79 + sval_t ret;
80 +
81 + ret.type = &ldouble_ctype;
82 + ret.ldvalue = fval;
83 + return sval_cast(type, ret);
84 +}
85 +
77 86 sval_t sval_from_val(struct expression *expr, long long val)
78 87 {
79 88 sval_t ret;
80 89
81 90 ret = sval_blank(expr);
82 91 ret.value = val;
83 92 ret = sval_cast(get_type(expr), ret);
84 93
85 94 return ret;
86 95 }
87 96
97 +sval_t sval_from_fval(struct expression *expr, long double fval)
98 +{
99 + sval_t ret;
100 +
101 + ret.type = &ldouble_ctype;
102 + ret.ldvalue = fval;
103 + ret = sval_cast(get_type(expr), ret);
104 +
105 + return ret;
106 +}
107 +
88 108 int sval_is_ptr(sval_t sval)
89 109 {
90 110 if (!sval.type)
91 111 return 0;
92 112 return (sval.type->type == SYM_PTR || sval.type->type == SYM_ARRAY);
93 113 }
94 114
115 +bool sval_is_fp(sval_t sval)
116 +{
117 + return type_is_fp(sval.type);
118 +}
119 +
95 120 int sval_unsigned(sval_t sval)
96 121 {
97 122 if (is_ptr_type(sval.type))
98 123 return true;
99 124 return type_unsigned(sval.type);
100 125 }
101 126
102 127 int sval_signed(sval_t sval)
103 128 {
104 129 return !type_unsigned(sval.type);
105 130 }
106 131
107 132 int sval_bits(sval_t sval)
108 133 {
109 134 return type_bits(sval.type);
110 135 }
111 136
112 137 int sval_bits_used(sval_t sval)
113 138 {
114 139 int i;
115 140
116 141 for (i = 64; i >= 1; i--) {
117 142 if (sval.uvalue & (1ULL << (i - 1)))
118 143 return i;
119 144 }
120 145 return 0;
121 146 }
122 147
123 148 int sval_is_negative(sval_t sval)
124 149 {
125 150 if (type_unsigned(sval.type))
126 151 return 0;
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
127 152 if (sval.value < 0)
128 153 return 1;
129 154 return 0;
130 155 }
131 156
132 157 int sval_is_positive(sval_t sval)
133 158 {
134 159 return !sval_is_negative(sval);
135 160 }
136 161
162 +static bool fp_is_min(sval_t sval)
163 +{
164 + if (sval.type == &float_ctype)
165 + return sval.fvalue == -FLT_MAX;
166 + if (sval.type == &double_ctype)
167 + return sval.dvalue == -DBL_MAX;
168 + if (sval.type == &ldouble_ctype)
169 + return sval.ldvalue == -LDBL_MAX;
170 + sm_perror("%s: bad type: '%s'", __func__, type_to_str(sval.type));
171 + return false;
172 +}
173 +
137 174 int sval_is_min(sval_t sval)
138 175 {
139 176 sval_t min = sval_type_min(sval.type);
140 177
178 + if (sval_is_fp(sval))
179 + return fp_is_min(sval);
180 +
141 181 if (sval_unsigned(sval)) {
142 182 if (sval.uvalue == 0)
143 183 return 1;
144 184 return 0;
145 185 }
146 186 /* return true for less than min as well */
147 187 return (sval.value <= min.value);
148 188 }
149 189
190 +static bool fp_is_max(sval_t sval)
191 +{
192 + if (sval.type == &float_ctype)
193 + return sval.fvalue == FLT_MAX;
194 + if (sval.type == &double_ctype)
195 + return sval.dvalue == DBL_MAX;
196 + if (sval.type == &ldouble_ctype)
197 + return sval.ldvalue == LDBL_MAX;
198 + sm_perror("%s: bad type: '%s'", __func__, type_to_str(sval.type));
199 + return false;
200 +}
201 +
150 202 int sval_is_max(sval_t sval)
151 203 {
152 204 sval_t max = sval_type_max(sval.type);
153 205
206 + if (sval_is_fp(sval))
207 + return fp_is_max(sval);
208 +
154 209 if (sval_unsigned(sval))
155 210 return (sval.uvalue >= max.value);
156 211 return (sval.value >= max.value);
157 212 }
158 213
159 214 int sval_is_a_min(sval_t sval)
160 215 {
161 216 if (sval_is_min(sval))
162 217 return 1;
218 +
219 + if (sval_is_fp(sval))
220 + return 0;
221 +
163 222 if (sval_signed(sval) && sval.value == SHRT_MIN)
164 223 return 1;
165 224 if (sval_signed(sval) && sval.value == INT_MIN)
166 225 return 1;
167 226 if (sval_signed(sval) && sval.value == LLONG_MIN)
168 227 return 1;
169 228 return 0;
170 229 }
171 230
172 231 int sval_is_a_max(sval_t sval)
173 232 {
174 233 if (sval_is_max(sval))
175 234 return 1;
235 +
236 + if (sval_is_fp(sval))
237 + return 0;
238 +
176 239 if (sval.uvalue == SHRT_MAX)
177 240 return 1;
178 241 if (sval.uvalue == INT_MAX)
179 242 return 1;
180 243 if (sval.uvalue == LLONG_MAX)
181 244 return 1;
182 245 if (sval.uvalue == USHRT_MAX)
183 246 return 1;
184 247 if (sval.uvalue == UINT_MAX)
185 248 return 1;
186 249 if (sval_unsigned(sval) && sval.uvalue == ULLONG_MAX)
187 250 return 1;
188 251 if (sval.value > valid_ptr_max - 1000 &&
189 252 sval.value < valid_ptr_max + 1000)
190 253 return 1;
191 254 return 0;
192 255 }
193 256
194 257 int sval_is_negative_min(sval_t sval)
195 258 {
259 + if (sval_is_fp(sval))
260 + return 0;
261 +
196 262 if (!sval_is_negative(sval))
197 263 return 0;
198 264 return sval_is_min(sval);
199 265 }
200 266
201 267 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two)
202 268 {
203 269 sval_t one_cast, two_cast;
204 270
205 271 one_cast = sval_cast(type, one);
206 272 two_cast = sval_cast(type, two);
207 273 return sval_cmp(one_cast, two_cast);
208 274 }
209 275
210 276 int sval_cmp_val(sval_t one, long long val)
211 277 {
212 278 sval_t sval;
213 279
214 280 sval = sval_type_val(&llong_ctype, val);
215 281 return sval_cmp(one, sval);
216 282 }
217 283
218 284 sval_t sval_min(sval_t one, sval_t two)
219 285 {
220 286 if (sval_cmp(one, two) > 0)
221 287 return two;
222 288 return one;
223 289 }
224 290
225 291 sval_t sval_max(sval_t one, sval_t two)
226 292 {
227 293 if (sval_cmp(one, two) < 0)
228 294 return two;
229 295 return one;
230 296 }
231 297
232 298 int sval_too_low(struct symbol *type, sval_t sval)
233 299 {
234 300 if (sval_is_negative(sval) && type_unsigned(type))
235 301 return 1;
236 302 if (type_signed(type) && sval_unsigned(sval))
237 303 return 0;
238 304 if (type_signed(sval.type) &&
239 305 sval.value < sval_type_min(type).value)
240 306 return 1;
241 307 if (sval_cmp(sval, sval_type_min(type)) < 0)
242 308 return 1;
243 309 return 0;
244 310 }
245 311
246 312 int sval_too_high(struct symbol *type, sval_t sval)
↓ open down ↓ |
41 lines elided |
↑ open up ↑ |
247 313 {
248 314 if (sval_is_negative(sval))
249 315 return 0;
250 316 if (sval.uvalue > sval_type_max(type).uvalue)
251 317 return 1;
252 318 return 0;
253 319 }
254 320
255 321 int sval_fits(struct symbol *type, sval_t sval)
256 322 {
323 + /* everything fits into floating point */
324 + if (type_is_fp(type))
325 + return 1;
326 + /* floating points don't fit into int */
327 + if (type_is_fp(sval.type))
328 + return 0;
329 +
257 330 if (sval_too_low(type, sval))
258 331 return 0;
259 332 if (sval_too_high(type, sval))
260 333 return 0;
261 334 return 1;
262 335 }
263 336
337 +static sval_t cast_to_fp(struct symbol *type, sval_t sval)
338 +{
339 + sval_t ret = {};
340 +
341 + ret.type = type;
342 + if (type == &float_ctype) {
343 + if (!sval_is_fp(sval)) {
344 + if (sval_unsigned(sval))
345 + ret.fvalue = sval.uvalue;
346 + else
347 + ret.fvalue = sval.value;
348 + } else if (sval.type == &float_ctype)
349 + ret.fvalue = sval.fvalue;
350 + else if (sval.type == &double_ctype)
351 + ret.fvalue = sval.dvalue;
352 + else
353 + ret.fvalue = sval.ldvalue;
354 + } else if (type == &double_ctype) {
355 + if (!sval_is_fp(sval)) {
356 + if (sval_unsigned(sval))
357 + ret.dvalue = sval.uvalue;
358 + else
359 + ret.dvalue = sval.value;
360 + } else if (sval.type == &float_ctype)
361 + ret.dvalue = sval.fvalue;
362 + else if (sval.type == &double_ctype)
363 + ret.dvalue = sval.dvalue;
364 + else
365 + ret.dvalue = sval.ldvalue;
366 + } else if (type == &ldouble_ctype) {
367 + if (!sval_is_fp(sval)) {
368 + if (sval_unsigned(sval))
369 + ret.ldvalue = (long double)sval.uvalue;
370 + else
371 + ret.ldvalue = (long double)sval.value;
372 + } else if (sval.type == &float_ctype)
373 + ret.ldvalue = sval.fvalue;
374 + else if (sval.type == &double_ctype)
375 + ret.ldvalue = sval.dvalue;
376 + else
377 + ret.ldvalue = sval.ldvalue;
378 + } else {
379 + sm_perror("%s: bad type: %s", __func__, type_to_str(type));
380 + }
381 +
382 + return ret;
383 +}
384 +
385 +static sval_t cast_from_fp(struct symbol *type, sval_t sval)
386 +{
387 + sval_t ret = {};
388 +
389 + ret.type = &llong_ctype;
390 + if (sval.type == &float_ctype)
391 + ret.value = sval.fvalue;
392 + else if (sval.type == &double_ctype)
393 + ret.value = sval.dvalue;
394 + else if (sval.type == &ldouble_ctype)
395 + ret.value = sval.ldvalue;
396 + else
397 + sm_perror("%s: bad type: %s", __func__, type_to_str(type));
398 +
399 + return sval_cast(type, ret);
400 +}
401 +
264 402 sval_t sval_cast(struct symbol *type, sval_t sval)
265 403 {
266 404 sval_t ret;
267 405
268 406 if (!type)
269 407 type = &int_ctype;
270 408
409 + if (type_is_fp(type))
410 + return cast_to_fp(type, sval);
411 + if (type_is_fp(sval.type))
412 + return cast_from_fp(type, sval);
413 +
271 414 ret.type = type;
272 415 switch (sval_bits(ret)) {
273 416 case 1:
274 417 ret.value = !!sval.value;
275 418 break;
276 419 case 8:
277 420 if (sval_unsigned(ret))
278 421 ret.value = (long long)(unsigned char)sval.value;
279 422 else
280 423 ret.value = (long long)(char)sval.value;
281 424 break;
282 425 case 16:
283 426 if (sval_unsigned(ret))
284 427 ret.value = (long long)(unsigned short)sval.value;
285 428 else
286 429 ret.value = (long long)(short)sval.value;
287 430 break;
288 431 case 32:
289 432 if (sval_unsigned(ret))
290 433 ret.value = (long long)(unsigned int)sval.value;
291 434 else
292 435 ret.value = (long long)(int)sval.value;
293 436 break;
294 437 default:
295 438 ret.value = sval.value;
296 439 }
297 440 return ret;
298 441
299 442 }
300 443
301 444 sval_t sval_preop(sval_t sval, int op)
302 445 {
303 446 switch (op) {
304 447 case '!':
305 448 sval.value = !sval.value;
306 449 break;
307 450 case '~':
308 451 sval.value = ~sval.value;
309 452 sval = sval_cast(sval.type, sval);
310 453 break;
311 454 case '-':
312 455 sval.value = -sval.value;
313 456 sval = sval_cast(sval.type, sval);
314 457 break;
315 458 }
316 459 return sval;
317 460 }
318 461
319 462 static sval_t sval_binop_unsigned(struct symbol *type, sval_t left, int op, sval_t right)
320 463 {
321 464 sval_t ret;
322 465
323 466 ret.type = type;
324 467 switch (op) {
325 468 case '*':
326 469 ret.uvalue = left.uvalue * right.uvalue;
327 470 break;
328 471 case '/':
329 472 if (right.uvalue == 0) {
330 473 sm_debug("%s: divide by zero", __func__);
331 474 ret.uvalue = 123456789;
332 475 } else {
333 476 ret.uvalue = left.uvalue / right.uvalue;
334 477 }
335 478 break;
336 479 case '+':
337 480 ret.uvalue = left.uvalue + right.uvalue;
338 481 break;
339 482 case '-':
340 483 ret.uvalue = left.uvalue - right.uvalue;
341 484 break;
342 485 case '%':
343 486 if (right.uvalue == 0) {
344 487 sm_perror(" %s: MOD by zero", __func__);
345 488 ret.uvalue = 123456789;
346 489 } else {
347 490 ret.uvalue = left.uvalue % right.uvalue;
348 491 }
349 492 break;
350 493 case '|':
351 494 ret.uvalue = left.uvalue | right.uvalue;
352 495 break;
353 496 case '&':
354 497 ret.uvalue = left.uvalue & right.uvalue;
355 498 break;
356 499 case SPECIAL_RIGHTSHIFT:
357 500 ret.uvalue = left.uvalue >> right.uvalue;
358 501 break;
359 502 case SPECIAL_LEFTSHIFT:
360 503 ret.uvalue = left.uvalue << right.uvalue;
361 504 break;
362 505 case '^':
363 506 ret.uvalue = left.uvalue ^ right.uvalue;
364 507 break;
365 508 default:
366 509 sm_perror(" %s: unhandled binop %s", __func__,
367 510 show_special(op));
368 511 ret.uvalue = 1234567;
369 512 }
370 513 return ret;
371 514 }
372 515
373 516
374 517 static sval_t sval_binop_signed(struct symbol *type, sval_t left, int op, sval_t right)
375 518 {
376 519 sval_t ret;
377 520
378 521 ret.type = type;
379 522 switch (op) {
380 523 case '*':
381 524 ret.value = left.value * right.value;
382 525 break;
383 526 case '/':
384 527 if (right.value == 0) {
385 528 sm_debug("%s: divide by zero", __func__);
386 529 ret.value = 123456789;
387 530 } else if (left.value == LLONG_MIN && right.value == -1) {
388 531 sm_debug("%s: invalid divide LLONG_MIN/-1", __func__);
389 532 ret.value = 12345678;
390 533 } else {
391 534 ret.value = left.value / right.value;
392 535 }
393 536 break;
394 537 case '+':
395 538 ret.value = left.value + right.value;
396 539 break;
397 540 case '-':
398 541 ret.value = left.value - right.value;
399 542 break;
400 543 case '%':
401 544 if (right.value == 0) {
402 545 sm_perror(" %s: MOD by zero", __func__);
403 546 ret.value = 123456789;
404 547 } else {
405 548 ret.value = left.value % right.value;
406 549 }
407 550 break;
408 551 case '|':
409 552 ret.value = left.value | right.value;
410 553 break;
411 554 case '&':
412 555 ret.value = left.value & right.value;
413 556 break;
414 557 case SPECIAL_RIGHTSHIFT:
415 558 ret.value = left.value >> right.value;
416 559 break;
417 560 case SPECIAL_LEFTSHIFT:
418 561 ret.value = left.value << right.value;
419 562 break;
420 563 case '^':
421 564 ret.value = left.value ^ right.value;
422 565 break;
423 566 default:
424 567 sm_perror(" %s: unhandled binop %s", __func__,
425 568 show_special(op));
426 569 ret.value = 1234567;
427 570 }
428 571 return ret;
429 572 }
430 573
431 574 static sval_t ptr_binop(struct symbol *type, sval_t left, int op, sval_t right)
432 575 {
433 576 sval_t ret;
434 577 int align;
435 578
436 579 if (op != '+' && op != '-')
437 580 return sval_binop_unsigned(type, left, op, right);
438 581
439 582 ret.type = type;
440 583 if (type->type == SYM_PTR)
441 584 type = get_real_base_type(type);
442 585 align = type->ctype.alignment;
443 586 if (align <= 0)
444 587 align = 1;
445 588
446 589 if (op == '+') {
447 590 if (type_is_ptr(left.type))
448 591 ret.value = left.value + right.value * align;
449 592 else
450 593 ret.value = left.value * align + right.value;
451 594 } else {
452 595 if (!type_is_ptr(left.type)) {
453 596 left.value = -left.value;
454 597 ret = ptr_binop(type, left, '+', right);
455 598 } else if (!type_is_ptr(right.type)) {
456 599 right.value = -right.value;
457 600 ret = ptr_binop(type, left, '+', right);
458 601 } else {
459 602 ret.value = (left.value - right.value) / align;
460 603 }
461 604 }
462 605
463 606 if (op == '-')
464 607 ret.type = ssize_t_ctype;
465 608 return ret;
466 609 }
467 610
468 611 sval_t sval_binop(sval_t left, int op, sval_t right)
469 612 {
470 613 struct symbol *type;
471 614 sval_t ret;
472 615
473 616 type = get_promoted_type(left.type, right.type);
474 617
475 618 if (type_is_ptr(type))
476 619 ret = ptr_binop(type, left, op, right);
477 620 else if (type_unsigned(type))
478 621 ret = sval_binop_unsigned(type, left, op, right);
479 622 else
480 623 ret = sval_binop_signed(type, left, op, right);
481 624 return sval_cast(type, ret);
482 625 }
483 626
484 627 int sval_unop_overflows(sval_t sval, int op)
485 628 {
486 629 if (op != '-')
487 630 return 0;
488 631 if (sval_positive_bits(sval) == 32 && sval.value == INT_MIN)
489 632 return 1;
490 633 if (sval_positive_bits(sval) == 64 && sval.value == LLONG_MIN)
491 634 return 1;
492 635 if (sval_is_negative(sval))
493 636 return 0;
494 637 if (sval_signed(sval))
495 638 return 0;
496 639 if (sval_bits(sval) == 32 && sval.uvalue > INT_MAX)
497 640 return 1;
498 641 if (sval_bits(sval) == 64 && sval.uvalue > LLONG_MAX)
499 642 return 1;
500 643 return 0;
501 644 }
502 645
503 646 int sval_binop_overflows(sval_t left, int op, sval_t right)
504 647 {
505 648 struct symbol *type;
506 649 sval_t max, min;
507 650
508 651 type = left.type;
509 652 if (type_positive_bits(right.type) > type_positive_bits(left.type))
510 653 type = right.type;
511 654 if (type_positive_bits(type) < 31)
512 655 type = &int_ctype;
513 656
514 657 max = sval_type_max(type);
515 658 min = sval_type_min(type);
516 659
517 660 switch (op) {
518 661 case '+':
519 662 if (sval_is_negative(left) && sval_is_negative(right)) {
520 663 if (left.value < min.value + right.value)
521 664 return 1;
522 665 return 0;
523 666 }
524 667 if (sval_is_negative(left) || sval_is_negative(right))
525 668 return 0;
526 669 if (left.uvalue > max.uvalue - right.uvalue)
527 670 return 1;
528 671 return 0;
529 672 case '*':
530 673 if (type_signed(type)) {
531 674 if (left.value == 0 || right.value == 0)
532 675 return 0;
533 676 if (left.value > max.value / right.value)
534 677 return 1;
535 678 if (left.value == -1 || right.value == -1)
536 679 return 0;
537 680 return left.value != left.value * right.value / right.value;
538 681
539 682 }
540 683 return right.uvalue != 0 && left.uvalue > max.uvalue / right.uvalue;
541 684 case '-':
542 685 if (type_unsigned(type)) {
543 686 if (sval_cmp(left, right) < 0)
544 687 return 1;
545 688 return 0;
546 689 }
547 690 if (sval_is_negative(left) && sval_is_negative(right))
548 691 return 0;
549 692
550 693 if (sval_is_negative(left)) {
551 694 if (left.value < min.value + right.value)
552 695 return 1;
553 696 return 0;
554 697 }
555 698 if (sval_is_negative(right)) {
556 699 if (right.value == min.value)
557 700 return 1;
558 701 right = sval_preop(right, '-');
559 702 if (sval_binop_overflows(left, '+', right))
560 703 return 1;
561 704 return 0;
562 705 }
563 706 return 0;
564 707 case SPECIAL_LEFTSHIFT:
565 708 if (sval_cmp(left, sval_binop(max, invert_op(op), right)) > 0)
566 709 return 1;
567 710 return 0;
568 711 }
569 712 return 0;
570 713 }
571 714
572 715 int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right)
573 716 {
574 717
575 718 struct symbol *type;
576 719
577 720 type = left.type;
578 721 if (type_positive_bits(right.type) > type_positive_bits(left.type))
579 722 type = right.type;
580 723 if (type_positive_bits(type) <= 31)
581 724 type = &uint_ctype;
582 725 else
583 726 type = &ullong_ctype;
584 727
585 728 left = sval_cast(type, left);
586 729 right = sval_cast(type, right);
587 730 return sval_binop_overflows(left, op, right);
588 731 }
589 732
590 733 int find_first_zero_bit(unsigned long long uvalue)
591 734 {
592 735 int i;
593 736
594 737 for (i = 0; i < 64; i++) {
595 738 if (!(uvalue & (1ULL << i)))
596 739 return i;
597 740 }
598 741 return i;
599 742 }
600 743
601 744 int sm_fls64(unsigned long long uvalue)
602 745 {
603 746 int high_bit = 0;
604 747
605 748 while (uvalue) {
606 749 uvalue >>= 1;
607 750 high_bit++;
608 751 }
609 752
610 753 return high_bit;
611 754 }
612 755
613 756 unsigned long long fls_mask(unsigned long long uvalue)
614 757 {
615 758 int high_bit = 0;
616 759
617 760 high_bit = sm_fls64(uvalue);
618 761 if (high_bit == 0)
↓ open down ↓ |
338 lines elided |
↑ open up ↑ |
619 762 return 0;
620 763
621 764 return ((unsigned long long)-1) >> (64 - high_bit);
622 765 }
623 766
624 767 unsigned long long sval_fls_mask(sval_t sval)
625 768 {
626 769 return fls_mask(sval.uvalue);
627 770 }
628 771
772 +static char *fp_to_str(sval_t sval)
773 +{
774 + char buf[32];
775 +
776 + if (sval.type == &float_ctype)
777 + snprintf(buf, sizeof(buf), "%f", sval.fvalue);
778 + else if (sval.type == &double_ctype)
779 + snprintf(buf, sizeof(buf), "%e", sval.dvalue);
780 + else if (sval.type == &ldouble_ctype) {
781 + snprintf(buf, sizeof(buf), "%Lf", sval.ldvalue);
782 + } else
783 + snprintf(buf, sizeof(buf), "nan");
784 +
785 + return alloc_sname(buf);
786 +}
787 +
629 788 const char *sval_to_str(sval_t sval)
630 789 {
631 - char buf[30];
790 + char buf[32];
632 791
792 + if (sval_is_fp(sval))
793 + return fp_to_str(sval);
794 +
633 795 if (sval_is_ptr(sval) && sval.value == valid_ptr_max)
634 796 return "ptr_max";
635 797 if (sval_unsigned(sval) && sval.value == ULLONG_MAX)
636 798 return "u64max";
637 799 if (sval_unsigned(sval) && sval.value == UINT_MAX)
638 800 return "u32max";
639 801 if (sval.value == USHRT_MAX)
640 802 return "u16max";
641 803
642 804 if (sval_signed(sval) && sval.value == LLONG_MAX)
643 805 return "s64max";
644 806 if (sval.value == INT_MAX)
645 807 return "s32max";
646 808 if (sval.value == SHRT_MAX)
647 809 return "s16max";
648 810
649 811 if (sval_signed(sval) && sval.value == SHRT_MIN)
650 812 return "s16min";
651 813 if (sval_signed(sval) && sval.value == INT_MIN)
652 814 return "s32min";
653 815 if (sval_signed(sval) && sval.value == LLONG_MIN)
654 816 return "s64min";
655 817
656 818 if (sval_unsigned(sval))
657 819 snprintf(buf, sizeof(buf), "%llu", sval.value);
658 820 else if (sval.value < 0)
659 821 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
660 822 else
661 823 snprintf(buf, sizeof(buf), "%lld", sval.value);
662 824
663 825 return alloc_sname(buf);
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
664 826 }
665 827
666 828 const char *sval_to_str_or_err_ptr(sval_t sval)
667 829 {
668 830 char buf[12];
669 831
670 832 if (option_project != PROJ_KERNEL ||
671 833 !is_ptr_type(sval.type))
672 834 return sval_to_str(sval);
673 835
674 - if (sval.uvalue >= -4905ULL) {
836 + if (!sval_is_fp(sval) && sval.uvalue >= -4905ULL) {
675 837 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
676 838 return alloc_sname(buf);
677 839 }
678 840
679 841 return sval_to_str(sval);
680 842 }
681 843
682 844 const char *sval_to_numstr(sval_t sval)
683 845 {
684 846 char buf[30];
685 847
848 + if (type_is_fp(sval.type))
849 + return fp_to_str(sval);
850 +
686 851 if (sval_unsigned(sval))
687 852 snprintf(buf, sizeof(buf), "%llu", sval.value);
688 853 else if (sval.value < 0)
689 854 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
690 855 else
691 856 snprintf(buf, sizeof(buf), "%lld", sval.value);
692 857
693 858 return alloc_sname(buf);
694 859 }
695 860
696 861 sval_t ll_to_sval(long long val)
697 862 {
698 863 sval_t ret;
699 864
700 865 ret.type = &llong_ctype;
701 866 ret.value = val;
702 867 return ret;
703 868 }
704 869
705 870 static void free_svals(struct symbol *sym)
706 871 {
707 872 if (__inline_fn)
708 873 return;
709 874 clear_sval_alloc();
710 875 }
711 876
712 877 void register_sval(int my_id)
713 878 {
714 879 add_hook(&free_svals, AFTER_FUNC_HOOK);
715 880 }
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX