v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
preparse-data.cc
Go to the documentation of this file.
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
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 #include "../include/v8stdint.h"
29 
30 #include "preparse-data-format.h"
31 #include "preparse-data.h"
32 
33 #include "checks.h"
34 #include "globals.h"
35 #include "hashmap.h"
36 
37 namespace v8 {
38 namespace internal {
39 
40 
41 template <typename Char>
42 static int vector_hash(Vector<const Char> string) {
43  int hash = 0;
44  for (int i = 0; i < string.length(); i++) {
45  int c = static_cast<int>(string[i]);
46  hash += c;
47  hash += (hash << 10);
48  hash ^= (hash >> 6);
49  }
50  return hash;
51 }
52 
53 
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;
64 }
65 
66 
68  : function_store_(0),
69  literal_chars_(0),
70  symbol_store_(0),
71  symbol_keys_(0),
72  string_table_(vector_compare),
73  symbol_id_(0) {
78  preamble_[PreparseDataConstants::kHasErrorOffset] = false;
83 #ifdef DEBUG
84  prev_start_ = -1;
85 #endif
86  should_log_symbols_ = true;
87 }
88 
89 
91  int end_pos,
92  const char* message,
93  const char* arg_opt) {
94  if (has_error()) return;
96  function_store_.Reset();
98  function_store_.Add(start_pos);
100  function_store_.Add(end_pos);
102  function_store_.Add((arg_opt == NULL) ? 0 : 1);
104  WriteString(CStrVector(message));
105  if (arg_opt != NULL) WriteString(CStrVector(arg_opt));
106  should_log_symbols_ = false;
107 }
108 
109 
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]);
114  }
115 }
116 
117 
119  Vector<const uint8_t> literal) {
121  int hash = vector_hash(literal);
122  LogSymbol(start, hash, true, literal);
123 }
124 
125 
127  Vector<const uint16_t> literal) {
129  int hash = vector_hash(literal);
130  LogSymbol(start, hash, false, Vector<const byte>::cast(literal));
131 }
132 
133 
134 void CompleteParserRecorder::LogSymbol(int start,
135  int hash,
136  bool is_one_byte,
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));
141  if (id == 0) {
142  // Copy literal contents for later comparison.
143  key.literal_bytes =
144  Vector<const byte>::cast(literal_chars_.AddBlock(literal_bytes));
145  // Put (symbol_id_ + 1) into entry and increment it.
146  id = ++symbol_id_;
147  entry->value = reinterpret_cast<void*>(id);
148  Vector<Key> symbol = symbol_keys_.AddBlock(1, key);
149  entry->key = &symbol[0];
150  }
151  WriteNumber(id - 1);
152 }
153 
154 
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) {
171  symbol_start));
172  }
173  if (!has_error()) {
174  symbol_store_.WriteTo(
175  Vector<byte>::cast(data.SubVector(symbol_start, total_size)));
176  }
177  return data;
178 }
179 
180 
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.
185  ASSERT(number >= 0);
186 
187  int mask = (1 << 28) - 1;
188  int i = 28;
189  // 26 million symbols ought to be enough for anybody.
190  ASSERT(number <= mask);
191  while (number < mask) {
192  mask >>= 7;
193  i -= 7;
194  }
195  while (i > 0) {
196  symbol_store_.Add(static_cast<byte>(number >> i) | 0x80u);
197  number &= mask;
198  mask >>= 7;
199  i -= 7;
200  }
201  ASSERT(number < (1 << 7));
202  symbol_store_.Add(static_cast<byte>(number));
203 }
204 
205 
206 } } // namespace v8::internal.
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
Vector< T > SubVector(int from, int to)
Definition: utils.h:412
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes number of stack frames inspected by the profiler percentage of ICs that must have type info to allow optimization extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long expose natives in global object expose freeBuffer extension expose gc extension under the specified name expose externalize string extension number of stack frames to capture disable builtin natives files print name of functions for which code is generated use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations always try to OSR functions trace optimize function deoptimization minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions trace debugging JSON request response trace out of bounds accesses to external arrays trace_js_array_abuse automatically set the debug break flag when debugger commands are in the queue abort by crashing maximum length of function source code printed in a stack trace max size of the new max size of the old max size of executable always perform global GCs print one trace line following each garbage collection do not print trace line after scavenger collection print statistics of the maximum memory committed for the heap in only print modified registers Don t break for ASM_UNIMPLEMENTED_BREAK macros print stack trace when an illegal exception is thrown randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot testing_bool_flag testing_int_flag string flag tmp file in which to serialize heap Print the time it takes to lazily compile hydrogen code stubs concurrent_recompilation concurrent_sweeping Print usage message
Definition: flags.cc:665
void Add(T value)
Definition: utils.h:622
#define ASSERT(condition)
Definition: checks.h:329
virtual void LogOneByteSymbol(int start, Vector< const uint8_t > literal)
Vector< T > AddBlock(int size, T initial_value)
Definition: utils.h:635
T * start() const
Definition: utils.h:426
STATIC_ASSERT(sizeof(CPURegister)==sizeof(Register))
static void MemCopy(void *dest, const void *src, size_t size)
Definition: platform.h:399
Entry * Lookup(void *key, uint32_t hash, bool insert, AllocationPolicy allocator=AllocationPolicy())
Definition: hashmap.h:131
int length() const
Definition: utils.h:420
static Vector< T > New(int length)
Definition: utils.h:406
virtual void LogTwoByteSymbol(int start, Vector< const uint16_t > literal)
Vector< const char > CStrVector(const char *data)
Definition: utils.h:574
static Vector< T > cast(Vector< S > input)
Definition: utils.h:477
static const unsigned char kNumberTerminator
virtual void LogMessage(int start, int end, const char *message, const char *argument_opt)
#define ASSERT_EQ(v1, v2)
Definition: checks.h:330
void WriteTo(Vector< T > destination)
Definition: utils.h:669
virtual void Reset()
Definition: utils-inl.h:37