blob: af5cdd65122a9df598affc9d85014fc58b363976 [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 Carlstrome24fa612011-09-29 00:53:55 -070027ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
28 switch (instruction_set) {
29 case kArm:
30 case kThumb2:
31 return arm::CreateAbstractMethodErrorStub();
32 case kX86:
33 return x86::CreateAbstractMethodErrorStub();
34 default:
35 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
36 return NULL;
37 }
38}
39
Brian Carlstrom16192862011-09-12 17:50:06 -070040Compiler::Compiler(InstructionSet insns) : instruction_set_(insns), jni_compiler_(insns),
41 verbose_(false) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070042 CHECK(!Runtime::Current()->IsStarted());
Shih-wei Liaoc486c112011-09-13 16:43:52 -070043}
44
Brian Carlstrom8a487412011-08-29 20:08:52 -070045void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070046 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070047 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070048 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070049 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070050 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070051 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070052}
53
54void Compiler::CompileOne(Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070055 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom8a487412011-08-29 20:08:52 -070056 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
57 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 Carlstrom8a487412011-08-29 20:08:52 -070060 CompileMethod(method);
61 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070062}
63
64void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070065 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070066 for (size_t i = 0; i != class_path.size(); ++i) {
67 const DexFile* dex_file = class_path[i];
68 CHECK(dex_file != NULL);
69 ResolveDexFile(class_loader, *dex_file);
70 }
71}
72
73void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
74 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
75
76 // Strings are easy, they always are simply resolved to literals in the same file
77 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstromffca45d2011-09-16 12:10:49 -070078 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
79 class_linker->ResolveString(dex_file, string_idx, dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070080 }
81
Brian Carlstrom845490b2011-09-19 15:56:53 -070082 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -070083 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
84 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -070085 if (klass == NULL) {
86 Thread::Current()->ClearException();
87 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070088 }
Brian Carlstrom845490b2011-09-19 15:56:53 -070089
90 // Method and Field are the worst. We can't resolve without either
91 // context from the code use (to disambiguate virtual vs direct
92 // method and instance vs static field) or from class
93 // definitions. While the compiler will resolve what it can as it
94 // needs it, here we try to resolve fields and methods used in class
95 // definitions, since many of them many never be referenced by
96 // generated code.
97 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
98 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
99
100 // Note the class_data pointer advances through the headers,
101 // static fields, instance fields, direct methods, and virtual
102 // methods.
103 const byte* class_data = dex_file.GetClassData(class_def);
104
105 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
106 size_t num_static_fields = header.static_fields_size_;
107 size_t num_instance_fields = header.instance_fields_size_;
108 size_t num_direct_methods = header.direct_methods_size_;
109 size_t num_virtual_methods = header.virtual_methods_size_;
110
111 if (num_static_fields != 0) {
112 uint32_t last_idx = 0;
113 for (size_t i = 0; i < num_static_fields; ++i) {
114 DexFile::Field dex_field;
115 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700116 Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache,
117 class_loader, true);
118 if (field == NULL) {
119 Thread* self = Thread::Current();
120 CHECK(self->IsExceptionPending());
121 self->ClearException();
122 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700123 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700124 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700125 if (num_instance_fields != 0) {
126 uint32_t last_idx = 0;
127 for (size_t i = 0; i < num_instance_fields; ++i) {
128 DexFile::Field dex_field;
129 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700130 Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache,
131 class_loader, false);
132 if (field == NULL) {
133 Thread* self = Thread::Current();
134 CHECK(self->IsExceptionPending());
135 self->ClearException();
136 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700137 }
138 }
139 if (num_direct_methods != 0) {
140 uint32_t last_idx = 0;
141 for (size_t i = 0; i < num_direct_methods; ++i) {
142 DexFile::Method dex_method;
143 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700144 Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache,
145 class_loader, true);
146 if (method == NULL) {
147 Thread* self = Thread::Current();
148 CHECK(self->IsExceptionPending());
149 self->ClearException();
150 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700151 }
152 }
153 if (num_virtual_methods != 0) {
154 uint32_t last_idx = 0;
155 for (size_t i = 0; i < num_virtual_methods; ++i) {
156 DexFile::Method dex_method;
157 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700158 Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache,
159 class_loader, false);
160 if (method == NULL) {
161 Thread* self = Thread::Current();
162 CHECK(self->IsExceptionPending());
163 self->ClearException();
164 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700165 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700166 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700167 }
168}
169
jeffhao98eacac2011-09-14 16:11:53 -0700170void Compiler::Verify(const ClassLoader* class_loader) {
171 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
172 for (size_t i = 0; i != class_path.size(); ++i) {
173 const DexFile* dex_file = class_path[i];
174 CHECK(dex_file != NULL);
175 VerifyDexFile(class_loader, *dex_file);
176 }
177}
178
179void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700180 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700181 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700182 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
183 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700184 const char* descriptor = dex_file.GetClassDescriptor(class_def);
185 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700186 if (klass == NULL) {
187 Thread* self = Thread::Current();
188 CHECK(self->IsExceptionPending());
189 self->ClearException();
190 continue;
191 }
192 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700193 class_linker->VerifyClass(klass);
jeffhao5cfd6fb2011-09-27 13:54:29 -0700194 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
195 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700196 }
jeffhaob4df5142011-09-19 20:25:32 -0700197 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700198}
199
200void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
201 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
202 for (size_t i = 0; i != class_path.size(); ++i) {
203 const DexFile* dex_file = class_path[i];
204 CHECK(dex_file != NULL);
205 InitializeClassesWithoutClinit(class_loader, *dex_file);
206 }
207}
208
209void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
210 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700211 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
212 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700213 const char* descriptor = dex_file.GetClassDescriptor(class_def);
214 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700215 if (klass != NULL) {
216 class_linker->EnsureInitialized(klass, false);
217 }
218 // clear any class not found or verification exceptions
219 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700220 }
221
222 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
223 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
224 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700225 if (klass == NULL) {
226 Thread::Current()->ClearException();
227 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700228 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
229 }
jeffhao98eacac2011-09-14 16:11:53 -0700230 }
231}
232
Brian Carlstrom83db7722011-08-26 17:32:56 -0700233void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700234 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700235 for (size_t i = 0; i != class_path.size(); ++i) {
236 const DexFile* dex_file = class_path[i];
237 CHECK(dex_file != NULL);
238 CompileDexFile(class_loader, *dex_file);
239 }
240}
241
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700242void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
243 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700244 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
245 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700246 const char* descriptor = dex_file.GetClassDescriptor(class_def);
247 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700248 if (klass == NULL) {
249 // previous verification error will cause FindClass to throw
250 Thread* self = Thread::Current();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700251 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700252 self->ClearException();
253 } else {
254 CompileClass(klass);
255 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700256 }
257}
258
259void Compiler::CompileClass(Class* klass) {
260 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
261 CompileMethod(klass->GetDirectMethod(i));
262 }
263 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
264 CompileMethod(klass->GetVirtualMethod(i));
265 }
266}
267
Ian Rogers2c8f6532011-09-02 17:16:34 -0700268namespace arm {
269 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700270}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700271namespace x86 {
272 void X86CreateInvokeStub(Method* method);
273}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700274
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700275void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700276 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700277 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700278 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700279 // TODO: this is only necessary for tests
280 if (!method->IsRegistered()) {
281 method->UnregisterNative();
282 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700283 } else if (method->IsAbstract()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700284 ByteArray* abstract_method_error_stub = Runtime::Current()->GetAbstractMethodErrorStubArray();
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700285 if (instruction_set_ == kX86) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700286 method->SetCodeArray(abstract_method_error_stub, kX86);
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700287 } else {
288 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700289 method->SetCodeArray(abstract_method_error_stub, kArm);
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700290 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700291 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700292 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700293 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294 CHECK(method->GetCode() != NULL) << PrettyMethod(method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700295
Ian Rogers2c8f6532011-09-02 17:16:34 -0700296 if (instruction_set_ == kX86) {
297 art::x86::X86CreateInvokeStub(method);
298 } else {
299 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
300 // Generates invocation stub using ARM instruction set
301 art::arm::ArmCreateInvokeStub(method);
302 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700303 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700304}
305
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700306void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700307 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700308 for (size_t i = 0; i != class_path.size(); ++i) {
309 const DexFile* dex_file = class_path[i];
310 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700311 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700312 }
313}
314
Brian Carlstrom8a487412011-08-29 20:08:52 -0700315void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700316 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
317 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700318 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700319 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700320 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700321 if (method == NULL) {
322 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
323 } else if (method->IsDirect()) {
324 code_and_direct_methods->SetResolvedDirectMethod(i, method);
325 } else {
326 // TODO: we currently leave the entry blank for resolved
327 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700328 }
329 }
330}
331
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700332} // namespace art