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 "base/mutex.h"
21 #include "base/os.h"
22 #include "dex/dex_file.h"
23 
24 namespace art {
25 
26 namespace jit {
27 class JitCodeCache;
28 class JitLogger;
29 }  // namespace jit
30 namespace mirror {
31 class ClassLoader;
32 class DexCache;
33 }  // namespace mirror
34 
35 class ArtMethod;
36 class CompilerDriver;
37 class CompiledMethod;
38 template<class T> class Handle;
39 class OatWriter;
40 class Thread;
41 
42 enum class CopyOption {
43   kNever,
44   kAlways,
45   kOnlyIfCompressed
46 };
47 
48 class Compiler {
49  public:
50   enum Kind {
51     kQuick,
52     kOptimizing
53   };
54 
55   static Compiler* Create(CompilerDriver* driver, Kind kind);
56 
57   virtual void Init() = 0;
58 
59   virtual void UnInit() const = 0;
60 
61   virtual bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const = 0;
62 
63   virtual CompiledMethod* Compile(const DexFile::CodeItem* code_item,
64                                   uint32_t access_flags,
65                                   InvokeType invoke_type,
66                                   uint16_t class_def_idx,
67                                   uint32_t method_idx,
68                                   Handle<mirror::ClassLoader> class_loader,
69                                   const DexFile& dex_file,
70                                   Handle<mirror::DexCache> dex_cache) const = 0;
71 
72   virtual CompiledMethod* JniCompile(uint32_t access_flags,
73                                      uint32_t method_idx,
74                                      const DexFile& dex_file,
75                                      Handle<mirror::DexCache> dex_cache) const = 0;
76 
JitCompile(Thread * self ATTRIBUTE_UNUSED,jit::JitCodeCache * code_cache ATTRIBUTE_UNUSED,ArtMethod * method ATTRIBUTE_UNUSED,bool osr ATTRIBUTE_UNUSED,jit::JitLogger * jit_logger ATTRIBUTE_UNUSED)77   virtual bool JitCompile(Thread* self ATTRIBUTE_UNUSED,
78                           jit::JitCodeCache* code_cache ATTRIBUTE_UNUSED,
79                           ArtMethod* method ATTRIBUTE_UNUSED,
80                           bool osr ATTRIBUTE_UNUSED,
81                           jit::JitLogger* jit_logger ATTRIBUTE_UNUSED)
82       REQUIRES_SHARED(Locks::mutator_lock_) {
83     return false;
84   }
85 
86   virtual uintptr_t GetEntryPointOf(ArtMethod* method) const
87      REQUIRES_SHARED(Locks::mutator_lock_) = 0;
88 
GetMaximumCompilationTimeBeforeWarning()89   uint64_t GetMaximumCompilationTimeBeforeWarning() const {
90     return maximum_compilation_time_before_warning_;
91   }
92 
~Compiler()93   virtual ~Compiler() {}
94 
95   /*
96    * @brief Generate and return Dwarf CFI initialization, if supported by the
97    * backend.
98    * @param driver CompilerDriver for this compile.
99    * @returns nullptr if not supported by backend or a vector of bytes for CFI DWARF
100    * information.
101    * @note This is used for backtrace information in generated code.
102    */
GetCallFrameInformationInitialization(const CompilerDriver & driver ATTRIBUTE_UNUSED)103   virtual std::vector<uint8_t>* GetCallFrameInformationInitialization(
104       const CompilerDriver& driver ATTRIBUTE_UNUSED) const {
105     return nullptr;
106   }
107 
108   // Returns whether the method to compile is such a pathological case that
109   // it's not worth compiling.
110   static bool IsPathologicalCase(const DexFile::CodeItem& code_item,
111                                  uint32_t method_idx,
112                                  const DexFile& dex_file);
113 
114  protected:
Compiler(CompilerDriver * driver,uint64_t warning)115   Compiler(CompilerDriver* driver, uint64_t warning) :
116       driver_(driver), maximum_compilation_time_before_warning_(warning) {
117   }
118 
GetCompilerDriver()119   CompilerDriver* GetCompilerDriver() const {
120     return driver_;
121   }
122 
123  private:
124   CompilerDriver* const driver_;
125   const uint64_t maximum_compilation_time_before_warning_;
126 
127   DISALLOW_COPY_AND_ASSIGN(Compiler);
128 };
129 
130 }  // namespace art
131 
132 #endif  // ART_COMPILER_COMPILER_H_
133