blob: c4c84248f7ab97d89e9c2879c8d930779d193966 [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"
Ian Rogers1bddec32012-02-04 12:27:34 -080027#include "compiler/CompilerIR.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070028#include "dex_cache.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 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800200 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800201 if (resolved_class == NULL) {
202 return false;
203 }
204 return IsImageClass(ClassHelper(resolved_class).GetDescriptor());
205}
206
Ian Rogers1bddec32012-02-04 12:27:34 -0800207bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache,
208 uint32_t string_idx) const {
209 // TODO: Add support for loading strings referenced by image_classes_
210 // See also Compiler::ResolveDexFile
211
212 // The following is a test saying that if we're building the image without a restricted set of
213 // image classes then we can assume the string is present in the dex cache if it is there now
214 return IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
215}
216
217bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
218 const DexFile& dex_file, uint32_t type_idx) const {
219 // Get type from dex cache assuming it was populated by the verifier
220 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
221 if (resolved_class == NULL) {
222 return false; // Unknown class needs access checks.
223 }
224 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
225 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
226 if (referrer_class == NULL) {
227 return false; // Incomplete referrer knowledge needs access check.
228 }
229 // Perform access check, will return true if access is ok or false if we're going to have to
230 // check this at runtime (for example for class loaders).
231 return referrer_class->CanAccess(resolved_class);
232}
233
234bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
235 const DexCache* dex_cache,
236 const DexFile& dex_file,
237 uint32_t type_idx) const {
238 // Get type from dex cache assuming it was populated by the verifier.
239 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
240 if (resolved_class == NULL) {
241 return false; // Unknown class needs access checks.
242 }
243 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
244 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
245 if (referrer_class == NULL) {
246 return false; // Incomplete referrer knowledge needs access check.
247 }
248 // Perform access and instantiable checks, will return true if access is ok or false if we're
249 // going to have to check this at runtime (for example for class loaders).
250 return referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
251}
252
253bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, CompilationUnit* cUnit,
254 int& field_offset, bool& is_volatile) const {
255 // Conservative defaults
256 field_offset = -1;
257 is_volatile = true;
258 // Try to resolve field
259 Field* resolved_field =
260 cUnit->class_linker->ResolveField(*cUnit->dex_file, field_idx, cUnit->dex_cache,
261 cUnit->class_loader, false);
262 if (resolved_field != NULL) {
263 const DexFile::MethodId& referrer_method_id = cUnit->dex_file->GetMethodId(cUnit->method_idx);
264 Class* referrer_class =
265 cUnit->class_linker->ResolveType(*cUnit->dex_file, referrer_method_id.class_idx_,
266 cUnit->dex_cache, cUnit->class_loader);
267 // Try to resolve referring class then access check, failure to pass the
268 Class* fields_class = resolved_field->GetDeclaringClass();
269 if (referrer_class != NULL &&
270 referrer_class->CanAccess(fields_class) &&
271 referrer_class->CanAccessMember(fields_class,
272 resolved_field->GetAccessFlags())) {
273 field_offset = resolved_field->GetOffset().Int32Value();
274 is_volatile = resolved_field->IsVolatile();
275 return true; // Fast path.
276 }
277 }
278 // Clean up any exception left by field/type resolution
279 Thread* thread = Thread::Current();
280 if (thread->IsExceptionPending()) {
281 thread->ClearException();
282 }
283 return false; // Incomplete knowledge needs slow path.
284}
285
286bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, CompilationUnit* cUnit,
287 int& field_offset, int& ssb_index,
288 bool& is_referrers_class, bool& is_volatile) const {
289 // Conservative defaults
290 field_offset = -1;
291 ssb_index = -1;
292 is_referrers_class = false;
293 is_volatile = true;
294 // Try to resolve field
295 Field* resolved_field =
296 cUnit->class_linker->ResolveField(*cUnit->dex_file, field_idx, cUnit->dex_cache,
297 cUnit->class_loader, true);
298 if (resolved_field != NULL) {
299 const DexFile::MethodId& referrer_method_id = cUnit->dex_file->GetMethodId(cUnit->method_idx);
300 Class* referrer_class =
301 cUnit->class_linker->ResolveType(*cUnit->dex_file, referrer_method_id.class_idx_,
302 cUnit->dex_cache, cUnit->class_loader);
303 if (referrer_class != NULL) {
304 if (resolved_field->GetDeclaringClass() == referrer_class) {
305 is_referrers_class = true; // implies no worrying about class initialization
306 field_offset = resolved_field->GetOffset().Int32Value();
307 is_volatile = resolved_field->IsVolatile();
308 return true; // fast path
309 } else {
310 Class* fields_class = resolved_field->GetDeclaringClass();
311 if (referrer_class->CanAccess(fields_class) &&
312 referrer_class->CanAccessMember(fields_class,
313 resolved_field->GetAccessFlags())) {
314 // We have the resolved field, we must make it into a ssbIndex for the referrer
315 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800316 // TODO: for images we can elide the static storage base null check
317 // if we know there's a non-null entry in the image
318 if (fields_class->GetDexCache() == cUnit->dex_cache) {
319 // common case where the dex cache of both the referrer and the field are the same,
320 // no need to search the dex file
321 ssb_index = fields_class->GetDexTypeIndex();
322 field_offset = resolved_field->GetOffset().Int32Value();
323 is_volatile = resolved_field->IsVolatile();
324 return true;
325 }
326 // Search dex file for localized ssb index
Ian Rogers1bddec32012-02-04 12:27:34 -0800327 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
328 const DexFile::StringId* string_id =
329 cUnit->dex_file->FindStringId(descriptor);
330 if (string_id != NULL) {
331 const DexFile::TypeId* type_id =
332 cUnit->dex_file->FindTypeId(cUnit->dex_file->GetIndexForStringId(*string_id));
333 if(type_id != NULL) {
334 // medium path, needs check of static storage base being initialized
Ian Rogers1bddec32012-02-04 12:27:34 -0800335 ssb_index = cUnit->dex_file->GetIndexForTypeId(*type_id);
336 field_offset = resolved_field->GetOffset().Int32Value();
337 is_volatile = resolved_field->IsVolatile();
338 return true;
339 }
340 }
341 }
342 }
343 }
344 }
345 // Clean up any exception left by field/type resolution
346 Thread* thread = Thread::Current();
347 if (thread->IsExceptionPending()) {
348 thread->ClearException();
349 }
350 return false; // Incomplete knowledge needs slow path.
351}
352
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800353// Return true if the class should be skipped during compilation. We
354// never skip classes in the boot class loader. However, if we have a
355// non-boot class loader and we can resolve the class in the boot
356// class loader, we do skip the class. This happens if an app bundles
357// classes found in the boot classpath. Since at runtime we will
358// select the class from the boot classpath, do not attempt to resolve
359// or compile it now.
360static bool SkipClass(const ClassLoader* class_loader,
361 const DexFile& dex_file,
362 const DexFile::ClassDef& class_def) {
363 if (class_loader == NULL) {
364 return false;
365 }
366 const char* descriptor = dex_file.GetClassDescriptor(class_def);
367 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
368 Class* klass = class_linker->FindClass(descriptor, NULL);
369 if (klass == NULL) {
370 Thread* self = Thread::Current();
371 CHECK(self->IsExceptionPending());
372 self->ClearException();
373 return false;
374 }
375 return true;
376}
377
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800378struct Context {
379 ClassLinker* class_linker;
380 const ClassLoader* class_loader;
Elliott Hughesc225caa2012-02-03 15:43:37 -0800381 Compiler* compiler;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800382 DexCache* dex_cache;
383 const DexFile* dex_file;
384};
385
386typedef void Callback(Context* context, size_t index);
387
388class WorkerThread {
389 public:
Elliott Hughes1e409252012-02-06 11:21:27 -0800390 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
391 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
392 if (spawn_) {
393 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread");
394 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800395 }
396
397 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800398 if (spawn_) {
399 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
400 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800401 }
402
403 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800404 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800405 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
406 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800407 if (worker->spawn_) {
408 runtime->AttachCurrentThread("Compiler Worker", true);
409 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800410 Thread::Current()->SetState(Thread::kRunnable);
411 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800412 if (worker->spawn_) {
413 Thread::Current()->SetState(Thread::kNative);
414 runtime->DetachCurrentThread();
415 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800416 return NULL;
417 }
418
Elliott Hughes1e409252012-02-06 11:21:27 -0800419 void Go() {
420 Go(this);
421 }
422
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800423 void Run() {
424 for (size_t i = begin_; i < end_; i += stripe_) {
425 callback_(context_, i);
426 }
427 }
428
429 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800430 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800431
432 Context* context_;
433 size_t begin_;
434 size_t end_;
435 Callback* callback_;
436 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800437
438 friend void ForAll(Context*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800439};
440
Elliott Hughes5523ee02012-02-03 18:18:34 -0800441void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800442 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -0800443
Elliott Hughes1e409252012-02-06 11:21:27 -0800444 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -0800445 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800446 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800447 }
Elliott Hughes1e409252012-02-06 11:21:27 -0800448 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800449
Elliott Hughes81d91512012-02-03 16:38:43 -0800450 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
451 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
452 STLDeleteElements(&threads);
453}
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800454
455static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
456 const DexFile& dex_file = *context->dex_file;
457
458 // Method and Field are the worst. We can't resolve without either
459 // context from the code use (to disambiguate virtual vs direct
460 // method and instance vs static field) or from class
461 // definitions. While the compiler will resolve what it can as it
462 // needs it, here we try to resolve fields and methods used in class
463 // definitions, since many of them many never be referenced by
464 // generated code.
465 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
466 if (SkipClass(context->class_loader, dex_file, class_def)) {
467 return;
468 }
469
470 // Note the class_data pointer advances through the headers,
471 // static fields, instance fields, direct methods, and virtual
472 // methods.
473 const byte* class_data = dex_file.GetClassData(class_def);
474 if (class_data == NULL) {
475 // empty class such as a marker interface
476 return;
477 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800478 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800479 ClassLinker* class_linker = context->class_linker;
480 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
481 ClassDataItemIterator it(dex_file, class_data);
482 while (it.HasNextStaticField()) {
483 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
484 context->class_loader, true);
485 if (field == NULL) {
486 CHECK(self->IsExceptionPending());
487 self->ClearException();
488 }
489 it.Next();
490 }
491 while (it.HasNextInstanceField()) {
492 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
493 context->class_loader, false);
494 if (field == NULL) {
495 CHECK(self->IsExceptionPending());
496 self->ClearException();
497 }
498 it.Next();
499 }
500 while (it.HasNextDirectMethod()) {
501 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
502 context->class_loader, true);
503 if (method == NULL) {
504 CHECK(self->IsExceptionPending());
505 self->ClearException();
506 }
507 it.Next();
508 }
509 while (it.HasNextVirtualMethod()) {
510 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
511 context->class_loader, false);
512 if (method == NULL) {
513 CHECK(self->IsExceptionPending());
514 self->ClearException();
515 }
516 it.Next();
517 }
518 DCHECK(!it.HasNext());
519}
520
521static void ResolveType(Context* context, size_t type_idx) {
522 // Class derived values are more complicated, they require the linker and loader.
523 Thread* self = Thread::Current();
524 ClassLinker* class_linker = context->class_linker;
525 const DexFile& dex_file = *context->dex_file;
526 Class* klass = class_linker->ResolveType(dex_file, type_idx, context->dex_cache, context->class_loader);
527 if (klass == NULL) {
528 CHECK(self->IsExceptionPending());
529 Thread::Current()->ClearException();
530 }
531}
532
533void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700534 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700535 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700536
Brian Carlstromae826982011-11-09 01:33:42 -0800537 // Strings are easy in that they always are simply resolved to literals in the same file
538 if (image_ && image_classes_ == NULL) {
539 // TODO: Add support for loading strings referenced by image_classes_
540 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700541 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
542 class_linker->ResolveString(dex_file, string_idx, dex_cache);
543 }
Elliott Hughesff738062012-02-03 15:00:42 -0800544 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700545 }
546
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800547 Context context;
548 context.class_linker = class_linker;
549 context.class_loader = class_loader;
550 context.dex_cache = dex_cache;
551 context.dex_file = &dex_file;
552
Elliott Hughes5523ee02012-02-03 18:18:34 -0800553 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800554 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700555
Elliott Hughes5523ee02012-02-03 18:18:34 -0800556 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800557 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700558}
559
Brian Carlstromae826982011-11-09 01:33:42 -0800560void Compiler::Verify(const ClassLoader* class_loader,
561 const std::vector<const DexFile*>& dex_files) {
562 for (size_t i = 0; i != dex_files.size(); ++i) {
563 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700564 CHECK(dex_file != NULL);
565 VerifyDexFile(class_loader, *dex_file);
566 }
567}
568
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800569static void VerifyClass(Context* context, size_t class_def_index) {
570 const DexFile::ClassDef& class_def = context->dex_file->GetClassDef(class_def_index);
571 const char* descriptor = context->dex_file->GetClassDescriptor(class_def);
572 Class* klass = context->class_linker->FindClass(descriptor, context->class_loader);
573 if (klass == NULL) {
574 Thread* self = Thread::Current();
575 CHECK(self->IsExceptionPending());
576 self->ClearException();
577 return;
578 }
579 CHECK(klass->IsResolved()) << PrettyClass(klass);
580 context->class_linker->VerifyClass(klass);
581
582 if (klass->IsErroneous()) {
583 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
584 CHECK(Thread::Current()->IsExceptionPending());
585 Thread::Current()->ClearException();
586 // We want to try verification again at run-time, so move back into the resolved state.
587 klass->SetStatus(Class::kStatusResolved);
588 }
589
590 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
591 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
592}
593
jeffhao98eacac2011-09-14 16:11:53 -0700594void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700595 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700596
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800597 Context context;
598 context.class_linker = Runtime::Current()->GetClassLinker();
599 context.class_loader = class_loader;
600 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800601 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700602
jeffhaob4df5142011-09-19 20:25:32 -0700603 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700604}
605
Brian Carlstromae826982011-11-09 01:33:42 -0800606void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
607 const std::vector<const DexFile*>& dex_files) {
608 for (size_t i = 0; i != dex_files.size(); ++i) {
609 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700610 CHECK(dex_file != NULL);
611 InitializeClassesWithoutClinit(class_loader, *dex_file);
612 }
613}
614
615void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
616 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700617 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
618 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700619 const char* descriptor = dex_file.GetClassDescriptor(class_def);
620 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700621 if (klass != NULL) {
622 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800623 // record the final class status if necessary
624 Class::Status status = klass->GetStatus();
625 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800626 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800627 CompiledClass* compiled_class = GetCompiledClass(ref);
628 if (compiled_class == NULL) {
629 compiled_class = new CompiledClass(status);
630 compiled_classes_[ref] = compiled_class;
631 } else {
632 DCHECK_EQ(status, compiled_class->GetStatus());
633 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700634 }
635 // clear any class not found or verification exceptions
636 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700637 }
638
639 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
640 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
641 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700642 if (klass == NULL) {
643 Thread::Current()->ClearException();
644 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700645 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
646 }
jeffhao98eacac2011-09-14 16:11:53 -0700647 }
648}
649
Brian Carlstromae826982011-11-09 01:33:42 -0800650void Compiler::Compile(const ClassLoader* class_loader,
651 const std::vector<const DexFile*>& dex_files) {
652 for (size_t i = 0; i != dex_files.size(); ++i) {
653 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700654 CHECK(dex_file != NULL);
655 CompileDexFile(class_loader, *dex_file);
656 }
657}
658
Elliott Hughesc225caa2012-02-03 15:43:37 -0800659void Compiler::CompileClass(Context* context, size_t class_def_index) {
660 const ClassLoader* class_loader = context->class_loader;
661 const DexFile& dex_file = *context->dex_file;
662 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800663 if (SkipClass(class_loader, dex_file, class_def)) {
664 return;
665 }
Ian Rogers0571d352011-11-03 19:51:38 -0700666 const byte* class_data = dex_file.GetClassData(class_def);
667 if (class_data == NULL) {
668 // empty class, probably a marker interface
669 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700670 }
Ian Rogers0571d352011-11-03 19:51:38 -0700671 ClassDataItemIterator it(dex_file, class_data);
672 // Skip fields
673 while (it.HasNextStaticField()) {
674 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700675 }
Ian Rogers0571d352011-11-03 19:51:38 -0700676 while (it.HasNextInstanceField()) {
677 it.Next();
678 }
679 // Compile direct methods
680 while (it.HasNextDirectMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800681 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
682 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700683 it.Next();
684 }
685 // Compile virtual methods
686 while (it.HasNextVirtualMethod()) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800687 context->compiler->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
688 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700689 it.Next();
690 }
691 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700692}
693
Elliott Hughesc225caa2012-02-03 15:43:37 -0800694void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
695 Context context;
696 context.class_loader = class_loader;
697 context.compiler = this;
698 context.dex_file = &dex_file;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800699 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800700}
701
Ian Rogersa3760aa2011-11-14 14:32:37 -0800702void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
703 uint32_t method_idx, const ClassLoader* class_loader,
704 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -0700705 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800706 uint64_t start_ns = NanoTime();
Ian Rogers169c9a72011-11-13 20:13:17 -0800707 if ((access_flags & kAccNative) != 0) {
708 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700709 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800710 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700711 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800712 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
713 dex_file, kThumb2);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800714 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
715 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800716 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -0800717 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800718 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800719 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -0700720 }
721
722 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -0700723 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800724 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800725 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700726 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800727 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -0700728 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700729
Ian Rogers0571d352011-11-03 19:51:38 -0700730 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers169c9a72011-11-13 20:13:17 -0800731 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700732 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
733 if (compiled_invoke_stub == NULL) {
734 if (instruction_set_ == kX86) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800735 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700736 } else {
737 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
738 // Generates invocation stub using ARM instruction set
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800739 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700740 }
741 CHECK(compiled_invoke_stub != NULL);
742 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700743 }
Ian Rogers0571d352011-11-03 19:51:38 -0700744 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700745}
746
Ian Rogers0571d352011-11-03 19:51:38 -0700747static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
748 std::string key(shorty);
749 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800750 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700751 }
Ian Rogers0571d352011-11-03 19:51:38 -0700752 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700753}
754
Ian Rogers0571d352011-11-03 19:51:38 -0700755const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800756 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -0800757 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700758 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700759 if (it == compiled_invoke_stubs_.end()) {
760 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -0700761 } else {
762 DCHECK(it->second != NULL);
763 return it->second;
764 }
765}
766
767void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
768 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800769 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -0800770 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -0700771 compiled_invoke_stubs_[key] = compiled_invoke_stub;
772}
773
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800774CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800775 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800776 ClassTable::const_iterator it = compiled_classes_.find(ref);
777 if (it == compiled_classes_.end()) {
778 return NULL;
779 }
780 CHECK(it->second != NULL);
781 return it->second;
782}
783
Ian Rogers0571d352011-11-03 19:51:38 -0700784CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800785 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -0700786 MethodTable::const_iterator it = compiled_methods_.find(ref);
787 if (it == compiled_methods_.end()) {
788 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700789 }
790 CHECK(it->second != NULL);
791 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700792}
793
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800794void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
795 for (size_t i = 0; i != dex_files.size(); ++i) {
796 const DexFile* dex_file = dex_files[i];
797 CHECK(dex_file != NULL);
798 SetGcMapsDexFile(class_loader, *dex_file);
799 }
800}
801
802void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
803 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
804 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
805 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
806 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
807 const char* descriptor = dex_file.GetClassDescriptor(class_def);
808 Class* klass = class_linker->FindClass(descriptor, class_loader);
809 if (klass == NULL || !klass->IsVerified()) {
810 Thread::Current()->ClearException();
811 continue;
812 }
813 const byte* class_data = dex_file.GetClassData(class_def);
814 if (class_data == NULL) {
815 // empty class such as a marker interface
816 continue;
817 }
818 ClassDataItemIterator it(dex_file, class_data);
819 while (it.HasNextStaticField()) {
820 it.Next();
821 }
822 while (it.HasNextInstanceField()) {
823 it.Next();
824 }
825 while (it.HasNextDirectMethod()) {
826 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
827 class_loader, true);
828 SetGcMapsMethod(dex_file, method);
829 it.Next();
830 }
831 while (it.HasNextVirtualMethod()) {
832 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
833 class_loader, false);
834 SetGcMapsMethod(dex_file, method);
835 it.Next();
836 }
837 }
838}
839
Ian Rogers1bddec32012-02-04 12:27:34 -0800840namespace verifier {
841 class DexVerifier {
842 public:
843 static const std::vector<uint8_t>* GetGcMap(Compiler::MethodReference ref);
844 };
845}
846
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800847void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
848 if (method == NULL) {
849 Thread::Current()->ClearException();
850 return;
851 }
852 uint16_t method_idx = method->GetDexMethodIndex();
853 MethodReference ref(&dex_file, method_idx);
854 CompiledMethod* compiled_method = GetCompiledMethod(ref);
855 if (compiled_method == NULL) {
856 return;
857 }
858 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
859 if (gc_map == NULL) {
860 return;
861 }
862 compiled_method->SetGcMap(*gc_map);
863}
864
Brian Carlstromae826982011-11-09 01:33:42 -0800865void Compiler::SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files) {
866 for (size_t i = 0; i != dex_files.size(); ++i) {
867 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700868 CHECK(dex_file != NULL);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700869 SetCodeAndDirectMethodsDexFile(*dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700870 }
871}
872
Brian Carlstrom8a487412011-08-29 20:08:52 -0700873void Compiler::SetCodeAndDirectMethodsDexFile(const DexFile& dex_file) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700874 Runtime* runtime = Runtime::Current();
875 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom83db7722011-08-26 17:32:56 -0700876 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700877 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700878 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700879 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700880 if (method == NULL || method->IsDirect()) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700881 Runtime::TrampolineType type = Runtime::GetTrampolineType(method);
882 ByteArray* res_trampoline = runtime->GetResolutionStubArray(type);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700883 code_and_direct_methods->SetResolvedDirectMethodTrampoline(i, res_trampoline);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700884 } else {
885 // TODO: we currently leave the entry blank for resolved
886 // non-direct methods. we could put in an error stub.
Brian Carlstrom83db7722011-08-26 17:32:56 -0700887 }
888 }
889}
890
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700891} // namespace art