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