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