blob: b88fd5a97d810b2abaf107d3bd9003ec331d5b7e [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);
65 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
66 class_linker->ResolveString(dex_file, i, dex_cache);
67 }
68
69 // Class derived values are more complicated, they require the linker and loader
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070070 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstroma5a97a22011-09-15 14:08:49 -070071 Class* klass = class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
72 CHECK(klass->IsResolved());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070073 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070074 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070075 // unknown if direct or virtual, try both
76 Method* method = class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, false);
77 if (method == NULL) {
78 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
79 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070080 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070081 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070082 // unknown if instance or static, try both
83 Field* field = class_linker->ResolveField(dex_file, i, dex_cache, class_loader, false);
84 if (field == NULL) {
Elliott Hughes1f1fa562011-08-26 10:22:53 -070085 class_linker->ResolveField(dex_file, i, 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();
101 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
102 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
103 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();
123 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
124 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
125 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);
140 // TODO: enable after crash investigation
141 // DexCache* dex_cache = class_linker->FindDexCache(dex_file);
142 // dex_cache->GetInitializedStaticStorage()->Set(i, klass);
jeffhao98eacac2011-09-14 16:11:53 -0700143 }
144}
145
Brian Carlstrom83db7722011-08-26 17:32:56 -0700146void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700147 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700148 for (size_t i = 0; i != class_path.size(); ++i) {
149 const DexFile* dex_file = class_path[i];
150 CHECK(dex_file != NULL);
151 CompileDexFile(class_loader, *dex_file);
152 }
153}
154
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700155void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
156 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
157 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
158 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
159 const char* descriptor = dex_file.GetClassDescriptor(class_def);
160 Class* klass = class_linker->FindClass(descriptor, class_loader);
161 CHECK(klass != NULL);
162 CompileClass(klass);
163 }
164}
165
166void Compiler::CompileClass(Class* klass) {
167 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
168 CompileMethod(klass->GetDirectMethod(i));
169 }
170 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
171 CompileMethod(klass->GetVirtualMethod(i));
172 }
173}
174
Ian Rogers2c8f6532011-09-02 17:16:34 -0700175namespace arm {
176 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700177}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700178namespace x86 {
179 void X86CreateInvokeStub(Method* method);
180}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700181
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700182void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700183 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700184 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700185 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700186 // TODO: this is only necessary for tests
187 if (!method->IsRegistered()) {
188 method->UnregisterNative();
189 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700190 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700191 DCHECK(abstract_method_error_stub_ != NULL);
192 if (instruction_set_ == kX86) {
193 method->SetCode(abstract_method_error_stub_, kX86);
194 } else {
195 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
196 method->SetCode(abstract_method_error_stub_, kArm);
197 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700198 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700199 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700200 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700201 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700202
Ian Rogers2c8f6532011-09-02 17:16:34 -0700203 if (instruction_set_ == kX86) {
204 art::x86::X86CreateInvokeStub(method);
205 } else {
206 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
207 // Generates invocation stub using ARM instruction set
208 art::arm::ArmCreateInvokeStub(method);
209 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700210 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700211}
212
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700213void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700214 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700215 for (size_t i = 0; i != class_path.size(); ++i) {
216 const DexFile* dex_file = class_path[i];
217 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700218 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700219 }
220}
221
Brian Carlstrom8a487412011-08-29 20:08:52 -0700222void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700223 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
224 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700225 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700226 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700227 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700228 if (method == NULL) {
229 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
230 } else if (method->IsDirect()) {
231 code_and_direct_methods->SetResolvedDirectMethod(i, method);
232 } else {
233 // TODO: we currently leave the entry blank for resolved
234 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700235 }
236 }
237}
238
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700239} // namespace art