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 | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 7 | #include <algorithm> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <utility> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 11 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 12 | #include "class_loader.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "globals.h" |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 14 | #include "heap.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 15 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 16 | #include "logging.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 17 | #include "dex_cache.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | #include "dex_file.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 19 | #include "runtime.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 23 | bool Object::IsString() const { |
| 24 | // TODO use "klass_ == String::GetJavaLangString()" instead? |
| 25 | return GetClass() == GetClass()->GetDescriptor()->GetClass(); |
| 26 | } |
| 27 | |
| 28 | // TODO: get global references for these |
| 29 | Class* Field::java_lang_reflect_Field_ = NULL; |
| 30 | |
| 31 | void Field::SetClass(Class* java_lang_reflect_Field) { |
| 32 | CHECK(java_lang_reflect_Field_ == NULL); |
| 33 | CHECK(java_lang_reflect_Field != NULL); |
| 34 | java_lang_reflect_Field_ = java_lang_reflect_Field; |
| 35 | } |
| 36 | |
| 37 | void Field::ResetClass() { |
| 38 | CHECK(java_lang_reflect_Field_ != NULL); |
| 39 | java_lang_reflect_Field_ = NULL; |
| 40 | } |
| 41 | |
| 42 | void Field::SetTypeIdx(uint32_t type_idx) { |
| 43 | SetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), type_idx, false); |
| 44 | } |
| 45 | |
| 46 | Class* Field::GetTypeDuringLinking() const { |
| 47 | // We are assured that the necessary primitive types are in the dex cache |
| 48 | // early during class linking |
| 49 | return GetDeclaringClass()->GetDexCache()->GetResolvedType(GetTypeIdx()); |
| 50 | } |
| 51 | |
| 52 | Class* Field::GetType() const { |
| 53 | DCHECK(Runtime::Current() != NULL) |
| 54 | << "Can't call GetType without an initialized runtime"; |
| 55 | // Do full linkage (which sets dex cache value to speed next call) |
| 56 | return Runtime::Current()->GetClassLinker()->ResolveType(GetTypeIdx(), this); |
| 57 | } |
| 58 | |
| 59 | uint32_t Field::Get32StaticFromCode(uint32_t field_idx, const Method* referrer) { |
| 60 | Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, referrer); |
| 61 | if (field == NULL) { |
| 62 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 63 | return 0; |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 64 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 65 | DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t)); |
| 66 | return field->Get32(NULL); |
| 67 | } |
| 68 | void Field::Set32StaticFromCode(uint32_t field_idx, const Method* referrer, uint32_t new_value) { |
| 69 | Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, referrer); |
| 70 | if (field == NULL) { |
| 71 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 72 | return; |
| 73 | } |
| 74 | DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t)); |
| 75 | field->Set32(NULL, new_value); |
| 76 | } |
| 77 | uint64_t Field::Get64StaticFromCode(uint32_t field_idx, const Method* referrer) { |
| 78 | Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, referrer); |
| 79 | if (field == NULL) { |
| 80 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 81 | return 0; |
| 82 | } |
| 83 | DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t)); |
| 84 | return field->Get64(NULL); |
| 85 | } |
| 86 | void Field::Set64StaticFromCode(uint32_t field_idx, const Method* referrer, uint64_t new_value) { |
| 87 | Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, referrer); |
| 88 | if (field == NULL) { |
| 89 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 90 | return; |
| 91 | } |
| 92 | DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t)); |
| 93 | field->Set64(NULL, new_value); |
| 94 | } |
| 95 | Object* Field::GetObjStaticFromCode(uint32_t field_idx, const Method* referrer) { |
| 96 | Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, referrer); |
| 97 | if (field == NULL) { |
| 98 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 99 | return 0; |
| 100 | } |
| 101 | DCHECK(!field->GetType()->IsPrimitive()); |
| 102 | return field->GetObj(NULL); |
| 103 | } |
| 104 | void Field::SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, Object* new_value) { |
| 105 | Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, referrer); |
| 106 | if (field == NULL) { |
| 107 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 108 | return; |
| 109 | } |
| 110 | DCHECK(!field->GetType()->IsPrimitive()); |
| 111 | field->SetObj(NULL, new_value); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 114 | uint32_t Field::Get32(const Object* object) const { |
| 115 | CHECK((object == NULL) == IsStatic()); |
| 116 | if (IsStatic()) { |
| 117 | object = declaring_class_; |
| 118 | } |
| 119 | return object->GetField32(GetOffset(), IsVolatile()); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 122 | void Field::Set32(Object* object, uint32_t new_value) const { |
| 123 | CHECK((object == NULL) == IsStatic()); |
| 124 | if (IsStatic()) { |
| 125 | object = declaring_class_; |
| 126 | } |
| 127 | object->SetField32(GetOffset(), new_value, IsVolatile()); |
| 128 | } |
| 129 | |
| 130 | uint64_t Field::Get64(const Object* object) const { |
| 131 | CHECK((object == NULL) == IsStatic()); |
| 132 | if (IsStatic()) { |
| 133 | object = declaring_class_; |
| 134 | } |
| 135 | return object->GetField64(GetOffset(), IsVolatile()); |
| 136 | } |
| 137 | |
| 138 | void Field::Set64(Object* object, uint64_t new_value) const { |
| 139 | CHECK((object == NULL) == IsStatic()); |
| 140 | if (IsStatic()) { |
| 141 | object = declaring_class_; |
| 142 | } |
| 143 | object->SetField64(GetOffset(), new_value, IsVolatile()); |
| 144 | } |
| 145 | |
| 146 | Object* Field::GetObj(const Object* object) const { |
| 147 | CHECK((object == NULL) == IsStatic()); |
| 148 | if (IsStatic()) { |
| 149 | object = declaring_class_; |
| 150 | } |
| 151 | return object->GetFieldObject<Object*>(GetOffset(), IsVolatile()); |
| 152 | } |
| 153 | |
| 154 | void Field::SetObj(Object* object, const Object* new_value) const { |
| 155 | CHECK((object == NULL) == IsStatic()); |
| 156 | if (IsStatic()) { |
| 157 | object = declaring_class_; |
| 158 | } |
| 159 | object->SetFieldObject(GetOffset(), new_value, IsVolatile()); |
| 160 | } |
| 161 | |
| 162 | bool Field::GetBoolean(const Object* object) const { |
| 163 | DCHECK(GetType()->IsPrimitiveBoolean()); |
| 164 | return Get32(object); |
| 165 | } |
| 166 | |
| 167 | void Field::SetBoolean(Object* object, bool z) const { |
| 168 | DCHECK(GetType()->IsPrimitiveBoolean()); |
| 169 | Set32(object, z); |
| 170 | } |
| 171 | |
| 172 | int8_t Field::GetByte(const Object* object) const { |
| 173 | DCHECK(GetType()->IsPrimitiveByte()); |
| 174 | return Get32(object); |
| 175 | } |
| 176 | |
| 177 | void Field::SetByte(Object* object, int8_t b) const { |
| 178 | DCHECK(GetType()->IsPrimitiveByte()); |
| 179 | Set32(object, b); |
| 180 | } |
| 181 | |
| 182 | uint16_t Field::GetChar(const Object* object) const { |
| 183 | DCHECK(GetType()->IsPrimitiveChar()); |
| 184 | return Get32(object); |
| 185 | } |
| 186 | |
| 187 | void Field::SetChar(Object* object, uint16_t c) const { |
| 188 | DCHECK(GetType()->IsPrimitiveChar()); |
| 189 | Set32(object, c); |
| 190 | } |
| 191 | |
| 192 | uint16_t Field::GetShort(const Object* object) const { |
| 193 | DCHECK(GetType()->IsPrimitiveShort()); |
| 194 | return Get32(object); |
| 195 | } |
| 196 | |
| 197 | void Field::SetShort(Object* object, uint16_t s) const { |
| 198 | DCHECK(GetType()->IsPrimitiveShort()); |
| 199 | Set32(object, s); |
| 200 | } |
| 201 | |
| 202 | int32_t Field::GetInt(const Object* object) const { |
| 203 | DCHECK(GetType()->IsPrimitiveInt()); |
| 204 | return Get32(object); |
| 205 | } |
| 206 | |
| 207 | void Field::SetInt(Object* object, int32_t i) const { |
| 208 | DCHECK(GetType()->IsPrimitiveInt()); |
| 209 | Set32(object, i); |
| 210 | } |
| 211 | |
| 212 | int64_t Field::GetLong(const Object* object) const { |
| 213 | DCHECK(GetType()->IsPrimitiveLong()); |
| 214 | return Get64(object); |
| 215 | } |
| 216 | |
| 217 | void Field::SetLong(Object* object, int64_t j) const { |
| 218 | DCHECK(GetType()->IsPrimitiveLong()); |
| 219 | Set64(object, j); |
| 220 | } |
| 221 | |
| 222 | float Field::GetFloat(const Object* object) const { |
| 223 | DCHECK(GetType()->IsPrimitiveFloat()); |
| 224 | JValue float_bits; |
| 225 | float_bits.i = Get32(object); |
| 226 | return float_bits.f; |
| 227 | } |
| 228 | |
| 229 | void Field::SetFloat(Object* object, float f) const { |
| 230 | DCHECK(GetType()->IsPrimitiveFloat()); |
| 231 | JValue float_bits; |
| 232 | float_bits.f = f; |
| 233 | Set32(object, float_bits.i); |
| 234 | } |
| 235 | |
| 236 | double Field::GetDouble(const Object* object) const { |
| 237 | DCHECK(GetType()->IsPrimitiveDouble()); |
| 238 | JValue double_bits; |
| 239 | double_bits.j = Get64(object); |
| 240 | return double_bits.d; |
| 241 | } |
| 242 | |
| 243 | void Field::SetDouble(Object* object, double d) const { |
| 244 | DCHECK(GetType()->IsPrimitiveDouble()); |
| 245 | JValue double_bits; |
| 246 | double_bits.d = d; |
| 247 | Set64(object, double_bits.j); |
| 248 | } |
| 249 | |
| 250 | Object* Field::GetObject(const Object* object) const { |
| 251 | CHECK(!GetType()->IsPrimitive()); |
| 252 | return GetObj(object); |
| 253 | } |
| 254 | |
| 255 | void Field::SetObject(Object* object, const Object* l) const { |
| 256 | CHECK(!GetType()->IsPrimitive()); |
| 257 | SetObj(object, l); |
| 258 | } |
| 259 | |
| 260 | // TODO: get global references for these |
| 261 | Class* Method::java_lang_reflect_Method_ = NULL; |
| 262 | |
| 263 | void Method::SetClass(Class* java_lang_reflect_Method) { |
| 264 | CHECK(java_lang_reflect_Method_ == NULL); |
| 265 | CHECK(java_lang_reflect_Method != NULL); |
| 266 | java_lang_reflect_Method_ = java_lang_reflect_Method; |
| 267 | } |
| 268 | |
| 269 | void Method::ResetClass() { |
| 270 | CHECK(java_lang_reflect_Method_ != NULL); |
| 271 | java_lang_reflect_Method_ = NULL; |
| 272 | } |
| 273 | |
| 274 | ObjectArray<String>* Method::GetDexCacheStrings() const { |
| 275 | return GetFieldObject<ObjectArray<String>*>( |
| 276 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false); |
| 277 | } |
| 278 | |
| 279 | void Method::SetReturnTypeIdx(uint32_t new_return_type_idx) { |
| 280 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_), |
| 281 | new_return_type_idx, false); |
| 282 | } |
| 283 | |
| 284 | Class* Method::GetReturnType() const { |
| 285 | DCHECK(GetDeclaringClass()->IsLinked()); |
| 286 | // Short-cut |
| 287 | Class* result = GetDexCacheResolvedTypes()->Get(GetReturnTypeIdx()); |
| 288 | if (result == NULL) { |
| 289 | // Do full linkage and set cache value for next call |
| 290 | result = Runtime::Current()->GetClassLinker()->ResolveType(GetReturnTypeIdx(), this); |
| 291 | } |
| 292 | CHECK(result != NULL); |
| 293 | return result; |
| 294 | } |
| 295 | |
| 296 | void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) { |
| 297 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), |
| 298 | new_dex_cache_strings, false); |
| 299 | } |
| 300 | |
| 301 | ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const { |
| 302 | return GetFieldObject<ObjectArray<Class>*>( |
| 303 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false); |
| 304 | } |
| 305 | |
| 306 | void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) { |
| 307 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), |
| 308 | new_dex_cache_classes, false); |
| 309 | } |
| 310 | |
| 311 | ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const { |
| 312 | return GetFieldObject<ObjectArray<Method>*>( |
| 313 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false); |
| 314 | } |
| 315 | |
| 316 | void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) { |
| 317 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), |
| 318 | new_dex_cache_methods, false); |
| 319 | } |
| 320 | |
| 321 | ObjectArray<Field>* Method::GetDexCacheResolvedFields() const { |
| 322 | return GetFieldObject<ObjectArray<Field>*>( |
| 323 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false); |
| 324 | } |
| 325 | |
| 326 | void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) { |
| 327 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), |
| 328 | new_dex_cache_fields, false); |
| 329 | } |
| 330 | |
| 331 | CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const { |
| 332 | return GetFieldPtr<CodeAndDirectMethods*>( |
| 333 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_), |
| 334 | false); |
| 335 | } |
| 336 | |
| 337 | void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) { |
| 338 | SetFieldPtr<CodeAndDirectMethods*>( |
| 339 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_), |
| 340 | new_value, false); |
| 341 | } |
| 342 | |
| 343 | ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const { |
| 344 | return GetFieldObject<ObjectArray<StaticStorageBase>*>( |
| 345 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_), |
| 346 | false); |
| 347 | } |
| 348 | |
| 349 | void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) { |
| 350 | SetFieldObject( |
| 351 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_), |
| 352 | new_value, false); |
| 353 | |
| 354 | } |
| 355 | |
| 356 | size_t Method::NumArgRegisters(const StringPiece& shorty) { |
| 357 | CHECK_LE(1, shorty.length()); |
| 358 | uint32_t num_registers = 0; |
| 359 | for (int i = 1; i < shorty.length(); ++i) { |
| 360 | char ch = shorty[i]; |
| 361 | if (ch == 'D' || ch == 'J') { |
| 362 | num_registers += 2; |
| 363 | } else { |
| 364 | num_registers += 1; |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 367 | return num_registers; |
| 368 | } |
| 369 | |
| 370 | size_t Method::NumArgArrayBytes() const { |
| 371 | const StringPiece& shorty = GetShorty(); |
| 372 | size_t num_bytes = 0; |
| 373 | for (int i = 1; i < shorty.length(); ++i) { |
| 374 | char ch = shorty[i]; |
| 375 | if (ch == 'D' || ch == 'J') { |
| 376 | num_bytes += 8; |
| 377 | } else if (ch == 'L') { |
| 378 | // Argument is a reference or an array. The shorty descriptor |
| 379 | // does not distinguish between these types. |
| 380 | num_bytes += sizeof(Object*); |
| 381 | } else { |
| 382 | num_bytes += 4; |
| 383 | } |
| 384 | } |
| 385 | return num_bytes; |
| 386 | } |
| 387 | |
| 388 | // The number of reference arguments to this method including implicit this |
| 389 | // pointer |
| 390 | size_t Method::NumReferenceArgs() const { |
| 391 | const StringPiece& shorty = GetShorty(); |
| 392 | size_t result = IsStatic() ? 0 : 1; // The implicit this pointer. |
| 393 | for (int i = 1; i < shorty.length(); i++) { |
| 394 | if ((shorty[i] == 'L') || (shorty[i] == '[')) { |
| 395 | result++; |
| 396 | } |
| 397 | } |
| 398 | return result; |
| 399 | } |
| 400 | |
| 401 | // The number of long or double arguments |
| 402 | size_t Method::NumLongOrDoubleArgs() const { |
| 403 | const StringPiece& shorty = GetShorty(); |
| 404 | size_t result = 0; |
| 405 | for (int i = 1; i < shorty.length(); i++) { |
| 406 | if ((shorty[i] == 'D') || (shorty[i] == 'J')) { |
| 407 | result++; |
| 408 | } |
| 409 | } |
| 410 | return result; |
| 411 | } |
| 412 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 413 | // Is the given method parameter a reference? |
| 414 | bool Method::IsParamAReference(unsigned int param) const { |
| 415 | CHECK_LT(param, NumArgs()); |
| 416 | if (IsStatic()) { |
| 417 | param++; // 0th argument must skip return value at start of the shorty |
| 418 | } else if (param == 0) { |
| 419 | return true; // this argument |
| 420 | } |
| 421 | return GetShorty()[param] == 'L'; |
| 422 | } |
| 423 | |
| 424 | // Is the given method parameter a long or double? |
| 425 | bool Method::IsParamALongOrDouble(unsigned int param) const { |
| 426 | CHECK_LT(param, NumArgs()); |
| 427 | if (IsStatic()) { |
| 428 | param++; // 0th argument must skip return value at start of the shorty |
| 429 | } else if (param == 0) { |
| 430 | return false; // this argument |
| 431 | } |
| 432 | return (GetShorty()[param] == 'J') || (GetShorty()[param] == 'D'); |
| 433 | } |
| 434 | |
| 435 | static size_t ShortyCharToSize(char x) { |
| 436 | switch (x) { |
| 437 | case 'V': return 0; |
| 438 | case '[': return kPointerSize; |
| 439 | case 'L': return kPointerSize; |
| 440 | case 'D': return 8; |
| 441 | case 'J': return 8; |
| 442 | default: return 4; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | size_t Method::ParamSize(unsigned int param) const { |
| 447 | CHECK_LT(param, NumArgs()); |
| 448 | if (IsStatic()) { |
| 449 | param++; // 0th argument must skip return value at start of the shorty |
| 450 | } else if (param == 0) { |
| 451 | return kPointerSize; // this argument |
| 452 | } |
| 453 | return ShortyCharToSize(GetShorty()[param]); |
| 454 | } |
| 455 | |
| 456 | size_t Method::ReturnSize() const { |
| 457 | return ShortyCharToSize(GetShorty()[0]); |
| 458 | } |
| 459 | |
| 460 | bool Method::HasSameNameAndDescriptor(const Method* that) const { |
| 461 | return (this->GetName()->Equals(that->GetName()) && |
| 462 | this->GetSignature()->Equals(that->GetSignature())); |
| 463 | } |
| 464 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 465 | void Method::SetCode(ByteArray* code_array, |
| 466 | InstructionSet instruction_set) { |
| 467 | CHECK(!HasCode() || IsNative()); |
| 468 | SetFieldPtr<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), code_array, false); |
| 469 | int8_t* code = code_array->GetData(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 470 | uintptr_t address = reinterpret_cast<uintptr_t>(code); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 471 | if (instruction_set == kThumb2) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 472 | // Set the low-order bit so a BLX will switch to Thumb mode |
| 473 | address |= 0x1; |
| 474 | } |
| 475 | SetFieldPtr<uintptr_t>(OFFSET_OF_OBJECT_MEMBER(Method, code_), address, false); |
| 476 | } |
| 477 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 478 | void Method::SetInvokeStub(const ByteArray* invoke_stub_array) { |
| 479 | const InvokeStub* invoke_stub = reinterpret_cast<InvokeStub*>(invoke_stub_array->GetData()); |
| 480 | SetFieldPtr<const ByteArray*>( |
| 481 | OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), invoke_stub_array, false); |
| 482 | SetFieldPtr<const InvokeStub*>( |
| 483 | OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), invoke_stub, false); |
| 484 | } |
| 485 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 486 | void Class::SetStatus(Status new_status) { |
| 487 | CHECK(new_status > GetStatus() || new_status == kStatusError || |
| 488 | Runtime::Current() == NULL); // no runtime implies we're not initialized |
| 489 | CHECK(sizeof(Status) == sizeof(uint32_t)); |
| 490 | return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), |
| 491 | new_status, false); |
| 492 | } |
| 493 | |
| 494 | DexCache* Class::GetDexCache() const { |
| 495 | return GetFieldObject<DexCache*>( |
| 496 | OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false); |
| 497 | } |
| 498 | |
| 499 | void Class::SetDexCache(DexCache* new_dex_cache) { |
| 500 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), |
| 501 | new_dex_cache, false); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 504 | Object* Class::AllocObjectFromCode(uint32_t type_idx, Method* method) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 505 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 506 | if (klass == NULL) { |
| 507 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 508 | if (klass == NULL) { |
| 509 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 510 | return NULL; |
| 511 | } |
| 512 | } |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 513 | return klass->AllocObject(); |
| 514 | } |
| 515 | |
| 516 | Object* Class::AllocObject() { |
| 517 | DCHECK(!IsAbstract()); |
| 518 | return Heap::AllocObject(this, this->object_size_); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 521 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
| 522 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 523 | // Sanity check that the number of bits set in the reference offset bitmap |
| 524 | // agrees with the number of references |
| 525 | Class* cur = this; |
| 526 | size_t cnt = 0; |
| 527 | while (cur) { |
| 528 | cnt += cur->NumReferenceInstanceFieldsDuringLinking(); |
| 529 | cur = cur->GetSuperClass(); |
| 530 | } |
| 531 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt); |
| 532 | } |
| 533 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
| 534 | new_reference_offsets, false); |
| 535 | } |
| 536 | |
| 537 | void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { |
| 538 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 539 | // Sanity check that the number of bits set in the reference offset bitmap |
| 540 | // agrees with the number of references |
| 541 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), |
| 542 | NumReferenceStaticFieldsDuringLinking()); |
| 543 | } |
| 544 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), |
| 545 | new_reference_offsets, false); |
| 546 | } |
| 547 | |
| 548 | size_t Class::PrimitiveSize() const { |
| 549 | switch (GetPrimitiveType()) { |
| 550 | case kPrimBoolean: |
| 551 | case kPrimByte: |
| 552 | case kPrimChar: |
| 553 | case kPrimShort: |
| 554 | case kPrimInt: |
| 555 | case kPrimFloat: |
| 556 | return sizeof(int32_t); |
| 557 | case kPrimLong: |
| 558 | case kPrimDouble: |
| 559 | return sizeof(int64_t); |
| 560 | default: |
| 561 | LOG(FATAL) << "Primitive type size calculation on invalid type " << this; |
| 562 | return 0; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | size_t Class::GetTypeSize(const String* descriptor) { |
| 567 | switch (descriptor->CharAt(0)) { |
| 568 | case 'B': return 1; // byte |
| 569 | case 'C': return 2; // char |
| 570 | case 'D': return 8; // double |
| 571 | case 'F': return 4; // float |
| 572 | case 'I': return 4; // int |
| 573 | case 'J': return 8; // long |
| 574 | case 'S': return 2; // short |
| 575 | case 'Z': return 1; // boolean |
| 576 | case 'L': return sizeof(Object*); |
| 577 | case '[': return sizeof(Array*); |
| 578 | default: |
| 579 | LOG(ERROR) << "Unknown type " << descriptor; |
| 580 | return 0; |
| 581 | } |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 584 | bool Class::Implements(const Class* klass) const { |
| 585 | DCHECK(klass != NULL); |
| 586 | DCHECK(klass->IsInterface()); |
| 587 | // All interfaces implemented directly and by our superclass, and |
| 588 | // recursively all super-interfaces of those interfaces, are listed |
| 589 | // in iftable_, so we can just do a linear scan through that. |
| 590 | for (size_t i = 0; i < iftable_count_; i++) { |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 591 | if (iftable_[i].GetInterface() == klass) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 592 | return true; |
| 593 | } |
| 594 | } |
| 595 | return false; |
| 596 | } |
| 597 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 598 | bool Class::CanPutArrayElementFromCode(const Class* elementClass, const Class* arrayClass) { |
| 599 | UNIMPLEMENTED(FATAL); |
| 600 | return false; |
| 601 | } |
| 602 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 603 | // Determine whether "this" is assignable from "klazz", where both of these |
| 604 | // are array classes. |
| 605 | // |
| 606 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 607 | // Y[][] = Y[][] --> true (identity) |
| 608 | // X[][] = Y[][] --> true (element superclass) |
| 609 | // Y = Y[][] --> false |
| 610 | // Y[] = Y[][] --> false |
| 611 | // Object = Y[][] --> true (everything is an object) |
| 612 | // Object[] = Y[][] --> true |
| 613 | // Object[][] = Y[][] --> true |
| 614 | // Object[][][] = Y[][] --> false (too many []s) |
| 615 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 616 | // Serializable[] = Y[][] --> true |
| 617 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 618 | // |
| 619 | // Don't forget about primitive types. |
| 620 | // int[] instanceof Object[] --> false |
| 621 | // |
| 622 | bool Class::IsArrayAssignableFromArray(const Class* klass) const { |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 623 | DCHECK(IsArrayClass()); |
| 624 | DCHECK(klass->IsArrayClass()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 625 | DCHECK_GT(GetArrayRank(), 0); |
| 626 | DCHECK_GT(klass->GetArrayRank(), 0); |
| 627 | DCHECK(GetComponentType() != NULL); |
| 628 | DCHECK(klass->GetComponentType() != NULL); |
| 629 | if (GetArrayRank() > klass->GetArrayRank()) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 630 | // Too many []s. |
| 631 | return false; |
| 632 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 633 | if (GetArrayRank() == klass->GetArrayRank()) { |
| 634 | return GetComponentType()->IsAssignableFrom(klass->GetComponentType()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 635 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 636 | DCHECK_LT(GetArrayRank(), klass->GetArrayRank()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 637 | // The thing we might be assignable from has more dimensions. We |
| 638 | // must be an Object or array of Object, or a standard array |
| 639 | // interface or array of standard array interfaces (the standard |
| 640 | // interfaces being java/lang/Cloneable and java/io/Serializable). |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 641 | if (GetComponentType()->IsInterface()) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 642 | // See if we implement our component type. We know the |
| 643 | // base element is an interface; if the array class implements |
| 644 | // it, we know it's a standard array interface. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 645 | return Implements(GetComponentType()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 646 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 647 | // See if this is an array of Object, Object[], etc. |
| 648 | return GetComponentType()->IsObjectClass(); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | bool Class::IsAssignableFromArray(const Class* klass) const { |
| 652 | DCHECK(!IsInterface()); // handled first in IsAssignableFrom |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 653 | DCHECK(klass->IsArrayClass()); |
| 654 | if (!IsArrayClass()) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 655 | // If "this" is not also an array, it must be Object. |
| 656 | // klass's super should be java_lang_Object, since it is an array. |
| 657 | Class* java_lang_Object = klass->GetSuperClass(); |
| 658 | DCHECK(java_lang_Object != NULL); |
| 659 | DCHECK(java_lang_Object->GetSuperClass() == NULL); |
| 660 | return this == java_lang_Object; |
| 661 | } |
| 662 | return IsArrayAssignableFromArray(klass); |
| 663 | } |
| 664 | |
| 665 | bool Class::IsSubClass(const Class* klass) const { |
| 666 | DCHECK(!IsInterface()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 667 | DCHECK(!klass->IsArrayClass()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 668 | const Class* current = this; |
| 669 | do { |
| 670 | if (current == klass) { |
| 671 | return true; |
| 672 | } |
| 673 | current = current->GetSuperClass(); |
| 674 | } while (current != NULL); |
| 675 | return false; |
| 676 | } |
| 677 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 678 | bool Class::IsInSamePackage(const String* descriptor_string_1, |
| 679 | const String* descriptor_string_2) { |
| 680 | const std::string descriptor1(descriptor_string_1->ToModifiedUtf8()); |
| 681 | const std::string descriptor2(descriptor_string_2->ToModifiedUtf8()); |
| 682 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 683 | size_t i = 0; |
| 684 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 685 | ++i; |
| 686 | } |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 687 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 688 | descriptor2.find('/', i) != StringPiece::npos) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 689 | return false; |
| 690 | } else { |
| 691 | return true; |
| 692 | } |
| 693 | } |
| 694 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 695 | #if 0 |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 696 | bool Class::IsInSamePackage(const StringPiece& descriptor1, |
| 697 | const StringPiece& descriptor2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 698 | size_t size = std::min(descriptor1.size(), descriptor2.size()); |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 699 | std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 700 | pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size, |
| 701 | descriptor2.begin()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 702 | return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos); |
| 703 | } |
| 704 | #endif |
| 705 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 706 | bool Class::IsInSamePackage(const Class* that) const { |
| 707 | const Class* klass1 = this; |
| 708 | const Class* klass2 = that; |
| 709 | if (klass1 == klass2) { |
| 710 | return true; |
| 711 | } |
| 712 | // Class loaders must match. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 713 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 714 | return false; |
| 715 | } |
| 716 | // Arrays are in the same package when their element classes are. |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 717 | if (klass1->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 718 | klass1 = klass1->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 719 | } |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 720 | if (klass2->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 721 | klass2 = klass2->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 722 | } |
| 723 | // Compare the package part of the descriptor string. |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 724 | return IsInSamePackage(klass1->descriptor_, klass2->descriptor_); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 727 | const ClassLoader* Class::GetClassLoader() const { |
| 728 | return GetFieldObject<const ClassLoader*>( |
| 729 | OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 732 | void Class::SetClassLoader(const ClassLoader* new_cl) { |
| 733 | ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl); |
| 734 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), |
| 735 | new_class_loader, false); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 738 | Method* Class::FindVirtualMethodForInterface(Method* method) { |
| 739 | Class* declaring_class = method->GetDeclaringClass(); |
| 740 | DCHECK(declaring_class->IsInterface()); |
| 741 | // TODO cache to improve lookup speed |
| 742 | for (size_t i = 0; i < iftable_count_; i++) { |
| 743 | InterfaceEntry& interface_entry = iftable_[i]; |
| 744 | if (interface_entry.GetInterface() == declaring_class) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 745 | return GetVTable()->Get( |
| 746 | interface_entry.GetMethodIndexArray()[method->GetMethodIndex()]); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | UNIMPLEMENTED(FATAL) << "Need to throw an error of some kind"; |
| 750 | return NULL; |
| 751 | } |
| 752 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 753 | Method* Class::FindDeclaredDirectMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 754 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 755 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 756 | Method* method = GetDirectMethod(i); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 757 | if (method->GetName()->Equals(name) && |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 758 | method->GetSignature()->Equals(signature)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 759 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 760 | } |
| 761 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 762 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 765 | Method* Class::FindDirectMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 766 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 767 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 768 | Method* method = klass->FindDeclaredDirectMethod(name, signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 769 | if (method != NULL) { |
| 770 | return method; |
| 771 | } |
| 772 | } |
| 773 | return NULL; |
| 774 | } |
| 775 | |
| 776 | Method* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 777 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 778 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 779 | Method* method = GetVirtualMethod(i); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 780 | if (method->GetName()->Equals(name) && |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 781 | method->GetSignature()->Equals(signature)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 782 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 783 | } |
| 784 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 785 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 788 | Method* Class::FindVirtualMethod(const StringPiece& name, |
| 789 | const StringPiece& descriptor) { |
| 790 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 791 | Method* method = klass->FindDeclaredVirtualMethod(name, descriptor); |
| 792 | if (method != NULL) { |
| 793 | return method; |
| 794 | } |
| 795 | } |
| 796 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 799 | Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 800 | // Is the field in this class? |
| 801 | // Interfaces are not relevant because they can't contain instance fields. |
| 802 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 803 | Field* f = GetInstanceField(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 804 | if (f->GetName()->Equals(name) && type == f->GetType()) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 805 | return f; |
| 806 | } |
| 807 | } |
| 808 | return NULL; |
| 809 | } |
| 810 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 811 | Field* Class::FindInstanceField(const StringPiece& name, Class* type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 812 | // Is the field in this class, or any of its superclasses? |
| 813 | // Interfaces are not relevant because they can't contain instance fields. |
| 814 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 815 | Field* f = c->FindDeclaredInstanceField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 816 | if (f != NULL) { |
| 817 | return f; |
| 818 | } |
| 819 | } |
| 820 | return NULL; |
| 821 | } |
| 822 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 823 | Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) { |
| 824 | DCHECK(type != NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 825 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 826 | Field* f = GetStaticField(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 827 | if (f->GetName()->Equals(name) && f->GetType() == type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 828 | return f; |
| 829 | } |
| 830 | } |
| 831 | return NULL; |
| 832 | } |
| 833 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 834 | Field* Class::FindStaticField(const StringPiece& name, Class* type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 835 | // Is the field in this class (or its interfaces), or any of its |
| 836 | // superclasses (or their interfaces)? |
| 837 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 838 | // Is the field in this class? |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 839 | Field* f = c->FindDeclaredStaticField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 840 | if (f != NULL) { |
| 841 | return f; |
| 842 | } |
| 843 | |
| 844 | // Is this field in any of this class' interfaces? |
| 845 | for (size_t i = 0; i < c->NumInterfaces(); ++i) { |
| 846 | Class* interface = c->GetInterface(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 847 | f = interface->FindDeclaredStaticField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 848 | if (f != NULL) { |
| 849 | return f; |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | return NULL; |
| 854 | } |
| 855 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 856 | Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) { |
| 857 | DCHECK_GE(component_count, 0); |
| 858 | DCHECK(array_class->IsArrayClass()); |
| 859 | size_t size = SizeOf(component_count, component_size); |
| 860 | Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size)); |
| 861 | if (array != NULL) { |
| 862 | DCHECK(array->IsArrayInstance()); |
| 863 | array->SetLength(component_count); |
| 864 | } |
| 865 | return array; |
| 866 | } |
| 867 | |
| 868 | Array* Array::Alloc(Class* array_class, int32_t component_count) { |
| 869 | return Alloc(array_class, component_count, array_class->GetComponentSize()); |
| 870 | } |
| 871 | |
| 872 | Array* Array::AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) { |
| 873 | // TODO: throw on negative component_count |
| 874 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 875 | if (klass == NULL) { |
| 876 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 877 | if (klass == NULL || !klass->IsArrayClass()) { |
| 878 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 879 | return NULL; |
| 880 | } |
| 881 | } |
| 882 | return Array::Alloc(klass, component_count); |
| 883 | } |
| 884 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 885 | template<typename T> |
| 886 | PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 887 | DCHECK(array_class_ != NULL); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 888 | Array* raw_array = Array::Alloc(array_class_, length, sizeof(T)); |
| 889 | return down_cast<PrimitiveArray<T>*>(raw_array); |
| 890 | } |
| 891 | |
| 892 | template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL; |
| 893 | |
| 894 | // Explicitly instantiate all the primitive array types. |
| 895 | template class PrimitiveArray<uint8_t>; // BooleanArray |
| 896 | template class PrimitiveArray<int8_t>; // ByteArray |
| 897 | template class PrimitiveArray<uint16_t>; // CharArray |
| 898 | template class PrimitiveArray<double>; // DoubleArray |
| 899 | template class PrimitiveArray<float>; // FloatArray |
| 900 | template class PrimitiveArray<int32_t>; // IntArray |
| 901 | template class PrimitiveArray<int64_t>; // LongArray |
| 902 | template class PrimitiveArray<int16_t>; // ShortArray |
| 903 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 904 | // TODO: get global references for these |
| 905 | Class* String::java_lang_String_ = NULL; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 906 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 907 | void String::SetClass(Class* java_lang_String) { |
| 908 | CHECK(java_lang_String_ == NULL); |
| 909 | CHECK(java_lang_String != NULL); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 910 | java_lang_String_ = java_lang_String; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 911 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 912 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 913 | void String::ResetClass() { |
| 914 | CHECK(java_lang_String_ != NULL); |
| 915 | java_lang_String_ = NULL; |
| 916 | } |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 917 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 918 | const String* String::Intern() const { |
| 919 | return Runtime::Current()->GetInternTable()->InternWeak(this); |
| 920 | } |
| 921 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 922 | int32_t String::GetHashCode() const { |
| 923 | int32_t result = GetField32( |
| 924 | OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); |
| 925 | DCHECK(result != 0 || |
| 926 | ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0); |
| 927 | return result; |
| 928 | } |
| 929 | |
| 930 | int32_t String::GetLength() const { |
| 931 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false); |
| 932 | DCHECK(result >= 0 && result <= GetCharArray()->GetLength()); |
| 933 | return result; |
| 934 | } |
| 935 | |
| 936 | uint16_t String::CharAt(int32_t index) const { |
| 937 | // TODO: do we need this? Equals is the only caller, and could |
| 938 | // bounds check itself. |
| 939 | if (index < 0 || index >= count_) { |
| 940 | Thread* self = Thread::Current(); |
| 941 | self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;", |
| 942 | "length=%i; index=%i", count_, index); |
| 943 | return 0; |
| 944 | } |
| 945 | return GetCharArray()->Get(index + GetOffset()); |
| 946 | } |
| 947 | |
| 948 | String* String::AllocFromUtf16(int32_t utf16_length, |
| 949 | const uint16_t* utf16_data_in, |
| 950 | int32_t hash_code) { |
| 951 | String* string = Alloc(GetJavaLangString(), utf16_length); |
| 952 | // TODO: use 16-bit wide memset variant |
| 953 | CharArray* array = const_cast<CharArray*>(string->GetCharArray()); |
| 954 | for (int i = 0; i < utf16_length; i++) { |
| 955 | array->Set(i, utf16_data_in[i]); |
| 956 | } |
| 957 | if (hash_code != 0) { |
| 958 | string->SetHashCode(hash_code); |
| 959 | } else { |
| 960 | string->ComputeHashCode(); |
| 961 | } |
| 962 | return string; |
| 963 | } |
| 964 | |
| 965 | String* String::AllocFromModifiedUtf8(const char* utf) { |
| 966 | size_t char_count = CountModifiedUtf8Chars(utf); |
| 967 | return AllocFromModifiedUtf8(char_count, utf); |
| 968 | } |
| 969 | |
| 970 | String* String::AllocFromModifiedUtf8(int32_t utf16_length, |
| 971 | const char* utf8_data_in) { |
| 972 | String* string = Alloc(GetJavaLangString(), utf16_length); |
| 973 | uint16_t* utf16_data_out = |
| 974 | const_cast<uint16_t*>(string->GetCharArray()->GetData()); |
| 975 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
| 976 | string->ComputeHashCode(); |
| 977 | return string; |
| 978 | } |
| 979 | |
| 980 | String* String::Alloc(Class* java_lang_String, int32_t utf16_length) { |
| 981 | return Alloc(java_lang_String, CharArray::Alloc(utf16_length)); |
| 982 | } |
| 983 | |
| 984 | String* String::Alloc(Class* java_lang_String, CharArray* array) { |
| 985 | String* string = down_cast<String*>(java_lang_String->AllocObject()); |
| 986 | string->SetArray(array); |
| 987 | string->SetCount(array->GetLength()); |
| 988 | return string; |
| 989 | } |
| 990 | |
| 991 | bool String::Equals(const String* that) const { |
| 992 | if (this == that) { |
| 993 | // Quick reference equality test |
| 994 | return true; |
| 995 | } else if (that == NULL) { |
| 996 | // Null isn't an instanceof anything |
| 997 | return false; |
| 998 | } else if (this->GetLength() != that->GetLength()) { |
| 999 | // Quick length inequality test |
| 1000 | return false; |
| 1001 | } else { |
| 1002 | // NB don't short circuit on hash code as we're presumably here as the |
| 1003 | // hash code was already equal |
| 1004 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
| 1005 | if (this->CharAt(i) != that->CharAt(i)) { |
| 1006 | return false; |
| 1007 | } |
| 1008 | } |
| 1009 | return true; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | bool String::Equals(const uint16_t* that_chars, int32_t that_offset, |
| 1014 | int32_t that_length) const { |
| 1015 | if (this->GetLength() != that_length) { |
| 1016 | return false; |
| 1017 | } else { |
| 1018 | for (int32_t i = 0; i < that_length; ++i) { |
| 1019 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
| 1020 | return false; |
| 1021 | } |
| 1022 | } |
| 1023 | return true; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | bool String::Equals(const char* modified_utf8) const { |
| 1028 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 1029 | uint16_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 1030 | if (ch == '\0' || ch != CharAt(i)) { |
| 1031 | return false; |
| 1032 | } |
| 1033 | } |
| 1034 | return *modified_utf8 == '\0'; |
| 1035 | } |
| 1036 | |
| 1037 | bool String::Equals(const StringPiece& modified_utf8) const { |
| 1038 | // TODO: do not assume C-string representation. |
| 1039 | return Equals(modified_utf8.data()); |
| 1040 | } |
| 1041 | |
| 1042 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 1043 | std::string String::ToModifiedUtf8() const { |
| 1044 | const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); |
| 1045 | size_t byte_count(CountUtf8Bytes(chars, GetLength())); |
| 1046 | std::string result(byte_count, char(0)); |
| 1047 | ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); |
| 1048 | return result; |
| 1049 | } |
| 1050 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1051 | Class* StackTraceElement::java_lang_StackTraceElement_ = NULL; |
| 1052 | |
| 1053 | void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) { |
| 1054 | CHECK(java_lang_StackTraceElement_ == NULL); |
| 1055 | CHECK(java_lang_StackTraceElement != NULL); |
| 1056 | java_lang_StackTraceElement_ = java_lang_StackTraceElement; |
| 1057 | } |
| 1058 | |
| 1059 | void StackTraceElement::ResetClass() { |
| 1060 | CHECK(java_lang_StackTraceElement_ != NULL); |
| 1061 | java_lang_StackTraceElement_ = NULL; |
| 1062 | } |
| 1063 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1064 | StackTraceElement* StackTraceElement::Alloc(const String* declaring_class, |
| 1065 | const String* method_name, |
| 1066 | const String* file_name, |
| 1067 | int32_t line_number) { |
| 1068 | StackTraceElement* trace = |
| 1069 | down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject()); |
| 1070 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), |
| 1071 | const_cast<String*>(declaring_class), false); |
| 1072 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), |
| 1073 | const_cast<String*>(method_name), false); |
| 1074 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), |
| 1075 | const_cast<String*>(file_name), false); |
| 1076 | trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), |
| 1077 | line_number, false); |
| 1078 | return trace; |
| 1079 | } |
| 1080 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1081 | static const char* kClassStatusNames[] = { |
| 1082 | "Error", |
| 1083 | "NotReady", |
| 1084 | "Idx", |
| 1085 | "Loaded", |
| 1086 | "Resolved", |
| 1087 | "Verifying", |
| 1088 | "Verified", |
| 1089 | "Initializing", |
| 1090 | "Initialized" |
| 1091 | }; |
| 1092 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) { |
| 1093 | if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) { |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 1094 | os << kClassStatusNames[rhs + 1]; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1095 | } else { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1096 | os << "Class::Status[" << static_cast<int>(rhs) << "]"; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1097 | } |
| 1098 | return os; |
| 1099 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1100 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1101 | } // namespace art |