blob: 52babf7fa0d270f131a6c7f4e05c6b56932c39d2 [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++) {
49 // TODO: move resolution into compiler proper where we will know method_type
50 int method_type = 0;
51 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, method_type);
52 }
53 for (size_t i = 0; i < dex_cache->NumFields(); i++) {
54 // TODO: move resolution into compiler proper where we will know is_static
55 bool is_static = false;
56 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, is_static);
57 }
58}
59
60void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
61 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
62 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
63 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
64 const char* descriptor = dex_file.GetClassDescriptor(class_def);
65 Class* klass = class_linker->FindClass(descriptor, class_loader);
66 CHECK(klass != NULL);
67 CompileClass(klass);
68 }
69}
70
71void Compiler::CompileClass(Class* klass) {
72 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
73 CompileMethod(klass->GetDirectMethod(i));
74 }
75 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
76 CompileMethod(klass->GetVirtualMethod(i));
77 }
78}
79
80void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070081 if (method->IsNative()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -070082 // TODO note code will be unmapped when JniCompiler goes out of scope
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070083 Assembler jni_asm;
84 JniCompiler jni_compiler;
85 jni_compiler.Compile(&jni_asm, method);
86 } else if (method->IsAbstract()) {
Brian Carlstrombffb1552011-08-25 12:23:53 -070087 // TODO: This might be also noted in the ClassLinker.
88 // Probably makes more sense to do here?
89 UNIMPLEMENTED(FATAL) << "compile stub to throw AbstractMethodError";
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070090 } else {
91 oatCompileMethod(method, kThumb2);
92 }
Brian Carlstrombffb1552011-08-25 12:23:53 -070093 // CHECK(method->HasCode()); // TODO: enable this check ASAP
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070094}
95
96} // namespace art