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() { |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 309 | // Switch to kVmWait while we're blocked waiting for the other threads to finish. |
| 310 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 311 | STLDeleteElements(&threads_); |
| 312 | } |
| 313 | |
| 314 | private: |
| 315 | std::vector<WorkerThread*> threads_; |
| 316 | }; |
| 317 | |
| 318 | static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) { |
| 319 | const DexFile& dex_file = *context->dex_file; |
| 320 | |
| 321 | // Method and Field are the worst. We can't resolve without either |
| 322 | // context from the code use (to disambiguate virtual vs direct |
| 323 | // method and instance vs static field) or from class |
| 324 | // definitions. While the compiler will resolve what it can as it |
| 325 | // needs it, here we try to resolve fields and methods used in class |
| 326 | // definitions, since many of them many never be referenced by |
| 327 | // generated code. |
| 328 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 329 | if (SkipClass(context->class_loader, dex_file, class_def)) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | // Note the class_data pointer advances through the headers, |
| 334 | // static fields, instance fields, direct methods, and virtual |
| 335 | // methods. |
| 336 | const byte* class_data = dex_file.GetClassData(class_def); |
| 337 | if (class_data == NULL) { |
| 338 | // empty class such as a marker interface |
| 339 | return; |
| 340 | } |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 341 | Thread* self = Thread::Current(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 342 | ClassLinker* class_linker = context->class_linker; |
| 343 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 344 | ClassDataItemIterator it(dex_file, class_data); |
| 345 | while (it.HasNextStaticField()) { |
| 346 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 347 | context->class_loader, true); |
| 348 | if (field == NULL) { |
| 349 | CHECK(self->IsExceptionPending()); |
| 350 | self->ClearException(); |
| 351 | } |
| 352 | it.Next(); |
| 353 | } |
| 354 | while (it.HasNextInstanceField()) { |
| 355 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 356 | context->class_loader, false); |
| 357 | if (field == NULL) { |
| 358 | CHECK(self->IsExceptionPending()); |
| 359 | self->ClearException(); |
| 360 | } |
| 361 | it.Next(); |
| 362 | } |
| 363 | while (it.HasNextDirectMethod()) { |
| 364 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 365 | context->class_loader, true); |
| 366 | if (method == NULL) { |
| 367 | CHECK(self->IsExceptionPending()); |
| 368 | self->ClearException(); |
| 369 | } |
| 370 | it.Next(); |
| 371 | } |
| 372 | while (it.HasNextVirtualMethod()) { |
| 373 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 374 | context->class_loader, false); |
| 375 | if (method == NULL) { |
| 376 | CHECK(self->IsExceptionPending()); |
| 377 | self->ClearException(); |
| 378 | } |
| 379 | it.Next(); |
| 380 | } |
| 381 | DCHECK(!it.HasNext()); |
| 382 | } |
| 383 | |
| 384 | static void ResolveType(Context* context, size_t type_idx) { |
| 385 | // Class derived values are more complicated, they require the linker and loader. |
| 386 | Thread* self = Thread::Current(); |
| 387 | ClassLinker* class_linker = context->class_linker; |
| 388 | const DexFile& dex_file = *context->dex_file; |
| 389 | Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader); |
| 390 | if (klass == NULL) { |
| 391 | CHECK(self->IsExceptionPending()); |
| 392 | Thread::Current()->ClearException(); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | 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] | 397 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 398 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 399 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 400 | // Strings are easy in that they always are simply resolved to literals in the same file |
| 401 | if (image_ && image_classes_ == NULL) { |
| 402 | // TODO: Add support for loading strings referenced by image_classes_ |
| 403 | // See also Compiler::CanAssumeTypeIsPresentInDexCache. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 404 | for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) { |
| 405 | class_linker->ResolveString(dex_file, string_idx, dex_cache); |
| 406 | } |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 407 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings"); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 410 | Context context; |
| 411 | context.class_linker = class_linker; |
| 412 | context.class_loader = class_loader; |
| 413 | context.dex_cache = dex_cache; |
| 414 | context.dex_file = &dex_file; |
| 415 | |
| 416 | { |
| 417 | Workers workers(&context, 0, dex_cache->NumResolvedTypes(), ResolveType); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 418 | } |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 419 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types"); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 420 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 421 | { |
| 422 | Workers workers(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 423 | } |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 424 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields"); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 427 | void Compiler::Verify(const ClassLoader* class_loader, |
| 428 | const std::vector<const DexFile*>& dex_files) { |
| 429 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 430 | const DexFile* dex_file = dex_files[i]; |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 431 | CHECK(dex_file != NULL); |
| 432 | VerifyDexFile(class_loader, *dex_file); |
| 433 | } |
| 434 | } |
| 435 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 436 | static void VerifyClass(Context* context, size_t class_def_index) { |
| 437 | const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index); |
| 438 | const char* descriptor = context->dex_file->GetClassDescriptor(class_def); |
| 439 | Class* klass = context->class_linker->FindClass(descriptor, context->class_loader); |
| 440 | if (klass == NULL) { |
| 441 | Thread* self = Thread::Current(); |
| 442 | CHECK(self->IsExceptionPending()); |
| 443 | self->ClearException(); |
| 444 | return; |
| 445 | } |
| 446 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
| 447 | context->class_linker->VerifyClass(klass); |
| 448 | |
| 449 | if (klass->IsErroneous()) { |
| 450 | // ClassLinker::VerifyClass throws, which isn't useful in the compiler. |
| 451 | CHECK(Thread::Current()->IsExceptionPending()); |
| 452 | Thread::Current()->ClearException(); |
| 453 | // We want to try verification again at run-time, so move back into the resolved state. |
| 454 | klass->SetStatus(Class::kStatusResolved); |
| 455 | } |
| 456 | |
| 457 | CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass); |
| 458 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException()); |
| 459 | } |
| 460 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 461 | void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 462 | dex_file.ChangePermissions(PROT_READ | PROT_WRITE); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 463 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 464 | Context context; |
| 465 | context.class_linker = Runtime::Current()->GetClassLinker(); |
| 466 | context.class_loader = class_loader; |
| 467 | context.dex_file = &dex_file; |
| 468 | Workers workers(&context, 0, dex_file.NumClassDefs(), VerifyClass); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 469 | |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 470 | dex_file.ChangePermissions(PROT_READ); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 473 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, |
| 474 | const std::vector<const DexFile*>& dex_files) { |
| 475 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 476 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 477 | CHECK(dex_file != NULL); |
| 478 | InitializeClassesWithoutClinit(class_loader, *dex_file); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 483 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 484 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 485 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 486 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 487 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 488 | if (klass != NULL) { |
| 489 | class_linker->EnsureInitialized(klass, false); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 490 | // record the final class status if necessary |
| 491 | Class::Status status = klass->GetStatus(); |
| 492 | ClassReference ref(&dex_file, class_def_index); |
| 493 | CompiledClass* compiled_class = GetCompiledClass(ref); |
| 494 | if (compiled_class == NULL) { |
| 495 | compiled_class = new CompiledClass(status); |
| 496 | compiled_classes_[ref] = compiled_class; |
| 497 | } else { |
| 498 | DCHECK_EQ(status, compiled_class->GetStatus()); |
| 499 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 500 | } |
| 501 | // clear any class not found or verification exceptions |
| 502 | Thread::Current()->ClearException(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 506 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 507 | 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] | 508 | if (klass == NULL) { |
| 509 | Thread::Current()->ClearException(); |
| 510 | } else if (klass->IsInitialized()) { |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 511 | dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass); |
| 512 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 516 | void Compiler::Compile(const ClassLoader* class_loader, |
| 517 | const std::vector<const DexFile*>& dex_files) { |
| 518 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 519 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 520 | CHECK(dex_file != NULL); |
| 521 | CompileDexFile(class_loader, *dex_file); |
| 522 | } |
| 523 | } |
| 524 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 525 | void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 526 | ++dex_file_count_; |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 527 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 528 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 529 | CompileClass(class_def, class_loader, dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 533 | void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader, |
| 534 | const DexFile& dex_file) { |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 535 | if (SkipClass(class_loader, dex_file, class_def)) { |
| 536 | return; |
| 537 | } |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 538 | ++class_count_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 539 | const byte* class_data = dex_file.GetClassData(class_def); |
| 540 | if (class_data == NULL) { |
| 541 | // empty class, probably a marker interface |
| 542 | return; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 543 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 544 | ClassDataItemIterator it(dex_file, class_data); |
| 545 | // Skip fields |
| 546 | while (it.HasNextStaticField()) { |
| 547 | it.Next(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 548 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 549 | while (it.HasNextInstanceField()) { |
| 550 | it.Next(); |
| 551 | } |
| 552 | // Compile direct methods |
| 553 | while (it.HasNextDirectMethod()) { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 554 | CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(), |
| 555 | class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 556 | it.Next(); |
| 557 | } |
| 558 | // Compile virtual methods |
| 559 | while (it.HasNextVirtualMethod()) { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 560 | CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(), |
| 561 | class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 562 | it.Next(); |
| 563 | } |
| 564 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 567 | void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
| 568 | uint32_t method_idx, const ClassLoader* class_loader, |
| 569 | const DexFile& dex_file) { |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 570 | CompiledMethod* compiled_method = NULL; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 571 | uint64_t start_ns = NanoTime(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 572 | if ((access_flags & kAccNative) != 0) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 573 | ++native_method_count_; |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 574 | 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] | 575 | CHECK(compiled_method != NULL); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 576 | } else if ((access_flags & kAccAbstract) != 0) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 577 | ++abstract_method_count_; |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 578 | } else { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 579 | ++regular_method_count_; |
| 580 | instruction_count_ += code_item->insns_size_in_code_units_; |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 581 | compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader, |
| 582 | dex_file, kThumb2); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 583 | CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file); |
| 584 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 585 | uint64_t duration_ns = NanoTime() - start_ns; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 586 | if (duration_ns > MsToNs(100)) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 587 | LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file) |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 588 | << " took " << PrettyDuration(duration_ns); |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | if (compiled_method != NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 592 | MethodReference ref(&dex_file, method_idx); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 593 | CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 594 | compiled_methods_[ref] = compiled_method; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 595 | DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 596 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 597 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 598 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 599 | bool is_static = (access_flags & kAccStatic) != 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 600 | const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty); |
| 601 | if (compiled_invoke_stub == NULL) { |
| 602 | if (instruction_set_ == kX86) { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 603 | compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 604 | } else { |
| 605 | CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2); |
| 606 | // Generates invocation stub using ARM instruction set |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 607 | compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 608 | } |
| 609 | CHECK(compiled_invoke_stub != NULL); |
| 610 | InsertInvokeStub(is_static, shorty, compiled_invoke_stub); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 611 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 612 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 615 | static std::string MakeInvokeStubKey(bool is_static, const char* shorty) { |
| 616 | std::string key(shorty); |
| 617 | if (is_static) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 618 | key += "$"; // Must not be a shorty type character. |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 619 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 620 | return key; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 623 | const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 624 | const std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 625 | InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 626 | if (it == compiled_invoke_stubs_.end()) { |
| 627 | return NULL; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 628 | } else { |
| 629 | DCHECK(it->second != NULL); |
| 630 | return it->second; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | void Compiler::InsertInvokeStub(bool is_static, const char* shorty, |
| 635 | const CompiledInvokeStub* compiled_invoke_stub) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 636 | std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 637 | compiled_invoke_stubs_[key] = compiled_invoke_stub; |
| 638 | } |
| 639 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 640 | CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const { |
| 641 | ClassTable::const_iterator it = compiled_classes_.find(ref); |
| 642 | if (it == compiled_classes_.end()) { |
| 643 | return NULL; |
| 644 | } |
| 645 | CHECK(it->second != NULL); |
| 646 | return it->second; |
| 647 | } |
| 648 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 649 | CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const { |
| 650 | MethodTable::const_iterator it = compiled_methods_.find(ref); |
| 651 | if (it == compiled_methods_.end()) { |
| 652 | return NULL; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 653 | } |
| 654 | CHECK(it->second != NULL); |
| 655 | return it->second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 658 | void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) { |
| 659 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 660 | const DexFile* dex_file = dex_files[i]; |
| 661 | CHECK(dex_file != NULL); |
| 662 | SetGcMapsDexFile(class_loader, *dex_file); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 667 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 668 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 669 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 670 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 671 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 672 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
| 673 | if (klass == NULL || !klass->IsVerified()) { |
| 674 | Thread::Current()->ClearException(); |
| 675 | continue; |
| 676 | } |
| 677 | const byte* class_data = dex_file.GetClassData(class_def); |
| 678 | if (class_data == NULL) { |
| 679 | // empty class such as a marker interface |
| 680 | continue; |
| 681 | } |
| 682 | ClassDataItemIterator it(dex_file, class_data); |
| 683 | while (it.HasNextStaticField()) { |
| 684 | it.Next(); |
| 685 | } |
| 686 | while (it.HasNextInstanceField()) { |
| 687 | it.Next(); |
| 688 | } |
| 689 | while (it.HasNextDirectMethod()) { |
| 690 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 691 | class_loader, true); |
| 692 | SetGcMapsMethod(dex_file, method); |
| 693 | it.Next(); |
| 694 | } |
| 695 | while (it.HasNextVirtualMethod()) { |
| 696 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 697 | class_loader, false); |
| 698 | SetGcMapsMethod(dex_file, method); |
| 699 | it.Next(); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) { |
| 705 | if (method == NULL) { |
| 706 | Thread::Current()->ClearException(); |
| 707 | return; |
| 708 | } |
| 709 | uint16_t method_idx = method->GetDexMethodIndex(); |
| 710 | MethodReference ref(&dex_file, method_idx); |
| 711 | CompiledMethod* compiled_method = GetCompiledMethod(ref); |
| 712 | if (compiled_method == NULL) { |
| 713 | return; |
| 714 | } |
| 715 | const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref); |
| 716 | if (gc_map == NULL) { |
| 717 | return; |
| 718 | } |
| 719 | compiled_method->SetGcMap(*gc_map); |
| 720 | } |
| 721 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 722 | void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) { |
| 723 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 724 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 725 | CHECK(dex_file != NULL); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 726 | SetCodeAndDirectMethodsDexFile(*dex_file); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 730 | void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 731 | Runtime* runtime = Runtime::Current(); |
| 732 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 733 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 734 | CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods(); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 735 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 736 | Method* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 737 | if (method == NULL || method->IsDirect()) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 738 | Runtime::TrampolineType type = Runtime::GetTrampolineType(method); |
| 739 | ByteArray* res_trampoline = runtime->GetResolutionStubArray(type); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 740 | code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 741 | } else { |
| 742 | // TODO: we currently leave the entry blank for resolved |
| 743 | // non-direct methods. we could put in an error stub. |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 748 | } // namespace art |