2012-02-14 14:14:51 +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-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifndef V8_COMPILER_H_
|
|
|
|
#define V8_COMPILER_H_
|
|
|
|
|
2017-11-14 16:29:43 +00:00
|
|
|
#include <forward_list>
|
2016-07-25 10:24:45 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/allocation.h"
|
2014-09-24 07:08:27 +00:00
|
|
|
#include "src/bailout-reason.h"
|
2017-10-30 13:18:12 +00:00
|
|
|
#include "src/code-events.h"
|
2016-08-18 10:26:19 +00:00
|
|
|
#include "src/contexts.h"
|
|
|
|
#include "src/isolate.h"
|
2018-02-15 16:05:21 +00:00
|
|
|
#include "src/unicode-cache.h"
|
2016-09-20 16:07:25 +00:00
|
|
|
#include "src/zone/zone.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2016-01-15 12:11:35 +00:00
|
|
|
// Forward declarations.
|
2016-03-08 12:07:27 +00:00
|
|
|
class CompilationInfo;
|
2016-04-27 17:54:16 +00:00
|
|
|
class CompilationJob;
|
2015-07-06 11:11:15 +00:00
|
|
|
class JavaScriptFrame;
|
2015-03-09 14:51:13 +00:00
|
|
|
class ParseInfo;
|
2018-02-15 16:05:21 +00:00
|
|
|
class Parser;
|
2015-03-09 14:51:13 +00:00
|
|
|
class ScriptData;
|
2018-02-15 16:05:21 +00:00
|
|
|
struct ScriptStreamingData;
|
2016-12-16 12:38:17 +00:00
|
|
|
|
2017-11-14 16:29:43 +00:00
|
|
|
typedef std::forward_list<std::unique_ptr<CompilationJob>> CompilationJobList;
|
|
|
|
|
2016-03-08 12:07:27 +00:00
|
|
|
// The V8 compiler API.
|
|
|
|
//
|
|
|
|
// This is the central hub for dispatching to the various compilers within V8.
|
|
|
|
// Logic for which compiler to choose and how to wire compilation results into
|
|
|
|
// the object heap should be kept inside this class.
|
|
|
|
//
|
|
|
|
// General strategy: Scripts are translated into anonymous functions w/o
|
|
|
|
// parameters which then can be executed. If the source code contains other
|
|
|
|
// functions, they might be compiled and allocated as part of the compilation
|
|
|
|
// of the source code or deferred for lazy compilation at a later point.
|
2017-01-25 22:41:57 +00:00
|
|
|
class V8_EXPORT_PRIVATE Compiler : public AllStatic {
|
2016-03-08 12:07:27 +00:00
|
|
|
public:
|
|
|
|
enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION };
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
// The following family of methods ensures a given function is compiled. The
|
|
|
|
// general contract is that failures will be reported by returning {false},
|
|
|
|
// whereas successful compilation ensures the {is_compiled} predicate on the
|
|
|
|
// given function holds (except for live-edit, which compiles the world).
|
|
|
|
|
2017-08-17 12:40:24 +00:00
|
|
|
static bool Compile(Handle<SharedFunctionInfo> shared,
|
|
|
|
ClearExceptionFlag flag);
|
2016-03-08 12:07:27 +00:00
|
|
|
static bool Compile(Handle<JSFunction> function, ClearExceptionFlag flag);
|
|
|
|
static bool CompileOptimized(Handle<JSFunction> function, ConcurrencyMode);
|
2016-05-18 07:09:37 +00:00
|
|
|
static MaybeHandle<JSArray> CompileForLiveEdit(Handle<Script> script);
|
2016-03-08 12:07:27 +00:00
|
|
|
|
2018-02-15 16:05:21 +00:00
|
|
|
// Creates a new task that when run will parse and compile the streamed
|
|
|
|
// script associated with |streaming_data| and can be finalized with
|
|
|
|
// Compiler::GetSharedFunctionInfoForStreamedScript.
|
|
|
|
// Note: does not take ownership of streaming_data.
|
|
|
|
static ScriptCompiler::ScriptStreamingTask* NewBackgroundCompileTask(
|
|
|
|
ScriptStreamingData* streaming_data, Isolate* isolate);
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2016-04-27 17:54:16 +00:00
|
|
|
// Generate and install code from previously queued compilation job.
|
2017-11-15 14:36:57 +00:00
|
|
|
static bool FinalizeCompilationJob(CompilationJob* job, Isolate* isolate);
|
2016-03-08 12:07:27 +00:00
|
|
|
|
|
|
|
// Give the compiler a chance to perform low-latency initialization tasks of
|
|
|
|
// the given {function} on its instantiation. Note that only the runtime will
|
|
|
|
// offer this chance, optimized closure instantiation will not call this.
|
|
|
|
static void PostInstantiation(Handle<JSFunction> function, PretenureFlag);
|
|
|
|
|
|
|
|
// Parser::Parse, then Compiler::Analyze.
|
2017-08-03 11:00:57 +00:00
|
|
|
static bool ParseAndAnalyze(ParseInfo* parse_info,
|
2017-07-21 09:32:32 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_info,
|
|
|
|
Isolate* isolate);
|
2018-01-24 16:40:24 +00:00
|
|
|
// Rewrite and analyze scopes.
|
2018-01-24 16:14:38 +00:00
|
|
|
static bool Analyze(ParseInfo* parse_info);
|
2016-07-25 09:42:48 +00:00
|
|
|
|
2016-03-08 12:07:27 +00:00
|
|
|
// ===========================================================================
|
|
|
|
// The following family of methods instantiates new functions for scripts or
|
|
|
|
// function literals. The decision whether those functions will be compiled,
|
|
|
|
// is left to the discretion of the compiler.
|
|
|
|
//
|
|
|
|
// Please note this interface returns shared function infos. This means you
|
|
|
|
// need to call Factory::NewFunctionFromSharedFunctionInfo before you have a
|
|
|
|
// real function with a context.
|
|
|
|
|
|
|
|
// Create a (bound) function for a String source within a context for eval.
|
|
|
|
MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromEval(
|
|
|
|
Handle<String> source, Handle<SharedFunctionInfo> outer_info,
|
|
|
|
Handle<Context> context, LanguageMode language_mode,
|
Implement new Function.prototype.toString --harmony-function-tostring
For functions declared in source code, the .toString() representation
will be an excerpt of the source code.
* For functions declared with the "function" keyword, the excerpt
starts at the "function" or "async" keyword and ends at the final "}".
The previous behavior would start the excerpt at the "(" of the
parameter list, and prepend a canonical `"function " + name` or
similar, which would discard comments and formatting surrounding the
function's name. Anonymous functions declared as function expressions
no longer get the name "anonymous" in their toString representation.
* For methods, the excerpt starts at the "get", "set", "*" (for
generator methods), or property name, whichever comes first.
Previously, the toString representation for methods would use a
canonical prefix before the "(" of the parameter list. Note that any
"static" keyword is omitted.
* For arrow functions and class declarations, the excerpt is unchanged.
For functions created with the Function, GeneratorFunction, or
AsyncFunction constructors:
* The string separating the parameter text and body text is now
"\n) {\n", where previously it was "\n/*``*/) {\n" or ") {\n".
* At one point, newline normalization was required by the spec here,
but that was removed from the spec, and so this CL does not do it.
Included in this CL is a fix for CreateDynamicFunction parsing. ')'
and '`' characters in the parameter string are no longer disallowed,
and Function("a=function(", "}){") is no longer allowed.
BUG=v8:4958, v8:4230
Review-Url: https://codereview.chromium.org/2156303002
Cr-Commit-Position: refs/heads/master@{#43262}
2017-02-16 20:19:24 +00:00
|
|
|
ParseRestriction restriction, int parameters_end_pos,
|
|
|
|
int eval_scope_position, int eval_position, int line_offset = 0,
|
|
|
|
int column_offset = 0, Handle<Object> script_name = Handle<Object>(),
|
2016-03-08 12:07:27 +00:00
|
|
|
ScriptOriginOptions options = ScriptOriginOptions());
|
|
|
|
|
2017-12-18 08:10:06 +00:00
|
|
|
// Create a function that results from wrapping |source| in a function,
|
|
|
|
// with |arguments| being a list of parameters for that function.
|
|
|
|
MUST_USE_RESULT static MaybeHandle<JSFunction> GetWrappedFunction(
|
|
|
|
Handle<String> source, Handle<FixedArray> arguments,
|
|
|
|
Handle<Context> context, int line_offset = 0, int column_offset = 0,
|
|
|
|
Handle<Object> script_name = Handle<Object>(),
|
|
|
|
ScriptOriginOptions options = ScriptOriginOptions());
|
|
|
|
|
2017-06-13 12:43:18 +00:00
|
|
|
// Returns true if the embedder permits compiling the given source string in
|
|
|
|
// the given context.
|
|
|
|
static bool CodeGenerationFromStringsAllowed(Isolate* isolate,
|
|
|
|
Handle<Context> context,
|
|
|
|
Handle<String> source);
|
|
|
|
|
2016-07-20 13:29:04 +00:00
|
|
|
// Create a (bound) function for a String source within a context for eval.
|
|
|
|
MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromString(
|
|
|
|
Handle<Context> context, Handle<String> source,
|
Implement new Function.prototype.toString --harmony-function-tostring
For functions declared in source code, the .toString() representation
will be an excerpt of the source code.
* For functions declared with the "function" keyword, the excerpt
starts at the "function" or "async" keyword and ends at the final "}".
The previous behavior would start the excerpt at the "(" of the
parameter list, and prepend a canonical `"function " + name` or
similar, which would discard comments and formatting surrounding the
function's name. Anonymous functions declared as function expressions
no longer get the name "anonymous" in their toString representation.
* For methods, the excerpt starts at the "get", "set", "*" (for
generator methods), or property name, whichever comes first.
Previously, the toString representation for methods would use a
canonical prefix before the "(" of the parameter list. Note that any
"static" keyword is omitted.
* For arrow functions and class declarations, the excerpt is unchanged.
For functions created with the Function, GeneratorFunction, or
AsyncFunction constructors:
* The string separating the parameter text and body text is now
"\n) {\n", where previously it was "\n/*``*/) {\n" or ") {\n".
* At one point, newline normalization was required by the spec here,
but that was removed from the spec, and so this CL does not do it.
Included in this CL is a fix for CreateDynamicFunction parsing. ')'
and '`' characters in the parameter string are no longer disallowed,
and Function("a=function(", "}){") is no longer allowed.
BUG=v8:4958, v8:4230
Review-Url: https://codereview.chromium.org/2156303002
Cr-Commit-Position: refs/heads/master@{#43262}
2017-02-16 20:19:24 +00:00
|
|
|
ParseRestriction restriction, int parameters_end_pos);
|
2016-07-20 13:29:04 +00:00
|
|
|
|
2018-02-21 14:35:23 +00:00
|
|
|
struct ScriptDetails {
|
|
|
|
ScriptDetails() : line_offset(0), column_offset(0) {}
|
|
|
|
explicit ScriptDetails(Handle<Object> script_name)
|
|
|
|
: line_offset(0), column_offset(0), name_obj(script_name) {}
|
|
|
|
|
|
|
|
int line_offset;
|
|
|
|
int column_offset;
|
|
|
|
i::MaybeHandle<i::Object> name_obj;
|
|
|
|
i::MaybeHandle<i::Object> source_map_url;
|
|
|
|
i::MaybeHandle<i::FixedArray> host_defined_options;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a shared function info object for a String source.
|
2017-10-04 22:48:12 +00:00
|
|
|
static MaybeHandle<SharedFunctionInfo> GetSharedFunctionInfoForScript(
|
2018-02-21 14:35:23 +00:00
|
|
|
Handle<String> source, const ScriptDetails& script_details,
|
|
|
|
ScriptOriginOptions origin_options, v8::Extension* extension,
|
|
|
|
ScriptData** cached_data, ScriptCompiler::CompileOptions compile_options,
|
2017-11-01 16:16:35 +00:00
|
|
|
ScriptCompiler::NoCacheReason no_cache_reason,
|
2018-02-21 14:35:23 +00:00
|
|
|
NativesFlag is_natives_code);
|
|
|
|
|
|
|
|
// Create a shared function info object for a Script source that has already
|
|
|
|
// been parsed and possibly compiled on a background thread while being loaded
|
|
|
|
// from a streamed source. On return, the data held by |streaming_data| will
|
|
|
|
// have been released, however the object itself isn't freed and is still
|
|
|
|
// owned by the caller.
|
2018-02-15 16:05:21 +00:00
|
|
|
static MaybeHandle<SharedFunctionInfo> GetSharedFunctionInfoForStreamedScript(
|
2018-02-21 14:35:23 +00:00
|
|
|
Handle<String> source, const ScriptDetails& script_details,
|
|
|
|
ScriptOriginOptions origin_options, ScriptStreamingData* streaming_data);
|
2017-11-14 16:29:43 +00:00
|
|
|
|
2017-11-29 16:17:41 +00:00
|
|
|
// Create a shared function info object for the given function literal
|
|
|
|
// node (the code may be lazily compiled).
|
|
|
|
static Handle<SharedFunctionInfo> GetSharedFunctionInfo(FunctionLiteral* node,
|
|
|
|
Handle<Script> script,
|
|
|
|
Isolate* isolate);
|
|
|
|
|
2016-03-08 12:07:27 +00:00
|
|
|
// ===========================================================================
|
|
|
|
// The following family of methods provides support for OSR. Code generated
|
|
|
|
// for entry via OSR might not be suitable for normal entry, hence will be
|
|
|
|
// returned directly to the caller.
|
|
|
|
//
|
|
|
|
// Please note this interface is the only part dealing with {Code} objects
|
|
|
|
// directly. Other methods are agnostic to {Code} and can use an interpreter
|
|
|
|
// instead of generating JIT code for a function at all.
|
|
|
|
|
|
|
|
// Generate and return optimized code for OSR, or empty handle on failure.
|
|
|
|
MUST_USE_RESULT static MaybeHandle<Code> GetOptimizedCodeForOSR(
|
2017-08-18 15:25:01 +00:00
|
|
|
Handle<JSFunction> function, BailoutId osr_offset,
|
2016-03-08 12:07:27 +00:00
|
|
|
JavaScriptFrame* osr_frame);
|
|
|
|
};
|
2014-07-10 10:28:05 +00:00
|
|
|
|
2016-04-11 10:01:09 +00:00
|
|
|
// A base class for compilation jobs intended to run concurrent to the main
|
|
|
|
// thread. The job is split into three phases which are called in sequence on
|
|
|
|
// different threads and with different limitations:
|
2016-08-22 11:48:59 +00:00
|
|
|
// 1) PrepareJob: Runs on main thread. No major limitations.
|
|
|
|
// 2) ExecuteJob: Runs concurrently. No heap allocation or handle derefs.
|
|
|
|
// 3) FinalizeJob: Runs on main thread. No dependency changes.
|
2016-04-11 10:01:09 +00:00
|
|
|
//
|
2016-08-22 11:48:59 +00:00
|
|
|
// Each of the three phases can either fail or succeed. The current state of
|
|
|
|
// the job can be checked using {state()}.
|
2017-01-30 19:27:00 +00:00
|
|
|
class V8_EXPORT_PRIVATE CompilationJob {
|
2012-07-17 16:24:40 +00:00
|
|
|
public:
|
2016-08-22 11:48:59 +00:00
|
|
|
enum Status { SUCCEEDED, FAILED };
|
|
|
|
enum class State {
|
|
|
|
kReadyToPrepare,
|
|
|
|
kReadyToExecute,
|
|
|
|
kReadyToFinalize,
|
|
|
|
kSucceeded,
|
|
|
|
kFailed,
|
|
|
|
};
|
2017-10-20 16:27:05 +00:00
|
|
|
CompilationJob(uintptr_t stack_limit, ParseInfo* parse_info,
|
|
|
|
CompilationInfo* compilation_info, const char* compiler_name,
|
2017-01-03 18:11:58 +00:00
|
|
|
State initial_state = State::kReadyToPrepare);
|
2016-04-27 17:54:16 +00:00
|
|
|
virtual ~CompilationJob() {}
|
2012-07-17 16:24:40 +00:00
|
|
|
|
2016-08-22 11:48:59 +00:00
|
|
|
// Prepare the compile job. Must be called on the main thread.
|
2017-11-15 14:36:57 +00:00
|
|
|
MUST_USE_RESULT Status PrepareJob(Isolate* isolate);
|
2012-07-17 16:24:40 +00:00
|
|
|
|
2016-08-25 10:24:53 +00:00
|
|
|
// Executes the compile job. Can be called on a background thread if
|
|
|
|
// can_execute_on_background_thread() returns true.
|
2016-08-22 11:48:59 +00:00
|
|
|
MUST_USE_RESULT Status ExecuteJob();
|
2012-07-17 16:24:40 +00:00
|
|
|
|
2016-08-22 11:48:59 +00:00
|
|
|
// Finalizes the compile job. Must be called on the main thread.
|
2017-11-15 14:36:57 +00:00
|
|
|
MUST_USE_RESULT Status FinalizeJob(Isolate* isolate);
|
2012-07-17 16:24:40 +00:00
|
|
|
|
2016-08-22 11:48:59 +00:00
|
|
|
// Report a transient failure, try again next time. Should only be called on
|
|
|
|
// optimization compilation jobs.
|
2016-08-31 08:49:14 +00:00
|
|
|
Status RetryOptimization(BailoutReason reason);
|
2013-12-23 14:30:35 +00:00
|
|
|
|
2016-08-22 11:48:59 +00:00
|
|
|
// Report a persistent failure, disable future optimization on the function.
|
|
|
|
// Should only be called on optimization compilation jobs.
|
2016-08-31 08:49:14 +00:00
|
|
|
Status AbortOptimization(BailoutReason reason);
|
2012-07-19 18:58:23 +00:00
|
|
|
|
2016-08-25 10:24:53 +00:00
|
|
|
void RecordOptimizedCompilationStats() const;
|
2017-11-15 14:36:57 +00:00
|
|
|
void RecordUnoptimizedCompilationStats(Isolate* isolate) const;
|
|
|
|
void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag,
|
|
|
|
Isolate* isolate) const;
|
2016-08-25 10:24:53 +00:00
|
|
|
|
|
|
|
void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; }
|
|
|
|
uintptr_t stack_limit() const { return stack_limit_; }
|
2016-04-11 10:01:09 +00:00
|
|
|
|
2016-08-22 11:48:59 +00:00
|
|
|
State state() const { return state_; }
|
2017-08-03 11:00:57 +00:00
|
|
|
ParseInfo* parse_info() const { return parse_info_; }
|
|
|
|
CompilationInfo* compilation_info() const { return compilation_info_; }
|
2017-06-20 09:19:27 +00:00
|
|
|
virtual size_t AllocatedMemory() const { return 0; }
|
2016-04-11 10:01:09 +00:00
|
|
|
|
2016-08-22 11:48:59 +00:00
|
|
|
protected:
|
2016-08-18 17:06:37 +00:00
|
|
|
// Overridden by the actual implementation.
|
2017-11-15 14:36:57 +00:00
|
|
|
virtual Status PrepareJobImpl(Isolate* isolate) = 0;
|
2016-08-22 11:48:59 +00:00
|
|
|
virtual Status ExecuteJobImpl() = 0;
|
2017-11-15 14:36:57 +00:00
|
|
|
virtual Status FinalizeJobImpl(Isolate* isolate) = 0;
|
2016-08-22 11:48:59 +00:00
|
|
|
|
2012-07-17 16:24:40 +00:00
|
|
|
private:
|
2017-08-03 11:00:57 +00:00
|
|
|
// TODO(6409): Remove parse_info once Fullcode and AstGraphBuilder are gone.
|
|
|
|
ParseInfo* parse_info_;
|
|
|
|
CompilationInfo* compilation_info_;
|
2016-08-22 11:48:59 +00:00
|
|
|
base::TimeDelta time_taken_to_prepare_;
|
|
|
|
base::TimeDelta time_taken_to_execute_;
|
|
|
|
base::TimeDelta time_taken_to_finalize_;
|
2016-04-11 12:06:41 +00:00
|
|
|
const char* compiler_name_;
|
2016-08-22 11:48:59 +00:00
|
|
|
State state_;
|
2016-08-25 10:24:53 +00:00
|
|
|
uintptr_t stack_limit_;
|
2016-08-22 11:48:59 +00:00
|
|
|
|
|
|
|
MUST_USE_RESULT Status UpdateState(Status status, State next_state) {
|
|
|
|
if (status == SUCCEEDED) {
|
|
|
|
state_ = next_state;
|
|
|
|
} else {
|
|
|
|
state_ = State::kFailed;
|
|
|
|
}
|
|
|
|
return status;
|
2012-07-17 16:24:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-15 16:05:21 +00:00
|
|
|
// Contains all data which needs to be transmitted between threads for
|
|
|
|
// background parsing and compiling and finalizing it on the main thread.
|
|
|
|
struct ScriptStreamingData {
|
|
|
|
ScriptStreamingData(ScriptCompiler::ExternalSourceStream* source_stream,
|
|
|
|
ScriptCompiler::StreamedSource::Encoding encoding);
|
|
|
|
~ScriptStreamingData();
|
|
|
|
|
|
|
|
void Release();
|
|
|
|
|
|
|
|
// Internal implementation of v8::ScriptCompiler::StreamedSource.
|
|
|
|
std::unique_ptr<ScriptCompiler::ExternalSourceStream> source_stream;
|
|
|
|
ScriptCompiler::StreamedSource::Encoding encoding;
|
|
|
|
std::unique_ptr<ScriptCompiler::CachedData> cached_data;
|
|
|
|
|
|
|
|
// Data needed for parsing, and data needed to to be passed between thread
|
|
|
|
// between parsing and compilation. These need to be initialized before the
|
|
|
|
// compilation starts.
|
|
|
|
UnicodeCache unicode_cache;
|
|
|
|
std::unique_ptr<ParseInfo> info;
|
|
|
|
std::unique_ptr<Parser> parser;
|
|
|
|
|
|
|
|
// Data needed for finalizing compilation after background compilation.
|
|
|
|
std::unique_ptr<CompilationJob> outer_function_job;
|
|
|
|
CompilationJobList inner_function_jobs;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ScriptStreamingData);
|
|
|
|
};
|
|
|
|
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#endif // V8_COMPILER_H_
|