Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/adlc/dict2.cpp
32285 views
/*1* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324// Dictionaries - An Abstract Data Type2526#include "adlc.hpp"2728// #include "dict.hpp"293031//------------------------------data-----------------------------------------32// String hash tables33#define MAXID 2034static char initflag = 0; // True after 1st initialization35static 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};36static short xsum[MAXID];3738//------------------------------bucket---------------------------------------39class bucket {40public:41int _cnt, _max; // Size of bucket42const void **_keyvals; // Array of keys and values43};4445//------------------------------Dict-----------------------------------------46// The dictionary is kept has a hash table. The hash table is a even power47// of two, for nice modulo operations. Each bucket in the hash table points48// to a linear list of key-value pairs; each key & value is just a (void *).49// The list starts with a count. A hash lookup finds the list head, then a50// simple linear scan finds the key. If the table gets too full, it's51// doubled in size; the total amount of EXTRA times all hash functions are52// computed for the doubling is no more than the current size - thus the53// doubling in size costs no more than a constant factor in speed.54Dict::Dict(CmpKey initcmp, Hash inithash) : _hash(inithash), _cmp(initcmp), _arena(NULL) {55init();56}5758Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena) : _hash(inithash), _cmp(initcmp), _arena(arena) {59init();60}6162void Dict::init() {63int i;6465// Precompute table of null character hashes66if (!initflag) { // Not initializated yet?67xsum[0] = (short) ((1 << shft[0]) + 1); // Initialize68for( i = 1; i < MAXID; i++) {69xsum[i] = (short) ((1 << shft[i]) + 1 + xsum[i-1]);70}71initflag = 1; // Never again72}7374_size = 16; // Size is a power of 275_cnt = 0; // Dictionary is empty76_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket) * _size);77memset(_bin, 0, sizeof(bucket) * _size);78}7980//------------------------------~Dict------------------------------------------81// Delete an existing dictionary.82Dict::~Dict() {83}8485//------------------------------Clear----------------------------------------86// Zap to empty; ready for re-use87void Dict::Clear() {88_cnt = 0; // Empty contents89for( int i=0; i<_size; i++ )90_bin[i]._cnt = 0; // Empty buckets, but leave allocated91// Leave _size & _bin alone, under the assumption that dictionary will92// grow to this size again.93}9495//------------------------------doubhash---------------------------------------96// Double hash table size. If can't do so, just suffer. If can, then run97// thru old hash table, moving things to new table. Note that since hash98// table doubled, exactly 1 new bit is exposed in the mask - so everything99// in the old table ends up on 1 of two lists in the new table; a hi and a100// lo list depending on the value of the bit.101void Dict::doubhash(void) {102int oldsize = _size;103_size <<= 1; // Double in size104_bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*oldsize, sizeof(bucket)*_size );105memset( &_bin[oldsize], 0, oldsize*sizeof(bucket) );106// Rehash things to spread into new table107for( int i=0; i < oldsize; i++) { // For complete OLD table do108bucket *b = &_bin[i]; // Handy shortcut for _bin[i]109if( !b->_keyvals ) continue; // Skip empties fast110111bucket *nb = &_bin[i+oldsize]; // New bucket shortcut112int j = b->_max; // Trim new bucket to nearest power of 2113while( j > b->_cnt ) j >>= 1; // above old bucket _cnt114if( !j ) j = 1; // Handle zero-sized buckets115nb->_max = j<<1;116// Allocate worst case space for key-value pairs117nb->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*nb->_max*2 );118int nbcnt = 0;119120for( j=0; j<b->_cnt; j++ ) { // Rehash all keys in this bucket121const void *key = b->_keyvals[j+j];122if( (_hash( key ) & (_size-1)) != i ) { // Moving to hi bucket?123nb->_keyvals[nbcnt+nbcnt] = key;124nb->_keyvals[nbcnt+nbcnt+1] = b->_keyvals[j+j+1];125nb->_cnt = nbcnt = nbcnt+1;126b->_cnt--; // Remove key/value from lo bucket127b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];128b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];129j--; // Hash compacted element also130}131} // End of for all key-value pairs in bucket132} // End of for all buckets133134135}136137//------------------------------Dict-----------------------------------------138// Deep copy a dictionary.139Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) {140_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);141memcpy( _bin, d._bin, sizeof(bucket)*_size );142for( int i=0; i<_size; i++ ) {143if( !_bin[i]._keyvals ) continue;144_bin[i]._keyvals=(const void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2);145memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*));146}147}148149//------------------------------Dict-----------------------------------------150// Deep copy a dictionary.151Dict &Dict::operator =( const Dict &d ) {152if( _size < d._size ) { // If must have more buckets153_arena = d._arena;154_bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );155memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) );156_size = d._size;157}158for( int i=0; i<_size; i++ ) // All buckets are empty159_bin[i]._cnt = 0; // But leave bucket allocations alone160_cnt = d._cnt;161*(Hash*)(&_hash) = d._hash;162*(CmpKey*)(&_cmp) = d._cmp;163for(int k=0; k<_size; k++ ) {164bucket *b = &d._bin[k]; // Shortcut to source bucket165for( int j=0; j<b->_cnt; j++ )166Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] );167}168return *this;169}170171//------------------------------Insert---------------------------------------172// Insert or replace a key/value pair in the given dictionary. If the173// dictionary is too full, it's size is doubled. The prior value being174// replaced is returned (NULL if this is a 1st insertion of that key). If175// an old value is found, it's swapped with the prior key-value pair on the176// list. This moves a commonly searched-for value towards the list head.177const void *Dict::Insert(const void *key, const void *val) {178int hash = _hash( key ); // Get hash key179int i = hash & (_size-1); // Get hash key, corrected for size180bucket *b = &_bin[i]; // Handy shortcut181for( int j=0; j<b->_cnt; j++ )182if( !_cmp(key,b->_keyvals[j+j]) ) {183const void *prior = b->_keyvals[j+j+1];184b->_keyvals[j+j ] = key; // Insert current key-value185b->_keyvals[j+j+1] = val;186return prior; // Return prior187}188189if( ++_cnt > _size ) { // Hash table is full190doubhash(); // Grow whole table if too full191i = hash & (_size-1); // Rehash192b = &_bin[i]; // Handy shortcut193}194if( b->_cnt == b->_max ) { // Must grow bucket?195if( !b->_keyvals ) {196b->_max = 2; // Initial bucket size197b->_keyvals = (const void**)_arena->Amalloc_4( sizeof(void *)*b->_max*2 );198} else {199b->_keyvals = (const void**)_arena->Arealloc( b->_keyvals, sizeof(void *)*b->_max*2, sizeof(void *)*b->_max*4 );200b->_max <<= 1; // Double bucket201}202}203b->_keyvals[b->_cnt+b->_cnt ] = key;204b->_keyvals[b->_cnt+b->_cnt+1] = val;205b->_cnt++;206return NULL; // Nothing found prior207}208209//------------------------------Delete---------------------------------------210// Find & remove a value from dictionary. Return old value.211const void *Dict::Delete(void *key) {212int i = _hash( key ) & (_size-1); // Get hash key, corrected for size213bucket *b = &_bin[i]; // Handy shortcut214for( int j=0; j<b->_cnt; j++ )215if( !_cmp(key,b->_keyvals[j+j]) ) {216const void *prior = b->_keyvals[j+j+1];217b->_cnt--; // Remove key/value from lo bucket218b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];219b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];220_cnt--; // One less thing in table221return prior;222}223return NULL;224}225226//------------------------------FindDict-------------------------------------227// Find a key-value pair in the given dictionary. If not found, return NULL.228// If found, move key-value pair towards head of list.229const void *Dict::operator [](const void *key) const {230int i = _hash( key ) & (_size-1); // Get hash key, corrected for size231bucket *b = &_bin[i]; // Handy shortcut232for( int j=0; j<b->_cnt; j++ )233if( !_cmp(key,b->_keyvals[j+j]) )234return b->_keyvals[j+j+1];235return NULL;236}237238//------------------------------CmpDict--------------------------------------239// CmpDict compares two dictionaries; they must have the same keys (their240// keys must match using CmpKey) and they must have the same values (pointer241// comparison). If so 1 is returned, if not 0 is returned.242int Dict::operator ==(const Dict &d2) const {243if( _cnt != d2._cnt ) return 0;244if( _hash != d2._hash ) return 0;245if( _cmp != d2._cmp ) return 0;246for( int i=0; i < _size; i++) { // For complete hash table do247bucket *b = &_bin[i]; // Handy shortcut248if( b->_cnt != d2._bin[i]._cnt ) return 0;249if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) )250return 0; // Key-value pairs must match251}252return 1; // All match, is OK253}254255256//------------------------------print----------------------------------------257static void printvoid(const void* x) { printf("%p", x); }258void Dict::print() {259print(printvoid, printvoid);260}261void Dict::print(PrintKeyOrValue print_key, PrintKeyOrValue print_value) {262for( int i=0; i < _size; i++) { // For complete hash table do263bucket *b = &_bin[i]; // Handy shortcut264for( int j=0; j<b->_cnt; j++ ) {265print_key( b->_keyvals[j+j ]);266printf(" -> ");267print_value(b->_keyvals[j+j+1]);268printf("\n");269}270}271}272273//------------------------------Hashing Functions----------------------------274// Convert string to hash key. This algorithm implements a universal hash275// function with the multipliers frozen (ok, so it's not universal). The276// multipliers (and allowable characters) are all odd, so the resultant sum277// is odd - guaranteed not divisible by any power of two, so the hash tables278// can be any power of two with good results. Also, I choose multipliers279// that have only 2 bits set (the low is always set to be odd) so280// multiplication requires only shifts and adds. Characters are required to281// be in the range 0-127 (I double & add 1 to force oddness). Keys are282// limited to MAXID characters in length. Experimental evidence on 150K of283// C text shows excellent spreading of values for any size hash table.284int hashstr(const void *t) {285register char c, k = 0;286register int sum = 0;287register const char *s = (const char *)t;288289while (((c = s[k]) != '\0') && (k < MAXID-1)) { // Get characters till nul290c = (char) ((c << 1) + 1); // Characters are always odd!291sum += c + (c << shft[k++]); // Universal hash function292}293assert(k < (MAXID), "Exceeded maximum name length");294return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size295}296297//------------------------------hashptr--------------------------------------298// Slimey cheap hash function; no guaranteed performance. Better than the299// default for pointers, especially on MS-DOS machines.300int hashptr(const void *key) {301#ifdef __TURBOC__302return (int)((intptr_t)key >> 16);303#else // __TURBOC__304return (int)((intptr_t)key >> 2);305#endif306}307308// Slimey cheap hash function; no guaranteed performance.309int hashkey(const void *key) {310return (int)((intptr_t)key);311}312313//------------------------------Key Comparator Functions---------------------314int cmpstr(const void *k1, const void *k2) {315return strcmp((const char *)k1,(const char *)k2);316}317318// Cheap key comparator.319int cmpkey(const void *key1, const void *key2) {320if (key1 == key2) return 0;321intptr_t delta = (intptr_t)key1 - (intptr_t)key2;322if (delta > 0) return 1;323return -1;324}325326//=============================================================================327//------------------------------reset------------------------------------------328// Create an iterator and initialize the first variables.329void DictI::reset( const Dict *dict ) {330_d = dict; // The dictionary331_i = (int)-1; // Before the first bin332_j = 0; // Nothing left in the current bin333++(*this); // Step to first real value334}335336//------------------------------next-------------------------------------------337// Find the next key-value pair in the dictionary, or return a NULL key and338// value.339void DictI::operator ++(void) {340if( _j-- ) { // Still working in current bin?341_key = _d->_bin[_i]._keyvals[_j+_j];342_value = _d->_bin[_i]._keyvals[_j+_j+1];343return;344}345346while( ++_i < _d->_size ) { // Else scan for non-zero bucket347_j = _d->_bin[_i]._cnt;348if( !_j ) continue;349_j--;350_key = _d->_bin[_i]._keyvals[_j+_j];351_value = _d->_bin[_i]._keyvals[_j+_j+1];352return;353}354_key = _value = NULL;355}356357358