blob: 9a49cdd6ec74204fe4d570fb3e1dabbb66b591ea [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"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070027#include "dex_cache.h"
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070028#include "jni_compiler.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070029#include "jni_internal.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080030#include "oat_compilation_unit.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 Rogers45619fc2012-02-29 11:15:25 -080052 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty, uint32_t shorty_len);
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 Rogers45619fc2012-02-29 11:15:25 -080058 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty, uint32_t shorty_len);
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}
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
Logan Chien4dd96f52012-02-29 01:26:58 +0800441static Class* ComputeReferrerClass(OatCompilationUnit* mUnit) {
442 const DexFile::MethodId& referrer_method_id =
443 mUnit->dex_file_->GetMethodId(mUnit->method_idx_);
444
445 return mUnit->class_linker_->ResolveType(
446 *mUnit->dex_file_, referrer_method_id.class_idx_,
447 mUnit->dex_cache_, mUnit->class_loader_);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800448}
449
Logan Chien4dd96f52012-02-29 01:26:58 +0800450static Field* ComputeReferrerField(OatCompilationUnit* mUnit, uint32_t field_idx) {
451 return mUnit->class_linker_->ResolveField(
452 *mUnit->dex_file_, field_idx, mUnit->dex_cache_,
453 mUnit->class_loader_, false);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800454}
455
Logan Chien4dd96f52012-02-29 01:26:58 +0800456static Method* ComputeReferrerMethod(OatCompilationUnit* mUnit, uint32_t method_idx) {
457 return mUnit->class_linker_->ResolveMethod(
458 *mUnit->dex_file_, method_idx, mUnit->dex_cache_,
459 mUnit->class_loader_, true);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800460}
461
Logan Chien4dd96f52012-02-29 01:26:58 +0800462bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
jeffhao8cd6dda2012-02-22 10:15:34 -0800463 int& field_offset, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800464 // Conservative defaults
465 field_offset = -1;
466 is_volatile = true;
467 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800468 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800469 if (resolved_field != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800470 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800471 // Try to resolve referring class then access check, failure to pass the
472 Class* fields_class = resolved_field->GetDeclaringClass();
jeffhao8cd6dda2012-02-22 10:15:34 -0800473 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
474 fields_class != referrer_class;
475 if (referrer_class != NULL && referrer_class->CanAccess(fields_class) &&
476 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
477 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800478 field_offset = resolved_field->GetOffset().Int32Value();
479 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800480 stats_->ResolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800481 return true; // Fast path.
482 }
483 }
484 // Clean up any exception left by field/type resolution
485 Thread* thread = Thread::Current();
486 if (thread->IsExceptionPending()) {
487 thread->ClearException();
488 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800489 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800490 return false; // Incomplete knowledge needs slow path.
491}
492
Logan Chien4dd96f52012-02-29 01:26:58 +0800493bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
Ian Rogers1bddec32012-02-04 12:27:34 -0800494 int& field_offset, int& ssb_index,
jeffhao8cd6dda2012-02-22 10:15:34 -0800495 bool& is_referrers_class, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800496 // Conservative defaults
497 field_offset = -1;
498 ssb_index = -1;
499 is_referrers_class = false;
500 is_volatile = true;
501 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800502 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800503 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800504 DCHECK(resolved_field->IsStatic());
Logan Chien4dd96f52012-02-29 01:26:58 +0800505 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800506 if (referrer_class != NULL) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800507 Class* fields_class = resolved_field->GetDeclaringClass();
508 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800509 is_referrers_class = true; // implies no worrying about class initialization
510 field_offset = resolved_field->GetOffset().Int32Value();
511 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800512 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800513 return true; // fast path
514 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -0800515 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogers1bddec32012-02-04 12:27:34 -0800516 if (referrer_class->CanAccess(fields_class) &&
jeffhao8cd6dda2012-02-22 10:15:34 -0800517 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
518 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800519 // We have the resolved field, we must make it into a ssbIndex for the referrer
520 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800521 // TODO: for images we can elide the static storage base null check
522 // if we know there's a non-null entry in the image
Logan Chien4dd96f52012-02-29 01:26:58 +0800523 if (fields_class->GetDexCache() == mUnit->dex_cache_) {
Ian Rogers4103ad22012-02-06 09:18:25 -0800524 // common case where the dex cache of both the referrer and the field are the same,
525 // no need to search the dex file
526 ssb_index = fields_class->GetDexTypeIndex();
527 field_offset = resolved_field->GetOffset().Int32Value();
528 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800529 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800530 return true;
531 }
532 // Search dex file for localized ssb index
Ian Rogers1bddec32012-02-04 12:27:34 -0800533 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
534 const DexFile::StringId* string_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800535 mUnit->dex_file_->FindStringId(descriptor);
Ian Rogers1bddec32012-02-04 12:27:34 -0800536 if (string_id != NULL) {
537 const DexFile::TypeId* type_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800538 mUnit->dex_file_->FindTypeId(mUnit->dex_file_->GetIndexForStringId(*string_id));
Ian Rogers1bddec32012-02-04 12:27:34 -0800539 if(type_id != NULL) {
540 // medium path, needs check of static storage base being initialized
Logan Chien4dd96f52012-02-29 01:26:58 +0800541 ssb_index = mUnit->dex_file_->GetIndexForTypeId(*type_id);
Ian Rogers1bddec32012-02-04 12:27:34 -0800542 field_offset = resolved_field->GetOffset().Int32Value();
543 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800544 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800545 return true;
546 }
547 }
548 }
549 }
550 }
551 }
552 // Clean up any exception left by field/type resolution
553 Thread* thread = Thread::Current();
554 if (thread->IsExceptionPending()) {
555 thread->ClearException();
556 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800557 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800558 return false; // Incomplete knowledge needs slow path.
559}
560
Logan Chien4dd96f52012-02-29 01:26:58 +0800561bool Compiler::ComputeInvokeInfo(uint32_t method_idx, OatCompilationUnit* mUnit, InvokeType type,
Ian Rogers996cc582012-02-14 22:23:29 -0800562 int& vtable_idx) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800563 vtable_idx = -1;
Logan Chien4dd96f52012-02-29 01:26:58 +0800564 Method* resolved_method = ComputeReferrerMethod(mUnit, method_idx);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800565 if (resolved_method != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800566 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800567 if (referrer_class != NULL) {
568 Class* methods_class = resolved_method->GetDeclaringClass();
569 if (!referrer_class->CanAccess(methods_class) ||
570 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800571 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800572 // The referring class can't access the resolved method, this may occur as a result of a
573 // protected method being made public by implementing an interface that re-declares the
574 // method public. Resort to the dex file to determine the correct class for the access check
Logan Chien4dd96f52012-02-29 01:26:58 +0800575 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800576 methods_class =
Logan Chien4dd96f52012-02-29 01:26:58 +0800577 mUnit->class_linker_->ResolveType(dex_file,
578 dex_file.GetMethodId(method_idx).class_idx_,
579 referrer_class);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800580
581 }
582 if (referrer_class->CanAccess(methods_class) &&
583 referrer_class->CanAccessMember(methods_class,
584 resolved_method->GetAccessFlags())) {
585 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800586 if (type != kSuper) {
587 // nothing left to do for static/direct/virtual/interface dispatch
588 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800589 return true;
590 } else {
591 // ensure the vtable index will be correct to dispatch in the vtable of the super class
Ian Rogers996cc582012-02-14 22:23:29 -0800592 if (referrer_class->IsSubClass(methods_class) &&
593 vtable_idx < methods_class->GetVTable()->GetLength()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800594 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800595 return true;
596 }
597 }
598 }
599 }
600 }
601 // Clean up any exception left by method/type resolution
602 Thread* thread = Thread::Current();
603 if (thread->IsExceptionPending()) {
604 thread->ClearException();
605 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800606 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800607 return false; // Incomplete knowledge needs slow path.
608}
609
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800610// Return true if the class should be skipped during compilation. We
611// never skip classes in the boot class loader. However, if we have a
612// non-boot class loader and we can resolve the class in the boot
613// class loader, we do skip the class. This happens if an app bundles
614// classes found in the boot classpath. Since at runtime we will
615// select the class from the boot classpath, do not attempt to resolve
616// or compile it now.
617static bool SkipClass(const ClassLoader* class_loader,
618 const DexFile& dex_file,
619 const DexFile::ClassDef& class_def) {
620 if (class_loader == NULL) {
621 return false;
622 }
623 const char* descriptor = dex_file.GetClassDescriptor(class_def);
624 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
625 Class* klass = class_linker->FindClass(descriptor, NULL);
626 if (klass == NULL) {
627 Thread* self = Thread::Current();
628 CHECK(self->IsExceptionPending());
629 self->ClearException();
630 return false;
631 }
632 return true;
633}
634
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800635struct Context {
636 ClassLinker* class_linker;
637 const ClassLoader* class_loader;
Elliott Hughesc225caa2012-02-03 15:43:37 -0800638 Compiler* compiler;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800639 DexCache* dex_cache;
640 const DexFile* dex_file;
641};
642
643typedef void Callback(Context* context, size_t index);
644
645class WorkerThread {
646 public:
Elliott Hughes1e409252012-02-06 11:21:27 -0800647 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
648 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
649 if (spawn_) {
650 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread");
651 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800652 }
653
654 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800655 if (spawn_) {
656 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
657 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800658 }
659
660 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800661 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800662 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
663 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800664 if (worker->spawn_) {
665 runtime->AttachCurrentThread("Compiler Worker", true);
666 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800667 Thread::Current()->SetState(Thread::kRunnable);
668 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800669 if (worker->spawn_) {
670 Thread::Current()->SetState(Thread::kNative);
671 runtime->DetachCurrentThread();
672 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800673 return NULL;
674 }
675
Elliott Hughes1e409252012-02-06 11:21:27 -0800676 void Go() {
677 Go(this);
678 }
679
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800680 void Run() {
681 for (size_t i = begin_; i < end_; i += stripe_) {
682 callback_(context_, i);
683 }
684 }
685
686 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800687 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800688
689 Context* context_;
690 size_t begin_;
691 size_t end_;
692 Callback* callback_;
693 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800694
695 friend void ForAll(Context*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800696};
697
Elliott Hughes5523ee02012-02-03 18:18:34 -0800698void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800699 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -0800700
Elliott Hughes1e409252012-02-06 11:21:27 -0800701 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -0800702 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800703 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800704 }
Elliott Hughes1e409252012-02-06 11:21:27 -0800705 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800706
Elliott Hughes81d91512012-02-03 16:38:43 -0800707 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
708 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
709 STLDeleteElements(&threads);
710}
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800711
712static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
713 const DexFile& dex_file = *context->dex_file;
714
715 // Method and Field are the worst. We can't resolve without either
716 // context from the code use (to disambiguate virtual vs direct
717 // method and instance vs static field) or from class
718 // definitions. While the compiler will resolve what it can as it
719 // needs it, here we try to resolve fields and methods used in class
720 // definitions, since many of them many never be referenced by
721 // generated code.
722 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
723 if (SkipClass(context->class_loader, dex_file, class_def)) {
724 return;
725 }
726
727 // Note the class_data pointer advances through the headers,
728 // static fields, instance fields, direct methods, and virtual
729 // methods.
730 const byte* class_data = dex_file.GetClassData(class_def);
731 if (class_data == NULL) {
732 // empty class such as a marker interface
733 return;
734 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800735 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800736 ClassLinker* class_linker = context->class_linker;
737 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
738 ClassDataItemIterator it(dex_file, class_data);
739 while (it.HasNextStaticField()) {
740 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
741 context->class_loader, true);
742 if (field == NULL) {
743 CHECK(self->IsExceptionPending());
744 self->ClearException();
745 }
746 it.Next();
747 }
748 while (it.HasNextInstanceField()) {
749 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
750 context->class_loader, false);
751 if (field == NULL) {
752 CHECK(self->IsExceptionPending());
753 self->ClearException();
754 }
755 it.Next();
756 }
757 while (it.HasNextDirectMethod()) {
758 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
759 context->class_loader, true);
760 if (method == NULL) {
761 CHECK(self->IsExceptionPending());
762 self->ClearException();
763 }
764 it.Next();
765 }
766 while (it.HasNextVirtualMethod()) {
767 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
768 context->class_loader, false);
769 if (method == NULL) {
770 CHECK(self->IsExceptionPending());
771 self->ClearException();
772 }
773 it.Next();
774 }
775 DCHECK(!it.HasNext());
776}
777
778static void ResolveType(Context* context, size_t type_idx) {
779 // Class derived values are more complicated, they require the linker and loader.
780 Thread* self = Thread::Current();
781 ClassLinker* class_linker = context->class_linker;
782 const DexFile& dex_file = *context->dex_file;
783 Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader);
784 if (klass == NULL) {
785 CHECK(self->IsExceptionPending());
786 Thread::Current()->ClearException();
787 }
788}
789
790void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700791 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700792 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700793
Brian Carlstromae826982011-11-09 01:33:42 -0800794 // Strings are easy in that they always are simply resolved to literals in the same file
795 if (image_ && image_classes_ == NULL) {
796 // TODO: Add support for loading strings referenced by image_classes_
797 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700798 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
799 class_linker->ResolveString(dex_file, string_idx, dex_cache);
800 }
Elliott Hughesff738062012-02-03 15:00:42 -0800801 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700802 }
803
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800804 Context context;
805 context.class_linker = class_linker;
806 context.class_loader = class_loader;
807 context.dex_cache = dex_cache;
808 context.dex_file = &dex_file;
809
Elliott Hughes5523ee02012-02-03 18:18:34 -0800810 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800811 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700812
Elliott Hughes5523ee02012-02-03 18:18:34 -0800813 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800814 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700815}
816
Brian Carlstromae826982011-11-09 01:33:42 -0800817void Compiler::Verify(const ClassLoader* class_loader,
818 const std::vector<const DexFile*>& dex_files) {
819 for (size_t i = 0; i != dex_files.size(); ++i) {
820 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700821 CHECK(dex_file != NULL);
822 VerifyDexFile(class_loader, *dex_file);
823 }
824}
825
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800826static void VerifyClass(Context* context, size_t class_def_index) {
827 const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index);
828 const char* descriptor = context->dex_file->GetClassDescriptor(class_def);
829 Class* klass = context->class_linker->FindClass(descriptor, context->class_loader);
830 if (klass == NULL) {
831 Thread* self = Thread::Current();
832 CHECK(self->IsExceptionPending());
833 self->ClearException();
834 return;
835 }
836 CHECK(klass->IsResolved()) << PrettyClass(klass);
837 context->class_linker->VerifyClass(klass);
838
839 if (klass->IsErroneous()) {
840 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
841 CHECK(Thread::Current()->IsExceptionPending());
842 Thread::Current()->ClearException();
843 // We want to try verification again at run-time, so move back into the resolved state.
844 klass->SetStatus(Class::kStatusResolved);
845 }
846
847 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
848 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
849}
850
jeffhao98eacac2011-09-14 16:11:53 -0700851void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700852 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700853
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800854 Context context;
855 context.class_linker = Runtime::Current()->GetClassLinker();
856 context.class_loader = class_loader;
857 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800858 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700859
jeffhaob4df5142011-09-19 20:25:32 -0700860 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700861}
862
Brian Carlstromae826982011-11-09 01:33:42 -0800863void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
864 const std::vector<const DexFile*>& dex_files) {
865 for (size_t i = 0; i != dex_files.size(); ++i) {
866 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700867 CHECK(dex_file != NULL);
868 InitializeClassesWithoutClinit(class_loader, *dex_file);
869 }
870}
871
872void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
873 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700874 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
875 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700876 const char* descriptor = dex_file.GetClassDescriptor(class_def);
877 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700878 if (klass != NULL) {
879 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800880 // record the final class status if necessary
881 Class::Status status = klass->GetStatus();
882 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800883 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800884 CompiledClass* compiled_class = GetCompiledClass(ref);
885 if (compiled_class == NULL) {
886 compiled_class = new CompiledClass(status);
887 compiled_classes_[ref] = compiled_class;
888 } else {
889 DCHECK_EQ(status, compiled_class->GetStatus());
890 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700891 }
892 // clear any class not found or verification exceptions
893 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700894 }
895
896 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
897 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
898 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700899 if (klass == NULL) {
900 Thread::Current()->ClearException();
901 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700902 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
903 }
jeffhao98eacac2011-09-14 16:11:53 -0700904 }
905}
906
Brian Carlstromae826982011-11-09 01:33:42 -0800907void Compiler::Compile(const ClassLoader* class_loader,
908 const std::vector<const DexFile*>& dex_files) {
909 for (size_t i = 0; i != dex_files.size(); ++i) {
910 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700911 CHECK(dex_file != NULL);
912 CompileDexFile(class_loader, *dex_file);
913 }
914}
915
Elliott Hughesc225caa2012-02-03 15:43:37 -0800916void Compiler::CompileClass(Context* context, size_t class_def_index) {
917 const ClassLoader* class_loader = context->class_loader;
918 const DexFile& dex_file = *context->dex_file;
919 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800920 if (SkipClass(class_loader, dex_file, class_def)) {
921 return;
922 }
Ian Rogers0571d352011-11-03 19:51:38 -0700923 const byte* class_data = dex_file.GetClassData(class_def);
924 if (class_data == NULL) {
925 // empty class, probably a marker interface
926 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700927 }
Ian Rogers0571d352011-11-03 19:51:38 -0700928 ClassDataItemIterator it(dex_file, class_data);
929 // Skip fields
930 while (it.HasNextStaticField()) {
931 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700932 }
Ian Rogers0571d352011-11-03 19:51:38 -0700933 while (it.HasNextInstanceField()) {
934 it.Next();
935 }
936 // Compile direct methods
937 while (it.HasNextDirectMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800938 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
939 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700940 it.Next();
941 }
942 // Compile virtual methods
943 while (it.HasNextVirtualMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800944 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
945 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700946 it.Next();
947 }
948 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700949}
950
Elliott Hughesc225caa2012-02-03 15:43:37 -0800951void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
952 Context context;
953 context.class_loader = class_loader;
954 context.compiler = this;
955 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800956 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800957}
958
Ian Rogersa3760aa2011-11-14 14:32:37 -0800959void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
960 uint32_t method_idx, const ClassLoader* class_loader,
961 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700962 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800963 uint64_t start_ns = NanoTime();
Logan Chien4dd96f52012-02-29 01:26:58 +0800964
965#if defined(ART_USE_LLVM_COMPILER)
966 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
967 DexCache *dex_cache = class_linker->FindDexCache(dex_file);
968
969 UniquePtr<OatCompilationUnit> oat_compilation_unit(
970 new OatCompilationUnit(class_loader, class_linker, dex_file, *dex_cache, code_item,
971 method_idx, access_flags));
972#endif
973
Ian Rogers169c9a72011-11-13 20:13:17 -0800974 if ((access_flags & kAccNative) != 0) {
975 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700976 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800977 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700978 } else {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800979#if defined(ART_USE_LLVM_COMPILER)
Logan Chien4dd96f52012-02-29 01:26:58 +0800980 compiled_method = compiler_llvm_->CompileDexMethod(oat_compilation_unit.get());
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800981#else
Ian Rogersa3760aa2011-11-14 14:32:37 -0800982 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
983 dex_file, kThumb2);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800984#endif
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800985 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
986 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800987 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800988 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800989 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800990 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700991 }
992
993 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700994 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800995 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800996 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700997 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800998 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700999 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001000
Ian Rogers45619fc2012-02-29 11:15:25 -08001001 uint32_t shorty_len;
1002 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx), &shorty_len);
Ian Rogers169c9a72011-11-13 20:13:17 -08001003 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001004 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
1005 if (compiled_invoke_stub == NULL) {
Logan Chienf04364f2012-02-10 12:01:39 +08001006#if defined(ART_USE_LLVM_COMPILER)
1007 compiled_invoke_stub = compiler_llvm_->CreateInvokeStub(is_static, shorty);
1008#else
Ian Rogers0571d352011-11-03 19:51:38 -07001009 if (instruction_set_ == kX86) {
Ian Rogers45619fc2012-02-29 11:15:25 -08001010 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty, shorty_len);
Ian Rogers0571d352011-11-03 19:51:38 -07001011 } else {
1012 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
1013 // Generates invocation stub using ARM instruction set
Ian Rogers45619fc2012-02-29 11:15:25 -08001014 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty, shorty_len);
Ian Rogers0571d352011-11-03 19:51:38 -07001015 }
Logan Chienf04364f2012-02-10 12:01:39 +08001016#endif
1017
Ian Rogers0571d352011-11-03 19:51:38 -07001018 CHECK(compiled_invoke_stub != NULL);
1019 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001020 }
Ian Rogers0571d352011-11-03 19:51:38 -07001021 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001022}
1023
Ian Rogers0571d352011-11-03 19:51:38 -07001024static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1025 std::string key(shorty);
1026 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001027 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001028 }
Ian Rogers0571d352011-11-03 19:51:38 -07001029 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001030}
1031
Ian Rogers0571d352011-11-03 19:51:38 -07001032const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001033 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001034 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001035 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001036 if (it == compiled_invoke_stubs_.end()) {
1037 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001038 } else {
1039 DCHECK(it->second != NULL);
1040 return it->second;
1041 }
1042}
1043
1044void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
1045 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001046 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001047 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001048 compiled_invoke_stubs_[key] = compiled_invoke_stub;
1049}
1050
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001051CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001052 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001053 ClassTable::const_iterator it = compiled_classes_.find(ref);
1054 if (it == compiled_classes_.end()) {
1055 return NULL;
1056 }
1057 CHECK(it->second != NULL);
1058 return it->second;
1059}
1060
Ian Rogers0571d352011-11-03 19:51:38 -07001061CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001062 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001063 MethodTable::const_iterator it = compiled_methods_.find(ref);
1064 if (it == compiled_methods_.end()) {
1065 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001066 }
1067 CHECK(it->second != NULL);
1068 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001069}
1070
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001071void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
1072 for (size_t i = 0; i != dex_files.size(); ++i) {
1073 const DexFile* dex_file = dex_files[i];
1074 CHECK(dex_file != NULL);
1075 SetGcMapsDexFile(class_loader, *dex_file);
1076 }
1077}
1078
1079void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
1080 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1081 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1082 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1083 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1084 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1085 Class* klass = class_linker->FindClass(descriptor, class_loader);
1086 if (klass == NULL || !klass->IsVerified()) {
1087 Thread::Current()->ClearException();
1088 continue;
1089 }
1090 const byte* class_data = dex_file.GetClassData(class_def);
1091 if (class_data == NULL) {
1092 // empty class such as a marker interface
1093 continue;
1094 }
1095 ClassDataItemIterator it(dex_file, class_data);
1096 while (it.HasNextStaticField()) {
1097 it.Next();
1098 }
1099 while (it.HasNextInstanceField()) {
1100 it.Next();
1101 }
1102 while (it.HasNextDirectMethod()) {
1103 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1104 class_loader, true);
1105 SetGcMapsMethod(dex_file, method);
1106 it.Next();
1107 }
1108 while (it.HasNextVirtualMethod()) {
1109 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1110 class_loader, false);
1111 SetGcMapsMethod(dex_file, method);
1112 it.Next();
1113 }
1114 }
1115}
1116
Ian Rogers1bddec32012-02-04 12:27:34 -08001117namespace verifier {
1118 class DexVerifier {
1119 public:
1120 static const std::vector<uint8_t>* GetGcMap(Compiler::MethodReference ref);
1121 };
1122}
1123
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001124void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
1125 if (method == NULL) {
1126 Thread::Current()->ClearException();
1127 return;
1128 }
1129 uint16_t method_idx = method->GetDexMethodIndex();
1130 MethodReference ref(&dex_file, method_idx);
1131 CompiledMethod* compiled_method = GetCompiledMethod(ref);
1132 if (compiled_method == NULL) {
1133 return;
1134 }
1135 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
1136 if (gc_map == NULL) {
1137 return;
1138 }
1139 compiled_method->SetGcMap(*gc_map);
1140}
1141
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001142} // namespace art