blob: 8a382f5a6bb5274540a61f481705389cd42f1a0d [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);
Brian Carlstrom16192862011-09-12 17:50:06 -070045 // TODO: add verification step
46
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);
Brian Carlstrom16192862011-09-12 17:50:06 -070061 // TODO: add verification step
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
Brian Carlstrom83db7722011-08-26 17:32:56 -0700104void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700105 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700106 for (size_t i = 0; i != class_path.size(); ++i) {
107 const DexFile* dex_file = class_path[i];
108 CHECK(dex_file != NULL);
109 CompileDexFile(class_loader, *dex_file);
110 }
111}
112
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700113void Compiler::CompileDexFile(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 CompileClass(klass);
121 }
122}
123
124void Compiler::CompileClass(Class* klass) {
125 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
126 CompileMethod(klass->GetDirectMethod(i));
127 }
128 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
129 CompileMethod(klass->GetVirtualMethod(i));
130 }
131}
132
Ian Rogers2c8f6532011-09-02 17:16:34 -0700133namespace arm {
134 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700135}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700136namespace x86 {
137 void X86CreateInvokeStub(Method* method);
138}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700139
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700140void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700141 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700142 jni_compiler_.Compile(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700143 // unregister will install the stub to lookup via dlsym
144 method->UnregisterNative();
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700145 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700146 DCHECK(abstract_method_error_stub_ != NULL);
147 if (instruction_set_ == kX86) {
148 method->SetCode(abstract_method_error_stub_, kX86);
149 } else {
150 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
151 method->SetCode(abstract_method_error_stub_, kArm);
152 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700153 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700154 oatCompileMethod(*this, method, kThumb2);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700155 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700156 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700157
Ian Rogers2c8f6532011-09-02 17:16:34 -0700158 if (instruction_set_ == kX86) {
159 art::x86::X86CreateInvokeStub(method);
160 } else {
161 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
162 // Generates invocation stub using ARM instruction set
163 art::arm::ArmCreateInvokeStub(method);
164 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700165 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700166}
167
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700168void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700169 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700170 for (size_t i = 0; i != class_path.size(); ++i) {
171 const DexFile* dex_file = class_path[i];
172 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700173 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700174 }
175}
176
Brian Carlstrom8a487412011-08-29 20:08:52 -0700177void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700178 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
179 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700180 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700181 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700182 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700183 if (method == NULL) {
184 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
185 } else if (method->IsDirect()) {
186 code_and_direct_methods->SetResolvedDirectMethod(i, method);
187 } else {
188 // TODO: we currently leave the entry blank for resolved
189 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700190 }
191 }
192}
193
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700194} // namespace art