OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [libcpp/] [symtab.c] - Diff between revs 816 and 826

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 816 Rev 826
/* Hash tables.
/* Hash tables.
   Copyright (C) 2000, 2001, 2003, 2004, 2008, 2009
   Copyright (C) 2000, 2001, 2003, 2004, 2008, 2009
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
 
 
This program is free software; you can redistribute it and/or modify it
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
Free Software Foundation; either version 3, or (at your option) any
later version.
later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING3.  If not see
along with this program; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.
<http://www.gnu.org/licenses/>.
 
 
 In other words, you are welcome to use, share and improve this program.
 In other words, you are welcome to use, share and improve this program.
 You are forbidden to forbid anyone else to use, share and improve
 You are forbidden to forbid anyone else to use, share and improve
 what you give them.   Help stamp out software-hoarding!  */
 what you give them.   Help stamp out software-hoarding!  */
 
 
#include "config.h"
#include "config.h"
#include "system.h"
#include "system.h"
#include "symtab.h"
#include "symtab.h"
 
 
/* The code below is a specialization of Vladimir Makarov's expandable
/* The code below is a specialization of Vladimir Makarov's expandable
   hash tables (see libiberty/hashtab.c).  The abstraction penalty was
   hash tables (see libiberty/hashtab.c).  The abstraction penalty was
   too high to continue using the generic form.  This code knows
   too high to continue using the generic form.  This code knows
   intrinsically how to calculate a hash value, and how to compare an
   intrinsically how to calculate a hash value, and how to compare an
   existing entry with a potential new one.  */
   existing entry with a potential new one.  */
 
 
static unsigned int calc_hash (const unsigned char *, size_t);
static unsigned int calc_hash (const unsigned char *, size_t);
static void ht_expand (hash_table *);
static void ht_expand (hash_table *);
static double approx_sqrt (double);
static double approx_sqrt (double);
 
 
/* A deleted entry.  */
/* A deleted entry.  */
#define DELETED ((hashnode) -1)
#define DELETED ((hashnode) -1)
 
 
/* Calculate the hash of the string STR of length LEN.  */
/* Calculate the hash of the string STR of length LEN.  */
 
 
static unsigned int
static unsigned int
calc_hash (const unsigned char *str, size_t len)
calc_hash (const unsigned char *str, size_t len)
{
{
  size_t n = len;
  size_t n = len;
  unsigned int r = 0;
  unsigned int r = 0;
 
 
  while (n--)
  while (n--)
    r = HT_HASHSTEP (r, *str++);
    r = HT_HASHSTEP (r, *str++);
 
 
  return HT_HASHFINISH (r, len);
  return HT_HASHFINISH (r, len);
}
}
 
 
/* Initialize an identifier hashtable.  */
/* Initialize an identifier hashtable.  */
 
 
hash_table *
hash_table *
ht_create (unsigned int order)
ht_create (unsigned int order)
{
{
  unsigned int nslots = 1 << order;
  unsigned int nslots = 1 << order;
  hash_table *table;
  hash_table *table;
 
 
  table = XCNEW (hash_table);
  table = XCNEW (hash_table);
 
 
  /* Strings need no alignment.  */
  /* Strings need no alignment.  */
  _obstack_begin (&table->stack, 0, 0,
  _obstack_begin (&table->stack, 0, 0,
                  (void *(*) (long)) xmalloc,
                  (void *(*) (long)) xmalloc,
                  (void (*) (void *)) free);
                  (void (*) (void *)) free);
 
 
  obstack_alignment_mask (&table->stack) = 0;
  obstack_alignment_mask (&table->stack) = 0;
 
 
  table->entries = XCNEWVEC (hashnode, nslots);
  table->entries = XCNEWVEC (hashnode, nslots);
  table->entries_owned = true;
  table->entries_owned = true;
  table->nslots = nslots;
  table->nslots = nslots;
  return table;
  return table;
}
}
 
 
/* Frees all memory associated with a hash table.  */
/* Frees all memory associated with a hash table.  */
 
 
void
void
ht_destroy (hash_table *table)
ht_destroy (hash_table *table)
{
{
  obstack_free (&table->stack, NULL);
  obstack_free (&table->stack, NULL);
  if (table->entries_owned)
  if (table->entries_owned)
    free (table->entries);
    free (table->entries);
  free (table);
  free (table);
}
}
 
 
/* Returns the hash entry for the a STR of length LEN.  If that string
/* Returns the hash entry for the a STR of length LEN.  If that string
   already exists in the table, returns the existing entry.  If the
   already exists in the table, returns the existing entry.  If the
   identifier hasn't been seen before, and INSERT is CPP_NO_INSERT,
   identifier hasn't been seen before, and INSERT is CPP_NO_INSERT,
   returns NULL.  Otherwise insert and returns a new entry.  A new
   returns NULL.  Otherwise insert and returns a new entry.  A new
   string is allocated.  */
   string is allocated.  */
