v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
compilation-cache.h
Go to the documentation of this file.
1 // Copyright 2012 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_COMPILATION_CACHE_H_
29 #define V8_COMPILATION_CACHE_H_
30 
31 namespace v8 {
32 namespace internal {
33 
34 // The compilation cache consists of several generational sub-caches which uses
35 // this class as a base class. A sub-cache contains a compilation cache tables
36 // for each generation of the sub-cache. Since the same source code string has
37 // different compiled code for scripts and evals, we use separate sub-caches
38 // for different compilation modes, to avoid retrieving the wrong result.
40  public:
42  : isolate_(isolate),
43  generations_(generations) {
44  tables_ = NewArray<Object*>(generations);
45  }
46 
48 
49  // Index for the first generation in the cache.
50  static const int kFirstGeneration = 0;
51 
52  // Get the compilation cache tables for a specific generation.
54 
55  // Accessors for first generation.
57  return GetTable(kFirstGeneration);
58  }
60  ASSERT(kFirstGeneration < generations_);
61  tables_[kFirstGeneration] = *value;
62  }
63 
64  // Age the sub-cache by evicting the oldest generation and creating a new
65  // young generation.
66  void Age();
67 
68  // GC support.
69  void Iterate(ObjectVisitor* v);
70  void IterateFunctions(ObjectVisitor* v);
71 
72  // Clear this sub-cache evicting all its content.
73  void Clear();
74 
75  // Remove given shared function info from sub-cache.
76  void Remove(Handle<SharedFunctionInfo> function_info);
77 
78  // Number of generations in this sub-cache.
79  inline int generations() { return generations_; }
80 
81  protected:
82  Isolate* isolate() { return isolate_; }
83 
84  private:
85  Isolate* isolate_;
86  int generations_; // Number of generations.
87  Object** tables_; // Compilation cache tables - one for each generation.
88 
89  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache);
90 };
91 
92 
93 // Sub-cache for scripts.
95  public:
97 
99  Handle<Object> name,
100  int line_offset,
101  int column_offset,
102  Handle<Context> context);
103  void Put(Handle<String> source,
104  Handle<Context> context,
105  Handle<SharedFunctionInfo> function_info);
106 
107  private:
108  MUST_USE_RESULT MaybeObject* TryTablePut(
109  Handle<String> source,
110  Handle<Context> context,
111  Handle<SharedFunctionInfo> function_info);
112 
113  // Note: Returns a new hash table if operation results in expansion.
115  Handle<String> source,
116  Handle<Context> context,
117  Handle<SharedFunctionInfo> function_info);
118 
119  bool HasOrigin(Handle<SharedFunctionInfo> function_info,
120  Handle<Object> name,
121  int line_offset,
122  int column_offset);
123 
124  void* script_histogram_;
125  bool script_histogram_initialized_;
126 
127  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
128 };
129 
130 
131 // Sub-cache for eval scripts. Two caches for eval are used. One for eval calls
132 // in native contexts and one for eval calls in other contexts. The cache
133 // considers the following pieces of information when checking for matching
134 // entries:
135 // 1. The source string.
136 // 2. The shared function info of the calling function.
137 // 3. Whether the source should be compiled as strict code or as non-strict
138 // code.
139 // Note: Currently there are clients of CompileEval that always compile
140 // non-strict code even if the calling function is a strict mode function.
141 // More specifically these are the CompileString, DebugEvaluate and
142 // DebugEvaluateGlobal runtime functions.
143 // 4. The start position of the calling scope.
145  public:
147  : CompilationSubCache(isolate, generations) { }
148 
150  Handle<Context> context,
151  LanguageMode language_mode,
152  int scope_position);
153 
154  void Put(Handle<String> source,
155  Handle<Context> context,
156  Handle<SharedFunctionInfo> function_info,
157  int scope_position);
158 
159  private:
160  MUST_USE_RESULT MaybeObject* TryTablePut(
161  Handle<String> source,
162  Handle<Context> context,
163  Handle<SharedFunctionInfo> function_info,
164  int scope_position);
165 
166  // Note: Returns a new hash table if operation results in expansion.
168  Handle<String> source,
169  Handle<Context> context,
170  Handle<SharedFunctionInfo> function_info,
171  int scope_position);
172 
173  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
174 };
175 
176 
177 // Sub-cache for regular expressions.
179  public:
181  : CompilationSubCache(isolate, generations) { }
182 
184 
185  void Put(Handle<String> source,
187  Handle<FixedArray> data);
188  private:
189  MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source,
191  Handle<FixedArray> data);
192 
193  // Note: Returns a new hash table if operation results in expansion.
196  Handle<FixedArray> data);
197 
198  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
199 };
200 
201 
202 // The compilation cache keeps shared function infos for compiled
203 // scripts and evals. The shared function infos are looked up using
204 // the source string as the key. For regular expressions the
205 // compilation data is cached.
207  public:
208  // Finds the script shared function info for a source
209  // string. Returns an empty handle if the cache doesn't contain a
210  // script for the given source string with the right origin.
212  Handle<Object> name,
213  int line_offset,
214  int column_offset,
215  Handle<Context> context);
216 
217  // Finds the shared function info for a source string for eval in a
218  // given context. Returns an empty handle if the cache doesn't
219  // contain a script for the given source string.
221  Handle<Context> context,
222  bool is_global,
223  LanguageMode language_mode,
224  int scope_position);
225 
226  // Returns the regexp data associated with the given regexp if it
227  // is in cache, otherwise an empty handle.
230 
231  // Associate the (source, kind) pair to the shared function
232  // info. This may overwrite an existing mapping.
233  void PutScript(Handle<String> source,
234  Handle<Context> context,
235  Handle<SharedFunctionInfo> function_info);
236 
237  // Associate the (source, context->closure()->shared(), kind) triple
238  // with the shared function info. This may overwrite an existing mapping.
239  void PutEval(Handle<String> source,
240  Handle<Context> context,
241  bool is_global,
242  Handle<SharedFunctionInfo> function_info,
243  int scope_position);
244 
245  // Associate the (source, flags) pair to the given regexp data.
246  // This may overwrite an existing mapping.
247  void PutRegExp(Handle<String> source,
248  JSRegExp::Flags flags,
249  Handle<FixedArray> data);
250 
251  // Clear the cache - also used to initialize the cache at startup.
252  void Clear();
253 
254  // Remove given shared function info from all caches.
255  void Remove(Handle<SharedFunctionInfo> function_info);
256 
257  // GC support.
258  void Iterate(ObjectVisitor* v);
259  void IterateFunctions(ObjectVisitor* v);
260 
261  // Notify the cache that a mark-sweep garbage collection is about to
262  // take place. This is used to retire entries from the cache to
263  // avoid keeping them alive too long without using them.
264  void MarkCompactPrologue();
265 
266  // Enable/disable compilation cache. Used by debugger to disable compilation
267  // cache during debugging to make sure new scripts are always compiled.
268  void Enable();
269  void Disable();
270 
271  private:
272  explicit CompilationCache(Isolate* isolate);
273  ~CompilationCache();
274 
275  HashMap* EagerOptimizingSet();
276 
277  // The number of sub caches covering the different types to cache.
278  static const int kSubCacheCount = 4;
279 
280  bool IsEnabled() { return FLAG_compilation_cache && enabled_; }
281 
282  Isolate* isolate() { return isolate_; }
283 
284  Isolate* isolate_;
285 
286  CompilationCacheScript script_;
287  CompilationCacheEval eval_global_;
288  CompilationCacheEval eval_contextual_;
289  CompilationCacheRegExp reg_exp_;
290  CompilationSubCache* subcaches_[kSubCacheCount];
291 
292  // Current enable state of the compilation cache.
293  bool enabled_;
294 
295  friend class Isolate;
296 
298 };
299 
300 
301 } } // namespace v8::internal
302 
303 #endif // V8_COMPILATION_CACHE_H_
void Put(Handle< String > source, Handle< Context > context, Handle< SharedFunctionInfo > function_info, int scope_position)
void Put(Handle< String > source, Handle< Context > context, Handle< SharedFunctionInfo > function_info)
Handle< FixedArray > LookupRegExp(Handle< String > source, JSRegExp::Flags flags)
#define ASSERT(condition)
Definition: checks.h:270
void PutEval(Handle< String > source, Handle< Context > context, bool is_global, Handle< SharedFunctionInfo > function_info, int scope_position)
CompilationCacheEval(Isolate *isolate, int generations)
void SetFirstTable(Handle< CompilationCacheTable > value)
Handle< CompilationCacheTable > GetTable(int generation)
void PutScript(Handle< String > source, Handle< Context > context, Handle< SharedFunctionInfo > function_info)
Handle< SharedFunctionInfo > LookupEval(Handle< String > source, Handle< Context > context, bool is_global, LanguageMode language_mode, int scope_position)
#define MUST_USE_RESULT
Definition: globals.h:346
CompilationCacheRegExp(Isolate *isolate, int generations)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:307
Handle< SharedFunctionInfo > Lookup(Handle< String > source, Handle< Object > name, int line_offset, int column_offset, Handle< Context > context)
void Iterate(ObjectVisitor *v)
void PutRegExp(Handle< String > source, JSRegExp::Flags flags, Handle< FixedArray > data)
Handle< FixedArray > Lookup(Handle< String > source, JSRegExp::Flags flags)
CompilationSubCache(Isolate *isolate, int generations)
Handle< CompilationCacheTable > GetFirstTable()
Handle< SharedFunctionInfo > LookupScript(Handle< String > source, Handle< Object > name, int line_offset, int column_offset, Handle< Context > context)
void IterateFunctions(ObjectVisitor *v)
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit 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 SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available 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 MIPS FPU instructions if expose natives in global object expose gc extension number of stack frames to capture disable builtin natives files print a stack trace if an assertion failure occurs use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations prepare for turning on always opt minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions automatically set the debug break flag when debugger commands are in the queue always cause a debug break before aborting maximum length of function source code printed in a stack trace max size of the new generation(in kBytes)") DEFINE_int(max_old_space_size
CompilationCacheScript(Isolate *isolate, int generations)
void Remove(Handle< SharedFunctionInfo > function_info)
void DeleteArray(T *array)
Definition: allocation.h:91
Handle< SharedFunctionInfo > Lookup(Handle< String > source, Handle< Context > context, LanguageMode language_mode, int scope_position)
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit 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 SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available 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 MIPS FPU instructions if expose natives in global object expose gc extension number of stack frames to capture disable builtin natives files print a stack trace if an assertion failure occurs use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations prepare for turning on always opt minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions automatically set the debug break flag when debugger commands are in the queue always cause a debug break before aborting 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 more details following each garbage collection print amount of external allocated memory after each time it is adjusted flush code that we expect not to use again before full gc do incremental marking steps track object counts and memory usage use caching Perform compaction on every full GC Never perform compaction on full GC testing only Compact code space on full incremental collections Default seed for initializing random allows verbose printing trace parsing and preparsing Check icache flushes in ARM and MIPS simulator Stack alingment in bytes in print stack trace when throwing exceptions randomize hashes to avoid predictable hash Fixed seed to use to hash property activate a timer that switches between V8 threads testing_bool_flag float flag Seed used for threading test randomness A filename with extra code to be included in the Print usage including flags
Definition: flags.cc:495
void Remove(Handle< SharedFunctionInfo > function_info)
void IterateFunctions(ObjectVisitor *v)
void Put(Handle< String > source, JSRegExp::Flags flags, Handle< FixedArray > data)