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++) { |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 49 | // 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 Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 54 | } |
| 55 | for (size_t i = 0; i < dex_cache->NumFields(); i++) { |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 56 | // 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 Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | void 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 | |
| 75 | void 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 | |
| 84 | void Compiler::CompileMethod(Method* method) { |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 85 | if (method->IsNative()) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 86 | // TODO note code will be unmapped when JniCompiler goes out of scope |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 87 | Assembler jni_asm; |
| 88 | JniCompiler jni_compiler; |
| 89 | jni_compiler.Compile(&jni_asm, method); |
| 90 | } else if (method->IsAbstract()) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 91 | // 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 Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 94 | } else { |
| 95 | oatCompileMethod(method, kThumb2); |
| 96 | } |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 97 | // CHECK(method->HasCode()); // TODO: enable this check ASAP |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace art |