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