Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 16 | |
| 17 | #include "compiler.h" |
| 18 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 21 | #include "assembler.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 22 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 23 | #include "class_loader.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 24 | #include "dex_cache.h" |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 25 | #include "dex_verifier.h" |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 26 | #include "jni_compiler.h" |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 27 | #include "jni_internal.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 28 | #include "oat_file.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 29 | #include "object_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 30 | #include "runtime.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 31 | #include "stl_util.h" |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame^] | 32 | #include "timing_logger.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 33 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 34 | namespace art { |
| 35 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 36 | CompiledMethod* oatCompileMethod(const Compiler& compiler, const DexFile::CodeItem* code_item, |
| 37 | uint32_t access_flags, uint32_t method_idx, |
| 38 | const ClassLoader* class_loader, |
| 39 | const DexFile& dex_file, InstructionSet); |
| 40 | |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 41 | namespace arm { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 42 | ByteArray* CreateAbstractMethodErrorStub(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 43 | CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 44 | ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type); |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 45 | ByteArray* CreateJniDlsymLookupStub(); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 46 | } |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 47 | namespace x86 { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 48 | ByteArray* CreateAbstractMethodErrorStub(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 49 | CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 50 | ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type); |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 51 | ByteArray* CreateJniDlsymLookupStub(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 54 | Compiler::Compiler(InstructionSet instruction_set, |
| 55 | bool image, |
| 56 | const std::set<std::string>* image_classes) |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 57 | : instruction_set_(instruction_set), |
| 58 | jni_compiler_(instruction_set), |
| 59 | image_(image), |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 60 | dex_file_count_(0), |
| 61 | class_count_(0), |
| 62 | abstract_method_count_(0), |
| 63 | native_method_count_(0), |
| 64 | regular_method_count_(0), |
| 65 | instruction_count_(0), |
| 66 | start_ns_(NanoTime()), |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 67 | image_classes_(image_classes) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 68 | CHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 69 | if (!image_) { |
| 70 | CHECK(image_classes_ == NULL); |
| 71 | } |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 74 | Compiler::~Compiler() { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 75 | STLDeleteValues(&compiled_classes_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 76 | STLDeleteValues(&compiled_methods_); |
| 77 | STLDeleteValues(&compiled_invoke_stubs_); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 78 | if (dex_file_count_ > 0) { |
| 79 | uint64_t duration_ns = NanoTime() - start_ns_; |
Elliott Hughes | aa56b72 | 2012-01-26 13:25:35 -0800 | [diff] [blame] | 80 | std::string stats(StringPrintf("Compiled files:%zd" |
| 81 | " classes:%zd" |
| 82 | " methods:(abstract:%zd" |
| 83 | " native:%zd" |
| 84 | " regular:%zd)" |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 85 | " instructions:%zd", |
Brian Carlstrom | fc0842b | 2012-01-26 11:41:11 -0800 | [diff] [blame] | 86 | dex_file_count_, |
| 87 | class_count_, |
| 88 | abstract_method_count_, |
| 89 | native_method_count_, |
| 90 | regular_method_count_, |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 91 | instruction_count_)); |
| 92 | stats += " (took ", |
| 93 | stats += PrettyDuration(duration_ns); |
Brian Carlstrom | fc0842b | 2012-01-26 11:41:11 -0800 | [diff] [blame] | 94 | if (instruction_count_ != 0) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 95 | stats += ", "; |
| 96 | stats += PrettyDuration(duration_ns / instruction_count_); |
| 97 | stats += "/instruction"; |
Brian Carlstrom | fc0842b | 2012-01-26 11:41:11 -0800 | [diff] [blame] | 98 | } |
| 99 | stats += ")"; |
| 100 | LOG(INFO) << stats; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 101 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 104 | ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set, |
| 105 | Runtime::TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 106 | if (instruction_set == kX86) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 107 | return x86::X86CreateResolutionTrampoline(type); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 108 | } else { |
| 109 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
| 110 | // Generates resolution stub using ARM instruction set |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 111 | return arm::ArmCreateResolutionTrampoline(type); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 115 | ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 116 | switch (instruction_set) { |
| 117 | case kArm: |
| 118 | case kThumb2: |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 119 | return arm::CreateJniDlsymLookupStub(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 120 | case kX86: |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 121 | return x86::CreateJniDlsymLookupStub(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 122 | default: |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 123 | LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 124 | return NULL; |
| 125 | } |
| 126 | } |
| 127 | |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 128 | ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) { |
| 129 | if (instruction_set == kX86) { |
| 130 | return x86::CreateAbstractMethodErrorStub(); |
| 131 | } else { |
| 132 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
| 133 | // Generates resolution stub using ARM instruction set |
| 134 | return arm::CreateAbstractMethodErrorStub(); |
| 135 | } |
| 136 | } |
| 137 | |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 138 | void Compiler::CompileAll(const ClassLoader* class_loader, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 139 | const std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 140 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 141 | |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame^] | 142 | TimingLogger timings("compiler"); |
| 143 | |
| 144 | PreCompile(class_loader, dex_files, timings); |
| 145 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 146 | Compile(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame^] | 147 | timings.AddSplit("Compile"); |
| 148 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 149 | PostCompile(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame^] | 150 | timings.AddSplit("PostCompile"); |
| 151 | |
| 152 | if (timings.GetTotalNs() > MsToNs(1000)) { |
| 153 | timings.Dump(); |
| 154 | } |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 157 | void Compiler::CompileOne(const Method* method) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 158 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 159 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 160 | const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 161 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 162 | // Find the dex_file |
| 163 | const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); |
| 164 | const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 165 | std::vector<const DexFile*> dex_files; |
| 166 | dex_files.push_back(&dex_file); |
| 167 | |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame^] | 168 | TimingLogger timings("CompileOne"); |
| 169 | PreCompile(class_loader, dex_files, timings); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 170 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 171 | uint32_t method_idx = method->GetDexMethodIndex(); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 172 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
| 173 | CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 174 | |
| 175 | PostCompile(class_loader, dex_files); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 178 | void Compiler::Resolve(const ClassLoader* class_loader, |
| 179 | const std::vector<const DexFile*>& dex_files) { |
| 180 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 181 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 182 | CHECK(dex_file != NULL); |
| 183 | ResolveDexFile(class_loader, *dex_file); |
| 184 | } |
| 185 | } |
| 186 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 187 | void Compiler::PreCompile(const ClassLoader* class_loader, |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame^] | 188 | const std::vector<const DexFile*>& dex_files, TimingLogger& timings) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 189 | Resolve(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame^] | 190 | timings.AddSplit("PreCompile.Resolve"); |
| 191 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 192 | Verify(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame^] | 193 | timings.AddSplit("PreCompile.Verify"); |
| 194 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 195 | InitializeClassesWithoutClinit(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame^] | 196 | timings.AddSplit("PreCompile.InitializeClassesWithoutClinit"); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void Compiler::PostCompile(const ClassLoader* class_loader, |
| 200 | const std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 201 | SetGcMaps(class_loader, dex_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 202 | SetCodeAndDirectMethods(dex_files); |
| 203 | } |
| 204 | |
| 205 | bool Compiler::IsImageClass(const std::string& descriptor) const { |
| 206 | if (image_classes_ == NULL) { |
| 207 | return true; |
| 208 | } |
| 209 | return image_classes_->find(descriptor) != image_classes_->end(); |
| 210 | } |
| 211 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 212 | bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache, |
| 213 | uint32_t type_idx) const { |
| 214 | if (!IsImage()) { |
| 215 | return false; |
| 216 | } |
| 217 | Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx); |
| 218 | if (resolved_class == NULL) { |
| 219 | return false; |
| 220 | } |
| 221 | return IsImageClass(ClassHelper(resolved_class).GetDescriptor()); |
| 222 | } |
| 223 | |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 224 | // Return true if the class should be skipped during compilation. We |
| 225 | // never skip classes in the boot class loader. However, if we have a |
| 226 | // non-boot class loader and we can resolve the class in the boot |
| 227 | // class loader, we do skip the class. This happens if an app bundles |
| 228 | // classes found in the boot classpath. Since at runtime we will |
| 229 | // select the class from the boot classpath, do not attempt to resolve |
| 230 | // or compile it now. |
| 231 | static bool SkipClass(const ClassLoader* class_loader, |
| 232 | const DexFile& dex_file, |
| 233 | const DexFile::ClassDef& class_def) { |
| 234 | if (class_loader == NULL) { |
| 235 | return false; |
| 236 | } |
| 237 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 238 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 239 | Class* klass = class_linker->FindClass(descriptor, NULL); |
| 240 | if (klass == NULL) { |
| 241 | Thread* self = Thread::Current(); |
| 242 | CHECK(self->IsExceptionPending()); |
| 243 | self->ClearException(); |
| 244 | return false; |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 249 | void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 250 | Thread* self = Thread::Current(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 251 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 252 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 253 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 254 | // Strings are easy in that they always are simply resolved to literals in the same file |
| 255 | if (image_ && image_classes_ == NULL) { |
| 256 | // TODO: Add support for loading strings referenced by image_classes_ |
| 257 | // See also Compiler::CanAssumeTypeIsPresentInDexCache. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 258 | for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) { |
| 259 | class_linker->ResolveString(dex_file, string_idx, dex_cache); |
| 260 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 263 | // Class derived values are more complicated, they require the linker and loader. |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 264 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 265 | 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] | 266 | if (klass == NULL) { |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 267 | CHECK(self->IsExceptionPending()); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 268 | Thread::Current()->ClearException(); |
| 269 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 270 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 271 | |
| 272 | // Method and Field are the worst. We can't resolve without either |
| 273 | // context from the code use (to disambiguate virtual vs direct |
| 274 | // method and instance vs static field) or from class |
| 275 | // definitions. While the compiler will resolve what it can as it |
| 276 | // needs it, here we try to resolve fields and methods used in class |
| 277 | // definitions, since many of them many never be referenced by |
| 278 | // generated code. |
| 279 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 280 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 281 | if (SkipClass(class_loader, dex_file, class_def)) { |
| 282 | continue; |
| 283 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 284 | |
| 285 | // Note the class_data pointer advances through the headers, |
| 286 | // static fields, instance fields, direct methods, and virtual |
| 287 | // methods. |
| 288 | const byte* class_data = dex_file.GetClassData(class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 289 | if (class_data == NULL) { |
| 290 | // empty class such as a marker interface |
| 291 | continue; |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 292 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 293 | ClassDataItemIterator it(dex_file, class_data); |
| 294 | while (it.HasNextStaticField()) { |
| 295 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 296 | class_loader, true); |
| 297 | if (field == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 298 | CHECK(self->IsExceptionPending()); |
| 299 | self->ClearException(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 300 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 301 | it.Next(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 302 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 303 | while (it.HasNextInstanceField()) { |
| 304 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 305 | class_loader, false); |
| 306 | if (field == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 307 | CHECK(self->IsExceptionPending()); |
| 308 | self->ClearException(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 309 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 310 | it.Next(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 311 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 312 | while (it.HasNextDirectMethod()) { |
| 313 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 314 | class_loader, true); |
| 315 | if (method == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 316 | CHECK(self->IsExceptionPending()); |
| 317 | self->ClearException(); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 318 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 319 | it.Next(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 320 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 321 | while (it.HasNextVirtualMethod()) { |
| 322 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 323 | class_loader, false); |
| 324 | if (method == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 325 | CHECK(self->IsExceptionPending()); |
| 326 | self->ClearException(); |
| 327 | } |
| 328 | it.Next(); |
| 329 | } |
| 330 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 334 | void Compiler::Verify(const ClassLoader* class_loader, |
| 335 | const std::vector<const DexFile*>& dex_files) { |
| 336 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 337 | const DexFile* dex_file = dex_files[i]; |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 338 | CHECK(dex_file != NULL); |
| 339 | VerifyDexFile(class_loader, *dex_file); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 344 | dex_file.ChangePermissions(PROT_READ | PROT_WRITE); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 345 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 346 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 347 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 348 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 349 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 350 | if (klass == NULL) { |
| 351 | Thread* self = Thread::Current(); |
| 352 | CHECK(self->IsExceptionPending()); |
| 353 | self->ClearException(); |
| 354 | continue; |
| 355 | } |
| 356 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 357 | class_linker->VerifyClass(klass); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 358 | |
| 359 | if (klass->IsErroneous()) { |
| 360 | // ClassLinker::VerifyClass throws, which isn't useful in the compiler. |
| 361 | CHECK(Thread::Current()->IsExceptionPending()); |
| 362 | Thread::Current()->ClearException(); |
| 363 | // We want to try verification again at run-time, so move back into the resolved state. |
| 364 | klass->SetStatus(Class::kStatusResolved); |
| 365 | } |
| 366 | |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 367 | CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass); |
| 368 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException()); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 369 | } |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 370 | dex_file.ChangePermissions(PROT_READ); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 373 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, |
| 374 | const std::vector<const DexFile*>& dex_files) { |
| 375 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 376 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 377 | CHECK(dex_file != NULL); |
| 378 | InitializeClassesWithoutClinit(class_loader, *dex_file); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 383 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 384 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 385 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 386 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 387 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 388 | if (klass != NULL) { |
| 389 | class_linker->EnsureInitialized(klass, false); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 390 | // record the final class status if necessary |
| 391 | Class::Status status = klass->GetStatus(); |
| 392 | ClassReference ref(&dex_file, class_def_index); |
| 393 | CompiledClass* compiled_class = GetCompiledClass(ref); |
| 394 | if (compiled_class == NULL) { |
| 395 | compiled_class = new CompiledClass(status); |
| 396 | compiled_classes_[ref] = compiled_class; |
| 397 | } else { |
| 398 | DCHECK_EQ(status, compiled_class->GetStatus()); |
| 399 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 400 | } |
| 401 | // clear any class not found or verification exceptions |
| 402 | Thread::Current()->ClearException(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 406 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 407 | 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] | 408 | if (klass == NULL) { |
| 409 | Thread::Current()->ClearException(); |
| 410 | } else if (klass->IsInitialized()) { |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 411 | dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass); |
| 412 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 416 | void Compiler::Compile(const ClassLoader* class_loader, |
| 417 | const std::vector<const DexFile*>& dex_files) { |
| 418 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 419 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 420 | CHECK(dex_file != NULL); |
| 421 | CompileDexFile(class_loader, *dex_file); |
| 422 | } |
| 423 | } |
| 424 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 425 | void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 426 | ++dex_file_count_; |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 427 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 428 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 429 | CompileClass(class_def, class_loader, dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 433 | void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader, |
| 434 | const DexFile& dex_file) { |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 435 | if (SkipClass(class_loader, dex_file, class_def)) { |
| 436 | return; |
| 437 | } |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 438 | ++class_count_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 439 | const byte* class_data = dex_file.GetClassData(class_def); |
| 440 | if (class_data == NULL) { |
| 441 | // empty class, probably a marker interface |
| 442 | return; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 443 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 444 | ClassDataItemIterator it(dex_file, class_data); |
| 445 | // Skip fields |
| 446 | while (it.HasNextStaticField()) { |
| 447 | it.Next(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 448 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 449 | while (it.HasNextInstanceField()) { |
| 450 | it.Next(); |
| 451 | } |
| 452 | // Compile direct methods |
| 453 | while (it.HasNextDirectMethod()) { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 454 | CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(), |
| 455 | class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 456 | it.Next(); |
| 457 | } |
| 458 | // Compile virtual methods |
| 459 | while (it.HasNextVirtualMethod()) { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 460 | CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(), |
| 461 | class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 462 | it.Next(); |
| 463 | } |
| 464 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 467 | void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
| 468 | uint32_t method_idx, const ClassLoader* class_loader, |
| 469 | const DexFile& dex_file) { |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 470 | CompiledMethod* compiled_method = NULL; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 471 | uint64_t start_ns = NanoTime(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 472 | if ((access_flags & kAccNative) != 0) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 473 | ++native_method_count_; |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 474 | 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] | 475 | CHECK(compiled_method != NULL); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 476 | } else if ((access_flags & kAccAbstract) != 0) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 477 | ++abstract_method_count_; |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 478 | } else { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 479 | ++regular_method_count_; |
| 480 | instruction_count_ += code_item->insns_size_in_code_units_; |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 481 | compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader, |
| 482 | dex_file, kThumb2); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 483 | CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file); |
| 484 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 485 | uint64_t duration_ns = NanoTime() - start_ns; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 486 | if (duration_ns > MsToNs(100)) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 487 | LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file) |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 488 | << " took " << PrettyDuration(duration_ns); |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if (compiled_method != NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 492 | MethodReference ref(&dex_file, method_idx); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 493 | CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 494 | compiled_methods_[ref] = compiled_method; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 495 | DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 496 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 497 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 498 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 499 | bool is_static = (access_flags & kAccStatic) != 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 500 | const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty); |
| 501 | if (compiled_invoke_stub == NULL) { |
| 502 | if (instruction_set_ == kX86) { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 503 | compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 504 | } else { |
| 505 | CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2); |
| 506 | // Generates invocation stub using ARM instruction set |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 507 | compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 508 | } |
| 509 | CHECK(compiled_invoke_stub != NULL); |
| 510 | InsertInvokeStub(is_static, shorty, compiled_invoke_stub); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 511 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 512 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 515 | static std::string MakeInvokeStubKey(bool is_static, const char* shorty) { |
| 516 | std::string key(shorty); |
| 517 | if (is_static) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 518 | key += "$"; // Must not be a shorty type character. |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 519 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 520 | return key; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 523 | const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 524 | const std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 525 | InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 526 | if (it == compiled_invoke_stubs_.end()) { |
| 527 | return NULL; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 528 | } else { |
| 529 | DCHECK(it->second != NULL); |
| 530 | return it->second; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | void Compiler::InsertInvokeStub(bool is_static, const char* shorty, |
| 535 | const CompiledInvokeStub* compiled_invoke_stub) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 536 | std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 537 | compiled_invoke_stubs_[key] = compiled_invoke_stub; |
| 538 | } |
| 539 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 540 | CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const { |
| 541 | ClassTable::const_iterator it = compiled_classes_.find(ref); |
| 542 | if (it == compiled_classes_.end()) { |
| 543 | return NULL; |
| 544 | } |
| 545 | CHECK(it->second != NULL); |
| 546 | return it->second; |
| 547 | } |
| 548 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 549 | CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const { |
| 550 | MethodTable::const_iterator it = compiled_methods_.find(ref); |
| 551 | if (it == compiled_methods_.end()) { |
| 552 | return NULL; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 553 | } |
| 554 | CHECK(it->second != NULL); |
| 555 | return it->second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 558 | void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) { |
| 559 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 560 | const DexFile* dex_file = dex_files[i]; |
| 561 | CHECK(dex_file != NULL); |
| 562 | SetGcMapsDexFile(class_loader, *dex_file); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 567 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 568 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 569 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 570 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 571 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 572 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
| 573 | if (klass == NULL || !klass->IsVerified()) { |
| 574 | Thread::Current()->ClearException(); |
| 575 | continue; |
| 576 | } |
| 577 | const byte* class_data = dex_file.GetClassData(class_def); |
| 578 | if (class_data == NULL) { |
| 579 | // empty class such as a marker interface |
| 580 | continue; |
| 581 | } |
| 582 | ClassDataItemIterator it(dex_file, class_data); |
| 583 | while (it.HasNextStaticField()) { |
| 584 | it.Next(); |
| 585 | } |
| 586 | while (it.HasNextInstanceField()) { |
| 587 | it.Next(); |
| 588 | } |
| 589 | while (it.HasNextDirectMethod()) { |
| 590 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 591 | class_loader, true); |
| 592 | SetGcMapsMethod(dex_file, method); |
| 593 | it.Next(); |
| 594 | } |
| 595 | while (it.HasNextVirtualMethod()) { |
| 596 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 597 | class_loader, false); |
| 598 | SetGcMapsMethod(dex_file, method); |
| 599 | it.Next(); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) { |
| 605 | if (method == NULL) { |
| 606 | Thread::Current()->ClearException(); |
| 607 | return; |
| 608 | } |
| 609 | uint16_t method_idx = method->GetDexMethodIndex(); |
| 610 | MethodReference ref(&dex_file, method_idx); |
| 611 | CompiledMethod* compiled_method = GetCompiledMethod(ref); |
| 612 | if (compiled_method == NULL) { |
| 613 | return; |
| 614 | } |
| 615 | const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref); |
| 616 | if (gc_map == NULL) { |
| 617 | return; |
| 618 | } |
| 619 | compiled_method->SetGcMap(*gc_map); |
| 620 | } |
| 621 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 622 | void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) { |
| 623 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 624 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 625 | CHECK(dex_file != NULL); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 626 | SetCodeAndDirectMethodsDexFile(*dex_file); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 630 | void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 631 | Runtime* runtime = Runtime::Current(); |
| 632 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 633 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 634 | CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods(); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 635 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 636 | Method* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 637 | if (method == NULL || method->IsDirect()) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 638 | Runtime::TrampolineType type = Runtime::GetTrampolineType(method); |
| 639 | ByteArray* res_trampoline = runtime->GetResolutionStubArray(type); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 640 | code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 641 | } else { |
| 642 | // TODO: we currently leave the entry blank for resolved |
| 643 | // non-direct methods. we could put in an error stub. |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 648 | } // namespace art |