hashnode
hashnode
ht_lookup (hash_table *table, const unsigned char *str, size_t len,
ht_lookup (hash_table *table, const unsigned char *str, size_t len,
           enum ht_lookup_option insert)
           enum ht_lookup_option insert)
{
{
  return ht_lookup_with_hash (table, str, len, calc_hash (str, len),
  return ht_lookup_with_hash (table, str, len, calc_hash (str, len),
                              insert);
                              insert);
}
}
 
 
hashnode
hashnode
ht_lookup_with_hash (hash_table *table, const unsigned char *str,
ht_lookup_with_hash (hash_table *table, const unsigned char *str,
                     size_t len, unsigned int hash,
                     size_t len, unsigned int hash,
                     enum ht_lookup_option insert)
                     enum ht_lookup_option insert)
{
{
  unsigned int hash2;
  unsigned int hash2;
  unsigned int index;
  unsigned int index;
  unsigned int deleted_index = table->nslots;
  unsigned int deleted_index = table->nslots;
  size_t sizemask;
  size_t sizemask;
  hashnode node;
  hashnode node;
 
 
  sizemask = table->nslots - 1;
  sizemask = table->nslots - 1;
  index = hash & sizemask;
  index = hash & sizemask;
  table->searches++;
  table->searches++;
 
 
  node = table->entries[index];
  node = table->entries[index];
 
 
  if (node != NULL)
  if (node != NULL)
    {
    {
      if (node == DELETED)
      if (node == DELETED)
        deleted_index = index;
        deleted_index = index;
      else if (node->hash_value == hash
      else if (node->hash_value == hash
               && HT_LEN (node) == (unsigned int) len
               && HT_LEN (node) == (unsigned int) len
               && !memcmp (HT_STR (node), str, len))
               && !memcmp (HT_STR (node), str, len))
        return node;
        return node;
 
 
      /* hash2 must be odd, so we're guaranteed to visit every possible
      /* hash2 must be odd, so we're guaranteed to visit every possible
         location in the table during rehashing.  */
         location in the table during rehashing.  */
      hash2 = ((hash * 17) & sizemask) | 1;
      hash2 = ((hash * 17) & sizemask) | 1;
 
 
      for (;;)
      for (;;)
        {
        {
          table->collisions++;
          table->collisions++;
          index = (index + hash2) & sizemask;
          index = (index + hash2) & sizemask;
          node = table->entries[index];
          node = table->entries[index];
          if (node == NULL)
          if (node == NULL)
            break;
            break;
 
 
          if (node == DELETED)
          if (node == DELETED)
            {
            {
              if (deleted_index != table->nslots)
              if (deleted_index != table->nslots)
                deleted_index = index;
                deleted_index = index;
            }
            }
          else if (node->hash_value == hash
          else if (node->hash_value == hash
                   && HT_LEN (node) == (unsigned int) len
                   && HT_LEN (node) == (unsigned int) len
                   && !memcmp (HT_STR (node), str, len))
                   && !memcmp (HT_STR (node), str, len))
            return node;
            return node;
        }
        }
    }
    }
 
 
  if (insert == HT_NO_INSERT)
  if (insert == HT_NO_INSERT)
    return NULL;
    return NULL;
 
 
  /* We prefer to overwrite the first deleted slot we saw.  */
  /* We prefer to overwrite the first deleted slot we saw.  */
  if (deleted_index != table->nslots)
  if (deleted_index != table->nslots)
    index = deleted_index;
    index = deleted_index;
 
 
  node = (*table->alloc_node) (table);
  node = (*table->alloc_node) (table);
  table->entries[index] = node;
  table->entries[index] = node;
 
 
  HT_LEN (node) = (unsigned int) len;
  HT_LEN (node) = (unsigned int) len;
  node->hash_value = hash;
  node->hash_value = hash;
 
 
  if (table->alloc_subobject)
  if (table->alloc_subobject)
    {
    {
      char *chars = (char *) table->alloc_subobject (len + 1);
      char *chars = (char *) table->alloc_subobject (len + 1);
      memcpy (chars, str, len);
      memcpy (chars, str, len);
      chars[len] = '\0';
      chars[len] = '\0';
      HT_STR (node) = (const unsigned char *) chars;
      HT_STR (node) = (const unsigned char *) chars;
    }
    }
  else
  else
    HT_STR (node) = (const unsigned char *) obstack_copy0 (&table->stack,
    HT_STR (node) = (const unsigned char *) obstack_copy0 (&table->stack,
                                                           str, len);
                                                           str, len);
 
 
  if (++table->nelements * 4 >= table->nslots * 3)
  if (++table->nelements * 4 >= table->nslots * 3)
    /* Must expand the string table.  */
    /* Must expand the string table.  */
    ht_expand (table);
    ht_expand (table);
 
 
  return node;
  return node;
}
}
 
 
/* Double the size of a hash table, re-hashing existing entries.  */
/* Double the size of a hash table, re-hashing existing entries.  */
 
 
static void
static void
ht_expand (hash_table *table)
ht_expand (hash_table *table)
{
{
  hashnode *nentries, *p, *limit;
  hashnode *nentries, *p, *limit;
  unsigned int size, sizemask;
  unsigned int size, sizemask;
 
 
  size = table->nslots * 2;
  size = table->nslots * 2;
  nentries = XCNEWVEC (hashnode, size);
  nentries = XCNEWVEC (hashnode, size);
  sizemask = size - 1;
  sizemask = size - 1;
 
 
  p = table->entries;
  p = table->entries;
  limit = p + table->nslots;
  limit = p + table->nslots;
  do
  do
    if (*p && *p != DELETED)
    if (*p && *p != DELETED)
      {
      {
        unsigned int index, hash, hash2;
        unsigned int index, hash, hash2;
 
 
        hash = (*p)->hash_value;
        hash = (*p)->hash_value;
        index = hash & sizemask;
        index = hash & sizemask;
 
 
        if (nentries[index])
        if (nentries[index])
          {
          {
            hash2 = ((hash * 17) & sizemask) | 1;
            hash2 = ((hash * 17) & sizemask) | 1;
            do
            do
              {
              {
                index = (index + hash2) & sizemask;
                index = (index + hash2) & sizemask;
              }
              }
            while (nentries[index]);
            while (nentries[index]);
          }
          }
        nentries[index] = *p;
        nentries[index] = *p;
      }
      }
  while (++p < limit);
  while (++p < limit);
 
 
  if (table->entries_owned)
  if (table->entries_owned)
    free (table->entries);
    free (table->entries);
  table->entries_owned = true;
  table->entries_owned = true;
  table->entries = nentries;
  table->entries = nentries;
  table->nslots = size;
  table->nslots = size;
}
}
 
 
/* For all nodes in TABLE, callback CB with parameters TABLE->PFILE,
/* For all nodes in TABLE, callback CB with parameters TABLE->PFILE,
   the node, and V.  */
   the node, and V.  */
