blob: 37a74146029dd8f9254015070008e7286c625673 [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,
jeffhao8cd6dda2012-02-22 10:15:34 -0800459 int& field_offset, bool& is_volatile, bool is_put) {
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();
jeffhao8cd6dda2012-02-22 10:15:34 -0800469 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
470 fields_class != referrer_class;
471 if (referrer_class != NULL && referrer_class->CanAccess(fields_class) &&
472 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
473 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800474 field_offset = resolved_field->GetOffset().Int32Value();
475 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800476 stats_->ResolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800477 return true; // Fast path.
478 }
479 }
480 // Clean up any exception left by field/type resolution
481 Thread* thread = Thread::Current();
482 if (thread->IsExceptionPending()) {
483 thread->ClearException();
484 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800485 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800486 return false; // Incomplete knowledge needs slow path.
487}
488
489bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, CompilationUnit* cUnit,
490 int& field_offset, int& ssb_index,
jeffhao8cd6dda2012-02-22 10:15:34 -0800491 bool& is_referrers_class, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800492 // Conservative defaults
493 field_offset = -1;
494 ssb_index = -1;
495 is_referrers_class = false;
496 is_volatile = true;
497 // Try to resolve field
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800498 Field* resolved_field = ComputeReferrerField(cUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800499 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800500 DCHECK(resolved_field->IsStatic());
501 Class* referrer_class = ComputeReferrerClass(cUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800502 if (referrer_class != NULL) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800503 Class* fields_class = resolved_field->GetDeclaringClass();
504 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800505 is_referrers_class = true; // implies no worrying about class initialization
506 field_offset = resolved_field->GetOffset().Int32Value();
507 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800508 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800509 return true; // fast path
510 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -0800511 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogers1bddec32012-02-04 12:27:34 -0800512 if (referrer_class->CanAccess(fields_class) &&
jeffhao8cd6dda2012-02-22 10:15:34 -0800513 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
514 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800515 // We have the resolved field, we must make it into a ssbIndex for the referrer
516 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800517 // TODO: for images we can elide the static storage base null check
518 // if we know there's a non-null entry in the image
519 if (fields_class->GetDexCache() == cUnit->dex_cache) {
520 // common case where the dex cache of both the referrer and the field are the same,
521 // no need to search the dex file
522 ssb_index = fields_class->GetDexTypeIndex();
523 field_offset = resolved_field->GetOffset().Int32Value();
524 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800525 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800526 return true;
527 }
528 // Search dex file for localized ssb index
Ian Rogers1bddec32012-02-04 12:27:34 -0800529 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
530 const DexFile::StringId* string_id =
531 cUnit->dex_file->FindStringId(descriptor);
532 if (string_id != NULL) {
533 const DexFile::TypeId* type_id =
534 cUnit->dex_file->FindTypeId(cUnit->dex_file->GetIndexForStringId(*string_id));
535 if(type_id != NULL) {
536 // medium path, needs check of static storage base being initialized
Ian Rogers1bddec32012-02-04 12:27:34 -0800537 ssb_index = cUnit->dex_file->GetIndexForTypeId(*type_id);
538 field_offset = resolved_field->GetOffset().Int32Value();
539 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800540 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800541 return true;
542 }
543 }
544 }
545 }
546 }
547 }
548 // Clean up any exception left by field/type resolution
549 Thread* thread = Thread::Current();
550 if (thread->IsExceptionPending()) {
551 thread->ClearException();
552 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800553 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800554 return false; // Incomplete knowledge needs slow path.
555}
556
Ian Rogersc8b306f2012-02-17 21:34:44 -0800557bool Compiler::ComputeInvokeInfo(uint32_t method_idx, CompilationUnit* cUnit, InvokeType type,
Ian Rogers996cc582012-02-14 22:23:29 -0800558 int& vtable_idx) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800559 vtable_idx = -1;
560 Method* resolved_method = ComputeReferrerMethod(cUnit, method_idx);
561 if (resolved_method != NULL) {
562 Class* referrer_class = ComputeReferrerClass(cUnit);
563 if (referrer_class != NULL) {
564 Class* methods_class = resolved_method->GetDeclaringClass();
565 if (!referrer_class->CanAccess(methods_class) ||
566 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800567 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800568 // The referring class can't access the resolved method, this may occur as a result of a
569 // protected method being made public by implementing an interface that re-declares the
570 // method public. Resort to the dex file to determine the correct class for the access check
571 const DexFile& dex_file = cUnit->class_linker->FindDexFile(referrer_class->GetDexCache());
572 methods_class =
573 cUnit->class_linker->ResolveType(dex_file,
574 dex_file.GetMethodId(method_idx).class_idx_,
575 referrer_class);
576
577 }
578 if (referrer_class->CanAccess(methods_class) &&
579 referrer_class->CanAccessMember(methods_class,
580 resolved_method->GetAccessFlags())) {
581 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800582 if (type != kSuper) {
583 // nothing left to do for static/direct/virtual/interface dispatch
584 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800585 return true;
586 } else {
587 // ensure the vtable index will be correct to dispatch in the vtable of the super class
Ian Rogers996cc582012-02-14 22:23:29 -0800588 if (referrer_class->IsSubClass(methods_class) &&
589 vtable_idx < methods_class->GetVTable()->GetLength()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800590 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800591 return true;
592 }
593 }
594 }
595 }
596 }
597 // Clean up any exception left by method/type resolution
598 Thread* thread = Thread::Current();
599 if (thread->IsExceptionPending()) {
600 thread->ClearException();
601 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800602 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800603 return false; // Incomplete knowledge needs slow path.
604}
605
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800606// Return true if the class should be skipped during compilation. We
607// never skip classes in the boot class loader. However, if we have a
608// non-boot class loader and we can resolve the class in the boot
609// class loader, we do skip the class. This happens if an app bundles
610// classes found in the boot classpath. Since at runtime we will
611// select the class from the boot classpath, do not attempt to resolve
612// or compile it now.
613static bool SkipClass(const ClassLoader* class_loader,
614 const DexFile& dex_file,
615 const DexFile::ClassDef& class_def) {
616 if (class_loader == NULL) {
617 return false;
618 }
619 const char* descriptor = dex_file.GetClassDescriptor(class_def);
620 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
621 Class* klass = class_linker->FindClass(descriptor, NULL);
622 if (klass == NULL) {
623 Thread* self = Thread::Current();
624 CHECK(self->IsExceptionPending());
625 self->ClearException();
626 return false;
627 }
628 return true;
629}
630
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800631struct Context {
632 ClassLinker* class_linker;
633 const ClassLoader* class_loader;
Elliott Hughesc225caa2012-02-03 15:43:37 -0800634 Compiler* compiler;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800635 DexCache* dex_cache;
636 const DexFile* dex_file;
637};
638
639typedef void Callback(Context* context, size_t index);
640
641class WorkerThread {
642 public:
Elliott Hughes1e409252012-02-06 11:21:27 -0800643 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
644 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
645 if (spawn_) {
646 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread");
647 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800648 }
649
650 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800651 if (spawn_) {
652 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
653 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800654 }
655
656 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800657 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800658 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
659 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800660 if (worker->spawn_) {
661 runtime->AttachCurrentThread("Compiler Worker", true);
662 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800663 Thread::Current()->SetState(Thread::kRunnable);
664 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800665 if (worker->spawn_) {
666 Thread::Current()->SetState(Thread::kNative);
667 runtime->DetachCurrentThread();
668 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800669 return NULL;
670 }
671
Elliott Hughes1e409252012-02-06 11:21:27 -0800672 void Go() {
673 Go(this);
674 }
675
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800676 void Run() {
677 for (size_t i = begin_; i < end_; i += stripe_) {
678 callback_(context_, i);
679 }
680 }
681
682 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800683 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800684
685 Context* context_;
686 size_t begin_;
687 size_t end_;
688 Callback* callback_;
689 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800690
691 friend void ForAll(Context*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800692};
693
Elliott Hughes5523ee02012-02-03 18:18:34 -0800694void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800695 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -0800696
Elliott Hughes1e409252012-02-06 11:21:27 -0800697 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -0800698 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800699 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800700 }
Elliott Hughes1e409252012-02-06 11:21:27 -0800701 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800702
Elliott Hughes81d91512012-02-03 16:38:43 -0800703 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
704 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
705 STLDeleteElements(&threads);
706}
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800707
708static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
709 const DexFile& dex_file = *context->dex_file;
710
711 // Method and Field are the worst. We can't resolve without either
712 // context from the code use (to disambiguate virtual vs direct
713 // method and instance vs static field) or from class
714 // definitions. While the compiler will resolve what it can as it
715 // needs it, here we try to resolve fields and methods used in class
716 // definitions, since many of them many never be referenced by
717 // generated code.
718 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
719 if (SkipClass(context->class_loader, dex_file, class_def)) {
720 return;
721 }
722
723 // Note the class_data pointer advances through the headers,
724 // static fields, instance fields, direct methods, and virtual
725 // methods.
726 const byte* class_data = dex_file.GetClassData(class_def);
727 if (class_data == NULL) {
728 // empty class such as a marker interface
729 return;
730 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800731 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800732 ClassLinker* class_linker = context->class_linker;
733 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
734 ClassDataItemIterator it(dex_file, class_data);
735 while (it.HasNextStaticField()) {
736 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
737 context->class_loader, true);
738 if (field == NULL) {
739 CHECK(self->IsExceptionPending());
740 self->ClearException();
741 }
742 it.Next();
743 }
744 while (it.HasNextInstanceField()) {
745 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
746 context->class_loader, false);
747 if (field == NULL) {
748 CHECK(self->IsExceptionPending());
749 self->ClearException();
750 }
751 it.Next();
752 }
753 while (it.HasNextDirectMethod()) {
754 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
755 context->class_loader, true);
756 if (method == NULL) {
757 CHECK(self->IsExceptionPending());
758 self->ClearException();
759 }
760 it.Next();
761 }
762 while (it.HasNextVirtualMethod()) {
763 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
764 context->class_loader, false);
765 if (method == NULL) {
766 CHECK(self->IsExceptionPending());
767 self->ClearException();
768 }
769 it.Next();
770 }
771 DCHECK(!it.HasNext());
772}
773
774static void ResolveType(Context* context, size_t type_idx) {
775 // Class derived values are more complicated, they require the linker and loader.
776 Thread* self = Thread::Current();
777 ClassLinker* class_linker = context->class_linker;
778 const DexFile& dex_file = *context->dex_file;
779 Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader);
780 if (klass == NULL) {
781 CHECK(self->IsExceptionPending());
782 Thread::Current()->ClearException();
783 }
784}
785
786void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700787 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700788 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700789
Brian Carlstromae826982011-11-09 01:33:42 -0800790 // Strings are easy in that they always are simply resolved to literals in the same file
791 if (image_ && image_classes_ == NULL) {
792 // TODO: Add support for loading strings referenced by image_classes_
793 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700794 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
795 class_linker->ResolveString(dex_file, string_idx, dex_cache);
796 }
Elliott Hughesff738062012-02-03 15:00:42 -0800797 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700798 }
799
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800800 Context context;
801 context.class_linker = class_linker;
802 context.class_loader = class_loader;
803 context.dex_cache = dex_cache;
804 context.dex_file = &dex_file;
805
Elliott Hughes5523ee02012-02-03 18:18:34 -0800806 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800807 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700808
Elliott Hughes5523ee02012-02-03 18:18:34 -0800809 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800810 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700811}
812
Brian Carlstromae826982011-11-09 01:33:42 -0800813void Compiler::Verify(const ClassLoader* class_loader,
814 const std::vector<const DexFile*>& dex_files) {
815 for (size_t i = 0; i != dex_files.size(); ++i) {
816 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700817 CHECK(dex_file != NULL);
818 VerifyDexFile(class_loader, *dex_file);
819 }
820}
821
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800822static void VerifyClass(Context* context, size_t class_def_index) {
823 const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index);
824 const char* descriptor = context->dex_file->GetClassDescriptor(class_def);
825 Class* klass = context->class_linker->FindClass(descriptor, context->class_loader);
826 if (klass == NULL) {
827 Thread* self = Thread::Current();
828 CHECK(self->IsExceptionPending());
829 self->ClearException();
830 return;
831 }
832 CHECK(klass->IsResolved()) << PrettyClass(klass);
833 context->class_linker->VerifyClass(klass);
834
835 if (klass->IsErroneous()) {
836 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
837 CHECK(Thread::Current()->IsExceptionPending());
838 Thread::Current()->ClearException();
839 // We want to try verification again at run-time, so move back into the resolved state.
840 klass->SetStatus(Class::kStatusResolved);
841 }
842
843 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
844 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
845}
846
jeffhao98eacac2011-09-14 16:11:53 -0700847void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700848 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700849
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800850 Context context;
851 context.class_linker = Runtime::Current()->GetClassLinker();
852 context.class_loader = class_loader;
853 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800854 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700855
jeffhaob4df5142011-09-19 20:25:32 -0700856 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700857}
858
Brian Carlstromae826982011-11-09 01:33:42 -0800859void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
860 const std::vector<const DexFile*>& dex_files) {
861 for (size_t i = 0; i != dex_files.size(); ++i) {
862 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700863 CHECK(dex_file != NULL);
864 InitializeClassesWithoutClinit(class_loader, *dex_file);
865 }
866}
867
868void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
869 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700870 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
871 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700872 const char* descriptor = dex_file.GetClassDescriptor(class_def);
873 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700874 if (klass != NULL) {
875 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800876 // record the final class status if necessary
877 Class::Status status = klass->GetStatus();
878 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800879 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800880 CompiledClass* compiled_class = GetCompiledClass(ref);
881 if (compiled_class == NULL) {
882 compiled_class = new CompiledClass(status);
883 compiled_classes_[ref] = compiled_class;
884 } else {
885 DCHECK_EQ(status, compiled_class->GetStatus());
886 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700887 }
888 // clear any class not found or verification exceptions
889 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700890 }
891
892 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
893 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
894 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700895 if (klass == NULL) {
896 Thread::Current()->ClearException();
897 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700898 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
899 }
jeffhao98eacac2011-09-14 16:11:53 -0700900 }
901}
902
Brian Carlstromae826982011-11-09 01:33:42 -0800903void Compiler::Compile(const ClassLoader* class_loader,
904 const std::vector<const DexFile*>& dex_files) {
905 for (size_t i = 0; i != dex_files.size(); ++i) {
906 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700907 CHECK(dex_file != NULL);
908 CompileDexFile(class_loader, *dex_file);
909 }
910}
911
Elliott Hughesc225caa2012-02-03 15:43:37 -0800912void Compiler::CompileClass(Context* context, size_t class_def_index) {
913 const ClassLoader* class_loader = context->class_loader;
914 const DexFile& dex_file = *context->dex_file;
915 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800916 if (SkipClass(class_loader, dex_file, class_def)) {
917 return;
918 }
Ian Rogers0571d352011-11-03 19:51:38 -0700919 const byte* class_data = dex_file.GetClassData(class_def);
920 if (class_data == NULL) {
921 // empty class, probably a marker interface
922 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700923 }
Ian Rogers0571d352011-11-03 19:51:38 -0700924 ClassDataItemIterator it(dex_file, class_data);
925 // Skip fields
926 while (it.HasNextStaticField()) {
927 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700928 }
Ian Rogers0571d352011-11-03 19:51:38 -0700929 while (it.HasNextInstanceField()) {
930 it.Next();
931 }
932 // Compile direct methods
933 while (it.HasNextDirectMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800934 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
935 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700936 it.Next();
937 }
938 // Compile virtual methods
939 while (it.HasNextVirtualMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800940 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
941 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700942 it.Next();
943 }
944 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700945}
946
Elliott Hughesc225caa2012-02-03 15:43:37 -0800947void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
948 Context context;
949 context.class_loader = class_loader;
950 context.compiler = this;
951 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800952 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800953}
954
Ian Rogersa3760aa2011-11-14 14:32:37 -0800955void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
956 uint32_t method_idx, const ClassLoader* class_loader,
957 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700958 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800959 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800960 if ((access_flags & kAccNative) != 0) {
961 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700962 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800963 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700964 } else {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800965#if defined(ART_USE_LLVM_COMPILER)
966 compiled_method =
967 compiler_llvm_->CompileDexMethod(code_item, access_flags, method_idx,
968 class_loader, dex_file);
969#else
Ian Rogersa3760aa2011-11-14 14:32:37 -0800970 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
971 dex_file, kThumb2);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800972#endif
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800973 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
974 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800975 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800976 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800977 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800978 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700979 }
980
981 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700982 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800983 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800984 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700985 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800986 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700987 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700988
Ian Rogers0571d352011-11-03 19:51:38 -0700989 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800990 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700991 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
992 if (compiled_invoke_stub == NULL) {
Logan Chienf04364f2012-02-10 12:01:39 +0800993#if defined(ART_USE_LLVM_COMPILER)
994 compiled_invoke_stub = compiler_llvm_->CreateInvokeStub(is_static, shorty);
995#else
Ian Rogers0571d352011-11-03 19:51:38 -0700996 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800997 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700998 } else {
999 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
1000 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001001 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -07001002 }
Logan Chienf04364f2012-02-10 12:01:39 +08001003#endif
1004
Ian Rogers0571d352011-11-03 19:51:38 -07001005 CHECK(compiled_invoke_stub != NULL);
1006 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001007 }
Ian Rogers0571d352011-11-03 19:51:38 -07001008 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001009}
1010
Ian Rogers0571d352011-11-03 19:51:38 -07001011static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1012 std::string key(shorty);
1013 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001014 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001015 }
Ian Rogers0571d352011-11-03 19:51:38 -07001016 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001017}
1018
Ian Rogers0571d352011-11-03 19:51:38 -07001019const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001020 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001021 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001022 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001023 if (it == compiled_invoke_stubs_.end()) {
1024 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001025 } else {
1026 DCHECK(it->second != NULL);
1027 return it->second;
1028 }
1029}
1030
1031void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
1032 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001033 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001034 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001035 compiled_invoke_stubs_[key] = compiled_invoke_stub;
1036}
1037
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001038CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001039 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001040 ClassTable::const_iterator it = compiled_classes_.find(ref);
1041 if (it == compiled_classes_.end()) {
1042 return NULL;
1043 }
1044 CHECK(it->second != NULL);
1045 return it->second;
1046}
1047
Ian Rogers0571d352011-11-03 19:51:38 -07001048CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001049 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001050 MethodTable::const_iterator it = compiled_methods_.find(ref);
1051 if (it == compiled_methods_.end()) {
1052 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001053 }
1054 CHECK(it->second != NULL);
1055 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001056}
1057
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001058void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
1059 for (size_t i = 0; i != dex_files.size(); ++i) {
1060 const DexFile* dex_file = dex_files[i];
1061 CHECK(dex_file != NULL);
1062 SetGcMapsDexFile(class_loader, *dex_file);
1063 }
1064}
1065
1066void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
1067 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1068 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1069 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1070 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1071 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1072 Class* klass = class_linker->FindClass(descriptor, class_loader);
1073 if (klass == NULL || !klass->IsVerified()) {
1074 Thread::Current()->ClearException();
1075 continue;
1076 }
1077 const byte* class_data = dex_file.GetClassData(class_def);
1078 if (class_data == NULL) {
1079 // empty class such as a marker interface
1080 continue;
1081 }
1082 ClassDataItemIterator it(dex_file, class_data);
1083 while (it.HasNextStaticField()) {
1084 it.Next();
1085 }
1086 while (it.HasNextInstanceField()) {
1087 it.Next();
1088 }
1089 while (it.HasNextDirectMethod()) {
1090 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1091 class_loader, true);
1092 SetGcMapsMethod(dex_file, method);
1093 it.Next();
1094 }
1095 while (it.HasNextVirtualMethod()) {
1096 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1097 class_loader, false);
1098 SetGcMapsMethod(dex_file, method);
1099 it.Next();
1100 }
1101 }
1102}
1103
Ian Rogers1bddec32012-02-04 12:27:34 -08001104namespace verifier {
1105 class DexVerifier {
1106 public:
1107 static const std::vector<uint8_t>* GetGcMap(Compiler::MethodReference ref);
1108 };
1109}
1110
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001111void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
1112 if (method == NULL) {
1113 Thread::Current()->ClearException();
1114 return;
1115 }
1116 uint16_t method_idx = method->GetDexMethodIndex();
1117 MethodReference ref(&dex_file, method_idx);
1118 CompiledMethod* compiled_method = GetCompiledMethod(ref);
1119 if (compiled_method == NULL) {
1120 return;
1121 }
1122 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
1123 if (gc_map == NULL) {
1124 return;
1125 }
1126 compiled_method->SetGcMap(*gc_map);
1127}
1128
Brian Carlstromae826982011-11-09 01:33:42 -08001129void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
1130 for (size_t i = 0; i != dex_files.size(); ++i) {
1131 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07001132 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -07001133 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -07001134 }
1135}
1136
Brian Carlstrom8a487412011-08-29 20:08:52 -07001137void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -07001138 Runtime* runtime = Runtime::Current();
1139 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -07001140 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001141 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001142 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -07001143 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001144 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -07001145 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
1146 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001147 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001148 } else {
1149 // TODO: we currently leave the entry blank for resolved
1150 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -07001151 }
1152 }
1153}
1154
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001155} // namespace art