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