Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "compiler.h" |
| 4 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 5 | #include <sys/mman.h> |
| 6 | |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 7 | #include "assembler.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 9 | #include "class_loader.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 10 | #include "dex_cache.h" |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 11 | #include "jni_compiler.h" |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 12 | #include "jni_internal.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 13 | #include "oat_file.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 14 | #include "object_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 15 | #include "runtime.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 16 | #include "stl_util.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 17 | |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 18 | art::CompiledMethod* oatCompileMethod(const art::Compiler& compiler, |
| 19 | const art::DexFile::CodeItem* code_item, |
| 20 | uint32_t access_flags, uint32_t method_idx, |
| 21 | const art::ClassLoader* class_loader, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 22 | const art::DexFile& dex_file, art::InstructionSet); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 26 | namespace arm { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 27 | ByteArray* CreateAbstractMethodErrorStub(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 28 | CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 29 | ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 30 | ByteArray* CreateJniDlysmLookupStub(); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 31 | } |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 32 | namespace x86 { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 33 | ByteArray* CreateAbstractMethodErrorStub(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 34 | CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 35 | ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 36 | ByteArray* CreateJniDlysmLookupStub(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 39 | Compiler::Compiler(InstructionSet instruction_set, |
| 40 | bool image, |
| 41 | const std::set<std::string>* image_classes) |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 42 | : instruction_set_(instruction_set), |
| 43 | jni_compiler_(instruction_set), |
| 44 | image_(image), |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 45 | image_classes_(image_classes) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 46 | CHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 47 | if (!image_) { |
| 48 | CHECK(image_classes_ == NULL); |
| 49 | } |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 52 | Compiler::~Compiler() { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 53 | STLDeleteValues(&compiled_classes_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 54 | STLDeleteValues(&compiled_methods_); |
| 55 | STLDeleteValues(&compiled_invoke_stubs_); |
| 56 | } |
| 57 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 58 | ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set, |
| 59 | Runtime::TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 60 | if (instruction_set == kX86) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 61 | return x86::X86CreateResolutionTrampoline(type); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 62 | } else { |
| 63 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
| 64 | // Generates resolution stub using ARM instruction set |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 65 | return arm::ArmCreateResolutionTrampoline(type); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 69 | ByteArray* Compiler::CreateJniDlysmLookupStub(InstructionSet instruction_set) { |
| 70 | switch (instruction_set) { |
| 71 | case kArm: |
| 72 | case kThumb2: |
| 73 | return arm::CreateJniDlysmLookupStub(); |
| 74 | case kX86: |
| 75 | return x86::CreateJniDlysmLookupStub(); |
| 76 | default: |
| 77 | LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set; |
| 78 | return NULL; |
| 79 | } |
| 80 | } |
| 81 | |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 82 | ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) { |
| 83 | if (instruction_set == kX86) { |
| 84 | return x86::CreateAbstractMethodErrorStub(); |
| 85 | } else { |
| 86 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
| 87 | // Generates resolution stub using ARM instruction set |
| 88 | return arm::CreateAbstractMethodErrorStub(); |
| 89 | } |
| 90 | } |
| 91 | |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 92 | void Compiler::CompileAll(const ClassLoader* class_loader, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 93 | const std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 94 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 95 | |
| 96 | PreCompile(class_loader, dex_files); |
| 97 | Compile(class_loader, dex_files); |
| 98 | PostCompile(class_loader, dex_files); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 101 | void Compiler::CompileOne(const Method* method) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 102 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 103 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 104 | const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 105 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 106 | // Find the dex_file |
| 107 | const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); |
| 108 | const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 109 | std::vector<const DexFile*> dex_files; |
| 110 | dex_files.push_back(&dex_file); |
| 111 | |
| 112 | PreCompile(class_loader, dex_files); |
| 113 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 114 | uint32_t method_idx = method->GetDexMethodIndex(); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 115 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
| 116 | CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 117 | |
| 118 | PostCompile(class_loader, dex_files); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 121 | void Compiler::Resolve(const ClassLoader* class_loader, |
| 122 | const std::vector<const DexFile*>& dex_files) { |
| 123 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 124 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 125 | CHECK(dex_file != NULL); |
| 126 | ResolveDexFile(class_loader, *dex_file); |
| 127 | } |
| 128 | } |
| 129 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 130 | void Compiler::PreCompile(const ClassLoader* class_loader, |
| 131 | const std::vector<const DexFile*>& dex_files) { |
| 132 | Resolve(class_loader, dex_files); |
| 133 | Verify(class_loader, dex_files); |
| 134 | InitializeClassesWithoutClinit(class_loader, dex_files); |
| 135 | } |
| 136 | |
| 137 | void Compiler::PostCompile(const ClassLoader* class_loader, |
| 138 | const std::vector<const DexFile*>& dex_files) { |
| 139 | SetCodeAndDirectMethods(dex_files); |
| 140 | } |
| 141 | |
| 142 | bool Compiler::IsImageClass(const std::string& descriptor) const { |
| 143 | if (image_classes_ == NULL) { |
| 144 | return true; |
| 145 | } |
| 146 | return image_classes_->find(descriptor) != image_classes_->end(); |
| 147 | } |
| 148 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 149 | bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache, |
| 150 | uint32_t type_idx) const { |
| 151 | if (!IsImage()) { |
| 152 | return false; |
| 153 | } |
| 154 | Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx); |
| 155 | if (resolved_class == NULL) { |
| 156 | return false; |
| 157 | } |
| 158 | return IsImageClass(ClassHelper(resolved_class).GetDescriptor()); |
| 159 | } |
| 160 | |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 161 | // Return true if the class should be skipped during compilation. We |
| 162 | // never skip classes in the boot class loader. However, if we have a |
| 163 | // non-boot class loader and we can resolve the class in the boot |
| 164 | // class loader, we do skip the class. This happens if an app bundles |
| 165 | // classes found in the boot classpath. Since at runtime we will |
| 166 | // select the class from the boot classpath, do not attempt to resolve |
| 167 | // or compile it now. |
| 168 | static bool SkipClass(const ClassLoader* class_loader, |
| 169 | const DexFile& dex_file, |
| 170 | const DexFile::ClassDef& class_def) { |
| 171 | if (class_loader == NULL) { |
| 172 | return false; |
| 173 | } |
| 174 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 175 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 176 | Class* klass = class_linker->FindClass(descriptor, NULL); |
| 177 | if (klass == NULL) { |
| 178 | Thread* self = Thread::Current(); |
| 179 | CHECK(self->IsExceptionPending()); |
| 180 | self->ClearException(); |
| 181 | return false; |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 186 | void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 187 | Thread* self = Thread::Current(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 188 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 189 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 190 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 191 | // Strings are easy in that they always are simply resolved to literals in the same file |
| 192 | if (image_ && image_classes_ == NULL) { |
| 193 | // TODO: Add support for loading strings referenced by image_classes_ |
| 194 | // See also Compiler::CanAssumeTypeIsPresentInDexCache. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 195 | for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) { |
| 196 | class_linker->ResolveString(dex_file, string_idx, dex_cache); |
| 197 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 200 | // Class derived values are more complicated, they require the linker and loader. |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 201 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 202 | Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 203 | if (klass == NULL) { |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 204 | CHECK(self->IsExceptionPending()); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 205 | Thread::Current()->ClearException(); |
| 206 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 207 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 208 | |
| 209 | // Method and Field are the worst. We can't resolve without either |
| 210 | // context from the code use (to disambiguate virtual vs direct |
| 211 | // method and instance vs static field) or from class |
| 212 | // definitions. While the compiler will resolve what it can as it |
| 213 | // needs it, here we try to resolve fields and methods used in class |
| 214 | // definitions, since many of them many never be referenced by |
| 215 | // generated code. |
| 216 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 217 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 218 | if (SkipClass(class_loader, dex_file, class_def)) { |
| 219 | continue; |
| 220 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 221 | |
| 222 | // Note the class_data pointer advances through the headers, |
| 223 | // static fields, instance fields, direct methods, and virtual |
| 224 | // methods. |
| 225 | const byte* class_data = dex_file.GetClassData(class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 226 | if (class_data == NULL) { |
| 227 | // empty class such as a marker interface |
| 228 | continue; |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 229 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 230 | ClassDataItemIterator it(dex_file, class_data); |
| 231 | while (it.HasNextStaticField()) { |
| 232 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 233 | class_loader, true); |
| 234 | if (field == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 235 | CHECK(self->IsExceptionPending()); |
| 236 | self->ClearException(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 237 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 238 | it.Next(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 239 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 240 | while (it.HasNextInstanceField()) { |
| 241 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 242 | class_loader, false); |
| 243 | if (field == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 244 | CHECK(self->IsExceptionPending()); |
| 245 | self->ClearException(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 246 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 247 | it.Next(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 248 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 249 | while (it.HasNextDirectMethod()) { |
| 250 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 251 | class_loader, true); |
| 252 | if (method == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 253 | CHECK(self->IsExceptionPending()); |
| 254 | self->ClearException(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 255 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 256 | it.Next(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 257 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 258 | while (it.HasNextVirtualMethod()) { |
| 259 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 260 | class_loader, false); |
| 261 | if (method == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 262 | CHECK(self->IsExceptionPending()); |
| 263 | self->ClearException(); |
| 264 | } |
| 265 | it.Next(); |
| 266 | } |
| 267 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 271 | void Compiler::Verify(const ClassLoader* class_loader, |
| 272 | const std::vector<const DexFile*>& dex_files) { |
| 273 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 274 | const DexFile* dex_file = dex_files[i]; |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 275 | CHECK(dex_file != NULL); |
| 276 | VerifyDexFile(class_loader, *dex_file); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 281 | dex_file.ChangePermissions(PROT_READ | PROT_WRITE); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 282 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 283 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 284 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 285 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 286 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 287 | if (klass == NULL) { |
| 288 | Thread* self = Thread::Current(); |
| 289 | CHECK(self->IsExceptionPending()); |
| 290 | self->ClearException(); |
| 291 | continue; |
| 292 | } |
| 293 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 294 | class_linker->VerifyClass(klass); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 295 | |
| 296 | if (klass->IsErroneous()) { |
| 297 | // ClassLinker::VerifyClass throws, which isn't useful in the compiler. |
| 298 | CHECK(Thread::Current()->IsExceptionPending()); |
| 299 | Thread::Current()->ClearException(); |
| 300 | // We want to try verification again at run-time, so move back into the resolved state. |
| 301 | klass->SetStatus(Class::kStatusResolved); |
| 302 | } |
| 303 | |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 304 | CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass); |
| 305 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException()); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 306 | } |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 307 | dex_file.ChangePermissions(PROT_READ); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 310 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, |
| 311 | const std::vector<const DexFile*>& dex_files) { |
| 312 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 313 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 314 | CHECK(dex_file != NULL); |
| 315 | InitializeClassesWithoutClinit(class_loader, *dex_file); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 320 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 321 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 322 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 323 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 324 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 325 | if (klass != NULL) { |
| 326 | class_linker->EnsureInitialized(klass, false); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 327 | // record the final class status if necessary |
| 328 | Class::Status status = klass->GetStatus(); |
| 329 | ClassReference ref(&dex_file, class_def_index); |
| 330 | CompiledClass* compiled_class = GetCompiledClass(ref); |
| 331 | if (compiled_class == NULL) { |
| 332 | compiled_class = new CompiledClass(status); |
| 333 | compiled_classes_[ref] = compiled_class; |
| 334 | } else { |
| 335 | DCHECK_EQ(status, compiled_class->GetStatus()); |
| 336 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 337 | } |
| 338 | // clear any class not found or verification exceptions |
| 339 | Thread::Current()->ClearException(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 340 | |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 344 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 345 | Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 346 | if (klass == NULL) { |
| 347 | Thread::Current()->ClearException(); |
| 348 | } else if (klass->IsInitialized()) { |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 349 | dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass); |
| 350 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 354 | void Compiler::Compile(const ClassLoader* class_loader, |
| 355 | const std::vector<const DexFile*>& dex_files) { |
| 356 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 357 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 358 | CHECK(dex_file != NULL); |
| 359 | CompileDexFile(class_loader, *dex_file); |
| 360 | } |
| 361 | } |
| 362 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 363 | void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 364 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 365 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 366 | CompileClass(class_def, class_loader, dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 370 | void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader, |
| 371 | const DexFile& dex_file) { |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 372 | if (SkipClass(class_loader, dex_file, class_def)) { |
| 373 | return; |
| 374 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 375 | const byte* class_data = dex_file.GetClassData(class_def); |
| 376 | if (class_data == NULL) { |
| 377 | // empty class, probably a marker interface |
| 378 | return; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 379 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 380 | ClassDataItemIterator it(dex_file, class_data); |
| 381 | // Skip fields |
| 382 | while (it.HasNextStaticField()) { |
| 383 | it.Next(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 384 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 385 | while (it.HasNextInstanceField()) { |
| 386 | it.Next(); |
| 387 | } |
| 388 | // Compile direct methods |
| 389 | while (it.HasNextDirectMethod()) { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 390 | CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(), |
| 391 | class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 392 | it.Next(); |
| 393 | } |
| 394 | // Compile virtual methods |
| 395 | while (it.HasNextVirtualMethod()) { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 396 | CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(), |
| 397 | class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 398 | it.Next(); |
| 399 | } |
| 400 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 403 | void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
| 404 | uint32_t method_idx, const ClassLoader* class_loader, |
| 405 | const DexFile& dex_file) { |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 406 | CompiledMethod* compiled_method = NULL; |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 407 | if ((access_flags & kAccNative) != 0) { |
| 408 | compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 409 | CHECK(compiled_method != NULL); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 410 | } else if ((access_flags & kAccAbstract) != 0) { |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 411 | } else { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 412 | compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader, |
| 413 | dex_file, kThumb2); |
| 414 | CHECK(compiled_method != NULL); |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if (compiled_method != NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 418 | MethodReference ref(&dex_file, method_idx); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 419 | CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 420 | compiled_methods_[ref] = compiled_method; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 421 | DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 422 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 423 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 424 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 425 | bool is_static = (access_flags & kAccStatic) != 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 426 | const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty); |
| 427 | if (compiled_invoke_stub == NULL) { |
| 428 | if (instruction_set_ == kX86) { |
| 429 | compiled_invoke_stub = art::x86::X86CreateInvokeStub(is_static, shorty); |
| 430 | } else { |
| 431 | CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2); |
| 432 | // Generates invocation stub using ARM instruction set |
| 433 | compiled_invoke_stub = art::arm::ArmCreateInvokeStub(is_static, shorty); |
| 434 | } |
| 435 | CHECK(compiled_invoke_stub != NULL); |
| 436 | InsertInvokeStub(is_static, shorty, compiled_invoke_stub); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 437 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 438 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 441 | static std::string MakeInvokeStubKey(bool is_static, const char* shorty) { |
| 442 | std::string key(shorty); |
| 443 | if (is_static) { |
| 444 | key += "$"; // musn't be a shorty type character |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 445 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 446 | return key; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 449 | const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 450 | const std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 451 | InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 452 | if (it == compiled_invoke_stubs_.end()) { |
| 453 | return NULL; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 454 | } else { |
| 455 | DCHECK(it->second != NULL); |
| 456 | return it->second; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void Compiler::InsertInvokeStub(bool is_static, const char* shorty, |
| 461 | const CompiledInvokeStub* compiled_invoke_stub) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 462 | std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 463 | compiled_invoke_stubs_[key] = compiled_invoke_stub; |
| 464 | } |
| 465 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 466 | CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const { |
| 467 | ClassTable::const_iterator it = compiled_classes_.find(ref); |
| 468 | if (it == compiled_classes_.end()) { |
| 469 | return NULL; |
| 470 | } |
| 471 | CHECK(it->second != NULL); |
| 472 | return it->second; |
| 473 | } |
| 474 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 475 | CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const { |
| 476 | MethodTable::const_iterator it = compiled_methods_.find(ref); |
| 477 | if (it == compiled_methods_.end()) { |
| 478 | return NULL; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 479 | } |
| 480 | CHECK(it->second != NULL); |
| 481 | return it->second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 484 | void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) { |
| 485 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 486 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 487 | CHECK(dex_file != NULL); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 488 | SetCodeAndDirectMethodsDexFile(*dex_file); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 492 | void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 493 | Runtime* runtime = Runtime::Current(); |
| 494 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 495 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 496 | CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods(); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 497 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 498 | Method* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 499 | if (method == NULL || method->IsDirect()) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 500 | Runtime::TrampolineType type = Runtime::GetTrampolineType(method); |
| 501 | ByteArray* res_trampoline = runtime->GetResolutionStubArray(type); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 502 | code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 503 | } else { |
| 504 | // TODO: we currently leave the entry blank for resolved |
| 505 | // non-direct methods. we could put in an error stub. |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 510 | } // namespace art |