blob: fa9867d2c7a452113481926834a17761b6d947cf [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) {
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
34Compiler::Compiler(InstructionSet insns) : instruction_set_(insns), jni_compiler_(insns) {
35 if (insns == kArm || insns == kThumb2) {
36 abstract_method_error_stub_ = arm::CreateAbstractMethodErrorStub(&ThrowAbstractMethodError);
37 } else if (insns == kX86) {
38 abstract_method_error_stub_ = x86::CreateAbstractMethodErrorStub(&ThrowAbstractMethodError);
39 }
40}
41
Brian Carlstrom8a487412011-08-29 20:08:52 -070042void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070043 Resolve(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070044 // TODO add verification step
45 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070046 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070047}
48
49void Compiler::CompileOne(Method* method) {
50 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
51 Resolve(class_loader);
52 // TODO add verification step
53 CompileMethod(method);
54 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070055}
56
57void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070058 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070059 for (size_t i = 0; i != class_path.size(); ++i) {
60 const DexFile* dex_file = class_path[i];
61 CHECK(dex_file != NULL);
62 ResolveDexFile(class_loader, *dex_file);
63 }
64}
65
66void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
67 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
68
69 // Strings are easy, they always are simply resolved to literals in the same file
70 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
71 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
72 class_linker->ResolveString(dex_file, i, dex_cache);
73 }
74
75 // Class derived values are more complicated, they require the linker and loader
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070076 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070077 class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
78 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070079 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070080 // unknown if direct or virtual, try both
81 Method* method = class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, false);
82 if (method == NULL) {
83 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
84 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070085 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070086 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070087 // unknown if instance or static, try both
88 Field* field = class_linker->ResolveField(dex_file, i, dex_cache, class_loader, false);
89 if (field == NULL) {
Elliott Hughes1f1fa562011-08-26 10:22:53 -070090 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070091 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070092 }
93}
94
Brian Carlstrom83db7722011-08-26 17:32:56 -070095void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070096 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070097 for (size_t i = 0; i != class_path.size(); ++i) {
98 const DexFile* dex_file = class_path[i];
99 CHECK(dex_file != NULL);
100 CompileDexFile(class_loader, *dex_file);
101 }
102}
103
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700104void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
105 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
106 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
107 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
108 const char* descriptor = dex_file.GetClassDescriptor(class_def);
109 Class* klass = class_linker->FindClass(descriptor, class_loader);
110 CHECK(klass != NULL);
111 CompileClass(klass);
112 }
113}
114
115void Compiler::CompileClass(Class* klass) {
116 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
117 CompileMethod(klass->GetDirectMethod(i));
118 }
119 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
120 CompileMethod(klass->GetVirtualMethod(i));
121 }
122}
123
Ian Rogers2c8f6532011-09-02 17:16:34 -0700124namespace arm {
125 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700126}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700127namespace x86 {
128 void X86CreateInvokeStub(Method* method);
129}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700130
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700131void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700132 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700133 jni_compiler_.Compile(method);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700134 } else if (method->IsAbstract()) {
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700135 DCHECK(abstract_method_error_stub_ != NULL);
136 if (instruction_set_ == kX86) {
137 method->SetCode(abstract_method_error_stub_, kX86);
138 } else {
139 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
140 method->SetCode(abstract_method_error_stub_, kArm);
141 }
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700142 } else {
143 oatCompileMethod(method, kThumb2);
144 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700145 CHECK(method->GetCode() != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700146
Ian Rogers2c8f6532011-09-02 17:16:34 -0700147 if (instruction_set_ == kX86) {
148 art::x86::X86CreateInvokeStub(method);
149 } else {
150 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
151 // Generates invocation stub using ARM instruction set
152 art::arm::ArmCreateInvokeStub(method);
153 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700154 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700155}
156
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700157void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700158 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700159 for (size_t i = 0; i != class_path.size(); ++i) {
160 const DexFile* dex_file = class_path[i];
161 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700162 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700163 }
164}
165
Brian Carlstrom8a487412011-08-29 20:08:52 -0700166void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700167 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
168 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700169 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700170 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700171 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700172 if (method == NULL) {
173 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
174 } else if (method->IsDirect()) {
175 code_and_direct_methods->SetResolvedDirectMethod(i, method);
176 } else {
177 // TODO: we currently leave the entry blank for resolved
178 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700179 }
180 }
181}
182
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700183} // namespace art