2012-02-23 09:12:57 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-09-11 10:51:52 +00:00
|
|
|
|
|
|
|
#ifndef V8_COMPILATION_CACHE_H_
|
|
|
|
#define V8_COMPILATION_CACHE_H_
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-09-11 10:51:52 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// The compilation cache consists of several generational sub-caches which uses
|
|
|
|
// this class as a base class. A sub-cache contains a compilation cache tables
|
|
|
|
// for each generation of the sub-cache. Since the same source code string has
|
|
|
|
// different compiled code for scripts and evals, we use separate sub-caches
|
|
|
|
// for different compilation modes, to avoid retrieving the wrong result.
|
|
|
|
class CompilationSubCache {
|
|
|
|
public:
|
2011-03-21 10:22:57 +00:00
|
|
|
CompilationSubCache(Isolate* isolate, int generations)
|
|
|
|
: isolate_(isolate),
|
|
|
|
generations_(generations) {
|
2011-03-18 20:35:07 +00:00
|
|
|
tables_ = NewArray<Object*>(generations);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CompilationSubCache() { DeleteArray(tables_); }
|
|
|
|
|
|
|
|
// Index for the first generation in the cache.
|
|
|
|
static const int kFirstGeneration = 0;
|
|
|
|
|
|
|
|
// Get the compilation cache tables for a specific generation.
|
|
|
|
Handle<CompilationCacheTable> GetTable(int generation);
|
|
|
|
|
|
|
|
// Accessors for first generation.
|
|
|
|
Handle<CompilationCacheTable> GetFirstTable() {
|
|
|
|
return GetTable(kFirstGeneration);
|
|
|
|
}
|
|
|
|
void SetFirstTable(Handle<CompilationCacheTable> value) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(kFirstGeneration < generations_);
|
2011-03-18 20:35:07 +00:00
|
|
|
tables_[kFirstGeneration] = *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Age the sub-cache by evicting the oldest generation and creating a new
|
|
|
|
// young generation.
|
|
|
|
void Age();
|
|
|
|
|
|
|
|
// GC support.
|
|
|
|
void Iterate(ObjectVisitor* v);
|
|
|
|
void IterateFunctions(ObjectVisitor* v);
|
|
|
|
|
|
|
|
// Clear this sub-cache evicting all its content.
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
// Remove given shared function info from sub-cache.
|
|
|
|
void Remove(Handle<SharedFunctionInfo> function_info);
|
|
|
|
|
|
|
|
// Number of generations in this sub-cache.
|
|
|
|
inline int generations() { return generations_; }
|
|
|
|
|
2011-03-21 10:22:57 +00:00
|
|
|
protected:
|
|
|
|
Isolate* isolate() { return isolate_; }
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
private:
|
2011-03-21 10:22:57 +00:00
|
|
|
Isolate* isolate_;
|
2011-03-18 20:35:07 +00:00
|
|
|
int generations_; // Number of generations.
|
|
|
|
Object** tables_; // Compilation cache tables - one for each generation.
|
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Sub-cache for scripts.
|
|
|
|
class CompilationCacheScript : public CompilationSubCache {
|
|
|
|
public:
|
2011-03-21 10:22:57 +00:00
|
|
|
CompilationCacheScript(Isolate* isolate, int generations);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> Lookup(Handle<String> source,
|
|
|
|
Handle<Object> name,
|
|
|
|
int line_offset,
|
2012-08-28 10:49:23 +00:00
|
|
|
int column_offset,
|
2013-07-30 17:05:50 +00:00
|
|
|
bool is_shared_cross_origin,
|
2012-08-28 10:49:23 +00:00
|
|
|
Handle<Context> context);
|
|
|
|
void Put(Handle<String> source,
|
|
|
|
Handle<Context> context,
|
|
|
|
Handle<SharedFunctionInfo> function_info);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool HasOrigin(Handle<SharedFunctionInfo> function_info,
|
|
|
|
Handle<Object> name,
|
|
|
|
int line_offset,
|
2013-07-30 17:05:50 +00:00
|
|
|
int column_offset,
|
|
|
|
bool is_shared_cross_origin);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
void* script_histogram_;
|
|
|
|
bool script_histogram_initialized_;
|
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-14 08:58:47 +00:00
|
|
|
// Sub-cache for eval scripts. Two caches for eval are used. One for eval calls
|
2012-08-17 09:03:08 +00:00
|
|
|
// in native contexts and one for eval calls in other contexts. The cache
|
2011-11-14 08:58:47 +00:00
|
|
|
// considers the following pieces of information when checking for matching
|
|
|
|
// entries:
|
|
|
|
// 1. The source string.
|
|
|
|
// 2. The shared function info of the calling function.
|
2014-03-11 14:39:08 +00:00
|
|
|
// 3. Whether the source should be compiled as strict code or as sloppy code.
|
2011-11-14 08:58:47 +00:00
|
|
|
// Note: Currently there are clients of CompileEval that always compile
|
2014-03-11 14:39:08 +00:00
|
|
|
// sloppy code even if the calling function is a strict mode function.
|
2011-11-14 08:58:47 +00:00
|
|
|
// More specifically these are the CompileString, DebugEvaluate and
|
|
|
|
// DebugEvaluateGlobal runtime functions.
|
|
|
|
// 4. The start position of the calling scope.
|
2011-03-18 20:35:07 +00:00
|
|
|
class CompilationCacheEval: public CompilationSubCache {
|
|
|
|
public:
|
2011-03-21 10:22:57 +00:00
|
|
|
CompilationCacheEval(Isolate* isolate, int generations)
|
|
|
|
: CompilationSubCache(isolate, generations) { }
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2014-04-08 12:33:08 +00:00
|
|
|
MaybeHandle<SharedFunctionInfo> Lookup(Handle<String> source,
|
|
|
|
Handle<Context> context,
|
|
|
|
StrictMode strict_mode,
|
|
|
|
int scope_position);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
void Put(Handle<String> source,
|
|
|
|
Handle<Context> context,
|
2011-11-14 08:58:47 +00:00
|
|
|
Handle<SharedFunctionInfo> function_info,
|
|
|
|
int scope_position);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Sub-cache for regular expressions.
|
|
|
|
class CompilationCacheRegExp: public CompilationSubCache {
|
|
|
|
public:
|
2011-03-21 10:22:57 +00:00
|
|
|
CompilationCacheRegExp(Isolate* isolate, int generations)
|
|
|
|
: CompilationSubCache(isolate, generations) { }
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2014-04-08 12:33:08 +00:00
|
|
|
MaybeHandle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
void Put(Handle<String> source,
|
|
|
|
JSRegExp::Flags flags,
|
|
|
|
Handle<FixedArray> data);
|
|
|
|
private:
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
|
|
|
|
};
|
|
|
|
|
2008-09-11 10:51:52 +00:00
|
|
|
|
2010-04-19 12:39:07 +00:00
|
|
|
// The compilation cache keeps shared function infos for compiled
|
|
|
|
// scripts and evals. The shared function infos are looked up using
|
|
|
|
// the source string as the key. For regular expressions the
|
|
|
|
// compilation data is cached.
|
2008-09-11 10:51:52 +00:00
|
|
|
class CompilationCache {
|
|
|
|
public:
|
2010-04-19 12:39:07 +00:00
|
|
|
// Finds the script shared function info for a source
|
2008-09-11 10:51:52 +00:00
|
|
|
// string. Returns an empty handle if the cache doesn't contain a
|
|
|
|
// script for the given source string with the right origin.
|
2014-04-08 12:33:08 +00:00
|
|
|
MaybeHandle<SharedFunctionInfo> LookupScript(
|
|
|
|
Handle<String> source, Handle<Object> name, int line_offset,
|
|
|
|
int column_offset, bool is_shared_cross_origin, Handle<Context> context);
|
2008-09-11 10:51:52 +00:00
|
|
|
|
2010-04-19 12:39:07 +00:00
|
|
|
// Finds the shared function info for a source string for eval in a
|
2009-02-24 13:11:53 +00:00
|
|
|
// given context. Returns an empty handle if the cache doesn't
|
|
|
|
// contain a script for the given source string.
|
2014-04-08 12:33:08 +00:00
|
|
|
MaybeHandle<SharedFunctionInfo> LookupEval(
|
|
|
|
Handle<String> source, Handle<Context> context, StrictMode strict_mode,
|
|
|
|
int scope_position);
|
2008-09-11 10:51:52 +00:00
|
|
|
|
2008-10-24 08:40:02 +00:00
|
|
|
// Returns the regexp data associated with the given regexp if it
|
|
|
|
// is in cache, otherwise an empty handle.
|
2014-04-08 12:33:08 +00:00
|
|
|
MaybeHandle<FixedArray> LookupRegExp(
|
|
|
|
Handle<String> source, JSRegExp::Flags flags);
|
2008-10-24 08:40:02 +00:00
|
|
|
|
2010-04-19 12:39:07 +00:00
|
|
|
// Associate the (source, kind) pair to the shared function
|
|
|
|
// info. This may overwrite an existing mapping.
|
2011-03-18 20:35:07 +00:00
|
|
|
void PutScript(Handle<String> source,
|
2012-08-28 10:49:23 +00:00
|
|
|
Handle<Context> context,
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<SharedFunctionInfo> function_info);
|
2009-03-03 13:35:05 +00:00
|
|
|
|
|
|
|
// Associate the (source, context->closure()->shared(), kind) triple
|
2010-04-19 12:39:07 +00:00
|
|
|
// with the shared function info. This may overwrite an existing mapping.
|
2011-03-18 20:35:07 +00:00
|
|
|
void PutEval(Handle<String> source,
|
|
|
|
Handle<Context> context,
|
2011-11-14 08:58:47 +00:00
|
|
|
Handle<SharedFunctionInfo> function_info,
|
|
|
|
int scope_position);
|
2009-03-03 13:35:05 +00:00
|
|
|
|
2008-10-24 08:40:02 +00:00
|
|
|
// Associate the (source, flags) pair to the given regexp data.
|
|
|
|
// This may overwrite an existing mapping.
|
2011-03-18 20:35:07 +00:00
|
|
|
void PutRegExp(Handle<String> source,
|
|
|
|
JSRegExp::Flags flags,
|
|
|
|
Handle<FixedArray> data);
|
2008-10-24 08:40:02 +00:00
|
|
|
|
2008-09-11 10:51:52 +00:00
|
|
|
// Clear the cache - also used to initialize the cache at startup.
|
2011-03-18 20:35:07 +00:00
|
|
|
void Clear();
|
2008-09-11 10:51:52 +00:00
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Remove given shared function info from all caches.
|
2011-03-18 20:35:07 +00:00
|
|
|
void Remove(Handle<SharedFunctionInfo> function_info);
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2008-09-11 10:51:52 +00:00
|
|
|
// GC support.
|
2011-03-18 20:35:07 +00:00
|
|
|
void Iterate(ObjectVisitor* v);
|
|
|
|
void IterateFunctions(ObjectVisitor* v);
|
2008-09-11 10:51:52 +00:00
|
|
|
|
|
|
|
// Notify the cache that a mark-sweep garbage collection is about to
|
|
|
|
// take place. This is used to retire entries from the cache to
|
2009-05-15 06:45:50 +00:00
|
|
|
// avoid keeping them alive too long without using them.
|
2011-03-18 20:35:07 +00:00
|
|
|
void MarkCompactPrologue();
|
2009-05-20 20:28:33 +00:00
|
|
|
|
|
|
|
// Enable/disable compilation cache. Used by debugger to disable compilation
|
|
|
|
// cache during debugging to make sure new scripts are always compiled.
|
2011-03-18 20:35:07 +00:00
|
|
|
void Enable();
|
|
|
|
void Disable();
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
private:
|
2011-03-21 10:22:57 +00:00
|
|
|
explicit CompilationCache(Isolate* isolate);
|
2011-03-18 20:35:07 +00:00
|
|
|
~CompilationCache();
|
|
|
|
|
|
|
|
HashMap* EagerOptimizingSet();
|
|
|
|
|
|
|
|
// The number of sub caches covering the different types to cache.
|
|
|
|
static const int kSubCacheCount = 4;
|
|
|
|
|
2011-03-21 10:22:57 +00:00
|
|
|
bool IsEnabled() { return FLAG_compilation_cache && enabled_; }
|
|
|
|
|
|
|
|
Isolate* isolate() { return isolate_; }
|
|
|
|
|
|
|
|
Isolate* isolate_;
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
CompilationCacheScript script_;
|
|
|
|
CompilationCacheEval eval_global_;
|
|
|
|
CompilationCacheEval eval_contextual_;
|
|
|
|
CompilationCacheRegExp reg_exp_;
|
|
|
|
CompilationSubCache* subcaches_[kSubCacheCount];
|
|
|
|
|
|
|
|
// Current enable state of the compilation cache.
|
|
|
|
bool enabled_;
|
|
|
|
|
|
|
|
friend class Isolate;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CompilationCache);
|
2008-09-11 10:51:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_COMPILATION_CACHE_H_
|