blob: 8a996b5006b7055177311e0d88ec5ef052c01da7 [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 Carlstrom27ec9612011-09-19 20:20:38 -07005#include <sys/mman.h>
6
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07007#include "assembler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07008#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -07009#include "class_loader.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070010#include "dex_cache.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080011#include "dex_verifier.h"
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070012#include "jni_compiler.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070013#include "jni_internal.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070014#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080015#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070016#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070017#include "stl_util.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070018
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070019namespace art {
20
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080021CompiledMethod* oatCompileMethod(const Compiler& compiler, const DexFile::CodeItem* code_item,
22 uint32_t access_flags, uint32_t method_idx,
23 const ClassLoader* class_loader,
24 const DexFile& dex_file, InstructionSet);
25
Shih-wei Liaoc486c112011-09-13 16:43:52 -070026namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070027 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070028 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070029 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080030 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070031}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070032namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070033 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070034 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070035 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080036 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070037}
38
Brian Carlstromae826982011-11-09 01:33:42 -080039Compiler::Compiler(InstructionSet instruction_set,
40 bool image,
41 const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -070042 : instruction_set_(instruction_set),
43 jni_compiler_(instruction_set),
44 image_(image),
Elliott Hughesbb551fa2012-01-25 16:35:29 -080045 dex_file_count_(0),
46 class_count_(0),
47 abstract_method_count_(0),
48 native_method_count_(0),
49 regular_method_count_(0),
50 instruction_count_(0),
51 start_ns_(NanoTime()),
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080052 image_classes_(image_classes) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070053 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080054 if (!image_) {
55 CHECK(image_classes_ == NULL);
56 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -070057}
58
Brian Carlstrom3320cf42011-10-04 14:58:28 -070059Compiler::~Compiler() {
Brian Carlstrom0755ec52012-01-11 15:19:46 -080060 STLDeleteValues(&compiled_classes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070061 STLDeleteValues(&compiled_methods_);
62 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -080063 if (dex_file_count_ > 0) {
64 uint64_t duration_ns = NanoTime() - start_ns_;
65 uint64_t duration_ms = NsToMs(duration_ns);
Brian Carlstromfc0842b2012-01-26 11:41:11 -080066 std::string stats(StringPrintf("Compiled files:%d"
67 " classes:%d"
68 " methods:(abstract:%d"
69 " native:%d"
70 " regular:%d)"
71 " instructions:%d"
72 " (took %llums",
73 dex_file_count_,
74 class_count_,
75 abstract_method_count_,
76 native_method_count_,
77 regular_method_count_,
78 instruction_count_,
79 duration_ms));
80 if (instruction_count_ != 0) {
81 stats += StringPrintf(", %llu ns/instruction", duration_ns/instruction_count_);
82 }
83 stats += ")";
84 LOG(INFO) << stats;
Elliott Hughesbb551fa2012-01-25 16:35:29 -080085 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -070086}
87
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070088ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
89 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -070090 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070091 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070092 } else {
93 CHECK(instruction_set == kArm || instruction_set == kThumb2);
94 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070095 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070096 }
97}
98
Elliott Hughes8add92d2012-01-18 18:18:43 -080099ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800100 switch (instruction_set) {
101 case kArm:
102 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800103 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800104 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800105 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800106 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800107 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -0800108 return NULL;
109 }
110}
111
Ian Rogersad25ac52011-10-04 19:13:33 -0700112ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
113 if (instruction_set == kX86) {
114 return x86::CreateAbstractMethodErrorStub();
115 } else {
116 CHECK(instruction_set == kArm || instruction_set == kThumb2);
117 // Generates resolution stub using ARM instruction set
118 return arm::CreateAbstractMethodErrorStub();
119 }
120}
121
Jesse Wilson254db0f2011-11-16 16:44:11 -0500122void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800123 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700124 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800125
126 PreCompile(class_loader, dex_files);
127 Compile(class_loader, dex_files);
128 PostCompile(class_loader, dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700129}
130
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700131void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700132 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800133
Brian Carlstrom8a487412011-08-29 20:08:52 -0700134 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800135
Ian Rogers0571d352011-11-03 19:51:38 -0700136 // Find the dex_file
137 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
138 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800139 std::vector<const DexFile*> dex_files;
140 dex_files.push_back(&dex_file);
141
142 PreCompile(class_loader, dex_files);
143
Ian Rogers0571d352011-11-03 19:51:38 -0700144 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800145 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
146 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800147
148 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700149}
150
Brian Carlstromae826982011-11-09 01:33:42 -0800151void Compiler::Resolve(const ClassLoader* class_loader,
152 const std::vector<const DexFile*>& dex_files) {
153 for (size_t i = 0; i != dex_files.size(); ++i) {
154 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700155 CHECK(dex_file != NULL);
156 ResolveDexFile(class_loader, *dex_file);
157 }
158}
159
Brian Carlstromae826982011-11-09 01:33:42 -0800160void Compiler::PreCompile(const ClassLoader* class_loader,
161 const std::vector<const DexFile*>& dex_files) {
162 Resolve(class_loader, dex_files);
163 Verify(class_loader, dex_files);
164 InitializeClassesWithoutClinit(class_loader, dex_files);
165}
166
167void Compiler::PostCompile(const ClassLoader* class_loader,
168 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800169 SetGcMaps(class_loader, dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800170 SetCodeAndDirectMethods(dex_files);
171}
172
173bool Compiler::IsImageClass(const std::string& descriptor) const {
174 if (image_classes_ == NULL) {
175 return true;
176 }
177 return image_classes_->find(descriptor) != image_classes_->end();
178}
179
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800180bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
181 uint32_t type_idx) const {
182 if (!IsImage()) {
183 return false;
184 }
185 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
186 if (resolved_class == NULL) {
187 return false;
188 }
189 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
190}
191
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800192// Return true if the class should be skipped during compilation. We
193// never skip classes in the boot class loader. However, if we have a
194// non-boot class loader and we can resolve the class in the boot
195// class loader, we do skip the class. This happens if an app bundles
196// classes found in the boot classpath. Since at runtime we will
197// select the class from the boot classpath, do not attempt to resolve
198// or compile it now.
199static bool SkipClass(const ClassLoader* class_loader,
200 const DexFile& dex_file,
201 const DexFile::ClassDef& class_def) {
202 if (class_loader == NULL) {
203 return false;
204 }
205 const char* descriptor = dex_file.GetClassDescriptor(class_def);
206 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
207 Class* klass = class_linker->FindClass(descriptor, NULL);
208 if (klass == NULL) {
209 Thread* self = Thread::Current();
210 CHECK(self->IsExceptionPending());
211 self->ClearException();
212 return false;
213 }
214 return true;
215}
216
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700217void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800218 Thread* self = Thread::Current();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700219 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700221
Brian Carlstromae826982011-11-09 01:33:42 -0800222 // Strings are easy in that they always are simply resolved to literals in the same file
223 if (image_ && image_classes_ == NULL) {
224 // TODO: Add support for loading strings referenced by image_classes_
225 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700226 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
227 class_linker->ResolveString(dex_file, string_idx, dex_cache);
228 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700229 }
230
Brian Carlstrom845490b2011-09-19 15:56:53 -0700231 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700232 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
233 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700234 if (klass == NULL) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800235 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700236 Thread::Current()->ClearException();
237 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700238 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700239
240 // Method and Field are the worst. We can't resolve without either
241 // context from the code use (to disambiguate virtual vs direct
242 // method and instance vs static field) or from class
243 // definitions. While the compiler will resolve what it can as it
244 // needs it, here we try to resolve fields and methods used in class
245 // definitions, since many of them many never be referenced by
246 // generated code.
247 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
248 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800249 if (SkipClass(class_loader, dex_file, class_def)) {
250 continue;
251 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700252
253 // Note the class_data pointer advances through the headers,
254 // static fields, instance fields, direct methods, and virtual
255 // methods.
256 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700257 if (class_data == NULL) {
258 // empty class such as a marker interface
259 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700260 }
Ian Rogers0571d352011-11-03 19:51:38 -0700261 ClassDataItemIterator it(dex_file, class_data);
262 while (it.HasNextStaticField()) {
263 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
264 class_loader, true);
265 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700266 CHECK(self->IsExceptionPending());
267 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700268 }
Ian Rogers0571d352011-11-03 19:51:38 -0700269 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700270 }
Ian Rogers0571d352011-11-03 19:51:38 -0700271 while (it.HasNextInstanceField()) {
272 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
273 class_loader, false);
274 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700275 CHECK(self->IsExceptionPending());
276 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700277 }
Ian Rogers0571d352011-11-03 19:51:38 -0700278 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700279 }
Ian Rogers0571d352011-11-03 19:51:38 -0700280 while (it.HasNextDirectMethod()) {
281 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
282 class_loader, true);
283 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700284 CHECK(self->IsExceptionPending());
285 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700286 }
Ian Rogers0571d352011-11-03 19:51:38 -0700287 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700288 }
Ian Rogers0571d352011-11-03 19:51:38 -0700289 while (it.HasNextVirtualMethod()) {
290 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
291 class_loader, false);
292 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700293 CHECK(self->IsExceptionPending());
294 self->ClearException();
295 }
296 it.Next();
297 }
298 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700299 }
300}
301
Brian Carlstromae826982011-11-09 01:33:42 -0800302void Compiler::Verify(const ClassLoader* class_loader,
303 const std::vector<const DexFile*>& dex_files) {
304 for (size_t i = 0; i != dex_files.size(); ++i) {
305 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700306 CHECK(dex_file != NULL);
307 VerifyDexFile(class_loader, *dex_file);
308 }
309}
310
311void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700312 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700313 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700314 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
315 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700316 const char* descriptor = dex_file.GetClassDescriptor(class_def);
317 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700318 if (klass == NULL) {
319 Thread* self = Thread::Current();
320 CHECK(self->IsExceptionPending());
321 self->ClearException();
322 continue;
323 }
324 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700325 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700326
327 if (klass->IsErroneous()) {
328 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
329 CHECK(Thread::Current()->IsExceptionPending());
330 Thread::Current()->ClearException();
331 // We want to try verification again at run-time, so move back into the resolved state.
332 klass->SetStatus(Class::kStatusResolved);
333 }
334
jeffhao5cfd6fb2011-09-27 13:54:29 -0700335 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
336 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700337 }
jeffhaob4df5142011-09-19 20:25:32 -0700338 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700339}
340
Brian Carlstromae826982011-11-09 01:33:42 -0800341void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
342 const std::vector<const DexFile*>& dex_files) {
343 for (size_t i = 0; i != dex_files.size(); ++i) {
344 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700345 CHECK(dex_file != NULL);
346 InitializeClassesWithoutClinit(class_loader, *dex_file);
347 }
348}
349
350void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
351 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700352 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
353 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700354 const char* descriptor = dex_file.GetClassDescriptor(class_def);
355 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700356 if (klass != NULL) {
357 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800358 // record the final class status if necessary
359 Class::Status status = klass->GetStatus();
360 ClassReference ref(&dex_file, class_def_index);
361 CompiledClass* compiled_class = GetCompiledClass(ref);
362 if (compiled_class == NULL) {
363 compiled_class = new CompiledClass(status);
364 compiled_classes_[ref] = compiled_class;
365 } else {
366 DCHECK_EQ(status, compiled_class->GetStatus());
367 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700368 }
369 // clear any class not found or verification exceptions
370 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700371 }
372
373 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
374 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
375 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700376 if (klass == NULL) {
377 Thread::Current()->ClearException();
378 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700379 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
380 }
jeffhao98eacac2011-09-14 16:11:53 -0700381 }
382}
383
Brian Carlstromae826982011-11-09 01:33:42 -0800384void Compiler::Compile(const ClassLoader* class_loader,
385 const std::vector<const DexFile*>& dex_files) {
386 for (size_t i = 0; i != dex_files.size(); ++i) {
387 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700388 CHECK(dex_file != NULL);
389 CompileDexFile(class_loader, *dex_file);
390 }
391}
392
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700393void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800394 ++dex_file_count_;
Brian Carlstromffca45d2011-09-16 12:10:49 -0700395 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
396 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700397 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700398 }
399}
400
Ian Rogers0571d352011-11-03 19:51:38 -0700401void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
402 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800403 if (SkipClass(class_loader, dex_file, class_def)) {
404 return;
405 }
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800406 ++class_count_;
Ian Rogers0571d352011-11-03 19:51:38 -0700407 const byte* class_data = dex_file.GetClassData(class_def);
408 if (class_data == NULL) {
409 // empty class, probably a marker interface
410 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700411 }
Ian Rogers0571d352011-11-03 19:51:38 -0700412 ClassDataItemIterator it(dex_file, class_data);
413 // Skip fields
414 while (it.HasNextStaticField()) {
415 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700416 }
Ian Rogers0571d352011-11-03 19:51:38 -0700417 while (it.HasNextInstanceField()) {
418 it.Next();
419 }
420 // Compile direct methods
421 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800422 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
423 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700424 it.Next();
425 }
426 // Compile virtual methods
427 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800428 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
429 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700430 it.Next();
431 }
432 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700433}
434
Ian Rogersa3760aa2011-11-14 14:32:37 -0800435void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
436 uint32_t method_idx, const ClassLoader* class_loader,
437 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700438 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800439 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800440 if ((access_flags & kAccNative) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800441 ++native_method_count_;
Ian Rogers169c9a72011-11-13 20:13:17 -0800442 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700443 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800444 } else if ((access_flags & kAccAbstract) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800445 ++abstract_method_count_;
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700446 } else {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800447 ++regular_method_count_;
448 instruction_count_ += code_item->insns_size_in_code_units_;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800449 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
450 dex_file, kThumb2);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800451 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
452 }
453 uint64_t duration_ms = NsToMs(NanoTime() - start_ns);
454 if (duration_ms > 10) {
455 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
456 << " took " << duration_ms << "ms";
Elliott Hughesf09afe82011-10-16 14:24:21 -0700457 }
458
459 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700460 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800461 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700462 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800463 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700464 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700465
Ian Rogers0571d352011-11-03 19:51:38 -0700466 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800467 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700468 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
469 if (compiled_invoke_stub == NULL) {
470 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800471 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700472 } else {
473 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
474 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800475 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700476 }
477 CHECK(compiled_invoke_stub != NULL);
478 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700479 }
Ian Rogers0571d352011-11-03 19:51:38 -0700480 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700481}
482
Ian Rogers0571d352011-11-03 19:51:38 -0700483static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
484 std::string key(shorty);
485 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800486 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700487 }
Ian Rogers0571d352011-11-03 19:51:38 -0700488 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700489}
490
Ian Rogers0571d352011-11-03 19:51:38 -0700491const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughes95572412011-12-13 18:14:20 -0800492 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700493 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700494 if (it == compiled_invoke_stubs_.end()) {
495 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700496 } else {
497 DCHECK(it->second != NULL);
498 return it->second;
499 }
500}
501
502void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
503 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughes95572412011-12-13 18:14:20 -0800504 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700505 compiled_invoke_stubs_[key] = compiled_invoke_stub;
506}
507
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800508CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
509 ClassTable::const_iterator it = compiled_classes_.find(ref);
510 if (it == compiled_classes_.end()) {
511 return NULL;
512 }
513 CHECK(it->second != NULL);
514 return it->second;
515}
516
Ian Rogers0571d352011-11-03 19:51:38 -0700517CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
518 MethodTable::const_iterator it = compiled_methods_.find(ref);
519 if (it == compiled_methods_.end()) {
520 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700521 }
522 CHECK(it->second != NULL);
523 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700524}
525
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800526void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
527 for (size_t i = 0; i != dex_files.size(); ++i) {
528 const DexFile* dex_file = dex_files[i];
529 CHECK(dex_file != NULL);
530 SetGcMapsDexFile(class_loader, *dex_file);
531 }
532}
533
534void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
535 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
536 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
537 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
538 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
539 const char* descriptor = dex_file.GetClassDescriptor(class_def);
540 Class* klass = class_linker->FindClass(descriptor, class_loader);
541 if (klass == NULL || !klass->IsVerified()) {
542 Thread::Current()->ClearException();
543 continue;
544 }
545 const byte* class_data = dex_file.GetClassData(class_def);
546 if (class_data == NULL) {
547 // empty class such as a marker interface
548 continue;
549 }
550 ClassDataItemIterator it(dex_file, class_data);
551 while (it.HasNextStaticField()) {
552 it.Next();
553 }
554 while (it.HasNextInstanceField()) {
555 it.Next();
556 }
557 while (it.HasNextDirectMethod()) {
558 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
559 class_loader, true);
560 SetGcMapsMethod(dex_file, method);
561 it.Next();
562 }
563 while (it.HasNextVirtualMethod()) {
564 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
565 class_loader, false);
566 SetGcMapsMethod(dex_file, method);
567 it.Next();
568 }
569 }
570}
571
572void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
573 if (method == NULL) {
574 Thread::Current()->ClearException();
575 return;
576 }
577 uint16_t method_idx = method->GetDexMethodIndex();
578 MethodReference ref(&dex_file, method_idx);
579 CompiledMethod* compiled_method = GetCompiledMethod(ref);
580 if (compiled_method == NULL) {
581 return;
582 }
583 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
584 if (gc_map == NULL) {
585 return;
586 }
587 compiled_method->SetGcMap(*gc_map);
588}
589
Brian Carlstromae826982011-11-09 01:33:42 -0800590void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
591 for (size_t i = 0; i != dex_files.size(); ++i) {
592 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700593 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700594 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700595 }
596}
597
Brian Carlstrom8a487412011-08-29 20:08:52 -0700598void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700599 Runtime* runtime = Runtime::Current();
600 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700601 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700602 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700603 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700604 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700605 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700606 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
607 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700608 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700609 } else {
610 // TODO: we currently leave the entry blank for resolved
611 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700612 }
613 }
614}
615
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700616} // namespace art