blob: 0a4117d2016a013ebb3064f37033955333059385 [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
Brian Carlstrom8a487412011-08-29 20:08:52 -070085void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070086 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070087 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070088 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070089 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070090 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070091 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070092}
93
Brian Carlstrom3320cf42011-10-04 14:58:28 -070094void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070095 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom8a487412011-08-29 20:08:52 -070096 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
97 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070098 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070099 InitializeClassesWithoutClinit(class_loader);
Ian Rogers0571d352011-11-03 19:51:38 -0700100 // Find the dex_file
101 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
102 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
103 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800104 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
105 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700106 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700107}
108
109void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700110 const std::vector<const DexFile*>& class_path
111 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700112 for (size_t i = 0; i != class_path.size(); ++i) {
113 const DexFile* dex_file = class_path[i];
114 CHECK(dex_file != NULL);
115 ResolveDexFile(class_loader, *dex_file);
116 }
117}
118
119void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
120 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700121 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700122
123 // Strings are easy, they always are simply resolved to literals in the same file
Brian Carlstromaded5f72011-10-07 17:15:04 -0700124 if (IsImage()) { // Only resolve when we'll have an image, so compiler won't choose fast path
125 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
126 class_linker->ResolveString(dex_file, string_idx, dex_cache);
127 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700128 }
129
Brian Carlstrom845490b2011-09-19 15:56:53 -0700130 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700131 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
132 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700133 if (klass == NULL) {
134 Thread::Current()->ClearException();
135 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700136 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700137
138 // Method and Field are the worst. We can't resolve without either
139 // context from the code use (to disambiguate virtual vs direct
140 // method and instance vs static field) or from class
141 // definitions. While the compiler will resolve what it can as it
142 // needs it, here we try to resolve fields and methods used in class
143 // definitions, since many of them many never be referenced by
144 // generated code.
145 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
146 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
147
148 // Note the class_data pointer advances through the headers,
149 // static fields, instance fields, direct methods, and virtual
150 // methods.
151 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700152 if (class_data == NULL) {
153 // empty class such as a marker interface
154 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700155 }
Ian Rogers0571d352011-11-03 19:51:38 -0700156 ClassDataItemIterator it(dex_file, class_data);
157 while (it.HasNextStaticField()) {
158 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
159 class_loader, true);
160 if (field == NULL) {
161 Thread* self = Thread::Current();
162 CHECK(self->IsExceptionPending());
163 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700164 }
Ian Rogers0571d352011-11-03 19:51:38 -0700165 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700166 }
Ian Rogers0571d352011-11-03 19:51:38 -0700167 while (it.HasNextInstanceField()) {
168 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
169 class_loader, false);
170 if (field == NULL) {
171 Thread* self = Thread::Current();
172 CHECK(self->IsExceptionPending());
173 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700174 }
Ian Rogers0571d352011-11-03 19:51:38 -0700175 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700176 }
Ian Rogers0571d352011-11-03 19:51:38 -0700177 while (it.HasNextDirectMethod()) {
178 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
179 class_loader, true);
180 if (method == NULL) {
181 Thread* self = Thread::Current();
182 CHECK(self->IsExceptionPending());
183 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700184 }
Ian Rogers0571d352011-11-03 19:51:38 -0700185 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700186 }
Ian Rogers0571d352011-11-03 19:51:38 -0700187 while (it.HasNextVirtualMethod()) {
188 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
189 class_loader, false);
190 if (method == NULL) {
191 Thread* self = Thread::Current();
192 CHECK(self->IsExceptionPending());
193 self->ClearException();
194 }
195 it.Next();
196 }
197 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700198 }
199}
200
jeffhao98eacac2011-09-14 16:11:53 -0700201void Compiler::Verify(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700202 const std::vector<const DexFile*>& class_path
203 = ClassLoader::GetCompileTimeClassPath(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -0700204 for (size_t i = 0; i != class_path.size(); ++i) {
205 const DexFile* dex_file = class_path[i];
206 CHECK(dex_file != NULL);
207 VerifyDexFile(class_loader, *dex_file);
208 }
209}
210
211void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700212 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700213 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700214 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
215 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700216 const char* descriptor = dex_file.GetClassDescriptor(class_def);
217 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700218 if (klass == NULL) {
219 Thread* self = Thread::Current();
220 CHECK(self->IsExceptionPending());
221 self->ClearException();
222 continue;
223 }
224 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700225 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700226
227 if (klass->IsErroneous()) {
228 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
229 CHECK(Thread::Current()->IsExceptionPending());
230 Thread::Current()->ClearException();
231 // We want to try verification again at run-time, so move back into the resolved state.
232 klass->SetStatus(Class::kStatusResolved);
233 }
234
jeffhao5cfd6fb2011-09-27 13:54:29 -0700235 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
236 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700237 }
jeffhaob4df5142011-09-19 20:25:32 -0700238 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700239}
240
241void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700242 const std::vector<const DexFile*>& class_path
243 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700244 for (size_t i = 0; i != class_path.size(); ++i) {
245 const DexFile* dex_file = class_path[i];
246 CHECK(dex_file != NULL);
247 InitializeClassesWithoutClinit(class_loader, *dex_file);
248 }
249}
250
251void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
252 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700253 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
254 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700255 const char* descriptor = dex_file.GetClassDescriptor(class_def);
256 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700257 if (klass != NULL) {
258 class_linker->EnsureInitialized(klass, false);
259 }
260 // clear any class not found or verification exceptions
261 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700262 }
263
264 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
265 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
266 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700267 if (klass == NULL) {
268 Thread::Current()->ClearException();
269 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700270 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
271 }
jeffhao98eacac2011-09-14 16:11:53 -0700272 }
273}
274
Brian Carlstrom83db7722011-08-26 17:32:56 -0700275void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700276 const std::vector<const DexFile*>& class_path
277 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700278 for (size_t i = 0; i != class_path.size(); ++i) {
279 const DexFile* dex_file = class_path[i];
280 CHECK(dex_file != NULL);
281 CompileDexFile(class_loader, *dex_file);
282 }
283}
284
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700285void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700286 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
287 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700288 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700289 }
290}
291
Ian Rogers0571d352011-11-03 19:51:38 -0700292void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
293 const DexFile& dex_file) {
294 const byte* class_data = dex_file.GetClassData(class_def);
295 if (class_data == NULL) {
296 // empty class, probably a marker interface
297 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700298 }
Ian Rogers0571d352011-11-03 19:51:38 -0700299 ClassDataItemIterator it(dex_file, class_data);
300 // Skip fields
301 while (it.HasNextStaticField()) {
302 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700303 }
Ian Rogers0571d352011-11-03 19:51:38 -0700304 while (it.HasNextInstanceField()) {
305 it.Next();
306 }
307 // Compile direct methods
308 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800309 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
310 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700311 it.Next();
312 }
313 // Compile virtual methods
314 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800315 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
316 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700317 it.Next();
318 }
319 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700320}
321
Ian Rogersa3760aa2011-11-14 14:32:37 -0800322void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
323 uint32_t method_idx, const ClassLoader* class_loader,
324 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700325 CompiledMethod* compiled_method = NULL;
Ian Rogers169c9a72011-11-13 20:13:17 -0800326 if ((access_flags & kAccNative) != 0) {
327 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700328 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800329 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700330 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800331 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
332 dex_file, kThumb2);
333 CHECK(compiled_method != NULL);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700334 }
335
336 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700337 MethodReference ref(&dex_file, method_idx);
338 CHECK(compiled_methods_.find(ref) == compiled_methods_.end())
339 << PrettyMethod(method_idx, dex_file);
340 compiled_methods_[ref] = compiled_method;
341 DCHECK(compiled_methods_.find(ref) != compiled_methods_.end())
342 << PrettyMethod(method_idx, dex_file);
343 DCHECK(GetCompiledMethod(ref) != NULL)
344 << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700345 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700346
Ian Rogers0571d352011-11-03 19:51:38 -0700347 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800348 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700349 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
350 if (compiled_invoke_stub == NULL) {
351 if (instruction_set_ == kX86) {
352 compiled_invoke_stub = art::x86::X86CreateInvokeStub(is_static, shorty);
353 } else {
354 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
355 // Generates invocation stub using ARM instruction set
356 compiled_invoke_stub = art::arm::ArmCreateInvokeStub(is_static, shorty);
357 }
358 CHECK(compiled_invoke_stub != NULL);
359 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700360 }
Ian Rogers0571d352011-11-03 19:51:38 -0700361 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700362}
363
Ian Rogers0571d352011-11-03 19:51:38 -0700364static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
365 std::string key(shorty);
366 if (is_static) {
367 key += "$"; // musn't be a shorty type character
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700368 }
Ian Rogers0571d352011-11-03 19:51:38 -0700369 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700370}
371
Ian Rogers0571d352011-11-03 19:51:38 -0700372const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
373 std::string key = MakeInvokeStubKey(is_static, shorty);
374 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700375 if (it == compiled_invoke_stubs_.end()) {
376 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700377 } else {
378 DCHECK(it->second != NULL);
379 return it->second;
380 }
381}
382
383void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
384 const CompiledInvokeStub* compiled_invoke_stub) {
385 std::string key = MakeInvokeStubKey(is_static, shorty);
386 compiled_invoke_stubs_[key] = compiled_invoke_stub;
387}
388
389CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
390 MethodTable::const_iterator it = compiled_methods_.find(ref);
391 if (it == compiled_methods_.end()) {
392 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700393 }
394 CHECK(it->second != NULL);
395 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700396}
397
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700398void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700399 const std::vector<const DexFile*>& class_path
400 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700401 for (size_t i = 0; i != class_path.size(); ++i) {
402 const DexFile* dex_file = class_path[i];
403 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700404 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700405 }
406}
407
Brian Carlstrom8a487412011-08-29 20:08:52 -0700408void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700409 Runtime* runtime = Runtime::Current();
410 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700411 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700412 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700413 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700414 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700415 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700416 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
417 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700418 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700419 } else {
420 // TODO: we currently leave the entry blank for resolved
421 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700422 }
423 }
424}
425
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700426} // namespace art