| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_COMPILER_H_ | 5 #ifndef V8_COMPILER_H_ |
| 6 #define V8_COMPILER_H_ | 6 #define V8_COMPILER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> |
| 9 | 10 |
| 10 #include "src/allocation.h" | 11 #include "src/allocation.h" |
| 11 #include "src/bailout-reason.h" | 12 #include "src/bailout-reason.h" |
| 12 #include "src/contexts.h" | 13 #include "src/contexts.h" |
| 13 #include "src/isolate.h" | 14 #include "src/isolate.h" |
| 14 #include "src/zone/zone.h" | 15 #include "src/zone/zone.h" |
| 15 | 16 |
| 16 namespace v8 { | 17 namespace v8 { |
| 17 namespace internal { | 18 namespace internal { |
| 18 | 19 |
| 19 // Forward declarations. | 20 // Forward declarations. |
| 20 class CompilationInfo; | 21 class CompilationInfo; |
| 21 class CompilationJob; | 22 class CompilationJob; |
| 22 class JavaScriptFrame; | 23 class JavaScriptFrame; |
| 23 class ParseInfo; | 24 class ParseInfo; |
| 24 class ScriptData; | 25 class ScriptData; |
| 25 | 26 |
| 27 // Used by indicate whether Compiler::GetSharedFunctionInfo should trigger a |
| 28 // compilation. |
| 29 enum class ShouldCompile { kIfNecessary, kNever }; |
| 30 |
| 26 // The V8 compiler API. | 31 // The V8 compiler API. |
| 27 // | 32 // |
| 28 // This is the central hub for dispatching to the various compilers within V8. | 33 // This is the central hub for dispatching to the various compilers within V8. |
| 29 // Logic for which compiler to choose and how to wire compilation results into | 34 // Logic for which compiler to choose and how to wire compilation results into |
| 30 // the object heap should be kept inside this class. | 35 // the object heap should be kept inside this class. |
| 31 // | 36 // |
| 32 // General strategy: Scripts are translated into anonymous functions w/o | 37 // General strategy: Scripts are translated into anonymous functions w/o |
| 33 // parameters which then can be executed. If the source code contains other | 38 // parameters which then can be executed. If the source code contains other |
| 34 // functions, they might be compiled and allocated as part of the compilation | 39 // functions, they might be compiled and allocated as part of the compilation |
| 35 // of the source code or deferred for lazy compilation at a later point. | 40 // of the source code or deferred for lazy compilation at a later point. |
| 36 class Compiler : public AllStatic { | 41 class Compiler : public AllStatic { |
| 37 public: | 42 public: |
| 38 enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION }; | 43 enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION }; |
| 39 enum ConcurrencyMode { NOT_CONCURRENT, CONCURRENT }; | 44 enum ConcurrencyMode { NOT_CONCURRENT, CONCURRENT }; |
| 40 enum CompilationTier { INTERPRETED, BASELINE, OPTIMIZED }; | 45 enum CompilationTier { INTERPRETED, BASELINE, OPTIMIZED }; |
| 41 | 46 |
| 42 // =========================================================================== | 47 // =========================================================================== |
| 43 // The following family of methods ensures a given function is compiled. The | 48 // The following family of methods ensures a given function is compiled. The |
| 44 // general contract is that failures will be reported by returning {false}, | 49 // general contract is that failures will be reported by returning {false}, |
| 45 // whereas successful compilation ensures the {is_compiled} predicate on the | 50 // whereas successful compilation ensures the {is_compiled} predicate on the |
| 46 // given function holds (except for live-edit, which compiles the world). | 51 // given function holds (except for live-edit, which compiles the world). |
| 47 | 52 |
| 48 static bool Compile(Handle<JSFunction> function, ClearExceptionFlag flag); | 53 static bool Compile(Handle<JSFunction> function, ClearExceptionFlag flag); |
| 49 static bool CompileBaseline(Handle<JSFunction> function); | 54 static bool CompileBaseline(Handle<JSFunction> function); |
| 50 static bool CompileOptimized(Handle<JSFunction> function, ConcurrencyMode); | 55 static bool CompileOptimized(Handle<JSFunction> function, ConcurrencyMode); |
| 51 static bool CompileDebugCode(Handle<JSFunction> function); | 56 static bool CompileDebugCode(Handle<JSFunction> function); |
| 52 static bool CompileDebugCode(Handle<SharedFunctionInfo> shared); | 57 static bool CompileDebugCode(Handle<SharedFunctionInfo> shared); |
| 53 static MaybeHandle<JSArray> CompileForLiveEdit(Handle<Script> script); | 58 static MaybeHandle<JSArray> CompileForLiveEdit(Handle<Script> script); |
| 54 | 59 |
| 55 // Prepare a compilation job for unoptimized code. Requires ParseAndAnalyse. | 60 // Prepare a series of compilation jobs for unoptimized code. Requires |
| 56 static CompilationJob* PrepareUnoptimizedCompilationJob( | 61 // ParseAndAnalyse. |
| 57 CompilationInfo* info); | 62 static std::vector<std::unique_ptr<CompilationJob>> |
| 63 PrepareUnoptimizedCompilationJobs(CompilationInfo* info); |
| 58 | 64 |
| 59 // Generate and install code from previously queued compilation job. | 65 // Generate and install code from previously queued compilation job. |
| 60 static bool FinalizeCompilationJob(CompilationJob* job); | 66 static bool FinalizeCompilationJob(CompilationJob* job); |
| 61 | 67 |
| 62 // Give the compiler a chance to perform low-latency initialization tasks of | 68 // Give the compiler a chance to perform low-latency initialization tasks of |
| 63 // the given {function} on its instantiation. Note that only the runtime will | 69 // the given {function} on its instantiation. Note that only the runtime will |
| 64 // offer this chance, optimized closure instantiation will not call this. | 70 // offer this chance, optimized closure instantiation will not call this. |
| 65 static void PostInstantiation(Handle<JSFunction> function, PretenureFlag); | 71 static void PostInstantiation(Handle<JSFunction> function, PretenureFlag); |
| 66 | 72 |
| 67 // Parser::Parse, then Compiler::Analyze. | 73 // Parser::Parse, then Compiler::Analyze. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 Handle<Object> source_map_url, Handle<Context> context, | 113 Handle<Object> source_map_url, Handle<Context> context, |
| 108 v8::Extension* extension, ScriptData** cached_data, | 114 v8::Extension* extension, ScriptData** cached_data, |
| 109 ScriptCompiler::CompileOptions compile_options, | 115 ScriptCompiler::CompileOptions compile_options, |
| 110 NativesFlag is_natives_code, bool is_module); | 116 NativesFlag is_natives_code, bool is_module); |
| 111 | 117 |
| 112 // Create a shared function info object for a Script that has already been | 118 // Create a shared function info object for a Script that has already been |
| 113 // parsed while the script was being loaded from a streamed source. | 119 // parsed while the script was being loaded from a streamed source. |
| 114 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForStreamedScript( | 120 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForStreamedScript( |
| 115 Handle<Script> script, ParseInfo* info, int source_length); | 121 Handle<Script> script, ParseInfo* info, int source_length); |
| 116 | 122 |
| 117 // Create a shared function info object (the code may be lazily compiled). | 123 // Create a shared function info object (the code may be lazily compiled if |
| 124 // |should_compile| is kIfNecessary). |
| 118 static Handle<SharedFunctionInfo> GetSharedFunctionInfo( | 125 static Handle<SharedFunctionInfo> GetSharedFunctionInfo( |
| 119 FunctionLiteral* node, Handle<Script> script, CompilationInfo* outer); | 126 FunctionLiteral* node, Handle<Script> script, CompilationInfo* outer, |
| 127 ShouldCompile should_compile = ShouldCompile::kIfNecessary); |
| 120 | 128 |
| 121 // Create a shared function info object for a native function literal. | 129 // Create a shared function info object for a native function literal. |
| 122 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForNative( | 130 static Handle<SharedFunctionInfo> GetSharedFunctionInfoForNative( |
| 123 v8::Extension* extension, Handle<String> name); | 131 v8::Extension* extension, Handle<String> name); |
| 124 | 132 |
| 125 // =========================================================================== | 133 // =========================================================================== |
| 126 // The following family of methods provides support for OSR. Code generated | 134 // The following family of methods provides support for OSR. Code generated |
| 127 // for entry via OSR might not be suitable for normal entry, hence will be | 135 // for entry via OSR might not be suitable for normal entry, hence will be |
| 128 // returned directly to the caller. | 136 // returned directly to the caller. |
| 129 // | 137 // |
| (...skipping 22 matching lines...) Expand all Loading... |
| 152 enum class State { | 160 enum class State { |
| 153 kReadyToPrepare, | 161 kReadyToPrepare, |
| 154 kReadyToExecute, | 162 kReadyToExecute, |
| 155 kReadyToFinalize, | 163 kReadyToFinalize, |
| 156 kSucceeded, | 164 kSucceeded, |
| 157 kFailed, | 165 kFailed, |
| 158 }; | 166 }; |
| 159 | 167 |
| 160 CompilationJob(Isolate* isolate, CompilationInfo* info, | 168 CompilationJob(Isolate* isolate, CompilationInfo* info, |
| 161 const char* compiler_name, | 169 const char* compiler_name, |
| 162 State initial_state = State::kReadyToPrepare) | 170 State initial_state = State::kReadyToPrepare); |
| 163 : info_(info), | 171 virtual ~CompilationJob(); |
| 164 compiler_name_(compiler_name), | |
| 165 state_(initial_state), | |
| 166 stack_limit_(isolate->stack_guard()->real_climit()) {} | |
| 167 virtual ~CompilationJob() {} | |
| 168 | 172 |
| 169 // Prepare the compile job. Must be called on the main thread. | 173 // Prepare the compile job. Must be called on the main thread. |
| 170 MUST_USE_RESULT Status PrepareJob(); | 174 MUST_USE_RESULT Status PrepareJob(); |
| 171 | 175 |
| 172 // Executes the compile job. Can be called on a background thread if | 176 // Executes the compile job. Can be called on a background thread if |
| 173 // can_execute_on_background_thread() returns true. | 177 // can_execute_on_background_thread() returns true. |
| 174 MUST_USE_RESULT Status ExecuteJob(); | 178 MUST_USE_RESULT Status ExecuteJob(); |
| 175 | 179 |
| 176 // Finalizes the compile job. Must be called on the main thread. | 180 // Finalizes the compile job. Must be called on the main thread. |
| 177 MUST_USE_RESULT Status FinalizeJob(); | 181 MUST_USE_RESULT Status FinalizeJob(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 189 | 193 |
| 190 virtual bool can_execute_on_background_thread() const { return true; } | 194 virtual bool can_execute_on_background_thread() const { return true; } |
| 191 | 195 |
| 192 void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } | 196 void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } |
| 193 uintptr_t stack_limit() const { return stack_limit_; } | 197 uintptr_t stack_limit() const { return stack_limit_; } |
| 194 | 198 |
| 195 State state() const { return state_; } | 199 State state() const { return state_; } |
| 196 CompilationInfo* info() const { return info_; } | 200 CompilationInfo* info() const { return info_; } |
| 197 Isolate* isolate() const; | 201 Isolate* isolate() const; |
| 198 | 202 |
| 203 // Destroy the CompilationInfo, its ParseInfo, and its zone at the end of |
| 204 // this CompilationJob's lifetime. |
| 205 void TakeOwnershipOfCompilationInfo(); |
| 206 |
| 199 protected: | 207 protected: |
| 200 // Overridden by the actual implementation. | 208 // Overridden by the actual implementation. |
| 201 virtual Status PrepareJobImpl() = 0; | 209 virtual Status PrepareJobImpl() = 0; |
| 202 virtual Status ExecuteJobImpl() = 0; | 210 virtual Status ExecuteJobImpl() = 0; |
| 203 virtual Status FinalizeJobImpl() = 0; | 211 virtual Status FinalizeJobImpl() = 0; |
| 204 | 212 |
| 205 // Registers weak object to optimized code dependencies. | 213 // Registers weak object to optimized code dependencies. |
| 206 // TODO(turbofan): Move this to pipeline.cc once Crankshaft dies. | 214 // TODO(turbofan): Move this to pipeline.cc once Crankshaft dies. |
| 207 void RegisterWeakObjectsInOptimizedCode(Handle<Code> code); | 215 void RegisterWeakObjectsInOptimizedCode(Handle<Code> code); |
| 208 | 216 |
| 209 private: | 217 private: |
| 218 // The following unique_ptr are used only for lifetime management and might |
| 219 // be empty. |
| 220 std::unique_ptr<Zone> zone_; |
| 221 std::unique_ptr<ParseInfo> parse_info_; |
| 222 std::unique_ptr<CompilationInfo> compilation_info_; |
| 223 |
| 210 CompilationInfo* info_; | 224 CompilationInfo* info_; |
| 211 base::TimeDelta time_taken_to_prepare_; | 225 base::TimeDelta time_taken_to_prepare_; |
| 212 base::TimeDelta time_taken_to_execute_; | 226 base::TimeDelta time_taken_to_execute_; |
| 213 base::TimeDelta time_taken_to_finalize_; | 227 base::TimeDelta time_taken_to_finalize_; |
| 214 const char* compiler_name_; | 228 const char* compiler_name_; |
| 215 State state_; | 229 State state_; |
| 216 uintptr_t stack_limit_; | 230 uintptr_t stack_limit_; |
| 217 | 231 |
| 218 MUST_USE_RESULT Status UpdateState(Status status, State next_state) { | 232 MUST_USE_RESULT Status UpdateState(Status status, State next_state) { |
| 219 if (status == SUCCEEDED) { | 233 if (status == SUCCEEDED) { |
| 220 state_ = next_state; | 234 state_ = next_state; |
| 221 } else { | 235 } else { |
| 222 state_ = State::kFailed; | 236 state_ = State::kFailed; |
| 223 } | 237 } |
| 224 return status; | 238 return status; |
| 225 } | 239 } |
| 240 |
| 241 DISALLOW_COPY_AND_ASSIGN(CompilationJob); |
| 226 }; | 242 }; |
| 227 | 243 |
| 228 } // namespace internal | 244 } // namespace internal |
| 229 } // namespace v8 | 245 } // namespace v8 |
| 230 | 246 |
| 231 #endif // V8_COMPILER_H_ | 247 #endif // V8_COMPILER_H_ |
| OLD | NEW |