Print this page
11972 resync smatch

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/smatch/src/symbol.c
          +++ new/usr/src/tools/smatch/src/symbol.c
↓ open down ↓ 25 lines elided ↑ open up ↑
  26   26  #include <stdio.h>
  27   27  #include <string.h>
  28   28  
  29   29  #include "lib.h"
  30   30  #include "allocate.h"
  31   31  #include "token.h"
  32   32  #include "parse.h"
  33   33  #include "symbol.h"
  34   34  #include "scope.h"
  35   35  #include "expression.h"
       36 +#include "evaluate.h"
  36   37  
  37   38  #include "target.h"
  38   39  
  39   40  /*
  40   41   * Secondary symbol list for stuff that needs to be output because it
  41   42   * was used. 
  42   43   */
  43   44  struct symbol_list *translation_unit_used_list = NULL;
  44   45  
  45   46  /*
  46   47   * If the symbol is an inline symbol, add it to the list of symbols to parse
  47   48   */
  48   49  void access_symbol(struct symbol *sym)
  49   50  {
  50   51          if (sym->ctype.modifiers & MOD_INLINE) {
  51      -                if (!(sym->ctype.modifiers & MOD_ACCESSED)) {
       52 +                if (!sym->accessed) {
  52   53                          add_symbol(&translation_unit_used_list, sym);
  53      -                        sym->ctype.modifiers |= MOD_ACCESSED;
       54 +                        sym->accessed = 1;
  54   55                  }
  55   56          }
  56   57  }
  57   58  
  58   59  struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
  59   60  {
  60   61          struct symbol *sym;
  61   62  
  62   63          for (sym = ident->symbols; sym; sym = sym->next_id) {
  63   64                  if (sym->namespace & ns) {
↓ open down ↓ 142 lines elided ↑ open up ↑
 206  207  }
 207  208  
 208  209  static struct symbol *examine_base_type(struct symbol *sym)
 209  210  {
 210  211          struct symbol *base_type;
 211  212  
 212  213          /* Check the base type */
 213  214          base_type = examine_symbol_type(sym->ctype.base_type);
 214  215          if (!base_type || base_type->type == SYM_PTR)
 215  216                  return base_type;
 216      -        sym->ctype.as |= base_type->ctype.as;
      217 +        combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
 217  218          sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
 218  219          concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
 219  220                          (struct ptr_list **)&sym->ctype.contexts);
 220  221          if (base_type->type == SYM_NODE) {
 221  222                  base_type = base_type->ctype.base_type;
 222  223                  sym->ctype.base_type = base_type;
 223  224          }
 224  225          return base_type;
 225  226  }
 226  227  
↓ open down ↓ 43 lines elided ↑ open up ↑
 270  271                  modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
 271  272          sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
 272  273          return sym;
 273  274  }
 274  275  
 275  276  /*
 276  277   * "typeof" will have to merge the types together
 277  278   */
 278  279  void merge_type(struct symbol *sym, struct symbol *base_type)
 279  280  {
 280      -        sym->ctype.as |= base_type->ctype.as;
      281 +        combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
 281  282          sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
 282  283          concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
 283  284                          (struct ptr_list **)&sym->ctype.contexts);
 284  285          sym->ctype.base_type = base_type->ctype.base_type;
 285  286          if (sym->ctype.base_type->type == SYM_NODE)
 286  287                  merge_type(sym, sym->ctype.base_type);
 287  288  }
 288  289  
 289  290  static int count_array_initializer(struct symbol *t, struct expression *expr)
 290  291  {
↓ open down ↓ 66 lines elided ↑ open up ↑
 357  358  
 358  359  static struct expression *get_symbol_initializer(struct symbol *sym)
 359  360  {
 360  361          do {
 361  362                  if (sym->initializer)
 362  363                          return sym->initializer;
 363  364          } while ((sym = sym->same_symbol) != NULL);
 364  365          return NULL;
 365  366  }
 366  367  
      368 +static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
      369 +{
      370 +        struct symbol *arr_ori = node->ctype.base_type;
      371 +        struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
      372 +        struct symbol *elem_type = arr_ori->ctype.base_type;
      373 +        struct expression *size = alloc_const_expression(node->pos, count);
      374 +        unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
      375 +
      376 +        *arr_new = *arr_ori;
      377 +        arr_new->bit_size = bit_size;
      378 +        arr_new->array_size = size;
      379 +        node->array_size = size;
      380 +        node->ctype.base_type = arr_new;
      381 +
      382 +        return bit_size;
      383 +}
      384 +
 367  385  static struct symbol * examine_node_type(struct symbol *sym)
 368  386  {
 369  387          struct symbol *base_type = examine_base_type(sym);
 370  388          int bit_size;
 371  389          unsigned long alignment;
 372  390  
 373  391          /* SYM_NODE - figure out what the type of the node was.. */
 374  392          bit_size = 0;
 375  393          alignment = 0;
 376  394          if (!base_type)
↓ open down ↓ 9 lines elided ↑ open up ↑
 386  404                  sym->ctype.alignment = alignment;
 387  405  
 388  406          /* Unsized array? The size might come from the initializer.. */
 389  407          if (bit_size < 0 && base_type->type == SYM_ARRAY) {
 390  408                  struct expression *initializer = get_symbol_initializer(sym);
 391  409                  if (initializer) {
 392  410                          struct symbol *node_type = base_type->ctype.base_type;
 393  411                          int count = count_array_initializer(node_type, initializer);
 394  412  
 395  413                          if (node_type && node_type->bit_size >= 0)
 396      -                                bit_size = array_element_offset(node_type->bit_size, count);
      414 +                                bit_size = implicit_array_size(sym, count);
 397  415                  }
 398  416          }
 399  417          
 400  418          sym->bit_size = bit_size;
 401  419          return sym;
 402  420  }
 403  421  
 404  422  static struct symbol *examine_enum_type(struct symbol *sym)
 405  423  {
 406  424          struct symbol *base_type = examine_base_type(sym);
↓ open down ↓ 65 lines elided ↑ open up ↑
 472  490                                  warning(base->pos, "typeof applied to bitfield type");
 473  491                          if (base->type == SYM_NODE) {
 474  492                                  mod |= base->ctype.modifiers & MOD_TYPEOF;
 475  493                                  base = base->ctype.base_type;
 476  494                          }
 477  495                          sym->type = SYM_NODE;
 478  496                          sym->ctype.modifiers = mod;
 479  497                          sym->ctype.base_type = base;
 480  498                          return examine_node_type(sym);
 481  499                  }
 482      -                break;
      500 +                sym->type = SYM_NODE;
      501 +                sym->ctype.base_type = &bad_ctype;
      502 +                return sym;
 483  503          }
 484  504          case SYM_PREPROCESSOR:
 485  505                  sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
 486  506                  return NULL;
 487  507          case SYM_UNINITIALIZED:
 488      -//              sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
      508 +//              sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
 489  509                  return NULL;
 490  510          case SYM_RESTRICT:
 491  511                  examine_base_type(sym);
 492  512                  return sym;
 493  513          case SYM_FOULED:
 494  514                  examine_base_type(sym);
 495  515                  return sym;
 496  516          default:
 497  517  //              sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
 498  518                  break;
