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" |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 27 | #include "compiler/CompilerIR.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 28 | #include "dex_cache.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 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame^] | 37 | #if defined(ART_USE_LLVM_COMPILER) |
| 38 | #include "compiler_llvm/compiler_llvm.h" |
| 39 | #endif |
| 40 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 41 | namespace art { |
| 42 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame^] | 43 | #if !defined(ART_USE_LLVM_COMPILER) |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 44 | CompiledMethod* oatCompileMethod(Compiler& compiler, const DexFile::CodeItem* code_item, |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 45 | uint32_t access_flags, uint32_t method_idx, |
| 46 | const ClassLoader* class_loader, |
| 47 | const DexFile& dex_file, InstructionSet); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame^] | 48 | #endif |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 49 | |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 50 | namespace arm { |
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* ArmCreateInvokeStub(bool is_static, const char* shorty); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 53 | ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type); |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 54 | ByteArray* CreateJniDlsymLookupStub(); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 55 | } |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 56 | namespace x86 { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 57 | ByteArray* CreateAbstractMethodErrorStub(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 58 | CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 59 | ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type); |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 60 | ByteArray* CreateJniDlsymLookupStub(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 63 | static double Percentage(size_t x, size_t y) { |
| 64 | return 100.0 * ((double)x) / ((double)(x + y)); |
| 65 | } |
| 66 | |
| 67 | static void DumpStat(size_t x, size_t y, const char* str) { |
| 68 | if (x == 0 && y == 0) { |
| 69 | return; |
| 70 | } |
| 71 | LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases"; |
| 72 | } |
| 73 | |
| 74 | void AOTCompilationStats::Dump() { |
| 75 | DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache"); |
| 76 | DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache"); |
| 77 | DumpStat(resolved_types_, unresolved_types_, "types resolved"); |
| 78 | DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved"); |
| 79 | DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_, |
| 80 | "static fields resolved"); |
| 81 | DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_, |
| 82 | "static fields local to a class"); |
| 83 | DumpStat(resolved_virtual_methods_, unresolved_virtual_methods_, "resolved virtual methods"); |
| 84 | DumpStat(resolved_super_methods_, unresolved_super_methods_, "resolved super-class methods"); |
| 85 | DumpStat(resolved_interface_methods_, unresolved_interface_methods_, "resolved interface methods"); |
| 86 | } |
| 87 | |
| 88 | // Allow lossy statistics in non-debug builds |
| 89 | #ifndef NDEBUG |
| 90 | #define STATS_LOCK() MutexLock mu(stats_lock_) |
| 91 | #else |
| 92 | #define STATS_LOCK() |
| 93 | #endif |
| 94 | |
| 95 | void AOTCompilationStats::TypeInDexCache() { |
| 96 | STATS_LOCK(); |
| 97 | types_in_dex_cache_++; |
| 98 | } |
| 99 | |
| 100 | void AOTCompilationStats::TypeNotInDexCache() { |
| 101 | STATS_LOCK(); |
| 102 | types_not_in_dex_cache_++; |
| 103 | } |
| 104 | |
| 105 | void AOTCompilationStats::StringInDexCache() { |
| 106 | STATS_LOCK(); |
| 107 | strings_in_dex_cache_++; |
| 108 | } |
| 109 | |
| 110 | void AOTCompilationStats::StringNotInDexCache() { |
| 111 | STATS_LOCK(); |
| 112 | strings_not_in_dex_cache_++; |
| 113 | } |
| 114 | |
| 115 | void AOTCompilationStats::TypeDoesntNeedAccessCheck() { |
| 116 | STATS_LOCK(); |
| 117 | resolved_types_++; |
| 118 | } |
| 119 | |
| 120 | void AOTCompilationStats::TypeNeedsAccessCheck() { |
| 121 | STATS_LOCK(); |
| 122 | unresolved_types_++; |
| 123 | } |
| 124 | |
| 125 | void AOTCompilationStats::ResolvedInstanceField() { |
| 126 | STATS_LOCK(); |
| 127 | resolved_instance_fields_++; |
| 128 | } |
| 129 | |
| 130 | void AOTCompilationStats::UnresolvedInstanceField(){ |
| 131 | STATS_LOCK(); |
| 132 | unresolved_instance_fields_++; |
| 133 | } |
| 134 | |
| 135 | void AOTCompilationStats::ResolvedLocalStaticField() { |
| 136 | STATS_LOCK(); |
| 137 | resolved_local_static_fields_++; |
| 138 | } |
| 139 | |
| 140 | void AOTCompilationStats::ResolvedStaticField() { |
| 141 | STATS_LOCK(); |
| 142 | resolved_static_fields_++; |
| 143 | } |
| 144 | |
| 145 | void AOTCompilationStats::UnresolvedStaticField() { |
| 146 | STATS_LOCK(); |
| 147 | unresolved_static_fields_++; |
| 148 | } |
| 149 | |
| 150 | void AOTCompilationStats::ResolvedMethod(bool is_interface, bool is_super) { |
| 151 | STATS_LOCK(); |
| 152 | if (is_interface) { |
| 153 | resolved_interface_methods_++; |
| 154 | } else if (is_super) { |
| 155 | resolved_super_methods_++; |
| 156 | } else { |
| 157 | resolved_virtual_methods_++; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void AOTCompilationStats::UnresolvedMethod(bool is_interface, bool is_super) { |
| 162 | STATS_LOCK(); |
| 163 | if (is_interface) { |
| 164 | unresolved_interface_methods_++; |
| 165 | } else if (is_super) { |
| 166 | unresolved_super_methods_++; |
| 167 | } else { |
| 168 | unresolved_virtual_methods_++; |
| 169 | } |
| 170 | } |
| 171 | |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 172 | Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 173 | const std::set<std::string>* image_classes) |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 174 | : instruction_set_(instruction_set), |
| 175 | jni_compiler_(instruction_set), |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 176 | compiled_classes_lock_("compiled classes lock"), |
| 177 | compiled_methods_lock_("compiled method lock"), |
| 178 | compiled_invoke_stubs_lock_("compiled invoke stubs lock"), |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 179 | image_(image), |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 180 | thread_count_(thread_count), |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame^] | 181 | image_classes_(image_classes) |
| 182 | #if defined(ART_USE_LLVM_COMPILER) |
| 183 | , |
| 184 | compiler_llvm_(new compiler_llvm::CompilerLLVM(this, instruction_set)) |
| 185 | #endif |
| 186 | { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 187 | CHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 188 | if (!image_) { |
| 189 | CHECK(image_classes_ == NULL); |
| 190 | } |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 193 | Compiler::~Compiler() { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 194 | { |
| 195 | MutexLock mu(compiled_classes_lock_); |
| 196 | STLDeleteValues(&compiled_classes_); |
| 197 | } |
| 198 | { |
| 199 | MutexLock mu(compiled_methods_lock_); |
| 200 | STLDeleteValues(&compiled_methods_); |
| 201 | } |
| 202 | { |
| 203 | MutexLock mu(compiled_invoke_stubs_lock_); |
| 204 | STLDeleteValues(&compiled_invoke_stubs_); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 205 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 208 | ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set, |
| 209 | Runtime::TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 210 | if (instruction_set == kX86) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 211 | return x86::X86CreateResolutionTrampoline(type); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 212 | } else { |
| 213 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
| 214 | // Generates resolution stub using ARM instruction set |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 215 | return arm::ArmCreateResolutionTrampoline(type); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 219 | ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 220 | switch (instruction_set) { |
| 221 | case kArm: |
| 222 | case kThumb2: |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 223 | return arm::CreateJniDlsymLookupStub(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 224 | case kX86: |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 225 | return x86::CreateJniDlsymLookupStub(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 226 | default: |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 227 | LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 228 | return NULL; |
| 229 | } |
| 230 | } |
| 231 | |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 232 | ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) { |
| 233 | if (instruction_set == kX86) { |
| 234 | return x86::CreateAbstractMethodErrorStub(); |
| 235 | } else { |
| 236 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
| 237 | // Generates resolution stub using ARM instruction set |
| 238 | return arm::CreateAbstractMethodErrorStub(); |
| 239 | } |
| 240 | } |
| 241 | |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 242 | void Compiler::CompileAll(const ClassLoader* class_loader, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 243 | const std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 244 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 245 | |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 246 | TimingLogger timings("compiler"); |
| 247 | |
| 248 | PreCompile(class_loader, dex_files, timings); |
| 249 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 250 | Compile(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 251 | timings.AddSplit("Compile"); |
| 252 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 253 | PostCompile(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 254 | timings.AddSplit("PostCompile"); |
| 255 | |
| 256 | if (timings.GetTotalNs() > MsToNs(1000)) { |
| 257 | timings.Dump(); |
| 258 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 259 | |
| 260 | stats_.Dump(); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 263 | void Compiler::CompileOne(const Method* method) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 264 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 265 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 266 | const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 267 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 268 | // Find the dex_file |
| 269 | const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); |
| 270 | const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 271 | std::vector<const DexFile*> dex_files; |
| 272 | dex_files.push_back(&dex_file); |
| 273 | |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 274 | TimingLogger timings("CompileOne"); |
| 275 | PreCompile(class_loader, dex_files, timings); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 276 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 277 | uint32_t method_idx = method->GetDexMethodIndex(); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 278 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
| 279 | CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 280 | |
| 281 | PostCompile(class_loader, dex_files); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 284 | void Compiler::Resolve(const ClassLoader* class_loader, |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 285 | const std::vector<const DexFile*>& dex_files, TimingLogger& timings) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 286 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 287 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 288 | CHECK(dex_file != NULL); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 289 | ResolveDexFile(class_loader, *dex_file, timings); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 293 | void Compiler::PreCompile(const ClassLoader* class_loader, |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 294 | const std::vector<const DexFile*>& dex_files, TimingLogger& timings) { |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 295 | Resolve(class_loader, dex_files, timings); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 296 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 297 | Verify(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 298 | timings.AddSplit("PreCompile.Verify"); |
| 299 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 300 | InitializeClassesWithoutClinit(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 301 | timings.AddSplit("PreCompile.InitializeClassesWithoutClinit"); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void Compiler::PostCompile(const ClassLoader* class_loader, |
| 305 | const std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 306 | SetGcMaps(class_loader, dex_files); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame^] | 307 | #if defined(ART_USE_LLVM_COMPILER) |
| 308 | compiler_llvm_->MaterializeLLVMModule(); |
| 309 | #endif |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 310 | SetCodeAndDirectMethods(dex_files); |
| 311 | } |
| 312 | |
| 313 | bool Compiler::IsImageClass(const std::string& descriptor) const { |
| 314 | if (image_classes_ == NULL) { |
| 315 | return true; |
| 316 | } |
| 317 | return image_classes_->find(descriptor) != image_classes_->end(); |
| 318 | } |
| 319 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 320 | bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 321 | uint32_t type_idx) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 322 | if (!IsImage()) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 323 | stats_.TypeNotInDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 324 | return false; |
| 325 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 326 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 327 | if (resolved_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 328 | stats_.TypeNotInDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 329 | return false; |
| 330 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 331 | bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor()); |
| 332 | if (result) { |
| 333 | stats_.TypeInDexCache(); |
| 334 | } else { |
| 335 | stats_.TypeNotInDexCache(); |
| 336 | } |
| 337 | return result; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 340 | bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 341 | uint32_t string_idx) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 342 | // TODO: Add support for loading strings referenced by image_classes_ |
| 343 | // See also Compiler::ResolveDexFile |
| 344 | |
| 345 | // The following is a test saying that if we're building the image without a restricted set of |
| 346 | // image classes then we can assume the string is present in the dex cache if it is there now |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 347 | bool result = IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL; |
| 348 | if (result) { |
| 349 | stats_.StringInDexCache(); |
| 350 | } else { |
| 351 | stats_.StringNotInDexCache(); |
| 352 | } |
| 353 | return result; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 357 | const DexFile& dex_file, uint32_t type_idx) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 358 | // Get type from dex cache assuming it was populated by the verifier |
| 359 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
| 360 | if (resolved_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 361 | stats_.TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 362 | return false; // Unknown class needs access checks. |
| 363 | } |
| 364 | const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx); |
| 365 | Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_); |
| 366 | if (referrer_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 367 | stats_.TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 368 | return false; // Incomplete referrer knowledge needs access check. |
| 369 | } |
| 370 | // Perform access check, will return true if access is ok or false if we're going to have to |
| 371 | // check this at runtime (for example for class loaders). |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 372 | bool result = referrer_class->CanAccess(resolved_class); |
| 373 | if (result) { |
| 374 | stats_.TypeDoesntNeedAccessCheck(); |
| 375 | } else { |
| 376 | stats_.TypeNeedsAccessCheck(); |
| 377 | } |
| 378 | return result; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, |
| 382 | const DexCache* dex_cache, |
| 383 | const DexFile& dex_file, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 384 | uint32_t type_idx) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 385 | // Get type from dex cache assuming it was populated by the verifier. |
| 386 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
| 387 | if (resolved_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 388 | stats_.TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 389 | return false; // Unknown class needs access checks. |
| 390 | } |
| 391 | const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx); |
| 392 | Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_); |
| 393 | if (referrer_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 394 | stats_.TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 395 | return false; // Incomplete referrer knowledge needs access check. |
| 396 | } |
| 397 | // Perform access and instantiable checks, will return true if access is ok or false if we're |
| 398 | // going to have to check this at runtime (for example for class loaders). |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 399 | bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable(); |
| 400 | if (result) { |
| 401 | stats_.TypeDoesntNeedAccessCheck(); |
| 402 | } else { |
| 403 | stats_.TypeNeedsAccessCheck(); |
| 404 | } |
| 405 | return result; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 408 | static Class* ComputeReferrerClass(CompilationUnit* cUnit) { |
| 409 | const DexFile::MethodId& referrer_method_id = cUnit->dex_file->GetMethodId(cUnit->method_idx); |
| 410 | return cUnit->class_linker->ResolveType(*cUnit->dex_file, referrer_method_id.class_idx_, |
| 411 | cUnit->dex_cache, cUnit->class_loader); |
| 412 | } |
| 413 | |
| 414 | static Field* ComputeReferrerField(CompilationUnit* cUnit, uint32_t field_idx) { |
| 415 | return cUnit->class_linker->ResolveField(*cUnit->dex_file, field_idx, cUnit->dex_cache, |
| 416 | cUnit->class_loader, false); |
| 417 | |
| 418 | } |
| 419 | |
| 420 | static Method* ComputeReferrerMethod(CompilationUnit* cUnit, uint32_t method_idx) { |
| 421 | return cUnit->class_linker->ResolveMethod(*cUnit->dex_file, method_idx, cUnit->dex_cache, |
| 422 | cUnit->class_loader, true); |
| 423 | } |
| 424 | |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 425 | bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, CompilationUnit* cUnit, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 426 | int& field_offset, bool& is_volatile) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 427 | // Conservative defaults |
| 428 | field_offset = -1; |
| 429 | is_volatile = true; |
| 430 | // Try to resolve field |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 431 | Field* resolved_field = ComputeReferrerField(cUnit, field_idx); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 432 | if (resolved_field != NULL) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 433 | Class* referrer_class = ComputeReferrerClass(cUnit); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 434 | // Try to resolve referring class then access check, failure to pass the |
| 435 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 436 | if (referrer_class != NULL && |
| 437 | referrer_class->CanAccess(fields_class) && |
| 438 | referrer_class->CanAccessMember(fields_class, |
| 439 | resolved_field->GetAccessFlags())) { |
| 440 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 441 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 442 | stats_.ResolvedInstanceField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 443 | return true; // Fast path. |
| 444 | } |
| 445 | } |
| 446 | // Clean up any exception left by field/type resolution |
| 447 | Thread* thread = Thread::Current(); |
| 448 | if (thread->IsExceptionPending()) { |
| 449 | thread->ClearException(); |
| 450 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 451 | stats_.UnresolvedInstanceField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 452 | return false; // Incomplete knowledge needs slow path. |
| 453 | } |
| 454 | |
| 455 | bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, CompilationUnit* cUnit, |
| 456 | int& field_offset, int& ssb_index, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 457 | bool& is_referrers_class, bool& is_volatile) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 458 | // Conservative defaults |
| 459 | field_offset = -1; |
| 460 | ssb_index = -1; |
| 461 | is_referrers_class = false; |
| 462 | is_volatile = true; |
| 463 | // Try to resolve field |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 464 | Field* resolved_field = ComputeReferrerField(cUnit, field_idx); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 465 | if (resolved_field != NULL) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 466 | DCHECK(resolved_field->IsStatic()); |
| 467 | Class* referrer_class = ComputeReferrerClass(cUnit); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 468 | if (referrer_class != NULL) { |
| 469 | if (resolved_field->GetDeclaringClass() == referrer_class) { |
| 470 | is_referrers_class = true; // implies no worrying about class initialization |
| 471 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 472 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 473 | stats_.ResolvedLocalStaticField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 474 | return true; // fast path |
| 475 | } else { |
| 476 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 477 | if (referrer_class->CanAccess(fields_class) && |
| 478 | referrer_class->CanAccessMember(fields_class, |
| 479 | resolved_field->GetAccessFlags())) { |
| 480 | // We have the resolved field, we must make it into a ssbIndex for the referrer |
| 481 | // in its static storage base (which may fail if it doesn't have a slot for it) |
Ian Rogers | 4103ad2 | 2012-02-06 09:18:25 -0800 | [diff] [blame] | 482 | // TODO: for images we can elide the static storage base null check |
| 483 | // if we know there's a non-null entry in the image |
| 484 | if (fields_class->GetDexCache() == cUnit->dex_cache) { |
| 485 | // common case where the dex cache of both the referrer and the field are the same, |
| 486 | // no need to search the dex file |
| 487 | ssb_index = fields_class->GetDexTypeIndex(); |
| 488 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 489 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 490 | stats_.ResolvedStaticField(); |
Ian Rogers | 4103ad2 | 2012-02-06 09:18:25 -0800 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | // Search dex file for localized ssb index |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 494 | std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor()); |
| 495 | const DexFile::StringId* string_id = |
| 496 | cUnit->dex_file->FindStringId(descriptor); |
| 497 | if (string_id != NULL) { |
| 498 | const DexFile::TypeId* type_id = |
| 499 | cUnit->dex_file->FindTypeId(cUnit->dex_file->GetIndexForStringId(*string_id)); |
| 500 | if(type_id != NULL) { |
| 501 | // medium path, needs check of static storage base being initialized |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 502 | ssb_index = cUnit->dex_file->GetIndexForTypeId(*type_id); |
| 503 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 504 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 505 | stats_.ResolvedStaticField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 506 | return true; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | // Clean up any exception left by field/type resolution |
| 514 | Thread* thread = Thread::Current(); |
| 515 | if (thread->IsExceptionPending()) { |
| 516 | thread->ClearException(); |
| 517 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 518 | stats_.UnresolvedStaticField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 519 | return false; // Incomplete knowledge needs slow path. |
| 520 | } |
| 521 | |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 522 | bool Compiler::ComputeInvokeInfo(uint32_t method_idx, CompilationUnit* cUnit, |
| 523 | bool is_interface, bool is_super, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 524 | int& vtable_idx) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 525 | vtable_idx = -1; |
| 526 | Method* resolved_method = ComputeReferrerMethod(cUnit, method_idx); |
| 527 | if (resolved_method != NULL) { |
| 528 | Class* referrer_class = ComputeReferrerClass(cUnit); |
| 529 | if (referrer_class != NULL) { |
| 530 | Class* methods_class = resolved_method->GetDeclaringClass(); |
| 531 | if (!referrer_class->CanAccess(methods_class) || |
| 532 | !referrer_class->CanAccessMember(methods_class, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 533 | resolved_method->GetAccessFlags())) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 534 | // The referring class can't access the resolved method, this may occur as a result of a |
| 535 | // protected method being made public by implementing an interface that re-declares the |
| 536 | // method public. Resort to the dex file to determine the correct class for the access check |
| 537 | const DexFile& dex_file = cUnit->class_linker->FindDexFile(referrer_class->GetDexCache()); |
| 538 | methods_class = |
| 539 | cUnit->class_linker->ResolveType(dex_file, |
| 540 | dex_file.GetMethodId(method_idx).class_idx_, |
| 541 | referrer_class); |
| 542 | |
| 543 | } |
| 544 | if (referrer_class->CanAccess(methods_class) && |
| 545 | referrer_class->CanAccessMember(methods_class, |
| 546 | resolved_method->GetAccessFlags())) { |
| 547 | vtable_idx = resolved_method->GetMethodIndex(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 548 | if (is_interface || !is_super) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 549 | // nothing left to do for virtual/interface dispatch |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 550 | stats_.ResolvedMethod(is_interface, is_super); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 551 | return true; |
| 552 | } else { |
| 553 | // ensure the vtable index will be correct to dispatch in the vtable of the super class |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 554 | if (referrer_class->IsSubClass(methods_class) && |
| 555 | vtable_idx < methods_class->GetVTable()->GetLength()) { |
| 556 | stats_.ResolvedMethod(is_interface, is_super); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | // Clean up any exception left by method/type resolution |
| 564 | Thread* thread = Thread::Current(); |
| 565 | if (thread->IsExceptionPending()) { |
| 566 | thread->ClearException(); |
| 567 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 568 | stats_.UnresolvedMethod(is_interface, is_super); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 569 | return false; // Incomplete knowledge needs slow path. |
| 570 | } |
| 571 | |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 572 | // Return true if the class should be skipped during compilation. We |
| 573 | // never skip classes in the boot class loader. However, if we have a |
| 574 | // non-boot class loader and we can resolve the class in the boot |
| 575 | // class loader, we do skip the class. This happens if an app bundles |
| 576 | // classes found in the boot classpath. Since at runtime we will |
| 577 | // select the class from the boot classpath, do not attempt to resolve |
| 578 | // or compile it now. |
| 579 | static bool SkipClass(const ClassLoader* class_loader, |
| 580 | const DexFile& dex_file, |
| 581 | const DexFile::ClassDef& class_def) { |
| 582 | if (class_loader == NULL) { |
| 583 | return false; |
| 584 | } |
| 585 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 586 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 587 | Class* klass = class_linker->FindClass(descriptor, NULL); |
| 588 | if (klass == NULL) { |
| 589 | Thread* self = Thread::Current(); |
| 590 | CHECK(self->IsExceptionPending()); |
| 591 | self->ClearException(); |
| 592 | return false; |
| 593 | } |
| 594 | return true; |
| 595 | } |
| 596 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 597 | struct Context { |
| 598 | ClassLinker* class_linker; |
| 599 | const ClassLoader* class_loader; |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 600 | Compiler* compiler; |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 601 | DexCache* dex_cache; |
| 602 | const DexFile* dex_file; |
| 603 | }; |
| 604 | |
| 605 | typedef void Callback(Context* context, size_t index); |
| 606 | |
| 607 | class WorkerThread { |
| 608 | public: |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 609 | WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn) |
| 610 | : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) { |
| 611 | if (spawn_) { |
| 612 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread"); |
| 613 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | ~WorkerThread() { |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 617 | if (spawn_) { |
| 618 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown"); |
| 619 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | private: |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 623 | static void* Go(void* arg) { |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 624 | WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg); |
| 625 | Runtime* runtime = Runtime::Current(); |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 626 | if (worker->spawn_) { |
| 627 | runtime->AttachCurrentThread("Compiler Worker", true); |
| 628 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 629 | Thread::Current()->SetState(Thread::kRunnable); |
| 630 | worker->Run(); |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 631 | if (worker->spawn_) { |
| 632 | Thread::Current()->SetState(Thread::kNative); |
| 633 | runtime->DetachCurrentThread(); |
| 634 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 635 | return NULL; |
| 636 | } |
| 637 | |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 638 | void Go() { |
| 639 | Go(this); |
| 640 | } |
| 641 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 642 | void Run() { |
| 643 | for (size_t i = begin_; i < end_; i += stripe_) { |
| 644 | callback_(context_, i); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | pthread_t pthread_; |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 649 | bool spawn_; |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 650 | |
| 651 | Context* context_; |
| 652 | size_t begin_; |
| 653 | size_t end_; |
| 654 | Callback* callback_; |
| 655 | size_t stripe_; |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 656 | |
| 657 | friend void ForAll(Context*, size_t, size_t, Callback, size_t); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 658 | }; |
| 659 | |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 660 | void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) { |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 661 | CHECK_GT(thread_count, 0U); |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 662 | |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 663 | std::vector<WorkerThread*> threads; |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 664 | for (size_t i = 0; i < thread_count; ++i) { |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 665 | threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0))); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 666 | } |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 667 | threads[0]->Go(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 668 | |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 669 | // Switch to kVmWait while we're blocked waiting for the other threads to finish. |
| 670 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 671 | STLDeleteElements(&threads); |
| 672 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 673 | |
| 674 | static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) { |
| 675 | const DexFile& dex_file = *context->dex_file; |
| 676 | |
| 677 | // Method and Field are the worst. We can't resolve without either |
| 678 | // context from the code use (to disambiguate virtual vs direct |
| 679 | // method and instance vs static field) or from class |
| 680 | // definitions. While the compiler will resolve what it can as it |
| 681 | // needs it, here we try to resolve fields and methods used in class |
| 682 | // definitions, since many of them many never be referenced by |
| 683 | // generated code. |
| 684 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 685 | if (SkipClass(context->class_loader, dex_file, class_def)) { |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | // Note the class_data pointer advances through the headers, |
| 690 | // static fields, instance fields, direct methods, and virtual |
| 691 | // methods. |
| 692 | const byte* class_data = dex_file.GetClassData(class_def); |
| 693 | if (class_data == NULL) { |
| 694 | // empty class such as a marker interface |
| 695 | return; |
| 696 | } |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 697 | Thread* self = Thread::Current(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 698 | ClassLinker* class_linker = context->class_linker; |
| 699 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 700 | ClassDataItemIterator it(dex_file, class_data); |
| 701 | while (it.HasNextStaticField()) { |
| 702 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 703 | context->class_loader, true); |
| 704 | if (field == NULL) { |
| 705 | CHECK(self->IsExceptionPending()); |
| 706 | self->ClearException(); |
| 707 | } |
| 708 | it.Next(); |
| 709 | } |
| 710 | while (it.HasNextInstanceField()) { |
| 711 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 712 | context->class_loader, false); |
| 713 | if (field == NULL) { |
| 714 | CHECK(self->IsExceptionPending()); |
| 715 | self->ClearException(); |
| 716 | } |
| 717 | it.Next(); |
| 718 | } |
| 719 | while (it.HasNextDirectMethod()) { |
| 720 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 721 | context->class_loader, true); |
| 722 | if (method == NULL) { |
| 723 | CHECK(self->IsExceptionPending()); |
| 724 | self->ClearException(); |
| 725 | } |
| 726 | it.Next(); |
| 727 | } |
| 728 | while (it.HasNextVirtualMethod()) { |
| 729 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 730 | context->class_loader, false); |
| 731 | if (method == NULL) { |
| 732 | CHECK(self->IsExceptionPending()); |
| 733 | self->ClearException(); |
| 734 | } |
| 735 | it.Next(); |
| 736 | } |
| 737 | DCHECK(!it.HasNext()); |
| 738 | } |
| 739 | |
| 740 | static void ResolveType(Context* context, size_t type_idx) { |
| 741 | // Class derived values are more complicated, they require the linker and loader. |
| 742 | Thread* self = Thread::Current(); |
| 743 | ClassLinker* class_linker = context->class_linker; |
| 744 | const DexFile& dex_file = *context->dex_file; |
| 745 | Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader); |
| 746 | if (klass == NULL) { |
| 747 | CHECK(self->IsExceptionPending()); |
| 748 | Thread::Current()->ClearException(); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | 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] | 753 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 754 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 755 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 756 | // Strings are easy in that they always are simply resolved to literals in the same file |
| 757 | if (image_ && image_classes_ == NULL) { |
| 758 | // TODO: Add support for loading strings referenced by image_classes_ |
| 759 | // See also Compiler::CanAssumeTypeIsPresentInDexCache. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 760 | for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) { |
| 761 | class_linker->ResolveString(dex_file, string_idx, dex_cache); |
| 762 | } |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 763 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings"); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 766 | Context context; |
| 767 | context.class_linker = class_linker; |
| 768 | context.class_loader = class_loader; |
| 769 | context.dex_cache = dex_cache; |
| 770 | context.dex_file = &dex_file; |
| 771 | |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 772 | ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 773 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types"); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 774 | |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 775 | ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 776 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields"); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 779 | void Compiler::Verify(const ClassLoader* class_loader, |
| 780 | const std::vector<const DexFile*>& dex_files) { |
| 781 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 782 | const DexFile* dex_file = dex_files[i]; |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 783 | CHECK(dex_file != NULL); |
| 784 | VerifyDexFile(class_loader, *dex_file); |
| 785 | } |
| 786 | } |
| 787 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 788 | static void VerifyClass(Context* context, size_t class_def_index) { |
| 789 | const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index); |
| 790 | const char* descriptor = context->dex_file->GetClassDescriptor(class_def); |
| 791 | Class* klass = context->class_linker->FindClass(descriptor, context->class_loader); |
| 792 | if (klass == NULL) { |
| 793 | Thread* self = Thread::Current(); |
| 794 | CHECK(self->IsExceptionPending()); |
| 795 | self->ClearException(); |
| 796 | return; |
| 797 | } |
| 798 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
| 799 | context->class_linker->VerifyClass(klass); |
| 800 | |
| 801 | if (klass->IsErroneous()) { |
| 802 | // ClassLinker::VerifyClass throws, which isn't useful in the compiler. |
| 803 | CHECK(Thread::Current()->IsExceptionPending()); |
| 804 | Thread::Current()->ClearException(); |
| 805 | // We want to try verification again at run-time, so move back into the resolved state. |
| 806 | klass->SetStatus(Class::kStatusResolved); |
| 807 | } |
| 808 | |
| 809 | CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass); |
| 810 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException()); |
| 811 | } |
| 812 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 813 | void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 814 | dex_file.ChangePermissions(PROT_READ | PROT_WRITE); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 815 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 816 | Context context; |
| 817 | context.class_linker = Runtime::Current()->GetClassLinker(); |
| 818 | context.class_loader = class_loader; |
| 819 | context.dex_file = &dex_file; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 820 | ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 821 | |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 822 | dex_file.ChangePermissions(PROT_READ); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 825 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, |
| 826 | const std::vector<const DexFile*>& dex_files) { |
| 827 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 828 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 829 | CHECK(dex_file != NULL); |
| 830 | InitializeClassesWithoutClinit(class_loader, *dex_file); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 835 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 836 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 837 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 838 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 839 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 840 | if (klass != NULL) { |
| 841 | class_linker->EnsureInitialized(klass, false); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 842 | // record the final class status if necessary |
| 843 | Class::Status status = klass->GetStatus(); |
| 844 | ClassReference ref(&dex_file, class_def_index); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 845 | MutexLock mu(compiled_classes_lock_); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 846 | CompiledClass* compiled_class = GetCompiledClass(ref); |
| 847 | if (compiled_class == NULL) { |
| 848 | compiled_class = new CompiledClass(status); |
| 849 | compiled_classes_[ref] = compiled_class; |
| 850 | } else { |
| 851 | DCHECK_EQ(status, compiled_class->GetStatus()); |
| 852 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 853 | } |
| 854 | // clear any class not found or verification exceptions |
| 855 | Thread::Current()->ClearException(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 859 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 860 | 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] | 861 | if (klass == NULL) { |
| 862 | Thread::Current()->ClearException(); |
| 863 | } else if (klass->IsInitialized()) { |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 864 | dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass); |
| 865 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 869 | void Compiler::Compile(const ClassLoader* class_loader, |
| 870 | const std::vector<const DexFile*>& dex_files) { |
| 871 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 872 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 873 | CHECK(dex_file != NULL); |
| 874 | CompileDexFile(class_loader, *dex_file); |
| 875 | } |
| 876 | } |
| 877 | |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 878 | void Compiler::CompileClass(Context* context, size_t class_def_index) { |
| 879 | const ClassLoader* class_loader = context->class_loader; |
| 880 | const DexFile& dex_file = *context->dex_file; |
| 881 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 882 | if (SkipClass(class_loader, dex_file, class_def)) { |
| 883 | return; |
| 884 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 885 | const byte* class_data = dex_file.GetClassData(class_def); |
| 886 | if (class_data == NULL) { |
| 887 | // empty class, probably a marker interface |
| 888 | return; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 889 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 890 | ClassDataItemIterator it(dex_file, class_data); |
| 891 | // Skip fields |
| 892 | while (it.HasNextStaticField()) { |
| 893 | it.Next(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 894 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 895 | while (it.HasNextInstanceField()) { |
| 896 | it.Next(); |
| 897 | } |
| 898 | // Compile direct methods |
| 899 | while (it.HasNextDirectMethod()) { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 900 | context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), |
| 901 | it.GetMemberIndex(), class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 902 | it.Next(); |
| 903 | } |
| 904 | // Compile virtual methods |
| 905 | while (it.HasNextVirtualMethod()) { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 906 | context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), |
| 907 | it.GetMemberIndex(), class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 908 | it.Next(); |
| 909 | } |
| 910 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 913 | void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 914 | Context context; |
| 915 | context.class_loader = class_loader; |
| 916 | context.compiler = this; |
| 917 | context.dex_file = &dex_file; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 918 | ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 919 | } |
| 920 | |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 921 | void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
| 922 | uint32_t method_idx, const ClassLoader* class_loader, |
| 923 | const DexFile& dex_file) { |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 924 | CompiledMethod* compiled_method = NULL; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 925 | uint64_t start_ns = NanoTime(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 926 | if ((access_flags & kAccNative) != 0) { |
| 927 | 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] | 928 | CHECK(compiled_method != NULL); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 929 | } else if ((access_flags & kAccAbstract) != 0) { |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 930 | } else { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame^] | 931 | #if defined(ART_USE_LLVM_COMPILER) |
| 932 | compiled_method = |
| 933 | compiler_llvm_->CompileDexMethod(code_item, access_flags, method_idx, |
| 934 | class_loader, dex_file); |
| 935 | #else |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 936 | compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader, |
| 937 | dex_file, kThumb2); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame^] | 938 | #endif |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 939 | CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file); |
| 940 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 941 | uint64_t duration_ns = NanoTime() - start_ns; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 942 | if (duration_ns > MsToNs(100)) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 943 | LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file) |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 944 | << " took " << PrettyDuration(duration_ns); |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | if (compiled_method != NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 948 | MethodReference ref(&dex_file, method_idx); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 949 | CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 950 | MutexLock mu(compiled_methods_lock_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 951 | compiled_methods_[ref] = compiled_method; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 952 | DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 953 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 954 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 955 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 956 | bool is_static = (access_flags & kAccStatic) != 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 957 | const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty); |
| 958 | if (compiled_invoke_stub == NULL) { |
| 959 | if (instruction_set_ == kX86) { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 960 | compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 961 | } else { |
| 962 | CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2); |
| 963 | // Generates invocation stub using ARM instruction set |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 964 | compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 965 | } |
| 966 | CHECK(compiled_invoke_stub != NULL); |
| 967 | InsertInvokeStub(is_static, shorty, compiled_invoke_stub); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 968 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 969 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 972 | static std::string MakeInvokeStubKey(bool is_static, const char* shorty) { |
| 973 | std::string key(shorty); |
| 974 | if (is_static) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 975 | key += "$"; // Must not be a shorty type character. |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 976 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 977 | return key; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 980 | const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 981 | MutexLock mu(compiled_invoke_stubs_lock_); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 982 | const std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 983 | InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 984 | if (it == compiled_invoke_stubs_.end()) { |
| 985 | return NULL; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 986 | } else { |
| 987 | DCHECK(it->second != NULL); |
| 988 | return it->second; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | void Compiler::InsertInvokeStub(bool is_static, const char* shorty, |
| 993 | const CompiledInvokeStub* compiled_invoke_stub) { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 994 | MutexLock mu(compiled_invoke_stubs_lock_); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 995 | std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 996 | compiled_invoke_stubs_[key] = compiled_invoke_stub; |
| 997 | } |
| 998 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 999 | CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 1000 | MutexLock mu(compiled_classes_lock_); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1001 | ClassTable::const_iterator it = compiled_classes_.find(ref); |
| 1002 | if (it == compiled_classes_.end()) { |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | CHECK(it->second != NULL); |
| 1006 | return it->second; |
| 1007 | } |
| 1008 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1009 | CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 1010 | MutexLock mu(compiled_methods_lock_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1011 | MethodTable::const_iterator it = compiled_methods_.find(ref); |
| 1012 | if (it == compiled_methods_.end()) { |
| 1013 | return NULL; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1014 | } |
| 1015 | CHECK(it->second != NULL); |
| 1016 | return it->second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1019 | void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) { |
| 1020 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 1021 | const DexFile* dex_file = dex_files[i]; |
| 1022 | CHECK(dex_file != NULL); |
| 1023 | SetGcMapsDexFile(class_loader, *dex_file); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 1028 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1029 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 1030 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 1031 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 1032 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 1033 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
| 1034 | if (klass == NULL || !klass->IsVerified()) { |
| 1035 | Thread::Current()->ClearException(); |
| 1036 | continue; |
| 1037 | } |
| 1038 | const byte* class_data = dex_file.GetClassData(class_def); |
| 1039 | if (class_data == NULL) { |
| 1040 | // empty class such as a marker interface |
| 1041 | continue; |
| 1042 | } |
| 1043 | ClassDataItemIterator it(dex_file, class_data); |
| 1044 | while (it.HasNextStaticField()) { |
| 1045 | it.Next(); |
| 1046 | } |
| 1047 | while (it.HasNextInstanceField()) { |
| 1048 | it.Next(); |
| 1049 | } |
| 1050 | while (it.HasNextDirectMethod()) { |
| 1051 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 1052 | class_loader, true); |
| 1053 | SetGcMapsMethod(dex_file, method); |
| 1054 | it.Next(); |
| 1055 | } |
| 1056 | while (it.HasNextVirtualMethod()) { |
| 1057 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 1058 | class_loader, false); |
| 1059 | SetGcMapsMethod(dex_file, method); |
| 1060 | it.Next(); |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 1065 | namespace verifier { |
| 1066 | class DexVerifier { |
| 1067 | public: |
| 1068 | static const std::vector<uint8_t>* GetGcMap(Compiler::MethodReference ref); |
| 1069 | }; |
| 1070 | } |
| 1071 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1072 | void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) { |
| 1073 | if (method == NULL) { |
| 1074 | Thread::Current()->ClearException(); |
| 1075 | return; |
| 1076 | } |
| 1077 | uint16_t method_idx = method->GetDexMethodIndex(); |
| 1078 | MethodReference ref(&dex_file, method_idx); |
| 1079 | CompiledMethod* compiled_method = GetCompiledMethod(ref); |
| 1080 | if (compiled_method == NULL) { |
| 1081 | return; |
| 1082 | } |
| 1083 | const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref); |
| 1084 | if (gc_map == NULL) { |
| 1085 | return; |
| 1086 | } |
| 1087 | compiled_method->SetGcMap(*gc_map); |
| 1088 | } |
| 1089 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1090 | void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) { |
| 1091 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 1092 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1093 | CHECK(dex_file != NULL); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 1094 | SetCodeAndDirectMethodsDexFile(*dex_file); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 1098 | void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 1099 | Runtime* runtime = Runtime::Current(); |
| 1100 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1101 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 1102 | CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods(); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1103 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1104 | Method* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1105 | if (method == NULL || method->IsDirect()) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 1106 | Runtime::TrampolineType type = Runtime::GetTrampolineType(method); |
| 1107 | ByteArray* res_trampoline = runtime->GetResolutionStubArray(type); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1108 | code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 1109 | } else { |
| 1110 | // TODO: we currently leave the entry blank for resolved |
| 1111 | // non-direct methods. we could put in an error stub. |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1116 | } // namespace art |