blob: 17975306b9dbc641011750be44a37ee022eb1f3a [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
Brian Carlstrom27ec9612011-09-19 20:20:38 -070021#include <sys/mman.h>
Elliott Hughesd9c67be2012-02-02 19:54:06 -080022#include <unistd.h>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070023
Brian Carlstrom2cc022b2011-08-25 10:05:39 -070024#include "assembler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070027#include "dex_cache.h"
jeffhaof56197c2012-03-05 18:01:54 -080028#include "dex_verifier.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070029#include "jni_internal.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080030#include "oat_compilation_unit.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070034#include "stl_util.h"
Elliott Hughes601a1232012-02-02 17:47:38 -080035#include "timing_logger.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070036
Logan Chien9787ac42012-03-06 18:24:03 +080037#if !defined(ART_USE_LLVM_COMPILER)
38#include "jni_compiler.h"
39#endif
40
Shih-wei Liaod1fec812012-02-13 09:51:10 -080041#if defined(ART_USE_LLVM_COMPILER)
42#include "compiler_llvm/compiler_llvm.h"
43#endif
44
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070045namespace art {
46
Shih-wei Liaod1fec812012-02-13 09:51:10 -080047#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers996cc582012-02-14 22:23:29 -080048CompiledMethod* oatCompileMethod(Compiler& compiler, const DexFile::CodeItem* code_item,
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080049 uint32_t access_flags, uint32_t method_idx,
50 const ClassLoader* class_loader,
51 const DexFile& dex_file, InstructionSet);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080052#endif
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080053
Shih-wei Liaoc486c112011-09-13 16:43:52 -070054namespace arm {
Ian Rogersbdb03912011-09-14 00:55:44 -070055 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers45619fc2012-02-29 11:15:25 -080056 CompiledInvokeStub* ArmCreateInvokeStub(bool is_static, const char* shorty, uint32_t shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070057 ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080058 ByteArray* CreateJniDlsymLookupStub();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070059}
Shih-wei Liaoc486c112011-09-13 16:43:52 -070060namespace x86 {
Ian Rogersbdb03912011-09-14 00:55:44 -070061 ByteArray* CreateAbstractMethodErrorStub();
Ian Rogers45619fc2012-02-29 11:15:25 -080062 CompiledInvokeStub* X86CreateInvokeStub(bool is_static, const char* shorty, uint32_t shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070063 ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType type);
Elliott Hughes8add92d2012-01-18 18:18:43 -080064 ByteArray* CreateJniDlsymLookupStub();
Brian Carlstrome24fa612011-09-29 00:53:55 -070065}
66
Ian Rogers996cc582012-02-14 22:23:29 -080067static double Percentage(size_t x, size_t y) {
68 return 100.0 * ((double)x) / ((double)(x + y));
69}
70
71static void DumpStat(size_t x, size_t y, const char* str) {
72 if (x == 0 && y == 0) {
73 return;
74 }
75 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
76}
77
Ian Rogersc8b306f2012-02-17 21:34:44 -080078class AOTCompilationStats {
79 public:
80 AOTCompilationStats() : stats_lock_("AOT compilation statistics lock"),
81 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
82 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
83 resolved_types_(0), unresolved_types_(0),
84 resolved_instance_fields_(0), unresolved_instance_fields_(0),
Ian Rogersfb6adba2012-03-04 21:51:51 -080085 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
86 virtual_made_direct_(0) {
Ian Rogersc8b306f2012-02-17 21:34:44 -080087 for (size_t i = 0; i < kMaxInvokeType; i++) {
88 resolved_methods_[i] = 0;
89 unresolved_methods_[i] = 0;
90 }
91 }
92
93 void Dump() {
94 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
95 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
96 DumpStat(resolved_types_, unresolved_types_, "types resolved");
97 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
98 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
99 "static fields resolved");
100 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
101 "static fields local to a class");
102
103 for (size_t i = 0; i < kMaxInvokeType; i++) {
104 std::ostringstream oss;
105 oss << "resolved " << static_cast<InvokeType>(i) << " methods";
106 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
107 }
Ian Rogersfb6adba2012-03-04 21:51:51 -0800108 DumpStat(virtual_made_direct_, resolved_methods_[kVirtual] + unresolved_methods_[kVirtual],
109 "made direct from virtual");
Ian Rogersc8b306f2012-02-17 21:34:44 -0800110 }
Ian Rogers996cc582012-02-14 22:23:29 -0800111
112// Allow lossy statistics in non-debug builds
113#ifndef NDEBUG
114#define STATS_LOCK() MutexLock mu(stats_lock_)
115#else
116#define STATS_LOCK()
117#endif
118
Ian Rogersc8b306f2012-02-17 21:34:44 -0800119 void TypeInDexCache() {
120 STATS_LOCK();
121 types_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800122 }
Ian Rogers996cc582012-02-14 22:23:29 -0800123
Ian Rogersc8b306f2012-02-17 21:34:44 -0800124 void TypeNotInDexCache() {
125 STATS_LOCK();
126 types_not_in_dex_cache_++;
Ian Rogers996cc582012-02-14 22:23:29 -0800127 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800128
129 void StringInDexCache() {
130 STATS_LOCK();
131 strings_in_dex_cache_++;
132 }
133
134 void StringNotInDexCache() {
135 STATS_LOCK();
136 strings_not_in_dex_cache_++;
137 }
138
139 void TypeDoesntNeedAccessCheck() {
140 STATS_LOCK();
141 resolved_types_++;
142 }
143
144 void TypeNeedsAccessCheck() {
145 STATS_LOCK();
146 unresolved_types_++;
147 }
148
149 void ResolvedInstanceField() {
150 STATS_LOCK();
151 resolved_instance_fields_++;
152 }
153
154 void UnresolvedInstanceField(){
155 STATS_LOCK();
156 unresolved_instance_fields_++;
157 }
158
159 void ResolvedLocalStaticField() {
160 STATS_LOCK();
161 resolved_local_static_fields_++;
162 }
163
164 void ResolvedStaticField() {
165 STATS_LOCK();
166 resolved_static_fields_++;
167 }
168
169 void UnresolvedStaticField() {
170 STATS_LOCK();
171 unresolved_static_fields_++;
172 }
173
174 void ResolvedMethod(InvokeType type) {
175 DCHECK_LE(type, kMaxInvokeType);
176 STATS_LOCK();
177 resolved_methods_[type]++;
178 }
179
180 void UnresolvedMethod(InvokeType type) {
181 DCHECK_LE(type, kMaxInvokeType);
182 STATS_LOCK();
183 unresolved_methods_[type]++;
184 }
185
Ian Rogersfb6adba2012-03-04 21:51:51 -0800186 void VirtualMadeDirect() {
187 STATS_LOCK();
188 virtual_made_direct_++;
189 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800190 private:
191 Mutex stats_lock_;
192
193 size_t types_in_dex_cache_;
194 size_t types_not_in_dex_cache_;
195
196 size_t strings_in_dex_cache_;
197 size_t strings_not_in_dex_cache_;
198
199 size_t resolved_types_;
200 size_t unresolved_types_;
201
202 size_t resolved_instance_fields_;
203 size_t unresolved_instance_fields_;
204
205 size_t resolved_local_static_fields_;
206 size_t resolved_static_fields_;
207 size_t unresolved_static_fields_;
208
209 size_t resolved_methods_[kMaxInvokeType + 1];
210 size_t unresolved_methods_[kMaxInvokeType + 1];
Ian Rogersfb6adba2012-03-04 21:51:51 -0800211 size_t virtual_made_direct_;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800212
213 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);;
214};
Ian Rogers996cc582012-02-14 22:23:29 -0800215
Elliott Hughes5523ee02012-02-03 18:18:34 -0800216Compiler::Compiler(InstructionSet instruction_set, bool image, size_t thread_count,
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800217 bool support_debugging, const std::set<std::string>* image_classes)
Brian Carlstromaded5f72011-10-07 17:15:04 -0700218 : instruction_set_(instruction_set),
Logan Chien9787ac42012-03-06 18:24:03 +0800219#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 jni_compiler_(instruction_set),
Logan Chien9787ac42012-03-06 18:24:03 +0800221#endif
Elliott Hughesc225caa2012-02-03 15:43:37 -0800222 compiled_classes_lock_("compiled classes lock"),
223 compiled_methods_lock_("compiled method lock"),
224 compiled_invoke_stubs_lock_("compiled invoke stubs lock"),
Brian Carlstromaded5f72011-10-07 17:15:04 -0700225 image_(image),
Elliott Hughes5523ee02012-02-03 18:18:34 -0800226 thread_count_(thread_count),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800227 support_debugging_(support_debugging),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800228 stats_(new AOTCompilationStats),
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800229 image_classes_(image_classes)
230#if defined(ART_USE_LLVM_COMPILER)
231 ,
232 compiler_llvm_(new compiler_llvm::CompilerLLVM(this, instruction_set))
233#endif
234 {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700235 CHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800236 if (!image_) {
237 CHECK(image_classes_ == NULL);
238 }
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700239}
240
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700241Compiler::~Compiler() {
Elliott Hughesc225caa2012-02-03 15:43:37 -0800242 {
243 MutexLock mu(compiled_classes_lock_);
244 STLDeleteValues(&compiled_classes_);
245 }
246 {
247 MutexLock mu(compiled_methods_lock_);
248 STLDeleteValues(&compiled_methods_);
249 }
250 {
251 MutexLock mu(compiled_invoke_stubs_lock_);
252 STLDeleteValues(&compiled_invoke_stubs_);
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800253 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700254}
255
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700256ByteArray* Compiler::CreateResolutionStub(InstructionSet instruction_set,
257 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700258 if (instruction_set == kX86) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700259 return x86::X86CreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700260 } else {
261 CHECK(instruction_set == kArm || instruction_set == kThumb2);
262 // Generates resolution stub using ARM instruction set
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700263 return arm::ArmCreateResolutionTrampoline(type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700264 }
265}
266
Elliott Hughes8add92d2012-01-18 18:18:43 -0800267ByteArray* Compiler::CreateJniDlsymLookupStub(InstructionSet instruction_set) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800268 switch (instruction_set) {
269 case kArm:
270 case kThumb2:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800271 return arm::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800272 case kX86:
Elliott Hughes8add92d2012-01-18 18:18:43 -0800273 return x86::CreateJniDlsymLookupStub();
Ian Rogers169c9a72011-11-13 20:13:17 -0800274 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800275 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Ian Rogers169c9a72011-11-13 20:13:17 -0800276 return NULL;
277 }
278}
279
Ian Rogersad25ac52011-10-04 19:13:33 -0700280ByteArray* Compiler::CreateAbstractMethodErrorStub(InstructionSet instruction_set) {
281 if (instruction_set == kX86) {
282 return x86::CreateAbstractMethodErrorStub();
283 } else {
284 CHECK(instruction_set == kArm || instruction_set == kThumb2);
285 // Generates resolution stub using ARM instruction set
286 return arm::CreateAbstractMethodErrorStub();
287 }
288}
289
Jesse Wilson254db0f2011-11-16 16:44:11 -0500290void Compiler::CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -0800291 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700292 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800293
Elliott Hughes601a1232012-02-02 17:47:38 -0800294 TimingLogger timings("compiler");
295
296 PreCompile(class_loader, dex_files, timings);
297
Brian Carlstromae826982011-11-09 01:33:42 -0800298 Compile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800299 timings.AddSplit("Compile");
300
Brian Carlstromae826982011-11-09 01:33:42 -0800301 PostCompile(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800302 timings.AddSplit("PostCompile");
303
304 if (timings.GetTotalNs() > MsToNs(1000)) {
305 timings.Dump();
306 }
Ian Rogers996cc582012-02-14 22:23:29 -0800307
Ian Rogersc8b306f2012-02-17 21:34:44 -0800308 stats_->Dump();
Brian Carlstrom8a487412011-08-29 20:08:52 -0700309}
310
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700311void Compiler::CompileOne(const Method* method) {
Brian Carlstrom25c33252011-09-18 15:58:35 -0700312 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstromae826982011-11-09 01:33:42 -0800313
Brian Carlstrom8a487412011-08-29 20:08:52 -0700314 const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromae826982011-11-09 01:33:42 -0800315
Ian Rogers0571d352011-11-03 19:51:38 -0700316 // Find the dex_file
317 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
318 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
Brian Carlstromae826982011-11-09 01:33:42 -0800319 std::vector<const DexFile*> dex_files;
320 dex_files.push_back(&dex_file);
321
Elliott Hughes601a1232012-02-02 17:47:38 -0800322 TimingLogger timings("CompileOne");
323 PreCompile(class_loader, dex_files, timings);
Brian Carlstromae826982011-11-09 01:33:42 -0800324
Ian Rogers0571d352011-11-03 19:51:38 -0700325 uint32_t method_idx = method->GetDexMethodIndex();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800326 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
327 CompileMethod(code_item, method->GetAccessFlags(), method_idx, class_loader, dex_file);
Brian Carlstromae826982011-11-09 01:33:42 -0800328
329 PostCompile(class_loader, dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700330}
331
Brian Carlstromae826982011-11-09 01:33:42 -0800332void Compiler::Resolve(const ClassLoader* class_loader,
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800333 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800334 for (size_t i = 0; i != dex_files.size(); ++i) {
335 const DexFile* dex_file = dex_files[i];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700336 CHECK(dex_file != NULL);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800337 ResolveDexFile(class_loader, *dex_file, timings);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700338 }
339}
340
Brian Carlstromae826982011-11-09 01:33:42 -0800341void Compiler::PreCompile(const ClassLoader* class_loader,
Elliott Hughes601a1232012-02-02 17:47:38 -0800342 const std::vector<const DexFile*>& dex_files, TimingLogger& timings) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800343 Resolve(class_loader, dex_files, timings);
Elliott Hughes601a1232012-02-02 17:47:38 -0800344
Brian Carlstromae826982011-11-09 01:33:42 -0800345 Verify(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800346 timings.AddSplit("PreCompile.Verify");
347
Brian Carlstromae826982011-11-09 01:33:42 -0800348 InitializeClassesWithoutClinit(class_loader, dex_files);
Elliott Hughes601a1232012-02-02 17:47:38 -0800349 timings.AddSplit("PreCompile.InitializeClassesWithoutClinit");
Brian Carlstromae826982011-11-09 01:33:42 -0800350}
351
352void Compiler::PostCompile(const ClassLoader* class_loader,
353 const std::vector<const DexFile*>& dex_files) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800354 SetGcMaps(class_loader, dex_files);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800355#if defined(ART_USE_LLVM_COMPILER)
Logan Chien7f767612012-03-01 18:54:49 +0800356 compiler_llvm_->MaterializeRemainder();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800357#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800358}
359
360bool Compiler::IsImageClass(const std::string& descriptor) const {
361 if (image_classes_ == NULL) {
362 return true;
363 }
364 return image_classes_->find(descriptor) != image_classes_->end();
365}
366
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800367bool Compiler::CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800368 uint32_t type_idx) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800369 if (!IsImage()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800370 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800371 return false;
372 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800373 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800374 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800375 stats_->TypeNotInDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800376 return false;
377 }
Ian Rogers996cc582012-02-14 22:23:29 -0800378 bool result = IsImageClass(ClassHelper(resolved_class).GetDescriptor());
379 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800380 stats_->TypeInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800381 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800382 stats_->TypeNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800383 }
384 return result;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800385}
386
Ian Rogers1bddec32012-02-04 12:27:34 -0800387bool Compiler::CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800388 uint32_t string_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800389 // TODO: Add support for loading strings referenced by image_classes_
390 // See also Compiler::ResolveDexFile
391
392 // The following is a test saying that if we're building the image without a restricted set of
393 // 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 -0800394 bool result = IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
395 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800396 stats_->StringInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800397 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800398 stats_->StringNotInDexCache();
Ian Rogers996cc582012-02-14 22:23:29 -0800399 }
400 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800401}
402
403bool Compiler::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
Ian Rogers996cc582012-02-14 22:23:29 -0800404 const DexFile& dex_file, uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800405 // Get type from dex cache assuming it was populated by the verifier
406 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
407 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800408 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800409 return false; // Unknown class needs access checks.
410 }
411 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
412 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
413 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800414 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800415 return false; // Incomplete referrer knowledge needs access check.
416 }
417 // Perform access check, will return true if access is ok or false if we're going to have to
418 // check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800419 bool result = referrer_class->CanAccess(resolved_class);
420 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800421 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800422 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800423 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800424 }
425 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800426}
427
428bool Compiler::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
429 const DexCache* dex_cache,
430 const DexFile& dex_file,
Ian Rogers996cc582012-02-14 22:23:29 -0800431 uint32_t type_idx) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800432 // Get type from dex cache assuming it was populated by the verifier.
433 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
434 if (resolved_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800435 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800436 return false; // Unknown class needs access checks.
437 }
438 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
439 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
440 if (referrer_class == NULL) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800441 stats_->TypeNeedsAccessCheck();
Ian Rogers1bddec32012-02-04 12:27:34 -0800442 return false; // Incomplete referrer knowledge needs access check.
443 }
444 // Perform access and instantiable checks, will return true if access is ok or false if we're
445 // going to have to check this at runtime (for example for class loaders).
Ian Rogers996cc582012-02-14 22:23:29 -0800446 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
447 if (result) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800448 stats_->TypeDoesntNeedAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800449 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800450 stats_->TypeNeedsAccessCheck();
Ian Rogers996cc582012-02-14 22:23:29 -0800451 }
452 return result;
Ian Rogers1bddec32012-02-04 12:27:34 -0800453}
454
Logan Chien4dd96f52012-02-29 01:26:58 +0800455static Class* ComputeReferrerClass(OatCompilationUnit* mUnit) {
456 const DexFile::MethodId& referrer_method_id =
457 mUnit->dex_file_->GetMethodId(mUnit->method_idx_);
458
459 return mUnit->class_linker_->ResolveType(
460 *mUnit->dex_file_, referrer_method_id.class_idx_,
461 mUnit->dex_cache_, mUnit->class_loader_);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800462}
463
Logan Chien4dd96f52012-02-29 01:26:58 +0800464static Field* ComputeReferrerField(OatCompilationUnit* mUnit, uint32_t field_idx) {
465 return mUnit->class_linker_->ResolveField(
466 *mUnit->dex_file_, field_idx, mUnit->dex_cache_,
467 mUnit->class_loader_, false);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800468}
469
Logan Chien4dd96f52012-02-29 01:26:58 +0800470static Method* ComputeReferrerMethod(OatCompilationUnit* mUnit, uint32_t method_idx) {
471 return mUnit->class_linker_->ResolveMethod(
472 *mUnit->dex_file_, method_idx, mUnit->dex_cache_,
473 mUnit->class_loader_, true);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800474}
475
Logan Chien4dd96f52012-02-29 01:26:58 +0800476bool Compiler::ComputeInstanceFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
jeffhao8cd6dda2012-02-22 10:15:34 -0800477 int& field_offset, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800478 // Conservative defaults
479 field_offset = -1;
480 is_volatile = true;
481 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800482 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800483 if (resolved_field != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800484 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800485 // Try to resolve referring class then access check, failure to pass the
486 Class* fields_class = resolved_field->GetDeclaringClass();
jeffhao8cd6dda2012-02-22 10:15:34 -0800487 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal() &&
488 fields_class != referrer_class;
489 if (referrer_class != NULL && referrer_class->CanAccess(fields_class) &&
490 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
491 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800492 field_offset = resolved_field->GetOffset().Int32Value();
493 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800494 stats_->ResolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800495 return true; // Fast path.
496 }
497 }
498 // Clean up any exception left by field/type resolution
499 Thread* thread = Thread::Current();
500 if (thread->IsExceptionPending()) {
501 thread->ClearException();
502 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800503 stats_->UnresolvedInstanceField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800504 return false; // Incomplete knowledge needs slow path.
505}
506
Logan Chien4dd96f52012-02-29 01:26:58 +0800507bool Compiler::ComputeStaticFieldInfo(uint32_t field_idx, OatCompilationUnit* mUnit,
Ian Rogers1bddec32012-02-04 12:27:34 -0800508 int& field_offset, int& ssb_index,
jeffhao8cd6dda2012-02-22 10:15:34 -0800509 bool& is_referrers_class, bool& is_volatile, bool is_put) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800510 // Conservative defaults
511 field_offset = -1;
512 ssb_index = -1;
513 is_referrers_class = false;
514 is_volatile = true;
515 // Try to resolve field
Logan Chien4dd96f52012-02-29 01:26:58 +0800516 Field* resolved_field = ComputeReferrerField(mUnit, field_idx);
Ian Rogers1bddec32012-02-04 12:27:34 -0800517 if (resolved_field != NULL) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800518 DCHECK(resolved_field->IsStatic());
Logan Chien4dd96f52012-02-29 01:26:58 +0800519 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogers1bddec32012-02-04 12:27:34 -0800520 if (referrer_class != NULL) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800521 Class* fields_class = resolved_field->GetDeclaringClass();
522 if (fields_class == referrer_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800523 is_referrers_class = true; // implies no worrying about class initialization
524 field_offset = resolved_field->GetOffset().Int32Value();
525 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800526 stats_->ResolvedLocalStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800527 return true; // fast path
528 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -0800529 bool is_write_to_final_from_wrong_class = is_put && resolved_field->IsFinal();
Ian Rogers1bddec32012-02-04 12:27:34 -0800530 if (referrer_class->CanAccess(fields_class) &&
jeffhao8cd6dda2012-02-22 10:15:34 -0800531 referrer_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) &&
532 !is_write_to_final_from_wrong_class) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800533 // We have the resolved field, we must make it into a ssbIndex for the referrer
534 // in its static storage base (which may fail if it doesn't have a slot for it)
Ian Rogers4103ad22012-02-06 09:18:25 -0800535 // TODO: for images we can elide the static storage base null check
536 // if we know there's a non-null entry in the image
Logan Chien4dd96f52012-02-29 01:26:58 +0800537 if (fields_class->GetDexCache() == mUnit->dex_cache_) {
Ian Rogers4103ad22012-02-06 09:18:25 -0800538 // common case where the dex cache of both the referrer and the field are the same,
539 // no need to search the dex file
540 ssb_index = fields_class->GetDexTypeIndex();
541 field_offset = resolved_field->GetOffset().Int32Value();
542 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800543 stats_->ResolvedStaticField();
Ian Rogers4103ad22012-02-06 09:18:25 -0800544 return true;
545 }
546 // Search dex file for localized ssb index
Ian Rogers1bddec32012-02-04 12:27:34 -0800547 std::string descriptor(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
548 const DexFile::StringId* string_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800549 mUnit->dex_file_->FindStringId(descriptor);
Ian Rogers1bddec32012-02-04 12:27:34 -0800550 if (string_id != NULL) {
551 const DexFile::TypeId* type_id =
Logan Chien4dd96f52012-02-29 01:26:58 +0800552 mUnit->dex_file_->FindTypeId(mUnit->dex_file_->GetIndexForStringId(*string_id));
Ian Rogers1bddec32012-02-04 12:27:34 -0800553 if(type_id != NULL) {
554 // medium path, needs check of static storage base being initialized
Logan Chien4dd96f52012-02-29 01:26:58 +0800555 ssb_index = mUnit->dex_file_->GetIndexForTypeId(*type_id);
Ian Rogers1bddec32012-02-04 12:27:34 -0800556 field_offset = resolved_field->GetOffset().Int32Value();
557 is_volatile = resolved_field->IsVolatile();
Ian Rogersc8b306f2012-02-17 21:34:44 -0800558 stats_->ResolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800559 return true;
560 }
561 }
562 }
563 }
564 }
565 }
566 // Clean up any exception left by field/type resolution
567 Thread* thread = Thread::Current();
568 if (thread->IsExceptionPending()) {
569 thread->ClearException();
570 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800571 stats_->UnresolvedStaticField();
Ian Rogers1bddec32012-02-04 12:27:34 -0800572 return false; // Incomplete knowledge needs slow path.
573}
574
Ian Rogersfb6adba2012-03-04 21:51:51 -0800575bool Compiler::ComputeInvokeInfo(uint32_t method_idx, OatCompilationUnit* mUnit, InvokeType& type,
Ian Rogers996cc582012-02-14 22:23:29 -0800576 int& vtable_idx) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800577 vtable_idx = -1;
Logan Chien4dd96f52012-02-29 01:26:58 +0800578 Method* resolved_method = ComputeReferrerMethod(mUnit, method_idx);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800579 if (resolved_method != NULL) {
Logan Chien4dd96f52012-02-29 01:26:58 +0800580 Class* referrer_class = ComputeReferrerClass(mUnit);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800581 if (referrer_class != NULL) {
582 Class* methods_class = resolved_method->GetDeclaringClass();
583 if (!referrer_class->CanAccess(methods_class) ||
584 !referrer_class->CanAccessMember(methods_class,
Ian Rogers996cc582012-02-14 22:23:29 -0800585 resolved_method->GetAccessFlags())) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800586 // The referring class can't access the resolved method, this may occur as a result of a
587 // protected method being made public by implementing an interface that re-declares the
588 // method public. Resort to the dex file to determine the correct class for the access check
Logan Chien4dd96f52012-02-29 01:26:58 +0800589 const DexFile& dex_file = mUnit->class_linker_->FindDexFile(referrer_class->GetDexCache());
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800590 methods_class =
Logan Chien4dd96f52012-02-29 01:26:58 +0800591 mUnit->class_linker_->ResolveType(dex_file,
592 dex_file.GetMethodId(method_idx).class_idx_,
593 referrer_class);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800594
595 }
596 if (referrer_class->CanAccess(methods_class) &&
597 referrer_class->CanAccessMember(methods_class,
598 resolved_method->GetAccessFlags())) {
599 vtable_idx = resolved_method->GetMethodIndex();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800600 if (type == kVirtual && (resolved_method->IsFinal() || methods_class->IsFinal())) {
601 stats_->ResolvedMethod(kVirtual);
602 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
603 // dex cache, check that this resolved method is where we expect it.
604 CHECK(referrer_class->GetDexCache()->GetResolvedMethod(method_idx) == resolved_method)
605 << PrettyMethod(resolved_method);
606 type = kDirect;
607 stats_->VirtualMadeDirect();
608 return true;
609 } else if (type != kSuper) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800610 // nothing left to do for static/direct/virtual/interface dispatch
611 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800612 return true;
613 } else {
614 // ensure the vtable index will be correct to dispatch in the vtable of the super class
Ian Rogers996cc582012-02-14 22:23:29 -0800615 if (referrer_class->IsSubClass(methods_class) &&
616 vtable_idx < methods_class->GetVTable()->GetLength()) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800617 stats_->ResolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800618 return true;
619 }
620 }
621 }
622 }
623 }
624 // Clean up any exception left by method/type resolution
625 Thread* thread = Thread::Current();
626 if (thread->IsExceptionPending()) {
627 thread->ClearException();
628 }
Ian Rogersc8b306f2012-02-17 21:34:44 -0800629 stats_->UnresolvedMethod(type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800630 return false; // Incomplete knowledge needs slow path.
631}
632
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800633// Return true if the class should be skipped during compilation. We
634// never skip classes in the boot class loader. However, if we have a
635// non-boot class loader and we can resolve the class in the boot
636// class loader, we do skip the class. This happens if an app bundles
637// classes found in the boot classpath. Since at runtime we will
638// select the class from the boot classpath, do not attempt to resolve
639// or compile it now.
640static bool SkipClass(const ClassLoader* class_loader,
641 const DexFile& dex_file,
642 const DexFile::ClassDef& class_def) {
643 if (class_loader == NULL) {
644 return false;
645 }
646 const char* descriptor = dex_file.GetClassDescriptor(class_def);
647 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
648 Class* klass = class_linker->FindClass(descriptor, NULL);
649 if (klass == NULL) {
650 Thread* self = Thread::Current();
651 CHECK(self->IsExceptionPending());
652 self->ClearException();
653 return false;
654 }
655 return true;
656}
657
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800658class Context {
659 public:
660 Context(ClassLinker* class_linker,
661 const ClassLoader* class_loader,
662 Compiler* compiler,
663 DexCache* dex_cache,
664 const DexFile* dex_file)
665 : class_linker_(class_linker),
666 class_loader_(class_loader),
667 compiler_(compiler),
668 dex_cache_(dex_cache),
669 dex_file_(dex_file) {}
670
671 ClassLinker* GetClassLinker() {
672 CHECK(class_linker_ != NULL);
673 return class_linker_;
674 }
675 const ClassLoader* GetClassLoader() {
676 return class_loader_;
677 }
678 Compiler* GetCompiler() {
679 CHECK(compiler_ != NULL);
680 return compiler_;
681 }
682 DexCache* GetDexCache() {
683 CHECK(dex_cache_ != NULL);
684 return dex_cache_;
685 }
686 const DexFile* GetDexFile() {
687 CHECK(dex_file_ != NULL);
688 return dex_file_;
689 }
690
691 private:
692 ClassLinker* class_linker_;
693 const ClassLoader* class_loader_;
694 Compiler* compiler_;
695 DexCache* dex_cache_;
696 const DexFile* dex_file_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800697};
698
699typedef void Callback(Context* context, size_t index);
700
701class WorkerThread {
702 public:
Elliott Hughes1e409252012-02-06 11:21:27 -0800703 WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
704 : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
705 if (spawn_) {
706 CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread");
707 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800708 }
709
710 ~WorkerThread() {
Elliott Hughes1e409252012-02-06 11:21:27 -0800711 if (spawn_) {
712 CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
713 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800714 }
715
716 private:
Elliott Hughes1e409252012-02-06 11:21:27 -0800717 static void* Go(void* arg) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800718 WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
719 Runtime* runtime = Runtime::Current();
Elliott Hughes1e409252012-02-06 11:21:27 -0800720 if (worker->spawn_) {
721 runtime->AttachCurrentThread("Compiler Worker", true);
722 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800723 Thread::Current()->SetState(Thread::kRunnable);
724 worker->Run();
Elliott Hughes1e409252012-02-06 11:21:27 -0800725 if (worker->spawn_) {
726 Thread::Current()->SetState(Thread::kNative);
727 runtime->DetachCurrentThread();
728 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800729 return NULL;
730 }
731
Elliott Hughes1e409252012-02-06 11:21:27 -0800732 void Go() {
733 Go(this);
734 }
735
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800736 void Run() {
737 for (size_t i = begin_; i < end_; i += stripe_) {
738 callback_(context_, i);
739 }
740 }
741
742 pthread_t pthread_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800743 bool spawn_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800744
745 Context* context_;
746 size_t begin_;
747 size_t end_;
748 Callback* callback_;
749 size_t stripe_;
Elliott Hughes1e409252012-02-06 11:21:27 -0800750
751 friend void ForAll(Context*, size_t, size_t, Callback, size_t);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800752};
753
Elliott Hughes5523ee02012-02-03 18:18:34 -0800754void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800755 CHECK_GT(thread_count, 0U);
Elliott Hughes81d91512012-02-03 16:38:43 -0800756
Elliott Hughes1e409252012-02-06 11:21:27 -0800757 std::vector<WorkerThread*> threads;
Elliott Hughes81d91512012-02-03 16:38:43 -0800758 for (size_t i = 0; i < thread_count; ++i) {
Elliott Hughes1e409252012-02-06 11:21:27 -0800759 threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800760 }
Elliott Hughes1e409252012-02-06 11:21:27 -0800761 threads[0]->Go();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800762
Elliott Hughes81d91512012-02-03 16:38:43 -0800763 // Switch to kVmWait while we're blocked waiting for the other threads to finish.
764 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
765 STLDeleteElements(&threads);
766}
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800767
768static void ResolveClassFieldsAndMethods(Context* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800769 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800770
771 // Method and Field are the worst. We can't resolve without either
772 // context from the code use (to disambiguate virtual vs direct
773 // method and instance vs static field) or from class
774 // definitions. While the compiler will resolve what it can as it
775 // needs it, here we try to resolve fields and methods used in class
776 // definitions, since many of them many never be referenced by
777 // generated code.
778 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800779 if (SkipClass(context->GetClassLoader(), dex_file, class_def)) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800780 return;
781 }
782
783 // Note the class_data pointer advances through the headers,
784 // static fields, instance fields, direct methods, and virtual
785 // methods.
786 const byte* class_data = dex_file.GetClassData(class_def);
787 if (class_data == NULL) {
788 // empty class such as a marker interface
789 return;
790 }
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800791 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800792 ClassLinker* class_linker = context->GetClassLinker();
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800793 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
794 ClassDataItemIterator it(dex_file, class_data);
795 while (it.HasNextStaticField()) {
796 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800797 context->GetClassLoader(), true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800798 if (field == NULL) {
799 CHECK(self->IsExceptionPending());
800 self->ClearException();
801 }
802 it.Next();
803 }
804 while (it.HasNextInstanceField()) {
805 Field* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800806 context->GetClassLoader(), false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800807 if (field == NULL) {
808 CHECK(self->IsExceptionPending());
809 self->ClearException();
810 }
811 it.Next();
812 }
813 while (it.HasNextDirectMethod()) {
814 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800815 context->GetClassLoader(), true);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800816 if (method == NULL) {
817 CHECK(self->IsExceptionPending());
818 self->ClearException();
819 }
820 it.Next();
821 }
822 while (it.HasNextVirtualMethod()) {
823 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800824 context->GetClassLoader(), false);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800825 if (method == NULL) {
826 CHECK(self->IsExceptionPending());
827 self->ClearException();
828 }
829 it.Next();
830 }
831 DCHECK(!it.HasNext());
832}
833
834static void ResolveType(Context* context, size_t type_idx) {
835 // Class derived values are more complicated, they require the linker and loader.
836 Thread* self = Thread::Current();
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800837 Class* klass = context->GetClassLinker()->ResolveType(*context->GetDexFile(),
838 type_idx,
839 context->GetDexCache(),
840 context->GetClassLoader());
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800841 if (klass == NULL) {
842 CHECK(self->IsExceptionPending());
843 Thread::Current()->ClearException();
844 }
845}
846
847void Compiler::ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file, TimingLogger& timings) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700848 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700849 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700850
Brian Carlstromae826982011-11-09 01:33:42 -0800851 // Strings are easy in that they always are simply resolved to literals in the same file
852 if (image_ && image_classes_ == NULL) {
853 // TODO: Add support for loading strings referenced by image_classes_
854 // See also Compiler::CanAssumeTypeIsPresentInDexCache.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700855 for (size_t string_idx = 0; string_idx < dex_cache->NumStrings(); string_idx++) {
856 class_linker->ResolveString(dex_file, string_idx, dex_cache);
857 }
Elliott Hughesff738062012-02-03 15:00:42 -0800858 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Strings");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700859 }
860
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800861 Context context(class_linker, class_loader, this, dex_cache, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800862 ForAll(&context, 0, dex_cache->NumResolvedTypes(), ResolveType, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800863 timings.AddSplit("Resolve " + dex_file.GetLocation() + " Types");
Brian Carlstrom845490b2011-09-19 15:56:53 -0700864
Elliott Hughes5523ee02012-02-03 18:18:34 -0800865 ForAll(&context, 0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
Elliott Hughesff738062012-02-03 15:00:42 -0800866 timings.AddSplit("Resolve " + dex_file.GetLocation() + " MethodsAndFields");
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700867}
868
Brian Carlstromae826982011-11-09 01:33:42 -0800869void Compiler::Verify(const ClassLoader* class_loader,
870 const std::vector<const DexFile*>& dex_files) {
871 for (size_t i = 0; i != dex_files.size(); ++i) {
872 const DexFile* dex_file = dex_files[i];
jeffhao98eacac2011-09-14 16:11:53 -0700873 CHECK(dex_file != NULL);
874 VerifyDexFile(class_loader, *dex_file);
875 }
876}
877
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800878static void VerifyClass(Context* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800879 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
880 const char* descriptor = context->GetDexFile()->GetClassDescriptor(class_def);
881 Class* klass = context->GetClassLinker()->FindClass(descriptor, context->GetClassLoader());
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800882 if (klass == NULL) {
883 Thread* self = Thread::Current();
884 CHECK(self->IsExceptionPending());
885 self->ClearException();
jeffhaof56197c2012-03-05 18:01:54 -0800886
887 /*
888 * At compile time, we can still structurally verify the class even if FindClass fails.
889 * This is to ensure the class is structurally sound for compilation. An unsound class
890 * will be rejected by the verifier and later skipped during compilation in the compiler.
891 */
892 std::string error_msg;
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800893 if (!verifier::DexVerifier::VerifyClass(context->GetDexFile(), context->GetDexCache(),
894 context->GetClassLoader(), class_def_index, error_msg)) {
895 const DexFile::ClassDef& class_def = context->GetDexFile()->GetClassDef(class_def_index);
jeffhaof56197c2012-03-05 18:01:54 -0800896 LOG(ERROR) << "Verification failed on class "
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800897 << PrettyDescriptor(context->GetDexFile()->GetClassDescriptor(class_def))
jeffhaof56197c2012-03-05 18:01:54 -0800898 << " because: " << error_msg;
899 }
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800900 return;
901 }
902 CHECK(klass->IsResolved()) << PrettyClass(klass);
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800903 context->GetClassLinker()->VerifyClass(klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800904
905 if (klass->IsErroneous()) {
906 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
907 CHECK(Thread::Current()->IsExceptionPending());
908 Thread::Current()->ClearException();
909 // We want to try verification again at run-time, so move back into the resolved state.
910 klass->SetStatus(Class::kStatusResolved);
911 }
912
913 CHECK(klass->IsVerified() || klass->IsResolved()) << PrettyClass(klass);
914 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyTypeOf(Thread::Current()->GetException());
915}
916
jeffhao98eacac2011-09-14 16:11:53 -0700917void Compiler::VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
jeffhaob4df5142011-09-19 20:25:32 -0700918 dex_file.ChangePermissions(PROT_READ | PROT_WRITE);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700919
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800920 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
921 Context context(class_linker, class_loader, this, class_linker->FindDexCache(dex_file), &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800922 ForAll(&context, 0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700923
jeffhaob4df5142011-09-19 20:25:32 -0700924 dex_file.ChangePermissions(PROT_READ);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700925}
926
Brian Carlstromae826982011-11-09 01:33:42 -0800927void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader,
928 const std::vector<const DexFile*>& dex_files) {
929 for (size_t i = 0; i != dex_files.size(); ++i) {
930 const DexFile* dex_file = dex_files[i];
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700931 CHECK(dex_file != NULL);
932 InitializeClassesWithoutClinit(class_loader, *dex_file);
933 }
934}
935
936void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
937 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700938 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
939 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700940 const char* descriptor = dex_file.GetClassDescriptor(class_def);
941 Class* klass = class_linker->FindClass(descriptor, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700942 if (klass != NULL) {
943 class_linker->EnsureInitialized(klass, false);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800944 // record the final class status if necessary
945 Class::Status status = klass->GetStatus();
946 ClassReference ref(&dex_file, class_def_index);
Elliott Hughesc225caa2012-02-03 15:43:37 -0800947 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800948 CompiledClass* compiled_class = GetCompiledClass(ref);
949 if (compiled_class == NULL) {
950 compiled_class = new CompiledClass(status);
951 compiled_classes_[ref] = compiled_class;
952 } else {
953 DCHECK_EQ(status, compiled_class->GetStatus());
954 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700955 }
956 // clear any class not found or verification exceptions
957 Thread::Current()->ClearException();
Brian Carlstromffca45d2011-09-16 12:10:49 -0700958 }
959
960 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
961 for (size_t type_idx = 0; type_idx < dex_cache->NumResolvedTypes(); type_idx++) {
962 Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700963 if (klass == NULL) {
964 Thread::Current()->ClearException();
965 } else if (klass->IsInitialized()) {
Brian Carlstromffca45d2011-09-16 12:10:49 -0700966 dex_cache->GetInitializedStaticStorage()->Set(type_idx, klass);
967 }
jeffhao98eacac2011-09-14 16:11:53 -0700968 }
969}
970
Brian Carlstromae826982011-11-09 01:33:42 -0800971void Compiler::Compile(const ClassLoader* class_loader,
972 const std::vector<const DexFile*>& dex_files) {
973 for (size_t i = 0; i != dex_files.size(); ++i) {
974 const DexFile* dex_file = dex_files[i];
Brian Carlstrom83db7722011-08-26 17:32:56 -0700975 CHECK(dex_file != NULL);
976 CompileDexFile(class_loader, *dex_file);
977 }
978}
979
Elliott Hughesc225caa2012-02-03 15:43:37 -0800980void Compiler::CompileClass(Context* context, size_t class_def_index) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -0800981 const ClassLoader* class_loader = context->GetClassLoader();
982 const DexFile& dex_file = *context->GetDexFile();
Elliott Hughesc225caa2012-02-03 15:43:37 -0800983 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Logan Chien7f767612012-03-01 18:54:49 +0800984
985#if defined(ART_USE_LLVM_COMPILER)
986 compiler_llvm::CompilerLLVM* compiler_llvm = context->compiler->GetCompilerLLVM();
987
988 MutexLock GUARD(compiler_llvm->compiler_lock_);
989 // TODO: Remove this. We should not lock the compiler_lock_ in CompileClass()
990 // However, without this mutex lock, we will get segmentation fault.
991#endif
992
Brian Carlstrom5ead0952011-11-28 22:55:52 -0800993 if (SkipClass(class_loader, dex_file, class_def)) {
994 return;
995 }
jeffhaod1224c72012-02-29 13:43:08 -0800996 ClassReference ref(&dex_file, class_def_index);
997 // Skip compiling classes with generic verifier failures since they will still fail at runtime
998 if (verifier::DexVerifier::IsClassRejected(ref)) {
999 return;
1000 }
Ian Rogers0571d352011-11-03 19:51:38 -07001001 const byte* class_data = dex_file.GetClassData(class_def);
1002 if (class_data == NULL) {
1003 // empty class, probably a marker interface
1004 return;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001005 }
Ian Rogers0571d352011-11-03 19:51:38 -07001006 ClassDataItemIterator it(dex_file, class_data);
1007 // Skip fields
1008 while (it.HasNextStaticField()) {
1009 it.Next();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001010 }
Ian Rogers0571d352011-11-03 19:51:38 -07001011 while (it.HasNextInstanceField()) {
1012 it.Next();
1013 }
1014 // Compile direct methods
1015 while (it.HasNextDirectMethod()) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001016 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
1017 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001018 it.Next();
1019 }
1020 // Compile virtual methods
1021 while (it.HasNextVirtualMethod()) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001022 context->GetCompiler()->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(),
1023 it.GetMemberIndex(), class_loader, dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -07001024 it.Next();
1025 }
1026 DCHECK(!it.HasNext());
Logan Chien7f767612012-03-01 18:54:49 +08001027
1028#if defined(ART_USE_LLVM_COMPILER)
1029 compiler_llvm->MaterializeIfThresholdReached();
1030#endif
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001031}
1032
Elliott Hughesc225caa2012-02-03 15:43:37 -08001033void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
Brian Carlstrom731b2ab2012-03-06 16:53:35 -08001034 Context context(NULL, class_loader, this, NULL, &dex_file);
Elliott Hughes5523ee02012-02-03 18:18:34 -08001035 ForAll(&context, 0, dex_file.NumClassDefs(), Compiler::CompileClass, thread_count_);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001036}
1037
Ian Rogersa3760aa2011-11-14 14:32:37 -08001038void Compiler::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
1039 uint32_t method_idx, const ClassLoader* class_loader,
1040 const DexFile& dex_file) {
Elliott Hughesf09afe82011-10-16 14:24:21 -07001041 CompiledMethod* compiled_method = NULL;
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001042 uint64_t start_ns = NanoTime();
Logan Chien4dd96f52012-02-29 01:26:58 +08001043
1044#if defined(ART_USE_LLVM_COMPILER)
1045 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
1046 DexCache *dex_cache = class_linker->FindDexCache(dex_file);
1047
Logan Chiendbe72bd2012-03-01 18:27:33 +08001048 OatCompilationUnit oat_compilation_unit(
1049 class_loader, class_linker, dex_file, *dex_cache, code_item,
1050 method_idx, access_flags);
Logan Chien4dd96f52012-02-29 01:26:58 +08001051#endif
1052
Ian Rogers169c9a72011-11-13 20:13:17 -08001053 if ((access_flags & kAccNative) != 0) {
Logan Chien88894ee2012-02-13 16:42:22 +08001054#if defined(ART_USE_LLVM_COMPILER)
Logan Chiendbe72bd2012-03-01 18:27:33 +08001055 compiled_method = compiler_llvm_->CompileNativeMethod(&oat_compilation_unit);
Logan Chien88894ee2012-02-13 16:42:22 +08001056#else
Ian Rogers169c9a72011-11-13 20:13:17 -08001057 compiled_method = jni_compiler_.Compile(access_flags, method_idx, class_loader, dex_file);
Logan Chien88894ee2012-02-13 16:42:22 +08001058#endif
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001059 CHECK(compiled_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -08001060 } else if ((access_flags & kAccAbstract) != 0) {
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001061 } else {
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001062#if defined(ART_USE_LLVM_COMPILER)
Logan Chiendbe72bd2012-03-01 18:27:33 +08001063 compiled_method = compiler_llvm_->CompileDexMethod(&oat_compilation_unit);
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001064#else
Ian Rogersa3760aa2011-11-14 14:32:37 -08001065 compiled_method = oatCompileMethod(*this, code_item, access_flags, method_idx, class_loader,
1066 dex_file, kThumb2);
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001067#endif
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001068 CHECK(compiled_method != NULL) << PrettyMethod(method_idx, dex_file);
1069 }
Ian Rogers3bb17a62012-01-27 23:56:44 -08001070 uint64_t duration_ns = NanoTime() - start_ns;
buzbee5abfa3e2012-01-31 17:01:43 -08001071 if (duration_ns > MsToNs(100)) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001072 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
Ian Rogers3bb17a62012-01-27 23:56:44 -08001073 << " took " << PrettyDuration(duration_ns);
Elliott Hughesf09afe82011-10-16 14:24:21 -07001074 }
1075
1076 if (compiled_method != NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -07001077 MethodReference ref(&dex_file, method_idx);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001078 CHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file);
Elliott Hughesc225caa2012-02-03 15:43:37 -08001079 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001080 compiled_methods_[ref] = compiled_method;
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001081 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom2cc022b2011-08-25 10:05:39 -07001082 }
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001083
Ian Rogers45619fc2012-02-29 11:15:25 -08001084 uint32_t shorty_len;
1085 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx), &shorty_len);
Ian Rogers169c9a72011-11-13 20:13:17 -08001086 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001087 const CompiledInvokeStub* compiled_invoke_stub = FindInvokeStub(is_static, shorty);
1088 if (compiled_invoke_stub == NULL) {
Logan Chienf04364f2012-02-10 12:01:39 +08001089#if defined(ART_USE_LLVM_COMPILER)
1090 compiled_invoke_stub = compiler_llvm_->CreateInvokeStub(is_static, shorty);
1091#else
Ian Rogers0571d352011-11-03 19:51:38 -07001092 if (instruction_set_ == kX86) {
Ian Rogers45619fc2012-02-29 11:15:25 -08001093 compiled_invoke_stub = ::art::x86::X86CreateInvokeStub(is_static, shorty, shorty_len);
Ian Rogers0571d352011-11-03 19:51:38 -07001094 } else {
1095 CHECK(instruction_set_ == kArm || instruction_set_ == kThumb2);
1096 // Generates invocation stub using ARM instruction set
Ian Rogers45619fc2012-02-29 11:15:25 -08001097 compiled_invoke_stub = ::art::arm::ArmCreateInvokeStub(is_static, shorty, shorty_len);
Ian Rogers0571d352011-11-03 19:51:38 -07001098 }
Logan Chienf04364f2012-02-10 12:01:39 +08001099#endif
1100
Ian Rogers0571d352011-11-03 19:51:38 -07001101 CHECK(compiled_invoke_stub != NULL);
1102 InsertInvokeStub(is_static, shorty, compiled_invoke_stub);
Ian Rogers2c8f6532011-09-02 17:16:34 -07001103 }
Ian Rogers0571d352011-11-03 19:51:38 -07001104 CHECK(!Thread::Current()->IsExceptionPending()) << PrettyMethod(method_idx, dex_file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001105}
1106
Ian Rogers0571d352011-11-03 19:51:38 -07001107static std::string MakeInvokeStubKey(bool is_static, const char* shorty) {
1108 std::string key(shorty);
1109 if (is_static) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -08001110 key += "$"; // Must not be a shorty type character.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001111 }
Ian Rogers0571d352011-11-03 19:51:38 -07001112 return key;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001113}
1114
Ian Rogers0571d352011-11-03 19:51:38 -07001115const CompiledInvokeStub* Compiler::FindInvokeStub(bool is_static, const char* shorty) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001116 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001117 const std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001118 InvokeStubTable::const_iterator it = compiled_invoke_stubs_.find(key);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001119 if (it == compiled_invoke_stubs_.end()) {
1120 return NULL;
Ian Rogers0571d352011-11-03 19:51:38 -07001121 } else {
1122 DCHECK(it->second != NULL);
1123 return it->second;
1124 }
1125}
1126
1127void Compiler::InsertInvokeStub(bool is_static, const char* shorty,
1128 const CompiledInvokeStub* compiled_invoke_stub) {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001129 MutexLock mu(compiled_invoke_stubs_lock_);
Elliott Hughes95572412011-12-13 18:14:20 -08001130 std::string key(MakeInvokeStubKey(is_static, shorty));
Ian Rogers0571d352011-11-03 19:51:38 -07001131 compiled_invoke_stubs_[key] = compiled_invoke_stub;
1132}
1133
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001134CompiledClass* Compiler::GetCompiledClass(ClassReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001135 MutexLock mu(compiled_classes_lock_);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001136 ClassTable::const_iterator it = compiled_classes_.find(ref);
1137 if (it == compiled_classes_.end()) {
1138 return NULL;
1139 }
1140 CHECK(it->second != NULL);
1141 return it->second;
1142}
1143
Ian Rogers0571d352011-11-03 19:51:38 -07001144CompiledMethod* Compiler::GetCompiledMethod(MethodReference ref) const {
Elliott Hughesc225caa2012-02-03 15:43:37 -08001145 MutexLock mu(compiled_methods_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001146 MethodTable::const_iterator it = compiled_methods_.find(ref);
1147 if (it == compiled_methods_.end()) {
1148 return NULL;
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001149 }
1150 CHECK(it->second != NULL);
1151 return it->second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001152}
1153
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001154void Compiler::SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files) {
1155 for (size_t i = 0; i != dex_files.size(); ++i) {
1156 const DexFile* dex_file = dex_files[i];
1157 CHECK(dex_file != NULL);
1158 SetGcMapsDexFile(class_loader, *dex_file);
1159 }
1160}
1161
1162void Compiler::SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {
1163 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1164 DexCache* dex_cache = class_linker->FindDexCache(dex_file);
1165 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
1166 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1167 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1168 Class* klass = class_linker->FindClass(descriptor, class_loader);
1169 if (klass == NULL || !klass->IsVerified()) {
1170 Thread::Current()->ClearException();
1171 continue;
1172 }
1173 const byte* class_data = dex_file.GetClassData(class_def);
1174 if (class_data == NULL) {
1175 // empty class such as a marker interface
1176 continue;
1177 }
1178 ClassDataItemIterator it(dex_file, class_data);
1179 while (it.HasNextStaticField()) {
1180 it.Next();
1181 }
1182 while (it.HasNextInstanceField()) {
1183 it.Next();
1184 }
1185 while (it.HasNextDirectMethod()) {
1186 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1187 class_loader, true);
1188 SetGcMapsMethod(dex_file, method);
1189 it.Next();
1190 }
1191 while (it.HasNextVirtualMethod()) {
1192 Method* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), dex_cache,
1193 class_loader, false);
1194 SetGcMapsMethod(dex_file, method);
1195 it.Next();
1196 }
1197 }
1198}
1199
1200void Compiler::SetGcMapsMethod(const DexFile& dex_file, Method* method) {
1201 if (method == NULL) {
1202 Thread::Current()->ClearException();
1203 return;
1204 }
1205 uint16_t method_idx = method->GetDexMethodIndex();
1206 MethodReference ref(&dex_file, method_idx);
1207 CompiledMethod* compiled_method = GetCompiledMethod(ref);
1208 if (compiled_method == NULL) {
1209 return;
1210 }
1211 const std::vector<uint8_t>* gc_map = verifier::DexVerifier::GetGcMap(ref);
1212 if (gc_map == NULL) {
1213 return;
1214 }
1215 compiled_method->SetGcMap(*gc_map);
1216}
1217
Logan Chien8b977d32012-02-21 19:14:55 +08001218#if defined(ART_USE_LLVM_COMPILER)
1219void Compiler::SetElfFileName(std::string const& filename) {
1220 compiler_llvm_->SetElfFileName(filename);
1221}
1222
1223void Compiler::SetBitcodeFileName(std::string const& filename) {
1224 compiler_llvm_->SetBitcodeFileName(filename);
1225}
1226#endif
1227
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001228} // namespace art