blob: 551d2e845582bdb324ac3e7c67ae9fd3788281aa [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002// 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 Murdoch257744e2011-11-30 15:57:28 +000028#ifndef V8_PREPARSE_DATA_H_
29#define V8_PREPARSE_DATA_H_
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080030
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080032#include "hashmap.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000033#include "utils-inl.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080034
35namespace v8 {
36namespace internal {
37
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080038// ----------------------------------------------------------------------------
39// ParserRecorder - Logging of preparser data.
40
41// Abstract interface for preparse data recorder.
42class 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 Block9fac8402011-05-12 15:51:54 +010054 virtual void LogAsciiSymbol(int start, Vector<const char> literal) { }
55 virtual void LogUC16Symbol(int start, Vector<const uc16> literal) { }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080056
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
82class 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
142class PartialParserRecorder : public FunctionLoggingParserRecorder {
143 public:
144 PartialParserRecorder() : FunctionLoggingParserRecorder() { }
Steve Block9fac8402011-05-12 15:51:54 +0100145 virtual void LogAsciiSymbol(int start, Vector<const char> literal) { }
146 virtual void LogUC16Symbol(int start, Vector<const uc16> literal) { }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800147 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
157class CompleteParserRecorder: public FunctionLoggingParserRecorder {
158 public:
159 CompleteParserRecorder();
160 virtual ~CompleteParserRecorder() { }
161
Steve Block9fac8402011-05-12 15:51:54 +0100162 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) Wang8a31eba2010-12-06 19:01:33 -0800173
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 Block9fac8402011-05-12 15:51:54 +0100180 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) Wang8a31eba2010-12-06 19:01:33 -0800192 int hash = 0;
193 for (int i = 0; i < string.length(); i++) {
Steve Block9fac8402011-05-12 15:51:54 +0100194 int c = static_cast<int>(string[i]);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800195 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 Block9fac8402011-05-12 15:51:54 +0100203 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) Wang8a31eba2010-12-06 19:01:33 -0800210 }
211
212 // Write a non-negative number to the symbol store.
213 void WriteNumber(int number);
214
Steve Block9fac8402011-05-12 15:51:54 +0100215 Collector<byte> literal_chars_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800216 Collector<byte> symbol_store_;
Steve Block9fac8402011-05-12 15:51:54 +0100217 Collector<Key> symbol_keys_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800218 HashMap symbol_table_;
219 int symbol_id_;
220};
221
222
223} } // namespace v8::internal.
224
Ben Murdoch257744e2011-11-30 15:57:28 +0000225#endif // V8_PREPARSE_DATA_H_