blob: 137f2424ca91b96acedc8c2a6a3f6fbd2f0e49a3 [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:
77 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
78 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 Carlstrom0755ec52012-01-11 15:19:46 -0800341
Brian Carlstromffca45d2011-09-16 12:10:49 -0700342 }
343
344 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
345 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
346 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700347 if (klass == NULL) {
348 Thread::Current()->ClearException();
349 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700350 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
351 }
jeffhao98eacac2011-09-14 16:11:53 -0700352 }
353}
354
Brian Carlstromae826982011-11-09 01:33:42 -0800355void Compiler::Compile(const ClassLoader* class_loader,
356 const std::vector<const DexFile*>& dex_files) {
357 for (size_t i = 0; i != dex_files.size(); ++i) {
358 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700359 CHECK(dex_file != NULL);
360 CompileDexFile(class_loader, *dex_file);
361 }
362}
363
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700364void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700365 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
366 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700367 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700368 }
369}
370
Ian Rogers0571d352011-11-03 19:51:38 -0700371void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
372 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800373 if (SkipClass(class_loader, dex_file, class_def)) {
374 return;
375 }
Ian Rogers0571d352011-11-03 19:51:38 -0700376 const byte* class_data = dex_file.GetClassData(class_def);
377 if (class_data == NULL) {
378 // empty class, probably a marker interface
379 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700380 }
Ian Rogers0571d352011-11-03 19:51:38 -0700381 ClassDataItemIterator it(dex_file, class_data);
382 // Skip fields
383 while (it.HasNextStaticField()) {
384 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700385 }
Ian Rogers0571d352011-11-03 19:51:38 -0700386 while (it.HasNextInstanceField()) {
387 it.Next();
388 }
389 // Compile direct methods
390 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800391 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
392 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700393 it.Next();
394 }
395 // Compile virtual methods
396 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800397 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
398 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700399 it.Next();
400 }
401 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700402}
403
Ian Rogersa3760aa2011-11-14 14:32:37 -0800404void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
405 uint32_t method_idx, const ClassLoader* class_loader,
406 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700407 CompiledMethod* compiled_method = NULL;
Ian Rogers169c9a72011-11-13 20:13:17 -0800408 if ((access_flags & kAccNative) != 0) {
409 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700410 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800411 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700412 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800413 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
414 dex_file, kThumb2);
415 CHECK(compiled_method != NULL);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700416 }
417
418 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700419 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800420 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700421 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800422 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700423 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700424
Ian Rogers0571d352011-11-03 19:51:38 -0700425 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800426 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700427 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
428 if (compiled_invoke_stub == NULL) {
429 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800430 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700431 } else {
432 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
433 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800434 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700435 }
436 CHECK(compiled_invoke_stub != NULL);
437 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700438 }
Ian Rogers0571d352011-11-03 19:51:38 -0700439 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700440}
441
Ian Rogers0571d352011-11-03 19:51:38 -0700442static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
443 std::string key(shorty);
444 if (is_static) {
445 key += "$"; // musn't be a shorty type character
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700446 }
Ian Rogers0571d352011-11-03 19:51:38 -0700447 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700448}
449
Ian Rogers0571d352011-11-03 19:51:38 -0700450const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughes95572412011-12-13 18:14:20 -0800451 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700452 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700453 if (it == compiled_invoke_stubs_.end()) {
454 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700455 } else {
456 DCHECK(it->second != NULL);
457 return it->second;
458 }
459}
460
461void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
462 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughes95572412011-12-13 18:14:20 -0800463 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700464 compiled_invoke_stubs_[key] = compiled_invoke_stub;
465}
466
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800467CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
468 ClassTable::const_iterator it = compiled_classes_.find(ref);
469 if (it == compiled_classes_.end()) {
470 return NULL;
471 }
472 CHECK(it->second != NULL);
473 return it->second;
474}
475
Ian Rogers0571d352011-11-03 19:51:38 -0700476CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
477 MethodTable::const_iterator it = compiled_methods_.find(ref);
478 if (it == compiled_methods_.end()) {
479 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700480 }
481 CHECK(it->second != NULL);
482 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700483}
484
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800485void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
486 for (size_t i = 0; i != dex_files.size(); ++i) {
487 const DexFile* dex_file = dex_files[i];
488 CHECK(dex_file != NULL);
489 SetGcMapsDexFile(class_loader, *dex_file);
490 }
491}
492
493void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
494 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
495 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
496 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
497 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
498 const char* descriptor = dex_file.GetClassDescriptor(class_def);
499 Class* klass = class_linker->FindClass(descriptor, class_loader);
500 if (klass == NULL || !klass->IsVerified()) {
501 Thread::Current()->ClearException();
502 continue;
503 }
504 const byte* class_data = dex_file.GetClassData(class_def);
505 if (class_data == NULL) {
506 // empty class such as a marker interface
507 continue;
508 }
509 ClassDataItemIterator it(dex_file, class_data);
510 while (it.HasNextStaticField()) {
511 it.Next();
512 }
513 while (it.HasNextInstanceField()) {
514 it.Next();
515 }
516 while (it.HasNextDirectMethod()) {
517 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
518 class_loader, true);
519 SetGcMapsMethod(dex_file, method);
520 it.Next();
521 }
522 while (it.HasNextVirtualMethod()) {
523 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
524 class_loader, false);
525 SetGcMapsMethod(dex_file, method);
526 it.Next();
527 }
528 }
529}
530
531void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
532 if (method == NULL) {
533 Thread::Current()->ClearException();
534 return;
535 }
536 uint16_t method_idx = method->GetDexMethodIndex();
537 MethodReference ref(&dex_file, method_idx);
538 CompiledMethod* compiled_method = GetCompiledMethod(ref);
539 if (compiled_method == NULL) {
540 return;
541 }
542 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
543 if (gc_map == NULL) {
544 return;
545 }
546 compiled_method->SetGcMap(*gc_map);
547}
548
Brian Carlstromae826982011-11-09 01:33:42 -0800549void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
550 for (size_t i = 0; i != dex_files.size(); ++i) {
551 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700552 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700553 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700554 }
555}
556
Brian Carlstrom8a487412011-08-29 20:08:52 -0700557void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700558 Runtime* runtime = Runtime::Current();
559 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700560 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700561 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700562 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700563 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700564 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700565 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
566 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700567 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700568 } else {
569 // TODO: we currently leave the entry blank for resolved
570 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700571 }
572 }
573}
574
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700575} // namespace art