summaryrefslogtreecommitdiff
path: root/deps/v8/src/preparse-data.h
blob: cc82bcc62c0cf3cbce4016a621ca4a2f1e2cbfcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_PREPARSER_DATA_H_
#define V8_PREPARSER_DATA_H_

#include "hashmap.h"

namespace v8 {
namespace internal {

// Generic and general data used by preparse data recorders and readers.

class PreparseDataConstants : public AllStatic {
 public:
  // Layout and constants of the preparse data exchange format.
  static const unsigned kMagicNumber = 0xBadDead;
  static const unsigned kCurrentVersion = 5;

  static const int kMagicOffset = 0;
  static const int kVersionOffset = 1;
  static const int kHasErrorOffset = 2;
  static const int kFunctionsSizeOffset = 3;
  static const int kSymbolCountOffset = 4;
  static const int kSizeOffset = 5;
  static const int kHeaderSize = 6;

  // If encoding a message, the following positions are fixed.
  static const int kMessageStartPos = 0;
  static const int kMessageEndPos = 1;
  static const int kMessageArgCountPos = 2;
  static const int kMessageTextPos = 3;

  static const byte kNumberTerminator = 0x80u;
};


// ----------------------------------------------------------------------------
// ParserRecorder - Logging of preparser data.

// Abstract interface for preparse data recorder.
class ParserRecorder {
 public:
  ParserRecorder() { }
  virtual ~ParserRecorder() { }

  // Logs the scope and some details of a function literal in the source.
  virtual void LogFunction(int start,
                           int end,
                           int literals,
                           int properties) = 0;

  // Logs a symbol creation of a literal or identifier.
  virtual void LogAsciiSymbol(int start, Vector<const char> literal) { }
  virtual void LogUC16Symbol(int start, Vector<const uc16> literal) { }

  // Logs an error message and marks the log as containing an error.
  // Further logging will be ignored, and ExtractData will return a vector
  // representing the error only.
  virtual void LogMessage(int start,
                          int end,
                          const char* message,
                          const char* argument_opt) = 0;

  virtual int function_position() = 0;

  virtual int symbol_position() = 0;

  virtual int symbol_ids() = 0;

  virtual Vector<unsigned> ExtractData() = 0;

  virtual void PauseRecording() = 0;

  virtual void ResumeRecording() = 0;
};


// ----------------------------------------------------------------------------
// FunctionLoggingParserRecorder - Record only function entries

class FunctionLoggingParserRecorder : public ParserRecorder {
 public:
  FunctionLoggingParserRecorder();
  virtual ~FunctionLoggingParserRecorder() {}

  virtual void LogFunction(int start, int end, int literals, int properties) {
    function_store_.Add(start);
    function_store_.Add(end);
    function_store_.Add(literals);
    function_store_.Add(properties);
  }

  // Logs an error message and marks the log as containing an error.
  // Further logging will be ignored, and ExtractData will return a vector
  // representing the error only.
  virtual void LogMessage(int start,
                          int end,
                          const char* message,
                          const char* argument_opt);

  virtual int function_position() { return function_store_.size(); }


  virtual Vector<unsigned> ExtractData() = 0;

  virtual void PauseRecording() {
    pause_count_++;
    is_recording_ = false;
  }

  virtual void ResumeRecording() {
    ASSERT(pause_count_ > 0);
    if (--pause_count_ == 0) is_recording_ = !has_error();
  }

 protected:
  bool has_error() {
    return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]);
  }

  bool is_recording() {
    return is_recording_;
  }

  void WriteString(Vector<const char> str);

  Collector<unsigned> function_store_;
  unsigned preamble_[PreparseDataConstants::kHeaderSize];
  bool is_recording_;
  int pause_count_;

#ifdef DEBUG
  int prev_start_;
#endif
};


// ----------------------------------------------------------------------------
// PartialParserRecorder - Record only function entries

class PartialParserRecorder : public FunctionLoggingParserRecorder {
 public:
  PartialParserRecorder() : FunctionLoggingParserRecorder() { }
  virtual void LogAsciiSymbol(int start, Vector<const char> literal) { }
  virtual void LogUC16Symbol(int start, Vector<const uc16> literal) { }
  virtual ~PartialParserRecorder() { }
  virtual Vector<unsigned> ExtractData();
  virtual int symbol_position() { return 0; }
  virtual int symbol_ids() { return 0; }
};


// ----------------------------------------------------------------------------
// CompleteParserRecorder -  Record both function entries and symbols.

class CompleteParserRecorder: public FunctionLoggingParserRecorder {
 public:
  CompleteParserRecorder();
  virtual ~CompleteParserRecorder() { }

  virtual void LogAsciiSymbol(int start, Vector<const char> literal) {
    if (!is_recording_) return;
    int hash = vector_hash(literal);
    LogSymbol(start, hash, true, Vector<const byte>::cast(literal));
  }

  virtual void LogUC16Symbol(int start, Vector<const uc16> literal) {
    if (!is_recording_) return;
    int hash = vector_hash(literal);
    LogSymbol(start, hash, false, Vector<const byte>::cast(literal));
  }

  virtual Vector<unsigned> ExtractData();

  virtual int symbol_position() { return symbol_store_.size(); }
  virtual int symbol_ids() { return symbol_id_; }

 private:
  struct Key {
    bool is_ascii;
    Vector<const byte> literal_bytes;
  };

  virtual void LogSymbol(int start,
                         int hash,
                         bool is_ascii,
                         Vector<const byte> literal);

  template <typename Char>
  static int vector_hash(Vector<const Char> string) {
    int hash = 0;
    for (int i = 0; i < string.length(); i++) {
      int c = static_cast<int>(string[i]);
      hash += c;
      hash += (hash << 10);
      hash ^= (hash >> 6);
    }
    return hash;
  }

  static bool vector_compare(void* a, void* b) {
    Key* string1 = reinterpret_cast<Key*>(a);
    Key* string2 = reinterpret_cast<Key*>(b);
    if (string1->is_ascii != string2->is_ascii) return false;
    int length = string1->literal_bytes.length();
    if (string2->literal_bytes.length() != length) return false;
    return memcmp(string1->literal_bytes.start(),
                  string2->literal_bytes.start(), length) == 0;
  }

  // Write a non-negative number to the symbol store.
  void WriteNumber(int number);

  Collector<byte> literal_chars_;
  Collector<byte> symbol_store_;
  Collector<Key> symbol_keys_;
  HashMap symbol_table_;
  int symbol_id_;
};


} }  // namespace v8::internal.

#endif  // V8_PREPARSER_DATA_H_