void
void
ht_forall (hash_table *table, ht_cb cb, const void *v)
ht_forall (hash_table *table, ht_cb cb, const void *v)
{
{
  hashnode *p, *limit;
  hashnode *p, *limit;
 
 
  p = table->entries;
  p = table->entries;
  limit = p + table->nslots;
  limit = p + table->nslots;
  do
  do
    if (*p && *p != DELETED)
    if (*p && *p != DELETED)
      {
      {
        if ((*cb) (table->pfile, *p, v) == 0)
        if ((*cb) (table->pfile, *p, v) == 0)
          break;
          break;
      }
      }
  while (++p < limit);
  while (++p < limit);
}
}
 
 
/* Like ht_forall, but a nonzero return from the callback means that
/* Like ht_forall, but a nonzero return from the callback means that
   the entry should be removed from the table.  */
   the entry should be removed from the table.  */
void
void
ht_purge (hash_table *table, ht_cb cb, const void *v)
ht_purge (hash_table *table, ht_cb cb, const void *v)
{
{
  hashnode *p, *limit;
  hashnode *p, *limit;
 
 
  p = table->entries;
  p = table->entries;
  limit = p + table->nslots;
  limit = p + table->nslots;
  do
  do
    if (*p && *p != DELETED)
    if (*p && *p != DELETED)
      {
      {
        if ((*cb) (table->pfile, *p, v))
        if ((*cb) (table->pfile, *p, v))
          *p = DELETED;
          *p = DELETED;
      }
      }
  while (++p < limit);
  while (++p < limit);
}
}
 
 
/* Restore the hash table.  */
/* Restore the hash table.  */
void
void
ht_load (hash_table *ht, hashnode *entries,
ht_load (hash_table *ht, hashnode *entries,
         unsigned int nslots, unsigned int nelements,
         unsigned int nslots, unsigned int nelements,
         bool own)
         bool own)
{
{
  if (ht->entries_owned)
  if (ht->entries_owned)
    free (ht->entries);
    free (ht->entries);
  ht->entries = entries;
  ht->entries = entries;
  ht->nslots = nslots;
  ht->nslots = nslots;
  ht->nelements = nelements;
  ht->nelements = nelements;
  ht->entries_owned = own;
  ht->entries_owned = own;
}
}
 
 
/* Dump allocation statistics to stderr.  */
/* Dump allocation statistics to stderr.  */
 
 
void
void
ht_dump_statistics (hash_table *table)
ht_dump_statistics (hash_table *table)
{
{
  size_t nelts, nids, overhead, headers;
  size_t nelts, nids, overhead, headers;
  size_t total_bytes, longest, deleted = 0;
  size_t total_bytes, longest, deleted = 0;
  double sum_of_squares, exp_len, exp_len2, exp2_len;
  double sum_of_squares, exp_len, exp_len2, exp2_len;
  hashnode *p, *limit;
  hashnode *p, *limit;
 
 
#define SCALE(x) ((unsigned long) ((x) < 1024*10 \
#define SCALE(x) ((unsigned long) ((x) < 1024*10 \
                  ? (x) \
                  ? (x) \
                  : ((x) < 1024*1024*10 \
                  : ((x) < 1024*1024*10 \
                     ? (x) / 1024 \
                     ? (x) / 1024 \
                     : (x) / (1024*1024))))
                     : (x) / (1024*1024))))
