blob: 3f7f637c2b770b23880662a4af5245c1c6b5a7c6 [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>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070022#include <sys/mman.h>
Elliott Hughesd9c67be2012-02-02 19:54:06 -080023#include <unistd.h>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070024
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070025#include "assembler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070028#include "dex_cache.h"
jeffhaof56197c2012-03-05 18:01:54 -080029#include "dex_verifier.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070030#include "jni_internal.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080031#include "oat_compilation_unit.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070032#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "runtime.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080035#include "space.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070036#include "stl_util.h"
Elliott Hughes601a1232012-02-02 17:47:38 -080037#include "timing_logger.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070038
Elliott Hughes059d5c12012-03-12 17:39:18 -070039#if defined(__APPLE__)
40#include <mach-o/dyld.h>
41#endif
42
Shih-wei Liaod1fec812012-02-13 09:51:10 -080043#if defined(ART_USE_LLVM_COMPILER)
44#include "compiler_llvm/compiler_llvm.h"
45#endif
46
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070047namespace art {
48
Shih-wei Liaoc486c112011-09-13 16:43:52 -070049namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070050 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070051 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080052 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070053}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070054namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070055 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070056 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080057 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070058}
59
Ian Rogers996cc582012-02-14 22:23:29 -080060static double Percentage(size_t x, size_t y) {
61 return 100.0 * ((double)x) / ((double)(x + y));
62}
63
64static void DumpStat(size_t x, size_t y, const char* str) {
65 if (x == 0 && y == 0) {
66 return;
67 }
68 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
69}
70
Ian Rogersc8b306f2012-02-17 21:34:44 -080071class AOTCompilationStats {
72 public:
73 AOTCompilationStats() : stats_lock_("AOT compilation statistics lock"),
74 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
75 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
76 resolved_types_(0), unresolved_types_(0),
77 resolved_instance_fields_(0), unresolved_instance_fields_(0),
Ian Rogersfb6adba2012-03-04 21:51:51 -080078 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
79 virtual_made_direct_(0) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080080 for (size_t i = 0; i < kMaxInvokeType; i++) {
81 resolved_methods_[i] = 0;
82 unresolved_methods_[i] = 0;
83 }
84 }
85
86 void Dump() {
87 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
88 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
89 DumpStat(resolved_types_, unresolved_types_, "types resolved");
90 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
91 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
92 "static fields resolved");
93 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
94 "static fields local to a class");
95
96 for (size_t i = 0; i < kMaxInvokeType; i++) {
97 std::ostringstream oss;
98 oss << "resolved " << static_cast<InvokeType>(i) << " methods";
99 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
100 }
Ian Rogersfb6adba2012-03-04 21:51:51 -0800101 DumpStat(virtual_made_direct_, resolved_methods_[kVirtual] + unresolved_methods_[kVirtual],
102 "made direct from virtual");
Ian Rogersc8b306f2012-02-17 21:34:44 -0800103 }
Ian Rogers996cc582012-02-14 22:23:29 -0800104
105// Allow lossy statistics in non-debug builds
106#ifndef NDEBUG
107#define STATS_LOCK() MutexLock mu(stats_lock_)
108#else
109#define STATS_LOCK()
110#endif
111
Ian Rogersc8b306f2012-02-17 21:34:44 -0800112 void TypeInDexCache() {
113 STATS_LOCK();
114 types_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800115 }
Ian Rogers996cc582012-02-14 22:23:29 -0800116
Ian Rogersc8b306f2012-02-17 21:34:44 -0800117 void TypeNotInDexCache() {
118 STATS_LOCK();
119 types_not_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800120 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800121
122 void StringInDexCache() {
123 STATS_LOCK();
124 strings_in_dex_cache_++;
125 }
126
127 void StringNotInDexCache() {
128 STATS_LOCK();
129 strings_not_in_dex_cache_++;
130 }
131
132 void TypeDoesntNeedAccessCheck() {
133 STATS_LOCK();
134 resolved_types_++;
135 }
136
137 void TypeNeedsAccessCheck() {
138 STATS_LOCK();
139 unresolved_types_++;
140 }
141
142 void ResolvedInstanceField() {
143 STATS_LOCK();
144 resolved_instance_fields_++;
145 }
146
147 void UnresolvedInstanceField(){
148 STATS_LOCK();
149 unresolved_instance_fields_++;
150 }
151
152 void ResolvedLocalStaticField() {
153 STATS_LOCK();
154 resolved_local_static_fields_++;
155 }
156
157 void ResolvedStaticField() {
158 STATS_LOCK();
159 resolved_static_fields_++;
160 }
161
162 void UnresolvedStaticField() {
163 STATS_LOCK();
164 unresolved_static_fields_++;
165 }
166
167 void ResolvedMethod(InvokeType type) {
168 DCHECK_LE(type, kMaxInvokeType);
169 STATS_LOCK();
170 resolved_methods_[type]++;
171 }
172
173 void UnresolvedMethod(InvokeType type) {
174 DCHECK_LE(type, kMaxInvokeType);
175 STATS_LOCK();
176 unresolved_methods_[type]++;
177 }
178
Ian Rogersfb6adba2012-03-04 21:51:51 -0800179 void VirtualMadeDirect() {
180 STATS_LOCK();
181 virtual_made_direct_++;
182 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800183 private:
184 Mutex stats_lock_;
185
186 size_t types_in_dex_cache_;
187 size_t types_not_in_dex_cache_;
188
189 size_t strings_in_dex_cache_;
190 size_t strings_not_in_dex_cache_;
191
192 size_t resolved_types_;
193 size_t unresolved_types_;
194
195 size_t resolved_instance_fields_;
196 size_t unresolved_instance_fields_;
197
198 size_t resolved_local_static_fields_;
199 size_t resolved_static_fields_;
200 size_t unresolved_static_fields_;
201
202 size_t resolved_methods_[kMaxInvokeType + 1];
203 size_t unresolved_methods_[kMaxInvokeType + 1];
Ian Rogersfb6adba2012-03-04 21:51:51 -0800204 size_t virtual_made_direct_;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800205
206 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);;
207};
Ian Rogers996cc582012-02-14 22:23:29 -0800208
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800209static std::string MakeCompilerSoName(InstructionSet instruction_set) {
210 // TODO: is the ARM/Thumb2 instruction set distinction really buying us anything,
211 // or just causing hassle like this?
212 if (instruction_set == kThumb2) {
213 instruction_set = kArm;
214 }
215
Elliott Hughes059d5c12012-03-12 17:39:18 -0700216 // Capitalize the instruction set, because that's what we do in the build system.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800217 std::ostringstream instruction_set_name_os;
218 instruction_set_name_os << instruction_set;
219 std::string instruction_set_name(instruction_set_name_os.str());
220 for (size_t i = 0; i < instruction_set_name.size(); ++i) {
221 instruction_set_name[i] = toupper(instruction_set_name[i]);
222 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700223
224 // Bad things happen if we pull in the libartd-compiler to a libart dex2oat or vice versa,
225 // because we end up with both libart and libartd in the same address space!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800226#ifndef NDEBUG
227 const char* suffix = "d";
228#else
229 const char* suffix = "";
230#endif
Elliott Hughes059d5c12012-03-12 17:39:18 -0700231
232 // Work out the filename for the compiler library.
233 std::string library_name(StringPrintf("art%s-compiler-%s", suffix, instruction_set_name.c_str()));
234 std::string filename(StringPrintf(OS_SHARED_LIB_FORMAT_STR, library_name.c_str()));
235
236#if defined(__APPLE__)
237 // On Linux, dex2oat will have been built with an RPATH of $ORIGIN/../lib, so dlopen(3) will find
238 // the .so by itself. On Mac OS, there isn't really an equivalent, so we have to manually do the
239 // same work.
240 std::vector<char> executable_path(1);
241 uint32_t executable_path_length = 0;
242 _NSGetExecutablePath(&executable_path[0], &executable_path_length);
243 while (_NSGetExecutablePath(&executable_path[0], &executable_path_length) == -1) {
244 executable_path.resize(executable_path_length);
245 }
246
247 executable_path.resize(executable_path.size() - 1); // Strip trailing NUL.
248 std::string path(&executable_path[0]);
249
250 // Strip the "/dex2oat".
251 size_t last_slash = path.find_last_of('/');
252 CHECK_NE(last_slash, std::string::npos) << path;
253 path.resize(last_slash);
254
255 // Strip the "/bin".
256 last_slash = path.find_last_of('/');
257 path.resize(last_slash);
258
259 filename = path + "/lib/" + filename;
260#endif
261 return filename;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800262}
263
Elliott Hughes46f060a2012-03-09 17:36:50 -0800264template<typename Fn>
265static Fn FindFunction(const std::string& compiler_so_name, void* library, const char* name) {
266 Fn fn = reinterpret_cast<Fn>(dlsym(library, name));
267 if (fn == NULL) {
268 LOG(FATAL) << "Couldn't find \"" << name << "\" in compiler library " << compiler_so_name << ": " << dlerror();
269 }
Elliott Hughes059d5c12012-03-12 17:39:18 -0700270 VLOG(compiler) << "Found \"" << name << "\" at " << reinterpret_cast<void*>(fn);
Elliott Hughes46f060a2012-03-09 17:36:50 -0800271 return fn;
272}
273
Elliott Hughes5523ee02012-02-03 18:18:34 -0800274Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count,
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800275 bool support_debugging, const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -0700276 : instruction_set_(instruction_set),
Elliott Hughesc225caa2012-02-03 15:43:37 -0800277 compiled_classes_lock_("compiled classes lock"),
278 compiled_methods_lock_("compiled method lock"),
279 compiled_invoke_stubs_lock_("compiled invoke stubs lock"),
Brian Carlstromaded5f72011-10-07 17:15:04 -0700280 image_(image),
Elliott Hughes5523ee02012-02-03 18:18:34 -0800281 thread_count_(thread_count),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800282 support_debugging_(support_debugging),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800283 stats_(new AOTCompilationStats),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800284 image_classes_(image_classes),
285#if !defined(ART_USE_LLVM_COMPILER)
286 compiler_library_(NULL),
Elliott Hughes46f060a2012-03-09 17:36:50 -0800287 compiler_(NULL),
288 jni_compiler_(NULL),
289 create_invoke_stub_(NULL)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800290#else
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800291 compiler_llvm_(new compiler_llvm::CompilerLLVM(this, instruction_set))
292#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800293{
294 std::string compiler_so_name(MakeCompilerSoName(instruction_set));
295 compiler_library_ = dlopen(compiler_so_name.c_str(), RTLD_LAZY);
296 if (compiler_library_ == NULL) {
297 LOG(FATAL) << "Couldn't find compiler library " << compiler_so_name << ": " << dlerror();
298 }
299 VLOG(compiler) << "dlopen(\"" << compiler_so_name << "\", RTLD_LAZY) returned " << compiler_library_;
300
Elliott Hughes46f060a2012-03-09 17:36:50 -0800301 compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "oatCompileMethod");
302 jni_compiler_ = FindFunction<JniCompilerFn>(compiler_so_name, compiler_library_, "ArtJniCompileMethod");
303 create_invoke_stub_ = FindFunction<CreateInvokeStubFn>(compiler_so_name, compiler_library_, "ArtCreateInvokeStub");
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800304
Brian Carlstrom25c33252011-09-18 15:58:35 -0700305 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800306 if (!image_) {
307 CHECK(image_classes_ == NULL);
308 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700309}
310
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700311Compiler::~Compiler() {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800312 {
313 MutexLock mu(compiled_classes_lock_);
314 STLDeleteValues(&compiled_classes_);
315 }
316 {
317 MutexLock mu(compiled_methods_lock_);
318 STLDeleteValues(&compiled_methods_);
319 }
320 {
321 MutexLock mu(compiled_invoke_stubs_lock_);
322 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800323 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800324 if (compiler_library_ != NULL) {
325 VLOG(compiler) << "dlclose(" << compiler_library_ << ")";
326 dlclose(compiler_library_);
327 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700328}
329
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700330ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
331 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700332 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700333 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700334 } else {
335 CHECK(instruction_set == kArm || instruction_set == kThumb2);
336 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700337 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700338 }
339}
340
Elliott Hughes8add92d2012-01-18 18:18:43 -0800341ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800342 switch (instruction_set) {
343 case kArm:
344 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800345 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800346 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800347 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800348 default:
Ian Rogers49c48942012-03-11 15:15:37 -0700349 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Ian Rogers169c9a72011-11-13 20:13:17 -0800350 return NULL;
351 }
352}
353
Ian Rogersad25ac52011-10-04 19:13:33 -0700354ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
355 if (instruction_set == kX86) {
356 return x86::CreateAbstractMethodErrorStub();
357 } else {
358 CHECK(instruction_set == kArm || instruction_set == kThumb2);
359 // Generates resolution stub using ARM instruction set
360 return arm::CreateAbstractMethodErrorStub();
361 }
362}
363
Jesse Wilson254db0f2011-11-16 16:44:11 -0500364void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800365 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700366 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800367
Elliott Hughes601a1232012-02-02 17:47:38 -0800368 TimingLogger timings("compiler");
369
370 PreCompile(class_loader, dex_files, timings);
371
Brian Carlstromae826982011-11-09 01:33:42 -0800372 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800373 timings.AddSplit("Compile");
374
Brian Carlstromae826982011-11-09 01:33:42 -0800375 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800376 timings.AddSplit("PostCompile");
377
378 if (timings.GetTotalNs() > MsToNs(1000)) {
379 timings.Dump();
380 }
Ian Rogers996cc582012-02-14 22:23:29 -0800381
Ian Rogersc8b306f2012-02-17 21:34:44 -0800382 stats_->Dump();
Brian Carlstrom8a487412011-08-29 20:08:52 -0700383}
384
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700385void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700386 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800387
Brian Carlstrom8a487412011-08-29 20:08:52 -0700388 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800389
Ian Rogers0571d352011-11-03 19:51:38 -0700390 // Find the dex_file
391 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
392 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800393 std::vector<const DexFile*> dex_files;
394 dex_files.push_back(&dex_file);
395
Elliott Hughes601a1232012-02-02 17:47:38 -0800396 TimingLogger timings("CompileOne");
397 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800398
Ian Rogers0571d352011-11-03 19:51:38 -0700399 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800400 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
401 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800402
403 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700404}
405
Brian Carlstromae826982011-11-09 01:33:42 -0800406void Compiler::Resolve(const ClassLoader* class_loader,
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800407 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800408 for (size_t i = 0; i != dex_files.size(); ++i) {
409 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700410 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800411 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700412 }
413}
414
Brian Carlstromae826982011-11-09 01:33:42 -0800415void Compiler::PreCompile(const ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800416 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800417 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800418
Brian Carlstromae826982011-11-09 01:33:42 -0800419 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800420 timings.AddSplit("PreCompile.Verify");
421
Brian Carlstromae826982011-11-09 01:33:42 -0800422 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800423 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800424}
425
426void Compiler::PostCompile(const ClassLoader* class_loader,
427 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800428 SetGcMaps(class_loader, dex_files);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800429#if defined(ART_USE_LLVM_COMPILER)
Logan Chien7f767612012-03-01 18:54:49 +0800430 compiler_llvm_->MaterializeRemainder();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800431#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800432}
433
434bool Compiler::IsImageClass(const std::string& descriptor) const {
435 if (image_classes_ == NULL) {
436 return true;
437 }
438 return image_classes_->find(descriptor) != image_classes_->end();
439}
440
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800441bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800442 uint32_t type_idx) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800443 if (!IsImage()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800444 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800445 return false;
446 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800447 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800448 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800449 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800450 return false;
451 }
Ian Rogers996cc582012-02-14 22:23:29 -0800452 bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor());
453 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800454 stats_->TypeInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800455 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800456 stats_->TypeNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800457 }
458 return result;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800459}
460
Ian Rogers1bddec32012-02-04 12:27:34 -0800461bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800462 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800463 // TODO: Add support for loading strings referenced by image_classes_
464 // See also Compiler::ResolveDexFile
465
466 // The following is a test saying that if we're building the image without a restricted set of
467 // 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 -0800468 bool result = IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
469 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800470 stats_->StringInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800471 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800472 stats_->StringNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800473 }
474 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800475}
476
477bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800478 const DexFile& dex_file, uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800479 // Get type from dex cache assuming it was populated by the verifier
480 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
481 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800482 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800483 return false; // Unknown class needs access checks.
484 }
485 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
486 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
487 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800488 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800489 return false; // Incomplete referrer knowledge needs access check.
490 }
491 // Perform access check, will return true if access is ok or false if we're going to have to
492 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800493 bool result = referrer_class->CanAccess(resolved_class);
494 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800495 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800496 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800497 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800498 }
499 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800500}
501
502bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
503 const DexCache* dex_cache,
504 const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800505 uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800506 // Get type from dex cache assuming it was populated by the verifier.
507 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
508 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800509 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800510 return false; // Unknown class needs access checks.
511 }
512 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
513 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
514 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800515 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800516 return false; // Incomplete referrer knowledge needs access check.
517 }
518 // Perform access and instantiable checks, will return true if access is ok or false if we're
519 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800520 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
521 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800522 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800523 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800524 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800525 }
526 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800527}
528
Logan Chien4dd96f52012-02-29 01:26:58 +0800529static Class* ComputeReferrerClass(OatCompilationUnit* mUnit) {
530 const DexFile::MethodId& referrer_method_id =
531 mUnit->dex_file_->GetMethodId(mUnit->method_idx_);
532
533 return mUnit->class_linker_->ResolveType(
534 *mUnit->dex_file_, referrer_method_id.class_idx_,
535 mUnit->dex_cache_, mUnit->class_loader_);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800536}
537
Logan Chien4dd96f52012-02-29 01:26:58 +0800538static Field* ComputeReferrerField(OatCompilationUnit* mUnit, uint32_t field_idx) {
539 return mUnit->class_linker_->ResolveField(
540 *mUnit->dex_file_, field_idx, mUnit->dex_cache_,
541 mUnit->class_loader_, false);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800542}
543
Logan Chien4dd96f52012-02-29 01:26:58 +0800544static Method* ComputeReferrerMethod(OatCompilationUnit* mUnit, uint32_t method_idx) {
545 return mUnit->class_linker_->ResolveMethod(
546 *mUnit->dex_file_, method_idx, mUnit->dex_cache_,
547 mUnit->class_loader_, true);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800548}
549
Logan Chien4dd96f52012-02-29 01:26:58 +0800550bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
jeffhao8cd6dda2012-02-22 10:15:34 -0800551 int& field_offset, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800552 // Conservative defaults
553 field_offset = -1;
554 is_volatile = true;
555 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800556 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800557 if (resolved_field != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800558 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800559 // Try to resolve referring class then access check, failure to pass the
560 Class* fields_class = resolved_field->GetDeclaringClass();
jeffhao8cd6dda2012-02-22 10:15:34 -0800561 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
562 fields_class != referrer_class;
563 if (referrer_class != NULL && referrer_class->CanAccess(fields_class) &&
564 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
565 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800566 field_offset = resolved_field->GetOffset().Int32Value();
567 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800568 stats_->ResolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800569 return true; // Fast path.
570 }
571 }
572 // Clean up any exception left by field/type resolution
573 Thread* thread = Thread::Current();
574 if (thread->IsExceptionPending()) {
575 thread->ClearException();
576 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800577 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800578 return false; // Incomplete knowledge needs slow path.
579}
580
Logan Chien4dd96f52012-02-29 01:26:58 +0800581bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
Ian Rogers1bddec32012-02-04 12:27:34 -0800582 int& field_offset, int& ssb_index,
jeffhao8cd6dda2012-02-22 10:15:34 -0800583 bool& is_referrers_class, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800584 // Conservative defaults
585 field_offset = -1;
586 ssb_index = -1;
587 is_referrers_class = false;
588 is_volatile = true;
589 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800590 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800591 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800592 DCHECK(resolved_field->IsStatic());
Logan Chien4dd96f52012-02-29 01:26:58 +0800593 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800594 if (referrer_class != NULL) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800595 Class* fields_class = resolved_field->GetDeclaringClass();
596 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800597 is_referrers_class = true; // implies no worrying about class initialization
598 field_offset = resolved_field->GetOffset().Int32Value();
599 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800600 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800601 return true; // fast path
602 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -0800603 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogers1bddec32012-02-04 12:27:34 -0800604 if (referrer_class->CanAccess(fields_class) &&
jeffhao8cd6dda2012-02-22 10:15:34 -0800605 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
606 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800607 // We have the resolved field, we must make it into a ssbIndex for the referrer
608 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800609 // TODO: for images we can elide the static storage base null check
610 // if we know there's a non-null entry in the image
Logan Chien4dd96f52012-02-29 01:26:58 +0800611 if (fields_class->GetDexCache() == mUnit->dex_cache_) {
Ian Rogers4103ad22012-02-06 09:18:25 -0800612 // common case where the dex cache of both the referrer and the field are the same,
613 // no need to search the dex file
614 ssb_index = fields_class->GetDexTypeIndex();
615 field_offset = resolved_field->GetOffset().Int32Value();
616 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800617 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800618 return true;
619 }
620 // Search dex file for localized ssb index
Ian Rogers1bddec32012-02-04 12:27:34 -0800621 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
622 const DexFile::StringId* string_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800623 mUnit->dex_file_->FindStringId(descriptor);
Ian Rogers1bddec32012-02-04 12:27:34 -0800624 if (string_id != NULL) {
625 const DexFile::TypeId* type_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800626 mUnit->dex_file_->FindTypeId(mUnit->dex_file_->GetIndexForStringId(*string_id));
Ian Rogers1bddec32012-02-04 12:27:34 -0800627 if(type_id != NULL) {
628 // medium path, needs check of static storage base being initialized
Logan Chien4dd96f52012-02-29 01:26:58 +0800629 ssb_index = mUnit->dex_file_->GetIndexForTypeId(*type_id);
Ian Rogers1bddec32012-02-04 12:27:34 -0800630 field_offset = resolved_field->GetOffset().Int32Value();
631 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800632 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800633 return true;
634 }
635 }
636 }
637 }
638 }
639 }
640 // Clean up any exception left by field/type resolution
641 Thread* thread = Thread::Current();
642 if (thread->IsExceptionPending()) {
643 thread->ClearException();
644 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800645 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800646 return false; // Incomplete knowledge needs slow path.
647}
648
Ian Rogersfb6adba2012-03-04 21:51:51 -0800649bool Compiler::ComputeInvokeInfo(uint32_t method_idx, OatCompilationUnit* mUnit, InvokeType& type,
Ian Rogers996cc582012-02-14 22:23:29 -0800650 int& vtable_idx) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800651 vtable_idx = -1;
Logan Chien4dd96f52012-02-29 01:26:58 +0800652 Method* resolved_method = ComputeReferrerMethod(mUnit, method_idx);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800653 if (resolved_method != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800654 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800655 if (referrer_class != NULL) {
656 Class* methods_class = resolved_method->GetDeclaringClass();
657 if (!referrer_class->CanAccess(methods_class) ||
658 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800659 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800660 // The referring class can't access the resolved method, this may occur as a result of a
661 // protected method being made public by implementing an interface that re-declares the
662 // method public. Resort to the dex file to determine the correct class for the access check
Logan Chien4dd96f52012-02-29 01:26:58 +0800663 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800664 methods_class =
Logan Chien4dd96f52012-02-29 01:26:58 +0800665 mUnit->class_linker_->ResolveType(dex_file,
666 dex_file.GetMethodId(method_idx).class_idx_,
667 referrer_class);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800668
669 }
670 if (referrer_class->CanAccess(methods_class) &&
671 referrer_class->CanAccessMember(methods_class,
672 resolved_method->GetAccessFlags())) {
673 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogers9a8a8882012-03-08 02:30:55 -0800674 const bool kEnableSharpening = false;
675 if (kEnableSharpening && type == kVirtual &&
676 (resolved_method->IsFinal() || methods_class->IsFinal())) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800677 stats_->ResolvedMethod(kVirtual);
678 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
679 // dex cache, check that this resolved method is where we expect it.
680 CHECK(referrer_class->GetDexCache()->GetResolvedMethod(method_idx) == resolved_method)
681 << PrettyMethod(resolved_method);
682 type = kDirect;
683 stats_->VirtualMadeDirect();
684 return true;
685 } else if (type != kSuper) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800686 // nothing left to do for static/direct/virtual/interface dispatch
687 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800688 return true;
689 } else {
690 // ensure the vtable index will be correct to dispatch in the vtable of the super class
Ian Rogers996cc582012-02-14 22:23:29 -0800691 if (referrer_class->IsSubClass(methods_class) &&
692 vtable_idx < methods_class->GetVTable()->GetLength()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800693 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800694 return true;
695 }
696 }
697 }
698 }
699 }
700 // Clean up any exception left by method/type resolution
701 Thread* thread = Thread::Current();
702 if (thread->IsExceptionPending()) {
703 thread->ClearException();
704 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800705 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800706 return false; // Incomplete knowledge needs slow path.
707}
708
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800709// Return true if the class should be skipped during compilation. We
710// never skip classes in the boot class loader. However, if we have a
711// non-boot class loader and we can resolve the class in the boot
712// class loader, we do skip the class. This happens if an app bundles
713// classes found in the boot classpath. Since at runtime we will
714// select the class from the boot classpath, do not attempt to resolve
715// or compile it now.
716static bool SkipClass(const ClassLoader* class_loader,
717 const DexFile& dex_file,
718 const DexFile::ClassDef& class_def) {
719 if (class_loader == NULL) {
720 return false;
721 }
722 const char* descriptor = dex_file.GetClassDescriptor(class_def);
723 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
724 Class* klass = class_linker->FindClass(descriptor, NULL);
725 if (klass == NULL) {
726 Thread* self = Thread::Current();
727 CHECK(self->IsExceptionPending());
728 self->ClearException();
729 return false;
730 }
731 return true;
732}
733
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800734class Context {
735 public:
736 Context(ClassLinker* class_linker,
737 const ClassLoader* class_loader,
738 Compiler* compiler,
739 DexCache* dex_cache,
740 const DexFile* dex_file)
741 : class_linker_(class_linker),
742 class_loader_(class_loader),
743 compiler_(compiler),
744 dex_cache_(dex_cache),
745 dex_file_(dex_file) {}
746
747 ClassLinker* GetClassLinker() {
748 CHECK(class_linker_ != NULL);
749 return class_linker_;
750 }
751 const ClassLoader* GetClassLoader() {
752 return class_loader_;
753 }
754 Compiler* GetCompiler() {
755 CHECK(compiler_ != NULL);
756 return compiler_;
757 }
758 DexCache* GetDexCache() {
759 CHECK(dex_cache_ != NULL);
760 return dex_cache_;
761 }
762 const DexFile* GetDexFile() {
763 CHECK(dex_file_ != NULL);
764 return dex_file_;
765 }
766
767 private:
768 ClassLinker* class_linker_;
769 const ClassLoader* class_loader_;
770 Compiler* compiler_;
771 DexCache* dex_cache_;
772 const DexFile* dex_file_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800773};
774
775typedef void Callback(Context* context, size_t index);
776
777class WorkerThread {
778 public:
Elliott Hughes1e409252012-02-06 11:21:27 -0800779 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
780 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
781 if (spawn_) {
782 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread");
783 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800784 }
785
786 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800787 if (spawn_) {
788 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
789 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800790 }
791
792 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800793 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800794 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
795 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800796 if (worker->spawn_) {
797 runtime->AttachCurrentThread("Compiler Worker", true);
798 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800799 Thread::Current()->SetState(Thread::kRunnable);
800 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800801 if (worker->spawn_) {
802 Thread::Current()->SetState(Thread::kNative);
803 runtime->DetachCurrentThread();
804 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800805 return NULL;
806 }
807
Elliott Hughes1e409252012-02-06 11:21:27 -0800808 void Go() {
809 Go(this);
810 }
811
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800812 void Run() {
813 for (size_t i = begin_; i < end_; i += stripe_) {
814 callback_(context_, i);
815 }
816 }
817
818 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800819 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800820
821 Context* context_;
822 size_t begin_;
823 size_t end_;
824 Callback* callback_;
825 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800826
827 friend void ForAll(Context*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800828};
829
Elliott Hughes5523ee02012-02-03 18:18:34 -0800830void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800831 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -0800832
Elliott Hughes1e409252012-02-06 11:21:27 -0800833 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -0800834 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800835 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800836 }
Elliott Hughes1e409252012-02-06 11:21:27 -0800837 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800838
Elliott Hughes81d91512012-02-03 16:38:43 -0800839 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
840 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
841 STLDeleteElements(&threads);
842}
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800843
844static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800845 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800846
847 // Method and Field are the worst. We can't resolve without either
848 // context from the code use (to disambiguate virtual vs direct
849 // method and instance vs static field) or from class
850 // definitions. While the compiler will resolve what it can as it
851 // needs it, here we try to resolve fields and methods used in class
852 // definitions, since many of them many never be referenced by
853 // generated code.
854 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800855 if (SkipClass(context->GetClassLoader(), dex_file, class_def)) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800856 return;
857 }
858
859 // Note the class_data pointer advances through the headers,
860 // static fields, instance fields, direct methods, and virtual
861 // methods.
862 const byte* class_data = dex_file.GetClassData(class_def);
863 if (class_data == NULL) {
864 // empty class such as a marker interface
865 return;
866 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800867 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800868 ClassLinker* class_linker = context->GetClassLinker();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800869 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
870 ClassDataItemIterator it(dex_file, class_data);
871 while (it.HasNextStaticField()) {
872 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800873 context->GetClassLoader(), true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800874 if (field == NULL) {
875 CHECK(self->IsExceptionPending());
876 self->ClearException();
877 }
878 it.Next();
879 }
880 while (it.HasNextInstanceField()) {
881 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800882 context->GetClassLoader(), false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800883 if (field == NULL) {
884 CHECK(self->IsExceptionPending());
885 self->ClearException();
886 }
887 it.Next();
888 }
889 while (it.HasNextDirectMethod()) {
890 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800891 context->GetClassLoader(), true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800892 if (method == NULL) {
893 CHECK(self->IsExceptionPending());
894 self->ClearException();
895 }
896 it.Next();
897 }
898 while (it.HasNextVirtualMethod()) {
899 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800900 context->GetClassLoader(), false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800901 if (method == NULL) {
902 CHECK(self->IsExceptionPending());
903 self->ClearException();
904 }
905 it.Next();
906 }
907 DCHECK(!it.HasNext());
908}
909
910static void ResolveType(Context* context, size_t type_idx) {
911 // Class derived values are more complicated, they require the linker and loader.
912 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800913 Class* klass = context->GetClassLinker()->ResolveType(*context->GetDexFile(),
914 type_idx,
915 context->GetDexCache(),
916 context->GetClassLoader());
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800917 if (klass == NULL) {
918 CHECK(self->IsExceptionPending());
919 Thread::Current()->ClearException();
920 }
921}
922
923void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700924 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700925 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700926
Brian Carlstromae826982011-11-09 01:33:42 -0800927 // Strings are easy in that they always are simply resolved to literals in the same file
928 if (image_ && image_classes_ == NULL) {
929 // TODO: Add support for loading strings referenced by image_classes_
930 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700931 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
932 class_linker->ResolveString(dex_file, string_idx, dex_cache);
933 }
Elliott Hughesff738062012-02-03 15:00:42 -0800934 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700935 }
936
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800937 Context context(class_linker, class_loader, this, dex_cache, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800938 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800939 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700940
Elliott Hughes5523ee02012-02-03 18:18:34 -0800941 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800942 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700943}
944
Brian Carlstromae826982011-11-09 01:33:42 -0800945void Compiler::Verify(const ClassLoader* class_loader,
946 const std::vector<const DexFile*>& dex_files) {
947 for (size_t i = 0; i != dex_files.size(); ++i) {
948 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700949 CHECK(dex_file != NULL);
950 VerifyDexFile(class_loader, *dex_file);
951 }
952}
953
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800954static void VerifyClass(Context* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800955 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
956 const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def);
957 Class* klass = context->GetClassLinker()->FindClass(descriptor, context->GetClassLoader());
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800958 if (klass == NULL) {
959 Thread* self = Thread::Current();
960 CHECK(self->IsExceptionPending());
961 self->ClearException();
jeffhaof56197c2012-03-05 18:01:54 -0800962
963 /*
964 * At compile time, we can still structurally verify the class even if FindClass fails.
965 * This is to ensure the class is structurally sound for compilation. An unsound class
966 * will be rejected by the verifier and later skipped during compilation in the compiler.
967 */
968 std::string error_msg;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800969 if (!verifier::DexVerifier::VerifyClass(context->GetDexFile(), context->GetDexCache(),
970 context->GetClassLoader(), class_def_index, error_msg)) {
971 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
jeffhaof56197c2012-03-05 18:01:54 -0800972 LOG(ERROR) << "Verification failed on class "
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800973 << PrettyDescriptor(context->GetDexFile()->GetClassDescriptor(class_def))
jeffhaof56197c2012-03-05 18:01:54 -0800974 << " because: " << error_msg;
975 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800976 return;
977 }
978 CHECK(klass->IsResolved()) << PrettyClass(klass);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800979 context->GetClassLinker()->VerifyClass(klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800980
981 if (klass->IsErroneous()) {
982 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
983 CHECK(Thread::Current()->IsExceptionPending());
984 Thread::Current()->ClearException();
985 // We want to try verification again at run-time, so move back into the resolved state.
986 klass->SetStatus(Class::kStatusResolved);
987 }
988
989 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
990 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
991}
992
jeffhao98eacac2011-09-14 16:11:53 -0700993void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700994 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700995
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800996 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
997 Context context(class_linker, class_loader, this, class_linker->FindDexCache(dex_file), &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800998 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700999
jeffhaob4df5142011-09-19 20:25:32 -07001000 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001001}
1002
Brian Carlstromae826982011-11-09 01:33:42 -08001003void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
1004 const std::vector<const DexFile*>& dex_files) {
1005 for (size_t i = 0; i != dex_files.size(); ++i) {
1006 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001007 CHECK(dex_file != NULL);
1008 InitializeClassesWithoutClinit(class_loader, *dex_file);
1009 }
1010}
1011
1012void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
1013 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -07001014 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1015 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -07001016 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1017 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001018 if (klass != NULL) {
1019 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001020 // record the final class status if necessary
1021 Class::Status status = klass->GetStatus();
1022 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001023 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001024 CompiledClass* compiled_class = GetCompiledClass(ref);
1025 if (compiled_class == NULL) {
1026 compiled_class = new CompiledClass(status);
1027 compiled_classes_[ref] = compiled_class;
1028 } else {
1029 DCHECK_EQ(status, compiled_class->GetStatus());
1030 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001031 }
1032 // clear any class not found or verification exceptions
1033 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -07001034 }
1035
1036 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1037 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
1038 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001039 if (klass == NULL) {
1040 Thread::Current()->ClearException();
1041 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -07001042 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
1043 }
jeffhao98eacac2011-09-14 16:11:53 -07001044 }
1045}
1046
Brian Carlstromae826982011-11-09 01:33:42 -08001047void Compiler::Compile(const ClassLoader* class_loader,
1048 const std::vector<const DexFile*>& dex_files) {
1049 for (size_t i = 0; i != dex_files.size(); ++i) {
1050 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -07001051 CHECK(dex_file != NULL);
1052 CompileDexFile(class_loader, *dex_file);
1053 }
1054}
1055
Elliott Hughesc225caa2012-02-03 15:43:37 -08001056void Compiler::CompileClass(Context* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001057 const ClassLoader* class_loader = context->GetClassLoader();
1058 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesc225caa2012-02-03 15:43:37 -08001059 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Logan Chien7f767612012-03-01 18:54:49 +08001060
1061#if defined(ART_USE_LLVM_COMPILER)
Shih-wei Liaobe5ff202012-03-06 21:57:49 -08001062 compiler_llvm::CompilerLLVM* compiler_llvm = context->GetCompiler()->GetCompilerLLVM();
Logan Chien7f767612012-03-01 18:54:49 +08001063
1064 MutexLock GUARD(compiler_llvm->compiler_lock_);
1065 // TODO: Remove this. We should not lock the compiler_lock_ in CompileClass()
1066 // However, without this mutex lock, we will get segmentation fault.
1067#endif
1068
Brian Carlstrom5ead0952011-11-28 22:55:52 -08001069 if (SkipClass(class_loader, dex_file, class_def)) {
1070 return;
1071 }
jeffhaod1224c72012-02-29 13:43:08 -08001072 ClassReference ref(&dex_file, class_def_index);
1073 // Skip compiling classes with generic verifier failures since they will still fail at runtime
1074 if (verifier::DexVerifier::IsClassRejected(ref)) {
1075 return;
1076 }
Ian Rogers0571d352011-11-03 19:51:38 -07001077 const byte* class_data = dex_file.GetClassData(class_def);
1078 if (class_data == NULL) {
1079 // empty class, probably a marker interface
1080 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001081 }
Ian Rogers0571d352011-11-03 19:51:38 -07001082 ClassDataItemIterator it(dex_file, class_data);
1083 // Skip fields
1084 while (it.HasNextStaticField()) {
1085 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001086 }
Ian Rogers0571d352011-11-03 19:51:38 -07001087 while (it.HasNextInstanceField()) {
1088 it.Next();
1089 }
1090 // Compile direct methods
1091 while (it.HasNextDirectMethod()) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001092 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
1093 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001094 it.Next();
1095 }
1096 // Compile virtual methods
1097 while (it.HasNextVirtualMethod()) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001098 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
1099 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001100 it.Next();
1101 }
1102 DCHECK(!it.HasNext());
Logan Chien7f767612012-03-01 18:54:49 +08001103
1104#if defined(ART_USE_LLVM_COMPILER)
1105 compiler_llvm->MaterializeIfThresholdReached();
1106#endif
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001107}
1108
Elliott Hughesc225caa2012-02-03 15:43:37 -08001109void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001110 Context context(NULL, class_loader, this, NULL, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001111 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001112}
1113
Ian Rogersa3760aa2011-11-14 14:32:37 -08001114void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
1115 uint32_t method_idx, const ClassLoader* class_loader,
1116 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -07001117 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001118 uint64_t start_ns = NanoTime();
Logan Chien4dd96f52012-02-29 01:26:58 +08001119
1120#if defined(ART_USE_LLVM_COMPILER)
1121 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
1122 DexCache *dex_cache = class_linker->FindDexCache(dex_file);
1123
Logan Chiendbe72bd2012-03-01 18:27:33 +08001124 OatCompilationUnit oat_compilation_unit(
1125 class_loader, class_linker, dex_file, *dex_cache, code_item,
1126 method_idx, access_flags);
Logan Chien4dd96f52012-02-29 01:26:58 +08001127#endif
1128
Ian Rogers169c9a72011-11-13 20:13:17 -08001129 if ((access_flags & kAccNative) != 0) {
Logan Chien88894ee2012-02-13 16:42:22 +08001130#if defined(ART_USE_LLVM_COMPILER)
Logan Chiendbe72bd2012-03-01 18:27:33 +08001131 compiled_method = compiler_llvm_->CompileNativeMethod(&oat_compilation_unit);
Logan Chien88894ee2012-02-13 16:42:22 +08001132#else
Elliott Hughes46f060a2012-03-09 17:36:50 -08001133 compiled_method = (*jni_compiler_)(*this, access_flags, method_idx, class_loader, dex_file);
Logan Chien88894ee2012-02-13 16:42:22 +08001134#endif
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001135 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -08001136 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001137 } else {
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001138#if defined(ART_USE_LLVM_COMPILER)
Logan Chiendbe72bd2012-03-01 18:27:33 +08001139 compiled_method = compiler_llvm_->CompileDexMethod(&oat_compilation_unit);
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001140#else
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001141 compiled_method = (*compiler_)(*this, code_item, access_flags, method_idx, class_loader,
1142 dex_file);
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001143#endif
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001144 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
1145 }
Ian Rogers3bb17a62012-01-27 23:56:44 -08001146 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -08001147 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001148 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -08001149 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -07001150 }
1151
1152 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -07001153 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001154 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001155 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001156 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001157 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001158 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001159
Ian Rogers45619fc2012-02-29 11:15:25 -08001160 uint32_t shorty_len;
1161 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx), &shorty_len);
Ian Rogers169c9a72011-11-13 20:13:17 -08001162 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001163 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
1164 if (compiled_invoke_stub == NULL) {
Logan Chienf04364f2012-02-10 12:01:39 +08001165#if defined(ART_USE_LLVM_COMPILER)
1166 compiled_invoke_stub = compiler_llvm_->CreateInvokeStub(is_static, shorty);
1167#else
Elliott Hughes46f060a2012-03-09 17:36:50 -08001168 compiled_invoke_stub = (*create_invoke_stub_)(is_static, shorty, shorty_len);
Logan Chienf04364f2012-02-10 12:01:39 +08001169#endif
1170
Ian Rogers0571d352011-11-03 19:51:38 -07001171 CHECK(compiled_invoke_stub != NULL);
1172 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001173 }
Ian Rogers0571d352011-11-03 19:51:38 -07001174 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001175}
1176
Ian Rogers0571d352011-11-03 19:51:38 -07001177static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1178 std::string key(shorty);
1179 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001180 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001181 }
Ian Rogers0571d352011-11-03 19:51:38 -07001182 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001183}
1184
Ian Rogers0571d352011-11-03 19:51:38 -07001185const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001186 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001187 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001188 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001189 if (it == compiled_invoke_stubs_.end()) {
1190 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001191 } else {
1192 DCHECK(it->second != NULL);
1193 return it->second;
1194 }
1195}
1196
1197void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
1198 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001199 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001200 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001201 compiled_invoke_stubs_[key] = compiled_invoke_stub;
1202}
1203
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001204CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001205 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001206 ClassTable::const_iterator it = compiled_classes_.find(ref);
1207 if (it == compiled_classes_.end()) {
1208 return NULL;
1209 }
1210 CHECK(it->second != NULL);
1211 return it->second;
1212}
1213
Ian Rogers0571d352011-11-03 19:51:38 -07001214CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001215 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001216 MethodTable::const_iterator it = compiled_methods_.find(ref);
1217 if (it == compiled_methods_.end()) {
1218 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001219 }
1220 CHECK(it->second != NULL);
1221 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001222}
1223
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001224void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
1225 for (size_t i = 0; i != dex_files.size(); ++i) {
1226 const DexFile* dex_file = dex_files[i];
1227 CHECK(dex_file != NULL);
1228 SetGcMapsDexFile(class_loader, *dex_file);
1229 }
1230}
1231
1232void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
1233 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1234 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1235 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1236 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1237 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1238 Class* klass = class_linker->FindClass(descriptor, class_loader);
1239 if (klass == NULL || !klass->IsVerified()) {
1240 Thread::Current()->ClearException();
1241 continue;
1242 }
1243 const byte* class_data = dex_file.GetClassData(class_def);
1244 if (class_data == NULL) {
1245 // empty class such as a marker interface
1246 continue;
1247 }
1248 ClassDataItemIterator it(dex_file, class_data);
1249 while (it.HasNextStaticField()) {
1250 it.Next();
1251 }
1252 while (it.HasNextInstanceField()) {
1253 it.Next();
1254 }
1255 while (it.HasNextDirectMethod()) {
1256 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1257 class_loader, true);
1258 SetGcMapsMethod(dex_file, method);
1259 it.Next();
1260 }
1261 while (it.HasNextVirtualMethod()) {
1262 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1263 class_loader, false);
1264 SetGcMapsMethod(dex_file, method);
1265 it.Next();
1266 }
1267 }
1268}
1269
1270void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
1271 if (method == NULL) {
1272 Thread::Current()->ClearException();
1273 return;
1274 }
1275 uint16_t method_idx = method->GetDexMethodIndex();
1276 MethodReference ref(&dex_file, method_idx);
1277 CompiledMethod* compiled_method = GetCompiledMethod(ref);
1278 if (compiled_method == NULL) {
1279 return;
1280 }
1281 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
1282 if (gc_map == NULL) {
1283 return;
1284 }
1285 compiled_method->SetGcMap(*gc_map);
1286}
1287
Logan Chien8b977d32012-02-21 19:14:55 +08001288#if defined(ART_USE_LLVM_COMPILER)
1289void Compiler::SetElfFileName(std::string const& filename) {
1290 compiler_llvm_->SetElfFileName(filename);
1291}
1292
1293void Compiler::SetBitcodeFileName(std::string const& filename) {
1294 compiler_llvm_->SetBitcodeFileName(filename);
1295}
1296#endif
1297
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001298} // namespace art