blob: 36e8d287c78195d9ab0aede0b1da308610ca67f4 [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 Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "runtime.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080032#include "space.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070033#include "stl_util.h"
Elliott Hughes601a1232012-02-02 17:47:38 -080034#include "timing_logger.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070035#include "verifier/method_verifier.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070036
Elliott Hughes059d5c12012-03-12 17:39:18 -070037#if defined(__APPLE__)
38#include <mach-o/dyld.h>
39#endif
40
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070041namespace art {
42
Shih-wei Liaoc486c112011-09-13 16:43:52 -070043namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070044 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070045 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080046 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070047}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070048namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070049 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070050 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080051 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070052}
53
Ian Rogers996cc582012-02-14 22:23:29 -080054static double Percentage(size_t x, size_t y) {
Elliott Hughes398f64b2012-03-26 18:05:48 -070055 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
Ian Rogers996cc582012-02-14 22:23:29 -080056}
57
58static void DumpStat(size_t x, size_t y, const char* str) {
59 if (x == 0 && y == 0) {
60 return;
61 }
62 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
63}
64
Ian Rogersc8b306f2012-02-17 21:34:44 -080065class AOTCompilationStats {
66 public:
Ian Rogersca190662012-06-26 15:45:57 -070067 AOTCompilationStats()
68 : stats_lock_("AOT compilation statistics lock"),
69 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
70 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
71 resolved_types_(0), unresolved_types_(0),
72 resolved_instance_fields_(0), unresolved_instance_fields_(0),
73 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0) {
Ian Rogers2ed3b952012-03-17 11:49:39 -070074 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080075 resolved_methods_[i] = 0;
76 unresolved_methods_[i] = 0;
Ian Rogers2ed3b952012-03-17 11:49:39 -070077 virtual_made_direct_[i] = 0;
78 direct_calls_to_boot_[i] = 0;
79 direct_methods_to_boot_[i] = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -070080 }
Ian Rogersc8b306f2012-02-17 21:34:44 -080081 }
82
83 void Dump() {
84 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
85 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
86 DumpStat(resolved_types_, unresolved_types_, "types resolved");
87 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
88 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
89 "static fields resolved");
90 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
91 "static fields local to a class");
92
Ian Rogers2ed3b952012-03-17 11:49:39 -070093 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080094 std::ostringstream oss;
Ian Rogers2ed3b952012-03-17 11:49:39 -070095 oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
Ian Rogersc8b306f2012-02-17 21:34:44 -080096 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
Ian Rogers2ed3b952012-03-17 11:49:39 -070097 if (virtual_made_direct_[i] > 0) {
98 std::ostringstream oss2;
99 oss2 << static_cast<InvokeType>(i) << " methods made direct";
100 DumpStat(virtual_made_direct_[i],
101 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
102 oss2.str().c_str());
103 }
104 if (direct_calls_to_boot_[i] > 0) {
105 std::ostringstream oss2;
106 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
107 DumpStat(direct_calls_to_boot_[i],
108 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
109 oss2.str().c_str());
110 }
111 if (direct_methods_to_boot_[i] > 0) {
112 std::ostringstream oss2;
113 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
114 DumpStat(direct_methods_to_boot_[i],
115 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
116 oss2.str().c_str());
117 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800118 }
119 }
Ian Rogers996cc582012-02-14 22:23:29 -0800120
121// Allow lossy statistics in non-debug builds
122#ifndef NDEBUG
123#define STATS_LOCK() MutexLock mu(stats_lock_)
124#else
125#define STATS_LOCK()
126#endif
127
Ian Rogersc8b306f2012-02-17 21:34:44 -0800128 void TypeInDexCache() {
129 STATS_LOCK();
130 types_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800131 }
Ian Rogers996cc582012-02-14 22:23:29 -0800132
Ian Rogersc8b306f2012-02-17 21:34:44 -0800133 void TypeNotInDexCache() {
134 STATS_LOCK();
135 types_not_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800136 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800137
138 void StringInDexCache() {
139 STATS_LOCK();
140 strings_in_dex_cache_++;
141 }
142
143 void StringNotInDexCache() {
144 STATS_LOCK();
145 strings_not_in_dex_cache_++;
146 }
147
148 void TypeDoesntNeedAccessCheck() {
149 STATS_LOCK();
150 resolved_types_++;
151 }
152
153 void TypeNeedsAccessCheck() {
154 STATS_LOCK();
155 unresolved_types_++;
156 }
157
158 void ResolvedInstanceField() {
159 STATS_LOCK();
160 resolved_instance_fields_++;
161 }
162
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700163 void UnresolvedInstanceField() {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800164 STATS_LOCK();
165 unresolved_instance_fields_++;
166 }
167
168 void ResolvedLocalStaticField() {
169 STATS_LOCK();
170 resolved_local_static_fields_++;
171 }
172
173 void ResolvedStaticField() {
174 STATS_LOCK();
175 resolved_static_fields_++;
176 }
177
178 void UnresolvedStaticField() {
179 STATS_LOCK();
180 unresolved_static_fields_++;
181 }
182
183 void ResolvedMethod(InvokeType type) {
184 DCHECK_LE(type, kMaxInvokeType);
185 STATS_LOCK();
186 resolved_methods_[type]++;
187 }
188
189 void UnresolvedMethod(InvokeType type) {
190 DCHECK_LE(type, kMaxInvokeType);
191 STATS_LOCK();
192 unresolved_methods_[type]++;
193 }
194
Ian Rogers2ed3b952012-03-17 11:49:39 -0700195 void VirtualMadeDirect(InvokeType type) {
196 DCHECK_LE(type, kMaxInvokeType);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800197 STATS_LOCK();
Ian Rogers2ed3b952012-03-17 11:49:39 -0700198 virtual_made_direct_[type]++;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800199 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700200
201 void DirectCallsToBoot(InvokeType type) {
202 DCHECK_LE(type, kMaxInvokeType);
203 STATS_LOCK();
204 direct_calls_to_boot_[type]++;
205 }
206
207 void DirectMethodsToBoot(InvokeType type) {
208 DCHECK_LE(type, kMaxInvokeType);
209 STATS_LOCK();
210 direct_methods_to_boot_[type]++;
211 }
212
Ian Rogersc8b306f2012-02-17 21:34:44 -0800213 private:
214 Mutex stats_lock_;
215
216 size_t types_in_dex_cache_;
217 size_t types_not_in_dex_cache_;
218
219 size_t strings_in_dex_cache_;
220 size_t strings_not_in_dex_cache_;
221
222 size_t resolved_types_;
223 size_t unresolved_types_;
224
225 size_t resolved_instance_fields_;
226 size_t unresolved_instance_fields_;
227
228 size_t resolved_local_static_fields_;
229 size_t resolved_static_fields_;
230 size_t unresolved_static_fields_;
231
232 size_t resolved_methods_[kMaxInvokeType + 1];
233 size_t unresolved_methods_[kMaxInvokeType + 1];
Ian Rogers2ed3b952012-03-17 11:49:39 -0700234 size_t virtual_made_direct_[kMaxInvokeType + 1];
235 size_t direct_calls_to_boot_[kMaxInvokeType + 1];
236 size_t direct_methods_to_boot_[kMaxInvokeType + 1];
Ian Rogersc8b306f2012-02-17 21:34:44 -0800237
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700238 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800239};
Ian Rogers996cc582012-02-14 22:23:29 -0800240
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800241static std::string MakeCompilerSoName(InstructionSet instruction_set) {
242 // TODO: is the ARM/Thumb2 instruction set distinction really buying us anything,
243 // or just causing hassle like this?
244 if (instruction_set == kThumb2) {
245 instruction_set = kArm;
246 }
247
Elliott Hughes059d5c12012-03-12 17:39:18 -0700248 // Capitalize the instruction set, because that's what we do in the build system.
Elliott Hughes6fcce302012-06-19 16:54:19 -0700249 std::string instruction_set_name(ToStr<InstructionSet>(instruction_set).str());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800250 for (size_t i = 0; i < instruction_set_name.size(); ++i) {
251 instruction_set_name[i] = toupper(instruction_set_name[i]);
252 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700253
254 // Bad things happen if we pull in the libartd-compiler to a libart dex2oat or vice versa,
255 // because we end up with both libart and libartd in the same address space!
Elliott Hughes67d92002012-03-26 15:08:51 -0700256 const char* suffix = (kIsDebugBuild ? "d" : "");
Elliott Hughes059d5c12012-03-12 17:39:18 -0700257
258 // Work out the filename for the compiler library.
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700259#if defined(ART_USE_LLVM_COMPILER)
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800260 std::string library_name(StringPrintf("art%s-compiler-llvm", suffix));
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700261#elif defined(ART_USE_GREENLAND_COMPILER)
262 std::string library_name(StringPrintf("art%s-compiler-greenland", suffix));
263#else
264 std::string library_name(StringPrintf("art%s-compiler-%s", suffix, instruction_set_name.c_str()));
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800265#endif
Elliott Hughes059d5c12012-03-12 17:39:18 -0700266 std::string filename(StringPrintf(OS_SHARED_LIB_FORMAT_STR, library_name.c_str()));
267
268#if defined(__APPLE__)
269 // On Linux, dex2oat will have been built with an RPATH of $ORIGIN/../lib, so dlopen(3) will find
270 // the .so by itself. On Mac OS, there isn't really an equivalent, so we have to manually do the
271 // same work.
Elliott Hughes059d5c12012-03-12 17:39:18 -0700272 uint32_t executable_path_length = 0;
Elliott Hughes448e93c2012-03-28 22:30:06 -0700273 _NSGetExecutablePath(NULL, &executable_path_length);
274 std::string path(executable_path_length, static_cast<char>(0));
275 CHECK_EQ(_NSGetExecutablePath(&path[0], &executable_path_length), 0);
Elliott Hughes059d5c12012-03-12 17:39:18 -0700276
277 // Strip the "/dex2oat".
278 size_t last_slash = path.find_last_of('/');
279 CHECK_NE(last_slash, std::string::npos) << path;
280 path.resize(last_slash);
281
282 // Strip the "/bin".
283 last_slash = path.find_last_of('/');
284 path.resize(last_slash);
285
286 filename = path + "/lib/" + filename;
287#endif
288 return filename;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800289}
290
Elliott Hughes46f060a2012-03-09 17:36:50 -0800291template<typename Fn>
292static Fn FindFunction(const std::string& compiler_so_name, void* library, const char* name) {
293 Fn fn = reinterpret_cast<Fn>(dlsym(library, name));
294 if (fn == NULL) {
295 LOG(FATAL) << "Couldn't find \"" << name << "\" in compiler library " << compiler_so_name << ": " << dlerror();
296 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700297 VLOG(compiler) << "Found \"" << name << "\" at " << reinterpret_cast<void*>(fn);
Elliott Hughes46f060a2012-03-09 17:36:50 -0800298 return fn;
299}
300
Elliott Hughes5523ee02012-02-03 18:18:34 -0800301Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700302 bool support_debugging, const std::set<std::string>* image_classes,
303 bool dump_stats, bool dump_timings)
Brian Carlstromaded5f72011-10-07 17:15:04 -0700304 : instruction_set_(instruction_set),
Elliott Hughesc225caa2012-02-03 15:43:37 -0800305 compiled_classes_lock_("compiled classes lock"),
306 compiled_methods_lock_("compiled method lock"),
307 compiled_invoke_stubs_lock_("compiled invoke stubs lock"),
Logan Chien7a2a23a2012-06-06 11:01:00 +0800308#if defined(ART_USE_LLVM_COMPILER)
309 compiled_proxy_stubs_lock_("compiled proxy stubs lock"),
310#endif
Brian Carlstromaded5f72011-10-07 17:15:04 -0700311 image_(image),
Elliott Hughes5523ee02012-02-03 18:18:34 -0800312 thread_count_(thread_count),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800313 support_debugging_(support_debugging),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800314 stats_(new AOTCompilationStats),
Brian Carlstromba0668e2012-03-26 13:14:07 -0700315 dump_stats_(dump_stats),
316 dump_timings_(dump_timings),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800317 image_classes_(image_classes),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800318 compiler_library_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800319 compiler_(NULL),
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700320 compiler_context_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800321 jni_compiler_(NULL),
Logan Chien971bf3f2012-05-01 15:47:55 +0800322 create_invoke_stub_(NULL)
323{
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800324 std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800325 compiler_library_ = dlopen(compiler_so_name.c_str(), RTLD_LAZY);
326 if (compiler_library_ == NULL) {
327 LOG(FATAL) << "Couldn't find compiler library " << compiler_so_name << ": " << dlerror();
328 }
329 VLOG(compiler) << "dlopen(\"" << compiler_so_name << "\", RTLD_LAZY) returned " << compiler_library_;
330
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700331#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chien106b2a02012-03-18 04:41:38 +0800332 // Initialize compiler_context_
333 typedef void (*InitCompilerContextFn)(Compiler&);
334
335 InitCompilerContextFn init_compiler_context =
336 FindFunction<void (*)(Compiler&)>(compiler_so_name,
337 compiler_library_,
338 "ArtInitCompilerContext");
339
340 init_compiler_context(*this);
341#endif
342
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700343 compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "ArtCompileMethod");
Elliott Hughes46f060a2012-03-09 17:36:50 -0800344 jni_compiler_ = FindFunction<JniCompilerFn>(compiler_so_name, compiler_library_, "ArtJniCompileMethod");
345 create_invoke_stub_ = FindFunction<CreateInvokeStubFn>(compiler_so_name, compiler_library_, "ArtCreateInvokeStub");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800346
Logan Chienf7015fd2012-03-18 01:19:37 +0800347#if defined(ART_USE_LLVM_COMPILER)
Logan Chien7a2a23a2012-06-06 11:01:00 +0800348 create_proxy_stub_ = FindFunction<CreateProxyStubFn>(
349 compiler_so_name, compiler_library_, "ArtCreateProxyStub");
Logan Chienf7015fd2012-03-18 01:19:37 +0800350#endif
351
Brian Carlstrom25c33252011-09-18 15:58:35 -0700352 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800353 if (!image_) {
354 CHECK(image_classes_ == NULL);
355 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700356}
357
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700358Compiler::~Compiler() {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800359 {
360 MutexLock mu(compiled_classes_lock_);
361 STLDeleteValues(&compiled_classes_);
362 }
363 {
364 MutexLock mu(compiled_methods_lock_);
365 STLDeleteValues(&compiled_methods_);
366 }
367 {
368 MutexLock mu(compiled_invoke_stubs_lock_);
369 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800370 }
Brian Carlstromd06dfe72012-06-06 22:15:39 -0700371#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700372 {
Logan Chien7a2a23a2012-06-06 11:01:00 +0800373 MutexLock mu(compiled_proxy_stubs_lock_);
374 STLDeleteValues(&compiled_proxy_stubs_);
375 }
Brian Carlstromd06dfe72012-06-06 22:15:39 -0700376#endif
Logan Chien7a2a23a2012-06-06 11:01:00 +0800377 {
Brian Carlstromf5822582012-03-19 22:34:31 -0700378 MutexLock mu(compiled_methods_lock_);
379 STLDeleteElements(&code_to_patch_);
380 }
381 {
382 MutexLock mu(compiled_methods_lock_);
383 STLDeleteElements(&methods_to_patch_);
384 }
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800385#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800386 // Uninitialize compiler_context_
387 typedef void (*UninitCompilerContextFn)(Compiler&);
388
389 std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
390
391 UninitCompilerContextFn uninit_compiler_context =
392 FindFunction<void (*)(Compiler&)>(compiler_so_name,
393 compiler_library_,
394 "ArtUnInitCompilerContext");
395
396 uninit_compiler_context(*this);
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800397#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800398 if (compiler_library_ != NULL) {
399 VLOG(compiler) << "dlclose(" << compiler_library_ << ")";
400 dlclose(compiler_library_);
401 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700402}
403
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700404ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
405 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700406 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700407 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700408 } else {
409 CHECK(instruction_set == kArm || instruction_set == kThumb2);
410 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700411 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700412 }
413}
414
Elliott Hughes8add92d2012-01-18 18:18:43 -0800415ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800416 switch (instruction_set) {
417 case kArm:
418 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800419 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800420 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800421 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800422 default:
Ian Rogers49c48942012-03-11 15:15:37 -0700423 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Ian Rogers169c9a72011-11-13 20:13:17 -0800424 return NULL;
425 }
426}
427
Ian Rogersad25ac52011-10-04 19:13:33 -0700428ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
429 if (instruction_set == kX86) {
430 return x86::CreateAbstractMethodErrorStub();
431 } else {
432 CHECK(instruction_set == kArm || instruction_set == kThumb2);
433 // Generates resolution stub using ARM instruction set
434 return arm::CreateAbstractMethodErrorStub();
435 }
436}
437
Ian Rogers365c1022012-06-22 15:05:28 -0700438void Compiler::CompileAll(ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800439 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700440 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800441
Elliott Hughes601a1232012-02-02 17:47:38 -0800442 TimingLogger timings("compiler");
443
Elliott Hughes4909ba42012-06-14 13:33:49 -0700444 // TODO: make the verifier thread-safe and remove this workaround.
445 size_t thread_count = thread_count_;
446 thread_count_ = 1;
Elliott Hughes601a1232012-02-02 17:47:38 -0800447 PreCompile(class_loader, dex_files, timings);
Elliott Hughes4909ba42012-06-14 13:33:49 -0700448 thread_count_ = thread_count;
Elliott Hughes601a1232012-02-02 17:47:38 -0800449
Brian Carlstromae826982011-11-09 01:33:42 -0800450 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800451 timings.AddSplit("Compile");
452
Brian Carlstromae826982011-11-09 01:33:42 -0800453 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800454 timings.AddSplit("PostCompile");
455
Brian Carlstromba0668e2012-03-26 13:14:07 -0700456 if (dump_timings_ && timings.GetTotalNs() > MsToNs(1000)) {
Elliott Hughes601a1232012-02-02 17:47:38 -0800457 timings.Dump();
458 }
Ian Rogers996cc582012-02-14 22:23:29 -0800459
Brian Carlstromba0668e2012-03-26 13:14:07 -0700460 if (dump_stats_) {
461 stats_->Dump();
462 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700463}
464
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700465void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700466 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800467
Ian Rogers365c1022012-06-22 15:05:28 -0700468 ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800469
Ian Rogers0571d352011-11-03 19:51:38 -0700470 // Find the dex_file
471 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
472 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800473 std::vector<const DexFile*> dex_files;
474 dex_files.push_back(&dex_file);
475
Elliott Hughes601a1232012-02-02 17:47:38 -0800476 TimingLogger timings("CompileOne");
477 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800478
Ian Rogers0571d352011-11-03 19:51:38 -0700479 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800480 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
481 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800482
483 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700484}
485
Ian Rogers365c1022012-06-22 15:05:28 -0700486void Compiler::Resolve(ClassLoader* class_loader,
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800487 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800488 for (size_t i = 0; i != dex_files.size(); ++i) {
489 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700490 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800491 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700492 }
493}
494
Ian Rogers365c1022012-06-22 15:05:28 -0700495void Compiler::PreCompile(ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800496 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800497 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800498
Brian Carlstromae826982011-11-09 01:33:42 -0800499 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800500 timings.AddSplit("PreCompile.Verify");
501
Brian Carlstromae826982011-11-09 01:33:42 -0800502 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800503 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800504}
505
Ian Rogers365c1022012-06-22 15:05:28 -0700506void Compiler::PostCompile(ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800507 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800508 SetGcMaps(class_loader, dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800509}
510
511bool Compiler::IsImageClass(const std::string& descriptor) const {
512 if (image_classes_ == NULL) {
513 return true;
514 }
515 return image_classes_->find(descriptor) != image_classes_->end();
516}
517
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800518bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800519 uint32_t type_idx) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 if (!IsImage()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800521 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800522 return false;
523 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800524 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800525 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800526 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800527 return false;
528 }
Ian Rogers996cc582012-02-14 22:23:29 -0800529 bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor());
530 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800531 stats_->TypeInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800532 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800533 stats_->TypeNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800534 }
535 return result;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800536}
537
Ian Rogers1bddec32012-02-04 12:27:34 -0800538bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800539 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800540 // TODO: Add support for loading strings referenced by image_classes_
541 // See also Compiler::ResolveDexFile
542
543 // The following is a test saying that if we're building the image without a restricted set of
544 // image classes then we can assume the string is present in the dex cache if it is there now
Ian Rogers996cc582012-02-14 22:23:29 -0800545 bool result = IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
546 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800547 stats_->StringInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800548 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800549 stats_->StringNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800550 }
551 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800552}
553
554bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800555 const DexFile& dex_file, uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800556 // Get type from dex cache assuming it was populated by the verifier
557 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
558 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800559 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800560 return false; // Unknown class needs access checks.
561 }
562 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
563 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
564 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800565 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800566 return false; // Incomplete referrer knowledge needs access check.
567 }
568 // Perform access check, will return true if access is ok or false if we're going to have to
569 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800570 bool result = referrer_class->CanAccess(resolved_class);
571 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800572 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800573 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800574 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800575 }
576 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800577}
578
579bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
580 const DexCache* dex_cache,
581 const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800582 uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800583 // Get type from dex cache assuming it was populated by the verifier.
584 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
585 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800586 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800587 return false; // Unknown class needs access checks.
588 }
589 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
590 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
591 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800592 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800593 return false; // Incomplete referrer knowledge needs access check.
594 }
595 // Perform access and instantiable checks, will return true if access is ok or false if we're
596 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800597 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
598 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800599 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800600 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800601 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800602 }
603 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800604}
605
Logan Chien4dd96f52012-02-29 01:26:58 +0800606static Class* ComputeReferrerClass(OatCompilationUnit* mUnit) {
607 const DexFile::MethodId& referrer_method_id =
608 mUnit->dex_file_->GetMethodId(mUnit->method_idx_);
609
610 return mUnit->class_linker_->ResolveType(
611 *mUnit->dex_file_, referrer_method_id.class_idx_,
612 mUnit->dex_cache_, mUnit->class_loader_);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800613}
614
Logan Chien4dd96f52012-02-29 01:26:58 +0800615static Field* ComputeReferrerField(OatCompilationUnit* mUnit, uint32_t field_idx) {
616 return mUnit->class_linker_->ResolveField(
617 *mUnit->dex_file_, field_idx, mUnit->dex_cache_,
618 mUnit->class_loader_, false);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800619}
620
Logan Chien4dd96f52012-02-29 01:26:58 +0800621static Method* ComputeReferrerMethod(OatCompilationUnit* mUnit, uint32_t method_idx) {
622 return mUnit->class_linker_->ResolveMethod(
623 *mUnit->dex_file_, method_idx, mUnit->dex_cache_,
624 mUnit->class_loader_, true);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800625}
626
Logan Chien4dd96f52012-02-29 01:26:58 +0800627bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
jeffhao8cd6dda2012-02-22 10:15:34 -0800628 int& field_offset, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800629 // Conservative defaults
630 field_offset = -1;
631 is_volatile = true;
632 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800633 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800634 if (resolved_field != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800635 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogerse2645d32012-04-11 14:42:42 -0700636 if (referrer_class != NULL) {
637 Class* fields_class = resolved_field->GetDeclaringClass();
638 bool access_ok = referrer_class->CanAccess(fields_class) &&
639 referrer_class->CanAccessMember(fields_class,
640 resolved_field->GetAccessFlags());
641 if (!access_ok) {
642 // The referring class can't access the resolved field, this may occur as a result of a
643 // protected field being made public by a sub-class. Resort to the dex file to determine
644 // the correct class for the access check.
645 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
646 Class* dex_fields_class = mUnit->class_linker_->ResolveType(dex_file,
647 dex_file.GetFieldId(field_idx).class_idx_,
648 referrer_class);
649 access_ok = referrer_class->CanAccess(dex_fields_class) &&
650 referrer_class->CanAccessMember(dex_fields_class,
651 resolved_field->GetAccessFlags());
652 }
653 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
654 fields_class != referrer_class;
655 if (access_ok && !is_write_to_final_from_wrong_class) {
656 field_offset = resolved_field->GetOffset().Int32Value();
657 is_volatile = resolved_field->IsVolatile();
658 stats_->ResolvedInstanceField();
659 return true; // Fast path.
660 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800661 }
662 }
663 // Clean up any exception left by field/type resolution
664 Thread* thread = Thread::Current();
665 if (thread->IsExceptionPending()) {
666 thread->ClearException();
667 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800668 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800669 return false; // Incomplete knowledge needs slow path.
670}
671
Logan Chien4dd96f52012-02-29 01:26:58 +0800672bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
Ian Rogers1bddec32012-02-04 12:27:34 -0800673 int& field_offset, int& ssb_index,
jeffhao8cd6dda2012-02-22 10:15:34 -0800674 bool& is_referrers_class, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800675 // Conservative defaults
676 field_offset = -1;
677 ssb_index = -1;
678 is_referrers_class = false;
679 is_volatile = true;
680 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800681 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800682 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800683 DCHECK(resolved_field->IsStatic());
Logan Chien4dd96f52012-02-29 01:26:58 +0800684 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800685 if (referrer_class != NULL) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800686 Class* fields_class = resolved_field->GetDeclaringClass();
687 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800688 is_referrers_class = true; // implies no worrying about class initialization
689 field_offset = resolved_field->GetOffset().Int32Value();
690 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800691 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800692 return true; // fast path
693 } else {
Ian Rogerse2645d32012-04-11 14:42:42 -0700694 bool access_ok = referrer_class->CanAccess(fields_class) &&
695 referrer_class->CanAccessMember(fields_class,
696 resolved_field->GetAccessFlags());
697 if (!access_ok) {
698 // The referring class can't access the resolved field, this may occur as a result of a
699 // protected field being made public by a sub-class. Resort to the dex file to determine
700 // the correct class for the access check. Don't change the field's class as that is
701 // used to identify the SSB.
702 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
703 Class* dex_fields_class =
704 mUnit->class_linker_->ResolveType(dex_file,
705 dex_file.GetFieldId(field_idx).class_idx_,
706 referrer_class);
707 access_ok = referrer_class->CanAccess(dex_fields_class) &&
708 referrer_class->CanAccessMember(dex_fields_class,
709 resolved_field->GetAccessFlags());
710 }
jeffhao8cd6dda2012-02-22 10:15:34 -0800711 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogerse2645d32012-04-11 14:42:42 -0700712 if (access_ok && !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800713 // We have the resolved field, we must make it into a ssbIndex for the referrer
714 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800715 // TODO: for images we can elide the static storage base null check
716 // if we know there's a non-null entry in the image
Logan Chien4dd96f52012-02-29 01:26:58 +0800717 if (fields_class->GetDexCache() == mUnit->dex_cache_) {
Ian Rogers4103ad22012-02-06 09:18:25 -0800718 // common case where the dex cache of both the referrer and the field are the same,
719 // no need to search the dex file
720 ssb_index = fields_class->GetDexTypeIndex();
721 field_offset = resolved_field->GetOffset().Int32Value();
722 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800723 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800724 return true;
725 }
Ian Rogerse2645d32012-04-11 14:42:42 -0700726 // Search dex file for localized ssb index, may fail if field's class is a parent
727 // of the class mentioned in the dex file and there is no dex cache entry.
Ian Rogers1bddec32012-02-04 12:27:34 -0800728 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
729 const DexFile::StringId* string_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800730 mUnit->dex_file_->FindStringId(descriptor);
Ian Rogers1bddec32012-02-04 12:27:34 -0800731 if (string_id != NULL) {
732 const DexFile::TypeId* type_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800733 mUnit->dex_file_->FindTypeId(mUnit->dex_file_->GetIndexForStringId(*string_id));
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700734 if (type_id != NULL) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800735 // medium path, needs check of static storage base being initialized
Logan Chien4dd96f52012-02-29 01:26:58 +0800736 ssb_index = mUnit->dex_file_->GetIndexForTypeId(*type_id);
Ian Rogers1bddec32012-02-04 12:27:34 -0800737 field_offset = resolved_field->GetOffset().Int32Value();
738 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800739 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800740 return true;
741 }
742 }
743 }
744 }
745 }
746 }
747 // Clean up any exception left by field/type resolution
748 Thread* thread = Thread::Current();
749 if (thread->IsExceptionPending()) {
750 thread->ClearException();
751 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800752 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800753 return false; // Incomplete knowledge needs slow path.
754}
755
Ian Rogers2ed3b952012-03-17 11:49:39 -0700756void Compiler::GetCodeAndMethodForDirectCall(InvokeType type, InvokeType sharp_type, Method* method,
757 uintptr_t& direct_code, uintptr_t& direct_method) {
758 direct_code = 0;
759 direct_method = 0;
760 if (sharp_type != kStatic && sharp_type != kDirect) {
761 return;
762 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700763 bool method_code_in_boot = method->GetDeclaringClass()->GetClassLoader() == NULL;
764 if (!method_code_in_boot) {
765 return;
766 }
767 bool has_clinit_trampoline = method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
768 if (has_clinit_trampoline) {
769 return;
770 }
771 stats_->DirectCallsToBoot(type);
772 stats_->DirectMethodsToBoot(type);
Ian Rogers3fa13792012-03-18 15:53:45 -0700773 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
774 if (compiling_boot) {
Brian Carlstrom0637e272012-03-20 01:07:52 -0700775 const bool kSupportBootImageFixup = true;
Ian Rogers3fa13792012-03-18 15:53:45 -0700776 if (kSupportBootImageFixup) {
777 MethodHelper mh(method);
778 if (IsImageClass(mh.GetDeclaringClassDescriptor())) {
Brian Carlstrom0637e272012-03-20 01:07:52 -0700779 // We can only branch directly to Methods that are resolved in the DexCache.
780 // Otherwise we won't invoke the resolution trampoline.
Ian Rogers3fa13792012-03-18 15:53:45 -0700781 direct_method = -1;
Brian Carlstrom0637e272012-03-20 01:07:52 -0700782 direct_code = -1;
Ian Rogers3fa13792012-03-18 15:53:45 -0700783 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700784 }
785 } else {
786 if (Runtime::Current()->GetHeap()->GetImageSpace()->Contains(method)) {
787 direct_method = reinterpret_cast<uintptr_t>(method);
788 }
789 direct_code = reinterpret_cast<uintptr_t>(method->GetCode());
Ian Rogers2ed3b952012-03-17 11:49:39 -0700790 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700791}
792
Ian Rogersfb6adba2012-03-04 21:51:51 -0800793bool Compiler::ComputeInvokeInfo(uint32_t method_idx, OatCompilationUnit* mUnit, InvokeType& type,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700794 int& vtable_idx, uintptr_t& direct_code,
795 uintptr_t& direct_method) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800796 vtable_idx = -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700797 direct_code = 0;
798 direct_method = 0;
Logan Chien4dd96f52012-02-29 01:26:58 +0800799 Method* resolved_method = ComputeReferrerMethod(mUnit, method_idx);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800800 if (resolved_method != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800801 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800802 if (referrer_class != NULL) {
803 Class* methods_class = resolved_method->GetDeclaringClass();
804 if (!referrer_class->CanAccess(methods_class) ||
805 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800806 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800807 // The referring class can't access the resolved method, this may occur as a result of a
808 // protected method being made public by implementing an interface that re-declares the
809 // method public. Resort to the dex file to determine the correct class for the access check
Logan Chien4dd96f52012-02-29 01:26:58 +0800810 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800811 methods_class =
Logan Chien4dd96f52012-02-29 01:26:58 +0800812 mUnit->class_linker_->ResolveType(dex_file,
813 dex_file.GetMethodId(method_idx).class_idx_,
814 referrer_class);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800815 }
816 if (referrer_class->CanAccess(methods_class) &&
817 referrer_class->CanAccessMember(methods_class,
818 resolved_method->GetAccessFlags())) {
819 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogersf320b632012-03-13 18:47:47 -0700820 const bool kEnableSharpening = true;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700821 // Sharpen a virtual call into a direct call when the target is known.
822 bool can_sharpen = type == kVirtual && (resolved_method->IsFinal() ||
823 methods_class->IsFinal());
824 // ensure the vtable index will be correct to dispatch in the vtable of the super class
jeffhao4155fcd2012-03-21 11:42:12 -0700825 can_sharpen = can_sharpen || (type == kSuper && referrer_class != methods_class &&
Ian Rogers2ed3b952012-03-17 11:49:39 -0700826 referrer_class->IsSubClass(methods_class) &&
jeffhao4155fcd2012-03-21 11:42:12 -0700827 vtable_idx < methods_class->GetVTable()->GetLength() &&
828 methods_class->GetVTable()->Get(vtable_idx) == resolved_method);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700829 if (kEnableSharpening && can_sharpen) {
830 stats_->ResolvedMethod(type);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800831 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
832 // dex cache, check that this resolved method is where we expect it.
833 CHECK(referrer_class->GetDexCache()->GetResolvedMethod(method_idx) == resolved_method)
834 << PrettyMethod(resolved_method);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700835 stats_->VirtualMadeDirect(type);
836 GetCodeAndMethodForDirectCall(type, kDirect, resolved_method, direct_code, direct_method);
837 type = kDirect;
838 return true;
839 } else if (type == kSuper) {
840 // Unsharpened super calls are suspicious so go slowpath.
841 } else {
842 stats_->ResolvedMethod(type);
843 GetCodeAndMethodForDirectCall(type, type, resolved_method, direct_code, direct_method);
844 return true;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800845 }
846 }
847 }
848 }
849 // Clean up any exception left by method/type resolution
850 Thread* thread = Thread::Current();
851 if (thread->IsExceptionPending()) {
852 thread->ClearException();
853 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800854 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800855 return false; // Incomplete knowledge needs slow path.
856}
857
Brian Carlstromf5822582012-03-19 22:34:31 -0700858void Compiler::AddCodePatch(DexCache* dex_cache,
859 const DexFile* dex_file,
860 uint32_t referrer_method_idx,
861 uint32_t referrer_access_flags,
862 uint32_t target_method_idx,
863 bool target_is_direct,
Ian Rogers3fa13792012-03-18 15:53:45 -0700864 size_t literal_offset) {
865 MutexLock mu(compiled_methods_lock_);
Brian Carlstromf5822582012-03-19 22:34:31 -0700866 code_to_patch_.push_back(new PatchInformation(dex_cache,
867 dex_file,
868 referrer_method_idx,
869 referrer_access_flags,
870 target_method_idx,
871 target_is_direct,
872 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -0700873}
Brian Carlstromf5822582012-03-19 22:34:31 -0700874void Compiler::AddMethodPatch(DexCache* dex_cache,
875 const DexFile* dex_file,
876 uint32_t referrer_method_idx,
877 uint32_t referrer_access_flags,
878 uint32_t target_method_idx,
879 bool target_is_direct,
Ian Rogers3fa13792012-03-18 15:53:45 -0700880 size_t literal_offset) {
881 MutexLock mu(compiled_methods_lock_);
Brian Carlstromf5822582012-03-19 22:34:31 -0700882 methods_to_patch_.push_back(new PatchInformation(dex_cache,
883 dex_file,
884 referrer_method_idx,
885 referrer_access_flags,
886 target_method_idx,
887 target_is_direct,
888 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -0700889}
890
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800891// Return true if the class should be skipped during compilation. We
892// never skip classes in the boot class loader. However, if we have a
893// non-boot class loader and we can resolve the class in the boot
894// class loader, we do skip the class. This happens if an app bundles
895// classes found in the boot classpath. Since at runtime we will
896// select the class from the boot classpath, do not attempt to resolve
897// or compile it now.
TDYa12750b69e32012-06-26 18:11:27 -0700898static bool SkipClass(ClassLoader* class_loader,
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800899 const DexFile& dex_file,
900 const DexFile::ClassDef& class_def) {
901 if (class_loader == NULL) {
902 return false;
903 }
904 const char* descriptor = dex_file.GetClassDescriptor(class_def);
905 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
906 Class* klass = class_linker->FindClass(descriptor, NULL);
907 if (klass == NULL) {
908 Thread* self = Thread::Current();
909 CHECK(self->IsExceptionPending());
910 self->ClearException();
911 return false;
912 }
913 return true;
914}
915
Ian Rogers0399dde2012-06-06 17:09:28 -0700916class CompilationContext {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800917 public:
Ian Rogers0399dde2012-06-06 17:09:28 -0700918 CompilationContext(ClassLinker* class_linker,
Ian Rogers365c1022012-06-22 15:05:28 -0700919 ClassLoader* class_loader,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800920 Compiler* compiler,
921 DexCache* dex_cache,
922 const DexFile* dex_file)
923 : class_linker_(class_linker),
924 class_loader_(class_loader),
925 compiler_(compiler),
926 dex_cache_(dex_cache),
927 dex_file_(dex_file) {}
928
929 ClassLinker* GetClassLinker() {
930 CHECK(class_linker_ != NULL);
931 return class_linker_;
932 }
Ian Rogers365c1022012-06-22 15:05:28 -0700933 ClassLoader* GetClassLoader() {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800934 return class_loader_;
935 }
936 Compiler* GetCompiler() {
937 CHECK(compiler_ != NULL);
938 return compiler_;
939 }
940 DexCache* GetDexCache() {
941 CHECK(dex_cache_ != NULL);
942 return dex_cache_;
943 }
944 const DexFile* GetDexFile() {
945 CHECK(dex_file_ != NULL);
946 return dex_file_;
947 }
948
949 private:
950 ClassLinker* class_linker_;
Ian Rogers365c1022012-06-22 15:05:28 -0700951 ClassLoader* class_loader_;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800952 Compiler* compiler_;
953 DexCache* dex_cache_;
954 const DexFile* dex_file_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800955};
956
Ian Rogers0399dde2012-06-06 17:09:28 -0700957typedef void Callback(CompilationContext* context, size_t index);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800958
959class WorkerThread {
960 public:
Ian Rogers0399dde2012-06-06 17:09:28 -0700961 WorkerThread(CompilationContext* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
Elliott Hughes1e409252012-02-06 11:21:27 -0800962 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
963 if (spawn_) {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700964 // Mac OS stacks are only 512KiB. Make sure we have the same stack size on all platforms.
965 pthread_attr_t attr;
966 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new compiler worker thread");
967 CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, 1*MB), "new compiler worker thread");
968 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Go, this), "new compiler worker thread");
969 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new compiler worker thread");
Elliott Hughes1e409252012-02-06 11:21:27 -0800970 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800971 }
972
973 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800974 if (spawn_) {
975 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
976 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800977 }
978
979 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800980 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800981 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
982 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800983 if (worker->spawn_) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700984 runtime->AttachCurrentThread("Compiler Worker", true, NULL);
Elliott Hughes1e409252012-02-06 11:21:27 -0800985 }
Elliott Hughes34e06962012-04-09 13:55:55 -0700986 Thread::Current()->SetState(kRunnable);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800987 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800988 if (worker->spawn_) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700989 Thread::Current()->SetState(kNative);
Elliott Hughes1e409252012-02-06 11:21:27 -0800990 runtime->DetachCurrentThread();
991 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800992 return NULL;
993 }
994
Elliott Hughes1e409252012-02-06 11:21:27 -0800995 void Go() {
996 Go(this);
997 }
998
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800999 void Run() {
Brian Carlstrom64277f32012-03-26 23:53:34 -07001000 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001001 for (size_t i = begin_; i < end_; i += stripe_) {
1002 callback_(context_, i);
Brian Carlstrom64277f32012-03-26 23:53:34 -07001003 CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException()) << " " << i;
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001004 }
1005 }
1006
1007 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -08001008 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001009
Ian Rogers0399dde2012-06-06 17:09:28 -07001010 CompilationContext* context_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001011 size_t begin_;
1012 size_t end_;
1013 Callback* callback_;
1014 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -08001015
Ian Rogers0399dde2012-06-06 17:09:28 -07001016 friend void ForAll(CompilationContext*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001017};
1018
Ian Rogers0399dde2012-06-06 17:09:28 -07001019void ForAll(CompilationContext* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Brian Carlstrom64277f32012-03-26 23:53:34 -07001020 Thread* self = Thread::Current();
1021 CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException());
Elliott Hughes1e409252012-02-06 11:21:27 -08001022 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -08001023
Elliott Hughes1e409252012-02-06 11:21:27 -08001024 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -08001025 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -08001026 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001027 }
Elliott Hughes1e409252012-02-06 11:21:27 -08001028 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001029
Elliott Hughes81d91512012-02-03 16:38:43 -08001030 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
Elliott Hughes34e06962012-04-09 13:55:55 -07001031 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes81d91512012-02-03 16:38:43 -08001032 STLDeleteElements(&threads);
1033}
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001034
Ian Rogers0399dde2012-06-06 17:09:28 -07001035static void ResolveClassFieldsAndMethods(CompilationContext* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001036 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001037
1038 // Method and Field are the worst. We can't resolve without either
1039 // context from the code use (to disambiguate virtual vs direct
1040 // method and instance vs static field) or from class
1041 // definitions. While the compiler will resolve what it can as it
1042 // needs it, here we try to resolve fields and methods used in class
1043 // definitions, since many of them many never be referenced by
1044 // generated code.
1045 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001046 if (SkipClass(context->GetClassLoader(), dex_file, class_def)) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001047 return;
1048 }
1049
1050 // Note the class_data pointer advances through the headers,
1051 // static fields, instance fields, direct methods, and virtual
1052 // methods.
1053 const byte* class_data = dex_file.GetClassData(class_def);
1054 if (class_data == NULL) {
1055 // empty class such as a marker interface
1056 return;
1057 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001058 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001059 ClassLinker* class_linker = context->GetClassLinker();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001060 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1061 ClassDataItemIterator it(dex_file, class_data);
1062 while (it.HasNextStaticField()) {
1063 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001064 context->GetClassLoader(), true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001065 if (field == NULL) {
1066 CHECK(self->IsExceptionPending());
1067 self->ClearException();
1068 }
1069 it.Next();
1070 }
1071 while (it.HasNextInstanceField()) {
1072 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001073 context->GetClassLoader(), false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001074 if (field == NULL) {
1075 CHECK(self->IsExceptionPending());
1076 self->ClearException();
1077 }
1078 it.Next();
1079 }
1080 while (it.HasNextDirectMethod()) {
1081 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001082 context->GetClassLoader(), true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001083 if (method == NULL) {
1084 CHECK(self->IsExceptionPending());
1085 self->ClearException();
1086 }
1087 it.Next();
1088 }
1089 while (it.HasNextVirtualMethod()) {
1090 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001091 context->GetClassLoader(), false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001092 if (method == NULL) {
1093 CHECK(self->IsExceptionPending());
1094 self->ClearException();
1095 }
1096 it.Next();
1097 }
1098 DCHECK(!it.HasNext());
1099}
1100
Ian Rogers0399dde2012-06-06 17:09:28 -07001101static void ResolveType(CompilationContext* context, size_t type_idx) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001102 // Class derived values are more complicated, they require the linker and loader.
1103 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001104 Class* klass = context->GetClassLinker()->ResolveType(*context->GetDexFile(),
1105 type_idx,
1106 context->GetDexCache(),
1107 context->GetClassLoader());
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001108 if (klass == NULL) {
1109 CHECK(self->IsExceptionPending());
1110 Thread::Current()->ClearException();
1111 }
1112}
1113
Ian Rogers365c1022012-06-22 15:05:28 -07001114void Compiler::ResolveDexFile(ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001115 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001116 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001117
Brian Carlstromae826982011-11-09 01:33:42 -08001118 // Strings are easy in that they always are simply resolved to literals in the same file
1119 if (image_ && image_classes_ == NULL) {
1120 // TODO: Add support for loading strings referenced by image_classes_
1121 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -07001122 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
1123 class_linker->ResolveString(dex_file, string_idx, dex_cache);
1124 }
Elliott Hughesff738062012-02-03 15:00:42 -08001125 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001126 }
1127
Ian Rogers0399dde2012-06-06 17:09:28 -07001128 CompilationContext context(class_linker, class_loader, this, dex_cache, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001129 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001130 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -07001131
Elliott Hughes5523ee02012-02-03 18:18:34 -08001132 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001133 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001134}
1135
Ian Rogers365c1022012-06-22 15:05:28 -07001136void Compiler::Verify(ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -08001137 const std::vector<const DexFile*>& dex_files) {
1138 for (size_t i = 0; i != dex_files.size(); ++i) {
1139 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -07001140 CHECK(dex_file != NULL);
1141 VerifyDexFile(class_loader, *dex_file);
1142 }
1143}
1144
Ian Rogers0399dde2012-06-06 17:09:28 -07001145static void VerifyClass(CompilationContext* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001146 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
1147 const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def);
1148 Class* klass = context->GetClassLinker()->FindClass(descriptor, context->GetClassLoader());
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001149 if (klass == NULL) {
1150 Thread* self = Thread::Current();
1151 CHECK(self->IsExceptionPending());
1152 self->ClearException();
jeffhaof56197c2012-03-05 18:01:54 -08001153
1154 /*
1155 * At compile time, we can still structurally verify the class even if FindClass fails.
1156 * This is to ensure the class is structurally sound for compilation. An unsound class
1157 * will be rejected by the verifier and later skipped during compilation in the compiler.
1158 */
1159 std::string error_msg;
jeffhaof1e6b7c2012-06-05 18:33:30 -07001160 if (verifier::MethodVerifier::VerifyClass(context->GetDexFile(), context->GetDexCache(),
1161 context->GetClassLoader(), class_def_index, error_msg) == verifier::MethodVerifier::kHardFailure) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001162 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
jeffhaof56197c2012-03-05 18:01:54 -08001163 LOG(ERROR) << "Verification failed on class "
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001164 << PrettyDescriptor(context->GetDexFile()->GetClassDescriptor(class_def))
jeffhaof56197c2012-03-05 18:01:54 -08001165 << " because: " << error_msg;
1166 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001167 return;
1168 }
1169 CHECK(klass->IsResolved()) << PrettyClass(klass);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001170 context->GetClassLinker()->VerifyClass(klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001171
1172 if (klass->IsErroneous()) {
1173 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1174 CHECK(Thread::Current()->IsExceptionPending());
1175 Thread::Current()->ClearException();
jeffhao1ac29442012-03-26 11:37:32 -07001176 art::Compiler::ClassReference ref(context->GetDexFile(), class_def_index);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001177 }
1178
jeffhaof1e6b7c2012-06-05 18:33:30 -07001179 CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous()) << PrettyClass(klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001180 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
1181}
1182
Ian Rogers365c1022012-06-22 15:05:28 -07001183void Compiler::VerifyDexFile(ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -07001184 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -07001185
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001186 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers0399dde2012-06-06 17:09:28 -07001187 CompilationContext context(class_linker, class_loader, this, class_linker->FindDexCache(dex_file), &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001188 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -07001189
jeffhaob4df5142011-09-19 20:25:32 -07001190 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001191}
1192
Ian Rogers365c1022012-06-22 15:05:28 -07001193void Compiler::InitializeClassesWithoutClinit(ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -08001194 const std::vector<const DexFile*>& dex_files) {
1195 for (size_t i = 0; i != dex_files.size(); ++i) {
1196 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001197 CHECK(dex_file != NULL);
1198 InitializeClassesWithoutClinit(class_loader, *dex_file);
1199 }
1200}
1201
Ian Rogers365c1022012-06-22 15:05:28 -07001202void Compiler::InitializeClassesWithoutClinit(ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001203 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -07001204 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1205 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001206 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1207 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001208 if (klass != NULL) {
jeffhao1ac29442012-03-26 11:37:32 -07001209 if (klass->IsVerified()) {
1210 // Only try to initialize classes that were successfully verified.
Ian Rogers0045a292012-03-31 21:08:41 -07001211 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
1212 bool can_init_static_fields = compiling_boot &&
1213 IsImageClass(descriptor);
1214 class_linker->EnsureInitialized(klass, false, can_init_static_fields);
jeffhao1ac29442012-03-26 11:37:32 -07001215 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001216 // record the final class status if necessary
1217 Class::Status status = klass->GetStatus();
1218 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001219 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001220 CompiledClass* compiled_class = GetCompiledClass(ref);
1221 if (compiled_class == NULL) {
1222 compiled_class = new CompiledClass(status);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001223 compiled_classes_.Put(ref, compiled_class);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001224 } else {
1225 DCHECK_EQ(status, compiled_class->GetStatus());
1226 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001227 }
1228 // clear any class not found or verification exceptions
1229 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -07001230 }
1231
1232 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1233 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
1234 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001235 if (klass == NULL) {
1236 Thread::Current()->ClearException();
1237 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -07001238 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
1239 }
jeffhao98eacac2011-09-14 16:11:53 -07001240 }
1241}
1242
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001243// TODO: This class shares some implementation with WorkerThread. We don't want
1244// to perturb the non-LLVM side at the same time. Staging the refactoring for
1245// the future.
1246class DexFilesWorkerThread {
1247 public:
Ian Rogers0399dde2012-06-06 17:09:28 -07001248 DexFilesWorkerThread(CompilationContext *worker_context, Callback class_callback,
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001249 const std::vector<const DexFile*>& dex_files,
1250 volatile int32_t* shared_class_index, bool spawn)
1251 : spawn_(spawn), worker_context_(worker_context),
1252 class_callback_(class_callback), dex_files_(dex_files),
1253 context_(NULL), shared_class_index_(shared_class_index) {
1254 if (spawn_) {
TDYa1278cea5fd2012-05-29 22:22:32 -07001255 pthread_attr_t attr;
1256 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new compiler worker thread");
TDYa12761b409c2012-06-05 23:54:30 -07001257 CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, 1*MB), "new compiler worker thread");
TDYa1278cea5fd2012-05-29 22:22:32 -07001258 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Go, this), "new compiler worker thread");
1259 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new compiler worker thread");
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001260 }
1261 }
1262
1263 ~DexFilesWorkerThread() {
1264 if (spawn_) {
1265 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
1266 }
1267 delete context_;
1268 }
1269
1270 private:
1271 static void* Go(void* arg) {
1272 DexFilesWorkerThread* worker = reinterpret_cast<DexFilesWorkerThread*>(arg);
1273 Runtime* runtime = Runtime::Current();
1274 if (worker->spawn_) {
1275 runtime->AttachCurrentThread("Compiler Worker", true, NULL);
1276 }
1277 Thread::Current()->SetState(kRunnable);
1278 worker->Run();
1279 if (worker->spawn_) {
1280 Thread::Current()->SetState(kNative);
1281 runtime->DetachCurrentThread();
1282 }
1283 return NULL;
1284 }
1285
1286 void Go() {
1287 Go(this);
1288 }
1289
1290 void SwitchToDexFile(size_t dex_file_index) {
Elliott Hughesa21039c2012-06-21 12:09:25 -07001291 CHECK_LT(dex_file_index, dex_files_.size());
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001292
1293 const DexFile* dex_file = dex_files_[dex_file_index];
1294 CHECK(dex_file != NULL);
1295
1296 // Destroy the old context
1297 delete context_;
1298
1299 // TODO: Add a callback to let the client specify the class_linker and
1300 // dex_cache in the context for the current working dex file.
Ian Rogers0399dde2012-06-06 17:09:28 -07001301 context_ = new CompilationContext(/* class_linker */NULL,
1302 worker_context_->GetClassLoader(),
1303 worker_context_->GetCompiler(),
1304 /* dex_cache */NULL, dex_file);
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001305
1306 CHECK(context_ != NULL);
1307 }
1308
1309 void Run() {
1310 Thread* self = Thread::Current();
1311 size_t cur_dex_file_index = 0;
1312 size_t class_index_base = 0;
1313
1314 SwitchToDexFile(0);
1315
1316 while (true) {
1317 size_t class_index =
1318 static_cast<size_t>(android_atomic_inc(shared_class_index_));
1319
1320 const DexFile* dex_file;
1321 do {
1322 dex_file = dex_files_[cur_dex_file_index];
1323
1324 if (class_index < (class_index_base + dex_file->NumClassDefs())) {
1325 break;
1326 }
1327 class_index_base += dex_file->NumClassDefs();
1328
1329 cur_dex_file_index++;
1330 } while (cur_dex_file_index < dex_files_.size());
1331
1332 if (cur_dex_file_index >= dex_files_.size()) {
1333 return;
1334 }
1335
1336 if (dex_file != context_->GetDexFile()) {
1337 SwitchToDexFile(cur_dex_file_index);
1338 }
1339
1340 class_index -= class_index_base;
1341 class_callback_(context_, class_index);
1342 CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException());
1343 }
1344 }
1345
1346 pthread_t pthread_;
1347 bool spawn_;
1348
Ian Rogers0399dde2012-06-06 17:09:28 -07001349 CompilationContext* worker_context_;
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001350 Callback* class_callback_;
1351 const std::vector<const DexFile*>& dex_files_;
1352
Ian Rogers0399dde2012-06-06 17:09:28 -07001353 CompilationContext* context_;
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001354 volatile int32_t* shared_class_index_;
1355
Ian Rogers0399dde2012-06-06 17:09:28 -07001356 friend void ForClassesInAllDexFiles(CompilationContext*,
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001357 const std::vector<const DexFile*>&,
1358 Callback, size_t);
1359};
1360
Ian Rogers0399dde2012-06-06 17:09:28 -07001361void ForClassesInAllDexFiles(CompilationContext* worker_context,
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001362 const std::vector<const DexFile*>& dex_files,
1363 Callback class_callback, size_t thread_count) {
1364 Thread* self = Thread::Current();
1365 CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException());
1366 CHECK_GT(thread_count, 0U);
1367
1368 std::vector<DexFilesWorkerThread*> threads;
1369 volatile int32_t shared_class_index = 0;
1370
1371 for (size_t i = 0; i < thread_count; ++i) {
1372 threads.push_back(new DexFilesWorkerThread(worker_context, class_callback,
1373 dex_files, &shared_class_index,
1374 /* spawn */ (i != 0)));
1375 }
1376 threads[0]->Go();
1377
1378 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
1379 ScopedThreadStateChange tsc(self, kVmWait);
1380 STLDeleteElements(&threads);
1381}
1382
Ian Rogers365c1022012-06-22 15:05:28 -07001383void Compiler::Compile(ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -08001384 const std::vector<const DexFile*>& dex_files) {
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001385#if defined(ART_USE_LLVM_COMPILER)
1386 if (dex_files.size() <= 0) {
1387 return; // No dex file
1388 }
Ian Rogers0399dde2012-06-06 17:09:28 -07001389 CompilationContext context(NULL, class_loader, this, NULL, NULL);
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001390 ForClassesInAllDexFiles(&context, dex_files, Compiler::CompileClass, thread_count_);
1391#else
Brian Carlstromae826982011-11-09 01:33:42 -08001392 for (size_t i = 0; i != dex_files.size(); ++i) {
1393 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07001394 CHECK(dex_file != NULL);
1395 CompileDexFile(class_loader, *dex_file);
1396 }
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001397#endif
Brian Carlstrom83db7722011-08-26 17:32:56 -07001398}
1399
Ian Rogers0399dde2012-06-06 17:09:28 -07001400void Compiler::CompileClass(CompilationContext* context, size_t class_def_index) {
TDYa12750b69e32012-06-26 18:11:27 -07001401 ClassLoader* class_loader = context->GetClassLoader();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001402 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesc225caa2012-02-03 15:43:37 -08001403 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001404 if (SkipClass(class_loader, dex_file, class_def)) {
1405 return;
1406 }
jeffhaod1224c72012-02-29 13:43:08 -08001407 ClassReference ref(&dex_file, class_def_index);
1408 // Skip compiling classes with generic verifier failures since they will still fail at runtime
Ian Rogers776ac1f2012-04-13 23:36:36 -07001409 if (verifier::MethodVerifier::IsClassRejected(ref)) {
jeffhaod1224c72012-02-29 13:43:08 -08001410 return;
1411 }
Ian Rogers0571d352011-11-03 19:51:38 -07001412 const byte* class_data = dex_file.GetClassData(class_def);
1413 if (class_data == NULL) {
1414 // empty class, probably a marker interface
1415 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001416 }
Ian Rogers0571d352011-11-03 19:51:38 -07001417 ClassDataItemIterator it(dex_file, class_data);
1418 // Skip fields
1419 while (it.HasNextStaticField()) {
1420 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001421 }
Ian Rogers0571d352011-11-03 19:51:38 -07001422 while (it.HasNextInstanceField()) {
1423 it.Next();
1424 }
1425 // Compile direct methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001426 int64_t previous_direct_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07001427 while (it.HasNextDirectMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001428 uint32_t method_idx = it.GetMemberIndex();
1429 if (method_idx == previous_direct_method_idx) {
1430 // smali can create dex files with two encoded_methods sharing the same method_idx
1431 // http://code.google.com/p/smali/issues/detail?id=119
1432 it.Next();
1433 continue;
1434 }
1435 previous_direct_method_idx = method_idx;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001436 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001437 method_idx, class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001438 it.Next();
1439 }
1440 // Compile virtual methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001441 int64_t previous_virtual_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07001442 while (it.HasNextVirtualMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001443 uint32_t method_idx = it.GetMemberIndex();
1444 if (method_idx == previous_virtual_method_idx) {
1445 // smali can create dex files with two encoded_methods sharing the same method_idx
1446 // http://code.google.com/p/smali/issues/detail?id=119
1447 it.Next();
1448 continue;
1449 }
1450 previous_virtual_method_idx = method_idx;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001451 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001452 method_idx, class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001453 it.Next();
1454 }
1455 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001456}
1457
Ian Rogers365c1022012-06-22 15:05:28 -07001458void Compiler::CompileDexFile(ClassLoader* class_loader, const DexFile& dex_file) {
Ian Rogers0399dde2012-06-06 17:09:28 -07001459 CompilationContext context(NULL, class_loader, this, NULL, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001460 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001461}
1462
Elliott Hughesa0e18062012-04-13 15:59:59 -07001463static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1464 std::string key(shorty);
1465 if (is_static) {
1466 key += "$"; // Must not be a shorty type character.
1467 }
1468 return key;
1469}
1470
Ian Rogersa3760aa2011-11-14 14:32:37 -08001471void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
TDYa12750b69e32012-06-26 18:11:27 -07001472 uint32_t method_idx, ClassLoader* class_loader,
Ian Rogersa3760aa2011-11-14 14:32:37 -08001473 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -07001474 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001475 uint64_t start_ns = NanoTime();
Logan Chien4dd96f52012-02-29 01:26:58 +08001476
Ian Rogers169c9a72011-11-13 20:13:17 -08001477 if ((access_flags & kAccNative) != 0) {
Ian Rogers57b86d42012-03-27 16:05:41 -07001478 compiled_method = (*jni_compiler_)(*this, access_flags, method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001479 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -08001480 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001481 } else {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001482 compiled_method = (*compiler_)(*this, code_item, access_flags, method_idx, class_loader,
1483 dex_file);
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001484 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
1485 }
Ian Rogers3bb17a62012-01-27 23:56:44 -08001486 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -08001487 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001488 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -08001489 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -07001490 }
1491
1492 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -07001493 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001494 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001495 MutexLock mu(compiled_methods_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001496 compiled_methods_.Put(ref, compiled_method);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001497 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001498 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001499
Ian Rogers45619fc2012-02-29 11:15:25 -08001500 uint32_t shorty_len;
1501 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx), &shorty_len);
Ian Rogers169c9a72011-11-13 20:13:17 -08001502 bool is_static = (access_flags & kAccStatic) != 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001503 std::string key(MakeInvokeStubKey(is_static, shorty));
1504 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(key);
Ian Rogers0571d352011-11-03 19:51:38 -07001505 if (compiled_invoke_stub == NULL) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -08001506 compiled_invoke_stub = (*create_invoke_stub_)(*this, is_static, shorty, shorty_len);
Ian Rogers0571d352011-11-03 19:51:38 -07001507 CHECK(compiled_invoke_stub != NULL);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001508 InsertInvokeStub(key, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001509 }
Logan Chien7a2a23a2012-06-06 11:01:00 +08001510
1511#if defined(ART_USE_LLVM_COMPILER)
1512 if (!is_static) {
1513 const CompiledInvokeStub* compiled_proxy_stub = FindProxyStub(shorty);
1514 if (compiled_proxy_stub == NULL) {
1515 compiled_proxy_stub = (*create_proxy_stub_)(*this, shorty, shorty_len);
1516 CHECK(compiled_proxy_stub != NULL);
1517 InsertProxyStub(shorty, compiled_proxy_stub);
1518 }
1519 }
1520#endif
1521
Ian Rogers0571d352011-11-03 19:51:38 -07001522 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001523}
1524
Elliott Hughesa0e18062012-04-13 15:59:59 -07001525const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
1526 const std::string key(MakeInvokeStubKey(is_static, shorty));
1527 return FindInvokeStub(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001528}
1529
Elliott Hughesa0e18062012-04-13 15:59:59 -07001530const CompiledInvokeStub* Compiler::FindInvokeStub(const std::string& key) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001531 MutexLock mu(compiled_invoke_stubs_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001532 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001533 if (it == compiled_invoke_stubs_.end()) {
1534 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001535 } else {
1536 DCHECK(it->second != NULL);
1537 return it->second;
1538 }
1539}
1540
Elliott Hughesa0e18062012-04-13 15:59:59 -07001541void Compiler::InsertInvokeStub(const std::string& key,
Ian Rogers0571d352011-11-03 19:51:38 -07001542 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001543 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001544 InvokeStubTable::iterator it = compiled_invoke_stubs_.find(key);
1545 if (it != compiled_invoke_stubs_.end()) {
1546 // Someone else won the race.
1547 delete compiled_invoke_stub;
1548 } else {
1549 compiled_invoke_stubs_.Put(key, compiled_invoke_stub);
1550 }
Ian Rogers0571d352011-11-03 19:51:38 -07001551}
1552
Logan Chien7a2a23a2012-06-06 11:01:00 +08001553#if defined(ART_USE_LLVM_COMPILER)
1554const CompiledInvokeStub* Compiler::FindProxyStub(const char* shorty) const {
1555 MutexLock mu(compiled_proxy_stubs_lock_);
1556 ProxyStubTable::const_iterator it = compiled_proxy_stubs_.find(shorty);
1557 if (it == compiled_proxy_stubs_.end()) {
1558 return NULL;
1559 } else {
1560 DCHECK(it->second != NULL);
1561 return it->second;
1562 }
1563}
1564
1565void Compiler::InsertProxyStub(const char* shorty,
1566 const CompiledInvokeStub* compiled_proxy_stub) {
1567 MutexLock mu(compiled_proxy_stubs_lock_);
1568 InvokeStubTable::iterator it = compiled_proxy_stubs_.find(shorty);
1569 if (it != compiled_proxy_stubs_.end()) {
1570 // Someone else won the race.
1571 delete compiled_proxy_stub;
1572 } else {
1573 compiled_proxy_stubs_.Put(shorty, compiled_proxy_stub);
1574 }
1575}
1576#endif
1577
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001578CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001579 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001580 ClassTable::const_iterator it = compiled_classes_.find(ref);
1581 if (it == compiled_classes_.end()) {
1582 return NULL;
1583 }
1584 CHECK(it->second != NULL);
1585 return it->second;
1586}
1587
Ian Rogers0571d352011-11-03 19:51:38 -07001588CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001589 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001590 MethodTable::const_iterator it = compiled_methods_.find(ref);
1591 if (it == compiled_methods_.end()) {
1592 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001593 }
1594 CHECK(it->second != NULL);
1595 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001596}
1597
Ian Rogers365c1022012-06-22 15:05:28 -07001598void Compiler::SetGcMaps(ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001599 for (size_t i = 0; i != dex_files.size(); ++i) {
1600 const DexFile* dex_file = dex_files[i];
1601 CHECK(dex_file != NULL);
1602 SetGcMapsDexFile(class_loader, *dex_file);
1603 }
1604}
1605
Ian Rogers365c1022012-06-22 15:05:28 -07001606void Compiler::SetGcMapsDexFile(ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001607 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1608 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1609 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1610 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1611 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1612 Class* klass = class_linker->FindClass(descriptor, class_loader);
1613 if (klass == NULL || !klass->IsVerified()) {
1614 Thread::Current()->ClearException();
1615 continue;
1616 }
1617 const byte* class_data = dex_file.GetClassData(class_def);
1618 if (class_data == NULL) {
1619 // empty class such as a marker interface
1620 continue;
1621 }
1622 ClassDataItemIterator it(dex_file, class_data);
1623 while (it.HasNextStaticField()) {
1624 it.Next();
1625 }
1626 while (it.HasNextInstanceField()) {
1627 it.Next();
1628 }
1629 while (it.HasNextDirectMethod()) {
1630 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1631 class_loader, true);
1632 SetGcMapsMethod(dex_file, method);
1633 it.Next();
1634 }
1635 while (it.HasNextVirtualMethod()) {
1636 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1637 class_loader, false);
1638 SetGcMapsMethod(dex_file, method);
1639 it.Next();
1640 }
1641 }
1642}
1643
1644void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
1645 if (method == NULL) {
1646 Thread::Current()->ClearException();
1647 return;
1648 }
1649 uint16_t method_idx = method->GetDexMethodIndex();
1650 MethodReference ref(&dex_file, method_idx);
1651 CompiledMethod* compiled_method = GetCompiledMethod(ref);
1652 if (compiled_method == NULL) {
1653 return;
1654 }
Ian Rogers776ac1f2012-04-13 23:36:36 -07001655 const std::vector<uint8_t>* gc_map = verifier::MethodVerifier::GetGcMap(ref);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001656 if (gc_map == NULL) {
1657 return;
1658 }
1659 compiled_method->SetGcMap(*gc_map);
1660}
1661
buzbee2cfc6392012-05-07 14:51:40 -07001662#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_QUICK_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +08001663void Compiler::SetBitcodeFileName(std::string const& filename) {
Logan Chien106b2a02012-03-18 04:41:38 +08001664 typedef void (*SetBitcodeFileNameFn)(Compiler&, std::string const&);
1665
1666 SetBitcodeFileNameFn set_bitcode_file_name =
1667 FindFunction<SetBitcodeFileNameFn>(MakeCompilerSoName(instruction_set_),
1668 compiler_library_,
1669 "compilerLLVMSetBitcodeFileName");
1670
1671 set_bitcode_file_name(*this, filename);
Logan Chien8b977d32012-02-21 19:14:55 +08001672}
buzbee2cfc6392012-05-07 14:51:40 -07001673#endif
Logan Chienf7015fd2012-03-18 01:19:37 +08001674
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001675} // namespace art