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