blob: 8f529ce767b3807cd0648a73e0e5e68cfa151f6e [file] [log] [blame]
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "compiler.h"
4
5#include "class_linker.h"
6#include "dex_cache.h"
7
8extern bool oatCompileMethod(art::Method*, art::InstructionSet);
9
10namespace art {
11
12void Compiler::Compile(std::vector<const DexFile*> class_path) {
13 ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
14 Resolve(class_loader);
15 for (size_t i = 0; i != class_path.size(); ++i) {
16 const DexFile* dex_file = class_path[i];
17 CHECK(dex_file != NULL);
18 CompileDexFile(class_loader, *dex_file);
19 }
20}
21
22void Compiler::Resolve(const ClassLoader* class_loader) {
23 const std::vector<const DexFile*>& class_path = class_loader->GetClassPath();
24 for (size_t i = 0; i != class_path.size(); ++i) {
25 const DexFile* dex_file = class_path[i];
26 CHECK(dex_file != NULL);
27 ResolveDexFile(class_loader, *dex_file);
28 }
29}
30
31void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
32 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
33
34 // Strings are easy, they always are simply resolved to literals in the same file
35 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
36 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
37 class_linker->ResolveString(dex_file, i, dex_cache);
38 }
39
40 // Class derived values are more complicated, they require the linker and loader
41 for (size_t i = 0; i < dex_cache->NumTypes(); i++) {
42 class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
43 }
44 for (size_t i = 0; i < dex_cache->NumMethods(); i++) {
45 // TODO: move resolution into compiler proper where we will know method_type
46 int method_type = 0;
47 class_linker->ResolveMethod(dex_file, i, dex_cache, class_loader, method_type);
48 }
49 for (size_t i = 0; i < dex_cache->NumFields(); i++) {
50 // TODO: move resolution into compiler proper where we will know is_static
51 bool is_static = false;
52 class_linker->ResolveField(dex_file, i, dex_cache, class_loader, is_static);
53 }
54}
55
56void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
57 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
58 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
59 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
60 const char* descriptor = dex_file.GetClassDescriptor(class_def);
61 Class* klass = class_linker->FindClass(descriptor, class_loader);
62 CHECK(klass != NULL);
63 CompileClass(klass);
64 }
65}
66
67void Compiler::CompileClass(Class* klass) {
68 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
69 CompileMethod(klass->GetDirectMethod(i));
70 }
71 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
72 CompileMethod(klass->GetVirtualMethod(i));
73 }
74}
75
76void Compiler::CompileMethod(Method* method) {
77// TODO need to compile art/src/compiler for host as well as target
78#ifdef __arm__
79 oatCompileMethod(method, kThumb2);
80#endif
81}
82
83} // namespace art