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