blob: e2768bfa6593c9ceb2fc836c7b81757cab61b4e4 [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
Brian Carlstrom8a487412011-08-29 20:08:52 -070017void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070018 Resolve(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070019 // TODO add verification step
20 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070021 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070022}
23
24void Compiler::CompileOne(Method* method) {
25 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
26 Resolve(class_loader);
27 // TODO add verification step
28 CompileMethod(method);
29 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070030}
31
32void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070033 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070034 for (size_t i = 0; i != class_path.size(); ++i) {
35 const DexFile* dex_file = class_path[i];
36 CHECK(dex_file != NULL);
37 ResolveDexFile(class_loader, *dex_file);
38 }
39}
40
41void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
42 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
43
44 // Strings are easy, they always are simply resolved to literals in the same file
45 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
46 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
47 class_linker->ResolveString(dex_file, i, dex_cache);
48 }
49
50 // Class derived values are more complicated, they require the linker and loader
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070051 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070052 class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
53 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070054 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070055 // unknown if direct or virtual, try both
56 Method* method = class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, false);
57 if (method == NULL) {
58 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
59 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070060 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070061 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070062 // unknown if instance or static, try both
63 Field* field = class_linker->ResolveField(dex_file, i, dex_cache, class_loader, false);
64 if (field == NULL) {
Elliott Hughes1f1fa562011-08-26 10:22:53 -070065 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070066 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070067 }
68}
69
Brian Carlstrom83db7722011-08-26 17:32:56 -070070void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070071 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070072 for (size_t i = 0; i != class_path.size(); ++i) {
73 const DexFile* dex_file = class_path[i];
74 CHECK(dex_file != NULL);
75 CompileDexFile(class_loader, *dex_file);
76 }
77}
78
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070079void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
80 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
81 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
82 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
83 const char* descriptor = dex_file.GetClassDescriptor(class_def);
84 Class* klass = class_linker->FindClass(descriptor, class_loader);
85 CHECK(klass != NULL);
86 CompileClass(klass);
87 }
88}
89
90void Compiler::CompileClass(Class* klass) {
91 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
92 CompileMethod(klass->GetDirectMethod(i));
93 }
94 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
95 CompileMethod(klass->GetVirtualMethod(i));
96 }
97}
98
Ian Rogers2c8f6532011-09-02 17:16:34 -070099namespace arm {
100 void ArmCreateInvokeStub(Method* method);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700101}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700102namespace x86 {
103 void X86CreateInvokeStub(Method* method);
104}
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700105
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700106void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700107 if (method->IsNative()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700108 jni_compiler_.Compile(method);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700109 } else if (method->IsAbstract()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700110 // TODO: This might be also noted in the ClassLinker.
111 // Probably makes more sense to do here?
buzbeee9a72f62011-09-04 17:59:07 -0700112 UNIMPLEMENTED(WARNING) << "compile stub to throw AbstractMethodError";
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700113 } else {
114 oatCompileMethod(method, kThumb2);
115 }
Elliott Hughes1240dad2011-09-09 16:24:50 -0700116 // CHECK(method->GetCode() != NULL); // TODO: enable this check ASAP
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700117
Ian Rogers2c8f6532011-09-02 17:16:34 -0700118 if (instruction_set_ == kX86) {
119 art::x86::X86CreateInvokeStub(method);
120 } else {
121 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
122 // Generates invocation stub using ARM instruction set
123 art::arm::ArmCreateInvokeStub(method);
124 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700125 CHECK(method->GetInvokeStub() != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700126}
127
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700128void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700129 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700130 for (size_t i = 0; i != class_path.size(); ++i) {
131 const DexFile* dex_file = class_path[i];
132 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700133 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700134 }
135}
136
Brian Carlstrom8a487412011-08-29 20:08:52 -0700137void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700138 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
139 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700140 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700141 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700142 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700143 if (method == NULL) {
144 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
145 } else if (method->IsDirect()) {
146 code_and_direct_methods->SetResolvedDirectMethod(i, method);
147 } else {
148 // TODO: we currently leave the entry blank for resolved
149 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700150 }
151 }
152}
153
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700154} // namespace art