blob: 08c6cc437fc0d0e0082f66104a6f2db01248a0ba [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrom9ea1cb12011-08-24 23:18:18 -070016
17#include "compiler.h"
18
Elliott Hughesd9c67be2012-02-02 19:54:06 -080019#include <vector>
20
Brian Carlstrom27ec9612011-09-19 20:20:38 -070021#include <sys/mman.h>
Elliott Hughesd9c67be2012-02-02 19:54:06 -080022#include <unistd.h>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070023
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070024#include "assembler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Ian Rogers1bddec32012-02-04 12:27:34 -080027#include "compiler/CompilerIR.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070028#include "dex_cache.h"
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070029#include "jni_compiler.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070030#include "jni_internal.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070034#include "stl_util.h"
Elliott Hughes601a1232012-02-02 17:47:38 -080035#include "timing_logger.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070036
Shih-wei Liaod1fec812012-02-13 09:51:10 -080037#if defined(ART_USE_LLVM_COMPILER)
38#include "compiler_llvm/compiler_llvm.h"
39#endif
40
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070041namespace art {
42
Shih-wei Liaod1fec812012-02-13 09:51:10 -080043#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers996cc582012-02-14 22:23:29 -080044CompiledMethod* oatCompileMethod(Compiler& compiler, const DexFile::CodeItem* code_item,
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080045 uint32_t access_flags, uint32_t method_idx,
46 const ClassLoader* class_loader,
47 const DexFile& dex_file, InstructionSet);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080048#endif
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080049
Shih-wei Liaoc486c112011-09-13 16:43:52 -070050namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070051 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070052 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070053 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080054 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070055}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070056namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070057 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070058 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070059 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080060 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070061}
62
Ian Rogers996cc582012-02-14 22:23:29 -080063static double Percentage(size_t x, size_t y) {
64 return 100.0 * ((double)x) / ((double)(x + y));
65}
66
67static void DumpStat(size_t x, size_t y, const char* str) {
68 if (x == 0 && y == 0) {
69 return;
70 }
71 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
72}
73
Ian Rogersc8b306f2012-02-17 21:34:44 -080074class AOTCompilationStats {
75 public:
76 AOTCompilationStats() : stats_lock_("AOT compilation statistics lock"),
77 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
78 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
79 resolved_types_(0), unresolved_types_(0),
80 resolved_instance_fields_(0), unresolved_instance_fields_(0),
81 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0) {
82 for (size_t i = 0; i < kMaxInvokeType; i++) {
83 resolved_methods_[i] = 0;
84 unresolved_methods_[i] = 0;
85 }
86 }
87
88 void Dump() {
89 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
90 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
91 DumpStat(resolved_types_, unresolved_types_, "types resolved");
92 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
93 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
94 "static fields resolved");
95 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
96 "static fields local to a class");
97
98 for (size_t i = 0; i < kMaxInvokeType; i++) {
99 std::ostringstream oss;
100 oss << "resolved " << static_cast<InvokeType>(i) << " methods";
101 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
102 }
103 }
Ian Rogers996cc582012-02-14 22:23:29 -0800104
105// Allow lossy statistics in non-debug builds
106#ifndef NDEBUG
107#define STATS_LOCK() MutexLock mu(stats_lock_)
108#else
109#define STATS_LOCK()
110#endif
111
Ian Rogersc8b306f2012-02-17 21:34:44 -0800112 void TypeInDexCache() {
113 STATS_LOCK();
114 types_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800115 }
Ian Rogers996cc582012-02-14 22:23:29 -0800116
Ian Rogersc8b306f2012-02-17 21:34:44 -0800117 void TypeNotInDexCache() {
118 STATS_LOCK();
119 types_not_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800120 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800121
122 void StringInDexCache() {
123 STATS_LOCK();
124 strings_in_dex_cache_++;
125 }
126
127 void StringNotInDexCache() {
128 STATS_LOCK();
129 strings_not_in_dex_cache_++;
130 }
131
132 void TypeDoesntNeedAccessCheck() {
133 STATS_LOCK();
134 resolved_types_++;
135 }
136
137 void TypeNeedsAccessCheck() {
138 STATS_LOCK();
139 unresolved_types_++;
140 }
141
142 void ResolvedInstanceField() {
143 STATS_LOCK();
144 resolved_instance_fields_++;
145 }
146
147 void UnresolvedInstanceField(){
148 STATS_LOCK();
149 unresolved_instance_fields_++;
150 }
151
152 void ResolvedLocalStaticField() {
153 STATS_LOCK();
154 resolved_local_static_fields_++;
155 }
156
157 void ResolvedStaticField() {
158 STATS_LOCK();
159 resolved_static_fields_++;
160 }
161
162 void UnresolvedStaticField() {
163 STATS_LOCK();
164 unresolved_static_fields_++;
165 }
166
167 void ResolvedMethod(InvokeType type) {
168 DCHECK_LE(type, kMaxInvokeType);
169 STATS_LOCK();
170 resolved_methods_[type]++;
171 }
172
173 void UnresolvedMethod(InvokeType type) {
174 DCHECK_LE(type, kMaxInvokeType);
175 STATS_LOCK();
176 unresolved_methods_[type]++;
177 }
178
179 private:
180 Mutex stats_lock_;
181
182 size_t types_in_dex_cache_;
183 size_t types_not_in_dex_cache_;
184
185 size_t strings_in_dex_cache_;
186 size_t strings_not_in_dex_cache_;
187
188 size_t resolved_types_;
189 size_t unresolved_types_;
190
191 size_t resolved_instance_fields_;
192 size_t unresolved_instance_fields_;
193
194 size_t resolved_local_static_fields_;
195 size_t resolved_static_fields_;
196 size_t unresolved_static_fields_;
197
198 size_t resolved_methods_[kMaxInvokeType + 1];
199 size_t unresolved_methods_[kMaxInvokeType + 1];
200
201 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);;
202};
Ian Rogers996cc582012-02-14 22:23:29 -0800203
Elliott Hughes5523ee02012-02-03 18:18:34 -0800204Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count,
Brian Carlstromae826982011-11-09 01:33:42 -0800205 const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -0700206 : instruction_set_(instruction_set),
207 jni_compiler_(instruction_set),
Elliott Hughesc225caa2012-02-03 15:43:37 -0800208 compiled_classes_lock_("compiled classes lock"),
209 compiled_methods_lock_("compiled method lock"),
210 compiled_invoke_stubs_lock_("compiled invoke stubs lock"),
Brian Carlstromaded5f72011-10-07 17:15:04 -0700211 image_(image),
Elliott Hughes5523ee02012-02-03 18:18:34 -0800212 thread_count_(thread_count),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800213 stats_(new AOTCompilationStats),
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800214 image_classes_(image_classes)
215#if defined(ART_USE_LLVM_COMPILER)
216 ,
217 compiler_llvm_(new compiler_llvm::CompilerLLVM(this, instruction_set))
218#endif
219 {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700220 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800221 if (!image_) {
222 CHECK(image_classes_ == NULL);
223 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700224}
225
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700226Compiler::~Compiler() {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800227 {
228 MutexLock mu(compiled_classes_lock_);
229 STLDeleteValues(&compiled_classes_);
230 }
231 {
232 MutexLock mu(compiled_methods_lock_);
233 STLDeleteValues(&compiled_methods_);
234 }
235 {
236 MutexLock mu(compiled_invoke_stubs_lock_);
237 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800238 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700239}
240
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700241ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
242 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700243 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700244 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700245 } else {
246 CHECK(instruction_set == kArm || instruction_set == kThumb2);
247 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700248 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700249 }
250}
251
Elliott Hughes8add92d2012-01-18 18:18:43 -0800252ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800253 switch (instruction_set) {
254 case kArm:
255 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800256 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800257 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800258 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800259 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800260 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -0800261 return NULL;
262 }
263}
264
Ian Rogersad25ac52011-10-04 19:13:33 -0700265ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
266 if (instruction_set == kX86) {
267 return x86::CreateAbstractMethodErrorStub();
268 } else {
269 CHECK(instruction_set == kArm || instruction_set == kThumb2);
270 // Generates resolution stub using ARM instruction set
271 return arm::CreateAbstractMethodErrorStub();
272 }
273}
274
Jesse Wilson254db0f2011-11-16 16:44:11 -0500275void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800276 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700277 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800278
Elliott Hughes601a1232012-02-02 17:47:38 -0800279 TimingLogger timings("compiler");
280
281 PreCompile(class_loader, dex_files, timings);
282
Brian Carlstromae826982011-11-09 01:33:42 -0800283 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800284 timings.AddSplit("Compile");
285
Brian Carlstromae826982011-11-09 01:33:42 -0800286 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800287 timings.AddSplit("PostCompile");
288
289 if (timings.GetTotalNs() > MsToNs(1000)) {
290 timings.Dump();
291 }
Ian Rogers996cc582012-02-14 22:23:29 -0800292
Ian Rogersc8b306f2012-02-17 21:34:44 -0800293 stats_->Dump();
Brian Carlstrom8a487412011-08-29 20:08:52 -0700294}
295
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700296void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700297 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800298
Brian Carlstrom8a487412011-08-29 20:08:52 -0700299 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800300
Ian Rogers0571d352011-11-03 19:51:38 -0700301 // Find the dex_file
302 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
303 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800304 std::vector<const DexFile*> dex_files;
305 dex_files.push_back(&dex_file);
306
Elliott Hughes601a1232012-02-02 17:47:38 -0800307 TimingLogger timings("CompileOne");
308 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800309
Ian Rogers0571d352011-11-03 19:51:38 -0700310 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800311 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
312 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800313
314 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700315}
316
Brian Carlstromae826982011-11-09 01:33:42 -0800317void Compiler::Resolve(const ClassLoader* class_loader,
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800318 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800319 for (size_t i = 0; i != dex_files.size(); ++i) {
320 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700321 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800322 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700323 }
324}
325
Brian Carlstromae826982011-11-09 01:33:42 -0800326void Compiler::PreCompile(const ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800327 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800328 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800329
Brian Carlstromae826982011-11-09 01:33:42 -0800330 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800331 timings.AddSplit("PreCompile.Verify");
332
Brian Carlstromae826982011-11-09 01:33:42 -0800333 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800334 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800335}
336
337void Compiler::PostCompile(const ClassLoader* class_loader,
338 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800339 SetGcMaps(class_loader, dex_files);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800340#if defined(ART_USE_LLVM_COMPILER)
341 compiler_llvm_->MaterializeLLVMModule();
342#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800343 SetCodeAndDirectMethods(dex_files);
344}
345
346bool Compiler::IsImageClass(const std::string& descriptor) const {
347 if (image_classes_ == NULL) {
348 return true;
349 }
350 return image_classes_->find(descriptor) != image_classes_->end();
351}
352
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800353bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800354 uint32_t type_idx) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800355 if (!IsImage()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800356 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800357 return false;
358 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800359 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800360 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800361 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800362 return false;
363 }
Ian Rogers996cc582012-02-14 22:23:29 -0800364 bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor());
365 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800366 stats_->TypeInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800367 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800368 stats_->TypeNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800369 }
370 return result;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800371}
372
Ian Rogers1bddec32012-02-04 12:27:34 -0800373bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800374 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800375 // TODO: Add support for loading strings referenced by image_classes_
376 // See also Compiler::ResolveDexFile
377
378 // The following is a test saying that if we're building the image without a restricted set of
379 // image classes then we can assume the string is present in the dex cache if it is there now
Ian Rogers996cc582012-02-14 22:23:29 -0800380 bool result = IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
381 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800382 stats_->StringInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800383 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800384 stats_->StringNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800385 }
386 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800387}
388
389bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800390 const DexFile& dex_file, uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800391 // Get type from dex cache assuming it was populated by the verifier
392 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
393 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800394 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800395 return false; // Unknown class needs access checks.
396 }
397 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
398 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
399 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800400 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800401 return false; // Incomplete referrer knowledge needs access check.
402 }
403 // Perform access check, will return true if access is ok or false if we're going to have to
404 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800405 bool result = referrer_class->CanAccess(resolved_class);
406 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800407 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800408 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800409 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800410 }
411 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800412}
413
414bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
415 const DexCache* dex_cache,
416 const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800417 uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800418 // Get type from dex cache assuming it was populated by the verifier.
419 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
420 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800421 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800422 return false; // Unknown class needs access checks.
423 }
424 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
425 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
426 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800427 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800428 return false; // Incomplete referrer knowledge needs access check.
429 }
430 // Perform access and instantiable checks, will return true if access is ok or false if we're
431 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800432 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
433 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800434 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800435 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800436 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800437 }
438 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800439}
440
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800441static Class* ComputeReferrerClass(CompilationUnit* cUnit) {
442 const DexFile::MethodId& referrer_method_id = cUnit->dex_file->GetMethodId(cUnit->method_idx);
443 return cUnit->class_linker->ResolveType(*cUnit->dex_file, referrer_method_id.class_idx_,
444 cUnit->dex_cache, cUnit->class_loader);
445}
446
447static Field* ComputeReferrerField(CompilationUnit* cUnit, uint32_t field_idx) {
448 return cUnit->class_linker->ResolveField(*cUnit->dex_file, field_idx, cUnit->dex_cache,
449 cUnit->class_loader, false);
450
451}
452
453static Method* ComputeReferrerMethod(CompilationUnit* cUnit, uint32_t method_idx) {
454 return cUnit->class_linker->ResolveMethod(*cUnit->dex_file, method_idx, cUnit->dex_cache,
455 cUnit->class_loader, true);
456}
457
Ian Rogers1bddec32012-02-04 12:27:34 -0800458bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, CompilationUnit* cUnit,
Ian Rogers996cc582012-02-14 22:23:29 -0800459 int& field_offset, bool& is_volatile) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800460 // Conservative defaults
461 field_offset = -1;
462 is_volatile = true;
463 // Try to resolve field
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800464 Field* resolved_field = ComputeReferrerField(cUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800465 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800466 Class* referrer_class = ComputeReferrerClass(cUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800467 // Try to resolve referring class then access check, failure to pass the
468 Class* fields_class = resolved_field->GetDeclaringClass();
469 if (referrer_class != NULL &&
470 referrer_class->CanAccess(fields_class) &&
471 referrer_class->CanAccessMember(fields_class,
472 resolved_field->GetAccessFlags())) {
473 field_offset = resolved_field->GetOffset().Int32Value();
474 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800475 stats_->ResolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800476 return true; // Fast path.
477 }
478 }
479 // Clean up any exception left by field/type resolution
480 Thread* thread = Thread::Current();
481 if (thread->IsExceptionPending()) {
482 thread->ClearException();
483 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800484 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800485 return false; // Incomplete knowledge needs slow path.
486}
487
488bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, CompilationUnit* cUnit,
489 int& field_offset, int& ssb_index,
Ian Rogers996cc582012-02-14 22:23:29 -0800490 bool& is_referrers_class, bool& is_volatile) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800491 // Conservative defaults
492 field_offset = -1;
493 ssb_index = -1;
494 is_referrers_class = false;
495 is_volatile = true;
496 // Try to resolve field
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800497 Field* resolved_field = ComputeReferrerField(cUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800498 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800499 DCHECK(resolved_field->IsStatic());
500 Class* referrer_class = ComputeReferrerClass(cUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800501 if (referrer_class != NULL) {
502 if (resolved_field->GetDeclaringClass() == referrer_class) {
503 is_referrers_class = true; // implies no worrying about class initialization
504 field_offset = resolved_field->GetOffset().Int32Value();
505 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800506 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800507 return true; // fast path
508 } else {
509 Class* fields_class = resolved_field->GetDeclaringClass();
510 if (referrer_class->CanAccess(fields_class) &&
511 referrer_class->CanAccessMember(fields_class,
512 resolved_field->GetAccessFlags())) {
513 // We have the resolved field, we must make it into a ssbIndex for the referrer
514 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800515 // TODO: for images we can elide the static storage base null check
516 // if we know there's a non-null entry in the image
517 if (fields_class->GetDexCache() == cUnit->dex_cache) {
518 // common case where the dex cache of both the referrer and the field are the same,
519 // no need to search the dex file
520 ssb_index = fields_class->GetDexTypeIndex();
521 field_offset = resolved_field->GetOffset().Int32Value();
522 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800523 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800524 return true;
525 }
526 // Search dex file for localized ssb index
Ian Rogers1bddec32012-02-04 12:27:34 -0800527 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
528 const DexFile::StringId* string_id =
529 cUnit->dex_file->FindStringId(descriptor);
530 if (string_id != NULL) {
531 const DexFile::TypeId* type_id =
532 cUnit->dex_file->FindTypeId(cUnit->dex_file->GetIndexForStringId(*string_id));
533 if(type_id != NULL) {
534 // medium path, needs check of static storage base being initialized
Ian Rogers1bddec32012-02-04 12:27:34 -0800535 ssb_index = cUnit->dex_file->GetIndexForTypeId(*type_id);
536 field_offset = resolved_field->GetOffset().Int32Value();
537 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800538 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800539 return true;
540 }
541 }
542 }
543 }
544 }
545 }
546 // Clean up any exception left by field/type resolution
547 Thread* thread = Thread::Current();
548 if (thread->IsExceptionPending()) {
549 thread->ClearException();
550 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800551 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800552 return false; // Incomplete knowledge needs slow path.
553}
554
Ian Rogersc8b306f2012-02-17 21:34:44 -0800555bool Compiler::ComputeInvokeInfo(uint32_t method_idx, CompilationUnit* cUnit, InvokeType type,
Ian Rogers996cc582012-02-14 22:23:29 -0800556 int& vtable_idx) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800557 vtable_idx = -1;
558 Method* resolved_method = ComputeReferrerMethod(cUnit, method_idx);
559 if (resolved_method != NULL) {
560 Class* referrer_class = ComputeReferrerClass(cUnit);
561 if (referrer_class != NULL) {
562 Class* methods_class = resolved_method->GetDeclaringClass();
563 if (!referrer_class->CanAccess(methods_class) ||
564 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800565 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800566 // The referring class can't access the resolved method, this may occur as a result of a
567 // protected method being made public by implementing an interface that re-declares the
568 // method public. Resort to the dex file to determine the correct class for the access check
569 const DexFile& dex_file = cUnit->class_linker->FindDexFile(referrer_class->GetDexCache());
570 methods_class =
571 cUnit->class_linker->ResolveType(dex_file,
572 dex_file.GetMethodId(method_idx).class_idx_,
573 referrer_class);
574
575 }
576 if (referrer_class->CanAccess(methods_class) &&
577 referrer_class->CanAccessMember(methods_class,
578 resolved_method->GetAccessFlags())) {
579 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800580 if (type != kSuper) {
581 // nothing left to do for static/direct/virtual/interface dispatch
582 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800583 return true;
584 } else {
585 // ensure the vtable index will be correct to dispatch in the vtable of the super class
Ian Rogers996cc582012-02-14 22:23:29 -0800586 if (referrer_class->IsSubClass(methods_class) &&
587 vtable_idx < methods_class->GetVTable()->GetLength()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800588 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800589 return true;
590 }
591 }
592 }
593 }
594 }
595 // Clean up any exception left by method/type resolution
596 Thread* thread = Thread::Current();
597 if (thread->IsExceptionPending()) {
598 thread->ClearException();
599 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800600 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800601 return false; // Incomplete knowledge needs slow path.
602}
603
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800604// Return true if the class should be skipped during compilation. We
605// never skip classes in the boot class loader. However, if we have a
606// non-boot class loader and we can resolve the class in the boot
607// class loader, we do skip the class. This happens if an app bundles
608// classes found in the boot classpath. Since at runtime we will
609// select the class from the boot classpath, do not attempt to resolve
610// or compile it now.
611static bool SkipClass(const ClassLoader* class_loader,
612 const DexFile& dex_file,
613 const DexFile::ClassDef& class_def) {
614 if (class_loader == NULL) {
615 return false;
616 }
617 const char* descriptor = dex_file.GetClassDescriptor(class_def);
618 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
619 Class* klass = class_linker->FindClass(descriptor, NULL);
620 if (klass == NULL) {
621 Thread* self = Thread::Current();
622 CHECK(self->IsExceptionPending());
623 self->ClearException();
624 return false;
625 }
626 return true;
627}
628
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800629struct Context {
630 ClassLinker* class_linker;
631 const ClassLoader* class_loader;
Elliott Hughesc225caa2012-02-03 15:43:37 -0800632 Compiler* compiler;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800633 DexCache* dex_cache;
634 const DexFile* dex_file;
635};
636
637typedef void Callback(Context* context, size_t index);
638
639class WorkerThread {
640 public:
Elliott Hughes1e409252012-02-06 11:21:27 -0800641 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
642 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
643 if (spawn_) {
644 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread");
645 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800646 }
647
648 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800649 if (spawn_) {
650 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
651 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800652 }
653
654 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800655 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800656 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
657 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800658 if (worker->spawn_) {
659 runtime->AttachCurrentThread("Compiler Worker", true);
660 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800661 Thread::Current()->SetState(Thread::kRunnable);
662 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800663 if (worker->spawn_) {
664 Thread::Current()->SetState(Thread::kNative);
665 runtime->DetachCurrentThread();
666 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800667 return NULL;
668 }
669
Elliott Hughes1e409252012-02-06 11:21:27 -0800670 void Go() {
671 Go(this);
672 }
673
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800674 void Run() {
675 for (size_t i = begin_; i < end_; i += stripe_) {
676 callback_(context_, i);
677 }
678 }
679
680 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800681 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800682
683 Context* context_;
684 size_t begin_;
685 size_t end_;
686 Callback* callback_;
687 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800688
689 friend void ForAll(Context*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800690};
691
Elliott Hughes5523ee02012-02-03 18:18:34 -0800692void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800693 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -0800694
Elliott Hughes1e409252012-02-06 11:21:27 -0800695 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -0800696 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800697 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800698 }
Elliott Hughes1e409252012-02-06 11:21:27 -0800699 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800700
Elliott Hughes81d91512012-02-03 16:38:43 -0800701 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
702 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
703 STLDeleteElements(&threads);
704}
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800705
706static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
707 const DexFile& dex_file = *context->dex_file;
708
709 // Method and Field are the worst. We can't resolve without either
710 // context from the code use (to disambiguate virtual vs direct
711 // method and instance vs static field) or from class
712 // definitions. While the compiler will resolve what it can as it
713 // needs it, here we try to resolve fields and methods used in class
714 // definitions, since many of them many never be referenced by
715 // generated code.
716 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
717 if (SkipClass(context->class_loader, dex_file, class_def)) {
718 return;
719 }
720
721 // Note the class_data pointer advances through the headers,
722 // static fields, instance fields, direct methods, and virtual
723 // methods.
724 const byte* class_data = dex_file.GetClassData(class_def);
725 if (class_data == NULL) {
726 // empty class such as a marker interface
727 return;
728 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800729 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800730 ClassLinker* class_linker = context->class_linker;
731 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
732 ClassDataItemIterator it(dex_file, class_data);
733 while (it.HasNextStaticField()) {
734 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
735 context->class_loader, true);
736 if (field == NULL) {
737 CHECK(self->IsExceptionPending());
738 self->ClearException();
739 }
740 it.Next();
741 }
742 while (it.HasNextInstanceField()) {
743 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
744 context->class_loader, false);
745 if (field == NULL) {
746 CHECK(self->IsExceptionPending());
747 self->ClearException();
748 }
749 it.Next();
750 }
751 while (it.HasNextDirectMethod()) {
752 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
753 context->class_loader, true);
754 if (method == NULL) {
755 CHECK(self->IsExceptionPending());
756 self->ClearException();
757 }
758 it.Next();
759 }
760 while (it.HasNextVirtualMethod()) {
761 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
762 context->class_loader, false);
763 if (method == NULL) {
764 CHECK(self->IsExceptionPending());
765 self->ClearException();
766 }
767 it.Next();
768 }
769 DCHECK(!it.HasNext());
770}
771
772static void ResolveType(Context* context, size_t type_idx) {
773 // Class derived values are more complicated, they require the linker and loader.
774 Thread* self = Thread::Current();
775 ClassLinker* class_linker = context->class_linker;
776 const DexFile& dex_file = *context->dex_file;
777 Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader);
778 if (klass == NULL) {
779 CHECK(self->IsExceptionPending());
780 Thread::Current()->ClearException();
781 }
782}
783
784void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700785 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700786 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700787
Brian Carlstromae826982011-11-09 01:33:42 -0800788 // Strings are easy in that they always are simply resolved to literals in the same file
789 if (image_ && image_classes_ == NULL) {
790 // TODO: Add support for loading strings referenced by image_classes_
791 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700792 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
793 class_linker->ResolveString(dex_file, string_idx, dex_cache);
794 }
Elliott Hughesff738062012-02-03 15:00:42 -0800795 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700796 }
797
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800798 Context context;
799 context.class_linker = class_linker;
800 context.class_loader = class_loader;
801 context.dex_cache = dex_cache;
802 context.dex_file = &dex_file;
803
Elliott Hughes5523ee02012-02-03 18:18:34 -0800804 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800805 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700806
Elliott Hughes5523ee02012-02-03 18:18:34 -0800807 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800808 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700809}
810
Brian Carlstromae826982011-11-09 01:33:42 -0800811void Compiler::Verify(const ClassLoader* class_loader,
812 const std::vector<const DexFile*>& dex_files) {
813 for (size_t i = 0; i != dex_files.size(); ++i) {
814 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700815 CHECK(dex_file != NULL);
816 VerifyDexFile(class_loader, *dex_file);
817 }
818}
819
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800820static void VerifyClass(Context* context, size_t class_def_index) {
821 const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index);
822 const char* descriptor = context->dex_file->GetClassDescriptor(class_def);
823 Class* klass = context->class_linker->FindClass(descriptor, context->class_loader);
824 if (klass == NULL) {
825 Thread* self = Thread::Current();
826 CHECK(self->IsExceptionPending());
827 self->ClearException();
828 return;
829 }
830 CHECK(klass->IsResolved()) << PrettyClass(klass);
831 context->class_linker->VerifyClass(klass);
832
833 if (klass->IsErroneous()) {
834 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
835 CHECK(Thread::Current()->IsExceptionPending());
836 Thread::Current()->ClearException();
837 // We want to try verification again at run-time, so move back into the resolved state.
838 klass->SetStatus(Class::kStatusResolved);
839 }
840
841 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
842 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
843}
844
jeffhao98eacac2011-09-14 16:11:53 -0700845void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700846 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700847
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800848 Context context;
849 context.class_linker = Runtime::Current()->GetClassLinker();
850 context.class_loader = class_loader;
851 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800852 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700853
jeffhaob4df5142011-09-19 20:25:32 -0700854 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700855}
856
Brian Carlstromae826982011-11-09 01:33:42 -0800857void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
858 const std::vector<const DexFile*>& dex_files) {
859 for (size_t i = 0; i != dex_files.size(); ++i) {
860 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700861 CHECK(dex_file != NULL);
862 InitializeClassesWithoutClinit(class_loader, *dex_file);
863 }
864}
865
866void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
867 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700868 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
869 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700870 const char* descriptor = dex_file.GetClassDescriptor(class_def);
871 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700872 if (klass != NULL) {
873 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800874 // record the final class status if necessary
875 Class::Status status = klass->GetStatus();
876 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800877 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800878 CompiledClass* compiled_class = GetCompiledClass(ref);
879 if (compiled_class == NULL) {
880 compiled_class = new CompiledClass(status);
881 compiled_classes_[ref] = compiled_class;
882 } else {
883 DCHECK_EQ(status, compiled_class->GetStatus());
884 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700885 }
886 // clear any class not found or verification exceptions
887 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700888 }
889
890 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
891 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
892 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700893 if (klass == NULL) {
894 Thread::Current()->ClearException();
895 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700896 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
897 }
jeffhao98eacac2011-09-14 16:11:53 -0700898 }
899}
900
Brian Carlstromae826982011-11-09 01:33:42 -0800901void Compiler::Compile(const ClassLoader* class_loader,
902 const std::vector<const DexFile*>& dex_files) {
903 for (size_t i = 0; i != dex_files.size(); ++i) {
904 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700905 CHECK(dex_file != NULL);
906 CompileDexFile(class_loader, *dex_file);
907 }
908}
909
Elliott Hughesc225caa2012-02-03 15:43:37 -0800910void Compiler::CompileClass(Context* context, size_t class_def_index) {
911 const ClassLoader* class_loader = context->class_loader;
912 const DexFile& dex_file = *context->dex_file;
913 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800914 if (SkipClass(class_loader, dex_file, class_def)) {
915 return;
916 }
Ian Rogers0571d352011-11-03 19:51:38 -0700917 const byte* class_data = dex_file.GetClassData(class_def);
918 if (class_data == NULL) {
919 // empty class, probably a marker interface
920 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700921 }
Ian Rogers0571d352011-11-03 19:51:38 -0700922 ClassDataItemIterator it(dex_file, class_data);
923 // Skip fields
924 while (it.HasNextStaticField()) {
925 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700926 }
Ian Rogers0571d352011-11-03 19:51:38 -0700927 while (it.HasNextInstanceField()) {
928 it.Next();
929 }
930 // Compile direct methods
931 while (it.HasNextDirectMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800932 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
933 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700934 it.Next();
935 }
936 // Compile virtual methods
937 while (it.HasNextVirtualMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800938 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
939 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700940 it.Next();
941 }
942 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700943}
944
Elliott Hughesc225caa2012-02-03 15:43:37 -0800945void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
946 Context context;
947 context.class_loader = class_loader;
948 context.compiler = this;
949 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800950 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800951}
952
Ian Rogersa3760aa2011-11-14 14:32:37 -0800953void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
954 uint32_t method_idx, const ClassLoader* class_loader,
955 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700956 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800957 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800958 if ((access_flags & kAccNative) != 0) {
959 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700960 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800961 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700962 } else {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800963#if defined(ART_USE_LLVM_COMPILER)
964 compiled_method =
965 compiler_llvm_->CompileDexMethod(code_item, access_flags, method_idx,
966 class_loader, dex_file);
967#else
Ian Rogersa3760aa2011-11-14 14:32:37 -0800968 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
969 dex_file, kThumb2);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800970#endif
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800971 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
972 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800973 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800974 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800975 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800976 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700977 }
978
979 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700980 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800981 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800982 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700983 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800984 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700985 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700986
Ian Rogers0571d352011-11-03 19:51:38 -0700987 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800988 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700989 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
990 if (compiled_invoke_stub == NULL) {
991 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800992 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700993 } else {
994 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
995 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800996 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700997 }
998 CHECK(compiled_invoke_stub != NULL);
999 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001000 }
Ian Rogers0571d352011-11-03 19:51:38 -07001001 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001002}
1003
Ian Rogers0571d352011-11-03 19:51:38 -07001004static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1005 std::string key(shorty);
1006 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001007 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001008 }
Ian Rogers0571d352011-11-03 19:51:38 -07001009 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001010}
1011
Ian Rogers0571d352011-11-03 19:51:38 -07001012const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001013 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001014 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001015 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001016 if (it == compiled_invoke_stubs_.end()) {
1017 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001018 } else {
1019 DCHECK(it->second != NULL);
1020 return it->second;
1021 }
1022}
1023
1024void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
1025 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001026 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001027 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001028 compiled_invoke_stubs_[key] = compiled_invoke_stub;
1029}
1030
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001031CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001032 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001033 ClassTable::const_iterator it = compiled_classes_.find(ref);
1034 if (it == compiled_classes_.end()) {
1035 return NULL;
1036 }
1037 CHECK(it->second != NULL);
1038 return it->second;
1039}
1040
Ian Rogers0571d352011-11-03 19:51:38 -07001041CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001042 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001043 MethodTable::const_iterator it = compiled_methods_.find(ref);
1044 if (it == compiled_methods_.end()) {
1045 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001046 }
1047 CHECK(it->second != NULL);
1048 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001049}
1050
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001051void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
1052 for (size_t i = 0; i != dex_files.size(); ++i) {
1053 const DexFile* dex_file = dex_files[i];
1054 CHECK(dex_file != NULL);
1055 SetGcMapsDexFile(class_loader, *dex_file);
1056 }
1057}
1058
1059void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
1060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1061 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1062 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1063 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1064 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1065 Class* klass = class_linker->FindClass(descriptor, class_loader);
1066 if (klass == NULL || !klass->IsVerified()) {
1067 Thread::Current()->ClearException();
1068 continue;
1069 }
1070 const byte* class_data = dex_file.GetClassData(class_def);
1071 if (class_data == NULL) {
1072 // empty class such as a marker interface
1073 continue;
1074 }
1075 ClassDataItemIterator it(dex_file, class_data);
1076 while (it.HasNextStaticField()) {
1077 it.Next();
1078 }
1079 while (it.HasNextInstanceField()) {
1080 it.Next();
1081 }
1082 while (it.HasNextDirectMethod()) {
1083 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1084 class_loader, true);
1085 SetGcMapsMethod(dex_file, method);
1086 it.Next();
1087 }
1088 while (it.HasNextVirtualMethod()) {
1089 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1090 class_loader, false);
1091 SetGcMapsMethod(dex_file, method);
1092 it.Next();
1093 }
1094 }
1095}
1096
Ian Rogers1bddec32012-02-04 12:27:34 -08001097namespace verifier {
1098 class DexVerifier {
1099 public:
1100 static const std::vector<uint8_t>* GetGcMap(Compiler::MethodReference ref);
1101 };
1102}
1103
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001104void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
1105 if (method == NULL) {
1106 Thread::Current()->ClearException();
1107 return;
1108 }
1109 uint16_t method_idx = method->GetDexMethodIndex();
1110 MethodReference ref(&dex_file, method_idx);
1111 CompiledMethod* compiled_method = GetCompiledMethod(ref);
1112 if (compiled_method == NULL) {
1113 return;
1114 }
1115 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
1116 if (gc_map == NULL) {
1117 return;
1118 }
1119 compiled_method->SetGcMap(*gc_map);
1120}
1121
Brian Carlstromae826982011-11-09 01:33:42 -08001122void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
1123 for (size_t i = 0; i != dex_files.size(); ++i) {
1124 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07001125 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -07001126 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -07001127 }
1128}
1129
Brian Carlstrom8a487412011-08-29 20:08:52 -07001130void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -07001131 Runtime* runtime = Runtime::Current();
1132 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -07001133 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001134 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001135 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -07001136 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001137 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -07001138 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
1139 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001140 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001141 } else {
1142 // TODO: we currently leave the entry blank for resolved
1143 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -07001144 }
1145 }
1146}
1147
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001148} // namespace art