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