ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 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 "../include/v8stdint.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 29 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 30 | #include "preparse-data-format.h" |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 31 | #include "preparse-data.h" |
| 32 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 33 | #include "checks.h" |
| 34 | #include "globals.h" |
| 35 | #include "hashmap.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 36 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 40 | |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 41 | template <typename Char> |
| 42 | static int vector_hash(Vector<const Char> string) { |
| 43 | int hash = 0; |
| 44 | for (int i = 0; i < string.length(); i++) { |
| 45 | int c = static_cast<int>(string[i]); |
| 46 | hash += c; |
| 47 | hash += (hash << 10); |
| 48 | hash ^= (hash >> 6); |
| 49 | } |
| 50 | return hash; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | static bool vector_compare(void* a, void* b) { |
| 55 | CompleteParserRecorder::Key* string1 = |
| 56 | reinterpret_cast<CompleteParserRecorder::Key*>(a); |
| 57 | CompleteParserRecorder::Key* string2 = |
| 58 | reinterpret_cast<CompleteParserRecorder::Key*>(b); |
| 59 | if (string1->is_one_byte != string2->is_one_byte) return false; |
| 60 | int length = string1->literal_bytes.length(); |
| 61 | if (string2->literal_bytes.length() != length) return false; |
| 62 | return memcmp(string1->literal_bytes.start(), |
| 63 | string2->literal_bytes.start(), length) == 0; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | CompleteParserRecorder::CompleteParserRecorder() |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 68 | : function_store_(0), |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 69 | literal_chars_(0), |
| 70 | symbol_store_(0), |
| 71 | symbol_keys_(0), |
| 72 | string_table_(vector_compare), |
| 73 | symbol_id_(0) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 74 | preamble_[PreparseDataConstants::kMagicOffset] = |
| 75 | PreparseDataConstants::kMagicNumber; |
| 76 | preamble_[PreparseDataConstants::kVersionOffset] = |
| 77 | PreparseDataConstants::kCurrentVersion; |
| 78 | preamble_[PreparseDataConstants::kHasErrorOffset] = false; |
| 79 | preamble_[PreparseDataConstants::kFunctionsSizeOffset] = 0; |
| 80 | preamble_[PreparseDataConstants::kSymbolCountOffset] = 0; |
| 81 | preamble_[PreparseDataConstants::kSizeOffset] = 0; |
| 82 | ASSERT_EQ(6, PreparseDataConstants::kHeaderSize); |
| 83 | #ifdef DEBUG |
| 84 | prev_start_ = -1; |
| 85 | #endif |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 86 | should_log_symbols_ = true; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 90 | void CompleteParserRecorder::LogMessage(int start_pos, |
machenbach@chromium.org | ed1a631 | 2014-04-02 00:05:15 +0000 | [diff] [blame] | 91 | int end_pos, |
| 92 | const char* message, |
| 93 | const char* arg_opt, |
| 94 | bool is_reference_error) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 95 | if (has_error()) return; |
| 96 | preamble_[PreparseDataConstants::kHasErrorOffset] = true; |
| 97 | function_store_.Reset(); |
| 98 | STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0); |
| 99 | function_store_.Add(start_pos); |
| 100 | STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1); |
| 101 | function_store_.Add(end_pos); |
| 102 | STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2); |
| 103 | function_store_.Add((arg_opt == NULL) ? 0 : 1); |
machenbach@chromium.org | ed1a631 | 2014-04-02 00:05:15 +0000 | [diff] [blame] | 104 | STATIC_ASSERT(PreparseDataConstants::kIsReferenceErrorPos == 3); |
| 105 | function_store_.Add(is_reference_error ? 1 : 0); |
| 106 | STATIC_ASSERT(PreparseDataConstants::kMessageTextPos == 4); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 107 | WriteString(CStrVector(message)); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 108 | if (arg_opt != NULL) WriteString(CStrVector(arg_opt)); |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 109 | should_log_symbols_ = false; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 113 | void CompleteParserRecorder::WriteString(Vector<const char> str) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 114 | function_store_.Add(str.length()); |
| 115 | for (int i = 0; i < str.length(); i++) { |
| 116 | function_store_.Add(str[i]); |
| 117 | } |
| 118 | } |
| 119 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 120 | |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 121 | void CompleteParserRecorder::LogOneByteSymbol(int start, |
| 122 | Vector<const uint8_t> literal) { |
| 123 | ASSERT(should_log_symbols_); |
| 124 | int hash = vector_hash(literal); |
| 125 | LogSymbol(start, hash, true, literal); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 129 | void CompleteParserRecorder::LogTwoByteSymbol(int start, |
| 130 | Vector<const uint16_t> literal) { |
| 131 | ASSERT(should_log_symbols_); |
| 132 | int hash = vector_hash(literal); |
| 133 | LogSymbol(start, hash, false, Vector<const byte>::cast(literal)); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 137 | void CompleteParserRecorder::LogSymbol(int start, |
| 138 | int hash, |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 139 | bool is_one_byte, |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 140 | Vector<const byte> literal_bytes) { |
machenbach@chromium.org | 381adef | 2014-03-14 03:04:56 +0000 | [diff] [blame] | 141 | Key key = { is_one_byte, literal_bytes }; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 142 | HashMap::Entry* entry = string_table_.Lookup(&key, hash, true); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 143 | int id = static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 144 | if (id == 0) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 145 | // Copy literal contents for later comparison. |
| 146 | key.literal_bytes = |
| 147 | Vector<const byte>::cast(literal_chars_.AddBlock(literal_bytes)); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 148 | // Put (symbol_id_ + 1) into entry and increment it. |
| 149 | id = ++symbol_id_; |
| 150 | entry->value = reinterpret_cast<void*>(id); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 151 | Vector<Key> symbol = symbol_keys_.AddBlock(1, key); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 152 | entry->key = &symbol[0]; |
| 153 | } |
| 154 | WriteNumber(id - 1); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | Vector<unsigned> CompleteParserRecorder::ExtractData() { |
| 159 | int function_size = function_store_.size(); |
| 160 | // Add terminator to symbols, then pad to unsigned size. |
| 161 | int symbol_size = symbol_store_.size(); |
| 162 | int padding = sizeof(unsigned) - (symbol_size % sizeof(unsigned)); |
| 163 | symbol_store_.AddBlock(padding, PreparseDataConstants::kNumberTerminator); |
| 164 | symbol_size += padding; |
| 165 | int total_size = PreparseDataConstants::kHeaderSize + function_size |
| 166 | + (symbol_size / sizeof(unsigned)); |
| 167 | Vector<unsigned> data = Vector<unsigned>::New(total_size); |
| 168 | preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size; |
| 169 | preamble_[PreparseDataConstants::kSymbolCountOffset] = symbol_id_; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 170 | OS::MemCopy(data.start(), preamble_, sizeof(preamble_)); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 171 | int symbol_start = PreparseDataConstants::kHeaderSize + function_size; |
| 172 | if (function_size > 0) { |
| 173 | function_store_.WriteTo(data.SubVector(PreparseDataConstants::kHeaderSize, |
| 174 | symbol_start)); |
| 175 | } |
| 176 | if (!has_error()) { |
| 177 | symbol_store_.WriteTo( |
| 178 | Vector<byte>::cast(data.SubVector(symbol_start, total_size))); |
| 179 | } |
| 180 | return data; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | void CompleteParserRecorder::WriteNumber(int number) { |
titzer@chromium.org | f5a2454 | 2014-03-04 09:06:17 +0000 | [diff] [blame] | 185 | // Split the number into chunks of 7 bits. Write them one after another (the |
| 186 | // most significant first). Use the MSB of each byte for signalling that the |
| 187 | // number continues. See ScriptDataImpl::ReadNumber for the reading side. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 188 | ASSERT(number >= 0); |
| 189 | |
| 190 | int mask = (1 << 28) - 1; |
titzer@chromium.org | f5a2454 | 2014-03-04 09:06:17 +0000 | [diff] [blame] | 191 | int i = 28; |
| 192 | // 26 million symbols ought to be enough for anybody. |
| 193 | ASSERT(number <= mask); |
| 194 | while (number < mask) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 195 | mask >>= 7; |
titzer@chromium.org | f5a2454 | 2014-03-04 09:06:17 +0000 | [diff] [blame] | 196 | i -= 7; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 197 | } |
titzer@chromium.org | f5a2454 | 2014-03-04 09:06:17 +0000 | [diff] [blame] | 198 | while (i > 0) { |
| 199 | symbol_store_.Add(static_cast<byte>(number >> i) | 0x80u); |
| 200 | number &= mask; |
| 201 | mask >>= 7; |
| 202 | i -= 7; |
| 203 | } |
| 204 | ASSERT(number < (1 << 7)); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 205 | symbol_store_.Add(static_cast<byte>(number)); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | } } // namespace v8::internal. |