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