Print this page
11506 smatch resync


  50         return ret;
  51 }
  52 
  53 sval_t sval_blank(struct expression *expr)
  54 {
  55         sval_t ret;
  56 
  57         ret.type = get_type(expr);
  58         if (!ret.type)
  59                 ret.type = &int_ctype;
  60         ret.value = 123456789;
  61 
  62         return ret;
  63 }
  64 
  65 sval_t sval_type_val(struct symbol *type, long long val)
  66 {
  67         sval_t ret;
  68 
  69         if (!type)
  70                 type = &int_ctype;
  71 
  72         ret.type = type;
  73         ret.value = val;
  74         return ret;
  75 }
  76 
  77 sval_t sval_from_val(struct expression *expr, long long val)
  78 {
  79         sval_t ret;
  80 
  81         ret = sval_blank(expr);
  82         ret.value = val;
  83         ret = sval_cast(get_type(expr), ret);
  84 
  85         return ret;
  86 }
  87 
  88 int sval_is_ptr(sval_t sval)
  89 {
  90         if (!sval.type)
  91                 return 0;
  92         return (sval.type->type == SYM_PTR || sval.type->type == SYM_ARRAY);
  93 }
  94 
  95 int sval_unsigned(sval_t sval)
  96 {


  97         return type_unsigned(sval.type);
  98 }
  99 
 100 int sval_signed(sval_t sval)
 101 {
 102         return !type_unsigned(sval.type);
 103 }
 104 
 105 int sval_bits(sval_t sval)
 106 {
 107         return type_bits(sval.type);
 108 }
 109 
 110 int sval_bits_used(sval_t sval)
 111 {
 112         int i;
 113 
 114         for (i = 64; i >= 1; i--) {
 115                 if (sval.uvalue & (1ULL << (i - 1)))
 116                         return i;


 441         if (align <= 0)
 442                 align = 1;
 443 
 444         if (op == '+') {
 445                 if (type_is_ptr(left.type))
 446                         ret.value = left.value + right.value * align;
 447                 else
 448                         ret.value = left.value * align + right.value;
 449         } else {
 450                 if (!type_is_ptr(left.type)) {
 451                         left.value = -left.value;
 452                         ret = ptr_binop(type, left, '+', right);
 453                 } else if (!type_is_ptr(right.type)) {
 454                         right.value = -right.value;
 455                         ret = ptr_binop(type, left, '+', right);
 456                 } else {
 457                         ret.value = (left.value - right.value) / align;
 458                 }
 459         }
 460 


 461         return ret;
 462 }
 463 
 464 sval_t sval_binop(sval_t left, int op, sval_t right)
 465 {
 466         struct symbol *type;
 467         sval_t ret;
 468 
 469         type = get_promoted_type(left.type, right.type);
 470 
 471         if (type_is_ptr(type))
 472                 ret = ptr_binop(type, left, op, right);
 473         else if (type_unsigned(type))
 474                 ret = sval_binop_unsigned(type, left, op, right);
 475         else
 476                 ret = sval_binop_signed(type, left, op, right);
 477         return sval_cast(type, ret);
 478 }
 479 
 480 int sval_unop_overflows(sval_t sval, int op)


 566 }
 567 
 568 int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right)
 569 {
 570 
 571         struct symbol *type;
 572 
 573         type = left.type;
 574         if (type_positive_bits(right.type) > type_positive_bits(left.type))
 575                 type = right.type;
 576         if (type_positive_bits(type) <= 31)
 577                 type = &uint_ctype;
 578         else
 579                 type = &ullong_ctype;
 580 
 581         left = sval_cast(type, left);
 582         right = sval_cast(type, right);
 583         return sval_binop_overflows(left, op, right);
 584 }
 585 
 586 unsigned long long fls_mask(unsigned long long uvalue)
 587 {
 588         unsigned long long high_bit = 0;
 589 











 590         while (uvalue) {
 591                 uvalue >>= 1;
 592                 high_bit++;
 593         }
 594 








 595         if (high_bit == 0)
 596                 return 0;
 597 
 598         return ((unsigned long long)-1) >> (64 - high_bit);
 599 }
 600 
 601 unsigned long long sval_fls_mask(sval_t sval)
 602 {
 603         return fls_mask(sval.uvalue);
 604 }
 605 
 606 const char *sval_to_str(sval_t sval)
 607 {
 608         char buf[30];
 609 


 610         if (sval_unsigned(sval) && sval.value == ULLONG_MAX)
 611                 return "u64max";
 612         if (sval_unsigned(sval) && sval.value == UINT_MAX)
 613                 return "u32max";
 614         if (sval.value == USHRT_MAX)
 615                 return "u16max";
 616 
 617         if (sval_signed(sval) && sval.value == LLONG_MAX)
 618                 return "s64max";
 619         if (sval.value == INT_MAX)
 620                 return "s32max";
 621         if (sval.value == SHRT_MAX)
 622                 return "s16max";
 623 
 624         if (sval_signed(sval) && sval.value == SHRT_MIN)
 625                 return "s16min";
 626         if (sval_signed(sval) && sval.value == INT_MIN)
 627                 return "s32min";
 628         if (sval_signed(sval) && sval.value == LLONG_MIN)
 629                 return "s64min";
 630 
 631         if (sval_unsigned(sval))
 632                 snprintf(buf, sizeof(buf), "%llu", sval.value);
 633         else if (sval.value < 0)
 634                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 635         else
 636                 snprintf(buf, sizeof(buf), "%lld", sval.value);
 637 
 638         return alloc_sname(buf);
 639 }
 640 
















 641 const char *sval_to_numstr(sval_t sval)
 642 {
 643         char buf[30];
 644 
 645         if (sval_unsigned(sval))
 646                 snprintf(buf, sizeof(buf), "%llu", sval.value);
 647         else if (sval.value < 0)
 648                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 649         else
 650                 snprintf(buf, sizeof(buf), "%lld", sval.value);
 651 
 652         return alloc_sname(buf);
 653 }
 654 
 655 sval_t ll_to_sval(long long val)
 656 {
 657         sval_t ret;
 658 
 659         ret.type = &llong_ctype;
 660         ret.value = val;


  50         return ret;
  51 }
  52 
  53 sval_t sval_blank(struct expression *expr)
  54 {
  55         sval_t ret;
  56 
  57         ret.type = get_type(expr);
  58         if (!ret.type)
  59                 ret.type = &int_ctype;
  60         ret.value = 123456789;
  61 
  62         return ret;
  63 }
  64 
  65 sval_t sval_type_val(struct symbol *type, long long val)
  66 {
  67         sval_t ret;
  68 
  69         if (!type)
  70                 type = &llong_ctype;
  71 
  72         ret.type = type;
  73         ret.value = val;
  74         return ret;
  75 }
  76 
  77 sval_t sval_from_val(struct expression *expr, long long val)
  78 {
  79         sval_t ret;
  80 
  81         ret = sval_blank(expr);
  82         ret.value = val;
  83         ret = sval_cast(get_type(expr), ret);
  84 
  85         return ret;
  86 }
  87 
  88 int sval_is_ptr(sval_t sval)
  89 {
  90         if (!sval.type)
  91                 return 0;
  92         return (sval.type->type == SYM_PTR || sval.type->type == SYM_ARRAY);
  93 }
  94 
  95 int sval_unsigned(sval_t sval)
  96 {
  97         if (is_ptr_type(sval.type))
  98                 return true;
  99         return type_unsigned(sval.type);
 100 }
 101 
 102 int sval_signed(sval_t sval)
 103 {
 104         return !type_unsigned(sval.type);
 105 }
 106 
 107 int sval_bits(sval_t sval)
 108 {
 109         return type_bits(sval.type);
 110 }
 111 
 112 int sval_bits_used(sval_t sval)
 113 {
 114         int i;
 115 
 116         for (i = 64; i >= 1; i--) {
 117                 if (sval.uvalue & (1ULL << (i - 1)))
 118                         return i;


 443         if (align <= 0)
 444                 align = 1;
 445 
 446         if (op == '+') {
 447                 if (type_is_ptr(left.type))
 448                         ret.value = left.value + right.value * align;
 449                 else
 450                         ret.value = left.value * align + right.value;
 451         } else {
 452                 if (!type_is_ptr(left.type)) {
 453                         left.value = -left.value;
 454                         ret = ptr_binop(type, left, '+', right);
 455                 } else if (!type_is_ptr(right.type)) {
 456                         right.value = -right.value;
 457                         ret = ptr_binop(type, left, '+', right);
 458                 } else {
 459                         ret.value = (left.value - right.value) / align;
 460                 }
 461         }
 462 
 463         if (op == '-')
 464                 ret.type = ssize_t_ctype;
 465         return ret;
 466 }
 467 
 468 sval_t sval_binop(sval_t left, int op, sval_t right)
 469 {
 470         struct symbol *type;
 471         sval_t ret;
 472 
 473         type = get_promoted_type(left.type, right.type);
 474 
 475         if (type_is_ptr(type))
 476                 ret = ptr_binop(type, left, op, right);
 477         else if (type_unsigned(type))
 478                 ret = sval_binop_unsigned(type, left, op, right);
 479         else
 480                 ret = sval_binop_signed(type, left, op, right);
 481         return sval_cast(type, ret);
 482 }
 483 
 484 int sval_unop_overflows(sval_t sval, int op)


 570 }
 571 
 572 int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right)
 573 {
 574 
 575         struct symbol *type;
 576 
 577         type = left.type;
 578         if (type_positive_bits(right.type) > type_positive_bits(left.type))
 579                 type = right.type;
 580         if (type_positive_bits(type) <= 31)
 581                 type = &uint_ctype;
 582         else
 583                 type = &ullong_ctype;
 584 
 585         left = sval_cast(type, left);
 586         right = sval_cast(type, right);
 587         return sval_binop_overflows(left, op, right);
 588 }
 589 
 590 int find_first_zero_bit(unsigned long long uvalue)
 591 {
 592         int i;
 593 
 594         for (i = 0; i < 64; i++) {
 595                 if (!(uvalue & (1ULL << i)))
 596                         return i;
 597         }
 598         return i;
 599 }
 600 
 601 int sm_fls64(unsigned long long uvalue)
 602 {
 603         int high_bit = 0;
 604 
 605         while (uvalue) {
 606                 uvalue >>= 1;
 607                 high_bit++;
 608         }
 609 
 610         return high_bit;
 611 }
 612 
 613 unsigned long long fls_mask(unsigned long long uvalue)
 614 {
 615         int high_bit = 0;
 616 
 617         high_bit = sm_fls64(uvalue);
 618         if (high_bit == 0)
 619                 return 0;
 620 
 621         return ((unsigned long long)-1) >> (64 - high_bit);
 622 }
 623 
 624 unsigned long long sval_fls_mask(sval_t sval)
 625 {
 626         return fls_mask(sval.uvalue);
 627 }
 628 
 629 const char *sval_to_str(sval_t sval)
 630 {
 631         char buf[30];
 632 
 633         if (sval_is_ptr(sval) && sval.value == valid_ptr_max)
 634                 return "ptr_max";
 635         if (sval_unsigned(sval) && sval.value == ULLONG_MAX)
 636                 return "u64max";
 637         if (sval_unsigned(sval) && sval.value == UINT_MAX)
 638                 return "u32max";
 639         if (sval.value == USHRT_MAX)
 640                 return "u16max";
 641 
 642         if (sval_signed(sval) && sval.value == LLONG_MAX)
 643                 return "s64max";
 644         if (sval.value == INT_MAX)
 645                 return "s32max";
 646         if (sval.value == SHRT_MAX)
 647                 return "s16max";
 648 
 649         if (sval_signed(sval) && sval.value == SHRT_MIN)
 650                 return "s16min";
 651         if (sval_signed(sval) && sval.value == INT_MIN)
 652                 return "s32min";
 653         if (sval_signed(sval) && sval.value == LLONG_MIN)
 654                 return "s64min";
 655 
 656         if (sval_unsigned(sval))
 657                 snprintf(buf, sizeof(buf), "%llu", sval.value);
 658         else if (sval.value < 0)
 659                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 660         else
 661                 snprintf(buf, sizeof(buf), "%lld", sval.value);
 662 
 663         return alloc_sname(buf);
 664 }
 665 
 666 const char *sval_to_str_or_err_ptr(sval_t sval)
 667 {
 668         char buf[12];
 669 
 670         if (option_project != PROJ_KERNEL ||
 671             !is_ptr_type(sval.type))
 672                 return sval_to_str(sval);
 673 
 674         if (sval.uvalue >= -4905ULL) {
 675                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 676                 return alloc_sname(buf);
 677         }
 678 
 679         return sval_to_str(sval);
 680 }
 681 
 682 const char *sval_to_numstr(sval_t sval)
 683 {
 684         char buf[30];
 685 
 686         if (sval_unsigned(sval))
 687                 snprintf(buf, sizeof(buf), "%llu", sval.value);
 688         else if (sval.value < 0)
 689                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 690         else
 691                 snprintf(buf, sizeof(buf), "%lld", sval.value);
 692 
 693         return alloc_sname(buf);
 694 }
 695 
 696 sval_t ll_to_sval(long long val)
 697 {
 698         sval_t ret;
 699 
 700         ret.type = &llong_ctype;
 701         ret.value = val;