// Copyright 2012 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_CODEGEN_COMPILER_H_ #define V8_CODEGEN_COMPILER_H_ #include #include #include "src/base/platform/elapsed-timer.h" #include "src/codegen/bailout-reason.h" #include "src/execution/isolate.h" #include "src/logging/code-events.h" #include "src/utils/allocation.h" #include "src/objects/contexts.h" #include "src/zone/zone.h" namespace v8 { namespace internal { // Forward declarations. class AstRawString; class BackgroundCompileTask; class IsCompiledScope; class JavaScriptFrame; class OptimizedCompilationInfo; class OptimizedCompilationJob; class ParseInfo; class Parser; class ScriptData; struct ScriptStreamingData; class TimedHistogram; class UnoptimizedCompilationInfo; class UnoptimizedCompilationJob; class WorkerThreadRuntimeCallStats; using UnoptimizedCompilationJobList = std::forward_list>; // 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. class V8_EXPORT_PRIVATE Compiler : public AllStatic { 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). static bool Compile(Handle shared, ClearExceptionFlag flag, IsCompiledScope* is_compiled_scope); static bool Compile(Handle function, ClearExceptionFlag flag, IsCompiledScope* is_compiled_scope); static bool CompileOptimized(Handle function, ConcurrencyMode); // Collect source positions for a function that has already been compiled to // bytecode, but for which source positions were not collected (e.g. because // they were not immediately needed). static bool CollectSourcePositions(Isolate* isolate, Handle shared); V8_WARN_UNUSED_RESULT static MaybeHandle CompileForLiveEdit(ParseInfo* parse_info, Isolate* isolate); // Finalize and install code from previously run background compile task. static bool FinalizeBackgroundCompileTask( BackgroundCompileTask* task, Handle shared_info, Isolate* isolate, ClearExceptionFlag flag); // Finalize and install optimized code from previously run job. static bool FinalizeOptimizedCompilationJob(OptimizedCompilationJob* job, Isolate* isolate); // 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 function, AllocationType); // Parser::Parse, then Compiler::Analyze. static bool ParseAndAnalyze(ParseInfo* parse_info, Handle shared_info, Isolate* isolate); // Rewrite and analyze scopes. static bool Analyze(ParseInfo* parse_info); // =========================================================================== // 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. V8_WARN_UNUSED_RESULT static MaybeHandle GetFunctionFromEval( Handle source, Handle outer_info, Handle context, LanguageMode language_mode, ParseRestriction restriction, int parameters_end_pos, int eval_scope_position, int eval_position); struct ScriptDetails { ScriptDetails() : line_offset(0), column_offset(0) {} explicit ScriptDetails(Handle script_name) : line_offset(0), column_offset(0), name_obj(script_name) {} int line_offset; int column_offset; i::MaybeHandle name_obj; i::MaybeHandle source_map_url; i::MaybeHandle host_defined_options; }; // Create a function that results from wrapping |source| in a function, // with |arguments| being a list of parameters for that function. V8_WARN_UNUSED_RESULT static MaybeHandle GetWrappedFunction( Handle source, Handle arguments, Handle context, const ScriptDetails& script_details, ScriptOriginOptions origin_options, ScriptData* cached_data, v8::ScriptCompiler::CompileOptions compile_options, v8::ScriptCompiler::NoCacheReason no_cache_reason); // Create a (bound) function for a String source within a context for eval. V8_WARN_UNUSED_RESULT static MaybeHandle GetFunctionFromString( Handle context, Handle source, ParseRestriction restriction, int parameters_end_pos); // Decompose GetFunctionFromString into two functions, to allow callers to // deal seperately with a case of object not handled by the embedder. V8_WARN_UNUSED_RESULT static std::pair, bool> ValidateDynamicCompilationSource(Isolate* isolate, Handle context, Handle source_object); V8_WARN_UNUSED_RESULT static MaybeHandle GetFunctionFromValidatedString(Handle context, MaybeHandle source, ParseRestriction restriction, int parameters_end_pos); // Create a shared function info object for a String source. static MaybeHandle GetSharedFunctionInfoForScript( Isolate* isolate, Handle source, const ScriptDetails& script_details, ScriptOriginOptions origin_options, v8::Extension* extension, ScriptData* cached_data, ScriptCompiler::CompileOptions compile_options, ScriptCompiler::NoCacheReason no_cache_reason, 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. static MaybeHandle GetSharedFunctionInfoForStreamedScript( Isolate* isolate, Handle source, const ScriptDetails& script_details, ScriptOriginOptions origin_options, ScriptStreamingData* streaming_data); // Create a shared function info object for the given function literal // node (the code may be lazily compiled). static Handle GetSharedFunctionInfo(FunctionLiteral* node, Handle