blob: ed5f56c8646919394ffdd37a5021acebb0c7032e [file] [log] [blame]
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "compiler.h"
4
Brian Carlstrom27ec9612011-09-19 20:20:38 -07005#include <sys/mman.h>
6
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07007#include "assembler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07008#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -07009#include "class_loader.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070010#include "dex_cache.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080011#include "dex_verifier.h"
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070012#include "jni_compiler.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070013#include "jni_internal.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070014#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080015#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070016#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070017#include "stl_util.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070018
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070019namespace art {
20
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080021CompiledMethod* oatCompileMethod(const Compiler& compiler, const DexFile::CodeItem* code_item,
22 uint32_t access_flags, uint32_t method_idx,
23 const ClassLoader* class_loader,
24 const DexFile& dex_file, InstructionSet);
25
Shih-wei Liaoc486c112011-09-13 16:43:52 -070026namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070027 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070028 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070029 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080030 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070031}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070032namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070033 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070034 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070035 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080036 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070037}
38
Brian Carlstromae826982011-11-09 01:33:42 -080039Compiler::Compiler(InstructionSet instruction_set,
40 bool image,
41 const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -070042 : instruction_set_(instruction_set),
43 jni_compiler_(instruction_set),
44 image_(image),
Elliott Hughesbb551fa2012-01-25 16:35:29 -080045 dex_file_count_(0),
46 class_count_(0),
47 abstract_method_count_(0),
48 native_method_count_(0),
49 regular_method_count_(0),
50 instruction_count_(0),
51 start_ns_(NanoTime()),
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080052 image_classes_(image_classes) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070053 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080054 if (!image_) {
55 CHECK(image_classes_ == NULL);
56 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -070057}
58
Brian Carlstrom3320cf42011-10-04 14:58:28 -070059Compiler::~Compiler() {
Brian Carlstrom0755ec52012-01-11 15:19:46 -080060 STLDeleteValues(&compiled_classes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070061 STLDeleteValues(&compiled_methods_);
62 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -080063 if (dex_file_count_ > 0) {
64 uint64_t duration_ns = NanoTime() - start_ns_;
Elliott Hughesaa56b722012-01-26 13:25:35 -080065 std::string stats(StringPrintf("Compiled files:%zd"
66 " classes:%zd"
67 " methods:(abstract:%zd"
68 " native:%zd"
69 " regular:%zd)"
Ian Rogers3bb17a62012-01-27 23:56:44 -080070 " instructions:%zd",
Brian Carlstromfc0842b2012-01-26 11:41:11 -080071 dex_file_count_,
72 class_count_,
73 abstract_method_count_,
74 native_method_count_,
75 regular_method_count_,
Ian Rogers3bb17a62012-01-27 23:56:44 -080076 instruction_count_));
77 stats += " (took ",
78 stats += PrettyDuration(duration_ns);
Brian Carlstromfc0842b2012-01-26 11:41:11 -080079 if (instruction_count_ != 0) {
Ian Rogers3bb17a62012-01-27 23:56:44 -080080 stats += ", ";
81 stats += PrettyDuration(duration_ns / instruction_count_);
82 stats += "/instruction";
Brian Carlstromfc0842b2012-01-26 11:41:11 -080083 }
84 stats += ")";
85 LOG(INFO) << stats;
Elliott Hughesbb551fa2012-01-25 16:35:29 -080086 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -070087}
88
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070089ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
90 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -070091 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070092 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070093 } else {
94 CHECK(instruction_set == kArm || instruction_set == kThumb2);
95 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070096 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070097 }
98}
99
Elliott Hughes8add92d2012-01-18 18:18:43 -0800100ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800101 switch (instruction_set) {
102 case kArm:
103 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800104 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800105 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800106 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800107 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800108 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -0800109 return NULL;
110 }
111}
112
Ian Rogersad25ac52011-10-04 19:13:33 -0700113ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
114 if (instruction_set == kX86) {
115 return x86::CreateAbstractMethodErrorStub();
116 } else {
117 CHECK(instruction_set == kArm || instruction_set == kThumb2);
118 // Generates resolution stub using ARM instruction set
119 return arm::CreateAbstractMethodErrorStub();
120 }
121}
122
Jesse Wilson254db0f2011-11-16 16:44:11 -0500123void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800124 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700125 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800126
127 PreCompile(class_loader, dex_files);
128 Compile(class_loader, dex_files);
129 PostCompile(class_loader, dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700130}
131
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700132void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700133 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800134
Brian Carlstrom8a487412011-08-29 20:08:52 -0700135 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800136
Ian Rogers0571d352011-11-03 19:51:38 -0700137 // Find the dex_file
138 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
139 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800140 std::vector<const DexFile*> dex_files;
141 dex_files.push_back(&dex_file);
142
143 PreCompile(class_loader, dex_files);
144
Ian Rogers0571d352011-11-03 19:51:38 -0700145 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800146 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
147 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800148
149 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700150}
151
Brian Carlstromae826982011-11-09 01:33:42 -0800152void Compiler::Resolve(const ClassLoader* class_loader,
153 const std::vector<const DexFile*>& dex_files) {
154 for (size_t i = 0; i != dex_files.size(); ++i) {
155 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700156 CHECK(dex_file != NULL);
157 ResolveDexFile(class_loader, *dex_file);
158 }
159}
160
Brian Carlstromae826982011-11-09 01:33:42 -0800161void Compiler::PreCompile(const ClassLoader* class_loader,
162 const std::vector<const DexFile*>& dex_files) {
163 Resolve(class_loader, dex_files);
164 Verify(class_loader, dex_files);
165 InitializeClassesWithoutClinit(class_loader, dex_files);
166}
167
168void Compiler::PostCompile(const ClassLoader* class_loader,
169 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800170 SetGcMaps(class_loader, dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800171 SetCodeAndDirectMethods(dex_files);
172}
173
174bool Compiler::IsImageClass(const std::string& descriptor) const {
175 if (image_classes_ == NULL) {
176 return true;
177 }
178 return image_classes_->find(descriptor) != image_classes_->end();
179}
180
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800181bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
182 uint32_t type_idx) const {
183 if (!IsImage()) {
184 return false;
185 }
186 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
187 if (resolved_class == NULL) {
188 return false;
189 }
190 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
191}
192
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800193// Return true if the class should be skipped during compilation. We
194// never skip classes in the boot class loader. However, if we have a
195// non-boot class loader and we can resolve the class in the boot
196// class loader, we do skip the class. This happens if an app bundles
197// classes found in the boot classpath. Since at runtime we will
198// select the class from the boot classpath, do not attempt to resolve
199// or compile it now.
200static bool SkipClass(const ClassLoader* class_loader,
201 const DexFile& dex_file,
202 const DexFile::ClassDef& class_def) {
203 if (class_loader == NULL) {
204 return false;
205 }
206 const char* descriptor = dex_file.GetClassDescriptor(class_def);
207 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
208 Class* klass = class_linker->FindClass(descriptor, NULL);
209 if (klass == NULL) {
210 Thread* self = Thread::Current();
211 CHECK(self->IsExceptionPending());
212 self->ClearException();
213 return false;
214 }
215 return true;
216}
217
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700218void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800219 Thread* self = Thread::Current();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700220 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700221 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700222
Brian Carlstromae826982011-11-09 01:33:42 -0800223 // Strings are easy in that they always are simply resolved to literals in the same file
224 if (image_ && image_classes_ == NULL) {
225 // TODO: Add support for loading strings referenced by image_classes_
226 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700227 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
228 class_linker->ResolveString(dex_file, string_idx, dex_cache);
229 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700230 }
231
Brian Carlstrom845490b2011-09-19 15:56:53 -0700232 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700233 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
234 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700235 if (klass == NULL) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800236 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700237 Thread::Current()->ClearException();
238 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700239 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700240
241 // Method and Field are the worst. We can't resolve without either
242 // context from the code use (to disambiguate virtual vs direct
243 // method and instance vs static field) or from class
244 // definitions. While the compiler will resolve what it can as it
245 // needs it, here we try to resolve fields and methods used in class
246 // definitions, since many of them many never be referenced by
247 // generated code.
248 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
249 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800250 if (SkipClass(class_loader, dex_file, class_def)) {
251 continue;
252 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700253
254 // Note the class_data pointer advances through the headers,
255 // static fields, instance fields, direct methods, and virtual
256 // methods.
257 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700258 if (class_data == NULL) {
259 // empty class such as a marker interface
260 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700261 }
Ian Rogers0571d352011-11-03 19:51:38 -0700262 ClassDataItemIterator it(dex_file, class_data);
263 while (it.HasNextStaticField()) {
264 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
265 class_loader, true);
266 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700267 CHECK(self->IsExceptionPending());
268 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700269 }
Ian Rogers0571d352011-11-03 19:51:38 -0700270 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700271 }
Ian Rogers0571d352011-11-03 19:51:38 -0700272 while (it.HasNextInstanceField()) {
273 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
274 class_loader, false);
275 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700276 CHECK(self->IsExceptionPending());
277 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700278 }
Ian Rogers0571d352011-11-03 19:51:38 -0700279 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700280 }
Ian Rogers0571d352011-11-03 19:51:38 -0700281 while (it.HasNextDirectMethod()) {
282 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
283 class_loader, true);
284 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700285 CHECK(self->IsExceptionPending());
286 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700287 }
Ian Rogers0571d352011-11-03 19:51:38 -0700288 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700289 }
Ian Rogers0571d352011-11-03 19:51:38 -0700290 while (it.HasNextVirtualMethod()) {
291 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
292 class_loader, false);
293 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700294 CHECK(self->IsExceptionPending());
295 self->ClearException();
296 }
297 it.Next();
298 }
299 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700300 }
301}
302
Brian Carlstromae826982011-11-09 01:33:42 -0800303void Compiler::Verify(const ClassLoader* class_loader,
304 const std::vector<const DexFile*>& dex_files) {
305 for (size_t i = 0; i != dex_files.size(); ++i) {
306 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700307 CHECK(dex_file != NULL);
308 VerifyDexFile(class_loader, *dex_file);
309 }
310}
311
312void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700313 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700314 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700315 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
316 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700317 const char* descriptor = dex_file.GetClassDescriptor(class_def);
318 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700319 if (klass == NULL) {
320 Thread* self = Thread::Current();
321 CHECK(self->IsExceptionPending());
322 self->ClearException();
323 continue;
324 }
325 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700326 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700327
328 if (klass->IsErroneous()) {
329 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
330 CHECK(Thread::Current()->IsExceptionPending());
331 Thread::Current()->ClearException();
332 // We want to try verification again at run-time, so move back into the resolved state.
333 klass->SetStatus(Class::kStatusResolved);
334 }
335
jeffhao5cfd6fb2011-09-27 13:54:29 -0700336 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
337 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700338 }
jeffhaob4df5142011-09-19 20:25:32 -0700339 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700340}
341
Brian Carlstromae826982011-11-09 01:33:42 -0800342void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
343 const std::vector<const DexFile*>& dex_files) {
344 for (size_t i = 0; i != dex_files.size(); ++i) {
345 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700346 CHECK(dex_file != NULL);
347 InitializeClassesWithoutClinit(class_loader, *dex_file);
348 }
349}
350
351void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
352 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700353 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
354 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700355 const char* descriptor = dex_file.GetClassDescriptor(class_def);
356 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700357 if (klass != NULL) {
358 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800359 // record the final class status if necessary
360 Class::Status status = klass->GetStatus();
361 ClassReference ref(&dex_file, class_def_index);
362 CompiledClass* compiled_class = GetCompiledClass(ref);
363 if (compiled_class == NULL) {
364 compiled_class = new CompiledClass(status);
365 compiled_classes_[ref] = compiled_class;
366 } else {
367 DCHECK_EQ(status, compiled_class->GetStatus());
368 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700369 }
370 // clear any class not found or verification exceptions
371 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700372 }
373
374 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
375 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
376 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700377 if (klass == NULL) {
378 Thread::Current()->ClearException();
379 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700380 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
381 }
jeffhao98eacac2011-09-14 16:11:53 -0700382 }
383}
384
Brian Carlstromae826982011-11-09 01:33:42 -0800385void Compiler::Compile(const ClassLoader* class_loader,
386 const std::vector<const DexFile*>& dex_files) {
387 for (size_t i = 0; i != dex_files.size(); ++i) {
388 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700389 CHECK(dex_file != NULL);
390 CompileDexFile(class_loader, *dex_file);
391 }
392}
393
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700394void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800395 ++dex_file_count_;
Brian Carlstromffca45d2011-09-16 12:10:49 -0700396 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
397 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700398 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700399 }
400}
401
Ian Rogers0571d352011-11-03 19:51:38 -0700402void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
403 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800404 if (SkipClass(class_loader, dex_file, class_def)) {
405 return;
406 }
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800407 ++class_count_;
Ian Rogers0571d352011-11-03 19:51:38 -0700408 const byte* class_data = dex_file.GetClassData(class_def);
409 if (class_data == NULL) {
410 // empty class, probably a marker interface
411 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700412 }
Ian Rogers0571d352011-11-03 19:51:38 -0700413 ClassDataItemIterator it(dex_file, class_data);
414 // Skip fields
415 while (it.HasNextStaticField()) {
416 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700417 }
Ian Rogers0571d352011-11-03 19:51:38 -0700418 while (it.HasNextInstanceField()) {
419 it.Next();
420 }
421 // Compile direct methods
422 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800423 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
424 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700425 it.Next();
426 }
427 // Compile virtual methods
428 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800429 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
430 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700431 it.Next();
432 }
433 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700434}
435
Ian Rogersa3760aa2011-11-14 14:32:37 -0800436void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
437 uint32_t method_idx, const ClassLoader* class_loader,
438 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700439 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800440 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800441 if ((access_flags & kAccNative) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800442 ++native_method_count_;
Ian Rogers169c9a72011-11-13 20:13:17 -0800443 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700444 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800445 } else if ((access_flags & kAccAbstract) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800446 ++abstract_method_count_;
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700447 } else {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800448 ++regular_method_count_;
449 instruction_count_ += code_item->insns_size_in_code_units_;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800450 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
451 dex_file, kThumb2);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800452 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
453 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800454 uint64_t duration_ns = NanoTime() - start_ns;
455 if (duration_ns > MsToNs(10)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800456 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800457 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700458 }
459
460 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700461 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800462 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700463 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800464 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700465 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700466
Ian Rogers0571d352011-11-03 19:51:38 -0700467 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800468 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700469 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
470 if (compiled_invoke_stub == NULL) {
471 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800472 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700473 } else {
474 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
475 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800476 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700477 }
478 CHECK(compiled_invoke_stub != NULL);
479 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700480 }
Ian Rogers0571d352011-11-03 19:51:38 -0700481 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482}
483
Ian Rogers0571d352011-11-03 19:51:38 -0700484static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
485 std::string key(shorty);
486 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800487 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700488 }
Ian Rogers0571d352011-11-03 19:51:38 -0700489 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700490}
491
Ian Rogers0571d352011-11-03 19:51:38 -0700492const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughes95572412011-12-13 18:14:20 -0800493 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700494 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700495 if (it == compiled_invoke_stubs_.end()) {
496 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700497 } else {
498 DCHECK(it->second != NULL);
499 return it->second;
500 }
501}
502
503void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
504 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughes95572412011-12-13 18:14:20 -0800505 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700506 compiled_invoke_stubs_[key] = compiled_invoke_stub;
507}
508
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800509CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
510 ClassTable::const_iterator it = compiled_classes_.find(ref);
511 if (it == compiled_classes_.end()) {
512 return NULL;
513 }
514 CHECK(it->second != NULL);
515 return it->second;
516}
517
Ian Rogers0571d352011-11-03 19:51:38 -0700518CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
519 MethodTable::const_iterator it = compiled_methods_.find(ref);
520 if (it == compiled_methods_.end()) {
521 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700522 }
523 CHECK(it->second != NULL);
524 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700525}
526
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800527void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
528 for (size_t i = 0; i != dex_files.size(); ++i) {
529 const DexFile* dex_file = dex_files[i];
530 CHECK(dex_file != NULL);
531 SetGcMapsDexFile(class_loader, *dex_file);
532 }
533}
534
535void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
536 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
537 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
538 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
539 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
540 const char* descriptor = dex_file.GetClassDescriptor(class_def);
541 Class* klass = class_linker->FindClass(descriptor, class_loader);
542 if (klass == NULL || !klass->IsVerified()) {
543 Thread::Current()->ClearException();
544 continue;
545 }
546 const byte* class_data = dex_file.GetClassData(class_def);
547 if (class_data == NULL) {
548 // empty class such as a marker interface
549 continue;
550 }
551 ClassDataItemIterator it(dex_file, class_data);
552 while (it.HasNextStaticField()) {
553 it.Next();
554 }
555 while (it.HasNextInstanceField()) {
556 it.Next();
557 }
558 while (it.HasNextDirectMethod()) {
559 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
560 class_loader, true);
561 SetGcMapsMethod(dex_file, method);
562 it.Next();
563 }
564 while (it.HasNextVirtualMethod()) {
565 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
566 class_loader, false);
567 SetGcMapsMethod(dex_file, method);
568 it.Next();
569 }
570 }
571}
572
573void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
574 if (method == NULL) {
575 Thread::Current()->ClearException();
576 return;
577 }
578 uint16_t method_idx = method->GetDexMethodIndex();
579 MethodReference ref(&dex_file, method_idx);
580 CompiledMethod* compiled_method = GetCompiledMethod(ref);
581 if (compiled_method == NULL) {
582 return;
583 }
584 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
585 if (gc_map == NULL) {
586 return;
587 }
588 compiled_method->SetGcMap(*gc_map);
589}
590
Brian Carlstromae826982011-11-09 01:33:42 -0800591void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
592 for (size_t i = 0; i != dex_files.size(); ++i) {
593 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700594 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700595 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700596 }
597}
598
Brian Carlstrom8a487412011-08-29 20:08:52 -0700599void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700600 Runtime* runtime = Runtime::Current();
601 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700602 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700603 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700604 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700605 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700606 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700607 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
608 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700609 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700610 } else {
611 // TODO: we currently leave the entry blank for resolved
612 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700613 }
614 }
615}
616
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700617} // namespace art