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