blob: fb510bb416011a4f22b17f900ecf942b340215d0 [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
13extern bool oatCompileMethod(art::Method*, art::InstructionSet);
14
15namespace art {
16
Shih-wei Liaoc486c112011-09-13 16:43:52 -070017typedef void (*ThrowAme)(Method*, Thread*);
18
19void ThrowAbstractMethodError(Method* method, Thread* thread) {
20 thread->ThrowNewException("Ljava/lang/AbstractMethodError",
21 "abstract method \"%s\"",
22 PrettyMethod(method).c_str());
23}
24
25namespace arm {
26 ByteArray* CreateAbstractMethodErrorStub(ThrowAme);
27}
28
29namespace x86 {
30 ByteArray* CreateAbstractMethodErrorStub(ThrowAme);
31}
32
33Compiler::Compiler(InstructionSet insns) : instruction_set_(insns), jni_compiler_(insns) {
34 if (insns == kArm || insns == kThumb2) {
35 abstract_method_error_stub_ = arm::CreateAbstractMethodErrorStub(&ThrowAbstractMethodError);
36 } else if (insns == kX86) {
37 abstract_method_error_stub_ = x86::CreateAbstractMethodErrorStub(&ThrowAbstractMethodError);
38 }
39}
40
Brian Carlstrom8a487412011-08-29 20:08:52 -070041void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070042 Resolve(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070043 // TODO add verification step
44 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070045 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070046}
47
48void Compiler::CompileOne(Method* method) {
49 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
50 Resolve(class_loader);
51 // TODO add verification step
52 CompileMethod(method);
53 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070054}
55
56void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070057 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070058 for (size_t i = 0; i != class_path.size(); ++i) {
59 const DexFile* dex_file = class_path[i];
60 CHECK(dex_file != NULL);
61 ResolveDexFile(class_loader, *dex_file);
62 }
63}
64
65void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
66 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
67
68 // Strings are easy, they always are simply resolved to literals in the same file
69 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
70 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
71 class_linker->ResolveString(dex_file, i, dex_cache);
72 }
73
74 // Class derived values are more complicated, they require the linker and loader
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070075 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070076 class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
77 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070078 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070079 // unknown if direct or virtual, try both
80 Method* method = class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, false);
81 if (method == NULL) {
82 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
83 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070084 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070085 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070086 // unknown if instance or static, try both
87 Field* field = class_linker->ResolveField(dex_file, i, dex_cache, class_loader, false);
88 if (field == NULL) {
Elliott Hughes1f1fa562011-08-26 10:22:53 -070089 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070090 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070091 }
92}
93
Brian Carlstrom83db7722011-08-26 17:32:56 -070094void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070095 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070096 for (size_t i = 0; i != class_path.size(); ++i) {
97 const DexFile* dex_file = class_path[i];
98 CHECK(dex_file != NULL);
99 CompileDexFile(class_loader, *dex_file);
100 }
101}
102
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
104 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
105 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
106 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
107 const char* descriptor = dex_file.GetClassDescriptor(class_def);
108 Class* klass = class_linker->FindClass(descriptor, class_loader);
109 CHECK(klass != NULL);
110 CompileClass(klass);
111 }
112}
113
114void Compiler::CompileClass(Class* klass) {
115 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
116 CompileMethod(klass->GetDirectMethod(i));
117 }
118 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
119 CompileMethod(klass->GetVirtualMethod(i));
120 }
121}
122
Ian Rogers2c8f6532011-09-02 17:16:34 -0700123namespace arm {
124 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700125}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700126namespace x86 {
127 void X86CreateInvokeStub(Method* method);
128}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700129
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700130void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700131 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700132 jni_compiler_.Compile(method);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700133 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700134 DCHECK(abstract_method_error_stub_ != NULL);
135 if (instruction_set_ == kX86) {
136 method->SetCode(abstract_method_error_stub_, kX86);
137 } else {
138 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
139 method->SetCode(abstract_method_error_stub_, kArm);
140 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700141 } else {
142 oatCompileMethod(method, kThumb2);
143 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700144 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700145
Ian Rogers2c8f6532011-09-02 17:16:34 -0700146 if (instruction_set_ == kX86) {
147 art::x86::X86CreateInvokeStub(method);
148 } else {
149 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
150 // Generates invocation stub using ARM instruction set
151 art::arm::ArmCreateInvokeStub(method);
152 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700153 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700154}
155
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700156void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700157 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700158 for (size_t i = 0; i != class_path.size(); ++i) {
159 const DexFile* dex_file = class_path[i];
160 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700161 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700162 }
163}
164
Brian Carlstrom8a487412011-08-29 20:08:52 -0700165void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700166 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
167 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700168 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700169 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700170 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700171 if (method == NULL) {
172 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
173 } else if (method->IsDirect()) {
174 code_and_direct_methods->SetResolvedDirectMethod(i, method);
175 } else {
176 // TODO: we currently leave the entry blank for resolved
177 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700178 }
179 }
180}
181
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700182} // namespace art