blob: fba767dc068e0aebe73d8e000f57baf305c66e21 [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"
Brian Carlstrom1f870082011-08-23 16:02:11 -070014#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070015#include "stl_util.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070016
Ian Rogersa3760aa2011-11-14 14:32:37 -080017art::CompiledMethod* oatCompileMethod(const art::Compiler& compiler,
18 const art::DexFile::CodeItem* code_item,
19 uint32_t access_flags, uint32_t method_idx,
20 const art::ClassLoader* class_loader,
Ian Rogers0571d352011-11-03 19:51:38 -070021 const art::DexFile& dex_file, art::InstructionSet);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070022
23namespace art {
24
Shih-wei Liaoc486c112011-09-13 16:43:52 -070025namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070026 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070027 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070028 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Ian Rogers169c9a72011-11-13 20:13:17 -080029 ByteArray* CreateJniDlysmLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070030}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070031namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070032 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070033 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070034 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Ian Rogers169c9a72011-11-13 20:13:17 -080035 ByteArray* CreateJniDlysmLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070036}
37
Brian Carlstromae826982011-11-09 01:33:42 -080038Compiler::Compiler(InstructionSet instruction_set,
39 bool image,
40 const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -070041 : instruction_set_(instruction_set),
42 jni_compiler_(instruction_set),
43 image_(image),
Brian Carlstromae826982011-11-09 01:33:42 -080044 image_classes_(image_classes),
Brian Carlstromaded5f72011-10-07 17:15:04 -070045 verbose_(false) {
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() {
53 STLDeleteValues(&compiled_methods_);
54 STLDeleteValues(&compiled_invoke_stubs_);
55}
56
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070057ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
58 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -070059 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070060 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070061 } else {
62 CHECK(instruction_set == kArm || instruction_set == kThumb2);
63 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070064 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070065 }
66}
67
Ian Rogers169c9a72011-11-13 20:13:17 -080068ByteArray* Compiler::CreateJniDlysmLookupStub(InstructionSet instruction_set) {
69 switch (instruction_set) {
70 case kArm:
71 case kThumb2:
72 return arm::CreateJniDlysmLookupStub();
73 case kX86:
74 return x86::CreateJniDlysmLookupStub();
75 default:
76 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
77 return NULL;
78 }
79}
80
Ian Rogersad25ac52011-10-04 19:13:33 -070081ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
82 if (instruction_set == kX86) {
83 return x86::CreateAbstractMethodErrorStub();
84 } else {
85 CHECK(instruction_set == kArm || instruction_set == kThumb2);
86 // Generates resolution stub using ARM instruction set
87 return arm::CreateAbstractMethodErrorStub();
88 }
89}
90
Jesse Wilson254db0f2011-11-16 16:44:11 -050091void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -080092 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070093 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080094
95 PreCompile(class_loader, dex_files);
96 Compile(class_loader, dex_files);
97 PostCompile(class_loader, dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -070098}
99
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700100void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700101 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800102
Brian Carlstrom8a487412011-08-29 20:08:52 -0700103 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800104
Ian Rogers0571d352011-11-03 19:51:38 -0700105 // Find the dex_file
106 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
107 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800108 std::vector<const DexFile*> dex_files;
109 dex_files.push_back(&dex_file);
110
111 PreCompile(class_loader, dex_files);
112
Ian Rogers0571d352011-11-03 19:51:38 -0700113 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800114 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
115 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800116
117 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700118}
119
Brian Carlstromae826982011-11-09 01:33:42 -0800120void Compiler::Resolve(const ClassLoader* class_loader,
121 const std::vector<const DexFile*>& dex_files) {
122 for (size_t i = 0; i != dex_files.size(); ++i) {
123 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700124 CHECK(dex_file != NULL);
125 ResolveDexFile(class_loader, *dex_file);
126 }
127}
128
Brian Carlstromae826982011-11-09 01:33:42 -0800129void Compiler::PreCompile(const ClassLoader* class_loader,
130 const std::vector<const DexFile*>& dex_files) {
131 Resolve(class_loader, dex_files);
132 Verify(class_loader, dex_files);
133 InitializeClassesWithoutClinit(class_loader, dex_files);
134}
135
136void Compiler::PostCompile(const ClassLoader* class_loader,
137 const std::vector<const DexFile*>& dex_files) {
138 SetCodeAndDirectMethods(dex_files);
139}
140
141bool Compiler::IsImageClass(const std::string& descriptor) const {
142 if (image_classes_ == NULL) {
143 return true;
144 }
145 return image_classes_->find(descriptor) != image_classes_->end();
146}
147
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800148// Return true if the class should be skipped during compilation. We
149// never skip classes in the boot class loader. However, if we have a
150// non-boot class loader and we can resolve the class in the boot
151// class loader, we do skip the class. This happens if an app bundles
152// classes found in the boot classpath. Since at runtime we will
153// select the class from the boot classpath, do not attempt to resolve
154// or compile it now.
155static bool SkipClass(const ClassLoader* class_loader,
156 const DexFile& dex_file,
157 const DexFile::ClassDef& class_def) {
158 if (class_loader == NULL) {
159 return false;
160 }
161 const char* descriptor = dex_file.GetClassDescriptor(class_def);
162 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
163 Class* klass = class_linker->FindClass(descriptor, NULL);
164 if (klass == NULL) {
165 Thread* self = Thread::Current();
166 CHECK(self->IsExceptionPending());
167 self->ClearException();
168 return false;
169 }
170 return true;
171}
172
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700173void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800174 Thread* self = Thread::Current();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700175 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700177
Brian Carlstromae826982011-11-09 01:33:42 -0800178 // Strings are easy in that they always are simply resolved to literals in the same file
179 if (image_ && image_classes_ == NULL) {
180 // TODO: Add support for loading strings referenced by image_classes_
181 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700182 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
183 class_linker->ResolveString(dex_file, string_idx, dex_cache);
184 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700185 }
186
Brian Carlstrom845490b2011-09-19 15:56:53 -0700187 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700188 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
189 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700190 if (klass == NULL) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800191 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700192 Thread::Current()->ClearException();
193 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700194 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700195
196 // Method and Field are the worst. We can't resolve without either
197 // context from the code use (to disambiguate virtual vs direct
198 // method and instance vs static field) or from class
199 // definitions. While the compiler will resolve what it can as it
200 // needs it, here we try to resolve fields and methods used in class
201 // definitions, since many of them many never be referenced by
202 // generated code.
203 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
204 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800205 if (SkipClass(class_loader, dex_file, class_def)) {
206 continue;
207 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700208
209 // Note the class_data pointer advances through the headers,
210 // static fields, instance fields, direct methods, and virtual
211 // methods.
212 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700213 if (class_data == NULL) {
214 // empty class such as a marker interface
215 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700216 }
Ian Rogers0571d352011-11-03 19:51:38 -0700217 ClassDataItemIterator it(dex_file, class_data);
218 while (it.HasNextStaticField()) {
219 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
220 class_loader, true);
221 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700222 CHECK(self->IsExceptionPending());
223 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700224 }
Ian Rogers0571d352011-11-03 19:51:38 -0700225 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700226 }
Ian Rogers0571d352011-11-03 19:51:38 -0700227 while (it.HasNextInstanceField()) {
228 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
229 class_loader, false);
230 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700231 CHECK(self->IsExceptionPending());
232 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700233 }
Ian Rogers0571d352011-11-03 19:51:38 -0700234 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700235 }
Ian Rogers0571d352011-11-03 19:51:38 -0700236 while (it.HasNextDirectMethod()) {
237 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
238 class_loader, true);
239 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700240 CHECK(self->IsExceptionPending());
241 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700242 }
Ian Rogers0571d352011-11-03 19:51:38 -0700243 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700244 }
Ian Rogers0571d352011-11-03 19:51:38 -0700245 while (it.HasNextVirtualMethod()) {
246 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
247 class_loader, false);
248 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700249 CHECK(self->IsExceptionPending());
250 self->ClearException();
251 }
252 it.Next();
253 }
254 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700255 }
256}
257
Brian Carlstromae826982011-11-09 01:33:42 -0800258void Compiler::Verify(const ClassLoader* class_loader,
259 const std::vector<const DexFile*>& dex_files) {
260 for (size_t i = 0; i != dex_files.size(); ++i) {
261 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700262 CHECK(dex_file != NULL);
263 VerifyDexFile(class_loader, *dex_file);
264 }
265}
266
267void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700268 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700269 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700270 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
271 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700272 const char* descriptor = dex_file.GetClassDescriptor(class_def);
273 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700274 if (klass == NULL) {
275 Thread* self = Thread::Current();
276 CHECK(self->IsExceptionPending());
277 self->ClearException();
278 continue;
279 }
280 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700281 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700282
283 if (klass->IsErroneous()) {
284 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
285 CHECK(Thread::Current()->IsExceptionPending());
286 Thread::Current()->ClearException();
287 // We want to try verification again at run-time, so move back into the resolved state.
288 klass->SetStatus(Class::kStatusResolved);
289 }
290
jeffhao5cfd6fb2011-09-27 13:54:29 -0700291 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
292 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700293 }
jeffhaob4df5142011-09-19 20:25:32 -0700294 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700295}
296
Brian Carlstromae826982011-11-09 01:33:42 -0800297void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
298 const std::vector<const DexFile*>& dex_files) {
299 for (size_t i = 0; i != dex_files.size(); ++i) {
300 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700301 CHECK(dex_file != NULL);
302 InitializeClassesWithoutClinit(class_loader, *dex_file);
303 }
304}
305
306void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
307 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700308 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
309 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700310 const char* descriptor = dex_file.GetClassDescriptor(class_def);
311 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700312 if (klass != NULL) {
313 class_linker->EnsureInitialized(klass, false);
314 }
315 // clear any class not found or verification exceptions
316 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700317 }
318
319 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
320 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
321 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700322 if (klass == NULL) {
323 Thread::Current()->ClearException();
324 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700325 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
326 }
jeffhao98eacac2011-09-14 16:11:53 -0700327 }
328}
329
Brian Carlstromae826982011-11-09 01:33:42 -0800330void Compiler::Compile(const ClassLoader* class_loader,
331 const std::vector<const DexFile*>& dex_files) {
332 for (size_t i = 0; i != dex_files.size(); ++i) {
333 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700334 CHECK(dex_file != NULL);
335 CompileDexFile(class_loader, *dex_file);
336 }
337}
338
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700339void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700340 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
341 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700342 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700343 }
344}
345
Ian Rogers0571d352011-11-03 19:51:38 -0700346void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
347 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800348 if (SkipClass(class_loader, dex_file, class_def)) {
349 return;
350 }
Ian Rogers0571d352011-11-03 19:51:38 -0700351 const byte* class_data = dex_file.GetClassData(class_def);
352 if (class_data == NULL) {
353 // empty class, probably a marker interface
354 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700355 }
Ian Rogers0571d352011-11-03 19:51:38 -0700356 ClassDataItemIterator it(dex_file, class_data);
357 // Skip fields
358 while (it.HasNextStaticField()) {
359 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700360 }
Ian Rogers0571d352011-11-03 19:51:38 -0700361 while (it.HasNextInstanceField()) {
362 it.Next();
363 }
364 // Compile direct methods
365 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800366 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
367 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700368 it.Next();
369 }
370 // Compile virtual methods
371 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800372 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
373 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700374 it.Next();
375 }
376 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700377}
378
Ian Rogersa3760aa2011-11-14 14:32:37 -0800379void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
380 uint32_t method_idx, const ClassLoader* class_loader,
381 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700382 CompiledMethod* compiled_method = NULL;
Ian Rogers169c9a72011-11-13 20:13:17 -0800383 if ((access_flags & kAccNative) != 0) {
384 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700385 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800386 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700387 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800388 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
389 dex_file, kThumb2);
390 CHECK(compiled_method != NULL);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700391 }
392
393 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700394 MethodReference ref(&dex_file, method_idx);
395 CHECK(compiled_methods_.find(ref) == compiled_methods_.end())
396 << PrettyMethod(method_idx, dex_file);
397 compiled_methods_[ref] = compiled_method;
398 DCHECK(compiled_methods_.find(ref) != compiled_methods_.end())
399 << PrettyMethod(method_idx, dex_file);
400 DCHECK(GetCompiledMethod(ref) != NULL)
401 << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700402 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700403
Ian Rogers0571d352011-11-03 19:51:38 -0700404 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800405 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700406 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
407 if (compiled_invoke_stub == NULL) {
408 if (instruction_set_ == kX86) {
409 compiled_invoke_stub = art::x86::X86CreateInvokeStub(is_static, shorty);
410 } else {
411 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
412 // Generates invocation stub using ARM instruction set
413 compiled_invoke_stub = art::arm::ArmCreateInvokeStub(is_static, shorty);
414 }
415 CHECK(compiled_invoke_stub != NULL);
416 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700417 }
Ian Rogers0571d352011-11-03 19:51:38 -0700418 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700419}
420
Ian Rogers0571d352011-11-03 19:51:38 -0700421static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
422 std::string key(shorty);
423 if (is_static) {
424 key += "$"; // musn't be a shorty type character
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425 }
Ian Rogers0571d352011-11-03 19:51:38 -0700426 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700427}
428
Ian Rogers0571d352011-11-03 19:51:38 -0700429const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Brian Carlstromae826982011-11-09 01:33:42 -0800430 const std::string key = MakeInvokeStubKey(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700431 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700432 if (it == compiled_invoke_stubs_.end()) {
433 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700434 } else {
435 DCHECK(it->second != NULL);
436 return it->second;
437 }
438}
439
440void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
441 const CompiledInvokeStub* compiled_invoke_stub) {
442 std::string key = MakeInvokeStubKey(is_static, shorty);
443 compiled_invoke_stubs_[key] = compiled_invoke_stub;
444}
445
446CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
447 MethodTable::const_iterator it = compiled_methods_.find(ref);
448 if (it == compiled_methods_.end()) {
449 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700450 }
451 CHECK(it->second != NULL);
452 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700453}
454
Brian Carlstromae826982011-11-09 01:33:42 -0800455void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
456 for (size_t i = 0; i != dex_files.size(); ++i) {
457 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700458 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700459 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700460 }
461}
462
Brian Carlstrom8a487412011-08-29 20:08:52 -0700463void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700464 Runtime* runtime = Runtime::Current();
465 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700466 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700467 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700468 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700469 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700470 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700471 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
472 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700473 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700474 } else {
475 // TODO: we currently leave the entry blank for resolved
476 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700477 }
478 }
479}
480
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700481} // namespace art