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