blob: 647a8edc9a5c19dd758cf874fd12e9036790bcfc [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 Hughes4dd9b4d2011-12-12 18:29:24 -080045 image_classes_(image_classes) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070046 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080047 if (!image_) {
48 CHECK(image_classes_ == NULL);
49 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -070050}
51
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052Compiler::~Compiler() {
Brian Carlstrom0755ec52012-01-11 15:19:46 -080053 STLDeleteValues(&compiled_classes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070054 STLDeleteValues(&compiled_methods_);
55 STLDeleteValues(&compiled_invoke_stubs_);
56}
57
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070058ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
59 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -070060 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070061 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070062 } else {
63 CHECK(instruction_set == kArm || instruction_set == kThumb2);
64 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070065 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070066 }
67}
68
Elliott Hughes8add92d2012-01-18 18:18:43 -080069ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -080070 switch (instruction_set) {
71 case kArm:
72 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -080073 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -080074 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -080075 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -080076 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -080077 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -080078 return NULL;
79 }
80}
81
Ian Rogersad25ac52011-10-04 19:13:33 -070082ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
83 if (instruction_set == kX86) {
84 return x86::CreateAbstractMethodErrorStub();
85 } else {
86 CHECK(instruction_set == kArm || instruction_set == kThumb2);
87 // Generates resolution stub using ARM instruction set
88 return arm::CreateAbstractMethodErrorStub();
89 }
90}
91
Jesse Wilson254db0f2011-11-16 16:44:11 -050092void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -080093 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070094 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080095
96 PreCompile(class_loader, dex_files);
97 Compile(class_loader, dex_files);
98 PostCompile(class_loader, dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -070099}
100
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700101void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700102 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800103
Brian Carlstrom8a487412011-08-29 20:08:52 -0700104 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800105
Ian Rogers0571d352011-11-03 19:51:38 -0700106 // Find the dex_file
107 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
108 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800109 std::vector<const DexFile*> dex_files;
110 dex_files.push_back(&dex_file);
111
112 PreCompile(class_loader, dex_files);
113
Ian Rogers0571d352011-11-03 19:51:38 -0700114 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800115 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
116 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800117
118 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700119}
120
Brian Carlstromae826982011-11-09 01:33:42 -0800121void Compiler::Resolve(const ClassLoader* class_loader,
122 const std::vector<const DexFile*>& dex_files) {
123 for (size_t i = 0; i != dex_files.size(); ++i) {
124 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700125 CHECK(dex_file != NULL);
126 ResolveDexFile(class_loader, *dex_file);
127 }
128}
129
Brian Carlstromae826982011-11-09 01:33:42 -0800130void Compiler::PreCompile(const ClassLoader* class_loader,
131 const std::vector<const DexFile*>& dex_files) {
132 Resolve(class_loader, dex_files);
133 Verify(class_loader, dex_files);
134 InitializeClassesWithoutClinit(class_loader, dex_files);
135}
136
137void Compiler::PostCompile(const ClassLoader* class_loader,
138 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800139 SetGcMaps(class_loader, dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800140 SetCodeAndDirectMethods(dex_files);
141}
142
143bool Compiler::IsImageClass(const std::string& descriptor) const {
144 if (image_classes_ == NULL) {
145 return true;
146 }
147 return image_classes_->find(descriptor) != image_classes_->end();
148}
149
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800150bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
151 uint32_t type_idx) const {
152 if (!IsImage()) {
153 return false;
154 }
155 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
156 if (resolved_class == NULL) {
157 return false;
158 }
159 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
160}
161
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800162// Return true if the class should be skipped during compilation. We
163// never skip classes in the boot class loader. However, if we have a
164// non-boot class loader and we can resolve the class in the boot
165// class loader, we do skip the class. This happens if an app bundles
166// classes found in the boot classpath. Since at runtime we will
167// select the class from the boot classpath, do not attempt to resolve
168// or compile it now.
169static bool SkipClass(const ClassLoader* class_loader,
170 const DexFile& dex_file,
171 const DexFile::ClassDef& class_def) {
172 if (class_loader == NULL) {
173 return false;
174 }
175 const char* descriptor = dex_file.GetClassDescriptor(class_def);
176 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
177 Class* klass = class_linker->FindClass(descriptor, NULL);
178 if (klass == NULL) {
179 Thread* self = Thread::Current();
180 CHECK(self->IsExceptionPending());
181 self->ClearException();
182 return false;
183 }
184 return true;
185}
186
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700187void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800188 Thread* self = Thread::Current();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700189 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700190 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700191
Brian Carlstromae826982011-11-09 01:33:42 -0800192 // Strings are easy in that they always are simply resolved to literals in the same file
193 if (image_ && image_classes_ == NULL) {
194 // TODO: Add support for loading strings referenced by image_classes_
195 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700196 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
197 class_linker->ResolveString(dex_file, string_idx, dex_cache);
198 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700199 }
200
Brian Carlstrom845490b2011-09-19 15:56:53 -0700201 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700202 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
203 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700204 if (klass == NULL) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800205 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700206 Thread::Current()->ClearException();
207 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700208 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700209
210 // Method and Field are the worst. We can't resolve without either
211 // context from the code use (to disambiguate virtual vs direct
212 // method and instance vs static field) or from class
213 // definitions. While the compiler will resolve what it can as it
214 // needs it, here we try to resolve fields and methods used in class
215 // definitions, since many of them many never be referenced by
216 // generated code.
217 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
218 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800219 if (SkipClass(class_loader, dex_file, class_def)) {
220 continue;
221 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700222
223 // Note the class_data pointer advances through the headers,
224 // static fields, instance fields, direct methods, and virtual
225 // methods.
226 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700227 if (class_data == NULL) {
228 // empty class such as a marker interface
229 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700230 }
Ian Rogers0571d352011-11-03 19:51:38 -0700231 ClassDataItemIterator it(dex_file, class_data);
232 while (it.HasNextStaticField()) {
233 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
234 class_loader, true);
235 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700236 CHECK(self->IsExceptionPending());
237 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700238 }
Ian Rogers0571d352011-11-03 19:51:38 -0700239 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700240 }
Ian Rogers0571d352011-11-03 19:51:38 -0700241 while (it.HasNextInstanceField()) {
242 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
243 class_loader, false);
244 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700245 CHECK(self->IsExceptionPending());
246 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700247 }
Ian Rogers0571d352011-11-03 19:51:38 -0700248 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700249 }
Ian Rogers0571d352011-11-03 19:51:38 -0700250 while (it.HasNextDirectMethod()) {
251 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
252 class_loader, true);
253 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700254 CHECK(self->IsExceptionPending());
255 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700256 }
Ian Rogers0571d352011-11-03 19:51:38 -0700257 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700258 }
Ian Rogers0571d352011-11-03 19:51:38 -0700259 while (it.HasNextVirtualMethod()) {
260 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
261 class_loader, false);
262 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700263 CHECK(self->IsExceptionPending());
264 self->ClearException();
265 }
266 it.Next();
267 }
268 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700269 }
270}
271
Brian Carlstromae826982011-11-09 01:33:42 -0800272void Compiler::Verify(const ClassLoader* class_loader,
273 const std::vector<const DexFile*>& dex_files) {
274 for (size_t i = 0; i != dex_files.size(); ++i) {
275 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700276 CHECK(dex_file != NULL);
277 VerifyDexFile(class_loader, *dex_file);
278 }
279}
280
281void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700282 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700283 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700284 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
285 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700286 const char* descriptor = dex_file.GetClassDescriptor(class_def);
287 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700288 if (klass == NULL) {
289 Thread* self = Thread::Current();
290 CHECK(self->IsExceptionPending());
291 self->ClearException();
292 continue;
293 }
294 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700295 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700296
297 if (klass->IsErroneous()) {
298 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
299 CHECK(Thread::Current()->IsExceptionPending());
300 Thread::Current()->ClearException();
301 // We want to try verification again at run-time, so move back into the resolved state.
302 klass->SetStatus(Class::kStatusResolved);
303 }
304
jeffhao5cfd6fb2011-09-27 13:54:29 -0700305 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
306 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700307 }
jeffhaob4df5142011-09-19 20:25:32 -0700308 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700309}
310
Brian Carlstromae826982011-11-09 01:33:42 -0800311void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
312 const std::vector<const DexFile*>& dex_files) {
313 for (size_t i = 0; i != dex_files.size(); ++i) {
314 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700315 CHECK(dex_file != NULL);
316 InitializeClassesWithoutClinit(class_loader, *dex_file);
317 }
318}
319
320void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
321 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700322 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
323 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700324 const char* descriptor = dex_file.GetClassDescriptor(class_def);
325 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700326 if (klass != NULL) {
327 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800328 // record the final class status if necessary
329 Class::Status status = klass->GetStatus();
330 ClassReference ref(&dex_file, class_def_index);
331 CompiledClass* compiled_class = GetCompiledClass(ref);
332 if (compiled_class == NULL) {
333 compiled_class = new CompiledClass(status);
334 compiled_classes_[ref] = compiled_class;
335 } else {
336 DCHECK_EQ(status, compiled_class->GetStatus());
337 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700338 }
339 // clear any class not found or verification exceptions
340 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700341 }
342
343 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
344 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
345 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700346 if (klass == NULL) {
347 Thread::Current()->ClearException();
348 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700349 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
350 }
jeffhao98eacac2011-09-14 16:11:53 -0700351 }
352}
353
Brian Carlstromae826982011-11-09 01:33:42 -0800354void Compiler::Compile(const ClassLoader* class_loader,
355 const std::vector<const DexFile*>& dex_files) {
356 for (size_t i = 0; i != dex_files.size(); ++i) {
357 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700358 CHECK(dex_file != NULL);
359 CompileDexFile(class_loader, *dex_file);
360 }
361}
362
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700363void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700364 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
365 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700366 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700367 }
368}
369
Ian Rogers0571d352011-11-03 19:51:38 -0700370void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
371 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800372 if (SkipClass(class_loader, dex_file, class_def)) {
373 return;
374 }
Ian Rogers0571d352011-11-03 19:51:38 -0700375 const byte* class_data = dex_file.GetClassData(class_def);
376 if (class_data == NULL) {
377 // empty class, probably a marker interface
378 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700379 }
Ian Rogers0571d352011-11-03 19:51:38 -0700380 ClassDataItemIterator it(dex_file, class_data);
381 // Skip fields
382 while (it.HasNextStaticField()) {
383 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700384 }
Ian Rogers0571d352011-11-03 19:51:38 -0700385 while (it.HasNextInstanceField()) {
386 it.Next();
387 }
388 // Compile direct methods
389 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800390 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
391 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700392 it.Next();
393 }
394 // Compile virtual methods
395 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800396 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
397 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700398 it.Next();
399 }
400 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700401}
402
Ian Rogersa3760aa2011-11-14 14:32:37 -0800403void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
404 uint32_t method_idx, const ClassLoader* class_loader,
405 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700406 CompiledMethod* compiled_method = NULL;
Ian Rogers169c9a72011-11-13 20:13:17 -0800407 if ((access_flags & kAccNative) != 0) {
408 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700409 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800410 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700411 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800412 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
413 dex_file, kThumb2);
414 CHECK(compiled_method != NULL);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700415 }
416
417 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700418 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800419 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700420 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800421 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700422 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700423
Ian Rogers0571d352011-11-03 19:51:38 -0700424 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800425 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700426 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
427 if (compiled_invoke_stub == NULL) {
428 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800429 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700430 } else {
431 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
432 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800433 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700434 }
435 CHECK(compiled_invoke_stub != NULL);
436 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700437 }
Ian Rogers0571d352011-11-03 19:51:38 -0700438 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700439}
440
Ian Rogers0571d352011-11-03 19:51:38 -0700441static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
442 std::string key(shorty);
443 if (is_static) {
444 key += "$"; // musn't be a shorty type character
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700445 }
Ian Rogers0571d352011-11-03 19:51:38 -0700446 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700447}
448
Ian Rogers0571d352011-11-03 19:51:38 -0700449const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughes95572412011-12-13 18:14:20 -0800450 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700451 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700452 if (it == compiled_invoke_stubs_.end()) {
453 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700454 } else {
455 DCHECK(it->second != NULL);
456 return it->second;
457 }
458}
459
460void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
461 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughes95572412011-12-13 18:14:20 -0800462 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700463 compiled_invoke_stubs_[key] = compiled_invoke_stub;
464}
465
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800466CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
467 ClassTable::const_iterator it = compiled_classes_.find(ref);
468 if (it == compiled_classes_.end()) {
469 return NULL;
470 }
471 CHECK(it->second != NULL);
472 return it->second;
473}
474
Ian Rogers0571d352011-11-03 19:51:38 -0700475CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
476 MethodTable::const_iterator it = compiled_methods_.find(ref);
477 if (it == compiled_methods_.end()) {
478 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700479 }
480 CHECK(it->second != NULL);
481 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700482}
483
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800484void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
485 for (size_t i = 0; i != dex_files.size(); ++i) {
486 const DexFile* dex_file = dex_files[i];
487 CHECK(dex_file != NULL);
488 SetGcMapsDexFile(class_loader, *dex_file);
489 }
490}
491
492void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
493 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
494 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
495 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
496 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
497 const char* descriptor = dex_file.GetClassDescriptor(class_def);
498 Class* klass = class_linker->FindClass(descriptor, class_loader);
499 if (klass == NULL || !klass->IsVerified()) {
500 Thread::Current()->ClearException();
501 continue;
502 }
503 const byte* class_data = dex_file.GetClassData(class_def);
504 if (class_data == NULL) {
505 // empty class such as a marker interface
506 continue;
507 }
508 ClassDataItemIterator it(dex_file, class_data);
509 while (it.HasNextStaticField()) {
510 it.Next();
511 }
512 while (it.HasNextInstanceField()) {
513 it.Next();
514 }
515 while (it.HasNextDirectMethod()) {
516 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
517 class_loader, true);
518 SetGcMapsMethod(dex_file, method);
519 it.Next();
520 }
521 while (it.HasNextVirtualMethod()) {
522 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
523 class_loader, false);
524 SetGcMapsMethod(dex_file, method);
525 it.Next();
526 }
527 }
528}
529
530void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
531 if (method == NULL) {
532 Thread::Current()->ClearException();
533 return;
534 }
535 uint16_t method_idx = method->GetDexMethodIndex();
536 MethodReference ref(&dex_file, method_idx);
537 CompiledMethod* compiled_method = GetCompiledMethod(ref);
538 if (compiled_method == NULL) {
539 return;
540 }
541 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
542 if (gc_map == NULL) {
543 return;
544 }
545 compiled_method->SetGcMap(*gc_map);
546}
547
Brian Carlstromae826982011-11-09 01:33:42 -0800548void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
549 for (size_t i = 0; i != dex_files.size(); ++i) {
550 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700551 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700552 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700553 }
554}
555
Brian Carlstrom8a487412011-08-29 20:08:52 -0700556void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700557 Runtime* runtime = Runtime::Current();
558 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700559 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700560 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700561 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700562 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700563 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700564 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
565 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700566 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700567 } else {
568 // TODO: we currently leave the entry blank for resolved
569 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700570 }
571 }
572}
573
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700574} // namespace art