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> |
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 | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 24 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 25 | #include "class_loader.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 26 | #include "dex_cache.h" |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 27 | #include "jni_internal.h" |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 28 | #include "oat_compilation_unit.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 29 | #include "oat_file.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 30 | #include "oat/runtime/stub.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 31 | #include "object_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 33 | #include "gc/space.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 34 | #include "scoped_thread_state_change.h" |
| 35 | #include "ScopedLocalRef.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 36 | #include "stl_util.h" |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 37 | #include "thread.h" |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 38 | #include "thread_pool.h" |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 39 | #include "timing_logger.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 40 | #include "verifier/method_verifier.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | 059d5c1 | 2012-03-12 17:39:18 -0700 | [diff] [blame] | 42 | #if defined(__APPLE__) |
| 43 | #include <mach-o/dyld.h> |
| 44 | #endif |
| 45 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 46 | namespace art { |
| 47 | |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 48 | static double Percentage(size_t x, size_t y) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 49 | return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y)); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void DumpStat(size_t x, size_t y, const char* str) { |
| 53 | if (x == 0 && y == 0) { |
| 54 | return; |
| 55 | } |
| 56 | LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases"; |
| 57 | } |
| 58 | |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 59 | class AOTCompilationStats { |
| 60 | public: |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 61 | AOTCompilationStats() |
| 62 | : stats_lock_("AOT compilation statistics lock"), |
| 63 | types_in_dex_cache_(0), types_not_in_dex_cache_(0), |
| 64 | strings_in_dex_cache_(0), strings_not_in_dex_cache_(0), |
| 65 | resolved_types_(0), unresolved_types_(0), |
| 66 | resolved_instance_fields_(0), unresolved_instance_fields_(0), |
| 67 | resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0) { |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 68 | for (size_t i = 0; i <= kMaxInvokeType; i++) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 69 | resolved_methods_[i] = 0; |
| 70 | unresolved_methods_[i] = 0; |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 71 | virtual_made_direct_[i] = 0; |
| 72 | direct_calls_to_boot_[i] = 0; |
| 73 | direct_methods_to_boot_[i] = 0; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 74 | } |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void Dump() { |
| 78 | DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache"); |
| 79 | DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache"); |
| 80 | DumpStat(resolved_types_, unresolved_types_, "types resolved"); |
| 81 | DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved"); |
| 82 | DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_, |
| 83 | "static fields resolved"); |
| 84 | DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_, |
| 85 | "static fields local to a class"); |
| 86 | |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 87 | for (size_t i = 0; i <= kMaxInvokeType; i++) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 88 | std::ostringstream oss; |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 89 | oss << static_cast<InvokeType>(i) << " methods were AOT resolved"; |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 90 | DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str()); |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 91 | if (virtual_made_direct_[i] > 0) { |
| 92 | std::ostringstream oss2; |
| 93 | oss2 << static_cast<InvokeType>(i) << " methods made direct"; |
| 94 | DumpStat(virtual_made_direct_[i], |
| 95 | resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i], |
| 96 | oss2.str().c_str()); |
| 97 | } |
| 98 | if (direct_calls_to_boot_[i] > 0) { |
| 99 | std::ostringstream oss2; |
| 100 | oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot"; |
| 101 | DumpStat(direct_calls_to_boot_[i], |
| 102 | resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i], |
| 103 | oss2.str().c_str()); |
| 104 | } |
| 105 | if (direct_methods_to_boot_[i] > 0) { |
| 106 | std::ostringstream oss2; |
| 107 | oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot"; |
| 108 | DumpStat(direct_methods_to_boot_[i], |
| 109 | resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i], |
| 110 | oss2.str().c_str()); |
| 111 | } |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 114 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 115 | // Allow lossy statistics in non-debug builds. |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 116 | #ifndef NDEBUG |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 117 | #define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_) |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 118 | #else |
| 119 | #define STATS_LOCK() |
| 120 | #endif |
| 121 | |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 122 | void TypeInDexCache() { |
| 123 | STATS_LOCK(); |
| 124 | types_in_dex_cache_++; |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 125 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 126 | |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 127 | void TypeNotInDexCache() { |
| 128 | STATS_LOCK(); |
| 129 | types_not_in_dex_cache_++; |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 130 | } |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 131 | |
| 132 | void StringInDexCache() { |
| 133 | STATS_LOCK(); |
| 134 | strings_in_dex_cache_++; |
| 135 | } |
| 136 | |
| 137 | void StringNotInDexCache() { |
| 138 | STATS_LOCK(); |
| 139 | strings_not_in_dex_cache_++; |
| 140 | } |
| 141 | |
| 142 | void TypeDoesntNeedAccessCheck() { |
| 143 | STATS_LOCK(); |
| 144 | resolved_types_++; |
| 145 | } |
| 146 | |
| 147 | void TypeNeedsAccessCheck() { |
| 148 | STATS_LOCK(); |
| 149 | unresolved_types_++; |
| 150 | } |
| 151 | |
| 152 | void ResolvedInstanceField() { |
| 153 | STATS_LOCK(); |
| 154 | resolved_instance_fields_++; |
| 155 | } |
| 156 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 157 | void UnresolvedInstanceField() { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 158 | STATS_LOCK(); |
| 159 | unresolved_instance_fields_++; |
| 160 | } |
| 161 | |
| 162 | void ResolvedLocalStaticField() { |
| 163 | STATS_LOCK(); |
| 164 | resolved_local_static_fields_++; |
| 165 | } |
| 166 | |
| 167 | void ResolvedStaticField() { |
| 168 | STATS_LOCK(); |
| 169 | resolved_static_fields_++; |
| 170 | } |
| 171 | |
| 172 | void UnresolvedStaticField() { |
| 173 | STATS_LOCK(); |
| 174 | unresolved_static_fields_++; |
| 175 | } |
| 176 | |
| 177 | void ResolvedMethod(InvokeType type) { |
| 178 | DCHECK_LE(type, kMaxInvokeType); |
| 179 | STATS_LOCK(); |
| 180 | resolved_methods_[type]++; |
| 181 | } |
| 182 | |
| 183 | void UnresolvedMethod(InvokeType type) { |
| 184 | DCHECK_LE(type, kMaxInvokeType); |
| 185 | STATS_LOCK(); |
| 186 | unresolved_methods_[type]++; |
| 187 | } |
| 188 | |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 189 | void VirtualMadeDirect(InvokeType type) { |
| 190 | DCHECK_LE(type, kMaxInvokeType); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 191 | STATS_LOCK(); |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 192 | virtual_made_direct_[type]++; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 193 | } |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 194 | |
| 195 | void DirectCallsToBoot(InvokeType type) { |
| 196 | DCHECK_LE(type, kMaxInvokeType); |
| 197 | STATS_LOCK(); |
| 198 | direct_calls_to_boot_[type]++; |
| 199 | } |
| 200 | |
| 201 | void DirectMethodsToBoot(InvokeType type) { |
| 202 | DCHECK_LE(type, kMaxInvokeType); |
| 203 | STATS_LOCK(); |
| 204 | direct_methods_to_boot_[type]++; |
| 205 | } |
| 206 | |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 207 | private: |
| 208 | Mutex stats_lock_; |
| 209 | |
| 210 | size_t types_in_dex_cache_; |
| 211 | size_t types_not_in_dex_cache_; |
| 212 | |
| 213 | size_t strings_in_dex_cache_; |
| 214 | size_t strings_not_in_dex_cache_; |
| 215 | |
| 216 | size_t resolved_types_; |
| 217 | size_t unresolved_types_; |
| 218 | |
| 219 | size_t resolved_instance_fields_; |
| 220 | size_t unresolved_instance_fields_; |
| 221 | |
| 222 | size_t resolved_local_static_fields_; |
| 223 | size_t resolved_static_fields_; |
| 224 | size_t unresolved_static_fields_; |
| 225 | |
| 226 | size_t resolved_methods_[kMaxInvokeType + 1]; |
| 227 | size_t unresolved_methods_[kMaxInvokeType + 1]; |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 228 | size_t virtual_made_direct_[kMaxInvokeType + 1]; |
| 229 | size_t direct_calls_to_boot_[kMaxInvokeType + 1]; |
| 230 | size_t direct_methods_to_boot_[kMaxInvokeType + 1]; |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 231 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 232 | DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats); |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 233 | }; |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 234 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 235 | static std::string MakeCompilerSoName(CompilerBackend compiler_backend, InstructionSet instruction_set) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 236 | // TODO: is the ARM/Thumb2 instruction set distinction really buying us anything, |
| 237 | // or just causing hassle like this? |
| 238 | if (instruction_set == kThumb2) { |
| 239 | instruction_set = kArm; |
| 240 | } |
| 241 | |
Brian Carlstrom | 5644f00 | 2012-06-27 23:05:17 -0700 | [diff] [blame] | 242 | // Lower case the instruction set, because that's what we do in the build system. |
Elliott Hughes | 6fcce30 | 2012-06-19 16:54:19 -0700 | [diff] [blame] | 243 | std::string instruction_set_name(ToStr<InstructionSet>(instruction_set).str()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 244 | for (size_t i = 0; i < instruction_set_name.size(); ++i) { |
Brian Carlstrom | 5644f00 | 2012-06-27 23:05:17 -0700 | [diff] [blame] | 245 | instruction_set_name[i] = tolower(instruction_set_name[i]); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 246 | } |
Elliott Hughes | 059d5c1 | 2012-03-12 17:39:18 -0700 | [diff] [blame] | 247 | |
| 248 | // Bad things happen if we pull in the libartd-compiler to a libart dex2oat or vice versa, |
| 249 | // because we end up with both libart and libartd in the same address space! |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 250 | const char* suffix = (kIsDebugBuild ? "d" : ""); |
Elliott Hughes | 059d5c1 | 2012-03-12 17:39:18 -0700 | [diff] [blame] | 251 | |
| 252 | // Work out the filename for the compiler library. |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 253 | std::string library_name; |
| 254 | if ((compiler_backend == kPortable) || (compiler_backend == kIceland)) { |
| 255 | library_name = StringPrintf("art%s-compiler-llvm", suffix); |
| 256 | } else { |
| 257 | library_name = StringPrintf("art%s-compiler-%s", suffix, instruction_set_name.c_str()); |
| 258 | } |
Elliott Hughes | 059d5c1 | 2012-03-12 17:39:18 -0700 | [diff] [blame] | 259 | std::string filename(StringPrintf(OS_SHARED_LIB_FORMAT_STR, library_name.c_str())); |
| 260 | |
| 261 | #if defined(__APPLE__) |
| 262 | // On Linux, dex2oat will have been built with an RPATH of $ORIGIN/../lib, so dlopen(3) will find |
| 263 | // the .so by itself. On Mac OS, there isn't really an equivalent, so we have to manually do the |
| 264 | // same work. |
Elliott Hughes | 059d5c1 | 2012-03-12 17:39:18 -0700 | [diff] [blame] | 265 | uint32_t executable_path_length = 0; |
Elliott Hughes | 448e93c | 2012-03-28 22:30:06 -0700 | [diff] [blame] | 266 | _NSGetExecutablePath(NULL, &executable_path_length); |
| 267 | std::string path(executable_path_length, static_cast<char>(0)); |
| 268 | CHECK_EQ(_NSGetExecutablePath(&path[0], &executable_path_length), 0); |
Elliott Hughes | 059d5c1 | 2012-03-12 17:39:18 -0700 | [diff] [blame] | 269 | |
| 270 | // Strip the "/dex2oat". |
| 271 | size_t last_slash = path.find_last_of('/'); |
| 272 | CHECK_NE(last_slash, std::string::npos) << path; |
| 273 | path.resize(last_slash); |
| 274 | |
| 275 | // Strip the "/bin". |
| 276 | last_slash = path.find_last_of('/'); |
| 277 | path.resize(last_slash); |
| 278 | |
| 279 | filename = path + "/lib/" + filename; |
| 280 | #endif |
| 281 | return filename; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Elliott Hughes | 46f060a | 2012-03-09 17:36:50 -0800 | [diff] [blame] | 284 | template<typename Fn> |
| 285 | static Fn FindFunction(const std::string& compiler_so_name, void* library, const char* name) { |
| 286 | Fn fn = reinterpret_cast<Fn>(dlsym(library, name)); |
| 287 | if (fn == NULL) { |
| 288 | LOG(FATAL) << "Couldn't find \"" << name << "\" in compiler library " << compiler_so_name << ": " << dlerror(); |
| 289 | } |
Elliott Hughes | 059d5c1 | 2012-03-12 17:39:18 -0700 | [diff] [blame] | 290 | VLOG(compiler) << "Found \"" << name << "\" at " << reinterpret_cast<void*>(fn); |
Elliott Hughes | 46f060a | 2012-03-09 17:36:50 -0800 | [diff] [blame] | 291 | return fn; |
| 292 | } |
| 293 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 294 | Compiler::Compiler(CompilerBackend compiler_backend, InstructionSet instruction_set, bool image, |
| 295 | size_t thread_count, bool support_debugging, const std::set<std::string>* image_classes, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 296 | bool dump_stats, bool dump_timings) |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 297 | : compiler_backend_(compiler_backend), |
| 298 | instruction_set_(instruction_set), |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 299 | compiled_classes_lock_("compiled classes lock"), |
| 300 | compiled_methods_lock_("compiled method lock"), |
| 301 | compiled_invoke_stubs_lock_("compiled invoke stubs lock"), |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 302 | compiled_proxy_stubs_lock_("compiled proxy stubs lock"), |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 303 | image_(image), |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 304 | thread_count_(thread_count), |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 305 | support_debugging_(support_debugging), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 306 | start_ns_(0), |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 307 | stats_(new AOTCompilationStats), |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 308 | dump_stats_(dump_stats), |
| 309 | dump_timings_(dump_timings), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 310 | image_classes_(image_classes), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 311 | compiler_library_(NULL), |
Elliott Hughes | 46f060a | 2012-03-09 17:36:50 -0800 | [diff] [blame] | 312 | compiler_(NULL), |
Elliott Hughes | 6f4976c | 2012-03-13 21:19:01 -0700 | [diff] [blame] | 313 | compiler_context_(NULL), |
Elliott Hughes | 46f060a | 2012-03-09 17:36:50 -0800 | [diff] [blame] | 314 | jni_compiler_(NULL), |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 315 | create_invoke_stub_(NULL), |
| 316 | thread_pool_(new ThreadPool(thread_count)) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 317 | { |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 318 | std::string compiler_so_name(MakeCompilerSoName(compiler_backend_, instruction_set_)); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 319 | compiler_library_ = dlopen(compiler_so_name.c_str(), RTLD_LAZY); |
| 320 | if (compiler_library_ == NULL) { |
| 321 | LOG(FATAL) << "Couldn't find compiler library " << compiler_so_name << ": " << dlerror(); |
| 322 | } |
| 323 | VLOG(compiler) << "dlopen(\"" << compiler_so_name << "\", RTLD_LAZY) returned " << compiler_library_; |
| 324 | |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 325 | CHECK_PTHREAD_CALL(pthread_key_create, (&tls_key_, NULL), "compiler tls key"); |
| 326 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 327 | // TODO: more work needed to combine initializations and allow per-method backend selection |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 328 | typedef void (*InitCompilerContextFn)(Compiler&); |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 329 | InitCompilerContextFn init_compiler_context; |
| 330 | if ((compiler_backend_ == kPortable) || (compiler_backend_ == kIceland)){ |
| 331 | // Initialize compiler_context_ |
| 332 | init_compiler_context = FindFunction<void (*)(Compiler&)>(compiler_so_name, |
| 333 | compiler_library_, "ArtInitCompilerContext"); |
| 334 | compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "ArtCompileMethod"); |
| 335 | } else { |
| 336 | init_compiler_context = FindFunction<void (*)(Compiler&)>(compiler_so_name, |
| 337 | compiler_library_, "ArtInitQuickCompilerContext"); |
| 338 | compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "ArtQuickCompileMethod"); |
| 339 | } |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 340 | |
| 341 | init_compiler_context(*this); |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 342 | |
Elliott Hughes | 46f060a | 2012-03-09 17:36:50 -0800 | [diff] [blame] | 343 | jni_compiler_ = FindFunction<JniCompilerFn>(compiler_so_name, compiler_library_, "ArtJniCompileMethod"); |
| 344 | create_invoke_stub_ = FindFunction<CreateInvokeStubFn>(compiler_so_name, compiler_library_, "ArtCreateInvokeStub"); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 345 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 346 | if ((compiler_backend_ == kPortable) || (compiler_backend_ == kIceland)) { |
| 347 | create_proxy_stub_ = FindFunction<CreateProxyStubFn>( |
| 348 | compiler_so_name, compiler_library_, "ArtCreateProxyStub"); |
| 349 | } |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 350 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 351 | CHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 352 | if (!image_) { |
| 353 | CHECK(image_classes_ == NULL); |
| 354 | } |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 357 | Compiler::~Compiler() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 358 | Thread* self = Thread::Current(); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 359 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 360 | MutexLock mu(self, compiled_classes_lock_); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 361 | STLDeleteValues(&compiled_classes_); |
| 362 | } |
| 363 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 364 | MutexLock mu(self, compiled_methods_lock_); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 365 | STLDeleteValues(&compiled_methods_); |
| 366 | } |
| 367 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 368 | MutexLock mu(self, compiled_invoke_stubs_lock_); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 369 | STLDeleteValues(&compiled_invoke_stubs_); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 370 | } |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 371 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 372 | MutexLock mu(self, compiled_proxy_stubs_lock_); |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 373 | STLDeleteValues(&compiled_proxy_stubs_); |
| 374 | } |
| 375 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 376 | MutexLock mu(self, compiled_methods_lock_); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 377 | STLDeleteElements(&code_to_patch_); |
| 378 | } |
| 379 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 380 | MutexLock mu(self, compiled_methods_lock_); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 381 | STLDeleteElements(&methods_to_patch_); |
| 382 | } |
Mathieu Chartier | ed6d5ed | 2012-10-12 14:51:46 -0700 | [diff] [blame] | 383 | CHECK_PTHREAD_CALL(pthread_key_delete, (tls_key_), "delete tls key"); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 384 | typedef void (*UninitCompilerContextFn)(Compiler&); |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 385 | std::string compiler_so_name(MakeCompilerSoName(compiler_backend_, instruction_set_)); |
| 386 | UninitCompilerContextFn uninit_compiler_context; |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 387 | // Uninitialize compiler_context_ |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 388 | // TODO: rework to combine initialization/uninitialization |
| 389 | if ((compiler_backend_ == kPortable) || (compiler_backend_ == kIceland)) { |
| 390 | uninit_compiler_context = FindFunction<void (*)(Compiler&)>(compiler_so_name, |
| 391 | compiler_library_, "ArtUnInitCompilerContext"); |
| 392 | } else { |
| 393 | uninit_compiler_context = FindFunction<void (*)(Compiler&)>(compiler_so_name, |
| 394 | compiler_library_, "ArtUnInitQuickCompilerContext"); |
| 395 | } |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 396 | uninit_compiler_context(*this); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 397 | if (compiler_library_ != NULL) { |
| 398 | VLOG(compiler) << "dlclose(" << compiler_library_ << ")"; |
buzbee | ca7a5e4 | 2012-08-20 11:12:18 -0700 | [diff] [blame] | 399 | /* |
| 400 | * FIXME: Temporary workaround |
| 401 | * Apparently, llvm is adding dctors to atexit, but if we unload |
| 402 | * the library here the code will no longer be around at exit time |
| 403 | * and we die a flaming death in __cxa_finalize(). Apparently, some |
| 404 | * dlclose() implementations will scan the atexit list on unload and |
| 405 | * handle any associated with the soon-to-be-unloaded library. |
| 406 | * However, this is not required by POSIX and we don't do it. |
| 407 | * See: http://b/issue?id=4998315 |
| 408 | * What's the right thing to do here? |
| 409 | */ |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 410 | dlclose(compiler_library_); |
| 411 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 412 | } |
| 413 | |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 414 | CompilerTls* Compiler::GetTls() { |
| 415 | // Lazily create thread-local storage |
| 416 | CompilerTls* res = static_cast<CompilerTls*>(pthread_getspecific(tls_key_)); |
| 417 | if (res == NULL) { |
| 418 | res = new CompilerTls(); |
| 419 | CHECK_PTHREAD_CALL(pthread_setspecific, (tls_key_, res), "compiler tls"); |
| 420 | } |
| 421 | return res; |
| 422 | } |
| 423 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 424 | ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set, |
| 425 | Runtime::TrampolineType type) { |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 426 | switch (instruction_set) { |
| 427 | case kArm: |
| 428 | case kThumb2: |
| 429 | return arm::ArmCreateResolutionTrampoline(type); |
| 430 | case kMips: |
| 431 | return mips::MipsCreateResolutionTrampoline(type); |
| 432 | case kX86: |
| 433 | return x86::X86CreateResolutionTrampoline(type); |
| 434 | default: |
| 435 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
| 436 | return NULL; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 440 | ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 441 | switch (instruction_set) { |
| 442 | case kArm: |
| 443 | case kThumb2: |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 444 | return arm::CreateJniDlsymLookupStub(); |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 445 | case kMips: |
| 446 | return mips::CreateJniDlsymLookupStub(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 447 | case kX86: |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 448 | return x86::CreateJniDlsymLookupStub(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 449 | default: |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 450 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 451 | return NULL; |
| 452 | } |
| 453 | } |
| 454 | |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 455 | ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) { |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 456 | switch (instruction_set) { |
| 457 | case kArm: |
| 458 | case kThumb2: |
| 459 | return arm::CreateAbstractMethodErrorStub(); |
| 460 | case kMips: |
| 461 | return mips::CreateAbstractMethodErrorStub(); |
| 462 | case kX86: |
| 463 | return x86::CreateAbstractMethodErrorStub(); |
| 464 | default: |
| 465 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
| 466 | return NULL; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 470 | void Compiler::CompileAll(jobject class_loader, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 471 | const std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 472 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 473 | |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 474 | TimingLogger timings("compiler"); |
| 475 | |
| 476 | PreCompile(class_loader, dex_files, timings); |
| 477 | |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 478 | Compile(class_loader, dex_files, timings); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 479 | |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 480 | if (dump_timings_ && timings.GetTotalNs() > MsToNs(1000)) { |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 481 | timings.Dump(); |
| 482 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 483 | |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 484 | if (dump_stats_) { |
| 485 | stats_->Dump(); |
| 486 | } |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 489 | void Compiler::CompileOne(const AbstractMethod* method) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 490 | DCHECK(!Runtime::Current()->IsStarted()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 491 | Thread* self = Thread::Current(); |
| 492 | jobject class_loader; |
| 493 | const DexCache* dex_cache; |
| 494 | const DexFile* dex_file; |
| 495 | { |
| 496 | ScopedObjectAccessUnchecked soa(self); |
| 497 | ScopedLocalRef<jobject> |
| 498 | local_class_loader(soa.Env(), |
| 499 | soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader())); |
| 500 | class_loader = soa.Env()->NewGlobalRef(local_class_loader.get()); |
| 501 | // Find the dex_file |
| 502 | dex_cache = method->GetDeclaringClass()->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 503 | dex_file = dex_cache->GetDexFile(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 504 | } |
| 505 | self->TransitionFromRunnableToSuspended(kNative); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 506 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 507 | std::vector<const DexFile*> dex_files; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 508 | dex_files.push_back(dex_file); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 509 | |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 510 | TimingLogger timings("CompileOne"); |
| 511 | PreCompile(class_loader, dex_files, timings); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 512 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 513 | uint32_t method_idx = method->GetDexMethodIndex(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 514 | const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 515 | CompileMethod(code_item, method->GetAccessFlags(), method->GetInvokeType(), |
| 516 | method_idx, class_loader, *dex_file); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 517 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 518 | self->GetJniEnv()->DeleteGlobalRef(class_loader); |
| 519 | |
| 520 | self->TransitionFromSuspendedToRunnable(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 523 | void Compiler::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files, |
| 524 | TimingLogger& timings) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 525 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 526 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 527 | CHECK(dex_file != NULL); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 528 | ResolveDexFile(class_loader, *dex_file, timings); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 532 | void Compiler::PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files, |
| 533 | TimingLogger& timings) { |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 534 | Resolve(class_loader, dex_files, timings); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 535 | |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 536 | Verify(class_loader, dex_files, timings); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 537 | |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 538 | InitializeClassesWithoutClinit(class_loader, dex_files, timings); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | bool Compiler::IsImageClass(const std::string& descriptor) const { |
| 542 | if (image_classes_ == NULL) { |
| 543 | return true; |
| 544 | } |
| 545 | return image_classes_->find(descriptor) != image_classes_->end(); |
| 546 | } |
| 547 | |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 548 | void Compiler::RecordClassStatus(ClassReference ref, CompiledClass* compiled_class) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 549 | MutexLock mu(Thread::Current(), Compiler::compiled_classes_lock_); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 550 | compiled_classes_.Put(ref, compiled_class); |
| 551 | } |
| 552 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 553 | bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 554 | uint32_t type_idx) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 555 | ScopedObjectAccess soa(Thread::Current()); |
| 556 | DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 557 | if (!IsImage()) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 558 | stats_->TypeNotInDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 559 | return false; |
| 560 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 561 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 562 | if (resolved_class == NULL) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 563 | stats_->TypeNotInDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 564 | return false; |
| 565 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 566 | bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor()); |
| 567 | if (result) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 568 | stats_->TypeInDexCache(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 569 | } else { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 570 | stats_->TypeNotInDexCache(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 571 | } |
| 572 | return result; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 575 | bool Compiler::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 576 | uint32_t string_idx) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 577 | // TODO: Add support for loading strings referenced by image_classes_ |
| 578 | // See also Compiler::ResolveDexFile |
| 579 | |
| 580 | // The following is a test saying that if we're building the image without a restricted set of |
| 581 | // image classes then we can assume the string is present in the dex cache if it is there now |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 582 | bool result = IsImage() && image_classes_ == NULL; |
| 583 | if (result) { |
| 584 | ScopedObjectAccess soa(Thread::Current()); |
| 585 | DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file); |
| 586 | result = dex_cache->GetResolvedString(string_idx) != NULL; |
| 587 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 588 | if (result) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 589 | stats_->StringInDexCache(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 590 | } else { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 591 | stats_->StringNotInDexCache(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 592 | } |
| 593 | return result; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 596 | bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, |
| 597 | uint32_t type_idx) { |
| 598 | ScopedObjectAccess soa(Thread::Current()); |
| 599 | DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 600 | // Get type from dex cache assuming it was populated by the verifier |
| 601 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
| 602 | if (resolved_class == NULL) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 603 | stats_->TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 604 | return false; // Unknown class needs access checks. |
| 605 | } |
| 606 | const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx); |
| 607 | Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_); |
| 608 | if (referrer_class == NULL) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 609 | stats_->TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 610 | return false; // Incomplete referrer knowledge needs access check. |
| 611 | } |
| 612 | // Perform access check, will return true if access is ok or false if we're going to have to |
| 613 | // check this at runtime (for example for class loaders). |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 614 | bool result = referrer_class->CanAccess(resolved_class); |
| 615 | if (result) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 616 | stats_->TypeDoesntNeedAccessCheck(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 617 | } else { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 618 | stats_->TypeNeedsAccessCheck(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 619 | } |
| 620 | return result; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 624 | const DexFile& dex_file, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 625 | uint32_t type_idx) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 626 | ScopedObjectAccess soa(Thread::Current()); |
| 627 | DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 628 | // Get type from dex cache assuming it was populated by the verifier. |
| 629 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
| 630 | if (resolved_class == NULL) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 631 | stats_->TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 632 | return false; // Unknown class needs access checks. |
| 633 | } |
| 634 | const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx); |
| 635 | Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_); |
| 636 | if (referrer_class == NULL) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 637 | stats_->TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 638 | return false; // Incomplete referrer knowledge needs access check. |
| 639 | } |
| 640 | // Perform access and instantiable checks, will return true if access is ok or false if we're |
| 641 | // 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] | 642 | bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable(); |
| 643 | if (result) { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 644 | stats_->TypeDoesntNeedAccessCheck(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 645 | } else { |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 646 | stats_->TypeNeedsAccessCheck(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 647 | } |
| 648 | return result; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 651 | static Class* ComputeCompilingMethodsClass(ScopedObjectAccess& soa, |
| 652 | OatCompilationUnit* mUnit) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 653 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 654 | DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_); |
| 655 | ClassLoader* class_loader = soa.Decode<ClassLoader*>(mUnit->class_loader_); |
| 656 | const DexFile::MethodId& referrer_method_id = mUnit->dex_file_->GetMethodId(mUnit->method_idx_); |
| 657 | return mUnit->class_linker_->ResolveType(*mUnit->dex_file_, referrer_method_id.class_idx_, |
| 658 | dex_cache, class_loader); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 661 | static Field* ComputeFieldReferencedFromCompilingMethod(ScopedObjectAccess& soa, |
| 662 | OatCompilationUnit* mUnit, |
| 663 | uint32_t field_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 664 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 665 | DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_); |
| 666 | ClassLoader* class_loader = soa.Decode<ClassLoader*>(mUnit->class_loader_); |
| 667 | return mUnit->class_linker_->ResolveField(*mUnit->dex_file_, field_idx, dex_cache, |
| 668 | class_loader, false); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 671 | static AbstractMethod* ComputeMethodReferencedFromCompilingMethod(ScopedObjectAccess& soa, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 672 | OatCompilationUnit* mUnit, |
| 673 | uint32_t method_idx, |
| 674 | InvokeType type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 675 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 676 | DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_); |
| 677 | ClassLoader* class_loader = soa.Decode<ClassLoader*>(mUnit->class_loader_); |
| 678 | return mUnit->class_linker_->ResolveMethod(*mUnit->dex_file_, method_idx, dex_cache, |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 679 | class_loader, NULL, type); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 682 | bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit, |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 683 | int& field_offset, bool& is_volatile, bool is_put) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 684 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 685 | // Conservative defaults. |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 686 | field_offset = -1; |
| 687 | is_volatile = true; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 688 | // Try to resolve field and ignore if an Incompatible Class Change Error (ie is static). |
| 689 | Field* resolved_field = ComputeFieldReferencedFromCompilingMethod(soa, mUnit, field_idx); |
| 690 | if (resolved_field != NULL && !resolved_field->IsStatic()) { |
| 691 | Class* referrer_class = ComputeCompilingMethodsClass(soa, mUnit); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 692 | if (referrer_class != NULL) { |
| 693 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 694 | bool access_ok = referrer_class->CanAccess(fields_class) && |
| 695 | referrer_class->CanAccessMember(fields_class, |
| 696 | resolved_field->GetAccessFlags()); |
| 697 | if (!access_ok) { |
| 698 | // The referring class can't access the resolved field, this may occur as a result of a |
| 699 | // protected field being made public by a sub-class. Resort to the dex file to determine |
| 700 | // the correct class for the access check. |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 701 | const DexFile& dex_file = *referrer_class->GetDexCache()->GetDexFile(); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 702 | Class* dex_fields_class = mUnit->class_linker_->ResolveType(dex_file, |
| 703 | dex_file.GetFieldId(field_idx).class_idx_, |
| 704 | referrer_class); |
| 705 | access_ok = referrer_class->CanAccess(dex_fields_class) && |
| 706 | referrer_class->CanAccessMember(dex_fields_class, |
| 707 | resolved_field->GetAccessFlags()); |
| 708 | } |
| 709 | bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() && |
| 710 | fields_class != referrer_class; |
| 711 | if (access_ok && !is_write_to_final_from_wrong_class) { |
| 712 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 713 | is_volatile = resolved_field->IsVolatile(); |
| 714 | stats_->ResolvedInstanceField(); |
| 715 | return true; // Fast path. |
| 716 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | // Clean up any exception left by field/type resolution |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 720 | if (soa.Self()->IsExceptionPending()) { |
| 721 | soa.Self()->ClearException(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 722 | } |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 723 | stats_->UnresolvedInstanceField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 724 | return false; // Incomplete knowledge needs slow path. |
| 725 | } |
| 726 | |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 727 | bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit, |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 728 | int& field_offset, int& ssb_index, |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 729 | bool& is_referrers_class, bool& is_volatile, bool is_put) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 730 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 731 | // Conservative defaults. |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 732 | field_offset = -1; |
| 733 | ssb_index = -1; |
| 734 | is_referrers_class = false; |
| 735 | is_volatile = true; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 736 | // Try to resolve field and ignore if an Incompatible Class Change Error (ie isn't static). |
| 737 | Field* resolved_field = ComputeFieldReferencedFromCompilingMethod(soa, mUnit, field_idx); |
| 738 | if (resolved_field != NULL && resolved_field->IsStatic()) { |
| 739 | Class* referrer_class = ComputeCompilingMethodsClass(soa, mUnit); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 740 | if (referrer_class != NULL) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 741 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 742 | if (fields_class == referrer_class) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 743 | is_referrers_class = true; // implies no worrying about class initialization |
| 744 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 745 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 746 | stats_->ResolvedLocalStaticField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 747 | return true; // fast path |
| 748 | } else { |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 749 | bool access_ok = referrer_class->CanAccess(fields_class) && |
| 750 | referrer_class->CanAccessMember(fields_class, |
| 751 | resolved_field->GetAccessFlags()); |
| 752 | if (!access_ok) { |
| 753 | // The referring class can't access the resolved field, this may occur as a result of a |
| 754 | // protected field being made public by a sub-class. Resort to the dex file to determine |
| 755 | // the correct class for the access check. Don't change the field's class as that is |
| 756 | // used to identify the SSB. |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 757 | const DexFile& dex_file = *referrer_class->GetDexCache()->GetDexFile(); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 758 | Class* dex_fields_class = |
| 759 | mUnit->class_linker_->ResolveType(dex_file, |
| 760 | dex_file.GetFieldId(field_idx).class_idx_, |
| 761 | referrer_class); |
| 762 | access_ok = referrer_class->CanAccess(dex_fields_class) && |
| 763 | referrer_class->CanAccessMember(dex_fields_class, |
| 764 | resolved_field->GetAccessFlags()); |
| 765 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 766 | bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal(); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 767 | if (access_ok && !is_write_to_final_from_wrong_class) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 768 | // We have the resolved field, we must make it into a ssbIndex for the referrer |
| 769 | // 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] | 770 | // TODO: for images we can elide the static storage base null check |
| 771 | // if we know there's a non-null entry in the image |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 772 | DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_); |
| 773 | if (fields_class->GetDexCache() == dex_cache) { |
Ian Rogers | 4103ad2 | 2012-02-06 09:18:25 -0800 | [diff] [blame] | 774 | // common case where the dex cache of both the referrer and the field are the same, |
| 775 | // no need to search the dex file |
| 776 | ssb_index = fields_class->GetDexTypeIndex(); |
| 777 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 778 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 779 | stats_->ResolvedStaticField(); |
Ian Rogers | 4103ad2 | 2012-02-06 09:18:25 -0800 | [diff] [blame] | 780 | return true; |
| 781 | } |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 782 | // Search dex file for localized ssb index, may fail if field's class is a parent |
| 783 | // of the class mentioned in the dex file and there is no dex cache entry. |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 784 | std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor()); |
| 785 | const DexFile::StringId* string_id = |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 786 | mUnit->dex_file_->FindStringId(descriptor); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 787 | if (string_id != NULL) { |
| 788 | const DexFile::TypeId* type_id = |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 789 | mUnit->dex_file_->FindTypeId(mUnit->dex_file_->GetIndexForStringId(*string_id)); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 790 | if (type_id != NULL) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 791 | // medium path, needs check of static storage base being initialized |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 792 | ssb_index = mUnit->dex_file_->GetIndexForTypeId(*type_id); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 793 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 794 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 795 | stats_->ResolvedStaticField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 796 | return true; |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | // Clean up any exception left by field/type resolution |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 804 | if (soa.Self()->IsExceptionPending()) { |
| 805 | soa.Self()->ClearException(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 806 | } |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 807 | stats_->UnresolvedStaticField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 808 | return false; // Incomplete knowledge needs slow path. |
| 809 | } |
| 810 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 811 | void Compiler::GetCodeAndMethodForDirectCall(InvokeType type, InvokeType sharp_type, AbstractMethod* method, |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 812 | uintptr_t& direct_code, uintptr_t& direct_method) { |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 813 | // For direct and static methods compute possible direct_code and direct_method values, ie |
| 814 | // an address for the Method* being invoked and an address of the code for that Method*. |
| 815 | // For interface calls compute a value for direct_method that is the interface method being |
| 816 | // invoked, so this can be passed to the out-of-line runtime support code. |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 817 | direct_code = 0; |
| 818 | direct_method = 0; |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 819 | if ((compiler_backend_ == kPortable) || (compiler_backend_ == kIceland)) { |
| 820 | if (sharp_type != kStatic && sharp_type != kDirect) { |
| 821 | return; |
| 822 | } |
| 823 | } else { |
| 824 | if (sharp_type != kStatic && sharp_type != kDirect && sharp_type != kInterface) { |
| 825 | return; |
| 826 | } |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 827 | } |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 828 | bool method_code_in_boot = method->GetDeclaringClass()->GetClassLoader() == NULL; |
| 829 | if (!method_code_in_boot) { |
| 830 | return; |
| 831 | } |
| 832 | bool has_clinit_trampoline = method->IsStatic() && !method->GetDeclaringClass()->IsInitialized(); |
| 833 | if (has_clinit_trampoline) { |
| 834 | return; |
| 835 | } |
Ian Rogers | c468e92 | 2012-10-10 18:11:33 -0700 | [diff] [blame] | 836 | if (sharp_type != kInterface) { // Interfaces always go via a trampoline. |
| 837 | stats_->DirectCallsToBoot(type); |
| 838 | } |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 839 | stats_->DirectMethodsToBoot(type); |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 840 | bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1; |
| 841 | if (compiling_boot) { |
Brian Carlstrom | 0637e27 | 2012-03-20 01:07:52 -0700 | [diff] [blame] | 842 | const bool kSupportBootImageFixup = true; |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 843 | if (kSupportBootImageFixup) { |
| 844 | MethodHelper mh(method); |
| 845 | if (IsImageClass(mh.GetDeclaringClassDescriptor())) { |
Brian Carlstrom | 0637e27 | 2012-03-20 01:07:52 -0700 | [diff] [blame] | 846 | // We can only branch directly to Methods that are resolved in the DexCache. |
| 847 | // Otherwise we won't invoke the resolution trampoline. |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 848 | direct_method = -1; |
Brian Carlstrom | 0637e27 | 2012-03-20 01:07:52 -0700 | [diff] [blame] | 849 | direct_code = -1; |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 850 | } |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 851 | } |
| 852 | } else { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 853 | if (Runtime::Current()->GetHeap()->FindSpaceFromObject(method)->IsImageSpace()) { |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 854 | direct_method = reinterpret_cast<uintptr_t>(method); |
| 855 | } |
| 856 | direct_code = reinterpret_cast<uintptr_t>(method->GetCode()); |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 857 | } |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 860 | bool Compiler::ComputeInvokeInfo(uint32_t method_idx, OatCompilationUnit* mUnit, InvokeType& type, |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 861 | int& vtable_idx, uintptr_t& direct_code, |
| 862 | uintptr_t& direct_method) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 863 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 864 | vtable_idx = -1; |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 865 | direct_code = 0; |
| 866 | direct_method = 0; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 867 | AbstractMethod* resolved_method = |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 868 | ComputeMethodReferencedFromCompilingMethod(soa, mUnit, method_idx, type); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 869 | if (resolved_method != NULL) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 870 | // Don't try to fast-path if we don't understand the caller's class or this appears to be an |
| 871 | // Incompatible Class Change Error. |
| 872 | Class* referrer_class = ComputeCompilingMethodsClass(soa, mUnit); |
| 873 | bool icce = resolved_method->CheckIncompatibleClassChange(type); |
| 874 | if (referrer_class != NULL && !icce) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 875 | Class* methods_class = resolved_method->GetDeclaringClass(); |
| 876 | if (!referrer_class->CanAccess(methods_class) || |
| 877 | !referrer_class->CanAccessMember(methods_class, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 878 | resolved_method->GetAccessFlags())) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 879 | // The referring class can't access the resolved method, this may occur as a result of a |
| 880 | // protected method being made public by implementing an interface that re-declares the |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 881 | // method public. Resort to the dex file to determine the correct class for the access |
| 882 | // check. |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 883 | const DexFile& dex_file = *referrer_class->GetDexCache()->GetDexFile(); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 884 | methods_class = |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 885 | mUnit->class_linker_->ResolveType(dex_file, |
| 886 | dex_file.GetMethodId(method_idx).class_idx_, |
| 887 | referrer_class); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 888 | } |
| 889 | if (referrer_class->CanAccess(methods_class) && |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 890 | referrer_class->CanAccessMember(methods_class, resolved_method->GetAccessFlags())) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 891 | vtable_idx = resolved_method->GetMethodIndex(); |
Ian Rogers | f320b63 | 2012-03-13 18:47:47 -0700 | [diff] [blame] | 892 | const bool kEnableSharpening = true; |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 893 | // Sharpen a virtual call into a direct call when the target is known. |
| 894 | bool can_sharpen = type == kVirtual && (resolved_method->IsFinal() || |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 895 | methods_class->IsFinal()); |
| 896 | // Ensure the vtable index will be correct to dispatch in the vtable of the super class. |
jeffhao | 4155fcd | 2012-03-21 11:42:12 -0700 | [diff] [blame] | 897 | can_sharpen = can_sharpen || (type == kSuper && referrer_class != methods_class && |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 898 | referrer_class->IsSubClass(methods_class) && |
| 899 | vtable_idx < methods_class->GetVTable()->GetLength() && |
| 900 | methods_class->GetVTable()->Get(vtable_idx) == resolved_method); |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 901 | if (kEnableSharpening && can_sharpen) { |
| 902 | stats_->ResolvedMethod(type); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 903 | // Sharpen a virtual call into a direct call. The method_idx is into referrer's |
| 904 | // dex cache, check that this resolved method is where we expect it. |
| 905 | CHECK(referrer_class->GetDexCache()->GetResolvedMethod(method_idx) == resolved_method) |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 906 | << PrettyMethod(resolved_method); |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 907 | stats_->VirtualMadeDirect(type); |
| 908 | GetCodeAndMethodForDirectCall(type, kDirect, resolved_method, direct_code, direct_method); |
| 909 | type = kDirect; |
| 910 | return true; |
| 911 | } else if (type == kSuper) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 912 | // Unsharpened super calls are suspicious so go slow-path. |
Ian Rogers | 2ed3b95 | 2012-03-17 11:49:39 -0700 | [diff] [blame] | 913 | } else { |
| 914 | stats_->ResolvedMethod(type); |
| 915 | GetCodeAndMethodForDirectCall(type, type, resolved_method, direct_code, direct_method); |
| 916 | return true; |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | // Clean up any exception left by method/type resolution |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 922 | if (soa.Self()->IsExceptionPending()) { |
| 923 | soa.Self()->ClearException(); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 924 | } |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 925 | stats_->UnresolvedMethod(type); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 926 | return false; // Incomplete knowledge needs slow path. |
| 927 | } |
| 928 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 929 | void Compiler::AddCodePatch(const DexFile* dex_file, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 930 | uint32_t referrer_method_idx, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 931 | InvokeType referrer_invoke_type, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 932 | uint32_t target_method_idx, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 933 | InvokeType target_invoke_type, |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 934 | size_t literal_offset) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 935 | MutexLock mu(Thread::Current(), compiled_methods_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 936 | code_to_patch_.push_back(new PatchInformation(dex_file, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 937 | referrer_method_idx, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 938 | referrer_invoke_type, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 939 | target_method_idx, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 940 | target_invoke_type, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 941 | literal_offset)); |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 942 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 943 | void Compiler::AddMethodPatch(const DexFile* dex_file, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 944 | uint32_t referrer_method_idx, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 945 | InvokeType referrer_invoke_type, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 946 | uint32_t target_method_idx, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 947 | InvokeType target_invoke_type, |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 948 | size_t literal_offset) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 949 | MutexLock mu(Thread::Current(), compiled_methods_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 950 | methods_to_patch_.push_back(new PatchInformation(dex_file, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 951 | referrer_method_idx, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 952 | referrer_invoke_type, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 953 | target_method_idx, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 954 | target_invoke_type, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 955 | literal_offset)); |
Ian Rogers | 3fa1379 | 2012-03-18 15:53:45 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 958 | class CompilationContext { |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 959 | public: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 960 | typedef void Callback(const CompilationContext* context, size_t index); |
| 961 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 962 | CompilationContext(ClassLinker* class_linker, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 963 | jobject class_loader, |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 964 | Compiler* compiler, |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 965 | const DexFile* dex_file, |
| 966 | ThreadPool* thread_pool) |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 967 | : class_linker_(class_linker), |
| 968 | class_loader_(class_loader), |
| 969 | compiler_(compiler), |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 970 | dex_file_(dex_file), |
| 971 | thread_pool_(thread_pool) {} |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 972 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 973 | ClassLinker* GetClassLinker() const { |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 974 | CHECK(class_linker_ != NULL); |
| 975 | return class_linker_; |
| 976 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 977 | |
| 978 | jobject GetClassLoader() const { |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 979 | return class_loader_; |
| 980 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 981 | |
| 982 | Compiler* GetCompiler() const { |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 983 | CHECK(compiler_ != NULL); |
| 984 | return compiler_; |
| 985 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 986 | |
| 987 | const DexFile* GetDexFile() const { |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 988 | CHECK(dex_file_ != NULL); |
| 989 | return dex_file_; |
| 990 | } |
| 991 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 992 | void ForAll(size_t begin, size_t end, Callback callback, size_t work_units) { |
| 993 | Thread* self = Thread::Current(); |
| 994 | self->AssertNoPendingException(); |
| 995 | CHECK_GT(work_units, 0U); |
| 996 | |
| 997 | std::vector<Closure*> closures(work_units); |
| 998 | for (size_t i = 0; i < work_units; ++i) { |
| 999 | closures[i] = new ForAllClosure(this, begin + i, end, callback, work_units); |
| 1000 | thread_pool_->AddTask(self, closures[i]); |
| 1001 | } |
| 1002 | thread_pool_->StartWorkers(self); |
| 1003 | |
| 1004 | // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker |
| 1005 | // thread destructor's called below perform join). |
| 1006 | CHECK_NE(self->GetState(), kRunnable); |
| 1007 | |
| 1008 | // Wait for all the worker threads to finish. |
| 1009 | thread_pool_->Wait(self); |
| 1010 | |
| 1011 | STLDeleteElements(&closures); |
| 1012 | } |
| 1013 | |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1014 | private: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1015 | |
| 1016 | class ForAllClosure : public Closure { |
| 1017 | public: |
| 1018 | ForAllClosure(CompilationContext* context, size_t begin, size_t end, Callback* callback, |
| 1019 | size_t stripe) |
| 1020 | : context_(context), |
| 1021 | begin_(begin), |
| 1022 | end_(end), |
| 1023 | callback_(callback), |
| 1024 | stripe_(stripe) |
| 1025 | { |
| 1026 | |
| 1027 | } |
| 1028 | |
| 1029 | virtual void Run(Thread* self) { |
| 1030 | for (size_t i = begin_; i < end_; i += stripe_) { |
| 1031 | callback_(context_, i); |
| 1032 | self->AssertNoPendingException(); |
| 1033 | } |
| 1034 | } |
| 1035 | private: |
| 1036 | CompilationContext* const context_; |
| 1037 | const size_t begin_; |
| 1038 | const size_t end_; |
| 1039 | const Callback* callback_; |
| 1040 | const size_t stripe_; |
| 1041 | }; |
| 1042 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1043 | ClassLinker* const class_linker_; |
| 1044 | const jobject class_loader_; |
| 1045 | Compiler* const compiler_; |
| 1046 | const DexFile* const dex_file_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1047 | ThreadPool* thread_pool_; |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1048 | }; |
| 1049 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1050 | // Return true if the class should be skipped during compilation. We |
| 1051 | // never skip classes in the boot class loader. However, if we have a |
| 1052 | // non-boot class loader and we can resolve the class in the boot |
| 1053 | // class loader, we do skip the class. This happens if an app bundles |
| 1054 | // classes found in the boot classpath. Since at runtime we will |
| 1055 | // select the class from the boot classpath, do not attempt to resolve |
| 1056 | // or compile it now. |
| 1057 | static bool SkipClass(ClassLoader* class_loader, |
| 1058 | const DexFile& dex_file, |
| 1059 | const DexFile::ClassDef& class_def) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1060 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1061 | if (class_loader == NULL) { |
| 1062 | return false; |
| 1063 | } |
| 1064 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 1065 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1066 | Class* klass = class_linker->FindClass(descriptor, NULL); |
| 1067 | if (klass == NULL) { |
| 1068 | Thread* self = Thread::Current(); |
| 1069 | CHECK(self->IsExceptionPending()); |
| 1070 | self->ClearException(); |
| 1071 | return false; |
| 1072 | } |
| 1073 | return true; |
| 1074 | } |
| 1075 | |
| 1076 | static void ResolveClassFieldsAndMethods(const CompilationContext* context, size_t class_def_index) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1077 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1078 | ScopedObjectAccess soa(Thread::Current()); |
| 1079 | ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader()); |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1080 | const DexFile& dex_file = *context->GetDexFile(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1081 | |
| 1082 | // Method and Field are the worst. We can't resolve without either |
| 1083 | // context from the code use (to disambiguate virtual vs direct |
| 1084 | // method and instance vs static field) or from class |
| 1085 | // definitions. While the compiler will resolve what it can as it |
| 1086 | // needs it, here we try to resolve fields and methods used in class |
| 1087 | // definitions, since many of them many never be referenced by |
| 1088 | // generated code. |
| 1089 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1090 | if (SkipClass(class_loader, dex_file, class_def)) { |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1091 | return; |
| 1092 | } |
| 1093 | |
| 1094 | // Note the class_data pointer advances through the headers, |
| 1095 | // static fields, instance fields, direct methods, and virtual |
| 1096 | // methods. |
| 1097 | const byte* class_data = dex_file.GetClassData(class_def); |
| 1098 | if (class_data == NULL) { |
| 1099 | // empty class such as a marker interface |
| 1100 | return; |
| 1101 | } |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 1102 | Thread* self = Thread::Current(); |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1103 | ClassLinker* class_linker = context->GetClassLinker(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1104 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 1105 | ClassDataItemIterator it(dex_file, class_data); |
| 1106 | while (it.HasNextStaticField()) { |
| 1107 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1108 | class_loader, true); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1109 | if (field == NULL) { |
| 1110 | CHECK(self->IsExceptionPending()); |
| 1111 | self->ClearException(); |
| 1112 | } |
| 1113 | it.Next(); |
| 1114 | } |
| 1115 | while (it.HasNextInstanceField()) { |
| 1116 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1117 | class_loader, false); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1118 | if (field == NULL) { |
| 1119 | CHECK(self->IsExceptionPending()); |
| 1120 | self->ClearException(); |
| 1121 | } |
| 1122 | it.Next(); |
| 1123 | } |
| 1124 | while (it.HasNextDirectMethod()) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1125 | AbstractMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 1126 | class_loader, NULL, it.GetMethodInvokeType(class_def)); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1127 | if (method == NULL) { |
| 1128 | CHECK(self->IsExceptionPending()); |
| 1129 | self->ClearException(); |
| 1130 | } |
| 1131 | it.Next(); |
| 1132 | } |
| 1133 | while (it.HasNextVirtualMethod()) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1134 | AbstractMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 1135 | class_loader, NULL, it.GetMethodInvokeType(class_def)); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1136 | if (method == NULL) { |
| 1137 | CHECK(self->IsExceptionPending()); |
| 1138 | self->ClearException(); |
| 1139 | } |
| 1140 | it.Next(); |
| 1141 | } |
| 1142 | DCHECK(!it.HasNext()); |
| 1143 | } |
| 1144 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1145 | static void ResolveType(const CompilationContext* context, size_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1146 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1147 | // Class derived values are more complicated, they require the linker and loader. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1148 | ScopedObjectAccess soa(Thread::Current()); |
| 1149 | ClassLinker* class_linker = context->GetClassLinker(); |
| 1150 | const DexFile& dex_file = *context->GetDexFile(); |
| 1151 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 1152 | ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader()); |
| 1153 | Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 1154 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1155 | if (klass == NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1156 | CHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1157 | Thread::Current()->ClearException(); |
| 1158 | } |
| 1159 | } |
| 1160 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1161 | void Compiler::ResolveDexFile(jobject class_loader, const DexFile& dex_file, |
| 1162 | TimingLogger& timings) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1163 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1164 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1165 | // TODO: we could resolve strings here, although the string table is largely filled with class |
| 1166 | // and method names. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1167 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1168 | CompilationContext context(class_linker, class_loader, this, &dex_file, thread_pool_.get()); |
| 1169 | context.ForAll(0, dex_file.NumTypeIds(), ResolveType, thread_count_); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 1170 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types"); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 1171 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1172 | context.ForAll(0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 1173 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields"); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1176 | void Compiler::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files, |
| 1177 | TimingLogger& timings) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1178 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 1179 | const DexFile* dex_file = dex_files[i]; |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1180 | CHECK(dex_file != NULL); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1181 | VerifyDexFile(class_loader, *dex_file, timings); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1185 | static void VerifyClass(const CompilationContext* context, size_t class_def_index) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1186 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1187 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1188 | const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index); |
| 1189 | const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1190 | Class* klass = |
| 1191 | context->GetClassLinker()->FindClass(descriptor, |
| 1192 | soa.Decode<ClassLoader*>(context->GetClassLoader())); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1193 | if (klass == NULL) { |
| 1194 | Thread* self = Thread::Current(); |
| 1195 | CHECK(self->IsExceptionPending()); |
| 1196 | self->ClearException(); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1197 | |
| 1198 | /* |
| 1199 | * At compile time, we can still structurally verify the class even if FindClass fails. |
| 1200 | * This is to ensure the class is structurally sound for compilation. An unsound class |
| 1201 | * will be rejected by the verifier and later skipped during compilation in the compiler. |
| 1202 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1203 | DexCache* dex_cache = context->GetClassLinker()->FindDexCache(*context->GetDexFile()); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1204 | std::string error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1205 | if (verifier::MethodVerifier::VerifyClass(context->GetDexFile(), |
| 1206 | dex_cache, |
| 1207 | soa.Decode<ClassLoader*>(context->GetClassLoader()), |
| 1208 | class_def_index, error_msg) == |
| 1209 | verifier::MethodVerifier::kHardFailure) { |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1210 | const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1211 | LOG(ERROR) << "Verification failed on class " |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1212 | << PrettyDescriptor(context->GetDexFile()->GetClassDescriptor(class_def)) |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1213 | << " because: " << error_msg; |
| 1214 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1215 | return; |
| 1216 | } |
| 1217 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1218 | context->GetClassLinker()->VerifyClass(klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1219 | |
| 1220 | if (klass->IsErroneous()) { |
| 1221 | // ClassLinker::VerifyClass throws, which isn't useful in the compiler. |
| 1222 | CHECK(Thread::Current()->IsExceptionPending()); |
| 1223 | Thread::Current()->ClearException(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 1226 | CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous()) |
| 1227 | << PrettyDescriptor(klass) << ": state=" << klass->GetStatus(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 1228 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException()); |
| 1229 | } |
| 1230 | |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1231 | void Compiler::VerifyDexFile(jobject class_loader, const DexFile& dex_file, TimingLogger& timings) { |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1232 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1233 | CompilationContext context(class_linker, class_loader, this, &dex_file, thread_pool_.get()); |
| 1234 | context.ForAll(0, dex_file.NumClassDefs(), VerifyClass, thread_count_); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1235 | timings.AddSplit("Verify " + dex_file.GetLocation()); |
| 1236 | } |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 1237 | |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1238 | static void InitializeClassWithoutClinit(const CompilationContext* context, |
| 1239 | size_t class_def_index) |
| 1240 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
| 1241 | const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index); |
| 1242 | ScopedObjectAccess soa(Thread::Current()); |
| 1243 | ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader()); |
| 1244 | const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def); |
| 1245 | Class* klass = context->GetClassLinker()->FindClass(descriptor, class_loader); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1246 | Thread* self = Thread::Current(); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1247 | if (klass != NULL) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1248 | ObjectLock lock(self, klass); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1249 | if (klass->IsVerified()) { |
| 1250 | // Only try to initialize classes that were successfully verified. |
| 1251 | bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1; |
| 1252 | bool can_init_static_fields = compiling_boot && |
| 1253 | context->GetCompiler()->IsImageClass(descriptor); |
| 1254 | context->GetClassLinker()->EnsureInitialized(klass, false, can_init_static_fields); |
| 1255 | // If successfully initialized place in SSB array. |
| 1256 | if (klass->IsInitialized()) { |
| 1257 | klass->GetDexCache()->GetInitializedStaticStorage()->Set(klass->GetDexTypeIndex(), klass); |
| 1258 | } |
| 1259 | } |
| 1260 | // Record the final class status if necessary. |
| 1261 | Class::Status status = klass->GetStatus(); |
| 1262 | Compiler::ClassReference ref(context->GetDexFile(), class_def_index); |
| 1263 | CompiledClass* compiled_class = context->GetCompiler()->GetCompiledClass(ref); |
| 1264 | if (compiled_class == NULL) { |
| 1265 | compiled_class = new CompiledClass(status); |
| 1266 | context->GetCompiler()->RecordClassStatus(ref, compiled_class); |
| 1267 | } else { |
| 1268 | DCHECK_EQ(status, compiled_class->GetStatus()); |
| 1269 | } |
| 1270 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1271 | // Clear any class not found or verification exceptions. |
| 1272 | self->ClearException(); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | void Compiler::InitializeClassesWithoutClinit(jobject jni_class_loader, const DexFile& dex_file, |
| 1276 | TimingLogger& timings) { |
| 1277 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1278 | CompilationContext context(class_linker, jni_class_loader, this, &dex_file, thread_pool_.get()); |
| 1279 | context.ForAll(0, dex_file.NumClassDefs(), InitializeClassWithoutClinit, thread_count_); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1280 | timings.AddSplit("InitializeNoClinit " + dex_file.GetLocation()); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1283 | void Compiler::InitializeClassesWithoutClinit(jobject class_loader, |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1284 | const std::vector<const DexFile*>& dex_files, |
| 1285 | TimingLogger& timings) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1286 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 1287 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 1288 | CHECK(dex_file != NULL); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1289 | InitializeClassesWithoutClinit(class_loader, *dex_file, timings); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1293 | void Compiler::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files, |
| 1294 | TimingLogger& timings) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1295 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 1296 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1297 | CHECK(dex_file != NULL); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1298 | CompileDexFile(class_loader, *dex_file, timings); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1302 | void Compiler::CompileClass(const CompilationContext* context, size_t class_def_index) { |
| 1303 | jobject class_loader = context->GetClassLoader(); |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1304 | const DexFile& dex_file = *context->GetDexFile(); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 1305 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1306 | { |
| 1307 | ScopedObjectAccess soa(Thread::Current()); |
| 1308 | ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader()); |
| 1309 | if (SkipClass(class_loader, dex_file, class_def)) { |
| 1310 | return; |
| 1311 | } |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 1312 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 1313 | ClassReference ref(&dex_file, class_def_index); |
| 1314 | // Skip compiling classes with generic verifier failures since they will still fail at runtime |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1315 | if (verifier::MethodVerifier::IsClassRejected(ref)) { |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 1316 | return; |
| 1317 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1318 | const byte* class_data = dex_file.GetClassData(class_def); |
| 1319 | if (class_data == NULL) { |
| 1320 | // empty class, probably a marker interface |
| 1321 | return; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1322 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1323 | ClassDataItemIterator it(dex_file, class_data); |
| 1324 | // Skip fields |
| 1325 | while (it.HasNextStaticField()) { |
| 1326 | it.Next(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1327 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1328 | while (it.HasNextInstanceField()) { |
| 1329 | it.Next(); |
| 1330 | } |
| 1331 | // Compile direct methods |
Brian Carlstrom | 68adbe4 | 2012-05-11 17:18:08 -0700 | [diff] [blame] | 1332 | int64_t previous_direct_method_idx = -1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1333 | while (it.HasNextDirectMethod()) { |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 1334 | uint32_t method_idx = it.GetMemberIndex(); |
| 1335 | if (method_idx == previous_direct_method_idx) { |
| 1336 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 1337 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 1338 | it.Next(); |
| 1339 | continue; |
| 1340 | } |
| 1341 | previous_direct_method_idx = method_idx; |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1342 | context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1343 | it.GetMethodInvokeType(class_def), method_idx, |
| 1344 | class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1345 | it.Next(); |
| 1346 | } |
| 1347 | // Compile virtual methods |
Brian Carlstrom | 68adbe4 | 2012-05-11 17:18:08 -0700 | [diff] [blame] | 1348 | int64_t previous_virtual_method_idx = -1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1349 | while (it.HasNextVirtualMethod()) { |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 1350 | uint32_t method_idx = it.GetMemberIndex(); |
| 1351 | if (method_idx == previous_virtual_method_idx) { |
| 1352 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 1353 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 1354 | it.Next(); |
| 1355 | continue; |
| 1356 | } |
| 1357 | previous_virtual_method_idx = method_idx; |
Brian Carlstrom | 731b2ab | 2012-03-06 16:53:35 -0800 | [diff] [blame] | 1358 | context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1359 | it.GetMethodInvokeType(class_def), method_idx, |
| 1360 | class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1361 | it.Next(); |
| 1362 | } |
| 1363 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1366 | void Compiler::CompileDexFile(jobject class_loader, const DexFile& dex_file, |
| 1367 | TimingLogger& timings) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1368 | CompilationContext context(NULL, class_loader, this, &dex_file, thread_pool_.get()); |
| 1369 | context.ForAll(0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_); |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 1370 | timings.AddSplit("Compile " + dex_file.GetLocation()); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 1371 | } |
| 1372 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1373 | static std::string MakeInvokeStubKey(bool is_static, const char* shorty) { |
| 1374 | std::string key(shorty); |
| 1375 | if (is_static) { |
| 1376 | key += "$"; // Must not be a shorty type character. |
| 1377 | } |
| 1378 | return key; |
| 1379 | } |
| 1380 | |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 1381 | void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1382 | InvokeType invoke_type, uint32_t method_idx, jobject class_loader, |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 1383 | const DexFile& dex_file) { |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 1384 | CompiledMethod* compiled_method = NULL; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 1385 | uint64_t start_ns = NanoTime(); |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 1386 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 1387 | if ((access_flags & kAccNative) != 0) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1388 | compiled_method = (*jni_compiler_)(*this, access_flags, method_idx, dex_file); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1389 | CHECK(compiled_method != NULL); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 1390 | } else if ((access_flags & kAccAbstract) != 0) { |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 1391 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1392 | compiled_method = (*compiler_)(*this, code_item, access_flags, invoke_type, method_idx, |
| 1393 | class_loader, dex_file); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 1394 | CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file); |
| 1395 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 1396 | uint64_t duration_ns = NanoTime() - start_ns; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 1397 | if (duration_ns > MsToNs(100)) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 1398 | LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file) |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 1399 | << " took " << PrettyDuration(duration_ns); |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1402 | Thread* self = Thread::Current(); |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 1403 | if (compiled_method != NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1404 | MethodReference ref(&dex_file, method_idx); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1405 | CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1406 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1407 | MutexLock mu(self, compiled_methods_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1408 | compiled_methods_.Put(ref, compiled_method); |
| 1409 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1410 | DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 1411 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1412 | |
Ian Rogers | 45619fc | 2012-02-29 11:15:25 -0800 | [diff] [blame] | 1413 | uint32_t shorty_len; |
| 1414 | 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] | 1415 | bool is_static = (access_flags & kAccStatic) != 0; |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1416 | std::string key(MakeInvokeStubKey(is_static, shorty)); |
| 1417 | const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(key); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1418 | if (compiled_invoke_stub == NULL) { |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 1419 | compiled_invoke_stub = (*create_invoke_stub_)(*this, is_static, shorty, shorty_len); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1420 | CHECK(compiled_invoke_stub != NULL); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1421 | InsertInvokeStub(key, compiled_invoke_stub); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 1422 | } |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 1423 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 1424 | if (((compiler_backend_ == kPortable) || (compiler_backend_ == kIceland)) && !is_static) { |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 1425 | const CompiledInvokeStub* compiled_proxy_stub = FindProxyStub(shorty); |
| 1426 | if (compiled_proxy_stub == NULL) { |
| 1427 | compiled_proxy_stub = (*create_proxy_stub_)(*this, shorty, shorty_len); |
| 1428 | CHECK(compiled_proxy_stub != NULL); |
| 1429 | InsertProxyStub(shorty, compiled_proxy_stub); |
| 1430 | } |
| 1431 | } |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 1432 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1433 | if (self->IsExceptionPending()) { |
| 1434 | ScopedObjectAccess soa(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1435 | LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n" |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1436 | << self->GetException()->Dump(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1437 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1440 | const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const { |
| 1441 | const std::string key(MakeInvokeStubKey(is_static, shorty)); |
| 1442 | return FindInvokeStub(key); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1445 | const CompiledInvokeStub* Compiler::FindInvokeStub(const std::string& key) const { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1446 | MutexLock mu(Thread::Current(), compiled_invoke_stubs_lock_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1447 | InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1448 | if (it == compiled_invoke_stubs_.end()) { |
| 1449 | return NULL; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1450 | } else { |
| 1451 | DCHECK(it->second != NULL); |
| 1452 | return it->second; |
| 1453 | } |
| 1454 | } |
| 1455 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1456 | void Compiler::InsertInvokeStub(const std::string& key, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1457 | const CompiledInvokeStub* compiled_invoke_stub) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1458 | MutexLock mu(Thread::Current(), compiled_invoke_stubs_lock_); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1459 | InvokeStubTable::iterator it = compiled_invoke_stubs_.find(key); |
| 1460 | if (it != compiled_invoke_stubs_.end()) { |
| 1461 | // Someone else won the race. |
| 1462 | delete compiled_invoke_stub; |
| 1463 | } else { |
| 1464 | compiled_invoke_stubs_.Put(key, compiled_invoke_stub); |
| 1465 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 1468 | const CompiledInvokeStub* Compiler::FindProxyStub(const char* shorty) const { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1469 | MutexLock mu(Thread::Current(), compiled_proxy_stubs_lock_); |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 1470 | ProxyStubTable::const_iterator it = compiled_proxy_stubs_.find(shorty); |
| 1471 | if (it == compiled_proxy_stubs_.end()) { |
| 1472 | return NULL; |
| 1473 | } else { |
| 1474 | DCHECK(it->second != NULL); |
| 1475 | return it->second; |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | void Compiler::InsertProxyStub(const char* shorty, |
| 1480 | const CompiledInvokeStub* compiled_proxy_stub) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1481 | MutexLock mu(Thread::Current(), compiled_proxy_stubs_lock_); |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 1482 | InvokeStubTable::iterator it = compiled_proxy_stubs_.find(shorty); |
| 1483 | if (it != compiled_proxy_stubs_.end()) { |
| 1484 | // Someone else won the race. |
| 1485 | delete compiled_proxy_stub; |
| 1486 | } else { |
| 1487 | compiled_proxy_stubs_.Put(shorty, compiled_proxy_stub); |
| 1488 | } |
| 1489 | } |
Logan Chien | 7a2a23a | 2012-06-06 11:01:00 +0800 | [diff] [blame] | 1490 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1491 | CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1492 | MutexLock mu(Thread::Current(), compiled_classes_lock_); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1493 | ClassTable::const_iterator it = compiled_classes_.find(ref); |
| 1494 | if (it == compiled_classes_.end()) { |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | CHECK(it->second != NULL); |
| 1498 | return it->second; |
| 1499 | } |
| 1500 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1501 | CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1502 | MutexLock mu(Thread::Current(), compiled_methods_lock_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1503 | MethodTable::const_iterator it = compiled_methods_.find(ref); |
| 1504 | if (it == compiled_methods_.end()) { |
| 1505 | return NULL; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1506 | } |
| 1507 | CHECK(it->second != NULL); |
| 1508 | return it->second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1511 | void Compiler::SetBitcodeFileName(std::string const& filename) { |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 1512 | typedef void (*SetBitcodeFileNameFn)(Compiler&, std::string const&); |
| 1513 | |
| 1514 | SetBitcodeFileNameFn set_bitcode_file_name = |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 1515 | FindFunction<SetBitcodeFileNameFn>(MakeCompilerSoName(compiler_backend_, instruction_set_), |
Logan Chien | 106b2a0 | 2012-03-18 04:41:38 +0800 | [diff] [blame] | 1516 | compiler_library_, |
| 1517 | "compilerLLVMSetBitcodeFileName"); |
| 1518 | |
| 1519 | set_bitcode_file_name(*this, filename); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1520 | } |
Logan Chien | f7015fd | 2012-03-18 01:19:37 +0800 | [diff] [blame] | 1521 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1522 | } // namespace art |