blob: cea54efdac141aa167eb42e880740bdb0c0ba3aa [file] [log] [blame]
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001// 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
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000028#include "v8.h"
29
30/*
31TODO(isolates): I incldue v8.h instead of these because we need Isolate and
32some classes (NativeAllocationChecker) are moved into isolate.h
ager@chromium.orgbeb25712010-11-29 08:02:25 +000033#include "../include/v8stdint.h"
34#include "globals.h"
35#include "checks.h"
36#include "allocation.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000037#include "allocation-inl.h"
ager@chromium.orgbeb25712010-11-29 08:02:25 +000038#include "utils.h"
39#include "list-inl.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000040#include "hashmap.h"
41*/
42
ager@chromium.orgbeb25712010-11-29 08:02:25 +000043#include "preparse-data.h"
44
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000045
ager@chromium.orgbeb25712010-11-29 08:02:25 +000046namespace v8 {
47namespace internal {
48
49// ----------------------------------------------------------------------------
50// FunctionLoggingParserRecorder
51
52FunctionLoggingParserRecorder::FunctionLoggingParserRecorder()
53 : function_store_(0),
54 is_recording_(true),
55 pause_count_(0) {
56 preamble_[PreparseDataConstants::kMagicOffset] =
57 PreparseDataConstants::kMagicNumber;
58 preamble_[PreparseDataConstants::kVersionOffset] =
59 PreparseDataConstants::kCurrentVersion;
60 preamble_[PreparseDataConstants::kHasErrorOffset] = false;
61 preamble_[PreparseDataConstants::kFunctionsSizeOffset] = 0;
62 preamble_[PreparseDataConstants::kSymbolCountOffset] = 0;
63 preamble_[PreparseDataConstants::kSizeOffset] = 0;
64 ASSERT_EQ(6, PreparseDataConstants::kHeaderSize);
65#ifdef DEBUG
66 prev_start_ = -1;
67#endif
68}
69
70
71void FunctionLoggingParserRecorder::LogMessage(int start_pos,
72 int end_pos,
73 const char* message,
74 const char* arg_opt) {
75 if (has_error()) return;
76 preamble_[PreparseDataConstants::kHasErrorOffset] = true;
77 function_store_.Reset();
78 STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0);
79 function_store_.Add(start_pos);
80 STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1);
81 function_store_.Add(end_pos);
82 STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2);
83 function_store_.Add((arg_opt == NULL) ? 0 : 1);
84 STATIC_ASSERT(PreparseDataConstants::kMessageTextPos == 3);
85 WriteString(CStrVector(message));
86 if (arg_opt) WriteString(CStrVector(arg_opt));
87 is_recording_ = false;
88}
89
90
91void FunctionLoggingParserRecorder::WriteString(Vector<const char> str) {
92 function_store_.Add(str.length());
93 for (int i = 0; i < str.length(); i++) {
94 function_store_.Add(str[i]);
95 }
96}
97
98// ----------------------------------------------------------------------------
99// PartialParserRecorder - Record both function entries and symbols.
100
101Vector<unsigned> PartialParserRecorder::ExtractData() {
102 int function_size = function_store_.size();
103 int total_size = PreparseDataConstants::kHeaderSize + function_size;
104 Vector<unsigned> data = Vector<unsigned>::New(total_size);
105 preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size;
106 preamble_[PreparseDataConstants::kSymbolCountOffset] = 0;
107 memcpy(data.start(), preamble_, sizeof(preamble_));
108 int symbol_start = PreparseDataConstants::kHeaderSize + function_size;
109 if (function_size > 0) {
110 function_store_.WriteTo(data.SubVector(PreparseDataConstants::kHeaderSize,
111 symbol_start));
112 }
113 return data;
114}
115
116
117// ----------------------------------------------------------------------------
118// CompleteParserRecorder - Record both function entries and symbols.
119
120CompleteParserRecorder::CompleteParserRecorder()
121 : FunctionLoggingParserRecorder(),
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000122 literal_chars_(0),
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000123 symbol_store_(0),
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000124 symbol_keys_(0),
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000125 symbol_table_(vector_compare),
126 symbol_id_(0) {
127}
128
129
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000130void CompleteParserRecorder::LogSymbol(int start,
131 int hash,
132 bool is_ascii,
133 Vector<const byte> literal_bytes) {
134 Key key = { is_ascii, literal_bytes };
135 HashMap::Entry* entry = symbol_table_.Lookup(&key, hash, true);
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000136 int id = static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
137 if (id == 0) {
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000138 // Copy literal contents for later comparison.
139 key.literal_bytes =
140 Vector<const byte>::cast(literal_chars_.AddBlock(literal_bytes));
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000141 // Put (symbol_id_ + 1) into entry and increment it.
142 id = ++symbol_id_;
143 entry->value = reinterpret_cast<void*>(id);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000144 Vector<Key> symbol = symbol_keys_.AddBlock(1, key);
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000145 entry->key = &symbol[0];
146 }
147 WriteNumber(id - 1);
148}
149
150
151Vector<unsigned> CompleteParserRecorder::ExtractData() {
152 int function_size = function_store_.size();
153 // Add terminator to symbols, then pad to unsigned size.
154 int symbol_size = symbol_store_.size();
155 int padding = sizeof(unsigned) - (symbol_size % sizeof(unsigned));
156 symbol_store_.AddBlock(padding, PreparseDataConstants::kNumberTerminator);
157 symbol_size += padding;
158 int total_size = PreparseDataConstants::kHeaderSize + function_size
159 + (symbol_size / sizeof(unsigned));
160 Vector<unsigned> data = Vector<unsigned>::New(total_size);
161 preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size;
162 preamble_[PreparseDataConstants::kSymbolCountOffset] = symbol_id_;
163 memcpy(data.start(), preamble_, sizeof(preamble_));
164 int symbol_start = PreparseDataConstants::kHeaderSize + function_size;
165 if (function_size > 0) {
166 function_store_.WriteTo(data.SubVector(PreparseDataConstants::kHeaderSize,
167 symbol_start));
168 }
169 if (!has_error()) {
170 symbol_store_.WriteTo(
171 Vector<byte>::cast(data.SubVector(symbol_start, total_size)));
172 }
173 return data;
174}
175
176
177void CompleteParserRecorder::WriteNumber(int number) {
178 ASSERT(number >= 0);
179
180 int mask = (1 << 28) - 1;
181 for (int i = 28; i > 0; i -= 7) {
182 if (number > mask) {
183 symbol_store_.Add(static_cast<byte>(number >> i) | 0x80u);
184 number &= mask;
185 }
186 mask >>= 7;
187 }
188 symbol_store_.Add(static_cast<byte>(number));
189}
190
191
192} } // namespace v8::internal.