Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_CLASS_LINKER_H_ |
| 4 | #define ART_SRC_CLASS_LINKER_H_ |
| 5 | |
| 6 | #include <map> |
| 7 | #include <utility> |
| 8 | #include <vector> |
| 9 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "macros.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame^] | 11 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 12 | #include "thread.h" |
| 13 | #include "object.h" |
| 14 | #include "gtest/gtest.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 15 | |
| 16 | namespace art { |
| 17 | |
| 18 | class ClassLinker { |
| 19 | public: |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 20 | // Initializes the class linker. |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 21 | static ClassLinker* Create(std::vector<RawDexFile*> boot_class_path); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 22 | |
| 23 | ~ClassLinker() {} |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 24 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame^] | 25 | DexCache* AllocDexCache(); |
| 26 | Class* AllocClass(DexCache* dex_cache); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 27 | StaticField* AllocStaticField(); |
| 28 | InstanceField* AllocInstanceField(); |
| 29 | Method* AllocMethod(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 30 | ObjectArray* AllocObjectArray(size_t length); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 31 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 32 | // Finds a class by its descriptor name. |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 33 | // If raw_dex_file is null, searches boot_class_path_. |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 34 | Class* FindClass(const StringPiece& descriptor, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 35 | Object* class_loader, |
| 36 | const RawDexFile* raw_dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 37 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 38 | Class* FindSystemClass(const StringPiece& descriptor) { |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 39 | return FindClass(descriptor, NULL, NULL); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 42 | bool InitializeClass(Class* klass); |
| 43 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 44 | Class* LookupClass(const StringPiece& descriptor, Object* class_loader); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 45 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 46 | Class* ResolveClass(const Class* referring, |
| 47 | uint32_t class_idx, |
| 48 | const RawDexFile* raw_dex_file); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 49 | |
| 50 | String* ResolveString(const Class* referring, uint32_t string_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 51 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 52 | void RegisterDexFile(RawDexFile* raw_dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 55 | ClassLinker() {} |
| 56 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 57 | void Init(std::vector<RawDexFile*> boot_class_path_); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 58 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 59 | Class* CreatePrimitiveClass(const StringPiece& descriptor); |
| 60 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 61 | Class* CreateArrayClass(const StringPiece& descriptor, |
| 62 | Object* class_loader, |
| 63 | const RawDexFile* raw_dex_file); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 64 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 65 | Class* FindPrimitiveClass(char type); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 66 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame^] | 67 | const RawDexFile* FindRawDexFile(const DexCache* dex_file) const; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 68 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame^] | 69 | DexCache* FindDexCache(const RawDexFile* raw_dex_file) const; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 70 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 71 | typedef std::pair<const RawDexFile*, const RawDexFile::ClassDef*> ClassPathEntry; |
| 72 | |
| 73 | void AppendToBootClassPath(RawDexFile* raw_dex_file); |
| 74 | |
| 75 | ClassPathEntry FindInBootClassPath(const StringPiece& descriptor); |
| 76 | |
| 77 | void LoadClass(const RawDexFile& raw_dex_file, |
| 78 | const RawDexFile::ClassDef& class_def, |
| 79 | Class* klass); |
| 80 | |
| 81 | void LoadInterfaces(const RawDexFile& raw_dex_file, |
| 82 | const RawDexFile::ClassDef& class_def, |
| 83 | Class *klass); |
| 84 | |
| 85 | void LoadField(const RawDexFile& raw_dex_file, |
| 86 | const RawDexFile::Field& src, |
| 87 | Class* klass, |
| 88 | Field* dst); |
| 89 | |
| 90 | void LoadMethod(const RawDexFile& raw_dex_file, |
| 91 | const RawDexFile::Method& src, |
| 92 | Class* klass, |
| 93 | Method* dst); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 94 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 95 | // Inserts a class into the class table. Returns true if the class |
| 96 | // was inserted. |
| 97 | bool InsertClass(Class* klass); |
| 98 | |
| 99 | bool InitializeSuperClass(Class* klass); |
| 100 | |
| 101 | void InitializeStaticFields(Class* klass); |
| 102 | |
| 103 | bool ValidateSuperClassDescriptors(const Class* klass); |
| 104 | |
| 105 | bool HasSameDescriptorClasses(const char* descriptor, |
| 106 | const Class* klass1, |
| 107 | const Class* klass2); |
| 108 | |
| 109 | bool HasSameMethodDescriptorClasses(const Method* descriptor, |
| 110 | const Class* klass1, |
| 111 | const Class* klass2); |
| 112 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 113 | bool HasSameNameAndPrototype(const Method* m1, const Method* m2) const { |
| 114 | return HasSameName(m1, m2) && HasSamePrototype(m1, m2); |
| 115 | } |
| 116 | |
| 117 | bool HasSameName(const Method* m1, const Method* m2) const { |
| 118 | return m1->GetName() == m2->GetName(); |
| 119 | } |
| 120 | |
| 121 | bool HasSamePrototype(const Method* m1, const Method* m2) const { |
| 122 | return HasSameReturnType(m1, m2) && HasSameArgumentTypes(m1, m2); |
| 123 | } |
| 124 | |
| 125 | bool HasSameReturnType(const Method* m1, const Method* m2) const; |
| 126 | |
| 127 | bool HasSameArgumentTypes(const Method* m1, const Method* m2) const; |
| 128 | |
| 129 | bool LinkClass(Class* klass, const RawDexFile* raw_dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 130 | |
| 131 | bool LinkSuperClass(Class* klass); |
| 132 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 133 | bool LinkInterfaces(Class* klass, const RawDexFile* raw_dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 134 | |
| 135 | bool LinkMethods(Class* klass); |
| 136 | |
| 137 | bool LinkVirtualMethods(Class* klass); |
| 138 | |
| 139 | bool LinkInterfaceMethods(Class* klass); |
| 140 | |
| 141 | void LinkAbstractMethods(Class* klass); |
| 142 | |
| 143 | bool LinkInstanceFields(Class* klass); |
| 144 | |
| 145 | void CreateReferenceOffsets(Class* klass); |
| 146 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 147 | std::vector<RawDexFile*> boot_class_path_; |
| 148 | |
| 149 | std::vector<RawDexFile*> raw_dex_files_; |
| 150 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame^] | 151 | std::vector<DexCache*> dex_caches_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 152 | |
| 153 | // TODO: multimap |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 154 | typedef std::map<const StringPiece, Class*> Table; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 155 | |
| 156 | Table classes_; |
| 157 | |
| 158 | Mutex* classes_lock_; |
| 159 | |
| 160 | // TODO: classpath |
| 161 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 162 | Class* java_lang_Class_; |
| 163 | Class* java_lang_Object_; |
| 164 | Class* java_lang_ref_Field_; |
| 165 | Class* java_lang_ref_Method_; |
| 166 | Class* java_lang_Cloneable_; |
| 167 | Class* java_io_Serializable_; |
| 168 | Class* java_lang_String_; |
| 169 | |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 170 | Class* primitive_boolean_; |
| 171 | Class* primitive_char_; |
| 172 | Class* primitive_float_; |
| 173 | Class* primitive_double_; |
| 174 | Class* primitive_byte_; |
| 175 | Class* primitive_short_; |
| 176 | Class* primitive_int_; |
| 177 | Class* primitive_long_; |
| 178 | Class* primitive_void_; |
| 179 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 180 | Class* object_array_class_; |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 181 | Class* char_array_class_; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 182 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 183 | FRIEND_TEST(ClassLinkerTest, ProtoCompare); |
| 184 | FRIEND_TEST(ClassLinkerTest, ProtoCompare2); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 185 | DISALLOW_COPY_AND_ASSIGN(ClassLinker); |
| 186 | }; |
| 187 | |
| 188 | } // namespace art |
| 189 | |
| 190 | #endif // ART_SRC_CLASS_LINKER_H_ |