src/share/vm/adlc/dict2.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 // Dictionaries - An Abstract Data Type
aoqi@0 26
aoqi@0 27 #include "adlc.hpp"
aoqi@0 28
aoqi@0 29 // #include "dict.hpp"
aoqi@0 30
aoqi@0 31
aoqi@0 32 //------------------------------data-----------------------------------------
aoqi@0 33 // String hash tables
aoqi@0 34 #define MAXID 20
aoqi@0 35 static char initflag = 0; // True after 1st initialization
aoqi@0 36 static char shft[MAXID + 1] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7};
aoqi@0 37 static short xsum[MAXID];
aoqi@0 38
aoqi@0 39 //------------------------------bucket---------------------------------------
aoqi@0 40 class bucket {
aoqi@0 41 public:
aoqi@0 42 int _cnt, _max; // Size of bucket
aoqi@0 43 const void **_keyvals; // Array of keys and values
aoqi@0 44 };
aoqi@0 45
aoqi@0 46 //------------------------------Dict-----------------------------------------
aoqi@0 47 // The dictionary is kept has a hash table. The hash table is a even power
aoqi@0 48 // of two, for nice modulo operations. Each bucket in the hash table points
aoqi@0 49 // to a linear list of key-value pairs; each key & value is just a (void *).
aoqi@0 50 // The list starts with a count. A hash lookup finds the list head, then a
aoqi@0 51 // simple linear scan finds the key. If the table gets too full, it's
aoqi@0 52 // doubled in size; the total amount of EXTRA times all hash functions are
aoqi@0 53 // computed for the doubling is no more than the current size - thus the
aoqi@0 54 // doubling in size costs no more than a constant factor in speed.
aoqi@0 55 Dict::Dict(CmpKey initcmp, Hash inithash) : _hash(inithash), _cmp(initcmp), _arena(NULL) {
aoqi@0 56 init();
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena) : _hash(inithash), _cmp(initcmp), _arena(arena) {
aoqi@0 60 init();
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 void Dict::init() {
aoqi@0 64 int i;
aoqi@0 65
aoqi@0 66 // Precompute table of null character hashes
aoqi@0 67 if (!initflag) { // Not initializated yet?
aoqi@0 68 xsum[0] = (short) ((1 << shft[0]) + 1); // Initialize
aoqi@0 69 for( i = 1; i < MAXID; i++) {
aoqi@0 70 xsum[i] = (short) ((1 << shft[i]) + 1 + xsum[i-1]);
aoqi@0 71 }
aoqi@0 72 initflag = 1; // Never again
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 _size = 16; // Size is a power of 2
aoqi@0 76 _cnt = 0; // Dictionary is empty
aoqi@0 77 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket) * _size);
aoqi@0 78 memset(_bin, 0, sizeof(bucket) * _size);
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 //------------------------------~Dict------------------------------------------
aoqi@0 82 // Delete an existing dictionary.
aoqi@0 83 Dict::~Dict() {
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 //------------------------------Clear----------------------------------------
aoqi@0 87 // Zap to empty; ready for re-use
aoqi@0 88 void Dict::Clear() {
aoqi@0 89 _cnt = 0; // Empty contents
aoqi@0 90 for( int i=0; i<_size; i++ )
aoqi@0 91 _bin[i]._cnt = 0; // Empty buckets, but leave allocated
aoqi@0 92 // Leave _size & _bin alone, under the assumption that dictionary will
aoqi@0 93 // grow to this size again.
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 //------------------------------doubhash---------------------------------------
aoqi@0 97 // Double hash table size. If can't do so, just suffer. If can, then run
aoqi@0 98 // thru old hash table, moving things to new table. Note that since hash
aoqi@0 99 // table doubled, exactly 1 new bit is exposed in the mask - so everything
aoqi@0 100 // in the old table ends up on 1 of two lists in the new table; a hi and a
aoqi@0 101 // lo list depending on the value of the bit.
aoqi@0 102 void Dict::doubhash(void) {
aoqi@0 103 int oldsize = _size;
aoqi@0 104 _size <<= 1; // Double in size
aoqi@0 105 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*oldsize, sizeof(bucket)*_size );
aoqi@0 106 memset( &_bin[oldsize], 0, oldsize*sizeof(bucket) );
aoqi@0 107 // Rehash things to spread into new table
aoqi@0 108 for( int i=0; i < oldsize; i++) { // For complete OLD table do
aoqi@0 109 bucket *b = &_bin[i]; // Handy shortcut for _bin[i]
aoqi@0 110 if( !b->_keyvals ) continue; // Skip empties fast
aoqi@0 111
aoqi@0 112 bucket *nb = &_bin[i+oldsize]; // New bucket shortcut
aoqi@0 113 int j = b->_max; // Trim new bucket to nearest power of 2
aoqi@0 114 while( j > b->_cnt ) j >>= 1; // above old bucket _cnt
aoqi@0 115 if( !j ) j = 1; // Handle zero-sized buckets
aoqi@0 116 nb->_max = j<<1;
aoqi@0 117 // Allocate worst case space for key-value pairs
aoqi@0 118 nb->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*nb->_max*2 );
aoqi@0 119 int nbcnt = 0;
aoqi@0 120
aoqi@0 121 for( j=0; j<b->_cnt; j++ ) { // Rehash all keys in this bucket
aoqi@0 122 const void *key = b->_keyvals[j+j];
aoqi@0 123 if( (_hash( key ) & (_size-1)) != i ) { // Moving to hi bucket?
aoqi@0 124 nb->_keyvals[nbcnt+nbcnt] = key;
aoqi@0 125 nb->_keyvals[nbcnt+nbcnt+1] = b->_keyvals[j+j+1];
aoqi@0 126 nb->_cnt = nbcnt = nbcnt+1;
aoqi@0 127 b->_cnt--; // Remove key/value from lo bucket
aoqi@0 128 b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];
aoqi@0 129 b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
aoqi@0 130 j--; // Hash compacted element also
aoqi@0 131 }
aoqi@0 132 } // End of for all key-value pairs in bucket
aoqi@0 133 } // End of for all buckets
aoqi@0 134
aoqi@0 135
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 //------------------------------Dict-----------------------------------------
aoqi@0 139 // Deep copy a dictionary.
aoqi@0 140 Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) {
aoqi@0 141 _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
aoqi@0 142 memcpy( _bin, d._bin, sizeof(bucket)*_size );
aoqi@0 143 for( int i=0; i<_size; i++ ) {
aoqi@0 144 if( !_bin[i]._keyvals ) continue;
aoqi@0 145 _bin[i]._keyvals=(const void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2);
aoqi@0 146 memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*));
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 //------------------------------Dict-----------------------------------------
aoqi@0 151 // Deep copy a dictionary.
aoqi@0 152 Dict &Dict::operator =( const Dict &d ) {
aoqi@0 153 if( _size < d._size ) { // If must have more buckets
aoqi@0 154 _arena = d._arena;
aoqi@0 155 _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );
aoqi@0 156 memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) );
aoqi@0 157 _size = d._size;
aoqi@0 158 }
aoqi@0 159 for( int i=0; i<_size; i++ ) // All buckets are empty
aoqi@0 160 _bin[i]._cnt = 0; // But leave bucket allocations alone
aoqi@0 161 _cnt = d._cnt;
aoqi@0 162 *(Hash*)(&_hash) = d._hash;
aoqi@0 163 *(CmpKey*)(&_cmp) = d._cmp;
aoqi@0 164 for(int k=0; k<_size; k++ ) {
aoqi@0 165 bucket *b = &d._bin[k]; // Shortcut to source bucket
aoqi@0 166 for( int j=0; j<b->_cnt; j++ )
aoqi@0 167 Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] );
aoqi@0 168 }
aoqi@0 169 return *this;
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 //------------------------------Insert---------------------------------------
aoqi@0 173 // Insert or replace a key/value pair in the given dictionary. If the
aoqi@0 174 // dictionary is too full, it's size is doubled. The prior value being
aoqi@0 175 // replaced is returned (NULL if this is a 1st insertion of that key). If
aoqi@0 176 // an old value is found, it's swapped with the prior key-value pair on the
aoqi@0 177 // list. This moves a commonly searched-for value towards the list head.
aoqi@0 178 const void *Dict::Insert(const void *key, const void *val) {
aoqi@0 179 int hash = _hash( key ); // Get hash key
aoqi@0 180 int i = hash & (_size-1); // Get hash key, corrected for size
aoqi@0 181 bucket *b = &_bin[i]; // Handy shortcut
aoqi@0 182 for( int j=0; j<b->_cnt; j++ )
aoqi@0 183 if( !_cmp(key,b->_keyvals[j+j]) ) {
aoqi@0 184 const void *prior = b->_keyvals[j+j+1];
aoqi@0 185 b->_keyvals[j+j ] = key; // Insert current key-value
aoqi@0 186 b->_keyvals[j+j+1] = val;
aoqi@0 187 return prior; // Return prior
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 if( ++_cnt > _size ) { // Hash table is full
aoqi@0 191 doubhash(); // Grow whole table if too full
aoqi@0 192 i = hash & (_size-1); // Rehash
aoqi@0 193 b = &_bin[i]; // Handy shortcut
aoqi@0 194 }
aoqi@0 195 if( b->_cnt == b->_max ) { // Must grow bucket?
aoqi@0 196 if( !b->_keyvals ) {
aoqi@0 197 b->_max = 2; // Initial bucket size
aoqi@0 198 b->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*b->_max*2 );
aoqi@0 199 } else {
aoqi@0 200 b->_keyvals = (const void**)_arena->Arealloc( b->_keyvals, sizeof(void *)*b->_max*2, sizeof(void *)*b->_max*4 );
aoqi@0 201 b->_max <<= 1; // Double bucket
aoqi@0 202 }
aoqi@0 203 }
aoqi@0 204 b->_keyvals[b->_cnt+b->_cnt ] = key;
aoqi@0 205 b->_keyvals[b->_cnt+b->_cnt+1] = val;
aoqi@0 206 b->_cnt++;
aoqi@0 207 return NULL; // Nothing found prior
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 //------------------------------Delete---------------------------------------
aoqi@0 211 // Find & remove a value from dictionary. Return old value.
aoqi@0 212 const void *Dict::Delete(void *key) {
aoqi@0 213 int i = _hash( key ) & (_size-1); // Get hash key, corrected for size
aoqi@0 214 bucket *b = &_bin[i]; // Handy shortcut
aoqi@0 215 for( int j=0; j<b->_cnt; j++ )
aoqi@0 216 if( !_cmp(key,b->_keyvals[j+j]) ) {
aoqi@0 217 const void *prior = b->_keyvals[j+j+1];
aoqi@0 218 b->_cnt--; // Remove key/value from lo bucket
aoqi@0 219 b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];
aoqi@0 220 b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
aoqi@0 221 _cnt--; // One less thing in table
aoqi@0 222 return prior;
aoqi@0 223 }
aoqi@0 224 return NULL;
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 //------------------------------FindDict-------------------------------------
aoqi@0 228 // Find a key-value pair in the given dictionary. If not found, return NULL.
aoqi@0 229 // If found, move key-value pair towards head of list.
aoqi@0 230 const void *Dict::operator [](const void *key) const {
aoqi@0 231 int i = _hash( key ) & (_size-1); // Get hash key, corrected for size
aoqi@0 232 bucket *b = &_bin[i]; // Handy shortcut
aoqi@0 233 for( int j=0; j<b->_cnt; j++ )
aoqi@0 234 if( !_cmp(key,b->_keyvals[j+j]) )
aoqi@0 235 return b->_keyvals[j+j+1];
aoqi@0 236 return NULL;
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 //------------------------------CmpDict--------------------------------------
aoqi@0 240 // CmpDict compares two dictionaries; they must have the same keys (their
aoqi@0 241 // keys must match using CmpKey) and they must have the same values (pointer
aoqi@0 242 // comparison). If so 1 is returned, if not 0 is returned.
aoqi@0 243 int Dict::operator ==(const Dict &d2) const {
aoqi@0 244 if( _cnt != d2._cnt ) return 0;
aoqi@0 245 if( _hash != d2._hash ) return 0;
aoqi@0 246 if( _cmp != d2._cmp ) return 0;
aoqi@0 247 for( int i=0; i < _size; i++) { // For complete hash table do
aoqi@0 248 bucket *b = &_bin[i]; // Handy shortcut
aoqi@0 249 if( b->_cnt != d2._bin[i]._cnt ) return 0;
aoqi@0 250 if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) )
aoqi@0 251 return 0; // Key-value pairs must match
aoqi@0 252 }
aoqi@0 253 return 1; // All match, is OK
aoqi@0 254 }
aoqi@0 255
aoqi@0 256
aoqi@0 257 //------------------------------print----------------------------------------
aoqi@0 258 static void printvoid(const void* x) { printf("%p", x); }
aoqi@0 259 void Dict::print() {
aoqi@0 260 print(printvoid, printvoid);
aoqi@0 261 }
aoqi@0 262 void Dict::print(PrintKeyOrValue print_key, PrintKeyOrValue print_value) {
aoqi@0 263 for( int i=0; i < _size; i++) { // For complete hash table do
aoqi@0 264 bucket *b = &_bin[i]; // Handy shortcut
aoqi@0 265 for( int j=0; j<b->_cnt; j++ ) {
aoqi@0 266 print_key( b->_keyvals[j+j ]);
aoqi@0 267 printf(" -> ");
aoqi@0 268 print_value(b->_keyvals[j+j+1]);
aoqi@0 269 printf("\n");
aoqi@0 270 }
aoqi@0 271 }
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 //------------------------------Hashing Functions----------------------------
aoqi@0 275 // Convert string to hash key. This algorithm implements a universal hash
aoqi@0 276 // function with the multipliers frozen (ok, so it's not universal). The
aoqi@0 277 // multipliers (and allowable characters) are all odd, so the resultant sum
aoqi@0 278 // is odd - guaranteed not divisible by any power of two, so the hash tables
aoqi@0 279 // can be any power of two with good results. Also, I choose multipliers
aoqi@0 280 // that have only 2 bits set (the low is always set to be odd) so
aoqi@0 281 // multiplication requires only shifts and adds. Characters are required to
aoqi@0 282 // be in the range 0-127 (I double & add 1 to force oddness). Keys are
aoqi@0 283 // limited to MAXID characters in length. Experimental evidence on 150K of
aoqi@0 284 // C text shows excellent spreading of values for any size hash table.
aoqi@0 285 int hashstr(const void *t) {
aoqi@0 286 register char c, k = 0;
aoqi@0 287 register int sum = 0;
aoqi@0 288 register const char *s = (const char *)t;
aoqi@0 289
aoqi@0 290 while (((c = s[k]) != '\0') && (k < MAXID-1)) { // Get characters till nul
aoqi@0 291 c = (char) ((c << 1) + 1); // Characters are always odd!
aoqi@0 292 sum += c + (c << shft[k++]); // Universal hash function
aoqi@0 293 }
aoqi@0 294 assert(k < (MAXID), "Exceeded maximum name length");
aoqi@0 295 return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size
aoqi@0 296 }
aoqi@0 297
aoqi@0 298 //------------------------------hashptr--------------------------------------
aoqi@0 299 // Slimey cheap hash function; no guaranteed performance. Better than the
aoqi@0 300 // default for pointers, especially on MS-DOS machines.
aoqi@0 301 int hashptr(const void *key) {
aoqi@0 302 #ifdef __TURBOC__
aoqi@0 303 return (int)((intptr_t)key >> 16);
aoqi@0 304 #else // __TURBOC__
aoqi@0 305 return (int)((intptr_t)key >> 2);
aoqi@0 306 #endif
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 // Slimey cheap hash function; no guaranteed performance.
aoqi@0 310 int hashkey(const void *key) {
aoqi@0 311 return (int)((intptr_t)key);
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 //------------------------------Key Comparator Functions---------------------
aoqi@0 315 int cmpstr(const void *k1, const void *k2) {
aoqi@0 316 return strcmp((const char *)k1,(const char *)k2);
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 // Cheap key comparator.
aoqi@0 320 int cmpkey(const void *key1, const void *key2) {
aoqi@0 321 if (key1 == key2) return 0;
aoqi@0 322 intptr_t delta = (intptr_t)key1 - (intptr_t)key2;
aoqi@0 323 if (delta > 0) return 1;
aoqi@0 324 return -1;
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 //=============================================================================
aoqi@0 328 //------------------------------reset------------------------------------------
aoqi@0 329 // Create an iterator and initialize the first variables.
aoqi@0 330 void DictI::reset( const Dict *dict ) {
aoqi@0 331 _d = dict; // The dictionary
aoqi@0 332 _i = (int)-1; // Before the first bin
aoqi@0 333 _j = 0; // Nothing left in the current bin
aoqi@0 334 ++(*this); // Step to first real value
aoqi@0 335 }
aoqi@0 336
aoqi@0 337 //------------------------------next-------------------------------------------
aoqi@0 338 // Find the next key-value pair in the dictionary, or return a NULL key and
aoqi@0 339 // value.
aoqi@0 340 void DictI::operator ++(void) {
aoqi@0 341 if( _j-- ) { // Still working in current bin?
aoqi@0 342 _key = _d->_bin[_i]._keyvals[_j+_j];
aoqi@0 343 _value = _d->_bin[_i]._keyvals[_j+_j+1];
aoqi@0 344 return;
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 while( ++_i < _d->_size ) { // Else scan for non-zero bucket
aoqi@0 348 _j = _d->_bin[_i]._cnt;
aoqi@0 349 if( !_j ) continue;
aoqi@0 350 _j--;
aoqi@0 351 _key = _d->_bin[_i]._keyvals[_j+_j];
aoqi@0 352 _value = _d->_bin[_i]._keyvals[_j+_j+1];
aoqi@0 353 return;
aoqi@0 354 }
aoqi@0 355 _key = _value = NULL;
aoqi@0 356 }

mercurial