blob: 186cf0d4d378977531186156840bd9b7d475c239 [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
Ian Rogers1212a022013-03-04 10:48:41 -080017#include "compiler_driver.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070018
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
Elliott Hughes1aa246d2012-12-13 09:29:36 -080024#include "base/stl_util.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080025#include "base/timing_logger.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070026#include "class_linker.h"
Jeff Hao0aba0ba2013-06-03 14:49:28 -070027#include "compiler/stubs/stubs.h"
Ian Rogers89756f22013-03-04 16:40:02 -080028#include "dex_compilation_unit.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070030#include "jni_internal.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "gc/card_table-inl.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070035#include "gc/space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class_loader.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/field-inl.h"
40#include "mirror/abstract_method-inl.h"
41#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
43#include "mirror/throwable.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044#include "scoped_thread_state_change.h"
45#include "ScopedLocalRef.h"
Ian Rogers50b35e22012-10-04 10:09:15 -070046#include "thread.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070047#include "thread_pool.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070048#include "verifier/method_verifier.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070049
Elliott Hughes059d5c12012-03-12 17:39:18 -070050#if defined(__APPLE__)
51#include <mach-o/dyld.h>
52#endif
53
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070054namespace art {
55
Ian Rogers996cc582012-02-14 22:23:29 -080056static double Percentage(size_t x, size_t y) {
Elliott Hughes398f64b2012-03-26 18:05:48 -070057 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
Ian Rogers996cc582012-02-14 22:23:29 -080058}
59
60static void DumpStat(size_t x, size_t y, const char* str) {
61 if (x == 0 && y == 0) {
62 return;
63 }
64 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
65}
66
Ian Rogersc8b306f2012-02-17 21:34:44 -080067class AOTCompilationStats {
68 public:
Ian Rogersca190662012-06-26 15:45:57 -070069 AOTCompilationStats()
70 : stats_lock_("AOT compilation statistics lock"),
71 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
72 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
73 resolved_types_(0), unresolved_types_(0),
74 resolved_instance_fields_(0), unresolved_instance_fields_(0),
Sameer Abu Asal02c42232013-04-30 12:09:45 -070075 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
Ian Rogersfae370a2013-06-05 08:33:27 -070076 type_based_devirtualization_(0),
77 safe_casts_(0), not_safe_casts_(0) {
Ian Rogers2ed3b952012-03-17 11:49:39 -070078 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080079 resolved_methods_[i] = 0;
80 unresolved_methods_[i] = 0;
Ian Rogers2ed3b952012-03-17 11:49:39 -070081 virtual_made_direct_[i] = 0;
82 direct_calls_to_boot_[i] = 0;
83 direct_methods_to_boot_[i] = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -070084 }
Ian Rogersc8b306f2012-02-17 21:34:44 -080085 }
86
87 void Dump() {
88 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
89 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
90 DumpStat(resolved_types_, unresolved_types_, "types resolved");
91 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
92 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
93 "static fields resolved");
94 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
95 "static fields local to a class");
Ian Rogersfae370a2013-06-05 08:33:27 -070096 DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information");
97 // Note, the code below subtracts the stat value so that when added to the stat value we have
98 // 100% of samples. TODO: clean this up.
99 DumpStat(type_based_devirtualization_,
100 resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] +
101 resolved_methods_[kInterface] + unresolved_methods_[kInterface] -
102 type_based_devirtualization_,
103 "virtual/interface calls made direct based on type information");
Ian Rogersc8b306f2012-02-17 21:34:44 -0800104
Ian Rogers2ed3b952012-03-17 11:49:39 -0700105 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800106 std::ostringstream oss;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700107 oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
Ian Rogersc8b306f2012-02-17 21:34:44 -0800108 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
Ian Rogers2ed3b952012-03-17 11:49:39 -0700109 if (virtual_made_direct_[i] > 0) {
110 std::ostringstream oss2;
111 oss2 << static_cast<InvokeType>(i) << " methods made direct";
112 DumpStat(virtual_made_direct_[i],
113 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
114 oss2.str().c_str());
115 }
116 if (direct_calls_to_boot_[i] > 0) {
117 std::ostringstream oss2;
118 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
119 DumpStat(direct_calls_to_boot_[i],
120 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
121 oss2.str().c_str());
122 }
123 if (direct_methods_to_boot_[i] > 0) {
124 std::ostringstream oss2;
125 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
126 DumpStat(direct_methods_to_boot_[i],
127 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
128 oss2.str().c_str());
129 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800130 }
131 }
Ian Rogers996cc582012-02-14 22:23:29 -0800132
Ian Rogers50b35e22012-10-04 10:09:15 -0700133// Allow lossy statistics in non-debug builds.
Ian Rogers996cc582012-02-14 22:23:29 -0800134#ifndef NDEBUG
Ian Rogers50b35e22012-10-04 10:09:15 -0700135#define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_)
Ian Rogers996cc582012-02-14 22:23:29 -0800136#else
137#define STATS_LOCK()
138#endif
139
Ian Rogersc8b306f2012-02-17 21:34:44 -0800140 void TypeInDexCache() {
141 STATS_LOCK();
142 types_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800143 }
Ian Rogers996cc582012-02-14 22:23:29 -0800144
Ian Rogersc8b306f2012-02-17 21:34:44 -0800145 void TypeNotInDexCache() {
146 STATS_LOCK();
147 types_not_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800148 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800149
150 void StringInDexCache() {
151 STATS_LOCK();
152 strings_in_dex_cache_++;
153 }
154
155 void StringNotInDexCache() {
156 STATS_LOCK();
157 strings_not_in_dex_cache_++;
158 }
159
160 void TypeDoesntNeedAccessCheck() {
161 STATS_LOCK();
162 resolved_types_++;
163 }
164
165 void TypeNeedsAccessCheck() {
166 STATS_LOCK();
167 unresolved_types_++;
168 }
169
170 void ResolvedInstanceField() {
171 STATS_LOCK();
172 resolved_instance_fields_++;
173 }
174
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700175 void UnresolvedInstanceField() {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800176 STATS_LOCK();
177 unresolved_instance_fields_++;
178 }
179
180 void ResolvedLocalStaticField() {
181 STATS_LOCK();
182 resolved_local_static_fields_++;
183 }
184
185 void ResolvedStaticField() {
186 STATS_LOCK();
187 resolved_static_fields_++;
188 }
189
190 void UnresolvedStaticField() {
191 STATS_LOCK();
192 unresolved_static_fields_++;
193 }
194
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700195 // Indicate that type information from the verifier led to devirtualization.
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700196 void PreciseTypeDevirtualization() {
197 STATS_LOCK();
198 type_based_devirtualization_++;
199 }
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700200
201 // Indicate that a method of the given type was resolved at compile time.
Ian Rogersc8b306f2012-02-17 21:34:44 -0800202 void ResolvedMethod(InvokeType type) {
203 DCHECK_LE(type, kMaxInvokeType);
204 STATS_LOCK();
205 resolved_methods_[type]++;
206 }
207
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700208 // Indicate that a method of the given type was unresolved at compile time as it was in an
209 // unknown dex file.
Ian Rogersc8b306f2012-02-17 21:34:44 -0800210 void UnresolvedMethod(InvokeType type) {
211 DCHECK_LE(type, kMaxInvokeType);
212 STATS_LOCK();
213 unresolved_methods_[type]++;
214 }
215
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700216 // Indicate that a type of virtual method dispatch has been converted into a direct method
217 // dispatch.
Ian Rogers2ed3b952012-03-17 11:49:39 -0700218 void VirtualMadeDirect(InvokeType type) {
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700219 DCHECK(type == kVirtual || type == kInterface || type == kSuper);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800220 STATS_LOCK();
Ian Rogers2ed3b952012-03-17 11:49:39 -0700221 virtual_made_direct_[type]++;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800222 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700223
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700224 // Indicate that a method of the given type was able to call directly into boot.
Ian Rogers2ed3b952012-03-17 11:49:39 -0700225 void DirectCallsToBoot(InvokeType type) {
226 DCHECK_LE(type, kMaxInvokeType);
227 STATS_LOCK();
228 direct_calls_to_boot_[type]++;
229 }
230
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700231 // Indicate that a method of the given type was able to be resolved directly from boot.
Ian Rogers2ed3b952012-03-17 11:49:39 -0700232 void DirectMethodsToBoot(InvokeType type) {
233 DCHECK_LE(type, kMaxInvokeType);
234 STATS_LOCK();
235 direct_methods_to_boot_[type]++;
236 }
237
Ian Rogersfae370a2013-06-05 08:33:27 -0700238 // A check-cast could be eliminated due to verifier type analysis.
239 void SafeCast() {
240 STATS_LOCK();
241 safe_casts_++;
242 }
243
244 // A check-cast couldn't be eliminated due to verifier type analysis.
245 void NotASafeCast() {
246 STATS_LOCK();
247 not_safe_casts_++;
248 }
249
Ian Rogersc8b306f2012-02-17 21:34:44 -0800250 private:
251 Mutex stats_lock_;
252
253 size_t types_in_dex_cache_;
254 size_t types_not_in_dex_cache_;
255
256 size_t strings_in_dex_cache_;
257 size_t strings_not_in_dex_cache_;
258
259 size_t resolved_types_;
260 size_t unresolved_types_;
261
262 size_t resolved_instance_fields_;
263 size_t unresolved_instance_fields_;
264
265 size_t resolved_local_static_fields_;
266 size_t resolved_static_fields_;
267 size_t unresolved_static_fields_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700268 // Type based devirtualization for invoke interface and virtual.
269 size_t type_based_devirtualization_;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800270
271 size_t resolved_methods_[kMaxInvokeType + 1];
272 size_t unresolved_methods_[kMaxInvokeType + 1];
Ian Rogers2ed3b952012-03-17 11:49:39 -0700273 size_t virtual_made_direct_[kMaxInvokeType + 1];
274 size_t direct_calls_to_boot_[kMaxInvokeType + 1];
275 size_t direct_methods_to_boot_[kMaxInvokeType + 1];
Ian Rogersc8b306f2012-02-17 21:34:44 -0800276
Ian Rogersfae370a2013-06-05 08:33:27 -0700277 size_t safe_casts_;
278 size_t not_safe_casts_;
279
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700280 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800281};
Ian Rogers996cc582012-02-14 22:23:29 -0800282
buzbee8c4bbb52012-11-26 14:00:58 -0800283static std::string MakeCompilerSoName(CompilerBackend compiler_backend) {
Elliott Hughes059d5c12012-03-12 17:39:18 -0700284
285 // Bad things happen if we pull in the libartd-compiler to a libart dex2oat or vice versa,
286 // because we end up with both libart and libartd in the same address space!
Elliott Hughes67d92002012-03-26 15:08:51 -0700287 const char* suffix = (kIsDebugBuild ? "d" : "");
Elliott Hughes059d5c12012-03-12 17:39:18 -0700288
289 // Work out the filename for the compiler library.
Brian Carlstrom00bc1dc2013-02-01 15:56:27 -0800290 std::string library_name(StringPrintf("art%s-compiler", suffix));
Elliott Hughes059d5c12012-03-12 17:39:18 -0700291 std::string filename(StringPrintf(OS_SHARED_LIB_FORMAT_STR, library_name.c_str()));
292
293#if defined(__APPLE__)
294 // On Linux, dex2oat will have been built with an RPATH of $ORIGIN/../lib, so dlopen(3) will find
295 // the .so by itself. On Mac OS, there isn't really an equivalent, so we have to manually do the
296 // same work.
Elliott Hughes059d5c12012-03-12 17:39:18 -0700297 uint32_t executable_path_length = 0;
Elliott Hughes448e93c2012-03-28 22:30:06 -0700298 _NSGetExecutablePath(NULL, &executable_path_length);
299 std::string path(executable_path_length, static_cast<char>(0));
300 CHECK_EQ(_NSGetExecutablePath(&path[0], &executable_path_length), 0);
Elliott Hughes059d5c12012-03-12 17:39:18 -0700301
302 // Strip the "/dex2oat".
303 size_t last_slash = path.find_last_of('/');
304 CHECK_NE(last_slash, std::string::npos) << path;
305 path.resize(last_slash);
306
307 // Strip the "/bin".
308 last_slash = path.find_last_of('/');
309 path.resize(last_slash);
310
311 filename = path + "/lib/" + filename;
312#endif
313 return filename;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800314}
315
Elliott Hughes46f060a2012-03-09 17:36:50 -0800316template<typename Fn>
317static Fn FindFunction(const std::string& compiler_so_name, void* library, const char* name) {
318 Fn fn = reinterpret_cast<Fn>(dlsym(library, name));
319 if (fn == NULL) {
320 LOG(FATAL) << "Couldn't find \"" << name << "\" in compiler library " << compiler_so_name << ": " << dlerror();
321 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700322 VLOG(compiler) << "Found \"" << name << "\" at " << reinterpret_cast<void*>(fn);
Elliott Hughes46f060a2012-03-09 17:36:50 -0800323 return fn;
324}
325
Ian Rogers1212a022013-03-04 10:48:41 -0800326CompilerDriver::CompilerDriver(CompilerBackend compiler_backend, InstructionSet instruction_set,
Brian Carlstrom96391602013-06-13 19:49:50 -0700327 bool image, DescriptorSet* image_classes,
328 size_t thread_count, bool support_debugging,
Anwar Ghuloumc4f105d2013-04-10 16:12:11 -0700329 bool dump_stats, bool dump_timings)
buzbeec531cef2012-10-18 07:09:20 -0700330 : compiler_backend_(compiler_backend),
331 instruction_set_(instruction_set),
Ian Rogersfffdb022013-01-04 15:14:08 -0800332 freezing_constructor_lock_("freezing constructor lock"),
Elliott Hughesc225caa2012-02-03 15:43:37 -0800333 compiled_classes_lock_("compiled classes lock"),
334 compiled_methods_lock_("compiled method lock"),
Brian Carlstromaded5f72011-10-07 17:15:04 -0700335 image_(image),
Brian Carlstrom96391602013-06-13 19:49:50 -0700336 image_classes_(image_classes),
Elliott Hughes5523ee02012-02-03 18:18:34 -0800337 thread_count_(thread_count),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800338 support_debugging_(support_debugging),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339 start_ns_(0),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800340 stats_(new AOTCompilationStats),
Brian Carlstromba0668e2012-03-26 13:14:07 -0700341 dump_stats_(dump_stats),
342 dump_timings_(dump_timings),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800343 compiler_library_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800344 compiler_(NULL),
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700345 compiler_context_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800346 jni_compiler_(NULL),
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700347 compiler_enable_auto_elf_loading_(NULL),
Brian Carlstrom96391602013-06-13 19:49:50 -0700348 compiler_get_method_code_addr_(NULL),
349 support_boot_image_fixup_(true)
Logan Chien971bf3f2012-05-01 15:47:55 +0800350{
buzbee8c4bbb52012-11-26 14:00:58 -0800351 std::string compiler_so_name(MakeCompilerSoName(compiler_backend_));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800352 compiler_library_ = dlopen(compiler_so_name.c_str(), RTLD_LAZY);
353 if (compiler_library_ == NULL) {
354 LOG(FATAL) << "Couldn't find compiler library " << compiler_so_name << ": " << dlerror();
355 }
356 VLOG(compiler) << "dlopen(\"" << compiler_so_name << "\", RTLD_LAZY) returned " << compiler_library_;
357
buzbee4df2bbd2012-10-11 14:46:06 -0700358 CHECK_PTHREAD_CALL(pthread_key_create, (&tls_key_, NULL), "compiler tls key");
359
buzbeec531cef2012-10-18 07:09:20 -0700360 // TODO: more work needed to combine initializations and allow per-method backend selection
Ian Rogers1212a022013-03-04 10:48:41 -0800361 typedef void (*InitCompilerContextFn)(CompilerDriver&);
buzbeec531cef2012-10-18 07:09:20 -0700362 InitCompilerContextFn init_compiler_context;
Ian Rogersc928de92013-02-27 14:30:44 -0800363 if (compiler_backend_ == kPortable){
buzbeec531cef2012-10-18 07:09:20 -0700364 // Initialize compiler_context_
Ian Rogers1212a022013-03-04 10:48:41 -0800365 init_compiler_context = FindFunction<void (*)(CompilerDriver&)>(compiler_so_name,
buzbeec531cef2012-10-18 07:09:20 -0700366 compiler_library_, "ArtInitCompilerContext");
367 compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "ArtCompileMethod");
368 } else {
Ian Rogers1212a022013-03-04 10:48:41 -0800369 init_compiler_context = FindFunction<void (*)(CompilerDriver&)>(compiler_so_name,
buzbeec531cef2012-10-18 07:09:20 -0700370 compiler_library_, "ArtInitQuickCompilerContext");
371 compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "ArtQuickCompileMethod");
372 }
Logan Chien106b2a02012-03-18 04:41:38 +0800373
374 init_compiler_context(*this);
buzbee692be802012-08-29 15:52:59 -0700375
Ian Rogersc928de92013-02-27 14:30:44 -0800376 if (compiler_backend_ == kPortable) {
Brian Carlstrom00bc1dc2013-02-01 15:56:27 -0800377 jni_compiler_ = FindFunction<JniCompilerFn>(compiler_so_name, compiler_library_, "ArtLLVMJniCompileMethod");
378 } else {
379 jni_compiler_ = FindFunction<JniCompilerFn>(compiler_so_name, compiler_library_, "ArtQuickJniCompileMethod");
380 }
381
Brian Carlstrom25c33252011-09-18 15:58:35 -0700382 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800383 if (!image_) {
Brian Carlstrom96391602013-06-13 19:49:50 -0700384 CHECK(image_classes_.get() == NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800385 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700386}
387
Ian Rogers1212a022013-03-04 10:48:41 -0800388CompilerDriver::~CompilerDriver() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700389 Thread* self = Thread::Current();
Elliott Hughesc225caa2012-02-03 15:43:37 -0800390 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700391 MutexLock mu(self, compiled_classes_lock_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800392 STLDeleteValues(&compiled_classes_);
393 }
394 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700395 MutexLock mu(self, compiled_methods_lock_);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800396 STLDeleteValues(&compiled_methods_);
397 }
398 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700399 MutexLock mu(self, compiled_methods_lock_);
Brian Carlstromf5822582012-03-19 22:34:31 -0700400 STLDeleteElements(&code_to_patch_);
401 }
402 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700403 MutexLock mu(self, compiled_methods_lock_);
Brian Carlstromf5822582012-03-19 22:34:31 -0700404 STLDeleteElements(&methods_to_patch_);
405 }
Mathieu Chartiered6d5ed2012-10-12 14:51:46 -0700406 CHECK_PTHREAD_CALL(pthread_key_delete, (tls_key_), "delete tls key");
Ian Rogers1212a022013-03-04 10:48:41 -0800407 typedef void (*UninitCompilerContextFn)(CompilerDriver&);
buzbee8c4bbb52012-11-26 14:00:58 -0800408 std::string compiler_so_name(MakeCompilerSoName(compiler_backend_));
buzbeec531cef2012-10-18 07:09:20 -0700409 UninitCompilerContextFn uninit_compiler_context;
buzbee692be802012-08-29 15:52:59 -0700410 // Uninitialize compiler_context_
buzbeec531cef2012-10-18 07:09:20 -0700411 // TODO: rework to combine initialization/uninitialization
Ian Rogersc928de92013-02-27 14:30:44 -0800412 if (compiler_backend_ == kPortable) {
Ian Rogers1212a022013-03-04 10:48:41 -0800413 uninit_compiler_context = FindFunction<void (*)(CompilerDriver&)>(compiler_so_name,
buzbeec531cef2012-10-18 07:09:20 -0700414 compiler_library_, "ArtUnInitCompilerContext");
415 } else {
Ian Rogers1212a022013-03-04 10:48:41 -0800416 uninit_compiler_context = FindFunction<void (*)(CompilerDriver&)>(compiler_so_name,
buzbeec531cef2012-10-18 07:09:20 -0700417 compiler_library_, "ArtUnInitQuickCompilerContext");
418 }
buzbee692be802012-08-29 15:52:59 -0700419 uninit_compiler_context(*this);
Brian Carlstrom22c05692013-03-27 15:20:06 -0700420#if 0
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800421 if (compiler_library_ != NULL) {
422 VLOG(compiler) << "dlclose(" << compiler_library_ << ")";
buzbeeca7a5e42012-08-20 11:12:18 -0700423 /*
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?
Brian Carlstrom22c05692013-03-27 15:20:06 -0700433 *
434 * This has now been completely disabled because mclinker was
435 * closing stdout on exit, which was affecting both quick and
436 * portable.
buzbeeca7a5e42012-08-20 11:12:18 -0700437 */
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800438 dlclose(compiler_library_);
439 }
Brian Carlstrom22c05692013-03-27 15:20:06 -0700440#endif
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700441}
442
Ian Rogers1212a022013-03-04 10:48:41 -0800443CompilerTls* CompilerDriver::GetTls() {
buzbee4df2bbd2012-10-11 14:46:06 -0700444 // Lazily create thread-local storage
445 CompilerTls* res = static_cast<CompilerTls*>(pthread_getspecific(tls_key_));
446 if (res == NULL) {
447 res = new CompilerTls();
448 CHECK_PTHREAD_CALL(pthread_setspecific, (tls_key_, res), "compiler tls");
449 }
450 return res;
451}
452
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700453const std::vector<uint8_t>* CompilerDriver::CreatePortableResolutionTrampoline() const {
454 switch (instruction_set_) {
455 case kArm:
456 case kThumb2:
457 return arm::CreatePortableResolutionTrampoline();
458 case kMips:
459 return mips::CreatePortableResolutionTrampoline();
460 case kX86:
461 return x86::CreatePortableResolutionTrampoline();
462 default:
463 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_;
464 return NULL;
465 }
466}
467
468const std::vector<uint8_t>* CompilerDriver::CreateQuickResolutionTrampoline() const {
469 switch (instruction_set_) {
470 case kArm:
471 case kThumb2:
472 return arm::CreateQuickResolutionTrampoline();
473 case kMips:
474 return mips::CreateQuickResolutionTrampoline();
475 case kX86:
476 return x86::CreateQuickResolutionTrampoline();
477 default:
478 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_;
479 return NULL;
480 }
481}
482
483const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToInterpreterEntry() const {
484 switch (instruction_set_) {
485 case kArm:
486 case kThumb2:
487 return arm::CreateInterpreterToInterpreterEntry();
488 case kMips:
489 return mips::CreateInterpreterToInterpreterEntry();
490 case kX86:
491 return x86::CreateInterpreterToInterpreterEntry();
492 default:
493 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_;
494 return NULL;
495 }
496}
497
498const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToQuickEntry() const {
499 switch (instruction_set_) {
500 case kArm:
501 case kThumb2:
502 return arm::CreateInterpreterToQuickEntry();
503 case kMips:
504 return mips::CreateInterpreterToQuickEntry();
505 case kX86:
506 return x86::CreateInterpreterToQuickEntry();
507 default:
508 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_;
509 return NULL;
510 }
511}
512
Ian Rogers1212a022013-03-04 10:48:41 -0800513void CompilerDriver::CompileAll(jobject class_loader,
514 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700515 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800516
Ian Rogers56edc432013-01-18 16:51:51 -0800517 UniquePtr<ThreadPool> thread_pool(new ThreadPool(thread_count_));
Sameer Abu Asala8439542013-02-14 16:06:42 -0800518 TimingLogger timings("compiler", false);
Elliott Hughes601a1232012-02-02 17:47:38 -0800519
Ian Rogers56edc432013-01-18 16:51:51 -0800520 PreCompile(class_loader, dex_files, *thread_pool.get(), timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800521
Ian Rogers56edc432013-01-18 16:51:51 -0800522 Compile(class_loader, dex_files, *thread_pool.get(), timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800523
Brian Carlstromba0668e2012-03-26 13:14:07 -0700524 if (dump_timings_ && timings.GetTotalNs() > MsToNs(1000)) {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800525 LOG(INFO) << Dumpable<TimingLogger>(timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800526 }
Ian Rogers996cc582012-02-14 22:23:29 -0800527
Brian Carlstromba0668e2012-03-26 13:14:07 -0700528 if (dump_stats_) {
529 stats_->Dump();
530 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700531}
532
Ian Rogers1212a022013-03-04 10:48:41 -0800533void CompilerDriver::CompileOne(const mirror::AbstractMethod* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700534 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700535 Thread* self = Thread::Current();
536 jobject class_loader;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537 const DexFile* dex_file;
Ian Rogersfffdb022013-01-04 15:14:08 -0800538 uint32_t class_def_idx;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700539 {
540 ScopedObjectAccessUnchecked soa(self);
541 ScopedLocalRef<jobject>
542 local_class_loader(soa.Env(),
543 soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader()));
544 class_loader = soa.Env()->NewGlobalRef(local_class_loader.get());
545 // Find the dex_file
Ian Rogersfffdb022013-01-04 15:14:08 -0800546 MethodHelper mh(method);
547 dex_file = &mh.GetDexFile();
548 class_def_idx = mh.GetClassDefIndex();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700549 }
550 self->TransitionFromRunnableToSuspended(kNative);
Brian Carlstromae826982011-11-09 01:33:42 -0800551
Brian Carlstromae826982011-11-09 01:33:42 -0800552 std::vector<const DexFile*> dex_files;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700553 dex_files.push_back(dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800554
Ian Rogers56edc432013-01-18 16:51:51 -0800555 UniquePtr<ThreadPool> thread_pool(new ThreadPool(1U));
Sameer Abu Asala8439542013-02-14 16:06:42 -0800556 TimingLogger timings("CompileOne", false);
Ian Rogers56edc432013-01-18 16:51:51 -0800557 PreCompile(class_loader, dex_files, *thread_pool.get(), timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800558
Ian Rogers0571d352011-11-03 19:51:38 -0700559 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700560 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
Ian Rogers08f753d2012-08-24 14:35:25 -0700561 CompileMethod(code_item, method->GetAccessFlags(), method->GetInvokeType(),
Ian Rogersfffdb022013-01-04 15:14:08 -0800562 class_def_idx, method_idx, class_loader, *dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800563
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700564 self->GetJniEnv()->DeleteGlobalRef(class_loader);
565
566 self->TransitionFromSuspendedToRunnable();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700567}
568
Ian Rogers1212a022013-03-04 10:48:41 -0800569void CompilerDriver::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
570 ThreadPool& thread_pool, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800571 for (size_t i = 0; i != dex_files.size(); ++i) {
572 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700573 CHECK(dex_file != NULL);
Brian Carlstrom2f663822012-11-07 22:49:06 -0800574 ResolveDexFile(class_loader, *dex_file, thread_pool, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700575 }
576}
577
Ian Rogers1212a022013-03-04 10:48:41 -0800578void CompilerDriver::PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
579 ThreadPool& thread_pool, TimingLogger& timings) {
Brian Carlstrom96391602013-06-13 19:49:50 -0700580 LoadImageClasses(timings);
581
Brian Carlstrom2f663822012-11-07 22:49:06 -0800582 Resolve(class_loader, dex_files, thread_pool, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800583
Brian Carlstrom2f663822012-11-07 22:49:06 -0800584 Verify(class_loader, dex_files, thread_pool, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800585
Brian Carlstrom2f663822012-11-07 22:49:06 -0800586 InitializeClasses(class_loader, dex_files, thread_pool, timings);
Brian Carlstrom96391602013-06-13 19:49:50 -0700587
588 UpdateImageClasses(timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800589}
590
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700591bool CompilerDriver::IsImageClass(const char* descriptor) const {
Brian Carlstrom96391602013-06-13 19:49:50 -0700592 DCHECK(descriptor != NULL);
593 if (image_classes_.get() == NULL) {
594 return true;
Brian Carlstromae826982011-11-09 01:33:42 -0800595 }
596 return image_classes_->find(descriptor) != image_classes_->end();
597}
598
Brian Carlstrom96391602013-06-13 19:49:50 -0700599static void ResolveExceptionsForMethod(MethodHelper* mh,
600 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve)
601 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
602 const DexFile::CodeItem* code_item = mh->GetCodeItem();
603 if (code_item == NULL) {
604 return; // native or abstract method
605 }
606 if (code_item->tries_size_ == 0) {
607 return; // nothing to process
608 }
609 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
610 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
611 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
612 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
613 bool has_catch_all = false;
614 if (encoded_catch_handler_size <= 0) {
615 encoded_catch_handler_size = -encoded_catch_handler_size;
616 has_catch_all = true;
617 }
618 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
619 uint16_t encoded_catch_handler_handlers_type_idx =
620 DecodeUnsignedLeb128(&encoded_catch_handler_list);
621 // Add to set of types to resolve if not already in the dex cache resolved types
622 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
623 exceptions_to_resolve.insert(
624 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
625 &mh->GetDexFile()));
626 }
627 // ignore address associated with catch handler
628 DecodeUnsignedLeb128(&encoded_catch_handler_list);
629 }
630 if (has_catch_all) {
631 // ignore catch all address
632 DecodeUnsignedLeb128(&encoded_catch_handler_list);
633 }
634 }
635}
636
637static bool ResolveCatchBlockExceptionsClassVisitor(mirror::Class* c, void* arg)
638 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
639 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
640 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
641 MethodHelper mh;
642 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
643 mirror::AbstractMethod* m = c->GetVirtualMethod(i);
644 mh.ChangeMethod(m);
645 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
646 }
647 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
648 mirror::AbstractMethod* m = c->GetDirectMethod(i);
649 mh.ChangeMethod(m);
650 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
651 }
652 return true;
653}
654
655static bool RecordImageClassesVisitor(mirror::Class* klass, void* arg)
656 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
657 CompilerDriver::DescriptorSet* image_classes =
658 reinterpret_cast<CompilerDriver::DescriptorSet*>(arg);
659 image_classes->insert(ClassHelper(klass).GetDescriptor());
660 return true;
661}
662
663// Make a list of descriptors for classes to include in the image
664void CompilerDriver::LoadImageClasses(TimingLogger& timings)
665 LOCKS_EXCLUDED(Locks::mutator_lock_) {
666 if (image_classes_.get() == NULL) {
667 return;
668 }
669
670 // Make a first class to load all classes explicitly listed in the file
671 Thread* self = Thread::Current();
672 ScopedObjectAccess soa(self);
673 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
674 typedef DescriptorSet::iterator It; // TODO: C++0x auto
675 for (It it = image_classes_->begin(), end = image_classes_->end(); it != end;) {
676 std::string descriptor(*it);
677 SirtRef<mirror::Class> klass(self, class_linker->FindSystemClass(descriptor.c_str()));
678 if (klass.get() == NULL) {
679 image_classes_->erase(it++);
680 LOG(WARNING) << "Failed to find class " << descriptor;
681 Thread::Current()->ClearException();
682 } else {
683 ++it;
684 }
685 }
686
687 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
688 // exceptions are resolved by the verifier when there is a catch block in an interested method.
689 // Do this here so that exception classes appear to have been specified image classes.
690 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
691 SirtRef<mirror::Class> java_lang_Throwable(self,
692 class_linker->FindSystemClass("Ljava/lang/Throwable;"));
693 do {
694 unresolved_exception_types.clear();
695 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
696 &unresolved_exception_types);
697 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
698 for (It it = unresolved_exception_types.begin(),
699 end = unresolved_exception_types.end();
700 it != end; ++it) {
701 uint16_t exception_type_idx = it->first;
702 const DexFile* dex_file = it->second;
703 mirror::DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
704 mirror:: ClassLoader* class_loader = NULL;
705 SirtRef<mirror::Class> klass(self, class_linker->ResolveType(*dex_file, exception_type_idx,
706 dex_cache, class_loader));
707 if (klass.get() == NULL) {
708 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
709 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
710 LOG(FATAL) << "Failed to resolve class " << descriptor;
711 }
712 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get()));
713 }
714 // Resolving exceptions may load classes that reference more exceptions, iterate until no
715 // more are found
716 } while (!unresolved_exception_types.empty());
717
718 // We walk the roots looking for classes so that we'll pick up the
719 // above classes plus any classes them depend on such super
720 // classes, interfaces, and the required ClassLinker roots.
721 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes_.get());
722
723 CHECK_NE(image_classes_->size(), 0U);
724 timings.AddSplit("LoadImageClasses");
725}
726
727static void MaybeAddToImageClasses(mirror::Class* klass, CompilerDriver::DescriptorSet* image_classes)
728 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
729 while (!klass->IsObjectClass()) {
730 ClassHelper kh(klass);
731 const char* descriptor = kh.GetDescriptor();
732 std::pair<CompilerDriver::DescriptorSet::iterator, bool> result =
733 image_classes->insert(descriptor);
734 if (result.second) {
735 LOG(INFO) << "Adding " << descriptor << " to image classes";
736 } else {
737 return;
738 }
739 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
740 MaybeAddToImageClasses(kh.GetDirectInterface(i), image_classes);
741 }
742 if (klass->IsArrayClass()) {
743 MaybeAddToImageClasses(klass->GetComponentType(), image_classes);
744 }
745 klass = klass->GetSuperClass();
746 }
747}
748
749void CompilerDriver::FindClinitImageClassesCallback(mirror::Object* object, void* arg) {
750 DCHECK(object != NULL);
751 DCHECK(arg != NULL);
752 CompilerDriver* compiler_driver = reinterpret_cast<CompilerDriver*>(arg);
753 MaybeAddToImageClasses(object->GetClass(), compiler_driver->image_classes_.get());
754}
755
756void CompilerDriver::UpdateImageClasses(TimingLogger& timings) {
757 if (image_classes_.get() == NULL) {
758 return;
759 }
760
761 // Update image_classes_ with classes for objects created by <clinit> methods.
762 Thread* self = Thread::Current();
763 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
764 Heap* heap = Runtime::Current()->GetHeap();
765 // TODO: Image spaces only?
766 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
767 heap->FlushAllocStack();
768 heap->GetLiveBitmap()->Walk(FindClinitImageClassesCallback, this);
769 self->EndAssertNoThreadSuspension(old_cause);
770 timings.AddSplit("UpdateImageClasses");
771}
772
Ian Rogers1212a022013-03-04 10:48:41 -0800773void CompilerDriver::RecordClassStatus(ClassReference ref, CompiledClass* compiled_class) {
774 MutexLock mu(Thread::Current(), CompilerDriver::compiled_classes_lock_);
Ian Rogers3d1548d2012-09-24 14:08:03 -0700775 compiled_classes_.Put(ref, compiled_class);
776}
777
Ian Rogers1212a022013-03-04 10:48:41 -0800778bool CompilerDriver::CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file,
779 uint32_t type_idx) {
Ian Rogers6fe568e2013-06-07 15:16:10 -0700780 if (IsImage() && IsImageClass(dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx)))) {
781 if (kIsDebugBuild) {
782 ScopedObjectAccess soa(Thread::Current());
783 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
784 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
785 CHECK(resolved_class != NULL);
786 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800787 stats_->TypeInDexCache();
Ian Rogers6fe568e2013-06-07 15:16:10 -0700788 return true;
Ian Rogers996cc582012-02-14 22:23:29 -0800789 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800790 stats_->TypeNotInDexCache();
Ian Rogers6fe568e2013-06-07 15:16:10 -0700791 return false;
Ian Rogers996cc582012-02-14 22:23:29 -0800792 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800793}
794
Ian Rogers1212a022013-03-04 10:48:41 -0800795bool CompilerDriver::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file,
796 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800797 // See also Compiler::ResolveDexFile
798
Ian Rogers5f7fa552012-11-02 11:45:53 -0700799 bool result = false;
800 if (IsImage()) {
801 // We resolve all const-string strings when building for the image.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700802 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800803 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Ian Rogers5f7fa552012-11-02 11:45:53 -0700804 Runtime::Current()->GetClassLinker()->ResolveString(dex_file, string_idx, dex_cache);
805 result = true;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 }
Ian Rogers996cc582012-02-14 22:23:29 -0800807 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800808 stats_->StringInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800809 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800810 stats_->StringNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800811 }
812 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800813}
814
Ian Rogers1212a022013-03-04 10:48:41 -0800815bool CompilerDriver::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
Ian Rogersc9e463c2013-06-05 16:52:26 -0700816 uint32_t type_idx,
817 bool* type_known_final, bool* type_known_abstract,
818 bool* equals_referrers_class) {
819 if (type_known_final != NULL) {
820 *type_known_final = false;
821 }
822 if (type_known_abstract != NULL) {
823 *type_known_abstract = false;
824 }
825 if (equals_referrers_class != NULL) {
826 *equals_referrers_class = false;
827 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800829 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Ian Rogers1bddec32012-02-04 12:27:34 -0800830 // Get type from dex cache assuming it was populated by the verifier
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800831 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800832 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800833 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800834 return false; // Unknown class needs access checks.
835 }
836 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
Ian Rogersc9e463c2013-06-05 16:52:26 -0700837 if (equals_referrers_class != NULL) {
838 *equals_referrers_class = (method_id.class_idx_ == type_idx);
839 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800840 mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
Ian Rogers1bddec32012-02-04 12:27:34 -0800841 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800842 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800843 return false; // Incomplete referrer knowledge needs access check.
844 }
845 // Perform access check, will return true if access is ok or false if we're going to have to
846 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800847 bool result = referrer_class->CanAccess(resolved_class);
848 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800849 stats_->TypeDoesntNeedAccessCheck();
Ian Rogersc9e463c2013-06-05 16:52:26 -0700850 if (type_known_final != NULL) {
851 *type_known_final = resolved_class->IsFinal() && !resolved_class->IsArrayClass();
852 }
853 if (type_known_abstract != NULL) {
854 *type_known_abstract = resolved_class->IsAbstract();
855 }
Ian Rogers996cc582012-02-14 22:23:29 -0800856 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800857 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800858 }
859 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800860}
861
Ian Rogers1212a022013-03-04 10:48:41 -0800862bool CompilerDriver::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
863 const DexFile& dex_file,
864 uint32_t type_idx) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800866 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
Ian Rogers1bddec32012-02-04 12:27:34 -0800867 // Get type from dex cache assuming it was populated by the verifier.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800868 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800869 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800870 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800871 return false; // Unknown class needs access checks.
872 }
873 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800874 mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
Ian Rogers1bddec32012-02-04 12:27:34 -0800875 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800876 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800877 return false; // Incomplete referrer knowledge needs access check.
878 }
879 // Perform access and instantiable checks, will return true if access is ok or false if we're
880 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800881 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
882 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800883 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800884 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800885 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800886 }
887 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800888}
889
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800890static mirror::Class* ComputeCompilingMethodsClass(ScopedObjectAccess& soa,
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700891 mirror::DexCache* dex_cache,
Ian Rogers89756f22013-03-04 16:40:02 -0800892 const DexCompilationUnit* mUnit)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700893 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700894 // The passed dex_cache is a hint, sanity check before asking the class linker that will take a
895 // lock.
896 if (dex_cache->GetDexFile() != mUnit->GetDexFile()) {
897 dex_cache = mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile());
898 }
Ian Rogers89756f22013-03-04 16:40:02 -0800899 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader());
900 const DexFile::MethodId& referrer_method_id = mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex());
901 return mUnit->GetClassLinker()->ResolveType(*mUnit->GetDexFile(), referrer_method_id.class_idx_,
902 dex_cache, class_loader);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800903}
904
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800905static mirror::Field* ComputeFieldReferencedFromCompilingMethod(ScopedObjectAccess& soa,
Ian Rogers89756f22013-03-04 16:40:02 -0800906 const DexCompilationUnit* mUnit,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800907 uint32_t field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700908 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers89756f22013-03-04 16:40:02 -0800909 mirror::DexCache* dex_cache = mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile());
910 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader());
911 return mUnit->GetClassLinker()->ResolveField(*mUnit->GetDexFile(), field_idx, dex_cache,
912 class_loader, false);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800913}
914
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800915static mirror::AbstractMethod* ComputeMethodReferencedFromCompilingMethod(ScopedObjectAccess& soa,
Ian Rogers89756f22013-03-04 16:40:02 -0800916 const DexCompilationUnit* mUnit,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800917 uint32_t method_idx,
918 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700919 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers89756f22013-03-04 16:40:02 -0800920 mirror::DexCache* dex_cache = mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile());
921 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader());
922 return mUnit->GetClassLinker()->ResolveMethod(*mUnit->GetDexFile(), method_idx, dex_cache,
923 class_loader, NULL, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800924}
925
Ian Rogers89756f22013-03-04 16:40:02 -0800926bool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
Ian Rogers1212a022013-03-04 10:48:41 -0800927 int& field_offset, bool& is_volatile, bool is_put) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 ScopedObjectAccess soa(Thread::Current());
Ian Rogers08f753d2012-08-24 14:35:25 -0700929 // Conservative defaults.
Ian Rogers1bddec32012-02-04 12:27:34 -0800930 field_offset = -1;
931 is_volatile = true;
Ian Rogers08f753d2012-08-24 14:35:25 -0700932 // Try to resolve field and ignore if an Incompatible Class Change Error (ie is static).
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800933 mirror::Field* resolved_field = ComputeFieldReferencedFromCompilingMethod(soa, mUnit, field_idx);
Ian Rogers08f753d2012-08-24 14:35:25 -0700934 if (resolved_field != NULL && !resolved_field->IsStatic()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700935 mirror::Class* referrer_class =
936 ComputeCompilingMethodsClass(soa, resolved_field->GetDeclaringClass()->GetDexCache(),
937 mUnit);
Ian Rogerse2645d32012-04-11 14:42:42 -0700938 if (referrer_class != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800939 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700940 bool access_ok = referrer_class->CanAccess(fields_class) &&
941 referrer_class->CanAccessMember(fields_class,
942 resolved_field->GetAccessFlags());
943 if (!access_ok) {
944 // The referring class can't access the resolved field, this may occur as a result of a
945 // protected field being made public by a sub-class. Resort to the dex file to determine
946 // the correct class for the access check.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700947 const DexFile& dex_file = *referrer_class->GetDexCache()->GetDexFile();
Ian Rogers89756f22013-03-04 16:40:02 -0800948 mirror::Class* dex_fields_class = mUnit->GetClassLinker()->ResolveType(dex_file,
Ian Rogerse2645d32012-04-11 14:42:42 -0700949 dex_file.GetFieldId(field_idx).class_idx_,
950 referrer_class);
951 access_ok = referrer_class->CanAccess(dex_fields_class) &&
952 referrer_class->CanAccessMember(dex_fields_class,
953 resolved_field->GetAccessFlags());
954 }
955 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
956 fields_class != referrer_class;
957 if (access_ok && !is_write_to_final_from_wrong_class) {
958 field_offset = resolved_field->GetOffset().Int32Value();
959 is_volatile = resolved_field->IsVolatile();
960 stats_->ResolvedInstanceField();
961 return true; // Fast path.
962 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800963 }
964 }
965 // Clean up any exception left by field/type resolution
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 if (soa.Self()->IsExceptionPending()) {
967 soa.Self()->ClearException();
Ian Rogers1bddec32012-02-04 12:27:34 -0800968 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800969 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800970 return false; // Incomplete knowledge needs slow path.
971}
972
Ian Rogers89756f22013-03-04 16:40:02 -0800973bool CompilerDriver::ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
Ian Rogers1212a022013-03-04 10:48:41 -0800974 int& field_offset, int& ssb_index,
975 bool& is_referrers_class, bool& is_volatile,
976 bool is_put) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 ScopedObjectAccess soa(Thread::Current());
Ian Rogers08f753d2012-08-24 14:35:25 -0700978 // Conservative defaults.
Ian Rogers1bddec32012-02-04 12:27:34 -0800979 field_offset = -1;
980 ssb_index = -1;
981 is_referrers_class = false;
982 is_volatile = true;
Ian Rogers08f753d2012-08-24 14:35:25 -0700983 // Try to resolve field and ignore if an Incompatible Class Change Error (ie isn't static).
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800984 mirror::Field* resolved_field = ComputeFieldReferencedFromCompilingMethod(soa, mUnit, field_idx);
Ian Rogers08f753d2012-08-24 14:35:25 -0700985 if (resolved_field != NULL && resolved_field->IsStatic()) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700986 mirror::Class* referrer_class =
987 ComputeCompilingMethodsClass(soa, resolved_field->GetDeclaringClass()->GetDexCache(),
988 mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800989 if (referrer_class != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800990 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
jeffhao8cd6dda2012-02-22 10:15:34 -0800991 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800992 is_referrers_class = true; // implies no worrying about class initialization
993 field_offset = resolved_field->GetOffset().Int32Value();
994 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800995 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800996 return true; // fast path
997 } else {
Ian Rogerse2645d32012-04-11 14:42:42 -0700998 bool access_ok = referrer_class->CanAccess(fields_class) &&
999 referrer_class->CanAccessMember(fields_class,
1000 resolved_field->GetAccessFlags());
1001 if (!access_ok) {
1002 // The referring class can't access the resolved field, this may occur as a result of a
1003 // protected field being made public by a sub-class. Resort to the dex file to determine
1004 // the correct class for the access check. Don't change the field's class as that is
1005 // used to identify the SSB.
Ian Rogers4445a7e2012-10-05 17:19:13 -07001006 const DexFile& dex_file = *referrer_class->GetDexCache()->GetDexFile();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001007 mirror::Class* dex_fields_class =
Ian Rogers89756f22013-03-04 16:40:02 -08001008 mUnit->GetClassLinker()->ResolveType(dex_file,
1009 dex_file.GetFieldId(field_idx).class_idx_,
1010 referrer_class);
Ian Rogerse2645d32012-04-11 14:42:42 -07001011 access_ok = referrer_class->CanAccess(dex_fields_class) &&
1012 referrer_class->CanAccessMember(dex_fields_class,
1013 resolved_field->GetAccessFlags());
1014 }
jeffhao8cd6dda2012-02-22 10:15:34 -08001015 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogerse2645d32012-04-11 14:42:42 -07001016 if (access_ok && !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -08001017 // We have the resolved field, we must make it into a ssbIndex for the referrer
1018 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -08001019 // TODO: for images we can elide the static storage base null check
1020 // if we know there's a non-null entry in the image
Ian Rogers89756f22013-03-04 16:40:02 -08001021 mirror::DexCache* dex_cache = mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 if (fields_class->GetDexCache() == dex_cache) {
Ian Rogers4103ad22012-02-06 09:18:25 -08001023 // common case where the dex cache of both the referrer and the field are the same,
1024 // no need to search the dex file
1025 ssb_index = fields_class->GetDexTypeIndex();
1026 field_offset = resolved_field->GetOffset().Int32Value();
1027 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001028 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -08001029 return true;
1030 }
Ian Rogerse2645d32012-04-11 14:42:42 -07001031 // Search dex file for localized ssb index, may fail if field's class is a parent
1032 // of the class mentioned in the dex file and there is no dex cache entry.
Ian Rogers1bddec32012-02-04 12:27:34 -08001033 const DexFile::StringId* string_id =
Ian Rogers637c65b2013-05-31 11:46:00 -07001034 mUnit->GetDexFile()->FindStringId(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
Ian Rogers1bddec32012-02-04 12:27:34 -08001035 if (string_id != NULL) {
1036 const DexFile::TypeId* type_id =
Ian Rogers89756f22013-03-04 16:40:02 -08001037 mUnit->GetDexFile()->FindTypeId(mUnit->GetDexFile()->GetIndexForStringId(*string_id));
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001038 if (type_id != NULL) {
Ian Rogers1bddec32012-02-04 12:27:34 -08001039 // medium path, needs check of static storage base being initialized
Ian Rogers89756f22013-03-04 16:40:02 -08001040 ssb_index = mUnit->GetDexFile()->GetIndexForTypeId(*type_id);
Ian Rogers1bddec32012-02-04 12:27:34 -08001041 field_offset = resolved_field->GetOffset().Int32Value();
1042 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001043 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -08001044 return true;
1045 }
1046 }
1047 }
1048 }
1049 }
1050 }
1051 // Clean up any exception left by field/type resolution
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001052 if (soa.Self()->IsExceptionPending()) {
1053 soa.Self()->ClearException();
Ian Rogers1bddec32012-02-04 12:27:34 -08001054 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001055 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -08001056 return false; // Incomplete knowledge needs slow path.
1057}
1058
Ian Rogers1212a022013-03-04 10:48:41 -08001059void CompilerDriver::GetCodeAndMethodForDirectCall(InvokeType type, InvokeType sharp_type,
Ian Rogers4f6ad8a2013-03-18 15:27:28 -07001060 mirror::Class* referrer_class,
Ian Rogers1212a022013-03-04 10:48:41 -08001061 mirror::AbstractMethod* method,
1062 uintptr_t& direct_code,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001063 uintptr_t& direct_method,
1064 bool update_stats) {
Ian Rogers137e88f2012-10-08 17:46:47 -07001065 // For direct and static methods compute possible direct_code and direct_method values, ie
1066 // an address for the Method* being invoked and an address of the code for that Method*.
1067 // For interface calls compute a value for direct_method that is the interface method being
1068 // invoked, so this can be passed to the out-of-line runtime support code.
Ian Rogers2ed3b952012-03-17 11:49:39 -07001069 direct_code = 0;
1070 direct_method = 0;
Ian Rogersc928de92013-02-27 14:30:44 -08001071 if (compiler_backend_ == kPortable) {
buzbeec531cef2012-10-18 07:09:20 -07001072 if (sharp_type != kStatic && sharp_type != kDirect) {
1073 return;
1074 }
1075 } else {
1076 if (sharp_type != kStatic && sharp_type != kDirect && sharp_type != kInterface) {
1077 return;
1078 }
Ian Rogers2ed3b952012-03-17 11:49:39 -07001079 }
Ian Rogers2ed3b952012-03-17 11:49:39 -07001080 bool method_code_in_boot = method->GetDeclaringClass()->GetClassLoader() == NULL;
1081 if (!method_code_in_boot) {
1082 return;
1083 }
1084 bool has_clinit_trampoline = method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -07001085 if (has_clinit_trampoline && (method->GetDeclaringClass() != referrer_class)) {
1086 // Ensure we run the clinit trampoline unless we are invoking a static method in the same class.
Ian Rogers2ed3b952012-03-17 11:49:39 -07001087 return;
1088 }
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001089 if (update_stats) {
1090 if (sharp_type != kInterface) { // Interfaces always go via a trampoline.
1091 stats_->DirectCallsToBoot(type);
1092 }
1093 stats_->DirectMethodsToBoot(type);
Ian Rogersc468e922012-10-10 18:11:33 -07001094 }
Ian Rogers3fa13792012-03-18 15:53:45 -07001095 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
1096 if (compiling_boot) {
Brian Carlstrom96391602013-06-13 19:49:50 -07001097 if (support_boot_image_fixup_) {
Ian Rogers3fa13792012-03-18 15:53:45 -07001098 MethodHelper mh(method);
1099 if (IsImageClass(mh.GetDeclaringClassDescriptor())) {
Brian Carlstrom0637e272012-03-20 01:07:52 -07001100 // We can only branch directly to Methods that are resolved in the DexCache.
1101 // Otherwise we won't invoke the resolution trampoline.
Ian Rogers3fa13792012-03-18 15:53:45 -07001102 direct_method = -1;
Brian Carlstrom0637e272012-03-20 01:07:52 -07001103 direct_code = -1;
Ian Rogers3fa13792012-03-18 15:53:45 -07001104 }
Ian Rogers3fa13792012-03-18 15:53:45 -07001105 }
1106 } else {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001107 if (Runtime::Current()->GetHeap()->FindSpaceFromObject(method)->IsImageSpace()) {
Ian Rogers3fa13792012-03-18 15:53:45 -07001108 direct_method = reinterpret_cast<uintptr_t>(method);
1109 }
Jeff Haoaa4a7932013-05-13 11:28:27 -07001110 direct_code = reinterpret_cast<uintptr_t>(method->GetEntryPointFromCompiledCode());
Ian Rogers2ed3b952012-03-17 11:49:39 -07001111 }
Ian Rogers2ed3b952012-03-17 11:49:39 -07001112}
1113
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001114bool CompilerDriver::ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
1115 InvokeType& invoke_type,
1116 MethodReference& target_method,
1117 int& vtable_idx,
1118 uintptr_t& direct_code, uintptr_t& direct_method,
1119 bool update_stats) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001120 ScopedObjectAccess soa(Thread::Current());
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001121 vtable_idx = -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -07001122 direct_code = 0;
1123 direct_method = 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001124 mirror::AbstractMethod* resolved_method =
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001125 ComputeMethodReferencedFromCompilingMethod(soa, mUnit, target_method.dex_method_index,
1126 invoke_type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001127 if (resolved_method != NULL) {
Ian Rogers08f753d2012-08-24 14:35:25 -07001128 // Don't try to fast-path if we don't understand the caller's class or this appears to be an
1129 // Incompatible Class Change Error.
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07001130 mirror::Class* referrer_class =
1131 ComputeCompilingMethodsClass(soa, resolved_method->GetDeclaringClass()->GetDexCache(),
1132 mUnit);
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001133 bool icce = resolved_method->CheckIncompatibleClassChange(invoke_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001134 if (referrer_class != NULL && !icce) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001135 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001136 if (!referrer_class->CanAccess(methods_class) ||
1137 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -08001138 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001139 // The referring class can't access the resolved method, this may occur as a result of a
1140 // protected method being made public by implementing an interface that re-declares the
Ian Rogers08f753d2012-08-24 14:35:25 -07001141 // method public. Resort to the dex file to determine the correct class for the access
1142 // check.
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001143 uint16_t class_idx =
1144 target_method.dex_file->GetMethodId(target_method.dex_method_index).class_idx_;
1145 methods_class = mUnit->GetClassLinker()->ResolveType(*target_method.dex_file,
1146 class_idx, referrer_class);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001147 }
1148 if (referrer_class->CanAccess(methods_class) &&
Ian Rogers137e88f2012-10-08 17:46:47 -07001149 referrer_class->CanAccessMember(methods_class, resolved_method->GetAccessFlags())) {
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001150 const bool kEnableFinalBasedSharpening = true;
1151 // Sharpen a virtual call into a direct call when the target is known not to have been
1152 // overridden (ie is final).
1153 bool can_sharpen_virtual_based_on_type =
1154 (invoke_type == kVirtual) && (resolved_method->IsFinal() || methods_class->IsFinal());
1155 // For invoke-super, ensure the vtable index will be correct to dispatch in the vtable of
1156 // the super class.
1157 bool can_sharpen_super_based_on_type = (invoke_type == kSuper) &&
1158 (referrer_class != methods_class) && referrer_class->IsSubClass(methods_class) &&
1159 resolved_method->GetMethodIndex() < methods_class->GetVTable()->GetLength() &&
1160 (methods_class->GetVTable()->Get(resolved_method->GetMethodIndex()) == resolved_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001161
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001162 if (kEnableFinalBasedSharpening && (can_sharpen_virtual_based_on_type ||
1163 can_sharpen_super_based_on_type)) {
Ian Rogersfb6adba2012-03-04 21:51:51 -08001164 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
1165 // dex cache, check that this resolved method is where we expect it.
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001166 CHECK(referrer_class->GetDexCache()->GetResolvedMethod(target_method.dex_method_index) ==
1167 resolved_method) << PrettyMethod(resolved_method);
1168 if (update_stats) {
1169 stats_->ResolvedMethod(invoke_type);
1170 stats_->VirtualMadeDirect(invoke_type);
1171 }
1172 GetCodeAndMethodForDirectCall(invoke_type, kDirect, referrer_class, resolved_method,
1173 direct_code, direct_method, update_stats);
1174 invoke_type = kDirect;
Ian Rogers2ed3b952012-03-17 11:49:39 -07001175 return true;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001176 }
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001177 const bool kEnableVerifierBasedSharpening = true;
1178 if (kEnableVerifierBasedSharpening && (invoke_type == kVirtual ||
1179 invoke_type == kInterface)) {
1180 // Did the verifier record a more precise invoke target based on its type information?
1181 const CompilerDriver::MethodReference caller_method(mUnit->GetDexFile(),
1182 mUnit->GetDexMethodIndex());
1183 const CompilerDriver::MethodReference* devirt_map_target =
1184 verifier::MethodVerifier::GetDevirtMap(caller_method, dex_pc);
1185 if (devirt_map_target != NULL) {
1186 mirror::DexCache* target_dex_cache =
1187 mUnit->GetClassLinker()->FindDexCache(*devirt_map_target->dex_file);
1188 mirror::ClassLoader* class_loader =
1189 soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader());
1190 mirror::AbstractMethod* called_method =
1191 mUnit->GetClassLinker()->ResolveMethod(*devirt_map_target->dex_file,
1192 devirt_map_target->dex_method_index,
1193 target_dex_cache, class_loader, NULL,
1194 kVirtual);
1195 CHECK(called_method != NULL);
1196 CHECK(!called_method->IsAbstract());
1197 GetCodeAndMethodForDirectCall(invoke_type, kDirect, referrer_class, called_method,
1198 direct_code, direct_method, update_stats);
1199 bool compiler_needs_dex_cache =
Ian Rogersf8582c32013-05-29 16:33:03 -07001200 (GetCompilerBackend() == kPortable) ||
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001201 (GetCompilerBackend() == kQuick && instruction_set_ != kThumb2) ||
1202 (direct_code == 0) || (direct_code == static_cast<unsigned int>(-1)) ||
1203 (direct_method == 0) || (direct_method == static_cast<unsigned int>(-1));
1204 if ((devirt_map_target->dex_file != target_method.dex_file) &&
1205 compiler_needs_dex_cache) {
1206 // We need to use the dex cache to find either the method or code, and the dex file
1207 // containing the method isn't the one expected for the target method. Try to find
1208 // the method within the expected target dex file.
1209 // TODO: the -1 could be handled as direct code if the patching new the target dex
1210 // file.
1211 // TODO: quick only supports direct pointers with Thumb2.
1212 // TODO: the following should be factored into a common helper routine to find
1213 // one dex file's method within another.
1214 const DexFile* dexfile = target_method.dex_file;
1215 const DexFile* cm_dexfile =
1216 called_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1217 const DexFile::MethodId& cm_method_id =
1218 cm_dexfile->GetMethodId(called_method->GetDexMethodIndex());
1219 const char* cm_descriptor = cm_dexfile->StringByTypeIdx(cm_method_id.class_idx_);
1220 const DexFile::StringId* descriptor = dexfile->FindStringId(cm_descriptor);
1221 if (descriptor != NULL) {
1222 const DexFile::TypeId* type_id =
1223 dexfile->FindTypeId(dexfile->GetIndexForStringId(*descriptor));
1224 if (type_id != NULL) {
1225 const char* cm_name = cm_dexfile->GetMethodName(cm_method_id);
1226 const DexFile::StringId* name = dexfile->FindStringId(cm_name);
1227 if (name != NULL) {
1228 uint16_t return_type_idx;
1229 std::vector<uint16_t> param_type_idxs;
1230 bool success = dexfile->CreateTypeList(&return_type_idx, &param_type_idxs,
1231 cm_dexfile->GetMethodSignature(cm_method_id));
1232 if (success) {
1233 const DexFile::ProtoId* sig =
1234 dexfile->FindProtoId(return_type_idx, param_type_idxs);
1235 if (sig != NULL) {
1236 const DexFile::MethodId* method_id = dexfile->FindMethodId(*type_id,
1237 *name, *sig);
1238 if (method_id != NULL) {
1239 if (update_stats) {
1240 stats_->ResolvedMethod(invoke_type);
1241 stats_->VirtualMadeDirect(invoke_type);
1242 stats_->PreciseTypeDevirtualization();
1243 }
1244 target_method.dex_method_index = dexfile->GetIndexForMethodId(*method_id);
1245 invoke_type = kDirect;
1246 return true;
1247 }
1248 }
1249 }
1250 }
1251 }
1252 }
Ian Rogersd0583802013-06-01 10:51:46 -07001253 // TODO: the stats for direct code and method are off as we failed to find the direct
1254 // method in the referring method's dex cache/file.
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001255 } else {
1256 if (update_stats) {
1257 stats_->ResolvedMethod(invoke_type);
1258 stats_->VirtualMadeDirect(invoke_type);
1259 stats_->PreciseTypeDevirtualization();
1260 }
1261 target_method = *devirt_map_target;
1262 invoke_type = kDirect;
1263 return true;
1264 }
1265 }
1266 }
1267 if (invoke_type == kSuper) {
Ian Rogers08f753d2012-08-24 14:35:25 -07001268 // Unsharpened super calls are suspicious so go slow-path.
Ian Rogers2ed3b952012-03-17 11:49:39 -07001269 } else {
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001270 // Sharpening failed so generate a regular resolved method dispatch.
1271 if (update_stats) {
1272 stats_->ResolvedMethod(invoke_type);
1273 }
1274 if (invoke_type == kVirtual || invoke_type == kSuper) {
1275 vtable_idx = resolved_method->GetMethodIndex();
1276 }
1277 GetCodeAndMethodForDirectCall(invoke_type, invoke_type, referrer_class, resolved_method,
1278 direct_code, direct_method, update_stats);
Ian Rogers2ed3b952012-03-17 11:49:39 -07001279 return true;
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001280 }
1281 }
1282 }
1283 }
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001284 // Clean up any exception left by method/invoke_type resolution
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001285 if (soa.Self()->IsExceptionPending()) {
1286 soa.Self()->ClearException();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001287 }
Ian Rogerse3cd2f02013-05-24 15:32:56 -07001288 if (update_stats) {
1289 stats_->UnresolvedMethod(invoke_type);
1290 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001291 return false; // Incomplete knowledge needs slow path.
1292}
1293
Ian Rogersfae370a2013-06-05 08:33:27 -07001294bool CompilerDriver::IsSafeCast(const MethodReference& mr, uint32_t dex_pc) {
1295 bool result = verifier::MethodVerifier::IsSafeCast(mr, dex_pc);
1296 if (result) {
1297 stats_->SafeCast();
1298 } else {
1299 stats_->NotASafeCast();
1300 }
1301 return result;
1302}
1303
1304
Ian Rogers1212a022013-03-04 10:48:41 -08001305void CompilerDriver::AddCodePatch(const DexFile* dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -07001306 uint32_t referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -07001307 InvokeType referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -07001308 uint32_t target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -07001309 InvokeType target_invoke_type,
Ian Rogers3fa13792012-03-18 15:53:45 -07001310 size_t literal_offset) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001311 MutexLock mu(Thread::Current(), compiled_methods_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 code_to_patch_.push_back(new PatchInformation(dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -07001313 referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -07001314 referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -07001315 target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -07001316 target_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -07001317 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -07001318}
Ian Rogers1212a022013-03-04 10:48:41 -08001319void CompilerDriver::AddMethodPatch(const DexFile* dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -07001320 uint32_t referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -07001321 InvokeType referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -07001322 uint32_t target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -07001323 InvokeType target_invoke_type,
Ian Rogers3fa13792012-03-18 15:53:45 -07001324 size_t literal_offset) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001325 MutexLock mu(Thread::Current(), compiled_methods_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 methods_to_patch_.push_back(new PatchInformation(dex_file,
Brian Carlstromf5822582012-03-19 22:34:31 -07001327 referrer_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -07001328 referrer_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -07001329 target_method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -07001330 target_invoke_type,
Brian Carlstromf5822582012-03-19 22:34:31 -07001331 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -07001332}
1333
Ian Rogers219b5a82013-03-04 13:48:24 -08001334class ParallelCompilationManager {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001335 public:
Ian Rogers219b5a82013-03-04 13:48:24 -08001336 typedef void Callback(const ParallelCompilationManager* manager, size_t index);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001337
Ian Rogers219b5a82013-03-04 13:48:24 -08001338 ParallelCompilationManager(ClassLinker* class_linker,
1339 jobject class_loader,
1340 CompilerDriver* compiler,
1341 const DexFile* dex_file,
1342 ThreadPool& thread_pool)
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001343 : class_linker_(class_linker),
1344 class_loader_(class_loader),
1345 compiler_(compiler),
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001346 dex_file_(dex_file),
Brian Carlstrom2f663822012-11-07 22:49:06 -08001347 thread_pool_(&thread_pool) {}
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001348
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001349 ClassLinker* GetClassLinker() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001350 CHECK(class_linker_ != NULL);
1351 return class_linker_;
1352 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001353
1354 jobject GetClassLoader() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001355 return class_loader_;
1356 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001357
Ian Rogers1212a022013-03-04 10:48:41 -08001358 CompilerDriver* GetCompiler() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001359 CHECK(compiler_ != NULL);
1360 return compiler_;
1361 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001362
1363 const DexFile* GetDexFile() const {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001364 CHECK(dex_file_ != NULL);
1365 return dex_file_;
1366 }
1367
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001368 void ForAll(size_t begin, size_t end, Callback callback, size_t work_units) {
1369 Thread* self = Thread::Current();
1370 self->AssertNoPendingException();
1371 CHECK_GT(work_units, 0U);
1372
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001373 std::vector<ForAllClosure*> closures(work_units);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001374 for (size_t i = 0; i < work_units; ++i) {
1375 closures[i] = new ForAllClosure(this, begin + i, end, callback, work_units);
1376 thread_pool_->AddTask(self, closures[i]);
1377 }
1378 thread_pool_->StartWorkers(self);
1379
1380 // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
1381 // thread destructor's called below perform join).
1382 CHECK_NE(self->GetState(), kRunnable);
1383
1384 // Wait for all the worker threads to finish.
1385 thread_pool_->Wait(self);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001386 }
1387
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001388 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001389
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001390 class ForAllClosure : public Task {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001391 public:
Ian Rogers219b5a82013-03-04 13:48:24 -08001392 ForAllClosure(ParallelCompilationManager* manager, size_t begin, size_t end, Callback* callback,
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001393 size_t stripe)
Ian Rogers219b5a82013-03-04 13:48:24 -08001394 : manager_(manager),
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001395 begin_(begin),
1396 end_(end),
1397 callback_(callback),
1398 stripe_(stripe)
1399 {
1400
1401 }
1402
1403 virtual void Run(Thread* self) {
1404 for (size_t i = begin_; i < end_; i += stripe_) {
Ian Rogers219b5a82013-03-04 13:48:24 -08001405 callback_(manager_, i);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001406 self->AssertNoPendingException();
1407 }
1408 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001409
1410 virtual void Finalize() {
1411 delete this;
1412 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001413 private:
Ian Rogers219b5a82013-03-04 13:48:24 -08001414 const ParallelCompilationManager* const manager_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001415 const size_t begin_;
1416 const size_t end_;
Ian Rogers219b5a82013-03-04 13:48:24 -08001417 const Callback* const callback_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001418 const size_t stripe_;
1419 };
1420
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 ClassLinker* const class_linker_;
1422 const jobject class_loader_;
Ian Rogers1212a022013-03-04 10:48:41 -08001423 CompilerDriver* const compiler_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001424 const DexFile* const dex_file_;
Ian Rogers219b5a82013-03-04 13:48:24 -08001425 ThreadPool* const thread_pool_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001426};
1427
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428// Return true if the class should be skipped during compilation. We
1429// never skip classes in the boot class loader. However, if we have a
1430// non-boot class loader and we can resolve the class in the boot
1431// class loader, we do skip the class. This happens if an app bundles
1432// classes found in the boot classpath. Since at runtime we will
1433// select the class from the boot classpath, do not attempt to resolve
1434// or compile it now.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001435static bool SkipClass(mirror::ClassLoader* class_loader,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001436 const DexFile& dex_file,
1437 const DexFile::ClassDef& class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 if (class_loader == NULL) {
1440 return false;
1441 }
1442 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1443 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001444 mirror::Class* klass = class_linker->FindClass(descriptor, NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001445 if (klass == NULL) {
1446 Thread* self = Thread::Current();
1447 CHECK(self->IsExceptionPending());
1448 self->ClearException();
1449 return false;
1450 }
1451 return true;
1452}
1453
Ian Rogers219b5a82013-03-04 13:48:24 -08001454static void ResolveClassFieldsAndMethods(const ParallelCompilationManager* manager, size_t class_def_index)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001455 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001456 ScopedObjectAccess soa(Thread::Current());
Ian Rogers219b5a82013-03-04 13:48:24 -08001457 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader());
1458 const DexFile& dex_file = *manager->GetDexFile();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001459
1460 // Method and Field are the worst. We can't resolve without either
1461 // context from the code use (to disambiguate virtual vs direct
1462 // method and instance vs static field) or from class
1463 // definitions. While the compiler will resolve what it can as it
1464 // needs it, here we try to resolve fields and methods used in class
1465 // definitions, since many of them many never be referenced by
1466 // generated code.
1467 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001468 if (SkipClass(class_loader, dex_file, class_def)) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001469 return;
1470 }
1471
1472 // Note the class_data pointer advances through the headers,
1473 // static fields, instance fields, direct methods, and virtual
1474 // methods.
1475 const byte* class_data = dex_file.GetClassData(class_def);
1476 if (class_data == NULL) {
1477 // empty class such as a marker interface
1478 return;
1479 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001480 Thread* self = Thread::Current();
Ian Rogers219b5a82013-03-04 13:48:24 -08001481 ClassLinker* class_linker = manager->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001482 mirror::DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001483 ClassDataItemIterator it(dex_file, class_data);
1484 while (it.HasNextStaticField()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001485 mirror::Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
1486 class_loader, true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001487 if (field == NULL) {
1488 CHECK(self->IsExceptionPending());
1489 self->ClearException();
1490 }
1491 it.Next();
1492 }
Ian Rogersfffdb022013-01-04 15:14:08 -08001493 // If an instance field is final then we need to have a barrier on the return, static final
1494 // fields are assigned within the lock held for class initialization.
1495 bool requires_constructor_barrier = false;
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001496 while (it.HasNextInstanceField()) {
Ian Rogersfffdb022013-01-04 15:14:08 -08001497 if ((it.GetMemberAccessFlags() & kAccFinal) != 0) {
1498 requires_constructor_barrier = true;
1499 }
1500
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001501 mirror::Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
1502 class_loader, false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001503 if (field == NULL) {
1504 CHECK(self->IsExceptionPending());
1505 self->ClearException();
1506 }
1507 it.Next();
1508 }
Ian Rogersfffdb022013-01-04 15:14:08 -08001509 if (requires_constructor_barrier) {
Ian Rogers219b5a82013-03-04 13:48:24 -08001510 manager->GetCompiler()->AddRequiresConstructorBarrier(soa.Self(), manager->GetDexFile(),
Ian Rogersfffdb022013-01-04 15:14:08 -08001511 class_def_index);
1512 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001513 while (it.HasNextDirectMethod()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001514 mirror::AbstractMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1515 dex_cache, class_loader, NULL,
1516 it.GetMethodInvokeType(class_def));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001517 if (method == NULL) {
1518 CHECK(self->IsExceptionPending());
1519 self->ClearException();
1520 }
1521 it.Next();
1522 }
1523 while (it.HasNextVirtualMethod()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001524 mirror::AbstractMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1525 dex_cache, class_loader, NULL,
1526 it.GetMethodInvokeType(class_def));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001527 if (method == NULL) {
1528 CHECK(self->IsExceptionPending());
1529 self->ClearException();
1530 }
1531 it.Next();
1532 }
1533 DCHECK(!it.HasNext());
1534}
1535
Ian Rogers219b5a82013-03-04 13:48:24 -08001536static void ResolveType(const ParallelCompilationManager* manager, size_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001537 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001538 // Class derived values are more complicated, they require the linker and loader.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001539 ScopedObjectAccess soa(Thread::Current());
Ian Rogers219b5a82013-03-04 13:48:24 -08001540 ClassLinker* class_linker = manager->GetClassLinker();
1541 const DexFile& dex_file = *manager->GetDexFile();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001542 mirror::DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Ian Rogers219b5a82013-03-04 13:48:24 -08001543 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001544 mirror::Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001545
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001546 if (klass == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001547 CHECK(soa.Self()->IsExceptionPending());
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001548 Thread::Current()->ClearException();
1549 }
1550}
1551
Ian Rogers1212a022013-03-04 10:48:41 -08001552void CompilerDriver::ResolveDexFile(jobject class_loader, const DexFile& dex_file,
1553 ThreadPool& thread_pool, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001554 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1555
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001556 // TODO: we could resolve strings here, although the string table is largely filled with class
1557 // and method names.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001558
Ian Rogers219b5a82013-03-04 13:48:24 -08001559 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, thread_pool);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001560 context.ForAll(0, dex_file.NumTypeIds(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001561 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -07001562
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001563 context.ForAll(0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001564 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001565}
1566
Ian Rogers1212a022013-03-04 10:48:41 -08001567void CompilerDriver::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1568 ThreadPool& thread_pool, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -08001569 for (size_t i = 0; i != dex_files.size(); ++i) {
1570 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -07001571 CHECK(dex_file != NULL);
Brian Carlstrom2f663822012-11-07 22:49:06 -08001572 VerifyDexFile(class_loader, *dex_file, thread_pool, timings);
jeffhao98eacac2011-09-14 16:11:53 -07001573 }
1574}
1575
Ian Rogers219b5a82013-03-04 13:48:24 -08001576static void VerifyClass(const ParallelCompilationManager* manager, size_t class_def_index)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001577 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001578 ScopedObjectAccess soa(Thread::Current());
Ian Rogers219b5a82013-03-04 13:48:24 -08001579 const DexFile::ClassDef& class_def = manager->GetDexFile()->GetClassDef(class_def_index);
1580 const char* descriptor = manager->GetDexFile()->GetClassDescriptor(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001581 mirror::Class* klass =
Ian Rogers219b5a82013-03-04 13:48:24 -08001582 manager->GetClassLinker()->FindClass(descriptor,
1583 soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader()));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001584 if (klass == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001585 CHECK(soa.Self()->IsExceptionPending());
1586 soa.Self()->ClearException();
jeffhaof56197c2012-03-05 18:01:54 -08001587
1588 /*
1589 * At compile time, we can still structurally verify the class even if FindClass fails.
1590 * This is to ensure the class is structurally sound for compilation. An unsound class
1591 * will be rejected by the verifier and later skipped during compilation in the compiler.
1592 */
Ian Rogers219b5a82013-03-04 13:48:24 -08001593 mirror::DexCache* dex_cache = manager->GetClassLinker()->FindDexCache(*manager->GetDexFile());
jeffhaof56197c2012-03-05 18:01:54 -08001594 std::string error_msg;
Ian Rogers219b5a82013-03-04 13:48:24 -08001595 if (verifier::MethodVerifier::VerifyClass(manager->GetDexFile(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001596 dex_cache,
Ian Rogers219b5a82013-03-04 13:48:24 -08001597 soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader()),
Jeff Haoee988952013-04-16 14:23:47 -07001598 class_def_index, error_msg, true) ==
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001599 verifier::MethodVerifier::kHardFailure) {
Ian Rogers219b5a82013-03-04 13:48:24 -08001600 const DexFile::ClassDef& class_def = manager->GetDexFile()->GetClassDef(class_def_index);
jeffhaof56197c2012-03-05 18:01:54 -08001601 LOG(ERROR) << "Verification failed on class "
Ian Rogers219b5a82013-03-04 13:48:24 -08001602 << PrettyDescriptor(manager->GetDexFile()->GetClassDescriptor(class_def))
jeffhaof56197c2012-03-05 18:01:54 -08001603 << " because: " << error_msg;
1604 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001605 return;
1606 }
1607 CHECK(klass->IsResolved()) << PrettyClass(klass);
Ian Rogers219b5a82013-03-04 13:48:24 -08001608 manager->GetClassLinker()->VerifyClass(klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001609
1610 if (klass->IsErroneous()) {
1611 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
Ian Rogers62d6c772013-02-27 08:32:07 -08001612 CHECK(soa.Self()->IsExceptionPending());
1613 soa.Self()->ClearException();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001614 }
1615
Ian Rogers9ffb0392012-09-10 11:56:50 -07001616 CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous())
1617 << PrettyDescriptor(klass) << ": state=" << klass->GetStatus();
Ian Rogers62d6c772013-02-27 08:32:07 -08001618 soa.Self()->AssertNoPendingException();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001619}
1620
Ian Rogers1212a022013-03-04 10:48:41 -08001621void CompilerDriver::VerifyDexFile(jobject class_loader, const DexFile& dex_file,
1622 ThreadPool& thread_pool, TimingLogger& timings) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001623 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers219b5a82013-03-04 13:48:24 -08001624 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, thread_pool);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001625 context.ForAll(0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001626 timings.AddSplit("Verify " + dex_file.GetLocation());
1627}
Elliott Hughesd9cdfe92011-10-06 16:09:04 -07001628
Ian Rogers64b6d142012-10-29 16:34:15 -07001629static const char* class_initializer_black_list[] = {
1630 "Landroid/app/ActivityThread;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1631 "Landroid/bluetooth/BluetoothAudioGateway;", // Calls android.bluetooth.BluetoothAudioGateway.classInitNative().
1632 "Landroid/bluetooth/HeadsetBase;", // Calls android.bluetooth.HeadsetBase.classInitNative().
1633 "Landroid/content/res/CompatibilityInfo;", // Requires android.util.DisplayMetrics -..-> android.os.SystemProperties.native_get_int.
1634 "Landroid/content/res/CompatibilityInfo$1;", // Requires android.util.DisplayMetrics -..-> android.os.SystemProperties.native_get_int.
1635 "Landroid/content/UriMatcher;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1636 "Landroid/database/CursorWindow;", // Requires android.util.DisplayMetrics -..-> android.os.SystemProperties.native_get_int.
1637 "Landroid/database/sqlite/SQLiteConnection;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1638 "Landroid/database/sqlite/SQLiteConnection$Operation;", // Requires SimpleDateFormat -> java.util.Locale.
1639 "Landroid/database/sqlite/SQLiteDatabaseConfiguration;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1640 "Landroid/database/sqlite/SQLiteDebug;", // Calls android.util.Log.isLoggable.
1641 "Landroid/database/sqlite/SQLiteOpenHelper;", // Calls Class.getSimpleName -> Class.isAnonymousClass -> Class.getDex.
1642 "Landroid/database/sqlite/SQLiteQueryBuilder;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1643 "Landroid/drm/DrmManagerClient;", // Calls System.loadLibrary.
1644 "Landroid/graphics/drawable/AnimatedRotateDrawable;", // Sub-class of Drawable.
1645 "Landroid/graphics/drawable/AnimationDrawable;", // Sub-class of Drawable.
1646 "Landroid/graphics/drawable/BitmapDrawable;", // Sub-class of Drawable.
1647 "Landroid/graphics/drawable/ClipDrawable;", // Sub-class of Drawable.
1648 "Landroid/graphics/drawable/ColorDrawable;", // Sub-class of Drawable.
1649 "Landroid/graphics/drawable/Drawable;", // Requires android.graphics.Rect.
1650 "Landroid/graphics/drawable/DrawableContainer;", // Sub-class of Drawable.
1651 "Landroid/graphics/drawable/GradientDrawable;", // Sub-class of Drawable.
1652 "Landroid/graphics/drawable/LayerDrawable;", // Sub-class of Drawable.
1653 "Landroid/graphics/drawable/NinePatchDrawable;", // Sub-class of Drawable.
1654 "Landroid/graphics/drawable/RotateDrawable;", // Sub-class of Drawable.
1655 "Landroid/graphics/drawable/ScaleDrawable;", // Sub-class of Drawable.
1656 "Landroid/graphics/drawable/ShapeDrawable;", // Sub-class of Drawable.
1657 "Landroid/graphics/drawable/StateListDrawable;", // Sub-class of Drawable.
1658 "Landroid/graphics/drawable/TransitionDrawable;", // Sub-class of Drawable.
1659 "Landroid/graphics/Matrix;", // Calls android.graphics.Matrix.native_create.
1660 "Landroid/graphics/Matrix$1;", // Requires Matrix.
1661 "Landroid/graphics/PixelFormat;", // Calls android.graphics.PixelFormat.nativeClassInit().
1662 "Landroid/graphics/Rect;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1663 "Landroid/graphics/SurfaceTexture;", // Calls android.graphics.SurfaceTexture.nativeClassInit().
1664 "Landroid/graphics/Typeface;", // Calls android.graphics.Typeface.nativeCreate.
1665 "Landroid/inputmethodservice/ExtractEditText;", // Requires android.widget.TextView.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001666 "Landroid/media/AmrInputStream;", // Calls OsConstants.initConstants.
1667 "Landroid/media/CamcorderProfile;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001668 "Landroid/media/CameraProfile;", // Calls System.loadLibrary.
1669 "Landroid/media/DecoderCapabilities;", // Calls System.loadLibrary.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001670 "Landroid/media/EncoderCapabilities;", // Calls OsConstants.initConstants.
1671 "Landroid/media/ExifInterface;", // Calls OsConstants.initConstants.
1672 "Landroid/media/MediaCodec;", // Calls OsConstants.initConstants.
1673 "Landroid/media/MediaCodecList;", // Calls OsConstants.initConstants.
1674 "Landroid/media/MediaCrypto;", // Calls OsConstants.initConstants.
1675 "Landroid/media/MediaDrm;", // Calls OsConstants.initConstants.
1676 "Landroid/media/MediaExtractor;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001677 "Landroid/media/MediaFile;", // Requires DecoderCapabilities.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001678 "Landroid/media/MediaMetadataRetriever;", // Calls OsConstants.initConstants.
1679 "Landroid/media/MediaMuxer;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001680 "Landroid/media/MediaPlayer;", // Calls System.loadLibrary.
1681 "Landroid/media/MediaRecorder;", // Calls System.loadLibrary.
1682 "Landroid/media/MediaScanner;", // Calls System.loadLibrary.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001683 "Landroid/media/ResampleInputStream;", // Calls OsConstants.initConstants.
1684 "Landroid/media/SoundPool;", // Calls OsConstants.initConstants.
1685 "Landroid/media/videoeditor/MediaArtistNativeHelper;", // Calls OsConstants.initConstants.
1686 "Landroid/media/videoeditor/VideoEditorProfile;", // Calls OsConstants.initConstants.
1687 "Landroid/mtp/MtpDatabase;", // Calls OsConstants.initConstants.
1688 "Landroid/mtp/MtpDevice;", // Calls OsConstants.initConstants.
1689 "Landroid/mtp/MtpServer;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001690 "Landroid/net/NetworkInfo;", // Calls java.util.EnumMap.<init> -> java.lang.Enum.getSharedConstants -> System.identityHashCode.
1691 "Landroid/net/Proxy;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1692 "Landroid/net/SSLCertificateSocketFactory;", // Requires javax.net.ssl.HttpsURLConnection.
1693 "Landroid/net/Uri;", // Calls Class.getSimpleName -> Class.isAnonymousClass -> Class.getDex.
1694 "Landroid/net/Uri$AbstractHierarchicalUri;", // Requires Uri.
1695 "Landroid/net/Uri$HierarchicalUri;", // Requires Uri.
1696 "Landroid/net/Uri$OpaqueUri;", // Requires Uri.
1697 "Landroid/net/Uri$StringUri;", // Requires Uri.
1698 "Landroid/net/WebAddress;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1699 "Landroid/nfc/NdefRecord;", // Calls String.getBytes -> java.nio.charset.Charset.
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07001700 "Landroid/opengl/EGL14;", // Calls android.opengl.EGL14._nativeClassInit.
Ian Rogers64b6d142012-10-29 16:34:15 -07001701 "Landroid/opengl/GLES10;", // Calls android.opengl.GLES10._nativeClassInit.
1702 "Landroid/opengl/GLES10Ext;", // Calls android.opengl.GLES10Ext._nativeClassInit.
1703 "Landroid/opengl/GLES11;", // Requires GLES10.
1704 "Landroid/opengl/GLES11Ext;", // Calls android.opengl.GLES11Ext._nativeClassInit.
1705 "Landroid/opengl/GLES20;", // Calls android.opengl.GLES20._nativeClassInit.
1706 "Landroid/opengl/GLUtils;", // Calls android.opengl.GLUtils.nativeClassInit.
1707 "Landroid/os/Build;", // Calls -..-> android.os.SystemProperties.native_get.
1708 "Landroid/os/Build$VERSION;", // Requires Build.
1709 "Landroid/os/Debug;", // Requires android.os.Environment.
1710 "Landroid/os/Environment;", // Calls System.getenv.
1711 "Landroid/os/FileUtils;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1712 "Landroid/os/StrictMode;", // Calls android.util.Log.isLoggable.
1713 "Landroid/os/StrictMode$VmPolicy;", // Requires StrictMode.
1714 "Landroid/os/Trace;", // Calls android.os.Trace.nativeGetEnabledTags.
1715 "Landroid/os/UEventObserver;", // Calls Class.getSimpleName -> Class.isAnonymousClass -> Class.getDex.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001716 "Landroid/provider/ContactsContract;", // Calls OsConstants.initConstants.
1717 "Landroid/provider/Settings$Global;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001718 "Landroid/provider/Settings$Secure;", // Requires android.net.Uri.
1719 "Landroid/provider/Settings$System;", // Requires android.net.Uri.
1720 "Landroid/renderscript/RenderScript;", // Calls System.loadLibrary.
1721 "Landroid/server/BluetoothService;", // Calls android.server.BluetoothService.classInitNative.
1722 "Landroid/server/BluetoothEventLoop;", // Calls android.server.BluetoothEventLoop.classInitNative.
1723 "Landroid/telephony/PhoneNumberUtils;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07001724 "Landroid/telephony/TelephonyManager;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001725 "Landroid/text/AutoText;", // Requires android.util.DisplayMetrics -..-> android.os.SystemProperties.native_get_int.
1726 "Landroid/text/Layout;", // Calls com.android.internal.util.ArrayUtils.emptyArray -> System.identityHashCode.
1727 "Landroid/text/BoringLayout;", // Requires Layout.
1728 "Landroid/text/DynamicLayout;", // Requires Layout.
1729 "Landroid/text/Html$HtmlParser;", // Calls -..-> String.toLowerCase -> java.util.Locale.
1730 "Landroid/text/StaticLayout;", // Requires Layout.
1731 "Landroid/text/TextUtils;", // Requires android.util.DisplayMetrics.
1732 "Landroid/util/DisplayMetrics;", // Calls SystemProperties.native_get_int.
1733 "Landroid/util/Patterns;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
Ian Rogers64b6d142012-10-29 16:34:15 -07001734 "Landroid/view/Choreographer;", // Calls SystemProperties.native_get_boolean.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001735 "Landroid/util/Patterns;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1736 "Landroid/view/GLES20Canvas;", // Calls GLES20Canvas.nIsAvailable().
Ian Rogers64b6d142012-10-29 16:34:15 -07001737 "Landroid/view/GLES20RecordingCanvas;", // Requires android.view.GLES20Canvas.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001738 "Landroid/view/GestureDetector;", // Calls android.view.GLES20Canvas.nIsAvailable.
Ian Rogers64b6d142012-10-29 16:34:15 -07001739 "Landroid/view/HardwareRenderer$Gl20Renderer;", // Requires SystemProperties.native_get.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001740 "Landroid/view/HardwareRenderer$GlRenderer;", // Requires SystemProperties.native_get.
Ian Rogers64b6d142012-10-29 16:34:15 -07001741 "Landroid/view/InputEventConsistencyVerifier;", // Requires android.os.Build.
1742 "Landroid/view/Surface;", // Requires SystemProperties.native_get.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001743 "Landroid/view/SurfaceControl;", // Calls OsConstants.initConstants.
1744 "Landroid/view/animation/AlphaAnimation;", // Requires Animation.
1745 "Landroid/view/animation/Animation;", // Calls SystemProperties.native_get_boolean.
1746 "Landroid/view/animation/AnimationSet;", // Calls OsConstants.initConstants.
1747 "Landroid/view/textservice/SpellCheckerSubtype;", // Calls Class.getDex().
Ian Rogers64b6d142012-10-29 16:34:15 -07001748 "Landroid/webkit/JniUtil;", // Calls System.loadLibrary.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001749 "Landroid/webkit/PluginManager;", // // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001750 "Landroid/webkit/WebViewCore;", // Calls System.loadLibrary.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001751 "Landroid/webkit/WebViewInputDispatcher;", // Calls Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1752 "Landroid/webkit/URLUtil;", // Calls Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
Ian Rogers64b6d142012-10-29 16:34:15 -07001753 "Landroid/widget/AutoCompleteTextView;", // Requires TextView.
1754 "Landroid/widget/Button;", // Requires TextView.
1755 "Landroid/widget/CheckBox;", // Requires TextView.
1756 "Landroid/widget/CheckedTextView;", // Requires TextView.
1757 "Landroid/widget/CompoundButton;", // Requires TextView.
1758 "Landroid/widget/EditText;", // Requires TextView.
1759 "Landroid/widget/NumberPicker;", // Requires java.util.Locale.
1760 "Landroid/widget/ScrollBarDrawable;", // Sub-class of Drawable.
1761 "Landroid/widget/SearchView$SearchAutoComplete;", // Requires TextView.
1762 "Landroid/widget/Switch;", // Requires TextView.
1763 "Landroid/widget/TextView;", // Calls Paint.<init> -> Paint.native_init.
1764 "Lcom/android/i18n/phonenumbers/AsYouTypeFormatter;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001765 "Lcom/android/i18n/phonenumbers/PhoneNumberMatcher;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
Ian Rogers64b6d142012-10-29 16:34:15 -07001766 "Lcom/android/i18n/phonenumbers/PhoneNumberUtil;", // Requires java.util.logging.LogManager.
1767 "Lcom/android/internal/os/SamplingProfilerIntegration;", // Calls SystemProperties.native_get_int.
1768 "Lcom/android/internal/policy/impl/PhoneWindow;", // Calls android.os.Binder.init.
1769 "Lcom/android/internal/view/menu/ActionMenuItemView;", // Requires TextView.
1770 "Lcom/android/internal/widget/DialogTitle;", // Requires TextView.
1771 "Lcom/android/org/bouncycastle/asn1/StreamUtil;", // Calls Runtime.getRuntime().maxMemory().
Kenny Roote40f3022013-04-30 16:13:40 -07001772 "Lcom/android/org/bouncycastle/crypto/digests/OpenSSLDigest$SHA1;", // Requires com.android.org.conscrypt.NativeCrypto.
Ian Rogers64b6d142012-10-29 16:34:15 -07001773 "Lcom/android/org/bouncycastle/crypto/engines/RSABlindedEngine;", // Calls native ... -> java.math.NativeBN.BN_new().
1774 "Lcom/android/org/bouncycastle/jce/provider/CertBlacklist;", // Calls System.getenv -> OsConstants.initConstants.
1775 "Lcom/android/org/bouncycastle/jce/provider/PKIXCertPathValidatorSpi;", // Calls System.getenv -> OsConstants.initConstants.
Kenny Roote40f3022013-04-30 16:13:40 -07001776 "Lcom/android/org/conscrypt/NativeCrypto;", // Calls native NativeCrypto.clinit().
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001777 "Lcom/android/org/conscrypt/OpenSSLECKeyPairGenerator;", // Calls OsConstants.initConstants.
1778 "Lcom/android/org/conscrypt/OpenSSLMac$HmacMD5;", // Calls native NativeCrypto.clinit().
1779 "Lcom/android/org/conscrypt/OpenSSLMac$HmacSHA1;", // Calls native NativeCrypto.clinit().
1780 "Lcom/android/org/conscrypt/OpenSSLMac$HmacSHA256;", // Calls native NativeCrypto.clinit().
1781 "Lcom/android/org/conscrypt/OpenSSLMac$HmacSHA384;", // Calls native NativeCrypto.clinit().
1782 "Lcom/android/org/conscrypt/OpenSSLMac$HmacSHA512;", // Calls native NativeCrypto.clinit().
Kenny Roote40f3022013-04-30 16:13:40 -07001783 "Lcom/android/org/conscrypt/OpenSSLMessageDigestJDK$MD5;", // Requires com.android.org.conscrypt.NativeCrypto.
1784 "Lcom/android/org/conscrypt/OpenSSLMessageDigestJDK$SHA1;", // Requires com.android.org.conscrypt.NativeCrypto.
1785 "Lcom/android/org/conscrypt/OpenSSLMessageDigestJDK$SHA512;", // Requires com.android.org.conscrypt.NativeCrypto.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001786 "Lcom/android/org/conscrypt/OpenSSLX509CertPath;", // Calls OsConstants.initConstants.
1787 "Lcom/android/org/conscrypt/OpenSSLX509CertificateFactory;", // Calls OsConstants.initConstants.
Kenny Roote40f3022013-04-30 16:13:40 -07001788 "Lcom/android/org/conscrypt/TrustedCertificateStore;", // Calls System.getenv -> OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001789 "Lcom/google/android/gles_jni/EGLContextImpl;", // Calls com.google.android.gles_jni.EGLImpl._nativeClassInit.
1790 "Lcom/google/android/gles_jni/EGLImpl;", // Calls com.google.android.gles_jni.EGLImpl._nativeClassInit.
1791 "Lcom/google/android/gles_jni/GLImpl;", // Calls com.google.android.gles_jni.GLImpl._nativeClassInit.
1792 "Ljava/io/Console;", // Has FileDescriptor(s).
1793 "Ljava/io/File;", // Calls to Random.<init> -> System.currentTimeMillis -> OsConstants.initConstants.
1794 "Ljava/io/FileDescriptor;", // Requires libcore.io.OsConstants.
1795 "Ljava/io/ObjectInputStream;", // Requires java.lang.ClassLoader$SystemClassLoader.
1796 "Ljava/io/ObjectStreamClass;", // Calls to Class.forName -> java.io.FileDescriptor.
1797 "Ljava/io/ObjectStreamConstants;", // Instance of non-image class SerializablePermission.
1798 "Ljava/lang/ClassLoader$SystemClassLoader;", // Calls System.getProperty -> OsConstants.initConstants.
Brian Carlstrom96391602013-06-13 19:49:50 -07001799 "Ljava/lang/HexStringParser;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1800 "Ljava/lang/ProcessManager;", // Calls Thread.currentThread.
Ian Rogers64b6d142012-10-29 16:34:15 -07001801 "Ljava/lang/Runtime;", // Calls System.getProperty -> OsConstants.initConstants.
1802 "Ljava/lang/System;", // Calls OsConstants.initConstants.
1803 "Ljava/math/BigDecimal;", // Calls native ... -> java.math.NativeBN.BN_new().
1804 "Ljava/math/BigInteger;", // Calls native ... -> java.math.NativeBN.BN_new().
Brian Carlstrom96391602013-06-13 19:49:50 -07001805 "Ljava/math/Primality;", // Calls native ... -> java.math.NativeBN.BN_new().
Ian Rogers64b6d142012-10-29 16:34:15 -07001806 "Ljava/math/Multiplication;", // Calls native ... -> java.math.NativeBN.BN_new().
1807 "Ljava/net/InetAddress;", // Requires libcore.io.OsConstants.
1808 "Ljava/net/Inet4Address;", // Sub-class of InetAddress.
1809 "Ljava/net/Inet6Address;", // Sub-class of InetAddress.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001810 "Ljava/net/InetUnixAddress;", // Sub-class of InetAddress.
Ian Rogers64b6d142012-10-29 16:34:15 -07001811 "Ljava/nio/charset/Charset;", // Calls Charset.getDefaultCharset -> System.getProperty -> OsConstants.initConstants.
1812 "Ljava/nio/charset/CharsetICU;", // Sub-class of Charset.
1813 "Ljava/nio/charset/Charsets;", // Calls Charset.forName.
Brian Carlstrom96391602013-06-13 19:49:50 -07001814 "Ljava/security/AlgorithmParameterGenerator;", // Calls OsConstants.initConstants.
1815 "Ljava/security/KeyPairGenerator$KeyPairGeneratorImpl;", // Calls OsConstants.initConstants.
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001816 "Ljava/security/KeyPairGenerator;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001817 "Ljava/security/Security;", // Tries to do disk IO for "security.properties".
Brian Carlstrom96391602013-06-13 19:49:50 -07001818 "Ljava/security/spec/RSAKeyGenParameterSpec;", // java.math.NativeBN.BN_new()
Brian Carlstrombf47e5f2013-05-04 16:06:09 -07001819 "Ljava/sql/Date;", // Calls OsConstants.initConstants.
Brian Carlstrom96391602013-06-13 19:49:50 -07001820 "Ljava/sql/DriverManager;", // Calls OsConstants.initConstants.
1821 "Ljava/sql/Time;", // Calls OsConstants.initConstants.
1822 "Ljava/sql/Timestamp;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001823 "Ljava/util/Date;", // Calls Date.<init> -> System.currentTimeMillis -> OsConstants.initConstants.
Brian Carlstrom96391602013-06-13 19:49:50 -07001824 "Ljava/util/ListResourceBundle;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001825 "Ljava/util/Locale;", // Calls System.getProperty -> OsConstants.initConstants.
Brian Carlstrom96391602013-06-13 19:49:50 -07001826 "Ljava/util/PropertyResourceBundle;", // Calls OsConstants.initConstants.
1827 "Ljava/util/ResourceBundle;", // Calls OsConstants.initConstants.
1828 "Ljava/util/ResourceBundle$MissingBundle;", // Calls OsConstants.initConstants.
1829 "Ljava/util/Scanner;", // regex.Pattern.compileImpl.
Ian Rogers64b6d142012-10-29 16:34:15 -07001830 "Ljava/util/SimpleTimeZone;", // Sub-class of TimeZone.
1831 "Ljava/util/TimeZone;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1832 "Ljava/util/concurrent/ConcurrentHashMap$Segment;", // Calls Runtime.getRuntime().availableProcessors().
Brian Carlstrom96391602013-06-13 19:49:50 -07001833 "Ljava/util/concurrent/ConcurrentSkipListMap;", // Calls OsConstants.initConstants.
1834 "Ljava/util/concurrent/Exchanger;", // Calls OsConstants.initConstants.
1835 "Ljava/util/concurrent/ForkJoinPool;", // Calls OsConstants.initConstants.
1836 "Ljava/util/concurrent/LinkedTransferQueue;", // Calls OsConstants.initConstants.
1837 "Ljava/util/concurrent/Phaser;", // Calls OsConstants.initConstants.
1838 "Ljava/util/concurrent/ScheduledThreadPoolExecutor;", // Calls AtomicLong.VMSupportsCS8()
1839 "Ljava/util/concurrent/SynchronousQueue;", // Calls OsConstants.initConstants.
1840 "Ljava/util/concurrent/atomic/AtomicLong;", // Calls AtomicLong.VMSupportsCS8()
Ian Rogers64b6d142012-10-29 16:34:15 -07001841 "Ljava/util/logging/LogManager;", // Calls System.getProperty -> OsConstants.initConstants.
Brian Carlstrom96391602013-06-13 19:49:50 -07001842 "Ljava/util/prefs/AbstractPreferences;", // Calls OsConstants.initConstants.
1843 "Ljava/util/prefs/FilePreferencesImpl;", // Calls OsConstants.initConstants.
1844 "Ljava/util/prefs/FilePreferencesFactoryImpl;", // Calls OsConstants.initConstants.
1845 "Ljava/util/prefs/Preferences;", // Calls OsConstants.initConstants.
1846 "Ljavax/crypto/KeyAgreement;", // Calls OsConstants.initConstants.
1847 "Ljavax/crypto/KeyGenerator;", // Calls OsConstants.initConstants.
1848 "Ljavax/security/cert/X509Certificate;", // Calls VMClassLoader.getBootClassPathSize.
1849 "Ljavax/security/cert/X509Certificate$1;", // Calls VMClassLoader.getBootClassPathSize.
Ian Rogers64b6d142012-10-29 16:34:15 -07001850 "Ljavax/microedition/khronos/egl/EGL10;", // Requires EGLContext.
1851 "Ljavax/microedition/khronos/egl/EGLContext;", // Requires com.google.android.gles_jni.EGLImpl.
1852 "Ljavax/net/ssl/HttpsURLConnection;", // Calls SSLSocketFactory.getDefault -> java.security.Security.getProperty.
Brian Carlstrom96391602013-06-13 19:49:50 -07001853 "Ljavax/xml/datatype/DatatypeConstants;", // Calls OsConstants.initConstants.
1854 "Ljavax/xml/datatype/FactoryFinder;", // Calls OsConstants.initConstants.
1855 "Ljavax/xml/namespace/QName;", // Calls OsConstants.initConstants.
1856 "Ljavax/xml/validation/SchemaFactoryFinder;", // Calls OsConstants.initConstants.
1857 "Ljavax/xml/xpath/XPathConstants;", // Calls OsConstants.initConstants.
1858 "Ljavax/xml/xpath/XPathFactoryFinder;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001859 "Llibcore/icu/LocaleData;", // Requires java.util.Locale.
Brian Carlstrom333a8ec2013-03-24 23:36:57 -07001860 "Llibcore/icu/TimeZoneNames;", // Requires java.util.TimeZone.
Ian Rogersf08e4732013-04-09 09:45:49 -07001861 "Llibcore/io/IoUtils;", // Calls Random.<init> -> System.currentTimeMillis -> FileDescriptor -> OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001862 "Llibcore/io/OsConstants;", // Platform specific.
1863 "Llibcore/net/MimeUtils;", // Calls libcore.net.MimeUtils.getContentTypesPropertiesStream -> System.getProperty.
Brian Carlstrom96391602013-06-13 19:49:50 -07001864 "Llibcore/reflect/Types;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001865 "Llibcore/util/ZoneInfo;", // Sub-class of TimeZone.
1866 "Llibcore/util/ZoneInfoDB;", // Calls System.getenv -> OsConstants.initConstants.
1867 "Lorg/apache/commons/logging/LogFactory;", // Calls System.getProperty.
1868 "Lorg/apache/harmony/security/fortress/Services;", // Calls ClassLoader.getSystemClassLoader -> System.getProperty.
1869 "Lorg/apache/harmony/security/provider/cert/X509CertFactoryImpl;", // Requires java.nio.charsets.Charsets.
1870 "Lorg/apache/harmony/security/provider/crypto/RandomBitsSupplier;", // Requires java.io.File.
1871 "Lorg/apache/harmony/security/utils/AlgNameMapper;", // Requires java.util.Locale.
Brian Carlstrom96391602013-06-13 19:49:50 -07001872 "Lorg/apache/harmony/security/pkcs10/CertificationRequest;", // Calls Thread.currentThread.
1873 "Lorg/apache/harmony/security/pkcs10/CertificationRequestInfo;", // Calls Thread.currentThread.
1874 "Lorg/apache/harmony/security/pkcs7/AuthenticatedAttributes;", // Calls Thread.currentThread.
1875 "Lorg/apache/harmony/security/pkcs7/SignedData;", // Calls Thread.currentThread.
1876 "Lorg/apache/harmony/security/pkcs7/SignerInfo;", // Calls Thread.currentThread.
1877 "Lorg/apache/harmony/security/pkcs8/PrivateKeyInfo;", // Calls Thread.currentThread.
1878 "Lorg/apache/harmony/security/provider/crypto/SHA1PRNG_SecureRandomImpl;", // Calls OsConstants.initConstants.
Ian Rogers64b6d142012-10-29 16:34:15 -07001879 "Lorg/apache/harmony/security/x501/AttributeTypeAndValue;", // Calls IntegralToString.convertInt -> Thread.currentThread.
1880 "Lorg/apache/harmony/security/x501/DirectoryString;", // Requires BigInteger.
1881 "Lorg/apache/harmony/security/x501/Name;", // Requires org.apache.harmony.security.x501.AttributeTypeAndValue.
Brian Carlstrom96391602013-06-13 19:49:50 -07001882 "Lorg/apache/harmony/security/x509/AccessDescription;", // Calls Thread.currentThread.
1883 "Lorg/apache/harmony/security/x509/AuthorityKeyIdentifier;", // Calls Thread.currentThread.
1884 "Lorg/apache/harmony/security/x509/CRLDistributionPoints;", // Calls Thread.currentThread.
Ian Rogers64b6d142012-10-29 16:34:15 -07001885 "Lorg/apache/harmony/security/x509/Certificate;", // Requires org.apache.harmony.security.x509.TBSCertificate.
Brian Carlstrom96391602013-06-13 19:49:50 -07001886 "Lorg/apache/harmony/security/x509/CertificateIssuer;", // Calls Thread.currentThread.
1887 "Lorg/apache/harmony/security/x509/CertificateList;", // Calls Thread.currentThread.
1888 "Lorg/apache/harmony/security/x509/DistributionPoint;", // Calls Thread.currentThread.
1889 "Lorg/apache/harmony/security/x509/DistributionPointName;", // Calls Thread.currentThread.
Ian Rogers64b6d142012-10-29 16:34:15 -07001890 "Lorg/apache/harmony/security/x509/EDIPartyName;", // Calls native ... -> java.math.NativeBN.BN_new().
1891 "Lorg/apache/harmony/security/x509/GeneralName;", // Requires org.apache.harmony.security.x501.Name.
1892 "Lorg/apache/harmony/security/x509/GeneralNames;", // Requires GeneralName.
Brian Carlstrom96391602013-06-13 19:49:50 -07001893 "Lorg/apache/harmony/security/x509/GeneralSubtree;", // Calls Thread.currentThread.
1894 "Lorg/apache/harmony/security/x509/GeneralSubtrees;", // Calls Thread.currentThread.
1895 "Lorg/apache/harmony/security/x509/InfoAccessSyntax;", // Calls Thread.currentThread.
1896 "Lorg/apache/harmony/security/x509/IssuingDistributionPoint;", // Calls Thread.currentThread.
1897 "Lorg/apache/harmony/security/x509/NameConstraints;", // Calls Thread.currentThread.
1898 "Lorg/apache/harmony/security/x509/TBSCertList$RevokedCertificate;", // Calls NativeBN.BN_new().
1899 "Lorg/apache/harmony/security/x509/TBSCertList;", // Calls Thread.currentThread.
1900 "Lorg/apache/harmony/security/x509/TBSCertificate;", // Requires org.apache.harmony.security.x501.Name.
Ian Rogers64b6d142012-10-29 16:34:15 -07001901 "Lorg/apache/harmony/security/x509/Time;", // Calls native ... -> java.math.NativeBN.BN_new().
1902 "Lorg/apache/harmony/security/x509/Validity;", // Requires x509.Time.
Brian Carlstrom96391602013-06-13 19:49:50 -07001903 "Lorg/apache/harmony/security/x509/tsp/TSTInfo;", // Calls Thread.currentThread.
Ian Rogers64b6d142012-10-29 16:34:15 -07001904 "Lorg/apache/harmony/xml/ExpatParser;", // Calls native ExpatParser.staticInitialize.
Brian Carlstrom96391602013-06-13 19:49:50 -07001905 "Lorg/apache/harmony/xml/ExpatParser$EntityParser;", // Calls ExpatParser.staticInitialize.
Ian Rogers64b6d142012-10-29 16:34:15 -07001906 "Lorg/apache/http/conn/params/ConnRouteParams;", // Requires java.util.Locale.
1907 "Lorg/apache/http/conn/ssl/SSLSocketFactory;", // Calls java.security.Security.getProperty.
1908 "Lorg/apache/http/conn/util/InetAddressUtils;", // Calls regex.Pattern.compile -..-> regex.Pattern.compileImpl.
1909};
1910
Ian Rogers219b5a82013-03-04 13:48:24 -08001911static void InitializeClass(const ParallelCompilationManager* manager, size_t class_def_index)
Ian Rogers3d1548d2012-09-24 14:08:03 -07001912 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers219b5a82013-03-04 13:48:24 -08001913 const DexFile::ClassDef& class_def = manager->GetDexFile()->GetClassDef(class_def_index);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001914 ScopedObjectAccess soa(Thread::Current());
Ian Rogers219b5a82013-03-04 13:48:24 -08001915 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader());
1916 const char* descriptor = manager->GetDexFile()->GetClassDescriptor(class_def);
1917 mirror::Class* klass = manager->GetClassLinker()->FindClass(descriptor, class_loader);
Ian Rogers64b6d142012-10-29 16:34:15 -07001918 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
1919 bool can_init_static_fields = compiling_boot &&
Ian Rogers219b5a82013-03-04 13:48:24 -08001920 manager->GetCompiler()->IsImageClass(descriptor);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001921 if (klass != NULL) {
Ian Rogers64b6d142012-10-29 16:34:15 -07001922 // We don't want class initialization occurring on multiple threads due to deadlock problems.
1923 // For example, a parent class is initialized (holding its lock) that refers to a sub-class
1924 // in its static/class initializer causing it to try to acquire the sub-class' lock. While
1925 // on a second thread the sub-class is initialized (holding its lock) after first initializing
1926 // its parents, whose locks are acquired. This leads to a parent-to-child and a child-to-parent
1927 // lock ordering and consequent potential deadlock.
1928 static Mutex lock1("Initializer lock", kMonitorLock);
Ian Rogers62d6c772013-02-27 08:32:07 -08001929 MutexLock mu(soa.Self(), lock1);
Ian Rogers64b6d142012-10-29 16:34:15 -07001930 // The lock required to initialize the class.
Ian Rogers62d6c772013-02-27 08:32:07 -08001931 ObjectLock lock2(soa.Self(), klass);
Ian Rogers64b6d142012-10-29 16:34:15 -07001932 // Only try to initialize classes that were successfully verified.
Ian Rogers3d1548d2012-09-24 14:08:03 -07001933 if (klass->IsVerified()) {
Ian Rogers219b5a82013-03-04 13:48:24 -08001934 manager->GetClassLinker()->EnsureInitialized(klass, false, can_init_static_fields);
Brian Carlstrom96391602013-06-13 19:49:50 -07001935 if (soa.Self()->IsExceptionPending()) {
1936 soa.Self()->GetException(NULL)->Dump();
1937 }
Ian Rogers64b6d142012-10-29 16:34:15 -07001938 if (!klass->IsInitialized()) {
1939 if (can_init_static_fields) {
1940 bool is_black_listed = false;
1941 for (size_t i = 0; i < arraysize(class_initializer_black_list); ++i) {
1942 if (StringPiece(descriptor) == class_initializer_black_list[i]) {
1943 is_black_listed = true;
1944 break;
1945 }
1946 }
1947 if (!is_black_listed) {
1948 LOG(INFO) << "Initializing: " << descriptor;
1949 if (StringPiece(descriptor) == "Ljava/lang/Void;"){
1950 // Hand initialize j.l.Void to avoid Dex file operations in un-started runtime.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001951 mirror::ObjectArray<mirror::Field>* fields = klass->GetSFields();
Ian Rogers64b6d142012-10-29 16:34:15 -07001952 CHECK_EQ(fields->GetLength(), 1);
Ian Rogers219b5a82013-03-04 13:48:24 -08001953 fields->Get(0)->SetObj(klass, manager->GetClassLinker()->FindPrimitiveClass('V'));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001954 klass->SetStatus(mirror::Class::kStatusInitialized);
Ian Rogers64b6d142012-10-29 16:34:15 -07001955 } else {
Ian Rogers219b5a82013-03-04 13:48:24 -08001956 manager->GetClassLinker()->EnsureInitialized(klass, true, can_init_static_fields);
Ian Rogers64b6d142012-10-29 16:34:15 -07001957 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001958 soa.Self()->AssertNoPendingException();
Ian Rogers64b6d142012-10-29 16:34:15 -07001959 }
1960 }
1961 }
Ian Rogers3d1548d2012-09-24 14:08:03 -07001962 // If successfully initialized place in SSB array.
1963 if (klass->IsInitialized()) {
1964 klass->GetDexCache()->GetInitializedStaticStorage()->Set(klass->GetDexTypeIndex(), klass);
1965 }
1966 }
1967 // Record the final class status if necessary.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001968 mirror::Class::Status status = klass->GetStatus();
Ian Rogers219b5a82013-03-04 13:48:24 -08001969 CompilerDriver::ClassReference ref(manager->GetDexFile(), class_def_index);
1970 CompiledClass* compiled_class = manager->GetCompiler()->GetCompiledClass(ref);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001971 if (compiled_class == NULL) {
1972 compiled_class = new CompiledClass(status);
Ian Rogers219b5a82013-03-04 13:48:24 -08001973 manager->GetCompiler()->RecordClassStatus(ref, compiled_class);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001974 } else {
Brian Carlstrom96391602013-06-13 19:49:50 -07001975 DCHECK_GE(status, compiled_class->GetStatus()) << descriptor;
Ian Rogers3d1548d2012-09-24 14:08:03 -07001976 }
1977 }
Ian Rogers1f539342012-10-03 21:09:42 -07001978 // Clear any class not found or verification exceptions.
Ian Rogers62d6c772013-02-27 08:32:07 -08001979 soa.Self()->ClearException();
Ian Rogers3d1548d2012-09-24 14:08:03 -07001980}
1981
Ian Rogers1212a022013-03-04 10:48:41 -08001982void CompilerDriver::InitializeClasses(jobject jni_class_loader, const DexFile& dex_file,
1983 ThreadPool& thread_pool, TimingLogger& timings) {
Ian Rogers64b6d142012-10-29 16:34:15 -07001984#ifndef NDEBUG
1985 for (size_t i = 0; i < arraysize(class_initializer_black_list); ++i) {
1986 const char* descriptor = class_initializer_black_list[i];
1987 CHECK(IsValidDescriptor(descriptor)) << descriptor;
1988 }
1989#endif
Ian Rogers3d1548d2012-09-24 14:08:03 -07001990 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers219b5a82013-03-04 13:48:24 -08001991 ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, thread_pool);
Ian Rogers64b6d142012-10-29 16:34:15 -07001992 context.ForAll(0, dex_file.NumClassDefs(), InitializeClass, thread_count_);
Ian Rogers3d1548d2012-09-24 14:08:03 -07001993 timings.AddSplit("InitializeNoClinit " + dex_file.GetLocation());
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001994}
1995
Ian Rogers1212a022013-03-04 10:48:41 -08001996void CompilerDriver::InitializeClasses(jobject class_loader,
1997 const std::vector<const DexFile*>& dex_files,
1998 ThreadPool& thread_pool, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -08001999 for (size_t i = 0; i != dex_files.size(); ++i) {
2000 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -07002001 CHECK(dex_file != NULL);
Brian Carlstrom2f663822012-11-07 22:49:06 -08002002 InitializeClasses(class_loader, *dex_file, thread_pool, timings);
Brian Carlstroma5a97a22011-09-15 14:08:49 -07002003 }
2004}
2005
Ian Rogers1212a022013-03-04 10:48:41 -08002006void CompilerDriver::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
Brian Carlstrom2f663822012-11-07 22:49:06 -08002007 ThreadPool& thread_pool, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -08002008 for (size_t i = 0; i != dex_files.size(); ++i) {
2009 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07002010 CHECK(dex_file != NULL);
Brian Carlstrom2f663822012-11-07 22:49:06 -08002011 CompileDexFile(class_loader, *dex_file, thread_pool, timings);
Brian Carlstrom83db7722011-08-26 17:32:56 -07002012 }
2013}
2014
Ian Rogers219b5a82013-03-04 13:48:24 -08002015void CompilerDriver::CompileClass(const ParallelCompilationManager* manager, size_t class_def_index) {
2016 jobject class_loader = manager->GetClassLoader();
2017 const DexFile& dex_file = *manager->GetDexFile();
Elliott Hughesc225caa2012-02-03 15:43:37 -08002018 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002019 {
2020 ScopedObjectAccess soa(Thread::Current());
Ian Rogers219b5a82013-03-04 13:48:24 -08002021 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002022 if (SkipClass(class_loader, dex_file, class_def)) {
2023 return;
2024 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -08002025 }
jeffhaod1224c72012-02-29 13:43:08 -08002026 ClassReference ref(&dex_file, class_def_index);
2027 // Skip compiling classes with generic verifier failures since they will still fail at runtime
Ian Rogers776ac1f2012-04-13 23:36:36 -07002028 if (verifier::MethodVerifier::IsClassRejected(ref)) {
jeffhaod1224c72012-02-29 13:43:08 -08002029 return;
2030 }
Ian Rogers0571d352011-11-03 19:51:38 -07002031 const byte* class_data = dex_file.GetClassData(class_def);
2032 if (class_data == NULL) {
2033 // empty class, probably a marker interface
2034 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002035 }
Ian Rogers0571d352011-11-03 19:51:38 -07002036 ClassDataItemIterator it(dex_file, class_data);
2037 // Skip fields
2038 while (it.HasNextStaticField()) {
2039 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002040 }
Ian Rogers0571d352011-11-03 19:51:38 -07002041 while (it.HasNextInstanceField()) {
2042 it.Next();
2043 }
2044 // Compile direct methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07002045 int64_t previous_direct_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07002046 while (it.HasNextDirectMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07002047 uint32_t method_idx = it.GetMemberIndex();
2048 if (method_idx == previous_direct_method_idx) {
2049 // smali can create dex files with two encoded_methods sharing the same method_idx
2050 // http://code.google.com/p/smali/issues/detail?id=119
2051 it.Next();
2052 continue;
2053 }
2054 previous_direct_method_idx = method_idx;
Ian Rogers219b5a82013-03-04 13:48:24 -08002055 manager->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Ian Rogersfffdb022013-01-04 15:14:08 -08002056 it.GetMethodInvokeType(class_def), class_def_index,
2057 method_idx, class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07002058 it.Next();
2059 }
2060 // Compile virtual methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07002061 int64_t previous_virtual_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07002062 while (it.HasNextVirtualMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07002063 uint32_t method_idx = it.GetMemberIndex();
2064 if (method_idx == previous_virtual_method_idx) {
2065 // smali can create dex files with two encoded_methods sharing the same method_idx
2066 // http://code.google.com/p/smali/issues/detail?id=119
2067 it.Next();
2068 continue;
2069 }
2070 previous_virtual_method_idx = method_idx;
Ian Rogers219b5a82013-03-04 13:48:24 -08002071 manager->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Ian Rogersfffdb022013-01-04 15:14:08 -08002072 it.GetMethodInvokeType(class_def), class_def_index,
2073 method_idx, class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07002074 it.Next();
2075 }
2076 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002077}
2078
Ian Rogers1212a022013-03-04 10:48:41 -08002079void CompilerDriver::CompileDexFile(jobject class_loader, const DexFile& dex_file,
2080 ThreadPool& thread_pool, TimingLogger& timings) {
Ian Rogers219b5a82013-03-04 13:48:24 -08002081 ParallelCompilationManager context(NULL, class_loader, this, &dex_file, thread_pool);
Ian Rogers1212a022013-03-04 10:48:41 -08002082 context.ForAll(0, dex_file.NumClassDefs(), CompilerDriver::CompileClass, thread_count_);
Ian Rogers3d1548d2012-09-24 14:08:03 -07002083 timings.AddSplit("Compile " + dex_file.GetLocation());
Elliott Hughesc225caa2012-02-03 15:43:37 -08002084}
2085
Ian Rogers1212a022013-03-04 10:48:41 -08002086void CompilerDriver::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
2087 InvokeType invoke_type, uint32_t class_def_idx,
2088 uint32_t method_idx, jobject class_loader,
2089 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -07002090 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -08002091 uint64_t start_ns = NanoTime();
Logan Chien4dd96f52012-02-29 01:26:58 +08002092
Ian Rogers169c9a72011-11-13 20:13:17 -08002093 if ((access_flags & kAccNative) != 0) {
Ian Rogers57b86d42012-03-27 16:05:41 -07002094 compiled_method = (*jni_compiler_)(*this, access_flags, method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07002095 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -08002096 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07002097 } else {
Anwar Ghuloum8447d842013-04-30 17:27:40 -07002098 // In small mode we only compile image classes.
Brian Carlstrom96391602013-06-13 19:49:50 -07002099 bool dont_compile = (Runtime::Current()->IsSmallMode() &&
2100 ((image_classes_.get() == NULL) || (image_classes_->size() == 0)));
Anwar Ghuloumc4f105d2013-04-10 16:12:11 -07002101
2102 // Don't compile class initializers, ever.
Anwar Ghuloum1f55ea02013-04-17 07:02:40 -07002103 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
Anwar Ghuloumc4f105d2013-04-10 16:12:11 -07002104 dont_compile = true;
Anwar Ghuloum8447d842013-04-30 17:27:40 -07002105 } else if (code_item->insns_size_in_code_units_ < Runtime::Current()->GetSmallModeMethodDexSizeLimit()) {
2106 // Do compile small methods.
2107 dont_compile = false;
Anwar Ghuloumc4f105d2013-04-10 16:12:11 -07002108 }
2109
Ian Rogersf3e98552013-03-20 15:49:49 -07002110 if (!dont_compile) {
2111 compiled_method = (*compiler_)(*this, code_item, access_flags, invoke_type, class_def_idx,
Anwar Ghuloumc44f68f2013-05-10 13:24:54 -07002112 method_idx, class_loader, dex_file);
Ian Rogersf3e98552013-03-20 15:49:49 -07002113 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
2114 }
Elliott Hughesbb551fa2012-01-25 16:35:29 -08002115 }
Ian Rogers3bb17a62012-01-27 23:56:44 -08002116 uint64_t duration_ns = NanoTime() - start_ns;
Ian Rogersc928de92013-02-27 14:30:44 -08002117#ifdef ART_USE_PORTABLE_COMPILER
Ian Rogers5354ec52013-01-23 14:27:27 -08002118 const uint64_t kWarnMilliSeconds = 1000;
2119#else
2120 const uint64_t kWarnMilliSeconds = 100;
2121#endif
2122 if (duration_ns > MsToNs(kWarnMilliSeconds)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08002123 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -08002124 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -07002125 }
2126
Ian Rogers50b35e22012-10-04 10:09:15 -07002127 Thread* self = Thread::Current();
Elliott Hughesf09afe82011-10-16 14:24:21 -07002128 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -07002129 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002130 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002131 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002132 MutexLock mu(self, compiled_methods_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002133 compiled_methods_.Put(ref, compiled_method);
2134 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002135 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07002136 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07002137
Ian Rogers50b35e22012-10-04 10:09:15 -07002138 if (self->IsExceptionPending()) {
2139 ScopedObjectAccess soa(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002140 LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -08002141 << self->GetException(NULL)->Dump();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002142 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -07002143}
2144
Ian Rogers1212a022013-03-04 10:48:41 -08002145CompiledClass* CompilerDriver::GetCompiledClass(ClassReference ref) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07002146 MutexLock mu(Thread::Current(), compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002147 ClassTable::const_iterator it = compiled_classes_.find(ref);
2148 if (it == compiled_classes_.end()) {
2149 return NULL;
2150 }
2151 CHECK(it->second != NULL);
2152 return it->second;
2153}
2154
Ian Rogers1212a022013-03-04 10:48:41 -08002155CompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07002156 MutexLock mu(Thread::Current(), compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07002157 MethodTable::const_iterator it = compiled_methods_.find(ref);
2158 if (it == compiled_methods_.end()) {
2159 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07002160 }
2161 CHECK(it->second != NULL);
2162 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002163}
2164
Ian Rogers1212a022013-03-04 10:48:41 -08002165void CompilerDriver::SetBitcodeFileName(std::string const& filename) {
2166 typedef void (*SetBitcodeFileNameFn)(CompilerDriver&, std::string const&);
Logan Chien106b2a02012-03-18 04:41:38 +08002167
2168 SetBitcodeFileNameFn set_bitcode_file_name =
buzbee8c4bbb52012-11-26 14:00:58 -08002169 FindFunction<SetBitcodeFileNameFn>(MakeCompilerSoName(compiler_backend_), compiler_library_,
Logan Chien106b2a02012-03-18 04:41:38 +08002170 "compilerLLVMSetBitcodeFileName");
2171
2172 set_bitcode_file_name(*this, filename);
Logan Chien8b977d32012-02-21 19:14:55 +08002173}
Logan Chienf7015fd2012-03-18 01:19:37 +08002174
Ian Rogersfffdb022013-01-04 15:14:08 -08002175
Ian Rogers1212a022013-03-04 10:48:41 -08002176void CompilerDriver::AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
Ian Rogersfffdb022013-01-04 15:14:08 -08002177 size_t class_def_index) {
2178 MutexLock mu(self, freezing_constructor_lock_);
2179 freezing_constructor_classes_.insert(ClassReference(dex_file, class_def_index));
2180}
2181
Ian Rogers1212a022013-03-04 10:48:41 -08002182bool CompilerDriver::RequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
Ian Rogersfffdb022013-01-04 15:14:08 -08002183 size_t class_def_index) {
2184 MutexLock mu(self, freezing_constructor_lock_);
2185 return freezing_constructor_classes_.count(ClassReference(dex_file, class_def_index)) != 0;
2186}
2187
Brian Carlstrom3f47c122013-03-07 00:02:40 -08002188bool CompilerDriver::WriteElf(const std::string& android_root,
Brian Carlstrom265091e2013-01-30 14:08:26 -08002189 bool is_host,
2190 const std::vector<const DexFile*>& dex_files,
2191 std::vector<uint8_t>& oat_contents,
2192 File* file) {
2193 typedef bool (*WriteElfFn)(CompilerDriver&,
Brian Carlstrom3f47c122013-03-07 00:02:40 -08002194 const std::string& android_root,
Brian Carlstrom265091e2013-01-30 14:08:26 -08002195 bool is_host,
2196 const std::vector<const DexFile*>& dex_files,
2197 std::vector<uint8_t>&,
2198 File*);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002199 WriteElfFn WriteElf =
2200 FindFunction<WriteElfFn>(MakeCompilerSoName(compiler_backend_), compiler_library_, "WriteElf");
Brian Carlstrom265091e2013-01-30 14:08:26 -08002201 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
Brian Carlstrom3f47c122013-03-07 00:02:40 -08002202 return WriteElf(*this, android_root, is_host, dex_files, oat_contents, file);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002203}
2204
Ian Rogers1212a022013-03-04 10:48:41 -08002205bool CompilerDriver::FixupElf(File* file, uintptr_t oat_data_begin) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002206 typedef bool (*FixupElfFn)(File*, uintptr_t oat_data_begin);
2207 FixupElfFn FixupElf =
2208 FindFunction<FixupElfFn>(MakeCompilerSoName(compiler_backend_), compiler_library_, "FixupElf");
2209 return FixupElf(file, oat_data_begin);
2210}
2211
Ian Rogers1212a022013-03-04 10:48:41 -08002212void CompilerDriver::GetOatElfInformation(File* file,
2213 size_t& oat_loaded_size,
2214 size_t& oat_data_offset) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002215 typedef bool (*GetOatElfInformationFn)(File*, size_t& oat_loaded_size, size_t& oat_data_offset);
2216 GetOatElfInformationFn GetOatElfInformation =
2217 FindFunction<GetOatElfInformationFn>(MakeCompilerSoName(compiler_backend_), compiler_library_,
2218 "GetOatElfInformation");
2219 GetOatElfInformation(file, oat_loaded_size, oat_data_offset);
2220}
2221
Brian Carlstrom265091e2013-01-30 14:08:26 -08002222bool CompilerDriver::StripElf(File* file) const {
2223 typedef bool (*StripElfFn)(File*);
2224 StripElfFn StripElf =
2225 FindFunction<StripElfFn>(MakeCompilerSoName(compiler_backend_), compiler_library_, "StripElf");
2226 return StripElf(file);
2227}
2228
Ian Rogers1212a022013-03-04 10:48:41 -08002229void CompilerDriver::InstructionSetToLLVMTarget(InstructionSet instruction_set,
2230 std::string& target_triple,
2231 std::string& target_cpu,
2232 std::string& target_attr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08002233 switch (instruction_set) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002234 case kThumb2:
2235 target_triple = "thumb-none-linux-gnueabi";
2236 target_cpu = "cortex-a9";
2237 target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
2238 break;
2239
2240 case kArm:
2241 target_triple = "armv7-none-linux-gnueabi";
2242 // TODO: Fix for Nexus S.
2243 target_cpu = "cortex-a9";
2244 // TODO: Fix for Xoom.
2245 target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
2246 break;
2247
2248 case kX86:
2249 target_triple = "i386-pc-linux-gnu";
2250 target_attr = "";
2251 break;
2252
2253 case kMips:
2254 target_triple = "mipsel-unknown-linux";
2255 target_attr = "mips32r2";
2256 break;
2257
2258 default:
2259 LOG(FATAL) << "Unknown instruction set: " << instruction_set;
2260 }
2261 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07002262} // namespace art