Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [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 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 28 | #ifndef V8_PREPARSE_DATA_H_ |
| 29 | #define V8_PREPARSE_DATA_H_ |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 30 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 31 | #include "allocation.h" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 32 | #include "hashmap.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 33 | #include "utils-inl.h" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 38 | // ---------------------------------------------------------------------------- |
| 39 | // ParserRecorder - Logging of preparser data. |
| 40 | |
| 41 | // Abstract interface for preparse data recorder. |
| 42 | class ParserRecorder { |
| 43 | public: |
| 44 | ParserRecorder() { } |
| 45 | virtual ~ParserRecorder() { } |
| 46 | |
| 47 | // Logs the scope and some details of a function literal in the source. |
| 48 | virtual void LogFunction(int start, |
| 49 | int end, |
| 50 | int literals, |
| 51 | int properties) = 0; |
| 52 | |
| 53 | // Logs a symbol creation of a literal or identifier. |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 54 | virtual void LogAsciiSymbol(int start, Vector<const char> literal) { } |
| 55 | virtual void LogUC16Symbol(int start, Vector<const uc16> literal) { } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 56 | |
| 57 | // Logs an error message and marks the log as containing an error. |
| 58 | // Further logging will be ignored, and ExtractData will return a vector |
| 59 | // representing the error only. |
| 60 | virtual void LogMessage(int start, |
| 61 | int end, |
| 62 | const char* message, |
| 63 | const char* argument_opt) = 0; |
| 64 | |
| 65 | virtual int function_position() = 0; |
| 66 | |
| 67 | virtual int symbol_position() = 0; |
| 68 | |
| 69 | virtual int symbol_ids() = 0; |
| 70 | |
| 71 | virtual Vector<unsigned> ExtractData() = 0; |
| 72 | |
| 73 | virtual void PauseRecording() = 0; |
| 74 | |
| 75 | virtual void ResumeRecording() = 0; |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | // ---------------------------------------------------------------------------- |
| 80 | // FunctionLoggingParserRecorder - Record only function entries |
| 81 | |
| 82 | class FunctionLoggingParserRecorder : public ParserRecorder { |
| 83 | public: |
| 84 | FunctionLoggingParserRecorder(); |
| 85 | virtual ~FunctionLoggingParserRecorder() {} |
| 86 | |
| 87 | virtual void LogFunction(int start, int end, int literals, int properties) { |
| 88 | function_store_.Add(start); |
| 89 | function_store_.Add(end); |
| 90 | function_store_.Add(literals); |
| 91 | function_store_.Add(properties); |
| 92 | } |
| 93 | |
| 94 | // Logs an error message and marks the log as containing an error. |
| 95 | // Further logging will be ignored, and ExtractData will return a vector |
| 96 | // representing the error only. |
| 97 | virtual void LogMessage(int start, |
| 98 | int end, |
| 99 | const char* message, |
| 100 | const char* argument_opt); |
| 101 | |
| 102 | virtual int function_position() { return function_store_.size(); } |
| 103 | |
| 104 | |
| 105 | virtual Vector<unsigned> ExtractData() = 0; |
| 106 | |
| 107 | virtual void PauseRecording() { |
| 108 | pause_count_++; |
| 109 | is_recording_ = false; |
| 110 | } |
| 111 | |
| 112 | virtual void ResumeRecording() { |
| 113 | ASSERT(pause_count_ > 0); |
| 114 | if (--pause_count_ == 0) is_recording_ = !has_error(); |
| 115 | } |
| 116 | |
| 117 | protected: |
| 118 | bool has_error() { |
| 119 | return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]); |
| 120 | } |
| 121 | |
| 122 | bool is_recording() { |
| 123 | return is_recording_; |
| 124 | } |
| 125 | |
| 126 | void WriteString(Vector<const char> str); |
| 127 | |
| 128 | Collector<unsigned> function_store_; |
| 129 | unsigned preamble_[PreparseDataConstants::kHeaderSize]; |
| 130 | bool is_recording_; |
| 131 | int pause_count_; |
| 132 | |
| 133 | #ifdef DEBUG |
| 134 | int prev_start_; |
| 135 | #endif |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | // ---------------------------------------------------------------------------- |
| 140 | // PartialParserRecorder - Record only function entries |
| 141 | |
| 142 | class PartialParserRecorder : public FunctionLoggingParserRecorder { |
| 143 | public: |
| 144 | PartialParserRecorder() : FunctionLoggingParserRecorder() { } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 145 | virtual void LogAsciiSymbol(int start, Vector<const char> literal) { } |
| 146 | virtual void LogUC16Symbol(int start, Vector<const uc16> literal) { } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 147 | virtual ~PartialParserRecorder() { } |
| 148 | virtual Vector<unsigned> ExtractData(); |
| 149 | virtual int symbol_position() { return 0; } |
| 150 | virtual int symbol_ids() { return 0; } |
| 151 | }; |
| 152 | |
| 153 | |
| 154 | // ---------------------------------------------------------------------------- |
| 155 | // CompleteParserRecorder - Record both function entries and symbols. |
| 156 | |
| 157 | class CompleteParserRecorder: public FunctionLoggingParserRecorder { |
| 158 | public: |
| 159 | CompleteParserRecorder(); |
| 160 | virtual ~CompleteParserRecorder() { } |
| 161 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 162 | virtual void LogAsciiSymbol(int start, Vector<const char> literal) { |
| 163 | if (!is_recording_) return; |
| 164 | int hash = vector_hash(literal); |
| 165 | LogSymbol(start, hash, true, Vector<const byte>::cast(literal)); |
| 166 | } |
| 167 | |
| 168 | virtual void LogUC16Symbol(int start, Vector<const uc16> literal) { |
| 169 | if (!is_recording_) return; |
| 170 | int hash = vector_hash(literal); |
| 171 | LogSymbol(start, hash, false, Vector<const byte>::cast(literal)); |
| 172 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 173 | |
| 174 | virtual Vector<unsigned> ExtractData(); |
| 175 | |
| 176 | virtual int symbol_position() { return symbol_store_.size(); } |
| 177 | virtual int symbol_ids() { return symbol_id_; } |
| 178 | |
| 179 | private: |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 180 | struct Key { |
| 181 | bool is_ascii; |
| 182 | Vector<const byte> literal_bytes; |
| 183 | }; |
| 184 | |
| 185 | virtual void LogSymbol(int start, |
| 186 | int hash, |
| 187 | bool is_ascii, |
| 188 | Vector<const byte> literal); |
| 189 | |
| 190 | template <typename Char> |
| 191 | static int vector_hash(Vector<const Char> string) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 192 | int hash = 0; |
| 193 | for (int i = 0; i < string.length(); i++) { |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 194 | int c = static_cast<int>(string[i]); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 195 | hash += c; |
| 196 | hash += (hash << 10); |
| 197 | hash ^= (hash >> 6); |
| 198 | } |
| 199 | return hash; |
| 200 | } |
| 201 | |
| 202 | static bool vector_compare(void* a, void* b) { |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 203 | Key* string1 = reinterpret_cast<Key*>(a); |
| 204 | Key* string2 = reinterpret_cast<Key*>(b); |
| 205 | if (string1->is_ascii != string2->is_ascii) return false; |
| 206 | int length = string1->literal_bytes.length(); |
| 207 | if (string2->literal_bytes.length() != length) return false; |
| 208 | return memcmp(string1->literal_bytes.start(), |
| 209 | string2->literal_bytes.start(), length) == 0; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Write a non-negative number to the symbol store. |
| 213 | void WriteNumber(int number); |
| 214 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 215 | Collector<byte> literal_chars_; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 216 | Collector<byte> symbol_store_; |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 217 | Collector<Key> symbol_keys_; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 218 | HashMap symbol_table_; |
| 219 | int symbol_id_; |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | } } // namespace v8::internal. |
| 224 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 225 | #endif // V8_PREPARSE_DATA_H_ |