↓ open down ↓ 171 lines elided ↑ open up ↑
 670  690                  char_ctype, schar_ctype, uchar_ctype,
 671  691                  short_ctype, sshort_ctype, ushort_ctype,
 672  692                  int_ctype, sint_ctype, uint_ctype,
 673  693                  long_ctype, slong_ctype, ulong_ctype,
 674  694                  llong_ctype, sllong_ctype, ullong_ctype,
 675  695                  lllong_ctype, slllong_ctype, ulllong_ctype,
 676  696                  float_ctype, double_ctype, ldouble_ctype,
 677  697                  string_ctype, ptr_ctype, lazy_ptr_ctype,
 678  698                  incomplete_ctype, label_ctype, bad_ctype,
 679  699                  null_ctype;
      700 +struct symbol   int_ptr_ctype, uint_ptr_ctype;
      701 +struct symbol   long_ptr_ctype, ulong_ptr_ctype;
      702 +struct symbol   llong_ptr_ctype, ullong_ptr_ctype;
      703 +struct symbol   float32_ctype, float32x_ctype;
      704 +struct symbol   float64_ctype, float64x_ctype;
      705 +struct symbol   float128_ctype;
      706 +struct symbol   const_void_ctype, const_char_ctype;
      707 +struct symbol   const_ptr_ctype, const_string_ctype;
 680  708  
 681  709  struct symbol   zero_int;
 682  710  
 683  711  #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
 684  712  #define __IDENT(n,str,res) \
 685  713          struct ident n  = __INIT_IDENT(str,res)
 686  714  
 687  715  #include "ident-list.h"
 688  716  
 689  717  void init_symbols(void)
