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