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