Line data Source code
1 : // Copyright 2012 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_COMPILATION_CACHE_H_
6 : #define V8_COMPILATION_CACHE_H_
7 :
8 : #include "src/allocation.h"
9 : #include "src/objects/compilation-cache.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 : template <typename T>
15 : class Handle;
16 :
17 : class RootVisitor;
18 :
19 : // The compilation cache consists of several generational sub-caches which uses
20 : // this class as a base class. A sub-cache contains a compilation cache tables
21 : // for each generation of the sub-cache. Since the same source code string has
22 : // different compiled code for scripts and evals, we use separate sub-caches
23 : // for different compilation modes, to avoid retrieving the wrong result.
24 : class CompilationSubCache {
25 : public:
26 : CompilationSubCache(Isolate* isolate, int generations)
27 : : isolate_(isolate),
28 243128 : generations_(generations) {
29 243128 : tables_ = NewArray<Object*>(generations);
30 : }
31 :
32 237140 : ~CompilationSubCache() { DeleteArray(tables_); }
33 :
34 : // Index for the first generation in the cache.
35 : static const int kFirstGeneration = 0;
36 :
37 : // Get the compilation cache tables for a specific generation.
38 : Handle<CompilationCacheTable> GetTable(int generation);
39 :
40 : // Accessors for first generation.
41 : Handle<CompilationCacheTable> GetFirstTable() {
42 2338483 : return GetTable(kFirstGeneration);
43 : }
44 : void SetFirstTable(Handle<CompilationCacheTable> value) {
45 : DCHECK(kFirstGeneration < generations_);
46 2338483 : tables_[kFirstGeneration] = *value;
47 : }
48 :
49 : // Age the sub-cache by evicting the oldest generation and creating a new
50 : // young generation.
51 : void Age();
52 :
53 : // GC support.
54 : void Iterate(RootVisitor* v);
55 : void IterateFunctions(ObjectVisitor* v);
56 :
57 : // Clear this sub-cache evicting all its content.
58 : void Clear();
59 :
60 : // Remove given shared function info from sub-cache.
61 : void Remove(Handle<SharedFunctionInfo> function_info);
62 :
63 : // Number of generations in this sub-cache.
64 : inline int generations() { return generations_; }
65 :
66 : protected:
67 : Isolate* isolate() { return isolate_; }
68 :
69 : private:
70 : Isolate* isolate_;
71 : int generations_; // Number of generations.
72 : Object** tables_; // Compilation cache tables - one for each generation.
73 :
74 : DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache);
75 : };
76 :
77 :
78 : // Sub-cache for scripts.
79 : class CompilationCacheScript : public CompilationSubCache {
80 : public:
81 : explicit CompilationCacheScript(Isolate* isolate);
82 :
83 : InfoVectorPair Lookup(Handle<String> source, Handle<Object> name,
84 : int line_offset, int column_offset,
85 : ScriptOriginOptions resource_options,
86 : Handle<Context> context, LanguageMode language_mode);
87 :
88 : void Put(Handle<String> source, Handle<Context> context,
89 : LanguageMode language_mode, Handle<SharedFunctionInfo> function_info,
90 : Handle<Cell> literals);
91 :
92 : private:
93 : bool HasOrigin(Handle<SharedFunctionInfo> function_info, Handle<Object> name,
94 : int line_offset, int column_offset,
95 : ScriptOriginOptions resource_options);
96 :
97 : DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
98 : };
99 :
100 :
101 : // Sub-cache for eval scripts. Two caches for eval are used. One for eval calls
102 : // in native contexts and one for eval calls in other contexts. The cache
103 : // considers the following pieces of information when checking for matching
104 : // entries:
105 : // 1. The source string.
106 : // 2. The shared function info of the calling function.
107 : // 3. Whether the source should be compiled as strict code or as sloppy code.
108 : // Note: Currently there are clients of CompileEval that always compile
109 : // sloppy code even if the calling function is a strict mode function.
110 : // More specifically these are the CompileString, DebugEvaluate and
111 : // DebugEvaluateGlobal runtime functions.
112 : // 4. The start position of the calling scope.
113 : class CompilationCacheEval: public CompilationSubCache {
114 : public:
115 : explicit CompilationCacheEval(Isolate* isolate)
116 : : CompilationSubCache(isolate, 1) {}
117 :
118 : InfoVectorPair Lookup(Handle<String> source,
119 : Handle<SharedFunctionInfo> outer_info,
120 : Handle<Context> native_context,
121 : LanguageMode language_mode, int position);
122 :
123 : void Put(Handle<String> source, Handle<SharedFunctionInfo> outer_info,
124 : Handle<SharedFunctionInfo> function_info,
125 : Handle<Context> native_context, Handle<Cell> literals, int position);
126 :
127 : private:
128 : DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
129 : };
130 :
131 :
132 : // Sub-cache for regular expressions.
133 : class CompilationCacheRegExp: public CompilationSubCache {
134 : public:
135 : CompilationCacheRegExp(Isolate* isolate, int generations)
136 : : CompilationSubCache(isolate, generations) { }
137 :
138 : MaybeHandle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
139 :
140 : void Put(Handle<String> source,
141 : JSRegExp::Flags flags,
142 : Handle<FixedArray> data);
143 : private:
144 : DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
145 : };
146 :
147 : // The compilation cache keeps shared function infos for compiled
148 : // scripts and evals. The shared function infos are looked up using
149 : // the source string as the key. For regular expressions the
150 : // compilation data is cached.
151 : class CompilationCache {
152 : public:
153 : // Finds the script shared function info for a source
154 : // string. Returns an empty handle if the cache doesn't contain a
155 : // script for the given source string with the right origin.
156 : InfoVectorPair LookupScript(Handle<String> source, Handle<Object> name,
157 : int line_offset, int column_offset,
158 : ScriptOriginOptions resource_options,
159 : Handle<Context> context,
160 : LanguageMode language_mode);
161 :
162 : // Finds the shared function info for a source string for eval in a
163 : // given context. Returns an empty handle if the cache doesn't
164 : // contain a script for the given source string.
165 : InfoVectorPair LookupEval(Handle<String> source,
166 : Handle<SharedFunctionInfo> outer_info,
167 : Handle<Context> context, LanguageMode language_mode,
168 : int position);
169 :
170 : // Returns the regexp data associated with the given regexp if it
171 : // is in cache, otherwise an empty handle.
172 : MaybeHandle<FixedArray> LookupRegExp(
173 : Handle<String> source, JSRegExp::Flags flags);
174 :
175 : // Associate the (source, kind) pair to the shared function
176 : // info. This may overwrite an existing mapping.
177 : void PutScript(Handle<String> source, Handle<Context> context,
178 : LanguageMode language_mode,
179 : Handle<SharedFunctionInfo> function_info,
180 : Handle<Cell> literals);
181 :
182 : // Associate the (source, context->closure()->shared(), kind) triple
183 : // with the shared function info. This may overwrite an existing mapping.
184 : void PutEval(Handle<String> source, Handle<SharedFunctionInfo> outer_info,
185 : Handle<Context> context,
186 : Handle<SharedFunctionInfo> function_info, Handle<Cell> literals,
187 : int position);
188 :
189 : // Associate the (source, flags) pair to the given regexp data.
190 : // This may overwrite an existing mapping.
191 : void PutRegExp(Handle<String> source,
192 : JSRegExp::Flags flags,
193 : Handle<FixedArray> data);
194 :
195 : // Clear the cache - also used to initialize the cache at startup.
196 : void Clear();
197 :
198 : // Remove given shared function info from all caches.
199 : void Remove(Handle<SharedFunctionInfo> function_info);
200 :
201 : // GC support.
202 : void Iterate(RootVisitor* v);
203 : void IterateFunctions(ObjectVisitor* v);
204 :
205 : // Notify the cache that a mark-sweep garbage collection is about to
206 : // take place. This is used to retire entries from the cache to
207 : // avoid keeping them alive too long without using them.
208 : void MarkCompactPrologue();
209 :
210 : // Enable/disable compilation cache. Used by debugger to disable compilation
211 : // cache during debugging to make sure new scripts are always compiled.
212 : void Enable();
213 : void Disable();
214 :
215 : private:
216 : explicit CompilationCache(Isolate* isolate);
217 : ~CompilationCache();
218 :
219 : base::HashMap* EagerOptimizingSet();
220 :
221 : // The number of sub caches covering the different types to cache.
222 : static const int kSubCacheCount = 4;
223 :
224 9087523 : bool IsEnabled() { return FLAG_compilation_cache && enabled_; }
225 :
226 : Isolate* isolate() { return isolate_; }
227 :
228 : Isolate* isolate_;
229 :
230 : CompilationCacheScript script_;
231 : CompilationCacheEval eval_global_;
232 : CompilationCacheEval eval_contextual_;
233 : CompilationCacheRegExp reg_exp_;
234 : CompilationSubCache* subcaches_[kSubCacheCount];
235 :
236 : // Current enable state of the compilation cache.
237 : bool enabled_;
238 :
239 : friend class Isolate;
240 :
241 : DISALLOW_COPY_AND_ASSIGN(CompilationCache);
242 : };
243 :
244 :
245 : } // namespace internal
246 : } // namespace v8
247 :
248 : #endif // V8_COMPILATION_CACHE_H_
|