blob: 4cc228cbea84304a506d56e56aaa0e8841adc769 [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
Elliott Hughesd9c67be2012-02-02 19:54:06 -080019#include <vector>
20
Brian Carlstrom27ec9612011-09-19 20:20:38 -070021#include <sys/mman.h>
Elliott Hughesd9c67be2012-02-02 19:54:06 -080022#include <unistd.h>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070023
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070024#include "assembler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070027#include "dex_cache.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080028#include "dex_verifier.h"
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070029#include "jni_compiler.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070030#include "jni_internal.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070034#include "stl_util.h"
Elliott Hughes601a1232012-02-02 17:47:38 -080035#include "timing_logger.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070036
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070037namespace art {
38
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080039CompiledMethod* oatCompileMethod(const Compiler& compiler, const DexFile::CodeItem* code_item,
40 uint32_t access_flags, uint32_t method_idx,
41 const ClassLoader* class_loader,
42 const DexFile& dex_file, InstructionSet);
43
Shih-wei Liaoc486c112011-09-13 16:43:52 -070044namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070045 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070046 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070047 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080048 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070049}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070050namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070051 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers0571d352011-11-03 19:51:38 -070052 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070053 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080054 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070055}
56
Brian Carlstromae826982011-11-09 01:33:42 -080057Compiler::Compiler(InstructionSet instruction_set,
58 bool image,
59 const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -070060 : instruction_set_(instruction_set),
61 jni_compiler_(instruction_set),
62 image_(image),
Elliott Hughesbb551fa2012-01-25 16:35:29 -080063 dex_file_count_(0),
64 class_count_(0),
65 abstract_method_count_(0),
66 native_method_count_(0),
67 regular_method_count_(0),
68 instruction_count_(0),
69 start_ns_(NanoTime()),
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080070 image_classes_(image_classes) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070071 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080072 if (!image_) {
73 CHECK(image_classes_ == NULL);
74 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -070075}
76
Brian Carlstrom3320cf42011-10-04 14:58:28 -070077Compiler::~Compiler() {
Brian Carlstrom0755ec52012-01-11 15:19:46 -080078 STLDeleteValues(&compiled_classes_);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070079 STLDeleteValues(&compiled_methods_);
80 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -080081 if (dex_file_count_ > 0) {
82 uint64_t duration_ns = NanoTime() - start_ns_;
Elliott Hughesaa56b722012-01-26 13:25:35 -080083 std::string stats(StringPrintf("Compiled files:%zd"
84 " classes:%zd"
85 " methods:(abstract:%zd"
86 " native:%zd"
87 " regular:%zd)"
Ian Rogers3bb17a62012-01-27 23:56:44 -080088 " instructions:%zd",
Brian Carlstromfc0842b2012-01-26 11:41:11 -080089 dex_file_count_,
90 class_count_,
91 abstract_method_count_,
92 native_method_count_,
93 regular_method_count_,
Ian Rogers3bb17a62012-01-27 23:56:44 -080094 instruction_count_));
95 stats += " (took ",
96 stats += PrettyDuration(duration_ns);
Brian Carlstromfc0842b2012-01-26 11:41:11 -080097 if (instruction_count_ != 0) {
Ian Rogers3bb17a62012-01-27 23:56:44 -080098 stats += ", ";
99 stats += PrettyDuration(duration_ns / instruction_count_);
100 stats += "/instruction";
Brian Carlstromfc0842b2012-01-26 11:41:11 -0800101 }
102 stats += ")";
103 LOG(INFO) << stats;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800104 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700105}
106
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700107ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
108 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700109 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700110 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700111 } else {
112 CHECK(instruction_set == kArm || instruction_set == kThumb2);
113 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700114 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700115 }
116}
117
Elliott Hughes8add92d2012-01-18 18:18:43 -0800118ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800119 switch (instruction_set) {
120 case kArm:
121 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800122 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800123 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800124 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800125 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800126 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -0800127 return NULL;
128 }
129}
130
Ian Rogersad25ac52011-10-04 19:13:33 -0700131ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
132 if (instruction_set == kX86) {
133 return x86::CreateAbstractMethodErrorStub();
134 } else {
135 CHECK(instruction_set == kArm || instruction_set == kThumb2);
136 // Generates resolution stub using ARM instruction set
137 return arm::CreateAbstractMethodErrorStub();
138 }
139}
140
Jesse Wilson254db0f2011-11-16 16:44:11 -0500141void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800142 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700143 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800144
Elliott Hughes601a1232012-02-02 17:47:38 -0800145 TimingLogger timings("compiler");
146
147 PreCompile(class_loader, dex_files, timings);
148
Brian Carlstromae826982011-11-09 01:33:42 -0800149 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800150 timings.AddSplit("Compile");
151
Brian Carlstromae826982011-11-09 01:33:42 -0800152 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800153 timings.AddSplit("PostCompile");
154
155 if (timings.GetTotalNs() > MsToNs(1000)) {
156 timings.Dump();
157 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700158}
159
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700160void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700161 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800162
Brian Carlstrom8a487412011-08-29 20:08:52 -0700163 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800164
Ian Rogers0571d352011-11-03 19:51:38 -0700165 // Find the dex_file
166 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
167 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800168 std::vector<const DexFile*> dex_files;
169 dex_files.push_back(&dex_file);
170
Elliott Hughes601a1232012-02-02 17:47:38 -0800171 TimingLogger timings("CompileOne");
172 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800173
Ian Rogers0571d352011-11-03 19:51:38 -0700174 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800175 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
176 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800177
178 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700179}
180
Brian Carlstromae826982011-11-09 01:33:42 -0800181void Compiler::Resolve(const ClassLoader* class_loader,
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800182 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800183 for (size_t i = 0; i != dex_files.size(); ++i) {
184 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700185 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800186 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700187 }
188}
189
Brian Carlstromae826982011-11-09 01:33:42 -0800190void Compiler::PreCompile(const ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800191 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800192 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800193
Brian Carlstromae826982011-11-09 01:33:42 -0800194 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800195 timings.AddSplit("PreCompile.Verify");
196
Brian Carlstromae826982011-11-09 01:33:42 -0800197 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800198 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800199}
200
201void Compiler::PostCompile(const ClassLoader* class_loader,
202 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800203 SetGcMaps(class_loader, dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800204 SetCodeAndDirectMethods(dex_files);
205}
206
207bool Compiler::IsImageClass(const std::string& descriptor) const {
208 if (image_classes_ == NULL) {
209 return true;
210 }
211 return image_classes_->find(descriptor) != image_classes_->end();
212}
213
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800214bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
215 uint32_t type_idx) const {
216 if (!IsImage()) {
217 return false;
218 }
219 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
220 if (resolved_class == NULL) {
221 return false;
222 }
223 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
224}
225
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800226// Return true if the class should be skipped during compilation. We
227// never skip classes in the boot class loader. However, if we have a
228// non-boot class loader and we can resolve the class in the boot
229// class loader, we do skip the class. This happens if an app bundles
230// classes found in the boot classpath. Since at runtime we will
231// select the class from the boot classpath, do not attempt to resolve
232// or compile it now.
233static bool SkipClass(const ClassLoader* class_loader,
234 const DexFile& dex_file,
235 const DexFile::ClassDef& class_def) {
236 if (class_loader == NULL) {
237 return false;
238 }
239 const char* descriptor = dex_file.GetClassDescriptor(class_def);
240 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
241 Class* klass = class_linker->FindClass(descriptor, NULL);
242 if (klass == NULL) {
243 Thread* self = Thread::Current();
244 CHECK(self->IsExceptionPending());
245 self->ClearException();
246 return false;
247 }
248 return true;
249}
250
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800251struct Context {
252 ClassLinker* class_linker;
253 const ClassLoader* class_loader;
254 DexCache* dex_cache;
255 const DexFile* dex_file;
256};
257
258typedef void Callback(Context* context, size_t index);
259
260class WorkerThread {
261 public:
262 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe)
263 : context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
264 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Init, this), "compiler worker thread");
265 }
266
267 ~WorkerThread() {
268 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
269 }
270
271 private:
272 static void* Init(void* arg) {
273 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
274 Runtime* runtime = Runtime::Current();
275 runtime->AttachCurrentThread("Compiler Worker", true);
276 Thread::Current()->SetState(Thread::kRunnable);
277 worker->Run();
278 Thread::Current()->SetState(Thread::kNative);
279 runtime->DetachCurrentThread();
280 return NULL;
281 }
282
283 void Run() {
284 for (size_t i = begin_; i < end_; i += stripe_) {
285 callback_(context_, i);
286 }
287 }
288
289 pthread_t pthread_;
290 Thread* thread_;
291
292 Context* context_;
293 size_t begin_;
294 size_t end_;
295 Callback* callback_;
296 size_t stripe_;
297};
298
299class Workers {
300 public:
301 Workers(Context* context, size_t begin, size_t end, Callback callback) {
302 const size_t thread_count = static_cast<size_t>(sysconf(_SC_NPROCESSORS_ONLN));
303 for (size_t i = 0; i < thread_count; ++i) {
304 threads_.push_back(new WorkerThread(context, begin + i, end, callback, thread_count));
305 }
306 }
307
308 ~Workers() {
Elliott Hughesff738062012-02-03 15:00:42 -0800309 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
310 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800311 STLDeleteElements(&threads_);
312 }
313
314 private:
315 std::vector<WorkerThread*> threads_;
316};
317
318static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
319 const DexFile& dex_file = *context->dex_file;
320
321 // Method and Field are the worst. We can't resolve without either
322 // context from the code use (to disambiguate virtual vs direct
323 // method and instance vs static field) or from class
324 // definitions. While the compiler will resolve what it can as it
325 // needs it, here we try to resolve fields and methods used in class
326 // definitions, since many of them many never be referenced by
327 // generated code.
328 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
329 if (SkipClass(context->class_loader, dex_file, class_def)) {
330 return;
331 }
332
333 // Note the class_data pointer advances through the headers,
334 // static fields, instance fields, direct methods, and virtual
335 // methods.
336 const byte* class_data = dex_file.GetClassData(class_def);
337 if (class_data == NULL) {
338 // empty class such as a marker interface
339 return;
340 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800341 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800342 ClassLinker* class_linker = context->class_linker;
343 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
344 ClassDataItemIterator it(dex_file, class_data);
345 while (it.HasNextStaticField()) {
346 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
347 context->class_loader, true);
348 if (field == NULL) {
349 CHECK(self->IsExceptionPending());
350 self->ClearException();
351 }
352 it.Next();
353 }
354 while (it.HasNextInstanceField()) {
355 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
356 context->class_loader, false);
357 if (field == NULL) {
358 CHECK(self->IsExceptionPending());
359 self->ClearException();
360 }
361 it.Next();
362 }
363 while (it.HasNextDirectMethod()) {
364 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
365 context->class_loader, true);
366 if (method == NULL) {
367 CHECK(self->IsExceptionPending());
368 self->ClearException();
369 }
370 it.Next();
371 }
372 while (it.HasNextVirtualMethod()) {
373 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
374 context->class_loader, false);
375 if (method == NULL) {
376 CHECK(self->IsExceptionPending());
377 self->ClearException();
378 }
379 it.Next();
380 }
381 DCHECK(!it.HasNext());
382}
383
384static void ResolveType(Context* context, size_t type_idx) {
385 // Class derived values are more complicated, they require the linker and loader.
386 Thread* self = Thread::Current();
387 ClassLinker* class_linker = context->class_linker;
388 const DexFile& dex_file = *context->dex_file;
389 Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader);
390 if (klass == NULL) {
391 CHECK(self->IsExceptionPending());
392 Thread::Current()->ClearException();
393 }
394}
395
396void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700397 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700398 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700399
Brian Carlstromae826982011-11-09 01:33:42 -0800400 // Strings are easy in that they always are simply resolved to literals in the same file
401 if (image_ && image_classes_ == NULL) {
402 // TODO: Add support for loading strings referenced by image_classes_
403 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700404 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
405 class_linker->ResolveString(dex_file, string_idx, dex_cache);
406 }
Elliott Hughesff738062012-02-03 15:00:42 -0800407 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700408 }
409
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800410 Context context;
411 context.class_linker = class_linker;
412 context.class_loader = class_loader;
413 context.dex_cache = dex_cache;
414 context.dex_file = &dex_file;
415
416 {
417 Workers workers(&context, 0, dex_cache->NumResolvedTypes(), ResolveType);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700418 }
Elliott Hughesff738062012-02-03 15:00:42 -0800419 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700420
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800421 {
422 Workers workers(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700423 }
Elliott Hughesff738062012-02-03 15:00:42 -0800424 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700425}
426
Brian Carlstromae826982011-11-09 01:33:42 -0800427void Compiler::Verify(const ClassLoader* class_loader,
428 const std::vector<const DexFile*>& dex_files) {
429 for (size_t i = 0; i != dex_files.size(); ++i) {
430 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700431 CHECK(dex_file != NULL);
432 VerifyDexFile(class_loader, *dex_file);
433 }
434}
435
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800436static void VerifyClass(Context* context, size_t class_def_index) {
437 const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index);
438 const char* descriptor = context->dex_file->GetClassDescriptor(class_def);
439 Class* klass = context->class_linker->FindClass(descriptor, context->class_loader);
440 if (klass == NULL) {
441 Thread* self = Thread::Current();
442 CHECK(self->IsExceptionPending());
443 self->ClearException();
444 return;
445 }
446 CHECK(klass->IsResolved()) << PrettyClass(klass);
447 context->class_linker->VerifyClass(klass);
448
449 if (klass->IsErroneous()) {
450 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
451 CHECK(Thread::Current()->IsExceptionPending());
452 Thread::Current()->ClearException();
453 // We want to try verification again at run-time, so move back into the resolved state.
454 klass->SetStatus(Class::kStatusResolved);
455 }
456
457 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
458 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
459}
460
jeffhao98eacac2011-09-14 16:11:53 -0700461void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700462 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700463
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800464 Context context;
465 context.class_linker = Runtime::Current()->GetClassLinker();
466 context.class_loader = class_loader;
467 context.dex_file = &dex_file;
468 Workers workers(&context, 0, dex_file.NumClassDefs(), VerifyClass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700469
jeffhaob4df5142011-09-19 20:25:32 -0700470 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700471}
472
Brian Carlstromae826982011-11-09 01:33:42 -0800473void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
474 const std::vector<const DexFile*>& dex_files) {
475 for (size_t i = 0; i != dex_files.size(); ++i) {
476 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700477 CHECK(dex_file != NULL);
478 InitializeClassesWithoutClinit(class_loader, *dex_file);
479 }
480}
481
482void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
483 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700484 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
485 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700486 const char* descriptor = dex_file.GetClassDescriptor(class_def);
487 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700488 if (klass != NULL) {
489 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800490 // record the final class status if necessary
491 Class::Status status = klass->GetStatus();
492 ClassReference ref(&dex_file, class_def_index);
493 CompiledClass* compiled_class = GetCompiledClass(ref);
494 if (compiled_class == NULL) {
495 compiled_class = new CompiledClass(status);
496 compiled_classes_[ref] = compiled_class;
497 } else {
498 DCHECK_EQ(status, compiled_class->GetStatus());
499 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700500 }
501 // clear any class not found or verification exceptions
502 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700503 }
504
505 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
506 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
507 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700508 if (klass == NULL) {
509 Thread::Current()->ClearException();
510 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700511 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
512 }
jeffhao98eacac2011-09-14 16:11:53 -0700513 }
514}
515
Brian Carlstromae826982011-11-09 01:33:42 -0800516void Compiler::Compile(const ClassLoader* class_loader,
517 const std::vector<const DexFile*>& dex_files) {
518 for (size_t i = 0; i != dex_files.size(); ++i) {
519 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700520 CHECK(dex_file != NULL);
521 CompileDexFile(class_loader, *dex_file);
522 }
523}
524
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700525void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800526 ++dex_file_count_;
Brian Carlstromffca45d2011-09-16 12:10:49 -0700527 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
528 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700529 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700530 }
531}
532
Ian Rogers0571d352011-11-03 19:51:38 -0700533void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
534 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800535 if (SkipClass(class_loader, dex_file, class_def)) {
536 return;
537 }
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800538 ++class_count_;
Ian Rogers0571d352011-11-03 19:51:38 -0700539 const byte* class_data = dex_file.GetClassData(class_def);
540 if (class_data == NULL) {
541 // empty class, probably a marker interface
542 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700543 }
Ian Rogers0571d352011-11-03 19:51:38 -0700544 ClassDataItemIterator it(dex_file, class_data);
545 // Skip fields
546 while (it.HasNextStaticField()) {
547 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700548 }
Ian Rogers0571d352011-11-03 19:51:38 -0700549 while (it.HasNextInstanceField()) {
550 it.Next();
551 }
552 // Compile direct methods
553 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800554 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
555 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700556 it.Next();
557 }
558 // Compile virtual methods
559 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800560 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
561 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700562 it.Next();
563 }
564 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700565}
566
Ian Rogersa3760aa2011-11-14 14:32:37 -0800567void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
568 uint32_t method_idx, const ClassLoader* class_loader,
569 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700570 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800571 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800572 if ((access_flags & kAccNative) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800573 ++native_method_count_;
Ian Rogers169c9a72011-11-13 20:13:17 -0800574 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700575 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800576 } else if ((access_flags & kAccAbstract) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800577 ++abstract_method_count_;
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700578 } else {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800579 ++regular_method_count_;
580 instruction_count_ += code_item->insns_size_in_code_units_;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800581 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
582 dex_file, kThumb2);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800583 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
584 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800585 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800586 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800587 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800588 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700589 }
590
591 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700592 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800593 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700594 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800595 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700596 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700597
Ian Rogers0571d352011-11-03 19:51:38 -0700598 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800599 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700600 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
601 if (compiled_invoke_stub == NULL) {
602 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800603 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700604 } else {
605 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
606 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800607 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700608 }
609 CHECK(compiled_invoke_stub != NULL);
610 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700611 }
Ian Rogers0571d352011-11-03 19:51:38 -0700612 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700613}
614
Ian Rogers0571d352011-11-03 19:51:38 -0700615static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
616 std::string key(shorty);
617 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800618 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700619 }
Ian Rogers0571d352011-11-03 19:51:38 -0700620 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700621}
622
Ian Rogers0571d352011-11-03 19:51:38 -0700623const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughes95572412011-12-13 18:14:20 -0800624 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700625 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700626 if (it == compiled_invoke_stubs_.end()) {
627 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700628 } else {
629 DCHECK(it->second != NULL);
630 return it->second;
631 }
632}
633
634void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
635 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughes95572412011-12-13 18:14:20 -0800636 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700637 compiled_invoke_stubs_[key] = compiled_invoke_stub;
638}
639
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800640CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
641 ClassTable::const_iterator it = compiled_classes_.find(ref);
642 if (it == compiled_classes_.end()) {
643 return NULL;
644 }
645 CHECK(it->second != NULL);
646 return it->second;
647}
648
Ian Rogers0571d352011-11-03 19:51:38 -0700649CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
650 MethodTable::const_iterator it = compiled_methods_.find(ref);
651 if (it == compiled_methods_.end()) {
652 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700653 }
654 CHECK(it->second != NULL);
655 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700656}
657
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800658void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
659 for (size_t i = 0; i != dex_files.size(); ++i) {
660 const DexFile* dex_file = dex_files[i];
661 CHECK(dex_file != NULL);
662 SetGcMapsDexFile(class_loader, *dex_file);
663 }
664}
665
666void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
667 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
668 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
669 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
670 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
671 const char* descriptor = dex_file.GetClassDescriptor(class_def);
672 Class* klass = class_linker->FindClass(descriptor, class_loader);
673 if (klass == NULL || !klass->IsVerified()) {
674 Thread::Current()->ClearException();
675 continue;
676 }
677 const byte* class_data = dex_file.GetClassData(class_def);
678 if (class_data == NULL) {
679 // empty class such as a marker interface
680 continue;
681 }
682 ClassDataItemIterator it(dex_file, class_data);
683 while (it.HasNextStaticField()) {
684 it.Next();
685 }
686 while (it.HasNextInstanceField()) {
687 it.Next();
688 }
689 while (it.HasNextDirectMethod()) {
690 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
691 class_loader, true);
692 SetGcMapsMethod(dex_file, method);
693 it.Next();
694 }
695 while (it.HasNextVirtualMethod()) {
696 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
697 class_loader, false);
698 SetGcMapsMethod(dex_file, method);
699 it.Next();
700 }
701 }
702}
703
704void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
705 if (method == NULL) {
706 Thread::Current()->ClearException();
707 return;
708 }
709 uint16_t method_idx = method->GetDexMethodIndex();
710 MethodReference ref(&dex_file, method_idx);
711 CompiledMethod* compiled_method = GetCompiledMethod(ref);
712 if (compiled_method == NULL) {
713 return;
714 }
715 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
716 if (gc_map == NULL) {
717 return;
718 }
719 compiled_method->SetGcMap(*gc_map);
720}
721
Brian Carlstromae826982011-11-09 01:33:42 -0800722void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
723 for (size_t i = 0; i != dex_files.size(); ++i) {
724 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700725 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700726 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700727 }
728}
729
Brian Carlstrom8a487412011-08-29 20:08:52 -0700730void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700731 Runtime* runtime = Runtime::Current();
732 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700733 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700734 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700735 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700736 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700737 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700738 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
739 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700740 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700741 } else {
742 // TODO: we currently leave the entry blank for resolved
743 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700744 }
745 }
746}
747
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700748} // namespace art