blob: d29e8cab06f1b0ee4b1c6570ac479faeddbaa730 [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();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070021}
22
23namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070024 ByteArray* CreateAbstractMethodErrorStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070025}
26
Brian Carlstrom16192862011-09-12 17:50:06 -070027Compiler::Compiler(InstructionSet insns) : instruction_set_(insns), jni_compiler_(insns),
28 verbose_(false) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070029 CHECK(!Runtime::Current()->IsStarted());
Shih-wei Liaoc486c112011-09-13 16:43:52 -070030 if (insns == kArm || insns == kThumb2) {
Ian Rogersbdb03912011-09-14 00:55:44 -070031 abstract_method_error_stub_ = arm::CreateAbstractMethodErrorStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070032 } else if (insns == kX86) {
Ian Rogersbdb03912011-09-14 00:55:44 -070033 abstract_method_error_stub_ = x86::CreateAbstractMethodErrorStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070034 }
35}
36
Brian Carlstrom8a487412011-08-29 20:08:52 -070037void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070038 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070039 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070040 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070041 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070042 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070043 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070044}
45
46void Compiler::CompileOne(Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070047 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom8a487412011-08-29 20:08:52 -070048 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
49 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070050 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070051 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070052 CompileMethod(method);
53 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070054}
55
56void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070057 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070058 for (size_t i = 0; i != class_path.size(); ++i) {
59 const DexFile* dex_file = class_path[i];
60 CHECK(dex_file != NULL);
61 ResolveDexFile(class_loader, *dex_file);
62 }
63}
64
65void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
66 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
67
68 // Strings are easy, they always are simply resolved to literals in the same file
69 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstromffca45d2011-09-16 12:10:49 -070070 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
71 class_linker->ResolveString(dex_file, string_idx, dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070072 }
73
Brian Carlstrom845490b2011-09-19 15:56:53 -070074 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -070075 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
76 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -070077 if (klass == NULL) {
78 Thread::Current()->ClearException();
79 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070080 }
Brian Carlstrom845490b2011-09-19 15:56:53 -070081
82 // Method and Field are the worst. We can't resolve without either
83 // context from the code use (to disambiguate virtual vs direct
84 // method and instance vs static field) or from class
85 // definitions. While the compiler will resolve what it can as it
86 // needs it, here we try to resolve fields and methods used in class
87 // definitions, since many of them many never be referenced by
88 // generated code.
89 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
90 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
91
92 // Note the class_data pointer advances through the headers,
93 // static fields, instance fields, direct methods, and virtual
94 // methods.
95 const byte* class_data = dex_file.GetClassData(class_def);
96
97 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
98 size_t num_static_fields = header.static_fields_size_;
99 size_t num_instance_fields = header.instance_fields_size_;
100 size_t num_direct_methods = header.direct_methods_size_;
101 size_t num_virtual_methods = header.virtual_methods_size_;
102
103 if (num_static_fields != 0) {
104 uint32_t last_idx = 0;
105 for (size_t i = 0; i < num_static_fields; ++i) {
106 DexFile::Field dex_field;
107 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700108 Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache,
109 class_loader, true);
110 if (field == NULL) {
111 Thread* self = Thread::Current();
112 CHECK(self->IsExceptionPending());
113 self->ClearException();
114 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700115 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700116 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700117 if (num_instance_fields != 0) {
118 uint32_t last_idx = 0;
119 for (size_t i = 0; i < num_instance_fields; ++i) {
120 DexFile::Field dex_field;
121 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700122 Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache,
123 class_loader, false);
124 if (field == NULL) {
125 Thread* self = Thread::Current();
126 CHECK(self->IsExceptionPending());
127 self->ClearException();
128 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700129 }
130 }
131 if (num_direct_methods != 0) {
132 uint32_t last_idx = 0;
133 for (size_t i = 0; i < num_direct_methods; ++i) {
134 DexFile::Method dex_method;
135 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700136 Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache,
137 class_loader, true);
138 if (method == NULL) {
139 Thread* self = Thread::Current();
140 CHECK(self->IsExceptionPending());
141 self->ClearException();
142 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700143 }
144 }
145 if (num_virtual_methods != 0) {
146 uint32_t last_idx = 0;
147 for (size_t i = 0; i < num_virtual_methods; ++i) {
148 DexFile::Method dex_method;
149 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700150 Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache,
151 class_loader, false);
152 if (method == NULL) {
153 Thread* self = Thread::Current();
154 CHECK(self->IsExceptionPending());
155 self->ClearException();
156 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700157 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700158 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700159 }
160}
161
jeffhao98eacac2011-09-14 16:11:53 -0700162void Compiler::Verify(const ClassLoader* class_loader) {
163 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
164 for (size_t i = 0; i != class_path.size(); ++i) {
165 const DexFile* dex_file = class_path[i];
166 CHECK(dex_file != NULL);
167 VerifyDexFile(class_loader, *dex_file);
168 }
169}
170
171void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700172 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700173 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700174 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
175 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700176 const char* descriptor = dex_file.GetClassDescriptor(class_def);
177 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700178 if (klass == NULL) {
179 Thread* self = Thread::Current();
180 CHECK(self->IsExceptionPending());
181 self->ClearException();
182 continue;
183 }
184 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700185 class_linker->VerifyClass(klass);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700186 CHECK(klass->IsVerified() || klass->IsErroneous()) << PrettyClass(klass);
187 //CHECK(!Thread::Current()->IsExceptionPending());
188 if (klass->IsErroneous()) {
189 Thread* self = Thread::Current();
190 if (self->IsExceptionPending()) {
191 UNIMPLEMENTED(WARNING) << "Verifier failed to cleanup exceptions internally";
192 self->ClearException();
193 }
194 }
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700195 }
jeffhaob4df5142011-09-19 20:25:32 -0700196 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700197}
198
199void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
200 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
201 for (size_t i = 0; i != class_path.size(); ++i) {
202 const DexFile* dex_file = class_path[i];
203 CHECK(dex_file != NULL);
204 InitializeClassesWithoutClinit(class_loader, *dex_file);
205 }
206}
207
208void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
209 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700210 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
211 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700212 const char* descriptor = dex_file.GetClassDescriptor(class_def);
213 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700214 if (klass != NULL) {
215 class_linker->EnsureInitialized(klass, false);
216 }
217 // clear any class not found or verification exceptions
218 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700219 }
220
221 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
222 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
223 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700224 if (klass == NULL) {
225 Thread::Current()->ClearException();
226 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700227 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
228 }
jeffhao98eacac2011-09-14 16:11:53 -0700229 }
230}
231
Brian Carlstrom83db7722011-08-26 17:32:56 -0700232void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700233 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700234 for (size_t i = 0; i != class_path.size(); ++i) {
235 const DexFile* dex_file = class_path[i];
236 CHECK(dex_file != NULL);
237 CompileDexFile(class_loader, *dex_file);
238 }
239}
240
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700241void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
242 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700243 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
244 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700245 const char* descriptor = dex_file.GetClassDescriptor(class_def);
246 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700247 if (klass == NULL) {
248 // previous verification error will cause FindClass to throw
249 Thread* self = Thread::Current();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700250 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700251 self->ClearException();
252 } else {
253 CompileClass(klass);
254 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700255 }
256}
257
258void Compiler::CompileClass(Class* klass) {
259 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
260 CompileMethod(klass->GetDirectMethod(i));
261 }
262 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
263 CompileMethod(klass->GetVirtualMethod(i));
264 }
265}
266
Ian Rogers2c8f6532011-09-02 17:16:34 -0700267namespace arm {
268 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700269}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700270namespace x86 {
271 void X86CreateInvokeStub(Method* method);
272}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700273
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700274void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700275 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700276 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700277 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700278 // TODO: this is only necessary for tests
279 if (!method->IsRegistered()) {
280 method->UnregisterNative();
281 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700282 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700283 DCHECK(abstract_method_error_stub_ != NULL);
284 if (instruction_set_ == kX86) {
285 method->SetCode(abstract_method_error_stub_, kX86);
286 } else {
287 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
288 method->SetCode(abstract_method_error_stub_, kArm);
289 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700290 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700291 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700292 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700293 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700294
Ian Rogers2c8f6532011-09-02 17:16:34 -0700295 if (instruction_set_ == kX86) {
296 art::x86::X86CreateInvokeStub(method);
297 } else {
298 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
299 // Generates invocation stub using ARM instruction set
300 art::arm::ArmCreateInvokeStub(method);
301 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700302 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700303}
304
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700305void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700306 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700307 for (size_t i = 0; i != class_path.size(); ++i) {
308 const DexFile* dex_file = class_path[i];
309 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700310 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700311 }
312}
313
Brian Carlstrom8a487412011-08-29 20:08:52 -0700314void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700315 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
316 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700317 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700318 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700319 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700320 if (method == NULL) {
321 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
322 } else if (method->IsDirect()) {
323 code_and_direct_methods->SetResolvedDirectMethod(i, method);
324 } else {
325 // TODO: we currently leave the entry blank for resolved
326 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700327 }
328 }
329}
330
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700331} // namespace art