blob: 96beaa61481df694544bcb5fc22c7ba222802989 [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 Rogers0571d352011-11-03 19:51:38 -070017art::CompiledMethod* oatCompileMethod(const art::Compiler& compiler, bool is_direct,
18 uint32_t method_idx, const art::ClassLoader* class_loader,
19 const art::DexFile& dex_file, art::InstructionSet);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070020
21namespace art {
22
Shih-wei Liaoc486c112011-09-13 16:43:52 -070023namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070024 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070025 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070026 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Shih-wei Liaoc486c112011-09-13 16:43:52 -070027}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070028namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070029 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070030 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070031 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Brian Carlstrome24fa612011-09-29 00:53:55 -070032}
33
Brian Carlstromaded5f72011-10-07 17:15:04 -070034Compiler::Compiler(InstructionSet instruction_set, bool image)
35 : instruction_set_(instruction_set),
36 jni_compiler_(instruction_set),
37 image_(image),
38 verbose_(false) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070039 CHECK(!Runtime::Current()->IsStarted());
Shih-wei Liaoc486c112011-09-13 16:43:52 -070040}
41
Brian Carlstrom3320cf42011-10-04 14:58:28 -070042Compiler::~Compiler() {
43 STLDeleteValues(&compiled_methods_);
44 STLDeleteValues(&compiled_invoke_stubs_);
45}
46
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070047ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
48 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -070049 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070050 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070051 } else {
52 CHECK(instruction_set == kArm || instruction_set == kThumb2);
53 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070054 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070055 }
56}
57
58ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
59 if (instruction_set == kX86) {
60 return x86::CreateAbstractMethodErrorStub();
61 } else {
62 CHECK(instruction_set == kArm || instruction_set == kThumb2);
63 // Generates resolution stub using ARM instruction set
64 return arm::CreateAbstractMethodErrorStub();
65 }
66}
67
Brian Carlstrom8a487412011-08-29 20:08:52 -070068void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070069 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070070 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070071 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070072 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070073 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070074 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070075}
76
Brian Carlstrom3320cf42011-10-04 14:58:28 -070077void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070078 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom8a487412011-08-29 20:08:52 -070079 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
80 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070081 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070082 InitializeClassesWithoutClinit(class_loader);
Ian Rogers0571d352011-11-03 19:51:38 -070083 // Find the dex_file
84 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
85 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
86 uint32_t method_idx = method->GetDexMethodIndex();
87 CompileMethod(method->IsDirect(), method->IsNative(), method->IsStatic(), method->IsAbstract(),
88 method_idx, class_loader, dex_file);
Brian Carlstrom8a487412011-08-29 20:08:52 -070089 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070090}
91
92void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070093 const std::vector<const DexFile*>& class_path
94 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070095 for (size_t i = 0; i != class_path.size(); ++i) {
96 const DexFile* dex_file = class_path[i];
97 CHECK(dex_file != NULL);
98 ResolveDexFile(class_loader, *dex_file);
99 }
100}
101
102void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
103 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700104 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700105
106 // Strings are easy, they always are simply resolved to literals in the same file
Brian Carlstromaded5f72011-10-07 17:15:04 -0700107 if (IsImage()) { // Only resolve when we'll have an image, so compiler won't choose fast path
108 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
109 class_linker->ResolveString(dex_file, string_idx, dex_cache);
110 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700111 }
112
Brian Carlstrom845490b2011-09-19 15:56:53 -0700113 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700114 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
115 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700116 if (klass == NULL) {
117 Thread::Current()->ClearException();
118 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700119 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700120
121 // Method and Field are the worst. We can't resolve without either
122 // context from the code use (to disambiguate virtual vs direct
123 // method and instance vs static field) or from class
124 // definitions. While the compiler will resolve what it can as it
125 // needs it, here we try to resolve fields and methods used in class
126 // definitions, since many of them many never be referenced by
127 // generated code.
128 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
129 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
130
131 // Note the class_data pointer advances through the headers,
132 // static fields, instance fields, direct methods, and virtual
133 // methods.
134 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700135 if (class_data == NULL) {
136 // empty class such as a marker interface
137 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700138 }
Ian Rogers0571d352011-11-03 19:51:38 -0700139 ClassDataItemIterator it(dex_file, class_data);
140 while (it.HasNextStaticField()) {
141 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
142 class_loader, true);
143 if (field == NULL) {
144 Thread* self = Thread::Current();
145 CHECK(self->IsExceptionPending());
146 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700147 }
Ian Rogers0571d352011-11-03 19:51:38 -0700148 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700149 }
Ian Rogers0571d352011-11-03 19:51:38 -0700150 while (it.HasNextInstanceField()) {
151 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
152 class_loader, false);
153 if (field == NULL) {
154 Thread* self = Thread::Current();
155 CHECK(self->IsExceptionPending());
156 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700157 }
Ian Rogers0571d352011-11-03 19:51:38 -0700158 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700159 }
Ian Rogers0571d352011-11-03 19:51:38 -0700160 while (it.HasNextDirectMethod()) {
161 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
162 class_loader, true);
163 if (method == NULL) {
164 Thread* self = Thread::Current();
165 CHECK(self->IsExceptionPending());
166 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700167 }
Ian Rogers0571d352011-11-03 19:51:38 -0700168 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700169 }
Ian Rogers0571d352011-11-03 19:51:38 -0700170 while (it.HasNextVirtualMethod()) {
171 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
172 class_loader, false);
173 if (method == NULL) {
174 Thread* self = Thread::Current();
175 CHECK(self->IsExceptionPending());
176 self->ClearException();
177 }
178 it.Next();
179 }
180 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700181 }
182}
183
jeffhao98eacac2011-09-14 16:11:53 -0700184void Compiler::Verify(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700185 const std::vector<const DexFile*>& class_path
186 = ClassLoader::GetCompileTimeClassPath(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -0700187 for (size_t i = 0; i != class_path.size(); ++i) {
188 const DexFile* dex_file = class_path[i];
189 CHECK(dex_file != NULL);
190 VerifyDexFile(class_loader, *dex_file);
191 }
192}
193
194void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700195 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700196 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700197 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
198 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700199 const char* descriptor = dex_file.GetClassDescriptor(class_def);
200 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700201 if (klass == NULL) {
202 Thread* self = Thread::Current();
203 CHECK(self->IsExceptionPending());
204 self->ClearException();
205 continue;
206 }
207 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700208 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700209
210 if (klass->IsErroneous()) {
211 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
212 CHECK(Thread::Current()->IsExceptionPending());
213 Thread::Current()->ClearException();
214 // We want to try verification again at run-time, so move back into the resolved state.
215 klass->SetStatus(Class::kStatusResolved);
216 }
217
jeffhao5cfd6fb2011-09-27 13:54:29 -0700218 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
219 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700220 }
jeffhaob4df5142011-09-19 20:25:32 -0700221 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700222}
223
224void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700225 const std::vector<const DexFile*>& class_path
226 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700227 for (size_t i = 0; i != class_path.size(); ++i) {
228 const DexFile* dex_file = class_path[i];
229 CHECK(dex_file != NULL);
230 InitializeClassesWithoutClinit(class_loader, *dex_file);
231 }
232}
233
234void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
235 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700236 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
237 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700238 const char* descriptor = dex_file.GetClassDescriptor(class_def);
239 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700240 if (klass != NULL) {
241 class_linker->EnsureInitialized(klass, false);
242 }
243 // clear any class not found or verification exceptions
244 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700245 }
246
247 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
248 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
249 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700250 if (klass == NULL) {
251 Thread::Current()->ClearException();
252 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700253 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
254 }
jeffhao98eacac2011-09-14 16:11:53 -0700255 }
256}
257
Brian Carlstrom83db7722011-08-26 17:32:56 -0700258void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700259 const std::vector<const DexFile*>& class_path
260 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700261 for (size_t i = 0; i != class_path.size(); ++i) {
262 const DexFile* dex_file = class_path[i];
263 CHECK(dex_file != NULL);
264 CompileDexFile(class_loader, *dex_file);
265 }
266}
267
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700268void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700269 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
270 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700271 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700272 }
273}
274
Ian Rogers0571d352011-11-03 19:51:38 -0700275void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
276 const DexFile& dex_file) {
277 const byte* class_data = dex_file.GetClassData(class_def);
278 if (class_data == NULL) {
279 // empty class, probably a marker interface
280 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700281 }
Ian Rogers0571d352011-11-03 19:51:38 -0700282 ClassDataItemIterator it(dex_file, class_data);
283 // Skip fields
284 while (it.HasNextStaticField()) {
285 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700286 }
Ian Rogers0571d352011-11-03 19:51:38 -0700287 while (it.HasNextInstanceField()) {
288 it.Next();
289 }
290 // Compile direct methods
291 while (it.HasNextDirectMethod()) {
292 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
293 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
294 bool is_abstract = (it.GetMemberAccessFlags() & kAccAbstract) != 0;
295 CompileMethod(true, is_native, is_static, is_abstract, it.GetMemberIndex(), class_loader,
296 dex_file);
297 it.Next();
298 }
299 // Compile virtual methods
300 while (it.HasNextVirtualMethod()) {
301 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
302 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
303 bool is_abstract = (it.GetMemberAccessFlags() & kAccAbstract) != 0;
304 CompileMethod(false, is_native, is_static, is_abstract, it.GetMemberIndex(), class_loader,
305 dex_file);
306 it.Next();
307 }
308 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700309}
310
Ian Rogers0571d352011-11-03 19:51:38 -0700311void Compiler::CompileMethod(bool is_direct, bool is_native, bool is_static, bool is_abstract,
312 uint32_t method_idx, const ClassLoader* class_loader,
313 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700314 CompiledMethod* compiled_method = NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700315 if (is_native) {
316 compiled_method = jni_compiler_.Compile(is_direct, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700317 CHECK(compiled_method != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700318 } else if (is_abstract) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700319 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700320 compiled_method = oatCompileMethod(*this, is_direct, method_idx, class_loader, dex_file,
321 kThumb2);
322 // TODO: assert compiled_method is not NULL, currently NULL may be returned if the method
323 // wasn't resolved
Elliott Hughesf09afe82011-10-16 14:24:21 -0700324 }
325
326 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700327 MethodReference ref(&dex_file, method_idx);
328 CHECK(compiled_methods_.find(ref) == compiled_methods_.end())
329 << PrettyMethod(method_idx, dex_file);
330 compiled_methods_[ref] = compiled_method;
331 DCHECK(compiled_methods_.find(ref) != compiled_methods_.end())
332 << PrettyMethod(method_idx, dex_file);
333 DCHECK(GetCompiledMethod(ref) != NULL)
334 << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700335 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700336
Ian Rogers0571d352011-11-03 19:51:38 -0700337 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
338 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
339 if (compiled_invoke_stub == NULL) {
340 if (instruction_set_ == kX86) {
341 compiled_invoke_stub = art::x86::X86CreateInvokeStub(is_static, shorty);
342 } else {
343 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
344 // Generates invocation stub using ARM instruction set
345 compiled_invoke_stub = art::arm::ArmCreateInvokeStub(is_static, shorty);
346 }
347 CHECK(compiled_invoke_stub != NULL);
348 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700349 }
Ian Rogers0571d352011-11-03 19:51:38 -0700350 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700351}
352
Ian Rogers0571d352011-11-03 19:51:38 -0700353static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
354 std::string key(shorty);
355 if (is_static) {
356 key += "$"; // musn't be a shorty type character
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700357 }
Ian Rogers0571d352011-11-03 19:51:38 -0700358 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700359}
360
Ian Rogers0571d352011-11-03 19:51:38 -0700361const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
362 std::string key = MakeInvokeStubKey(is_static, shorty);
363 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700364 if (it == compiled_invoke_stubs_.end()) {
365 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700366 } else {
367 DCHECK(it->second != NULL);
368 return it->second;
369 }
370}
371
372void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
373 const CompiledInvokeStub* compiled_invoke_stub) {
374 std::string key = MakeInvokeStubKey(is_static, shorty);
375 compiled_invoke_stubs_[key] = compiled_invoke_stub;
376}
377
378CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
379 MethodTable::const_iterator it = compiled_methods_.find(ref);
380 if (it == compiled_methods_.end()) {
381 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700382 }
383 CHECK(it->second != NULL);
384 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700385}
386
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700387void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700388 const std::vector<const DexFile*>& class_path
389 = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700390 for (size_t i = 0; i != class_path.size(); ++i) {
391 const DexFile* dex_file = class_path[i];
392 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700393 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700394 }
395}
396
Brian Carlstrom8a487412011-08-29 20:08:52 -0700397void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700398 Runtime* runtime = Runtime::Current();
399 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700400 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700401 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700402 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700403 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700404 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700405 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
406 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700407 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700408 } else {
409 // TODO: we currently leave the entry blank for resolved
410 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700411 }
412 }
413}
414
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700415} // namespace art