blob: 3b92267fc16d8ab5b3c4c18ea4f363363ea3d26e [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 Carlstrom1f870082011-08-23 16:02:11 -070013#include "runtime.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070014
Brian Carlstrom16192862011-09-12 17:50:06 -070015extern bool oatCompileMethod(const art::Compiler& compiler, art::Method*, art::InstructionSet);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070016
17namespace art {
18
Shih-wei Liaoc486c112011-09-13 16:43:52 -070019namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070020 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogersad25ac52011-10-04 19:13:33 -070021 void ArmCreateInvokeStub(Method* method);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070022 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Shih-wei Liaoc486c112011-09-13 16:43:52 -070023}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070024namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070025 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogersad25ac52011-10-04 19:13:33 -070026 void X86CreateInvokeStub(Method* method);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070027 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Brian Carlstrome24fa612011-09-29 00:53:55 -070028}
29
Brian Carlstrom16192862011-09-12 17:50:06 -070030Compiler::Compiler(InstructionSet insns) : instruction_set_(insns), jni_compiler_(insns),
31 verbose_(false) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070032 CHECK(!Runtime::Current()->IsStarted());
Shih-wei Liaoc486c112011-09-13 16:43:52 -070033}
34
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070035ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
36 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -070037 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070038 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070039 } else {
40 CHECK(instruction_set == kArm || instruction_set == kThumb2);
41 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070042 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070043 }
44}
45
46ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
47 if (instruction_set == kX86) {
48 return x86::CreateAbstractMethodErrorStub();
49 } else {
50 CHECK(instruction_set == kArm || instruction_set == kThumb2);
51 // Generates resolution stub using ARM instruction set
52 return arm::CreateAbstractMethodErrorStub();
53 }
54}
55
Brian Carlstrom8a487412011-08-29 20:08:52 -070056void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070057 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070058 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070059 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070060 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070061 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070062 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070063}
64
65void Compiler::CompileOne(Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070066 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom8a487412011-08-29 20:08:52 -070067 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
68 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070069 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070070 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070071 CompileMethod(method);
72 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070073}
74
75void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070076 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070077 for (size_t i = 0; i != class_path.size(); ++i) {
78 const DexFile* dex_file = class_path[i];
79 CHECK(dex_file != NULL);
80 ResolveDexFile(class_loader, *dex_file);
81 }
82}
83
84void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
85 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
86
87 // Strings are easy, they always are simply resolved to literals in the same file
88 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstromffca45d2011-09-16 12:10:49 -070089 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
90 class_linker->ResolveString(dex_file, string_idx, dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070091 }
92
Brian Carlstrom845490b2011-09-19 15:56:53 -070093 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -070094 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
95 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -070096 if (klass == NULL) {
97 Thread::Current()->ClearException();
98 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070099 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700100
101 // Method and Field are the worst. We can't resolve without either
102 // context from the code use (to disambiguate virtual vs direct
103 // method and instance vs static field) or from class
104 // definitions. While the compiler will resolve what it can as it
105 // needs it, here we try to resolve fields and methods used in class
106 // definitions, since many of them many never be referenced by
107 // generated code.
108 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
109 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
110
111 // Note the class_data pointer advances through the headers,
112 // static fields, instance fields, direct methods, and virtual
113 // methods.
114 const byte* class_data = dex_file.GetClassData(class_def);
115
116 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
117 size_t num_static_fields = header.static_fields_size_;
118 size_t num_instance_fields = header.instance_fields_size_;
119 size_t num_direct_methods = header.direct_methods_size_;
120 size_t num_virtual_methods = header.virtual_methods_size_;
121
122 if (num_static_fields != 0) {
123 uint32_t last_idx = 0;
124 for (size_t i = 0; i < num_static_fields; ++i) {
125 DexFile::Field dex_field;
126 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700127 Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache,
128 class_loader, true);
129 if (field == NULL) {
130 Thread* self = Thread::Current();
131 CHECK(self->IsExceptionPending());
132 self->ClearException();
133 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700134 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700135 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700136 if (num_instance_fields != 0) {
137 uint32_t last_idx = 0;
138 for (size_t i = 0; i < num_instance_fields; ++i) {
139 DexFile::Field dex_field;
140 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700141 Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache,
142 class_loader, false);
143 if (field == NULL) {
144 Thread* self = Thread::Current();
145 CHECK(self->IsExceptionPending());
146 self->ClearException();
147 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700148 }
149 }
150 if (num_direct_methods != 0) {
151 uint32_t last_idx = 0;
152 for (size_t i = 0; i < num_direct_methods; ++i) {
153 DexFile::Method dex_method;
154 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700155 Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache,
156 class_loader, true);
157 if (method == NULL) {
158 Thread* self = Thread::Current();
159 CHECK(self->IsExceptionPending());
160 self->ClearException();
161 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700162 }
163 }
164 if (num_virtual_methods != 0) {
165 uint32_t last_idx = 0;
166 for (size_t i = 0; i < num_virtual_methods; ++i) {
167 DexFile::Method dex_method;
168 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700169 Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache,
170 class_loader, false);
171 if (method == NULL) {
172 Thread* self = Thread::Current();
173 CHECK(self->IsExceptionPending());
174 self->ClearException();
175 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700176 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700177 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700178 }
179}
180
jeffhao98eacac2011-09-14 16:11:53 -0700181void Compiler::Verify(const ClassLoader* class_loader) {
182 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
183 for (size_t i = 0; i != class_path.size(); ++i) {
184 const DexFile* dex_file = class_path[i];
185 CHECK(dex_file != NULL);
186 VerifyDexFile(class_loader, *dex_file);
187 }
188}
189
190void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700191 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700192 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700193 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
194 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700195 const char* descriptor = dex_file.GetClassDescriptor(class_def);
196 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700197 if (klass == NULL) {
198 Thread* self = Thread::Current();
199 CHECK(self->IsExceptionPending());
200 self->ClearException();
201 continue;
202 }
203 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700204 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700205
206 if (klass->IsErroneous()) {
207 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
208 CHECK(Thread::Current()->IsExceptionPending());
209 Thread::Current()->ClearException();
210 // We want to try verification again at run-time, so move back into the resolved state.
211 klass->SetStatus(Class::kStatusResolved);
212 }
213
jeffhao5cfd6fb2011-09-27 13:54:29 -0700214 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
215 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700216 }
jeffhaob4df5142011-09-19 20:25:32 -0700217 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700218}
219
220void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
221 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
222 for (size_t i = 0; i != class_path.size(); ++i) {
223 const DexFile* dex_file = class_path[i];
224 CHECK(dex_file != NULL);
225 InitializeClassesWithoutClinit(class_loader, *dex_file);
226 }
227}
228
229void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
230 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700231 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
232 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700233 const char* descriptor = dex_file.GetClassDescriptor(class_def);
234 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700235 if (klass != NULL) {
236 class_linker->EnsureInitialized(klass, false);
237 }
238 // clear any class not found or verification exceptions
239 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700240 }
241
242 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
243 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
244 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700245 if (klass == NULL) {
246 Thread::Current()->ClearException();
247 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700248 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
249 }
jeffhao98eacac2011-09-14 16:11:53 -0700250 }
251}
252
Brian Carlstrom83db7722011-08-26 17:32:56 -0700253void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700254 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -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 CompileDexFile(class_loader, *dex_file);
259 }
260}
261
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700262void Compiler::CompileDexFile(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 Carlstrom9ea1cb12011-08-24 23:18:18 -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 // previous verification error will cause FindClass to throw
270 Thread* self = Thread::Current();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700271 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700272 self->ClearException();
273 } else {
274 CompileClass(klass);
275 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700276 }
277}
278
279void Compiler::CompileClass(Class* klass) {
280 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
281 CompileMethod(klass->GetDirectMethod(i));
282 }
283 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
284 CompileMethod(klass->GetVirtualMethod(i));
285 }
286}
287
288void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700289 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700290 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700291 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700292 // TODO: this is only necessary for tests
293 if (!method->IsRegistered()) {
294 method->UnregisterNative();
295 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700296 } else if (method->IsAbstract()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297 ByteArray* abstract_method_error_stub = Runtime::Current()->GetAbstractMethodErrorStubArray();
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700298 if (instruction_set_ == kX86) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700299 method->SetCodeArray(abstract_method_error_stub, kX86);
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700300 } else {
301 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700302 method->SetCodeArray(abstract_method_error_stub, kArm);
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700303 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700304 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700305 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700306 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700307 CHECK(method->GetCode() != NULL) << PrettyMethod(method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700308
Ian Rogers2c8f6532011-09-02 17:16:34 -0700309 if (instruction_set_ == kX86) {
310 art::x86::X86CreateInvokeStub(method);
311 } else {
312 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
313 // Generates invocation stub using ARM instruction set
314 art::arm::ArmCreateInvokeStub(method);
315 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700316 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700317}
318
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700319void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700320 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700321 for (size_t i = 0; i != class_path.size(); ++i) {
322 const DexFile* dex_file = class_path[i];
323 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700324 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700325 }
326}
327
Brian Carlstrom8a487412011-08-29 20:08:52 -0700328void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700329 Runtime* runtime = Runtime::Current();
330 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700331 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700332 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700333 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700334 Method* method = dex_cache->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700335 if ((method == NULL) || (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized())) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700336 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
337 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700338 if (instruction_set_ == kX86) {
339 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline, kX86);
340 } else {
341 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
342 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline, kArm);
343 }
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700344 } else if (method->IsDirect()) {
345 code_and_direct_methods->SetResolvedDirectMethod(i, method);
346 } else {
347 // TODO: we currently leave the entry blank for resolved
348 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700349 }
350 }
351}
352
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700353} // namespace art