Home | History | Annotate | Download | only in compiler-dispatcher
      1 // Copyright 2016 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_JOB_H_
      6 #define V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_JOB_H_
      7 
      8 #include <memory>
      9 
     10 #include "include/v8.h"
     11 #include "src/base/macros.h"
     12 #include "src/globals.h"
     13 #include "src/handles.h"
     14 #include "testing/gtest/include/gtest/gtest_prod.h"
     15 
     16 namespace v8 {
     17 namespace internal {
     18 
     19 class AstValueFactory;
     20 class CompilerDispatcherTracer;
     21 class CompilationInfo;
     22 class CompilationJob;
     23 class DeferredHandles;
     24 class FunctionLiteral;
     25 class Isolate;
     26 class ParseInfo;
     27 class Parser;
     28 class SharedFunctionInfo;
     29 class String;
     30 class UnicodeCache;
     31 class Utf16CharacterStream;
     32 
     33 enum class CompileJobStatus {
     34   kInitial,
     35   kReadyToParse,
     36   kParsed,
     37   kReadyToAnalyze,
     38   kAnalyzed,
     39   kReadyToCompile,
     40   kCompiled,
     41   kFailed,
     42   kDone,
     43 };
     44 
     45 class V8_EXPORT_PRIVATE CompilerDispatcherJob {
     46  public:
     47   // Creates a CompilerDispatcherJob in the initial state.
     48   CompilerDispatcherJob(Isolate* isolate, CompilerDispatcherTracer* tracer,
     49                         Handle<SharedFunctionInfo> shared,
     50                         size_t max_stack_size);
     51   // Creates a CompilerDispatcherJob in the analyzed state.
     52   CompilerDispatcherJob(Isolate* isolate, CompilerDispatcherTracer* tracer,
     53                         Handle<Script> script,
     54                         Handle<SharedFunctionInfo> shared,
     55                         FunctionLiteral* literal,
     56                         std::shared_ptr<Zone> parse_zone,
     57                         std::shared_ptr<DeferredHandles> parse_handles,
     58                         std::shared_ptr<DeferredHandles> compile_handles,
     59                         size_t max_stack_size);
     60   ~CompilerDispatcherJob();
     61 
     62   CompileJobStatus status() const { return status_; }
     63 
     64   Context* context() { return *context_; }
     65 
     66   // Returns true if this CompilerDispatcherJob was created for the given
     67   // function.
     68   bool IsAssociatedWith(Handle<SharedFunctionInfo> shared) const;
     69 
     70   // Transition from kInitial to kReadyToParse.
     71   void PrepareToParseOnMainThread();
     72 
     73   // Transition from kReadyToParse to kParsed.
     74   void Parse();
     75 
     76   // Transition from kParsed to kReadyToAnalyze (or kFailed). Returns false
     77   // when transitioning to kFailed. In that case, an exception is pending.
     78   bool FinalizeParsingOnMainThread();
     79 
     80   // Transition from kReadyToAnalyze to kAnalyzed (or kFailed). Returns
     81   // false when transitioning to kFailed. In that case, an exception is pending.
     82   bool AnalyzeOnMainThread();
     83 
     84   // Transition from kAnalyzed to kReadyToCompile (or kFailed). Returns
     85   // false when transitioning to kFailed. In that case, an exception is pending.
     86   bool PrepareToCompileOnMainThread();
     87 
     88   // Transition from kReadyToCompile to kCompiled.
     89   void Compile();
     90 
     91   // Transition from kCompiled to kDone (or kFailed). Returns false when
     92   // transitioning to kFailed. In that case, an exception is pending.
     93   bool FinalizeCompilingOnMainThread();
     94 
     95   // Transition from any state to kInitial and free all resources.
     96   void ResetOnMainThread();
     97 
     98   // Estimate how long the next step will take using the tracer.
     99   double EstimateRuntimeOfNextStepInMs() const;
    100 
    101   // Even though the name does not imply this, ShortPrint() must only be invoked
    102   // on the main thread.
    103   void ShortPrint();
    104 
    105  private:
    106   FRIEND_TEST(CompilerDispatcherJobTest, ScopeChain);
    107 
    108   CompileJobStatus status_;
    109   Isolate* isolate_;
    110   CompilerDispatcherTracer* tracer_;
    111   Handle<Context> context_;            // Global handle.
    112   Handle<SharedFunctionInfo> shared_;  // Global handle.
    113   Handle<String> source_;        // Global handle.
    114   Handle<String> wrapper_;       // Global handle.
    115   std::unique_ptr<v8::String::ExternalStringResourceBase> source_wrapper_;
    116   size_t max_stack_size_;
    117 
    118   // Members required for parsing.
    119   std::unique_ptr<UnicodeCache> unicode_cache_;
    120   std::unique_ptr<Utf16CharacterStream> character_stream_;
    121   std::unique_ptr<ParseInfo> parse_info_;
    122   std::unique_ptr<Parser> parser_;
    123 
    124   // Members required for compiling a parsed function.
    125   std::shared_ptr<Zone> parse_zone_;
    126 
    127   // Members required for compiling.
    128   std::unique_ptr<CompilationInfo> compile_info_;
    129   std::unique_ptr<CompilationJob> compile_job_;
    130 
    131   bool trace_compiler_dispatcher_jobs_;
    132 
    133   DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherJob);
    134 };
    135 
    136 }  // namespace internal
    137 }  // namespace v8
    138 
    139 #endif  // V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_JOB_H_
    140