blob: 41d2f6a88d1503f6f7ab14a689a7c684f2be40c8 [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() {
309 STLDeleteElements(&threads_);
310 }
311
312 private:
313 std::vector<WorkerThread*> threads_;
314};
315
316static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
317 const DexFile& dex_file = *context->dex_file;
318
319 // Method and Field are the worst. We can't resolve without either
320 // context from the code use (to disambiguate virtual vs direct
321 // method and instance vs static field) or from class
322 // definitions. While the compiler will resolve what it can as it
323 // needs it, here we try to resolve fields and methods used in class
324 // definitions, since many of them many never be referenced by
325 // generated code.
326 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
327 if (SkipClass(context->class_loader, dex_file, class_def)) {
328 return;
329 }
330
331 // Note the class_data pointer advances through the headers,
332 // static fields, instance fields, direct methods, and virtual
333 // methods.
334 const byte* class_data = dex_file.GetClassData(class_def);
335 if (class_data == NULL) {
336 // empty class such as a marker interface
337 return;
338 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800339 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800340 ClassLinker* class_linker = context->class_linker;
341 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
342 ClassDataItemIterator it(dex_file, class_data);
343 while (it.HasNextStaticField()) {
344 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
345 context->class_loader, true);
346 if (field == NULL) {
347 CHECK(self->IsExceptionPending());
348 self->ClearException();
349 }
350 it.Next();
351 }
352 while (it.HasNextInstanceField()) {
353 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
354 context->class_loader, false);
355 if (field == NULL) {
356 CHECK(self->IsExceptionPending());
357 self->ClearException();
358 }
359 it.Next();
360 }
361 while (it.HasNextDirectMethod()) {
362 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
363 context->class_loader, true);
364 if (method == NULL) {
365 CHECK(self->IsExceptionPending());
366 self->ClearException();
367 }
368 it.Next();
369 }
370 while (it.HasNextVirtualMethod()) {
371 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
372 context->class_loader, false);
373 if (method == NULL) {
374 CHECK(self->IsExceptionPending());
375 self->ClearException();
376 }
377 it.Next();
378 }
379 DCHECK(!it.HasNext());
380}
381
382static void ResolveType(Context* context, size_t type_idx) {
383 // Class derived values are more complicated, they require the linker and loader.
384 Thread* self = Thread::Current();
385 ClassLinker* class_linker = context->class_linker;
386 const DexFile& dex_file = *context->dex_file;
387 Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader);
388 if (klass == NULL) {
389 CHECK(self->IsExceptionPending());
390 Thread::Current()->ClearException();
391 }
392}
393
394void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700395 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700396 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700397
Brian Carlstromae826982011-11-09 01:33:42 -0800398 // Strings are easy in that they always are simply resolved to literals in the same file
399 if (image_ && image_classes_ == NULL) {
400 // TODO: Add support for loading strings referenced by image_classes_
401 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700402 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
403 class_linker->ResolveString(dex_file, string_idx, dex_cache);
404 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700405 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800406 timings.AddSplit("Resolve.Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700407
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800408 Context context;
409 context.class_linker = class_linker;
410 context.class_loader = class_loader;
411 context.dex_cache = dex_cache;
412 context.dex_file = &dex_file;
413
414 {
415 Workers workers(&context, 0, dex_cache->NumResolvedTypes(), ResolveType);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700416 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800417 timings.AddSplit("Resolve.Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700418
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800419 {
420 Workers workers(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700421 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800422 timings.AddSplit("Resolve.MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700423}
424
Brian Carlstromae826982011-11-09 01:33:42 -0800425void Compiler::Verify(const ClassLoader* class_loader,
426 const std::vector<const DexFile*>& dex_files) {
427 for (size_t i = 0; i != dex_files.size(); ++i) {
428 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700429 CHECK(dex_file != NULL);
430 VerifyDexFile(class_loader, *dex_file);
431 }
432}
433
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800434static void VerifyClass(Context* context, size_t class_def_index) {
435 const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index);
436 const char* descriptor = context->dex_file->GetClassDescriptor(class_def);
437 Class* klass = context->class_linker->FindClass(descriptor, context->class_loader);
438 if (klass == NULL) {
439 Thread* self = Thread::Current();
440 CHECK(self->IsExceptionPending());
441 self->ClearException();
442 return;
443 }
444 CHECK(klass->IsResolved()) << PrettyClass(klass);
445 context->class_linker->VerifyClass(klass);
446
447 if (klass->IsErroneous()) {
448 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
449 CHECK(Thread::Current()->IsExceptionPending());
450 Thread::Current()->ClearException();
451 // We want to try verification again at run-time, so move back into the resolved state.
452 klass->SetStatus(Class::kStatusResolved);
453 }
454
455 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
456 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
457}
458
jeffhao98eacac2011-09-14 16:11:53 -0700459void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700460 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700461
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800462 Context context;
463 context.class_linker = Runtime::Current()->GetClassLinker();
464 context.class_loader = class_loader;
465 context.dex_file = &dex_file;
466 Workers workers(&context, 0, dex_file.NumClassDefs(), VerifyClass);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700467
jeffhaob4df5142011-09-19 20:25:32 -0700468 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700469}
470
Brian Carlstromae826982011-11-09 01:33:42 -0800471void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
472 const std::vector<const DexFile*>& dex_files) {
473 for (size_t i = 0; i != dex_files.size(); ++i) {
474 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700475 CHECK(dex_file != NULL);
476 InitializeClassesWithoutClinit(class_loader, *dex_file);
477 }
478}
479
480void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
481 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700482 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
483 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700484 const char* descriptor = dex_file.GetClassDescriptor(class_def);
485 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700486 if (klass != NULL) {
487 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800488 // record the final class status if necessary
489 Class::Status status = klass->GetStatus();
490 ClassReference ref(&dex_file, class_def_index);
491 CompiledClass* compiled_class = GetCompiledClass(ref);
492 if (compiled_class == NULL) {
493 compiled_class = new CompiledClass(status);
494 compiled_classes_[ref] = compiled_class;
495 } else {
496 DCHECK_EQ(status, compiled_class->GetStatus());
497 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700498 }
499 // clear any class not found or verification exceptions
500 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700501 }
502
503 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
504 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
505 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700506 if (klass == NULL) {
507 Thread::Current()->ClearException();
508 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700509 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
510 }
jeffhao98eacac2011-09-14 16:11:53 -0700511 }
512}
513
Brian Carlstromae826982011-11-09 01:33:42 -0800514void Compiler::Compile(const ClassLoader* class_loader,
515 const std::vector<const DexFile*>& dex_files) {
516 for (size_t i = 0; i != dex_files.size(); ++i) {
517 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700518 CHECK(dex_file != NULL);
519 CompileDexFile(class_loader, *dex_file);
520 }
521}
522
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700523void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800524 ++dex_file_count_;
Brian Carlstromffca45d2011-09-16 12:10:49 -0700525 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
526 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700527 CompileClass(class_def, class_loader, dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700528 }
529}
530
Ian Rogers0571d352011-11-03 19:51:38 -0700531void Compiler::CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
532 const DexFile& dex_file) {
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800533 if (SkipClass(class_loader, dex_file, class_def)) {
534 return;
535 }
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800536 ++class_count_;
Ian Rogers0571d352011-11-03 19:51:38 -0700537 const byte* class_data = dex_file.GetClassData(class_def);
538 if (class_data == NULL) {
539 // empty class, probably a marker interface
540 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700541 }
Ian Rogers0571d352011-11-03 19:51:38 -0700542 ClassDataItemIterator it(dex_file, class_data);
543 // Skip fields
544 while (it.HasNextStaticField()) {
545 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700546 }
Ian Rogers0571d352011-11-03 19:51:38 -0700547 while (it.HasNextInstanceField()) {
548 it.Next();
549 }
550 // Compile direct methods
551 while (it.HasNextDirectMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800552 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
553 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700554 it.Next();
555 }
556 // Compile virtual methods
557 while (it.HasNextVirtualMethod()) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800558 CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), it.GetMemberIndex(),
559 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700560 it.Next();
561 }
562 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700563}
564
Ian Rogersa3760aa2011-11-14 14:32:37 -0800565void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
566 uint32_t method_idx, const ClassLoader* class_loader,
567 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700568 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800569 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800570 if ((access_flags & kAccNative) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800571 ++native_method_count_;
Ian Rogers169c9a72011-11-13 20:13:17 -0800572 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700573 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800574 } else if ((access_flags & kAccAbstract) != 0) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800575 ++abstract_method_count_;
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700576 } else {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800577 ++regular_method_count_;
578 instruction_count_ += code_item->insns_size_in_code_units_;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800579 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
580 dex_file, kThumb2);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800581 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
582 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800583 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800584 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800585 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800586 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700587 }
588
589 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700590 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800591 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700592 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800593 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700594 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700595
Ian Rogers0571d352011-11-03 19:51:38 -0700596 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800597 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700598 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
599 if (compiled_invoke_stub == NULL) {
600 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800601 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700602 } else {
603 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
604 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800605 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700606 }
607 CHECK(compiled_invoke_stub != NULL);
608 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700609 }
Ian Rogers0571d352011-11-03 19:51:38 -0700610 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700611}
612
Ian Rogers0571d352011-11-03 19:51:38 -0700613static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
614 std::string key(shorty);
615 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800616 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700617 }
Ian Rogers0571d352011-11-03 19:51:38 -0700618 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700619}
620
Ian Rogers0571d352011-11-03 19:51:38 -0700621const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughes95572412011-12-13 18:14:20 -0800622 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700623 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700624 if (it == compiled_invoke_stubs_.end()) {
625 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700626 } else {
627 DCHECK(it->second != NULL);
628 return it->second;
629 }
630}
631
632void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
633 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughes95572412011-12-13 18:14:20 -0800634 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700635 compiled_invoke_stubs_[key] = compiled_invoke_stub;
636}
637
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800638CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
639 ClassTable::const_iterator it = compiled_classes_.find(ref);
640 if (it == compiled_classes_.end()) {
641 return NULL;
642 }
643 CHECK(it->second != NULL);
644 return it->second;
645}
646
Ian Rogers0571d352011-11-03 19:51:38 -0700647CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
648 MethodTable::const_iterator it = compiled_methods_.find(ref);
649 if (it == compiled_methods_.end()) {
650 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700651 }
652 CHECK(it->second != NULL);
653 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700654}
655
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800656void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
657 for (size_t i = 0; i != dex_files.size(); ++i) {
658 const DexFile* dex_file = dex_files[i];
659 CHECK(dex_file != NULL);
660 SetGcMapsDexFile(class_loader, *dex_file);
661 }
662}
663
664void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
665 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
666 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
667 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
668 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
669 const char* descriptor = dex_file.GetClassDescriptor(class_def);
670 Class* klass = class_linker->FindClass(descriptor, class_loader);
671 if (klass == NULL || !klass->IsVerified()) {
672 Thread::Current()->ClearException();
673 continue;
674 }
675 const byte* class_data = dex_file.GetClassData(class_def);
676 if (class_data == NULL) {
677 // empty class such as a marker interface
678 continue;
679 }
680 ClassDataItemIterator it(dex_file, class_data);
681 while (it.HasNextStaticField()) {
682 it.Next();
683 }
684 while (it.HasNextInstanceField()) {
685 it.Next();
686 }
687 while (it.HasNextDirectMethod()) {
688 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
689 class_loader, true);
690 SetGcMapsMethod(dex_file, method);
691 it.Next();
692 }
693 while (it.HasNextVirtualMethod()) {
694 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
695 class_loader, false);
696 SetGcMapsMethod(dex_file, method);
697 it.Next();
698 }
699 }
700}
701
702void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
703 if (method == NULL) {
704 Thread::Current()->ClearException();
705 return;
706 }
707 uint16_t method_idx = method->GetDexMethodIndex();
708 MethodReference ref(&dex_file, method_idx);
709 CompiledMethod* compiled_method = GetCompiledMethod(ref);
710 if (compiled_method == NULL) {
711 return;
712 }
713 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
714 if (gc_map == NULL) {
715 return;
716 }
717 compiled_method->SetGcMap(*gc_map);
718}
719
Brian Carlstromae826982011-11-09 01:33:42 -0800720void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
721 for (size_t i = 0; i != dex_files.size(); ++i) {
722 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700723 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700724 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700725 }
726}
727
Brian Carlstrom8a487412011-08-29 20:08:52 -0700728void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700729 Runtime* runtime = Runtime::Current();
730 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700731 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700732 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700733 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700734 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700735 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700736 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
737 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700738 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700739 } else {
740 // TODO: we currently leave the entry blank for resolved
741 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700742 }
743 }
744}
745
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700746} // namespace art