blob: 10b4939bb0559c569d606bbfea09b7b2917c7c51 [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
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080021#include <dlfcn.h>
Elliott Hughesd9c67be2012-02-02 19:54:06 -080022#include <unistd.h>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070023
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070024#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070025#include "class_loader.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070026#include "dex_cache.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070027#include "jni_internal.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080028#include "oat_compilation_unit.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070029#include "oat_file.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030#include "oat/runtime/stub.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080033#include "space.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
35#include "ScopedLocalRef.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070036#include "stl_util.h"
Elliott Hughes601a1232012-02-02 17:47:38 -080037#include "timing_logger.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070038#include "verifier/method_verifier.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070039
Elliott Hughes059d5c12012-03-12 17:39:18 -070040#if defined(__APPLE__)
41#include <mach-o/dyld.h>
42#endif
43
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070044namespace art {
45
Ian Rogers996cc582012-02-14 22:23:29 -080046static double Percentage(size_t x, size_t y) {
Elliott Hughes398f64b2012-03-26 18:05:48 -070047 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
Ian Rogers996cc582012-02-14 22:23:29 -080048}
49
50static void DumpStat(size_t x, size_t y, const char* str) {
51 if (x == 0 && y == 0) {
52 return;
53 }
54 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
55}
56
Ian Rogersc8b306f2012-02-17 21:34:44 -080057class AOTCompilationStats {
58 public:
Ian Rogersca190662012-06-26 15:45:57 -070059 AOTCompilationStats()
60 : stats_lock_("AOT compilation statistics lock"),
61 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
62 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
63 resolved_types_(0), unresolved_types_(0),
64 resolved_instance_fields_(0), unresolved_instance_fields_(0),
65 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0) {
Ian Rogers2ed3b952012-03-17 11:49:39 -070066 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080067 resolved_methods_[i] = 0;
68 unresolved_methods_[i] = 0;
Ian Rogers2ed3b952012-03-17 11:49:39 -070069 virtual_made_direct_[i] = 0;
70 direct_calls_to_boot_[i] = 0;
71 direct_methods_to_boot_[i] = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -070072 }
Ian Rogersc8b306f2012-02-17 21:34:44 -080073 }
74
75 void Dump() {
76 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
77 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
78 DumpStat(resolved_types_, unresolved_types_, "types resolved");
79 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
80 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
81 "static fields resolved");
82 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
83 "static fields local to a class");
84
Ian Rogers2ed3b952012-03-17 11:49:39 -070085 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080086 std::ostringstream oss;
Ian Rogers2ed3b952012-03-17 11:49:39 -070087 oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
Ian Rogersc8b306f2012-02-17 21:34:44 -080088 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
Ian Rogers2ed3b952012-03-17 11:49:39 -070089 if (virtual_made_direct_[i] > 0) {
90 std::ostringstream oss2;
91 oss2 << static_cast<InvokeType>(i) << " methods made direct";
92 DumpStat(virtual_made_direct_[i],
93 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
94 oss2.str().c_str());
95 }
96 if (direct_calls_to_boot_[i] > 0) {
97 std::ostringstream oss2;
98 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
99 DumpStat(direct_calls_to_boot_[i],
100 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
101 oss2.str().c_str());
102 }
103 if (direct_methods_to_boot_[i] > 0) {
104 std::ostringstream oss2;
105 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
106 DumpStat(direct_methods_to_boot_[i],
107 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
108 oss2.str().c_str());
109 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800110 }
111 }
Ian Rogers996cc582012-02-14 22:23:29 -0800112
113// Allow lossy statistics in non-debug builds
114#ifndef NDEBUG
115#define STATS_LOCK() MutexLock mu(stats_lock_)
116#else
117#define STATS_LOCK()
118#endif
119
Ian Rogersc8b306f2012-02-17 21:34:44 -0800120 void TypeInDexCache() {
121 STATS_LOCK();
122 types_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800123 }
Ian Rogers996cc582012-02-14 22:23:29 -0800124
Ian Rogersc8b306f2012-02-17 21:34:44 -0800125 void TypeNotInDexCache() {
126 STATS_LOCK();
127 types_not_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800128 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800129
130 void StringInDexCache() {
131 STATS_LOCK();
132 strings_in_dex_cache_++;
133 }
134
135 void StringNotInDexCache() {
136 STATS_LOCK();
137 strings_not_in_dex_cache_++;
138 }
139
140 void TypeDoesntNeedAccessCheck() {
141 STATS_LOCK();
142 resolved_types_++;
143 }
144
145 void TypeNeedsAccessCheck() {
146 STATS_LOCK();
147 unresolved_types_++;
148 }
149
150 void ResolvedInstanceField() {
151 STATS_LOCK();
152 resolved_instance_fields_++;
153 }
154
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700155 void UnresolvedInstanceField() {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800156 STATS_LOCK();
157 unresolved_instance_fields_++;
158 }
159
160 void ResolvedLocalStaticField() {
161 STATS_LOCK();
162 resolved_local_static_fields_++;
163 }
164
165 void ResolvedStaticField() {
166 STATS_LOCK();
167 resolved_static_fields_++;
168 }
169
170 void UnresolvedStaticField() {
171 STATS_LOCK();
172 unresolved_static_fields_++;
173 }
174
175 void ResolvedMethod(InvokeType type) {
176 DCHECK_LE(type, kMaxInvokeType);
177 STATS_LOCK();
178 resolved_methods_[type]++;
179 }
180
181 void UnresolvedMethod(InvokeType type) {
182 DCHECK_LE(type, kMaxInvokeType);
183 STATS_LOCK();
184 unresolved_methods_[type]++;
185 }
186
Ian Rogers2ed3b952012-03-17 11:49:39 -0700187 void VirtualMadeDirect(InvokeType type) {
188 DCHECK_LE(type, kMaxInvokeType);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800189 STATS_LOCK();
Ian Rogers2ed3b952012-03-17 11:49:39 -0700190 virtual_made_direct_[type]++;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800191 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700192
193 void DirectCallsToBoot(InvokeType type) {
194 DCHECK_LE(type, kMaxInvokeType);
195 STATS_LOCK();
196 direct_calls_to_boot_[type]++;
197 }
198
199 void DirectMethodsToBoot(InvokeType type) {
200 DCHECK_LE(type, kMaxInvokeType);
201 STATS_LOCK();
202 direct_methods_to_boot_[type]++;
203 }
204
Ian Rogersc8b306f2012-02-17 21:34:44 -0800205 private:
206 Mutex stats_lock_;
207
208 size_t types_in_dex_cache_;
209 size_t types_not_in_dex_cache_;
210
211 size_t strings_in_dex_cache_;
212 size_t strings_not_in_dex_cache_;
213
214 size_t resolved_types_;
215 size_t unresolved_types_;
216
217 size_t resolved_instance_fields_;
218 size_t unresolved_instance_fields_;
219
220 size_t resolved_local_static_fields_;
221 size_t resolved_static_fields_;
222 size_t unresolved_static_fields_;
223
224 size_t resolved_methods_[kMaxInvokeType + 1];
225 size_t unresolved_methods_[kMaxInvokeType + 1];
Ian Rogers2ed3b952012-03-17 11:49:39 -0700226 size_t virtual_made_direct_[kMaxInvokeType + 1];
227 size_t direct_calls_to_boot_[kMaxInvokeType + 1];
228 size_t direct_methods_to_boot_[kMaxInvokeType + 1];
Ian Rogersc8b306f2012-02-17 21:34:44 -0800229
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700230 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800231};
Ian Rogers996cc582012-02-14 22:23:29 -0800232
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800233static std::string MakeCompilerSoName(InstructionSet instruction_set) {
234 // TODO: is the ARM/Thumb2 instruction set distinction really buying us anything,
235 // or just causing hassle like this?
236 if (instruction_set == kThumb2) {
237 instruction_set = kArm;
238 }
239
Brian Carlstrom5644f002012-06-27 23:05:17 -0700240 // Lower case the instruction set, because that's what we do in the build system.
Elliott Hughes6fcce302012-06-19 16:54:19 -0700241 std::string instruction_set_name(ToStr<InstructionSet>(instruction_set).str());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800242 for (size_t i = 0; i < instruction_set_name.size(); ++i) {
Brian Carlstrom5644f002012-06-27 23:05:17 -0700243 instruction_set_name[i] = tolower(instruction_set_name[i]);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800244 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700245
246 // Bad things happen if we pull in the libartd-compiler to a libart dex2oat or vice versa,
247 // because we end up with both libart and libartd in the same address space!
Elliott Hughes67d92002012-03-26 15:08:51 -0700248 const char* suffix = (kIsDebugBuild ? "d" : "");
Elliott Hughes059d5c12012-03-12 17:39:18 -0700249
250 // Work out the filename for the compiler library.
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700251#if defined(ART_USE_LLVM_COMPILER)
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800252 std::string library_name(StringPrintf("art%s-compiler-llvm", suffix));
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700253#elif defined(ART_USE_GREENLAND_COMPILER)
254 std::string library_name(StringPrintf("art%s-compiler-greenland", suffix));
255#else
256 std::string library_name(StringPrintf("art%s-compiler-%s", suffix, instruction_set_name.c_str()));
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800257#endif
Elliott Hughes059d5c12012-03-12 17:39:18 -0700258 std::string filename(StringPrintf(OS_SHARED_LIB_FORMAT_STR, library_name.c_str()));
259
260#if defined(__APPLE__)
261 // On Linux, dex2oat will have been built with an RPATH of $ORIGIN/../lib, so dlopen(3) will find
262 // the .so by itself. On Mac OS, there isn't really an equivalent, so we have to manually do the
263 // same work.
Elliott Hughes059d5c12012-03-12 17:39:18 -0700264 uint32_t executable_path_length = 0;
Elliott Hughes448e93c2012-03-28 22:30:06 -0700265 _NSGetExecutablePath(NULL, &executable_path_length);
266 std::string path(executable_path_length, static_cast<char>(0));
267 CHECK_EQ(_NSGetExecutablePath(&path[0], &executable_path_length), 0);
Elliott Hughes059d5c12012-03-12 17:39:18 -0700268
269 // Strip the "/dex2oat".
270 size_t last_slash = path.find_last_of('/');
271 CHECK_NE(last_slash, std::string::npos) << path;
272 path.resize(last_slash);
273
274 // Strip the "/bin".
275 last_slash = path.find_last_of('/');
276 path.resize(last_slash);
277
278 filename = path + "/lib/" + filename;
279#endif
280 return filename;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800281}
282
Elliott Hughes46f060a2012-03-09 17:36:50 -0800283template<typename Fn>
284static Fn FindFunction(const std::string& compiler_so_name, void* library, const char* name) {
285 Fn fn = reinterpret_cast<Fn>(dlsym(library, name));
286 if (fn == NULL) {
287 LOG(FATAL) << "Couldn't find \"" << name << "\" in compiler library " << compiler_so_name << ": " << dlerror();
288 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700289 VLOG(compiler) << "Found \"" << name << "\" at " << reinterpret_cast<void*>(fn);
Elliott Hughes46f060a2012-03-09 17:36:50 -0800290 return fn;
291}
292
Elliott Hughes5523ee02012-02-03 18:18:34 -0800293Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700294 bool support_debugging, const std::set<std::string>* image_classes,
295 bool dump_stats, bool dump_timings)
Brian Carlstromaded5f72011-10-07 17:15:04 -0700296 : instruction_set_(instruction_set),
Elliott Hughesc225caa2012-02-03 15:43:37 -0800297 compiled_classes_lock_("compiled classes lock"),
298 compiled_methods_lock_("compiled method lock"),
299 compiled_invoke_stubs_lock_("compiled invoke stubs lock"),
Logan Chien7a2a23a2012-06-06 11:01:00 +0800300#if defined(ART_USE_LLVM_COMPILER)
301 compiled_proxy_stubs_lock_("compiled proxy stubs lock"),
302#endif
Brian Carlstromaded5f72011-10-07 17:15:04 -0700303 image_(image),
Elliott Hughes5523ee02012-02-03 18:18:34 -0800304 thread_count_(thread_count),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800305 support_debugging_(support_debugging),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700306 start_ns_(0),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800307 stats_(new AOTCompilationStats),
Brian Carlstromba0668e2012-03-26 13:14:07 -0700308 dump_stats_(dump_stats),
309 dump_timings_(dump_timings),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800310 image_classes_(image_classes),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800311 compiler_library_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800312 compiler_(NULL),
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700313 compiler_context_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800314 jni_compiler_(NULL),
Logan Chien971bf3f2012-05-01 15:47:55 +0800315 create_invoke_stub_(NULL)
316{
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800317 std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800318 compiler_library_ = dlopen(compiler_so_name.c_str(), RTLD_LAZY);
319 if (compiler_library_ == NULL) {
320 LOG(FATAL) << "Couldn't find compiler library " << compiler_so_name << ": " << dlerror();
321 }
322 VLOG(compiler) << "dlopen(\"" << compiler_so_name << "\", RTLD_LAZY) returned " << compiler_library_;
323
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700324#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chien106b2a02012-03-18 04:41:38 +0800325 // Initialize compiler_context_
326 typedef void (*InitCompilerContextFn)(Compiler&);
327
328 InitCompilerContextFn init_compiler_context =
329 FindFunction<void (*)(Compiler&)>(compiler_so_name,
330 compiler_library_,
331 "ArtInitCompilerContext");
332
333 init_compiler_context(*this);
buzbee692be802012-08-29 15:52:59 -0700334#elif defined(ART_USE_QUICK_COMPILER)
335 // Initialize compiler_context_
336 typedef void (*InitCompilerContextFn)(Compiler&);
337
338 InitCompilerContextFn init_compiler_context =
339 FindFunction<void (*)(Compiler&)>(compiler_so_name,
340 compiler_library_,
341 "ArtInitQuickCompilerContext");
342
343 init_compiler_context(*this);
Logan Chien106b2a02012-03-18 04:41:38 +0800344#endif
345
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700346 compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "ArtCompileMethod");
Elliott Hughes46f060a2012-03-09 17:36:50 -0800347 jni_compiler_ = FindFunction<JniCompilerFn>(compiler_so_name, compiler_library_, "ArtJniCompileMethod");
348 create_invoke_stub_ = FindFunction<CreateInvokeStubFn>(compiler_so_name, compiler_library_, "ArtCreateInvokeStub");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800349
Logan Chienf7015fd2012-03-18 01:19:37 +0800350#if defined(ART_USE_LLVM_COMPILER)
Logan Chien7a2a23a2012-06-06 11:01:00 +0800351 create_proxy_stub_ = FindFunction<CreateProxyStubFn>(
352 compiler_so_name, compiler_library_, "ArtCreateProxyStub");
Logan Chienf7015fd2012-03-18 01:19:37 +0800353#endif
354
Brian Carlstrom25c33252011-09-18 15:58:35 -0700355 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800356 if (!image_) {
357 CHECK(image_classes_ == NULL);
358 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700359}
360
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700361Compiler::~Compiler() {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800362 {
363 MutexLock mu(compiled_classes_lock_);
364 STLDeleteValues(&compiled_classes_);
365 }
366 {
367 MutexLock mu(compiled_methods_lock_);
368 STLDeleteValues(&compiled_methods_);
369 }
370 {
371 MutexLock mu(compiled_invoke_stubs_lock_);
372 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800373 }
Brian Carlstromd06dfe72012-06-06 22:15:39 -0700374#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700375 {
Logan Chien7a2a23a2012-06-06 11:01:00 +0800376 MutexLock mu(compiled_proxy_stubs_lock_);
377 STLDeleteValues(&compiled_proxy_stubs_);
378 }
Brian Carlstromd06dfe72012-06-06 22:15:39 -0700379#endif
Logan Chien7a2a23a2012-06-06 11:01:00 +0800380 {
Brian Carlstromf5822582012-03-19 22:34:31 -0700381 MutexLock mu(compiled_methods_lock_);
382 STLDeleteElements(&code_to_patch_);
383 }
384 {
385 MutexLock mu(compiled_methods_lock_);
386 STLDeleteElements(&methods_to_patch_);
387 }
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800388#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800389 // Uninitialize compiler_context_
390 typedef void (*UninitCompilerContextFn)(Compiler&);
391
392 std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
393
394 UninitCompilerContextFn uninit_compiler_context =
395 FindFunction<void (*)(Compiler&)>(compiler_so_name,
396 compiler_library_,
397 "ArtUnInitCompilerContext");
398
399 uninit_compiler_context(*this);
buzbee692be802012-08-29 15:52:59 -0700400#elif defined(ART_USE_QUICK_COMPILER)
401 // Uninitialize compiler_context_
402 typedef void (*UninitCompilerContextFn)(Compiler&);
403
404 std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
405
406 UninitCompilerContextFn uninit_compiler_context =
407 FindFunction<void (*)(Compiler&)>(compiler_so_name,
408 compiler_library_,
409 "ArtUnInitQuickCompilerContext");
410
411 uninit_compiler_context(*this);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800412#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800413 if (compiler_library_ != NULL) {
414 VLOG(compiler) << "dlclose(" << compiler_library_ << ")";
buzbeeca7a5e42012-08-20 11:12:18 -0700415#if !defined(ART_USE_QUICK_COMPILER)
416 /*
417 * FIXME: Temporary workaround
418 * Apparently, llvm is adding dctors to atexit, but if we unload
419 * the library here the code will no longer be around at exit time
420 * and we die a flaming death in __cxa_finalize(). Apparently, some
421 * dlclose() implementations will scan the atexit list on unload and
422 * handle any associated with the soon-to-be-unloaded library.
423 * However, this is not required by POSIX and we don't do it.
424 * See: http://b/issue?id=4998315
425 * What's the right thing to do here?
426 */
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800427 dlclose(compiler_library_);
buzbeeca7a5e42012-08-20 11:12:18 -0700428#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800429 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700430}
431
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700432ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
433 Runtime::TrampolineType type) {
jeffhao7fbee072012-08-24 17:56:54 -0700434 switch (instruction_set) {
435 case kArm:
436 case kThumb2:
437 return arm::ArmCreateResolutionTrampoline(type);
438 case kMips:
439 return mips::MipsCreateResolutionTrampoline(type);
440 case kX86:
441 return x86::X86CreateResolutionTrampoline(type);
442 default:
443 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
444 return NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700445 }
446}
447
Elliott Hughes8add92d2012-01-18 18:18:43 -0800448ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800449 switch (instruction_set) {
450 case kArm:
451 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800452 return arm::CreateJniDlsymLookupStub();
jeffhao7fbee072012-08-24 17:56:54 -0700453 case kMips:
454 return mips::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800455 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800456 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800457 default:
Ian Rogers49c48942012-03-11 15:15:37 -0700458 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Ian Rogers169c9a72011-11-13 20:13:17 -0800459 return NULL;
460 }
461}
462
Ian Rogersad25ac52011-10-04 19:13:33 -0700463ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
jeffhao7fbee072012-08-24 17:56:54 -0700464 switch (instruction_set) {
465 case kArm:
466 case kThumb2:
467 return arm::CreateAbstractMethodErrorStub();
468 case kMips:
469 return mips::CreateAbstractMethodErrorStub();
470 case kX86:
471 return x86::CreateAbstractMethodErrorStub();
472 default:
473 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
474 return NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700475 }
476}
477
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478void Compiler::CompileAll(jobject class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800479 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700480 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800481
Elliott Hughes601a1232012-02-02 17:47:38 -0800482 TimingLogger timings("compiler");
483
484 PreCompile(class_loader, dex_files, timings);
485
Ian Rogers3d1548d2012-09-24 14:08:03 -0700486 Compile(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800487
Brian Carlstromba0668e2012-03-26 13:14:07 -0700488 if (dump_timings_ && timings.GetTotalNs() > MsToNs(1000)) {
Elliott Hughes601a1232012-02-02 17:47:38 -0800489 timings.Dump();
490 }
Ian Rogers996cc582012-02-14 22:23:29 -0800491
Brian Carlstromba0668e2012-03-26 13:14:07 -0700492 if (dump_stats_) {
493 stats_->Dump();
494 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700495}
496
Mathieu Chartier66f19252012-09-18 08:57:04 -0700497void Compiler::CompileOne(const AbstractMethod* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700498 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700499 Thread* self = Thread::Current();
500 jobject class_loader;
501 const DexCache* dex_cache;
502 const DexFile* dex_file;
503 {
504 ScopedObjectAccessUnchecked soa(self);
505 ScopedLocalRef<jobject>
506 local_class_loader(soa.Env(),
507 soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader()));
508 class_loader = soa.Env()->NewGlobalRef(local_class_loader.get());
509 // Find the dex_file
510 dex_cache = method->GetDeclaringClass()->GetDexCache();
511 dex_file = &Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
512 }
513 self->TransitionFromRunnableToSuspended(kNative);
Brian Carlstromae826982011-11-09 01:33:42 -0800514
Brian Carlstromae826982011-11-09 01:33:42 -0800515 std::vector<const DexFile*> dex_files;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700516 dex_files.push_back(dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800517
Elliott Hughes601a1232012-02-02 17:47:38 -0800518 TimingLogger timings("CompileOne");
519 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800520
Ian Rogers0571d352011-11-03 19:51:38 -0700521 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700522 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
Ian Rogers08f753d2012-08-24 14:35:25 -0700523 CompileMethod(code_item, method->GetAccessFlags(), method->GetInvokeType(),
524 method_idx, class_loader, *dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800525
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700526 self->GetJniEnv()->DeleteGlobalRef(class_loader);
527
528 self->TransitionFromSuspendedToRunnable();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700529}
530
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700531void Compiler::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
532 TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800533 for (size_t i = 0; i != dex_files.size(); ++i) {
534 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700535 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800536 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700537 }
538}
539
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700540void Compiler::PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
541 TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800542 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800543
Ian Rogers3d1548d2012-09-24 14:08:03 -0700544 Verify(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800545
Ian Rogers3d1548d2012-09-24 14:08:03 -0700546 InitializeClassesWithoutClinit(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800547}
548
549bool Compiler::IsImageClass(const std::string& descriptor) const {
550 if (image_classes_ == NULL) {
551 return true;
552 }
553 return image_classes_->find(descriptor) != image_classes_->end();
554}
555
Ian Rogers3d1548d2012-09-24 14:08:03 -0700556void Compiler::RecordClassStatus(ClassReference ref, CompiledClass* compiled_class) {
557 MutexLock mu(Compiler::compiled_classes_lock_);
558 compiled_classes_.Put(ref, compiled_class);
559}
560
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700561bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800562 uint32_t type_idx) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700563 ScopedObjectAccess soa(Thread::Current());
564 DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800565 if (!IsImage()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800566 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800567 return false;
568 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800569 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800570 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800571 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800572 return false;
573 }
Ian Rogers996cc582012-02-14 22:23:29 -0800574 bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor());
575 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800576 stats_->TypeInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800577 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800578 stats_->TypeNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800579 }
580 return result;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800581}
582
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700583bool Compiler::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800584 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800585 // TODO: Add support for loading strings referenced by image_classes_
586 // See also Compiler::ResolveDexFile
587
588 // The following is a test saying that if we're building the image without a restricted set of
589 // image classes then we can assume the string is present in the dex cache if it is there now
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590 bool result = IsImage() && image_classes_ == NULL;
591 if (result) {
592 ScopedObjectAccess soa(Thread::Current());
593 DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
594 result = dex_cache->GetResolvedString(string_idx) != NULL;
595 }
Ian Rogers996cc582012-02-14 22:23:29 -0800596 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800597 stats_->StringInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800598 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800599 stats_->StringNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800600 }
601 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800602}
603
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
605 uint32_t type_idx) {
606 ScopedObjectAccess soa(Thread::Current());
607 DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Ian Rogers1bddec32012-02-04 12:27:34 -0800608 // Get type from dex cache assuming it was populated by the verifier
609 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
610 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800611 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800612 return false; // Unknown class needs access checks.
613 }
614 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
615 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
616 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800617 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800618 return false; // Incomplete referrer knowledge needs access check.
619 }
620 // Perform access check, will return true if access is ok or false if we're going to have to
621 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800622 bool result = referrer_class->CanAccess(resolved_class);
623 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800624 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800625 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800626 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800627 }
628 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800629}
630
631bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
Ian Rogers1bddec32012-02-04 12:27:34 -0800632 const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800633 uint32_t type_idx) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 ScopedObjectAccess soa(Thread::Current());
635 DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Ian Rogers1bddec32012-02-04 12:27:34 -0800636 // Get type from dex cache assuming it was populated by the verifier.
637 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
638 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800639 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800640 return false; // Unknown class needs access checks.
641 }
642 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
643 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
644 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800645 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800646 return false; // Incomplete referrer knowledge needs access check.
647 }
648 // Perform access and instantiable checks, will return true if access is ok or false if we're
649 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800650 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
651 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800652 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800653 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800654 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800655 }
656 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800657}
658
Ian Rogers08f753d2012-08-24 14:35:25 -0700659static Class* ComputeCompilingMethodsClass(ScopedObjectAccess& soa,
660 OatCompilationUnit* mUnit)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700661 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_);
663 ClassLoader* class_loader = soa.Decode<ClassLoader*>(mUnit->class_loader_);
664 const DexFile::MethodId& referrer_method_id = mUnit->dex_file_->GetMethodId(mUnit->method_idx_);
665 return mUnit->class_linker_->ResolveType(*mUnit->dex_file_, referrer_method_id.class_idx_,
666 dex_cache, class_loader);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800667}
668
Ian Rogers08f753d2012-08-24 14:35:25 -0700669static Field* ComputeFieldReferencedFromCompilingMethod(ScopedObjectAccess& soa,
670 OatCompilationUnit* mUnit,
671 uint32_t field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700672 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_);
674 ClassLoader* class_loader = soa.Decode<ClassLoader*>(mUnit->class_loader_);
675 return mUnit->class_linker_->ResolveField(*mUnit->dex_file_, field_idx, dex_cache,
676 class_loader, false);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800677}
678
Mathieu Chartier66f19252012-09-18 08:57:04 -0700679static AbstractMethod* ComputeMethodReferencedFromCompilingMethod(ScopedObjectAccess& soa,
Ian Rogers08f753d2012-08-24 14:35:25 -0700680 OatCompilationUnit* mUnit,
681 uint32_t method_idx,
682 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700683 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700684 DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_);
685 ClassLoader* class_loader = soa.Decode<ClassLoader*>(mUnit->class_loader_);
686 return mUnit->class_linker_->ResolveMethod(*mUnit->dex_file_, method_idx, dex_cache,
jeffhaoc0228b82012-08-29 18:15:05 -0700687 class_loader, NULL, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800688}
689
Logan Chien4dd96f52012-02-29 01:26:58 +0800690bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
jeffhao8cd6dda2012-02-22 10:15:34 -0800691 int& field_offset, bool& is_volatile, bool is_put) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692 ScopedObjectAccess soa(Thread::Current());
Ian Rogers08f753d2012-08-24 14:35:25 -0700693 // Conservative defaults.
Ian Rogers1bddec32012-02-04 12:27:34 -0800694 field_offset = -1;
695 is_volatile = true;
Ian Rogers08f753d2012-08-24 14:35:25 -0700696 // Try to resolve field and ignore if an Incompatible Class Change Error (ie is static).
697 Field* resolved_field = ComputeFieldReferencedFromCompilingMethod(soa, mUnit, field_idx);
698 if (resolved_field != NULL && !resolved_field->IsStatic()) {
699 Class* referrer_class = ComputeCompilingMethodsClass(soa, mUnit);
Ian Rogerse2645d32012-04-11 14:42:42 -0700700 if (referrer_class != NULL) {
701 Class* fields_class = resolved_field->GetDeclaringClass();
702 bool access_ok = referrer_class->CanAccess(fields_class) &&
703 referrer_class->CanAccessMember(fields_class,
704 resolved_field->GetAccessFlags());
705 if (!access_ok) {
706 // The referring class can't access the resolved field, this may occur as a result of a
707 // protected field being made public by a sub-class. Resort to the dex file to determine
708 // the correct class for the access check.
709 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
710 Class* dex_fields_class = mUnit->class_linker_->ResolveType(dex_file,
711 dex_file.GetFieldId(field_idx).class_idx_,
712 referrer_class);
713 access_ok = referrer_class->CanAccess(dex_fields_class) &&
714 referrer_class->CanAccessMember(dex_fields_class,
715 resolved_field->GetAccessFlags());
716 }
717 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
718 fields_class != referrer_class;
719 if (access_ok && !is_write_to_final_from_wrong_class) {
720 field_offset = resolved_field->GetOffset().Int32Value();
721 is_volatile = resolved_field->IsVolatile();
722 stats_->ResolvedInstanceField();
723 return true; // Fast path.
724 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800725 }
726 }
727 // Clean up any exception left by field/type resolution
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 if (soa.Self()->IsExceptionPending()) {
729 soa.Self()->ClearException();
Ian Rogers1bddec32012-02-04 12:27:34 -0800730 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800731 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800732 return false; // Incomplete knowledge needs slow path.
733}
734
Logan Chien4dd96f52012-02-29 01:26:58 +0800735bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
Ian Rogers1bddec32012-02-04 12:27:34 -0800736 int& field_offset, int& ssb_index,
jeffhao8cd6dda2012-02-22 10:15:34 -0800737 bool& is_referrers_class, bool& is_volatile, bool is_put) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700738 ScopedObjectAccess soa(Thread::Current());
Ian Rogers08f753d2012-08-24 14:35:25 -0700739 // Conservative defaults.
Ian Rogers1bddec32012-02-04 12:27:34 -0800740 field_offset = -1;
741 ssb_index = -1;
742 is_referrers_class = false;
743 is_volatile = true;
Ian Rogers08f753d2012-08-24 14:35:25 -0700744 // Try to resolve field and ignore if an Incompatible Class Change Error (ie isn't static).
745 Field* resolved_field = ComputeFieldReferencedFromCompilingMethod(soa, mUnit, field_idx);
746 if (resolved_field != NULL && resolved_field->IsStatic()) {
747 Class* referrer_class = ComputeCompilingMethodsClass(soa, mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800748 if (referrer_class != NULL) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800749 Class* fields_class = resolved_field->GetDeclaringClass();
750 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800751 is_referrers_class = true; // implies no worrying about class initialization
752 field_offset = resolved_field->GetOffset().Int32Value();
753 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800754 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800755 return true; // fast path
756 } else {
Ian Rogerse2645d32012-04-11 14:42:42 -0700757 bool access_ok = referrer_class->CanAccess(fields_class) &&
758 referrer_class->CanAccessMember(fields_class,
759 resolved_field->GetAccessFlags());
760 if (!access_ok) {
761 // The referring class can't access the resolved field, this may occur as a result of a
762 // protected field being made public by a sub-class. Resort to the dex file to determine
763 // the correct class for the access check. Don't change the field's class as that is
764 // used to identify the SSB.
765 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
766 Class* dex_fields_class =
767 mUnit->class_linker_->ResolveType(dex_file,
768 dex_file.GetFieldId(field_idx).class_idx_,
769 referrer_class);
770 access_ok = referrer_class->CanAccess(dex_fields_class) &&
771 referrer_class->CanAccessMember(dex_fields_class,
772 resolved_field->GetAccessFlags());
773 }
jeffhao8cd6dda2012-02-22 10:15:34 -0800774 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogerse2645d32012-04-11 14:42:42 -0700775 if (access_ok && !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800776 // We have the resolved field, we must make it into a ssbIndex for the referrer
777 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800778 // TODO: for images we can elide the static storage base null check
779 // if we know there's a non-null entry in the image
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_);
781 if (fields_class->GetDexCache() == dex_cache) {
Ian Rogers4103ad22012-02-06 09:18:25 -0800782 // common case where the dex cache of both the referrer and the field are the same,
783 // no need to search the dex file
784 ssb_index = fields_class->GetDexTypeIndex();
785 field_offset = resolved_field->GetOffset().Int32Value();
786 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800787 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800788 return true;
789 }
Ian Rogerse2645d32012-04-11 14:42:42 -0700790 // Search dex file for localized ssb index, may fail if field's class is a parent
791 // of the class mentioned in the dex file and there is no dex cache entry.
Ian Rogers1bddec32012-02-04 12:27:34 -0800792 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
793 const DexFile::StringId* string_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800794 mUnit->dex_file_->FindStringId(descriptor);
Ian Rogers1bddec32012-02-04 12:27:34 -0800795 if (string_id != NULL) {
796 const DexFile::TypeId* type_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800797 mUnit->dex_file_->FindTypeId(mUnit->dex_file_->GetIndexForStringId(*string_id));
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700798 if (type_id != NULL) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800799 // medium path, needs check of static storage base being initialized
Logan Chien4dd96f52012-02-29 01:26:58 +0800800 ssb_index = mUnit->dex_file_->GetIndexForTypeId(*type_id);
Ian Rogers1bddec32012-02-04 12:27:34 -0800801 field_offset = resolved_field->GetOffset().Int32Value();
802 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800803 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800804 return true;
805 }
806 }
807 }
808 }
809 }
810 }
811 // Clean up any exception left by field/type resolution
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 if (soa.Self()->IsExceptionPending()) {
813 soa.Self()->ClearException();
Ian Rogers1bddec32012-02-04 12:27:34 -0800814 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800815 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800816 return false; // Incomplete knowledge needs slow path.
817}
818
Mathieu Chartier66f19252012-09-18 08:57:04 -0700819void Compiler::GetCodeAndMethodForDirectCall(InvokeType type, InvokeType sharp_type, AbstractMethod* method,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700820 uintptr_t& direct_code, uintptr_t& direct_method) {
821 direct_code = 0;
822 direct_method = 0;
823 if (sharp_type != kStatic && sharp_type != kDirect) {
824 return;
825 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700826 bool method_code_in_boot = method->GetDeclaringClass()->GetClassLoader() == NULL;
827 if (!method_code_in_boot) {
828 return;
829 }
830 bool has_clinit_trampoline = method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
831 if (has_clinit_trampoline) {
832 return;
833 }
834 stats_->DirectCallsToBoot(type);
835 stats_->DirectMethodsToBoot(type);
Ian Rogers3fa13792012-03-18 15:53:45 -0700836 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
837 if (compiling_boot) {
Brian Carlstrom0637e272012-03-20 01:07:52 -0700838 const bool kSupportBootImageFixup = true;
Ian Rogers3fa13792012-03-18 15:53:45 -0700839 if (kSupportBootImageFixup) {
840 MethodHelper mh(method);
841 if (IsImageClass(mh.GetDeclaringClassDescriptor())) {
Brian Carlstrom0637e272012-03-20 01:07:52 -0700842 // We can only branch directly to Methods that are resolved in the DexCache.
843 // Otherwise we won't invoke the resolution trampoline.
Ian Rogers3fa13792012-03-18 15:53:45 -0700844 direct_method = -1;
Brian Carlstrom0637e272012-03-20 01:07:52 -0700845 direct_code = -1;
Ian Rogers3fa13792012-03-18 15:53:45 -0700846 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700847 }
848 } else {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700849 if (Runtime::Current()->GetHeap()->FindSpaceFromObject(method)->IsImageSpace()) {
Ian Rogers3fa13792012-03-18 15:53:45 -0700850 direct_method = reinterpret_cast<uintptr_t>(method);
851 }
852 direct_code = reinterpret_cast<uintptr_t>(method->GetCode());
Ian Rogers2ed3b952012-03-17 11:49:39 -0700853 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700854}
855
Ian Rogersfb6adba2012-03-04 21:51:51 -0800856bool Compiler::ComputeInvokeInfo(uint32_t method_idx, OatCompilationUnit* mUnit, InvokeType& type,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700857 int& vtable_idx, uintptr_t& direct_code,
858 uintptr_t& direct_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700859 ScopedObjectAccess soa(Thread::Current());
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800860 vtable_idx = -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700861 direct_code = 0;
862 direct_method = 0;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700863 AbstractMethod* resolved_method =
Ian Rogers08f753d2012-08-24 14:35:25 -0700864 ComputeMethodReferencedFromCompilingMethod(soa, mUnit, method_idx, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800865 if (resolved_method != NULL) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700866 // Don't try to fast-path if we don't understand the caller's class or this appears to be an
867 // Incompatible Class Change Error.
868 Class* referrer_class = ComputeCompilingMethodsClass(soa, mUnit);
869 bool icce = resolved_method->CheckIncompatibleClassChange(type);
870 if (referrer_class != NULL && !icce) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800871 Class* methods_class = resolved_method->GetDeclaringClass();
872 if (!referrer_class->CanAccess(methods_class) ||
873 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800874 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800875 // The referring class can't access the resolved method, this may occur as a result of a
876 // protected method being made public by implementing an interface that re-declares the
Ian Rogers08f753d2012-08-24 14:35:25 -0700877 // method public. Resort to the dex file to determine the correct class for the access
878 // check.
Logan Chien4dd96f52012-02-29 01:26:58 +0800879 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800880 methods_class =
Logan Chien4dd96f52012-02-29 01:26:58 +0800881 mUnit->class_linker_->ResolveType(dex_file,
882 dex_file.GetMethodId(method_idx).class_idx_,
883 referrer_class);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800884 }
885 if (referrer_class->CanAccess(methods_class) &&
886 referrer_class->CanAccessMember(methods_class,
887 resolved_method->GetAccessFlags())) {
888 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogersf320b632012-03-13 18:47:47 -0700889 const bool kEnableSharpening = true;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700890 // Sharpen a virtual call into a direct call when the target is known.
891 bool can_sharpen = type == kVirtual && (resolved_method->IsFinal() ||
Ian Rogers08f753d2012-08-24 14:35:25 -0700892 methods_class->IsFinal());
893 // Ensure the vtable index will be correct to dispatch in the vtable of the super class.
jeffhao4155fcd2012-03-21 11:42:12 -0700894 can_sharpen = can_sharpen || (type == kSuper && referrer_class != methods_class &&
Ian Rogers08f753d2012-08-24 14:35:25 -0700895 referrer_class->IsSubClass(methods_class) &&
896 vtable_idx < methods_class->GetVTable()->GetLength() &&
897 methods_class->GetVTable()->Get(vtable_idx) == resolved_method);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700898 if (kEnableSharpening && can_sharpen) {
899 stats_->ResolvedMethod(type);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800900 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
901 // dex cache, check that this resolved method is where we expect it.
902 CHECK(referrer_class->GetDexCache()->GetResolvedMethod(method_idx) == resolved_method)
Ian Rogers08f753d2012-08-24 14:35:25 -0700903 << PrettyMethod(resolved_method);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700904 stats_->VirtualMadeDirect(type);
905 GetCodeAndMethodForDirectCall(type, kDirect, resolved_method, direct_code, direct_method);
906 type = kDirect;
907 return true;
908 } else if (type == kSuper) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700909 // Unsharpened super calls are suspicious so go slow-path.
Ian Rogers2ed3b952012-03-17 11:49:39 -0700910 } else {
911 stats_->ResolvedMethod(type);
912 GetCodeAndMethodForDirectCall(type, type, resolved_method, direct_code, direct_method);
913 return true;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800914 }
915 }
916 }
917 }
918 // Clean up any exception left by method/type resolution
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 if (soa.Self()->IsExceptionPending()) {
920 soa.Self()->ClearException();
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800921 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800922 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800923 return false; // Incomplete knowledge needs slow path.
924}
925
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926void Compiler::AddCodePatch(const DexFile* dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -0700927 uint32_t referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700928 InvokeType referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700929 uint32_t target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700930 InvokeType target_invoke_type,
Ian Rogers3fa13792012-03-18 15:53:45 -0700931 size_t literal_offset) {
932 MutexLock mu(compiled_methods_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 code_to_patch_.push_back(new PatchInformation(dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -0700934 referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700935 referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700936 target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700937 target_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700938 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -0700939}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940void Compiler::AddMethodPatch(const DexFile* dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -0700941 uint32_t referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700942 InvokeType referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700943 uint32_t target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700944 InvokeType target_invoke_type,
Ian Rogers3fa13792012-03-18 15:53:45 -0700945 size_t literal_offset) {
946 MutexLock mu(compiled_methods_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 methods_to_patch_.push_back(new PatchInformation(dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -0700948 referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700949 referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700950 target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700951 target_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700952 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -0700953}
954
Ian Rogers0399dde2012-06-06 17:09:28 -0700955class CompilationContext {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800956 public:
Ian Rogers0399dde2012-06-06 17:09:28 -0700957 CompilationContext(ClassLinker* class_linker,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 jobject class_loader,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800959 Compiler* compiler,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800960 const DexFile* dex_file)
961 : class_linker_(class_linker),
962 class_loader_(class_loader),
963 compiler_(compiler),
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800964 dex_file_(dex_file) {}
965
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 ClassLinker* GetClassLinker() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800967 CHECK(class_linker_ != NULL);
968 return class_linker_;
969 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970
971 jobject GetClassLoader() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800972 return class_loader_;
973 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974
975 Compiler* GetCompiler() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800976 CHECK(compiler_ != NULL);
977 return compiler_;
978 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979
980 const DexFile* GetDexFile() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800981 CHECK(dex_file_ != NULL);
982 return dex_file_;
983 }
984
985 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 ClassLinker* const class_linker_;
987 const jobject class_loader_;
988 Compiler* const compiler_;
989 const DexFile* const dex_file_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800990};
991
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992typedef void Callback(const CompilationContext* context, size_t index);
993
994static void ForAll(CompilationContext* context, size_t begin, size_t end, Callback callback,
995 size_t thread_count);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800996
997class WorkerThread {
998 public:
Ian Rogers0399dde2012-06-06 17:09:28 -0700999 WorkerThread(CompilationContext* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
Elliott Hughes1e409252012-02-06 11:21:27 -08001000 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
1001 if (spawn_) {
Elliott Hughesbf1b4572012-05-24 19:11:39 -07001002 // Mac OS stacks are only 512KiB. Make sure we have the same stack size on all platforms.
1003 pthread_attr_t attr;
1004 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new compiler worker thread");
1005 CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, 1*MB), "new compiler worker thread");
1006 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Go, this), "new compiler worker thread");
1007 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new compiler worker thread");
Elliott Hughes1e409252012-02-06 11:21:27 -08001008 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001009 }
1010
1011 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -08001012 if (spawn_) {
1013 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
1014 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001015 }
1016
1017 private:
Ian Rogersb726dcb2012-09-05 08:57:23 -07001018 static void* Go(void* arg) LOCKS_EXCLUDED(Locks::mutator_lock_) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001019 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
1020 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -08001021 if (worker->spawn_) {
Ian Rogers120f1c72012-09-28 17:17:10 -07001022 CHECK(runtime->AttachCurrentThread("Compiler Worker", true, NULL));
Elliott Hughes1e409252012-02-06 11:21:27 -08001023 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001024 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -08001025 if (worker->spawn_) {
Elliott Hughes1e409252012-02-06 11:21:27 -08001026 runtime->DetachCurrentThread();
1027 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001028 return NULL;
1029 }
1030
Ian Rogersb726dcb2012-09-05 08:57:23 -07001031 void Go() LOCKS_EXCLUDED(Locks::mutator_lock_) {
Elliott Hughes1e409252012-02-06 11:21:27 -08001032 Go(this);
1033 }
1034
Ian Rogersb726dcb2012-09-05 08:57:23 -07001035 void Run() LOCKS_EXCLUDED(Locks::mutator_lock_) {
Brian Carlstrom64277f32012-03-26 23:53:34 -07001036 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001037 for (size_t i = begin_; i < end_; i += stripe_) {
1038 callback_(context_, i);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039 self->AssertNoPendingException();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001040 }
1041 }
1042
1043 pthread_t pthread_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 // Was this thread spawned or is it the main thread?
1045 const bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001046
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001047 const CompilationContext* const context_;
1048 const size_t begin_;
1049 const size_t end_;
Ian Rogers1b09b092012-08-20 15:35:52 -07001050 Callback* callback_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 const size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -08001052
Ian Rogers0399dde2012-06-06 17:09:28 -07001053 friend void ForAll(CompilationContext*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001054};
1055
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056static void ForAll(CompilationContext* context, size_t begin, size_t end, Callback callback,
1057 size_t thread_count)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001058 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Brian Carlstrom64277f32012-03-26 23:53:34 -07001059 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 self->AssertNoPendingException();
Elliott Hughes1e409252012-02-06 11:21:27 -08001061 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -08001062
Elliott Hughes1e409252012-02-06 11:21:27 -08001063 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -08001064 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -08001065 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001066 }
Elliott Hughes1e409252012-02-06 11:21:27 -08001067 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001068
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
1070 // thread destructor's called below perform join).
1071 {
Ian Rogersb726dcb2012-09-05 08:57:23 -07001072 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 CHECK_NE(self->GetState(), kRunnable);
1074 }
Elliott Hughes81d91512012-02-03 16:38:43 -08001075 STLDeleteElements(&threads);
1076}
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001077
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078// Return true if the class should be skipped during compilation. We
1079// never skip classes in the boot class loader. However, if we have a
1080// non-boot class loader and we can resolve the class in the boot
1081// class loader, we do skip the class. This happens if an app bundles
1082// classes found in the boot classpath. Since at runtime we will
1083// select the class from the boot classpath, do not attempt to resolve
1084// or compile it now.
1085static bool SkipClass(ClassLoader* class_loader,
1086 const DexFile& dex_file,
1087 const DexFile::ClassDef& class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 if (class_loader == NULL) {
1090 return false;
1091 }
1092 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1093 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1094 Class* klass = class_linker->FindClass(descriptor, NULL);
1095 if (klass == NULL) {
1096 Thread* self = Thread::Current();
1097 CHECK(self->IsExceptionPending());
1098 self->ClearException();
1099 return false;
1100 }
1101 return true;
1102}
1103
1104static void ResolveClassFieldsAndMethods(const CompilationContext* context, size_t class_def_index)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001105 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(Thread::Current());
1107 ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader());
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001108 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001109
1110 // Method and Field are the worst. We can't resolve without either
1111 // context from the code use (to disambiguate virtual vs direct
1112 // method and instance vs static field) or from class
1113 // definitions. While the compiler will resolve what it can as it
1114 // needs it, here we try to resolve fields and methods used in class
1115 // definitions, since many of them many never be referenced by
1116 // generated code.
1117 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 if (SkipClass(class_loader, dex_file, class_def)) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001119 return;
1120 }
1121
1122 // Note the class_data pointer advances through the headers,
1123 // static fields, instance fields, direct methods, and virtual
1124 // methods.
1125 const byte* class_data = dex_file.GetClassData(class_def);
1126 if (class_data == NULL) {
1127 // empty class such as a marker interface
1128 return;
1129 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001130 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001131 ClassLinker* class_linker = context->GetClassLinker();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001132 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1133 ClassDataItemIterator it(dex_file, class_data);
1134 while (it.HasNextStaticField()) {
1135 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 class_loader, true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001137 if (field == NULL) {
1138 CHECK(self->IsExceptionPending());
1139 self->ClearException();
1140 }
1141 it.Next();
1142 }
1143 while (it.HasNextInstanceField()) {
1144 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145 class_loader, false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001146 if (field == NULL) {
1147 CHECK(self->IsExceptionPending());
1148 self->ClearException();
1149 }
1150 it.Next();
1151 }
1152 while (it.HasNextDirectMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001153 AbstractMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
jeffhaoc0228b82012-08-29 18:15:05 -07001154 class_loader, NULL, it.GetMethodInvokeType(class_def));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001155 if (method == NULL) {
1156 CHECK(self->IsExceptionPending());
1157 self->ClearException();
1158 }
1159 it.Next();
1160 }
1161 while (it.HasNextVirtualMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001162 AbstractMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
jeffhaoc0228b82012-08-29 18:15:05 -07001163 class_loader, NULL, it.GetMethodInvokeType(class_def));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001164 if (method == NULL) {
1165 CHECK(self->IsExceptionPending());
1166 self->ClearException();
1167 }
1168 it.Next();
1169 }
1170 DCHECK(!it.HasNext());
1171}
1172
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173static void ResolveType(const CompilationContext* context, size_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001174 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001175 // Class derived values are more complicated, they require the linker and loader.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 ScopedObjectAccess soa(Thread::Current());
1177 ClassLinker* class_linker = context->GetClassLinker();
1178 const DexFile& dex_file = *context->GetDexFile();
1179 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1180 ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader());
1181 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
1182
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001183 if (klass == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 CHECK(soa.Self()->IsExceptionPending());
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001185 Thread::Current()->ClearException();
1186 }
1187}
1188
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001189void Compiler::ResolveDexFile(jobject class_loader, const DexFile& dex_file,
1190 TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001191 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1192
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 // TODO: we could resolve strings here, although the string table is largely filled with class
1194 // and method names.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001195
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 CompilationContext context(class_linker, class_loader, this, &dex_file);
1197 ForAll(&context, 0, dex_file.NumTypeIds(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001198 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -07001199
Elliott Hughes5523ee02012-02-03 18:18:34 -08001200 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001201 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001202}
1203
Ian Rogers3d1548d2012-09-24 14:08:03 -07001204void Compiler::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1205 TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -08001206 for (size_t i = 0; i != dex_files.size(); ++i) {
1207 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -07001208 CHECK(dex_file != NULL);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001209 VerifyDexFile(class_loader, *dex_file, timings);
jeffhao98eacac2011-09-14 16:11:53 -07001210 }
1211}
1212
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001213static void VerifyClass(const CompilationContext* context, size_t class_def_index)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001214 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001215 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001216 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
1217 const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 Class* klass =
1219 context->GetClassLinker()->FindClass(descriptor,
1220 soa.Decode<ClassLoader*>(context->GetClassLoader()));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001221 if (klass == NULL) {
1222 Thread* self = Thread::Current();
1223 CHECK(self->IsExceptionPending());
1224 self->ClearException();
jeffhaof56197c2012-03-05 18:01:54 -08001225
1226 /*
1227 * At compile time, we can still structurally verify the class even if FindClass fails.
1228 * This is to ensure the class is structurally sound for compilation. An unsound class
1229 * will be rejected by the verifier and later skipped during compilation in the compiler.
1230 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 DexCache* dex_cache = context->GetClassLinker()->FindDexCache(*context->GetDexFile());
jeffhaof56197c2012-03-05 18:01:54 -08001232 std::string error_msg;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001233 if (verifier::MethodVerifier::VerifyClass(context->GetDexFile(),
1234 dex_cache,
1235 soa.Decode<ClassLoader*>(context->GetClassLoader()),
1236 class_def_index, error_msg) ==
1237 verifier::MethodVerifier::kHardFailure) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001238 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
jeffhaof56197c2012-03-05 18:01:54 -08001239 LOG(ERROR) << "Verification failed on class "
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001240 << PrettyDescriptor(context->GetDexFile()->GetClassDescriptor(class_def))
jeffhaof56197c2012-03-05 18:01:54 -08001241 << " because: " << error_msg;
1242 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001243 return;
1244 }
1245 CHECK(klass->IsResolved()) << PrettyClass(klass);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001246 context->GetClassLinker()->VerifyClass(klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001247
1248 if (klass->IsErroneous()) {
1249 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1250 CHECK(Thread::Current()->IsExceptionPending());
1251 Thread::Current()->ClearException();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001252 }
1253
Ian Rogers9ffb0392012-09-10 11:56:50 -07001254 CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous())
1255 << PrettyDescriptor(klass) << ": state=" << klass->GetStatus();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001256 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
1257}
1258
Ian Rogers3d1548d2012-09-24 14:08:03 -07001259void Compiler::VerifyDexFile(jobject class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001260 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 CompilationContext context(class_linker, class_loader, this, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001262 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001263 timings.AddSplit("Verify " + dex_file.GetLocation());
1264}
Elliott Hughesd9cdfe92011-10-06 16:09:04 -07001265
Ian Rogers3d1548d2012-09-24 14:08:03 -07001266static void InitializeClassWithoutClinit(const CompilationContext* context,
1267 size_t class_def_index)
1268 LOCKS_EXCLUDED(Locks::mutator_lock_) {
1269 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
1270 ScopedObjectAccess soa(Thread::Current());
1271 ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader());
1272 const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def);
1273 Class* klass = context->GetClassLinker()->FindClass(descriptor, class_loader);
Ian Rogers1f539342012-10-03 21:09:42 -07001274 Thread* self = Thread::Current();
Ian Rogers3d1548d2012-09-24 14:08:03 -07001275 if (klass != NULL) {
Ian Rogers1f539342012-10-03 21:09:42 -07001276 ObjectLock lock(self, klass);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001277 if (klass->IsVerified()) {
1278 // Only try to initialize classes that were successfully verified.
1279 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
1280 bool can_init_static_fields = compiling_boot &&
1281 context->GetCompiler()->IsImageClass(descriptor);
1282 context->GetClassLinker()->EnsureInitialized(klass, false, can_init_static_fields);
1283 // If successfully initialized place in SSB array.
1284 if (klass->IsInitialized()) {
1285 klass->GetDexCache()->GetInitializedStaticStorage()->Set(klass->GetDexTypeIndex(), klass);
1286 }
1287 }
1288 // Record the final class status if necessary.
1289 Class::Status status = klass->GetStatus();
1290 Compiler::ClassReference ref(context->GetDexFile(), class_def_index);
1291 CompiledClass* compiled_class = context->GetCompiler()->GetCompiledClass(ref);
1292 if (compiled_class == NULL) {
1293 compiled_class = new CompiledClass(status);
1294 context->GetCompiler()->RecordClassStatus(ref, compiled_class);
1295 } else {
1296 DCHECK_EQ(status, compiled_class->GetStatus());
1297 }
1298 }
Ian Rogers1f539342012-10-03 21:09:42 -07001299 // Clear any class not found or verification exceptions.
1300 self->ClearException();
Ian Rogers3d1548d2012-09-24 14:08:03 -07001301}
1302
1303void Compiler::InitializeClassesWithoutClinit(jobject jni_class_loader, const DexFile& dex_file,
1304 TimingLogger& timings) {
1305 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1306 CompilationContext context(class_linker, jni_class_loader, this, &dex_file);
1307 ForAll(&context, 0, dex_file.NumClassDefs(), InitializeClassWithoutClinit, thread_count_);
1308 timings.AddSplit("InitializeNoClinit " + dex_file.GetLocation());
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001309}
1310
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001311void Compiler::InitializeClassesWithoutClinit(jobject class_loader,
Ian Rogers3d1548d2012-09-24 14:08:03 -07001312 const std::vector<const DexFile*>& dex_files,
1313 TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -08001314 for (size_t i = 0; i != dex_files.size(); ++i) {
1315 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001316 CHECK(dex_file != NULL);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001317 InitializeClassesWithoutClinit(class_loader, *dex_file, timings);
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001318 }
1319}
1320
Ian Rogers3d1548d2012-09-24 14:08:03 -07001321void Compiler::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1322 TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -08001323 for (size_t i = 0; i != dex_files.size(); ++i) {
1324 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07001325 CHECK(dex_file != NULL);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001326 CompileDexFile(class_loader, *dex_file, timings);
Brian Carlstrom83db7722011-08-26 17:32:56 -07001327 }
1328}
1329
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330void Compiler::CompileClass(const CompilationContext* context, size_t class_def_index) {
1331 jobject class_loader = context->GetClassLoader();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001332 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesc225caa2012-02-03 15:43:37 -08001333 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001334 {
1335 ScopedObjectAccess soa(Thread::Current());
1336 ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader());
1337 if (SkipClass(class_loader, dex_file, class_def)) {
1338 return;
1339 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001340 }
jeffhaod1224c72012-02-29 13:43:08 -08001341 ClassReference ref(&dex_file, class_def_index);
1342 // Skip compiling classes with generic verifier failures since they will still fail at runtime
Ian Rogers776ac1f2012-04-13 23:36:36 -07001343 if (verifier::MethodVerifier::IsClassRejected(ref)) {
jeffhaod1224c72012-02-29 13:43:08 -08001344 return;
1345 }
Ian Rogers0571d352011-11-03 19:51:38 -07001346 const byte* class_data = dex_file.GetClassData(class_def);
1347 if (class_data == NULL) {
1348 // empty class, probably a marker interface
1349 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001350 }
Ian Rogers0571d352011-11-03 19:51:38 -07001351 ClassDataItemIterator it(dex_file, class_data);
1352 // Skip fields
1353 while (it.HasNextStaticField()) {
1354 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001355 }
Ian Rogers0571d352011-11-03 19:51:38 -07001356 while (it.HasNextInstanceField()) {
1357 it.Next();
1358 }
1359 // Compile direct methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001360 int64_t previous_direct_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07001361 while (it.HasNextDirectMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001362 uint32_t method_idx = it.GetMemberIndex();
1363 if (method_idx == previous_direct_method_idx) {
1364 // smali can create dex files with two encoded_methods sharing the same method_idx
1365 // http://code.google.com/p/smali/issues/detail?id=119
1366 it.Next();
1367 continue;
1368 }
1369 previous_direct_method_idx = method_idx;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001370 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Ian Rogers08f753d2012-08-24 14:35:25 -07001371 it.GetMethodInvokeType(class_def), method_idx,
1372 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001373 it.Next();
1374 }
1375 // Compile virtual methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001376 int64_t previous_virtual_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07001377 while (it.HasNextVirtualMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001378 uint32_t method_idx = it.GetMemberIndex();
1379 if (method_idx == previous_virtual_method_idx) {
1380 // smali can create dex files with two encoded_methods sharing the same method_idx
1381 // http://code.google.com/p/smali/issues/detail?id=119
1382 it.Next();
1383 continue;
1384 }
1385 previous_virtual_method_idx = method_idx;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001386 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Ian Rogers08f753d2012-08-24 14:35:25 -07001387 it.GetMethodInvokeType(class_def), method_idx,
1388 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001389 it.Next();
1390 }
1391 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001392}
1393
Ian Rogers3d1548d2012-09-24 14:08:03 -07001394void Compiler::CompileDexFile(jobject class_loader, const DexFile& dex_file,
1395 TimingLogger& timings) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001396 CompilationContext context(NULL, class_loader, this, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001397 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001398 timings.AddSplit("Compile " + dex_file.GetLocation());
Elliott Hughesc225caa2012-02-03 15:43:37 -08001399}
1400
Elliott Hughesa0e18062012-04-13 15:59:59 -07001401static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1402 std::string key(shorty);
1403 if (is_static) {
1404 key += "$"; // Must not be a shorty type character.
1405 }
1406 return key;
1407}
1408
Ian Rogersa3760aa2011-11-14 14:32:37 -08001409void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
Ian Rogers08f753d2012-08-24 14:35:25 -07001410 InvokeType invoke_type, uint32_t method_idx, jobject class_loader,
Ian Rogersa3760aa2011-11-14 14:32:37 -08001411 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -07001412 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001413 uint64_t start_ns = NanoTime();
Logan Chien4dd96f52012-02-29 01:26:58 +08001414
Ian Rogers169c9a72011-11-13 20:13:17 -08001415 if ((access_flags & kAccNative) != 0) {
Ian Rogers57b86d42012-03-27 16:05:41 -07001416 compiled_method = (*jni_compiler_)(*this, access_flags, method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001417 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -08001418 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001419 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -07001420 compiled_method = (*compiler_)(*this, code_item, access_flags, invoke_type, method_idx,
1421 class_loader, dex_file);
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001422 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
1423 }
Ian Rogers3bb17a62012-01-27 23:56:44 -08001424 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -08001425 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001426 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -08001427 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -07001428 }
1429
1430 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -07001431 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001432 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001433 {
1434 MutexLock mu(compiled_methods_lock_);
1435 compiled_methods_.Put(ref, compiled_method);
1436 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001437 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001438 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001439
Ian Rogers45619fc2012-02-29 11:15:25 -08001440 uint32_t shorty_len;
1441 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx), &shorty_len);
Ian Rogers169c9a72011-11-13 20:13:17 -08001442 bool is_static = (access_flags & kAccStatic) != 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001443 std::string key(MakeInvokeStubKey(is_static, shorty));
1444 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(key);
Ian Rogers0571d352011-11-03 19:51:38 -07001445 if (compiled_invoke_stub == NULL) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -08001446 compiled_invoke_stub = (*create_invoke_stub_)(*this, is_static, shorty, shorty_len);
Ian Rogers0571d352011-11-03 19:51:38 -07001447 CHECK(compiled_invoke_stub != NULL);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001448 InsertInvokeStub(key, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001449 }
Logan Chien7a2a23a2012-06-06 11:01:00 +08001450
1451#if defined(ART_USE_LLVM_COMPILER)
1452 if (!is_static) {
1453 const CompiledInvokeStub* compiled_proxy_stub = FindProxyStub(shorty);
1454 if (compiled_proxy_stub == NULL) {
1455 compiled_proxy_stub = (*create_proxy_stub_)(*this, shorty, shorty_len);
1456 CHECK(compiled_proxy_stub != NULL);
1457 InsertProxyStub(shorty, compiled_proxy_stub);
1458 }
1459 }
1460#endif
1461
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001462 if (Thread::Current()->IsExceptionPending()) {
1463 ScopedObjectAccess soa(Thread::Current());
1464 LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n"
1465 << Thread::Current()->GetException()->Dump();
1466 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001467}
1468
Elliott Hughesa0e18062012-04-13 15:59:59 -07001469const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
1470 const std::string key(MakeInvokeStubKey(is_static, shorty));
1471 return FindInvokeStub(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001472}
1473
Elliott Hughesa0e18062012-04-13 15:59:59 -07001474const CompiledInvokeStub* Compiler::FindInvokeStub(const std::string& key) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001475 MutexLock mu(compiled_invoke_stubs_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001476 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001477 if (it == compiled_invoke_stubs_.end()) {
1478 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001479 } else {
1480 DCHECK(it->second != NULL);
1481 return it->second;
1482 }
1483}
1484
Elliott Hughesa0e18062012-04-13 15:59:59 -07001485void Compiler::InsertInvokeStub(const std::string& key,
Ian Rogers0571d352011-11-03 19:51:38 -07001486 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001487 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001488 InvokeStubTable::iterator it = compiled_invoke_stubs_.find(key);
1489 if (it != compiled_invoke_stubs_.end()) {
1490 // Someone else won the race.
1491 delete compiled_invoke_stub;
1492 } else {
1493 compiled_invoke_stubs_.Put(key, compiled_invoke_stub);
1494 }
Ian Rogers0571d352011-11-03 19:51:38 -07001495}
1496
Logan Chien7a2a23a2012-06-06 11:01:00 +08001497#if defined(ART_USE_LLVM_COMPILER)
1498const CompiledInvokeStub* Compiler::FindProxyStub(const char* shorty) const {
1499 MutexLock mu(compiled_proxy_stubs_lock_);
1500 ProxyStubTable::const_iterator it = compiled_proxy_stubs_.find(shorty);
1501 if (it == compiled_proxy_stubs_.end()) {
1502 return NULL;
1503 } else {
1504 DCHECK(it->second != NULL);
1505 return it->second;
1506 }
1507}
1508
1509void Compiler::InsertProxyStub(const char* shorty,
1510 const CompiledInvokeStub* compiled_proxy_stub) {
1511 MutexLock mu(compiled_proxy_stubs_lock_);
1512 InvokeStubTable::iterator it = compiled_proxy_stubs_.find(shorty);
1513 if (it != compiled_proxy_stubs_.end()) {
1514 // Someone else won the race.
1515 delete compiled_proxy_stub;
1516 } else {
1517 compiled_proxy_stubs_.Put(shorty, compiled_proxy_stub);
1518 }
1519}
1520#endif
1521
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001522CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001523 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001524 ClassTable::const_iterator it = compiled_classes_.find(ref);
1525 if (it == compiled_classes_.end()) {
1526 return NULL;
1527 }
1528 CHECK(it->second != NULL);
1529 return it->second;
1530}
1531
Ian Rogers0571d352011-11-03 19:51:38 -07001532CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001533 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001534 MethodTable::const_iterator it = compiled_methods_.find(ref);
1535 if (it == compiled_methods_.end()) {
1536 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001537 }
1538 CHECK(it->second != NULL);
1539 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001540}
1541
buzbee2cfc6392012-05-07 14:51:40 -07001542#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_QUICK_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +08001543void Compiler::SetBitcodeFileName(std::string const& filename) {
Logan Chien106b2a02012-03-18 04:41:38 +08001544 typedef void (*SetBitcodeFileNameFn)(Compiler&, std::string const&);
1545
1546 SetBitcodeFileNameFn set_bitcode_file_name =
1547 FindFunction<SetBitcodeFileNameFn>(MakeCompilerSoName(instruction_set_),
1548 compiler_library_,
1549 "compilerLLVMSetBitcodeFileName");
1550
1551 set_bitcode_file_name(*this, filename);
Logan Chien8b977d32012-02-21 19:14:55 +08001552}
buzbee2cfc6392012-05-07 14:51:40 -07001553#endif
Logan Chienf7015fd2012-03-18 01:19:37 +08001554
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001555} // namespace art