blob: 71727fdb28c8e194cede626ace10abb3921e8459 [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) {
jeffhaob4df5142011-09-19 20:25:32 -0700146 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700147 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700148 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
149 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700150 const char* descriptor = dex_file.GetClassDescriptor(class_def);
151 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700152 CHECK(klass->IsResolved());
jeffhao98eacac2011-09-14 16:11:53 -0700153 CHECK(klass != NULL);
154 class_linker->VerifyClass(klass);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700155 CHECK(klass->IsVerified() || klass->IsErroneous());
156 }
jeffhaob4df5142011-09-19 20:25:32 -0700157 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700158}
159
160void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
161 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
162 for (size_t i = 0; i != class_path.size(); ++i) {
163 const DexFile* dex_file = class_path[i];
164 CHECK(dex_file != NULL);
165 InitializeClassesWithoutClinit(class_loader, *dex_file);
166 }
167}
168
169void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
170 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700171 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
172 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700173 const char* descriptor = dex_file.GetClassDescriptor(class_def);
174 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700175 class_linker->EnsureInitialized(klass, false);
Brian Carlstromffca45d2011-09-16 12:10:49 -0700176 }
177
178 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
179 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
180 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
181 if (klass->IsInitialized()) {
182 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
183 }
jeffhao98eacac2011-09-14 16:11:53 -0700184 }
185}
186
Brian Carlstrom83db7722011-08-26 17:32:56 -0700187void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700188 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700189 for (size_t i = 0; i != class_path.size(); ++i) {
190 const DexFile* dex_file = class_path[i];
191 CHECK(dex_file != NULL);
192 CompileDexFile(class_loader, *dex_file);
193 }
194}
195
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700196void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
197 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700198 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
199 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700200 const char* descriptor = dex_file.GetClassDescriptor(class_def);
201 Class* klass = class_linker->FindClass(descriptor, class_loader);
202 CHECK(klass != NULL);
203 CompileClass(klass);
204 }
205}
206
207void Compiler::CompileClass(Class* klass) {
208 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
209 CompileMethod(klass->GetDirectMethod(i));
210 }
211 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
212 CompileMethod(klass->GetVirtualMethod(i));
213 }
214}
215
Ian Rogers2c8f6532011-09-02 17:16:34 -0700216namespace arm {
217 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700218}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700219namespace x86 {
220 void X86CreateInvokeStub(Method* method);
221}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700222
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700223void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700224 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700225 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700226 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700227 // TODO: this is only necessary for tests
228 if (!method->IsRegistered()) {
229 method->UnregisterNative();
230 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700231 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700232 DCHECK(abstract_method_error_stub_ != NULL);
233 if (instruction_set_ == kX86) {
234 method->SetCode(abstract_method_error_stub_, kX86);
235 } else {
236 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
237 method->SetCode(abstract_method_error_stub_, kArm);
238 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700239 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700240 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700241 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700242 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700243
Ian Rogers2c8f6532011-09-02 17:16:34 -0700244 if (instruction_set_ == kX86) {
245 art::x86::X86CreateInvokeStub(method);
246 } else {
247 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
248 // Generates invocation stub using ARM instruction set
249 art::arm::ArmCreateInvokeStub(method);
250 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700251 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700252}
253
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700254void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700255 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700256 for (size_t i = 0; i != class_path.size(); ++i) {
257 const DexFile* dex_file = class_path[i];
258 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700259 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700260 }
261}
262
Brian Carlstrom8a487412011-08-29 20:08:52 -0700263void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700264 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
265 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700266 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700267 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700268 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700269 if (method == NULL) {
270 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
271 } else if (method->IsDirect()) {
272 code_and_direct_methods->SetResolvedDirectMethod(i, method);
273 } else {
274 // TODO: we currently leave the entry blank for resolved
275 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700276 }
277 }
278}
279
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700280} // namespace art