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" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 27 | #include "dex_cache.h" |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 28 | #include "dex_verifier.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 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 57 | Compiler::Compiler(InstructionSet instruction_set, |
| 58 | bool image, |
| 59 | const std::set<std::string>* image_classes) |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 60 | : instruction_set_(instruction_set), |
| 61 | jni_compiler_(instruction_set), |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 62 | compiled_classes_lock_("compiled classes lock"), |
| 63 | compiled_methods_lock_("compiled method lock"), |
| 64 | compiled_invoke_stubs_lock_("compiled invoke stubs lock"), |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 65 | image_(image), |
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 | } |
| 200 | Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx); |
| 201 | if (resolved_class == NULL) { |
| 202 | return false; |
| 203 | } |
| 204 | return IsImageClass(ClassHelper(resolved_class).GetDescriptor()); |
| 205 | } |
| 206 | |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 207 | // Return true if the class should be skipped during compilation. We |
| 208 | // never skip classes in the boot class loader. However, if we have a |
| 209 | // non-boot class loader and we can resolve the class in the boot |
| 210 | // class loader, we do skip the class. This happens if an app bundles |
| 211 | // classes found in the boot classpath. Since at runtime we will |
| 212 | // select the class from the boot classpath, do not attempt to resolve |
| 213 | // or compile it now. |
| 214 | static bool SkipClass(const ClassLoader* class_loader, |
| 215 | const DexFile& dex_file, |
| 216 | const DexFile::ClassDef& class_def) { |
| 217 | if (class_loader == NULL) { |
| 218 | return false; |
| 219 | } |
| 220 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 221 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 222 | Class* klass = class_linker->FindClass(descriptor, NULL); |
| 223 | if (klass == NULL) { |
| 224 | Thread* self = Thread::Current(); |
| 225 | CHECK(self->IsExceptionPending()); |
| 226 | self->ClearException(); |
| 227 | return false; |
| 228 | } |
| 229 | return true; |
| 230 | } |
| 231 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 232 | struct Context { |
| 233 | ClassLinker* class_linker; |
| 234 | const ClassLoader* class_loader; |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 235 | Compiler* compiler; |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 236 | DexCache* dex_cache; |
| 237 | const DexFile* dex_file; |
| 238 | }; |
| 239 | |
| 240 | typedef void Callback(Context* context, size_t index); |
| 241 | |
| 242 | class WorkerThread { |
| 243 | public: |
| 244 | WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe) |
| 245 | : context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) { |
| 246 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Init, this), "compiler worker thread"); |
| 247 | } |
| 248 | |
| 249 | ~WorkerThread() { |
| 250 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown"); |
| 251 | } |
| 252 | |
| 253 | private: |
| 254 | static void* Init(void* arg) { |
| 255 | WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg); |
| 256 | Runtime* runtime = Runtime::Current(); |
| 257 | runtime->AttachCurrentThread("Compiler Worker", true); |
| 258 | Thread::Current()->SetState(Thread::kRunnable); |
| 259 | worker->Run(); |
| 260 | Thread::Current()->SetState(Thread::kNative); |
| 261 | runtime->DetachCurrentThread(); |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | void Run() { |
| 266 | for (size_t i = begin_; i < end_; i += stripe_) { |
| 267 | callback_(context_, i); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | pthread_t pthread_; |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 272 | |
| 273 | Context* context_; |
| 274 | size_t begin_; |
| 275 | size_t end_; |
| 276 | Callback* callback_; |
| 277 | size_t stripe_; |
| 278 | }; |
| 279 | |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 280 | void ForAll(Context* context, size_t begin, size_t end, Callback callback) { |
| 281 | std::vector<WorkerThread*> threads; |
| 282 | |
| 283 | const size_t thread_count = static_cast<size_t>(sysconf(_SC_NPROCESSORS_ONLN)); |
| 284 | for (size_t i = 0; i < thread_count; ++i) { |
| 285 | threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count)); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 288 | // Switch to kVmWait while we're blocked waiting for the other threads to finish. |
| 289 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait); |
| 290 | STLDeleteElements(&threads); |
| 291 | } |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 292 | |
| 293 | static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) { |
| 294 | const DexFile& dex_file = *context->dex_file; |
| 295 | |
| 296 | // Method and Field are the worst. We can't resolve without either |
| 297 | // context from the code use (to disambiguate virtual vs direct |
| 298 | // method and instance vs static field) or from class |
| 299 | // definitions. While the compiler will resolve what it can as it |
| 300 | // needs it, here we try to resolve fields and methods used in class |
| 301 | // definitions, since many of them many never be referenced by |
| 302 | // generated code. |
| 303 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 304 | if (SkipClass(context->class_loader, dex_file, class_def)) { |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | // Note the class_data pointer advances through the headers, |
| 309 | // static fields, instance fields, direct methods, and virtual |
| 310 | // methods. |
| 311 | const byte* class_data = dex_file.GetClassData(class_def); |
| 312 | if (class_data == NULL) { |
| 313 | // empty class such as a marker interface |
| 314 | return; |
| 315 | } |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 316 | Thread* self = Thread::Current(); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 317 | ClassLinker* class_linker = context->class_linker; |
| 318 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 319 | ClassDataItemIterator it(dex_file, class_data); |
| 320 | while (it.HasNextStaticField()) { |
| 321 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 322 | context->class_loader, true); |
| 323 | if (field == NULL) { |
| 324 | CHECK(self->IsExceptionPending()); |
| 325 | self->ClearException(); |
| 326 | } |
| 327 | it.Next(); |
| 328 | } |
| 329 | while (it.HasNextInstanceField()) { |
| 330 | Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache, |
| 331 | context->class_loader, false); |
| 332 | if (field == NULL) { |
| 333 | CHECK(self->IsExceptionPending()); |
| 334 | self->ClearException(); |
| 335 | } |
| 336 | it.Next(); |
| 337 | } |
| 338 | while (it.HasNextDirectMethod()) { |
| 339 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 340 | context->class_loader, true); |
| 341 | if (method == NULL) { |
| 342 | CHECK(self->IsExceptionPending()); |
| 343 | self->ClearException(); |
| 344 | } |
| 345 | it.Next(); |
| 346 | } |
| 347 | while (it.HasNextVirtualMethod()) { |
| 348 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 349 | context->class_loader, false); |
| 350 | if (method == NULL) { |
| 351 | CHECK(self->IsExceptionPending()); |
| 352 | self->ClearException(); |
| 353 | } |
| 354 | it.Next(); |
| 355 | } |
| 356 | DCHECK(!it.HasNext()); |
| 357 | } |
| 358 | |
| 359 | static void ResolveType(Context* context, size_t type_idx) { |
| 360 | // Class derived values are more complicated, they require the linker and loader. |
| 361 | Thread* self = Thread::Current(); |
| 362 | ClassLinker* class_linker = context->class_linker; |
| 363 | const DexFile& dex_file = *context->dex_file; |
| 364 | Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader); |
| 365 | if (klass == NULL) { |
| 366 | CHECK(self->IsExceptionPending()); |
| 367 | Thread::Current()->ClearException(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | 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] | 372 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 373 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 374 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 375 | // Strings are easy in that they always are simply resolved to literals in the same file |
| 376 | if (image_ && image_classes_ == NULL) { |
| 377 | // TODO: Add support for loading strings referenced by image_classes_ |
| 378 | // See also Compiler::CanAssumeTypeIsPresentInDexCache. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 379 | for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) { |
| 380 | class_linker->ResolveString(dex_file, string_idx, dex_cache); |
| 381 | } |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 382 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings"); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 385 | Context context; |
| 386 | context.class_linker = class_linker; |
| 387 | context.class_loader = class_loader; |
| 388 | context.dex_cache = dex_cache; |
| 389 | context.dex_file = &dex_file; |
| 390 | |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 391 | ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 392 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types"); |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 393 | |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 394 | ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 395 | timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields"); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 398 | void Compiler::Verify(const ClassLoader* class_loader, |
| 399 | const std::vector<const DexFile*>& dex_files) { |
| 400 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 401 | const DexFile* dex_file = dex_files[i]; |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 402 | CHECK(dex_file != NULL); |
| 403 | VerifyDexFile(class_loader, *dex_file); |
| 404 | } |
| 405 | } |
| 406 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 407 | static void VerifyClass(Context* context, size_t class_def_index) { |
| 408 | const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index); |
| 409 | const char* descriptor = context->dex_file->GetClassDescriptor(class_def); |
| 410 | Class* klass = context->class_linker->FindClass(descriptor, context->class_loader); |
| 411 | if (klass == NULL) { |
| 412 | Thread* self = Thread::Current(); |
| 413 | CHECK(self->IsExceptionPending()); |
| 414 | self->ClearException(); |
| 415 | return; |
| 416 | } |
| 417 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
| 418 | context->class_linker->VerifyClass(klass); |
| 419 | |
| 420 | if (klass->IsErroneous()) { |
| 421 | // ClassLinker::VerifyClass throws, which isn't useful in the compiler. |
| 422 | CHECK(Thread::Current()->IsExceptionPending()); |
| 423 | Thread::Current()->ClearException(); |
| 424 | // We want to try verification again at run-time, so move back into the resolved state. |
| 425 | klass->SetStatus(Class::kStatusResolved); |
| 426 | } |
| 427 | |
| 428 | CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass); |
| 429 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException()); |
| 430 | } |
| 431 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 432 | void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 433 | dex_file.ChangePermissions(PROT_READ | PROT_WRITE); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 434 | |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 435 | Context context; |
| 436 | context.class_linker = Runtime::Current()->GetClassLinker(); |
| 437 | context.class_loader = class_loader; |
| 438 | context.dex_file = &dex_file; |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 439 | ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 440 | |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 441 | dex_file.ChangePermissions(PROT_READ); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 444 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, |
| 445 | const std::vector<const DexFile*>& dex_files) { |
| 446 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 447 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 448 | CHECK(dex_file != NULL); |
| 449 | InitializeClassesWithoutClinit(class_loader, *dex_file); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 454 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 455 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 456 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 457 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 458 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 459 | if (klass != NULL) { |
| 460 | class_linker->EnsureInitialized(klass, false); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 461 | // record the final class status if necessary |
| 462 | Class::Status status = klass->GetStatus(); |
| 463 | ClassReference ref(&dex_file, class_def_index); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 464 | MutexLock mu(compiled_classes_lock_); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 465 | CompiledClass* compiled_class = GetCompiledClass(ref); |
| 466 | if (compiled_class == NULL) { |
| 467 | compiled_class = new CompiledClass(status); |
| 468 | compiled_classes_[ref] = compiled_class; |
| 469 | } else { |
| 470 | DCHECK_EQ(status, compiled_class->GetStatus()); |
| 471 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 472 | } |
| 473 | // clear any class not found or verification exceptions |
| 474 | Thread::Current()->ClearException(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 478 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 479 | 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] | 480 | if (klass == NULL) { |
| 481 | Thread::Current()->ClearException(); |
| 482 | } else if (klass->IsInitialized()) { |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 483 | dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass); |
| 484 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 488 | void Compiler::Compile(const ClassLoader* class_loader, |
| 489 | const std::vector<const DexFile*>& dex_files) { |
| 490 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 491 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 492 | CHECK(dex_file != NULL); |
| 493 | CompileDexFile(class_loader, *dex_file); |
| 494 | } |
| 495 | } |
| 496 | |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 497 | void Compiler::CompileClass(Context* context, size_t class_def_index) { |
| 498 | const ClassLoader* class_loader = context->class_loader; |
| 499 | const DexFile& dex_file = *context->dex_file; |
| 500 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | 5ead095 | 2011-11-28 22:55:52 -0800 | [diff] [blame] | 501 | if (SkipClass(class_loader, dex_file, class_def)) { |
| 502 | return; |
| 503 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 504 | const byte* class_data = dex_file.GetClassData(class_def); |
| 505 | if (class_data == NULL) { |
| 506 | // empty class, probably a marker interface |
| 507 | return; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 508 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 509 | ClassDataItemIterator it(dex_file, class_data); |
| 510 | // Skip fields |
| 511 | while (it.HasNextStaticField()) { |
| 512 | it.Next(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 513 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 514 | while (it.HasNextInstanceField()) { |
| 515 | it.Next(); |
| 516 | } |
| 517 | // Compile direct methods |
| 518 | while (it.HasNextDirectMethod()) { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 519 | context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), |
| 520 | it.GetMemberIndex(), class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 521 | it.Next(); |
| 522 | } |
| 523 | // Compile virtual methods |
| 524 | while (it.HasNextVirtualMethod()) { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 525 | context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), |
| 526 | it.GetMemberIndex(), class_loader, dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 527 | it.Next(); |
| 528 | } |
| 529 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 532 | void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 533 | Context context; |
| 534 | context.class_loader = class_loader; |
| 535 | context.compiler = this; |
| 536 | context.dex_file = &dex_file; |
Elliott Hughes | 81d9151 | 2012-02-03 16:38:43 -0800 | [diff] [blame] | 537 | ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 540 | void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
| 541 | uint32_t method_idx, const ClassLoader* class_loader, |
| 542 | const DexFile& dex_file) { |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 543 | CompiledMethod* compiled_method = NULL; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 544 | uint64_t start_ns = NanoTime(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 545 | if ((access_flags & kAccNative) != 0) { |
| 546 | 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] | 547 | CHECK(compiled_method != NULL); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 548 | } else if ((access_flags & kAccAbstract) != 0) { |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 549 | } else { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 550 | compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader, |
| 551 | dex_file, kThumb2); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 552 | CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file); |
| 553 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 554 | uint64_t duration_ns = NanoTime() - start_ns; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 555 | if (duration_ns > MsToNs(100)) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 556 | LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file) |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 557 | << " took " << PrettyDuration(duration_ns); |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | if (compiled_method != NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 561 | MethodReference ref(&dex_file, method_idx); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 562 | CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file); |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 563 | MutexLock mu(compiled_methods_lock_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 564 | compiled_methods_[ref] = compiled_method; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 565 | DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 566 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 567 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 568 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 569 | bool is_static = (access_flags & kAccStatic) != 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 570 | const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty); |
| 571 | if (compiled_invoke_stub == NULL) { |
| 572 | if (instruction_set_ == kX86) { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 573 | compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 574 | } else { |
| 575 | CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2); |
| 576 | // Generates invocation stub using ARM instruction set |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 577 | compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 578 | } |
| 579 | CHECK(compiled_invoke_stub != NULL); |
| 580 | InsertInvokeStub(is_static, shorty, compiled_invoke_stub); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 581 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 582 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 585 | static std::string MakeInvokeStubKey(bool is_static, const char* shorty) { |
| 586 | std::string key(shorty); |
| 587 | if (is_static) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 588 | key += "$"; // Must not be a shorty type character. |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 589 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 590 | return key; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 593 | const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 594 | MutexLock mu(compiled_invoke_stubs_lock_); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 595 | const std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 596 | InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 597 | if (it == compiled_invoke_stubs_.end()) { |
| 598 | return NULL; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 599 | } else { |
| 600 | DCHECK(it->second != NULL); |
| 601 | return it->second; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | void Compiler::InsertInvokeStub(bool is_static, const char* shorty, |
| 606 | const CompiledInvokeStub* compiled_invoke_stub) { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 607 | MutexLock mu(compiled_invoke_stubs_lock_); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 608 | std::string key(MakeInvokeStubKey(is_static, shorty)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 609 | compiled_invoke_stubs_[key] = compiled_invoke_stub; |
| 610 | } |
| 611 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 612 | CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 613 | MutexLock mu(compiled_classes_lock_); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 614 | ClassTable::const_iterator it = compiled_classes_.find(ref); |
| 615 | if (it == compiled_classes_.end()) { |
| 616 | return NULL; |
| 617 | } |
| 618 | CHECK(it->second != NULL); |
| 619 | return it->second; |
| 620 | } |
| 621 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 622 | CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const { |
Elliott Hughes | c225caa | 2012-02-03 15:43:37 -0800 | [diff] [blame] | 623 | MutexLock mu(compiled_methods_lock_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 624 | MethodTable::const_iterator it = compiled_methods_.find(ref); |
| 625 | if (it == compiled_methods_.end()) { |
| 626 | return NULL; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 627 | } |
| 628 | CHECK(it->second != NULL); |
| 629 | return it->second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 632 | void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) { |
| 633 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 634 | const DexFile* dex_file = dex_files[i]; |
| 635 | CHECK(dex_file != NULL); |
| 636 | SetGcMapsDexFile(class_loader, *dex_file); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 641 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 642 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 643 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 644 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 645 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 646 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
| 647 | if (klass == NULL || !klass->IsVerified()) { |
| 648 | Thread::Current()->ClearException(); |
| 649 | continue; |
| 650 | } |
| 651 | const byte* class_data = dex_file.GetClassData(class_def); |
| 652 | if (class_data == NULL) { |
| 653 | // empty class such as a marker interface |
| 654 | continue; |
| 655 | } |
| 656 | ClassDataItemIterator it(dex_file, class_data); |
| 657 | while (it.HasNextStaticField()) { |
| 658 | it.Next(); |
| 659 | } |
| 660 | while (it.HasNextInstanceField()) { |
| 661 | it.Next(); |
| 662 | } |
| 663 | while (it.HasNextDirectMethod()) { |
| 664 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 665 | class_loader, true); |
| 666 | SetGcMapsMethod(dex_file, method); |
| 667 | it.Next(); |
| 668 | } |
| 669 | while (it.HasNextVirtualMethod()) { |
| 670 | Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache, |
| 671 | class_loader, false); |
| 672 | SetGcMapsMethod(dex_file, method); |
| 673 | it.Next(); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) { |
| 679 | if (method == NULL) { |
| 680 | Thread::Current()->ClearException(); |
| 681 | return; |
| 682 | } |
| 683 | uint16_t method_idx = method->GetDexMethodIndex(); |
| 684 | MethodReference ref(&dex_file, method_idx); |
| 685 | CompiledMethod* compiled_method = GetCompiledMethod(ref); |
| 686 | if (compiled_method == NULL) { |
| 687 | return; |
| 688 | } |
| 689 | const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref); |
| 690 | if (gc_map == NULL) { |
| 691 | return; |
| 692 | } |
| 693 | compiled_method->SetGcMap(*gc_map); |
| 694 | } |
| 695 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 696 | void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) { |
| 697 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 698 | const DexFile* dex_file = dex_files[i]; |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 699 | CHECK(dex_file != NULL); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 700 | SetCodeAndDirectMethodsDexFile(*dex_file); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 704 | void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 705 | Runtime* runtime = Runtime::Current(); |
| 706 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 707 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 708 | CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods(); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 709 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 710 | Method* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 711 | if (method == NULL || method->IsDirect()) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 712 | Runtime::TrampolineType type = Runtime::GetTrampolineType(method); |
| 713 | ByteArray* res_trampoline = runtime->GetResolutionStubArray(type); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 714 | code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 715 | } else { |
| 716 | // TODO: we currently leave the entry blank for resolved |
| 717 | // non-direct methods. we could put in an error stub. |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 722 | } // namespace art |