blob: 1f652ef38d53e9a807801ea7d1729bf87d60d297 [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
Elliott Hughes5523ee02012-02-03 18:18:34 -080057Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count,
Brian Carlstromae826982011-11-09 01:33:42 -080058 const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -070059 : instruction_set_(instruction_set),
60 jni_compiler_(instruction_set),
Elliott Hughesc225caa2012-02-03 15:43:37 -080061 compiled_classes_lock_("compiled classes lock"),
62 compiled_methods_lock_("compiled method lock"),
63 compiled_invoke_stubs_lock_("compiled invoke stubs lock"),
Brian Carlstromaded5f72011-10-07 17:15:04 -070064 image_(image),
Elliott Hughes5523ee02012-02-03 18:18:34 -080065 thread_count_(thread_count),
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080066 image_classes_(image_classes) {
Brian Carlstrom25c33252011-09-18 15:58:35 -070067 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -080068 if (!image_) {
69 CHECK(image_classes_ == NULL);
70 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -070071}
72
Brian Carlstrom3320cf42011-10-04 14:58:28 -070073Compiler::~Compiler() {
Elliott Hughesc225caa2012-02-03 15:43:37 -080074 {
75 MutexLock mu(compiled_classes_lock_);
76 STLDeleteValues(&compiled_classes_);
77 }
78 {
79 MutexLock mu(compiled_methods_lock_);
80 STLDeleteValues(&compiled_methods_);
81 }
82 {
83 MutexLock mu(compiled_invoke_stubs_lock_);
84 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -080085 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -070086}
87
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070088ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
89 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -070090 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070091 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070092 } else {
93 CHECK(instruction_set == kArm || instruction_set == kThumb2);
94 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070095 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -070096 }
97}
98
Elliott Hughes8add92d2012-01-18 18:18:43 -080099ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800100 switch (instruction_set) {
101 case kArm:
102 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800103 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800104 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800105 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800106 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800107 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -0800108 return NULL;
109 }
110}
111
Ian Rogersad25ac52011-10-04 19:13:33 -0700112ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
113 if (instruction_set == kX86) {
114 return x86::CreateAbstractMethodErrorStub();
115 } else {
116 CHECK(instruction_set == kArm || instruction_set == kThumb2);
117 // Generates resolution stub using ARM instruction set
118 return arm::CreateAbstractMethodErrorStub();
119 }
120}
121
Jesse Wilson254db0f2011-11-16 16:44:11 -0500122void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800123 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700124 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800125
Elliott Hughes601a1232012-02-02 17:47:38 -0800126 TimingLogger timings("compiler");
127
128 PreCompile(class_loader, dex_files, timings);
129
Brian Carlstromae826982011-11-09 01:33:42 -0800130 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800131 timings.AddSplit("Compile");
132
Brian Carlstromae826982011-11-09 01:33:42 -0800133 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800134 timings.AddSplit("PostCompile");
135
136 if (timings.GetTotalNs() > MsToNs(1000)) {
137 timings.Dump();
138 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700139}
140
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700142 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800143
Brian Carlstrom8a487412011-08-29 20:08:52 -0700144 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800145
Ian Rogers0571d352011-11-03 19:51:38 -0700146 // Find the dex_file
147 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
148 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800149 std::vector<const DexFile*> dex_files;
150 dex_files.push_back(&dex_file);
151
Elliott Hughes601a1232012-02-02 17:47:38 -0800152 TimingLogger timings("CompileOne");
153 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800154
Ian Rogers0571d352011-11-03 19:51:38 -0700155 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800156 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
157 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800158
159 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700160}
161
Brian Carlstromae826982011-11-09 01:33:42 -0800162void Compiler::Resolve(const ClassLoader* class_loader,
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800163 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800164 for (size_t i = 0; i != dex_files.size(); ++i) {
165 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700166 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800167 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700168 }
169}
170
Brian Carlstromae826982011-11-09 01:33:42 -0800171void Compiler::PreCompile(const ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800172 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800173 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800174
Brian Carlstromae826982011-11-09 01:33:42 -0800175 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800176 timings.AddSplit("PreCompile.Verify");
177
Brian Carlstromae826982011-11-09 01:33:42 -0800178 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800179 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800180}
181
182void Compiler::PostCompile(const ClassLoader* class_loader,
183 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800184 SetGcMaps(class_loader, dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800185 SetCodeAndDirectMethods(dex_files);
186}
187
188bool Compiler::IsImageClass(const std::string& descriptor) const {
189 if (image_classes_ == NULL) {
190 return true;
191 }
192 return image_classes_->find(descriptor) != image_classes_->end();
193}
194
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800195bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
196 uint32_t type_idx) const {
197 if (!IsImage()) {
198 return false;
199 }
200 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
201 if (resolved_class == NULL) {
202 return false;
203 }
204 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
205}
206
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800207// Return true if the class should be skipped during compilation. We
208// never skip classes in the boot class loader. However, if we have a
209// non-boot class loader and we can resolve the class in the boot
210// class loader, we do skip the class. This happens if an app bundles
211// classes found in the boot classpath. Since at runtime we will
212// select the class from the boot classpath, do not attempt to resolve
213// or compile it now.
214static bool SkipClass(const ClassLoader* class_loader,
215 const DexFile& dex_file,
216 const DexFile::ClassDef& class_def) {
217 if (class_loader == NULL) {
218 return false;
219 }
220 const char* descriptor = dex_file.GetClassDescriptor(class_def);
221 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
222 Class* klass = class_linker->FindClass(descriptor, NULL);
223 if (klass == NULL) {
224 Thread* self = Thread::Current();
225 CHECK(self->IsExceptionPending());
226 self->ClearException();
227 return false;
228 }
229 return true;
230}
231
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800232struct Context {
233 ClassLinker* class_linker;
234 const ClassLoader* class_loader;
Elliott Hughesc225caa2012-02-03 15:43:37 -0800235 Compiler* compiler;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800236 DexCache* dex_cache;
237 const DexFile* dex_file;
238};
239
240typedef void Callback(Context* context, size_t index);
241
242class WorkerThread {
243 public:
244 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe)
245 : context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
246 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Init, this), "compiler worker thread");
247 }
248
249 ~WorkerThread() {
250 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
251 }
252
253 private:
254 static void* Init(void* arg) {
255 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
256 Runtime* runtime = Runtime::Current();
257 runtime->AttachCurrentThread("Compiler Worker", true);
258 Thread::Current()->SetState(Thread::kRunnable);
259 worker->Run();
260 Thread::Current()->SetState(Thread::kNative);
261 runtime->DetachCurrentThread();
262 return NULL;
263 }
264
265 void Run() {
266 for (size_t i = begin_; i < end_; i += stripe_) {
267 callback_(context_, i);
268 }
269 }
270
271 pthread_t pthread_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800272
273 Context* context_;
274 size_t begin_;
275 size_t end_;
276 Callback* callback_;
277 size_t stripe_;
278};
279
Elliott Hughes5523ee02012-02-03 18:18:34 -0800280void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Elliott Hughes81d91512012-02-03 16:38:43 -0800281 std::vector<WorkerThread*> threads;
282
Elliott Hughes81d91512012-02-03 16:38:43 -0800283 for (size_t i = 0; i < thread_count; ++i) {
284 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count));
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800285 }
286
Elliott Hughes81d91512012-02-03 16:38:43 -0800287 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
288 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
289 STLDeleteElements(&threads);
290}
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800291
292static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
293 const DexFile& dex_file = *context->dex_file;
294
295 // Method and Field are the worst. We can't resolve without either
296 // context from the code use (to disambiguate virtual vs direct
297 // method and instance vs static field) or from class
298 // definitions. While the compiler will resolve what it can as it
299 // needs it, here we try to resolve fields and methods used in class
300 // definitions, since many of them many never be referenced by
301 // generated code.
302 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
303 if (SkipClass(context->class_loader, dex_file, class_def)) {
304 return;
305 }
306
307 // Note the class_data pointer advances through the headers,
308 // static fields, instance fields, direct methods, and virtual
309 // methods.
310 const byte* class_data = dex_file.GetClassData(class_def);
311 if (class_data == NULL) {
312 // empty class such as a marker interface
313 return;
314 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800315 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800316 ClassLinker* class_linker = context->class_linker;
317 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
318 ClassDataItemIterator it(dex_file, class_data);
319 while (it.HasNextStaticField()) {
320 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
321 context->class_loader, true);
322 if (field == NULL) {
323 CHECK(self->IsExceptionPending());
324 self->ClearException();
325 }
326 it.Next();
327 }
328 while (it.HasNextInstanceField()) {
329 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
330 context->class_loader, false);
331 if (field == NULL) {
332 CHECK(self->IsExceptionPending());
333 self->ClearException();
334 }
335 it.Next();
336 }
337 while (it.HasNextDirectMethod()) {
338 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
339 context->class_loader, true);
340 if (method == NULL) {
341 CHECK(self->IsExceptionPending());
342 self->ClearException();
343 }
344 it.Next();
345 }
346 while (it.HasNextVirtualMethod()) {
347 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
348 context->class_loader, false);
349 if (method == NULL) {
350 CHECK(self->IsExceptionPending());
351 self->ClearException();
352 }
353 it.Next();
354 }
355 DCHECK(!it.HasNext());
356}
357
358static void ResolveType(Context* context, size_t type_idx) {
359 // Class derived values are more complicated, they require the linker and loader.
360 Thread* self = Thread::Current();
361 ClassLinker* class_linker = context->class_linker;
362 const DexFile& dex_file = *context->dex_file;
363 Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader);
364 if (klass == NULL) {
365 CHECK(self->IsExceptionPending());
366 Thread::Current()->ClearException();
367 }
368}
369
370void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700371 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700372 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700373
Brian Carlstromae826982011-11-09 01:33:42 -0800374 // Strings are easy in that they always are simply resolved to literals in the same file
375 if (image_ && image_classes_ == NULL) {
376 // TODO: Add support for loading strings referenced by image_classes_
377 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700378 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
379 class_linker->ResolveString(dex_file, string_idx, dex_cache);
380 }
Elliott Hughesff738062012-02-03 15:00:42 -0800381 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700382 }
383
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800384 Context context;
385 context.class_linker = class_linker;
386 context.class_loader = class_loader;
387 context.dex_cache = dex_cache;
388 context.dex_file = &dex_file;
389
Elliott Hughes5523ee02012-02-03 18:18:34 -0800390 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800391 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700392
Elliott Hughes5523ee02012-02-03 18:18:34 -0800393 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800394 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700395}
396
Brian Carlstromae826982011-11-09 01:33:42 -0800397void Compiler::Verify(const ClassLoader* class_loader,
398 const std::vector<const DexFile*>& dex_files) {
399 for (size_t i = 0; i != dex_files.size(); ++i) {
400 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700401 CHECK(dex_file != NULL);
402 VerifyDexFile(class_loader, *dex_file);
403 }
404}
405
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800406static void VerifyClass(Context* context, size_t class_def_index) {
407 const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index);
408 const char* descriptor = context->dex_file->GetClassDescriptor(class_def);
409 Class* klass = context->class_linker->FindClass(descriptor, context->class_loader);
410 if (klass == NULL) {
411 Thread* self = Thread::Current();
412 CHECK(self->IsExceptionPending());
413 self->ClearException();
414 return;
415 }
416 CHECK(klass->IsResolved()) << PrettyClass(klass);
417 context->class_linker->VerifyClass(klass);
418
419 if (klass->IsErroneous()) {
420 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
421 CHECK(Thread::Current()->IsExceptionPending());
422 Thread::Current()->ClearException();
423 // We want to try verification again at run-time, so move back into the resolved state.
424 klass->SetStatus(Class::kStatusResolved);
425 }
426
427 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
428 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
429}
430
jeffhao98eacac2011-09-14 16:11:53 -0700431void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700432 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700433
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800434 Context context;
435 context.class_linker = Runtime::Current()->GetClassLinker();
436 context.class_loader = class_loader;
437 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800438 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700439
jeffhaob4df5142011-09-19 20:25:32 -0700440 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700441}
442
Brian Carlstromae826982011-11-09 01:33:42 -0800443void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
444 const std::vector<const DexFile*>& dex_files) {
445 for (size_t i = 0; i != dex_files.size(); ++i) {
446 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700447 CHECK(dex_file != NULL);
448 InitializeClassesWithoutClinit(class_loader, *dex_file);
449 }
450}
451
452void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
453 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700454 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
455 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700456 const char* descriptor = dex_file.GetClassDescriptor(class_def);
457 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700458 if (klass != NULL) {
459 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800460 // record the final class status if necessary
461 Class::Status status = klass->GetStatus();
462 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800463 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800464 CompiledClass* compiled_class = GetCompiledClass(ref);
465 if (compiled_class == NULL) {
466 compiled_class = new CompiledClass(status);
467 compiled_classes_[ref] = compiled_class;
468 } else {
469 DCHECK_EQ(status, compiled_class->GetStatus());
470 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700471 }
472 // clear any class not found or verification exceptions
473 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700474 }
475
476 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
477 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
478 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700479 if (klass == NULL) {
480 Thread::Current()->ClearException();
481 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700482 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
483 }
jeffhao98eacac2011-09-14 16:11:53 -0700484 }
485}
486
Brian Carlstromae826982011-11-09 01:33:42 -0800487void Compiler::Compile(const ClassLoader* class_loader,
488 const std::vector<const DexFile*>& dex_files) {
489 for (size_t i = 0; i != dex_files.size(); ++i) {
490 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700491 CHECK(dex_file != NULL);
492 CompileDexFile(class_loader, *dex_file);
493 }
494}
495
Elliott Hughesc225caa2012-02-03 15:43:37 -0800496void Compiler::CompileClass(Context* context, size_t class_def_index) {
497 const ClassLoader* class_loader = context->class_loader;
498 const DexFile& dex_file = *context->dex_file;
499 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800500 if (SkipClass(class_loader, dex_file, class_def)) {
501 return;
502 }
Ian Rogers0571d352011-11-03 19:51:38 -0700503 const byte* class_data = dex_file.GetClassData(class_def);
504 if (class_data == NULL) {
505 // empty class, probably a marker interface
506 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700507 }
Ian Rogers0571d352011-11-03 19:51:38 -0700508 ClassDataItemIterator it(dex_file, class_data);
509 // Skip fields
510 while (it.HasNextStaticField()) {
511 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700512 }
Ian Rogers0571d352011-11-03 19:51:38 -0700513 while (it.HasNextInstanceField()) {
514 it.Next();
515 }
516 // Compile direct methods
517 while (it.HasNextDirectMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800518 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
519 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700520 it.Next();
521 }
522 // Compile virtual methods
523 while (it.HasNextVirtualMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800524 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
525 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700526 it.Next();
527 }
528 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700529}
530
Elliott Hughesc225caa2012-02-03 15:43:37 -0800531void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
532 Context context;
533 context.class_loader = class_loader;
534 context.compiler = this;
535 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800536 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800537}
538
Ian Rogersa3760aa2011-11-14 14:32:37 -0800539void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
540 uint32_t method_idx, const ClassLoader* class_loader,
541 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700542 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800543 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800544 if ((access_flags & kAccNative) != 0) {
545 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700546 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800547 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700548 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800549 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
550 dex_file, kThumb2);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800551 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
552 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800553 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800554 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800555 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800556 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700557 }
558
559 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700560 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800561 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800562 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700563 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800564 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700565 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700566
Ian Rogers0571d352011-11-03 19:51:38 -0700567 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800568 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700569 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
570 if (compiled_invoke_stub == NULL) {
571 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800572 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700573 } else {
574 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
575 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800576 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700577 }
578 CHECK(compiled_invoke_stub != NULL);
579 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700580 }
Ian Rogers0571d352011-11-03 19:51:38 -0700581 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700582}
583
Ian Rogers0571d352011-11-03 19:51:38 -0700584static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
585 std::string key(shorty);
586 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800587 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700588 }
Ian Rogers0571d352011-11-03 19:51:38 -0700589 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700590}
591
Ian Rogers0571d352011-11-03 19:51:38 -0700592const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800593 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -0800594 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700595 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700596 if (it == compiled_invoke_stubs_.end()) {
597 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700598 } else {
599 DCHECK(it->second != NULL);
600 return it->second;
601 }
602}
603
604void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
605 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800606 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -0800607 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700608 compiled_invoke_stubs_[key] = compiled_invoke_stub;
609}
610
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800611CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800612 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800613 ClassTable::const_iterator it = compiled_classes_.find(ref);
614 if (it == compiled_classes_.end()) {
615 return NULL;
616 }
617 CHECK(it->second != NULL);
618 return it->second;
619}
620
Ian Rogers0571d352011-11-03 19:51:38 -0700621CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800622 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700623 MethodTable::const_iterator it = compiled_methods_.find(ref);
624 if (it == compiled_methods_.end()) {
625 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700626 }
627 CHECK(it->second != NULL);
628 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700629}
630
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800631void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
632 for (size_t i = 0; i != dex_files.size(); ++i) {
633 const DexFile* dex_file = dex_files[i];
634 CHECK(dex_file != NULL);
635 SetGcMapsDexFile(class_loader, *dex_file);
636 }
637}
638
639void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
640 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
641 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
642 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
643 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
644 const char* descriptor = dex_file.GetClassDescriptor(class_def);
645 Class* klass = class_linker->FindClass(descriptor, class_loader);
646 if (klass == NULL || !klass->IsVerified()) {
647 Thread::Current()->ClearException();
648 continue;
649 }
650 const byte* class_data = dex_file.GetClassData(class_def);
651 if (class_data == NULL) {
652 // empty class such as a marker interface
653 continue;
654 }
655 ClassDataItemIterator it(dex_file, class_data);
656 while (it.HasNextStaticField()) {
657 it.Next();
658 }
659 while (it.HasNextInstanceField()) {
660 it.Next();
661 }
662 while (it.HasNextDirectMethod()) {
663 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
664 class_loader, true);
665 SetGcMapsMethod(dex_file, method);
666 it.Next();
667 }
668 while (it.HasNextVirtualMethod()) {
669 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
670 class_loader, false);
671 SetGcMapsMethod(dex_file, method);
672 it.Next();
673 }
674 }
675}
676
677void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
678 if (method == NULL) {
679 Thread::Current()->ClearException();
680 return;
681 }
682 uint16_t method_idx = method->GetDexMethodIndex();
683 MethodReference ref(&dex_file, method_idx);
684 CompiledMethod* compiled_method = GetCompiledMethod(ref);
685 if (compiled_method == NULL) {
686 return;
687 }
688 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
689 if (gc_map == NULL) {
690 return;
691 }
692 compiled_method->SetGcMap(*gc_map);
693}
694
Brian Carlstromae826982011-11-09 01:33:42 -0800695void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
696 for (size_t i = 0; i != dex_files.size(); ++i) {
697 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700698 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700699 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700700 }
701}
702
Brian Carlstrom8a487412011-08-29 20:08:52 -0700703void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700704 Runtime* runtime = Runtime::Current();
705 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700706 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700707 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700708 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700709 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700710 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700711 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
712 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700713 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700714 } else {
715 // TODO: we currently leave the entry blank for resolved
716 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700717 }
718 }
719}
720
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700721} // namespace art