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
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.
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.
28 #ifndef V8_COMPILATION_CACHE_H_
29 #define V8_COMPILATION_CACHE_H_
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 {
41 CompilationSubCache(Isolate* isolate, int generations)
43 generations_(generations) {
44 tables_ = NewArray<Object*>(generations);
47 ~CompilationSubCache() { DeleteArray(tables_); }
49 // Index for the first generation in the cache.
50 static const int kFirstGeneration = 0;
52 // Get the compilation cache tables for a specific generation.
53 Handle<CompilationCacheTable> GetTable(int generation);
55 // Accessors for first generation.
56 Handle<CompilationCacheTable> GetFirstTable() {
57 return GetTable(kFirstGeneration);
59 void SetFirstTable(Handle<CompilationCacheTable> value) {
60 ASSERT(kFirstGeneration < generations_);
61 tables_[kFirstGeneration] = *value;
64 // Age the sub-cache by evicting the oldest generation and creating a new
69 void Iterate(ObjectVisitor* v);
70 void IterateFunctions(ObjectVisitor* v);
72 // Clear this sub-cache evicting all its content.
75 // Remove given shared function info from sub-cache.
76 void Remove(Handle<SharedFunctionInfo> function_info);
78 // Number of generations in this sub-cache.
79 inline int generations() { return generations_; }
82 Isolate* isolate() { return isolate_; }
86 int generations_; // Number of generations.
87 Object** tables_; // Compilation cache tables - one for each generation.
89 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache);
93 // Sub-cache for scripts.
94 class CompilationCacheScript : public CompilationSubCache {
96 CompilationCacheScript(Isolate* isolate, int generations);
98 Handle<SharedFunctionInfo> Lookup(Handle<String> source,
102 void Put(Handle<String> source, Handle<SharedFunctionInfo> function_info);
105 MUST_USE_RESULT MaybeObject* TryTablePut(
106 Handle<String> source, Handle<SharedFunctionInfo> function_info);
108 // Note: Returns a new hash table if operation results in expansion.
109 Handle<CompilationCacheTable> TablePut(
110 Handle<String> source, Handle<SharedFunctionInfo> function_info);
112 bool HasOrigin(Handle<SharedFunctionInfo> function_info,
117 void* script_histogram_;
118 bool script_histogram_initialized_;
120 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
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
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
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 {
139 CompilationCacheEval(Isolate* isolate, int generations)
140 : CompilationSubCache(isolate, generations) { }
142 Handle<SharedFunctionInfo> Lookup(Handle<String> source,
143 Handle<Context> context,
144 LanguageMode language_mode,
147 void Put(Handle<String> source,
148 Handle<Context> context,
149 Handle<SharedFunctionInfo> function_info,
153 MUST_USE_RESULT MaybeObject* TryTablePut(
154 Handle<String> source,
155 Handle<Context> context,
156 Handle<SharedFunctionInfo> function_info,
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,
166 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
170 // Sub-cache for regular expressions.
171 class CompilationCacheRegExp: public CompilationSubCache {
173 CompilationCacheRegExp(Isolate* isolate, int generations)
174 : CompilationSubCache(isolate, generations) { }
176 Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
178 void Put(Handle<String> source,
179 JSRegExp::Flags flags,
180 Handle<FixedArray> data);
182 MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source,
183 JSRegExp::Flags flags,
184 Handle<FixedArray> data);
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);
191 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
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 {
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,
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,
215 LanguageMode language_mode,
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);
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);
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,
233 Handle<SharedFunctionInfo> function_info,
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);
242 // Clear the cache - also used to initialize the cache at startup.
245 // Remove given shared function info from all caches.
246 void Remove(Handle<SharedFunctionInfo> function_info);
249 void Iterate(ObjectVisitor* v);
250 void IterateFunctions(ObjectVisitor* v);
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();
257 // Enable/disable compilation cache. Used by debugger to disable compilation
258 // cache during debugging to make sure new scripts are always compiled.
263 explicit CompilationCache(Isolate* isolate);
266 HashMap* EagerOptimizingSet();
268 // The number of sub caches covering the different types to cache.
269 static const int kSubCacheCount = 4;
271 bool IsEnabled() { return FLAG_compilation_cache && enabled_; }
273 Isolate* isolate() { return isolate_; }
277 CompilationCacheScript script_;
278 CompilationCacheEval eval_global_;
279 CompilationCacheEval eval_contextual_;
280 CompilationCacheRegExp reg_exp_;
281 CompilationSubCache* subcaches_[kSubCacheCount];
283 // Current enable state of the compilation cache.
286 friend class Isolate;
288 DISALLOW_COPY_AND_ASSIGN(CompilationCache);
292 } } // namespace v8::internal
294 #endif // V8_COMPILATION_CACHE_H_