Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "compiler.h" |
| 4 | |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 5 | #include "assembler.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 6 | #include "class_linker.h" |
| 7 | #include "dex_cache.h" |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 8 | #include "jni_compiler.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9 | |
| 10 | extern bool oatCompileMethod(art::Method*, art::InstructionSet); |
| 11 | |
| 12 | namespace art { |
| 13 | |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 14 | // TODO need to specify target |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 15 | const ClassLoader* Compiler::Compile(std::vector<const DexFile*> class_path) { |
| 16 | const ClassLoader* class_loader = PathClassLoader::Alloc(class_path); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 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 | } |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 23 | return class_loader; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void 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 | |
| 35 | void 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 | |
| 60 | void 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 | |
| 71 | void 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 | |
| 80 | void Compiler::CompileMethod(Method* method) { |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 81 | if (method->IsNative()) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 82 | // TODO note code will be unmapped when JniCompiler goes out of scope |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 83 | Assembler jni_asm; |
| 84 | JniCompiler jni_compiler; |
| 85 | jni_compiler.Compile(&jni_asm, method); |
| 86 | } else if (method->IsAbstract()) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 87 | // 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 Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 90 | } else { |
| 91 | oatCompileMethod(method, kThumb2); |
| 92 | } |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 93 | // CHECK(method->HasCode()); // TODO: enable this check ASAP |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace art |