blob: 0d583accb5922a5192bc8f496b55568408ad5a8a [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 -070017typedef void (*ThrowAme)(Method*, Thread*);
18
19void ThrowAbstractMethodError(Method* method, Thread* thread) {
Shih-wei Liao303b01e2011-09-14 00:46:13 -070020 LOG(FATAL) << "Unimplemented Exception Handling. Remove this when ThrowException works.";
Shih-wei Liaoc486c112011-09-13 16:43:52 -070021 thread->ThrowNewException("Ljava/lang/AbstractMethodError",
22 "abstract method \"%s\"",
23 PrettyMethod(method).c_str());
24}
25
26namespace arm {
27 ByteArray* CreateAbstractMethodErrorStub(ThrowAme);
28}
29
30namespace x86 {
31 ByteArray* CreateAbstractMethodErrorStub(ThrowAme);
32}
33
Brian Carlstrom16192862011-09-12 17:50:06 -070034Compiler::Compiler(InstructionSet insns) : instruction_set_(insns), jni_compiler_(insns),
35 verbose_(false) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -070036 if (insns == kArm || insns == kThumb2) {
37 abstract_method_error_stub_ = arm::CreateAbstractMethodErrorStub(&ThrowAbstractMethodError);
38 } else if (insns == kX86) {
39 abstract_method_error_stub_ = x86::CreateAbstractMethodErrorStub(&ThrowAbstractMethodError);
40 }
41}
42
Brian Carlstrom8a487412011-08-29 20:08:52 -070043void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070044 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 Carlstrom83db7722011-08-26 17:32:56 -070047 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070048 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070049}
50
51void Compiler::CompileOne(Method* method) {
52 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
53 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070054 Verify(class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -070055 InitializeClassesWithoutClinit(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070056 CompileMethod(method);
57 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070058}
59
60void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070061 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070062 for (size_t i = 0; i != class_path.size(); ++i) {
63 const DexFile* dex_file = class_path[i];
64 CHECK(dex_file != NULL);
65 ResolveDexFile(class_loader, *dex_file);
66 }
67}
68
69void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
70 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
71
72 // Strings are easy, they always are simply resolved to literals in the same file
73 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
74 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
75 class_linker->ResolveString(dex_file, i, dex_cache);
76 }
77
78 // Class derived values are more complicated, they require the linker and loader
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070079 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstroma5a97a22011-09-15 14:08:49 -070080 Class* klass = class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
81 CHECK(klass->IsResolved());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070082 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070083 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070084 // unknown if direct or virtual, try both
85 Method* method = class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, false);
86 if (method == NULL) {
87 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
88 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070089 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070090 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070091 // unknown if instance or static, try both
92 Field* field = class_linker->ResolveField(dex_file, i, dex_cache, class_loader, false);
93 if (field == NULL) {
Elliott Hughes1f1fa562011-08-26 10:22:53 -070094 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070095 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070096 }
97}
98
jeffhao98eacac2011-09-14 16:11:53 -070099void Compiler::Verify(const ClassLoader* class_loader) {
100 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
101 for (size_t i = 0; i != class_path.size(); ++i) {
102 const DexFile* dex_file = class_path[i];
103 CHECK(dex_file != NULL);
104 VerifyDexFile(class_loader, *dex_file);
105 }
106}
107
108void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
109 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
110 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
111 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
112 const char* descriptor = dex_file.GetClassDescriptor(class_def);
113 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700114 CHECK(klass->IsResolved());
jeffhao98eacac2011-09-14 16:11:53 -0700115 CHECK(klass != NULL);
116 class_linker->VerifyClass(klass);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700117 CHECK(klass->IsVerified() || klass->IsErroneous());
118 }
119}
120
121void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
122 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
123 for (size_t i = 0; i != class_path.size(); ++i) {
124 const DexFile* dex_file = class_path[i];
125 CHECK(dex_file != NULL);
126 InitializeClassesWithoutClinit(class_loader, *dex_file);
127 }
128}
129
130void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
131 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
132 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
133 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
134 const char* descriptor = dex_file.GetClassDescriptor(class_def);
135 Class* klass = class_linker->FindClass(descriptor, class_loader);
136 CHECK(klass != NULL);
137 if (klass->IsInitialized()) {
138 continue;
139 }
140 CHECK(klass->IsVerified() || klass->IsErroneous());
141 if (!klass->IsVerified()) {
142 continue;
143 }
144 Method* clinit = klass->FindDirectMethod("<clinit>", "()V");
145 if (clinit != NULL) {
146 continue;
147 }
148 klass->SetStatus(Class::kStatusInitialized);
149 // TODO: enable after crash investigation
150 // DexCache* dex_cache = class_linker->FindDexCache(dex_file);
151 // dex_cache->GetInitializedStaticStorage()->Set(i, klass);
jeffhao98eacac2011-09-14 16:11:53 -0700152 }
153}
154
Brian Carlstrom83db7722011-08-26 17:32:56 -0700155void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700156 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700157 for (size_t i = 0; i != class_path.size(); ++i) {
158 const DexFile* dex_file = class_path[i];
159 CHECK(dex_file != NULL);
160 CompileDexFile(class_loader, *dex_file);
161 }
162}
163
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700164void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
165 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
166 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
167 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
168 const char* descriptor = dex_file.GetClassDescriptor(class_def);
169 Class* klass = class_linker->FindClass(descriptor, class_loader);
170 CHECK(klass != NULL);
171 CompileClass(klass);
172 }
173}
174
175void Compiler::CompileClass(Class* klass) {
176 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
177 CompileMethod(klass->GetDirectMethod(i));
178 }
179 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
180 CompileMethod(klass->GetVirtualMethod(i));
181 }
182}
183
Ian Rogers2c8f6532011-09-02 17:16:34 -0700184namespace arm {
185 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700186}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700187namespace x86 {
188 void X86CreateInvokeStub(Method* method);
189}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700190
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700191void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700192 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700193 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700194 // unregister will install the stub to lookup via dlsym
195 method->UnregisterNative();
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700196 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700197 DCHECK(abstract_method_error_stub_ != NULL);
198 if (instruction_set_ == kX86) {
199 method->SetCode(abstract_method_error_stub_, kX86);
200 } else {
201 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
202 method->SetCode(abstract_method_error_stub_, kArm);
203 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700204 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700205 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700206 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700207 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700208
Ian Rogers2c8f6532011-09-02 17:16:34 -0700209 if (instruction_set_ == kX86) {
210 art::x86::X86CreateInvokeStub(method);
211 } else {
212 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
213 // Generates invocation stub using ARM instruction set
214 art::arm::ArmCreateInvokeStub(method);
215 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700216 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700217}
218
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700219void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700220 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700221 for (size_t i = 0; i != class_path.size(); ++i) {
222 const DexFile* dex_file = class_path[i];
223 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700224 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700225 }
226}
227
Brian Carlstrom8a487412011-08-29 20:08:52 -0700228void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700229 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
230 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700231 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700232 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700233 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700234 if (method == NULL) {
235 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
236 } else if (method->IsDirect()) {
237 code_and_direct_methods->SetResolvedDirectMethod(i, method);
238 } else {
239 // TODO: we currently leave the entry blank for resolved
240 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700241 }
242 }
243}
244
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700245} // namespace art