Home | History | Annotate | Download | only in compiler
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ART_COMPILER_COMPILER_H_
     18 #define ART_COMPILER_COMPILER_H_
     19 
     20 #include "dex_file.h"
     21 #include "base/mutex.h"
     22 #include "os.h"
     23 
     24 namespace art {
     25 
     26 namespace jit {
     27   class JitCodeCache;
     28 }
     29 namespace mirror {
     30   class ClassLoader;
     31   class DexCache;
     32 }
     33 
     34 class ArtMethod;
     35 class CompilerDriver;
     36 class CompiledMethod;
     37 template<class T> class Handle;
     38 class OatWriter;
     39 class Thread;
     40 
     41 class Compiler {
     42  public:
     43   enum Kind {
     44     kQuick,
     45     kOptimizing
     46   };
     47 
     48   enum JniOptimizationFlags {
     49     kNone                       = 0x0,
     50     kFastNative                 = 0x1,
     51     kCriticalNative             = 0x2,
     52   };
     53 
     54   static Compiler* Create(CompilerDriver* driver, Kind kind);
     55 
     56   virtual void Init() = 0;
     57 
     58   virtual void UnInit() const = 0;
     59 
     60   virtual bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const = 0;
     61 
     62   virtual CompiledMethod* Compile(const DexFile::CodeItem* code_item,
     63                                   uint32_t access_flags,
     64                                   InvokeType invoke_type,
     65                                   uint16_t class_def_idx,
     66                                   uint32_t method_idx,
     67                                   Handle<mirror::ClassLoader> class_loader,
     68                                   const DexFile& dex_file,
     69                                   Handle<mirror::DexCache> dex_cache) const = 0;
     70 
     71   virtual CompiledMethod* JniCompile(uint32_t access_flags,
     72                                      uint32_t method_idx,
     73                                      const DexFile& dex_file,
     74                                      JniOptimizationFlags optimization_flags) const = 0;
     75 
     76   virtual bool JitCompile(Thread* self ATTRIBUTE_UNUSED,
     77                           jit::JitCodeCache* code_cache ATTRIBUTE_UNUSED,
     78                           ArtMethod* method ATTRIBUTE_UNUSED,
     79                           bool osr ATTRIBUTE_UNUSED)
     80       REQUIRES_SHARED(Locks::mutator_lock_) {
     81     return false;
     82   }
     83 
     84   virtual uintptr_t GetEntryPointOf(ArtMethod* method) const
     85      REQUIRES_SHARED(Locks::mutator_lock_) = 0;
     86 
     87   uint64_t GetMaximumCompilationTimeBeforeWarning() const {
     88     return maximum_compilation_time_before_warning_;
     89   }
     90 
     91   virtual ~Compiler() {}
     92 
     93   /*
     94    * @brief Generate and return Dwarf CFI initialization, if supported by the
     95    * backend.
     96    * @param driver CompilerDriver for this compile.
     97    * @returns nullptr if not supported by backend or a vector of bytes for CFI DWARF
     98    * information.
     99    * @note This is used for backtrace information in generated code.
    100    */
    101   virtual std::vector<uint8_t>* GetCallFrameInformationInitialization(
    102       const CompilerDriver& driver ATTRIBUTE_UNUSED) const {
    103     return nullptr;
    104   }
    105 
    106   // Returns whether the method to compile is such a pathological case that
    107   // it's not worth compiling.
    108   static bool IsPathologicalCase(const DexFile::CodeItem& code_item,
    109                                  uint32_t method_idx,
    110                                  const DexFile& dex_file);
    111 
    112  protected:
    113   Compiler(CompilerDriver* driver, uint64_t warning) :
    114       driver_(driver), maximum_compilation_time_before_warning_(warning) {
    115   }
    116 
    117   CompilerDriver* GetCompilerDriver() const {
    118     return driver_;
    119   }
    120 
    121  private:
    122   CompilerDriver* const driver_;
    123   const uint64_t maximum_compilation_time_before_warning_;
    124 
    125   DISALLOW_COPY_AND_ASSIGN(Compiler);
    126 };
    127 
    128 }  // namespace art
    129 
    130 #endif  // ART_COMPILER_COMPILER_H_
    131