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