blob: a96e50fa1061628b500b4d0ed2e47578c86ae090 [file] [log] [blame]
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001// 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#ifndef V8_PREPARSER_DATA_H_
29#define V8_PREPARSER_DATA_H_
30
31#include "hashmap.h"
32
33namespace v8 {
34namespace internal {
35
36// Generic and general data used by preparse data recorders and readers.
37
38class PreparseDataConstants : public AllStatic {
39 public:
40 // Layout and constants of the preparse data exchange format.
41 static const unsigned kMagicNumber = 0xBadDead;
42 static const unsigned kCurrentVersion = 5;
43
44 static const int kMagicOffset = 0;
45 static const int kVersionOffset = 1;
46 static const int kHasErrorOffset = 2;
47 static const int kFunctionsSizeOffset = 3;
48 static const int kSymbolCountOffset = 4;
49 static const int kSizeOffset = 5;
50 static const int kHeaderSize = 6;
51
52 // If encoding a message, the following positions are fixed.
53 static const int kMessageStartPos = 0;
54 static const int kMessageEndPos = 1;
55 static const int kMessageArgCountPos = 2;
56 static const int kMessageTextPos = 3;
57
58 static const byte kNumberTerminator = 0x80u;
59};
60
61
62// ----------------------------------------------------------------------------
63// ParserRecorder - Logging of preparser data.
64
65// Abstract interface for preparse data recorder.
66class ParserRecorder {
67 public:
68 ParserRecorder() { }
69 virtual ~ParserRecorder() { }
70
71 // Logs the scope and some details of a function literal in the source.
72 virtual void LogFunction(int start,
73 int end,
74 int literals,
75 int properties) = 0;
76
77 // Logs a symbol creation of a literal or identifier.
78 virtual void LogSymbol(int start, const char* symbol, int length) = 0;
79
80 // Logs an error message and marks the log as containing an error.
81 // Further logging will be ignored, and ExtractData will return a vector
82 // representing the error only.
83 virtual void LogMessage(int start,
84 int end,
85 const char* message,
86 const char* argument_opt) = 0;
87
88 virtual int function_position() = 0;
89
90 virtual int symbol_position() = 0;
91
92 virtual int symbol_ids() = 0;
93
94 virtual Vector<unsigned> ExtractData() = 0;
95
96 virtual void PauseRecording() = 0;
97
98 virtual void ResumeRecording() = 0;
99};
100
101
102// ----------------------------------------------------------------------------
103// FunctionLoggingParserRecorder - Record only function entries
104
105class FunctionLoggingParserRecorder : public ParserRecorder {
106 public:
107 FunctionLoggingParserRecorder();
108 virtual ~FunctionLoggingParserRecorder() {}
109
110 virtual void LogFunction(int start, int end, int literals, int properties) {
111 function_store_.Add(start);
112 function_store_.Add(end);
113 function_store_.Add(literals);
114 function_store_.Add(properties);
115 }
116
117 // Logs an error message and marks the log as containing an error.
118 // Further logging will be ignored, and ExtractData will return a vector
119 // representing the error only.
120 virtual void LogMessage(int start,
121 int end,
122 const char* message,
123 const char* argument_opt);
124
125 virtual int function_position() { return function_store_.size(); }
126
127
128 virtual Vector<unsigned> ExtractData() = 0;
129
130 virtual void PauseRecording() {
131 pause_count_++;
132 is_recording_ = false;
133 }
134
135 virtual void ResumeRecording() {
136 ASSERT(pause_count_ > 0);
137 if (--pause_count_ == 0) is_recording_ = !has_error();
138 }
139
140 protected:
141 bool has_error() {
142 return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]);
143 }
144
145 bool is_recording() {
146 return is_recording_;
147 }
148
149 void WriteString(Vector<const char> str);
150
151 Collector<unsigned> function_store_;
152 unsigned preamble_[PreparseDataConstants::kHeaderSize];
153 bool is_recording_;
154 int pause_count_;
155
156#ifdef DEBUG
157 int prev_start_;
158#endif
159};
160
161
162// ----------------------------------------------------------------------------
163// PartialParserRecorder - Record only function entries
164
165class PartialParserRecorder : public FunctionLoggingParserRecorder {
166 public:
167 PartialParserRecorder() : FunctionLoggingParserRecorder() { }
168 virtual void LogSymbol(int start, const char* symbol, int length) { }
169 virtual ~PartialParserRecorder() { }
170 virtual Vector<unsigned> ExtractData();
171 virtual int symbol_position() { return 0; }
172 virtual int symbol_ids() { return 0; }
173};
174
175
176// ----------------------------------------------------------------------------
177// CompleteParserRecorder - Record both function entries and symbols.
178
179class CompleteParserRecorder: public FunctionLoggingParserRecorder {
180 public:
181 CompleteParserRecorder();
182 virtual ~CompleteParserRecorder() { }
183
184 virtual void LogSymbol(int start, const char* symbol, int length);
185
186 virtual Vector<unsigned> ExtractData();
187
188 virtual int symbol_position() { return symbol_store_.size(); }
189 virtual int symbol_ids() { return symbol_id_; }
190
191 private:
192 static int vector_hash(Vector<const char> string) {
193 int hash = 0;
194 for (int i = 0; i < string.length(); i++) {
195 int c = string[i];
196 hash += c;
197 hash += (hash << 10);
198 hash ^= (hash >> 6);
199 }
200 return hash;
201 }
202
203 static bool vector_compare(void* a, void* b) {
204 Vector<const char>* string1 = reinterpret_cast<Vector<const char>* >(a);
205 Vector<const char>* string2 = reinterpret_cast<Vector<const char>* >(b);
206 int length = string1->length();
207 if (string2->length() != length) return false;
208 return memcmp(string1->start(), string2->start(), length) == 0;
209 }
210
211 // Write a non-negative number to the symbol store.
212 void WriteNumber(int number);
213
214 Collector<byte> symbol_store_;
215 Collector<Vector<const char> > symbol_entries_;
216 HashMap symbol_table_;
217 int symbol_id_;
218};
219
220
221} } // namespace v8::internal.
222
223#endif // V8_PREPARSER_DATA_H_