blob: b1a7066bfe94d0af7b1a94bcf750132889ffe256 [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
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070037namespace art {
38
Ian Rogers996cc582012-02-14 22:23:29 -080039CompiledMethod* oatCompileMethod(Compiler& compiler, const DexFile::CodeItem* code_item,
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080040 uint32_t access_flags, uint32_t method_idx,
41 const ClassLoader* class_loader,
42 const DexFile& dex_file, InstructionSet);
43
Shih-wei Liaoc486c112011-09-13 16:43:52 -070044namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070045 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070046 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070047 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080048 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070049}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070050namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070051 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070052 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070053 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080054 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070055}
56
Ian Rogers996cc582012-02-14 22:23:29 -080057static double Percentage(size_t x, size_t y) {
58 return 100.0 * ((double)x) / ((double)(x + y));
59}
60
61static void DumpStat(size_t x, size_t y, const char* str) {
62 if (x == 0 && y == 0) {
63 return;
64 }
65 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
66}
67
68void AOTCompilationStats::Dump() {
69 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
70 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
71 DumpStat(resolved_types_, unresolved_types_, "types resolved");
72 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
73 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
74 "static fields resolved");
75 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
76 "static fields local to a class");
77 DumpStat(resolved_virtual_methods_, unresolved_virtual_methods_, "resolved virtual methods");
78 DumpStat(resolved_super_methods_, unresolved_super_methods_, "resolved super-class methods");
79 DumpStat(resolved_interface_methods_, unresolved_interface_methods_, "resolved interface methods");
80}
81
82// Allow lossy statistics in non-debug builds
83#ifndef NDEBUG
84#define STATS_LOCK() MutexLock mu(stats_lock_)
85#else
86#define STATS_LOCK()
87#endif
88
89void AOTCompilationStats::TypeInDexCache() {
90 STATS_LOCK();
91 types_in_dex_cache_++;
92}
93
94void AOTCompilationStats::TypeNotInDexCache() {
95 STATS_LOCK();
96 types_not_in_dex_cache_++;
97}
98
99void AOTCompilationStats::StringInDexCache() {
100 STATS_LOCK();
101 strings_in_dex_cache_++;
102}
103
104void AOTCompilationStats::StringNotInDexCache() {
105 STATS_LOCK();
106 strings_not_in_dex_cache_++;
107}
108
109void AOTCompilationStats::TypeDoesntNeedAccessCheck() {
110 STATS_LOCK();
111 resolved_types_++;
112}
113
114void AOTCompilationStats::TypeNeedsAccessCheck() {
115 STATS_LOCK();
116 unresolved_types_++;
117}
118
119void AOTCompilationStats::ResolvedInstanceField() {
120 STATS_LOCK();
121 resolved_instance_fields_++;
122}
123
124void AOTCompilationStats::UnresolvedInstanceField(){
125 STATS_LOCK();
126 unresolved_instance_fields_++;
127}
128
129void AOTCompilationStats::ResolvedLocalStaticField() {
130 STATS_LOCK();
131 resolved_local_static_fields_++;
132}
133
134void AOTCompilationStats::ResolvedStaticField() {
135 STATS_LOCK();
136 resolved_static_fields_++;
137}
138
139void AOTCompilationStats::UnresolvedStaticField() {
140 STATS_LOCK();
141 unresolved_static_fields_++;
142}
143
144void AOTCompilationStats::ResolvedMethod(bool is_interface, bool is_super) {
145 STATS_LOCK();
146 if (is_interface) {
147 resolved_interface_methods_++;
148 } else if (is_super) {
149 resolved_super_methods_++;
150 } else {
151 resolved_virtual_methods_++;
152 }
153}
154
155void AOTCompilationStats::UnresolvedMethod(bool is_interface, bool is_super) {
156 STATS_LOCK();
157 if (is_interface) {
158 unresolved_interface_methods_++;
159 } else if (is_super) {
160 unresolved_super_methods_++;
161 } else {
162 unresolved_virtual_methods_++;
163 }
164}
165
Elliott Hughes5523ee02012-02-03 18:18:34 -0800166Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count,
Brian Carlstromae826982011-11-09 01:33:42 -0800167 const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -0700168 : instruction_set_(instruction_set),
169 jni_compiler_(instruction_set),
Elliott Hughesc225caa2012-02-03 15:43:37 -0800170 compiled_classes_lock_("compiled classes lock"),
171 compiled_methods_lock_("compiled method lock"),
172 compiled_invoke_stubs_lock_("compiled invoke stubs lock"),
Brian Carlstromaded5f72011-10-07 17:15:04 -0700173 image_(image),
Elliott Hughes5523ee02012-02-03 18:18:34 -0800174 thread_count_(thread_count),
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800175 image_classes_(image_classes) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700176 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800177 if (!image_) {
178 CHECK(image_classes_ == NULL);
179 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700180}
181
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700182Compiler::~Compiler() {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800183 {
184 MutexLock mu(compiled_classes_lock_);
185 STLDeleteValues(&compiled_classes_);
186 }
187 {
188 MutexLock mu(compiled_methods_lock_);
189 STLDeleteValues(&compiled_methods_);
190 }
191 {
192 MutexLock mu(compiled_invoke_stubs_lock_);
193 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800194 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700195}
196
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700197ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
198 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700199 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700200 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700201 } else {
202 CHECK(instruction_set == kArm || instruction_set == kThumb2);
203 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700204 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700205 }
206}
207
Elliott Hughes8add92d2012-01-18 18:18:43 -0800208ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800209 switch (instruction_set) {
210 case kArm:
211 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800212 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800213 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800214 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800215 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800216 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -0800217 return NULL;
218 }
219}
220
Ian Rogersad25ac52011-10-04 19:13:33 -0700221ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
222 if (instruction_set == kX86) {
223 return x86::CreateAbstractMethodErrorStub();
224 } else {
225 CHECK(instruction_set == kArm || instruction_set == kThumb2);
226 // Generates resolution stub using ARM instruction set
227 return arm::CreateAbstractMethodErrorStub();
228 }
229}
230
Jesse Wilson254db0f2011-11-16 16:44:11 -0500231void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800232 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700233 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800234
Elliott Hughes601a1232012-02-02 17:47:38 -0800235 TimingLogger timings("compiler");
236
237 PreCompile(class_loader, dex_files, timings);
238
Brian Carlstromae826982011-11-09 01:33:42 -0800239 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800240 timings.AddSplit("Compile");
241
Brian Carlstromae826982011-11-09 01:33:42 -0800242 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800243 timings.AddSplit("PostCompile");
244
245 if (timings.GetTotalNs() > MsToNs(1000)) {
246 timings.Dump();
247 }
Ian Rogers996cc582012-02-14 22:23:29 -0800248
249 stats_.Dump();
Brian Carlstrom8a487412011-08-29 20:08:52 -0700250}
251
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700252void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700253 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800254
Brian Carlstrom8a487412011-08-29 20:08:52 -0700255 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800256
Ian Rogers0571d352011-11-03 19:51:38 -0700257 // Find the dex_file
258 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
259 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800260 std::vector<const DexFile*> dex_files;
261 dex_files.push_back(&dex_file);
262
Elliott Hughes601a1232012-02-02 17:47:38 -0800263 TimingLogger timings("CompileOne");
264 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800265
Ian Rogers0571d352011-11-03 19:51:38 -0700266 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800267 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
268 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800269
270 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700271}
272
Brian Carlstromae826982011-11-09 01:33:42 -0800273void Compiler::Resolve(const ClassLoader* class_loader,
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800274 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800275 for (size_t i = 0; i != dex_files.size(); ++i) {
276 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700277 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800278 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700279 }
280}
281
Brian Carlstromae826982011-11-09 01:33:42 -0800282void Compiler::PreCompile(const ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800283 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800284 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800285
Brian Carlstromae826982011-11-09 01:33:42 -0800286 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800287 timings.AddSplit("PreCompile.Verify");
288
Brian Carlstromae826982011-11-09 01:33:42 -0800289 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800290 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800291}
292
293void Compiler::PostCompile(const ClassLoader* class_loader,
294 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800295 SetGcMaps(class_loader, dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800296 SetCodeAndDirectMethods(dex_files);
297}
298
299bool Compiler::IsImageClass(const std::string& descriptor) const {
300 if (image_classes_ == NULL) {
301 return true;
302 }
303 return image_classes_->find(descriptor) != image_classes_->end();
304}
305
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800306bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800307 uint32_t type_idx) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800308 if (!IsImage()) {
Ian Rogers996cc582012-02-14 22:23:29 -0800309 stats_.TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800310 return false;
311 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800312 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 if (resolved_class == NULL) {
Ian Rogers996cc582012-02-14 22:23:29 -0800314 stats_.TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800315 return false;
316 }
Ian Rogers996cc582012-02-14 22:23:29 -0800317 bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor());
318 if (result) {
319 stats_.TypeInDexCache();
320 } else {
321 stats_.TypeNotInDexCache();
322 }
323 return result;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800324}
325
Ian Rogers1bddec32012-02-04 12:27:34 -0800326bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800327 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800328 // TODO: Add support for loading strings referenced by image_classes_
329 // See also Compiler::ResolveDexFile
330
331 // The following is a test saying that if we're building the image without a restricted set of
332 // 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 -0800333 bool result = IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
334 if (result) {
335 stats_.StringInDexCache();
336 } else {
337 stats_.StringNotInDexCache();
338 }
339 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800340}
341
342bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800343 const DexFile& dex_file, uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800344 // Get type from dex cache assuming it was populated by the verifier
345 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
346 if (resolved_class == NULL) {
Ian Rogers996cc582012-02-14 22:23:29 -0800347 stats_.TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800348 return false; // Unknown class needs access checks.
349 }
350 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
351 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
352 if (referrer_class == NULL) {
Ian Rogers996cc582012-02-14 22:23:29 -0800353 stats_.TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800354 return false; // Incomplete referrer knowledge needs access check.
355 }
356 // Perform access check, will return true if access is ok or false if we're going to have to
357 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800358 bool result = referrer_class->CanAccess(resolved_class);
359 if (result) {
360 stats_.TypeDoesntNeedAccessCheck();
361 } else {
362 stats_.TypeNeedsAccessCheck();
363 }
364 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800365}
366
367bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
368 const DexCache* dex_cache,
369 const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800370 uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800371 // Get type from dex cache assuming it was populated by the verifier.
372 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
373 if (resolved_class == NULL) {
Ian Rogers996cc582012-02-14 22:23:29 -0800374 stats_.TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800375 return false; // Unknown class needs access checks.
376 }
377 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
378 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
379 if (referrer_class == NULL) {
Ian Rogers996cc582012-02-14 22:23:29 -0800380 stats_.TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800381 return false; // Incomplete referrer knowledge needs access check.
382 }
383 // Perform access and instantiable checks, will return true if access is ok or false if we're
384 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800385 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
386 if (result) {
387 stats_.TypeDoesntNeedAccessCheck();
388 } else {
389 stats_.TypeNeedsAccessCheck();
390 }
391 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800392}
393
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800394static Class* ComputeReferrerClass(CompilationUnit* cUnit) {
395 const DexFile::MethodId& referrer_method_id = cUnit->dex_file->GetMethodId(cUnit->method_idx);
396 return cUnit->class_linker->ResolveType(*cUnit->dex_file, referrer_method_id.class_idx_,
397 cUnit->dex_cache, cUnit->class_loader);
398}
399
400static Field* ComputeReferrerField(CompilationUnit* cUnit, uint32_t field_idx) {
401 return cUnit->class_linker->ResolveField(*cUnit->dex_file, field_idx, cUnit->dex_cache,
402 cUnit->class_loader, false);
403
404}
405
406static Method* ComputeReferrerMethod(CompilationUnit* cUnit, uint32_t method_idx) {
407 return cUnit->class_linker->ResolveMethod(*cUnit->dex_file, method_idx, cUnit->dex_cache,
408 cUnit->class_loader, true);
409}
410
Ian Rogers1bddec32012-02-04 12:27:34 -0800411bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, CompilationUnit* cUnit,
Ian Rogers996cc582012-02-14 22:23:29 -0800412 int& field_offset, bool& is_volatile) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800413 // Conservative defaults
414 field_offset = -1;
415 is_volatile = true;
416 // Try to resolve field
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800417 Field* resolved_field = ComputeReferrerField(cUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800418 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800419 Class* referrer_class = ComputeReferrerClass(cUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800420 // Try to resolve referring class then access check, failure to pass the
421 Class* fields_class = resolved_field->GetDeclaringClass();
422 if (referrer_class != NULL &&
423 referrer_class->CanAccess(fields_class) &&
424 referrer_class->CanAccessMember(fields_class,
425 resolved_field->GetAccessFlags())) {
426 field_offset = resolved_field->GetOffset().Int32Value();
427 is_volatile = resolved_field->IsVolatile();
Ian Rogers996cc582012-02-14 22:23:29 -0800428 stats_.ResolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800429 return true; // Fast path.
430 }
431 }
432 // Clean up any exception left by field/type resolution
433 Thread* thread = Thread::Current();
434 if (thread->IsExceptionPending()) {
435 thread->ClearException();
436 }
Ian Rogers996cc582012-02-14 22:23:29 -0800437 stats_.UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800438 return false; // Incomplete knowledge needs slow path.
439}
440
441bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, CompilationUnit* cUnit,
442 int& field_offset, int& ssb_index,
Ian Rogers996cc582012-02-14 22:23:29 -0800443 bool& is_referrers_class, bool& is_volatile) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800444 // Conservative defaults
445 field_offset = -1;
446 ssb_index = -1;
447 is_referrers_class = false;
448 is_volatile = true;
449 // Try to resolve field
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800450 Field* resolved_field = ComputeReferrerField(cUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800451 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800452 DCHECK(resolved_field->IsStatic());
453 Class* referrer_class = ComputeReferrerClass(cUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800454 if (referrer_class != NULL) {
455 if (resolved_field->GetDeclaringClass() == referrer_class) {
456 is_referrers_class = true; // implies no worrying about class initialization
457 field_offset = resolved_field->GetOffset().Int32Value();
458 is_volatile = resolved_field->IsVolatile();
Ian Rogers996cc582012-02-14 22:23:29 -0800459 stats_.ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800460 return true; // fast path
461 } else {
462 Class* fields_class = resolved_field->GetDeclaringClass();
463 if (referrer_class->CanAccess(fields_class) &&
464 referrer_class->CanAccessMember(fields_class,
465 resolved_field->GetAccessFlags())) {
466 // We have the resolved field, we must make it into a ssbIndex for the referrer
467 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800468 // TODO: for images we can elide the static storage base null check
469 // if we know there's a non-null entry in the image
470 if (fields_class->GetDexCache() == cUnit->dex_cache) {
471 // common case where the dex cache of both the referrer and the field are the same,
472 // no need to search the dex file
473 ssb_index = fields_class->GetDexTypeIndex();
474 field_offset = resolved_field->GetOffset().Int32Value();
475 is_volatile = resolved_field->IsVolatile();
Ian Rogers996cc582012-02-14 22:23:29 -0800476 stats_.ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800477 return true;
478 }
479 // Search dex file for localized ssb index
Ian Rogers1bddec32012-02-04 12:27:34 -0800480 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
481 const DexFile::StringId* string_id =
482 cUnit->dex_file->FindStringId(descriptor);
483 if (string_id != NULL) {
484 const DexFile::TypeId* type_id =
485 cUnit->dex_file->FindTypeId(cUnit->dex_file->GetIndexForStringId(*string_id));
486 if(type_id != NULL) {
487 // medium path, needs check of static storage base being initialized
Ian Rogers1bddec32012-02-04 12:27:34 -0800488 ssb_index = cUnit->dex_file->GetIndexForTypeId(*type_id);
489 field_offset = resolved_field->GetOffset().Int32Value();
490 is_volatile = resolved_field->IsVolatile();
Ian Rogers996cc582012-02-14 22:23:29 -0800491 stats_.ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800492 return true;
493 }
494 }
495 }
496 }
497 }
498 }
499 // Clean up any exception left by field/type resolution
500 Thread* thread = Thread::Current();
501 if (thread->IsExceptionPending()) {
502 thread->ClearException();
503 }
Ian Rogers996cc582012-02-14 22:23:29 -0800504 stats_.UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800505 return false; // Incomplete knowledge needs slow path.
506}
507
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800508bool Compiler::ComputeInvokeInfo(uint32_t method_idx, CompilationUnit* cUnit,
509 bool is_interface, bool is_super,
Ian Rogers996cc582012-02-14 22:23:29 -0800510 int& vtable_idx) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800511 vtable_idx = -1;
512 Method* resolved_method = ComputeReferrerMethod(cUnit, method_idx);
513 if (resolved_method != NULL) {
514 Class* referrer_class = ComputeReferrerClass(cUnit);
515 if (referrer_class != NULL) {
516 Class* methods_class = resolved_method->GetDeclaringClass();
517 if (!referrer_class->CanAccess(methods_class) ||
518 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800519 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800520 // The referring class can't access the resolved method, this may occur as a result of a
521 // protected method being made public by implementing an interface that re-declares the
522 // method public. Resort to the dex file to determine the correct class for the access check
523 const DexFile& dex_file = cUnit->class_linker->FindDexFile(referrer_class->GetDexCache());
524 methods_class =
525 cUnit->class_linker->ResolveType(dex_file,
526 dex_file.GetMethodId(method_idx).class_idx_,
527 referrer_class);
528
529 }
530 if (referrer_class->CanAccess(methods_class) &&
531 referrer_class->CanAccessMember(methods_class,
532 resolved_method->GetAccessFlags())) {
533 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogers996cc582012-02-14 22:23:29 -0800534 if (is_interface || !is_super) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800535 // nothing left to do for virtual/interface dispatch
Ian Rogers996cc582012-02-14 22:23:29 -0800536 stats_.ResolvedMethod(is_interface, is_super);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800537 return true;
538 } else {
539 // ensure the vtable index will be correct to dispatch in the vtable of the super class
Ian Rogers996cc582012-02-14 22:23:29 -0800540 if (referrer_class->IsSubClass(methods_class) &&
541 vtable_idx < methods_class->GetVTable()->GetLength()) {
542 stats_.ResolvedMethod(is_interface, is_super);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800543 return true;
544 }
545 }
546 }
547 }
548 }
549 // Clean up any exception left by method/type resolution
550 Thread* thread = Thread::Current();
551 if (thread->IsExceptionPending()) {
552 thread->ClearException();
553 }
Ian Rogers996cc582012-02-14 22:23:29 -0800554 stats_.UnresolvedMethod(is_interface, is_super);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800555 return false; // Incomplete knowledge needs slow path.
556}
557
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800558// Return true if the class should be skipped during compilation. We
559// never skip classes in the boot class loader. However, if we have a
560// non-boot class loader and we can resolve the class in the boot
561// class loader, we do skip the class. This happens if an app bundles
562// classes found in the boot classpath. Since at runtime we will
563// select the class from the boot classpath, do not attempt to resolve
564// or compile it now.
565static bool SkipClass(const ClassLoader* class_loader,
566 const DexFile& dex_file,
567 const DexFile::ClassDef& class_def) {
568 if (class_loader == NULL) {
569 return false;
570 }
571 const char* descriptor = dex_file.GetClassDescriptor(class_def);
572 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
573 Class* klass = class_linker->FindClass(descriptor, NULL);
574 if (klass == NULL) {
575 Thread* self = Thread::Current();
576 CHECK(self->IsExceptionPending());
577 self->ClearException();
578 return false;
579 }
580 return true;
581}
582
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800583struct Context {
584 ClassLinker* class_linker;
585 const ClassLoader* class_loader;
Elliott Hughesc225caa2012-02-03 15:43:37 -0800586 Compiler* compiler;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800587 DexCache* dex_cache;
588 const DexFile* dex_file;
589};
590
591typedef void Callback(Context* context, size_t index);
592
593class WorkerThread {
594 public:
Elliott Hughes1e409252012-02-06 11:21:27 -0800595 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
596 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
597 if (spawn_) {
598 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread");
599 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800600 }
601
602 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800603 if (spawn_) {
604 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
605 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800606 }
607
608 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800609 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800610 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
611 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800612 if (worker->spawn_) {
613 runtime->AttachCurrentThread("Compiler Worker", true);
614 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800615 Thread::Current()->SetState(Thread::kRunnable);
616 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800617 if (worker->spawn_) {
618 Thread::Current()->SetState(Thread::kNative);
619 runtime->DetachCurrentThread();
620 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800621 return NULL;
622 }
623
Elliott Hughes1e409252012-02-06 11:21:27 -0800624 void Go() {
625 Go(this);
626 }
627
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800628 void Run() {
629 for (size_t i = begin_; i < end_; i += stripe_) {
630 callback_(context_, i);
631 }
632 }
633
634 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800635 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800636
637 Context* context_;
638 size_t begin_;
639 size_t end_;
640 Callback* callback_;
641 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800642
643 friend void ForAll(Context*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800644};
645
Elliott Hughes5523ee02012-02-03 18:18:34 -0800646void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800647 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -0800648
Elliott Hughes1e409252012-02-06 11:21:27 -0800649 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -0800650 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800651 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800652 }
Elliott Hughes1e409252012-02-06 11:21:27 -0800653 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800654
Elliott Hughes81d91512012-02-03 16:38:43 -0800655 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
656 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
657 STLDeleteElements(&threads);
658}
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800659
660static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
661 const DexFile& dex_file = *context->dex_file;
662
663 // Method and Field are the worst. We can't resolve without either
664 // context from the code use (to disambiguate virtual vs direct
665 // method and instance vs static field) or from class
666 // definitions. While the compiler will resolve what it can as it
667 // needs it, here we try to resolve fields and methods used in class
668 // definitions, since many of them many never be referenced by
669 // generated code.
670 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
671 if (SkipClass(context->class_loader, dex_file, class_def)) {
672 return;
673 }
674
675 // Note the class_data pointer advances through the headers,
676 // static fields, instance fields, direct methods, and virtual
677 // methods.
678 const byte* class_data = dex_file.GetClassData(class_def);
679 if (class_data == NULL) {
680 // empty class such as a marker interface
681 return;
682 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800683 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800684 ClassLinker* class_linker = context->class_linker;
685 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
686 ClassDataItemIterator it(dex_file, class_data);
687 while (it.HasNextStaticField()) {
688 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
689 context->class_loader, true);
690 if (field == NULL) {
691 CHECK(self->IsExceptionPending());
692 self->ClearException();
693 }
694 it.Next();
695 }
696 while (it.HasNextInstanceField()) {
697 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
698 context->class_loader, false);
699 if (field == NULL) {
700 CHECK(self->IsExceptionPending());
701 self->ClearException();
702 }
703 it.Next();
704 }
705 while (it.HasNextDirectMethod()) {
706 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
707 context->class_loader, true);
708 if (method == NULL) {
709 CHECK(self->IsExceptionPending());
710 self->ClearException();
711 }
712 it.Next();
713 }
714 while (it.HasNextVirtualMethod()) {
715 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
716 context->class_loader, false);
717 if (method == NULL) {
718 CHECK(self->IsExceptionPending());
719 self->ClearException();
720 }
721 it.Next();
722 }
723 DCHECK(!it.HasNext());
724}
725
726static void ResolveType(Context* context, size_t type_idx) {
727 // Class derived values are more complicated, they require the linker and loader.
728 Thread* self = Thread::Current();
729 ClassLinker* class_linker = context->class_linker;
730 const DexFile& dex_file = *context->dex_file;
731 Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader);
732 if (klass == NULL) {
733 CHECK(self->IsExceptionPending());
734 Thread::Current()->ClearException();
735 }
736}
737
738void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700739 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700740 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700741
Brian Carlstromae826982011-11-09 01:33:42 -0800742 // Strings are easy in that they always are simply resolved to literals in the same file
743 if (image_ && image_classes_ == NULL) {
744 // TODO: Add support for loading strings referenced by image_classes_
745 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700746 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
747 class_linker->ResolveString(dex_file, string_idx, dex_cache);
748 }
Elliott Hughesff738062012-02-03 15:00:42 -0800749 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700750 }
751
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800752 Context context;
753 context.class_linker = class_linker;
754 context.class_loader = class_loader;
755 context.dex_cache = dex_cache;
756 context.dex_file = &dex_file;
757
Elliott Hughes5523ee02012-02-03 18:18:34 -0800758 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800759 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700760
Elliott Hughes5523ee02012-02-03 18:18:34 -0800761 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800762 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700763}
764
Brian Carlstromae826982011-11-09 01:33:42 -0800765void Compiler::Verify(const ClassLoader* class_loader,
766 const std::vector<const DexFile*>& dex_files) {
767 for (size_t i = 0; i != dex_files.size(); ++i) {
768 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700769 CHECK(dex_file != NULL);
770 VerifyDexFile(class_loader, *dex_file);
771 }
772}
773
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800774static void VerifyClass(Context* context, size_t class_def_index) {
775 const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index);
776 const char* descriptor = context->dex_file->GetClassDescriptor(class_def);
777 Class* klass = context->class_linker->FindClass(descriptor, context->class_loader);
778 if (klass == NULL) {
779 Thread* self = Thread::Current();
780 CHECK(self->IsExceptionPending());
781 self->ClearException();
782 return;
783 }
784 CHECK(klass->IsResolved()) << PrettyClass(klass);
785 context->class_linker->VerifyClass(klass);
786
787 if (klass->IsErroneous()) {
788 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
789 CHECK(Thread::Current()->IsExceptionPending());
790 Thread::Current()->ClearException();
791 // We want to try verification again at run-time, so move back into the resolved state.
792 klass->SetStatus(Class::kStatusResolved);
793 }
794
795 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
796 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
797}
798
jeffhao98eacac2011-09-14 16:11:53 -0700799void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700800 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700801
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800802 Context context;
803 context.class_linker = Runtime::Current()->GetClassLinker();
804 context.class_loader = class_loader;
805 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800806 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700807
jeffhaob4df5142011-09-19 20:25:32 -0700808 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700809}
810
Brian Carlstromae826982011-11-09 01:33:42 -0800811void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
812 const std::vector<const DexFile*>& dex_files) {
813 for (size_t i = 0; i != dex_files.size(); ++i) {
814 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700815 CHECK(dex_file != NULL);
816 InitializeClassesWithoutClinit(class_loader, *dex_file);
817 }
818}
819
820void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
821 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700822 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
823 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700824 const char* descriptor = dex_file.GetClassDescriptor(class_def);
825 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700826 if (klass != NULL) {
827 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800828 // record the final class status if necessary
829 Class::Status status = klass->GetStatus();
830 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800831 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800832 CompiledClass* compiled_class = GetCompiledClass(ref);
833 if (compiled_class == NULL) {
834 compiled_class = new CompiledClass(status);
835 compiled_classes_[ref] = compiled_class;
836 } else {
837 DCHECK_EQ(status, compiled_class->GetStatus());
838 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700839 }
840 // clear any class not found or verification exceptions
841 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700842 }
843
844 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
845 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
846 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700847 if (klass == NULL) {
848 Thread::Current()->ClearException();
849 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700850 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
851 }
jeffhao98eacac2011-09-14 16:11:53 -0700852 }
853}
854
Brian Carlstromae826982011-11-09 01:33:42 -0800855void Compiler::Compile(const ClassLoader* class_loader,
856 const std::vector<const DexFile*>& dex_files) {
857 for (size_t i = 0; i != dex_files.size(); ++i) {
858 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700859 CHECK(dex_file != NULL);
860 CompileDexFile(class_loader, *dex_file);
861 }
862}
863
Elliott Hughesc225caa2012-02-03 15:43:37 -0800864void Compiler::CompileClass(Context* context, size_t class_def_index) {
865 const ClassLoader* class_loader = context->class_loader;
866 const DexFile& dex_file = *context->dex_file;
867 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800868 if (SkipClass(class_loader, dex_file, class_def)) {
869 return;
870 }
Ian Rogers0571d352011-11-03 19:51:38 -0700871 const byte* class_data = dex_file.GetClassData(class_def);
872 if (class_data == NULL) {
873 // empty class, probably a marker interface
874 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700875 }
Ian Rogers0571d352011-11-03 19:51:38 -0700876 ClassDataItemIterator it(dex_file, class_data);
877 // Skip fields
878 while (it.HasNextStaticField()) {
879 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700880 }
Ian Rogers0571d352011-11-03 19:51:38 -0700881 while (it.HasNextInstanceField()) {
882 it.Next();
883 }
884 // Compile direct methods
885 while (it.HasNextDirectMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800886 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
887 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700888 it.Next();
889 }
890 // Compile virtual methods
891 while (it.HasNextVirtualMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800892 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
893 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700894 it.Next();
895 }
896 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700897}
898
Elliott Hughesc225caa2012-02-03 15:43:37 -0800899void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
900 Context context;
901 context.class_loader = class_loader;
902 context.compiler = this;
903 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800904 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800905}
906
Ian Rogersa3760aa2011-11-14 14:32:37 -0800907void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
908 uint32_t method_idx, const ClassLoader* class_loader,
909 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700910 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800911 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800912 if ((access_flags & kAccNative) != 0) {
913 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700914 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800915 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700916 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800917 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
918 dex_file, kThumb2);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800919 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
920 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800921 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800922 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800923 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800924 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700925 }
926
927 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700928 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800929 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800930 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700931 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800932 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700933 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700934
Ian Rogers0571d352011-11-03 19:51:38 -0700935 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800936 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700937 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
938 if (compiled_invoke_stub == NULL) {
939 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800940 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700941 } else {
942 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
943 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800944 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700945 }
946 CHECK(compiled_invoke_stub != NULL);
947 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700948 }
Ian Rogers0571d352011-11-03 19:51:38 -0700949 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700950}
951
Ian Rogers0571d352011-11-03 19:51:38 -0700952static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
953 std::string key(shorty);
954 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800955 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700956 }
Ian Rogers0571d352011-11-03 19:51:38 -0700957 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700958}
959
Ian Rogers0571d352011-11-03 19:51:38 -0700960const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800961 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -0800962 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700963 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700964 if (it == compiled_invoke_stubs_.end()) {
965 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700966 } else {
967 DCHECK(it->second != NULL);
968 return it->second;
969 }
970}
971
972void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
973 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800974 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -0800975 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700976 compiled_invoke_stubs_[key] = compiled_invoke_stub;
977}
978
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800979CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800980 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800981 ClassTable::const_iterator it = compiled_classes_.find(ref);
982 if (it == compiled_classes_.end()) {
983 return NULL;
984 }
985 CHECK(it->second != NULL);
986 return it->second;
987}
988
Ian Rogers0571d352011-11-03 19:51:38 -0700989CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800990 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700991 MethodTable::const_iterator it = compiled_methods_.find(ref);
992 if (it == compiled_methods_.end()) {
993 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700994 }
995 CHECK(it->second != NULL);
996 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700997}
998
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800999void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
1000 for (size_t i = 0; i != dex_files.size(); ++i) {
1001 const DexFile* dex_file = dex_files[i];
1002 CHECK(dex_file != NULL);
1003 SetGcMapsDexFile(class_loader, *dex_file);
1004 }
1005}
1006
1007void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
1008 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1009 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1010 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1011 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1012 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1013 Class* klass = class_linker->FindClass(descriptor, class_loader);
1014 if (klass == NULL || !klass->IsVerified()) {
1015 Thread::Current()->ClearException();
1016 continue;
1017 }
1018 const byte* class_data = dex_file.GetClassData(class_def);
1019 if (class_data == NULL) {
1020 // empty class such as a marker interface
1021 continue;
1022 }
1023 ClassDataItemIterator it(dex_file, class_data);
1024 while (it.HasNextStaticField()) {
1025 it.Next();
1026 }
1027 while (it.HasNextInstanceField()) {
1028 it.Next();
1029 }
1030 while (it.HasNextDirectMethod()) {
1031 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1032 class_loader, true);
1033 SetGcMapsMethod(dex_file, method);
1034 it.Next();
1035 }
1036 while (it.HasNextVirtualMethod()) {
1037 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1038 class_loader, false);
1039 SetGcMapsMethod(dex_file, method);
1040 it.Next();
1041 }
1042 }
1043}
1044
Ian Rogers1bddec32012-02-04 12:27:34 -08001045namespace verifier {
1046 class DexVerifier {
1047 public:
1048 static const std::vector<uint8_t>* GetGcMap(Compiler::MethodReference ref);
1049 };
1050}
1051
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001052void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
1053 if (method == NULL) {
1054 Thread::Current()->ClearException();
1055 return;
1056 }
1057 uint16_t method_idx = method->GetDexMethodIndex();
1058 MethodReference ref(&dex_file, method_idx);
1059 CompiledMethod* compiled_method = GetCompiledMethod(ref);
1060 if (compiled_method == NULL) {
1061 return;
1062 }
1063 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
1064 if (gc_map == NULL) {
1065 return;
1066 }
1067 compiled_method->SetGcMap(*gc_map);
1068}
1069
Brian Carlstromae826982011-11-09 01:33:42 -08001070void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
1071 for (size_t i = 0; i != dex_files.size(); ++i) {
1072 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07001073 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -07001074 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -07001075 }
1076}
1077
Brian Carlstrom8a487412011-08-29 20:08:52 -07001078void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -07001079 Runtime* runtime = Runtime::Current();
1080 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -07001081 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001082 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001083 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -07001084 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001085 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -07001086 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
1087 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001088 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001089 } else {
1090 // TODO: we currently leave the entry blank for resolved
1091 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -07001092 }
1093 }
1094}
1095
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001096} // namespace art