blob: f4e887401ef108f93b5740cdab71da126b12dc21 [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);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700204
205 if (klass->IsErroneous()) {
206 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
207 CHECK(Thread::Current()->IsExceptionPending());
208 Thread::Current()->ClearException();
209 // We want to try verification again at run-time, so move back into the resolved state.
210 klass->SetStatus(Class::kStatusResolved);
211 }
212
jeffhao5cfd6fb2011-09-27 13:54:29 -0700213 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
214 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700215 }
jeffhaob4df5142011-09-19 20:25:32 -0700216 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700217}
218
219void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
220 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
221 for (size_t i = 0; i != class_path.size(); ++i) {
222 const DexFile* dex_file = class_path[i];
223 CHECK(dex_file != NULL);
224 InitializeClassesWithoutClinit(class_loader, *dex_file);
225 }
226}
227
228void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
229 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700230 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
231 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700232 const char* descriptor = dex_file.GetClassDescriptor(class_def);
233 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700234 if (klass != NULL) {
235 class_linker->EnsureInitialized(klass, false);
236 }
237 // clear any class not found or verification exceptions
238 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700239 }
240
241 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
242 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
243 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700244 if (klass == NULL) {
245 Thread::Current()->ClearException();
246 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700247 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
248 }
jeffhao98eacac2011-09-14 16:11:53 -0700249 }
250}
251
Brian Carlstrom83db7722011-08-26 17:32:56 -0700252void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700253 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700254 for (size_t i = 0; i != class_path.size(); ++i) {
255 const DexFile* dex_file = class_path[i];
256 CHECK(dex_file != NULL);
257 CompileDexFile(class_loader, *dex_file);
258 }
259}
260
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700261void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
262 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700263 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
264 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700265 const char* descriptor = dex_file.GetClassDescriptor(class_def);
266 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700267 if (klass == NULL) {
268 // previous verification error will cause FindClass to throw
269 Thread* self = Thread::Current();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700270 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700271 self->ClearException();
272 } else {
273 CompileClass(klass);
274 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700275 }
276}
277
278void Compiler::CompileClass(Class* klass) {
279 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
280 CompileMethod(klass->GetDirectMethod(i));
281 }
282 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
283 CompileMethod(klass->GetVirtualMethod(i));
284 }
285}
286
287void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700288 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700289 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700290 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700291 // TODO: this is only necessary for tests
292 if (!method->IsRegistered()) {
293 method->UnregisterNative();
294 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700295 } else if (method->IsAbstract()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700296 ByteArray* abstract_method_error_stub = Runtime::Current()->GetAbstractMethodErrorStubArray();
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700297 if (instruction_set_ == kX86) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700298 method->SetCodeArray(abstract_method_error_stub, kX86);
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700299 } else {
300 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700301 method->SetCodeArray(abstract_method_error_stub, kArm);
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700302 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700303 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700304 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700305 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700306 CHECK(method->GetCode() != NULL) << PrettyMethod(method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700307
Ian Rogers2c8f6532011-09-02 17:16:34 -0700308 if (instruction_set_ == kX86) {
309 art::x86::X86CreateInvokeStub(method);
310 } else {
311 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
312 // Generates invocation stub using ARM instruction set
313 art::arm::ArmCreateInvokeStub(method);
314 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700315 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700316}
317
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700318void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700319 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700320 for (size_t i = 0; i != class_path.size(); ++i) {
321 const DexFile* dex_file = class_path[i];
322 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700323 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700324 }
325}
326
Brian Carlstrom8a487412011-08-29 20:08:52 -0700327void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700328 Runtime* runtime = Runtime::Current();
329 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700330 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700331 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700332 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700333 Method* method = dex_cache->GetResolvedMethod(i);
Ian Rogersad25ac52011-10-04 19:13:33 -0700334 if ((method == NULL) || (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized())) {
335 ByteArray* res_trampoline = runtime->GetResolutionStubArray(method->IsStatic());
336 if (instruction_set_ == kX86) {
337 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline, kX86);
338 } else {
339 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
340 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline, kArm);
341 }
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700342 } else if (method->IsDirect()) {
343 code_and_direct_methods->SetResolvedDirectMethod(i, method);
344 } else {
345 // TODO: we currently leave the entry blank for resolved
346 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700347 }
348 }
349}
350
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700351} // namespace art