#define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
#define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
 
 
  total_bytes = longest = sum_of_squares = nids = 0;
  total_bytes = longest = sum_of_squares = nids = 0;
  p = table->entries;
  p = table->entries;
  limit = p + table->nslots;
  limit = p + table->nslots;
  do
  do
    if (*p == DELETED)
    if (*p == DELETED)
      ++deleted;
      ++deleted;
    else if (*p)
    else if (*p)
      {
      {
        size_t n = HT_LEN (*p);
        size_t n = HT_LEN (*p);
 
 
        total_bytes += n;
        total_bytes += n;
        sum_of_squares += (double) n * n;
        sum_of_squares += (double) n * n;
        if (n > longest)
        if (n > longest)
          longest = n;
          longest = n;
        nids++;
        nids++;
      }
      }
  while (++p < limit);
  while (++p < limit);
 
 
  nelts = table->nelements;
  nelts = table->nelements;
  overhead = obstack_memory_used (&table->stack) - total_bytes;
  overhead = obstack_memory_used (&table->stack) - total_bytes;
  headers = table->nslots * sizeof (hashnode);
  headers = table->nslots * sizeof (hashnode);
 
 
  fprintf (stderr, "\nString pool\nentries\t\t%lu\n",
  fprintf (stderr, "\nString pool\nentries\t\t%lu\n",
           (unsigned long) nelts);
           (unsigned long) nelts);
  fprintf (stderr, "identifiers\t%lu (%.2f%%)\n",
  fprintf (stderr, "identifiers\t%lu (%.2f%%)\n",
           (unsigned long) nids, nids * 100.0 / nelts);
           (unsigned long) nids, nids * 100.0 / nelts);
  fprintf (stderr, "slots\t\t%lu\n",
  fprintf (stderr, "slots\t\t%lu\n",
           (unsigned long) table->nslots);
           (unsigned long) table->nslots);
  fprintf (stderr, "deleted\t\t%lu\n",
  fprintf (stderr, "deleted\t\t%lu\n",
           (unsigned long) deleted);
           (unsigned long) deleted);
  fprintf (stderr, "bytes\t\t%lu%c (%lu%c overhead)\n",
  fprintf (stderr, "bytes\t\t%lu%c (%lu%c overhead)\n",
           SCALE (total_bytes), LABEL (total_bytes),
           SCALE (total_bytes), LABEL (total_bytes),
           SCALE (overhead), LABEL (overhead));
           SCALE (overhead), LABEL (overhead));
  fprintf (stderr, "table size\t%lu%c\n",
  fprintf (stderr, "table size\t%lu%c\n",
           SCALE (headers), LABEL (headers));
           SCALE (headers), LABEL (headers));
 
 
  exp_len = (double)total_bytes / (double)nelts;
  exp_len = (double)total_bytes / (double)nelts;
  exp2_len = exp_len * exp_len;
  exp2_len = exp_len * exp_len;
  exp_len2 = (double) sum_of_squares / (double) nelts;
  exp_len2 = (double) sum_of_squares / (double) nelts;
 
 
  fprintf (stderr, "coll/search\t%.4f\n",
  fprintf (stderr, "coll/search\t%.4f\n",
           (double) table->collisions / (double) table->searches);
           (double) table->collisions / (double) table->searches);
  fprintf (stderr, "ins/search\t%.4f\n",
  fprintf (stderr, "ins/search\t%.4f\n",
           (double) nelts / (double) table->searches);
           (double) nelts / (double) table->searches);
  fprintf (stderr, "avg. entry\t%.2f bytes (+/- %.2f)\n",
  fprintf (stderr, "avg. entry\t%.2f bytes (+/- %.2f)\n",
           exp_len, approx_sqrt (exp_len2 - exp2_len));
           exp_len, approx_sqrt (exp_len2 - exp2_len));
  fprintf (stderr, "longest entry\t%lu\n",
  fprintf (stderr, "longest entry\t%lu\n",
           (unsigned long) longest);
           (unsigned long) longest);
#undef SCALE
#undef SCALE
#undef LABEL
#undef LABEL
}
}
 
 
/* Return the approximate positive square root of a number N.  This is for
/* Return the approximate positive square root of a number N.  This is for
   statistical reports, not code generation.  */
   statistical reports, not code generation.  */
static double
static double
approx_sqrt (double x)
approx_sqrt (double x)
{
{
  double s, d;
  double s, d;
 
 
  if (x < 0)
  if (x < 0)
    abort ();
    abort ();
  if (x == 0)
  if (x == 0)
    return 0;
    return 0;
 
 
  s = x;
  s = x;
  do
  do
    {
    {
      d = (s * s - x) / (2 * s);
      d = (s * s - x) / (2 * s);
      s -= d;
      s -= d;
    }
    }
  while (d > .0001);
  while (d > .0001);
  return s;
  return s;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.