blob: 9d2b64cdaabf11cc79a500cb210180b6f82a38ea [file] [log] [blame]
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_COMPILER_H_
4#define ART_SRC_COMPILER_H_
5
Elliott Hughese5448b52012-01-18 16:44:06 -08006#include <map>
7
Brian Carlstrom0755ec52012-01-11 15:19:46 -08008#include "compiled_class.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -07009#include "compiled_method.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070010#include "constants.h"
Ian Rogersa3760aa2011-11-14 14:32:37 -080011#include "dex_cache.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070012#include "dex_file.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070013#include "jni_compiler.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070014#include "oat_file.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070015#include "object.h"
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070016#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070017#include "unordered_map.h"
Ian Rogersd6b1f612011-09-27 13:38:14 -070018
Brian Carlstromae826982011-11-09 01:33:42 -080019#include <set>
Ian Rogers0571d352011-11-03 19:51:38 -070020#include <string>
21
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070022namespace art {
23
24class Compiler {
25 public:
Brian Carlstromaded5f72011-10-07 17:15:04 -070026 // Create a compiler targeting the requested "instruction_set".
Brian Carlstromae826982011-11-09 01:33:42 -080027 // "image" should be true if image specific optimizations should be
28 // enabled. "image_classes" lets the compiler know what classes it
29 // can assume will be in the image, with NULL implying all available
30 // classes.
31 explicit Compiler(InstructionSet instruction_set,
32 bool image,
33 const std::set<std::string>* image_classes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070034
35 ~Compiler();
Ian Rogers2c8f6532011-09-02 17:16:34 -070036
Jesse Wilson254db0f2011-11-16 16:44:11 -050037 void CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -080038 const std::vector<const DexFile*>& dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -070039
40 // Compile a single Method
Brian Carlstrom3320cf42011-10-04 14:58:28 -070041 void CompileOne(const Method* method);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070042
Brian Carlstrom3320cf42011-10-04 14:58:28 -070043 InstructionSet GetInstructionSet() const {
44 return instruction_set_;
45 }
46
Brian Carlstromaded5f72011-10-07 17:15:04 -070047 bool IsImage() const {
48 return image_;
49 }
50
Brian Carlstrome24fa612011-09-29 00:53:55 -070051 // Stub to throw AbstractMethodError
Brian Carlstrome24fa612011-09-29 00:53:55 -070052 static ByteArray* CreateAbstractMethodErrorStub(InstructionSet instruction_set);
53
Brian Carlstrom3320cf42011-10-04 14:58:28 -070054
Ian Rogersad25ac52011-10-04 19:13:33 -070055 // Generate the trampoline that's invoked by unresolved direct methods
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070056 static ByteArray* CreateResolutionStub(InstructionSet instruction_set,
57 Runtime::TrampolineType type);
Ian Rogersad25ac52011-10-04 19:13:33 -070058
Ian Rogers169c9a72011-11-13 20:13:17 -080059 static ByteArray* CreateJniDlysmLookupStub(InstructionSet instruction_set);
60
Brian Carlstrom0755ec52012-01-11 15:19:46 -080061 // A class is uniquely located by its DexFile and the class_defs_ table index into that DexFile
62 typedef std::pair<const DexFile*, uint32_t> ClassReference;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080063 struct ClassReferenceHash {
64 size_t operator()(const ClassReference& id) const {
65 size_t dex = reinterpret_cast<size_t>(id.first);
66 DCHECK_NE(dex, static_cast<size_t>(0));
67 dex += 33; // dex is an aligned pointer, get some non-zero low bits
68 size_t idx = id.second;
69 if (idx == 0) { // special case of a method index of 0
70 return dex * 5381;
71 } else {
72 return dex * idx;
73 }
74 }
75 };
Brian Carlstrom0755ec52012-01-11 15:19:46 -080076 CompiledClass* GetCompiledClass(ClassReference ref) const;
Ian Rogers0571d352011-11-03 19:51:38 -070077
Brian Carlstrom0755ec52012-01-11 15:19:46 -080078 // A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile
79 typedef std::pair<const DexFile*, uint32_t> MethodReference;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080080 struct MethodReferenceHash {
81 size_t operator()(const MethodReference& id) const {
82 size_t dex = reinterpret_cast<size_t>(id.first);
83 DCHECK_NE(dex, static_cast<size_t>(0));
84 dex += 33; // dex is an aligned pointer, get some non-zero low bits
85 size_t idx = id.second;
86 if (idx == 0) { // special case of a method index of 0
87 return dex * 5381;
88 } else {
89 return dex * idx;
90 }
91 }
92 };
Ian Rogers0571d352011-11-03 19:51:38 -070093 CompiledMethod* GetCompiledMethod(MethodReference ref) const;
Brian Carlstrom0755ec52012-01-11 15:19:46 -080094
Ian Rogers0571d352011-11-03 19:51:38 -070095 const CompiledInvokeStub* FindInvokeStub(bool is_static, const char* shorty) const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070096
Ian Rogers28ad40d2011-10-27 15:19:26 -070097 // Callbacks from OAT/ART compiler to see what runtime checks must be generated
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080098 bool CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache, uint32_t type_idx) const;
Ian Rogersa3760aa2011-11-14 14:32:37 -080099 bool CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache, uint32_t string_idx) const {
Brian Carlstromae826982011-11-09 01:33:42 -0800100 // TODO: Add support for loading strings referenced by image_classes_
101 // See also Compiler::ResolveDexFile
102 return IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
Ian Rogers28ad40d2011-10-27 15:19:26 -0700103 }
Ian Rogersa3760aa2011-11-14 14:32:37 -0800104 bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
105 const DexFile& dex_file, uint32_t type_idx) const {
Brian Carlstromae826982011-11-09 01:33:42 -0800106 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700107 // We should never ask whether a type needs access checks to raise a verification error,
108 // all other cases where this following test could fail should have been rewritten by the
Ian Rogersa3760aa2011-11-14 14:32:37 -0800109 // verifier to verification errors. Also need to handle a lack of knowledge at compile time.
110#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -0800111 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
112 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800113 DCHECK(resolved_class == NULL || referrer_class == NULL ||
114 referrer_class->CanAccess(resolved_class));
115#endif
Ian Rogers28ad40d2011-10-27 15:19:26 -0700116 return resolved_class != NULL;
117 }
Ian Rogers0571d352011-11-03 19:51:38 -0700118
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700119 private:
Brian Carlstromae826982011-11-09 01:33:42 -0800120
121 // Checks if class specified by type_idx is one of the image_classes_
122 bool IsImageClass(const std::string& descriptor) const;
123
124 void PreCompile(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
125 void PostCompile(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
126
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700127 // Attempt to resolve all type, methods, fields, and strings
128 // referenced from code in the dex file following PathClassLoader
129 // ordering semantics.
Brian Carlstromae826982011-11-09 01:33:42 -0800130 void Resolve(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700131 void ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
132
Brian Carlstromae826982011-11-09 01:33:42 -0800133 void Verify(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
jeffhao98eacac2011-09-14 16:11:53 -0700134 void VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
135
Brian Carlstromae826982011-11-09 01:33:42 -0800136 void InitializeClassesWithoutClinit(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700137 void InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file);
138
Brian Carlstromae826982011-11-09 01:33:42 -0800139 void Compile(const ClassLoader* class_loader,
140 const std::vector<const DexFile*>& dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700141 void CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700142 void CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
143 const DexFile& dex_file);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800144 void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, uint32_t method_idx,
145 const ClassLoader* class_loader, const DexFile& dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700146
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800147 void SetGcMaps(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
148 void SetGcMapsDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
149 void SetGcMapsMethod(const DexFile& dex_file, Method* method);
150
Brian Carlstrom83db7722011-08-26 17:32:56 -0700151 // After compiling, walk all the DexCaches and set the code and
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700152 // method pointers of CodeAndDirectMethods entries in the DexCaches.
Brian Carlstromae826982011-11-09 01:33:42 -0800153 void SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700154 void SetCodeAndDirectMethodsDexFile(const DexFile& dex_file);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700155
Ian Rogers0571d352011-11-03 19:51:38 -0700156 void InsertInvokeStub(bool is_static, const char* shorty,
157 const CompiledInvokeStub* compiled_invoke_stub);
158
Ian Rogers2c8f6532011-09-02 17:16:34 -0700159 InstructionSet instruction_set_;
160 JniCompiler jni_compiler_;
161
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800162 typedef std::tr1::unordered_map<const ClassReference, CompiledClass*, ClassReferenceHash> ClassTable;
163 // All class references that this compiler has compiled
164 ClassTable compiled_classes_;
165
Ian Rogers0571d352011-11-03 19:51:38 -0700166 typedef std::tr1::unordered_map<const MethodReference, CompiledMethod*, MethodReferenceHash> MethodTable;
167 // All method references that this compiler has compiled
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700168 MethodTable compiled_methods_;
169
Elliott Hughese5448b52012-01-18 16:44:06 -0800170 typedef std::map<std::string, const CompiledInvokeStub*> InvokeStubTable;
Ian Rogers0571d352011-11-03 19:51:38 -0700171 // Invocation stubs created to allow invocation of the compiled methods
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700172 InvokeStubTable compiled_invoke_stubs_;
173
Brian Carlstromaded5f72011-10-07 17:15:04 -0700174 bool image_;
175
Brian Carlstromae826982011-11-09 01:33:42 -0800176 const std::set<std::string>* image_classes_;
177
Ian Rogers2c8f6532011-09-02 17:16:34 -0700178 DISALLOW_COPY_AND_ASSIGN(Compiler);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700179};
180
181} // namespace art
182
183#endif // ART_SRC_COMPILER_H_