blob: 4d7ad19351f3648602934cd253f99adb042472a4 [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
72 // 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 Carlstromffca45d2011-09-16 12:10:49 -070077 for (size_t method_idx = 0; method_idx < dex_cache->NumResolvedMethods(); method_idx++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070078 // unknown if direct or virtual, try both
Brian Carlstromffca45d2011-09-16 12:10:49 -070079 Method* method = class_linker->ResolveMethod(dex_file, method_idx, dex_cache, class_loader, false);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070080 if (method == NULL) {
Brian Carlstromffca45d2011-09-16 12:10:49 -070081 class_linker->ResolveMethod(dex_file, method_idx, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070082 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070083 }
Brian Carlstromffca45d2011-09-16 12:10:49 -070084 for (size_t field_idx = 0; field_idx < dex_cache->NumResolvedFields(); field_idx++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070085 // unknown if instance or static, try both
Brian Carlstromffca45d2011-09-16 12:10:49 -070086 Field* field = class_linker->ResolveField(dex_file, field_idx, dex_cache, class_loader, false);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070087 if (field == NULL) {
Brian Carlstromffca45d2011-09-16 12:10:49 -070088 class_linker->ResolveField(dex_file, field_idx, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070089 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070090 }
91}
92
jeffhao98eacac2011-09-14 16:11:53 -070093void Compiler::Verify(const ClassLoader* class_loader) {
94 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
95 for (size_t i = 0; i != class_path.size(); ++i) {
96 const DexFile* dex_file = class_path[i];
97 CHECK(dex_file != NULL);
98 VerifyDexFile(class_loader, *dex_file);
99 }
100}
101
102void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
103 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700104 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
105 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700106 const char* descriptor = dex_file.GetClassDescriptor(class_def);
107 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700108 CHECK(klass->IsResolved());
jeffhao98eacac2011-09-14 16:11:53 -0700109 CHECK(klass != NULL);
110 class_linker->VerifyClass(klass);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700111 CHECK(klass->IsVerified() || klass->IsErroneous());
112 }
113}
114
115void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
116 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
117 for (size_t i = 0; i != class_path.size(); ++i) {
118 const DexFile* dex_file = class_path[i];
119 CHECK(dex_file != NULL);
120 InitializeClassesWithoutClinit(class_loader, *dex_file);
121 }
122}
123
124void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
125 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700126 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
127 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700128 const char* descriptor = dex_file.GetClassDescriptor(class_def);
129 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700130 class_linker->EnsureInitialized(klass, false);
Brian Carlstromffca45d2011-09-16 12:10:49 -0700131 }
132
133 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
134 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
135 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
136 if (klass->IsInitialized()) {
137 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
138 }
jeffhao98eacac2011-09-14 16:11:53 -0700139 }
140}
141
Brian Carlstrom83db7722011-08-26 17:32:56 -0700142void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700143 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700144 for (size_t i = 0; i != class_path.size(); ++i) {
145 const DexFile* dex_file = class_path[i];
146 CHECK(dex_file != NULL);
147 CompileDexFile(class_loader, *dex_file);
148 }
149}
150
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700151void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
152 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700153 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
154 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700155 const char* descriptor = dex_file.GetClassDescriptor(class_def);
156 Class* klass = class_linker->FindClass(descriptor, class_loader);
157 CHECK(klass != NULL);
158 CompileClass(klass);
159 }
160}
161
162void Compiler::CompileClass(Class* klass) {
163 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
164 CompileMethod(klass->GetDirectMethod(i));
165 }
166 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
167 CompileMethod(klass->GetVirtualMethod(i));
168 }
169}
170
Ian Rogers2c8f6532011-09-02 17:16:34 -0700171namespace arm {
172 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700173}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700174namespace x86 {
175 void X86CreateInvokeStub(Method* method);
176}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700177
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700178void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700179 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700180 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700181 // unregister will install the stub to lookup via dlsym
Ian Rogersbdb03912011-09-14 00:55:44 -0700182 // TODO: this is only necessary for tests
183 if (!method->IsRegistered()) {
184 method->UnregisterNative();
185 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700186 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700187 DCHECK(abstract_method_error_stub_ != NULL);
188 if (instruction_set_ == kX86) {
189 method->SetCode(abstract_method_error_stub_, kX86);
190 } else {
191 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
192 method->SetCode(abstract_method_error_stub_, kArm);
193 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700194 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700195 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700196 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700197 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700198
Ian Rogers2c8f6532011-09-02 17:16:34 -0700199 if (instruction_set_ == kX86) {
200 art::x86::X86CreateInvokeStub(method);
201 } else {
202 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
203 // Generates invocation stub using ARM instruction set
204 art::arm::ArmCreateInvokeStub(method);
205 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700206 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700207}
208
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700209void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700210 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700211 for (size_t i = 0; i != class_path.size(); ++i) {
212 const DexFile* dex_file = class_path[i];
213 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700214 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700215 }
216}
217
Brian Carlstrom8a487412011-08-29 20:08:52 -0700218void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700219 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
220 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700221 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700222 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700223 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700224 if (method == NULL) {
225 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
226 } else if (method->IsDirect()) {
227 code_and_direct_methods->SetResolvedDirectMethod(i, method);
228 } else {
229 // TODO: we currently leave the entry blank for resolved
230 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700231 }
232 }
233}
234
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700235} // namespace art