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 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 6 | #include <map> |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame^] | 7 | #include <set> |
| 8 | #include <string> |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 9 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 10 | #include "compiled_class.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 11 | #include "compiled_method.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 12 | #include "constants.h" |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 13 | #include "dex_cache.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 14 | #include "dex_file.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 15 | #include "jni_compiler.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 16 | #include "oat_file.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 17 | #include "object.h" |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 18 | #include "runtime.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 19 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 20 | namespace art { |
| 21 | |
| 22 | class Compiler { |
| 23 | public: |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 24 | // Create a compiler targeting the requested "instruction_set". |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 25 | // "image" should be true if image specific optimizations should be |
| 26 | // enabled. "image_classes" lets the compiler know what classes it |
| 27 | // can assume will be in the image, with NULL implying all available |
| 28 | // classes. |
| 29 | explicit Compiler(InstructionSet instruction_set, |
| 30 | bool image, |
| 31 | const std::set<std::string>* image_classes); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 32 | |
| 33 | ~Compiler(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 34 | |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 35 | void CompileAll(const ClassLoader* class_loader, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 36 | const std::vector<const DexFile*>& dex_files); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 37 | |
| 38 | // Compile a single Method |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 39 | void CompileOne(const Method* method); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 40 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 41 | InstructionSet GetInstructionSet() const { |
| 42 | return instruction_set_; |
| 43 | } |
| 44 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 45 | bool IsImage() const { |
| 46 | return image_; |
| 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 | |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame^] | 57 | static ByteArray* CreateJniDlsymLookupStub(InstructionSet instruction_set); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 58 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 59 | // A class is uniquely located by its DexFile and the class_defs_ table index into that DexFile |
| 60 | typedef std::pair<const DexFile*, uint32_t> ClassReference; |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame^] | 61 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 62 | CompiledClass* GetCompiledClass(ClassReference ref) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 63 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 64 | // A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile |
| 65 | typedef std::pair<const DexFile*, uint32_t> MethodReference; |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame^] | 66 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 67 | CompiledMethod* GetCompiledMethod(MethodReference ref) const; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 68 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 69 | const CompiledInvokeStub* FindInvokeStub(bool is_static, const char* shorty) const; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 70 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 71 | // 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] | 72 | bool CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache, uint32_t type_idx) const; |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 73 | bool CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache, uint32_t string_idx) const { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 74 | // TODO: Add support for loading strings referenced by image_classes_ |
| 75 | // See also Compiler::ResolveDexFile |
| 76 | return IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 77 | } |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 78 | bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache, |
| 79 | const DexFile& dex_file, uint32_t type_idx) const { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 80 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 81 | // We should never ask whether a type needs access checks to raise a verification error, |
| 82 | // 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] | 83 | // verifier to verification errors. Also need to handle a lack of knowledge at compile time. |
| 84 | #ifndef NDEBUG |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 85 | const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx); |
| 86 | Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 87 | DCHECK(resolved_class == NULL || referrer_class == NULL || |
| 88 | referrer_class->CanAccess(resolved_class)); |
| 89 | #endif |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 90 | return resolved_class != NULL; |
| 91 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 92 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 93 | private: |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 94 | |
| 95 | // Checks if class specified by type_idx is one of the image_classes_ |
| 96 | bool IsImageClass(const std::string& descriptor) const; |
| 97 | |
| 98 | void PreCompile(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files); |
| 99 | void PostCompile(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files); |
| 100 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 101 | // Attempt to resolve all type, methods, fields, and strings |
| 102 | // referenced from code in the dex file following PathClassLoader |
| 103 | // ordering semantics. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 104 | 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] | 105 | void ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file); |
| 106 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 107 | void Verify(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 108 | void VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file); |
| 109 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 110 | 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] | 111 | void InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file); |
| 112 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 113 | void Compile(const ClassLoader* class_loader, |
| 114 | const std::vector<const DexFile*>& dex_files); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 115 | void CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 116 | void CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader, |
| 117 | const DexFile& dex_file); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 118 | void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, uint32_t method_idx, |
| 119 | const ClassLoader* class_loader, const DexFile& dex_file); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 120 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 121 | void SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files); |
| 122 | void SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file); |
| 123 | void SetGcMapsMethod(const DexFile& dex_file, Method* method); |
| 124 | |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 125 | // After compiling, walk all the DexCaches and set the code and |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 126 | // method pointers of CodeAndDirectMethods entries in the DexCaches. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 127 | void SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 128 | void SetCodeAndDirectMethodsDexFile(const DexFile& dex_file); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 129 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 130 | void InsertInvokeStub(bool is_static, const char* shorty, |
| 131 | const CompiledInvokeStub* compiled_invoke_stub); |
| 132 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 133 | InstructionSet instruction_set_; |
| 134 | JniCompiler jni_compiler_; |
| 135 | |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame^] | 136 | typedef std::map<const ClassReference, CompiledClass*> ClassTable; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 137 | // All class references that this compiler has compiled |
| 138 | ClassTable compiled_classes_; |
| 139 | |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame^] | 140 | typedef std::map<const MethodReference, CompiledMethod*> MethodTable; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 141 | // All method references that this compiler has compiled |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 142 | MethodTable compiled_methods_; |
| 143 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 144 | typedef std::map<std::string, const CompiledInvokeStub*> InvokeStubTable; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 145 | // Invocation stubs created to allow invocation of the compiled methods |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 146 | InvokeStubTable compiled_invoke_stubs_; |
| 147 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 148 | bool image_; |
| 149 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 150 | const std::set<std::string>* image_classes_; |
| 151 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 152 | DISALLOW_COPY_AND_ASSIGN(Compiler); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame^] | 155 | inline bool operator<(const Compiler::ClassReference& lhs, const Compiler::ClassReference& rhs) { |
| 156 | if (lhs.second < rhs.second) { |
| 157 | return true; |
| 158 | } else if (lhs.second > rhs.second) { |
| 159 | return false; |
| 160 | } else { |
| 161 | return (lhs.first < rhs.first); |
| 162 | } |
| 163 | } |
| 164 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 165 | } // namespace art |
| 166 | |
| 167 | #endif // ART_SRC_COMPILER_H_ |