blob: a623d20d50a6e27b00311934b2b15efe08ec4778 [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 Carlstrom9ea1cb12011-08-24 23:18:18 -070015void Compiler::Compile(std::vector<const DexFile*> class_path) {
16 ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
17 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 }
23}
24
25void Compiler::Resolve(const ClassLoader* class_loader) {
26 const std::vector<const DexFile*>& class_path = class_loader->GetClassPath();
27 for (size_t i = 0; i != class_path.size(); ++i) {
28 const DexFile* dex_file = class_path[i];
29 CHECK(dex_file != NULL);
30 ResolveDexFile(class_loader, *dex_file);
31 }
32}
33
34void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
35 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
36
37 // Strings are easy, they always are simply resolved to literals in the same file
38 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
39 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
40 class_linker->ResolveString(dex_file, i, dex_cache);
41 }
42
43 // Class derived values are more complicated, they require the linker and loader
44 for (size_t i = 0; i < dex_cache->NumTypes(); i++) {
45 class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
46 }
47 for (size_t i = 0; i < dex_cache->NumMethods(); i++) {
48 // TODO: move resolution into compiler proper where we will know method_type
49 int method_type = 0;
50 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, method_type);
51 }
52 for (size_t i = 0; i < dex_cache->NumFields(); i++) {
53 // TODO: move resolution into compiler proper where we will know is_static
54 bool is_static = false;
55 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, is_static);
56 }
57}
58
59void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
60 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
61 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
62 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
63 const char* descriptor = dex_file.GetClassDescriptor(class_def);
64 Class* klass = class_linker->FindClass(descriptor, class_loader);
65 CHECK(klass != NULL);
66 CompileClass(klass);
67 }
68}
69
70void Compiler::CompileClass(Class* klass) {
71 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
72 CompileMethod(klass->GetDirectMethod(i));
73 }
74 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
75 CompileMethod(klass->GetVirtualMethod(i));
76 }
77}
78
79void Compiler::CompileMethod(Method* method) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070080 // TODO remove this as various compilers come on line
81 if (true) {
82 return;
83 }
84 if (method->IsNative()) {
85 Assembler jni_asm;
86 JniCompiler jni_compiler;
87 jni_compiler.Compile(&jni_asm, method);
88 } else if (method->IsAbstract()) {
89 // TODO setup precanned code to throw something like AbstractMethodError
90 } else {
91 oatCompileMethod(method, kThumb2);
92 }
93 CHECK(method->HasCode());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070094}
95
96} // namespace art