blob: f59587f270749b4e93dd91293b2b16aa30df3475 [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
Brian Carlstrom0755ec52012-01-11 15:19:46 -08006#include "compiled_class.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -07007#include "compiled_method.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -07008#include "constants.h"
Ian Rogersa3760aa2011-11-14 14:32:37 -08009#include "dex_cache.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070010#include "dex_file.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070011#include "jni_compiler.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070012#include "oat_file.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070013#include "object.h"
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070014#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070015#include "unordered_map.h"
Ian Rogersd6b1f612011-09-27 13:38:14 -070016
Brian Carlstromae826982011-11-09 01:33:42 -080017#include <set>
Ian Rogers0571d352011-11-03 19:51:38 -070018#include <string>
19
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070020namespace art {
21
22class Compiler {
23 public:
Brian Carlstromaded5f72011-10-07 17:15:04 -070024 // Create a compiler targeting the requested "instruction_set".
Brian Carlstromae826982011-11-09 01:33:42 -080025 // "image" should be true if image specific optimizations should be
26 // enabled. "image_classes" lets the compiler know what classes it
27 // can assume will be in the image, with NULL implying all available
28 // classes.
29 explicit Compiler(InstructionSet instruction_set,
30 bool image,
31 const std::set<std::string>* image_classes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070032
33 ~Compiler();
Ian Rogers2c8f6532011-09-02 17:16:34 -070034
Jesse Wilson254db0f2011-11-16 16:44:11 -050035 void CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -080036 const std::vector<const DexFile*>& dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -070037
38 // Compile a single Method
Brian Carlstrom3320cf42011-10-04 14:58:28 -070039 void CompileOne(const Method* method);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070040
Brian Carlstrom3320cf42011-10-04 14:58:28 -070041 InstructionSet GetInstructionSet() const {
42 return instruction_set_;
43 }
44
Brian Carlstromaded5f72011-10-07 17:15:04 -070045 bool IsImage() const {
46 return image_;
47 }
48
Brian Carlstrome24fa612011-09-29 00:53:55 -070049 // Stub to throw AbstractMethodError
Brian Carlstrome24fa612011-09-29 00:53:55 -070050 static ByteArray* CreateAbstractMethodErrorStub(InstructionSet instruction_set);
51
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052
Ian Rogersad25ac52011-10-04 19:13:33 -070053 // Generate the trampoline that's invoked by unresolved direct methods
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070054 static ByteArray* CreateResolutionStub(InstructionSet instruction_set,
55 Runtime::TrampolineType type);
Ian Rogersad25ac52011-10-04 19:13:33 -070056
Ian Rogers169c9a72011-11-13 20:13:17 -080057 static ByteArray* CreateJniDlysmLookupStub(InstructionSet instruction_set);
58
Brian Carlstrom0755ec52012-01-11 15:19:46 -080059 // A class is uniquely located by its DexFile and the class_defs_ table index into that DexFile
60 typedef std::pair<const DexFile*, uint32_t> ClassReference;
61 CompiledClass* GetCompiledClass(ClassReference ref) const;
Ian Rogers0571d352011-11-03 19:51:38 -070062
Brian Carlstrom0755ec52012-01-11 15:19:46 -080063 // A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile
64 typedef std::pair<const DexFile*, uint32_t> MethodReference;
Ian Rogers0571d352011-11-03 19:51:38 -070065 CompiledMethod* GetCompiledMethod(MethodReference ref) const;
Brian Carlstrom0755ec52012-01-11 15:19:46 -080066
Ian Rogers0571d352011-11-03 19:51:38 -070067 const CompiledInvokeStub* FindInvokeStub(bool is_static, const char* shorty) const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070068
Ian Rogers28ad40d2011-10-27 15:19:26 -070069 // Callbacks from OAT/ART compiler to see what runtime checks must be generated
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080070 bool CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache, uint32_t type_idx) const;
Ian Rogersa3760aa2011-11-14 14:32:37 -080071 bool CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache, uint32_t string_idx) const {
Brian Carlstromae826982011-11-09 01:33:42 -080072 // TODO: Add support for loading strings referenced by image_classes_
73 // See also Compiler::ResolveDexFile
74 return IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
Ian Rogers28ad40d2011-10-27 15:19:26 -070075 }
Ian Rogersa3760aa2011-11-14 14:32:37 -080076 bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
77 const DexFile& dex_file, uint32_t type_idx) const {
Brian Carlstromae826982011-11-09 01:33:42 -080078 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -070079 // We should never ask whether a type needs access checks to raise a verification error,
80 // all other cases where this following test could fail should have been rewritten by the
Ian Rogersa3760aa2011-11-14 14:32:37 -080081 // verifier to verification errors. Also need to handle a lack of knowledge at compile time.
82#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -080083 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
84 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
Ian Rogersa3760aa2011-11-14 14:32:37 -080085 DCHECK(resolved_class == NULL || referrer_class == NULL ||
86 referrer_class->CanAccess(resolved_class));
87#endif
Ian Rogers28ad40d2011-10-27 15:19:26 -070088 return resolved_class != NULL;
89 }
Ian Rogers0571d352011-11-03 19:51:38 -070090
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070091 private:
Brian Carlstromae826982011-11-09 01:33:42 -080092
93 // Checks if class specified by type_idx is one of the image_classes_
94 bool IsImageClass(const std::string& descriptor) const;
95
96 void PreCompile(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
97 void PostCompile(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
98
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070099 // Attempt to resolve all type, methods, fields, and strings
100 // referenced from code in the dex file following PathClassLoader
101 // ordering semantics.
Brian Carlstromae826982011-11-09 01:33:42 -0800102 void Resolve(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103 void ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
104
Brian Carlstromae826982011-11-09 01:33:42 -0800105 void Verify(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
jeffhao98eacac2011-09-14 16:11:53 -0700106 void VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
107
Brian Carlstromae826982011-11-09 01:33:42 -0800108 void InitializeClassesWithoutClinit(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700109 void InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file);
110
Brian Carlstromae826982011-11-09 01:33:42 -0800111 void Compile(const ClassLoader* class_loader,
112 const std::vector<const DexFile*>& dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700113 void CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700114 void CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
115 const DexFile& dex_file);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800116 void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, uint32_t method_idx,
117 const ClassLoader* class_loader, const DexFile& dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700118
119 // After compiling, walk all the DexCaches and set the code and
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700120 // method pointers of CodeAndDirectMethods entries in the DexCaches.
Brian Carlstromae826982011-11-09 01:33:42 -0800121 void SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700122 void SetCodeAndDirectMethodsDexFile(const DexFile& dex_file);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700123
Ian Rogers0571d352011-11-03 19:51:38 -0700124 void InsertInvokeStub(bool is_static, const char* shorty,
125 const CompiledInvokeStub* compiled_invoke_stub);
126
Ian Rogers2c8f6532011-09-02 17:16:34 -0700127 InstructionSet instruction_set_;
128 JniCompiler jni_compiler_;
129
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800130 struct ClassReferenceHash {
131 size_t operator()(const ClassReference& id) const {
132 size_t dex = reinterpret_cast<size_t>(id.first);
133 DCHECK_NE(dex, static_cast<size_t>(0));
134 dex += 33; // dex is an aligned pointer, get some non-zero low bits
135 size_t idx = id.second;
136 if (idx == 0) { // special case of a method index of 0
137 return dex * 5381;
138 } else {
139 return dex * idx;
140 }
141 }
142 };
143 typedef std::tr1::unordered_map<const ClassReference, CompiledClass*, ClassReferenceHash> ClassTable;
144 // All class references that this compiler has compiled
145 ClassTable compiled_classes_;
146
Ian Rogers0571d352011-11-03 19:51:38 -0700147 struct MethodReferenceHash {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800148 size_t operator()(const MethodReference& id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700149 size_t dex = reinterpret_cast<size_t>(id.first);
150 DCHECK_NE(dex, static_cast<size_t>(0));
151 dex += 33; // dex is an aligned pointer, get some non-zero low bits
152 size_t idx = id.second;
153 if (idx == 0) { // special case of a method index of 0
154 return dex * 5381;
155 } else {
156 return dex * idx;
157 }
158 }
159 };
160 typedef std::tr1::unordered_map<const MethodReference, CompiledMethod*, MethodReferenceHash> MethodTable;
161 // All method references that this compiler has compiled
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700162 MethodTable compiled_methods_;
163
Ian Rogers0571d352011-11-03 19:51:38 -0700164 typedef std::tr1::unordered_map<std::string, const CompiledInvokeStub*> InvokeStubTable;
165 // Invocation stubs created to allow invocation of the compiled methods
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700166 InvokeStubTable compiled_invoke_stubs_;
167
Brian Carlstromaded5f72011-10-07 17:15:04 -0700168 bool image_;
169
Brian Carlstromae826982011-11-09 01:33:42 -0800170 const std::set<std::string>* image_classes_;
171
Ian Rogers2c8f6532011-09-02 17:16:34 -0700172 DISALLOW_COPY_AND_ASSIGN(Compiler);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700173};
174
175} // namespace art
176
177#endif // ART_SRC_COMPILER_H_