daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | /****************************************************************************\ |
| 2 | Copyright (c) 2002, NVIDIA Corporation. |
| 3 | |
| 4 | NVIDIA Corporation("NVIDIA") supplies this software to you in |
| 5 | consideration of your agreement to the following terms, and your use, |
| 6 | installation, modification or redistribution of this NVIDIA software |
| 7 | constitutes acceptance of these terms. If you do not agree with these |
| 8 | terms, please do not use, install, modify or redistribute this NVIDIA |
| 9 | software. |
| 10 | |
| 11 | In consideration of your agreement to abide by the following terms, and |
| 12 | subject to these terms, NVIDIA grants you a personal, non-exclusive |
| 13 | license, under NVIDIA's copyrights in this original NVIDIA software (the |
| 14 | "NVIDIA Software"), to use, reproduce, modify and redistribute the |
| 15 | NVIDIA Software, with or without modifications, in source and/or binary |
| 16 | forms; provided that if you redistribute the NVIDIA Software, you must |
| 17 | retain the copyright notice of NVIDIA, this notice and the following |
| 18 | text and disclaimers in all such redistributions of the NVIDIA Software. |
| 19 | Neither the name, trademarks, service marks nor logos of NVIDIA |
| 20 | Corporation may be used to endorse or promote products derived from the |
| 21 | NVIDIA Software without specific prior written permission from NVIDIA. |
| 22 | Except as expressly stated in this notice, no other rights or licenses |
| 23 | express or implied, are granted by NVIDIA herein, including but not |
| 24 | limited to any patent rights that may be infringed by your derivative |
| 25 | works or by other works in which the NVIDIA Software may be |
| 26 | incorporated. No hardware is licensed hereunder. |
| 27 | |
| 28 | THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT |
| 29 | WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, |
| 30 | INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE, |
| 31 | NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR |
| 32 | ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER |
| 33 | PRODUCTS. |
| 34 | |
| 35 | IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, |
| 36 | INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 37 | TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 38 | USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY |
| 39 | OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE |
| 40 | NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, |
| 41 | TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF |
| 42 | NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 | \****************************************************************************/ |
| 44 | |
| 45 | // |
| 46 | // atom.c |
| 47 | // |
| 48 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 49 | #include <stdlib.h> |
| 50 | #include <stdio.h> |
| 51 | #include <string.h> |
| 52 | |
alokp@chromium.org | 91b7232 | 2010-06-02 15:50:56 +0000 | [diff] [blame] | 53 | #include "compiler/debug.h" |
daniel@transgaming.com | e684229 | 2010-04-20 18:52:50 +0000 | [diff] [blame] | 54 | #include "compiler/preprocessor/slglobals.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 55 | |
| 56 | #undef malloc |
| 57 | #undef realloc |
| 58 | #undef free |
| 59 | |
| 60 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 61 | ////////////////////////////////////////// String table: ////////////////////////////////////// |
| 62 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 63 | |
| 64 | static const struct { |
| 65 | int val; |
| 66 | const char *str; |
| 67 | } tokens[] = { |
| 68 | { CPP_AND_OP, "&&" }, |
| 69 | { CPP_AND_ASSIGN, "&=" }, |
| 70 | { CPP_SUB_ASSIGN, "-=" }, |
| 71 | { CPP_MOD_ASSIGN, "%=" }, |
| 72 | { CPP_ADD_ASSIGN, "+=" }, |
| 73 | { CPP_DIV_ASSIGN, "/=" }, |
| 74 | { CPP_MUL_ASSIGN, "*=" }, |
| 75 | { CPP_RIGHT_BRACKET, ":>" }, |
| 76 | { CPP_EQ_OP, "==" }, |
| 77 | { CPP_XOR_OP, "^^" }, |
| 78 | { CPP_XOR_ASSIGN, "^=" }, |
| 79 | { CPP_FLOATCONSTANT, "<float-const>" }, |
| 80 | { CPP_GE_OP, ">=" }, |
| 81 | { CPP_RIGHT_OP, ">>" }, |
| 82 | { CPP_RIGHT_ASSIGN, ">>=" }, |
| 83 | { CPP_IDENTIFIER, "<ident>" }, |
| 84 | { CPP_INTCONSTANT, "<int-const>" }, |
| 85 | { CPP_LE_OP, "<=" }, |
| 86 | { CPP_LEFT_OP, "<<" }, |
| 87 | { CPP_LEFT_ASSIGN, "<<=" }, |
| 88 | { CPP_LEFT_BRACKET, "<:" }, |
| 89 | { CPP_LEFT_BRACE, "<%" }, |
| 90 | { CPP_DEC_OP, "--" }, |
| 91 | { CPP_RIGHT_BRACE, "%>" }, |
| 92 | { CPP_NE_OP, "!=" }, |
| 93 | { CPP_OR_OP, "||" }, |
| 94 | { CPP_OR_ASSIGN, "|=" }, |
| 95 | { CPP_INC_OP, "++" }, |
| 96 | { CPP_STRCONSTANT, "<string-const>" }, |
| 97 | { CPP_TYPEIDENTIFIER, "<type-ident>" }, |
| 98 | }; |
| 99 | |
| 100 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 101 | ////////////////////////////////////////// String table: ////////////////////////////////////// |
| 102 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | #define INIT_STRING_TABLE_SIZE 16384 |
| 105 | |
| 106 | typedef struct StringTable_Rec { |
| 107 | char *strings; |
| 108 | int nextFree; |
| 109 | int size; |
| 110 | } StringTable; |
| 111 | |
| 112 | /* |
| 113 | * InitStringTable() - Initialize the string table. |
| 114 | * |
| 115 | */ |
| 116 | |
| 117 | static int InitStringTable(StringTable *stable) |
| 118 | { |
| 119 | stable->strings = (char *) malloc(INIT_STRING_TABLE_SIZE); |
| 120 | if (!stable->strings) |
| 121 | return 0; |
| 122 | // Zero-th offset means "empty" so don't use it. |
| 123 | stable->nextFree = 1; |
| 124 | stable->size = INIT_STRING_TABLE_SIZE; |
| 125 | return 1; |
| 126 | } // InitStringTable |
| 127 | |
| 128 | /* |
| 129 | * FreeStringTable() - Free the string table. |
| 130 | * |
| 131 | */ |
| 132 | |
| 133 | static void FreeStringTable(StringTable *stable) |
| 134 | { |
| 135 | if (stable->strings) |
| 136 | free(stable->strings); |
| 137 | stable->strings = NULL; |
| 138 | stable->nextFree = 0; |
| 139 | stable->size = 0; |
| 140 | } // FreeStringTable |
| 141 | |
| 142 | /* |
| 143 | * HashString() - Hash a string with the base hash function. |
| 144 | * |
| 145 | */ |
| 146 | |
| 147 | static int HashString(const char *s) |
| 148 | { |
| 149 | int hval = 0; |
| 150 | |
| 151 | while (*s) { |
| 152 | hval = (hval*13507 + *s*197) ^ (hval >> 2); |
| 153 | s++; |
| 154 | } |
| 155 | return hval & 0x7fffffff; |
| 156 | } // HashString |
| 157 | |
| 158 | /* |
| 159 | * HashString2() - Hash a string with the incrimenting hash function. |
| 160 | * |
| 161 | */ |
| 162 | |
| 163 | static int HashString2(const char *s) |
| 164 | { |
| 165 | int hval = 0; |
| 166 | |
| 167 | while (*s) { |
| 168 | hval = (hval*729 + *s*37) ^ (hval >> 1); |
| 169 | s++; |
| 170 | } |
| 171 | return hval; |
| 172 | } // HashString2 |
| 173 | |
| 174 | /* |
| 175 | * AddString() - Add a string to a string table. Return it's offset. |
| 176 | * |
| 177 | */ |
| 178 | |
| 179 | static int AddString(StringTable *stable, const char *s) |
| 180 | { |
| 181 | int len, loc; |
| 182 | char *str; |
| 183 | |
| 184 | len = (int) strlen(s); |
daniel@transgaming.com | f02c9e6 | 2011-04-04 14:03:20 +0000 | [diff] [blame] | 185 | while (stable->nextFree + len + 1 >= stable->size) { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 186 | assert(stable->size < 1000000); |
| 187 | str = (char *) malloc(stable->size*2); |
| 188 | memcpy(str, stable->strings, stable->size); |
| 189 | free(stable->strings); |
| 190 | stable->strings = str; |
daniel@transgaming.com | f02c9e6 | 2011-04-04 14:03:20 +0000 | [diff] [blame] | 191 | stable->size = stable->size*2; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 192 | } |
| 193 | loc = stable->nextFree; |
| 194 | strcpy(&stable->strings[loc], s); |
| 195 | stable->nextFree += len + 1; |
| 196 | return loc; |
| 197 | } // AddString |
| 198 | |
| 199 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 200 | /////////////////////////////////////////// Hash table: /////////////////////////////////////// |
| 201 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 202 | |
| 203 | #define INIT_HASH_TABLE_SIZE 2047 |
| 204 | #define HASH_TABLE_MAX_COLLISIONS 3 |
| 205 | |
| 206 | typedef struct HashEntry_Rec { |
| 207 | int index; // String table offset of string representation |
| 208 | int value; // Atom (symbol) value |
| 209 | } HashEntry; |
| 210 | |
| 211 | typedef struct HashTable_Rec { |
| 212 | HashEntry *entry; |
| 213 | int size; |
| 214 | int entries; |
| 215 | int counts[HASH_TABLE_MAX_COLLISIONS + 1]; |
| 216 | } HashTable; |
| 217 | |
| 218 | /* |
| 219 | * InitHashTable() - Initialize the hash table. |
| 220 | * |
| 221 | */ |
| 222 | |
| 223 | static int InitHashTable(HashTable *htable, int fsize) |
| 224 | { |
| 225 | int ii; |
| 226 | |
| 227 | htable->entry = (HashEntry *) malloc(sizeof(HashEntry)*fsize); |
| 228 | if (!htable->entry) |
| 229 | return 0; |
| 230 | htable->size = fsize; |
| 231 | for (ii = 0; ii < fsize; ii++) { |
| 232 | htable->entry[ii].index = 0; |
| 233 | htable->entry[ii].value = 0; |
| 234 | } |
| 235 | htable->entries = 0; |
| 236 | for (ii = 0; ii <= HASH_TABLE_MAX_COLLISIONS; ii++) |
| 237 | htable->counts[ii] = 0; |
| 238 | return 1; |
| 239 | } // InitHashTable |
| 240 | |
| 241 | /* |
| 242 | * FreeHashTable() - Free the hash table. |
| 243 | * |
| 244 | */ |
| 245 | |
| 246 | static void FreeHashTable(HashTable *htable) |
| 247 | { |
| 248 | if (htable->entry) |
| 249 | free(htable->entry); |
| 250 | htable->entry = NULL; |
| 251 | htable->size = 0; |
| 252 | htable->entries = 0; |
| 253 | } // FreeHashTable |
| 254 | |
| 255 | /* |
| 256 | * Empty() - See if a hash table entry is empty. |
| 257 | * |
| 258 | */ |
| 259 | |
| 260 | static int Empty(HashTable *htable, int hashloc) |
| 261 | { |
| 262 | assert(hashloc >= 0 && hashloc < htable->size); |
| 263 | if (htable->entry[hashloc].index == 0) { |
| 264 | return 1; |
| 265 | } else { |
| 266 | return 0; |
| 267 | } |
| 268 | } // Empty |
| 269 | |
| 270 | /* |
| 271 | * Match() - See if a hash table entry is matches a string. |
| 272 | * |
| 273 | */ |
| 274 | |
| 275 | static int Match(HashTable *htable, StringTable *stable, const char *s, int hashloc) |
| 276 | { |
| 277 | int strloc; |
| 278 | |
| 279 | strloc = htable->entry[hashloc].index; |
| 280 | if (!strcmp(s, &stable->strings[strloc])) { |
| 281 | return 1; |
| 282 | } else { |
| 283 | return 0; |
| 284 | } |
| 285 | } // Match |
| 286 | |
| 287 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 288 | /////////////////////////////////////////// Atom table: /////////////////////////////////////// |
| 289 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 290 | |
| 291 | #define INIT_ATOM_TABLE_SIZE 1024 |
| 292 | |
| 293 | |
| 294 | struct AtomTable_Rec { |
| 295 | StringTable stable; // String table. |
| 296 | HashTable htable; // Hashes string to atom number and token value. Multiple strings can |
| 297 | // have the same token value but each unique string is a unique atom. |
| 298 | int *amap; // Maps atom value to offset in string table. Atoms all map to unique |
| 299 | // strings except for some undefined values in the lower, fixed part |
| 300 | // of the atom table that map to "<undefined>". The lowest 256 atoms |
| 301 | // correspond to single character ASCII values except for alphanumeric |
| 302 | // characters and '_', which can be other tokens. Next come the |
| 303 | // language tokens with their atom values equal to the token value. |
| 304 | // Then come predefined atoms, followed by user specified identifiers. |
| 305 | int *arev; // Reversed atom for symbol table use. |
| 306 | int nextFree; |
| 307 | int size; |
| 308 | }; |
| 309 | |
| 310 | static AtomTable latable = { { 0 } }; |
| 311 | AtomTable *atable = &latable; |
| 312 | |
| 313 | static int AddAtomFixed(AtomTable *atable, const char *s, int atom); |
| 314 | |
| 315 | /* |
| 316 | * GrowAtomTable() - Grow the atom table to at least "size" if it's smaller. |
| 317 | * |
| 318 | */ |
| 319 | |
| 320 | static int GrowAtomTable(AtomTable *atable, int size) |
| 321 | { |
| 322 | int *newmap, *newrev; |
| 323 | |
| 324 | if (atable->size < size) { |
| 325 | if (atable->amap) { |
| 326 | newmap = realloc(atable->amap, sizeof(int)*size); |
| 327 | newrev = realloc(atable->arev, sizeof(int)*size); |
| 328 | } else { |
| 329 | newmap = malloc(sizeof(int)*size); |
| 330 | newrev = malloc(sizeof(int)*size); |
| 331 | atable->size = 0; |
| 332 | } |
| 333 | if (!newmap || !newrev) { |
| 334 | /* failed to grow -- error */ |
| 335 | if (newmap) |
| 336 | atable->amap = newmap; |
| 337 | if (newrev) |
daniel@transgaming.com | 0f87e7f | 2011-06-24 14:03:32 +0000 | [diff] [blame] | 338 | atable->arev = newrev; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 339 | return -1; |
| 340 | } |
| 341 | memset(&newmap[atable->size], 0, (size - atable->size) * sizeof(int)); |
| 342 | memset(&newrev[atable->size], 0, (size - atable->size) * sizeof(int)); |
| 343 | atable->amap = newmap; |
| 344 | atable->arev = newrev; |
| 345 | atable->size = size; |
| 346 | } |
| 347 | return 0; |
| 348 | } // GrowAtomTable |
| 349 | |
| 350 | /* |
| 351 | * lReverse() - Reverse the bottom 20 bits of a 32 bit int. |
| 352 | * |
| 353 | */ |
| 354 | |
| 355 | static int lReverse(int fval) |
| 356 | { |
| 357 | unsigned int in = fval; |
| 358 | int result = 0, cnt = 0; |
| 359 | |
| 360 | while(in) { |
| 361 | result <<= 1; |
| 362 | result |= in&1; |
| 363 | in >>= 1; |
| 364 | cnt++; |
| 365 | } |
| 366 | |
| 367 | // Don't use all 31 bits. One million atoms is plenty and sometimes the |
| 368 | // upper bits are used for other things. |
| 369 | |
| 370 | if (cnt < 20) |
| 371 | result <<= 20 - cnt; |
| 372 | return result; |
| 373 | } // lReverse |
| 374 | |
| 375 | /* |
| 376 | * AllocateAtom() - Allocate a new atom. Associated with the "undefined" value of -1. |
| 377 | * |
| 378 | */ |
| 379 | |
| 380 | static int AllocateAtom(AtomTable *atable) |
| 381 | { |
| 382 | if (atable->nextFree >= atable->size) |
| 383 | GrowAtomTable(atable, atable->nextFree*2); |
| 384 | atable->amap[atable->nextFree] = -1; |
| 385 | atable->arev[atable->nextFree] = lReverse(atable->nextFree); |
| 386 | atable->nextFree++; |
| 387 | return atable->nextFree - 1; |
| 388 | } // AllocateAtom |
| 389 | |
| 390 | /* |
| 391 | * SetAtomValue() - Allocate a new atom associated with "hashindex". |
| 392 | * |
| 393 | */ |
| 394 | |
| 395 | static void SetAtomValue(AtomTable *atable, int atomnumber, int hashindex) |
| 396 | { |
| 397 | atable->amap[atomnumber] = atable->htable.entry[hashindex].index; |
| 398 | atable->htable.entry[hashindex].value = atomnumber; |
| 399 | } // SetAtomValue |
| 400 | |
| 401 | /* |
| 402 | * FindHashLoc() - Find the hash location for this string. Return -1 it hash table is full. |
| 403 | * |
| 404 | */ |
| 405 | |
| 406 | static int FindHashLoc(AtomTable *atable, const char *s) |
| 407 | { |
| 408 | int hashloc, hashdelta, count; |
| 409 | int FoundEmptySlot = 0; |
| 410 | int collision[HASH_TABLE_MAX_COLLISIONS + 1]; |
| 411 | |
| 412 | hashloc = HashString(s) % atable->htable.size; |
| 413 | if (!Empty(&atable->htable, hashloc)) { |
| 414 | if (Match(&atable->htable, &atable->stable, s, hashloc)) |
| 415 | return hashloc; |
| 416 | collision[0] = hashloc; |
| 417 | hashdelta = HashString2(s); |
| 418 | count = 0; |
| 419 | while (count < HASH_TABLE_MAX_COLLISIONS) { |
| 420 | hashloc = ((hashloc + hashdelta) & 0x7fffffff) % atable->htable.size; |
| 421 | if (!Empty(&atable->htable, hashloc)) { |
| 422 | if (Match(&atable->htable, &atable->stable, s, hashloc)) { |
| 423 | return hashloc; |
| 424 | } |
| 425 | } else { |
| 426 | FoundEmptySlot = 1; |
| 427 | break; |
| 428 | } |
| 429 | count++; |
| 430 | collision[count] = hashloc; |
| 431 | } |
| 432 | |
| 433 | if (!FoundEmptySlot) { |
| 434 | if (cpp->options.DumpAtomTable) { |
| 435 | int ii; |
| 436 | char str[200]; |
kbr@chromium.org | ddb6e8e | 2012-04-25 00:48:13 +0000 | [diff] [blame^] | 437 | snprintf(str, sizeof(str), "*** Hash failed with more than %d collisions. Must increase hash table size. ***", |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 438 | HASH_TABLE_MAX_COLLISIONS); |
| 439 | CPPShInfoLogMsg(str); |
| 440 | |
kbr@chromium.org | ddb6e8e | 2012-04-25 00:48:13 +0000 | [diff] [blame^] | 441 | snprintf(str, sizeof(str), "*** New string \"%s\", hash=%04x, delta=%04x", s, collision[0], hashdelta); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 442 | CPPShInfoLogMsg(str); |
| 443 | for (ii = 0; ii <= HASH_TABLE_MAX_COLLISIONS; ii++) { |
kbr@chromium.org | ddb6e8e | 2012-04-25 00:48:13 +0000 | [diff] [blame^] | 444 | snprintf(str, sizeof(str), "*** Collides on try %d at hash entry %04x with \"%s\"", |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 445 | ii + 1, collision[ii], GetAtomString(atable, atable->htable.entry[collision[ii]].value)); |
| 446 | CPPShInfoLogMsg(str); |
| 447 | } |
| 448 | } |
| 449 | return -1; |
| 450 | } else { |
| 451 | atable->htable.counts[count]++; |
| 452 | } |
| 453 | } |
| 454 | return hashloc; |
| 455 | } // FindHashLoc |
| 456 | |
| 457 | /* |
| 458 | * IncreaseHashTableSize() |
| 459 | * |
| 460 | */ |
| 461 | |
| 462 | static int IncreaseHashTableSize(AtomTable *atable) |
| 463 | { |
| 464 | int ii, strloc, oldhashloc, value, size; |
| 465 | AtomTable oldtable; |
| 466 | char *s; |
| 467 | |
| 468 | // Save the old atom table and create a new one: |
| 469 | |
| 470 | oldtable = *atable; |
| 471 | size = oldtable.htable.size*2 + 1; |
| 472 | if (!InitAtomTable(atable, size)) |
| 473 | return 0; |
| 474 | |
| 475 | // Add all the existing values to the new atom table preserving their atom values: |
| 476 | |
| 477 | for (ii = atable->nextFree; ii < oldtable.nextFree; ii++) { |
| 478 | strloc = oldtable.amap[ii]; |
| 479 | s = &oldtable.stable.strings[strloc]; |
| 480 | oldhashloc = FindHashLoc(&oldtable, s); |
| 481 | assert(oldhashloc >= 0); |
| 482 | value = oldtable.htable.entry[oldhashloc].value; |
| 483 | AddAtomFixed(atable, s, value); |
| 484 | } |
| 485 | FreeAtomTable(&oldtable); |
| 486 | return 1; |
| 487 | } // IncreaseHashTableSize |
| 488 | |
| 489 | /* |
| 490 | * LookUpAddStringHash() - Lookup a string in the hash table. If it's not there, add it and |
| 491 | * initialize the atom value in the hash table to 0. Return the hash table index. |
| 492 | */ |
| 493 | |
| 494 | static int LookUpAddStringHash(AtomTable *atable, const char *s) |
| 495 | { |
| 496 | int hashloc, strloc; |
| 497 | |
| 498 | while(1) { |
| 499 | hashloc = FindHashLoc(atable, s); |
| 500 | if (hashloc >= 0) |
| 501 | break; |
| 502 | IncreaseHashTableSize(atable); |
| 503 | } |
| 504 | |
| 505 | if (Empty(&atable->htable, hashloc)) { |
| 506 | atable->htable.entries++; |
| 507 | strloc = AddString(&atable->stable, s); |
| 508 | atable->htable.entry[hashloc].index = strloc; |
| 509 | atable->htable.entry[hashloc].value = 0; |
| 510 | } |
| 511 | return hashloc; |
| 512 | } // LookUpAddStringHash |
| 513 | |
| 514 | /* |
| 515 | * LookUpAddString() - Lookup a string in the hash table. If it's not there, add it and |
| 516 | * initialize the atom value in the hash table to the next atom number. |
| 517 | * Return the atom value of string. |
| 518 | */ |
| 519 | |
| 520 | int LookUpAddString(AtomTable *atable, const char *s) |
| 521 | { |
| 522 | int hashindex, atom; |
| 523 | |
| 524 | hashindex = LookUpAddStringHash(atable, s); |
| 525 | atom = atable->htable.entry[hashindex].value; |
| 526 | if (atom == 0) { |
| 527 | atom = AllocateAtom(atable); |
| 528 | SetAtomValue(atable, atom, hashindex); |
| 529 | } |
| 530 | return atom; |
| 531 | } // LookUpAddString |
| 532 | |
| 533 | /* |
| 534 | * GetAtomString() |
| 535 | * |
| 536 | */ |
| 537 | |
| 538 | const char *GetAtomString(AtomTable *atable, int atom) |
| 539 | { |
| 540 | int soffset; |
| 541 | |
| 542 | if (atom > 0 && atom < atable->nextFree) { |
| 543 | soffset = atable->amap[atom]; |
| 544 | if (soffset > 0 && soffset < atable->stable.nextFree) { |
| 545 | return &atable->stable.strings[soffset]; |
| 546 | } else { |
| 547 | return "<internal error: bad soffset>"; |
| 548 | } |
| 549 | } else { |
| 550 | if (atom == 0) { |
| 551 | return "<null atom>"; |
| 552 | } else { |
| 553 | if (atom == EOF) { |
| 554 | return "<EOF>"; |
| 555 | } else { |
| 556 | return "<invalid atom>"; |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | } // GetAtomString |
| 561 | |
| 562 | /* |
| 563 | * GetReversedAtom() |
| 564 | * |
| 565 | */ |
| 566 | |
| 567 | int GetReversedAtom(AtomTable *atable, int atom) |
| 568 | { |
| 569 | if (atom > 0 && atom < atable->nextFree) { |
| 570 | return atable->arev[atom]; |
| 571 | } else { |
| 572 | return 0; |
| 573 | } |
| 574 | } // GetReversedAtom |
| 575 | |
| 576 | /* |
| 577 | * AddAtom() - Add a string to the atom, hash and string tables if it isn't already there. |
| 578 | * Return it's atom index. |
| 579 | */ |
| 580 | |
| 581 | int AddAtom(AtomTable *atable, const char *s) |
| 582 | { |
| 583 | int atom; |
| 584 | |
| 585 | atom = LookUpAddString(atable, s); |
| 586 | return atom; |
| 587 | } // AddAtom |
| 588 | |
| 589 | /* |
| 590 | * AddAtomFixed() - Add an atom to the hash and string tables if it isn't already there. |
| 591 | * Assign it the atom value of "atom". |
| 592 | */ |
| 593 | |
| 594 | static int AddAtomFixed(AtomTable *atable, const char *s, int atom) |
| 595 | { |
| 596 | int hashindex, lsize; |
| 597 | |
| 598 | hashindex = LookUpAddStringHash(atable, s); |
| 599 | if (atable->nextFree >= atable->size || atom >= atable->size) { |
| 600 | lsize = atable->size*2; |
| 601 | if (lsize <= atom) |
| 602 | lsize = atom + 1; |
| 603 | GrowAtomTable(atable, lsize); |
| 604 | } |
| 605 | atable->amap[atom] = atable->htable.entry[hashindex].index; |
| 606 | atable->htable.entry[hashindex].value = atom; |
| 607 | //if (atom >= atable->nextFree) |
| 608 | // atable->nextFree = atom + 1; |
| 609 | while (atom >= atable->nextFree) { |
| 610 | atable->arev[atable->nextFree] = lReverse(atable->nextFree); |
| 611 | atable->nextFree++; |
| 612 | } |
| 613 | return atom; |
| 614 | } // AddAtomFixed |
| 615 | |
| 616 | /* |
| 617 | * InitAtomTable() - Initialize the atom table. |
| 618 | * |
| 619 | */ |
| 620 | |
| 621 | int InitAtomTable(AtomTable *atable, int htsize) |
| 622 | { |
alokp@chromium.org | bcfba4c | 2010-08-09 22:30:49 +0000 | [diff] [blame] | 623 | unsigned int ii; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 624 | |
| 625 | htsize = htsize <= 0 ? INIT_HASH_TABLE_SIZE : htsize; |
| 626 | if (!InitStringTable(&atable->stable)) |
| 627 | return 0; |
| 628 | if (!InitHashTable(&atable->htable, htsize)) |
| 629 | return 0; |
| 630 | |
| 631 | atable->nextFree = 0; |
| 632 | atable->amap = NULL; |
| 633 | atable->size = 0; |
| 634 | GrowAtomTable(atable, INIT_ATOM_TABLE_SIZE); |
| 635 | if (!atable->amap) |
| 636 | return 0; |
| 637 | |
| 638 | // Initialize lower part of atom table to "<undefined>" atom: |
| 639 | |
| 640 | AddAtomFixed(atable, "<undefined>", 0); |
| 641 | for (ii = 0; ii < FIRST_USER_TOKEN_SY; ii++) |
| 642 | atable->amap[ii] = atable->amap[0]; |
| 643 | |
| 644 | // Add single character tokens to the atom table: |
| 645 | |
| 646 | { |
| 647 | const char *s = "~!%^&*()-+=|,.<>/?;:[]{}#"; |
| 648 | char t[2]; |
| 649 | |
| 650 | t[1] = '\0'; |
| 651 | while (*s) { |
| 652 | t[0] = *s; |
| 653 | AddAtomFixed(atable, t, s[0]); |
| 654 | s++; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | // Add multiple character scanner tokens : |
| 659 | |
| 660 | for (ii = 0; ii < sizeof(tokens)/sizeof(tokens[0]); ii++) |
| 661 | AddAtomFixed(atable, tokens[ii].str, tokens[ii].val); |
| 662 | |
| 663 | // Add error symbol if running in error mode: |
| 664 | |
| 665 | if (cpp->options.ErrorMode) |
| 666 | AddAtomFixed(atable, "error", ERROR_SY); |
| 667 | |
| 668 | AddAtom(atable, "<*** end fixed atoms ***>"); |
| 669 | |
| 670 | return 1; |
| 671 | } // InitAtomTable |
| 672 | |
| 673 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 674 | ////////////////////////////////// Debug Printing Functions: ////////////////////////////////// |
| 675 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 676 | |
| 677 | /* |
| 678 | * PrintAtomTable() |
| 679 | * |
| 680 | */ |
| 681 | |
| 682 | void PrintAtomTable(AtomTable *atable) |
| 683 | { |
| 684 | int ii; |
| 685 | char str[200]; |
| 686 | |
| 687 | for (ii = 0; ii < atable->nextFree; ii++) { |
kbr@chromium.org | ddb6e8e | 2012-04-25 00:48:13 +0000 | [diff] [blame^] | 688 | snprintf(str, sizeof(str), "%d: \"%s\"", ii, &atable->stable.strings[atable->amap[ii]]); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 689 | CPPDebugLogMsg(str); |
| 690 | } |
kbr@chromium.org | ddb6e8e | 2012-04-25 00:48:13 +0000 | [diff] [blame^] | 691 | snprintf(str, sizeof(str), "Hash table: size=%d, entries=%d, collisions=", |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 692 | atable->htable.size, atable->htable.entries); |
| 693 | CPPDebugLogMsg(str); |
| 694 | for (ii = 0; ii < HASH_TABLE_MAX_COLLISIONS; ii++) { |
kbr@chromium.org | ddb6e8e | 2012-04-25 00:48:13 +0000 | [diff] [blame^] | 695 | snprintf(str, sizeof(str), " %d", atable->htable.counts[ii]); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 696 | CPPDebugLogMsg(str); |
| 697 | } |
| 698 | |
| 699 | } // PrintAtomTable |
| 700 | |
| 701 | |
| 702 | /* |
| 703 | * GetStringOfAtom() |
| 704 | * |
| 705 | */ |
| 706 | |
| 707 | char* GetStringOfAtom(AtomTable *atable, int atom) |
| 708 | { |
| 709 | char* chr_str; |
| 710 | chr_str=&atable->stable.strings[atable->amap[atom]]; |
| 711 | return chr_str; |
| 712 | } // GetStringOfAtom |
| 713 | |
| 714 | /* |
| 715 | * FreeAtomTable() - Free the atom table and associated memory |
| 716 | * |
| 717 | */ |
| 718 | |
| 719 | void FreeAtomTable(AtomTable *atable) |
| 720 | { |
| 721 | FreeStringTable(&atable->stable); |
| 722 | FreeHashTable(&atable->htable); |
| 723 | if (atable->amap) |
| 724 | free(atable->amap); |
| 725 | if (atable->arev) |
| 726 | free(atable->arev); |
| 727 | atable->amap = NULL; |
| 728 | atable->arev = NULL; |
| 729 | atable->nextFree = 0; |
| 730 | atable->size = 0; |
| 731 | } // FreeAtomTable |
| 732 | |
| 733 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 734 | ///////////////////////////////////////// End of atom.c /////////////////////////////////////// |
| 735 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 736 | |