blob: 51a5caadfba615561f95a5a62e845d063acfe677 [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
Ian Rogersa3760aa2011-11-14 14:32:37 -080019art::CompiledMethod* oatCompileMethod(const art::Compiler& compiler,
20 const art::DexFile::CodeItem* code_item,
21 uint32_t access_flags, uint32_t method_idx,
22 const art::ClassLoader* class_loader,
Ian Rogers0571d352011-11-03 19:51:38 -070023 const art::DexFile& dex_file, art::InstructionSet);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070024
25namespace art {
26
Shih-wei Liaoc486c112011-09-13 16:43:52 -070027namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070028 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070029 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070030 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080031 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070032}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070033namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070034 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070035 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070036 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080037 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070038}
39
Brian Carlstromae826982011-11-09 01:33:42 -080040Compiler::Compiler(InstructionSet instruction_set,
41 bool image,
42 const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -070043 : instruction_set_(instruction_set),
44 jni_compiler_(instruction_set),
45 image_(image),
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080046 image_classes_(image_classes) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070047 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080048 if (!image_) {
49 CHECK(image_classes_ == NULL);
50 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -070051}
52
Brian Carlstrom3320cf42011-10-04 14:58:28 -070053Compiler::~Compiler() {
Brian Carlstrom0755ec52012-01-11 15:19:46 -080054 STLDeleteValues(&compiled_classes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070055 STLDeleteValues(&compiled_methods_);
56 STLDeleteValues(&compiled_invoke_stubs_);
57}
58
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070059ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
60 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -070061 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070062 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070063 } else {
64 CHECK(instruction_set == kArm || instruction_set == kThumb2);
65 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070066 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070067 }
68}
69
Elliott Hughes8add92d2012-01-18 18:18:43 -080070ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -080071 switch (instruction_set) {
72 case kArm:
73 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -080074 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -080075 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -080076 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -080077 default:
78 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
79 return NULL;
80 }
81}
82
Ian Rogersad25ac52011-10-04 19:13:33 -070083ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
84 if (instruction_set == kX86) {
85 return x86::CreateAbstractMethodErrorStub();
86 } else {
87 CHECK(instruction_set == kArm || instruction_set == kThumb2);
88 // Generates resolution stub using ARM instruction set
89 return arm::CreateAbstractMethodErrorStub();
90 }
91}
92
Jesse Wilson254db0f2011-11-16 16:44:11 -050093void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -080094 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070095 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080096
97 PreCompile(class_loader, dex_files);
98 Compile(class_loader, dex_files);
99 PostCompile(class_loader, dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700100}
101
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700102void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700103 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800104
Brian Carlstrom8a487412011-08-29 20:08:52 -0700105 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800106
Ian Rogers0571d352011-11-03 19:51:38 -0700107 // Find the dex_file
108 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
109 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800110 std::vector<const DexFile*> dex_files;
111 dex_files.push_back(&dex_file);
112
113 PreCompile(class_loader, dex_files);
114
Ian Rogers0571d352011-11-03 19:51:38 -0700115 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800116 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
117 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800118
119 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700120}
121
Brian Carlstromae826982011-11-09 01:33:42 -0800122void Compiler::Resolve(const ClassLoader* class_loader,
123 const std::vector<const DexFile*>& dex_files) {
124 for (size_t i = 0; i != dex_files.size(); ++i) {
125 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700126 CHECK(dex_file != NULL);
127 ResolveDexFile(class_loader, *dex_file);
128 }
129}
130
Brian Carlstromae826982011-11-09 01:33:42 -0800131void Compiler::PreCompile(const ClassLoader* class_loader,
132 const std::vector<const DexFile*>& dex_files) {
133 Resolve(class_loader, dex_files);
134 Verify(class_loader, dex_files);
135 InitializeClassesWithoutClinit(class_loader, dex_files);
136}
137
138void Compiler::PostCompile(const ClassLoader* class_loader,
139 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800140 SetGcMaps(class_loader, dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800141 SetCodeAndDirectMethods(dex_files);
142}
143
144bool Compiler::IsImageClass(const std::string& descriptor) const {
145 if (image_classes_ == NULL) {
146 return true;
147 }
148 return image_classes_->find(descriptor) != image_classes_->end();
149}
150
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800151bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
152 uint32_t type_idx) const {
153 if (!IsImage()) {
154 return false;
155 }
156 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
157 if (resolved_class == NULL) {
158 return false;
159 }
160 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
161}
162
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800163// Return true if the class should be skipped during compilation. We
164// never skip classes in the boot class loader. However, if we have a
165// non-boot class loader and we can resolve the class in the boot
166// class loader, we do skip the class. This happens if an app bundles
167// classes found in the boot classpath. Since at runtime we will
168// select the class from the boot classpath, do not attempt to resolve
169// or compile it now.
170static bool SkipClass(const ClassLoader* class_loader,
171 const DexFile& dex_file,
172 const DexFile::ClassDef& class_def) {
173 if (class_loader == NULL) {
174 return false;
175 }
176 const char* descriptor = dex_file.GetClassDescriptor(class_def);
177 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
178 Class* klass = class_linker->FindClass(descriptor, NULL);
179 if (klass == NULL) {
180 Thread* self = Thread::Current();
181 CHECK(self->IsExceptionPending());
182 self->ClearException();
183 return false;
184 }
185 return true;
186}
187
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700188void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800189 Thread* self = Thread::Current();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700191 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700192
Brian Carlstromae826982011-11-09 01:33:42 -0800193 // Strings are easy in that they always are simply resolved to literals in the same file
194 if (image_ && image_classes_ == NULL) {
195 // TODO: Add support for loading strings referenced by image_classes_
196 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700197 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
198 class_linker->ResolveString(dex_file, string_idx, dex_cache);
199 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700200 }
201
Brian Carlstrom845490b2011-09-19 15:56:53 -0700202 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700203 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
204 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700205 if (klass == NULL) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800206 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700207 Thread::Current()->ClearException();
208 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700209 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700210
211 // Method and Field are the worst. We can't resolve without either
212 // context from the code use (to disambiguate virtual vs direct
213 // method and instance vs static field) or from class
214 // definitions. While the compiler will resolve what it can as it
215 // needs it, here we try to resolve fields and methods used in class
216 // definitions, since many of them many never be referenced by
217 // generated code.
218 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
219 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800220 if (SkipClass(class_loader, dex_file, class_def)) {
221 continue;
222 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700223
224 // Note the class_data pointer advances through the headers,
225 // static fields, instance fields, direct methods, and virtual
226 // methods.
227 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700228 if (class_data == NULL) {
229 // empty class such as a marker interface
230 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700231 }
Ian Rogers0571d352011-11-03 19:51:38 -0700232 ClassDataItemIterator it(dex_file, class_data);
233 while (it.HasNextStaticField()) {
234 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
235 class_loader, true);
236 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700237 CHECK(self->IsExceptionPending());
238 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700239 }
Ian Rogers0571d352011-11-03 19:51:38 -0700240 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700241 }
Ian Rogers0571d352011-11-03 19:51:38 -0700242 while (it.HasNextInstanceField()) {
243 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
244 class_loader, false);
245 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700246 CHECK(self->IsExceptionPending());
247 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700248 }
Ian Rogers0571d352011-11-03 19:51:38 -0700249 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700250 }
Ian Rogers0571d352011-11-03 19:51:38 -0700251 while (it.HasNextDirectMethod()) {
252 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
253 class_loader, true);
254 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700255 CHECK(self->IsExceptionPending());
256 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700257 }
Ian Rogers0571d352011-11-03 19:51:38 -0700258 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700259 }
Ian Rogers0571d352011-11-03 19:51:38 -0700260 while (it.HasNextVirtualMethod()) {
261 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
262 class_loader, false);
263 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700264 CHECK(self->IsExceptionPending());
265 self->ClearException();
266 }
267 it.Next();
268 }
269 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700270 }
271}
272
Brian Carlstromae826982011-11-09 01:33:42 -0800273void Compiler::Verify(const ClassLoader* class_loader,
274 const std::vector<const DexFile*>& dex_files) {
275 for (size_t i = 0; i != dex_files.size(); ++i) {
276 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700277 CHECK(dex_file != NULL);
278 VerifyDexFile(class_loader, *dex_file);
279 }
280}
281
282void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700283 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700284 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700285 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
286 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700287 const char* descriptor = dex_file.GetClassDescriptor(class_def);
288 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700289 if (klass == NULL) {
290 Thread* self = Thread::Current();
291 CHECK(self->IsExceptionPending());
292 self->ClearException();
293 continue;
294 }
295 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700296 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700297
298 if (klass->IsErroneous()) {
299 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
300 CHECK(Thread::Current()->IsExceptionPending());
301 Thread::Current()->ClearException();
302 // We want to try verification again at run-time, so move back into the resolved state.
303 klass->SetStatus(Class::kStatusResolved);
304 }
305
jeffhao5cfd6fb2011-09-27 13:54:29 -0700306 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
307 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700308 }
jeffhaob4df5142011-09-19 20:25:32 -0700309 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700310}
311
Brian Carlstromae826982011-11-09 01:33:42 -0800312void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
313 const std::vector<const DexFile*>& dex_files) {
314 for (size_t i = 0; i != dex_files.size(); ++i) {
315 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700316 CHECK(dex_file != NULL);
317 InitializeClassesWithoutClinit(class_loader, *dex_file);
318 }
319}
320
321void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
322 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700323 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
324 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700325 const char* descriptor = dex_file.GetClassDescriptor(class_def);
326 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700327 if (klass != NULL) {
328 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800329 // record the final class status if necessary
330 Class::Status status = klass->GetStatus();
331 ClassReference ref(&dex_file, class_def_index);
332 CompiledClass* compiled_class = GetCompiledClass(ref);
333 if (compiled_class == NULL) {
334 compiled_class = new CompiledClass(status);
335 compiled_classes_[ref] = compiled_class;
336 } else {
337 DCHECK_EQ(status, compiled_class->GetStatus());
338 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700339 }
340 // clear any class not found or verification exceptions
341 Thread::Current()->ClearException();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800342
Brian Carlstromffca45d2011-09-16 12:10:49 -0700343 }
344
345 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
346 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
347 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700348 if (klass == NULL) {
349 Thread::Current()->ClearException();
350 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700351 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
352 }
jeffhao98eacac2011-09-14 16:11:53 -0700353 }
354}
355
Brian Carlstromae826982011-11-09 01:33:42 -0800356void Compiler::Compile(const ClassLoader* class_loader,
357 const std::vector<const DexFile*>& dex_files) {
358 for (size_t i = 0; i != dex_files.size(); ++i) {
359 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700360 CHECK(dex_file != NULL);
361 CompileDexFile(class_loader, *dex_file);
362 }
363}
364
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700365void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700366 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
367 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700368 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700369 }
370}
371
Ian Rogers0571d352011-11-03 19:51:38 -0700372void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
373 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800374 if (SkipClass(class_loader, dex_file, class_def)) {
375 return;
376 }
Ian Rogers0571d352011-11-03 19:51:38 -0700377 const byte* class_data = dex_file.GetClassData(class_def);
378 if (class_data == NULL) {
379 // empty class, probably a marker interface
380 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700381 }
Ian Rogers0571d352011-11-03 19:51:38 -0700382 ClassDataItemIterator it(dex_file, class_data);
383 // Skip fields
384 while (it.HasNextStaticField()) {
385 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700386 }
Ian Rogers0571d352011-11-03 19:51:38 -0700387 while (it.HasNextInstanceField()) {
388 it.Next();
389 }
390 // Compile direct methods
391 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800392 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
393 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700394 it.Next();
395 }
396 // Compile virtual methods
397 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800398 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
399 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700400 it.Next();
401 }
402 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700403}
404
Ian Rogersa3760aa2011-11-14 14:32:37 -0800405void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
406 uint32_t method_idx, const ClassLoader* class_loader,
407 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700408 CompiledMethod* compiled_method = NULL;
Ian Rogers169c9a72011-11-13 20:13:17 -0800409 if ((access_flags & kAccNative) != 0) {
410 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700411 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800412 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700413 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800414 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
415 dex_file, kThumb2);
416 CHECK(compiled_method != NULL);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700417 }
418
419 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700420 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800421 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700422 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800423 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700424 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700425
Ian Rogers0571d352011-11-03 19:51:38 -0700426 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800427 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700428 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
429 if (compiled_invoke_stub == NULL) {
430 if (instruction_set_ == kX86) {
431 compiled_invoke_stub = art::x86::X86CreateInvokeStub(is_static, shorty);
432 } else {
433 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
434 // Generates invocation stub using ARM instruction set
435 compiled_invoke_stub = art::arm::ArmCreateInvokeStub(is_static, shorty);
436 }
437 CHECK(compiled_invoke_stub != NULL);
438 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700439 }
Ian Rogers0571d352011-11-03 19:51:38 -0700440 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700441}
442
Ian Rogers0571d352011-11-03 19:51:38 -0700443static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
444 std::string key(shorty);
445 if (is_static) {
446 key += "$"; // musn't be a shorty type character
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700447 }
Ian Rogers0571d352011-11-03 19:51:38 -0700448 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700449}
450
Ian Rogers0571d352011-11-03 19:51:38 -0700451const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughes95572412011-12-13 18:14:20 -0800452 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700453 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700454 if (it == compiled_invoke_stubs_.end()) {
455 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700456 } else {
457 DCHECK(it->second != NULL);
458 return it->second;
459 }
460}
461
462void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
463 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughes95572412011-12-13 18:14:20 -0800464 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700465 compiled_invoke_stubs_[key] = compiled_invoke_stub;
466}
467
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800468CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
469 ClassTable::const_iterator it = compiled_classes_.find(ref);
470 if (it == compiled_classes_.end()) {
471 return NULL;
472 }
473 CHECK(it->second != NULL);
474 return it->second;
475}
476
Ian Rogers0571d352011-11-03 19:51:38 -0700477CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
478 MethodTable::const_iterator it = compiled_methods_.find(ref);
479 if (it == compiled_methods_.end()) {
480 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700481 }
482 CHECK(it->second != NULL);
483 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700484}
485
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800486void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
487 for (size_t i = 0; i != dex_files.size(); ++i) {
488 const DexFile* dex_file = dex_files[i];
489 CHECK(dex_file != NULL);
490 SetGcMapsDexFile(class_loader, *dex_file);
491 }
492}
493
494void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
495 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
496 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
497 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
498 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
499 const char* descriptor = dex_file.GetClassDescriptor(class_def);
500 Class* klass = class_linker->FindClass(descriptor, class_loader);
501 if (klass == NULL || !klass->IsVerified()) {
502 Thread::Current()->ClearException();
503 continue;
504 }
505 const byte* class_data = dex_file.GetClassData(class_def);
506 if (class_data == NULL) {
507 // empty class such as a marker interface
508 continue;
509 }
510 ClassDataItemIterator it(dex_file, class_data);
511 while (it.HasNextStaticField()) {
512 it.Next();
513 }
514 while (it.HasNextInstanceField()) {
515 it.Next();
516 }
517 while (it.HasNextDirectMethod()) {
518 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
519 class_loader, true);
520 SetGcMapsMethod(dex_file, method);
521 it.Next();
522 }
523 while (it.HasNextVirtualMethod()) {
524 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
525 class_loader, false);
526 SetGcMapsMethod(dex_file, method);
527 it.Next();
528 }
529 }
530}
531
532void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
533 if (method == NULL) {
534 Thread::Current()->ClearException();
535 return;
536 }
537 uint16_t method_idx = method->GetDexMethodIndex();
538 MethodReference ref(&dex_file, method_idx);
539 CompiledMethod* compiled_method = GetCompiledMethod(ref);
540 if (compiled_method == NULL) {
541 return;
542 }
543 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
544 if (gc_map == NULL) {
545 return;
546 }
547 compiled_method->SetGcMap(*gc_map);
548}
549
Brian Carlstromae826982011-11-09 01:33:42 -0800550void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
551 for (size_t i = 0; i != dex_files.size(); ++i) {
552 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700553 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700554 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700555 }
556}
557
Brian Carlstrom8a487412011-08-29 20:08:52 -0700558void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700559 Runtime* runtime = Runtime::Current();
560 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700561 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700562 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700563 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700564 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700565 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700566 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
567 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700568 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700569 } else {
570 // TODO: we currently leave the entry blank for resolved
571 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700572 }
573 }
574}
575
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700576} // namespace art