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
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.
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.
28 #include "../include/v8stdint.h"
30 #include "preparse-data-format.h"
31 #include "preparse-data.h"
41 template <typename Char>
42 static int vector_hash(Vector<const Char> string) {
44 for (int i = 0; i < string.length(); i++) {
45 int c = static_cast<int>(string[i]);
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;
67 CompleteParserRecorder::CompleteParserRecorder()
72 string_table_(vector_compare),
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);
86 should_log_symbols_ = true;
90 void CompleteParserRecorder::LogMessage(int start_pos,
93 const char* arg_opt) {
94 if (has_error()) return;
95 preamble_[PreparseDataConstants::kHasErrorOffset] = true;
96 function_store_.Reset();
97 STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0);
98 function_store_.Add(start_pos);
99 STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1);
100 function_store_.Add(end_pos);
101 STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2);
102 function_store_.Add((arg_opt == NULL) ? 0 : 1);
103 STATIC_ASSERT(PreparseDataConstants::kMessageTextPos == 3);
104 WriteString(CStrVector(message));
105 if (arg_opt != NULL) WriteString(CStrVector(arg_opt));
106 should_log_symbols_ = false;
110 void CompleteParserRecorder::WriteString(Vector<const char> str) {
111 function_store_.Add(str.length());
112 for (int i = 0; i < str.length(); i++) {
113 function_store_.Add(str[i]);
118 void CompleteParserRecorder::LogOneByteSymbol(int start,
119 Vector<const uint8_t> literal) {
120 ASSERT(should_log_symbols_);
121 int hash = vector_hash(literal);
122 LogSymbol(start, hash, true, literal);
126 void CompleteParserRecorder::LogTwoByteSymbol(int start,
127 Vector<const uint16_t> literal) {
128 ASSERT(should_log_symbols_);
129 int hash = vector_hash(literal);
130 LogSymbol(start, hash, false, Vector<const byte>::cast(literal));
134 void CompleteParserRecorder::LogSymbol(int start,
137 Vector<const byte> literal_bytes) {
138 Key key = { is_one_byte, literal_bytes };
139 HashMap::Entry* entry = string_table_.Lookup(&key, hash, true);
140 int id = static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
142 // Copy literal contents for later comparison.
144 Vector<const byte>::cast(literal_chars_.AddBlock(literal_bytes));
145 // Put (symbol_id_ + 1) into entry and increment it.
147 entry->value = reinterpret_cast<void*>(id);
148 Vector<Key> symbol = symbol_keys_.AddBlock(1, key);
149 entry->key = &symbol[0];
155 Vector<unsigned> CompleteParserRecorder::ExtractData() {
156 int function_size = function_store_.size();
157 // Add terminator to symbols, then pad to unsigned size.
158 int symbol_size = symbol_store_.size();
159 int padding = sizeof(unsigned) - (symbol_size % sizeof(unsigned));
160 symbol_store_.AddBlock(padding, PreparseDataConstants::kNumberTerminator);
161 symbol_size += padding;
162 int total_size = PreparseDataConstants::kHeaderSize + function_size
163 + (symbol_size / sizeof(unsigned));
164 Vector<unsigned> data = Vector<unsigned>::New(total_size);
165 preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size;
166 preamble_[PreparseDataConstants::kSymbolCountOffset] = symbol_id_;
167 OS::MemCopy(data.start(), preamble_, sizeof(preamble_));
168 int symbol_start = PreparseDataConstants::kHeaderSize + function_size;
169 if (function_size > 0) {
170 function_store_.WriteTo(data.SubVector(PreparseDataConstants::kHeaderSize,
174 symbol_store_.WriteTo(
175 Vector<byte>::cast(data.SubVector(symbol_start, total_size)));
181 void CompleteParserRecorder::WriteNumber(int number) {
182 // Split the number into chunks of 7 bits. Write them one after another (the
183 // most significant first). Use the MSB of each byte for signalling that the
184 // number continues. See ScriptDataImpl::ReadNumber for the reading side.
187 int mask = (1 << 28) - 1;
189 // 26 million symbols ought to be enough for anybody.
190 ASSERT(number <= mask);
191 while (number < mask) {
196 symbol_store_.Add(static_cast<byte>(number >> i) | 0x80u);
201 ASSERT(number < (1 << 7));
202 symbol_store_.Add(static_cast<byte>(number));
206 } } // namespace v8::internal.