blob: 75f7484f8dc29c826c3d4eead2125c53a183618f [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,
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800205 bool support_debugging, 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),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800213 support_debugging_(support_debugging),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800214 stats_(new AOTCompilationStats),
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800215 image_classes_(image_classes)
216#if defined(ART_USE_LLVM_COMPILER)
217 ,
218 compiler_llvm_(new compiler_llvm::CompilerLLVM(this, instruction_set))
219#endif
220 {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700221 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800222 if (!image_) {
223 CHECK(image_classes_ == NULL);
224 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700225}
226
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700227Compiler::~Compiler() {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800228 {
229 MutexLock mu(compiled_classes_lock_);
230 STLDeleteValues(&compiled_classes_);
231 }
232 {
233 MutexLock mu(compiled_methods_lock_);
234 STLDeleteValues(&compiled_methods_);
235 }
236 {
237 MutexLock mu(compiled_invoke_stubs_lock_);
238 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800239 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700240}
241
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700242ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
243 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700244 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700245 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700246 } else {
247 CHECK(instruction_set == kArm || instruction_set == kThumb2);
248 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700249 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700250 }
251}
252
Elliott Hughes8add92d2012-01-18 18:18:43 -0800253ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800254 switch (instruction_set) {
255 case kArm:
256 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800257 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800258 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800259 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800260 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800261 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -0800262 return NULL;
263 }
264}
265
Ian Rogersad25ac52011-10-04 19:13:33 -0700266ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
267 if (instruction_set == kX86) {
268 return x86::CreateAbstractMethodErrorStub();
269 } else {
270 CHECK(instruction_set == kArm || instruction_set == kThumb2);
271 // Generates resolution stub using ARM instruction set
272 return arm::CreateAbstractMethodErrorStub();
273 }
274}
275
Jesse Wilson254db0f2011-11-16 16:44:11 -0500276void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800277 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700278 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800279
Elliott Hughes601a1232012-02-02 17:47:38 -0800280 TimingLogger timings("compiler");
281
282 PreCompile(class_loader, dex_files, timings);
283
Brian Carlstromae826982011-11-09 01:33:42 -0800284 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800285 timings.AddSplit("Compile");
286
Brian Carlstromae826982011-11-09 01:33:42 -0800287 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800288 timings.AddSplit("PostCompile");
289
290 if (timings.GetTotalNs() > MsToNs(1000)) {
291 timings.Dump();
292 }
Ian Rogers996cc582012-02-14 22:23:29 -0800293
Ian Rogersc8b306f2012-02-17 21:34:44 -0800294 stats_->Dump();
Brian Carlstrom8a487412011-08-29 20:08:52 -0700295}
296
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700297void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700298 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800299
Brian Carlstrom8a487412011-08-29 20:08:52 -0700300 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800301
Ian Rogers0571d352011-11-03 19:51:38 -0700302 // Find the dex_file
303 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
304 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800305 std::vector<const DexFile*> dex_files;
306 dex_files.push_back(&dex_file);
307
Elliott Hughes601a1232012-02-02 17:47:38 -0800308 TimingLogger timings("CompileOne");
309 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800310
Ian Rogers0571d352011-11-03 19:51:38 -0700311 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800312 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
313 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800314
315 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700316}
317
Brian Carlstromae826982011-11-09 01:33:42 -0800318void Compiler::Resolve(const ClassLoader* class_loader,
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800319 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800320 for (size_t i = 0; i != dex_files.size(); ++i) {
321 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700322 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800323 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700324 }
325}
326
Brian Carlstromae826982011-11-09 01:33:42 -0800327void Compiler::PreCompile(const ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800328 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800329 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800330
Brian Carlstromae826982011-11-09 01:33:42 -0800331 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800332 timings.AddSplit("PreCompile.Verify");
333
Brian Carlstromae826982011-11-09 01:33:42 -0800334 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800335 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800336}
337
338void Compiler::PostCompile(const ClassLoader* class_loader,
339 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800340 SetGcMaps(class_loader, dex_files);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800341#if defined(ART_USE_LLVM_COMPILER)
342 compiler_llvm_->MaterializeLLVMModule();
343#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800344 SetCodeAndDirectMethods(dex_files);
345}
346
347bool Compiler::IsImageClass(const std::string& descriptor) const {
348 if (image_classes_ == NULL) {
349 return true;
350 }
351 return image_classes_->find(descriptor) != image_classes_->end();
352}
353
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800354bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800355 uint32_t type_idx) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800356 if (!IsImage()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800357 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800358 return false;
359 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800360 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800361 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800362 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800363 return false;
364 }
Ian Rogers996cc582012-02-14 22:23:29 -0800365 bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor());
366 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800367 stats_->TypeInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800368 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800369 stats_->TypeNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800370 }
371 return result;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800372}
373
Ian Rogers1bddec32012-02-04 12:27:34 -0800374bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800375 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800376 // TODO: Add support for loading strings referenced by image_classes_
377 // See also Compiler::ResolveDexFile
378
379 // The following is a test saying that if we're building the image without a restricted set of
380 // 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 -0800381 bool result = IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
382 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800383 stats_->StringInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800384 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800385 stats_->StringNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800386 }
387 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800388}
389
390bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800391 const DexFile& dex_file, uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800392 // Get type from dex cache assuming it was populated by the verifier
393 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
394 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800395 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800396 return false; // Unknown class needs access checks.
397 }
398 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
399 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
400 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800401 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800402 return false; // Incomplete referrer knowledge needs access check.
403 }
404 // Perform access check, will return true if access is ok or false if we're going to have to
405 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800406 bool result = referrer_class->CanAccess(resolved_class);
407 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800408 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800409 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800410 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800411 }
412 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800413}
414
415bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
416 const DexCache* dex_cache,
417 const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800418 uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800419 // Get type from dex cache assuming it was populated by the verifier.
420 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
421 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800422 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800423 return false; // Unknown class needs access checks.
424 }
425 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
426 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
427 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800428 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800429 return false; // Incomplete referrer knowledge needs access check.
430 }
431 // Perform access and instantiable checks, will return true if access is ok or false if we're
432 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800433 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
434 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800435 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800436 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800437 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800438 }
439 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800440}
441
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800442static Class* ComputeReferrerClass(CompilationUnit* cUnit) {
443 const DexFile::MethodId& referrer_method_id = cUnit->dex_file->GetMethodId(cUnit->method_idx);
444 return cUnit->class_linker->ResolveType(*cUnit->dex_file, referrer_method_id.class_idx_,
445 cUnit->dex_cache, cUnit->class_loader);
446}
447
448static Field* ComputeReferrerField(CompilationUnit* cUnit, uint32_t field_idx) {
449 return cUnit->class_linker->ResolveField(*cUnit->dex_file, field_idx, cUnit->dex_cache,
450 cUnit->class_loader, false);
451
452}
453
454static Method* ComputeReferrerMethod(CompilationUnit* cUnit, uint32_t method_idx) {
455 return cUnit->class_linker->ResolveMethod(*cUnit->dex_file, method_idx, cUnit->dex_cache,
456 cUnit->class_loader, true);
457}
458
Ian Rogers1bddec32012-02-04 12:27:34 -0800459bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, CompilationUnit* cUnit,
jeffhao8cd6dda2012-02-22 10:15:34 -0800460 int& field_offset, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800461 // Conservative defaults
462 field_offset = -1;
463 is_volatile = true;
464 // Try to resolve field
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800465 Field* resolved_field = ComputeReferrerField(cUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800466 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800467 Class* referrer_class = ComputeReferrerClass(cUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800468 // Try to resolve referring class then access check, failure to pass the
469 Class* fields_class = resolved_field->GetDeclaringClass();
jeffhao8cd6dda2012-02-22 10:15:34 -0800470 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
471 fields_class != referrer_class;
472 if (referrer_class != NULL && referrer_class->CanAccess(fields_class) &&
473 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
474 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800475 field_offset = resolved_field->GetOffset().Int32Value();
476 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800477 stats_->ResolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800478 return true; // Fast path.
479 }
480 }
481 // Clean up any exception left by field/type resolution
482 Thread* thread = Thread::Current();
483 if (thread->IsExceptionPending()) {
484 thread->ClearException();
485 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800486 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800487 return false; // Incomplete knowledge needs slow path.
488}
489
490bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, CompilationUnit* cUnit,
491 int& field_offset, int& ssb_index,
jeffhao8cd6dda2012-02-22 10:15:34 -0800492 bool& is_referrers_class, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800493 // Conservative defaults
494 field_offset = -1;
495 ssb_index = -1;
496 is_referrers_class = false;
497 is_volatile = true;
498 // Try to resolve field
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800499 Field* resolved_field = ComputeReferrerField(cUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800500 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800501 DCHECK(resolved_field->IsStatic());
502 Class* referrer_class = ComputeReferrerClass(cUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800503 if (referrer_class != NULL) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800504 Class* fields_class = resolved_field->GetDeclaringClass();
505 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800506 is_referrers_class = true; // implies no worrying about class initialization
507 field_offset = resolved_field->GetOffset().Int32Value();
508 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800509 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800510 return true; // fast path
511 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -0800512 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogers1bddec32012-02-04 12:27:34 -0800513 if (referrer_class->CanAccess(fields_class) &&
jeffhao8cd6dda2012-02-22 10:15:34 -0800514 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
515 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800516 // We have the resolved field, we must make it into a ssbIndex for the referrer
517 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800518 // TODO: for images we can elide the static storage base null check
519 // if we know there's a non-null entry in the image
520 if (fields_class->GetDexCache() == cUnit->dex_cache) {
521 // common case where the dex cache of both the referrer and the field are the same,
522 // no need to search the dex file
523 ssb_index = fields_class->GetDexTypeIndex();
524 field_offset = resolved_field->GetOffset().Int32Value();
525 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800526 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800527 return true;
528 }
529 // Search dex file for localized ssb index
Ian Rogers1bddec32012-02-04 12:27:34 -0800530 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
531 const DexFile::StringId* string_id =
532 cUnit->dex_file->FindStringId(descriptor);
533 if (string_id != NULL) {
534 const DexFile::TypeId* type_id =
535 cUnit->dex_file->FindTypeId(cUnit->dex_file->GetIndexForStringId(*string_id));
536 if(type_id != NULL) {
537 // medium path, needs check of static storage base being initialized
Ian Rogers1bddec32012-02-04 12:27:34 -0800538 ssb_index = cUnit->dex_file->GetIndexForTypeId(*type_id);
539 field_offset = resolved_field->GetOffset().Int32Value();
540 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800541 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800542 return true;
543 }
544 }
545 }
546 }
547 }
548 }
549 // Clean up any exception left by field/type resolution
550 Thread* thread = Thread::Current();
551 if (thread->IsExceptionPending()) {
552 thread->ClearException();
553 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800554 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800555 return false; // Incomplete knowledge needs slow path.
556}
557
Ian Rogersc8b306f2012-02-17 21:34:44 -0800558bool Compiler::ComputeInvokeInfo(uint32_t method_idx, CompilationUnit* cUnit, InvokeType type,
Ian Rogers996cc582012-02-14 22:23:29 -0800559 int& vtable_idx) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800560 vtable_idx = -1;
561 Method* resolved_method = ComputeReferrerMethod(cUnit, method_idx);
562 if (resolved_method != NULL) {
563 Class* referrer_class = ComputeReferrerClass(cUnit);
564 if (referrer_class != NULL) {
565 Class* methods_class = resolved_method->GetDeclaringClass();
566 if (!referrer_class->CanAccess(methods_class) ||
567 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800568 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800569 // The referring class can't access the resolved method, this may occur as a result of a
570 // protected method being made public by implementing an interface that re-declares the
571 // method public. Resort to the dex file to determine the correct class for the access check
572 const DexFile& dex_file = cUnit->class_linker->FindDexFile(referrer_class->GetDexCache());
573 methods_class =
574 cUnit->class_linker->ResolveType(dex_file,
575 dex_file.GetMethodId(method_idx).class_idx_,
576 referrer_class);
577
578 }
579 if (referrer_class->CanAccess(methods_class) &&
580 referrer_class->CanAccessMember(methods_class,
581 resolved_method->GetAccessFlags())) {
582 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800583 if (type != kSuper) {
584 // nothing left to do for static/direct/virtual/interface dispatch
585 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800586 return true;
587 } else {
588 // ensure the vtable index will be correct to dispatch in the vtable of the super class
Ian Rogers996cc582012-02-14 22:23:29 -0800589 if (referrer_class->IsSubClass(methods_class) &&
590 vtable_idx < methods_class->GetVTable()->GetLength()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800591 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800592 return true;
593 }
594 }
595 }
596 }
597 }
598 // Clean up any exception left by method/type resolution
599 Thread* thread = Thread::Current();
600 if (thread->IsExceptionPending()) {
601 thread->ClearException();
602 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800603 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800604 return false; // Incomplete knowledge needs slow path.
605}
606
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800607// Return true if the class should be skipped during compilation. We
608// never skip classes in the boot class loader. However, if we have a
609// non-boot class loader and we can resolve the class in the boot
610// class loader, we do skip the class. This happens if an app bundles
611// classes found in the boot classpath. Since at runtime we will
612// select the class from the boot classpath, do not attempt to resolve
613// or compile it now.
614static bool SkipClass(const ClassLoader* class_loader,
615 const DexFile& dex_file,
616 const DexFile::ClassDef& class_def) {
617 if (class_loader == NULL) {
618 return false;
619 }
620 const char* descriptor = dex_file.GetClassDescriptor(class_def);
621 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
622 Class* klass = class_linker->FindClass(descriptor, NULL);
623 if (klass == NULL) {
624 Thread* self = Thread::Current();
625 CHECK(self->IsExceptionPending());
626 self->ClearException();
627 return false;
628 }
629 return true;
630}
631
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800632struct Context {
633 ClassLinker* class_linker;
634 const ClassLoader* class_loader;
Elliott Hughesc225caa2012-02-03 15:43:37 -0800635 Compiler* compiler;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800636 DexCache* dex_cache;
637 const DexFile* dex_file;
638};
639
640typedef void Callback(Context* context, size_t index);
641
642class WorkerThread {
643 public:
Elliott Hughes1e409252012-02-06 11:21:27 -0800644 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
645 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
646 if (spawn_) {
647 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread");
648 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800649 }
650
651 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800652 if (spawn_) {
653 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
654 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800655 }
656
657 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800658 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800659 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
660 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800661 if (worker->spawn_) {
662 runtime->AttachCurrentThread("Compiler Worker", true);
663 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800664 Thread::Current()->SetState(Thread::kRunnable);
665 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800666 if (worker->spawn_) {
667 Thread::Current()->SetState(Thread::kNative);
668 runtime->DetachCurrentThread();
669 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800670 return NULL;
671 }
672
Elliott Hughes1e409252012-02-06 11:21:27 -0800673 void Go() {
674 Go(this);
675 }
676
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800677 void Run() {
678 for (size_t i = begin_; i < end_; i += stripe_) {
679 callback_(context_, i);
680 }
681 }
682
683 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800684 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800685
686 Context* context_;
687 size_t begin_;
688 size_t end_;
689 Callback* callback_;
690 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800691
692 friend void ForAll(Context*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800693};
694
Elliott Hughes5523ee02012-02-03 18:18:34 -0800695void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800696 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -0800697
Elliott Hughes1e409252012-02-06 11:21:27 -0800698 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -0800699 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800700 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800701 }
Elliott Hughes1e409252012-02-06 11:21:27 -0800702 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800703
Elliott Hughes81d91512012-02-03 16:38:43 -0800704 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
705 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
706 STLDeleteElements(&threads);
707}
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800708
709static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
710 const DexFile& dex_file = *context->dex_file;
711
712 // Method and Field are the worst. We can't resolve without either
713 // context from the code use (to disambiguate virtual vs direct
714 // method and instance vs static field) or from class
715 // definitions. While the compiler will resolve what it can as it
716 // needs it, here we try to resolve fields and methods used in class
717 // definitions, since many of them many never be referenced by
718 // generated code.
719 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
720 if (SkipClass(context->class_loader, dex_file, class_def)) {
721 return;
722 }
723
724 // Note the class_data pointer advances through the headers,
725 // static fields, instance fields, direct methods, and virtual
726 // methods.
727 const byte* class_data = dex_file.GetClassData(class_def);
728 if (class_data == NULL) {
729 // empty class such as a marker interface
730 return;
731 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800732 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800733 ClassLinker* class_linker = context->class_linker;
734 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
735 ClassDataItemIterator it(dex_file, class_data);
736 while (it.HasNextStaticField()) {
737 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
738 context->class_loader, true);
739 if (field == NULL) {
740 CHECK(self->IsExceptionPending());
741 self->ClearException();
742 }
743 it.Next();
744 }
745 while (it.HasNextInstanceField()) {
746 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
747 context->class_loader, false);
748 if (field == NULL) {
749 CHECK(self->IsExceptionPending());
750 self->ClearException();
751 }
752 it.Next();
753 }
754 while (it.HasNextDirectMethod()) {
755 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
756 context->class_loader, true);
757 if (method == NULL) {
758 CHECK(self->IsExceptionPending());
759 self->ClearException();
760 }
761 it.Next();
762 }
763 while (it.HasNextVirtualMethod()) {
764 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
765 context->class_loader, false);
766 if (method == NULL) {
767 CHECK(self->IsExceptionPending());
768 self->ClearException();
769 }
770 it.Next();
771 }
772 DCHECK(!it.HasNext());
773}
774
775static void ResolveType(Context* context, size_t type_idx) {
776 // Class derived values are more complicated, they require the linker and loader.
777 Thread* self = Thread::Current();
778 ClassLinker* class_linker = context->class_linker;
779 const DexFile& dex_file = *context->dex_file;
780 Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader);
781 if (klass == NULL) {
782 CHECK(self->IsExceptionPending());
783 Thread::Current()->ClearException();
784 }
785}
786
787void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700788 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700789 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700790
Brian Carlstromae826982011-11-09 01:33:42 -0800791 // Strings are easy in that they always are simply resolved to literals in the same file
792 if (image_ && image_classes_ == NULL) {
793 // TODO: Add support for loading strings referenced by image_classes_
794 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700795 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
796 class_linker->ResolveString(dex_file, string_idx, dex_cache);
797 }
Elliott Hughesff738062012-02-03 15:00:42 -0800798 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700799 }
800
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800801 Context context;
802 context.class_linker = class_linker;
803 context.class_loader = class_loader;
804 context.dex_cache = dex_cache;
805 context.dex_file = &dex_file;
806
Elliott Hughes5523ee02012-02-03 18:18:34 -0800807 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800808 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700809
Elliott Hughes5523ee02012-02-03 18:18:34 -0800810 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800811 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700812}
813
Brian Carlstromae826982011-11-09 01:33:42 -0800814void Compiler::Verify(const ClassLoader* class_loader,
815 const std::vector<const DexFile*>& dex_files) {
816 for (size_t i = 0; i != dex_files.size(); ++i) {
817 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700818 CHECK(dex_file != NULL);
819 VerifyDexFile(class_loader, *dex_file);
820 }
821}
822
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800823static void VerifyClass(Context* context, size_t class_def_index) {
824 const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index);
825 const char* descriptor = context->dex_file->GetClassDescriptor(class_def);
826 Class* klass = context->class_linker->FindClass(descriptor, context->class_loader);
827 if (klass == NULL) {
828 Thread* self = Thread::Current();
829 CHECK(self->IsExceptionPending());
830 self->ClearException();
831 return;
832 }
833 CHECK(klass->IsResolved()) << PrettyClass(klass);
834 context->class_linker->VerifyClass(klass);
835
836 if (klass->IsErroneous()) {
837 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
838 CHECK(Thread::Current()->IsExceptionPending());
839 Thread::Current()->ClearException();
840 // We want to try verification again at run-time, so move back into the resolved state.
841 klass->SetStatus(Class::kStatusResolved);
842 }
843
844 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
845 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
846}
847
jeffhao98eacac2011-09-14 16:11:53 -0700848void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700849 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700850
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800851 Context context;
852 context.class_linker = Runtime::Current()->GetClassLinker();
853 context.class_loader = class_loader;
854 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800855 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700856
jeffhaob4df5142011-09-19 20:25:32 -0700857 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700858}
859
Brian Carlstromae826982011-11-09 01:33:42 -0800860void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
861 const std::vector<const DexFile*>& dex_files) {
862 for (size_t i = 0; i != dex_files.size(); ++i) {
863 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700864 CHECK(dex_file != NULL);
865 InitializeClassesWithoutClinit(class_loader, *dex_file);
866 }
867}
868
869void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
870 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700871 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
872 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700873 const char* descriptor = dex_file.GetClassDescriptor(class_def);
874 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700875 if (klass != NULL) {
876 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800877 // record the final class status if necessary
878 Class::Status status = klass->GetStatus();
879 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800880 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800881 CompiledClass* compiled_class = GetCompiledClass(ref);
882 if (compiled_class == NULL) {
883 compiled_class = new CompiledClass(status);
884 compiled_classes_[ref] = compiled_class;
885 } else {
886 DCHECK_EQ(status, compiled_class->GetStatus());
887 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700888 }
889 // clear any class not found or verification exceptions
890 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700891 }
892
893 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
894 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
895 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700896 if (klass == NULL) {
897 Thread::Current()->ClearException();
898 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700899 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
900 }
jeffhao98eacac2011-09-14 16:11:53 -0700901 }
902}
903
Brian Carlstromae826982011-11-09 01:33:42 -0800904void Compiler::Compile(const ClassLoader* class_loader,
905 const std::vector<const DexFile*>& dex_files) {
906 for (size_t i = 0; i != dex_files.size(); ++i) {
907 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700908 CHECK(dex_file != NULL);
909 CompileDexFile(class_loader, *dex_file);
910 }
911}
912
Elliott Hughesc225caa2012-02-03 15:43:37 -0800913void Compiler::CompileClass(Context* context, size_t class_def_index) {
914 const ClassLoader* class_loader = context->class_loader;
915 const DexFile& dex_file = *context->dex_file;
916 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800917 if (SkipClass(class_loader, dex_file, class_def)) {
918 return;
919 }
Ian Rogers0571d352011-11-03 19:51:38 -0700920 const byte* class_data = dex_file.GetClassData(class_def);
921 if (class_data == NULL) {
922 // empty class, probably a marker interface
923 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700924 }
Ian Rogers0571d352011-11-03 19:51:38 -0700925 ClassDataItemIterator it(dex_file, class_data);
926 // Skip fields
927 while (it.HasNextStaticField()) {
928 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700929 }
Ian Rogers0571d352011-11-03 19:51:38 -0700930 while (it.HasNextInstanceField()) {
931 it.Next();
932 }
933 // Compile direct methods
934 while (it.HasNextDirectMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800935 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
936 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700937 it.Next();
938 }
939 // Compile virtual methods
940 while (it.HasNextVirtualMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800941 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
942 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700943 it.Next();
944 }
945 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700946}
947
Elliott Hughesc225caa2012-02-03 15:43:37 -0800948void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
949 Context context;
950 context.class_loader = class_loader;
951 context.compiler = this;
952 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800953 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800954}
955
Ian Rogersa3760aa2011-11-14 14:32:37 -0800956void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
957 uint32_t method_idx, const ClassLoader* class_loader,
958 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700959 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800960 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800961 if ((access_flags & kAccNative) != 0) {
962 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700963 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800964 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700965 } else {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800966#if defined(ART_USE_LLVM_COMPILER)
967 compiled_method =
968 compiler_llvm_->CompileDexMethod(code_item, access_flags, method_idx,
969 class_loader, dex_file);
970#else
Ian Rogersa3760aa2011-11-14 14:32:37 -0800971 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
972 dex_file, kThumb2);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800973#endif
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800974 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
975 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800976 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800977 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800978 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800979 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700980 }
981
982 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700983 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800984 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800985 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700986 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800987 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700988 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700989
Ian Rogers0571d352011-11-03 19:51:38 -0700990 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800991 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700992 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
993 if (compiled_invoke_stub == NULL) {
Logan Chienf04364f2012-02-10 12:01:39 +0800994#if defined(ART_USE_LLVM_COMPILER)
995 compiled_invoke_stub = compiler_llvm_->CreateInvokeStub(is_static, shorty);
996#else
Ian Rogers0571d352011-11-03 19:51:38 -0700997 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800998 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700999 } else {
1000 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
1001 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001002 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -07001003 }
Logan Chienf04364f2012-02-10 12:01:39 +08001004#endif
1005
Ian Rogers0571d352011-11-03 19:51:38 -07001006 CHECK(compiled_invoke_stub != NULL);
1007 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001008 }
Ian Rogers0571d352011-11-03 19:51:38 -07001009 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001010}
1011
Ian Rogers0571d352011-11-03 19:51:38 -07001012static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1013 std::string key(shorty);
1014 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001015 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001016 }
Ian Rogers0571d352011-11-03 19:51:38 -07001017 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001018}
1019
Ian Rogers0571d352011-11-03 19:51:38 -07001020const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001021 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001022 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001023 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001024 if (it == compiled_invoke_stubs_.end()) {
1025 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001026 } else {
1027 DCHECK(it->second != NULL);
1028 return it->second;
1029 }
1030}
1031
1032void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
1033 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001034 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001035 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001036 compiled_invoke_stubs_[key] = compiled_invoke_stub;
1037}
1038
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001039CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001040 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001041 ClassTable::const_iterator it = compiled_classes_.find(ref);
1042 if (it == compiled_classes_.end()) {
1043 return NULL;
1044 }
1045 CHECK(it->second != NULL);
1046 return it->second;
1047}
1048
Ian Rogers0571d352011-11-03 19:51:38 -07001049CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001050 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001051 MethodTable::const_iterator it = compiled_methods_.find(ref);
1052 if (it == compiled_methods_.end()) {
1053 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001054 }
1055 CHECK(it->second != NULL);
1056 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001057}
1058
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001059void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
1060 for (size_t i = 0; i != dex_files.size(); ++i) {
1061 const DexFile* dex_file = dex_files[i];
1062 CHECK(dex_file != NULL);
1063 SetGcMapsDexFile(class_loader, *dex_file);
1064 }
1065}
1066
1067void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
1068 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1069 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1070 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1071 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1072 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1073 Class* klass = class_linker->FindClass(descriptor, class_loader);
1074 if (klass == NULL || !klass->IsVerified()) {
1075 Thread::Current()->ClearException();
1076 continue;
1077 }
1078 const byte* class_data = dex_file.GetClassData(class_def);
1079 if (class_data == NULL) {
1080 // empty class such as a marker interface
1081 continue;
1082 }
1083 ClassDataItemIterator it(dex_file, class_data);
1084 while (it.HasNextStaticField()) {
1085 it.Next();
1086 }
1087 while (it.HasNextInstanceField()) {
1088 it.Next();
1089 }
1090 while (it.HasNextDirectMethod()) {
1091 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1092 class_loader, true);
1093 SetGcMapsMethod(dex_file, method);
1094 it.Next();
1095 }
1096 while (it.HasNextVirtualMethod()) {
1097 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1098 class_loader, false);
1099 SetGcMapsMethod(dex_file, method);
1100 it.Next();
1101 }
1102 }
1103}
1104
Ian Rogers1bddec32012-02-04 12:27:34 -08001105namespace verifier {
1106 class DexVerifier {
1107 public:
1108 static const std::vector<uint8_t>* GetGcMap(Compiler::MethodReference ref);
1109 };
1110}
1111
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001112void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
1113 if (method == NULL) {
1114 Thread::Current()->ClearException();
1115 return;
1116 }
1117 uint16_t method_idx = method->GetDexMethodIndex();
1118 MethodReference ref(&dex_file, method_idx);
1119 CompiledMethod* compiled_method = GetCompiledMethod(ref);
1120 if (compiled_method == NULL) {
1121 return;
1122 }
1123 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
1124 if (gc_map == NULL) {
1125 return;
1126 }
1127 compiled_method->SetGcMap(*gc_map);
1128}
1129
Brian Carlstromae826982011-11-09 01:33:42 -08001130void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
1131 for (size_t i = 0; i != dex_files.size(); ++i) {
1132 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07001133 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -07001134 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -07001135 }
1136}
1137
Brian Carlstrom8a487412011-08-29 20:08:52 -07001138void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -07001139 Runtime* runtime = Runtime::Current();
1140 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -07001141 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001142 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001143 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -07001144 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001145 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -07001146 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
1147 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001148 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001149 } else {
1150 // TODO: we currently leave the entry blank for resolved
1151 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -07001152 }
1153 }
1154}
1155
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001156} // namespace art