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