blob: 5203ffb98e293cc62349d2847cf54d7e4ab59567 [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 Carlstrom3320cf42011-10-04 14:58:28 -07006#include "compiled_method.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -07007#include "constants.h"
Ian Rogersa3760aa2011-11-14 14:32:37 -08008#include "dex_cache.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07009#include "dex_file.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070010#include "jni_compiler.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070011#include "oat_file.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070012#include "object.h"
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070013#include "runtime.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070014#include "unordered_map.h"
Ian Rogersd6b1f612011-09-27 13:38:14 -070015
Brian Carlstromae826982011-11-09 01:33:42 -080016#include <set>
Ian Rogers0571d352011-11-03 19:51:38 -070017#include <string>
18
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070019namespace art {
20
21class Compiler {
22 public:
Brian Carlstromaded5f72011-10-07 17:15:04 -070023 // Create a compiler targeting the requested "instruction_set".
Brian Carlstromae826982011-11-09 01:33:42 -080024 // "image" should be true if image specific optimizations should be
25 // enabled. "image_classes" lets the compiler know what classes it
26 // can assume will be in the image, with NULL implying all available
27 // classes.
28 explicit Compiler(InstructionSet instruction_set,
29 bool image,
30 const std::set<std::string>* image_classes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031
32 ~Compiler();
Ian Rogers2c8f6532011-09-02 17:16:34 -070033
Jesse Wilson254db0f2011-11-16 16:44:11 -050034 void CompileAll(const ClassLoader* class_loader,
Brian Carlstromae826982011-11-09 01:33:42 -080035 const std::vector<const DexFile*>& dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -070036
37 // Compile a single Method
Brian Carlstrom3320cf42011-10-04 14:58:28 -070038 void CompileOne(const Method* method);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070039
Brian Carlstrom3320cf42011-10-04 14:58:28 -070040 InstructionSet GetInstructionSet() const {
41 return instruction_set_;
42 }
43
Brian Carlstromaded5f72011-10-07 17:15:04 -070044 bool IsImage() const {
45 return image_;
46 }
47
48 void SetVerbose(bool verbose) {
49 verbose_ = verbose;
50 }
51
Brian Carlstrom16192862011-09-12 17:50:06 -070052 bool IsVerbose() const {
53 return verbose_;
54 }
55
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 // Stub to throw AbstractMethodError
Brian Carlstrome24fa612011-09-29 00:53:55 -070057 static ByteArray* CreateAbstractMethodErrorStub(InstructionSet instruction_set);
58
Brian Carlstrom3320cf42011-10-04 14:58:28 -070059
Ian Rogersad25ac52011-10-04 19:13:33 -070060 // Generate the trampoline that's invoked by unresolved direct methods
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070061 static ByteArray* CreateResolutionStub(InstructionSet instruction_set,
62 Runtime::TrampolineType type);
Ian Rogersad25ac52011-10-04 19:13:33 -070063
Ian Rogers169c9a72011-11-13 20:13:17 -080064 static ByteArray* CreateJniDlysmLookupStub(InstructionSet instruction_set);
65
Ian Rogers0571d352011-11-03 19:51:38 -070066 // A method is uniquely located by its DexFile and index into the method_id table of that dex file
67 typedef std::pair<const DexFile*, uint32_t> MethodReference;
68
69 CompiledMethod* GetCompiledMethod(MethodReference ref) const;
70 const CompiledInvokeStub* FindInvokeStub(bool is_static, const char* shorty) const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070071
Ian Rogers28ad40d2011-10-27 15:19:26 -070072 // Callbacks from OAT/ART compiler to see what runtime checks must be generated
Ian Rogersa3760aa2011-11-14 14:32:37 -080073 bool CanAssumeTypeIsPresentInDexCache(const DexCache* dex_cache, uint32_t type_idx) const {
Brian Carlstromae826982011-11-09 01:33:42 -080074 if (!IsImage()) {
75 return false;
76 }
77 Class* resolved_class = dex_cache->GetResolvedTypes()->Get(type_idx);
78 if (resolved_class == NULL) {
79 return false;
80 }
81 return IsImageClass(resolved_class->GetDescriptor()->ToModifiedUtf8());
Ian Rogers28ad40d2011-10-27 15:19:26 -070082 }
Ian Rogersa3760aa2011-11-14 14:32:37 -080083 bool CanAssumeStringIsPresentInDexCache(const DexCache* dex_cache, uint32_t string_idx) const {
Brian Carlstromae826982011-11-09 01:33:42 -080084 // TODO: Add support for loading strings referenced by image_classes_
85 // See also Compiler::ResolveDexFile
86 return IsImage() && image_classes_ == NULL && dex_cache->GetResolvedString(string_idx) != NULL;
Ian Rogers28ad40d2011-10-27 15:19:26 -070087 }
Ian Rogersa3760aa2011-11-14 14:32:37 -080088 bool CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexCache* dex_cache,
89 const DexFile& dex_file, uint32_t type_idx) const {
Brian Carlstromae826982011-11-09 01:33:42 -080090 Class* resolved_class = dex_cache->GetResolvedType(type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -070091 // We should never ask whether a type needs access checks to raise a verification error,
92 // all other cases where this following test could fail should have been rewritten by the
Ian Rogersa3760aa2011-11-14 14:32:37 -080093 // verifier to verification errors. Also need to handle a lack of knowledge at compile time.
94#ifndef NDEBUG
Brian Carlstromae826982011-11-09 01:33:42 -080095 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
96 Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
Ian Rogersa3760aa2011-11-14 14:32:37 -080097 DCHECK(resolved_class == NULL || referrer_class == NULL ||
98 referrer_class->CanAccess(resolved_class));
99#endif
Ian Rogers28ad40d2011-10-27 15:19:26 -0700100 return resolved_class != NULL;
101 }
Ian Rogers0571d352011-11-03 19:51:38 -0700102
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103 private:
Brian Carlstromae826982011-11-09 01:33:42 -0800104
105 // Checks if class specified by type_idx is one of the image_classes_
106 bool IsImageClass(const std::string& descriptor) const;
107
108 void PreCompile(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
109 void PostCompile(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
110
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700111 // Attempt to resolve all type, methods, fields, and strings
112 // referenced from code in the dex file following PathClassLoader
113 // ordering semantics.
Brian Carlstromae826982011-11-09 01:33:42 -0800114 void Resolve(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700115 void ResolveDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
116
Brian Carlstromae826982011-11-09 01:33:42 -0800117 void Verify(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
jeffhao98eacac2011-09-14 16:11:53 -0700118 void VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
119
Brian Carlstromae826982011-11-09 01:33:42 -0800120 void InitializeClassesWithoutClinit(const ClassLoader* class_loader, const std::vector<const DexFile*>& dex_files);
Brian Carlstroma5a97a22011-09-15 14:08:49 -0700121 void InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file);
122
Brian Carlstromae826982011-11-09 01:33:42 -0800123 void Compile(const ClassLoader* class_loader,
124 const std::vector<const DexFile*>& dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700125 void CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700126 void CompileClass(const DexFile::ClassDef& class_def, const ClassLoader* class_loader,
127 const DexFile& dex_file);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800128 void CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, uint32_t method_idx,
129 const ClassLoader* class_loader, const DexFile& dex_file);
Brian Carlstrom83db7722011-08-26 17:32:56 -0700130
131 // After compiling, walk all the DexCaches and set the code and
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700132 // method pointers of CodeAndDirectMethods entries in the DexCaches.
Brian Carlstromae826982011-11-09 01:33:42 -0800133 void SetCodeAndDirectMethods(const std::vector<const DexFile*>& dex_files);
Brian Carlstrom8a487412011-08-29 20:08:52 -0700134 void SetCodeAndDirectMethodsDexFile(const DexFile& dex_file);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700135
Ian Rogers0571d352011-11-03 19:51:38 -0700136 void InsertInvokeStub(bool is_static, const char* shorty,
137 const CompiledInvokeStub* compiled_invoke_stub);
138
Ian Rogers2c8f6532011-09-02 17:16:34 -0700139 InstructionSet instruction_set_;
140 JniCompiler jni_compiler_;
141
Ian Rogers0571d352011-11-03 19:51:38 -0700142 struct MethodReferenceHash {
143 size_t operator()(const MethodReference id) const {
144 size_t dex = reinterpret_cast<size_t>(id.first);
145 DCHECK_NE(dex, static_cast<size_t>(0));
146 dex += 33; // dex is an aligned pointer, get some non-zero low bits
147 size_t idx = id.second;
148 if (idx == 0) { // special case of a method index of 0
149 return dex * 5381;
150 } else {
151 return dex * idx;
152 }
153 }
154 };
155 typedef std::tr1::unordered_map<const MethodReference, CompiledMethod*, MethodReferenceHash> MethodTable;
156 // All method references that this compiler has compiled
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700157 MethodTable compiled_methods_;
158
Ian Rogers0571d352011-11-03 19:51:38 -0700159 typedef std::tr1::unordered_map<std::string, const CompiledInvokeStub*> InvokeStubTable;
160 // Invocation stubs created to allow invocation of the compiled methods
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700161 InvokeStubTable compiled_invoke_stubs_;
162
Brian Carlstromaded5f72011-10-07 17:15:04 -0700163 bool image_;
164
Brian Carlstromae826982011-11-09 01:33:42 -0800165 const std::set<std::string>* image_classes_;
166
Brian Carlstrom16192862011-09-12 17:50:06 -0700167 bool verbose_;
168
Ian Rogers2c8f6532011-09-02 17:16:34 -0700169 DISALLOW_COPY_AND_ASSIGN(Compiler);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700170};
171
172} // namespace art
173
174#endif // ART_SRC_COMPILER_H_