blob: 34263da63756b11d0d5c9afc952b292b3b529b86 [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);
jeffhao5cfd6fb2011-09-27 13:54:29 -0700186 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
187 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700188 }
jeffhaob4df5142011-09-19 20:25:32 -0700189 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700190}
191
192void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
193 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
194 for (size_t i = 0; i != class_path.size(); ++i) {
195 const DexFile* dex_file = class_path[i];
196 CHECK(dex_file != NULL);
197 InitializeClassesWithoutClinit(class_loader, *dex_file);
198 }
199}
200
201void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
202 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700203 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
204 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700205 const char* descriptor = dex_file.GetClassDescriptor(class_def);
206 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700207 if (klass != NULL) {
208 class_linker->EnsureInitialized(klass, false);
209 }
210 // clear any class not found or verification exceptions
211 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700212 }
213
214 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
215 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
216 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700217 if (klass == NULL) {
218 Thread::Current()->ClearException();
219 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700220 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
221 }
jeffhao98eacac2011-09-14 16:11:53 -0700222 }
223}
224
Brian Carlstrom83db7722011-08-26 17:32:56 -0700225void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700226 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700227 for (size_t i = 0; i != class_path.size(); ++i) {
228 const DexFile* dex_file = class_path[i];
229 CHECK(dex_file != NULL);
230 CompileDexFile(class_loader, *dex_file);
231 }
232}
233
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700234void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
235 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700236 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
237 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700238 const char* descriptor = dex_file.GetClassDescriptor(class_def);
239 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700240 if (klass == NULL) {
241 // previous verification error will cause FindClass to throw
242 Thread* self = Thread::Current();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700243 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700244 self->ClearException();
245 } else {
246 CompileClass(klass);
247 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700248 }
249}
250
251void Compiler::CompileClass(Class* klass) {
252 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
253 CompileMethod(klass->GetDirectMethod(i));
254 }
255 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
256 CompileMethod(klass->GetVirtualMethod(i));
257 }
258}
259
Ian Rogers2c8f6532011-09-02 17:16:34 -0700260namespace arm {
261 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700262}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700263namespace x86 {
264 void X86CreateInvokeStub(Method* method);
265}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700266
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700267void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700268 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700269 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700270 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700271 // TODO: this is only necessary for tests
272 if (!method->IsRegistered()) {
273 method->UnregisterNative();
274 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700275 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700276 DCHECK(abstract_method_error_stub_ != NULL);
277 if (instruction_set_ == kX86) {
278 method->SetCode(abstract_method_error_stub_, kX86);
279 } else {
280 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
281 method->SetCode(abstract_method_error_stub_, kArm);
282 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700283 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700284 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700285 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700286 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700287
Ian Rogers2c8f6532011-09-02 17:16:34 -0700288 if (instruction_set_ == kX86) {
289 art::x86::X86CreateInvokeStub(method);
290 } else {
291 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
292 // Generates invocation stub using ARM instruction set
293 art::arm::ArmCreateInvokeStub(method);
294 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700295 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700296}
297
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700298void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700299 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700300 for (size_t i = 0; i != class_path.size(); ++i) {
301 const DexFile* dex_file = class_path[i];
302 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700303 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700304 }
305}
306
Brian Carlstrom8a487412011-08-29 20:08:52 -0700307void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700308 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
309 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700310 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700311 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700312 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700313 if (method == NULL) {
314 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
315 } else if (method->IsDirect()) {
316 code_and_direct_methods->SetResolvedDirectMethod(i, method);
317 } else {
318 // TODO: we currently leave the entry blank for resolved
319 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700320 }
321 }
322}
323
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700324} // namespace art