blob: 89f35b361aaf3ceba3132e0ea674e049ccd77d3e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070016
17#include "compiler.h"
18
Elliott Hughesd9c67be2012-02-02 19:54:06 -080019#include <vector>
20
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080021#include <dlfcn.h>
Elliott Hughesd9c67be2012-02-02 19:54:06 -080022#include <unistd.h>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070023
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070024#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070025#include "class_loader.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070026#include "dex_cache.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070027#include "jni_internal.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080028#include "oat_compilation_unit.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070029#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "runtime.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080032#include "space.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070033#include "stl_util.h"
Elliott Hughes601a1232012-02-02 17:47:38 -080034#include "timing_logger.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070035#include "verifier/method_verifier.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070036
Elliott Hughes059d5c12012-03-12 17:39:18 -070037#if defined(__APPLE__)
38#include <mach-o/dyld.h>
39#endif
40
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070041namespace art {
42
Shih-wei Liaoc486c112011-09-13 16:43:52 -070043namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070044 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070045 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080046 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070047}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070048namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070049 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070050 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080051 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070052}
53
Ian Rogers996cc582012-02-14 22:23:29 -080054static double Percentage(size_t x, size_t y) {
Elliott Hughes398f64b2012-03-26 18:05:48 -070055 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
Ian Rogers996cc582012-02-14 22:23:29 -080056}
57
58static void DumpStat(size_t x, size_t y, const char* str) {
59 if (x == 0 && y == 0) {
60 return;
61 }
62 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
63}
64
Ian Rogersc8b306f2012-02-17 21:34:44 -080065class AOTCompilationStats {
66 public:
67 AOTCompilationStats() : stats_lock_("AOT compilation statistics lock"),
68 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
69 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
70 resolved_types_(0), unresolved_types_(0),
71 resolved_instance_fields_(0), unresolved_instance_fields_(0),
Ian Rogers2ed3b952012-03-17 11:49:39 -070072 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0) {
73 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080074 resolved_methods_[i] = 0;
75 unresolved_methods_[i] = 0;
Ian Rogers2ed3b952012-03-17 11:49:39 -070076 virtual_made_direct_[i] = 0;
77 direct_calls_to_boot_[i] = 0;
78 direct_methods_to_boot_[i] = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -070079 }
Ian Rogersc8b306f2012-02-17 21:34:44 -080080 }
81
82 void Dump() {
83 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
84 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
85 DumpStat(resolved_types_, unresolved_types_, "types resolved");
86 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
87 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
88 "static fields resolved");
89 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
90 "static fields local to a class");
91
Ian Rogers2ed3b952012-03-17 11:49:39 -070092 for (size_t i = 0; i <= kMaxInvokeType; i++) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080093 std::ostringstream oss;
Ian Rogers2ed3b952012-03-17 11:49:39 -070094 oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
Ian Rogersc8b306f2012-02-17 21:34:44 -080095 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
Ian Rogers2ed3b952012-03-17 11:49:39 -070096 if (virtual_made_direct_[i] > 0) {
97 std::ostringstream oss2;
98 oss2 << static_cast<InvokeType>(i) << " methods made direct";
99 DumpStat(virtual_made_direct_[i],
100 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
101 oss2.str().c_str());
102 }
103 if (direct_calls_to_boot_[i] > 0) {
104 std::ostringstream oss2;
105 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
106 DumpStat(direct_calls_to_boot_[i],
107 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
108 oss2.str().c_str());
109 }
110 if (direct_methods_to_boot_[i] > 0) {
111 std::ostringstream oss2;
112 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
113 DumpStat(direct_methods_to_boot_[i],
114 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
115 oss2.str().c_str());
116 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800117 }
118 }
Ian Rogers996cc582012-02-14 22:23:29 -0800119
120// Allow lossy statistics in non-debug builds
121#ifndef NDEBUG
122#define STATS_LOCK() MutexLock mu(stats_lock_)
123#else
124#define STATS_LOCK()
125#endif
126
Ian Rogersc8b306f2012-02-17 21:34:44 -0800127 void TypeInDexCache() {
128 STATS_LOCK();
129 types_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800130 }
Ian Rogers996cc582012-02-14 22:23:29 -0800131
Ian Rogersc8b306f2012-02-17 21:34:44 -0800132 void TypeNotInDexCache() {
133 STATS_LOCK();
134 types_not_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800135 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800136
137 void StringInDexCache() {
138 STATS_LOCK();
139 strings_in_dex_cache_++;
140 }
141
142 void StringNotInDexCache() {
143 STATS_LOCK();
144 strings_not_in_dex_cache_++;
145 }
146
147 void TypeDoesntNeedAccessCheck() {
148 STATS_LOCK();
149 resolved_types_++;
150 }
151
152 void TypeNeedsAccessCheck() {
153 STATS_LOCK();
154 unresolved_types_++;
155 }
156
157 void ResolvedInstanceField() {
158 STATS_LOCK();
159 resolved_instance_fields_++;
160 }
161
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700162 void UnresolvedInstanceField() {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800163 STATS_LOCK();
164 unresolved_instance_fields_++;
165 }
166
167 void ResolvedLocalStaticField() {
168 STATS_LOCK();
169 resolved_local_static_fields_++;
170 }
171
172 void ResolvedStaticField() {
173 STATS_LOCK();
174 resolved_static_fields_++;
175 }
176
177 void UnresolvedStaticField() {
178 STATS_LOCK();
179 unresolved_static_fields_++;
180 }
181
182 void ResolvedMethod(InvokeType type) {
183 DCHECK_LE(type, kMaxInvokeType);
184 STATS_LOCK();
185 resolved_methods_[type]++;
186 }
187
188 void UnresolvedMethod(InvokeType type) {
189 DCHECK_LE(type, kMaxInvokeType);
190 STATS_LOCK();
191 unresolved_methods_[type]++;
192 }
193
Ian Rogers2ed3b952012-03-17 11:49:39 -0700194 void VirtualMadeDirect(InvokeType type) {
195 DCHECK_LE(type, kMaxInvokeType);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800196 STATS_LOCK();
Ian Rogers2ed3b952012-03-17 11:49:39 -0700197 virtual_made_direct_[type]++;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800198 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700199
200 void DirectCallsToBoot(InvokeType type) {
201 DCHECK_LE(type, kMaxInvokeType);
202 STATS_LOCK();
203 direct_calls_to_boot_[type]++;
204 }
205
206 void DirectMethodsToBoot(InvokeType type) {
207 DCHECK_LE(type, kMaxInvokeType);
208 STATS_LOCK();
209 direct_methods_to_boot_[type]++;
210 }
211
Ian Rogersc8b306f2012-02-17 21:34:44 -0800212 private:
213 Mutex stats_lock_;
214
215 size_t types_in_dex_cache_;
216 size_t types_not_in_dex_cache_;
217
218 size_t strings_in_dex_cache_;
219 size_t strings_not_in_dex_cache_;
220
221 size_t resolved_types_;
222 size_t unresolved_types_;
223
224 size_t resolved_instance_fields_;
225 size_t unresolved_instance_fields_;
226
227 size_t resolved_local_static_fields_;
228 size_t resolved_static_fields_;
229 size_t unresolved_static_fields_;
230
231 size_t resolved_methods_[kMaxInvokeType + 1];
232 size_t unresolved_methods_[kMaxInvokeType + 1];
Ian Rogers2ed3b952012-03-17 11:49:39 -0700233 size_t virtual_made_direct_[kMaxInvokeType + 1];
234 size_t direct_calls_to_boot_[kMaxInvokeType + 1];
235 size_t direct_methods_to_boot_[kMaxInvokeType + 1];
Ian Rogersc8b306f2012-02-17 21:34:44 -0800236
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700237 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800238};
Ian Rogers996cc582012-02-14 22:23:29 -0800239
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800240static std::string MakeCompilerSoName(InstructionSet instruction_set) {
241 // TODO: is the ARM/Thumb2 instruction set distinction really buying us anything,
242 // or just causing hassle like this?
243 if (instruction_set == kThumb2) {
244 instruction_set = kArm;
245 }
246
Elliott Hughes059d5c12012-03-12 17:39:18 -0700247 // Capitalize the instruction set, because that's what we do in the build system.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800248 std::ostringstream instruction_set_name_os;
249 instruction_set_name_os << instruction_set;
250 std::string instruction_set_name(instruction_set_name_os.str());
251 for (size_t i = 0; i < instruction_set_name.size(); ++i) {
252 instruction_set_name[i] = toupper(instruction_set_name[i]);
253 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700254
255 // Bad things happen if we pull in the libartd-compiler to a libart dex2oat or vice versa,
256 // because we end up with both libart and libartd in the same address space!
Elliott Hughes67d92002012-03-26 15:08:51 -0700257 const char* suffix = (kIsDebugBuild ? "d" : "");
Elliott Hughes059d5c12012-03-12 17:39:18 -0700258
259 // Work out the filename for the compiler library.
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700260#if defined(ART_USE_LLVM_COMPILER)
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800261 std::string library_name(StringPrintf("art%s-compiler-llvm", suffix));
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700262#elif defined(ART_USE_GREENLAND_COMPILER)
263 std::string library_name(StringPrintf("art%s-compiler-greenland", suffix));
264#else
265 std::string library_name(StringPrintf("art%s-compiler-%s", suffix, instruction_set_name.c_str()));
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800266#endif
Elliott Hughes059d5c12012-03-12 17:39:18 -0700267 std::string filename(StringPrintf(OS_SHARED_LIB_FORMAT_STR, library_name.c_str()));
268
269#if defined(__APPLE__)
270 // On Linux, dex2oat will have been built with an RPATH of $ORIGIN/../lib, so dlopen(3) will find
271 // the .so by itself. On Mac OS, there isn't really an equivalent, so we have to manually do the
272 // same work.
Elliott Hughes059d5c12012-03-12 17:39:18 -0700273 uint32_t executable_path_length = 0;
Elliott Hughes448e93c2012-03-28 22:30:06 -0700274 _NSGetExecutablePath(NULL, &executable_path_length);
275 std::string path(executable_path_length, static_cast<char>(0));
276 CHECK_EQ(_NSGetExecutablePath(&path[0], &executable_path_length), 0);
Elliott Hughes059d5c12012-03-12 17:39:18 -0700277
278 // Strip the "/dex2oat".
279 size_t last_slash = path.find_last_of('/');
280 CHECK_NE(last_slash, std::string::npos) << path;
281 path.resize(last_slash);
282
283 // Strip the "/bin".
284 last_slash = path.find_last_of('/');
285 path.resize(last_slash);
286
287 filename = path + "/lib/" + filename;
288#endif
289 return filename;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800290}
291
Elliott Hughes46f060a2012-03-09 17:36:50 -0800292template<typename Fn>
293static Fn FindFunction(const std::string& compiler_so_name, void* library, const char* name) {
294 Fn fn = reinterpret_cast<Fn>(dlsym(library, name));
295 if (fn == NULL) {
296 LOG(FATAL) << "Couldn't find \"" << name << "\" in compiler library " << compiler_so_name << ": " << dlerror();
297 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700298 VLOG(compiler) << "Found \"" << name << "\" at " << reinterpret_cast<void*>(fn);
Elliott Hughes46f060a2012-03-09 17:36:50 -0800299 return fn;
300}
301
Elliott Hughes5523ee02012-02-03 18:18:34 -0800302Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700303 bool support_debugging, const std::set<std::string>* image_classes,
304 bool dump_stats, bool dump_timings)
Brian Carlstromaded5f72011-10-07 17:15:04 -0700305 : instruction_set_(instruction_set),
Elliott Hughesc225caa2012-02-03 15:43:37 -0800306 compiled_classes_lock_("compiled classes lock"),
307 compiled_methods_lock_("compiled method lock"),
308 compiled_invoke_stubs_lock_("compiled invoke stubs lock"),
Logan Chien7a2a23a2012-06-06 11:01:00 +0800309#if defined(ART_USE_LLVM_COMPILER)
310 compiled_proxy_stubs_lock_("compiled proxy stubs lock"),
311#endif
Brian Carlstromaded5f72011-10-07 17:15:04 -0700312 image_(image),
Elliott Hughes5523ee02012-02-03 18:18:34 -0800313 thread_count_(thread_count),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800314 support_debugging_(support_debugging),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800315 stats_(new AOTCompilationStats),
Brian Carlstromba0668e2012-03-26 13:14:07 -0700316 dump_stats_(dump_stats),
317 dump_timings_(dump_timings),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800318 image_classes_(image_classes),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800319 compiler_library_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800320 compiler_(NULL),
Elliott Hughes6f4976c2012-03-13 21:19:01 -0700321 compiler_context_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800322 jni_compiler_(NULL),
Elliott Hughes5ddbe0b2012-06-01 12:58:24 -0700323#if !defined(ART_USE_LLVM_COMPILER)
324 create_invoke_stub_(NULL) {
325#else
326 create_invoke_stub_(NULL),
327 compiler_enable_auto_elf_loading_(NULL),
Logan Chienf7015fd2012-03-18 01:19:37 +0800328 compiler_get_method_code_addr_(NULL),
Elliott Hughes5ddbe0b2012-06-01 12:58:24 -0700329 compiler_get_method_invoke_stub_addr_(NULL) {
Logan Chienf7015fd2012-03-18 01:19:37 +0800330#endif
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800331 std::string compiler_so_name(MakeCompilerSoName(instruction_set_));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800332 compiler_library_ = dlopen(compiler_so_name.c_str(), RTLD_LAZY);
333 if (compiler_library_ == NULL) {
334 LOG(FATAL) << "Couldn't find compiler library " << compiler_so_name << ": " << dlerror();
335 }
336 VLOG(compiler) << "dlopen(\"" << compiler_so_name << "\", RTLD_LAZY) returned " << compiler_library_;
337
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700338#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Logan Chien106b2a02012-03-18 04:41:38 +0800339 // Initialize compiler_context_
340 typedef void (*InitCompilerContextFn)(Compiler&);
341
342 InitCompilerContextFn init_compiler_context =
343 FindFunction<void (*)(Compiler&)>(compiler_so_name,
344 compiler_library_,
345 "ArtInitCompilerContext");
346
347 init_compiler_context(*this);
348#endif
349
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700350 compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "ArtCompileMethod");
Elliott Hughes46f060a2012-03-09 17:36:50 -0800351 jni_compiler_ = FindFunction<JniCompilerFn>(compiler_so_name, compiler_library_, "ArtJniCompileMethod");
352 create_invoke_stub_ = FindFunction<CreateInvokeStubFn>(compiler_so_name, compiler_library_, "ArtCreateInvokeStub");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800353
Logan Chienf7015fd2012-03-18 01:19:37 +0800354#if defined(ART_USE_LLVM_COMPILER)
Logan Chien7a2a23a2012-06-06 11:01:00 +0800355 create_proxy_stub_ = FindFunction<CreateProxyStubFn>(
356 compiler_so_name, compiler_library_, "ArtCreateProxyStub");
Logan Chienf7015fd2012-03-18 01:19:37 +0800357 compiler_enable_auto_elf_loading_ = FindFunction<CompilerEnableAutoElfLoadingFn>(
358 compiler_so_name, compiler_library_, "compilerLLVMEnableAutoElfLoading");
359 compiler_get_method_code_addr_ = FindFunction<CompilerGetMethodCodeAddrFn>(
360 compiler_so_name, compiler_library_, "compilerLLVMGetMethodCodeAddr");
361 compiler_get_method_invoke_stub_addr_ = FindFunction<CompilerGetMethodInvokeStubAddrFn>(
362 compiler_so_name, compiler_library_, "compilerLLVMGetMethodInvokeStubAddr");
363#endif
364
Brian Carlstrom25c33252011-09-18 15:58:35 -0700365 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800366 if (!image_) {
367 CHECK(image_classes_ == NULL);
368 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700369}
370
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700371Compiler::~Compiler() {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800372 {
373 MutexLock mu(compiled_classes_lock_);
374 STLDeleteValues(&compiled_classes_);
375 }
376 {
377 MutexLock mu(compiled_methods_lock_);
378 STLDeleteValues(&compiled_methods_);
379 }
380 {
381 MutexLock mu(compiled_invoke_stubs_lock_);
382 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800383 }
Brian Carlstromd06dfe72012-06-06 22:15:39 -0700384#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700385 {
Logan Chien7a2a23a2012-06-06 11:01:00 +0800386 MutexLock mu(compiled_proxy_stubs_lock_);
387 STLDeleteValues(&compiled_proxy_stubs_);
388 }
Brian Carlstromd06dfe72012-06-06 22:15:39 -0700389#endif
Logan Chien7a2a23a2012-06-06 11:01:00 +0800390 {
Brian Carlstromf5822582012-03-19 22:34:31 -0700391 MutexLock mu(compiled_methods_lock_);
392 STLDeleteElements(&code_to_patch_);
393 }
394 {
395 MutexLock mu(compiled_methods_lock_);
396 STLDeleteElements(&methods_to_patch_);
397 }
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800398#if defined(ART_USE_LLVM_COMPILER)
399 CompilerCallbackFn f = FindFunction<CompilerCallbackFn>(MakeCompilerSoName(instruction_set_),
400 compiler_library_,
401 "compilerLLVMDispose");
402 (*f)(*this);
403#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800404 if (compiler_library_ != NULL) {
405 VLOG(compiler) << "dlclose(" << compiler_library_ << ")";
406 dlclose(compiler_library_);
407 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700408}
409
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700410ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
411 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700412 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700413 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700414 } else {
415 CHECK(instruction_set == kArm || instruction_set == kThumb2);
416 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700417 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700418 }
419}
420
Elliott Hughes8add92d2012-01-18 18:18:43 -0800421ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800422 switch (instruction_set) {
423 case kArm:
424 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800425 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800426 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800427 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800428 default:
Ian Rogers49c48942012-03-11 15:15:37 -0700429 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Ian Rogers169c9a72011-11-13 20:13:17 -0800430 return NULL;
431 }
432}
433
Ian Rogersad25ac52011-10-04 19:13:33 -0700434ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
435 if (instruction_set == kX86) {
436 return x86::CreateAbstractMethodErrorStub();
437 } else {
438 CHECK(instruction_set == kArm || instruction_set == kThumb2);
439 // Generates resolution stub using ARM instruction set
440 return arm::CreateAbstractMethodErrorStub();
441 }
442}
443
Jesse Wilson254db0f2011-11-16 16:44:11 -0500444void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800445 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700446 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800447
Elliott Hughes601a1232012-02-02 17:47:38 -0800448 TimingLogger timings("compiler");
449
450 PreCompile(class_loader, dex_files, timings);
451
Brian Carlstromae826982011-11-09 01:33:42 -0800452 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800453 timings.AddSplit("Compile");
454
Brian Carlstromae826982011-11-09 01:33:42 -0800455 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800456 timings.AddSplit("PostCompile");
457
Brian Carlstromba0668e2012-03-26 13:14:07 -0700458 if (dump_timings_ && timings.GetTotalNs() > MsToNs(1000)) {
Elliott Hughes601a1232012-02-02 17:47:38 -0800459 timings.Dump();
460 }
Ian Rogers996cc582012-02-14 22:23:29 -0800461
Brian Carlstromba0668e2012-03-26 13:14:07 -0700462 if (dump_stats_) {
463 stats_->Dump();
464 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700465}
466
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700467void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700468 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800469
Brian Carlstrom8a487412011-08-29 20:08:52 -0700470 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800471
Ian Rogers0571d352011-11-03 19:51:38 -0700472 // Find the dex_file
473 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
474 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800475 std::vector<const DexFile*> dex_files;
476 dex_files.push_back(&dex_file);
477
Elliott Hughes601a1232012-02-02 17:47:38 -0800478 TimingLogger timings("CompileOne");
479 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800480
Ian Rogers0571d352011-11-03 19:51:38 -0700481 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800482 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
483 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800484
485 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700486}
487
Brian Carlstromae826982011-11-09 01:33:42 -0800488void Compiler::Resolve(const ClassLoader* class_loader,
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800489 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800490 for (size_t i = 0; i != dex_files.size(); ++i) {
491 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700492 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800493 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700494 }
495}
496
Brian Carlstromae826982011-11-09 01:33:42 -0800497void Compiler::PreCompile(const ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800498 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800499 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800500
Brian Carlstromae826982011-11-09 01:33:42 -0800501 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800502 timings.AddSplit("PreCompile.Verify");
503
Brian Carlstromae826982011-11-09 01:33:42 -0800504 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800505 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800506}
507
508void Compiler::PostCompile(const ClassLoader* class_loader,
509 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800510 SetGcMaps(class_loader, dex_files);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800511#if defined(ART_USE_LLVM_COMPILER)
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800512 CompilerCallbackFn f = FindFunction<CompilerCallbackFn>(MakeCompilerSoName(instruction_set_),
513 compiler_library_,
514 "compilerLLVMMaterializeRemainder");
515 (*f)(*this);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800516#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800517}
518
519bool Compiler::IsImageClass(const std::string& descriptor) const {
520 if (image_classes_ == NULL) {
521 return true;
522 }
523 return image_classes_->find(descriptor) != image_classes_->end();
524}
525
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800526bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800527 uint32_t type_idx) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800528 if (!IsImage()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800529 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800530 return false;
531 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800532 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800533 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800534 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800535 return false;
536 }
Ian Rogers996cc582012-02-14 22:23:29 -0800537 bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor());
538 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800539 stats_->TypeInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800540 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800541 stats_->TypeNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800542 }
543 return result;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800544}
545
Ian Rogers1bddec32012-02-04 12:27:34 -0800546bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800547 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800548 // TODO: Add support for loading strings referenced by image_classes_
549 // See also Compiler::ResolveDexFile
550
551 // The following is a test saying that if we're building the image without a restricted set of
552 // image classes then we can assume the string is present in the dex cache if it is there now
Ian Rogers996cc582012-02-14 22:23:29 -0800553 bool result = IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
554 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800555 stats_->StringInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800556 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800557 stats_->StringNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800558 }
559 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800560}
561
562bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800563 const DexFile& dex_file, uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800564 // Get type from dex cache assuming it was populated by the verifier
565 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
566 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800567 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800568 return false; // Unknown class needs access checks.
569 }
570 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
571 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
572 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800573 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800574 return false; // Incomplete referrer knowledge needs access check.
575 }
576 // Perform access check, will return true if access is ok or false if we're going to have to
577 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800578 bool result = referrer_class->CanAccess(resolved_class);
579 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800580 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800581 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800582 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800583 }
584 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800585}
586
587bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
588 const DexCache* dex_cache,
589 const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800590 uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800591 // Get type from dex cache assuming it was populated by the verifier.
592 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
593 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800594 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800595 return false; // Unknown class needs access checks.
596 }
597 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
598 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
599 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800600 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800601 return false; // Incomplete referrer knowledge needs access check.
602 }
603 // Perform access and instantiable checks, will return true if access is ok or false if we're
604 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800605 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
606 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800607 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800608 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800609 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800610 }
611 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800612}
613
Logan Chien4dd96f52012-02-29 01:26:58 +0800614static Class* ComputeReferrerClass(OatCompilationUnit* mUnit) {
615 const DexFile::MethodId& referrer_method_id =
616 mUnit->dex_file_->GetMethodId(mUnit->method_idx_);
617
618 return mUnit->class_linker_->ResolveType(
619 *mUnit->dex_file_, referrer_method_id.class_idx_,
620 mUnit->dex_cache_, mUnit->class_loader_);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800621}
622
Logan Chien4dd96f52012-02-29 01:26:58 +0800623static Field* ComputeReferrerField(OatCompilationUnit* mUnit, uint32_t field_idx) {
624 return mUnit->class_linker_->ResolveField(
625 *mUnit->dex_file_, field_idx, mUnit->dex_cache_,
626 mUnit->class_loader_, false);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800627}
628
Logan Chien4dd96f52012-02-29 01:26:58 +0800629static Method* ComputeReferrerMethod(OatCompilationUnit* mUnit, uint32_t method_idx) {
630 return mUnit->class_linker_->ResolveMethod(
631 *mUnit->dex_file_, method_idx, mUnit->dex_cache_,
632 mUnit->class_loader_, true);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800633}
634
Logan Chien4dd96f52012-02-29 01:26:58 +0800635bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
jeffhao8cd6dda2012-02-22 10:15:34 -0800636 int& field_offset, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800637 // Conservative defaults
638 field_offset = -1;
639 is_volatile = true;
640 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800641 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800642 if (resolved_field != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800643 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogerse2645d32012-04-11 14:42:42 -0700644 if (referrer_class != NULL) {
645 Class* fields_class = resolved_field->GetDeclaringClass();
646 bool access_ok = referrer_class->CanAccess(fields_class) &&
647 referrer_class->CanAccessMember(fields_class,
648 resolved_field->GetAccessFlags());
649 if (!access_ok) {
650 // The referring class can't access the resolved field, this may occur as a result of a
651 // protected field being made public by a sub-class. Resort to the dex file to determine
652 // the correct class for the access check.
653 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
654 Class* dex_fields_class = mUnit->class_linker_->ResolveType(dex_file,
655 dex_file.GetFieldId(field_idx).class_idx_,
656 referrer_class);
657 access_ok = referrer_class->CanAccess(dex_fields_class) &&
658 referrer_class->CanAccessMember(dex_fields_class,
659 resolved_field->GetAccessFlags());
660 }
661 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
662 fields_class != referrer_class;
663 if (access_ok && !is_write_to_final_from_wrong_class) {
664 field_offset = resolved_field->GetOffset().Int32Value();
665 is_volatile = resolved_field->IsVolatile();
666 stats_->ResolvedInstanceField();
667 return true; // Fast path.
668 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800669 }
670 }
671 // Clean up any exception left by field/type resolution
672 Thread* thread = Thread::Current();
673 if (thread->IsExceptionPending()) {
674 thread->ClearException();
675 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800676 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800677 return false; // Incomplete knowledge needs slow path.
678}
679
Logan Chien4dd96f52012-02-29 01:26:58 +0800680bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
Ian Rogers1bddec32012-02-04 12:27:34 -0800681 int& field_offset, int& ssb_index,
jeffhao8cd6dda2012-02-22 10:15:34 -0800682 bool& is_referrers_class, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800683 // Conservative defaults
684 field_offset = -1;
685 ssb_index = -1;
686 is_referrers_class = false;
687 is_volatile = true;
688 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800689 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800690 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800691 DCHECK(resolved_field->IsStatic());
Logan Chien4dd96f52012-02-29 01:26:58 +0800692 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800693 if (referrer_class != NULL) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800694 Class* fields_class = resolved_field->GetDeclaringClass();
695 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800696 is_referrers_class = true; // implies no worrying about class initialization
697 field_offset = resolved_field->GetOffset().Int32Value();
698 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800699 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800700 return true; // fast path
701 } else {
Ian Rogerse2645d32012-04-11 14:42:42 -0700702 bool access_ok = referrer_class->CanAccess(fields_class) &&
703 referrer_class->CanAccessMember(fields_class,
704 resolved_field->GetAccessFlags());
705 if (!access_ok) {
706 // The referring class can't access the resolved field, this may occur as a result of a
707 // protected field being made public by a sub-class. Resort to the dex file to determine
708 // the correct class for the access check. Don't change the field's class as that is
709 // used to identify the SSB.
710 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
711 Class* dex_fields_class =
712 mUnit->class_linker_->ResolveType(dex_file,
713 dex_file.GetFieldId(field_idx).class_idx_,
714 referrer_class);
715 access_ok = referrer_class->CanAccess(dex_fields_class) &&
716 referrer_class->CanAccessMember(dex_fields_class,
717 resolved_field->GetAccessFlags());
718 }
jeffhao8cd6dda2012-02-22 10:15:34 -0800719 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogerse2645d32012-04-11 14:42:42 -0700720 if (access_ok && !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800721 // We have the resolved field, we must make it into a ssbIndex for the referrer
722 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800723 // TODO: for images we can elide the static storage base null check
724 // if we know there's a non-null entry in the image
Logan Chien4dd96f52012-02-29 01:26:58 +0800725 if (fields_class->GetDexCache() == mUnit->dex_cache_) {
Ian Rogers4103ad22012-02-06 09:18:25 -0800726 // common case where the dex cache of both the referrer and the field are the same,
727 // no need to search the dex file
728 ssb_index = fields_class->GetDexTypeIndex();
729 field_offset = resolved_field->GetOffset().Int32Value();
730 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800731 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800732 return true;
733 }
Ian Rogerse2645d32012-04-11 14:42:42 -0700734 // Search dex file for localized ssb index, may fail if field's class is a parent
735 // of the class mentioned in the dex file and there is no dex cache entry.
Ian Rogers1bddec32012-02-04 12:27:34 -0800736 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
737 const DexFile::StringId* string_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800738 mUnit->dex_file_->FindStringId(descriptor);
Ian Rogers1bddec32012-02-04 12:27:34 -0800739 if (string_id != NULL) {
740 const DexFile::TypeId* type_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800741 mUnit->dex_file_->FindTypeId(mUnit->dex_file_->GetIndexForStringId(*string_id));
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700742 if (type_id != NULL) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800743 // medium path, needs check of static storage base being initialized
Logan Chien4dd96f52012-02-29 01:26:58 +0800744 ssb_index = mUnit->dex_file_->GetIndexForTypeId(*type_id);
Ian Rogers1bddec32012-02-04 12:27:34 -0800745 field_offset = resolved_field->GetOffset().Int32Value();
746 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800747 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800748 return true;
749 }
750 }
751 }
752 }
753 }
754 }
755 // Clean up any exception left by field/type resolution
756 Thread* thread = Thread::Current();
757 if (thread->IsExceptionPending()) {
758 thread->ClearException();
759 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800760 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800761 return false; // Incomplete knowledge needs slow path.
762}
763
Ian Rogers2ed3b952012-03-17 11:49:39 -0700764void Compiler::GetCodeAndMethodForDirectCall(InvokeType type, InvokeType sharp_type, Method* method,
765 uintptr_t& direct_code, uintptr_t& direct_method) {
766 direct_code = 0;
767 direct_method = 0;
768 if (sharp_type != kStatic && sharp_type != kDirect) {
769 return;
770 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700771 bool method_code_in_boot = method->GetDeclaringClass()->GetClassLoader() == NULL;
772 if (!method_code_in_boot) {
773 return;
774 }
775 bool has_clinit_trampoline = method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
776 if (has_clinit_trampoline) {
777 return;
778 }
779 stats_->DirectCallsToBoot(type);
780 stats_->DirectMethodsToBoot(type);
Ian Rogers3fa13792012-03-18 15:53:45 -0700781 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
782 if (compiling_boot) {
Brian Carlstrom0637e272012-03-20 01:07:52 -0700783 const bool kSupportBootImageFixup = true;
Ian Rogers3fa13792012-03-18 15:53:45 -0700784 if (kSupportBootImageFixup) {
785 MethodHelper mh(method);
786 if (IsImageClass(mh.GetDeclaringClassDescriptor())) {
Brian Carlstrom0637e272012-03-20 01:07:52 -0700787 // We can only branch directly to Methods that are resolved in the DexCache.
788 // Otherwise we won't invoke the resolution trampoline.
Ian Rogers3fa13792012-03-18 15:53:45 -0700789 direct_method = -1;
Brian Carlstrom0637e272012-03-20 01:07:52 -0700790 direct_code = -1;
Ian Rogers3fa13792012-03-18 15:53:45 -0700791 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700792 }
793 } else {
794 if (Runtime::Current()->GetHeap()->GetImageSpace()->Contains(method)) {
795 direct_method = reinterpret_cast<uintptr_t>(method);
796 }
797 direct_code = reinterpret_cast<uintptr_t>(method->GetCode());
Ian Rogers2ed3b952012-03-17 11:49:39 -0700798 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700799}
800
Ian Rogersfb6adba2012-03-04 21:51:51 -0800801bool Compiler::ComputeInvokeInfo(uint32_t method_idx, OatCompilationUnit* mUnit, InvokeType& type,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700802 int& vtable_idx, uintptr_t& direct_code,
803 uintptr_t& direct_method) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800804 vtable_idx = -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700805 direct_code = 0;
806 direct_method = 0;
Logan Chien4dd96f52012-02-29 01:26:58 +0800807 Method* resolved_method = ComputeReferrerMethod(mUnit, method_idx);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800808 if (resolved_method != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800809 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800810 if (referrer_class != NULL) {
811 Class* methods_class = resolved_method->GetDeclaringClass();
812 if (!referrer_class->CanAccess(methods_class) ||
813 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800814 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800815 // The referring class can't access the resolved method, this may occur as a result of a
816 // protected method being made public by implementing an interface that re-declares the
817 // method public. Resort to the dex file to determine the correct class for the access check
Logan Chien4dd96f52012-02-29 01:26:58 +0800818 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800819 methods_class =
Logan Chien4dd96f52012-02-29 01:26:58 +0800820 mUnit->class_linker_->ResolveType(dex_file,
821 dex_file.GetMethodId(method_idx).class_idx_,
822 referrer_class);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800823 }
824 if (referrer_class->CanAccess(methods_class) &&
825 referrer_class->CanAccessMember(methods_class,
826 resolved_method->GetAccessFlags())) {
827 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogersf320b632012-03-13 18:47:47 -0700828 const bool kEnableSharpening = true;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700829 // Sharpen a virtual call into a direct call when the target is known.
830 bool can_sharpen = type == kVirtual && (resolved_method->IsFinal() ||
831 methods_class->IsFinal());
832 // ensure the vtable index will be correct to dispatch in the vtable of the super class
jeffhao4155fcd2012-03-21 11:42:12 -0700833 can_sharpen = can_sharpen || (type == kSuper && referrer_class != methods_class &&
Ian Rogers2ed3b952012-03-17 11:49:39 -0700834 referrer_class->IsSubClass(methods_class) &&
jeffhao4155fcd2012-03-21 11:42:12 -0700835 vtable_idx < methods_class->GetVTable()->GetLength() &&
836 methods_class->GetVTable()->Get(vtable_idx) == resolved_method);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700837 if (kEnableSharpening && can_sharpen) {
838 stats_->ResolvedMethod(type);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800839 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
840 // dex cache, check that this resolved method is where we expect it.
841 CHECK(referrer_class->GetDexCache()->GetResolvedMethod(method_idx) == resolved_method)
842 << PrettyMethod(resolved_method);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700843 stats_->VirtualMadeDirect(type);
844 GetCodeAndMethodForDirectCall(type, kDirect, resolved_method, direct_code, direct_method);
845 type = kDirect;
846 return true;
847 } else if (type == kSuper) {
848 // Unsharpened super calls are suspicious so go slowpath.
849 } else {
850 stats_->ResolvedMethod(type);
851 GetCodeAndMethodForDirectCall(type, type, resolved_method, direct_code, direct_method);
852 return true;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800853 }
854 }
855 }
856 }
857 // Clean up any exception left by method/type resolution
858 Thread* thread = Thread::Current();
859 if (thread->IsExceptionPending()) {
860 thread->ClearException();
861 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800862 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800863 return false; // Incomplete knowledge needs slow path.
864}
865
Brian Carlstromf5822582012-03-19 22:34:31 -0700866void Compiler::AddCodePatch(DexCache* dex_cache,
867 const DexFile* dex_file,
868 uint32_t referrer_method_idx,
869 uint32_t referrer_access_flags,
870 uint32_t target_method_idx,
871 bool target_is_direct,
Ian Rogers3fa13792012-03-18 15:53:45 -0700872 size_t literal_offset) {
873 MutexLock mu(compiled_methods_lock_);
Brian Carlstromf5822582012-03-19 22:34:31 -0700874 code_to_patch_.push_back(new PatchInformation(dex_cache,
875 dex_file,
876 referrer_method_idx,
877 referrer_access_flags,
878 target_method_idx,
879 target_is_direct,
880 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -0700881}
Brian Carlstromf5822582012-03-19 22:34:31 -0700882void Compiler::AddMethodPatch(DexCache* dex_cache,
883 const DexFile* dex_file,
884 uint32_t referrer_method_idx,
885 uint32_t referrer_access_flags,
886 uint32_t target_method_idx,
887 bool target_is_direct,
Ian Rogers3fa13792012-03-18 15:53:45 -0700888 size_t literal_offset) {
889 MutexLock mu(compiled_methods_lock_);
Brian Carlstromf5822582012-03-19 22:34:31 -0700890 methods_to_patch_.push_back(new PatchInformation(dex_cache,
891 dex_file,
892 referrer_method_idx,
893 referrer_access_flags,
894 target_method_idx,
895 target_is_direct,
896 literal_offset));
Ian Rogers3fa13792012-03-18 15:53:45 -0700897}
898
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800899// Return true if the class should be skipped during compilation. We
900// never skip classes in the boot class loader. However, if we have a
901// non-boot class loader and we can resolve the class in the boot
902// class loader, we do skip the class. This happens if an app bundles
903// classes found in the boot classpath. Since at runtime we will
904// select the class from the boot classpath, do not attempt to resolve
905// or compile it now.
906static bool SkipClass(const ClassLoader* class_loader,
907 const DexFile& dex_file,
908 const DexFile::ClassDef& class_def) {
909 if (class_loader == NULL) {
910 return false;
911 }
912 const char* descriptor = dex_file.GetClassDescriptor(class_def);
913 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
914 Class* klass = class_linker->FindClass(descriptor, NULL);
915 if (klass == NULL) {
916 Thread* self = Thread::Current();
917 CHECK(self->IsExceptionPending());
918 self->ClearException();
919 return false;
920 }
921 return true;
922}
923
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800924class Context {
925 public:
926 Context(ClassLinker* class_linker,
927 const ClassLoader* class_loader,
928 Compiler* compiler,
929 DexCache* dex_cache,
930 const DexFile* dex_file)
931 : class_linker_(class_linker),
932 class_loader_(class_loader),
933 compiler_(compiler),
934 dex_cache_(dex_cache),
935 dex_file_(dex_file) {}
936
937 ClassLinker* GetClassLinker() {
938 CHECK(class_linker_ != NULL);
939 return class_linker_;
940 }
941 const ClassLoader* GetClassLoader() {
942 return class_loader_;
943 }
944 Compiler* GetCompiler() {
945 CHECK(compiler_ != NULL);
946 return compiler_;
947 }
948 DexCache* GetDexCache() {
949 CHECK(dex_cache_ != NULL);
950 return dex_cache_;
951 }
952 const DexFile* GetDexFile() {
953 CHECK(dex_file_ != NULL);
954 return dex_file_;
955 }
956
957 private:
958 ClassLinker* class_linker_;
959 const ClassLoader* class_loader_;
960 Compiler* compiler_;
961 DexCache* dex_cache_;
962 const DexFile* dex_file_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800963};
964
965typedef void Callback(Context* context, size_t index);
966
967class WorkerThread {
968 public:
Elliott Hughes1e409252012-02-06 11:21:27 -0800969 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
970 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
971 if (spawn_) {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700972 // Mac OS stacks are only 512KiB. Make sure we have the same stack size on all platforms.
973 pthread_attr_t attr;
974 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new compiler worker thread");
975 CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, 1*MB), "new compiler worker thread");
976 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Go, this), "new compiler worker thread");
977 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new compiler worker thread");
Elliott Hughes1e409252012-02-06 11:21:27 -0800978 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800979 }
980
981 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800982 if (spawn_) {
983 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
984 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800985 }
986
987 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800988 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800989 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
990 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800991 if (worker->spawn_) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700992 runtime->AttachCurrentThread("Compiler Worker", true, NULL);
Elliott Hughes1e409252012-02-06 11:21:27 -0800993 }
Elliott Hughes34e06962012-04-09 13:55:55 -0700994 Thread::Current()->SetState(kRunnable);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800995 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800996 if (worker->spawn_) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700997 Thread::Current()->SetState(kNative);
Elliott Hughes1e409252012-02-06 11:21:27 -0800998 runtime->DetachCurrentThread();
999 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001000 return NULL;
1001 }
1002
Elliott Hughes1e409252012-02-06 11:21:27 -08001003 void Go() {
1004 Go(this);
1005 }
1006
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001007 void Run() {
Brian Carlstrom64277f32012-03-26 23:53:34 -07001008 Thread* self = Thread::Current();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001009 for (size_t i = begin_; i < end_; i += stripe_) {
1010 callback_(context_, i);
Brian Carlstrom64277f32012-03-26 23:53:34 -07001011 CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException()) << " " << i;
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001012 }
1013 }
1014
1015 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -08001016 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001017
1018 Context* context_;
1019 size_t begin_;
1020 size_t end_;
1021 Callback* callback_;
1022 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -08001023
1024 friend void ForAll(Context*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001025};
1026
Elliott Hughes5523ee02012-02-03 18:18:34 -08001027void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Brian Carlstrom64277f32012-03-26 23:53:34 -07001028 Thread* self = Thread::Current();
1029 CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException());
Elliott Hughes1e409252012-02-06 11:21:27 -08001030 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -08001031
Elliott Hughes1e409252012-02-06 11:21:27 -08001032 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -08001033 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -08001034 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001035 }
Elliott Hughes1e409252012-02-06 11:21:27 -08001036 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001037
Elliott Hughes81d91512012-02-03 16:38:43 -08001038 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
Elliott Hughes34e06962012-04-09 13:55:55 -07001039 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes81d91512012-02-03 16:38:43 -08001040 STLDeleteElements(&threads);
1041}
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001042
1043static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001044 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001045
1046 // Method and Field are the worst. We can't resolve without either
1047 // context from the code use (to disambiguate virtual vs direct
1048 // method and instance vs static field) or from class
1049 // definitions. While the compiler will resolve what it can as it
1050 // needs it, here we try to resolve fields and methods used in class
1051 // definitions, since many of them many never be referenced by
1052 // generated code.
1053 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001054 if (SkipClass(context->GetClassLoader(), dex_file, class_def)) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001055 return;
1056 }
1057
1058 // Note the class_data pointer advances through the headers,
1059 // static fields, instance fields, direct methods, and virtual
1060 // methods.
1061 const byte* class_data = dex_file.GetClassData(class_def);
1062 if (class_data == NULL) {
1063 // empty class such as a marker interface
1064 return;
1065 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001066 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001067 ClassLinker* class_linker = context->GetClassLinker();
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001068 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1069 ClassDataItemIterator it(dex_file, class_data);
1070 while (it.HasNextStaticField()) {
1071 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001072 context->GetClassLoader(), true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001073 if (field == NULL) {
1074 CHECK(self->IsExceptionPending());
1075 self->ClearException();
1076 }
1077 it.Next();
1078 }
1079 while (it.HasNextInstanceField()) {
1080 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001081 context->GetClassLoader(), false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001082 if (field == NULL) {
1083 CHECK(self->IsExceptionPending());
1084 self->ClearException();
1085 }
1086 it.Next();
1087 }
1088 while (it.HasNextDirectMethod()) {
1089 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001090 context->GetClassLoader(), true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001091 if (method == NULL) {
1092 CHECK(self->IsExceptionPending());
1093 self->ClearException();
1094 }
1095 it.Next();
1096 }
1097 while (it.HasNextVirtualMethod()) {
1098 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001099 context->GetClassLoader(), false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001100 if (method == NULL) {
1101 CHECK(self->IsExceptionPending());
1102 self->ClearException();
1103 }
1104 it.Next();
1105 }
1106 DCHECK(!it.HasNext());
1107}
1108
1109static void ResolveType(Context* context, size_t type_idx) {
1110 // Class derived values are more complicated, they require the linker and loader.
1111 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001112 Class* klass = context->GetClassLinker()->ResolveType(*context->GetDexFile(),
1113 type_idx,
1114 context->GetDexCache(),
1115 context->GetClassLoader());
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001116 if (klass == NULL) {
1117 CHECK(self->IsExceptionPending());
1118 Thread::Current()->ClearException();
1119 }
1120}
1121
1122void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001123 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001124 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001125
Brian Carlstromae826982011-11-09 01:33:42 -08001126 // Strings are easy in that they always are simply resolved to literals in the same file
1127 if (image_ && image_classes_ == NULL) {
1128 // TODO: Add support for loading strings referenced by image_classes_
1129 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -07001130 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
1131 class_linker->ResolveString(dex_file, string_idx, dex_cache);
1132 }
Elliott Hughesff738062012-02-03 15:00:42 -08001133 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001134 }
1135
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001136 Context context(class_linker, class_loader, this, dex_cache, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001137 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001138 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -07001139
Elliott Hughes5523ee02012-02-03 18:18:34 -08001140 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -08001141 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001142}
1143
Brian Carlstromae826982011-11-09 01:33:42 -08001144void Compiler::Verify(const ClassLoader* class_loader,
1145 const std::vector<const DexFile*>& dex_files) {
1146 for (size_t i = 0; i != dex_files.size(); ++i) {
1147 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -07001148 CHECK(dex_file != NULL);
1149 VerifyDexFile(class_loader, *dex_file);
1150 }
1151}
1152
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001153static void VerifyClass(Context* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001154 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
1155 const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def);
1156 Class* klass = context->GetClassLinker()->FindClass(descriptor, context->GetClassLoader());
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001157 if (klass == NULL) {
1158 Thread* self = Thread::Current();
1159 CHECK(self->IsExceptionPending());
1160 self->ClearException();
jeffhaof56197c2012-03-05 18:01:54 -08001161
1162 /*
1163 * At compile time, we can still structurally verify the class even if FindClass fails.
1164 * This is to ensure the class is structurally sound for compilation. An unsound class
1165 * will be rejected by the verifier and later skipped during compilation in the compiler.
1166 */
1167 std::string error_msg;
jeffhaof1e6b7c2012-06-05 18:33:30 -07001168 if (verifier::MethodVerifier::VerifyClass(context->GetDexFile(), context->GetDexCache(),
1169 context->GetClassLoader(), class_def_index, error_msg) == verifier::MethodVerifier::kHardFailure) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001170 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
jeffhaof56197c2012-03-05 18:01:54 -08001171 LOG(ERROR) << "Verification failed on class "
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001172 << PrettyDescriptor(context->GetDexFile()->GetClassDescriptor(class_def))
jeffhaof56197c2012-03-05 18:01:54 -08001173 << " because: " << error_msg;
1174 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001175 return;
1176 }
1177 CHECK(klass->IsResolved()) << PrettyClass(klass);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001178 context->GetClassLinker()->VerifyClass(klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001179
1180 if (klass->IsErroneous()) {
1181 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1182 CHECK(Thread::Current()->IsExceptionPending());
1183 Thread::Current()->ClearException();
jeffhao1ac29442012-03-26 11:37:32 -07001184 art::Compiler::ClassReference ref(context->GetDexFile(), class_def_index);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001185 }
1186
jeffhaof1e6b7c2012-06-05 18:33:30 -07001187 CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous()) << PrettyClass(klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08001188 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
1189}
1190
jeffhao98eacac2011-09-14 16:11:53 -07001191void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -07001192 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -07001193
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001194 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1195 Context context(class_linker, class_loader, this, class_linker->FindDexCache(dex_file), &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001196 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -07001197
jeffhaob4df5142011-09-19 20:25:32 -07001198 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001199}
1200
Brian Carlstromae826982011-11-09 01:33:42 -08001201void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
1202 const std::vector<const DexFile*>& dex_files) {
1203 for (size_t i = 0; i != dex_files.size(); ++i) {
1204 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001205 CHECK(dex_file != NULL);
1206 InitializeClassesWithoutClinit(class_loader, *dex_file);
1207 }
1208}
1209
1210void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
1211 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -07001212 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1213 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001214 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1215 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001216 if (klass != NULL) {
jeffhao1ac29442012-03-26 11:37:32 -07001217 if (klass->IsVerified()) {
1218 // Only try to initialize classes that were successfully verified.
Ian Rogers0045a292012-03-31 21:08:41 -07001219 bool compiling_boot = Runtime::Current()->GetHeap()->GetSpaces().size() == 1;
1220 bool can_init_static_fields = compiling_boot &&
1221 IsImageClass(descriptor);
1222 class_linker->EnsureInitialized(klass, false, can_init_static_fields);
jeffhao1ac29442012-03-26 11:37:32 -07001223 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001224 // record the final class status if necessary
1225 Class::Status status = klass->GetStatus();
1226 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001227 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001228 CompiledClass* compiled_class = GetCompiledClass(ref);
1229 if (compiled_class == NULL) {
1230 compiled_class = new CompiledClass(status);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001231 compiled_classes_.Put(ref, compiled_class);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001232 } else {
1233 DCHECK_EQ(status, compiled_class->GetStatus());
1234 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001235 }
1236 // clear any class not found or verification exceptions
1237 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -07001238 }
1239
1240 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1241 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
1242 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001243 if (klass == NULL) {
1244 Thread::Current()->ClearException();
1245 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -07001246 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
1247 }
jeffhao98eacac2011-09-14 16:11:53 -07001248 }
1249}
1250
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001251// TODO: This class shares some implementation with WorkerThread. We don't want
1252// to perturb the non-LLVM side at the same time. Staging the refactoring for
1253// the future.
1254class DexFilesWorkerThread {
1255 public:
1256 DexFilesWorkerThread(Context *worker_context, Callback class_callback,
1257 const std::vector<const DexFile*>& dex_files,
1258 volatile int32_t* shared_class_index, bool spawn)
1259 : spawn_(spawn), worker_context_(worker_context),
1260 class_callback_(class_callback), dex_files_(dex_files),
1261 context_(NULL), shared_class_index_(shared_class_index) {
1262 if (spawn_) {
TDYa1278cea5fd2012-05-29 22:22:32 -07001263 pthread_attr_t attr;
1264 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new compiler worker thread");
TDYa12761b409c2012-06-05 23:54:30 -07001265 CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, 1*MB), "new compiler worker thread");
TDYa1278cea5fd2012-05-29 22:22:32 -07001266 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Go, this), "new compiler worker thread");
1267 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new compiler worker thread");
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001268 }
1269 }
1270
1271 ~DexFilesWorkerThread() {
1272 if (spawn_) {
1273 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
1274 }
1275 delete context_;
1276 }
1277
1278 private:
1279 static void* Go(void* arg) {
1280 DexFilesWorkerThread* worker = reinterpret_cast<DexFilesWorkerThread*>(arg);
1281 Runtime* runtime = Runtime::Current();
1282 if (worker->spawn_) {
1283 runtime->AttachCurrentThread("Compiler Worker", true, NULL);
1284 }
1285 Thread::Current()->SetState(kRunnable);
1286 worker->Run();
1287 if (worker->spawn_) {
1288 Thread::Current()->SetState(kNative);
1289 runtime->DetachCurrentThread();
1290 }
1291 return NULL;
1292 }
1293
1294 void Go() {
1295 Go(this);
1296 }
1297
1298 void SwitchToDexFile(size_t dex_file_index) {
1299 CHECK (dex_file_index < dex_files_.size());
1300
1301 const DexFile* dex_file = dex_files_[dex_file_index];
1302 CHECK(dex_file != NULL);
1303
1304 // Destroy the old context
1305 delete context_;
1306
1307 // TODO: Add a callback to let the client specify the class_linker and
1308 // dex_cache in the context for the current working dex file.
1309 context_ = new Context(/* class_linker */NULL,
1310 worker_context_->GetClassLoader(),
1311 worker_context_->GetCompiler(),
1312 /* dex_cache */NULL, dex_file);
1313
1314 CHECK(context_ != NULL);
1315 }
1316
1317 void Run() {
1318 Thread* self = Thread::Current();
1319 size_t cur_dex_file_index = 0;
1320 size_t class_index_base = 0;
1321
1322 SwitchToDexFile(0);
1323
1324 while (true) {
1325 size_t class_index =
1326 static_cast<size_t>(android_atomic_inc(shared_class_index_));
1327
1328 const DexFile* dex_file;
1329 do {
1330 dex_file = dex_files_[cur_dex_file_index];
1331
1332 if (class_index < (class_index_base + dex_file->NumClassDefs())) {
1333 break;
1334 }
1335 class_index_base += dex_file->NumClassDefs();
1336
1337 cur_dex_file_index++;
1338 } while (cur_dex_file_index < dex_files_.size());
1339
1340 if (cur_dex_file_index >= dex_files_.size()) {
1341 return;
1342 }
1343
1344 if (dex_file != context_->GetDexFile()) {
1345 SwitchToDexFile(cur_dex_file_index);
1346 }
1347
1348 class_index -= class_index_base;
1349 class_callback_(context_, class_index);
1350 CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException());
1351 }
1352 }
1353
1354 pthread_t pthread_;
1355 bool spawn_;
1356
1357 Context* worker_context_;
1358 Callback* class_callback_;
1359 const std::vector<const DexFile*>& dex_files_;
1360
1361 Context* context_;
1362 volatile int32_t* shared_class_index_;
1363
1364 friend void ForClassesInAllDexFiles(Context*,
1365 const std::vector<const DexFile*>&,
1366 Callback, size_t);
1367};
1368
1369void ForClassesInAllDexFiles(Context* worker_context,
1370 const std::vector<const DexFile*>& dex_files,
1371 Callback class_callback, size_t thread_count) {
1372 Thread* self = Thread::Current();
1373 CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException());
1374 CHECK_GT(thread_count, 0U);
1375
1376 std::vector<DexFilesWorkerThread*> threads;
1377 volatile int32_t shared_class_index = 0;
1378
1379 for (size_t i = 0; i < thread_count; ++i) {
1380 threads.push_back(new DexFilesWorkerThread(worker_context, class_callback,
1381 dex_files, &shared_class_index,
1382 /* spawn */ (i != 0)));
1383 }
1384 threads[0]->Go();
1385
1386 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
1387 ScopedThreadStateChange tsc(self, kVmWait);
1388 STLDeleteElements(&threads);
1389}
1390
Brian Carlstromae826982011-11-09 01:33:42 -08001391void Compiler::Compile(const ClassLoader* class_loader,
1392 const std::vector<const DexFile*>& dex_files) {
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001393#if defined(ART_USE_LLVM_COMPILER)
1394 if (dex_files.size() <= 0) {
1395 return; // No dex file
1396 }
1397 Context context(NULL, class_loader, this, NULL, NULL);
1398 ForClassesInAllDexFiles(&context, dex_files, Compiler::CompileClass, thread_count_);
1399#else
Brian Carlstromae826982011-11-09 01:33:42 -08001400 for (size_t i = 0; i != dex_files.size(); ++i) {
1401 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07001402 CHECK(dex_file != NULL);
1403 CompileDexFile(class_loader, *dex_file);
1404 }
Shih-wei Liao90dc30f2012-04-23 02:03:30 -07001405#endif
Brian Carlstrom83db7722011-08-26 17:32:56 -07001406}
1407
Elliott Hughesc225caa2012-02-03 15:43:37 -08001408void Compiler::CompileClass(Context* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001409 const ClassLoader* class_loader = context->GetClassLoader();
1410 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesc225caa2012-02-03 15:43:37 -08001411 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001412 if (SkipClass(class_loader, dex_file, class_def)) {
1413 return;
1414 }
jeffhaod1224c72012-02-29 13:43:08 -08001415 ClassReference ref(&dex_file, class_def_index);
1416 // Skip compiling classes with generic verifier failures since they will still fail at runtime
Ian Rogers776ac1f2012-04-13 23:36:36 -07001417 if (verifier::MethodVerifier::IsClassRejected(ref)) {
jeffhaod1224c72012-02-29 13:43:08 -08001418 return;
1419 }
Ian Rogers0571d352011-11-03 19:51:38 -07001420 const byte* class_data = dex_file.GetClassData(class_def);
1421 if (class_data == NULL) {
1422 // empty class, probably a marker interface
1423 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001424 }
Ian Rogers0571d352011-11-03 19:51:38 -07001425 ClassDataItemIterator it(dex_file, class_data);
1426 // Skip fields
1427 while (it.HasNextStaticField()) {
1428 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001429 }
Ian Rogers0571d352011-11-03 19:51:38 -07001430 while (it.HasNextInstanceField()) {
1431 it.Next();
1432 }
1433 // Compile direct methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001434 int64_t previous_direct_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07001435 while (it.HasNextDirectMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001436 uint32_t method_idx = it.GetMemberIndex();
1437 if (method_idx == previous_direct_method_idx) {
1438 // smali can create dex files with two encoded_methods sharing the same method_idx
1439 // http://code.google.com/p/smali/issues/detail?id=119
1440 it.Next();
1441 continue;
1442 }
1443 previous_direct_method_idx = method_idx;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001444 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001445 method_idx, class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001446 it.Next();
1447 }
1448 // Compile virtual methods
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001449 int64_t previous_virtual_method_idx = -1;
Ian Rogers0571d352011-11-03 19:51:38 -07001450 while (it.HasNextVirtualMethod()) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001451 uint32_t method_idx = it.GetMemberIndex();
1452 if (method_idx == previous_virtual_method_idx) {
1453 // smali can create dex files with two encoded_methods sharing the same method_idx
1454 // http://code.google.com/p/smali/issues/detail?id=119
1455 it.Next();
1456 continue;
1457 }
1458 previous_virtual_method_idx = method_idx;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001459 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001460 method_idx, class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001461 it.Next();
1462 }
1463 DCHECK(!it.HasNext());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001464}
1465
Elliott Hughesc225caa2012-02-03 15:43:37 -08001466void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001467 Context context(NULL, class_loader, this, NULL, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001468 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001469}
1470
Elliott Hughesa0e18062012-04-13 15:59:59 -07001471static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1472 std::string key(shorty);
1473 if (is_static) {
1474 key += "$"; // Must not be a shorty type character.
1475 }
1476 return key;
1477}
1478
Ian Rogersa3760aa2011-11-14 14:32:37 -08001479void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
1480 uint32_t method_idx, const ClassLoader* class_loader,
1481 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -07001482 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001483 uint64_t start_ns = NanoTime();
Logan Chien4dd96f52012-02-29 01:26:58 +08001484
Ian Rogers169c9a72011-11-13 20:13:17 -08001485 if ((access_flags & kAccNative) != 0) {
Ian Rogers57b86d42012-03-27 16:05:41 -07001486 compiled_method = (*jni_compiler_)(*this, access_flags, method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001487 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -08001488 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001489 } else {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001490 compiled_method = (*compiler_)(*this, code_item, access_flags, method_idx, class_loader,
1491 dex_file);
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001492 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
1493 }
Ian Rogers3bb17a62012-01-27 23:56:44 -08001494 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -08001495 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001496 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -08001497 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -07001498 }
1499
1500 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -07001501 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001502 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001503 MutexLock mu(compiled_methods_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001504 compiled_methods_.Put(ref, compiled_method);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001505 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001506 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001507
Ian Rogers45619fc2012-02-29 11:15:25 -08001508 uint32_t shorty_len;
1509 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx), &shorty_len);
Ian Rogers169c9a72011-11-13 20:13:17 -08001510 bool is_static = (access_flags & kAccStatic) != 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001511 std::string key(MakeInvokeStubKey(is_static, shorty));
1512 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(key);
Ian Rogers0571d352011-11-03 19:51:38 -07001513 if (compiled_invoke_stub == NULL) {
Shih-wei Liaoc4c98812012-03-10 21:55:51 -08001514 compiled_invoke_stub = (*create_invoke_stub_)(*this, is_static, shorty, shorty_len);
Ian Rogers0571d352011-11-03 19:51:38 -07001515 CHECK(compiled_invoke_stub != NULL);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001516 InsertInvokeStub(key, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001517 }
Logan Chien7a2a23a2012-06-06 11:01:00 +08001518
1519#if defined(ART_USE_LLVM_COMPILER)
1520 if (!is_static) {
1521 const CompiledInvokeStub* compiled_proxy_stub = FindProxyStub(shorty);
1522 if (compiled_proxy_stub == NULL) {
1523 compiled_proxy_stub = (*create_proxy_stub_)(*this, shorty, shorty_len);
1524 CHECK(compiled_proxy_stub != NULL);
1525 InsertProxyStub(shorty, compiled_proxy_stub);
1526 }
1527 }
1528#endif
1529
Ian Rogers0571d352011-11-03 19:51:38 -07001530 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001531}
1532
Elliott Hughesa0e18062012-04-13 15:59:59 -07001533const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
1534 const std::string key(MakeInvokeStubKey(is_static, shorty));
1535 return FindInvokeStub(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001536}
1537
Elliott Hughesa0e18062012-04-13 15:59:59 -07001538const CompiledInvokeStub* Compiler::FindInvokeStub(const std::string& key) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001539 MutexLock mu(compiled_invoke_stubs_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001540 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001541 if (it == compiled_invoke_stubs_.end()) {
1542 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001543 } else {
1544 DCHECK(it->second != NULL);
1545 return it->second;
1546 }
1547}
1548
Elliott Hughesa0e18062012-04-13 15:59:59 -07001549void Compiler::InsertInvokeStub(const std::string& key,
Ian Rogers0571d352011-11-03 19:51:38 -07001550 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001551 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001552 InvokeStubTable::iterator it = compiled_invoke_stubs_.find(key);
1553 if (it != compiled_invoke_stubs_.end()) {
1554 // Someone else won the race.
1555 delete compiled_invoke_stub;
1556 } else {
1557 compiled_invoke_stubs_.Put(key, compiled_invoke_stub);
1558 }
Ian Rogers0571d352011-11-03 19:51:38 -07001559}
1560
Logan Chien7a2a23a2012-06-06 11:01:00 +08001561#if defined(ART_USE_LLVM_COMPILER)
1562const CompiledInvokeStub* Compiler::FindProxyStub(const char* shorty) const {
1563 MutexLock mu(compiled_proxy_stubs_lock_);
1564 ProxyStubTable::const_iterator it = compiled_proxy_stubs_.find(shorty);
1565 if (it == compiled_proxy_stubs_.end()) {
1566 return NULL;
1567 } else {
1568 DCHECK(it->second != NULL);
1569 return it->second;
1570 }
1571}
1572
1573void Compiler::InsertProxyStub(const char* shorty,
1574 const CompiledInvokeStub* compiled_proxy_stub) {
1575 MutexLock mu(compiled_proxy_stubs_lock_);
1576 InvokeStubTable::iterator it = compiled_proxy_stubs_.find(shorty);
1577 if (it != compiled_proxy_stubs_.end()) {
1578 // Someone else won the race.
1579 delete compiled_proxy_stub;
1580 } else {
1581 compiled_proxy_stubs_.Put(shorty, compiled_proxy_stub);
1582 }
1583}
1584#endif
1585
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001586CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001587 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001588 ClassTable::const_iterator it = compiled_classes_.find(ref);
1589 if (it == compiled_classes_.end()) {
1590 return NULL;
1591 }
1592 CHECK(it->second != NULL);
1593 return it->second;
1594}
1595
Ian Rogers0571d352011-11-03 19:51:38 -07001596CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001597 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001598 MethodTable::const_iterator it = compiled_methods_.find(ref);
1599 if (it == compiled_methods_.end()) {
1600 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001601 }
1602 CHECK(it->second != NULL);
1603 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001604}
1605
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001606void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
1607 for (size_t i = 0; i != dex_files.size(); ++i) {
1608 const DexFile* dex_file = dex_files[i];
1609 CHECK(dex_file != NULL);
1610 SetGcMapsDexFile(class_loader, *dex_file);
1611 }
1612}
1613
1614void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
1615 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1616 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1617 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1618 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1619 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1620 Class* klass = class_linker->FindClass(descriptor, class_loader);
1621 if (klass == NULL || !klass->IsVerified()) {
1622 Thread::Current()->ClearException();
1623 continue;
1624 }
1625 const byte* class_data = dex_file.GetClassData(class_def);
1626 if (class_data == NULL) {
1627 // empty class such as a marker interface
1628 continue;
1629 }
1630 ClassDataItemIterator it(dex_file, class_data);
1631 while (it.HasNextStaticField()) {
1632 it.Next();
1633 }
1634 while (it.HasNextInstanceField()) {
1635 it.Next();
1636 }
1637 while (it.HasNextDirectMethod()) {
1638 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1639 class_loader, true);
1640 SetGcMapsMethod(dex_file, method);
1641 it.Next();
1642 }
1643 while (it.HasNextVirtualMethod()) {
1644 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1645 class_loader, false);
1646 SetGcMapsMethod(dex_file, method);
1647 it.Next();
1648 }
1649 }
1650}
1651
1652void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
1653 if (method == NULL) {
1654 Thread::Current()->ClearException();
1655 return;
1656 }
1657 uint16_t method_idx = method->GetDexMethodIndex();
1658 MethodReference ref(&dex_file, method_idx);
1659 CompiledMethod* compiled_method = GetCompiledMethod(ref);
1660 if (compiled_method == NULL) {
1661 return;
1662 }
Ian Rogers776ac1f2012-04-13 23:36:36 -07001663 const std::vector<uint8_t>* gc_map = verifier::MethodVerifier::GetGcMap(ref);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001664 if (gc_map == NULL) {
1665 return;
1666 }
1667 compiled_method->SetGcMap(*gc_map);
1668}
1669
buzbee2cfc6392012-05-07 14:51:40 -07001670#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_QUICK_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +08001671void Compiler::SetBitcodeFileName(std::string const& filename) {
Logan Chien106b2a02012-03-18 04:41:38 +08001672 typedef void (*SetBitcodeFileNameFn)(Compiler&, std::string const&);
1673
1674 SetBitcodeFileNameFn set_bitcode_file_name =
1675 FindFunction<SetBitcodeFileNameFn>(MakeCompilerSoName(instruction_set_),
1676 compiler_library_,
1677 "compilerLLVMSetBitcodeFileName");
1678
1679 set_bitcode_file_name(*this, filename);
Logan Chien8b977d32012-02-21 19:14:55 +08001680}
buzbee2cfc6392012-05-07 14:51:40 -07001681#endif
Logan Chienf7015fd2012-03-18 01:19:37 +08001682
buzbee2cfc6392012-05-07 14:51:40 -07001683#if defined(ART_USE_LLVM_COMPILER)
Logan Chienf7015fd2012-03-18 01:19:37 +08001684void Compiler::EnableAutoElfLoading() {
1685 compiler_enable_auto_elf_loading_(*this);
1686}
1687
1688const void* Compiler::GetMethodCodeAddr(const CompiledMethod* cm,
1689 const Method* method) const {
1690 return compiler_get_method_code_addr_(*this, cm, method);
1691}
1692
1693const Method::InvokeStub* Compiler::GetMethodInvokeStubAddr(const CompiledInvokeStub* cm,
1694 const Method* method) const {
1695 return compiler_get_method_invoke_stub_addr_(*this, cm, method);
1696}
Logan Chiendf576142012-03-20 17:36:32 +08001697
1698std::vector<ElfImage> Compiler::GetElfImages() const {
1699 typedef std::vector<ElfImage> (*GetElfImagesFn)(const Compiler&);
1700
1701 GetElfImagesFn get_elf_images =
1702 FindFunction<GetElfImagesFn>(MakeCompilerSoName(instruction_set_),
1703 compiler_library_,
1704 "compilerLLVMGetElfImages");
1705
1706 return get_elf_images(*this);
1707}
Logan Chien8b977d32012-02-21 19:14:55 +08001708#endif
1709
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001710} // namespace art