Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_COMPILER_H_ |
| 4 | #define ART_SRC_COMPILER_H_ |
| 5 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 6 | #include "compiled_method.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 7 | #include "constants.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8 | #include "dex_file.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 9 | #include "jni_compiler.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 10 | #include "oat_file.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 11 | #include "object.h" |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 12 | #include "runtime.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 13 | #include "unordered_map.h" |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 14 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 17 | namespace art { |
| 18 | |
| 19 | class Compiler { |
| 20 | public: |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 21 | // Create a compiler targeting the requested "instruction_set". |
| 22 | // "image" should be true if image specific optimizations should be enabled. |
| 23 | explicit Compiler(InstructionSet instruction_set, bool image); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 24 | |
| 25 | ~Compiler(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 26 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 27 | // Compile all Methods of all the Classes of all the DexFiles that are part of a ClassLoader. |
| 28 | void CompileAll(const ClassLoader* class_loader); |
| 29 | |
| 30 | // Compile a single Method |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 31 | void CompileOne(const Method* method); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 32 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 33 | InstructionSet GetInstructionSet() const { |
| 34 | return instruction_set_; |
| 35 | } |
| 36 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 37 | bool IsImage() const { |
| 38 | return image_; |
| 39 | } |
| 40 | |
| 41 | void SetVerbose(bool verbose) { |
| 42 | verbose_ = verbose; |
| 43 | } |
| 44 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 45 | bool IsVerbose() const { |
| 46 | return verbose_; |
| 47 | } |
| 48 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 49 | // Stub to throw AbstractMethodError |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 50 | static ByteArray* CreateAbstractMethodErrorStub(InstructionSet instruction_set); |
| 51 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 52 | |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 53 | // Generate the trampoline that's invoked by unresolved direct methods |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 54 | static ByteArray* CreateResolutionStub(InstructionSet instruction_set, |
| 55 | Runtime::TrampolineType type); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 56 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame^] | 57 | static ByteArray* CreateJniDlysmLookupStub(InstructionSet instruction_set); |
| 58 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 59 | // A method is uniquely located by its DexFile and index into the method_id table of that dex file |
| 60 | typedef std::pair<const DexFile*, uint32_t> MethodReference; |
| 61 | |
| 62 | CompiledMethod* GetCompiledMethod(MethodReference ref) const; |
| 63 | const CompiledInvokeStub* FindInvokeStub(bool is_static, const char* shorty) const; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 64 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 65 | // Callbacks from OAT/ART compiler to see what runtime checks must be generated |
| 66 | bool CanAssumeTypeIsPresentInDexCache(const Method* referrer, uint32_t type_idx) const { |
| 67 | return IsImage() && referrer->GetDexCacheResolvedTypes()->Get(type_idx) != NULL; |
| 68 | } |
| 69 | bool CanAssumeStringIsPresentInDexCache(const Method* referrer, uint32_t string_idx) const { |
| 70 | return IsImage() && referrer->GetDexCacheStrings()->Get(string_idx) != NULL; |
| 71 | } |
| 72 | bool CanAccessTypeWithoutChecks(const Method* referrer, uint32_t type_idx) const { |
| 73 | Class* resolved_class = referrer->GetDexCacheResolvedTypes()->Get(type_idx); |
| 74 | // We should never ask whether a type needs access checks to raise a verification error, |
| 75 | // all other cases where this following test could fail should have been rewritten by the |
| 76 | // verifier to verification errors. |
| 77 | DCHECK(resolved_class == NULL || referrer->GetDeclaringClass()->CanAccess(resolved_class)); |
| 78 | return resolved_class != NULL; |
| 79 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 80 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 81 | private: |
| 82 | // Attempt to resolve all type, methods, fields, and strings |
| 83 | // referenced from code in the dex file following PathClassLoader |
| 84 | // ordering semantics. |
| 85 | void Resolve(const ClassLoader* class_loader); |
| 86 | void ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file); |
| 87 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 88 | void Verify(const ClassLoader* class_loader); |
| 89 | void VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file); |
| 90 | |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 91 | void InitializeClassesWithoutClinit(const ClassLoader* class_loader); |
| 92 | void InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file); |
| 93 | |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 94 | void Compile(const ClassLoader* class_loader); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 95 | void CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 96 | void CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader, |
| 97 | const DexFile& dex_file); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame^] | 98 | void CompileMethod(uint32_t access_flags, uint32_t method_idx, const ClassLoader* class_loader, |
| 99 | const DexFile& dex_file); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 100 | |
| 101 | // After compiling, walk all the DexCaches and set the code and |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 102 | // method pointers of CodeAndDirectMethods entries in the DexCaches. |
| 103 | void SetCodeAndDirectMethods(const ClassLoader* class_loader); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 104 | void SetCodeAndDirectMethodsDexFile(const DexFile& dex_file); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 105 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 106 | void InsertInvokeStub(bool is_static, const char* shorty, |
| 107 | const CompiledInvokeStub* compiled_invoke_stub); |
| 108 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 109 | InstructionSet instruction_set_; |
| 110 | JniCompiler jni_compiler_; |
| 111 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 112 | struct MethodReferenceHash { |
| 113 | size_t operator()(const MethodReference id) const { |
| 114 | size_t dex = reinterpret_cast<size_t>(id.first); |
| 115 | DCHECK_NE(dex, static_cast<size_t>(0)); |
| 116 | dex += 33; // dex is an aligned pointer, get some non-zero low bits |
| 117 | size_t idx = id.second; |
| 118 | if (idx == 0) { // special case of a method index of 0 |
| 119 | return dex * 5381; |
| 120 | } else { |
| 121 | return dex * idx; |
| 122 | } |
| 123 | } |
| 124 | }; |
| 125 | typedef std::tr1::unordered_map<const MethodReference, CompiledMethod*, MethodReferenceHash> MethodTable; |
| 126 | // All method references that this compiler has compiled |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 127 | MethodTable compiled_methods_; |
| 128 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 129 | typedef std::tr1::unordered_map<std::string, const CompiledInvokeStub*> InvokeStubTable; |
| 130 | // Invocation stubs created to allow invocation of the compiled methods |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 131 | InvokeStubTable compiled_invoke_stubs_; |
| 132 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 133 | bool image_; |
| 134 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 135 | bool verbose_; |
| 136 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 137 | DISALLOW_COPY_AND_ASSIGN(Compiler); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | } // namespace art |
| 141 | |
| 142 | #endif // ART_SRC_COMPILER_H_ |