blob: 37f82980510be59f3e4b6ef5df8a109efc55e70d [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"
7#include "dex_cache.h"
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07008#include "jni_compiler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07009
10extern bool oatCompileMethod(art::Method*, art::InstructionSet);
11
12namespace art {
13
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070014// TODO need to specify target
Brian Carlstrom8a487412011-08-29 20:08:52 -070015void Compiler::CompileAll(const ClassLoader* class_loader) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070016 Resolve(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070017 // TODO add verification step
18 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070019 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom8a487412011-08-29 20:08:52 -070020}
21
22void Compiler::CompileOne(Method* method) {
23 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
24 Resolve(class_loader);
25 // TODO add verification step
26 CompileMethod(method);
27 SetCodeAndDirectMethods(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070028}
29
30void Compiler::Resolve(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070031 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070032 for (size_t i = 0; i != class_path.size(); ++i) {
33 const DexFile* dex_file = class_path[i];
34 CHECK(dex_file != NULL);
35 ResolveDexFile(class_loader, *dex_file);
36 }
37}
38
39void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
40 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
41
42 // Strings are easy, they always are simply resolved to literals in the same file
43 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
44 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
45 class_linker->ResolveString(dex_file, i, dex_cache);
46 }
47
48 // Class derived values are more complicated, they require the linker and loader
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070049 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070050 class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
51 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070052 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070053 // unknown if direct or virtual, try both
54 Method* method = class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, false);
55 if (method == NULL) {
56 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
57 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070058 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070059 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070060 // unknown if instance or static, try both
61 Field* field = class_linker->ResolveField(dex_file, i, dex_cache, class_loader, false);
62 if (field == NULL) {
Elliott Hughes1f1fa562011-08-26 10:22:53 -070063 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070064 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070065 }
66}
67
Brian Carlstrom83db7722011-08-26 17:32:56 -070068void Compiler::Compile(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -070069 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070070 for (size_t i = 0; i != class_path.size(); ++i) {
71 const DexFile* dex_file = class_path[i];
72 CHECK(dex_file != NULL);
73 CompileDexFile(class_loader, *dex_file);
74 }
75}
76
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070077void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
78 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
79 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
80 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
81 const char* descriptor = dex_file.GetClassDescriptor(class_def);
82 Class* klass = class_linker->FindClass(descriptor, class_loader);
83 CHECK(klass != NULL);
84 CompileClass(klass);
85 }
86}
87
88void Compiler::CompileClass(Class* klass) {
89 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
90 CompileMethod(klass->GetDirectMethod(i));
91 }
92 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
93 CompileMethod(klass->GetVirtualMethod(i));
94 }
95}
96
97void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070098 if (method->IsNative()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -070099 // TODO note code will be unmapped when JniCompiler goes out of scope
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700100 Assembler jni_asm;
101 JniCompiler jni_compiler;
102 jni_compiler.Compile(&jni_asm, method);
103 } else if (method->IsAbstract()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700104 // TODO: This might be also noted in the ClassLinker.
105 // Probably makes more sense to do here?
106 UNIMPLEMENTED(FATAL) << "compile stub to throw AbstractMethodError";
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700107 } else {
108 oatCompileMethod(method, kThumb2);
109 }
Brian Carlstrombffb1552011-08-25 12:23:53 -0700110 // CHECK(method->HasCode()); // TODO: enable this check ASAP
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700111}
112
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700113void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700114 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700115 for (size_t i = 0; i != class_path.size(); ++i) {
116 const DexFile* dex_file = class_path[i];
117 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700118 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700119 }
120}
121
Brian Carlstrom8a487412011-08-29 20:08:52 -0700122void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700123 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
124 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700125 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700126 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700127 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700128 if (method == NULL) {
129 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
130 } else if (method->IsDirect()) {
131 code_and_direct_methods->SetResolvedDirectMethod(i, method);
132 } else {
133 // TODO: we currently leave the entry blank for resolved
134 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700135 }
136 }
137}
138
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700139} // namespace art