blob: 54793bad65dd809f91e3d584759f6ad518e20779 [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);
108 class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache, class_loader, true);
109 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700110 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700111 if (num_instance_fields != 0) {
112 uint32_t last_idx = 0;
113 for (size_t i = 0; i < num_instance_fields; ++i) {
114 DexFile::Field dex_field;
115 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
116 class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache, class_loader, false);
117 }
118 }
119 if (num_direct_methods != 0) {
120 uint32_t last_idx = 0;
121 for (size_t i = 0; i < num_direct_methods; ++i) {
122 DexFile::Method dex_method;
123 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
124 class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache, class_loader,
125 true);
126 }
127 }
128 if (num_virtual_methods != 0) {
129 uint32_t last_idx = 0;
130 for (size_t i = 0; i < num_virtual_methods; ++i) {
131 DexFile::Method dex_method;
132 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
133 class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache, class_loader,
134 false);
135 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700136 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700137 }
138}
139
jeffhao98eacac2011-09-14 16:11:53 -0700140void Compiler::Verify(const ClassLoader* class_loader) {
141 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
142 for (size_t i = 0; i != class_path.size(); ++i) {
143 const DexFile* dex_file = class_path[i];
144 CHECK(dex_file != NULL);
145 VerifyDexFile(class_loader, *dex_file);
146 }
147}
148
149void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700150 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700151 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700152 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
153 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700154 const char* descriptor = dex_file.GetClassDescriptor(class_def);
155 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700156 CHECK(klass->IsResolved());
jeffhao98eacac2011-09-14 16:11:53 -0700157 CHECK(klass != NULL);
158 class_linker->VerifyClass(klass);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700159 CHECK(klass->IsVerified() || klass->IsErroneous());
160 }
jeffhaob4df5142011-09-19 20:25:32 -0700161 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700162}
163
164void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
165 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
166 for (size_t i = 0; i != class_path.size(); ++i) {
167 const DexFile* dex_file = class_path[i];
168 CHECK(dex_file != NULL);
169 InitializeClassesWithoutClinit(class_loader, *dex_file);
170 }
171}
172
173void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
174 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700175 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
176 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700177 const char* descriptor = dex_file.GetClassDescriptor(class_def);
178 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700179 if (klass != NULL) {
180 class_linker->EnsureInitialized(klass, false);
181 }
182 // clear any class not found or verification exceptions
183 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700184 }
185
186 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
187 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
188 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700189 if (klass == NULL) {
190 Thread::Current()->ClearException();
191 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700192 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
193 }
jeffhao98eacac2011-09-14 16:11:53 -0700194 }
195}
196
Brian Carlstrom83db7722011-08-26 17:32:56 -0700197void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700198 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700199 for (size_t i = 0; i != class_path.size(); ++i) {
200 const DexFile* dex_file = class_path[i];
201 CHECK(dex_file != NULL);
202 CompileDexFile(class_loader, *dex_file);
203 }
204}
205
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700206void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
207 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700208 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
209 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700210 const char* descriptor = dex_file.GetClassDescriptor(class_def);
211 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700212 if (klass == NULL) {
213 // previous verification error will cause FindClass to throw
214 Thread* self = Thread::Current();
215 // CHECK(self->IsExceptionPending());
216 UNIMPLEMENTED(WARNING) << "CHECK for verification error after FindClass " << descriptor;
217 self->ClearException();
218 } else {
219 CompileClass(klass);
220 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700221 }
222}
223
224void Compiler::CompileClass(Class* klass) {
225 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
226 CompileMethod(klass->GetDirectMethod(i));
227 }
228 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
229 CompileMethod(klass->GetVirtualMethod(i));
230 }
231}
232
Ian Rogers2c8f6532011-09-02 17:16:34 -0700233namespace arm {
234 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700235}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700236namespace x86 {
237 void X86CreateInvokeStub(Method* method);
238}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700239
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700240void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700241 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700242 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700243 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700244 // TODO: this is only necessary for tests
245 if (!method->IsRegistered()) {
246 method->UnregisterNative();
247 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700248 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700249 DCHECK(abstract_method_error_stub_ != NULL);
250 if (instruction_set_ == kX86) {
251 method->SetCode(abstract_method_error_stub_, kX86);
252 } else {
253 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
254 method->SetCode(abstract_method_error_stub_, kArm);
255 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700256 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700257 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700258 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700259 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700260
Ian Rogers2c8f6532011-09-02 17:16:34 -0700261 if (instruction_set_ == kX86) {
262 art::x86::X86CreateInvokeStub(method);
263 } else {
264 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
265 // Generates invocation stub using ARM instruction set
266 art::arm::ArmCreateInvokeStub(method);
267 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700268 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700269}
270
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700271void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700272 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700273 for (size_t i = 0; i != class_path.size(); ++i) {
274 const DexFile* dex_file = class_path[i];
275 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700276 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700277 }
278}
279
Brian Carlstrom8a487412011-08-29 20:08:52 -0700280void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700281 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
282 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700283 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700284 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700285 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700286 if (method == NULL) {
287 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
288 } else if (method->IsDirect()) {
289 code_and_direct_methods->SetResolvedDirectMethod(i, method);
290 } else {
291 // TODO: we currently leave the entry blank for resolved
292 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700293 }
294 }
295}
296
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700297} // namespace art