Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4 | |
| 5 | #include <vector> |
| 6 | #include <utility> |
| 7 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 8 | #include "casts.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 9 | #include "dex_cache.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "dex_verifier.h" |
| 11 | #include "heap.h" |
| 12 | #include "logging.h" |
| 13 | #include "monitor.h" |
| 14 | #include "object.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 15 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 16 | #include "scoped_ptr.h" |
| 17 | #include "thread.h" |
| 18 | #include "utils.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 19 | |
| 20 | namespace art { |
| 21 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 22 | ClassLinker* ClassLinker::Create(const std::vector<DexFile*>& boot_class_path) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 23 | scoped_ptr<ClassLinker> class_linker(new ClassLinker); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 24 | class_linker->Init(boot_class_path); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 25 | // TODO: check for failure during initialization |
| 26 | return class_linker.release(); |
| 27 | } |
| 28 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 29 | void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path) { |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 30 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 31 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 32 | // java_lang_Class comes first, its needed for AllocClass |
| 33 | Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class))); |
| 34 | CHECK(java_lang_Class != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 35 | // java_lang_Class->descriptor_ = "Ljava/lang/Class;"; // XXX bdc can these be created later? |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 36 | java_lang_Class->object_size_ = sizeof(Class); |
| 37 | java_lang_Class->klass_ = java_lang_Class; |
| 38 | // AllocClass(Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 39 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 40 | // java_lang_Object comes next so that object_array_class can be created |
| 41 | Class* java_lang_Object = AllocClass(java_lang_Class); |
| 42 | CHECK(java_lang_Object != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 43 | // java_lang_Object->descriptor_ = "Ljava/lang/Object;"; // XXX bdc can these be created later? |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 44 | // backfill Object as the super class of Class |
| 45 | java_lang_Class->super_class_ = java_lang_Object; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 46 | // mark as non-primitive for object_array_class |
| 47 | java_lang_Object->primitive_type_ = Class::kPrimNot; |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 48 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 49 | // object_array_class is for root_classes to provide the storage for these classes |
| 50 | Class* object_array_class = AllocClass(java_lang_Class); |
| 51 | CHECK(object_array_class != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 52 | // object_array_class->descriptor_ = "[Ljava/lang/Object;"; // XXX bdc can these be created later? |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 53 | object_array_class->component_type_ = java_lang_Object; |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 54 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 55 | // String and char[] are necessary so that FindClass can assign names to members |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 56 | Class* java_lang_String = AllocClass(java_lang_Class); |
| 57 | CHECK(java_lang_String != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 58 | // java_lang_String->descriptor_ = "Ljava/lang/String;"; // XXX can these be created later? |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 59 | CHECK_LT(java_lang_String->object_size_, sizeof(String)); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 60 | java_lang_String->object_size_ = sizeof(String); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 61 | String::InitClass(java_lang_String); |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 62 | Class* char_array_class = AllocClass(java_lang_Class); |
| 63 | CHECK(char_array_class != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 64 | // char_array_class->descriptor_ = "[C"; // XXX can these be created later? |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 65 | CharArray::SetArrayClass(char_array_class); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 66 | // Now String::Alloc* can be used |
| 67 | |
| 68 | // backfill Class descriptors missing until this point |
| 69 | java_lang_Class->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Class;"); |
| 70 | java_lang_Object->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Object;"); |
| 71 | object_array_class->descriptor_ = String::AllocFromModifiedUtf8("[Ljava/lang/Object;"); |
| 72 | java_lang_String->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/String;"); |
| 73 | char_array_class->descriptor_ = String::AllocFromModifiedUtf8("[C"); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 74 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 75 | // int[] and long[] are used for static field storage |
| 76 | Class* int_array_class = AllocClass(java_lang_Class); |
| 77 | CHECK(int_array_class != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 78 | int_array_class->descriptor_ = String::AllocFromModifiedUtf8("[I"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 79 | IntArray::SetArrayClass(int_array_class); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 80 | Class* long_array_class = AllocClass(java_lang_Class); |
| 81 | CHECK(long_array_class != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 82 | long_array_class->descriptor_ = String::AllocFromModifiedUtf8("[J"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 83 | LongArray::SetArrayClass(long_array_class); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 84 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 85 | // Field and Method are necessary so that FindClass can link members |
| 86 | Class* java_lang_reflect_Field = AllocClass(java_lang_Class); |
| 87 | CHECK(java_lang_reflect_Field != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 88 | java_lang_reflect_Field->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;"); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 89 | CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field)); |
| 90 | java_lang_reflect_Field->object_size_ = sizeof(Field); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 91 | Class* java_lang_reflect_Method = AllocClass(java_lang_Class); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 92 | java_lang_reflect_Method->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;"); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 93 | CHECK(java_lang_reflect_Method != NULL); |
| 94 | CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method)); |
| 95 | java_lang_reflect_Method->object_size_ = sizeof(Method); |
| 96 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 97 | // create storage for root classes, save away our work so far |
| 98 | class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax); |
| 99 | class_roots_->Set(kJavaLangClass, java_lang_Class); |
| 100 | class_roots_->Set(kJavaLangObject, java_lang_Object); |
| 101 | class_roots_->Set(kObjectArrayClass, object_array_class); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 102 | class_roots_->Set(kJavaLangString, java_lang_String); |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 103 | class_roots_->Set(kCharArrayClass, char_array_class); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 104 | class_roots_->Set(kIntArrayClass, int_array_class); |
| 105 | class_roots_->Set(kLongArrayClass, long_array_class); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 106 | class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field); |
| 107 | class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 108 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 109 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 110 | // setup boot_class_path_ now that we can use AllocObjectArray to |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 111 | // create DexCache instances |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 112 | for (size_t i = 0; i != boot_class_path.size(); ++i) { |
| 113 | AppendToBootClassPath(boot_class_path[i]); |
| 114 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 115 | // now we can use FindSystemClass, at least for non-arrays classes. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 116 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 117 | // run Class, Field, and Method through FindSystemClass. |
| 118 | // this initializes their dex_cache_ fields and register them in classes_. |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 119 | // we also override their object_size_ values to accommodate the extra C++ fields. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 120 | Class* Class_class = FindSystemClass("Ljava/lang/Class;"); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 121 | CHECK_EQ(java_lang_Class, Class_class); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 122 | CHECK_LT(java_lang_Class->object_size_, sizeof(Class)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 123 | java_lang_Class->object_size_ = sizeof(Class); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 124 | Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;"); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 125 | CHECK_EQ(java_lang_reflect_Field, Field_class); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 126 | CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field)); |
| 127 | java_lang_reflect_Field->object_size_ = sizeof(Field); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 128 | Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;"); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 129 | CHECK_EQ(java_lang_reflect_Method, Method_class); |
| 130 | CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 131 | java_lang_reflect_Method->object_size_ = sizeof(Method); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 132 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 133 | // Object and String just need more minimal setup, since they do not have extra C++ fields. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 134 | Class* Object_class = FindSystemClass("Ljava/lang/Object;"); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 135 | CHECK_EQ(java_lang_Object, Object_class); |
| 136 | CHECK_EQ(java_lang_Object->object_size_, sizeof(Object)); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 137 | Class* String_class = FindSystemClass("Ljava/lang/String;"); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 138 | CHECK_EQ(java_lang_String, String_class); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 139 | CHECK_EQ(java_lang_String->object_size_, sizeof(String)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 140 | |
| 141 | // Setup the ClassLoaders, adjusting the object_size_ as necessary |
| 142 | Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;"); |
| 143 | CHECK(java_lang_ClassLoader != NULL); |
| 144 | CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader)); |
| 145 | java_lang_ClassLoader->object_size_ = sizeof(ClassLoader); |
| 146 | class_roots_->Set(kJavaLangClassLoader, java_lang_ClassLoader); |
| 147 | Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;"); |
| 148 | CHECK(dalvik_system_BaseDexClassLoader != NULL); |
| 149 | CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader)); |
| 150 | class_roots_->Set(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader); |
| 151 | Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;"); |
| 152 | CHECK(dalvik_system_PathClassLoader != NULL); |
| 153 | CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader)); |
| 154 | class_roots_->Set(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 155 | |
| 156 | // Setup a single, global copy of "interfaces" and "iftable" for |
| 157 | // reuse across array classes |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 158 | Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;"); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 159 | CHECK(java_lang_Cloneable != NULL); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 160 | Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;"); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 161 | CHECK(java_io_Serializable != NULL); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 162 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 163 | array_interfaces_ = AllocObjectArray<Class>(2); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 164 | CHECK(array_interfaces_ != NULL); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 165 | array_interfaces_->Set(0, java_lang_Cloneable); |
| 166 | array_interfaces_->Set(1, java_io_Serializable); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 167 | |
| 168 | // We assume that Cloneable/Serializable don't have superinterfaces -- |
| 169 | // normally we'd have to crawl up and explicitly list all of the |
| 170 | // supers as well. These interfaces don't have any methods, so we |
| 171 | // don't have to worry about the ifviPool either. |
| 172 | array_iftable_ = new InterfaceEntry[2]; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 173 | array_iftable_[0].SetClass(array_interfaces_->Get(0)); |
| 174 | array_iftable_[1].SetClass(array_interfaces_->Get(1)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 175 | // now FindClass can be used for non-primitive array classes |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 176 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 177 | // run Object[] through FindClass to complete initialization |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 178 | Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;"); |
| 179 | CHECK_EQ(object_array_class, found_object_array_class); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 180 | CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0)); |
| 181 | CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1)); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 182 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 183 | // Setup the primitive type classes. |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 184 | class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z")); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 185 | class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B")); |
| 186 | class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C")); |
| 187 | class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D")); |
| 188 | class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F")); |
| 189 | class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I")); |
| 190 | class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J")); |
| 191 | class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S")); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 192 | class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V")); |
| 193 | // now we can use FindSystemClass for anything, including for "[C" |
| 194 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 195 | // run char[], int[] and long[] through FindClass to complete initialization |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 196 | Class* found_char_array_class = FindSystemClass("[C"); |
| 197 | CHECK_EQ(char_array_class, found_char_array_class); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 198 | Class* found_int_array_class = FindSystemClass("[I"); |
| 199 | CHECK_EQ(int_array_class, found_int_array_class); |
| 200 | Class* found_long_array_class = FindSystemClass("[J"); |
| 201 | CHECK_EQ(long_array_class, found_long_array_class); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 202 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 203 | // Initialize all the other primitive array types for PrimitiveArray::Alloc. |
| 204 | // These are easy because everything we need has already been set up. |
| 205 | class_roots_->Set(kBooleanArrayClass, FindSystemClass("[Z")); |
| 206 | class_roots_->Set(kByteArrayClass, FindSystemClass("[B")); |
| 207 | class_roots_->Set(kDoubleArrayClass, FindSystemClass("[D")); |
| 208 | class_roots_->Set(kFloatArrayClass, FindSystemClass("[F")); |
| 209 | class_roots_->Set(kShortArrayClass, FindSystemClass("[S")); |
| 210 | BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 211 | ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 212 | DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 213 | FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 214 | ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 215 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 216 | // ensure all class_roots_ were initialized |
| 217 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 218 | CHECK(GetClassRoot(static_cast<ClassRoot>(i))); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 222 | // that Object, Class, and Object[] are setup |
| 223 | init_done_ = true; |
| 224 | } |
| 225 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 226 | void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) { |
Brian Carlstrom | b88e944 | 2011-07-28 15:15:51 -0700 | [diff] [blame] | 227 | root_visitor(class_roots_, arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 228 | |
| 229 | for (size_t i = 0; i < dex_caches_.size(); i++) { |
Brian Carlstrom | b88e944 | 2011-07-28 15:15:51 -0700 | [diff] [blame] | 230 | root_visitor(dex_caches_[i], arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 233 | { |
| 234 | MutexLock mu(classes_lock_); |
| 235 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 236 | for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) { |
| 237 | root_visitor(it->second, arg); |
| 238 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 239 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 240 | |
| 241 | intern_table_.VisitRoots(root_visitor, arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 242 | |
Brian Carlstrom | b88e944 | 2011-07-28 15:15:51 -0700 | [diff] [blame] | 243 | root_visitor(array_interfaces_, arg); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 246 | DexCache* ClassLinker::AllocDexCache() { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 247 | return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 250 | Class* ClassLinker::AllocClass(Class* java_lang_Class) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 251 | return java_lang_Class->NewInstance()->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | Class* ClassLinker::AllocClass() { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 255 | return AllocClass(GetClassRoot(kJavaLangClass)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 258 | Field* ClassLinker::AllocField() { |
| 259 | return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->NewInstance()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | Method* ClassLinker::AllocMethod() { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 263 | return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->NewInstance()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 266 | // TODO: remove once we can use java.lang.Class.getSystemClassLoader |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 267 | PathClassLoader* ClassLinker::AllocPathClassLoader(std::vector<const DexFile*> dex_files) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 268 | PathClassLoader* cl = down_cast<PathClassLoader*>(GetClassRoot(kDalvikSystemPathClassLoader)->NewInstance()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 269 | cl->SetClassPath(dex_files); |
| 270 | return cl; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 273 | Class* ClassLinker::FindClass(const StringPiece& descriptor, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 274 | ClassLoader* class_loader) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 275 | // TODO: remove this contrived parent class loader check when we have a real ClassLoader. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 276 | if (class_loader != NULL) { |
| 277 | Class* klass = FindClass(descriptor, NULL); |
| 278 | if (klass != NULL) { |
| 279 | return klass; |
| 280 | } |
| 281 | } |
| 282 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 283 | Thread* self = Thread::Current(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 284 | DCHECK(self != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 285 | CHECK(!self->IsExceptionPending()); |
| 286 | // Find the class in the loaded classes table. |
| 287 | Class* klass = LookupClass(descriptor, class_loader); |
| 288 | if (klass == NULL) { |
| 289 | // Class is not yet loaded. |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 290 | if (descriptor[0] == '[') { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 291 | return CreateArrayClass(descriptor, class_loader); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 292 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 293 | DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_); |
| 294 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 295 | if (pair.second == NULL) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 296 | LG << "Class " << PrintableString(descriptor) << " not found in class loader " << class_loader; // TODO: NoClassDefFoundError |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 297 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 298 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 299 | const DexFile& dex_file = *pair.first; |
| 300 | const DexFile::ClassDef& dex_class_def = *pair.second; |
| 301 | DexCache* dex_cache = FindDexCache(pair.first); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 302 | // Load the class from the dex file. |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 303 | if (!init_done_) { |
| 304 | // finish up init of hand crafted class_roots_ |
| 305 | if (descriptor == "Ljava/lang/Object;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 306 | klass = GetClassRoot(kJavaLangObject); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 307 | } else if (descriptor == "Ljava/lang/Class;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 308 | klass = GetClassRoot(kJavaLangClass); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 309 | } else if (descriptor == "Ljava/lang/String;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 310 | klass = GetClassRoot(kJavaLangString); |
| 311 | } else if (descriptor == "Ljava/lang/reflect/Field;") { |
| 312 | klass = GetClassRoot(kJavaLangReflectField); |
| 313 | } else if (descriptor == "Ljava/lang/reflect/Method;") { |
| 314 | klass = GetClassRoot(kJavaLangReflectMethod); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 315 | } else { |
| 316 | klass = AllocClass(); |
| 317 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 318 | } else { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 319 | klass = AllocClass(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 320 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 321 | klass->dex_cache_ = dex_cache; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 322 | LoadClass(dex_file, dex_class_def, klass, class_loader); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 323 | // Check for a pending exception during load |
| 324 | if (self->IsExceptionPending()) { |
| 325 | // TODO: free native allocations in klass |
| 326 | return NULL; |
| 327 | } |
| 328 | { |
| 329 | ObjectLock lock(klass); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 330 | klass->clinit_thread_id_ = self->GetId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 331 | // Add the newly loaded class to the loaded classes table. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 332 | bool success = InsertClass(descriptor, klass); // TODO: just return collision |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 333 | if (!success) { |
| 334 | // We may fail to insert if we raced with another thread. |
| 335 | klass->clinit_thread_id_ = 0; |
| 336 | // TODO: free native allocations in klass |
| 337 | klass = LookupClass(descriptor, class_loader); |
| 338 | CHECK(klass != NULL); |
| 339 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 340 | // Finish loading (if necessary) by finding parents |
| 341 | if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) { |
| 342 | // Loading failed. |
| 343 | // TODO: CHECK(self->IsExceptionPending()); |
| 344 | lock.NotifyAll(); |
| 345 | return NULL; |
| 346 | } |
| 347 | CHECK(klass->IsLoaded()); |
| 348 | // Link the class (if necessary) |
| 349 | if (!klass->IsLinked() && !LinkClass(klass, dex_file)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 350 | // Linking failed. |
| 351 | // TODO: CHECK(self->IsExceptionPending()); |
| 352 | lock.NotifyAll(); |
| 353 | return NULL; |
| 354 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 355 | CHECK(klass->IsLinked()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
| 359 | // Link the class if it has not already been linked. |
| 360 | if (!klass->IsLinked() && !klass->IsErroneous()) { |
| 361 | ObjectLock lock(klass); |
| 362 | // Check for circular dependencies between classes. |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 363 | if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 364 | LG << "Recursive link"; // TODO: ClassCircularityError |
| 365 | return NULL; |
| 366 | } |
| 367 | // Wait for the pending initialization to complete. |
| 368 | while (!klass->IsLinked() && !klass->IsErroneous()) { |
| 369 | lock.Wait(); |
| 370 | } |
| 371 | } |
| 372 | if (klass->IsErroneous()) { |
| 373 | LG << "EarlierClassFailure"; // TODO: EarlierClassFailure |
| 374 | return NULL; |
| 375 | } |
| 376 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 377 | CHECK(klass->IsLinked()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 378 | CHECK(!self->IsExceptionPending()); |
| 379 | return klass; |
| 380 | } |
| 381 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 382 | void ClassLinker::LoadClass(const DexFile& dex_file, |
| 383 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 384 | Class* klass, |
| 385 | ClassLoader* class_loader) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 386 | CHECK(klass != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 387 | CHECK(klass->dex_cache_ != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 388 | CHECK_EQ(Class::kStatusNotReady, klass->status_); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 389 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 390 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 391 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 392 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 393 | CHECK(descriptor != NULL); |
| 394 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 395 | klass->klass_ = GetClassRoot(kJavaLangClass); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 396 | klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 397 | klass->access_flags_ = dex_class_def.access_flags_; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 398 | klass->class_loader_ = class_loader; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 399 | klass->primitive_type_ = Class::kPrimNot; |
| 400 | klass->status_ = Class::kStatusIdx; |
| 401 | |
| 402 | klass->super_class_ = NULL; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 403 | klass->super_class_idx_ = dex_class_def.superclass_idx_; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 404 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 405 | size_t num_static_fields = header.static_fields_size_; |
| 406 | size_t num_instance_fields = header.instance_fields_size_; |
| 407 | size_t num_direct_methods = header.direct_methods_size_; |
| 408 | size_t num_virtual_methods = header.virtual_methods_size_; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 409 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 410 | klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 411 | |
| 412 | // Load class interfaces. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 413 | LoadInterfaces(dex_file, dex_class_def, klass); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 414 | |
| 415 | // Load static fields. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 416 | DCHECK(klass->sfields_ == NULL); |
| 417 | if (num_static_fields != 0) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 418 | klass->sfields_ = AllocObjectArray<Field>(num_static_fields); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 419 | uint32_t last_idx = 0; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 420 | for (size_t i = 0; i < klass->NumStaticFields(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 421 | DexFile::Field dex_field; |
| 422 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 423 | Field* sfield = AllocField(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 424 | klass->SetStaticField(i, sfield); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 425 | LoadField(dex_file, dex_field, klass, sfield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | // Load instance fields. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 430 | DCHECK(klass->ifields_ == NULL); |
| 431 | if (num_instance_fields != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 432 | // TODO: allocate on the object heap. |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 433 | klass->ifields_ = AllocObjectArray<Field>(num_instance_fields); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 434 | uint32_t last_idx = 0; |
| 435 | for (size_t i = 0; i < klass->NumInstanceFields(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 436 | DexFile::Field dex_field; |
| 437 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 438 | Field* ifield = AllocField(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 439 | klass->SetInstanceField(i, ifield); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 440 | LoadField(dex_file, dex_field, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | // Load direct methods. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 445 | DCHECK(klass->direct_methods_ == NULL); |
| 446 | if (num_direct_methods != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 447 | // TODO: append direct methods to class object |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 448 | klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 449 | uint32_t last_idx = 0; |
| 450 | for (size_t i = 0; i < klass->NumDirectMethods(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 451 | DexFile::Method dex_method; |
| 452 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 453 | Method* meth = AllocMethod(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 454 | klass->SetDirectMethod(i, meth); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 455 | LoadMethod(dex_file, dex_method, klass, meth); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 456 | // TODO: register maps |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | // Load virtual methods. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 461 | DCHECK(klass->virtual_methods_ == NULL); |
| 462 | if (num_virtual_methods != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 463 | // TODO: append virtual methods to class object |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 464 | klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 465 | uint32_t last_idx = 0; |
| 466 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 467 | DexFile::Method dex_method; |
| 468 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 469 | Method* meth = AllocMethod(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 470 | klass->SetVirtualMethod(i, meth); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 471 | LoadMethod(dex_file, dex_method, klass, meth); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 472 | // TODO: register maps |
| 473 | } |
| 474 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 477 | void ClassLinker::LoadInterfaces(const DexFile& dex_file, |
| 478 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 479 | Class* klass) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 480 | const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 481 | if (list != NULL) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 482 | DCHECK(klass->interfaces_ == NULL); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 483 | klass->interfaces_ = AllocObjectArray<Class>(list->Size()); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 484 | DCHECK(klass->interfaces_idx_ == NULL); |
| 485 | klass->interfaces_idx_ = new uint32_t[list->Size()]; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 486 | for (size_t i = 0; i < list->Size(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 487 | const DexFile::TypeItem& type_item = list->GetTypeItem(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 488 | klass->interfaces_idx_[i] = type_item.type_idx_; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 493 | void ClassLinker::LoadField(const DexFile& dex_file, |
| 494 | const DexFile::Field& src, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 495 | Class* klass, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 496 | Field* dst) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 497 | const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 498 | dst->declaring_class_ = klass; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 499 | dst->name_ = ResolveString(klass, field_id.name_idx_, dex_file); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 500 | dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_)); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 501 | // TODO: Assign dst->type_. |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 502 | dst->access_flags_ = src.access_flags_; |
| 503 | } |
| 504 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 505 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
| 506 | const DexFile::Method& src, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 507 | Class* klass, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 508 | Method* dst) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 509 | const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 510 | dst->declaring_class_ = klass; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 511 | dst->name_ = ResolveString(klass, method_id.name_idx_, dex_file); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 512 | { |
| 513 | int32_t utf16_length; |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 514 | scoped_array<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 515 | &utf16_length)); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 516 | dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 517 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 518 | dst->proto_idx_ = method_id.proto_idx_; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 519 | dst->code_off_ = src.code_off_; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 520 | dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 521 | dst->access_flags_ = src.access_flags_; |
| 522 | |
| 523 | // TODO: check for finalize method |
| 524 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 525 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 526 | if (code_item != NULL) { |
| 527 | dst->num_registers_ = code_item->registers_size_; |
| 528 | dst->num_ins_ = code_item->ins_size_; |
| 529 | dst->num_outs_ = code_item->outs_size_; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 530 | } else { |
| 531 | uint16_t num_args = dst->NumArgRegisters(); |
| 532 | if (!dst->IsStatic()) { |
| 533 | ++num_args; |
| 534 | } |
| 535 | dst->num_registers_ = dst->num_ins_ + num_args; |
| 536 | // TODO: native methods |
| 537 | } |
| 538 | } |
| 539 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 540 | void ClassLinker::AppendToBootClassPath(DexFile* dex_file) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 541 | CHECK(dex_file != NULL); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 542 | boot_class_path_.push_back(dex_file); |
| 543 | RegisterDexFile(dex_file); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 546 | void ClassLinker::RegisterDexFile(const DexFile* dex_file) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 547 | CHECK(dex_file != NULL); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 548 | dex_files_.push_back(dex_file); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 549 | DexCache* dex_cache = AllocDexCache(); |
| 550 | CHECK(dex_cache != NULL); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 551 | dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()), |
| 552 | AllocObjectArray<Class>(dex_file->NumTypeIds()), |
| 553 | AllocObjectArray<Method>(dex_file->NumMethodIds()), |
| 554 | AllocObjectArray<Field>(dex_file->NumFieldIds())); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 555 | dex_caches_.push_back(dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 558 | const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 559 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
| 560 | if (dex_caches_[i] == dex_cache) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 561 | return *dex_files_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 564 | CHECK(false) << "Could not find DexFile"; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 565 | return *dex_files_[-1]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 568 | DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 569 | for (size_t i = 0; i != dex_files_.size(); ++i) { |
| 570 | if (dex_files_[i] == dex_file) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 571 | return dex_caches_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 574 | CHECK(false) << "Could not find DexCache"; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 575 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 578 | Class* ClassLinker::CreatePrimitiveClass(const char* descriptor) { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 579 | Class* klass = AllocClass(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 580 | CHECK(klass != NULL); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 581 | klass->super_class_ = NULL; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 582 | klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 583 | klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 584 | klass->status_ = Class::kStatusInitialized; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 585 | bool success = InsertClass(descriptor, klass); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 586 | CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed"; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 587 | return klass; |
| 588 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 589 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 590 | // Create an array class (i.e. the class object for the array, not the |
| 591 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 592 | // "[Ljava/lang/String;". |
| 593 | // |
| 594 | // If "descriptor" refers to an array of primitives, look up the |
| 595 | // primitive type's internally-generated class object. |
| 596 | // |
| 597 | // "loader" is the class loader of the class that's referring to us. It's |
| 598 | // used to ensure that we're looking for the element type in the right |
| 599 | // context. It does NOT become the class loader for the array class; that |
| 600 | // always comes from the base element class. |
| 601 | // |
| 602 | // Returns NULL with an exception raised on failure. |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 603 | Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 604 | ClassLoader* class_loader) |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 605 | { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 606 | CHECK(descriptor[0] == '['); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 607 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 608 | // Identify the underlying element class and the array dimension depth. |
| 609 | Class* component_type_ = NULL; |
| 610 | int array_rank; |
| 611 | if (descriptor[1] == '[') { |
| 612 | // array of arrays; keep descriptor and grab stuff from parent |
| 613 | Class* outer = FindClass(descriptor.substr(1), class_loader); |
| 614 | if (outer != NULL) { |
| 615 | // want the base class, not "outer", in our component_type_ |
| 616 | component_type_ = outer->component_type_; |
| 617 | array_rank = outer->array_rank_ + 1; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 618 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 619 | DCHECK(component_type_ == NULL); // make sure we fail |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 620 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 621 | } else { |
| 622 | array_rank = 1; |
| 623 | if (descriptor[1] == 'L') { |
| 624 | // array of objects; strip off "[" and look up descriptor. |
| 625 | const StringPiece subDescriptor = descriptor.substr(1); |
| 626 | component_type_ = FindClass(subDescriptor, class_loader); |
| 627 | } else { |
| 628 | // array of a primitive type |
| 629 | component_type_ = FindPrimitiveClass(descriptor[1]); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 630 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 631 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 632 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 633 | if (component_type_ == NULL) { |
| 634 | // failed |
| 635 | // DCHECK(Thread::Current()->IsExceptionPending()); // TODO |
| 636 | return NULL; |
| 637 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 638 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 639 | // See if the component type is already loaded. Array classes are |
| 640 | // always associated with the class loader of their underlying |
| 641 | // element type -- an array of Strings goes with the loader for |
| 642 | // java/lang/String -- so we need to look for it there. (The |
| 643 | // caller should have checked for the existence of the class |
| 644 | // before calling here, but they did so with *their* class loader, |
| 645 | // not the component type's loader.) |
| 646 | // |
| 647 | // If we find it, the caller adds "loader" to the class' initiating |
| 648 | // loader list, which should prevent us from going through this again. |
| 649 | // |
| 650 | // This call is unnecessary if "loader" and "component_type_->class_loader_" |
| 651 | // are the same, because our caller (FindClass) just did the |
| 652 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 653 | // because we effectively do this lookup again when we add the new |
| 654 | // class to the hash table --- necessary because of possible races with |
| 655 | // other threads.) |
| 656 | if (class_loader != component_type_->class_loader_) { |
| 657 | Class* new_class = LookupClass(descriptor, component_type_->class_loader_); |
| 658 | if (new_class != NULL) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 659 | return new_class; |
| 660 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 661 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 662 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 663 | // Fill out the fields in the Class. |
| 664 | // |
| 665 | // It is possible to execute some methods against arrays, because |
| 666 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 667 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 668 | // |
| 669 | // Array classes are simple enough that we don't need to do a full |
| 670 | // link step. |
| 671 | |
| 672 | Class* new_class = NULL; |
| 673 | if (!init_done_) { |
| 674 | if (descriptor == "[Ljava/lang/Object;") { |
| 675 | new_class = GetClassRoot(kObjectArrayClass); |
| 676 | } else if (descriptor == "[C") { |
| 677 | new_class = GetClassRoot(kCharArrayClass); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 678 | } else if (descriptor == "[I") { |
| 679 | new_class = GetClassRoot(kIntArrayClass); |
| 680 | } else if (descriptor == "[J") { |
| 681 | new_class = GetClassRoot(kLongArrayClass); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | if (new_class == NULL) { |
| 685 | new_class = AllocClass(); |
| 686 | if (new_class == NULL) { |
| 687 | return NULL; |
| 688 | } |
| 689 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 690 | new_class->descriptor_ = String::AllocFromModifiedUtf8(descriptor.ToString().c_str()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 691 | Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
| 692 | new_class->super_class_ = java_lang_Object; |
| 693 | new_class->vtable_ = java_lang_Object->vtable_; |
| 694 | new_class->primitive_type_ = Class::kPrimNot; |
| 695 | new_class->component_type_ = component_type_; |
| 696 | new_class->class_loader_ = component_type_->class_loader_; |
| 697 | new_class->array_rank_ = array_rank; |
| 698 | new_class->status_ = Class::kStatusInitialized; |
| 699 | // don't need to set new_class->object_size_ |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 700 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 701 | |
| 702 | |
| 703 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 704 | // interfaces. We need to set that up here, so that stuff like |
| 705 | // "instanceof" works right. |
| 706 | // |
| 707 | // Note: The GC could run during the call to FindSystemClass, |
| 708 | // so we need to make sure the class object is GC-valid while we're in |
| 709 | // there. Do this by clearing the interface list so the GC will just |
| 710 | // think that the entries are null. |
| 711 | |
| 712 | |
| 713 | // Use the single, global copies of "interfaces" and "iftable" |
| 714 | // (remember not to free them for arrays). |
| 715 | DCHECK(array_interfaces_ != NULL); |
| 716 | new_class->interfaces_ = array_interfaces_; |
| 717 | new_class->iftable_count_ = 2; |
| 718 | DCHECK(array_iftable_ != NULL); |
| 719 | new_class->iftable_ = array_iftable_; |
| 720 | |
| 721 | // Inherit access flags from the component type. Arrays can't be |
| 722 | // used as a superclass or interface, so we want to add "final" |
| 723 | // and remove "interface". |
| 724 | // |
| 725 | // Don't inherit any non-standard flags (e.g., kAccFinal) |
| 726 | // from component_type_. We assume that the array class does not |
| 727 | // override finalize(). |
| 728 | new_class->access_flags_ = ((new_class->component_type_->access_flags_ & |
| 729 | ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask; |
| 730 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 731 | if (InsertClass(descriptor, new_class)) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 732 | return new_class; |
| 733 | } |
| 734 | // Another thread must have loaded the class after we |
| 735 | // started but before we finished. Abandon what we've |
| 736 | // done. |
| 737 | // |
| 738 | // (Yes, this happens.) |
| 739 | |
| 740 | // Grab the winning class. |
| 741 | Class* other_class = LookupClass(descriptor, component_type_->class_loader_); |
| 742 | DCHECK(other_class != NULL); |
| 743 | return other_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | Class* ClassLinker::FindPrimitiveClass(char type) { |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 747 | switch (type) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 748 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 749 | return GetClassRoot(kPrimitiveByte); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 750 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 751 | return GetClassRoot(kPrimitiveChar); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 752 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 753 | return GetClassRoot(kPrimitiveDouble); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 754 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 755 | return GetClassRoot(kPrimitiveFloat); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 756 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 757 | return GetClassRoot(kPrimitiveInt); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 758 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 759 | return GetClassRoot(kPrimitiveLong); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 760 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 761 | return GetClassRoot(kPrimitiveShort); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 762 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 763 | return GetClassRoot(kPrimitiveBoolean); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 764 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 765 | return GetClassRoot(kPrimitiveVoid); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 766 | case 'L': |
| 767 | case '[': |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 768 | LOG(ERROR) << "Not a primitive type " << PrintableChar(type); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 769 | default: |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 770 | LOG(ERROR) << "Unknown primitive type " << PrintableChar(type); |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 771 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 772 | return NULL; // Not reachable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 775 | bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) { |
| 776 | size_t hash = StringPieceHash()(descriptor); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 777 | MutexLock mu(classes_lock_); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 778 | Table::iterator it = classes_.insert(std::make_pair(hash, klass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 779 | return ((*it).second == klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 782 | Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 783 | size_t hash = StringPieceHash()(descriptor); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 784 | MutexLock mu(classes_lock_); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 785 | typedef Table::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 786 | for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 787 | Class* klass = it->second; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 788 | if (klass->descriptor_->Equals(descriptor) && klass->class_loader_ == class_loader) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 789 | return klass; |
| 790 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 791 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 792 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | bool ClassLinker::InitializeClass(Class* klass) { |
| 796 | CHECK(klass->GetStatus() == Class::kStatusResolved || |
| 797 | klass->GetStatus() == Class::kStatusError); |
| 798 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 799 | Thread* self = Thread::Current(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 800 | |
| 801 | { |
| 802 | ObjectLock lock(klass); |
| 803 | |
| 804 | if (klass->GetStatus() < Class::kStatusVerified) { |
| 805 | if (klass->IsErroneous()) { |
| 806 | LG << "re-initializing failed class"; // TODO: throw |
| 807 | return false; |
| 808 | } |
| 809 | |
| 810 | CHECK(klass->GetStatus() == Class::kStatusResolved); |
| 811 | |
| 812 | klass->status_ = Class::kStatusVerifying; |
| 813 | if (!DexVerify::VerifyClass(klass)) { |
| 814 | LG << "Verification failed"; // TODO: ThrowVerifyError |
| 815 | Object* exception = self->GetException(); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 816 | klass->SetVerifyErrorClass(exception->GetClass()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 817 | klass->SetStatus(Class::kStatusError); |
| 818 | return false; |
| 819 | } |
| 820 | |
| 821 | klass->SetStatus(Class::kStatusVerified); |
| 822 | } |
| 823 | |
| 824 | if (klass->status_ == Class::kStatusInitialized) { |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | while (klass->status_ == Class::kStatusInitializing) { |
| 829 | // we caught somebody else in the act; was it us? |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 830 | if (klass->clinit_thread_id_ == self->GetId()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 831 | LG << "recursive <clinit>"; |
| 832 | return true; |
| 833 | } |
| 834 | |
| 835 | CHECK(!self->IsExceptionPending()); |
| 836 | |
| 837 | lock.Wait(); // TODO: check for interruption |
| 838 | |
| 839 | // When we wake up, repeat the test for init-in-progress. If |
| 840 | // there's an exception pending (only possible if |
| 841 | // "interruptShouldThrow" was set), bail out. |
| 842 | if (self->IsExceptionPending()) { |
| 843 | CHECK(false); |
| 844 | LG << "Exception in initialization."; // TODO: ExceptionInInitializerError |
| 845 | klass->SetStatus(Class::kStatusError); |
| 846 | return false; |
| 847 | } |
| 848 | if (klass->GetStatus() == Class::kStatusInitializing) { |
| 849 | continue; |
| 850 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 851 | DCHECK(klass->GetStatus() == Class::kStatusInitialized || |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 852 | klass->GetStatus() == Class::kStatusError); |
| 853 | if (klass->IsErroneous()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 854 | // The caller wants an exception, but it was thrown in a |
| 855 | // different thread. Synthesize one here. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 856 | LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError |
| 857 | return false; |
| 858 | } |
| 859 | return true; // otherwise, initialized |
| 860 | } |
| 861 | |
| 862 | // see if we failed previously |
| 863 | if (klass->IsErroneous()) { |
| 864 | // might be wise to unlock before throwing; depends on which class |
| 865 | // it is that we have locked |
| 866 | |
| 867 | // TODO: throwEarlierClassFailure(klass); |
| 868 | return false; |
| 869 | } |
| 870 | |
| 871 | if (!ValidateSuperClassDescriptors(klass)) { |
| 872 | klass->SetStatus(Class::kStatusError); |
| 873 | return false; |
| 874 | } |
| 875 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 876 | DCHECK(klass->status_ < Class::kStatusInitializing); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 877 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 878 | klass->clinit_thread_id_ = self->GetId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 879 | klass->status_ = Class::kStatusInitializing; |
| 880 | } |
| 881 | |
| 882 | if (!InitializeSuperClass(klass)) { |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | InitializeStaticFields(klass); |
| 887 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 888 | Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 889 | if (clinit != NULL) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 890 | // JValue unused; |
| 891 | // TODO: dvmCallMethod(self, method, NULL, &unused); |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 892 | // UNIMPLEMENTED(FATAL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | { |
| 896 | ObjectLock lock(klass); |
| 897 | |
| 898 | if (self->IsExceptionPending()) { |
| 899 | klass->SetStatus(Class::kStatusError); |
| 900 | } else { |
| 901 | klass->SetStatus(Class::kStatusInitialized); |
| 902 | } |
| 903 | lock.NotifyAll(); |
| 904 | } |
| 905 | |
| 906 | return true; |
| 907 | } |
| 908 | |
| 909 | bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) { |
| 910 | if (klass->IsInterface()) { |
| 911 | return true; |
| 912 | } |
| 913 | // begin with the methods local to the superclass |
| 914 | if (klass->HasSuperClass() && |
| 915 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
| 916 | const Class* super = klass->GetSuperClass(); |
| 917 | for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) { |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 918 | const Method* method = super->GetVirtualMethod(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 919 | if (method != super->GetVirtualMethod(i) && |
| 920 | !HasSameMethodDescriptorClasses(method, super, klass)) { |
| 921 | LG << "Classes resolve differently in superclass"; |
| 922 | return false; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | for (size_t i = 0; i < klass->iftable_count_; ++i) { |
| 927 | const InterfaceEntry* iftable = &klass->iftable_[i]; |
| 928 | Class* interface = iftable->GetClass(); |
| 929 | if (klass->GetClassLoader() != interface->GetClassLoader()) { |
| 930 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
| 931 | uint32_t vtable_index = iftable->method_index_array_[j]; |
| 932 | const Method* method = klass->GetVirtualMethod(vtable_index); |
| 933 | if (!HasSameMethodDescriptorClasses(method, interface, |
| 934 | method->GetClass())) { |
| 935 | LG << "Classes resolve differently in interface"; // TODO: LinkageError |
| 936 | return false; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | return true; |
| 942 | } |
| 943 | |
| 944 | bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 945 | const Class* klass1, |
| 946 | const Class* klass2) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 947 | const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache()); |
| 948 | const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 949 | DexFile::ParameterIterator *it; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 950 | for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 951 | const char* descriptor = it->GetDescriptor(); |
| 952 | if (descriptor == NULL) { |
| 953 | break; |
| 954 | } |
| 955 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 956 | // Found a non-primitive type. |
| 957 | if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) { |
| 958 | return false; |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | // Check the return type |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 963 | const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 964 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 965 | if (HasSameDescriptorClasses(descriptor, klass1, klass2)) { |
| 966 | return false; |
| 967 | } |
| 968 | } |
| 969 | return true; |
| 970 | } |
| 971 | |
| 972 | // Returns true if classes referenced by the descriptor are the |
| 973 | // same classes in klass1 as they are in klass2. |
| 974 | bool ClassLinker::HasSameDescriptorClasses(const char* descriptor, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 975 | const Class* klass1, |
| 976 | const Class* klass2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 977 | CHECK(descriptor != NULL); |
| 978 | CHECK(klass1 != NULL); |
| 979 | CHECK(klass2 != NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 980 | Class* found1 = FindClass(descriptor, klass1->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 981 | // TODO: found1 == NULL |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 982 | Class* found2 = FindClass(descriptor, klass2->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 983 | // TODO: found2 == NULL |
| 984 | // TODO: lookup found1 in initiating loader list |
| 985 | if (found1 == NULL || found2 == NULL) { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 986 | Thread::Current()->ClearException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 987 | if (found1 == found2) { |
| 988 | return true; |
| 989 | } else { |
| 990 | return false; |
| 991 | } |
| 992 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 993 | return true; |
| 994 | } |
| 995 | |
| 996 | bool ClassLinker::InitializeSuperClass(Class* klass) { |
| 997 | CHECK(klass != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 998 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 999 | Class* super_class = klass->GetSuperClass(); |
| 1000 | if (super_class->GetStatus() != Class::kStatusInitialized) { |
| 1001 | CHECK(!super_class->IsInterface()); |
| 1002 | klass->MonitorExit(); |
| 1003 | bool super_initialized = InitializeClass(super_class); |
| 1004 | klass->MonitorEnter(); |
| 1005 | // TODO: check for a pending exception |
| 1006 | if (!super_initialized) { |
| 1007 | klass->SetStatus(Class::kStatusError); |
| 1008 | klass->NotifyAll(); |
| 1009 | return false; |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 1016 | bool ClassLinker::EnsureInitialized(Class* c) { |
| 1017 | CHECK(c != NULL); |
| 1018 | if (c->IsInitialized()) { |
| 1019 | return true; |
| 1020 | } |
| 1021 | |
| 1022 | c->MonitorExit(); |
| 1023 | InitializeClass(c); |
| 1024 | c->MonitorEnter(); |
| 1025 | return !Thread::Current()->IsExceptionPending(); |
| 1026 | } |
| 1027 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1028 | void ClassLinker::InitializeStaticFields(Class* klass) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1029 | size_t num_static_fields = klass->NumStaticFields(); |
| 1030 | if (num_static_fields == 0) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1031 | return; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1032 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1033 | DexCache* dex_cache = klass->GetDexCache(); |
| 1034 | if (dex_cache == NULL) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1035 | return; |
| 1036 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 1037 | const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1038 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 1039 | const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1040 | CHECK(dex_class_def != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1041 | const byte* addr = dex_file.GetEncodedArray(*dex_class_def); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 1042 | if (addr == NULL) { |
| 1043 | // All this class' static fields have default values. |
| 1044 | return; |
| 1045 | } |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1046 | size_t array_size = DecodeUnsignedLeb128(&addr); |
| 1047 | for (size_t i = 0; i < array_size; ++i) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1048 | Field* field = klass->GetStaticField(i); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1049 | JValue value; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1050 | DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1051 | switch (type) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1052 | case DexFile::kByte: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1053 | field->SetByte(value.b); |
| 1054 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1055 | case DexFile::kShort: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1056 | field->SetShort(value.s); |
| 1057 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1058 | case DexFile::kChar: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1059 | field->SetChar(value.c); |
| 1060 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1061 | case DexFile::kInt: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1062 | field->SetInt(value.i); |
| 1063 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1064 | case DexFile::kLong: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1065 | field->SetLong(value.j); |
| 1066 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1067 | case DexFile::kFloat: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1068 | field->SetFloat(value.f); |
| 1069 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1070 | case DexFile::kDouble: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1071 | field->SetDouble(value.d); |
| 1072 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1073 | case DexFile::kString: { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1074 | uint32_t string_idx = value.i; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1075 | String* resolved = ResolveString(klass, string_idx, dex_file); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1076 | field->SetObject(resolved); |
| 1077 | break; |
| 1078 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1079 | case DexFile::kBoolean: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1080 | field->SetBoolean(value.z); |
| 1081 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1082 | case DexFile::kNull: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1083 | field->SetObject(value.l); |
| 1084 | break; |
| 1085 | default: |
Carl Shapiro | 606258b | 2011-07-09 16:09:09 -0700 | [diff] [blame] | 1086 | LOG(FATAL) << "Unknown type " << static_cast<int>(type); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1087 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1091 | bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) { |
| 1092 | CHECK_EQ(Class::kStatusLoaded, klass->status_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1093 | if (!LinkSuperClass(klass)) { |
| 1094 | return false; |
| 1095 | } |
| 1096 | if (!LinkMethods(klass)) { |
| 1097 | return false; |
| 1098 | } |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1099 | if (!LinkStaticFields(klass)) { |
| 1100 | return false; |
| 1101 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1102 | if (!LinkInstanceFields(klass)) { |
| 1103 | return false; |
| 1104 | } |
| 1105 | CreateReferenceOffsets(klass); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1106 | CHECK_EQ(Class::kStatusLoaded, klass->status_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1107 | klass->status_ = Class::kStatusResolved; |
| 1108 | return true; |
| 1109 | } |
| 1110 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1111 | bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) { |
| 1112 | CHECK_EQ(Class::kStatusIdx, klass->status_); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1113 | if (klass->super_class_idx_ != DexFile::kDexNoIndex) { |
| 1114 | Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1115 | if (super_class == NULL) { |
| 1116 | LG << "Failed to resolve superclass"; |
| 1117 | return false; |
| 1118 | } |
| 1119 | klass->super_class_ = super_class; // TODO: write barrier |
| 1120 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1121 | if (klass->NumInterfaces() > 0) { |
| 1122 | for (size_t i = 0; i < klass->NumInterfaces(); ++i) { |
| 1123 | uint32_t idx = klass->interfaces_idx_[i]; |
| 1124 | klass->SetInterface(i, ResolveClass(klass, idx, dex_file)); |
| 1125 | if (klass->GetInterface(i) == NULL) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1126 | LG << "Failed to resolve interface"; |
| 1127 | return false; |
| 1128 | } |
| 1129 | // Verify |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1130 | if (!klass->CanAccess(klass->GetInterface(i))) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1131 | LG << "Inaccessible interface"; |
| 1132 | return false; |
| 1133 | } |
| 1134 | } |
| 1135 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1136 | // Mark the class as loaded. |
| 1137 | klass->status_ = Class::kStatusLoaded; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1138 | return true; |
| 1139 | } |
| 1140 | |
| 1141 | bool ClassLinker::LinkSuperClass(Class* klass) { |
| 1142 | CHECK(!klass->IsPrimitive()); |
| 1143 | const Class* super = klass->GetSuperClass(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 1144 | if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1145 | if (super != NULL) { |
| 1146 | LG << "Superclass must not be defined"; // TODO: ClassFormatError |
| 1147 | return false; |
| 1148 | } |
| 1149 | // TODO: clear finalize attribute |
| 1150 | return true; |
| 1151 | } |
| 1152 | if (super == NULL) { |
| 1153 | LG << "No superclass defined"; // TODO: LinkageError |
| 1154 | return false; |
| 1155 | } |
| 1156 | // Verify |
| 1157 | if (super->IsFinal()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 1158 | LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1159 | return false; |
| 1160 | } |
| 1161 | if (super->IsInterface()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 1162 | LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1163 | return false; |
| 1164 | } |
| 1165 | if (!klass->CanAccess(super)) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 1166 | LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1167 | return false; |
| 1168 | } |
| 1169 | return true; |
| 1170 | } |
| 1171 | |
| 1172 | // Populate the class vtable and itable. |
| 1173 | bool ClassLinker::LinkMethods(Class* klass) { |
| 1174 | if (klass->IsInterface()) { |
| 1175 | // No vtable. |
| 1176 | size_t count = klass->NumVirtualMethods(); |
| 1177 | if (!IsUint(16, count)) { |
| 1178 | LG << "Too many methods on interface"; // TODO: VirtualMachineError |
| 1179 | return false; |
| 1180 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 1181 | for (size_t i = 0; i < count; ++i) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1182 | klass->GetVirtualMethod(i)->method_index_ = i; |
| 1183 | } |
| 1184 | } else { |
| 1185 | // Link virtual method tables |
| 1186 | LinkVirtualMethods(klass); |
| 1187 | |
| 1188 | // Link interface method tables |
| 1189 | LinkInterfaceMethods(klass); |
| 1190 | |
| 1191 | // Insert stubs. |
| 1192 | LinkAbstractMethods(klass); |
| 1193 | } |
| 1194 | return true; |
| 1195 | } |
| 1196 | |
| 1197 | bool ClassLinker::LinkVirtualMethods(Class* klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1198 | if (klass->HasSuperClass()) { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1199 | uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1200 | size_t actual_count = klass->GetSuperClass()->vtable_->GetLength(); |
| 1201 | CHECK_LE(actual_count, max_count); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1202 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1203 | klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1204 | // See if any of our virtual methods override the superclass. |
| 1205 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
| 1206 | Method* local_method = klass->GetVirtualMethod(i); |
| 1207 | size_t j = 0; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1208 | for (; j < actual_count; ++j) { |
| 1209 | Method* super_method = klass->vtable_->Get(j); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1210 | if (local_method->HasSameNameAndDescriptor(super_method)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1211 | // Verify |
| 1212 | if (super_method->IsFinal()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1213 | LG << "Method overrides final method"; // TODO: VirtualMachineError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1214 | return false; |
| 1215 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1216 | klass->vtable_->Set(j, local_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1217 | local_method->method_index_ = j; |
| 1218 | break; |
| 1219 | } |
| 1220 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1221 | if (j == actual_count) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1222 | // Not overriding, append. |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1223 | klass->vtable_->Set(actual_count, local_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1224 | local_method->method_index_ = actual_count; |
| 1225 | actual_count += 1; |
| 1226 | } |
| 1227 | } |
| 1228 | if (!IsUint(16, actual_count)) { |
| 1229 | LG << "Too many methods defined on class"; // TODO: VirtualMachineError |
| 1230 | return false; |
| 1231 | } |
| 1232 | CHECK_LE(actual_count, max_count); |
| 1233 | if (actual_count < max_count) { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1234 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1235 | klass->vtable_ = klass->vtable_->CopyOf(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1236 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1237 | } else { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame^] | 1238 | CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;")); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1239 | uint32_t num_virtual_methods = klass->NumVirtualMethods(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1240 | if (!IsUint(16, num_virtual_methods)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1241 | LG << "Too many methods"; // TODO: VirtualMachineError |
| 1242 | return false; |
| 1243 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1244 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1245 | klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods); |
| 1246 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1247 | klass->vtable_->Set(i, klass->GetVirtualMethod(i)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1248 | klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF; |
| 1249 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1250 | } |
| 1251 | return true; |
| 1252 | } |
| 1253 | |
| 1254 | bool ClassLinker::LinkInterfaceMethods(Class* klass) { |
| 1255 | int pool_offset = 0; |
| 1256 | int pool_size = 0; |
| 1257 | int miranda_count = 0; |
| 1258 | int miranda_alloc = 0; |
| 1259 | size_t super_ifcount; |
| 1260 | if (klass->HasSuperClass()) { |
| 1261 | super_ifcount = klass->GetSuperClass()->iftable_count_; |
| 1262 | } else { |
| 1263 | super_ifcount = 0; |
| 1264 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1265 | size_t ifcount = super_ifcount; |
| 1266 | ifcount += klass->NumInterfaces(); |
| 1267 | for (size_t i = 0; i < klass->NumInterfaces(); i++) { |
| 1268 | ifcount += klass->GetInterface(i)->iftable_count_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1269 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1270 | if (ifcount == 0) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1271 | DCHECK(klass->iftable_count_ == 0); |
| 1272 | DCHECK(klass->iftable_ == NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1273 | return true; |
| 1274 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1275 | klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)]; |
| 1276 | memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1277 | if (super_ifcount != 0) { |
| 1278 | memcpy(klass->iftable_, klass->GetSuperClass()->iftable_, |
| 1279 | sizeof(InterfaceEntry) * super_ifcount); |
| 1280 | } |
| 1281 | // Flatten the interface inheritance hierarchy. |
| 1282 | size_t idx = super_ifcount; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1283 | for (size_t i = 0; i < klass->NumInterfaces(); i++) { |
| 1284 | Class* interf = klass->GetInterface(i); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1285 | DCHECK(interf != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1286 | if (!interf->IsInterface()) { |
| 1287 | LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError |
| 1288 | return false; |
| 1289 | } |
| 1290 | klass->iftable_[idx++].SetClass(interf); |
| 1291 | for (size_t j = 0; j < interf->iftable_count_; j++) { |
| 1292 | klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass()); |
| 1293 | } |
| 1294 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1295 | CHECK_EQ(idx, ifcount); |
| 1296 | klass->iftable_count_ = ifcount; |
| 1297 | if (klass->IsInterface() || super_ifcount == ifcount) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1298 | return true; |
| 1299 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1300 | for (size_t i = super_ifcount; i < ifcount; i++) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1301 | pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods(); |
| 1302 | } |
| 1303 | if (pool_size == 0) { |
| 1304 | return true; |
| 1305 | } |
| 1306 | klass->ifvi_pool_count_ = pool_size; |
| 1307 | klass->ifvi_pool_ = new uint32_t[pool_size]; |
| 1308 | std::vector<Method*> miranda_list; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1309 | for (size_t i = super_ifcount; i < ifcount; ++i) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1310 | klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset; |
| 1311 | Class* interface = klass->iftable_[i].GetClass(); |
| 1312 | pool_offset += interface->NumVirtualMethods(); // end here |
| 1313 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
| 1314 | Method* interface_method = interface->GetVirtualMethod(j); |
| 1315 | int k; // must be signed |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1316 | for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1317 | Method* vtable_method = klass->vtable_->Get(k); |
| 1318 | if (interface_method->HasSameNameAndDescriptor(vtable_method)) { |
| 1319 | if (!vtable_method->IsPublic()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1320 | LG << "Implementation not public"; |
| 1321 | return false; |
| 1322 | } |
| 1323 | klass->iftable_[i].method_index_array_[j] = k; |
| 1324 | break; |
| 1325 | } |
| 1326 | } |
| 1327 | if (k < 0) { |
| 1328 | if (miranda_count == miranda_alloc) { |
| 1329 | miranda_alloc += 8; |
| 1330 | if (miranda_list.empty()) { |
| 1331 | miranda_list.resize(miranda_alloc); |
| 1332 | } else { |
| 1333 | miranda_list.resize(miranda_alloc); |
| 1334 | } |
| 1335 | } |
| 1336 | int mir; |
| 1337 | for (mir = 0; mir < miranda_count; mir++) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1338 | Method* miranda_method = miranda_list[mir]; |
| 1339 | if (miranda_method->HasSameNameAndDescriptor(interface_method)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1340 | break; |
| 1341 | } |
| 1342 | } |
| 1343 | // point the interface table at a phantom slot index |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1344 | klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1345 | if (mir == miranda_count) { |
| 1346 | miranda_list[miranda_count++] = interface_method; |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | if (miranda_count != 0) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1352 | int old_method_count = klass->NumVirtualMethods(); |
| 1353 | int new_method_count = old_method_count + miranda_count; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1354 | klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1355 | |
| 1356 | CHECK(klass->vtable_ != NULL); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1357 | int old_vtable_count = klass->vtable_->GetLength(); |
| 1358 | int new_vtable_count = old_vtable_count + miranda_count; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1359 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1360 | klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1361 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1362 | for (int i = 0; i < miranda_count; i++) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1363 | Method* meth = AllocMethod(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1364 | memcpy(meth, miranda_list[i], sizeof(Method)); |
| 1365 | meth->klass_ = klass; |
| 1366 | meth->access_flags_ |= kAccMiranda; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1367 | meth->method_index_ = 0xFFFF & (old_vtable_count + i); |
| 1368 | klass->SetVirtualMethod(old_method_count + i, meth); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1369 | klass->vtable_->Set(old_vtable_count + i, meth); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | return true; |
| 1373 | } |
| 1374 | |
| 1375 | void ClassLinker::LinkAbstractMethods(Class* klass) { |
| 1376 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
| 1377 | Method* method = klass->GetVirtualMethod(i); |
| 1378 | if (method->IsAbstract()) { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 1379 | LG << "AbstractMethodError"; |
| 1380 | method->code_off_ = 0xFFFFFFFF; |
| 1381 | // TODO: throw AbstractMethodError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1386 | // Each static field will be stored in one of three arrays: static_references_, |
| 1387 | // static_32bit_primitives_, or static_64bit_primitives_. This assigns each |
| 1388 | // field a slot in its array and create the arrays. |
| 1389 | bool ClassLinker::LinkStaticFields(Class* klass) { |
| 1390 | size_t next_reference_slot = 0; |
| 1391 | size_t next_32bit_primitive_slot = 0; |
| 1392 | size_t next_64bit_primitive_slot = 0; |
| 1393 | |
| 1394 | for (size_t i = 0; i < klass->NumStaticFields(); i++) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1395 | Field* field = klass->GetStaticField(i); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1396 | char type = field->GetType(); |
| 1397 | if (type == '[' || type == 'L') { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1398 | field->offset_ = next_reference_slot++; |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1399 | } else if (type == 'J' || type == 'D') { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1400 | field->offset_ = next_64bit_primitive_slot++; |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1401 | } else { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1402 | field->offset_ = next_32bit_primitive_slot++; |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | if (next_reference_slot > 0) { |
| 1407 | Class* array_class = GetClassRoot(kObjectArrayClass); |
| 1408 | klass->static_references_ = ObjectArray<Object>::Alloc(array_class, next_reference_slot); |
| 1409 | } |
| 1410 | if (next_32bit_primitive_slot > 0) { |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1411 | klass->static_32bit_primitives_ = IntArray::Alloc(next_32bit_primitive_slot); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1412 | } |
| 1413 | if (next_64bit_primitive_slot > 0) { |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1414 | klass->static_64bit_primitives_ = LongArray::Alloc(next_64bit_primitive_slot); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1420 | bool ClassLinker::LinkInstanceFields(Class* klass) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1421 | int field_offset; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1422 | if (klass->GetSuperClass() != NULL) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1423 | field_offset = klass->GetSuperClass()->object_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1424 | } else { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1425 | field_offset = OFFSETOF_MEMBER(DataObject, fields_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1426 | } |
| 1427 | // Move references to the front. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1428 | klass->num_reference_instance_fields_ = 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1429 | size_t i = 0; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1430 | for ( ; i < klass->NumInstanceFields(); i++) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1431 | Field* pField = klass->GetInstanceField(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1432 | char c = pField->GetType(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1433 | if (c != '[' && c != 'L') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1434 | for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1435 | Field* refField = klass->GetInstanceField(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1436 | char rc = refField->GetType(); |
| 1437 | if (rc == '[' || rc == 'L') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1438 | klass->SetInstanceField(i, refField); |
| 1439 | klass->SetInstanceField(j, pField); |
| 1440 | pField = refField; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1441 | c = rc; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1442 | klass->num_reference_instance_fields_++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1443 | break; |
| 1444 | } |
| 1445 | } |
| 1446 | } else { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1447 | klass->num_reference_instance_fields_++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1448 | } |
| 1449 | if (c != '[' && c != 'L') { |
| 1450 | break; |
| 1451 | } |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1452 | pField->SetOffset(field_offset); |
| 1453 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | // Now we want to pack all of the double-wide fields together. If |
| 1457 | // we're not aligned, though, we want to shuffle one 32-bit field |
| 1458 | // into place. If we can't find one, we'll have to pad it. |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1459 | if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1460 | Field* pField = klass->GetInstanceField(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1461 | char c = pField->GetType(); |
| 1462 | |
| 1463 | if (c != 'J' && c != 'D') { |
| 1464 | // The field that comes next is 32-bit, so just advance past it. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1465 | DCHECK(c != '['); |
| 1466 | DCHECK(c != 'L'); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1467 | pField->SetOffset(field_offset); |
| 1468 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1469 | i++; |
| 1470 | } else { |
| 1471 | // Next field is 64-bit, so search for a 32-bit field we can |
| 1472 | // swap into it. |
| 1473 | bool found = false; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1474 | for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1475 | Field* singleField = klass->GetInstanceField(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1476 | char rc = singleField->GetType(); |
| 1477 | if (rc != 'J' && rc != 'D') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1478 | klass->SetInstanceField(i, singleField); |
| 1479 | klass->SetInstanceField(j, pField); |
| 1480 | pField = singleField; |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1481 | pField->SetOffset(field_offset); |
| 1482 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1483 | found = true; |
| 1484 | i++; |
| 1485 | break; |
| 1486 | } |
| 1487 | } |
| 1488 | if (!found) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1489 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | // Alignment is good, shuffle any double-wide fields forward, and |
| 1495 | // finish assigning field offsets to all fields. |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1496 | DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1497 | for ( ; i < klass->NumInstanceFields(); i++) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1498 | Field* pField = klass->GetInstanceField(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1499 | char c = pField->GetType(); |
| 1500 | if (c != 'D' && c != 'J') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1501 | for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1502 | Field* doubleField = klass->GetInstanceField(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1503 | char rc = doubleField->GetType(); |
| 1504 | if (rc == 'D' || rc == 'J') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1505 | klass->SetInstanceField(i, doubleField); |
| 1506 | klass->SetInstanceField(j, pField); |
| 1507 | pField = doubleField; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1508 | c = rc; |
| 1509 | break; |
| 1510 | } |
| 1511 | } |
| 1512 | } else { |
| 1513 | // This is a double-wide field, leave it be. |
| 1514 | } |
| 1515 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1516 | pField->SetOffset(field_offset); |
| 1517 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1518 | if (c == 'J' || c == 'D') |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1519 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | #ifndef NDEBUG |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1523 | // Make sure that all reference fields appear before |
| 1524 | // non-reference fields, and all double-wide fields are aligned. |
| 1525 | bool seen_non_ref = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1526 | for (i = 0; i < klass->NumInstanceFields(); i++) { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1527 | Field *pField = klass->GetInstanceField(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1528 | char c = pField->GetType(); |
| 1529 | |
| 1530 | if (c == 'D' || c == 'J') { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1531 | DCHECK_EQ(0U, pField->GetOffset() & 0x07); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | if (c != '[' && c != 'L') { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1535 | if (!seen_non_ref) { |
| 1536 | seen_non_ref = true; |
Brian Carlstrom | 07d579f | 2011-07-27 13:31:51 -0700 | [diff] [blame] | 1537 | DCHECK_EQ(klass->NumReferenceInstanceFields(), i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1538 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1539 | } else { |
| 1540 | DCHECK(!seen_non_ref); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1541 | } |
| 1542 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1543 | if (!seen_non_ref) { |
Brian Carlstrom | 07d579f | 2011-07-27 13:31:51 -0700 | [diff] [blame] | 1544 | DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1545 | } |
| 1546 | #endif |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1547 | klass->object_size_ = field_offset; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1548 | return true; |
| 1549 | } |
| 1550 | |
| 1551 | // Set the bitmap of reference offsets, refOffsets, from the ifields |
| 1552 | // list. |
| 1553 | void ClassLinker::CreateReferenceOffsets(Class* klass) { |
| 1554 | uint32_t reference_offsets = 0; |
| 1555 | if (klass->HasSuperClass()) { |
| 1556 | reference_offsets = klass->GetSuperClass()->GetReferenceOffsets(); |
| 1557 | } |
| 1558 | // If our superclass overflowed, we don't stand a chance. |
| 1559 | if (reference_offsets != CLASS_WALK_SUPER) { |
| 1560 | // All of the fields that contain object references are guaranteed |
| 1561 | // to be at the beginning of the ifields list. |
| 1562 | for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) { |
| 1563 | // Note that, per the comment on struct InstField, f->byteOffset |
| 1564 | // is the offset from the beginning of obj, not the offset into |
| 1565 | // obj->instanceData. |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1566 | const Field* field = klass->GetInstanceField(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1567 | size_t byte_offset = field->GetOffset(); |
| 1568 | CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET); |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1569 | CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1570 | if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) { |
| 1571 | uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset); |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1572 | CHECK_NE(new_bit, 0U); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1573 | reference_offsets |= new_bit; |
| 1574 | } else { |
| 1575 | reference_offsets = CLASS_WALK_SUPER; |
| 1576 | break; |
| 1577 | } |
| 1578 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1579 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 1580 | klass->SetReferenceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1583 | Class* ClassLinker::ResolveClass(const Class* referrer, |
| 1584 | uint32_t class_idx, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1585 | const DexFile& dex_file) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1586 | DexCache* dex_cache = referrer->GetDexCache(); |
| 1587 | Class* resolved = dex_cache->GetResolvedClass(class_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1588 | if (resolved != NULL) { |
| 1589 | return resolved; |
| 1590 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1591 | const char* descriptor = dex_file.dexStringByTypeIdx(class_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1592 | if (descriptor[0] != '\0' && descriptor[1] == '\0') { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1593 | resolved = FindPrimitiveClass(descriptor[0]); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1594 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1595 | resolved = FindClass(descriptor, referrer->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1596 | } |
| 1597 | if (resolved != NULL) { |
| 1598 | Class* check = resolved->IsArray() ? resolved->component_type_ : resolved; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1599 | if (referrer->GetDexCache() != check->GetDexCache()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1600 | if (check->GetClassLoader() != NULL) { |
| 1601 | LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1605 | dex_cache->SetResolvedClass(class_idx, resolved); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1606 | } else { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1607 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1608 | } |
| 1609 | return resolved; |
| 1610 | } |
| 1611 | |
| 1612 | Method* ResolveMethod(const Class* referrer, uint32_t method_idx, |
| 1613 | /*MethodType*/ int method_type) { |
| 1614 | CHECK(false); |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1618 | String* ClassLinker::ResolveString(const Class* referring, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1619 | uint32_t string_idx, |
| 1620 | const DexFile& dex_file) { |
| 1621 | const DexFile::StringId& string_id = dex_file.GetStringId(string_idx); |
| 1622 | int32_t utf16_length = dex_file.GetStringLength(string_id); |
| 1623 | const char* utf8_data = dex_file.GetStringData(string_id); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1624 | String* string = intern_table_.Intern(utf16_length, utf8_data); |
| 1625 | referring->GetDexCache()->SetResolvedString(string_idx, string); |
| 1626 | return string; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1629 | } // namespace art |