ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "token.h" |
| 31 | |
| 32 | namespace v8 { namespace internal { |
| 33 | |
| 34 | #ifdef DEBUG |
| 35 | #define T(name, string, precedence) #name, |
| 36 | const char* Token::name_[NUM_TOKENS] = { |
| 37 | TOKEN_LIST(T, T, IGNORE_TOKEN) |
| 38 | }; |
| 39 | #undef T |
| 40 | #endif |
| 41 | |
| 42 | |
| 43 | #define T(name, string, precedence) string, |
| 44 | const char* Token::string_[NUM_TOKENS] = { |
| 45 | TOKEN_LIST(T, T, IGNORE_TOKEN) |
| 46 | }; |
| 47 | #undef T |
| 48 | |
| 49 | |
| 50 | #define T(name, string, precedence) precedence, |
| 51 | int8_t Token::precedence_[NUM_TOKENS] = { |
| 52 | TOKEN_LIST(T, T, IGNORE_TOKEN) |
| 53 | }; |
| 54 | #undef T |
| 55 | |
| 56 | |
| 57 | // A perfect (0 collision) hash table of keyword token values. |
| 58 | |
| 59 | // larger N will reduce the number of collisions (power of 2 for fast %) |
| 60 | const unsigned int N = 128; |
| 61 | // make this small since we have <= 256 tokens |
| 62 | static uint8_t Hashtable[N]; |
| 63 | static bool IsInitialized = false; |
| 64 | |
| 65 | |
| 66 | static unsigned int Hash(const char* s) { |
| 67 | // The following constants have been found using trial-and-error. If the |
| 68 | // keyword set changes, they may have to be recomputed (make them flags |
| 69 | // and play with the flag values). Increasing N is the simplest way to |
| 70 | // reduce the number of collisions. |
| 71 | |
| 72 | // we must use at least 4 or more chars ('const' and 'continue' share |
| 73 | // 'con') |
| 74 | const unsigned int L = 5; |
| 75 | // smaller S tend to reduce the number of collisions |
| 76 | const unsigned int S = 4; |
| 77 | // make this a prime, or at least an odd number |
| 78 | const unsigned int M = 3; |
| 79 | |
| 80 | unsigned int h = 0; |
| 81 | for (unsigned int i = 0; s[i] != '\0' && i < L; i++) { |
| 82 | h += (h << S) + s[i]; |
| 83 | } |
| 84 | // unsigned int % by a power of 2 (otherwise this will not be a bit mask) |
| 85 | return h * M % N; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | Token::Value Token::Lookup(const char* str) { |
| 90 | ASSERT(IsInitialized); |
| 91 | Value k = static_cast<Value>(Hashtable[Hash(str)]); |
| 92 | const char* s = string_[k]; |
| 93 | ASSERT(s != NULL || k == IDENTIFIER); |
| 94 | if (s == NULL || strcmp(s, str) == 0) { |
| 95 | return k; |
| 96 | } |
| 97 | return IDENTIFIER; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | #ifdef DEBUG |
| 102 | // We need this function because C++ doesn't allow the expression |
| 103 | // NULL == NULL, which is a result of macro expansion below. What |
| 104 | // the hell? |
| 105 | static bool IsNull(const char* s) { |
| 106 | return s == NULL; |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | |
| 111 | void Token::Initialize() { |
| 112 | if (IsInitialized) return; |
| 113 | |
| 114 | // A list of all keywords, terminated by ILLEGAL. |
| 115 | #define T(name, string, precedence) name, |
| 116 | static Value keyword[] = { |
| 117 | TOKEN_LIST(IGNORE_TOKEN, T, IGNORE_TOKEN) |
| 118 | ILLEGAL |
| 119 | }; |
| 120 | #undef T |
| 121 | |
| 122 | // Assert that the keyword array contains the 25 keywords, 3 future |
| 123 | // reserved words (const, debugger, and native), and the 3 named literals |
| 124 | // defined by ECMA-262 standard. |
| 125 | ASSERT(ARRAY_SIZE(keyword) == 25 + 3 + 3 + 1); // +1 for ILLEGAL sentinel |
| 126 | |
| 127 | // Initialize Hashtable. |
| 128 | ASSERT(NUM_TOKENS <= 256); // Hashtable contains uint8_t elements |
| 129 | for (unsigned int i = 0; i < N; i++) { |
| 130 | Hashtable[i] = IDENTIFIER; |
| 131 | } |
| 132 | |
| 133 | // Insert all keywords into Hashtable. |
| 134 | int collisions = 0; |
| 135 | for (int i = 0; keyword[i] != ILLEGAL; i++) { |
| 136 | Value k = keyword[i]; |
| 137 | unsigned int h = Hash(string_[k]); |
| 138 | if (Hashtable[h] != IDENTIFIER) collisions++; |
| 139 | Hashtable[h] = k; |
| 140 | } |
| 141 | |
| 142 | if (collisions > 0) { |
| 143 | PrintF("%d collisions in keyword hashtable\n", collisions); |
| 144 | FATAL("Fix keyword lookup!"); |
| 145 | } |
| 146 | |
| 147 | IsInitialized = true; |
| 148 | |
| 149 | // Verify hash table. |
| 150 | #define T(name, string, precedence) \ |
| 151 | ASSERT(IsNull(string) || Lookup(string) == IDENTIFIER); |
| 152 | |
| 153 | #define K(name, string, precedence) \ |
| 154 | ASSERT(Lookup(string) == name); |
| 155 | |
| 156 | TOKEN_LIST(T, K, IGNORE_TOKEN) |
| 157 | |
| 158 | #undef K |
| 159 | #undef T |
| 160 | } |
| 161 | |
| 162 | } } // namespace v8::internal |