blob: c6705a6d3435483c543455d32a62c198155d130b [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);
22 ByteArray* ArmCreateResolutionTrampoline(bool is_static);
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);
27 ByteArray* X86CreateResolutionTrampoline(bool is_static);
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 Rogersad25ac52011-10-04 19:13:33 -070035ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set, bool is_static) {
36 if (instruction_set == kX86) {
37 return x86::X86CreateResolutionTrampoline(is_static);
38 } else {
39 CHECK(instruction_set == kArm || instruction_set == kThumb2);
40 // Generates resolution stub using ARM instruction set
41 return arm::ArmCreateResolutionTrampoline(is_static);
42 }
43}
44
45ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
46 if (instruction_set == kX86) {
47 return x86::CreateAbstractMethodErrorStub();
48 } else {
49 CHECK(instruction_set == kArm || instruction_set == kThumb2);
50 // Generates resolution stub using ARM instruction set
51 return arm::CreateAbstractMethodErrorStub();
52 }
53}
54
Brian Carlstrom8a487412011-08-29 20:08:52 -070055void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070056 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070057 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070058 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070059 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070060 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070061 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070062}
63
64void Compiler::CompileOne(Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070065 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom8a487412011-08-29 20:08:52 -070066 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
67 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070068 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070069 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070070 CompileMethod(method);
71 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070072}
73
74void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070075 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070076 for (size_t i = 0; i != class_path.size(); ++i) {
77 const DexFile* dex_file = class_path[i];
78 CHECK(dex_file != NULL);
79 ResolveDexFile(class_loader, *dex_file);
80 }
81}
82
83void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
84 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
85
86 // Strings are easy, they always are simply resolved to literals in the same file
87 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstromffca45d2011-09-16 12:10:49 -070088 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
89 class_linker->ResolveString(dex_file, string_idx, dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070090 }
91
Brian Carlstrom845490b2011-09-19 15:56:53 -070092 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -070093 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
94 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -070095 if (klass == NULL) {
96 Thread::Current()->ClearException();
97 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070098 }
Brian Carlstrom845490b2011-09-19 15:56:53 -070099
100 // Method and Field are the worst. We can't resolve without either
101 // context from the code use (to disambiguate virtual vs direct
102 // method and instance vs static field) or from class
103 // definitions. While the compiler will resolve what it can as it
104 // needs it, here we try to resolve fields and methods used in class
105 // definitions, since many of them many never be referenced by
106 // generated code.
107 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
108 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
109
110 // Note the class_data pointer advances through the headers,
111 // static fields, instance fields, direct methods, and virtual
112 // methods.
113 const byte* class_data = dex_file.GetClassData(class_def);
114
115 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
116 size_t num_static_fields = header.static_fields_size_;
117 size_t num_instance_fields = header.instance_fields_size_;
118 size_t num_direct_methods = header.direct_methods_size_;
119 size_t num_virtual_methods = header.virtual_methods_size_;
120
121 if (num_static_fields != 0) {
122 uint32_t last_idx = 0;
123 for (size_t i = 0; i < num_static_fields; ++i) {
124 DexFile::Field dex_field;
125 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700126 Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache,
127 class_loader, true);
128 if (field == NULL) {
129 Thread* self = Thread::Current();
130 CHECK(self->IsExceptionPending());
131 self->ClearException();
132 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700133 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700134 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700135 if (num_instance_fields != 0) {
136 uint32_t last_idx = 0;
137 for (size_t i = 0; i < num_instance_fields; ++i) {
138 DexFile::Field dex_field;
139 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700140 Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache,
141 class_loader, false);
142 if (field == NULL) {
143 Thread* self = Thread::Current();
144 CHECK(self->IsExceptionPending());
145 self->ClearException();
146 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700147 }
148 }
149 if (num_direct_methods != 0) {
150 uint32_t last_idx = 0;
151 for (size_t i = 0; i < num_direct_methods; ++i) {
152 DexFile::Method dex_method;
153 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700154 Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache,
155 class_loader, true);
156 if (method == NULL) {
157 Thread* self = Thread::Current();
158 CHECK(self->IsExceptionPending());
159 self->ClearException();
160 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700161 }
162 }
163 if (num_virtual_methods != 0) {
164 uint32_t last_idx = 0;
165 for (size_t i = 0; i < num_virtual_methods; ++i) {
166 DexFile::Method dex_method;
167 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700168 Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache,
169 class_loader, false);
170 if (method == NULL) {
171 Thread* self = Thread::Current();
172 CHECK(self->IsExceptionPending());
173 self->ClearException();
174 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700175 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700176 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700177 }
178}
179
jeffhao98eacac2011-09-14 16:11:53 -0700180void Compiler::Verify(const ClassLoader* class_loader) {
181 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
182 for (size_t i = 0; i != class_path.size(); ++i) {
183 const DexFile* dex_file = class_path[i];
184 CHECK(dex_file != NULL);
185 VerifyDexFile(class_loader, *dex_file);
186 }
187}
188
189void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700190 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700191 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700192 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
193 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700194 const char* descriptor = dex_file.GetClassDescriptor(class_def);
195 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700196 if (klass == NULL) {
197 Thread* self = Thread::Current();
198 CHECK(self->IsExceptionPending());
199 self->ClearException();
200 continue;
201 }
202 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700203 class_linker->VerifyClass(klass);
jeffhao5cfd6fb2011-09-27 13:54:29 -0700204 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
205 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700206 }
jeffhaob4df5142011-09-19 20:25:32 -0700207 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700208}
209
210void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
211 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
212 for (size_t i = 0; i != class_path.size(); ++i) {
213 const DexFile* dex_file = class_path[i];
214 CHECK(dex_file != NULL);
215 InitializeClassesWithoutClinit(class_loader, *dex_file);
216 }
217}
218
219void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
220 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700221 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
222 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700223 const char* descriptor = dex_file.GetClassDescriptor(class_def);
224 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700225 if (klass != NULL) {
226 class_linker->EnsureInitialized(klass, false);
227 }
228 // clear any class not found or verification exceptions
229 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700230 }
231
232 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
233 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
234 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700235 if (klass == NULL) {
236 Thread::Current()->ClearException();
237 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700238 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
239 }
jeffhao98eacac2011-09-14 16:11:53 -0700240 }
241}
242
Brian Carlstrom83db7722011-08-26 17:32:56 -0700243void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700244 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700245 for (size_t i = 0; i != class_path.size(); ++i) {
246 const DexFile* dex_file = class_path[i];
247 CHECK(dex_file != NULL);
248 CompileDexFile(class_loader, *dex_file);
249 }
250}
251
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700252void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
253 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700254 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
255 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700256 const char* descriptor = dex_file.GetClassDescriptor(class_def);
257 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700258 if (klass == NULL) {
259 // previous verification error will cause FindClass to throw
260 Thread* self = Thread::Current();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700261 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700262 self->ClearException();
263 } else {
264 CompileClass(klass);
265 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700266 }
267}
268
269void Compiler::CompileClass(Class* klass) {
270 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
271 CompileMethod(klass->GetDirectMethod(i));
272 }
273 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
274 CompileMethod(klass->GetVirtualMethod(i));
275 }
276}
277
278void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700279 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700280 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700281 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700282 // TODO: this is only necessary for tests
283 if (!method->IsRegistered()) {
284 method->UnregisterNative();
285 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700286 } else if (method->IsAbstract()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700287 ByteArray* abstract_method_error_stub = Runtime::Current()->GetAbstractMethodErrorStubArray();
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700288 if (instruction_set_ == kX86) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700289 method->SetCodeArray(abstract_method_error_stub, kX86);
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700290 } else {
291 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700292 method->SetCodeArray(abstract_method_error_stub, kArm);
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700293 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700294 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700295 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700296 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297 CHECK(method->GetCode() != NULL) << PrettyMethod(method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700298
Ian Rogers2c8f6532011-09-02 17:16:34 -0700299 if (instruction_set_ == kX86) {
300 art::x86::X86CreateInvokeStub(method);
301 } else {
302 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
303 // Generates invocation stub using ARM instruction set
304 art::arm::ArmCreateInvokeStub(method);
305 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700306 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700307}
308
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700309void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700310 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700311 for (size_t i = 0; i != class_path.size(); ++i) {
312 const DexFile* dex_file = class_path[i];
313 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700314 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700315 }
316}
317
Brian Carlstrom8a487412011-08-29 20:08:52 -0700318void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700319 Runtime* runtime = Runtime::Current();
320 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700321 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700322 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700323 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700324 Method* method = dex_cache->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700325 if ((method == NULL) || (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized())) {
326 ByteArray* res_trampoline = runtime->GetResolutionStubArray(method->IsStatic());
327 if (instruction_set_ == kX86) {
328 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline, kX86);
329 } else {
330 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
331 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline, kArm);
332 }
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700333 } else if (method->IsDirect()) {
334 code_and_direct_methods->SetResolvedDirectMethod(i, method);
335 } else {
336 // TODO: we currently leave the entry blank for resolved
337 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700338 }
339 }
340}
341
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700342} // namespace art