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 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 21 | #include <sys/mman.h> |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 22 | #include <unistd.h> |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 23 | |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 24 | #include "assembler.h" |
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" |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 27 | #include "compiler/CompilerIR.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 28 | #include "dex_cache.h" |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 29 | #include "jni_compiler.h" |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 30 | #include "jni_internal.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" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 34 | #include "stl_util.h" |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 35 | #include "timing_logger.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 36 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 39 | CompiledMethod* oatCompileMethod(Compiler& compiler, const DexFile::CodeItem* code_item, |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 40 | uint32_t access_flags, uint32_t method_idx, |
| 41 | const ClassLoader* class_loader, |
| 42 | const DexFile& dex_file, InstructionSet); |
| 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 | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 46 | CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 47 | ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type); |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 48 | ByteArray* CreateJniDlsymLookupStub(); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 49 | } |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 50 | namespace x86 { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 51 | ByteArray* CreateAbstractMethodErrorStub(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 52 | CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 53 | ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type); |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 54 | ByteArray* CreateJniDlsymLookupStub(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 57 | static double Percentage(size_t x, size_t y) { |
| 58 | return 100.0 * ((double)x) / ((double)(x + y)); |
| 59 | } |
| 60 | |
| 61 | static void DumpStat(size_t x, size_t y, const char* str) { |
| 62 | if (x == 0 && y == 0) { |
| 63 | return; |
| 64 | } |
| 65 | LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases"; |
| 66 | } |
| 67 | |
| 68 | void AOTCompilationStats::Dump() { |
| 69 | DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache"); |
| 70 | DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache"); |
| 71 | DumpStat(resolved_types_, unresolved_types_, "types resolved"); |
| 72 | DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved"); |
| 73 | DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_, |
| 74 | "static fields resolved"); |
| 75 | DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_, |
| 76 | "static fields local to a class"); |
| 77 | DumpStat(resolved_virtual_methods_, unresolved_virtual_methods_, "resolved virtual methods"); |
| 78 | DumpStat(resolved_super_methods_, unresolved_super_methods_, "resolved super-class methods"); |
| 79 | DumpStat(resolved_interface_methods_, unresolved_interface_methods_, "resolved interface methods"); |
| 80 | } |
| 81 | |
| 82 | // Allow lossy statistics in non-debug builds |
| 83 | #ifndef NDEBUG |
| 84 | #define STATS_LOCK() MutexLock mu(stats_lock_) |
| 85 | #else |
| 86 | #define STATS_LOCK() |
| 87 | #endif |
| 88 | |
| 89 | void AOTCompilationStats::TypeInDexCache() { |
| 90 | STATS_LOCK(); |
| 91 | types_in_dex_cache_++; |
| 92 | } |
| 93 | |
| 94 | void AOTCompilationStats::TypeNotInDexCache() { |
| 95 | STATS_LOCK(); |
| 96 | types_not_in_dex_cache_++; |
| 97 | } |
| 98 | |
| 99 | void AOTCompilationStats::StringInDexCache() { |
| 100 | STATS_LOCK(); |
| 101 | strings_in_dex_cache_++; |
| 102 | } |
| 103 | |
| 104 | void AOTCompilationStats::StringNotInDexCache() { |
| 105 | STATS_LOCK(); |
| 106 | strings_not_in_dex_cache_++; |
| 107 | } |
| 108 | |
| 109 | void AOTCompilationStats::TypeDoesntNeedAccessCheck() { |
| 110 | STATS_LOCK(); |
| 111 | resolved_types_++; |
| 112 | } |
| 113 | |
| 114 | void AOTCompilationStats::TypeNeedsAccessCheck() { |
| 115 | STATS_LOCK(); |
| 116 | unresolved_types_++; |
| 117 | } |
| 118 | |
| 119 | void AOTCompilationStats::ResolvedInstanceField() { |
| 120 | STATS_LOCK(); |
| 121 | resolved_instance_fields_++; |
| 122 | } |
| 123 | |
| 124 | void AOTCompilationStats::UnresolvedInstanceField(){ |
| 125 | STATS_LOCK(); |
| 126 | unresolved_instance_fields_++; |
| 127 | } |
| 128 | |
| 129 | void AOTCompilationStats::ResolvedLocalStaticField() { |
| 130 | STATS_LOCK(); |
| 131 | resolved_local_static_fields_++; |
| 132 | } |
| 133 | |
| 134 | void AOTCompilationStats::ResolvedStaticField() { |
| 135 | STATS_LOCK(); |
| 136 | resolved_static_fields_++; |
| 137 | } |
| 138 | |
| 139 | void AOTCompilationStats::UnresolvedStaticField() { |
| 140 | STATS_LOCK(); |
| 141 | unresolved_static_fields_++; |
| 142 | } |
| 143 | |
| 144 | void AOTCompilationStats::ResolvedMethod(bool is_interface, bool is_super) { |
| 145 | STATS_LOCK(); |
| 146 | if (is_interface) { |
| 147 | resolved_interface_methods_++; |
| 148 | } else if (is_super) { |
| 149 | resolved_super_methods_++; |
| 150 | } else { |
| 151 | resolved_virtual_methods_++; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void AOTCompilationStats::UnresolvedMethod(bool is_interface, bool is_super) { |
| 156 | STATS_LOCK(); |
| 157 | if (is_interface) { |
| 158 | unresolved_interface_methods_++; |
| 159 | } else if (is_super) { |
| 160 | unresolved_super_methods_++; |
| 161 | } else { |
| 162 | unresolved_virtual_methods_++; |
| 163 | } |
| 164 | } |
| 165 | |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 166 | Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 167 | const std::set<std::string>* image_classes) |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 168 | : instruction_set_(instruction_set), |
| 169 | jni_compiler_(instruction_set), |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 170 | compiled_classes_lock_("compiled classes lock"), |
| 171 | compiled_methods_lock_("compiled method lock"), |
| 172 | compiled_invoke_stubs_lock_("compiled invoke stubs lock"), |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 173 | image_(image), |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 174 | thread_count_(thread_count), |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 175 | image_classes_(image_classes) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 176 | CHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 177 | if (!image_) { |
| 178 | CHECK(image_classes_ == NULL); |
| 179 | } |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 182 | Compiler::~Compiler() { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 183 | { |
| 184 | MutexLock mu(compiled_classes_lock_); |
| 185 | STLDeleteValues(&compiled_classes_); |
| 186 | } |
| 187 | { |
| 188 | MutexLock mu(compiled_methods_lock_); |
| 189 | STLDeleteValues(&compiled_methods_); |
| 190 | } |
| 191 | { |
| 192 | MutexLock mu(compiled_invoke_stubs_lock_); |
| 193 | STLDeleteValues(&compiled_invoke_stubs_); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 194 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 197 | ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set, |
| 198 | Runtime::TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 199 | if (instruction_set == kX86) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 200 | return x86::X86CreateResolutionTrampoline(type); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 201 | } else { |
| 202 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
| 203 | // Generates resolution stub using ARM instruction set |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 204 | return arm::ArmCreateResolutionTrampoline(type); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 208 | ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 209 | switch (instruction_set) { |
| 210 | case kArm: |
| 211 | case kThumb2: |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 212 | return arm::CreateJniDlsymLookupStub(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 213 | case kX86: |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 214 | return x86::CreateJniDlsymLookupStub(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 215 | default: |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 216 | LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | } |
| 220 | |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 221 | ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) { |
| 222 | if (instruction_set == kX86) { |
| 223 | return x86::CreateAbstractMethodErrorStub(); |
| 224 | } else { |
| 225 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
| 226 | // Generates resolution stub using ARM instruction set |
| 227 | return arm::CreateAbstractMethodErrorStub(); |
| 228 | } |
| 229 | } |
| 230 | |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 231 | void Compiler::CompileAll(const ClassLoader* class_loader, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 232 | const std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 233 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 234 | |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 235 | TimingLogger timings("compiler"); |
| 236 | |
| 237 | PreCompile(class_loader, dex_files, timings); |
| 238 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 239 | Compile(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 240 | timings.AddSplit("Compile"); |
| 241 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 242 | PostCompile(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 243 | timings.AddSplit("PostCompile"); |
| 244 | |
| 245 | if (timings.GetTotalNs() > MsToNs(1000)) { |
| 246 | timings.Dump(); |
| 247 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 248 | |
| 249 | stats_.Dump(); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 252 | void Compiler::CompileOne(const Method* method) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 253 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 254 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 255 | const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 256 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 257 | // Find the dex_file |
| 258 | const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); |
| 259 | const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 260 | std::vector<const DexFile*> dex_files; |
| 261 | dex_files.push_back(&dex_file); |
| 262 | |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 263 | TimingLogger timings("CompileOne"); |
| 264 | PreCompile(class_loader, dex_files, timings); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 265 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 266 | uint32_t method_idx = method->GetDexMethodIndex(); |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 267 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
| 268 | CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 269 | |
| 270 | PostCompile(class_loader, dex_files); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 273 | void Compiler::Resolve(const ClassLoader* class_loader, |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 274 | const std::vector<const DexFile*>& dex_files, TimingLogger& timings) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 275 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 276 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 277 | CHECK(dex_file != NULL); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 278 | ResolveDexFile(class_loader, *dex_file, timings); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 282 | void Compiler::PreCompile(const ClassLoader* class_loader, |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 283 | const std::vector<const DexFile*>& dex_files, TimingLogger& timings) { |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 284 | Resolve(class_loader, dex_files, timings); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 285 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 286 | Verify(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 287 | timings.AddSplit("PreCompile.Verify"); |
| 288 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 289 | InitializeClassesWithoutClinit(class_loader, dex_files); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 290 | timings.AddSplit("PreCompile.InitializeClassesWithoutClinit"); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void Compiler::PostCompile(const ClassLoader* class_loader, |
| 294 | const std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 295 | SetGcMaps(class_loader, dex_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 296 | SetCodeAndDirectMethods(dex_files); |
| 297 | } |
| 298 | |
| 299 | bool Compiler::IsImageClass(const std::string& descriptor) const { |
| 300 | if (image_classes_ == NULL) { |
| 301 | return true; |
| 302 | } |
| 303 | return image_classes_->find(descriptor) != image_classes_->end(); |
| 304 | } |
| 305 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 306 | bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 307 | uint32_t type_idx) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 308 | if (!IsImage()) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 309 | stats_.TypeNotInDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 310 | return false; |
| 311 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 312 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 313 | if (resolved_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 314 | stats_.TypeNotInDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 315 | return false; |
| 316 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 317 | bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor()); |
| 318 | if (result) { |
| 319 | stats_.TypeInDexCache(); |
| 320 | } else { |
| 321 | stats_.TypeNotInDexCache(); |
| 322 | } |
| 323 | return result; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 326 | bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 327 | uint32_t string_idx) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 328 | // TODO: Add support for loading strings referenced by image_classes_ |
| 329 | // See also Compiler::ResolveDexFile |
| 330 | |
| 331 | // The following is a test saying that if we're building the image without a restricted set of |
| 332 | // 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] | 333 | bool result = IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL; |
| 334 | if (result) { |
| 335 | stats_.StringInDexCache(); |
| 336 | } else { |
| 337 | stats_.StringNotInDexCache(); |
| 338 | } |
| 339 | return result; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 343 | const DexFile& dex_file, uint32_t type_idx) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 344 | // Get type from dex cache assuming it was populated by the verifier |
| 345 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
| 346 | if (resolved_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 347 | stats_.TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 348 | return false; // Unknown class needs access checks. |
| 349 | } |
| 350 | const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx); |
| 351 | Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_); |
| 352 | if (referrer_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 353 | stats_.TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 354 | return false; // Incomplete referrer knowledge needs access check. |
| 355 | } |
| 356 | // Perform access check, will return true if access is ok or false if we're going to have to |
| 357 | // check this at runtime (for example for class loaders). |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 358 | bool result = referrer_class->CanAccess(resolved_class); |
| 359 | if (result) { |
| 360 | stats_.TypeDoesntNeedAccessCheck(); |
| 361 | } else { |
| 362 | stats_.TypeNeedsAccessCheck(); |
| 363 | } |
| 364 | return result; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, |
| 368 | const DexCache* dex_cache, |
| 369 | const DexFile& dex_file, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 370 | uint32_t type_idx) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 371 | // Get type from dex cache assuming it was populated by the verifier. |
| 372 | Class* resolved_class = dex_cache->GetResolvedType(type_idx); |
| 373 | if (resolved_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 374 | stats_.TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 375 | return false; // Unknown class needs access checks. |
| 376 | } |
| 377 | const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx); |
| 378 | Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_); |
| 379 | if (referrer_class == NULL) { |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 380 | stats_.TypeNeedsAccessCheck(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 381 | return false; // Incomplete referrer knowledge needs access check. |
| 382 | } |
| 383 | // Perform access and instantiable checks, will return true if access is ok or false if we're |
| 384 | // 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] | 385 | bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable(); |
| 386 | if (result) { |
| 387 | stats_.TypeDoesntNeedAccessCheck(); |
| 388 | } else { |
| 389 | stats_.TypeNeedsAccessCheck(); |
| 390 | } |
| 391 | return result; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 394 | static Class* ComputeReferrerClass(CompilationUnit* cUnit) { |
| 395 | const DexFile::MethodId& referrer_method_id = cUnit->dex_file->GetMethodId(cUnit->method_idx); |
| 396 | return cUnit->class_linker->ResolveType(*cUnit->dex_file, referrer_method_id.class_idx_, |
| 397 | cUnit->dex_cache, cUnit->class_loader); |
| 398 | } |
| 399 | |
| 400 | static Field* ComputeReferrerField(CompilationUnit* cUnit, uint32_t field_idx) { |
| 401 | return cUnit->class_linker->ResolveField(*cUnit->dex_file, field_idx, cUnit->dex_cache, |
| 402 | cUnit->class_loader, false); |
| 403 | |
| 404 | } |
| 405 | |
| 406 | static Method* ComputeReferrerMethod(CompilationUnit* cUnit, uint32_t method_idx) { |
| 407 | return cUnit->class_linker->ResolveMethod(*cUnit->dex_file, method_idx, cUnit->dex_cache, |
| 408 | cUnit->class_loader, true); |
| 409 | } |
| 410 | |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 411 | bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, CompilationUnit* cUnit, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 412 | int& field_offset, bool& is_volatile) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 413 | // Conservative defaults |
| 414 | field_offset = -1; |
| 415 | is_volatile = true; |
| 416 | // Try to resolve field |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 417 | Field* resolved_field = ComputeReferrerField(cUnit, field_idx); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 418 | if (resolved_field != NULL) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 419 | Class* referrer_class = ComputeReferrerClass(cUnit); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 420 | // Try to resolve referring class then access check, failure to pass the |
| 421 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 422 | if (referrer_class != NULL && |
| 423 | referrer_class->CanAccess(fields_class) && |
| 424 | referrer_class->CanAccessMember(fields_class, |
| 425 | resolved_field->GetAccessFlags())) { |
| 426 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 427 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 428 | stats_.ResolvedInstanceField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 429 | return true; // Fast path. |
| 430 | } |
| 431 | } |
| 432 | // Clean up any exception left by field/type resolution |
| 433 | Thread* thread = Thread::Current(); |
| 434 | if (thread->IsExceptionPending()) { |
| 435 | thread->ClearException(); |
| 436 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 437 | stats_.UnresolvedInstanceField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 438 | return false; // Incomplete knowledge needs slow path. |
| 439 | } |
| 440 | |
| 441 | bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, CompilationUnit* cUnit, |
| 442 | int& field_offset, int& ssb_index, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 443 | bool& is_referrers_class, bool& is_volatile) { |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 444 | // Conservative defaults |
| 445 | field_offset = -1; |
| 446 | ssb_index = -1; |
| 447 | is_referrers_class = false; |
| 448 | is_volatile = true; |
| 449 | // Try to resolve field |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 450 | Field* resolved_field = ComputeReferrerField(cUnit, field_idx); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 451 | if (resolved_field != NULL) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 452 | DCHECK(resolved_field->IsStatic()); |
| 453 | Class* referrer_class = ComputeReferrerClass(cUnit); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 454 | if (referrer_class != NULL) { |
| 455 | if (resolved_field->GetDeclaringClass() == referrer_class) { |
| 456 | is_referrers_class = true; // implies no worrying about class initialization |
| 457 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 458 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 459 | stats_.ResolvedLocalStaticField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 460 | return true; // fast path |
| 461 | } else { |
| 462 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 463 | if (referrer_class->CanAccess(fields_class) && |
| 464 | referrer_class->CanAccessMember(fields_class, |
| 465 | resolved_field->GetAccessFlags())) { |
| 466 | // We have the resolved field, we must make it into a ssbIndex for the referrer |
| 467 | // 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] | 468 | // TODO: for images we can elide the static storage base null check |
| 469 | // if we know there's a non-null entry in the image |
| 470 | if (fields_class->GetDexCache() == cUnit->dex_cache) { |
| 471 | // common case where the dex cache of both the referrer and the field are the same, |
| 472 | // no need to search the dex file |
| 473 | ssb_index = fields_class->GetDexTypeIndex(); |
| 474 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 475 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 476 | stats_.ResolvedStaticField(); |
Ian Rogers | 4103ad2 | 2012-02-06 09:18:25 -0800 | [diff] [blame] | 477 | return true; |
| 478 | } |
| 479 | // Search dex file for localized ssb index |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 480 | std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor()); |
| 481 | const DexFile::StringId* string_id = |
| 482 | cUnit->dex_file->FindStringId(descriptor); |
| 483 | if (string_id != NULL) { |
| 484 | const DexFile::TypeId* type_id = |
| 485 | cUnit->dex_file->FindTypeId(cUnit->dex_file->GetIndexForStringId(*string_id)); |
| 486 | if(type_id != NULL) { |
| 487 | // medium path, needs check of static storage base being initialized |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 488 | ssb_index = cUnit->dex_file->GetIndexForTypeId(*type_id); |
| 489 | field_offset = resolved_field->GetOffset().Int32Value(); |
| 490 | is_volatile = resolved_field->IsVolatile(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 491 | stats_.ResolvedStaticField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 492 | return true; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | // Clean up any exception left by field/type resolution |
| 500 | Thread* thread = Thread::Current(); |
| 501 | if (thread->IsExceptionPending()) { |
| 502 | thread->ClearException(); |
| 503 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 504 | stats_.UnresolvedStaticField(); |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 505 | return false; // Incomplete knowledge needs slow path. |
| 506 | } |
| 507 | |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 508 | bool Compiler::ComputeInvokeInfo(uint32_t method_idx, CompilationUnit* cUnit, |
| 509 | bool is_interface, bool is_super, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 510 | int& vtable_idx) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 511 | vtable_idx = -1; |
| 512 | Method* resolved_method = ComputeReferrerMethod(cUnit, method_idx); |
| 513 | if (resolved_method != NULL) { |
| 514 | Class* referrer_class = ComputeReferrerClass(cUnit); |
| 515 | if (referrer_class != NULL) { |
| 516 | Class* methods_class = resolved_method->GetDeclaringClass(); |
| 517 | if (!referrer_class->CanAccess(methods_class) || |
| 518 | !referrer_class->CanAccessMember(methods_class, |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 519 | resolved_method->GetAccessFlags())) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 520 | // The referring class can't access the resolved method, this may occur as a result of a |
| 521 | // protected method being made public by implementing an interface that re-declares the |
| 522 | // method public. Resort to the dex file to determine the correct class for the access check |
| 523 | const DexFile& dex_file = cUnit->class_linker->FindDexFile(referrer_class->GetDexCache()); |
| 524 | methods_class = |
| 525 | cUnit->class_linker->ResolveType(dex_file, |
| 526 | dex_file.GetMethodId(method_idx).class_idx_, |
| 527 | referrer_class); |
| 528 | |
| 529 | } |
| 530 | if (referrer_class->CanAccess(methods_class) && |
| 531 | referrer_class->CanAccessMember(methods_class, |
| 532 | resolved_method->GetAccessFlags())) { |
| 533 | vtable_idx = resolved_method->GetMethodIndex(); |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 534 | if (is_interface || !is_super) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 535 | // nothing left to do for virtual/interface dispatch |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 536 | stats_.ResolvedMethod(is_interface, is_super); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 537 | return true; |
| 538 | } else { |
| 539 | // ensure the vtable index will be correct to dispatch in the vtable of the super class |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 540 | if (referrer_class->IsSubClass(methods_class) && |
| 541 | vtable_idx < methods_class->GetVTable()->GetLength()) { |
| 542 | stats_.ResolvedMethod(is_interface, is_super); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 543 | return true; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | // Clean up any exception left by method/type resolution |
| 550 | Thread* thread = Thread::Current(); |
| 551 | if (thread->IsExceptionPending()) { |
| 552 | thread->ClearException(); |
| 553 | } |
Ian Rogers | 996cc58 | 2012-02-14 22:23:29 -0800 | [diff] [blame] | 554 | stats_.UnresolvedMethod(is_interface, is_super); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 555 | return false; // Incomplete knowledge needs slow path. |
| 556 | } |
| 557 | |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 558 | // Return true if the class should be skipped during compilation. We |
| 559 | // never skip classes in the boot class loader. However, if we have a |
| 560 | // non-boot class loader and we can resolve the class in the boot |
| 561 | // class loader, we do skip the class. This happens if an app bundles |
| 562 | // classes found in the boot classpath. Since at runtime we will |
| 563 | // select the class from the boot classpath, do not attempt to resolve |
| 564 | // or compile it now. |
| 565 | static bool SkipClass(const ClassLoader* class_loader, |
| 566 | const DexFile& dex_file, |
| 567 | const DexFile::ClassDef& class_def) { |
| 568 | if (class_loader == NULL) { |
| 569 | return false; |
| 570 | } |
| 571 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 572 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 573 | Class* klass = class_linker->FindClass(descriptor, NULL); |
| 574 | if (klass == NULL) { |
| 575 | Thread* self = Thread::Current(); |
| 576 | CHECK(self->IsExceptionPending()); |
| 577 | self->ClearException(); |
| 578 | return false; |
| 579 | } |
| 580 | return true; |
| 581 | } |
| 582 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 583 | struct Context { |
| 584 | ClassLinker* class_linker; |
| 585 | const ClassLoader* class_loader; |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 586 | Compiler* compiler; |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 587 | DexCache* dex_cache; |
| 588 | const DexFile* dex_file; |
| 589 | }; |
| 590 | |
| 591 | typedef void Callback(Context* context, size_t index); |
| 592 | |
| 593 | class WorkerThread { |
| 594 | public: |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 595 | WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn) |
| 596 | : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) { |
| 597 | if (spawn_) { |
| 598 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread"); |
| 599 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | ~WorkerThread() { |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 603 | if (spawn_) { |
| 604 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown"); |
| 605 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | private: |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 609 | static void* Go(void* arg) { |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 610 | WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg); |
| 611 | Runtime* runtime = Runtime::Current(); |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 612 | if (worker->spawn_) { |
| 613 | runtime->AttachCurrentThread("Compiler Worker", true); |
| 614 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 615 | Thread::Current()->SetState(Thread::kRunnable); |
| 616 | worker->Run(); |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 617 | if (worker->spawn_) { |
| 618 | Thread::Current()->SetState(Thread::kNative); |
| 619 | runtime->DetachCurrentThread(); |
| 620 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 621 | return NULL; |
| 622 | } |
| 623 | |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 624 | void Go() { |
| 625 | Go(this); |
| 626 | } |
| 627 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 628 | void Run() { |
| 629 | for (size_t i = begin_; i < end_; i += stripe_) { |
| 630 | callback_(context_, i); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | pthread_t pthread_; |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 635 | bool spawn_; |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 636 | |
| 637 | Context* context_; |
| 638 | size_t begin_; |
| 639 | size_t end_; |
| 640 | Callback* callback_; |
| 641 | size_t stripe_; |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 642 | |
| 643 | friend void ForAll(Context*, size_t, size_t, Callback, size_t); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 644 | }; |
| 645 | |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 646 | void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) { |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 647 | CHECK_GT(thread_count, 0U); |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 648 | |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 649 | std::vector<WorkerThread*> threads; |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 650 | for (size_t i = 0; i < thread_count; ++i) { |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 651 | 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] | 652 | } |
Elliott Hughes | 1e40925 | 2012-02-06 11:21:27 -0800 | [diff] [blame] | 653 | threads[0]->Go(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 654 | |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 655 | // Switch to kVmWait while we're blocked waiting for the other threads to finish. |
| 656 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 657 | STLDeleteElements(&threads); |
| 658 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 659 | |
| 660 | static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) { |
| 661 | const DexFile& dex_file = *context->dex_file; |
| 662 | |
| 663 | // Method and Field are the worst. We can't resolve without either |
| 664 | // context from the code use (to disambiguate virtual vs direct |
| 665 | // method and instance vs static field) or from class |
| 666 | // definitions. While the compiler will resolve what it can as it |
| 667 | // needs it, here we try to resolve fields and methods used in class |
| 668 | // definitions, since many of them many never be referenced by |
| 669 | // generated code. |
| 670 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 671 | if (SkipClass(context->class_loader, dex_file, class_def)) { |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // Note the class_data pointer advances through the headers, |
| 676 | // static fields, instance fields, direct methods, and virtual |
| 677 | // methods. |
| 678 | const byte* class_data = dex_file.GetClassData(class_def); |
| 679 | if (class_data == NULL) { |
| 680 | // empty class such as a marker interface |
| 681 | return; |
| 682 | } |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 683 | Thread* self = Thread::Current(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 684 | ClassLinker* class_linker = context->class_linker; |
| 685 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 686 | ClassDataItemIterator it(dex_file, class_data); |
| 687 | while (it.HasNextStaticField()) { |
| 688 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 689 | context->class_loader, true); |
| 690 | if (field == NULL) { |
| 691 | CHECK(self->IsExceptionPending()); |
| 692 | self->ClearException(); |
| 693 | } |
| 694 | it.Next(); |
| 695 | } |
| 696 | while (it.HasNextInstanceField()) { |
| 697 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 698 | context->class_loader, false); |
| 699 | if (field == NULL) { |
| 700 | CHECK(self->IsExceptionPending()); |
| 701 | self->ClearException(); |
| 702 | } |
| 703 | it.Next(); |
| 704 | } |
| 705 | while (it.HasNextDirectMethod()) { |
| 706 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 707 | context->class_loader, true); |
| 708 | if (method == NULL) { |
| 709 | CHECK(self->IsExceptionPending()); |
| 710 | self->ClearException(); |
| 711 | } |
| 712 | it.Next(); |
| 713 | } |
| 714 | while (it.HasNextVirtualMethod()) { |
| 715 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 716 | context->class_loader, false); |
| 717 | if (method == NULL) { |
| 718 | CHECK(self->IsExceptionPending()); |
| 719 | self->ClearException(); |
| 720 | } |
| 721 | it.Next(); |
| 722 | } |
| 723 | DCHECK(!it.HasNext()); |
| 724 | } |
| 725 | |
| 726 | static void ResolveType(Context* context, size_t type_idx) { |
| 727 | // Class derived values are more complicated, they require the linker and loader. |
| 728 | Thread* self = Thread::Current(); |
| 729 | ClassLinker* class_linker = context->class_linker; |
| 730 | const DexFile& dex_file = *context->dex_file; |
| 731 | Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader); |
| 732 | if (klass == NULL) { |
| 733 | CHECK(self->IsExceptionPending()); |
| 734 | Thread::Current()->ClearException(); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | 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] | 739 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 740 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 741 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 742 | // Strings are easy in that they always are simply resolved to literals in the same file |
| 743 | if (image_ && image_classes_ == NULL) { |
| 744 | // TODO: Add support for loading strings referenced by image_classes_ |
| 745 | // See also Compiler::CanAssumeTypeIsPresentInDexCache. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 746 | for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) { |
| 747 | class_linker->ResolveString(dex_file, string_idx, dex_cache); |
| 748 | } |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 749 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings"); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 752 | Context context; |
| 753 | context.class_linker = class_linker; |
| 754 | context.class_loader = class_loader; |
| 755 | context.dex_cache = dex_cache; |
| 756 | context.dex_file = &dex_file; |
| 757 | |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 758 | ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 759 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types"); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 760 | |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 761 | ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 762 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields"); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 765 | void Compiler::Verify(const ClassLoader* class_loader, |
| 766 | const std::vector<const DexFile*>& dex_files) { |
| 767 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 768 | const DexFile* dex_file = dex_files[i]; |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 769 | CHECK(dex_file != NULL); |
| 770 | VerifyDexFile(class_loader, *dex_file); |
| 771 | } |
| 772 | } |
| 773 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 774 | static void VerifyClass(Context* context, size_t class_def_index) { |
| 775 | const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index); |
| 776 | const char* descriptor = context->dex_file->GetClassDescriptor(class_def); |
| 777 | Class* klass = context->class_linker->FindClass(descriptor, context->class_loader); |
| 778 | if (klass == NULL) { |
| 779 | Thread* self = Thread::Current(); |
| 780 | CHECK(self->IsExceptionPending()); |
| 781 | self->ClearException(); |
| 782 | return; |
| 783 | } |
| 784 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
| 785 | context->class_linker->VerifyClass(klass); |
| 786 | |
| 787 | if (klass->IsErroneous()) { |
| 788 | // ClassLinker::VerifyClass throws, which isn't useful in the compiler. |
| 789 | CHECK(Thread::Current()->IsExceptionPending()); |
| 790 | Thread::Current()->ClearException(); |
| 791 | // We want to try verification again at run-time, so move back into the resolved state. |
| 792 | klass->SetStatus(Class::kStatusResolved); |
| 793 | } |
| 794 | |
| 795 | CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass); |
| 796 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException()); |
| 797 | } |
| 798 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 799 | void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 800 | dex_file.ChangePermissions(PROT_READ | PROT_WRITE); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 801 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 802 | Context context; |
| 803 | context.class_linker = Runtime::Current()->GetClassLinker(); |
| 804 | context.class_loader = class_loader; |
| 805 | context.dex_file = &dex_file; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 806 | ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 807 | |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 808 | dex_file.ChangePermissions(PROT_READ); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 811 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, |
| 812 | const std::vector<const DexFile*>& dex_files) { |
| 813 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 814 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 815 | CHECK(dex_file != NULL); |
| 816 | InitializeClassesWithoutClinit(class_loader, *dex_file); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 821 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 822 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 823 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 824 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 825 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 826 | if (klass != NULL) { |
| 827 | class_linker->EnsureInitialized(klass, false); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 828 | // record the final class status if necessary |
| 829 | Class::Status status = klass->GetStatus(); |
| 830 | ClassReference ref(&dex_file, class_def_index); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 831 | MutexLock mu(compiled_classes_lock_); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 832 | CompiledClass* compiled_class = GetCompiledClass(ref); |
| 833 | if (compiled_class == NULL) { |
| 834 | compiled_class = new CompiledClass(status); |
| 835 | compiled_classes_[ref] = compiled_class; |
| 836 | } else { |
| 837 | DCHECK_EQ(status, compiled_class->GetStatus()); |
| 838 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 839 | } |
| 840 | // clear any class not found or verification exceptions |
| 841 | Thread::Current()->ClearException(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 845 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 846 | 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] | 847 | if (klass == NULL) { |
| 848 | Thread::Current()->ClearException(); |
| 849 | } else if (klass->IsInitialized()) { |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 850 | dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass); |
| 851 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 855 | void Compiler::Compile(const ClassLoader* class_loader, |
| 856 | const std::vector<const DexFile*>& dex_files) { |
| 857 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 858 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 859 | CHECK(dex_file != NULL); |
| 860 | CompileDexFile(class_loader, *dex_file); |
| 861 | } |
| 862 | } |
| 863 | |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 864 | void Compiler::CompileClass(Context* context, size_t class_def_index) { |
| 865 | const ClassLoader* class_loader = context->class_loader; |
| 866 | const DexFile& dex_file = *context->dex_file; |
| 867 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 868 | if (SkipClass(class_loader, dex_file, class_def)) { |
| 869 | return; |
| 870 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 871 | const byte* class_data = dex_file.GetClassData(class_def); |
| 872 | if (class_data == NULL) { |
| 873 | // empty class, probably a marker interface |
| 874 | return; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 875 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 876 | ClassDataItemIterator it(dex_file, class_data); |
| 877 | // Skip fields |
| 878 | while (it.HasNextStaticField()) { |
| 879 | it.Next(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 880 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 881 | while (it.HasNextInstanceField()) { |
| 882 | it.Next(); |
| 883 | } |
| 884 | // Compile direct methods |
| 885 | while (it.HasNextDirectMethod()) { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 886 | context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), |
| 887 | it.GetMemberIndex(), class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 888 | it.Next(); |
| 889 | } |
| 890 | // Compile virtual methods |
| 891 | while (it.HasNextVirtualMethod()) { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 892 | context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), |
| 893 | it.GetMemberIndex(), class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 894 | it.Next(); |
| 895 | } |
| 896 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 899 | void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 900 | Context context; |
| 901 | context.class_loader = class_loader; |
| 902 | context.compiler = this; |
| 903 | context.dex_file = &dex_file; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 904 | ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 905 | } |
| 906 | |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 907 | void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
| 908 | uint32_t method_idx, const ClassLoader* class_loader, |
| 909 | const DexFile& dex_file) { |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 910 | CompiledMethod* compiled_method = NULL; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 911 | uint64_t start_ns = NanoTime(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 912 | if ((access_flags & kAccNative) != 0) { |
| 913 | compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 914 | CHECK(compiled_method != NULL); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 915 | } else if ((access_flags & kAccAbstract) != 0) { |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 916 | } else { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 917 | compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader, |
| 918 | dex_file, kThumb2); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 919 | CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file); |
| 920 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 921 | uint64_t duration_ns = NanoTime() - start_ns; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 922 | if (duration_ns > MsToNs(100)) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 923 | LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file) |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 924 | << " took " << PrettyDuration(duration_ns); |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | if (compiled_method != NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 928 | MethodReference ref(&dex_file, method_idx); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 929 | CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 930 | MutexLock mu(compiled_methods_lock_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 931 | compiled_methods_[ref] = compiled_method; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 932 | DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 933 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 934 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 935 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 936 | bool is_static = (access_flags & kAccStatic) != 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 937 | const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty); |
| 938 | if (compiled_invoke_stub == NULL) { |
| 939 | if (instruction_set_ == kX86) { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 940 | compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 941 | } else { |
| 942 | CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2); |
| 943 | // Generates invocation stub using ARM instruction set |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 944 | compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 945 | } |
| 946 | CHECK(compiled_invoke_stub != NULL); |
| 947 | InsertInvokeStub(is_static, shorty, compiled_invoke_stub); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 948 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 949 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 952 | static std::string MakeInvokeStubKey(bool is_static, const char* shorty) { |
| 953 | std::string key(shorty); |
| 954 | if (is_static) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 955 | key += "$"; // Must not be a shorty type character. |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 956 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 957 | return key; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 960 | const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 961 | MutexLock mu(compiled_invoke_stubs_lock_); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 962 | const std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 963 | InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 964 | if (it == compiled_invoke_stubs_.end()) { |
| 965 | return NULL; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 966 | } else { |
| 967 | DCHECK(it->second != NULL); |
| 968 | return it->second; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | void Compiler::InsertInvokeStub(bool is_static, const char* shorty, |
| 973 | const CompiledInvokeStub* compiled_invoke_stub) { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 974 | MutexLock mu(compiled_invoke_stubs_lock_); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 975 | std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 976 | compiled_invoke_stubs_[key] = compiled_invoke_stub; |
| 977 | } |
| 978 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 979 | CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 980 | MutexLock mu(compiled_classes_lock_); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 981 | ClassTable::const_iterator it = compiled_classes_.find(ref); |
| 982 | if (it == compiled_classes_.end()) { |
| 983 | return NULL; |
| 984 | } |
| 985 | CHECK(it->second != NULL); |
| 986 | return it->second; |
| 987 | } |
| 988 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 989 | CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 990 | MutexLock mu(compiled_methods_lock_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 991 | MethodTable::const_iterator it = compiled_methods_.find(ref); |
| 992 | if (it == compiled_methods_.end()) { |
| 993 | return NULL; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 994 | } |
| 995 | CHECK(it->second != NULL); |
| 996 | return it->second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 999 | void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) { |
| 1000 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 1001 | const DexFile* dex_file = dex_files[i]; |
| 1002 | CHECK(dex_file != NULL); |
| 1003 | SetGcMapsDexFile(class_loader, *dex_file); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 1008 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1009 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 1010 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 1011 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 1012 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 1013 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
| 1014 | if (klass == NULL || !klass->IsVerified()) { |
| 1015 | Thread::Current()->ClearException(); |
| 1016 | continue; |
| 1017 | } |
| 1018 | const byte* class_data = dex_file.GetClassData(class_def); |
| 1019 | if (class_data == NULL) { |
| 1020 | // empty class such as a marker interface |
| 1021 | continue; |
| 1022 | } |
| 1023 | ClassDataItemIterator it(dex_file, class_data); |
| 1024 | while (it.HasNextStaticField()) { |
| 1025 | it.Next(); |
| 1026 | } |
| 1027 | while (it.HasNextInstanceField()) { |
| 1028 | it.Next(); |
| 1029 | } |
| 1030 | while (it.HasNextDirectMethod()) { |
| 1031 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 1032 | class_loader, true); |
| 1033 | SetGcMapsMethod(dex_file, method); |
| 1034 | it.Next(); |
| 1035 | } |
| 1036 | while (it.HasNextVirtualMethod()) { |
| 1037 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 1038 | class_loader, false); |
| 1039 | SetGcMapsMethod(dex_file, method); |
| 1040 | it.Next(); |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 1045 | namespace verifier { |
| 1046 | class DexVerifier { |
| 1047 | public: |
| 1048 | static const std::vector<uint8_t>* GetGcMap(Compiler::MethodReference ref); |
| 1049 | }; |
| 1050 | } |
| 1051 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1052 | void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) { |
| 1053 | if (method == NULL) { |
| 1054 | Thread::Current()->ClearException(); |
| 1055 | return; |
| 1056 | } |
| 1057 | uint16_t method_idx = method->GetDexMethodIndex(); |
| 1058 | MethodReference ref(&dex_file, method_idx); |
| 1059 | CompiledMethod* compiled_method = GetCompiledMethod(ref); |
| 1060 | if (compiled_method == NULL) { |
| 1061 | return; |
| 1062 | } |
| 1063 | const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref); |
| 1064 | if (gc_map == NULL) { |
| 1065 | return; |
| 1066 | } |
| 1067 | compiled_method->SetGcMap(*gc_map); |
| 1068 | } |
| 1069 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1070 | void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) { |
| 1071 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 1072 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1073 | CHECK(dex_file != NULL); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 1074 | SetCodeAndDirectMethodsDexFile(*dex_file); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 1078 | void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 1079 | Runtime* runtime = Runtime::Current(); |
| 1080 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1081 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 1082 | CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods(); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1083 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1084 | Method* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1085 | if (method == NULL || method->IsDirect()) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 1086 | Runtime::TrampolineType type = Runtime::GetTrampolineType(method); |
| 1087 | ByteArray* res_trampoline = runtime->GetResolutionStubArray(type); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1088 | code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 1089 | } else { |
| 1090 | // TODO: we currently leave the entry blank for resolved |
| 1091 | // non-direct methods. we could put in an error stub. |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1096 | } // namespace art |