blob: 528a0b23bff90035242fe3f5b646dfbe91c2b051 [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 Carlstrom16192862011-09-12 17:50:06 -070046
47 // TODO: mark all verified classes initialized if they have no <clinit>
48 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
49 Class* Class_class = class_linker->FindSystemClass("Ljava/lang/Class;");
50 Method* Class_clinit = Class_class->FindDirectMethod("<clinit>", "()V");
51 CHECK(Class_clinit == NULL);
52 Class_class->SetStatus(Class::kStatusInitialized);
53
Brian Carlstrom83db7722011-08-26 17:32:56 -070054 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070055 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070056}
57
58void Compiler::CompileOne(Method* method) {
59 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
60 Resolve(class_loader);
jeffhao98eacac2011-09-14 16:11:53 -070061 Verify(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070062 CompileMethod(method);
63 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070064}
65
66void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070067 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070068 for (size_t i = 0; i != class_path.size(); ++i) {
69 const DexFile* dex_file = class_path[i];
70 CHECK(dex_file != NULL);
71 ResolveDexFile(class_loader, *dex_file);
72 }
73}
74
75void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
76 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
77
78 // Strings are easy, they always are simply resolved to literals in the same file
79 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
80 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
81 class_linker->ResolveString(dex_file, i, dex_cache);
82 }
83
84 // Class derived values are more complicated, they require the linker and loader
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070085 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070086 class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
87 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070088 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070089 // unknown if direct or virtual, try both
90 Method* method = class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, false);
91 if (method == NULL) {
92 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
93 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070094 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070095 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070096 // unknown if instance or static, try both
97 Field* field = class_linker->ResolveField(dex_file, i, dex_cache, class_loader, false);
98 if (field == NULL) {
Elliott Hughes1f1fa562011-08-26 10:22:53 -070099 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700100 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700101 }
102}
103
jeffhao98eacac2011-09-14 16:11:53 -0700104void Compiler::Verify(const ClassLoader* class_loader) {
105 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
106 for (size_t i = 0; i != class_path.size(); ++i) {
107 const DexFile* dex_file = class_path[i];
108 CHECK(dex_file != NULL);
109 VerifyDexFile(class_loader, *dex_file);
110 }
111}
112
113void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
114 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
115 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
116 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
117 const char* descriptor = dex_file.GetClassDescriptor(class_def);
118 Class* klass = class_linker->FindClass(descriptor, class_loader);
119 CHECK(klass != NULL);
120 class_linker->VerifyClass(klass);
121 }
122}
123
Brian Carlstrom83db7722011-08-26 17:32:56 -0700124void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700125 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700126 for (size_t i = 0; i != class_path.size(); ++i) {
127 const DexFile* dex_file = class_path[i];
128 CHECK(dex_file != NULL);
129 CompileDexFile(class_loader, *dex_file);
130 }
131}
132
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700133void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
134 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
135 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
136 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
137 const char* descriptor = dex_file.GetClassDescriptor(class_def);
138 Class* klass = class_linker->FindClass(descriptor, class_loader);
139 CHECK(klass != NULL);
140 CompileClass(klass);
141 }
142}
143
144void Compiler::CompileClass(Class* klass) {
145 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
146 CompileMethod(klass->GetDirectMethod(i));
147 }
148 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
149 CompileMethod(klass->GetVirtualMethod(i));
150 }
151}
152
Ian Rogers2c8f6532011-09-02 17:16:34 -0700153namespace arm {
154 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700155}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700156namespace x86 {
157 void X86CreateInvokeStub(Method* method);
158}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700159
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700160void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700161 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700162 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700163 // unregister will install the stub to lookup via dlsym
164 method->UnregisterNative();
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700165 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700166 DCHECK(abstract_method_error_stub_ != NULL);
167 if (instruction_set_ == kX86) {
168 method->SetCode(abstract_method_error_stub_, kX86);
169 } else {
170 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
171 method->SetCode(abstract_method_error_stub_, kArm);
172 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700173 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700174 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700175 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700176 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700177
Ian Rogers2c8f6532011-09-02 17:16:34 -0700178 if (instruction_set_ == kX86) {
179 art::x86::X86CreateInvokeStub(method);
180 } else {
181 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
182 // Generates invocation stub using ARM instruction set
183 art::arm::ArmCreateInvokeStub(method);
184 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700185 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700186}
187
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700188void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700189 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700190 for (size_t i = 0; i != class_path.size(); ++i) {
191 const DexFile* dex_file = class_path[i];
192 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700193 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700194 }
195}
196
Brian Carlstrom8a487412011-08-29 20:08:52 -0700197void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700198 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
199 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700200 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700201 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700202 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700203 if (method == NULL) {
204 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
205 } else if (method->IsDirect()) {
206 code_and_direct_methods->SetResolvedDirectMethod(i, method);
207 } else {
208 // TODO: we currently leave the entry blank for resolved
209 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700210 }
211 }
212}
213
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700214} // namespace art