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