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