blob: 31f290968068ceb0610c383417808f109e07be7f [file] [log] [blame]
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001// Copyright 2011 the V8 project authors. All rights reserved.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000031namespace v8 {
32namespace internal {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000033
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000034class HashMap;
35
36// The compilation cache consists of several generational sub-caches which uses
37// this class as a base class. A sub-cache contains a compilation cache tables
38// for each generation of the sub-cache. Since the same source code string has
39// different compiled code for scripts and evals, we use separate sub-caches
40// for different compilation modes, to avoid retrieving the wrong result.
41class CompilationSubCache {
42 public:
43 CompilationSubCache(Isolate* isolate, int generations)
44 : isolate_(isolate),
45 generations_(generations) {
46 tables_ = NewArray<Object*>(generations);
47 }
48
49 ~CompilationSubCache() { DeleteArray(tables_); }
50
51 // Index for the first generation in the cache.
52 static const int kFirstGeneration = 0;
53
54 // Get the compilation cache tables for a specific generation.
55 Handle<CompilationCacheTable> GetTable(int generation);
56
57 // Accessors for first generation.
58 Handle<CompilationCacheTable> GetFirstTable() {
59 return GetTable(kFirstGeneration);
60 }
61 void SetFirstTable(Handle<CompilationCacheTable> value) {
62 ASSERT(kFirstGeneration < generations_);
63 tables_[kFirstGeneration] = *value;
64 }
65
66 // Age the sub-cache by evicting the oldest generation and creating a new
67 // young generation.
68 void Age();
69
70 // GC support.
71 void Iterate(ObjectVisitor* v);
72 void IterateFunctions(ObjectVisitor* v);
73
74 // Clear this sub-cache evicting all its content.
75 void Clear();
76
77 // Remove given shared function info from sub-cache.
78 void Remove(Handle<SharedFunctionInfo> function_info);
79
80 // Number of generations in this sub-cache.
81 inline int generations() { return generations_; }
82
83 protected:
84 Isolate* isolate() { return isolate_; }
85
86 private:
87 Isolate* isolate_;
88 int generations_; // Number of generations.
89 Object** tables_; // Compilation cache tables - one for each generation.
90
91 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache);
92};
93
94
95// Sub-cache for scripts.
96class CompilationCacheScript : public CompilationSubCache {
97 public:
98 CompilationCacheScript(Isolate* isolate, int generations);
99
100 Handle<SharedFunctionInfo> Lookup(Handle<String> source,
101 Handle<Object> name,
102 int line_offset,
103 int column_offset);
104 void Put(Handle<String> source, Handle<SharedFunctionInfo> function_info);
105
106 private:
107 MUST_USE_RESULT MaybeObject* TryTablePut(
108 Handle<String> source, Handle<SharedFunctionInfo> function_info);
109
110 // Note: Returns a new hash table if operation results in expansion.
111 Handle<CompilationCacheTable> TablePut(
112 Handle<String> source, Handle<SharedFunctionInfo> function_info);
113
114 bool HasOrigin(Handle<SharedFunctionInfo> function_info,
115 Handle<Object> name,
116 int line_offset,
117 int column_offset);
118
119 void* script_histogram_;
120 bool script_histogram_initialized_;
121
122 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
123};
124
125
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000126// Sub-cache for eval scripts. Two caches for eval are used. One for eval calls
127// in global contexts and one for eval calls in other contexts. The cache
128// considers the following pieces of information when checking for matching
129// entries:
130// 1. The source string.
131// 2. The shared function info of the calling function.
132// 3. Whether the source should be compiled as strict code or as non-strict
133// code.
134// Note: Currently there are clients of CompileEval that always compile
135// non-strict code even if the calling function is a strict mode function.
136// More specifically these are the CompileString, DebugEvaluate and
137// DebugEvaluateGlobal runtime functions.
138// 4. The start position of the calling scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139class CompilationCacheEval: public CompilationSubCache {
140 public:
141 CompilationCacheEval(Isolate* isolate, int generations)
142 : CompilationSubCache(isolate, generations) { }
143
144 Handle<SharedFunctionInfo> Lookup(Handle<String> source,
145 Handle<Context> context,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000146 LanguageMode language_mode,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000147 int scope_position);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148
149 void Put(Handle<String> source,
150 Handle<Context> context,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000151 Handle<SharedFunctionInfo> function_info,
152 int scope_position);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000153
154 private:
155 MUST_USE_RESULT MaybeObject* TryTablePut(
156 Handle<String> source,
157 Handle<Context> context,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000158 Handle<SharedFunctionInfo> function_info,
159 int scope_position);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000160
161 // Note: Returns a new hash table if operation results in expansion.
162 Handle<CompilationCacheTable> TablePut(
163 Handle<String> source,
164 Handle<Context> context,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000165 Handle<SharedFunctionInfo> function_info,
166 int scope_position);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000167
168 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
169};
170
171
172// Sub-cache for regular expressions.
173class CompilationCacheRegExp: public CompilationSubCache {
174 public:
175 CompilationCacheRegExp(Isolate* isolate, int generations)
176 : CompilationSubCache(isolate, generations) { }
177
178 Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
179
180 void Put(Handle<String> source,
181 JSRegExp::Flags flags,
182 Handle<FixedArray> data);
183 private:
184 MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source,
185 JSRegExp::Flags flags,
186 Handle<FixedArray> data);
187
188 // Note: Returns a new hash table if operation results in expansion.
189 Handle<CompilationCacheTable> TablePut(Handle<String> source,
190 JSRegExp::Flags flags,
191 Handle<FixedArray> data);
192
193 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
194};
195
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000196
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000197// The compilation cache keeps shared function infos for compiled
198// scripts and evals. The shared function infos are looked up using
199// the source string as the key. For regular expressions the
200// compilation data is cached.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000201class CompilationCache {
202 public:
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000203 // Finds the script shared function info for a source
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000204 // string. Returns an empty handle if the cache doesn't contain a
205 // script for the given source string with the right origin.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000206 Handle<SharedFunctionInfo> LookupScript(Handle<String> source,
207 Handle<Object> name,
208 int line_offset,
209 int column_offset);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000210
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000211 // Finds the shared function info for a source string for eval in a
ager@chromium.org381abbb2009-02-25 13:23:22 +0000212 // given context. Returns an empty handle if the cache doesn't
213 // contain a script for the given source string.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000214 Handle<SharedFunctionInfo> LookupEval(Handle<String> source,
215 Handle<Context> context,
216 bool is_global,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000217 LanguageMode language_mode,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000218 int scope_position);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000219
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000220 // Returns the regexp data associated with the given regexp if it
221 // is in cache, otherwise an empty handle.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000222 Handle<FixedArray> LookupRegExp(Handle<String> source,
223 JSRegExp::Flags flags);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000224
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000225 // Associate the (source, kind) pair to the shared function
226 // info. This may overwrite an existing mapping.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000227 void PutScript(Handle<String> source,
228 Handle<SharedFunctionInfo> function_info);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000229
230 // Associate the (source, context->closure()->shared(), kind) triple
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000231 // with the shared function info. This may overwrite an existing mapping.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000232 void PutEval(Handle<String> source,
233 Handle<Context> context,
234 bool is_global,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000235 Handle<SharedFunctionInfo> function_info,
236 int scope_position);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000237
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000238 // Associate the (source, flags) pair to the given regexp data.
239 // This may overwrite an existing mapping.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000240 void PutRegExp(Handle<String> source,
241 JSRegExp::Flags flags,
242 Handle<FixedArray> data);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000243
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000244 // Clear the cache - also used to initialize the cache at startup.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000245 void Clear();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000246
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000247 // Remove given shared function info from all caches.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000248 void Remove(Handle<SharedFunctionInfo> function_info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000249
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000250 // GC support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000251 void Iterate(ObjectVisitor* v);
252 void IterateFunctions(ObjectVisitor* v);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000253
254 // Notify the cache that a mark-sweep garbage collection is about to
255 // take place. This is used to retire entries from the cache to
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000256 // avoid keeping them alive too long without using them.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000257 void MarkCompactPrologue();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000258
259 // Enable/disable compilation cache. Used by debugger to disable compilation
260 // cache during debugging to make sure new scripts are always compiled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000261 void Enable();
262 void Disable();
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000263
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000264 private:
265 explicit CompilationCache(Isolate* isolate);
266 ~CompilationCache();
267
268 HashMap* EagerOptimizingSet();
269
270 // The number of sub caches covering the different types to cache.
271 static const int kSubCacheCount = 4;
272
273 bool IsEnabled() { return FLAG_compilation_cache && enabled_; }
274
275 Isolate* isolate() { return isolate_; }
276
277 Isolate* isolate_;
278
279 CompilationCacheScript script_;
280 CompilationCacheEval eval_global_;
281 CompilationCacheEval eval_contextual_;
282 CompilationCacheRegExp reg_exp_;
283 CompilationSubCache* subcaches_[kSubCacheCount];
284
285 // Current enable state of the compilation cache.
286 bool enabled_;
287
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000288 friend class Isolate;
289
290 DISALLOW_COPY_AND_ASSIGN(CompilationCache);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000291};
292
293
294} } // namespace v8::internal
295
296#endif // V8_COMPILATION_CACHE_H_