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