↓ open down ↓ 6 lines elided ↑ open up ↑
 696  724  
 697  725          init_parser(stream);
 698  726          init_builtins(stream);
 699  727  }
 700  728  
 701  729  #ifdef __CHAR_UNSIGNED__
 702  730  #define CHAR_SIGNEDNESS MOD_UNSIGNED
 703  731  #else
 704  732  #define CHAR_SIGNEDNESS MOD_SIGNED
 705  733  #endif
      734 +// For fix-sized types
      735 +static int bits_in_type32 = 32;
      736 +static int bits_in_type64 = 64;
      737 +static int bits_in_type128 = 128;
 706  738  
 707  739  #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
 708  740  #define MOD_LL (MOD_LONG | MOD_LONGLONG)
 709  741  #define MOD_LLL MOD_LONGLONGLONG
 710  742  static const struct ctype_declare {
 711  743          struct symbol *ptr;
 712  744          enum type type;
 713  745          unsigned long modifiers;
 714  746          int *bit_size;
 715  747          int *maxalign;
↓ open down ↓ 21 lines elided ↑ open up ↑
 737  769          { &sllong_ctype,    SYM_BASETYPE, MOD_ESIGNED | MOD_LL,     &bits_in_longlong,       &max_int_alignment, &int_type },
 738  770          { &ullong_ctype,    SYM_BASETYPE, MOD_UNSIGNED | MOD_LL,    &bits_in_longlong,       &max_int_alignment, &int_type },
 739  771          { &lllong_ctype,    SYM_BASETYPE, MOD_SIGNED | MOD_LLL,     &bits_in_longlonglong,   &max_int_alignment, &int_type },
 740  772          { &slllong_ctype,   SYM_BASETYPE, MOD_ESIGNED | MOD_LLL,    &bits_in_longlonglong,   &max_int_alignment, &int_type },
 741  773          { &ulllong_ctype,   SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL,   &bits_in_longlonglong,   &max_int_alignment, &int_type },
 742  774  
 743  775          { &float_ctype,     SYM_BASETYPE,  0,                       &bits_in_float,          &max_fp_alignment,  &fp_type },
 744  776          { &double_ctype,    SYM_BASETYPE, MOD_LONG,                 &bits_in_double,         &max_fp_alignment,  &fp_type },
 745  777          { &ldouble_ctype,   SYM_BASETYPE, MOD_LONG | MOD_LONGLONG,  &bits_in_longdouble,     &max_fp_alignment,  &fp_type },
 746  778  
      779 +        { &float32_ctype,   SYM_BASETYPE,  0,                       &bits_in_type32,          &max_fp_alignment, &fp_type },
      780 +        { &float32x_ctype,  SYM_BASETYPE, MOD_LONG,                 &bits_in_double,         &max_fp_alignment,  &fp_type },
      781 +        { &float64_ctype,   SYM_BASETYPE,  0,                       &bits_in_type64,          &max_fp_alignment, &fp_type },
      782 +        { &float64x_ctype,  SYM_BASETYPE, MOD_LONG | MOD_LONGLONG,  &bits_in_longdouble,     &max_fp_alignment,  &fp_type },
      783 +        { &float128_ctype,  SYM_BASETYPE,  0,                       &bits_in_type128,         &max_alignment,    &fp_type },
      784 +
 747  785          { &string_ctype,    SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &char_ctype },
 748  786          { &ptr_ctype,       SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 749  787          { &null_ctype,      SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 750  788          { &label_ctype,     SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 751  789          { &lazy_ptr_ctype,  SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
      790 +        { &int_ptr_ctype,   SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &int_ctype },
      791 +        { &uint_ptr_ctype,  SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &uint_ctype },
      792 +        { &long_ptr_ctype,  SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &long_ctype },
      793 +        { &ulong_ptr_ctype, SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &ulong_ctype },
      794 +        { &llong_ptr_ctype, SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &llong_ctype },
      795 +        { &ullong_ptr_ctype,SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &ullong_ctype },
      796 +
      797 +        { &const_void_ctype, SYM_NODE,    MOD_CONST,                NULL, NULL, &void_ctype },
      798 +        { &const_char_ctype, SYM_NODE,    MOD_CONST,                &bits_in_char, &max_int_alignment, &char_ctype },
      799 +        { &const_ptr_ctype, SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &const_void_ctype },
      800 +        { &const_string_ctype,SYM_PTR,    0,                        &bits_in_pointer,        &pointer_alignment, &const_char_ctype },
 752  801          { NULL, }
 753  802  };
 754  803  #undef MOD_LLL
 755  804  #undef MOD_LL
 756  805  #undef MOD_ESIGNED
 757  806  
 758  807  void init_ctype(void)
 759  808  {
 760  809          const struct ctype_declare *ctype;
 761  810  
↓ open down ↓ 4 lines elided ↑ open up ↑
 766  815                  unsigned long alignment = bits_to_bytes(bit_size);
 767  816  
 768  817                  if (alignment > maxalign)
 769  818                          alignment = maxalign;
 770  819                  sym->type = ctype->type;
 771  820                  sym->bit_size = bit_size;
 772  821                  sym->ctype.alignment = alignment;
 773  822                  sym->ctype.base_type = ctype->base_type;
 774  823                  sym->ctype.modifiers = ctype->modifiers;
 775  824          }
      825 +
      826 +        // and now some adjustments
      827 +        if (funsigned_char) {
      828 +                char_ctype.ctype.modifiers |= MOD_UNSIGNED;
      829 +                char_ctype.ctype.modifiers &= ~MOD_SIGNED;
      830 +        }
 776  831  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX