blob: fabd8fdb82dc8b1b6245364aad2e7221dcac4daf [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 Carlstromaded5f72011-10-07 17:15:04 -070038Compiler::Compiler(InstructionSet instruction_set, bool image)
39 : instruction_set_(instruction_set),
40 jni_compiler_(instruction_set),
41 image_(image),
42 verbose_(false) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070043 CHECK(!Runtime::Current()->IsStarted());
Shih-wei Liaoc486c112011-09-13 16:43:52 -070044}
45
Brian Carlstrom3320cf42011-10-04 14:58:28 -070046Compiler::~Compiler() {
47 STLDeleteValues(&compiled_methods_);
48 STLDeleteValues(&compiled_invoke_stubs_);
49}
50
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070051ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
52 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -070053 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070054 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070055 } else {
56 CHECK(instruction_set == kArm || instruction_set == kThumb2);
57 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070058 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070059 }
60}
61
Ian Rogers169c9a72011-11-13 20:13:17 -080062ByteArray* Compiler::CreateJniDlysmLookupStub(InstructionSet instruction_set) {
63 switch (instruction_set) {
64 case kArm:
65 case kThumb2:
66 return arm::CreateJniDlysmLookupStub();
67 case kX86:
68 return x86::CreateJniDlysmLookupStub();
69 default:
70 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
71 return NULL;
72 }
73}
74
Ian Rogersad25ac52011-10-04 19:13:33 -070075ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
76 if (instruction_set == kX86) {
77 return x86::CreateAbstractMethodErrorStub();
78 } else {
79 CHECK(instruction_set == kArm || instruction_set == kThumb2);
80 // Generates resolution stub using ARM instruction set
81 return arm::CreateAbstractMethodErrorStub();
82 }
83}
84
Jesse Wilson254db0f2011-11-16 16:44:11 -050085void Compiler::CompileAll(const ClassLoader* class_loader,
86 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070087 DCHECK(!Runtime::Current()->IsStarted());
Jesse Wilson254db0f2011-11-16 16:44:11 -050088 for (size_t i = 0; i != dex_files.size(); ++i) {
89 ResolveDexFile(class_loader, *dex_files[i]);
90 }
91 for (size_t i = 0; i != dex_files.size(); ++i) {
92 VerifyDexFile(class_loader, *dex_files[i]);
93 }
94 for (size_t i = 0; i != dex_files.size(); ++i) {
95 InitializeClassesWithoutClinit(class_loader, *dex_files[i]);
96 }
97 for (size_t i = 0; i != dex_files.size(); ++i) {
98 CompileDexFile(class_loader, *dex_files[i]);
99 }
100 for (size_t i = 0; i != dex_files.size(); ++i) {
101 SetCodeAndDirectMethodsDexFile(*dex_files[i]);
102 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700103}
104
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700105void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700106 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom8a487412011-08-29 20:08:52 -0700107 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
108 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -0700109 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700110 InitializeClassesWithoutClinit(class_loader);
Ian Rogers0571d352011-11-03 19:51:38 -0700111 // Find the dex_file
112 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
113 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
114 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 Carlstrom8a487412011-08-29 20:08:52 -0700117 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700118}
119
120void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700121 const std::vector<const DexFile*>& class_path
122 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700123 for (size_t i = 0; i != class_path.size(); ++i) {
124 const DexFile* dex_file = class_path[i];
125 CHECK(dex_file != NULL);
126 ResolveDexFile(class_loader, *dex_file);
127 }
128}
129
130void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
131 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700132 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700133
134 // Strings are easy, they always are simply resolved to literals in the same file
Brian Carlstromaded5f72011-10-07 17:15:04 -0700135 if (IsImage()) { // Only resolve when we'll have an image, so compiler won't choose fast path
136 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
137 class_linker->ResolveString(dex_file, string_idx, dex_cache);
138 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700139 }
140
Brian Carlstrom845490b2011-09-19 15:56:53 -0700141 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700142 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
143 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700144 if (klass == NULL) {
145 Thread::Current()->ClearException();
146 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700147 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700148
149 // Method and Field are the worst. We can't resolve without either
150 // context from the code use (to disambiguate virtual vs direct
151 // method and instance vs static field) or from class
152 // definitions. While the compiler will resolve what it can as it
153 // needs it, here we try to resolve fields and methods used in class
154 // definitions, since many of them many never be referenced by
155 // generated code.
156 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
157 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
158
159 // Note the class_data pointer advances through the headers,
160 // static fields, instance fields, direct methods, and virtual
161 // methods.
162 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700163 if (class_data == NULL) {
164 // empty class such as a marker interface
165 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700166 }
Ian Rogers0571d352011-11-03 19:51:38 -0700167 ClassDataItemIterator it(dex_file, class_data);
168 while (it.HasNextStaticField()) {
169 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
170 class_loader, true);
171 if (field == NULL) {
172 Thread* self = Thread::Current();
173 CHECK(self->IsExceptionPending());
174 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700175 }
Ian Rogers0571d352011-11-03 19:51:38 -0700176 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700177 }
Ian Rogers0571d352011-11-03 19:51:38 -0700178 while (it.HasNextInstanceField()) {
179 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
180 class_loader, false);
181 if (field == NULL) {
182 Thread* self = Thread::Current();
183 CHECK(self->IsExceptionPending());
184 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700185 }
Ian Rogers0571d352011-11-03 19:51:38 -0700186 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700187 }
Ian Rogers0571d352011-11-03 19:51:38 -0700188 while (it.HasNextDirectMethod()) {
189 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
190 class_loader, true);
191 if (method == NULL) {
192 Thread* self = Thread::Current();
193 CHECK(self->IsExceptionPending());
194 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700195 }
Ian Rogers0571d352011-11-03 19:51:38 -0700196 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700197 }
Ian Rogers0571d352011-11-03 19:51:38 -0700198 while (it.HasNextVirtualMethod()) {
199 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
200 class_loader, false);
201 if (method == NULL) {
202 Thread* self = Thread::Current();
203 CHECK(self->IsExceptionPending());
204 self->ClearException();
205 }
206 it.Next();
207 }
208 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700209 }
210}
211
jeffhao98eacac2011-09-14 16:11:53 -0700212void Compiler::Verify(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700213 const std::vector<const DexFile*>& class_path
214 = ClassLoader::GetCompileTimeClassPath(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -0700215 for (size_t i = 0; i != class_path.size(); ++i) {
216 const DexFile* dex_file = class_path[i];
217 CHECK(dex_file != NULL);
218 VerifyDexFile(class_loader, *dex_file);
219 }
220}
221
222void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700223 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700224 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700225 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
226 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700227 const char* descriptor = dex_file.GetClassDescriptor(class_def);
228 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700229 if (klass == NULL) {
230 Thread* self = Thread::Current();
231 CHECK(self->IsExceptionPending());
232 self->ClearException();
233 continue;
234 }
235 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700236 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700237
238 if (klass->IsErroneous()) {
239 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
240 CHECK(Thread::Current()->IsExceptionPending());
241 Thread::Current()->ClearException();
242 // We want to try verification again at run-time, so move back into the resolved state.
243 klass->SetStatus(Class::kStatusResolved);
244 }
245
jeffhao5cfd6fb2011-09-27 13:54:29 -0700246 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
247 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700248 }
jeffhaob4df5142011-09-19 20:25:32 -0700249 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700250}
251
252void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700253 const std::vector<const DexFile*>& class_path
254 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700255 for (size_t i = 0; i != class_path.size(); ++i) {
256 const DexFile* dex_file = class_path[i];
257 CHECK(dex_file != NULL);
258 InitializeClassesWithoutClinit(class_loader, *dex_file);
259 }
260}
261
262void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
263 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700264 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
265 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700266 const char* descriptor = dex_file.GetClassDescriptor(class_def);
267 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700268 if (klass != NULL) {
269 class_linker->EnsureInitialized(klass, false);
270 }
271 // clear any class not found or verification exceptions
272 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700273 }
274
275 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
276 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
277 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700278 if (klass == NULL) {
279 Thread::Current()->ClearException();
280 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700281 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
282 }
jeffhao98eacac2011-09-14 16:11:53 -0700283 }
284}
285
Brian Carlstrom83db7722011-08-26 17:32:56 -0700286void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700287 const std::vector<const DexFile*>& class_path
288 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700289 for (size_t i = 0; i != class_path.size(); ++i) {
290 const DexFile* dex_file = class_path[i];
291 CHECK(dex_file != NULL);
292 CompileDexFile(class_loader, *dex_file);
293 }
294}
295
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700296void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700297 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
298 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700299 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700300 }
301}
302
Ian Rogers0571d352011-11-03 19:51:38 -0700303void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
304 const DexFile& dex_file) {
305 const byte* class_data = dex_file.GetClassData(class_def);
306 if (class_data == NULL) {
307 // empty class, probably a marker interface
308 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700309 }
Ian Rogers0571d352011-11-03 19:51:38 -0700310 ClassDataItemIterator it(dex_file, class_data);
311 // Skip fields
312 while (it.HasNextStaticField()) {
313 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700314 }
Ian Rogers0571d352011-11-03 19:51:38 -0700315 while (it.HasNextInstanceField()) {
316 it.Next();
317 }
318 // Compile direct methods
319 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800320 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
321 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700322 it.Next();
323 }
324 // Compile virtual methods
325 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800326 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
327 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700328 it.Next();
329 }
330 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700331}
332
Ian Rogersa3760aa2011-11-14 14:32:37 -0800333void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
334 uint32_t method_idx, const ClassLoader* class_loader,
335 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700336 CompiledMethod* compiled_method = NULL;
Ian Rogers169c9a72011-11-13 20:13:17 -0800337 if ((access_flags & kAccNative) != 0) {
338 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700339 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800340 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700341 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800342 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
343 dex_file, kThumb2);
344 CHECK(compiled_method != NULL);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700345 }
346
347 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700348 MethodReference ref(&dex_file, method_idx);
349 CHECK(compiled_methods_.find(ref) == compiled_methods_.end())
350 << PrettyMethod(method_idx, dex_file);
351 compiled_methods_[ref] = compiled_method;
352 DCHECK(compiled_methods_.find(ref) != compiled_methods_.end())
353 << PrettyMethod(method_idx, dex_file);
354 DCHECK(GetCompiledMethod(ref) != NULL)
355 << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700356 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700357
Ian Rogers0571d352011-11-03 19:51:38 -0700358 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800359 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700360 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
361 if (compiled_invoke_stub == NULL) {
362 if (instruction_set_ == kX86) {
363 compiled_invoke_stub = art::x86::X86CreateInvokeStub(is_static, shorty);
364 } else {
365 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
366 // Generates invocation stub using ARM instruction set
367 compiled_invoke_stub = art::arm::ArmCreateInvokeStub(is_static, shorty);
368 }
369 CHECK(compiled_invoke_stub != NULL);
370 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700371 }
Ian Rogers0571d352011-11-03 19:51:38 -0700372 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373}
374
Ian Rogers0571d352011-11-03 19:51:38 -0700375static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
376 std::string key(shorty);
377 if (is_static) {
378 key += "$"; // musn't be a shorty type character
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700379 }
Ian Rogers0571d352011-11-03 19:51:38 -0700380 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700381}
382
Ian Rogers0571d352011-11-03 19:51:38 -0700383const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
384 std::string key = MakeInvokeStubKey(is_static, shorty);
385 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700386 if (it == compiled_invoke_stubs_.end()) {
387 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700388 } else {
389 DCHECK(it->second != NULL);
390 return it->second;
391 }
392}
393
394void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
395 const CompiledInvokeStub* compiled_invoke_stub) {
396 std::string key = MakeInvokeStubKey(is_static, shorty);
397 compiled_invoke_stubs_[key] = compiled_invoke_stub;
398}
399
400CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
401 MethodTable::const_iterator it = compiled_methods_.find(ref);
402 if (it == compiled_methods_.end()) {
403 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700404 }
405 CHECK(it->second != NULL);
406 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700407}
408
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700409void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700410 const std::vector<const DexFile*>& class_path
411 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700412 for (size_t i = 0; i != class_path.size(); ++i) {
413 const DexFile* dex_file = class_path[i];
414 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700415 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700416 }
417}
418
Brian Carlstrom8a487412011-08-29 20:08:52 -0700419void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700420 Runtime* runtime = Runtime::Current();
421 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700422 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700423 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700424 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700425 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700426 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700427 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
428 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700429 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700430 } else {
431 // TODO: we currently leave the entry blank for resolved
432 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700433 }
434 }
435}
436
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700437} // namespace art