• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
39 class CompilationSubCache {
40  public:
CompilationSubCache(Isolate * isolate,int generations)41   CompilationSubCache(Isolate* isolate, int generations)
42       : isolate_(isolate),
43         generations_(generations) {
44     tables_ = NewArray<Object*>(generations);
45   }
46 
~CompilationSubCache()47   ~CompilationSubCache() { DeleteArray(tables_); }
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.
53   Handle<CompilationCacheTable> GetTable(int generation);
54 
55   // Accessors for first generation.
GetFirstTable()56   Handle<CompilationCacheTable> GetFirstTable() {
57     return GetTable(kFirstGeneration);
58   }
SetFirstTable(Handle<CompilationCacheTable> value)59   void SetFirstTable(Handle<CompilationCacheTable> value) {
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.
generations()79   inline int generations() { return generations_; }
80 
81  protected:
isolate()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.
94 class CompilationCacheScript : public CompilationSubCache {
95  public:
96   CompilationCacheScript(Isolate* isolate, int generations);
97 
98   Handle<SharedFunctionInfo> Lookup(Handle<String> source,
99                                     Handle<Object> name,
100                                     int line_offset,
101                                     int column_offset);
102   void Put(Handle<String> source, Handle<SharedFunctionInfo> function_info);
103 
104  private:
105   MUST_USE_RESULT MaybeObject* TryTablePut(
106       Handle<String> source, Handle<SharedFunctionInfo> function_info);
107 
108   // Note: Returns a new hash table if operation results in expansion.
109   Handle<CompilationCacheTable> TablePut(
110       Handle<String> source, Handle<SharedFunctionInfo> function_info);
111 
112   bool HasOrigin(Handle<SharedFunctionInfo> function_info,
113                  Handle<Object> name,
114                  int line_offset,
115                  int column_offset);
116 
117   void* script_histogram_;
118   bool script_histogram_initialized_;
119 
120   DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
121 };
122 
123 
124 // Sub-cache for eval scripts. Two caches for eval are used. One for eval calls
125 // in global contexts and one for eval calls in other contexts. The cache
126 // considers the following pieces of information when checking for matching
127 // entries:
128 // 1. The source string.
129 // 2. The shared function info of the calling function.
130 // 3. Whether the source should be compiled as strict code or as non-strict
131 //    code.
132 //    Note: Currently there are clients of CompileEval that always compile
133 //    non-strict code even if the calling function is a strict mode function.
134 //    More specifically these are the CompileString, DebugEvaluate and
135 //    DebugEvaluateGlobal runtime functions.
136 // 4. The start position of the calling scope.
137 class CompilationCacheEval: public CompilationSubCache {
138  public:
CompilationCacheEval(Isolate * isolate,int generations)139   CompilationCacheEval(Isolate* isolate, int generations)
140       : CompilationSubCache(isolate, generations) { }
141 
142   Handle<SharedFunctionInfo> Lookup(Handle<String> source,
143                                     Handle<Context> context,
144                                     LanguageMode language_mode,
145                                     int scope_position);
146 
147   void Put(Handle<String> source,
148            Handle<Context> context,
149            Handle<SharedFunctionInfo> function_info,
150            int scope_position);
151 
152  private:
153   MUST_USE_RESULT MaybeObject* TryTablePut(
154       Handle<String> source,
155       Handle<Context> context,
156       Handle<SharedFunctionInfo> function_info,
157       int scope_position);
158 
159   // Note: Returns a new hash table if operation results in expansion.
160   Handle<CompilationCacheTable> TablePut(
161       Handle<String> source,
162       Handle<Context> context,
163       Handle<SharedFunctionInfo> function_info,
164       int scope_position);
165 
166   DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
167 };
168 
169 
170 // Sub-cache for regular expressions.
171 class CompilationCacheRegExp: public CompilationSubCache {
172  public:
CompilationCacheRegExp(Isolate * isolate,int generations)173   CompilationCacheRegExp(Isolate* isolate, int generations)
174       : CompilationSubCache(isolate, generations) { }
175 
176   Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
177 
178   void Put(Handle<String> source,
179            JSRegExp::Flags flags,
180            Handle<FixedArray> data);
181  private:
182   MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source,
183                                       JSRegExp::Flags flags,
184                                       Handle<FixedArray> data);
185 
186   // Note: Returns a new hash table if operation results in expansion.
187   Handle<CompilationCacheTable> TablePut(Handle<String> source,
188                                          JSRegExp::Flags flags,
189                                          Handle<FixedArray> data);
190 
191   DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
192 };
193 
194 
195 // The compilation cache keeps shared function infos for compiled
196 // scripts and evals. The shared function infos are looked up using
197 // the source string as the key. For regular expressions the
198 // compilation data is cached.
199 class CompilationCache {
200  public:
201   // Finds the script shared function info for a source
202   // string. Returns an empty handle if the cache doesn't contain a
203   // script for the given source string with the right origin.
204   Handle<SharedFunctionInfo> LookupScript(Handle<String> source,
205                                           Handle<Object> name,
206                                           int line_offset,
207                                           int column_offset);
208 
209   // Finds the shared function info for a source string for eval in a
210   // given context.  Returns an empty handle if the cache doesn't
211   // contain a script for the given source string.
212   Handle<SharedFunctionInfo> LookupEval(Handle<String> source,
213                                         Handle<Context> context,
214                                         bool is_global,
215                                         LanguageMode language_mode,
216                                         int scope_position);
217 
218   // Returns the regexp data associated with the given regexp if it
219   // is in cache, otherwise an empty handle.
220   Handle<FixedArray> LookupRegExp(Handle<String> source,
221                                   JSRegExp::Flags flags);
222 
223   // Associate the (source, kind) pair to the shared function
224   // info. This may overwrite an existing mapping.
225   void PutScript(Handle<String> source,
226                  Handle<SharedFunctionInfo> function_info);
227 
228   // Associate the (source, context->closure()->shared(), kind) triple
229   // with the shared function info. This may overwrite an existing mapping.
230   void PutEval(Handle<String> source,
231                Handle<Context> context,
232                bool is_global,
233                Handle<SharedFunctionInfo> function_info,
234                int scope_position);
235 
236   // Associate the (source, flags) pair to the given regexp data.
237   // This may overwrite an existing mapping.
238   void PutRegExp(Handle<String> source,
239                  JSRegExp::Flags flags,
240                  Handle<FixedArray> data);
241 
242   // Clear the cache - also used to initialize the cache at startup.
243   void Clear();
244 
245   // Remove given shared function info from all caches.
246   void Remove(Handle<SharedFunctionInfo> function_info);
247 
248   // GC support.
249   void Iterate(ObjectVisitor* v);
250   void IterateFunctions(ObjectVisitor* v);
251 
252   // Notify the cache that a mark-sweep garbage collection is about to
253   // take place. This is used to retire entries from the cache to
254   // avoid keeping them alive too long without using them.
255   void MarkCompactPrologue();
256 
257   // Enable/disable compilation cache. Used by debugger to disable compilation
258   // cache during debugging to make sure new scripts are always compiled.
259   void Enable();
260   void Disable();
261 
262  private:
263   explicit CompilationCache(Isolate* isolate);
264   ~CompilationCache();
265 
266   HashMap* EagerOptimizingSet();
267 
268   // The number of sub caches covering the different types to cache.
269   static const int kSubCacheCount = 4;
270 
IsEnabled()271   bool IsEnabled() { return FLAG_compilation_cache && enabled_; }
272 
isolate()273   Isolate* isolate() { return isolate_; }
274 
275   Isolate* isolate_;
276 
277   CompilationCacheScript script_;
278   CompilationCacheEval eval_global_;
279   CompilationCacheEval eval_contextual_;
280   CompilationCacheRegExp reg_exp_;
281   CompilationSubCache* subcaches_[kSubCacheCount];
282 
283   // Current enable state of the compilation cache.
284   bool enabled_;
285 
286   friend class Isolate;
287 
288   DISALLOW_COPY_AND_ASSIGN(CompilationCache);
289 };
290 
291 
292 } }  // namespace v8::internal
293 
294 #endif  // V8_COMPILATION_CACHE_H_
295