blob: 0324586d7dee7cdc4e550de301d77e73333efaf7 [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);
18 for (size_t i = 0; i != class_path.size(); ++i) {
19 const DexFile* dex_file = class_path[i];
20 CHECK(dex_file != NULL);
21 CompileDexFile(class_loader, *dex_file);
22 }
Brian Carlstrombffb1552011-08-25 12:23:53 -070023 return class_loader;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070024}
25
26void Compiler::Resolve(const ClassLoader* class_loader) {
27 const std::vector<const DexFile*>& class_path = class_loader->GetClassPath();
28 for (size_t i = 0; i != class_path.size(); ++i) {
29 const DexFile* dex_file = class_path[i];
30 CHECK(dex_file != NULL);
31 ResolveDexFile(class_loader, *dex_file);
32 }
33}
34
35void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
36 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
37
38 // Strings are easy, they always are simply resolved to literals in the same file
39 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
40 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
41 class_linker->ResolveString(dex_file, i, dex_cache);
42 }
43
44 // Class derived values are more complicated, they require the linker and loader
45 for (size_t i = 0; i < dex_cache->NumTypes(); i++) {
46 class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
47 }
48 for (size_t i = 0; i < dex_cache->NumMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070049 // unknown if direct or virtual, try both
50 Method* method = class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, false);
51 if (method == NULL) {
52 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
53 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070054 }
55 for (size_t i = 0; i < dex_cache->NumFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070056 // unknown if instance or static, try both
57 Field* field = class_linker->ResolveField(dex_file, i, dex_cache, class_loader, false);
58 if (field == NULL) {
59 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, true);
60 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070061 }
62}
63
64void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
65 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
66 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
67 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
68 const char* descriptor = dex_file.GetClassDescriptor(class_def);
69 Class* klass = class_linker->FindClass(descriptor, class_loader);
70 CHECK(klass != NULL);
71 CompileClass(klass);
72 }
73}
74
75void Compiler::CompileClass(Class* klass) {
76 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
77 CompileMethod(klass->GetDirectMethod(i));
78 }
79 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
80 CompileMethod(klass->GetVirtualMethod(i));
81 }
82}
83
84void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070085 if (method->IsNative()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -070086 // TODO note code will be unmapped when JniCompiler goes out of scope
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070087 Assembler jni_asm;
88 JniCompiler jni_compiler;
89 jni_compiler.Compile(&jni_asm, method);
90 } else if (method->IsAbstract()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -070091 // TODO: This might be also noted in the ClassLinker.
92 // Probably makes more sense to do here?
93 UNIMPLEMENTED(FATAL) << "compile stub to throw AbstractMethodError";
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070094 } else {
95 oatCompileMethod(method, kThumb2);
96 }
Brian Carlstrombffb1552011-08-25 12:23:53 -070097 // CHECK(method->HasCode()); // TODO: enable this check ASAP
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070098}
99
100} // namespace art