| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_COMPILER_H_ |
| 6 | #define V8_COMPILER_H_ |
| 7 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 8 | #include <memory> |
| 9 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/allocation.h" |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | #include "src/bailout-reason.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12 | #include "src/compilation-dependencies.h" |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 13 | #include "src/contexts.h" |
| 14 | #include "src/frames.h" |
| 15 | #include "src/isolate.h" |
| 16 | #include "src/objects-inl.h" |
| 17 | #include "src/source-position-table.h" |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 18 | #include "src/source-position.h" |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19 | #include "src/zone.h" |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | |
| 21 | namespace v8 { |
| 22 | namespace internal { |
| 23 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 24 | // Forward declarations. |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 25 | class CompilationInfo; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 26 | class CompilationJob; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 27 | class JavaScriptFrame; |
| 28 | class ParseInfo; |
| 29 | class ScriptData; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 31 | // The V8 compiler API. |
| 32 | // |
| 33 | // This is the central hub for dispatching to the various compilers within V8. |
| 34 | // Logic for which compiler to choose and how to wire compilation results into |
| 35 | // the object heap should be kept inside this class. |
| 36 | // |
| 37 | // General strategy: Scripts are translated into anonymous functions w/o |
| 38 | // parameters which then can be executed. If the source code contains other |
| 39 | // functions, they might be compiled and allocated as part of the compilation |
| 40 | // of the source code or deferred for lazy compilation at a later point. |
| 41 | class Compiler : public AllStatic { |
| 42 | public: |
| 43 | enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION }; |
| 44 | enum ConcurrencyMode { NOT_CONCURRENT, CONCURRENT }; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 45 | enum CompilationTier { INTERPRETED, BASELINE, OPTIMIZED }; |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 46 | |
| 47 | // =========================================================================== |
| 48 | // The following family of methods ensures a given function is compiled. The |
| 49 | // general contract is that failures will be reported by returning {false}, |
| 50 | // whereas successful compilation ensures the {is_compiled} predicate on the |
| 51 | // given function holds (except for live-edit, which compiles the world). |
| 52 | |
| 53 | static bool Compile(Handle<JSFunction> function, ClearExceptionFlag flag); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 54 | static bool CompileBaseline(Handle<JSFunction> function); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 55 | static bool CompileOptimized(Handle<JSFunction> function, ConcurrencyMode); |
| 56 | static bool CompileDebugCode(Handle<JSFunction> function); |
| 57 | static bool CompileDebugCode(Handle<SharedFunctionInfo> shared); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 58 | static MaybeHandle<JSArray> CompileForLiveEdit(Handle<Script> script); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 59 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 60 | // Generate and install code from previously queued compilation job. |
| 61 | static void FinalizeCompilationJob(CompilationJob* job); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 62 | |
| 63 | // Give the compiler a chance to perform low-latency initialization tasks of |
| 64 | // the given {function} on its instantiation. Note that only the runtime will |
| 65 | // offer this chance, optimized closure instantiation will not call this. |
| 66 | static void PostInstantiation(Handle<JSFunction> function, PretenureFlag); |
| 67 | |
| 68 | // Parser::Parse, then Compiler::Analyze. |
| 69 | static bool ParseAndAnalyze(ParseInfo* info); |
| 70 | // Rewrite, analyze scopes, and renumber. |
| 71 | static bool Analyze(ParseInfo* info); |
| 72 | // Adds deoptimization support, requires ParseAndAnalyze. |
| 73 | static bool EnsureDeoptimizationSupport(CompilationInfo* info); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 74 | // Ensures that bytecode is generated, calls ParseAndAnalyze internally. |
| 75 | static bool EnsureBytecode(CompilationInfo* info); |
| 76 | |
| 77 | // The next compilation tier which the function should be compiled to for |
| 78 | // optimization. This is used as a hint by the runtime profiler. |
| 79 | static CompilationTier NextCompilationTier(JSFunction* function); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 80 | |
| 81 | // =========================================================================== |
| 82 | // The following family of methods instantiates new functions for scripts or |
| 83 | // function literals. The decision whether those functions will be compiled, |
| 84 | // is left to the discretion of the compiler. |
| 85 | // |
| 86 | // Please note this interface returns shared function infos. This means you |
| 87 | // need to call Factory::NewFunctionFromSharedFunctionInfo before you have a |
| 88 | // real function with a context. |
| 89 | |
| 90 | // Create a (bound) function for a String source within a context for eval. |
| 91 | MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromEval( |
| 92 | Handle<String> source, Handle<SharedFunctionInfo> outer_info, |
| 93 | Handle<Context> context, LanguageMode language_mode, |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 94 | ParseRestriction restriction, int eval_scope_position, int eval_position, |
| 95 | int line_offset = 0, int column_offset = 0, |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 96 | Handle<Object> script_name = Handle<Object>(), |
| 97 | ScriptOriginOptions options = ScriptOriginOptions()); |
| 98 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 99 | // Create a (bound) function for a String source within a context for eval. |
| 100 | MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromString( |
| 101 | Handle<Context> context, Handle<String> source, |
| 102 | ParseRestriction restriction); |
| 103 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 104 | // Create a shared function info object for a String source within a context. |
| 105 | static Handle<SharedFunctionInfo> GetSharedFunctionInfoForScript( |
| 106 | Handle<String> source, Handle<Object> script_name, int line_offset, |
| 107 | int column_offset, ScriptOriginOptions resource_options, |
| 108 | Handle<Object> source_map_url, Handle<Context> context, |
| 109 | v8::Extension* extension, ScriptData** cached_data, |
| 110 | ScriptCompiler::CompileOptions compile_options, |
| 111 | NativesFlag is_natives_code, bool is_module); |
| 112 | |
| 113 | // Create a shared function info object for a Script that has already been |
| 114 | // parsed while the script was being loaded from a streamed source. |
| 115 | static Handle<SharedFunctionInfo> GetSharedFunctionInfoForStreamedScript( |
| 116 | Handle<Script> script, ParseInfo* info, int source_length); |
| 117 | |
| 118 | // Create a shared function info object (the code may be lazily compiled). |
| 119 | static Handle<SharedFunctionInfo> GetSharedFunctionInfo( |
| 120 | FunctionLiteral* node, Handle<Script> script, CompilationInfo* outer); |
| 121 | |
| 122 | // Create a shared function info object for a native function literal. |
| 123 | static Handle<SharedFunctionInfo> GetSharedFunctionInfoForNative( |
| 124 | v8::Extension* extension, Handle<String> name); |
| 125 | |
| 126 | // =========================================================================== |
| 127 | // The following family of methods provides support for OSR. Code generated |
| 128 | // for entry via OSR might not be suitable for normal entry, hence will be |
| 129 | // returned directly to the caller. |
| 130 | // |
| 131 | // Please note this interface is the only part dealing with {Code} objects |
| 132 | // directly. Other methods are agnostic to {Code} and can use an interpreter |
| 133 | // instead of generating JIT code for a function at all. |
| 134 | |
| 135 | // Generate and return optimized code for OSR, or empty handle on failure. |
| 136 | MUST_USE_RESULT static MaybeHandle<Code> GetOptimizedCodeForOSR( |
| 137 | Handle<JSFunction> function, BailoutId osr_ast_id, |
| 138 | JavaScriptFrame* osr_frame); |
| 139 | }; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 140 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 142 | // CompilationInfo encapsulates some information known at compile time. It |
| 143 | // is constructed based on the resources available at compile-time. |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 144 | class CompilationInfo final { |
| Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 145 | public: |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 146 | // Various configuration flags for a compilation, as well as some properties |
| 147 | // of the compiled code produced by a compilation. |
| 148 | enum Flag { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 149 | kDeferredCalling = 1 << 0, |
| 150 | kNonDeferredCalling = 1 << 1, |
| 151 | kSavesCallerDoubles = 1 << 2, |
| 152 | kRequiresFrame = 1 << 3, |
| 153 | kMustNotHaveEagerFrame = 1 << 4, |
| 154 | kDeoptimizationSupport = 1 << 5, |
| 155 | kDebug = 1 << 6, |
| 156 | kSerializing = 1 << 7, |
| 157 | kFunctionContextSpecializing = 1 << 8, |
| 158 | kFrameSpecializing = 1 << 9, |
| 159 | kNativeContextSpecializing = 1 << 10, |
| 160 | kInliningEnabled = 1 << 11, |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 161 | kDisableFutureOptimization = 1 << 12, |
| 162 | kSplittingEnabled = 1 << 13, |
| 163 | kDeoptimizationEnabled = 1 << 14, |
| 164 | kSourcePositionsEnabled = 1 << 15, |
| 165 | kBailoutOnUninitialized = 1 << 16, |
| 166 | kOptimizeFromBytecode = 1 << 17, |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 167 | kTypeFeedbackEnabled = 1 << 18, |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 168 | kAccessorInliningEnabled = 1 << 19, |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 169 | }; |
| Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 170 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 171 | CompilationInfo(ParseInfo* parse_info, Handle<JSFunction> closure); |
| 172 | CompilationInfo(Vector<const char> debug_name, Isolate* isolate, Zone* zone, |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 173 | Code::Flags code_flags = Code::ComputeFlags(Code::STUB)); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 174 | ~CompilationInfo(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 176 | ParseInfo* parse_info() const { return parse_info_; } |
| 177 | |
| 178 | // ----------------------------------------------------------- |
| 179 | // TODO(titzer): inline and delete accessors of ParseInfo |
| 180 | // ----------------------------------------------------------- |
| 181 | Handle<Script> script() const; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 182 | FunctionLiteral* literal() const; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 183 | DeclarationScope* scope() const; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 184 | Handle<Context> context() const; |
| 185 | Handle<SharedFunctionInfo> shared_info() const; |
| 186 | bool has_shared_info() const; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 187 | // ----------------------------------------------------------- |
| 188 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 189 | Isolate* isolate() const { |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 190 | return isolate_; |
| 191 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 192 | Zone* zone() { return zone_; } |
| 193 | bool is_osr() const { return !osr_ast_id_.IsNone(); } |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 194 | Handle<JSFunction> closure() const { return closure_; } |
| Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 195 | Handle<Code> code() const { return code_; } |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 196 | Code::Flags code_flags() const { return code_flags_; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 197 | BailoutId osr_ast_id() const { return osr_ast_id_; } |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 198 | JavaScriptFrame* osr_frame() const { return osr_frame_; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 199 | int num_parameters() const; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 200 | int num_parameters_including_this() const; |
| 201 | bool is_this_defined() const; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 202 | |
| 203 | void set_parameter_count(int parameter_count) { |
| 204 | DCHECK(IsStub()); |
| 205 | parameter_count_ = parameter_count; |
| Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 206 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 207 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 208 | bool has_bytecode_array() const { return !bytecode_array_.is_null(); } |
| 209 | Handle<BytecodeArray> bytecode_array() const { return bytecode_array_; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 211 | bool is_tracking_positions() const { return track_positions_; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 212 | |
| 213 | bool is_calling() const { |
| 214 | return GetFlag(kDeferredCalling) || GetFlag(kNonDeferredCalling); |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 215 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 216 | |
| 217 | void MarkAsDeferredCalling() { SetFlag(kDeferredCalling); } |
| 218 | |
| 219 | bool is_deferred_calling() const { return GetFlag(kDeferredCalling); } |
| 220 | |
| 221 | void MarkAsNonDeferredCalling() { SetFlag(kNonDeferredCalling); } |
| 222 | |
| 223 | bool is_non_deferred_calling() const { return GetFlag(kNonDeferredCalling); } |
| 224 | |
| 225 | void MarkAsSavesCallerDoubles() { SetFlag(kSavesCallerDoubles); } |
| 226 | |
| 227 | bool saves_caller_doubles() const { return GetFlag(kSavesCallerDoubles); } |
| 228 | |
| 229 | void MarkAsRequiresFrame() { SetFlag(kRequiresFrame); } |
| 230 | |
| 231 | bool requires_frame() const { return GetFlag(kRequiresFrame); } |
| 232 | |
| 233 | void MarkMustNotHaveEagerFrame() { SetFlag(kMustNotHaveEagerFrame); } |
| 234 | |
| 235 | bool GetMustNotHaveEagerFrame() const { |
| 236 | return GetFlag(kMustNotHaveEagerFrame); |
| 237 | } |
| 238 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 239 | // Compiles marked as debug produce unoptimized code with debug break slots. |
| 240 | // Inner functions that cannot be compiled w/o context are compiled eagerly. |
| 241 | // Always include deoptimization support to avoid having to recompile again. |
| 242 | void MarkAsDebug() { |
| 243 | SetFlag(kDebug); |
| 244 | SetFlag(kDeoptimizationSupport); |
| 245 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 246 | |
| 247 | bool is_debug() const { return GetFlag(kDebug); } |
| 248 | |
| 249 | void PrepareForSerializing() { SetFlag(kSerializing); } |
| 250 | |
| 251 | bool will_serialize() const { return GetFlag(kSerializing); } |
| 252 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 253 | void MarkAsFunctionContextSpecializing() { |
| 254 | SetFlag(kFunctionContextSpecializing); |
| 255 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 256 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 257 | bool is_function_context_specializing() const { |
| 258 | return GetFlag(kFunctionContextSpecializing); |
| 259 | } |
| 260 | |
| 261 | void MarkAsFrameSpecializing() { SetFlag(kFrameSpecializing); } |
| 262 | |
| 263 | bool is_frame_specializing() const { return GetFlag(kFrameSpecializing); } |
| 264 | |
| 265 | void MarkAsNativeContextSpecializing() { |
| 266 | SetFlag(kNativeContextSpecializing); |
| 267 | } |
| 268 | |
| 269 | bool is_native_context_specializing() const { |
| 270 | return GetFlag(kNativeContextSpecializing); |
| 271 | } |
| 272 | |
| 273 | void MarkAsDeoptimizationEnabled() { SetFlag(kDeoptimizationEnabled); } |
| 274 | |
| 275 | bool is_deoptimization_enabled() const { |
| 276 | return GetFlag(kDeoptimizationEnabled); |
| 277 | } |
| 278 | |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 279 | void MarkAsTypeFeedbackEnabled() { SetFlag(kTypeFeedbackEnabled); } |
| 280 | |
| 281 | bool is_type_feedback_enabled() const { |
| 282 | return GetFlag(kTypeFeedbackEnabled); |
| 283 | } |
| 284 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 285 | void MarkAsAccessorInliningEnabled() { SetFlag(kAccessorInliningEnabled); } |
| 286 | |
| 287 | bool is_accessor_inlining_enabled() const { |
| 288 | return GetFlag(kAccessorInliningEnabled); |
| 289 | } |
| 290 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 291 | void MarkAsSourcePositionsEnabled() { SetFlag(kSourcePositionsEnabled); } |
| 292 | |
| 293 | bool is_source_positions_enabled() const { |
| 294 | return GetFlag(kSourcePositionsEnabled); |
| 295 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 296 | |
| 297 | void MarkAsInliningEnabled() { SetFlag(kInliningEnabled); } |
| 298 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 299 | bool is_inlining_enabled() const { return GetFlag(kInliningEnabled); } |
| 300 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 301 | void MarkAsSplittingEnabled() { SetFlag(kSplittingEnabled); } |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 302 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 303 | bool is_splitting_enabled() const { return GetFlag(kSplittingEnabled); } |
| Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 304 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 305 | void MarkAsBailoutOnUninitialized() { SetFlag(kBailoutOnUninitialized); } |
| 306 | |
| 307 | bool is_bailout_on_uninitialized() const { |
| 308 | return GetFlag(kBailoutOnUninitialized); |
| 309 | } |
| 310 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 311 | void MarkAsOptimizeFromBytecode() { SetFlag(kOptimizeFromBytecode); } |
| 312 | |
| 313 | bool is_optimizing_from_bytecode() const { |
| 314 | return GetFlag(kOptimizeFromBytecode); |
| 315 | } |
| 316 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 317 | bool GeneratePreagedPrologue() const { |
| 318 | // Generate a pre-aged prologue if we are optimizing for size, which |
| 319 | // will make code flushing more aggressive. Only apply to Code::FUNCTION, |
| 320 | // since StaticMarkingVisitor::IsFlushable only flushes proper functions. |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 321 | return FLAG_optimize_for_size && FLAG_age_code && !is_debug() && |
| 322 | output_code_kind() == Code::FUNCTION; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 325 | void SetCode(Handle<Code> code) { code_ = code; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 326 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 327 | void SetBytecodeArray(Handle<BytecodeArray> bytecode_array) { |
| 328 | bytecode_array_ = bytecode_array; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | bool ShouldTrapOnDeopt() const { |
| 332 | return (FLAG_trap_on_deopt && IsOptimizing()) || |
| 333 | (FLAG_trap_on_stub_deopt && IsStub()); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 334 | } |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 335 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 336 | bool has_native_context() const { |
| 337 | return !closure().is_null() && (closure()->native_context() != nullptr); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 338 | } |
| 339 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 340 | Context* native_context() const { |
| 341 | return has_native_context() ? closure()->native_context() : nullptr; |
| 342 | } |
| 343 | |
| 344 | bool has_global_object() const { return has_native_context(); } |
| 345 | |
| 346 | JSGlobalObject* global_object() const { |
| 347 | return has_global_object() ? native_context()->global_object() : nullptr; |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // Accessors for the different compilation modes. |
| 351 | bool IsOptimizing() const { return mode_ == OPTIMIZE; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 352 | bool IsStub() const { return mode_ == STUB; } |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 353 | void SetOptimizing() { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 354 | DCHECK(has_shared_info()); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 355 | SetMode(OPTIMIZE); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 356 | optimization_id_ = isolate()->NextOptimizationId(); |
| 357 | code_flags_ = |
| 358 | Code::KindField::update(code_flags_, Code::OPTIMIZED_FUNCTION); |
| 359 | } |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 360 | void SetOptimizingForOsr(BailoutId osr_ast_id, JavaScriptFrame* osr_frame) { |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 361 | SetOptimizing(); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 362 | osr_ast_id_ = osr_ast_id; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 363 | osr_frame_ = osr_frame; |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 364 | } |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 365 | |
| 366 | // Deoptimization support. |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 367 | bool HasDeoptimizationSupport() const { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 368 | return GetFlag(kDeoptimizationSupport); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 369 | } |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 370 | void EnableDeoptimizationSupport() { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 371 | DCHECK_EQ(BASE, mode_); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 372 | SetFlag(kDeoptimizationSupport); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 373 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 374 | bool ShouldEnsureSpaceForLazyDeopt() { return !IsStub(); } |
| 375 | |
| 376 | bool ExpectsJSReceiverAsReceiver(); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 377 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 378 | // Determines whether or not to insert a self-optimization header. |
| 379 | bool ShouldSelfOptimize(); |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 380 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 381 | void set_deferred_handles(DeferredHandles* deferred_handles) { |
| 382 | DCHECK(deferred_handles_ == NULL); |
| 383 | deferred_handles_ = deferred_handles; |
| 384 | } |
| 385 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 386 | void ReopenHandlesInNewHandleScope() { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 387 | closure_ = Handle<JSFunction>(*closure_); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void AbortOptimization(BailoutReason reason) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 391 | DCHECK(reason != kNoReason); |
| 392 | if (bailout_reason_ == kNoReason) bailout_reason_ = reason; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 393 | SetFlag(kDisableFutureOptimization); |
| 394 | } |
| 395 | |
| 396 | void RetryOptimization(BailoutReason reason) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 397 | DCHECK(reason != kNoReason); |
| 398 | if (GetFlag(kDisableFutureOptimization)) return; |
| 399 | bailout_reason_ = reason; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | BailoutReason bailout_reason() const { return bailout_reason_; } |
| 403 | |
| 404 | int prologue_offset() const { |
| 405 | DCHECK_NE(Code::kPrologueOffsetNotSet, prologue_offset_); |
| 406 | return prologue_offset_; |
| 407 | } |
| 408 | |
| 409 | void set_prologue_offset(int prologue_offset) { |
| 410 | DCHECK_EQ(Code::kPrologueOffsetNotSet, prologue_offset_); |
| 411 | prologue_offset_ = prologue_offset; |
| 412 | } |
| 413 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 414 | CompilationDependencies* dependencies() { return &dependencies_; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 415 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 416 | int optimization_id() const { return optimization_id_; } |
| 417 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 418 | int osr_expr_stack_height() { return osr_expr_stack_height_; } |
| 419 | void set_osr_expr_stack_height(int height) { |
| 420 | DCHECK(height >= 0); |
| 421 | osr_expr_stack_height_ = height; |
| 422 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 423 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 424 | bool has_simple_parameters(); |
| 425 | |
| 426 | struct InlinedFunctionHolder { |
| 427 | Handle<SharedFunctionInfo> shared_info; |
| 428 | |
| 429 | // Root that holds the unoptimized code of the inlined function alive |
| 430 | // (and out of reach of code flushing) until we finish compilation. |
| 431 | // Do not remove. |
| 432 | Handle<Code> inlined_code_object_root; |
| 433 | |
| 434 | explicit InlinedFunctionHolder( |
| 435 | Handle<SharedFunctionInfo> inlined_shared_info) |
| 436 | : shared_info(inlined_shared_info), |
| 437 | inlined_code_object_root(inlined_shared_info->code()) {} |
| 438 | }; |
| 439 | |
| 440 | typedef std::vector<InlinedFunctionHolder> InlinedFunctionList; |
| 441 | InlinedFunctionList const& inlined_functions() const { |
| 442 | return inlined_functions_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 445 | void AddInlinedFunction(Handle<SharedFunctionInfo> inlined_function) { |
| 446 | inlined_functions_.push_back(InlinedFunctionHolder(inlined_function)); |
| 447 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 448 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 449 | std::unique_ptr<char[]> GetDebugName() const; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 450 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 451 | Code::Kind output_code_kind() const { |
| 452 | return Code::ExtractKindFromFlags(code_flags_); |
| 453 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 454 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 455 | StackFrame::Type GetOutputStackFrameType() const; |
| 456 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 457 | int GetDeclareGlobalsFlags() const; |
| 458 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 459 | SourcePositionTableBuilder::RecordingMode SourcePositionRecordingMode() const; |
| Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 460 | |
| Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 461 | private: |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 462 | // Compilation mode. |
| 463 | // BASE is generated by the full codegen, optionally prepared for bailouts. |
| 464 | // OPTIMIZE is optimized code generated by the Hydrogen-based backend. |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 465 | enum Mode { |
| 466 | BASE, |
| 467 | OPTIMIZE, |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 468 | STUB |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 469 | }; |
| 470 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 471 | CompilationInfo(ParseInfo* parse_info, Vector<const char> debug_name, |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 472 | Code::Flags code_flags, Mode mode, Isolate* isolate, |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 473 | Zone* zone); |
| 474 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 475 | ParseInfo* parse_info_; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 476 | Isolate* isolate_; |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 477 | |
| 478 | void SetMode(Mode mode) { |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 479 | mode_ = mode; |
| 480 | } |
| 481 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 482 | void SetFlag(Flag flag) { flags_ |= flag; } |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 483 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 484 | void SetFlag(Flag flag, bool value) { |
| 485 | flags_ = value ? flags_ | flag : flags_ & ~flag; |
| 486 | } |
| 487 | |
| 488 | bool GetFlag(Flag flag) const { return (flags_ & flag) != 0; } |
| Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 489 | |
| Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 490 | unsigned flags_; |
| 491 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 492 | Code::Flags code_flags_; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 493 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 494 | Handle<JSFunction> closure_; |
| 495 | |
| Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 496 | // The compiled code. |
| 497 | Handle<Code> code_; |
| 498 | |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 499 | // Compilation mode flag and whether deoptimization is allowed. |
| 500 | Mode mode_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 501 | BailoutId osr_ast_id_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 502 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 503 | // Holds the bytecode array generated by the interpreter. |
| 504 | // TODO(rmcilroy/mstarzinger): Temporary work-around until compiler.cc is |
| 505 | // refactored to avoid us needing to carry the BytcodeArray around. |
| 506 | Handle<BytecodeArray> bytecode_array_; |
| 507 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 508 | // The zone from which the compilation pipeline working on this |
| 509 | // CompilationInfo allocates. |
| 510 | Zone* zone_; |
| 511 | |
| 512 | DeferredHandles* deferred_handles_; |
| 513 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 514 | // Dependencies for this compilation, e.g. stable maps. |
| 515 | CompilationDependencies dependencies_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 516 | |
| 517 | BailoutReason bailout_reason_; |
| 518 | |
| 519 | int prologue_offset_; |
| 520 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 521 | bool track_positions_; |
| 522 | |
| 523 | InlinedFunctionList inlined_functions_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 524 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 525 | // Number of parameters used for compilation of stubs that require arguments. |
| 526 | int parameter_count_; |
| 527 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 528 | int optimization_id_; |
| 529 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 530 | int osr_expr_stack_height_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 531 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 532 | // The current OSR frame for specialization or {nullptr}. |
| 533 | JavaScriptFrame* osr_frame_ = nullptr; |
| 534 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 535 | Vector<const char> debug_name_; |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 536 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 537 | DISALLOW_COPY_AND_ASSIGN(CompilationInfo); |
| Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 540 | // A base class for compilation jobs intended to run concurrent to the main |
| 541 | // thread. The job is split into three phases which are called in sequence on |
| 542 | // different threads and with different limitations: |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 543 | // 1) PrepareJob: Runs on main thread. No major limitations. |
| 544 | // 2) ExecuteJob: Runs concurrently. No heap allocation or handle derefs. |
| 545 | // 3) FinalizeJob: Runs on main thread. No dependency changes. |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 546 | // |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 547 | // Each of the three phases can either fail or succeed. The current state of |
| 548 | // the job can be checked using {state()}. |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 549 | class CompilationJob { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 550 | public: |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 551 | enum Status { SUCCEEDED, FAILED }; |
| 552 | enum class State { |
| 553 | kReadyToPrepare, |
| 554 | kReadyToExecute, |
| 555 | kReadyToFinalize, |
| 556 | kSucceeded, |
| 557 | kFailed, |
| 558 | }; |
| 559 | |
| 560 | explicit CompilationJob(CompilationInfo* info, const char* compiler_name, |
| 561 | State initial_state = State::kReadyToPrepare) |
| 562 | : info_(info), compiler_name_(compiler_name), state_(initial_state) {} |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 563 | virtual ~CompilationJob() {} |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 564 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 565 | // Prepare the compile job. Must be called on the main thread. |
| 566 | MUST_USE_RESULT Status PrepareJob(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 567 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 568 | // Executes the compile job. Can be called off the main thread. |
| 569 | MUST_USE_RESULT Status ExecuteJob(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 570 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 571 | // Finalizes the compile job. Must be called on the main thread. |
| 572 | MUST_USE_RESULT Status FinalizeJob(); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 573 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 574 | // Report a transient failure, try again next time. Should only be called on |
| 575 | // optimization compilation jobs. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 576 | Status RetryOptimization(BailoutReason reason) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 577 | DCHECK(info_->IsOptimizing()); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 578 | info_->RetryOptimization(reason); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 579 | state_ = State::kFailed; |
| 580 | return FAILED; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 583 | // Report a persistent failure, disable future optimization on the function. |
| 584 | // Should only be called on optimization compilation jobs. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 585 | Status AbortOptimization(BailoutReason reason) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 586 | DCHECK(info_->IsOptimizing()); |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 587 | info_->AbortOptimization(reason); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 588 | state_ = State::kFailed; |
| 589 | return FAILED; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 592 | void RecordOptimizationStats(); |
| 593 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 594 | State state() const { return state_; } |
| 595 | CompilationInfo* info() const { return info_; } |
| 596 | Isolate* isolate() const { return info()->isolate(); } |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 597 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 598 | protected: |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 599 | // Overridden by the actual implementation. |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 600 | virtual Status PrepareJobImpl() = 0; |
| 601 | virtual Status ExecuteJobImpl() = 0; |
| 602 | virtual Status FinalizeJobImpl() = 0; |
| 603 | |
| 604 | // Registers weak object to optimized code dependencies. |
| 605 | // TODO(turbofan): Move this to pipeline.cc once Crankshaft dies. |
| 606 | void RegisterWeakObjectsInOptimizedCode(Handle<Code> code); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 607 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 608 | private: |
| 609 | CompilationInfo* info_; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 610 | base::TimeDelta time_taken_to_prepare_; |
| 611 | base::TimeDelta time_taken_to_execute_; |
| 612 | base::TimeDelta time_taken_to_finalize_; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 613 | const char* compiler_name_; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 614 | State state_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 615 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 616 | MUST_USE_RESULT Status UpdateState(Status status, State next_state) { |
| 617 | if (status == SUCCEEDED) { |
| 618 | state_ = next_state; |
| 619 | } else { |
| 620 | state_ = State::kFailed; |
| 621 | } |
| 622 | return status; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 623 | } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 624 | }; |
| 625 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 626 | } // namespace internal |
| 627 | } // namespace v8 |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 628 | |
| 629 | #endif // V8_COMPILER_H_ |