yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2 | // 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.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 31 | namespace v8 { |
| 32 | namespace internal { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 33 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 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 { |
| 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. |
| 94 | class 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, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 101 | int column_offset, |
| 102 | Handle<Context> context); |
| 103 | void Put(Handle<String> source, |
| 104 | Handle<Context> context, |
| 105 | Handle<SharedFunctionInfo> function_info); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | MUST_USE_RESULT MaybeObject* TryTablePut( |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 109 | Handle<String> source, |
| 110 | Handle<Context> context, |
| 111 | Handle<SharedFunctionInfo> function_info); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 112 | |
| 113 | // Note: Returns a new hash table if operation results in expansion. |
| 114 | Handle<CompilationCacheTable> TablePut( |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 115 | Handle<String> source, |
| 116 | Handle<Context> context, |
| 117 | Handle<SharedFunctionInfo> function_info); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 118 | |
| 119 | bool HasOrigin(Handle<SharedFunctionInfo> function_info, |
| 120 | Handle<Object> name, |
| 121 | int line_offset, |
| 122 | int column_offset); |
| 123 | |
| 124 | void* script_histogram_; |
| 125 | bool script_histogram_initialized_; |
| 126 | |
| 127 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript); |
| 128 | }; |
| 129 | |
| 130 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 131 | // Sub-cache for eval scripts. Two caches for eval are used. One for eval calls |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 132 | // in native contexts and one for eval calls in other contexts. The cache |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 133 | // considers the following pieces of information when checking for matching |
| 134 | // entries: |
| 135 | // 1. The source string. |
| 136 | // 2. The shared function info of the calling function. |
| 137 | // 3. Whether the source should be compiled as strict code or as non-strict |
| 138 | // code. |
| 139 | // Note: Currently there are clients of CompileEval that always compile |
| 140 | // non-strict code even if the calling function is a strict mode function. |
| 141 | // More specifically these are the CompileString, DebugEvaluate and |
| 142 | // DebugEvaluateGlobal runtime functions. |
| 143 | // 4. The start position of the calling scope. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 144 | class CompilationCacheEval: public CompilationSubCache { |
| 145 | public: |
| 146 | CompilationCacheEval(Isolate* isolate, int generations) |
| 147 | : CompilationSubCache(isolate, generations) { } |
| 148 | |
| 149 | Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 150 | Handle<Context> context, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 151 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 152 | int scope_position); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 153 | |
| 154 | void Put(Handle<String> source, |
| 155 | Handle<Context> context, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 156 | Handle<SharedFunctionInfo> function_info, |
| 157 | int scope_position); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 158 | |
| 159 | private: |
| 160 | MUST_USE_RESULT MaybeObject* TryTablePut( |
| 161 | Handle<String> source, |
| 162 | Handle<Context> context, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 163 | Handle<SharedFunctionInfo> function_info, |
| 164 | int scope_position); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 165 | |
| 166 | // Note: Returns a new hash table if operation results in expansion. |
| 167 | Handle<CompilationCacheTable> TablePut( |
| 168 | Handle<String> source, |
| 169 | Handle<Context> context, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 170 | Handle<SharedFunctionInfo> function_info, |
| 171 | int scope_position); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 172 | |
| 173 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval); |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | // Sub-cache for regular expressions. |
| 178 | class CompilationCacheRegExp: public CompilationSubCache { |
| 179 | public: |
| 180 | CompilationCacheRegExp(Isolate* isolate, int generations) |
| 181 | : CompilationSubCache(isolate, generations) { } |
| 182 | |
| 183 | Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags); |
| 184 | |
| 185 | void Put(Handle<String> source, |
| 186 | JSRegExp::Flags flags, |
| 187 | Handle<FixedArray> data); |
| 188 | private: |
| 189 | MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source, |
| 190 | JSRegExp::Flags flags, |
| 191 | Handle<FixedArray> data); |
| 192 | |
| 193 | // Note: Returns a new hash table if operation results in expansion. |
| 194 | Handle<CompilationCacheTable> TablePut(Handle<String> source, |
| 195 | JSRegExp::Flags flags, |
| 196 | Handle<FixedArray> data); |
| 197 | |
| 198 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp); |
| 199 | }; |
| 200 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 201 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 202 | // The compilation cache keeps shared function infos for compiled |
| 203 | // scripts and evals. The shared function infos are looked up using |
| 204 | // the source string as the key. For regular expressions the |
| 205 | // compilation data is cached. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 206 | class CompilationCache { |
| 207 | public: |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 208 | // Finds the script shared function info for a source |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 209 | // string. Returns an empty handle if the cache doesn't contain a |
| 210 | // script for the given source string with the right origin. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 211 | Handle<SharedFunctionInfo> LookupScript(Handle<String> source, |
| 212 | Handle<Object> name, |
| 213 | int line_offset, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 214 | int column_offset, |
| 215 | Handle<Context> context); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 216 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 217 | // Finds the shared function info for a source string for eval in a |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 218 | // given context. Returns an empty handle if the cache doesn't |
| 219 | // contain a script for the given source string. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 220 | Handle<SharedFunctionInfo> LookupEval(Handle<String> source, |
| 221 | Handle<Context> context, |
| 222 | bool is_global, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 223 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 224 | int scope_position); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 225 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 226 | // Returns the regexp data associated with the given regexp if it |
| 227 | // is in cache, otherwise an empty handle. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 228 | Handle<FixedArray> LookupRegExp(Handle<String> source, |
| 229 | JSRegExp::Flags flags); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 230 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 231 | // Associate the (source, kind) pair to the shared function |
| 232 | // info. This may overwrite an existing mapping. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 233 | void PutScript(Handle<String> source, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 234 | Handle<Context> context, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 235 | Handle<SharedFunctionInfo> function_info); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 236 | |
| 237 | // Associate the (source, context->closure()->shared(), kind) triple |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 238 | // with the shared function info. This may overwrite an existing mapping. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 239 | void PutEval(Handle<String> source, |
| 240 | Handle<Context> context, |
| 241 | bool is_global, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 242 | Handle<SharedFunctionInfo> function_info, |
| 243 | int scope_position); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 244 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 245 | // Associate the (source, flags) pair to the given regexp data. |
| 246 | // This may overwrite an existing mapping. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 247 | void PutRegExp(Handle<String> source, |
| 248 | JSRegExp::Flags flags, |
| 249 | Handle<FixedArray> data); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 250 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 251 | // Clear the cache - also used to initialize the cache at startup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 252 | void Clear(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 253 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 254 | // Remove given shared function info from all caches. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 255 | void Remove(Handle<SharedFunctionInfo> function_info); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 256 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 257 | // GC support. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 258 | void Iterate(ObjectVisitor* v); |
| 259 | void IterateFunctions(ObjectVisitor* v); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 260 | |
| 261 | // Notify the cache that a mark-sweep garbage collection is about to |
| 262 | // take place. This is used to retire entries from the cache to |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 263 | // avoid keeping them alive too long without using them. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 264 | void MarkCompactPrologue(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 265 | |
| 266 | // Enable/disable compilation cache. Used by debugger to disable compilation |
| 267 | // cache during debugging to make sure new scripts are always compiled. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 268 | void Enable(); |
| 269 | void Disable(); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 270 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 271 | private: |
| 272 | explicit CompilationCache(Isolate* isolate); |
| 273 | ~CompilationCache(); |
| 274 | |
| 275 | HashMap* EagerOptimizingSet(); |
| 276 | |
| 277 | // The number of sub caches covering the different types to cache. |
| 278 | static const int kSubCacheCount = 4; |
| 279 | |
| 280 | bool IsEnabled() { return FLAG_compilation_cache && enabled_; } |
| 281 | |
| 282 | Isolate* isolate() { return isolate_; } |
| 283 | |
| 284 | Isolate* isolate_; |
| 285 | |
| 286 | CompilationCacheScript script_; |
| 287 | CompilationCacheEval eval_global_; |
| 288 | CompilationCacheEval eval_contextual_; |
| 289 | CompilationCacheRegExp reg_exp_; |
| 290 | CompilationSubCache* subcaches_[kSubCacheCount]; |
| 291 | |
| 292 | // Current enable state of the compilation cache. |
| 293 | bool enabled_; |
| 294 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 295 | friend class Isolate; |
| 296 | |
| 297 | DISALLOW_COPY_AND_ASSIGN(CompilationCache); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | |
| 301 | } } // namespace v8::internal |
| 302 | |
| 303 | #endif // V8_COMPILATION_CACHE_H_ |