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/invoke_type.h"
23 
24 namespace art {
25 
26 namespace dex {
27 struct CodeItem;
28 }  // namespace dex
29 namespace jit {
30 class JitCodeCache;
31 class JitLogger;
32 }  // namespace jit
33 namespace mirror {
34 class ClassLoader;
35 class DexCache;
36 }  // namespace mirror
37 
38 class ArtMethod;
39 class CompiledMethod;
40 class CompiledMethodStorage;
41 class CompilerOptions;
42 class DexFile;
43 template<class T> class Handle;
44 class OatWriter;
45 class Thread;
46 
47 class Compiler {
48  public:
49   enum Kind {
50     kQuick,
51     kOptimizing
52   };
53 
54   static Compiler* Create(const CompilerOptions& compiler_options,
55                           CompiledMethodStorage* storage,
56                           Kind kind);
57 
58   virtual bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const = 0;
59 
60   virtual CompiledMethod* Compile(const dex::CodeItem* code_item,
61                                   uint32_t access_flags,
62                                   InvokeType invoke_type,
63                                   uint16_t class_def_idx,
64                                   uint32_t method_idx,
65                                   Handle<mirror::ClassLoader> class_loader,
66                                   const DexFile& dex_file,
67                                   Handle<mirror::DexCache> dex_cache) const = 0;
68 
69   virtual CompiledMethod* JniCompile(uint32_t access_flags,
70                                      uint32_t method_idx,
71                                      const DexFile& dex_file,
72                                      Handle<mirror::DexCache> dex_cache) const = 0;
73 
JitCompile(Thread * self ATTRIBUTE_UNUSED,jit::JitCodeCache * code_cache ATTRIBUTE_UNUSED,ArtMethod * method ATTRIBUTE_UNUSED,bool baseline ATTRIBUTE_UNUSED,bool osr ATTRIBUTE_UNUSED,jit::JitLogger * jit_logger ATTRIBUTE_UNUSED)74   virtual bool JitCompile(Thread* self ATTRIBUTE_UNUSED,
75                           jit::JitCodeCache* code_cache ATTRIBUTE_UNUSED,
76                           ArtMethod* method ATTRIBUTE_UNUSED,
77                           bool baseline ATTRIBUTE_UNUSED,
78                           bool osr ATTRIBUTE_UNUSED,
79                           jit::JitLogger* jit_logger 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 
GetMaximumCompilationTimeBeforeWarning()87   uint64_t GetMaximumCompilationTimeBeforeWarning() const {
88     return maximum_compilation_time_before_warning_;
89   }
90 
~Compiler()91   virtual ~Compiler() {}
92 
93   // Returns whether the method to compile is such a pathological case that
94   // it's not worth compiling.
95   static bool IsPathologicalCase(const dex::CodeItem& code_item,
96                                  uint32_t method_idx,
97                                  const DexFile& dex_file);
98 
99  protected:
Compiler(const CompilerOptions & compiler_options,CompiledMethodStorage * storage,uint64_t warning)100   Compiler(const CompilerOptions& compiler_options,
101            CompiledMethodStorage* storage,
102            uint64_t warning) :
103       compiler_options_(compiler_options),
104       storage_(storage),
105       maximum_compilation_time_before_warning_(warning) {
106   }
107 
GetCompilerOptions()108   const CompilerOptions& GetCompilerOptions() const {
109     return compiler_options_;
110   }
111 
GetCompiledMethodStorage()112   CompiledMethodStorage* GetCompiledMethodStorage() const {
113     return storage_;
114   }
115 
116  private:
117   const CompilerOptions& compiler_options_;
118   CompiledMethodStorage* const storage_;
119   const uint64_t maximum_compilation_time_before_warning_;
120 
121   DISALLOW_COPY_AND_ASSIGN(Compiler);
122 };
123 
124 }  // namespace art
125 
126 #endif  // ART_COMPILER_COMPILER_H_
127