blob: 7ed953bc342eb478f00529136f1566a3fc7baa59 [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() {
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
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800148bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
149 uint32_t type_idx) const {
150 if (!IsImage()) {
151 return false;
152 }
153 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
154 if (resolved_class == NULL) {
155 return false;
156 }
157 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
158}
159
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800160// Return true if the class should be skipped during compilation. We
161// never skip classes in the boot class loader. However, if we have a
162// non-boot class loader and we can resolve the class in the boot
163// class loader, we do skip the class. This happens if an app bundles
164// classes found in the boot classpath. Since at runtime we will
165// select the class from the boot classpath, do not attempt to resolve
166// or compile it now.
167static bool SkipClass(const ClassLoader* class_loader,
168 const DexFile& dex_file,
169 const DexFile::ClassDef& class_def) {
170 if (class_loader == NULL) {
171 return false;
172 }
173 const char* descriptor = dex_file.GetClassDescriptor(class_def);
174 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
175 Class* klass = class_linker->FindClass(descriptor, NULL);
176 if (klass == NULL) {
177 Thread* self = Thread::Current();
178 CHECK(self->IsExceptionPending());
179 self->ClearException();
180 return false;
181 }
182 return true;
183}
184
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700185void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800186 Thread* self = Thread::Current();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700187 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700188 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700189
Brian Carlstromae826982011-11-09 01:33:42 -0800190 // Strings are easy in that they always are simply resolved to literals in the same file
191 if (image_ && image_classes_ == NULL) {
192 // TODO: Add support for loading strings referenced by image_classes_
193 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700194 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
195 class_linker->ResolveString(dex_file, string_idx, dex_cache);
196 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700197 }
198
Brian Carlstrom845490b2011-09-19 15:56:53 -0700199 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700200 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
201 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700202 if (klass == NULL) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800203 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700204 Thread::Current()->ClearException();
205 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700206 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700207
208 // Method and Field are the worst. We can't resolve without either
209 // context from the code use (to disambiguate virtual vs direct
210 // method and instance vs static field) or from class
211 // definitions. While the compiler will resolve what it can as it
212 // needs it, here we try to resolve fields and methods used in class
213 // definitions, since many of them many never be referenced by
214 // generated code.
215 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
216 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800217 if (SkipClass(class_loader, dex_file, class_def)) {
218 continue;
219 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700220
221 // Note the class_data pointer advances through the headers,
222 // static fields, instance fields, direct methods, and virtual
223 // methods.
224 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700225 if (class_data == NULL) {
226 // empty class such as a marker interface
227 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700228 }
Ian Rogers0571d352011-11-03 19:51:38 -0700229 ClassDataItemIterator it(dex_file, class_data);
230 while (it.HasNextStaticField()) {
231 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
232 class_loader, true);
233 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700234 CHECK(self->IsExceptionPending());
235 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700236 }
Ian Rogers0571d352011-11-03 19:51:38 -0700237 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700238 }
Ian Rogers0571d352011-11-03 19:51:38 -0700239 while (it.HasNextInstanceField()) {
240 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
241 class_loader, false);
242 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700243 CHECK(self->IsExceptionPending());
244 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700245 }
Ian Rogers0571d352011-11-03 19:51:38 -0700246 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700247 }
Ian Rogers0571d352011-11-03 19:51:38 -0700248 while (it.HasNextDirectMethod()) {
249 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
250 class_loader, true);
251 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700252 CHECK(self->IsExceptionPending());
253 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700254 }
Ian Rogers0571d352011-11-03 19:51:38 -0700255 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700256 }
Ian Rogers0571d352011-11-03 19:51:38 -0700257 while (it.HasNextVirtualMethod()) {
258 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
259 class_loader, false);
260 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700261 CHECK(self->IsExceptionPending());
262 self->ClearException();
263 }
264 it.Next();
265 }
266 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700267 }
268}
269
Brian Carlstromae826982011-11-09 01:33:42 -0800270void Compiler::Verify(const ClassLoader* class_loader,
271 const std::vector<const DexFile*>& dex_files) {
272 for (size_t i = 0; i != dex_files.size(); ++i) {
273 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700274 CHECK(dex_file != NULL);
275 VerifyDexFile(class_loader, *dex_file);
276 }
277}
278
279void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700280 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700281 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700282 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
283 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700284 const char* descriptor = dex_file.GetClassDescriptor(class_def);
285 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700286 if (klass == NULL) {
287 Thread* self = Thread::Current();
288 CHECK(self->IsExceptionPending());
289 self->ClearException();
290 continue;
291 }
292 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700293 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700294
295 if (klass->IsErroneous()) {
296 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
297 CHECK(Thread::Current()->IsExceptionPending());
298 Thread::Current()->ClearException();
299 // We want to try verification again at run-time, so move back into the resolved state.
300 klass->SetStatus(Class::kStatusResolved);
301 }
302
jeffhao5cfd6fb2011-09-27 13:54:29 -0700303 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
304 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700305 }
jeffhaob4df5142011-09-19 20:25:32 -0700306 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700307}
308
Brian Carlstromae826982011-11-09 01:33:42 -0800309void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
310 const std::vector<const DexFile*>& dex_files) {
311 for (size_t i = 0; i != dex_files.size(); ++i) {
312 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700313 CHECK(dex_file != NULL);
314 InitializeClassesWithoutClinit(class_loader, *dex_file);
315 }
316}
317
318void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
319 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700320 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
321 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700322 const char* descriptor = dex_file.GetClassDescriptor(class_def);
323 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700324 if (klass != NULL) {
325 class_linker->EnsureInitialized(klass, false);
326 }
327 // clear any class not found or verification exceptions
328 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700329 }
330
331 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
332 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
333 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700334 if (klass == NULL) {
335 Thread::Current()->ClearException();
336 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700337 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
338 }
jeffhao98eacac2011-09-14 16:11:53 -0700339 }
340}
341
Brian Carlstromae826982011-11-09 01:33:42 -0800342void Compiler::Compile(const ClassLoader* class_loader,
343 const std::vector<const DexFile*>& dex_files) {
344 for (size_t i = 0; i != dex_files.size(); ++i) {
345 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700346 CHECK(dex_file != NULL);
347 CompileDexFile(class_loader, *dex_file);
348 }
349}
350
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700351void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700352 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
353 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700354 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700355 }
356}
357
Ian Rogers0571d352011-11-03 19:51:38 -0700358void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
359 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800360 if (SkipClass(class_loader, dex_file, class_def)) {
361 return;
362 }
Ian Rogers0571d352011-11-03 19:51:38 -0700363 const byte* class_data = dex_file.GetClassData(class_def);
364 if (class_data == NULL) {
365 // empty class, probably a marker interface
366 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700367 }
Ian Rogers0571d352011-11-03 19:51:38 -0700368 ClassDataItemIterator it(dex_file, class_data);
369 // Skip fields
370 while (it.HasNextStaticField()) {
371 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700372 }
Ian Rogers0571d352011-11-03 19:51:38 -0700373 while (it.HasNextInstanceField()) {
374 it.Next();
375 }
376 // Compile direct methods
377 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800378 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
379 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700380 it.Next();
381 }
382 // Compile virtual methods
383 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800384 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
385 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700386 it.Next();
387 }
388 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700389}
390
Ian Rogersa3760aa2011-11-14 14:32:37 -0800391void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
392 uint32_t method_idx, const ClassLoader* class_loader,
393 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700394 CompiledMethod* compiled_method = NULL;
Ian Rogers169c9a72011-11-13 20:13:17 -0800395 if ((access_flags & kAccNative) != 0) {
396 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700397 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800398 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700399 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800400 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
401 dex_file, kThumb2);
402 CHECK(compiled_method != NULL);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700403 }
404
405 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700406 MethodReference ref(&dex_file, method_idx);
407 CHECK(compiled_methods_.find(ref) == compiled_methods_.end())
408 << PrettyMethod(method_idx, dex_file);
409 compiled_methods_[ref] = compiled_method;
410 DCHECK(compiled_methods_.find(ref) != compiled_methods_.end())
411 << PrettyMethod(method_idx, dex_file);
412 DCHECK(GetCompiledMethod(ref) != NULL)
413 << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700414 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700415
Ian Rogers0571d352011-11-03 19:51:38 -0700416 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800417 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700418 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
419 if (compiled_invoke_stub == NULL) {
420 if (instruction_set_ == kX86) {
421 compiled_invoke_stub = art::x86::X86CreateInvokeStub(is_static, shorty);
422 } else {
423 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
424 // Generates invocation stub using ARM instruction set
425 compiled_invoke_stub = art::arm::ArmCreateInvokeStub(is_static, shorty);
426 }
427 CHECK(compiled_invoke_stub != NULL);
428 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700429 }
Ian Rogers0571d352011-11-03 19:51:38 -0700430 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700431}
432
Ian Rogers0571d352011-11-03 19:51:38 -0700433static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
434 std::string key(shorty);
435 if (is_static) {
436 key += "$"; // musn't be a shorty type character
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700437 }
Ian Rogers0571d352011-11-03 19:51:38 -0700438 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700439}
440
Ian Rogers0571d352011-11-03 19:51:38 -0700441const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Brian Carlstromae826982011-11-09 01:33:42 -0800442 const std::string key = MakeInvokeStubKey(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700443 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700444 if (it == compiled_invoke_stubs_.end()) {
445 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700446 } else {
447 DCHECK(it->second != NULL);
448 return it->second;
449 }
450}
451
452void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
453 const CompiledInvokeStub* compiled_invoke_stub) {
454 std::string key = MakeInvokeStubKey(is_static, shorty);
455 compiled_invoke_stubs_[key] = compiled_invoke_stub;
456}
457
458CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
459 MethodTable::const_iterator it = compiled_methods_.find(ref);
460 if (it == compiled_methods_.end()) {
461 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700462 }
463 CHECK(it->second != NULL);
464 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700465}
466
Brian Carlstromae826982011-11-09 01:33:42 -0800467void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
468 for (size_t i = 0; i != dex_files.size(); ++i) {
469 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700470 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700471 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700472 }
473}
474
Brian Carlstrom8a487412011-08-29 20:08:52 -0700475void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700476 Runtime* runtime = Runtime::Current();
477 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700478 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700479 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700480 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700481 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700483 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
484 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700485 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700486 } else {
487 // TODO: we currently leave the entry blank for resolved
488 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700489 }
490 }
491}
492
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700493} // namespace art