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 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 30 | #include "preparse-data.h" |
| 31 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame^] | 32 | #include "checks.h" |
| 33 | #include "globals.h" |
| 34 | #include "hashmap.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 35 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | // FunctionLoggingParserRecorder |
| 41 | |
| 42 | FunctionLoggingParserRecorder::FunctionLoggingParserRecorder() |
| 43 | : function_store_(0), |
| 44 | is_recording_(true), |
| 45 | pause_count_(0) { |
| 46 | preamble_[PreparseDataConstants::kMagicOffset] = |
| 47 | PreparseDataConstants::kMagicNumber; |
| 48 | preamble_[PreparseDataConstants::kVersionOffset] = |
| 49 | PreparseDataConstants::kCurrentVersion; |
| 50 | preamble_[PreparseDataConstants::kHasErrorOffset] = false; |
| 51 | preamble_[PreparseDataConstants::kFunctionsSizeOffset] = 0; |
| 52 | preamble_[PreparseDataConstants::kSymbolCountOffset] = 0; |
| 53 | preamble_[PreparseDataConstants::kSizeOffset] = 0; |
| 54 | ASSERT_EQ(6, PreparseDataConstants::kHeaderSize); |
| 55 | #ifdef DEBUG |
| 56 | prev_start_ = -1; |
| 57 | #endif |
| 58 | } |
| 59 | |
| 60 | |
| 61 | void FunctionLoggingParserRecorder::LogMessage(int start_pos, |
| 62 | int end_pos, |
| 63 | const char* message, |
| 64 | const char* arg_opt) { |
| 65 | if (has_error()) return; |
| 66 | preamble_[PreparseDataConstants::kHasErrorOffset] = true; |
| 67 | function_store_.Reset(); |
| 68 | STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0); |
| 69 | function_store_.Add(start_pos); |
| 70 | STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1); |
| 71 | function_store_.Add(end_pos); |
| 72 | STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2); |
| 73 | function_store_.Add((arg_opt == NULL) ? 0 : 1); |
| 74 | STATIC_ASSERT(PreparseDataConstants::kMessageTextPos == 3); |
| 75 | WriteString(CStrVector(message)); |
| 76 | if (arg_opt) WriteString(CStrVector(arg_opt)); |
| 77 | is_recording_ = false; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | void FunctionLoggingParserRecorder::WriteString(Vector<const char> str) { |
| 82 | function_store_.Add(str.length()); |
| 83 | for (int i = 0; i < str.length(); i++) { |
| 84 | function_store_.Add(str[i]); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // ---------------------------------------------------------------------------- |
| 89 | // PartialParserRecorder - Record both function entries and symbols. |
| 90 | |
| 91 | Vector<unsigned> PartialParserRecorder::ExtractData() { |
| 92 | int function_size = function_store_.size(); |
| 93 | int total_size = PreparseDataConstants::kHeaderSize + function_size; |
| 94 | Vector<unsigned> data = Vector<unsigned>::New(total_size); |
| 95 | preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size; |
| 96 | preamble_[PreparseDataConstants::kSymbolCountOffset] = 0; |
| 97 | memcpy(data.start(), preamble_, sizeof(preamble_)); |
| 98 | int symbol_start = PreparseDataConstants::kHeaderSize + function_size; |
| 99 | if (function_size > 0) { |
| 100 | function_store_.WriteTo(data.SubVector(PreparseDataConstants::kHeaderSize, |
| 101 | symbol_start)); |
| 102 | } |
| 103 | return data; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | // ---------------------------------------------------------------------------- |
| 108 | // CompleteParserRecorder - Record both function entries and symbols. |
| 109 | |
| 110 | CompleteParserRecorder::CompleteParserRecorder() |
| 111 | : FunctionLoggingParserRecorder(), |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 112 | literal_chars_(0), |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 113 | symbol_store_(0), |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 114 | symbol_keys_(0), |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 115 | symbol_table_(vector_compare), |
| 116 | symbol_id_(0) { |
| 117 | } |
| 118 | |
| 119 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 120 | void CompleteParserRecorder::LogSymbol(int start, |
| 121 | int hash, |
| 122 | bool is_ascii, |
| 123 | Vector<const byte> literal_bytes) { |
| 124 | Key key = { is_ascii, literal_bytes }; |
| 125 | HashMap::Entry* entry = symbol_table_.Lookup(&key, hash, true); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 126 | int id = static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 127 | if (id == 0) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 128 | // Copy literal contents for later comparison. |
| 129 | key.literal_bytes = |
| 130 | Vector<const byte>::cast(literal_chars_.AddBlock(literal_bytes)); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 131 | // Put (symbol_id_ + 1) into entry and increment it. |
| 132 | id = ++symbol_id_; |
| 133 | entry->value = reinterpret_cast<void*>(id); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 134 | Vector<Key> symbol = symbol_keys_.AddBlock(1, key); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 135 | entry->key = &symbol[0]; |
| 136 | } |
| 137 | WriteNumber(id - 1); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | Vector<unsigned> CompleteParserRecorder::ExtractData() { |
| 142 | int function_size = function_store_.size(); |
| 143 | // Add terminator to symbols, then pad to unsigned size. |
| 144 | int symbol_size = symbol_store_.size(); |
| 145 | int padding = sizeof(unsigned) - (symbol_size % sizeof(unsigned)); |
| 146 | symbol_store_.AddBlock(padding, PreparseDataConstants::kNumberTerminator); |
| 147 | symbol_size += padding; |
| 148 | int total_size = PreparseDataConstants::kHeaderSize + function_size |
| 149 | + (symbol_size / sizeof(unsigned)); |
| 150 | Vector<unsigned> data = Vector<unsigned>::New(total_size); |
| 151 | preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size; |
| 152 | preamble_[PreparseDataConstants::kSymbolCountOffset] = symbol_id_; |
| 153 | memcpy(data.start(), preamble_, sizeof(preamble_)); |
| 154 | int symbol_start = PreparseDataConstants::kHeaderSize + function_size; |
| 155 | if (function_size > 0) { |
| 156 | function_store_.WriteTo(data.SubVector(PreparseDataConstants::kHeaderSize, |
| 157 | symbol_start)); |
| 158 | } |
| 159 | if (!has_error()) { |
| 160 | symbol_store_.WriteTo( |
| 161 | Vector<byte>::cast(data.SubVector(symbol_start, total_size))); |
| 162 | } |
| 163 | return data; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void CompleteParserRecorder::WriteNumber(int number) { |
| 168 | ASSERT(number >= 0); |
| 169 | |
| 170 | int mask = (1 << 28) - 1; |
| 171 | for (int i = 28; i > 0; i -= 7) { |
| 172 | if (number > mask) { |
| 173 | symbol_store_.Add(static_cast<byte>(number >> i) | 0x80u); |
| 174 | number &= mask; |
| 175 | } |
| 176 | mask >>= 7; |
| 177 | } |
| 178 | symbol_store_.Add(static_cast<byte>(number)); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | } } // namespace v8::internal. |