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