blob: c8ff794f5d1dcef58504ad8379b62a34f1782bd4 [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 Carlstrombffb1552011-08-25 12:23:53 -070015const ClassLoader* Compiler::Compile(std::vector<const DexFile*> class_path) {
16 const ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070017 Resolve(class_loader);
Brian Carlstrom83db7722011-08-26 17:32:56 -070018 // TODO add verification step
19 Compile(class_loader);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070020 SetCodeAndDirectMethods(class_loader);
Brian Carlstrombffb1552011-08-25 12:23:53 -070021 return class_loader;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070022}
23
24void Compiler::Resolve(const ClassLoader* class_loader) {
25 const std::vector<const DexFile*>& class_path = class_loader->GetClassPath();
26 for (size_t i = 0; i != class_path.size(); ++i) {
27 const DexFile* dex_file = class_path[i];
28 CHECK(dex_file != NULL);
29 ResolveDexFile(class_loader, *dex_file);
30 }
31}
32
33void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
34 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
35
36 // Strings are easy, they always are simply resolved to literals in the same file
37 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
38 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
39 class_linker->ResolveString(dex_file, i, dex_cache);
40 }
41
42 // Class derived values are more complicated, they require the linker and loader
43 for (size_t i = 0; i < dex_cache->NumTypes(); i++) {
44 class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
45 }
46 for (size_t i = 0; i < dex_cache->NumMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070047 // unknown if direct or virtual, try both
48 Method* method = class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, false);
49 if (method == NULL) {
50 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
51 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070052 }
53 for (size_t i = 0; i < dex_cache->NumFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070054 // unknown if instance or static, try both
55 Field* field = class_linker->ResolveField(dex_file, i, dex_cache, class_loader, false);
56 if (field == NULL) {
Elliott Hughes1f1fa562011-08-26 10:22:53 -070057 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, true);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070058 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070059 }
60}
61
Brian Carlstrom83db7722011-08-26 17:32:56 -070062void Compiler::Compile(const ClassLoader* class_loader) {
63 const std::vector<const DexFile*>& class_path = class_loader->GetClassPath();
64 for (size_t i = 0; i != class_path.size(); ++i) {
65 const DexFile* dex_file = class_path[i];
66 CHECK(dex_file != NULL);
67 CompileDexFile(class_loader, *dex_file);
68 }
69}
70
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070071void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
72 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
73 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
74 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
75 const char* descriptor = dex_file.GetClassDescriptor(class_def);
76 Class* klass = class_linker->FindClass(descriptor, class_loader);
77 CHECK(klass != NULL);
78 CompileClass(klass);
79 }
80}
81
82void Compiler::CompileClass(Class* klass) {
83 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
84 CompileMethod(klass->GetDirectMethod(i));
85 }
86 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
87 CompileMethod(klass->GetVirtualMethod(i));
88 }
89}
90
91void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070092 if (method->IsNative()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -070093 // TODO note code will be unmapped when JniCompiler goes out of scope
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070094 Assembler jni_asm;
95 JniCompiler jni_compiler;
96 jni_compiler.Compile(&jni_asm, method);
97 } else if (method->IsAbstract()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -070098 // TODO: This might be also noted in the ClassLinker.
99 // Probably makes more sense to do here?
100 UNIMPLEMENTED(FATAL) << "compile stub to throw AbstractMethodError";
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700101 } else {
102 oatCompileMethod(method, kThumb2);
103 }
Brian Carlstrombffb1552011-08-25 12:23:53 -0700104 // CHECK(method->HasCode()); // TODO: enable this check ASAP
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700105}
106
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700107void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700108 const std::vector<const DexFile*>& class_path = class_loader->GetClassPath();
109 for (size_t i = 0; i != class_path.size(); ++i) {
110 const DexFile* dex_file = class_path[i];
111 CHECK(dex_file != NULL);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700112 SetCodeAndDirectMethodsDexFile(class_loader, *dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700113 }
114}
115
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700116void Compiler::SetCodeAndDirectMethodsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700117 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
118 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700119 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700120 for (size_t i = 0; i < dex_cache->NumMethods(); i++) {
121 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700122 if (method == NULL) {
123 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i);
124 } else if (method->IsDirect()) {
125 code_and_direct_methods->SetResolvedDirectMethod(i, method);
126 } else {
127 // TODO: we currently leave the entry blank for resolved
128 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700129 }
130 }
131}
132
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700133} // namespace art