blob: 017861fa32da6037445b322b4c208067624e6056 [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 Carlstrom2cc022b2011-08-25 10:05:39 -07005#include "assembler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07006#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -07007#include "class_loader.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07008#include "dex_cache.h"
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07009#include "jni_compiler.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070010#include "jni_internal.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070011#include "runtime.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070012
Brian Carlstrom16192862011-09-12 17:50:06 -070013extern bool oatCompileMethod(const art::Compiler& compiler, art::Method*, art::InstructionSet);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070014
15namespace art {
16
Shih-wei Liaoc486c112011-09-13 16:43:52 -070017namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070018 ByteArray* CreateAbstractMethodErrorStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070019}
20
21namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070022 ByteArray* CreateAbstractMethodErrorStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070023}
24
Brian Carlstrom16192862011-09-12 17:50:06 -070025Compiler::Compiler(InstructionSet insns) : instruction_set_(insns), jni_compiler_(insns),
26 verbose_(false) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -070027 if (insns == kArm || insns == kThumb2) {
Ian Rogersbdb03912011-09-14 00:55:44 -070028 abstract_method_error_stub_ = arm::CreateAbstractMethodErrorStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070029 } else if (insns == kX86) {
Ian Rogersbdb03912011-09-14 00:55:44 -070030 abstract_method_error_stub_ = x86::CreateAbstractMethodErrorStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070031 }
32}
33
Brian Carlstrom8a487412011-08-29 20:08:52 -070034void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070035 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070036 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070037 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070038 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070039 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070040}
41
42void Compiler::CompileOne(Method* method) {
43 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
44 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070045 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070046 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070047 CompileMethod(method);
48 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070049}
50
51void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070052 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070053 for (size_t i = 0; i != class_path.size(); ++i) {
54 const DexFile* dex_file = class_path[i];
55 CHECK(dex_file != NULL);
56 ResolveDexFile(class_loader, *dex_file);
57 }
58}
59
60void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
61 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
62
63 // Strings are easy, they always are simply resolved to literals in the same file
64 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstromffca45d2011-09-16 12:10:49 -070065 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
66 class_linker->ResolveString(dex_file, string_idx, dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070067 }
68
69 // Class derived values are more complicated, they require the linker and loader
Brian Carlstromffca45d2011-09-16 12:10:49 -070070 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
71 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070072 CHECK(klass->IsResolved());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070073 }
Brian Carlstromffca45d2011-09-16 12:10:49 -070074 for (size_t method_idx = 0; method_idx < dex_cache->NumResolvedMethods(); method_idx++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070075 // unknown if direct or virtual, try both
Brian Carlstromffca45d2011-09-16 12:10:49 -070076 Method* method = class_linker->ResolveMethod(dex_file, method_idx, dex_cache, class_loader, false);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070077 if (method == NULL) {
Brian Carlstromffca45d2011-09-16 12:10:49 -070078 class_linker->ResolveMethod(dex_file, method_idx, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070079 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070080 }
Brian Carlstromffca45d2011-09-16 12:10:49 -070081 for (size_t field_idx = 0; field_idx < dex_cache->NumResolvedFields(); field_idx++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070082 // unknown if instance or static, try both
Brian Carlstromffca45d2011-09-16 12:10:49 -070083 Field* field = class_linker->ResolveField(dex_file, field_idx, dex_cache, class_loader, false);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070084 if (field == NULL) {
Brian Carlstromffca45d2011-09-16 12:10:49 -070085 class_linker->ResolveField(dex_file, field_idx, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070086 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070087 }
88}
89
jeffhao98eacac2011-09-14 16:11:53 -070090void Compiler::Verify(const ClassLoader* class_loader) {
91 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
92 for (size_t i = 0; i != class_path.size(); ++i) {
93 const DexFile* dex_file = class_path[i];
94 CHECK(dex_file != NULL);
95 VerifyDexFile(class_loader, *dex_file);
96 }
97}
98
99void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
100 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700101 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
102 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700103 const char* descriptor = dex_file.GetClassDescriptor(class_def);
104 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700105 CHECK(klass->IsResolved());
jeffhao98eacac2011-09-14 16:11:53 -0700106 CHECK(klass != NULL);
107 class_linker->VerifyClass(klass);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700108 CHECK(klass->IsVerified() || klass->IsErroneous());
109 }
110}
111
112void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
113 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
114 for (size_t i = 0; i != class_path.size(); ++i) {
115 const DexFile* dex_file = class_path[i];
116 CHECK(dex_file != NULL);
117 InitializeClassesWithoutClinit(class_loader, *dex_file);
118 }
119}
120
121void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
122 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700123 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
124 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700125 const char* descriptor = dex_file.GetClassDescriptor(class_def);
126 Class* klass = class_linker->FindClass(descriptor, class_loader);
127 CHECK(klass != NULL);
128 if (klass->IsInitialized()) {
129 continue;
130 }
131 CHECK(klass->IsVerified() || klass->IsErroneous());
132 if (!klass->IsVerified()) {
133 continue;
134 }
135 Method* clinit = klass->FindDirectMethod("<clinit>", "()V");
136 if (clinit != NULL) {
137 continue;
138 }
139 klass->SetStatus(Class::kStatusInitialized);
Brian Carlstromffca45d2011-09-16 12:10:49 -0700140 }
141
142 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
143 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
144 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
145 if (klass->IsInitialized()) {
146 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
147 }
jeffhao98eacac2011-09-14 16:11:53 -0700148 }
149}
150
Brian Carlstrom83db7722011-08-26 17:32:56 -0700151void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700152 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700153 for (size_t i = 0; i != class_path.size(); ++i) {
154 const DexFile* dex_file = class_path[i];
155 CHECK(dex_file != NULL);
156 CompileDexFile(class_loader, *dex_file);
157 }
158}
159
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700160void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
161 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700162 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
163 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700164 const char* descriptor = dex_file.GetClassDescriptor(class_def);
165 Class* klass = class_linker->FindClass(descriptor, class_loader);
166 CHECK(klass != NULL);
167 CompileClass(klass);
168 }
169}
170
171void Compiler::CompileClass(Class* klass) {
172 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
173 CompileMethod(klass->GetDirectMethod(i));
174 }
175 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
176 CompileMethod(klass->GetVirtualMethod(i));
177 }
178}
179
Ian Rogers2c8f6532011-09-02 17:16:34 -0700180namespace arm {
181 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700182}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700183namespace x86 {
184 void X86CreateInvokeStub(Method* method);
185}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700186
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700187void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700188 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700189 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700190 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700191 // TODO: this is only necessary for tests
192 if (!method->IsRegistered()) {
193 method->UnregisterNative();
194 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700195 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700196 DCHECK(abstract_method_error_stub_ != NULL);
197 if (instruction_set_ == kX86) {
198 method->SetCode(abstract_method_error_stub_, kX86);
199 } else {
200 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
201 method->SetCode(abstract_method_error_stub_, kArm);
202 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700203 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700204 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700205 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700206 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700207
Ian Rogers2c8f6532011-09-02 17:16:34 -0700208 if (instruction_set_ == kX86) {
209 art::x86::X86CreateInvokeStub(method);
210 } else {
211 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
212 // Generates invocation stub using ARM instruction set
213 art::arm::ArmCreateInvokeStub(method);
214 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700215 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700216}
217
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700218void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700219 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700220 for (size_t i = 0; i != class_path.size(); ++i) {
221 const DexFile* dex_file = class_path[i];
222 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700223 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700224 }
225}
226
Brian Carlstrom8a487412011-08-29 20:08:52 -0700227void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700228 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
229 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700230 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700231 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700232 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700233 if (method == NULL) {
234 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
235 } else if (method->IsDirect()) {
236 code_and_direct_methods->SetResolvedDirectMethod(i, method);
237 } else {
238 // TODO: we currently leave the entry blank for resolved
239 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700240 }
241 }
242}
243
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700244} // namespace art