blob: 454c8f86f2b05bf87ff2d25fed53e8c0d1b6bb92 [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) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070027 CHECK(!Runtime::Current()->IsStarted());
Shih-wei Liaoc486c112011-09-13 16:43:52 -070028 if (insns == kArm || insns == kThumb2) {
Ian Rogersbdb03912011-09-14 00:55:44 -070029 abstract_method_error_stub_ = arm::CreateAbstractMethodErrorStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070030 } else if (insns == kX86) {
Ian Rogersbdb03912011-09-14 00:55:44 -070031 abstract_method_error_stub_ = x86::CreateAbstractMethodErrorStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070032 }
33}
34
Brian Carlstrom8a487412011-08-29 20:08:52 -070035void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070036 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070037 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070038 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070039 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070040 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070041 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070042}
43
44void Compiler::CompileOne(Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070045 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom8a487412011-08-29 20:08:52 -070046 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
47 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 Carlstrom8a487412011-08-29 20:08:52 -070050 CompileMethod(method);
51 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070052}
53
54void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070055 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070056 for (size_t i = 0; i != class_path.size(); ++i) {
57 const DexFile* dex_file = class_path[i];
58 CHECK(dex_file != NULL);
59 ResolveDexFile(class_loader, *dex_file);
60 }
61}
62
63void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
64 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
65
66 // Strings are easy, they always are simply resolved to literals in the same file
67 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstromffca45d2011-09-16 12:10:49 -070068 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
69 class_linker->ResolveString(dex_file, string_idx, dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070070 }
71
Brian Carlstrom845490b2011-09-19 15:56:53 -070072 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -070073 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
74 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070075 CHECK(klass->IsResolved());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070076 }
Brian Carlstrom845490b2011-09-19 15:56:53 -070077
78 // Method and Field are the worst. We can't resolve without either
79 // context from the code use (to disambiguate virtual vs direct
80 // method and instance vs static field) or from class
81 // definitions. While the compiler will resolve what it can as it
82 // needs it, here we try to resolve fields and methods used in class
83 // definitions, since many of them many never be referenced by
84 // generated code.
85 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
86 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
87
88 // Note the class_data pointer advances through the headers,
89 // static fields, instance fields, direct methods, and virtual
90 // methods.
91 const byte* class_data = dex_file.GetClassData(class_def);
92
93 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
94 size_t num_static_fields = header.static_fields_size_;
95 size_t num_instance_fields = header.instance_fields_size_;
96 size_t num_direct_methods = header.direct_methods_size_;
97 size_t num_virtual_methods = header.virtual_methods_size_;
98
99 if (num_static_fields != 0) {
100 uint32_t last_idx = 0;
101 for (size_t i = 0; i < num_static_fields; ++i) {
102 DexFile::Field dex_field;
103 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
104 class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache, class_loader, true);
105 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700106 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700107 if (num_instance_fields != 0) {
108 uint32_t last_idx = 0;
109 for (size_t i = 0; i < num_instance_fields; ++i) {
110 DexFile::Field dex_field;
111 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
112 class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache, class_loader, false);
113 }
114 }
115 if (num_direct_methods != 0) {
116 uint32_t last_idx = 0;
117 for (size_t i = 0; i < num_direct_methods; ++i) {
118 DexFile::Method dex_method;
119 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
120 class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache, class_loader,
121 true);
122 }
123 }
124 if (num_virtual_methods != 0) {
125 uint32_t last_idx = 0;
126 for (size_t i = 0; i < num_virtual_methods; ++i) {
127 DexFile::Method dex_method;
128 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
129 class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache, class_loader,
130 false);
131 }
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700132 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700133 }
134}
135
jeffhao98eacac2011-09-14 16:11:53 -0700136void Compiler::Verify(const ClassLoader* class_loader) {
137 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
138 for (size_t i = 0; i != class_path.size(); ++i) {
139 const DexFile* dex_file = class_path[i];
140 CHECK(dex_file != NULL);
141 VerifyDexFile(class_loader, *dex_file);
142 }
143}
144
145void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
146 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700147 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
148 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700149 const char* descriptor = dex_file.GetClassDescriptor(class_def);
150 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700151 CHECK(klass->IsResolved());
jeffhao98eacac2011-09-14 16:11:53 -0700152 CHECK(klass != NULL);
153 class_linker->VerifyClass(klass);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700154 CHECK(klass->IsVerified() || klass->IsErroneous());
155 }
156}
157
158void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
159 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
160 for (size_t i = 0; i != class_path.size(); ++i) {
161 const DexFile* dex_file = class_path[i];
162 CHECK(dex_file != NULL);
163 InitializeClassesWithoutClinit(class_loader, *dex_file);
164 }
165}
166
167void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
168 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700169 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
170 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700171 const char* descriptor = dex_file.GetClassDescriptor(class_def);
172 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700173 class_linker->EnsureInitialized(klass, false);
Brian Carlstromffca45d2011-09-16 12:10:49 -0700174 }
175
176 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
177 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
178 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
179 if (klass->IsInitialized()) {
180 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
181 }
jeffhao98eacac2011-09-14 16:11:53 -0700182 }
183}
184
Brian Carlstrom83db7722011-08-26 17:32:56 -0700185void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700186 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700187 for (size_t i = 0; i != class_path.size(); ++i) {
188 const DexFile* dex_file = class_path[i];
189 CHECK(dex_file != NULL);
190 CompileDexFile(class_loader, *dex_file);
191 }
192}
193
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700194void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
195 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700196 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
197 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700198 const char* descriptor = dex_file.GetClassDescriptor(class_def);
199 Class* klass = class_linker->FindClass(descriptor, class_loader);
200 CHECK(klass != NULL);
201 CompileClass(klass);
202 }
203}
204
205void Compiler::CompileClass(Class* klass) {
206 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
207 CompileMethod(klass->GetDirectMethod(i));
208 }
209 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
210 CompileMethod(klass->GetVirtualMethod(i));
211 }
212}
213
Ian Rogers2c8f6532011-09-02 17:16:34 -0700214namespace arm {
215 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700216}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700217namespace x86 {
218 void X86CreateInvokeStub(Method* method);
219}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700220
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700221void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700222 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700223 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700224 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700225 // TODO: this is only necessary for tests
226 if (!method->IsRegistered()) {
227 method->UnregisterNative();
228 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700229 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700230 DCHECK(abstract_method_error_stub_ != NULL);
231 if (instruction_set_ == kX86) {
232 method->SetCode(abstract_method_error_stub_, kX86);
233 } else {
234 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
235 method->SetCode(abstract_method_error_stub_, kArm);
236 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700237 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700238 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700239 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700240 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700241
Ian Rogers2c8f6532011-09-02 17:16:34 -0700242 if (instruction_set_ == kX86) {
243 art::x86::X86CreateInvokeStub(method);
244 } else {
245 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
246 // Generates invocation stub using ARM instruction set
247 art::arm::ArmCreateInvokeStub(method);
248 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700249 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700250}
251
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700252void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700253 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700254 for (size_t i = 0; i != class_path.size(); ++i) {
255 const DexFile* dex_file = class_path[i];
256 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700257 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700258 }
259}
260
Brian Carlstrom8a487412011-08-29 20:08:52 -0700261void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700262 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
263 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700264 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700265 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700266 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700267 if (method == NULL) {
268 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
269 } else if (method->IsDirect()) {
270 code_and_direct_methods->SetResolvedDirectMethod(i, method);
271 } else {
272 // TODO: we currently leave the entry blank for resolved
273 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700274 }
275 }
276}
277
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700278} // namespace art