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