blob: 2f2fbadb2e5904cff99896bb38b7cbefb34e3d35 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
31namespace v8 {
32namespace internal {
33
Steve Block44f0eee2011-05-26 01:26:41 +010034// 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.
39class CompilationSubCache {
40 public:
41 CompilationSubCache(Isolate* isolate, int generations)
42 : isolate_(isolate),
43 generations_(generations) {
44 tables_ = NewArray<Object*>(generations);
45 }
46
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.
56 Handle<CompilationCacheTable> GetFirstTable() {
57 return GetTable(kFirstGeneration);
58 }
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.
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.
94class 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
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100124// 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.
Steve Block44f0eee2011-05-26 01:26:41 +0100137class CompilationCacheEval: public CompilationSubCache {
138 public:
139 CompilationCacheEval(Isolate* isolate, int generations)
140 : CompilationSubCache(isolate, generations) { }
141
142 Handle<SharedFunctionInfo> Lookup(Handle<String> source,
143 Handle<Context> context,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100144 LanguageMode language_mode,
145 int scope_position);
Steve Block44f0eee2011-05-26 01:26:41 +0100146
147 void Put(Handle<String> source,
148 Handle<Context> context,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100149 Handle<SharedFunctionInfo> function_info,
150 int scope_position);
Steve Block44f0eee2011-05-26 01:26:41 +0100151
152 private:
153 MUST_USE_RESULT MaybeObject* TryTablePut(
154 Handle<String> source,
155 Handle<Context> context,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100156 Handle<SharedFunctionInfo> function_info,
157 int scope_position);
Steve Block44f0eee2011-05-26 01:26:41 +0100158
159 // Note: Returns a new hash table if operation results in expansion.
160 Handle<CompilationCacheTable> TablePut(
161 Handle<String> source,
162 Handle<Context> context,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100163 Handle<SharedFunctionInfo> function_info,
164 int scope_position);
Steve Block44f0eee2011-05-26 01:26:41 +0100165
166 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
167};
168
169
170// Sub-cache for regular expressions.
171class CompilationCacheRegExp: public CompilationSubCache {
172 public:
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
Steve Blocka7e24c12009-10-30 11:49:00 +0000194
Steve Block6ded16b2010-05-10 14:33:55 +0100195// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +0000199class CompilationCache {
200 public:
Steve Block6ded16b2010-05-10 14:33:55 +0100201 // Finds the script shared function info for a source
Steve Blocka7e24c12009-10-30 11:49:00 +0000202 // string. Returns an empty handle if the cache doesn't contain a
203 // script for the given source string with the right origin.
Steve Block44f0eee2011-05-26 01:26:41 +0100204 Handle<SharedFunctionInfo> LookupScript(Handle<String> source,
205 Handle<Object> name,
206 int line_offset,
207 int column_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +0000208
Steve Block6ded16b2010-05-10 14:33:55 +0100209 // Finds the shared function info for a source string for eval in a
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 // given context. Returns an empty handle if the cache doesn't
211 // contain a script for the given source string.
Steve Block44f0eee2011-05-26 01:26:41 +0100212 Handle<SharedFunctionInfo> LookupEval(Handle<String> source,
213 Handle<Context> context,
214 bool is_global,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100215 LanguageMode language_mode,
216 int scope_position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000217
218 // Returns the regexp data associated with the given regexp if it
219 // is in cache, otherwise an empty handle.
Steve Block44f0eee2011-05-26 01:26:41 +0100220 Handle<FixedArray> LookupRegExp(Handle<String> source,
221 JSRegExp::Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000222
Steve Block6ded16b2010-05-10 14:33:55 +0100223 // Associate the (source, kind) pair to the shared function
224 // info. This may overwrite an existing mapping.
Steve Block44f0eee2011-05-26 01:26:41 +0100225 void PutScript(Handle<String> source,
226 Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000227
228 // Associate the (source, context->closure()->shared(), kind) triple
Steve Block6ded16b2010-05-10 14:33:55 +0100229 // with the shared function info. This may overwrite an existing mapping.
Steve Block44f0eee2011-05-26 01:26:41 +0100230 void PutEval(Handle<String> source,
231 Handle<Context> context,
232 bool is_global,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100233 Handle<SharedFunctionInfo> function_info,
234 int scope_position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000235
236 // Associate the (source, flags) pair to the given regexp data.
237 // This may overwrite an existing mapping.
Steve Block44f0eee2011-05-26 01:26:41 +0100238 void PutRegExp(Handle<String> source,
239 JSRegExp::Flags flags,
240 Handle<FixedArray> data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000241
242 // Clear the cache - also used to initialize the cache at startup.
Steve Block44f0eee2011-05-26 01:26:41 +0100243 void Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +0000244
Ben Murdochb0fe1622011-05-05 13:52:32 +0100245 // Remove given shared function info from all caches.
Steve Block44f0eee2011-05-26 01:26:41 +0100246 void Remove(Handle<SharedFunctionInfo> function_info);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100247
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 // GC support.
Steve Block44f0eee2011-05-26 01:26:41 +0100249 void Iterate(ObjectVisitor* v);
250 void IterateFunctions(ObjectVisitor* v);
Steve Blocka7e24c12009-10-30 11:49:00 +0000251
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.
Steve Block44f0eee2011-05-26 01:26:41 +0100255 void MarkCompactPrologue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000256
257 // Enable/disable compilation cache. Used by debugger to disable compilation
258 // cache during debugging to make sure new scripts are always compiled.
Steve Block44f0eee2011-05-26 01:26:41 +0100259 void Enable();
260 void Disable();
Ben Murdoch589d6972011-11-30 16:04:58 +0000261
Steve Block44f0eee2011-05-26 01:26:41 +0100262 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
271 bool IsEnabled() { return FLAG_compilation_cache && enabled_; }
272
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
Steve Block44f0eee2011-05-26 01:26:41 +0100286 friend class Isolate;
287
288 DISALLOW_COPY_AND_ASSIGN(CompilationCache);
Steve Blocka7e24c12009-10-30 11:49:00 +0000289};
290
291
292} } // namespace v8::internal
293
294#endif // V8_COMPILATION_CACHE_H_