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 | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 5 | #include <sys/mman.h> |
| 6 | |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 7 | #include "assembler.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 9 | #include "class_loader.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 10 | #include "dex_cache.h" |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 11 | #include "jni_compiler.h" |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 12 | #include "jni_internal.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 13 | #include "runtime.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 14 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 15 | extern bool oatCompileMethod(const art::Compiler& compiler, art::Method*, art::InstructionSet); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 16 | |
| 17 | namespace art { |
| 18 | |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 19 | namespace arm { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 20 | ByteArray* CreateAbstractMethodErrorStub(); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | namespace x86 { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 24 | ByteArray* CreateAbstractMethodErrorStub(); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 27 | ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) { |
| 28 | switch (instruction_set) { |
| 29 | case kArm: |
| 30 | case kThumb2: |
| 31 | return arm::CreateAbstractMethodErrorStub(); |
| 32 | case kX86: |
| 33 | return x86::CreateAbstractMethodErrorStub(); |
| 34 | default: |
| 35 | LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set; |
| 36 | return NULL; |
| 37 | } |
| 38 | } |
| 39 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 40 | Compiler::Compiler(InstructionSet insns) : instruction_set_(insns), jni_compiler_(insns), |
| 41 | verbose_(false) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 42 | CHECK(!Runtime::Current()->IsStarted()); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 45 | void Compiler::CompileAll(const ClassLoader* class_loader) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 46 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 47 | Resolve(class_loader); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 48 | Verify(class_loader); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 49 | InitializeClassesWithoutClinit(class_loader); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 50 | Compile(class_loader); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 51 | SetCodeAndDirectMethods(class_loader); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void Compiler::CompileOne(Method* method) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 55 | DCHECK(!Runtime::Current()->IsStarted()); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 56 | const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader(); |
| 57 | Resolve(class_loader); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 58 | Verify(class_loader); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 59 | InitializeClassesWithoutClinit(class_loader); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 60 | CompileMethod(method); |
| 61 | SetCodeAndDirectMethods(class_loader); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void Compiler::Resolve(const ClassLoader* class_loader) { |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 65 | const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 66 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 67 | const DexFile* dex_file = class_path[i]; |
| 68 | CHECK(dex_file != NULL); |
| 69 | ResolveDexFile(class_loader, *dex_file); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 74 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 75 | |
| 76 | // Strings are easy, they always are simply resolved to literals in the same file |
| 77 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 78 | for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) { |
| 79 | class_linker->ResolveString(dex_file, string_idx, dex_cache); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 82 | // Class derived values are more complicated, they require the linker and loader. |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 83 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 84 | Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 85 | if (klass == NULL) { |
| 86 | Thread::Current()->ClearException(); |
| 87 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 88 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 89 | |
| 90 | // Method and Field are the worst. We can't resolve without either |
| 91 | // context from the code use (to disambiguate virtual vs direct |
| 92 | // method and instance vs static field) or from class |
| 93 | // definitions. While the compiler will resolve what it can as it |
| 94 | // needs it, here we try to resolve fields and methods used in class |
| 95 | // definitions, since many of them many never be referenced by |
| 96 | // generated code. |
| 97 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 98 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 99 | |
| 100 | // Note the class_data pointer advances through the headers, |
| 101 | // static fields, instance fields, direct methods, and virtual |
| 102 | // methods. |
| 103 | const byte* class_data = dex_file.GetClassData(class_def); |
| 104 | |
| 105 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
| 106 | size_t num_static_fields = header.static_fields_size_; |
| 107 | size_t num_instance_fields = header.instance_fields_size_; |
| 108 | size_t num_direct_methods = header.direct_methods_size_; |
| 109 | size_t num_virtual_methods = header.virtual_methods_size_; |
| 110 | |
| 111 | if (num_static_fields != 0) { |
| 112 | uint32_t last_idx = 0; |
| 113 | for (size_t i = 0; i < num_static_fields; ++i) { |
| 114 | DexFile::Field dex_field; |
| 115 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 116 | Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache, |
| 117 | class_loader, true); |
| 118 | if (field == NULL) { |
| 119 | Thread* self = Thread::Current(); |
| 120 | CHECK(self->IsExceptionPending()); |
| 121 | self->ClearException(); |
| 122 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 123 | } |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 124 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 125 | if (num_instance_fields != 0) { |
| 126 | uint32_t last_idx = 0; |
| 127 | for (size_t i = 0; i < num_instance_fields; ++i) { |
| 128 | DexFile::Field dex_field; |
| 129 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 130 | Field* field = class_linker->ResolveField(dex_file, dex_field.field_idx_, dex_cache, |
| 131 | class_loader, false); |
| 132 | if (field == NULL) { |
| 133 | Thread* self = Thread::Current(); |
| 134 | CHECK(self->IsExceptionPending()); |
| 135 | self->ClearException(); |
| 136 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | if (num_direct_methods != 0) { |
| 140 | uint32_t last_idx = 0; |
| 141 | for (size_t i = 0; i < num_direct_methods; ++i) { |
| 142 | DexFile::Method dex_method; |
| 143 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 144 | Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache, |
| 145 | class_loader, true); |
| 146 | if (method == NULL) { |
| 147 | Thread* self = Thread::Current(); |
| 148 | CHECK(self->IsExceptionPending()); |
| 149 | self->ClearException(); |
| 150 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | if (num_virtual_methods != 0) { |
| 154 | uint32_t last_idx = 0; |
| 155 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 156 | DexFile::Method dex_method; |
| 157 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 158 | Method* method = class_linker->ResolveMethod(dex_file, dex_method.method_idx_, dex_cache, |
| 159 | class_loader, false); |
| 160 | if (method == NULL) { |
| 161 | Thread* self = Thread::Current(); |
| 162 | CHECK(self->IsExceptionPending()); |
| 163 | self->ClearException(); |
| 164 | } |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 165 | } |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 166 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 170 | void Compiler::Verify(const ClassLoader* class_loader) { |
| 171 | const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader); |
| 172 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 173 | const DexFile* dex_file = class_path[i]; |
| 174 | CHECK(dex_file != NULL); |
| 175 | VerifyDexFile(class_loader, *dex_file); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 180 | dex_file.ChangePermissions(PROT_READ | PROT_WRITE); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 181 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 182 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 183 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 184 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 185 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 186 | if (klass == NULL) { |
| 187 | Thread* self = Thread::Current(); |
| 188 | CHECK(self->IsExceptionPending()); |
| 189 | self->ClearException(); |
| 190 | continue; |
| 191 | } |
| 192 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 193 | class_linker->VerifyClass(klass); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 194 | CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass); |
| 195 | CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException()); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 196 | } |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 197 | dex_file.ChangePermissions(PROT_READ); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) { |
| 201 | const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader); |
| 202 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 203 | const DexFile* dex_file = class_path[i]; |
| 204 | CHECK(dex_file != NULL); |
| 205 | InitializeClassesWithoutClinit(class_loader, *dex_file); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 210 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 211 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 212 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 213 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 214 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 215 | if (klass != NULL) { |
| 216 | class_linker->EnsureInitialized(klass, false); |
| 217 | } |
| 218 | // clear any class not found or verification exceptions |
| 219 | Thread::Current()->ClearException(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
| 223 | for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) { |
| 224 | Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 225 | if (klass == NULL) { |
| 226 | Thread::Current()->ClearException(); |
| 227 | } else if (klass->IsInitialized()) { |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 228 | dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass); |
| 229 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 233 | void Compiler::Compile(const ClassLoader* class_loader) { |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 234 | const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 235 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 236 | const DexFile* dex_file = class_path[i]; |
| 237 | CHECK(dex_file != NULL); |
| 238 | CompileDexFile(class_loader, *dex_file); |
| 239 | } |
| 240 | } |
| 241 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 242 | void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) { |
| 243 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ffca45d | 2011-09-16 12:10:49 -0700 | [diff] [blame] | 244 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) { |
| 245 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 246 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 247 | Class* klass = class_linker->FindClass(descriptor, class_loader); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 248 | if (klass == NULL) { |
| 249 | // previous verification error will cause FindClass to throw |
| 250 | Thread* self = Thread::Current(); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 251 | CHECK(self->IsExceptionPending()); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 252 | self->ClearException(); |
| 253 | } else { |
| 254 | CompileClass(klass); |
| 255 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | void Compiler::CompileClass(Class* klass) { |
| 260 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 261 | CompileMethod(klass->GetDirectMethod(i)); |
| 262 | } |
| 263 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 264 | CompileMethod(klass->GetVirtualMethod(i)); |
| 265 | } |
| 266 | } |
| 267 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 268 | namespace arm { |
| 269 | void ArmCreateInvokeStub(Method* method); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 270 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 271 | namespace x86 { |
| 272 | void X86CreateInvokeStub(Method* method); |
| 273 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 274 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 275 | void Compiler::CompileMethod(Method* method) { |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 276 | if (method->IsNative()) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 277 | jni_compiler_.Compile(method); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 278 | // unregister will install the stub to lookup via dlsym |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 279 | // TODO: this is only necessary for tests |
| 280 | if (!method->IsRegistered()) { |
| 281 | method->UnregisterNative(); |
| 282 | } |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 283 | } else if (method->IsAbstract()) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 284 | ByteArray* abstract_method_error_stub = Runtime::Current()->GetAbstractMethodErrorStubArray(); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 285 | if (instruction_set_ == kX86) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 286 | method->SetCodeArray(abstract_method_error_stub, kX86); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 287 | } else { |
| 288 | CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 289 | method->SetCodeArray(abstract_method_error_stub, kArm); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 290 | } |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 291 | } else { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 292 | oatCompileMethod(*this, method, kThumb2); |
Brian Carlstrom | 2cc022b | 2011-08-25 10:05:39 -0700 | [diff] [blame] | 293 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 294 | CHECK(method->GetCode() != NULL) << PrettyMethod(method); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 295 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 296 | if (instruction_set_ == kX86) { |
| 297 | art::x86::X86CreateInvokeStub(method); |
| 298 | } else { |
| 299 | CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2); |
| 300 | // Generates invocation stub using ARM instruction set |
| 301 | art::arm::ArmCreateInvokeStub(method); |
| 302 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 303 | CHECK(method->GetInvokeStub() != NULL); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 306 | void Compiler::SetCodeAndDirectMethods(const ClassLoader* class_loader) { |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 307 | const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 308 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 309 | const DexFile* dex_file = class_path[i]; |
| 310 | CHECK(dex_file != NULL); |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 311 | SetCodeAndDirectMethodsDexFile(*dex_file); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 315 | void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 316 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 317 | DexCache* dex_cache = class_linker->FindDexCache(dex_file); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 318 | CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods(); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 319 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 320 | Method* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 321 | if (method == NULL) { |
| 322 | code_and_direct_methods->SetResolvedDirectMethodTrampoline(i); |
| 323 | } else if (method->IsDirect()) { |
| 324 | code_and_direct_methods->SetResolvedDirectMethod(i, method); |
| 325 | } else { |
| 326 | // TODO: we currently leave the entry blank for resolved |
| 327 | // non-direct methods. we could put in an error stub. |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 332 | } // namespace art |