blob: 02367d3fa9e5aa8da9138763576a1f128f5eb16b [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070016
17#include "compiler.h"
18
Brian Carlstrom27ec9612011-09-19 20:20:38 -070019#include <sys/mman.h>
20
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070021#include "assembler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070022#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_loader.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070024#include "dex_cache.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080025#include "dex_verifier.h"
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070026#include "jni_compiler.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070027#include "jni_internal.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070028#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080029#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070030#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031#include "stl_util.h"
Elliott Hughes601a1232012-02-02 17:47:38 -080032#include "timing_logger.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070033
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070034namespace art {
35
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080036CompiledMethod* oatCompileMethod(const Compiler& compiler, const DexFile::CodeItem* code_item,
37 uint32_t access_flags, uint32_t method_idx,
38 const ClassLoader* class_loader,
39 const DexFile& dex_file, InstructionSet);
40
Shih-wei Liaoc486c112011-09-13 16:43:52 -070041namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070042 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070043 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070044 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080045 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070046}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070047namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070048 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070049 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070050 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080051 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070052}
53
Brian Carlstromae826982011-11-09 01:33:42 -080054Compiler::Compiler(InstructionSet instruction_set,
55 bool image,
56 const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -070057 : instruction_set_(instruction_set),
58 jni_compiler_(instruction_set),
59 image_(image),
Elliott Hughesbb551fa2012-01-25 16:35:29 -080060 dex_file_count_(0),
61 class_count_(0),
62 abstract_method_count_(0),
63 native_method_count_(0),
64 regular_method_count_(0),
65 instruction_count_(0),
66 start_ns_(NanoTime()),
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080067 image_classes_(image_classes) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070068 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080069 if (!image_) {
70 CHECK(image_classes_ == NULL);
71 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -070072}
73
Brian Carlstrom3320cf42011-10-04 14:58:28 -070074Compiler::~Compiler() {
Brian Carlstrom0755ec52012-01-11 15:19:46 -080075 STLDeleteValues(&compiled_classes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070076 STLDeleteValues(&compiled_methods_);
77 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -080078 if (dex_file_count_ > 0) {
79 uint64_t duration_ns = NanoTime() - start_ns_;
Elliott Hughesaa56b722012-01-26 13:25:35 -080080 std::string stats(StringPrintf("Compiled files:%zd"
81 " classes:%zd"
82 " methods:(abstract:%zd"
83 " native:%zd"
84 " regular:%zd)"
Ian Rogers3bb17a62012-01-27 23:56:44 -080085 " instructions:%zd",
Brian Carlstromfc0842b2012-01-26 11:41:11 -080086 dex_file_count_,
87 class_count_,
88 abstract_method_count_,
89 native_method_count_,
90 regular_method_count_,
Ian Rogers3bb17a62012-01-27 23:56:44 -080091 instruction_count_));
92 stats += " (took ",
93 stats += PrettyDuration(duration_ns);
Brian Carlstromfc0842b2012-01-26 11:41:11 -080094 if (instruction_count_ != 0) {
Ian Rogers3bb17a62012-01-27 23:56:44 -080095 stats += ", ";
96 stats += PrettyDuration(duration_ns / instruction_count_);
97 stats += "/instruction";
Brian Carlstromfc0842b2012-01-26 11:41:11 -080098 }
99 stats += ")";
100 LOG(INFO) << stats;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800101 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700102}
103
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700104ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
105 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700106 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700107 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700108 } else {
109 CHECK(instruction_set == kArm || instruction_set == kThumb2);
110 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700111 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700112 }
113}
114
Elliott Hughes8add92d2012-01-18 18:18:43 -0800115ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800116 switch (instruction_set) {
117 case kArm:
118 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800119 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800120 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800121 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800122 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800123 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -0800124 return NULL;
125 }
126}
127
Ian Rogersad25ac52011-10-04 19:13:33 -0700128ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
129 if (instruction_set == kX86) {
130 return x86::CreateAbstractMethodErrorStub();
131 } else {
132 CHECK(instruction_set == kArm || instruction_set == kThumb2);
133 // Generates resolution stub using ARM instruction set
134 return arm::CreateAbstractMethodErrorStub();
135 }
136}
137
Jesse Wilson254db0f2011-11-16 16:44:11 -0500138void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800139 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700140 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800141
Elliott Hughes601a1232012-02-02 17:47:38 -0800142 TimingLogger timings("compiler");
143
144 PreCompile(class_loader, dex_files, timings);
145
Brian Carlstromae826982011-11-09 01:33:42 -0800146 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800147 timings.AddSplit("Compile");
148
Brian Carlstromae826982011-11-09 01:33:42 -0800149 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800150 timings.AddSplit("PostCompile");
151
152 if (timings.GetTotalNs() > MsToNs(1000)) {
153 timings.Dump();
154 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700155}
156
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700157void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700158 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800159
Brian Carlstrom8a487412011-08-29 20:08:52 -0700160 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800161
Ian Rogers0571d352011-11-03 19:51:38 -0700162 // Find the dex_file
163 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
164 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800165 std::vector<const DexFile*> dex_files;
166 dex_files.push_back(&dex_file);
167
Elliott Hughes601a1232012-02-02 17:47:38 -0800168 TimingLogger timings("CompileOne");
169 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800170
Ian Rogers0571d352011-11-03 19:51:38 -0700171 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800172 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
173 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800174
175 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700176}
177
Brian Carlstromae826982011-11-09 01:33:42 -0800178void Compiler::Resolve(const ClassLoader* class_loader,
179 const std::vector<const DexFile*>& dex_files) {
180 for (size_t i = 0; i != dex_files.size(); ++i) {
181 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700182 CHECK(dex_file != NULL);
183 ResolveDexFile(class_loader, *dex_file);
184 }
185}
186
Brian Carlstromae826982011-11-09 01:33:42 -0800187void Compiler::PreCompile(const ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800188 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800189 Resolve(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800190 timings.AddSplit("PreCompile.Resolve");
191
Brian Carlstromae826982011-11-09 01:33:42 -0800192 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800193 timings.AddSplit("PreCompile.Verify");
194
Brian Carlstromae826982011-11-09 01:33:42 -0800195 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800196 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800197}
198
199void Compiler::PostCompile(const ClassLoader* class_loader,
200 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800201 SetGcMaps(class_loader, dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800202 SetCodeAndDirectMethods(dex_files);
203}
204
205bool Compiler::IsImageClass(const std::string& descriptor) const {
206 if (image_classes_ == NULL) {
207 return true;
208 }
209 return image_classes_->find(descriptor) != image_classes_->end();
210}
211
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800212bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
213 uint32_t type_idx) const {
214 if (!IsImage()) {
215 return false;
216 }
217 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
218 if (resolved_class == NULL) {
219 return false;
220 }
221 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
222}
223
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800224// Return true if the class should be skipped during compilation. We
225// never skip classes in the boot class loader. However, if we have a
226// non-boot class loader and we can resolve the class in the boot
227// class loader, we do skip the class. This happens if an app bundles
228// classes found in the boot classpath. Since at runtime we will
229// select the class from the boot classpath, do not attempt to resolve
230// or compile it now.
231static bool SkipClass(const ClassLoader* class_loader,
232 const DexFile& dex_file,
233 const DexFile::ClassDef& class_def) {
234 if (class_loader == NULL) {
235 return false;
236 }
237 const char* descriptor = dex_file.GetClassDescriptor(class_def);
238 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
239 Class* klass = class_linker->FindClass(descriptor, NULL);
240 if (klass == NULL) {
241 Thread* self = Thread::Current();
242 CHECK(self->IsExceptionPending());
243 self->ClearException();
244 return false;
245 }
246 return true;
247}
248
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700249void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800250 Thread* self = Thread::Current();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700251 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700252 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700253
Brian Carlstromae826982011-11-09 01:33:42 -0800254 // Strings are easy in that they always are simply resolved to literals in the same file
255 if (image_ && image_classes_ == NULL) {
256 // TODO: Add support for loading strings referenced by image_classes_
257 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700258 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
259 class_linker->ResolveString(dex_file, string_idx, dex_cache);
260 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700261 }
262
Brian Carlstrom845490b2011-09-19 15:56:53 -0700263 // Class derived values are more complicated, they require the linker and loader.
Brian Carlstromffca45d2011-09-16 12:10:49 -0700264 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
265 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700266 if (klass == NULL) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800267 CHECK(self->IsExceptionPending());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700268 Thread::Current()->ClearException();
269 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700270 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700271
272 // Method and Field are the worst. We can't resolve without either
273 // context from the code use (to disambiguate virtual vs direct
274 // method and instance vs static field) or from class
275 // definitions. While the compiler will resolve what it can as it
276 // needs it, here we try to resolve fields and methods used in class
277 // definitions, since many of them many never be referenced by
278 // generated code.
279 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
280 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800281 if (SkipClass(class_loader, dex_file, class_def)) {
282 continue;
283 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700284
285 // Note the class_data pointer advances through the headers,
286 // static fields, instance fields, direct methods, and virtual
287 // methods.
288 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700289 if (class_data == NULL) {
290 // empty class such as a marker interface
291 continue;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700292 }
Ian Rogers0571d352011-11-03 19:51:38 -0700293 ClassDataItemIterator it(dex_file, class_data);
294 while (it.HasNextStaticField()) {
295 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
296 class_loader, true);
297 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700298 CHECK(self->IsExceptionPending());
299 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700300 }
Ian Rogers0571d352011-11-03 19:51:38 -0700301 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700302 }
Ian Rogers0571d352011-11-03 19:51:38 -0700303 while (it.HasNextInstanceField()) {
304 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
305 class_loader, false);
306 if (field == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700307 CHECK(self->IsExceptionPending());
308 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700309 }
Ian Rogers0571d352011-11-03 19:51:38 -0700310 it.Next();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700311 }
Ian Rogers0571d352011-11-03 19:51:38 -0700312 while (it.HasNextDirectMethod()) {
313 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
314 class_loader, true);
315 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700316 CHECK(self->IsExceptionPending());
317 self->ClearException();
Brian Carlstrom845490b2011-09-19 15:56:53 -0700318 }
Ian Rogers0571d352011-11-03 19:51:38 -0700319 it.Next();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700320 }
Ian Rogers0571d352011-11-03 19:51:38 -0700321 while (it.HasNextVirtualMethod()) {
322 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
323 class_loader, false);
324 if (method == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700325 CHECK(self->IsExceptionPending());
326 self->ClearException();
327 }
328 it.Next();
329 }
330 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700331 }
332}
333
Brian Carlstromae826982011-11-09 01:33:42 -0800334void Compiler::Verify(const ClassLoader* class_loader,
335 const std::vector<const DexFile*>& dex_files) {
336 for (size_t i = 0; i != dex_files.size(); ++i) {
337 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700338 CHECK(dex_file != NULL);
339 VerifyDexFile(class_loader, *dex_file);
340 }
341}
342
343void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700344 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
jeffhao98eacac2011-09-14 16:11:53 -0700345 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700346 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
347 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
jeffhao98eacac2011-09-14 16:11:53 -0700348 const char* descriptor = dex_file.GetClassDescriptor(class_def);
349 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700350 if (klass == NULL) {
351 Thread* self = Thread::Current();
352 CHECK(self->IsExceptionPending());
353 self->ClearException();
354 continue;
355 }
356 CHECK(klass->IsResolved()) << PrettyClass(klass);
jeffhao98eacac2011-09-14 16:11:53 -0700357 class_linker->VerifyClass(klass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700358
359 if (klass->IsErroneous()) {
360 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
361 CHECK(Thread::Current()->IsExceptionPending());
362 Thread::Current()->ClearException();
363 // We want to try verification again at run-time, so move back into the resolved state.
364 klass->SetStatus(Class::kStatusResolved);
365 }
366
jeffhao5cfd6fb2011-09-27 13:54:29 -0700367 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
368 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700369 }
jeffhaob4df5142011-09-19 20:25:32 -0700370 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700371}
372
Brian Carlstromae826982011-11-09 01:33:42 -0800373void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
374 const std::vector<const DexFile*>& dex_files) {
375 for (size_t i = 0; i != dex_files.size(); ++i) {
376 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700377 CHECK(dex_file != NULL);
378 InitializeClassesWithoutClinit(class_loader, *dex_file);
379 }
380}
381
382void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
383 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700384 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
385 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700386 const char* descriptor = dex_file.GetClassDescriptor(class_def);
387 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700388 if (klass != NULL) {
389 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800390 // record the final class status if necessary
391 Class::Status status = klass->GetStatus();
392 ClassReference ref(&dex_file, class_def_index);
393 CompiledClass* compiled_class = GetCompiledClass(ref);
394 if (compiled_class == NULL) {
395 compiled_class = new CompiledClass(status);
396 compiled_classes_[ref] = compiled_class;
397 } else {
398 DCHECK_EQ(status, compiled_class->GetStatus());
399 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700400 }
401 // clear any class not found or verification exceptions
402 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700403 }
404
405 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
406 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
407 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700408 if (klass == NULL) {
409 Thread::Current()->ClearException();
410 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700411 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
412 }
jeffhao98eacac2011-09-14 16:11:53 -0700413 }
414}
415
Brian Carlstromae826982011-11-09 01:33:42 -0800416void Compiler::Compile(const ClassLoader* class_loader,
417 const std::vector<const DexFile*>& dex_files) {
418 for (size_t i = 0; i != dex_files.size(); ++i) {
419 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700420 CHECK(dex_file != NULL);
421 CompileDexFile(class_loader, *dex_file);
422 }
423}
424
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700425void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800426 ++dex_file_count_;
Brian Carlstromffca45d2011-09-16 12:10:49 -0700427 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
428 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700429 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700430 }
431}
432
Ian Rogers0571d352011-11-03 19:51:38 -0700433void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
434 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800435 if (SkipClass(class_loader, dex_file, class_def)) {
436 return;
437 }
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800438 ++class_count_;
Ian Rogers0571d352011-11-03 19:51:38 -0700439 const byte* class_data = dex_file.GetClassData(class_def);
440 if (class_data == NULL) {
441 // empty class, probably a marker interface
442 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700443 }
Ian Rogers0571d352011-11-03 19:51:38 -0700444 ClassDataItemIterator it(dex_file, class_data);
445 // Skip fields
446 while (it.HasNextStaticField()) {
447 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700448 }
Ian Rogers0571d352011-11-03 19:51:38 -0700449 while (it.HasNextInstanceField()) {
450 it.Next();
451 }
452 // Compile direct methods
453 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800454 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
455 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700456 it.Next();
457 }
458 // Compile virtual methods
459 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800460 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
461 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700462 it.Next();
463 }
464 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700465}
466
Ian Rogersa3760aa2011-11-14 14:32:37 -0800467void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
468 uint32_t method_idx, const ClassLoader* class_loader,
469 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700470 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800471 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800472 if ((access_flags & kAccNative) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800473 ++native_method_count_;
Ian Rogers169c9a72011-11-13 20:13:17 -0800474 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800476 } else if ((access_flags & kAccAbstract) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800477 ++abstract_method_count_;
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700478 } else {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800479 ++regular_method_count_;
480 instruction_count_ += code_item->insns_size_in_code_units_;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800481 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
482 dex_file, kThumb2);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800483 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
484 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800485 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800486 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800487 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800488 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700489 }
490
491 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700492 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800493 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700494 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800495 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700496 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700497
Ian Rogers0571d352011-11-03 19:51:38 -0700498 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800499 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700500 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
501 if (compiled_invoke_stub == NULL) {
502 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800503 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700504 } else {
505 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
506 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800507 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700508 }
509 CHECK(compiled_invoke_stub != NULL);
510 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700511 }
Ian Rogers0571d352011-11-03 19:51:38 -0700512 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700513}
514
Ian Rogers0571d352011-11-03 19:51:38 -0700515static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
516 std::string key(shorty);
517 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800518 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700519 }
Ian Rogers0571d352011-11-03 19:51:38 -0700520 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700521}
522
Ian Rogers0571d352011-11-03 19:51:38 -0700523const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughes95572412011-12-13 18:14:20 -0800524 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700525 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700526 if (it == compiled_invoke_stubs_.end()) {
527 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700528 } else {
529 DCHECK(it->second != NULL);
530 return it->second;
531 }
532}
533
534void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
535 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughes95572412011-12-13 18:14:20 -0800536 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700537 compiled_invoke_stubs_[key] = compiled_invoke_stub;
538}
539
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800540CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
541 ClassTable::const_iterator it = compiled_classes_.find(ref);
542 if (it == compiled_classes_.end()) {
543 return NULL;
544 }
545 CHECK(it->second != NULL);
546 return it->second;
547}
548
Ian Rogers0571d352011-11-03 19:51:38 -0700549CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
550 MethodTable::const_iterator it = compiled_methods_.find(ref);
551 if (it == compiled_methods_.end()) {
552 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700553 }
554 CHECK(it->second != NULL);
555 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700556}
557
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800558void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
559 for (size_t i = 0; i != dex_files.size(); ++i) {
560 const DexFile* dex_file = dex_files[i];
561 CHECK(dex_file != NULL);
562 SetGcMapsDexFile(class_loader, *dex_file);
563 }
564}
565
566void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
567 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
568 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
569 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
570 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
571 const char* descriptor = dex_file.GetClassDescriptor(class_def);
572 Class* klass = class_linker->FindClass(descriptor, class_loader);
573 if (klass == NULL || !klass->IsVerified()) {
574 Thread::Current()->ClearException();
575 continue;
576 }
577 const byte* class_data = dex_file.GetClassData(class_def);
578 if (class_data == NULL) {
579 // empty class such as a marker interface
580 continue;
581 }
582 ClassDataItemIterator it(dex_file, class_data);
583 while (it.HasNextStaticField()) {
584 it.Next();
585 }
586 while (it.HasNextInstanceField()) {
587 it.Next();
588 }
589 while (it.HasNextDirectMethod()) {
590 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
591 class_loader, true);
592 SetGcMapsMethod(dex_file, method);
593 it.Next();
594 }
595 while (it.HasNextVirtualMethod()) {
596 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
597 class_loader, false);
598 SetGcMapsMethod(dex_file, method);
599 it.Next();
600 }
601 }
602}
603
604void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
605 if (method == NULL) {
606 Thread::Current()->ClearException();
607 return;
608 }
609 uint16_t method_idx = method->GetDexMethodIndex();
610 MethodReference ref(&dex_file, method_idx);
611 CompiledMethod* compiled_method = GetCompiledMethod(ref);
612 if (compiled_method == NULL) {
613 return;
614 }
615 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
616 if (gc_map == NULL) {
617 return;
618 }
619 compiled_method->SetGcMap(*gc_map);
620}
621
Brian Carlstromae826982011-11-09 01:33:42 -0800622void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
623 for (size_t i = 0; i != dex_files.size(); ++i) {
624 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700625 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700626 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700627 }
628}
629
Brian Carlstrom8a487412011-08-29 20:08:52 -0700630void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700631 Runtime* runtime = Runtime::Current();
632 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700633 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700634 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700635 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700636 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700637 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700638 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
639 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700640 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700641 } else {
642 // TODO: we currently leave the entry blank for resolved
643 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700644 }
645 }
646}
647
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700648} // namespace art