blob: 8396bdffa52efc183553ce243b6454a7814cd851 [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 Carlstrom1f870082011-08-23 16:02:11 -070010#include "runtime.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070011
12extern bool oatCompileMethod(art::Method*, art::InstructionSet);
13
14namespace art {
15
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070016// TODO need to specify target
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
99void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700100 if (method->IsNative()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700101 // TODO note code will be unmapped when JniCompiler goes out of scope
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700102 Assembler jni_asm;
103 JniCompiler jni_compiler;
104 jni_compiler.Compile(&jni_asm, method);
105 } else if (method->IsAbstract()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700106 // TODO: This might be also noted in the ClassLinker.
107 // Probably makes more sense to do here?
108 UNIMPLEMENTED(FATAL) << "compile stub to throw AbstractMethodError";
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700109 } else {
110 oatCompileMethod(method, kThumb2);
111 }
Brian Carlstrombffb1552011-08-25 12:23:53 -0700112 // CHECK(method->HasCode()); // TODO: enable this check ASAP
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700113}
114
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700115void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom8a487412011-08-29 20:08:52 -0700116 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700117 for (size_t i = 0; i != class_path.size(); ++i) {
118 const DexFile* dex_file = class_path[i];
119 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700120 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700121 }
122}
123
Brian Carlstrom8a487412011-08-29 20:08:52 -0700124void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700125 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
126 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700127 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700128 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700129 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700130 if (method == NULL) {
131 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
132 } else if (method->IsDirect()) {
133 code_and_direct_methods->SetResolvedDirectMethod(i, method);
134 } else {
135 // TODO: we currently leave the entry blank for resolved
136 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700137 }
138 }
139}
140
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700141} // namespace art