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