blob: 88e156ded53ac65e26bb2400113d83ba2aaaa1a1 [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 Carlstrom2cc022b2011-08-25 10:05:39 -070011#include "jni_compiler.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070012#include "jni_internal.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070013#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080014#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070015#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070016#include "stl_util.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070017
Ian Rogersa3760aa2011-11-14 14:32:37 -080018art::CompiledMethod* oatCompileMethod(const art::Compiler& compiler,
19 const art::DexFile::CodeItem* code_item,
20 uint32_t access_flags, uint32_t method_idx,
21 const art::ClassLoader* class_loader,
Ian Rogers0571d352011-11-03 19:51:38 -070022 const art::DexFile& dex_file, art::InstructionSet);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070023
24namespace art {
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);
Ian Rogers169c9a72011-11-13 20:13:17 -080030 ByteArray* CreateJniDlysmLookupStub();
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);
Ian Rogers169c9a72011-11-13 20:13:17 -080036 ByteArray* CreateJniDlysmLookupStub();
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
Ian Rogers169c9a72011-11-13 20:13:17 -080069ByteArray* Compiler::CreateJniDlysmLookupStub(InstructionSet instruction_set) {
70 switch (instruction_set) {
71 case kArm:
72 case kThumb2:
73 return arm::CreateJniDlysmLookupStub();
74 case kX86:
75 return x86::CreateJniDlysmLookupStub();
76 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) {
139 SetCodeAndDirectMethods(dex_files);
140}
141
142bool Compiler::IsImageClass(const std::string& descriptor) const {
143 if (image_classes_ == NULL) {
144 return true;
145 }
146 return image_classes_->find(descriptor) != image_classes_->end();
147}
148
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800149bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
150 uint32_t type_idx) const {
151 if (!IsImage()) {
152 return false;
153 }
154 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
155 if (resolved_class == NULL) {
156 return false;
157 }
158 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
159}
160
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800161// Return true if the class should be skipped during compilation. We
162// never skip classes in the boot class loader. However, if we have a
163// non-boot class loader and we can resolve the class in the boot
164// class loader, we do skip the class. This happens if an app bundles
165// classes found in the boot classpath. Since at runtime we will
166// select the class from the boot classpath, do not attempt to resolve
167// or compile it now.
168static bool SkipClass(const ClassLoader* class_loader,
169 const DexFile& dex_file,
170 const DexFile::ClassDef& class_def) {
171 if (class_loader == NULL) {
172 return false;
173 }
174 const char* descriptor = dex_file.GetClassDescriptor(class_def);
175 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
176 Class* klass = class_linker->FindClass(descriptor, NULL);
177 if (klass == NULL) {
178 Thread* self = Thread::Current();
179 CHECK(self->IsExceptionPending());
180 self->ClearException();
181 return false;
182 }
183 return true;
184}
185
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700186void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800187 Thread* self = Thread::Current();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700188 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700189 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700190
Brian Carlstromae826982011-11-09 01:33:42 -0800191 // Strings are easy in that they always are simply resolved to literals in the same file
192 if (image_ && image_classes_ == NULL) {
193 // TODO: Add support for loading strings referenced by image_classes_
194 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700195 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
196 class_linker->ResolveString(dex_file, string_idx, dex_cache);
197 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700198 }
199
Brian Carlstrom845490b2011-09-19 15:56:53 -0700200 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700201 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
202 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700203 if (klass == NULL) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800204 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700205 Thread::Current()->ClearException();
206 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700207 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700208
209 // Method and Field are the worst. We can't resolve without either
210 // context from the code use (to disambiguate virtual vs direct
211 // method and instance vs static field) or from class
212 // definitions. While the compiler will resolve what it can as it
213 // needs it, here we try to resolve fields and methods used in class
214 // definitions, since many of them many never be referenced by
215 // generated code.
216 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
217 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800218 if (SkipClass(class_loader, dex_file, class_def)) {
219 continue;
220 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700221
222 // Note the class_data pointer advances through the headers,
223 // static fields, instance fields, direct methods, and virtual
224 // methods.
225 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700226 if (class_data == NULL) {
227 // empty class such as a marker interface
228 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700229 }
Ian Rogers0571d352011-11-03 19:51:38 -0700230 ClassDataItemIterator it(dex_file, class_data);
231 while (it.HasNextStaticField()) {
232 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
233 class_loader, true);
234 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700235 CHECK(self->IsExceptionPending());
236 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700237 }
Ian Rogers0571d352011-11-03 19:51:38 -0700238 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700239 }
Ian Rogers0571d352011-11-03 19:51:38 -0700240 while (it.HasNextInstanceField()) {
241 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
242 class_loader, false);
243 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700244 CHECK(self->IsExceptionPending());
245 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700246 }
Ian Rogers0571d352011-11-03 19:51:38 -0700247 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700248 }
Ian Rogers0571d352011-11-03 19:51:38 -0700249 while (it.HasNextDirectMethod()) {
250 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
251 class_loader, true);
252 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700253 CHECK(self->IsExceptionPending());
254 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700255 }
Ian Rogers0571d352011-11-03 19:51:38 -0700256 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700257 }
Ian Rogers0571d352011-11-03 19:51:38 -0700258 while (it.HasNextVirtualMethod()) {
259 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
260 class_loader, false);
261 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700262 CHECK(self->IsExceptionPending());
263 self->ClearException();
264 }
265 it.Next();
266 }
267 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700268 }
269}
270
Brian Carlstromae826982011-11-09 01:33:42 -0800271void Compiler::Verify(const ClassLoader* class_loader,
272 const std::vector<const DexFile*>& dex_files) {
273 for (size_t i = 0; i != dex_files.size(); ++i) {
274 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700275 CHECK(dex_file != NULL);
276 VerifyDexFile(class_loader, *dex_file);
277 }
278}
279
280void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700281 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700283 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
284 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700285 const char* descriptor = dex_file.GetClassDescriptor(class_def);
286 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700287 if (klass == NULL) {
288 Thread* self = Thread::Current();
289 CHECK(self->IsExceptionPending());
290 self->ClearException();
291 continue;
292 }
293 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700294 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700295
296 if (klass->IsErroneous()) {
297 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
298 CHECK(Thread::Current()->IsExceptionPending());
299 Thread::Current()->ClearException();
300 // We want to try verification again at run-time, so move back into the resolved state.
301 klass->SetStatus(Class::kStatusResolved);
302 }
303
jeffhao5cfd6fb2011-09-27 13:54:29 -0700304 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
305 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700306 }
jeffhaob4df5142011-09-19 20:25:32 -0700307 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700308}
309
Brian Carlstromae826982011-11-09 01:33:42 -0800310void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
311 const std::vector<const DexFile*>& dex_files) {
312 for (size_t i = 0; i != dex_files.size(); ++i) {
313 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700314 CHECK(dex_file != NULL);
315 InitializeClassesWithoutClinit(class_loader, *dex_file);
316 }
317}
318
319void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
320 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700321 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
322 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700323 const char* descriptor = dex_file.GetClassDescriptor(class_def);
324 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700325 if (klass != NULL) {
326 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800327 // record the final class status if necessary
328 Class::Status status = klass->GetStatus();
329 ClassReference ref(&dex_file, class_def_index);
330 CompiledClass* compiled_class = GetCompiledClass(ref);
331 if (compiled_class == NULL) {
332 compiled_class = new CompiledClass(status);
333 compiled_classes_[ref] = compiled_class;
334 } else {
335 DCHECK_EQ(status, compiled_class->GetStatus());
336 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700337 }
338 // clear any class not found or verification exceptions
339 Thread::Current()->ClearException();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800340
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) {
429 compiled_invoke_stub = art::x86::X86CreateInvokeStub(is_static, shorty);
430 } else {
431 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
432 // Generates invocation stub using ARM instruction set
433 compiled_invoke_stub = art::arm::ArmCreateInvokeStub(is_static, shorty);
434 }
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 Carlstromae826982011-11-09 01:33:42 -0800484void Compiler::SetCodeAndDirectMethods(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];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700487 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700488 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700489 }
490}
491
Brian Carlstrom8a487412011-08-29 20:08:52 -0700492void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700493 Runtime* runtime = Runtime::Current();
494 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700495 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700496 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700497 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700498 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700499 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700500 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
501 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700503 } else {
504 // TODO: we currently leave the entry blank for resolved
505 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700506 }
507 }
508}
509
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700510} // namespace art