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