Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "object.h" |
| 4 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 5 | #include <string.h> |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 6 | #include <algorithm> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 7 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 8 | #include "class_linker.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "globals.h" |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 10 | #include "heap.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "logging.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 12 | #include "dex_cache.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "dex_file.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 14 | |
| 15 | namespace art { |
| 16 | |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 17 | Array* Array::Alloc(Class* array_class, size_t component_count) { |
| 18 | return Alloc(array_class, component_count, array_class->GetComponentSize()); |
| 19 | } |
| 20 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 21 | bool Class::Implements(const Class* klass) const { |
| 22 | DCHECK(klass != NULL); |
| 23 | DCHECK(klass->IsInterface()); |
| 24 | // All interfaces implemented directly and by our superclass, and |
| 25 | // recursively all super-interfaces of those interfaces, are listed |
| 26 | // in iftable_, so we can just do a linear scan through that. |
| 27 | for (size_t i = 0; i < iftable_count_; i++) { |
| 28 | if (iftable_[i].GetClass() == klass) { |
| 29 | return true; |
| 30 | } |
| 31 | } |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | // Determine whether "this" is assignable from "klazz", where both of these |
| 36 | // are array classes. |
| 37 | // |
| 38 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 39 | // Y[][] = Y[][] --> true (identity) |
| 40 | // X[][] = Y[][] --> true (element superclass) |
| 41 | // Y = Y[][] --> false |
| 42 | // Y[] = Y[][] --> false |
| 43 | // Object = Y[][] --> true (everything is an object) |
| 44 | // Object[] = Y[][] --> true |
| 45 | // Object[][] = Y[][] --> true |
| 46 | // Object[][][] = Y[][] --> false (too many []s) |
| 47 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 48 | // Serializable[] = Y[][] --> true |
| 49 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 50 | // |
| 51 | // Don't forget about primitive types. |
| 52 | // int[] instanceof Object[] --> false |
| 53 | // |
| 54 | bool Class::IsArrayAssignableFromArray(const Class* klass) const { |
| 55 | DCHECK(IsArray()); |
| 56 | DCHECK(klass->IsArray()); |
| 57 | DCHECK_GT(array_rank_, 0); |
| 58 | DCHECK_GT(klass->array_rank_, 0); |
| 59 | DCHECK(component_type_ != NULL); |
| 60 | DCHECK(klass->component_type_ != NULL); |
| 61 | if (array_rank_ > klass->array_rank_) { |
| 62 | // Too many []s. |
| 63 | return false; |
| 64 | } |
| 65 | if (array_rank_ == klass->array_rank_) { |
| 66 | return component_type_->IsAssignableFrom(klass->component_type_); |
| 67 | } |
| 68 | DCHECK_LT(array_rank_, klass->array_rank_); |
| 69 | // The thing we might be assignable from has more dimensions. We |
| 70 | // must be an Object or array of Object, or a standard array |
| 71 | // interface or array of standard array interfaces (the standard |
| 72 | // interfaces being java/lang/Cloneable and java/io/Serializable). |
| 73 | if (component_type_->IsInterface()) { |
| 74 | // See if we implement our component type. We know the |
| 75 | // base element is an interface; if the array class implements |
| 76 | // it, we know it's a standard array interface. |
| 77 | return Implements(component_type_); |
| 78 | } |
| 79 | // See if this is an array of Object, Object[], etc. We know |
| 80 | // that the superclass of an array is always Object, so we |
| 81 | // just compare the element type to that. |
| 82 | Class* java_lang_Object = GetSuperClass(); |
| 83 | DCHECK(java_lang_Object != NULL); |
| 84 | DCHECK(java_lang_Object->GetSuperClass() == NULL); |
| 85 | return (component_type_ == java_lang_Object); |
| 86 | } |
| 87 | |
| 88 | bool Class::IsAssignableFromArray(const Class* klass) const { |
| 89 | DCHECK(!IsInterface()); // handled first in IsAssignableFrom |
| 90 | DCHECK(klass->IsArray()); |
| 91 | if (!IsArray()) { |
| 92 | // If "this" is not also an array, it must be Object. |
| 93 | // klass's super should be java_lang_Object, since it is an array. |
| 94 | Class* java_lang_Object = klass->GetSuperClass(); |
| 95 | DCHECK(java_lang_Object != NULL); |
| 96 | DCHECK(java_lang_Object->GetSuperClass() == NULL); |
| 97 | return this == java_lang_Object; |
| 98 | } |
| 99 | return IsArrayAssignableFromArray(klass); |
| 100 | } |
| 101 | |
| 102 | bool Class::IsSubClass(const Class* klass) const { |
| 103 | DCHECK(!IsInterface()); |
| 104 | DCHECK(!klass->IsArray()); |
| 105 | const Class* current = this; |
| 106 | do { |
| 107 | if (current == klass) { |
| 108 | return true; |
| 109 | } |
| 110 | current = current->GetSuperClass(); |
| 111 | } while (current != NULL); |
| 112 | return false; |
| 113 | } |
| 114 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 115 | bool Class::IsInSamePackage(const String* descriptor_string_1, |
| 116 | const String* descriptor_string_2) { |
| 117 | const std::string descriptor1(descriptor_string_1->ToModifiedUtf8()); |
| 118 | const std::string descriptor2(descriptor_string_2->ToModifiedUtf8()); |
| 119 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 120 | size_t i = 0; |
| 121 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 122 | ++i; |
| 123 | } |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 124 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 125 | descriptor2.find('/', i) != StringPiece::npos) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 126 | return false; |
| 127 | } else { |
| 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 132 | #if 0 |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 133 | bool Class::IsInSamePackage(const StringPiece& descriptor1, |
| 134 | const StringPiece& descriptor2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 135 | size_t size = std::min(descriptor1.size(), descriptor2.size()); |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 136 | std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 137 | pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size, |
| 138 | descriptor2.begin()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 139 | return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos); |
| 140 | } |
| 141 | #endif |
| 142 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 143 | bool Class::IsInSamePackage(const Class* that) const { |
| 144 | const Class* klass1 = this; |
| 145 | const Class* klass2 = that; |
| 146 | if (klass1 == klass2) { |
| 147 | return true; |
| 148 | } |
| 149 | // Class loaders must match. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 150 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | // Arrays are in the same package when their element classes are. |
| 154 | if (klass1->IsArray()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 155 | klass1 = klass1->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 156 | } |
| 157 | if (klass2->IsArray()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 158 | klass2 = klass2->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 159 | } |
| 160 | // Compare the package part of the descriptor string. |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 161 | return IsInSamePackage(klass1->descriptor_, klass2->descriptor_); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 164 | uint32_t Field::Get32(const Object* object) const { |
| 165 | CHECK((object == NULL) == IsStatic()); |
| 166 | if (IsStatic()) { |
| 167 | object = declaring_class_; |
| 168 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame^] | 169 | // TODO: volatile |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 170 | return object->GetField32(GetOffset()); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 173 | void Field::Set32(Object* object, uint32_t new_value) const { |
| 174 | CHECK((object == NULL) == IsStatic()); |
| 175 | if (IsStatic()) { |
| 176 | object = declaring_class_; |
| 177 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame^] | 178 | // TODO: volatile |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 179 | object->SetField32(GetOffset(), new_value); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 182 | uint64_t Field::Get64(const Object* object) const { |
| 183 | CHECK((object == NULL) == IsStatic()); |
| 184 | if (IsStatic()) { |
| 185 | object = declaring_class_; |
| 186 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame^] | 187 | // TODO: volatile |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 188 | return object->GetField64(GetOffset()); |
| 189 | } |
| 190 | |
| 191 | void Field::Set64(Object* object, uint64_t new_value) const { |
| 192 | CHECK((object == NULL) == IsStatic()); |
| 193 | if (IsStatic()) { |
| 194 | object = declaring_class_; |
| 195 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame^] | 196 | // TODO: volatile |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 197 | object->SetField64(GetOffset(), new_value); |
| 198 | } |
| 199 | |
| 200 | Object* Field::GetObj(const Object* object) const { |
| 201 | CHECK((object == NULL) == IsStatic()); |
| 202 | if (IsStatic()) { |
| 203 | object = declaring_class_; |
| 204 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame^] | 205 | // TODO: volatile |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 206 | return object->GetFieldObject(GetOffset()); |
| 207 | } |
| 208 | |
| 209 | void Field::SetObj(Object* object, Object* new_value) const { |
| 210 | CHECK((object == NULL) == IsStatic()); |
| 211 | if (IsStatic()) { |
| 212 | object = declaring_class_; |
| 213 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame^] | 214 | // TODO: volatile |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 215 | object->SetFieldObject(GetOffset(), new_value); |
| 216 | } |
| 217 | |
| 218 | bool Field::GetBoolean(const Object* object) const { |
| 219 | CHECK_EQ(GetType(), 'Z'); |
| 220 | return Get32(object); |
| 221 | } |
| 222 | |
| 223 | void Field::SetBoolean(Object* object, bool z) const { |
| 224 | CHECK_EQ(GetType(), 'Z'); |
| 225 | Set32(object, z); |
| 226 | } |
| 227 | |
| 228 | int8_t Field::GetByte(const Object* object) const { |
| 229 | CHECK_EQ(GetType(), 'B'); |
| 230 | return Get32(object); |
| 231 | } |
| 232 | |
| 233 | void Field::SetByte(Object* object, int8_t b) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 234 | CHECK_EQ(GetType(), 'B'); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 235 | Set32(object, b); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 236 | } |
| 237 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 238 | uint16_t Field::GetChar(const Object* object) const { |
| 239 | CHECK_EQ(GetType(), 'C'); |
| 240 | return Get32(object); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 241 | } |
| 242 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 243 | void Field::SetChar(Object* object, uint16_t c) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 244 | CHECK_EQ(GetType(), 'C'); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 245 | Set32(object, c); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 246 | } |
| 247 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 248 | uint16_t Field::GetShort(const Object* object) const { |
| 249 | CHECK_EQ(GetType(), 'S'); |
| 250 | return Get32(object); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 253 | void Field::SetShort(Object* object, uint16_t s) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 254 | CHECK_EQ(GetType(), 'S'); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 255 | Set32(object, s); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 258 | int32_t Field::GetInt(const Object* object) const { |
| 259 | CHECK_EQ(GetType(), 'I'); |
| 260 | return Get32(object); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 263 | void Field::SetInt(Object* object, int32_t i) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 264 | CHECK_EQ(GetType(), 'I'); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 265 | Set32(object, i); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 268 | int64_t Field::GetLong(const Object* object) const { |
| 269 | CHECK_EQ(GetType(), 'J'); |
| 270 | return Get64(object); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 273 | void Field::SetLong(Object* object, int64_t j) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 274 | CHECK_EQ(GetType(), 'J'); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 275 | Set64(object, j); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 278 | float Field::GetFloat(const Object* object) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 279 | CHECK_EQ(GetType(), 'F'); |
| 280 | JValue float_bits; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 281 | float_bits.i = Get32(object); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 282 | return float_bits.f; |
| 283 | } |
| 284 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 285 | void Field::SetFloat(Object* object, float f) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 286 | CHECK_EQ(GetType(), 'F'); |
| 287 | JValue float_bits; |
| 288 | float_bits.f = f; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 289 | Set32(object, float_bits.i); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 292 | double Field::GetDouble(const Object* object) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 293 | CHECK_EQ(GetType(), 'D'); |
| 294 | JValue double_bits; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 295 | double_bits.j = Get64(object); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 296 | return double_bits.d; |
| 297 | } |
| 298 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 299 | void Field::SetDouble(Object* object, double d) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 300 | CHECK_EQ(GetType(), 'D'); |
| 301 | JValue double_bits; |
| 302 | double_bits.d = d; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 303 | Set64(object, double_bits.j); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 304 | } |
| 305 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 306 | Object* Field::GetObject(const Object* object) const { |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 307 | CHECK(GetType() == 'L' || GetType() == '['); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 308 | return GetObj(object); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 311 | void Field::SetObject(Object* object, Object* l) const { |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 312 | CHECK(GetType() == 'L' || GetType() == '['); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 313 | SetObj(object, l); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 316 | uint32_t Method::NumArgRegisters() const { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 317 | CHECK(shorty_ != NULL); |
| 318 | uint32_t num_registers = 0; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 319 | for (int i = 1; i < shorty_.length(); ++i) { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 320 | char ch = shorty_[i]; |
| 321 | if (ch == 'D' || ch == 'J') { |
| 322 | num_registers += 2; |
| 323 | } else { |
| 324 | num_registers += 1; |
| 325 | } |
| 326 | } |
| 327 | return num_registers; |
| 328 | } |
| 329 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 330 | size_t Method::NumArgArrayBytes() const { |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 331 | const StringPiece& shorty = GetShorty(); |
| 332 | size_t num_bytes = 0; |
| 333 | for (int i = 1; i < shorty.size(); ++i) { |
| 334 | char ch = shorty[i]; |
| 335 | if (ch == 'D' || ch == 'J') { |
| 336 | num_bytes += 8; |
Carl Shapiro | 84d00e6 | 2011-08-18 14:57:20 -0700 | [diff] [blame] | 337 | } else if (ch == 'L') { |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 338 | // Argument is a reference or an array. The shorty descriptor |
| 339 | // does not distinguish between these types. |
| 340 | num_bytes += sizeof(Object*); |
| 341 | } else { |
| 342 | num_bytes += 4; |
| 343 | } |
| 344 | } |
| 345 | return num_bytes; |
| 346 | } |
| 347 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 348 | // The number of reference arguments to this method including implicit this |
| 349 | // pointer |
| 350 | size_t Method::NumReferenceArgs() const { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 351 | size_t result = IsStatic() ? 0 : 1; // The implicit this pointer. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 352 | for (int i = 1; i < shorty_.length(); i++) { |
| 353 | if ((shorty_[i] == 'L') || (shorty_[i] == '[')) { |
| 354 | result++; |
| 355 | } |
| 356 | } |
| 357 | return result; |
| 358 | } |
| 359 | |
| 360 | // The number of long or double arguments |
| 361 | size_t Method::NumLongOrDoubleArgs() const { |
| 362 | size_t result = 0; |
| 363 | for (int i = 1; i < shorty_.length(); i++) { |
| 364 | if ((shorty_[i] == 'D') || (shorty_[i] == 'J')) { |
| 365 | result++; |
| 366 | } |
| 367 | } |
| 368 | return result; |
| 369 | } |
| 370 | |
| 371 | // The number of reference arguments to this method before the given parameter |
| 372 | // index |
| 373 | size_t Method::NumReferenceArgsBefore(unsigned int param) const { |
| 374 | CHECK_LT(param, NumArgs()); |
| 375 | unsigned int result = IsStatic() ? 0 : 1; |
| 376 | for (unsigned int i = 1; (i < (unsigned int)shorty_.length()) && |
| 377 | (i < (param + 1)); i++) { |
| 378 | if ((shorty_[i] == 'L') || (shorty_[i] == '[')) { |
| 379 | result++; |
| 380 | } |
| 381 | } |
| 382 | return result; |
| 383 | } |
| 384 | |
| 385 | // Is the given method parameter a reference? |
| 386 | bool Method::IsParamAReference(unsigned int param) const { |
| 387 | CHECK_LT(param, NumArgs()); |
| 388 | if (IsStatic()) { |
| 389 | param++; // 0th argument must skip return value at start of the shorty |
| 390 | } else if (param == 0) { |
| 391 | return true; // this argument |
| 392 | } |
| 393 | return ((shorty_[param] == 'L') || (shorty_[param] == '[')); |
| 394 | } |
| 395 | |
| 396 | // Is the given method parameter a long or double? |
| 397 | bool Method::IsParamALongOrDouble(unsigned int param) const { |
| 398 | CHECK_LT(param, NumArgs()); |
| 399 | if (IsStatic()) { |
| 400 | param++; // 0th argument must skip return value at start of the shorty |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 401 | } else if (param == 0) { |
| 402 | return false; // this argument |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 403 | } |
| 404 | return (shorty_[param] == 'J') || (shorty_[param] == 'D'); |
| 405 | } |
| 406 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 407 | static size_t ShortyCharToSize(char x) { |
| 408 | switch (x) { |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 409 | case 'V': return 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 410 | case '[': return kPointerSize; |
| 411 | case 'L': return kPointerSize; |
| 412 | case 'D': return 8; |
| 413 | case 'J': return 8; |
| 414 | default: return 4; |
| 415 | } |
| 416 | } |
| 417 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 418 | size_t Method::ParamSize(unsigned int param) const { |
| 419 | CHECK_LT(param, NumArgs()); |
| 420 | if (IsStatic()) { |
| 421 | param++; // 0th argument must skip return value at start of the shorty |
| 422 | } else if (param == 0) { |
| 423 | return kPointerSize; // this argument |
| 424 | } |
| 425 | return ShortyCharToSize(shorty_[param]); |
| 426 | } |
| 427 | |
| 428 | size_t Method::ReturnSize() const { |
| 429 | return ShortyCharToSize(shorty_[0]); |
| 430 | } |
| 431 | |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 432 | bool Method::HasSameNameAndDescriptor(const Method* that) const { |
| 433 | return (this->GetName()->Equals(that->GetName()) && |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 434 | this->GetSignature()->Equals(that->GetSignature())); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 437 | Method* Class::FindDeclaredDirectMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 438 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 439 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 440 | Method* method = GetDirectMethod(i); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 441 | if (method->GetName()->Equals(name) && |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 442 | method->GetSignature()->Equals(signature)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 443 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 446 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 449 | Method* Class::FindDirectMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 450 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 451 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 452 | Method* method = klass->FindDeclaredDirectMethod(name, signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 453 | if (method != NULL) { |
| 454 | return method; |
| 455 | } |
| 456 | } |
| 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | Method* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 461 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 462 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 463 | Method* method = GetVirtualMethod(i); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 464 | if (method->GetName()->Equals(name) && |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 465 | method->GetSignature()->Equals(signature)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 466 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 469 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 472 | Method* Class::FindVirtualMethod(const StringPiece& name, |
| 473 | const StringPiece& descriptor) { |
| 474 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 475 | Method* method = klass->FindDeclaredVirtualMethod(name, descriptor); |
| 476 | if (method != NULL) { |
| 477 | return method; |
| 478 | } |
| 479 | } |
| 480 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 483 | Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& descriptor) { |
| 484 | // Is the field in this class? |
| 485 | // Interfaces are not relevant because they can't contain instance fields. |
| 486 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 487 | Field* f = GetInstanceField(i); |
| 488 | if (f->GetName()->Equals(name) && f->GetDescriptor() == descriptor) { |
| 489 | return f; |
| 490 | } |
| 491 | } |
| 492 | return NULL; |
| 493 | } |
| 494 | |
| 495 | Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& descriptor) { |
| 496 | // Is the field in this class, or any of its superclasses? |
| 497 | // Interfaces are not relevant because they can't contain instance fields. |
| 498 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 499 | Field* f = c->FindDeclaredInstanceField(name, descriptor); |
| 500 | if (f != NULL) { |
| 501 | return f; |
| 502 | } |
| 503 | } |
| 504 | return NULL; |
| 505 | } |
| 506 | |
| 507 | Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& descriptor) { |
| 508 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 509 | Field* f = GetStaticField(i); |
| 510 | if (f->GetName()->Equals(name) && f->GetDescriptor() == descriptor) { |
| 511 | return f; |
| 512 | } |
| 513 | } |
| 514 | return NULL; |
| 515 | } |
| 516 | |
| 517 | Field* Class::FindStaticField(const StringPiece& name, const StringPiece& descriptor) { |
| 518 | // Is the field in this class (or its interfaces), or any of its |
| 519 | // superclasses (or their interfaces)? |
| 520 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 521 | // Is the field in this class? |
| 522 | Field* f = c->FindDeclaredStaticField(name, descriptor); |
| 523 | if (f != NULL) { |
| 524 | return f; |
| 525 | } |
| 526 | |
| 527 | // Is this field in any of this class' interfaces? |
| 528 | for (size_t i = 0; i < c->NumInterfaces(); ++i) { |
| 529 | Class* interface = c->GetInterface(i); |
| 530 | f = interface->FindDeclaredStaticField(name, descriptor); |
| 531 | if (f != NULL) { |
| 532 | return f; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | return NULL; |
| 537 | } |
| 538 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 539 | template<typename T> |
| 540 | PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) { |
| 541 | Array* raw_array = Array::Alloc(array_class_, length, sizeof(T)); |
| 542 | return down_cast<PrimitiveArray<T>*>(raw_array); |
| 543 | } |
| 544 | |
| 545 | template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL; |
| 546 | |
| 547 | // Explicitly instantiate all the primitive array types. |
| 548 | template class PrimitiveArray<uint8_t>; // BooleanArray |
| 549 | template class PrimitiveArray<int8_t>; // ByteArray |
| 550 | template class PrimitiveArray<uint16_t>; // CharArray |
| 551 | template class PrimitiveArray<double>; // DoubleArray |
| 552 | template class PrimitiveArray<float>; // FloatArray |
| 553 | template class PrimitiveArray<int32_t>; // IntArray |
| 554 | template class PrimitiveArray<int64_t>; // LongArray |
| 555 | template class PrimitiveArray<int16_t>; // ShortArray |
| 556 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 557 | // TODO: get global references for these |
| 558 | Class* String::java_lang_String_ = NULL; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 559 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 560 | void String::SetClass(Class* java_lang_String) { |
| 561 | CHECK(java_lang_String_ == NULL); |
| 562 | CHECK(java_lang_String != NULL); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 563 | java_lang_String_ = java_lang_String; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 564 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 565 | void String::ResetClass() { |
| 566 | CHECK(java_lang_String_ != NULL); |
| 567 | java_lang_String_ = NULL; |
| 568 | } |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 569 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 570 | static const char* kClassStatusNames[] = { |
| 571 | "Error", |
| 572 | "NotReady", |
| 573 | "Idx", |
| 574 | "Loaded", |
| 575 | "Resolved", |
| 576 | "Verifying", |
| 577 | "Verified", |
| 578 | "Initializing", |
| 579 | "Initialized" |
| 580 | }; |
| 581 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) { |
| 582 | if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) { |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 583 | os << kClassStatusNames[rhs + 1]; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 584 | } else { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 585 | os << "Class::Status[" << static_cast<int>(rhs) << "]"; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 586 | } |
| 587 | return os; |
| 588 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 589 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 590 | } // namespace art |