blob: ddf9e87e743eedf9f82e2234c161950093e3e49c [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"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070033#include "gc/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"
Ian Rogers50b35e22012-10-04 10:09:15 -070037#include "thread.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070038#include "thread_pool.h"
Elliott Hughes601a1232012-02-02 17:47:38 -080039#include "timing_logger.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070040#include "verifier/method_verifier.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070041
Elliott Hughes059d5c12012-03-12 17:39:18 -070042#if defined(__APPLE__)
43#include <mach-o/dyld.h>
44#endif
45
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070046namespace art {
47
Ian Rogers996cc582012-02-14 22:23:29 -080048static double Percentage(size_t x, size_t y) {
Elliott Hughes398f64b2012-03-26 18:05:48 -070049 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
Ian Rogers996cc582012-02-14 22:23:29 -080050}
51
52static void DumpStat(size_t x, size_t y, const char* str) {
53 if (x == 0 && y == 0) {
54 return;
55 }
56 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
57}
58
Ian Rogersc8b306f2012-02-17 21:34:44 -080059class AOTCompilationStats {
60 public:
Ian Rogersca190662012-06-26 15:45:57 -070061 AOTCompilationStats()
62 : stats_lock_("AOT compilation statistics lock"),
63 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
64 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
65 resolved_types_(0), unresolved_types_(0),
66 resolved_instance_fields_(0), unresolved_instance_fields_(0),
67 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0) {
Ian Rogers2ed3b952012-03-17 11:49:39 -070068 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080069 resolved_methods_[i] = 0;
70 unresolved_methods_[i] = 0;
Ian Rogers2ed3b952012-03-17 11:49:39 -070071 virtual_made_direct_[i] = 0;
72 direct_calls_to_boot_[i] = 0;
73 direct_methods_to_boot_[i] = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -070074 }
Ian Rogersc8b306f2012-02-17 21:34:44 -080075 }
76
77 void Dump() {
78 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
79 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
80 DumpStat(resolved_types_, unresolved_types_, "types resolved");
81 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
82 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
83 "static fields resolved");
84 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
85 "static fields local to a class");
86
Ian Rogers2ed3b952012-03-17 11:49:39 -070087 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080088 std::ostringstream oss;
Ian Rogers2ed3b952012-03-17 11:49:39 -070089 oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
Ian Rogersc8b306f2012-02-17 21:34:44 -080090 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
Ian Rogers2ed3b952012-03-17 11:49:39 -070091 if (virtual_made_direct_[i] > 0) {
92 std::ostringstream oss2;
93 oss2 << static_cast<InvokeType>(i) << " methods made direct";
94 DumpStat(virtual_made_direct_[i],
95 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
96 oss2.str().c_str());
97 }
98 if (direct_calls_to_boot_[i] > 0) {
99 std::ostringstream oss2;
100 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
101 DumpStat(direct_calls_to_boot_[i],
102 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
103 oss2.str().c_str());
104 }
105 if (direct_methods_to_boot_[i] > 0) {
106 std::ostringstream oss2;
107 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
108 DumpStat(direct_methods_to_boot_[i],
109 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
110 oss2.str().c_str());
111 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800112 }
113 }
Ian Rogers996cc582012-02-14 22:23:29 -0800114
Ian Rogers50b35e22012-10-04 10:09:15 -0700115// Allow lossy statistics in non-debug builds.
Ian Rogers996cc582012-02-14 22:23:29 -0800116#ifndef NDEBUG
Ian Rogers50b35e22012-10-04 10:09:15 -0700117#define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_)
Ian Rogers996cc582012-02-14 22:23:29 -0800118#else
119#define STATS_LOCK()
120#endif
121
Ian Rogersc8b306f2012-02-17 21:34:44 -0800122 void TypeInDexCache() {
123 STATS_LOCK();
124 types_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800125 }
Ian Rogers996cc582012-02-14 22:23:29 -0800126
Ian Rogersc8b306f2012-02-17 21:34:44 -0800127 void TypeNotInDexCache() {
128 STATS_LOCK();
129 types_not_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800130 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800131
132 void StringInDexCache() {
133 STATS_LOCK();
134 strings_in_dex_cache_++;
135 }
136
137 void StringNotInDexCache() {
138 STATS_LOCK();
139 strings_not_in_dex_cache_++;
140 }
141
142 void TypeDoesntNeedAccessCheck() {
143 STATS_LOCK();
144 resolved_types_++;
145 }
146
147 void TypeNeedsAccessCheck() {
148 STATS_LOCK();
149 unresolved_types_++;
150 }
151
152 void ResolvedInstanceField() {
153 STATS_LOCK();
154 resolved_instance_fields_++;
155 }
156
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700157 void UnresolvedInstanceField() {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800158 STATS_LOCK();
159 unresolved_instance_fields_++;
160 }
161
162 void ResolvedLocalStaticField() {
163 STATS_LOCK();
164 resolved_local_static_fields_++;
165 }
166
167 void ResolvedStaticField() {
168 STATS_LOCK();
169 resolved_static_fields_++;
170 }
171
172 void UnresolvedStaticField() {
173 STATS_LOCK();
174 unresolved_static_fields_++;
175 }
176
177 void ResolvedMethod(InvokeType type) {
178 DCHECK_LE(type, kMaxInvokeType);
179 STATS_LOCK();
180 resolved_methods_[type]++;
181 }
182
183 void UnresolvedMethod(InvokeType type) {
184 DCHECK_LE(type, kMaxInvokeType);
185 STATS_LOCK();
186 unresolved_methods_[type]++;
187 }
188
Ian Rogers2ed3b952012-03-17 11:49:39 -0700189 void VirtualMadeDirect(InvokeType type) {
190 DCHECK_LE(type, kMaxInvokeType);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800191 STATS_LOCK();
Ian Rogers2ed3b952012-03-17 11:49:39 -0700192 virtual_made_direct_[type]++;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800193 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700194
195 void DirectCallsToBoot(InvokeType type) {
196 DCHECK_LE(type, kMaxInvokeType);
197 STATS_LOCK();
198 direct_calls_to_boot_[type]++;
199 }
200
201 void DirectMethodsToBoot(InvokeType type) {
202 DCHECK_LE(type, kMaxInvokeType);
203 STATS_LOCK();
204 direct_methods_to_boot_[type]++;
205 }
206
Ian Rogersc8b306f2012-02-17 21:34:44 -0800207 private:
208 Mutex stats_lock_;
209
210 size_t types_in_dex_cache_;
211 size_t types_not_in_dex_cache_;
212
213 size_t strings_in_dex_cache_;
214 size_t strings_not_in_dex_cache_;
215
216 size_t resolved_types_;
217 size_t unresolved_types_;
218
219 size_t resolved_instance_fields_;
220 size_t unresolved_instance_fields_;
221
222 size_t resolved_local_static_fields_;
223 size_t resolved_static_fields_;
224 size_t unresolved_static_fields_;
225
226 size_t resolved_methods_[kMaxInvokeType + 1];
227 size_t unresolved_methods_[kMaxInvokeType + 1];
Ian Rogers2ed3b952012-03-17 11:49:39 -0700228 size_t virtual_made_direct_[kMaxInvokeType + 1];
229 size_t direct_calls_to_boot_[kMaxInvokeType + 1];
230 size_t direct_methods_to_boot_[kMaxInvokeType + 1];
Ian Rogersc8b306f2012-02-17 21:34:44 -0800231
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700232 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800233};
Ian Rogers996cc582012-02-14 22:23:29 -0800234
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800235static std::string MakeCompilerSoName(InstructionSet instruction_set) {
236 // TODO: is the ARM/Thumb2 instruction set distinction really buying us anything,
237 // or just causing hassle like this?
238 if (instruction_set == kThumb2) {
239 instruction_set = kArm;
240 }
241
Brian Carlstrom5644f002012-06-27 23:05:17 -0700242 // Lower case the instruction set, because that's what we do in the build system.
Elliott Hughes6fcce302012-06-19 16:54:19 -0700243 std::string instruction_set_name(ToStr<InstructionSet>(instruction_set).str());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800244 for (size_t i = 0; i < instruction_set_name.size(); ++i) {
Brian Carlstrom5644f002012-06-27 23:05:17 -0700245 instruction_set_name[i] = tolower(instruction_set_name[i]);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800246 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700247
248 // Bad things happen if we pull in the libartd-compiler to a libart dex2oat or vice versa,
249 // because we end up with both libart and libartd in the same address space!
Elliott Hughes67d92002012-03-26 15:08:51 -0700250 const char* suffix = (kIsDebugBuild ? "d" : "");
Elliott Hughes059d5c12012-03-12 17:39:18 -0700251
252 // Work out the filename for the compiler library.
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700253#if defined(ART_USE_LLVM_COMPILER)
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800254 std::string library_name(StringPrintf("art%s-compiler-llvm", suffix));
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700255#elif defined(ART_USE_GREENLAND_COMPILER)
256 std::string library_name(StringPrintf("art%s-compiler-greenland", suffix));
257#else
258 std::string library_name(StringPrintf("art%s-compiler-%s", suffix, instruction_set_name.c_str()));
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800259#endif
Elliott Hughes059d5c12012-03-12 17:39:18 -0700260 std::string filename(StringPrintf(OS_SHARED_LIB_FORMAT_STR, library_name.c_str()));
261
262#if defined(__APPLE__)
263 // On Linux, dex2oat will have been built with an RPATH of $ORIGIN/../lib, so dlopen(3) will find
264 // the .so by itself. On Mac OS, there isn't really an equivalent, so we have to manually do the
265 // same work.
Elliott Hughes059d5c12012-03-12 17:39:18 -0700266 uint32_t executable_path_length = 0;
Elliott Hughes448e93c2012-03-28 22:30:06 -0700267 _NSGetExecutablePath(NULL, &executable_path_length);
268 std::string path(executable_path_length, static_cast<char>(0));
269 CHECK_EQ(_NSGetExecutablePath(&path[0], &executable_path_length), 0);
Elliott Hughes059d5c12012-03-12 17:39:18 -0700270
271 // Strip the "/dex2oat".
272 size_t last_slash = path.find_last_of('/');
273 CHECK_NE(last_slash, std::string::npos) << path;
274 path.resize(last_slash);
275
276 // Strip the "/bin".
277 last_slash = path.find_last_of('/');
278 path.resize(last_slash);
279
280 filename = path + "/lib/" + filename;
281#endif
282 return filename;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800283}
284
Elliott Hughes46f060a2012-03-09 17:36:50 -0800285template<typename Fn>
286static Fn FindFunction(const std::string& compiler_so_name, void* library, const char* name) {
287 Fn fn = reinterpret_cast<Fn>(dlsym(library, name));
288 if (fn == NULL) {
289 LOG(FATAL) << "Couldn't find \"" << name << "\" in compiler library " << compiler_so_name << ": " << dlerror();
290 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700291 VLOG(compiler) << "Found \"" << name << "\" at " << reinterpret_cast<void*>(fn);
Elliott Hughes46f060a2012-03-09 17:36:50 -0800292 return fn;
293}
294
Elliott Hughes5523ee02012-02-03 18:18:34 -0800295Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700296 bool support_debugging, const std::set<std::string>* image_classes,
297 bool dump_stats, bool dump_timings)
Brian Carlstromaded5f72011-10-07 17:15:04 -0700298 : instruction_set_(instruction_set),
Elliott Hughesc225caa2012-02-03 15:43:37 -0800299 compiled_classes_lock_("compiled classes lock"),
300 compiled_methods_lock_("compiled method lock"),
301 compiled_invoke_stubs_lock_("compiled invoke stubs lock"),
Logan Chien7a2a23a2012-06-06 11:01:00 +0800302#if defined(ART_USE_LLVM_COMPILER)
303 compiled_proxy_stubs_lock_("compiled proxy stubs lock"),
304#endif
Brian Carlstromaded5f72011-10-07 17:15:04 -0700305 image_(image),
Elliott Hughes5523ee02012-02-03 18:18:34 -0800306 thread_count_(thread_count),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800307 support_debugging_(support_debugging),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 start_ns_(0),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800309 stats_(new AOTCompilationStats),
Brian Carlstromba0668e2012-03-26 13:14:07 -0700310 dump_stats_(dump_stats),
311 dump_timings_(dump_timings),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800312 image_classes_(image_classes),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800313 compiler_library_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800314 compiler_(NULL),
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700315 compiler_context_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800316 jni_compiler_(NULL),
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700317 create_invoke_stub_(NULL),
318 thread_pool_(new ThreadPool(thread_count))
Logan Chien971bf3f2012-05-01 15:47:55 +0800319{
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800320 std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800321 compiler_library_ = dlopen(compiler_so_name.c_str(), RTLD_LAZY);
322 if (compiler_library_ == NULL) {
323 LOG(FATAL) << "Couldn't find compiler library " << compiler_so_name << ": " << dlerror();
324 }
325 VLOG(compiler) << "dlopen(\"" << compiler_so_name << "\", RTLD_LAZY) returned " << compiler_library_;
326
buzbee4df2bbd2012-10-11 14:46:06 -0700327 CHECK_PTHREAD_CALL(pthread_key_create, (&tls_key_, NULL), "compiler tls key");
328
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700329#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chien106b2a02012-03-18 04:41:38 +0800330 // Initialize compiler_context_
331 typedef void (*InitCompilerContextFn)(Compiler&);
332
333 InitCompilerContextFn init_compiler_context =
334 FindFunction<void (*)(Compiler&)>(compiler_so_name,
335 compiler_library_,
336 "ArtInitCompilerContext");
337
338 init_compiler_context(*this);
buzbee692be802012-08-29 15:52:59 -0700339#elif defined(ART_USE_QUICK_COMPILER)
340 // Initialize compiler_context_
341 typedef void (*InitCompilerContextFn)(Compiler&);
342
343 InitCompilerContextFn init_compiler_context =
344 FindFunction<void (*)(Compiler&)>(compiler_so_name,
345 compiler_library_,
346 "ArtInitQuickCompilerContext");
347
348 init_compiler_context(*this);
Logan Chien106b2a02012-03-18 04:41:38 +0800349#endif
350
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700351 compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "ArtCompileMethod");
Elliott Hughes46f060a2012-03-09 17:36:50 -0800352 jni_compiler_ = FindFunction<JniCompilerFn>(compiler_so_name, compiler_library_, "ArtJniCompileMethod");
353 create_invoke_stub_ = FindFunction<CreateInvokeStubFn>(compiler_so_name, compiler_library_, "ArtCreateInvokeStub");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800354
Logan Chienf7015fd2012-03-18 01:19:37 +0800355#if defined(ART_USE_LLVM_COMPILER)
Logan Chien7a2a23a2012-06-06 11:01:00 +0800356 create_proxy_stub_ = FindFunction<CreateProxyStubFn>(
357 compiler_so_name, compiler_library_, "ArtCreateProxyStub");
Logan Chienf7015fd2012-03-18 01:19:37 +0800358#endif
359
Brian Carlstrom25c33252011-09-18 15:58:35 -0700360 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800361 if (!image_) {
362 CHECK(image_classes_ == NULL);
363 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700364}
365
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700366Compiler::~Compiler() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700367 Thread* self = Thread::Current();
Elliott Hughesc225caa2012-02-03 15:43:37 -0800368 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700369 MutexLock mu(self, compiled_classes_lock_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800370 STLDeleteValues(&compiled_classes_);
371 }
372 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700373 MutexLock mu(self, compiled_methods_lock_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800374 STLDeleteValues(&compiled_methods_);
375 }
376 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700377 MutexLock mu(self, compiled_invoke_stubs_lock_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800378 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800379 }
Brian Carlstromd06dfe72012-06-06 22:15:39 -0700380#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700381 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700382 MutexLock mu(self, compiled_proxy_stubs_lock_);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800383 STLDeleteValues(&compiled_proxy_stubs_);
384 }
Brian Carlstromd06dfe72012-06-06 22:15:39 -0700385#endif
Logan Chien7a2a23a2012-06-06 11:01:00 +0800386 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700387 MutexLock mu(self, compiled_methods_lock_);
Brian Carlstromf5822582012-03-19 22:34:31 -0700388 STLDeleteElements(&code_to_patch_);
389 }
390 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700391 MutexLock mu(self, compiled_methods_lock_);
Brian Carlstromf5822582012-03-19 22:34:31 -0700392 STLDeleteElements(&methods_to_patch_);
393 }
Mathieu Chartiered6d5ed2012-10-12 14:51:46 -0700394 CHECK_PTHREAD_CALL(pthread_key_delete, (tls_key_), "delete tls key");
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800395#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800396 // Uninitialize compiler_context_
397 typedef void (*UninitCompilerContextFn)(Compiler&);
398
399 std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
400
401 UninitCompilerContextFn uninit_compiler_context =
402 FindFunction<void (*)(Compiler&)>(compiler_so_name,
403 compiler_library_,
404 "ArtUnInitCompilerContext");
405
406 uninit_compiler_context(*this);
buzbee692be802012-08-29 15:52:59 -0700407#elif defined(ART_USE_QUICK_COMPILER)
408 // Uninitialize compiler_context_
409 typedef void (*UninitCompilerContextFn)(Compiler&);
410
411 std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
412
413 UninitCompilerContextFn uninit_compiler_context =
414 FindFunction<void (*)(Compiler&)>(compiler_so_name,
415 compiler_library_,
416 "ArtUnInitQuickCompilerContext");
417
418 uninit_compiler_context(*this);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800419#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800420 if (compiler_library_ != NULL) {
421 VLOG(compiler) << "dlclose(" << compiler_library_ << ")";
buzbeeca7a5e42012-08-20 11:12:18 -0700422#if !defined(ART_USE_QUICK_COMPILER)
423 /*
424 * FIXME: Temporary workaround
425 * Apparently, llvm is adding dctors to atexit, but if we unload
426 * the library here the code will no longer be around at exit time
427 * and we die a flaming death in __cxa_finalize(). Apparently, some
428 * dlclose() implementations will scan the atexit list on unload and
429 * handle any associated with the soon-to-be-unloaded library.
430 * However, this is not required by POSIX and we don't do it.
431 * See: http://b/issue?id=4998315
432 * What's the right thing to do here?
433 */
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800434 dlclose(compiler_library_);
buzbeeca7a5e42012-08-20 11:12:18 -0700435#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800436 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700437}
438
buzbee4df2bbd2012-10-11 14:46:06 -0700439CompilerTls* Compiler::GetTls() {
440 // Lazily create thread-local storage
441 CompilerTls* res = static_cast<CompilerTls*>(pthread_getspecific(tls_key_));
442 if (res == NULL) {
443 res = new CompilerTls();
444 CHECK_PTHREAD_CALL(pthread_setspecific, (tls_key_, res), "compiler tls");
445 }
446 return res;
447}
448
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700449ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
450 Runtime::TrampolineType type) {
jeffhao7fbee072012-08-24 17:56:54 -0700451 switch (instruction_set) {
452 case kArm:
453 case kThumb2:
454 return arm::ArmCreateResolutionTrampoline(type);
455 case kMips:
456 return mips::MipsCreateResolutionTrampoline(type);
457 case kX86:
458 return x86::X86CreateResolutionTrampoline(type);
459 default:
460 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
461 return NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700462 }
463}
464
Elliott Hughes8add92d2012-01-18 18:18:43 -0800465ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800466 switch (instruction_set) {
467 case kArm:
468 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800469 return arm::CreateJniDlsymLookupStub();
jeffhao7fbee072012-08-24 17:56:54 -0700470 case kMips:
471 return mips::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800472 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800473 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800474 default:
Ian Rogers49c48942012-03-11 15:15:37 -0700475 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Ian Rogers169c9a72011-11-13 20:13:17 -0800476 return NULL;
477 }
478}
479
Ian Rogersad25ac52011-10-04 19:13:33 -0700480ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
jeffhao7fbee072012-08-24 17:56:54 -0700481 switch (instruction_set) {
482 case kArm:
483 case kThumb2:
484 return arm::CreateAbstractMethodErrorStub();
485 case kMips:
486 return mips::CreateAbstractMethodErrorStub();
487 case kX86:
488 return x86::CreateAbstractMethodErrorStub();
489 default:
490 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
491 return NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700492 }
493}
494
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700495void Compiler::CompileAll(jobject class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800496 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700497 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800498
Elliott Hughes601a1232012-02-02 17:47:38 -0800499 TimingLogger timings("compiler");
500
501 PreCompile(class_loader, dex_files, timings);
502
Ian Rogers3d1548d2012-09-24 14:08:03 -0700503 Compile(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800504
Brian Carlstromba0668e2012-03-26 13:14:07 -0700505 if (dump_timings_ && timings.GetTotalNs() > MsToNs(1000)) {
Elliott Hughes601a1232012-02-02 17:47:38 -0800506 timings.Dump();
507 }
Ian Rogers996cc582012-02-14 22:23:29 -0800508
Brian Carlstromba0668e2012-03-26 13:14:07 -0700509 if (dump_stats_) {
510 stats_->Dump();
511 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700512}
513
Mathieu Chartier66f19252012-09-18 08:57:04 -0700514void Compiler::CompileOne(const AbstractMethod* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700515 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700516 Thread* self = Thread::Current();
517 jobject class_loader;
518 const DexCache* dex_cache;
519 const DexFile* dex_file;
520 {
521 ScopedObjectAccessUnchecked soa(self);
522 ScopedLocalRef<jobject>
523 local_class_loader(soa.Env(),
524 soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader()));
525 class_loader = soa.Env()->NewGlobalRef(local_class_loader.get());
526 // Find the dex_file
527 dex_cache = method->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700528 dex_file = dex_cache->GetDexFile();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700529 }
530 self->TransitionFromRunnableToSuspended(kNative);
Brian Carlstromae826982011-11-09 01:33:42 -0800531
Brian Carlstromae826982011-11-09 01:33:42 -0800532 std::vector<const DexFile*> dex_files;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533 dex_files.push_back(dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800534
Elliott Hughes601a1232012-02-02 17:47:38 -0800535 TimingLogger timings("CompileOne");
536 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800537
Ian Rogers0571d352011-11-03 19:51:38 -0700538 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700539 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
Ian Rogers08f753d2012-08-24 14:35:25 -0700540 CompileMethod(code_item, method->GetAccessFlags(), method->GetInvokeType(),
541 method_idx, class_loader, *dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800542
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543 self->GetJniEnv()->DeleteGlobalRef(class_loader);
544
545 self->TransitionFromSuspendedToRunnable();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700546}
547
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700548void Compiler::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
549 TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800550 for (size_t i = 0; i != dex_files.size(); ++i) {
551 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700552 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800553 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700554 }
555}
556
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700557void Compiler::PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
558 TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800559 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800560
Ian Rogers3d1548d2012-09-24 14:08:03 -0700561 Verify(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800562
Ian Rogers3d1548d2012-09-24 14:08:03 -0700563 InitializeClassesWithoutClinit(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800564}
565
566bool Compiler::IsImageClass(const std::string& descriptor) const {
567 if (image_classes_ == NULL) {
568 return true;
569 }
570 return image_classes_->find(descriptor) != image_classes_->end();
571}
572
Ian Rogers3d1548d2012-09-24 14:08:03 -0700573void Compiler::RecordClassStatus(ClassReference ref, CompiledClass* compiled_class) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700574 MutexLock mu(Thread::Current(), Compiler::compiled_classes_lock_);
Ian Rogers3d1548d2012-09-24 14:08:03 -0700575 compiled_classes_.Put(ref, compiled_class);
576}
577
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700578bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800579 uint32_t type_idx) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700580 ScopedObjectAccess soa(Thread::Current());
581 DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800582 if (!IsImage()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800583 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800584 return false;
585 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800586 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800587 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800588 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800589 return false;
590 }
Ian Rogers996cc582012-02-14 22:23:29 -0800591 bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor());
592 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800593 stats_->TypeInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800594 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800595 stats_->TypeNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800596 }
597 return result;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800598}
599
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700600bool Compiler::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800601 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800602 // TODO: Add support for loading strings referenced by image_classes_
603 // See also Compiler::ResolveDexFile
604
605 // The following is a test saying that if we're building the image without a restricted set of
606 // 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 -0700607 bool result = IsImage() && image_classes_ == NULL;
608 if (result) {
609 ScopedObjectAccess soa(Thread::Current());
610 DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
611 result = dex_cache->GetResolvedString(string_idx) != NULL;
612 }
Ian Rogers996cc582012-02-14 22:23:29 -0800613 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800614 stats_->StringInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800615 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800616 stats_->StringNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800617 }
618 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800619}
620
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
622 uint32_t type_idx) {
623 ScopedObjectAccess soa(Thread::Current());
624 DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Ian Rogers1bddec32012-02-04 12:27:34 -0800625 // Get type from dex cache assuming it was populated by the verifier
626 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
627 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800628 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800629 return false; // Unknown class needs access checks.
630 }
631 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
632 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
633 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800634 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800635 return false; // Incomplete referrer knowledge needs access check.
636 }
637 // Perform access check, will return true if access is ok or false if we're going to have to
638 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800639 bool result = referrer_class->CanAccess(resolved_class);
640 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800641 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800642 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800643 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800644 }
645 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800646}
647
648bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
Ian Rogers1bddec32012-02-04 12:27:34 -0800649 const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800650 uint32_t type_idx) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700651 ScopedObjectAccess soa(Thread::Current());
652 DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Ian Rogers1bddec32012-02-04 12:27:34 -0800653 // Get type from dex cache assuming it was populated by the verifier.
654 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
655 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800656 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800657 return false; // Unknown class needs access checks.
658 }
659 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
660 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
661 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800662 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800663 return false; // Incomplete referrer knowledge needs access check.
664 }
665 // Perform access and instantiable checks, will return true if access is ok or false if we're
666 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800667 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
668 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800669 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800670 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800671 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800672 }
673 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800674}
675
Ian Rogers08f753d2012-08-24 14:35:25 -0700676static Class* ComputeCompilingMethodsClass(ScopedObjectAccess& soa,
677 OatCompilationUnit* mUnit)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700678 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_);
680 ClassLoader* class_loader = soa.Decode<ClassLoader*>(mUnit->class_loader_);
681 const DexFile::MethodId& referrer_method_id = mUnit->dex_file_->GetMethodId(mUnit->method_idx_);
682 return mUnit->class_linker_->ResolveType(*mUnit->dex_file_, referrer_method_id.class_idx_,
683 dex_cache, class_loader);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800684}
685
Ian Rogers08f753d2012-08-24 14:35:25 -0700686static Field* ComputeFieldReferencedFromCompilingMethod(ScopedObjectAccess& soa,
687 OatCompilationUnit* mUnit,
688 uint32_t field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700689 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_);
691 ClassLoader* class_loader = soa.Decode<ClassLoader*>(mUnit->class_loader_);
692 return mUnit->class_linker_->ResolveField(*mUnit->dex_file_, field_idx, dex_cache,
693 class_loader, false);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800694}
695
Mathieu Chartier66f19252012-09-18 08:57:04 -0700696static AbstractMethod* ComputeMethodReferencedFromCompilingMethod(ScopedObjectAccess& soa,
Ian Rogers08f753d2012-08-24 14:35:25 -0700697 OatCompilationUnit* mUnit,
698 uint32_t method_idx,
699 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701 DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_);
702 ClassLoader* class_loader = soa.Decode<ClassLoader*>(mUnit->class_loader_);
703 return mUnit->class_linker_->ResolveMethod(*mUnit->dex_file_, method_idx, dex_cache,
jeffhaoc0228b82012-08-29 18:15:05 -0700704 class_loader, NULL, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800705}
706
Logan Chien4dd96f52012-02-29 01:26:58 +0800707bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
jeffhao8cd6dda2012-02-22 10:15:34 -0800708 int& field_offset, bool& is_volatile, bool is_put) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700709 ScopedObjectAccess soa(Thread::Current());
Ian Rogers08f753d2012-08-24 14:35:25 -0700710 // Conservative defaults.
Ian Rogers1bddec32012-02-04 12:27:34 -0800711 field_offset = -1;
712 is_volatile = true;
Ian Rogers08f753d2012-08-24 14:35:25 -0700713 // Try to resolve field and ignore if an Incompatible Class Change Error (ie is static).
714 Field* resolved_field = ComputeFieldReferencedFromCompilingMethod(soa, mUnit, field_idx);
715 if (resolved_field != NULL && !resolved_field->IsStatic()) {
716 Class* referrer_class = ComputeCompilingMethodsClass(soa, mUnit);
Ian Rogerse2645d32012-04-11 14:42:42 -0700717 if (referrer_class != NULL) {
718 Class* fields_class = resolved_field->GetDeclaringClass();
719 bool access_ok = referrer_class->CanAccess(fields_class) &&
720 referrer_class->CanAccessMember(fields_class,
721 resolved_field->GetAccessFlags());
722 if (!access_ok) {
723 // The referring class can't access the resolved field, this may occur as a result of a
724 // protected field being made public by a sub-class. Resort to the dex file to determine
725 // the correct class for the access check.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700726 const DexFile& dex_file = *referrer_class->GetDexCache()->GetDexFile();
Ian Rogerse2645d32012-04-11 14:42:42 -0700727 Class* dex_fields_class = mUnit->class_linker_->ResolveType(dex_file,
728 dex_file.GetFieldId(field_idx).class_idx_,
729 referrer_class);
730 access_ok = referrer_class->CanAccess(dex_fields_class) &&
731 referrer_class->CanAccessMember(dex_fields_class,
732 resolved_field->GetAccessFlags());
733 }
734 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
735 fields_class != referrer_class;
736 if (access_ok && !is_write_to_final_from_wrong_class) {
737 field_offset = resolved_field->GetOffset().Int32Value();
738 is_volatile = resolved_field->IsVolatile();
739 stats_->ResolvedInstanceField();
740 return true; // Fast path.
741 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800742 }
743 }
744 // Clean up any exception left by field/type resolution
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700745 if (soa.Self()->IsExceptionPending()) {
746 soa.Self()->ClearException();
Ian Rogers1bddec32012-02-04 12:27:34 -0800747 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800748 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800749 return false; // Incomplete knowledge needs slow path.
750}
751
Logan Chien4dd96f52012-02-29 01:26:58 +0800752bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
Ian Rogers1bddec32012-02-04 12:27:34 -0800753 int& field_offset, int& ssb_index,
jeffhao8cd6dda2012-02-22 10:15:34 -0800754 bool& is_referrers_class, bool& is_volatile, bool is_put) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700755 ScopedObjectAccess soa(Thread::Current());
Ian Rogers08f753d2012-08-24 14:35:25 -0700756 // Conservative defaults.
Ian Rogers1bddec32012-02-04 12:27:34 -0800757 field_offset = -1;
758 ssb_index = -1;
759 is_referrers_class = false;
760 is_volatile = true;
Ian Rogers08f753d2012-08-24 14:35:25 -0700761 // Try to resolve field and ignore if an Incompatible Class Change Error (ie isn't static).
762 Field* resolved_field = ComputeFieldReferencedFromCompilingMethod(soa, mUnit, field_idx);
763 if (resolved_field != NULL && resolved_field->IsStatic()) {
764 Class* referrer_class = ComputeCompilingMethodsClass(soa, mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800765 if (referrer_class != NULL) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800766 Class* fields_class = resolved_field->GetDeclaringClass();
767 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800768 is_referrers_class = true; // implies no worrying about class initialization
769 field_offset = resolved_field->GetOffset().Int32Value();
770 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800771 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800772 return true; // fast path
773 } else {
Ian Rogerse2645d32012-04-11 14:42:42 -0700774 bool access_ok = referrer_class->CanAccess(fields_class) &&
775 referrer_class->CanAccessMember(fields_class,
776 resolved_field->GetAccessFlags());
777 if (!access_ok) {
778 // The referring class can't access the resolved field, this may occur as a result of a
779 // protected field being made public by a sub-class. Resort to the dex file to determine
780 // the correct class for the access check. Don't change the field's class as that is
781 // used to identify the SSB.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700782 const DexFile& dex_file = *referrer_class->GetDexCache()->GetDexFile();
Ian Rogerse2645d32012-04-11 14:42:42 -0700783 Class* dex_fields_class =
784 mUnit->class_linker_->ResolveType(dex_file,
785 dex_file.GetFieldId(field_idx).class_idx_,
786 referrer_class);
787 access_ok = referrer_class->CanAccess(dex_fields_class) &&
788 referrer_class->CanAccessMember(dex_fields_class,
789 resolved_field->GetAccessFlags());
790 }
jeffhao8cd6dda2012-02-22 10:15:34 -0800791 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogerse2645d32012-04-11 14:42:42 -0700792 if (access_ok && !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800793 // We have the resolved field, we must make it into a ssbIndex for the referrer
794 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800795 // TODO: for images we can elide the static storage base null check
796 // if we know there's a non-null entry in the image
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700797 DexCache* dex_cache = mUnit->class_linker_->FindDexCache(*mUnit->dex_file_);
798 if (fields_class->GetDexCache() == dex_cache) {
Ian Rogers4103ad22012-02-06 09:18:25 -0800799 // common case where the dex cache of both the referrer and the field are the same,
800 // no need to search the dex file
801 ssb_index = fields_class->GetDexTypeIndex();
802 field_offset = resolved_field->GetOffset().Int32Value();
803 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800804 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800805 return true;
806 }
Ian Rogerse2645d32012-04-11 14:42:42 -0700807 // Search dex file for localized ssb index, may fail if field's class is a parent
808 // of the class mentioned in the dex file and there is no dex cache entry.
Ian Rogers1bddec32012-02-04 12:27:34 -0800809 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
810 const DexFile::StringId* string_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800811 mUnit->dex_file_->FindStringId(descriptor);
Ian Rogers1bddec32012-02-04 12:27:34 -0800812 if (string_id != NULL) {
813 const DexFile::TypeId* type_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800814 mUnit->dex_file_->FindTypeId(mUnit->dex_file_->GetIndexForStringId(*string_id));
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700815 if (type_id != NULL) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800816 // medium path, needs check of static storage base being initialized
Logan Chien4dd96f52012-02-29 01:26:58 +0800817 ssb_index = mUnit->dex_file_->GetIndexForTypeId(*type_id);
Ian Rogers1bddec32012-02-04 12:27:34 -0800818 field_offset = resolved_field->GetOffset().Int32Value();
819 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800820 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800821 return true;
822 }
823 }
824 }
825 }
826 }
827 }
828 // Clean up any exception left by field/type resolution
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700829 if (soa.Self()->IsExceptionPending()) {
830 soa.Self()->ClearException();
Ian Rogers1bddec32012-02-04 12:27:34 -0800831 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800832 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800833 return false; // Incomplete knowledge needs slow path.
834}
835
Mathieu Chartier66f19252012-09-18 08:57:04 -0700836void Compiler::GetCodeAndMethodForDirectCall(InvokeType type, InvokeType sharp_type, AbstractMethod* method,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700837 uintptr_t& direct_code, uintptr_t& direct_method) {
Ian Rogers137e88f2012-10-08 17:46:47 -0700838 // For direct and static methods compute possible direct_code and direct_method values, ie
839 // an address for the Method* being invoked and an address of the code for that Method*.
840 // For interface calls compute a value for direct_method that is the interface method being
841 // invoked, so this can be passed to the out-of-line runtime support code.
Ian Rogers2ed3b952012-03-17 11:49:39 -0700842 direct_code = 0;
843 direct_method = 0;
TDYa127b8404a72012-10-15 10:32:44 -0700844#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers137e88f2012-10-08 17:46:47 -0700845 if (sharp_type != kStatic && sharp_type != kDirect && sharp_type != kInterface) {
Ian Rogers2ed3b952012-03-17 11:49:39 -0700846 return;
847 }
TDYa127b8404a72012-10-15 10:32:44 -0700848#else
849 if (sharp_type != kStatic && sharp_type != kDirect) {
850 return;
851 }
852#endif
Ian Rogers2ed3b952012-03-17 11:49:39 -0700853 bool method_code_in_boot = method->GetDeclaringClass()->GetClassLoader() == NULL;
854 if (!method_code_in_boot) {
855 return;
856 }
857 bool has_clinit_trampoline = method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
858 if (has_clinit_trampoline) {
859 return;
860 }
Ian Rogersc468e922012-10-10 18:11:33 -0700861 if (sharp_type != kInterface) { // Interfaces always go via a trampoline.
862 stats_->DirectCallsToBoot(type);
863 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700864 stats_->DirectMethodsToBoot(type);
Ian Rogers3fa13792012-03-18 15:53:45 -0700865 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
866 if (compiling_boot) {
Brian Carlstrom0637e272012-03-20 01:07:52 -0700867 const bool kSupportBootImageFixup = true;
Ian Rogers3fa13792012-03-18 15:53:45 -0700868 if (kSupportBootImageFixup) {
869 MethodHelper mh(method);
870 if (IsImageClass(mh.GetDeclaringClassDescriptor())) {
Brian Carlstrom0637e272012-03-20 01:07:52 -0700871 // We can only branch directly to Methods that are resolved in the DexCache.
872 // Otherwise we won't invoke the resolution trampoline.
Ian Rogers3fa13792012-03-18 15:53:45 -0700873 direct_method = -1;
Brian Carlstrom0637e272012-03-20 01:07:52 -0700874 direct_code = -1;
Ian Rogers3fa13792012-03-18 15:53:45 -0700875 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700876 }
877 } else {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700878 if (Runtime::Current()->GetHeap()->FindSpaceFromObject(method)->IsImageSpace()) {
Ian Rogers3fa13792012-03-18 15:53:45 -0700879 direct_method = reinterpret_cast<uintptr_t>(method);
880 }
881 direct_code = reinterpret_cast<uintptr_t>(method->GetCode());
Ian Rogers2ed3b952012-03-17 11:49:39 -0700882 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700883}
884
Ian Rogersfb6adba2012-03-04 21:51:51 -0800885bool Compiler::ComputeInvokeInfo(uint32_t method_idx, OatCompilationUnit* mUnit, InvokeType& type,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700886 int& vtable_idx, uintptr_t& direct_code,
887 uintptr_t& direct_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 ScopedObjectAccess soa(Thread::Current());
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800889 vtable_idx = -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700890 direct_code = 0;
891 direct_method = 0;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700892 AbstractMethod* resolved_method =
Ian Rogers08f753d2012-08-24 14:35:25 -0700893 ComputeMethodReferencedFromCompilingMethod(soa, mUnit, method_idx, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800894 if (resolved_method != NULL) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700895 // Don't try to fast-path if we don't understand the caller's class or this appears to be an
896 // Incompatible Class Change Error.
897 Class* referrer_class = ComputeCompilingMethodsClass(soa, mUnit);
898 bool icce = resolved_method->CheckIncompatibleClassChange(type);
899 if (referrer_class != NULL && !icce) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800900 Class* methods_class = resolved_method->GetDeclaringClass();
901 if (!referrer_class->CanAccess(methods_class) ||
902 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800903 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800904 // The referring class can't access the resolved method, this may occur as a result of a
905 // protected method being made public by implementing an interface that re-declares the
Ian Rogers08f753d2012-08-24 14:35:25 -0700906 // method public. Resort to the dex file to determine the correct class for the access
907 // check.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700908 const DexFile& dex_file = *referrer_class->GetDexCache()->GetDexFile();
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800909 methods_class =
Logan Chien4dd96f52012-02-29 01:26:58 +0800910 mUnit->class_linker_->ResolveType(dex_file,
911 dex_file.GetMethodId(method_idx).class_idx_,
912 referrer_class);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800913 }
914 if (referrer_class->CanAccess(methods_class) &&
Ian Rogers137e88f2012-10-08 17:46:47 -0700915 referrer_class->CanAccessMember(methods_class, resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800916 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogersf320b632012-03-13 18:47:47 -0700917 const bool kEnableSharpening = true;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700918 // Sharpen a virtual call into a direct call when the target is known.
919 bool can_sharpen = type == kVirtual && (resolved_method->IsFinal() ||
Ian Rogers08f753d2012-08-24 14:35:25 -0700920 methods_class->IsFinal());
921 // Ensure the vtable index will be correct to dispatch in the vtable of the super class.
jeffhao4155fcd2012-03-21 11:42:12 -0700922 can_sharpen = can_sharpen || (type == kSuper && referrer_class != methods_class &&
Ian Rogers08f753d2012-08-24 14:35:25 -0700923 referrer_class->IsSubClass(methods_class) &&
924 vtable_idx < methods_class->GetVTable()->GetLength() &&
925 methods_class->GetVTable()->Get(vtable_idx) == resolved_method);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700926 if (kEnableSharpening && can_sharpen) {
927 stats_->ResolvedMethod(type);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800928 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
929 // dex cache, check that this resolved method is where we expect it.
930 CHECK(referrer_class->GetDexCache()->GetResolvedMethod(method_idx) == resolved_method)
Ian Rogers08f753d2012-08-24 14:35:25 -0700931 << PrettyMethod(resolved_method);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700932 stats_->VirtualMadeDirect(type);
933 GetCodeAndMethodForDirectCall(type, kDirect, resolved_method, direct_code, direct_method);
934 type = kDirect;
935 return true;
936 } else if (type == kSuper) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700937 // Unsharpened super calls are suspicious so go slow-path.
Ian Rogers2ed3b952012-03-17 11:49:39 -0700938 } else {
939 stats_->ResolvedMethod(type);
940 GetCodeAndMethodForDirectCall(type, type, resolved_method, direct_code, direct_method);
941 return true;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800942 }
943 }
944 }
945 }
946 // Clean up any exception left by method/type resolution
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 if (soa.Self()->IsExceptionPending()) {
948 soa.Self()->ClearException();
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800949 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800950 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800951 return false; // Incomplete knowledge needs slow path.
952}
953
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954void Compiler::AddCodePatch(const DexFile* dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -0700955 uint32_t referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700956 InvokeType referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700957 uint32_t target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700958 InvokeType target_invoke_type,
Ian Rogers3fa13792012-03-18 15:53:45 -0700959 size_t literal_offset) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700960 MutexLock mu(Thread::Current(), compiled_methods_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 code_to_patch_.push_back(new PatchInformation(dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -0700962 referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700963 referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700964 target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700965 target_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700966 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -0700967}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968void Compiler::AddMethodPatch(const DexFile* dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -0700969 uint32_t referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700970 InvokeType referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700971 uint32_t target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700972 InvokeType target_invoke_type,
Ian Rogers3fa13792012-03-18 15:53:45 -0700973 size_t literal_offset) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700974 MutexLock mu(Thread::Current(), compiled_methods_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 methods_to_patch_.push_back(new PatchInformation(dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -0700976 referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700977 referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700978 target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700979 target_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -0700980 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -0700981}
982
Ian Rogers0399dde2012-06-06 17:09:28 -0700983class CompilationContext {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800984 public:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700985 typedef void Callback(const CompilationContext* context, size_t index);
986
Ian Rogers0399dde2012-06-06 17:09:28 -0700987 CompilationContext(ClassLinker* class_linker,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700988 jobject class_loader,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800989 Compiler* compiler,
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700990 const DexFile* dex_file,
991 ThreadPool* thread_pool)
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800992 : class_linker_(class_linker),
993 class_loader_(class_loader),
994 compiler_(compiler),
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700995 dex_file_(dex_file),
996 thread_pool_(thread_pool) {}
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800997
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 ClassLinker* GetClassLinker() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800999 CHECK(class_linker_ != NULL);
1000 return class_linker_;
1001 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002
1003 jobject GetClassLoader() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001004 return class_loader_;
1005 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001006
1007 Compiler* GetCompiler() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001008 CHECK(compiler_ != NULL);
1009 return compiler_;
1010 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011
1012 const DexFile* GetDexFile() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001013 CHECK(dex_file_ != NULL);
1014 return dex_file_;
1015 }
1016
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001017 void ForAll(size_t begin, size_t end, Callback callback, size_t work_units) {
1018 Thread* self = Thread::Current();
1019 self->AssertNoPendingException();
1020 CHECK_GT(work_units, 0U);
1021
1022 std::vector<Closure*> closures(work_units);
1023 for (size_t i = 0; i < work_units; ++i) {
1024 closures[i] = new ForAllClosure(this, begin + i, end, callback, work_units);
1025 thread_pool_->AddTask(self, closures[i]);
1026 }
1027 thread_pool_->StartWorkers(self);
1028
1029 // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
1030 // thread destructor's called below perform join).
1031 CHECK_NE(self->GetState(), kRunnable);
1032
1033 // Wait for all the worker threads to finish.
1034 thread_pool_->Wait(self);
1035
1036 STLDeleteElements(&closures);
1037 }
1038
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001039 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001040
1041 class ForAllClosure : public Closure {
1042 public:
1043 ForAllClosure(CompilationContext* context, size_t begin, size_t end, Callback* callback,
1044 size_t stripe)
1045 : context_(context),
1046 begin_(begin),
1047 end_(end),
1048 callback_(callback),
1049 stripe_(stripe)
1050 {
1051
1052 }
1053
1054 virtual void Run(Thread* self) {
1055 for (size_t i = begin_; i < end_; i += stripe_) {
1056 callback_(context_, i);
1057 self->AssertNoPendingException();
1058 }
1059 }
1060 private:
1061 CompilationContext* const context_;
1062 const size_t begin_;
1063 const size_t end_;
1064 const Callback* callback_;
1065 const size_t stripe_;
1066 };
1067
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 ClassLinker* const class_linker_;
1069 const jobject class_loader_;
1070 Compiler* const compiler_;
1071 const DexFile* const dex_file_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001072 ThreadPool* thread_pool_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001073};
1074
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001075// Return true if the class should be skipped during compilation. We
1076// never skip classes in the boot class loader. However, if we have a
1077// non-boot class loader and we can resolve the class in the boot
1078// class loader, we do skip the class. This happens if an app bundles
1079// classes found in the boot classpath. Since at runtime we will
1080// select the class from the boot classpath, do not attempt to resolve
1081// or compile it now.
1082static bool SkipClass(ClassLoader* class_loader,
1083 const DexFile& dex_file,
1084 const DexFile::ClassDef& class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 if (class_loader == NULL) {
1087 return false;
1088 }
1089 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1090 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1091 Class* klass = class_linker->FindClass(descriptor, NULL);
1092 if (klass == NULL) {
1093 Thread* self = Thread::Current();
1094 CHECK(self->IsExceptionPending());
1095 self->ClearException();
1096 return false;
1097 }
1098 return true;
1099}
1100
1101static void ResolveClassFieldsAndMethods(const CompilationContext* context, size_t class_def_index)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001102 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 ScopedObjectAccess soa(Thread::Current());
1104 ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader());
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001105 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001106
1107 // Method and Field are the worst. We can't resolve without either
1108 // context from the code use (to disambiguate virtual vs direct
1109 // method and instance vs static field) or from class
1110 // definitions. While the compiler will resolve what it can as it
1111 // needs it, here we try to resolve fields and methods used in class
1112 // definitions, since many of them many never be referenced by
1113 // generated code.
1114 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 if (SkipClass(class_loader, dex_file, class_def)) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001116 return;
1117 }
1118
1119 // Note the class_data pointer advances through the headers,
1120 // static fields, instance fields, direct methods, and virtual
1121 // methods.
1122 const byte* class_data = dex_file.GetClassData(class_def);
1123 if (class_data == NULL) {
1124 // empty class such as a marker interface
1125 return;
1126 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001127 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001128 ClassLinker* class_linker = context->GetClassLinker();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001129 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1130 ClassDataItemIterator it(dex_file, class_data);
1131 while (it.HasNextStaticField()) {
1132 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 class_loader, true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001134 if (field == NULL) {
1135 CHECK(self->IsExceptionPending());
1136 self->ClearException();
1137 }
1138 it.Next();
1139 }
1140 while (it.HasNextInstanceField()) {
1141 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 class_loader, false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001143 if (field == NULL) {
1144 CHECK(self->IsExceptionPending());
1145 self->ClearException();
1146 }
1147 it.Next();
1148 }
1149 while (it.HasNextDirectMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001150 AbstractMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
jeffhaoc0228b82012-08-29 18:15:05 -07001151 class_loader, NULL, it.GetMethodInvokeType(class_def));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001152 if (method == NULL) {
1153 CHECK(self->IsExceptionPending());
1154 self->ClearException();
1155 }
1156 it.Next();
1157 }
1158 while (it.HasNextVirtualMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001159 AbstractMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
jeffhaoc0228b82012-08-29 18:15:05 -07001160 class_loader, NULL, it.GetMethodInvokeType(class_def));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001161 if (method == NULL) {
1162 CHECK(self->IsExceptionPending());
1163 self->ClearException();
1164 }
1165 it.Next();
1166 }
1167 DCHECK(!it.HasNext());
1168}
1169
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001170static void ResolveType(const CompilationContext* context, size_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001171 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001172 // Class derived values are more complicated, they require the linker and loader.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173 ScopedObjectAccess soa(Thread::Current());
1174 ClassLinker* class_linker = context->GetClassLinker();
1175 const DexFile& dex_file = *context->GetDexFile();
1176 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1177 ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader());
1178 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
1179
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001180 if (klass == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 CHECK(soa.Self()->IsExceptionPending());
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001182 Thread::Current()->ClearException();
1183 }
1184}
1185
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186void Compiler::ResolveDexFile(jobject class_loader, const DexFile& dex_file,
1187 TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001188 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1189
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 // TODO: we could resolve strings here, although the string table is largely filled with class
1191 // and method names.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001192
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001193 CompilationContext context(class_linker, class_loader, this, &dex_file, thread_pool_.get());
1194 context.ForAll(0, dex_file.NumTypeIds(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001195 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -07001196
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001197 context.ForAll(0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001198 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001199}
1200
Ian Rogers3d1548d2012-09-24 14:08:03 -07001201void Compiler::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1202 TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -08001203 for (size_t i = 0; i != dex_files.size(); ++i) {
1204 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -07001205 CHECK(dex_file != NULL);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001206 VerifyDexFile(class_loader, *dex_file, timings);
jeffhao98eacac2011-09-14 16:11:53 -07001207 }
1208}
1209
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001210static void VerifyClass(const CompilationContext* context, size_t class_def_index)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001211 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001213 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
1214 const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001215 Class* klass =
1216 context->GetClassLinker()->FindClass(descriptor,
1217 soa.Decode<ClassLoader*>(context->GetClassLoader()));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001218 if (klass == NULL) {
1219 Thread* self = Thread::Current();
1220 CHECK(self->IsExceptionPending());
1221 self->ClearException();
jeffhaof56197c2012-03-05 18:01:54 -08001222
1223 /*
1224 * At compile time, we can still structurally verify the class even if FindClass fails.
1225 * This is to ensure the class is structurally sound for compilation. An unsound class
1226 * will be rejected by the verifier and later skipped during compilation in the compiler.
1227 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 DexCache* dex_cache = context->GetClassLinker()->FindDexCache(*context->GetDexFile());
jeffhaof56197c2012-03-05 18:01:54 -08001229 std::string error_msg;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 if (verifier::MethodVerifier::VerifyClass(context->GetDexFile(),
1231 dex_cache,
1232 soa.Decode<ClassLoader*>(context->GetClassLoader()),
1233 class_def_index, error_msg) ==
1234 verifier::MethodVerifier::kHardFailure) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001235 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
jeffhaof56197c2012-03-05 18:01:54 -08001236 LOG(ERROR) << "Verification failed on class "
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001237 << PrettyDescriptor(context->GetDexFile()->GetClassDescriptor(class_def))
jeffhaof56197c2012-03-05 18:01:54 -08001238 << " because: " << error_msg;
1239 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001240 return;
1241 }
1242 CHECK(klass->IsResolved()) << PrettyClass(klass);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001243 context->GetClassLinker()->VerifyClass(klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001244
1245 if (klass->IsErroneous()) {
1246 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1247 CHECK(Thread::Current()->IsExceptionPending());
1248 Thread::Current()->ClearException();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001249 }
1250
Ian Rogers9ffb0392012-09-10 11:56:50 -07001251 CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous())
1252 << PrettyDescriptor(klass) << ": state=" << klass->GetStatus();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001253 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
1254}
1255
Ian Rogers3d1548d2012-09-24 14:08:03 -07001256void Compiler::VerifyDexFile(jobject class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001257 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001258 CompilationContext context(class_linker, class_loader, this, &dex_file, thread_pool_.get());
1259 context.ForAll(0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001260 timings.AddSplit("Verify " + dex_file.GetLocation());
1261}
Elliott Hughesd9cdfe92011-10-06 16:09:04 -07001262
Ian Rogers3d1548d2012-09-24 14:08:03 -07001263static void InitializeClassWithoutClinit(const CompilationContext* context,
1264 size_t class_def_index)
1265 LOCKS_EXCLUDED(Locks::mutator_lock_) {
1266 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
1267 ScopedObjectAccess soa(Thread::Current());
1268 ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader());
1269 const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def);
1270 Class* klass = context->GetClassLinker()->FindClass(descriptor, class_loader);
Ian Rogers1f539342012-10-03 21:09:42 -07001271 Thread* self = Thread::Current();
Ian Rogers3d1548d2012-09-24 14:08:03 -07001272 if (klass != NULL) {
Ian Rogers1f539342012-10-03 21:09:42 -07001273 ObjectLock lock(self, klass);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001274 if (klass->IsVerified()) {
1275 // Only try to initialize classes that were successfully verified.
1276 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
1277 bool can_init_static_fields = compiling_boot &&
1278 context->GetCompiler()->IsImageClass(descriptor);
1279 context->GetClassLinker()->EnsureInitialized(klass, false, can_init_static_fields);
1280 // If successfully initialized place in SSB array.
1281 if (klass->IsInitialized()) {
1282 klass->GetDexCache()->GetInitializedStaticStorage()->Set(klass->GetDexTypeIndex(), klass);
1283 }
1284 }
1285 // Record the final class status if necessary.
1286 Class::Status status = klass->GetStatus();
1287 Compiler::ClassReference ref(context->GetDexFile(), class_def_index);
1288 CompiledClass* compiled_class = context->GetCompiler()->GetCompiledClass(ref);
1289 if (compiled_class == NULL) {
1290 compiled_class = new CompiledClass(status);
1291 context->GetCompiler()->RecordClassStatus(ref, compiled_class);
1292 } else {
1293 DCHECK_EQ(status, compiled_class->GetStatus());
1294 }
1295 }
Ian Rogers1f539342012-10-03 21:09:42 -07001296 // Clear any class not found or verification exceptions.
1297 self->ClearException();
Ian Rogers3d1548d2012-09-24 14:08:03 -07001298}
1299
1300void Compiler::InitializeClassesWithoutClinit(jobject jni_class_loader, const DexFile& dex_file,
1301 TimingLogger& timings) {
1302 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001303 CompilationContext context(class_linker, jni_class_loader, this, &dex_file, thread_pool_.get());
1304 context.ForAll(0, dex_file.NumClassDefs(), InitializeClassWithoutClinit, thread_count_);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001305 timings.AddSplit("InitializeNoClinit " + dex_file.GetLocation());
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001306}
1307
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001308void Compiler::InitializeClassesWithoutClinit(jobject class_loader,
Ian Rogers3d1548d2012-09-24 14:08:03 -07001309 const std::vector<const DexFile*>& dex_files,
1310 TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -08001311 for (size_t i = 0; i != dex_files.size(); ++i) {
1312 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001313 CHECK(dex_file != NULL);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001314 InitializeClassesWithoutClinit(class_loader, *dex_file, timings);
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001315 }
1316}
1317
Ian Rogers3d1548d2012-09-24 14:08:03 -07001318void Compiler::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1319 TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -08001320 for (size_t i = 0; i != dex_files.size(); ++i) {
1321 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07001322 CHECK(dex_file != NULL);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001323 CompileDexFile(class_loader, *dex_file, timings);
Brian Carlstrom83db7722011-08-26 17:32:56 -07001324 }
1325}
1326
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001327void Compiler::CompileClass(const CompilationContext* context, size_t class_def_index) {
1328 jobject class_loader = context->GetClassLoader();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001329 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesc225caa2012-02-03 15:43:37 -08001330 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 {
1332 ScopedObjectAccess soa(Thread::Current());
1333 ClassLoader* class_loader = soa.Decode<ClassLoader*>(context->GetClassLoader());
1334 if (SkipClass(class_loader, dex_file, class_def)) {
1335 return;
1336 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001337 }
jeffhaod1224c72012-02-29 13:43:08 -08001338 ClassReference ref(&dex_file, class_def_index);
1339 // Skip compiling classes with generic verifier failures since they will still fail at runtime
Ian Rogers776ac1f2012-04-13 23:36:36 -07001340 if (verifier::MethodVerifier::IsClassRejected(ref)) {
jeffhaod1224c72012-02-29 13:43:08 -08001341 return;
1342 }
Ian Rogers0571d352011-11-03 19:51:38 -07001343 const byte* class_data = dex_file.GetClassData(class_def);
1344 if (class_data == NULL) {
1345 // empty class, probably a marker interface
1346 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001347 }
Ian Rogers0571d352011-11-03 19:51:38 -07001348 ClassDataItemIterator it(dex_file, class_data);
1349 // Skip fields
1350 while (it.HasNextStaticField()) {
1351 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001352 }
Ian Rogers0571d352011-11-03 19:51:38 -07001353 while (it.HasNextInstanceField()) {
1354 it.Next();
1355 }
1356 // Compile direct methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001357 int64_t previous_direct_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07001358 while (it.HasNextDirectMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001359 uint32_t method_idx = it.GetMemberIndex();
1360 if (method_idx == previous_direct_method_idx) {
1361 // smali can create dex files with two encoded_methods sharing the same method_idx
1362 // http://code.google.com/p/smali/issues/detail?id=119
1363 it.Next();
1364 continue;
1365 }
1366 previous_direct_method_idx = method_idx;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001367 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Ian Rogers08f753d2012-08-24 14:35:25 -07001368 it.GetMethodInvokeType(class_def), method_idx,
1369 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001370 it.Next();
1371 }
1372 // Compile virtual methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001373 int64_t previous_virtual_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07001374 while (it.HasNextVirtualMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001375 uint32_t method_idx = it.GetMemberIndex();
1376 if (method_idx == previous_virtual_method_idx) {
1377 // smali can create dex files with two encoded_methods sharing the same method_idx
1378 // http://code.google.com/p/smali/issues/detail?id=119
1379 it.Next();
1380 continue;
1381 }
1382 previous_virtual_method_idx = method_idx;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001383 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Ian Rogers08f753d2012-08-24 14:35:25 -07001384 it.GetMethodInvokeType(class_def), method_idx,
1385 class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001386 it.Next();
1387 }
1388 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001389}
1390
Ian Rogers3d1548d2012-09-24 14:08:03 -07001391void Compiler::CompileDexFile(jobject class_loader, const DexFile& dex_file,
1392 TimingLogger& timings) {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001393 CompilationContext context(NULL, class_loader, this, &dex_file, thread_pool_.get());
1394 context.ForAll(0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001395 timings.AddSplit("Compile " + dex_file.GetLocation());
Elliott Hughesc225caa2012-02-03 15:43:37 -08001396}
1397
Elliott Hughesa0e18062012-04-13 15:59:59 -07001398static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1399 std::string key(shorty);
1400 if (is_static) {
1401 key += "$"; // Must not be a shorty type character.
1402 }
1403 return key;
1404}
1405
Ian Rogersa3760aa2011-11-14 14:32:37 -08001406void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
Ian Rogers08f753d2012-08-24 14:35:25 -07001407 InvokeType invoke_type, uint32_t method_idx, jobject class_loader,
Ian Rogersa3760aa2011-11-14 14:32:37 -08001408 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -07001409 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001410 uint64_t start_ns = NanoTime();
Logan Chien4dd96f52012-02-29 01:26:58 +08001411
Ian Rogers169c9a72011-11-13 20:13:17 -08001412 if ((access_flags & kAccNative) != 0) {
Ian Rogers57b86d42012-03-27 16:05:41 -07001413 compiled_method = (*jni_compiler_)(*this, access_flags, method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001414 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -08001415 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001416 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -07001417 compiled_method = (*compiler_)(*this, code_item, access_flags, invoke_type, method_idx,
1418 class_loader, dex_file);
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001419 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
1420 }
Ian Rogers3bb17a62012-01-27 23:56:44 -08001421 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -08001422 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001423 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -08001424 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -07001425 }
1426
Ian Rogers50b35e22012-10-04 10:09:15 -07001427 Thread* self = Thread::Current();
Elliott Hughesf09afe82011-10-16 14:24:21 -07001428 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -07001429 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001430 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001431 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001432 MutexLock mu(self, compiled_methods_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001433 compiled_methods_.Put(ref, compiled_method);
1434 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001435 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001436 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001437
Ian Rogers45619fc2012-02-29 11:15:25 -08001438 uint32_t shorty_len;
1439 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx), &shorty_len);
Ian Rogers169c9a72011-11-13 20:13:17 -08001440 bool is_static = (access_flags & kAccStatic) != 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001441 std::string key(MakeInvokeStubKey(is_static, shorty));
1442 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(key);
Ian Rogers0571d352011-11-03 19:51:38 -07001443 if (compiled_invoke_stub == NULL) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -08001444 compiled_invoke_stub = (*create_invoke_stub_)(*this, is_static, shorty, shorty_len);
Ian Rogers0571d352011-11-03 19:51:38 -07001445 CHECK(compiled_invoke_stub != NULL);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001446 InsertInvokeStub(key, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001447 }
Logan Chien7a2a23a2012-06-06 11:01:00 +08001448
1449#if defined(ART_USE_LLVM_COMPILER)
1450 if (!is_static) {
1451 const CompiledInvokeStub* compiled_proxy_stub = FindProxyStub(shorty);
1452 if (compiled_proxy_stub == NULL) {
1453 compiled_proxy_stub = (*create_proxy_stub_)(*this, shorty, shorty_len);
1454 CHECK(compiled_proxy_stub != NULL);
1455 InsertProxyStub(shorty, compiled_proxy_stub);
1456 }
1457 }
1458#endif
1459
Ian Rogers50b35e22012-10-04 10:09:15 -07001460 if (self->IsExceptionPending()) {
1461 ScopedObjectAccess soa(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001462 LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n"
Ian Rogers50b35e22012-10-04 10:09:15 -07001463 << self->GetException()->Dump();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001464 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001465}
1466
Elliott Hughesa0e18062012-04-13 15:59:59 -07001467const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
1468 const std::string key(MakeInvokeStubKey(is_static, shorty));
1469 return FindInvokeStub(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001470}
1471
Elliott Hughesa0e18062012-04-13 15:59:59 -07001472const CompiledInvokeStub* Compiler::FindInvokeStub(const std::string& key) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07001473 MutexLock mu(Thread::Current(), compiled_invoke_stubs_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001474 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001475 if (it == compiled_invoke_stubs_.end()) {
1476 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001477 } else {
1478 DCHECK(it->second != NULL);
1479 return it->second;
1480 }
1481}
1482
Elliott Hughesa0e18062012-04-13 15:59:59 -07001483void Compiler::InsertInvokeStub(const std::string& key,
Ian Rogers0571d352011-11-03 19:51:38 -07001484 const CompiledInvokeStub* compiled_invoke_stub) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001485 MutexLock mu(Thread::Current(), compiled_invoke_stubs_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001486 InvokeStubTable::iterator it = compiled_invoke_stubs_.find(key);
1487 if (it != compiled_invoke_stubs_.end()) {
1488 // Someone else won the race.
1489 delete compiled_invoke_stub;
1490 } else {
1491 compiled_invoke_stubs_.Put(key, compiled_invoke_stub);
1492 }
Ian Rogers0571d352011-11-03 19:51:38 -07001493}
1494
Logan Chien7a2a23a2012-06-06 11:01:00 +08001495#if defined(ART_USE_LLVM_COMPILER)
1496const CompiledInvokeStub* Compiler::FindProxyStub(const char* shorty) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07001497 MutexLock mu(Thread::Current(), compiled_proxy_stubs_lock_);
Logan Chien7a2a23a2012-06-06 11:01:00 +08001498 ProxyStubTable::const_iterator it = compiled_proxy_stubs_.find(shorty);
1499 if (it == compiled_proxy_stubs_.end()) {
1500 return NULL;
1501 } else {
1502 DCHECK(it->second != NULL);
1503 return it->second;
1504 }
1505}
1506
1507void Compiler::InsertProxyStub(const char* shorty,
1508 const CompiledInvokeStub* compiled_proxy_stub) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001509 MutexLock mu(Thread::Current(), compiled_proxy_stubs_lock_);
Logan Chien7a2a23a2012-06-06 11:01:00 +08001510 InvokeStubTable::iterator it = compiled_proxy_stubs_.find(shorty);
1511 if (it != compiled_proxy_stubs_.end()) {
1512 // Someone else won the race.
1513 delete compiled_proxy_stub;
1514 } else {
1515 compiled_proxy_stubs_.Put(shorty, compiled_proxy_stub);
1516 }
1517}
1518#endif
1519
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001520CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07001521 MutexLock mu(Thread::Current(), compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001522 ClassTable::const_iterator it = compiled_classes_.find(ref);
1523 if (it == compiled_classes_.end()) {
1524 return NULL;
1525 }
1526 CHECK(it->second != NULL);
1527 return it->second;
1528}
1529
Ian Rogers0571d352011-11-03 19:51:38 -07001530CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07001531 MutexLock mu(Thread::Current(), compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001532 MethodTable::const_iterator it = compiled_methods_.find(ref);
1533 if (it == compiled_methods_.end()) {
1534 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001535 }
1536 CHECK(it->second != NULL);
1537 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001538}
1539
buzbee2cfc6392012-05-07 14:51:40 -07001540#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_QUICK_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +08001541void Compiler::SetBitcodeFileName(std::string const& filename) {
Logan Chien106b2a02012-03-18 04:41:38 +08001542 typedef void (*SetBitcodeFileNameFn)(Compiler&, std::string const&);
1543
1544 SetBitcodeFileNameFn set_bitcode_file_name =
1545 FindFunction<SetBitcodeFileNameFn>(MakeCompilerSoName(instruction_set_),
1546 compiler_library_,
1547 "compilerLLVMSetBitcodeFileName");
1548
1549 set_bitcode_file_name(*this, filename);
Logan Chien8b977d32012-02-21 19:14:55 +08001550}
buzbee2cfc6392012-05-07 14:51:40 -07001551#endif
Logan Chienf7015fd2012-03-18 01:19:37 +08001552
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001553} // namespace art