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 { |
Ian Rogers | 93dd966 | 2011-09-17 23:21:22 -0700 | [diff] [blame^] | 582 | #if defined(__arm__) |
| 583 | pc &= ~0x1; // clear any possible thumb instruction mode bit |
| 584 | #endif |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 585 | uint32_t rel_offset = pc - reinterpret_cast<uintptr_t>(GetCodeArray()->GetData()); |
Ian Rogers | 93dd966 | 2011-09-17 23:21:22 -0700 | [diff] [blame^] | 586 | // Strictly the following test should be a less-than, however, if the last |
| 587 | // instruction is a call to an exception throw we may see return addresses |
| 588 | // that are 1 beyond the end of code. |
| 589 | return rel_offset <= static_cast<uint32_t>(GetCodeArray()->GetLength()); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 593 | void Method::SetInvokeStub(const ByteArray* invoke_stub_array) { |
| 594 | const InvokeStub* invoke_stub = reinterpret_cast<InvokeStub*>(invoke_stub_array->GetData()); |
| 595 | SetFieldPtr<const ByteArray*>( |
| 596 | OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), invoke_stub_array, false); |
| 597 | SetFieldPtr<const InvokeStub*>( |
| 598 | OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), invoke_stub, false); |
| 599 | } |
| 600 | |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 601 | void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const { |
| 602 | // Push a transition back into managed code onto the linked list in thread. |
| 603 | CHECK_EQ(Thread::kRunnable, self->GetState()); |
| 604 | NativeToManagedRecord record; |
| 605 | self->PushNativeToManagedRecord(&record); |
| 606 | |
| 607 | // Call the invoke stub associated with the method. |
| 608 | // Pass everything as arguments. |
| 609 | const Method::InvokeStub* stub = GetInvokeStub(); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 610 | |
| 611 | bool have_executable_code = (GetCode() != NULL); |
| 612 | #if !defined(__arm__) |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 613 | // Currently we can only compile non-native methods for ARM. |
| 614 | have_executable_code = IsNative(); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 615 | #endif |
| 616 | |
| 617 | if (have_executable_code && stub != NULL) { |
| 618 | LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub; |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 619 | (*stub)(this, receiver, self, args, result); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 620 | LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub; |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 621 | } else { |
| 622 | LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this); |
| 623 | if (result != NULL) { |
| 624 | result->j = 0; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | // Pop transition. |
| 629 | self->PopNativeToManagedRecord(record); |
| 630 | } |
| 631 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 632 | bool Method::IsRegistered() { |
| 633 | void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false); |
| 634 | void* jni_stub = Runtime::Current()->GetJniStubArray()->GetData(); |
| 635 | return native_method != jni_stub; |
| 636 | } |
| 637 | |
| 638 | void Method::RegisterNative(const void* native_method) { |
| 639 | CHECK(IsNative()); |
| 640 | CHECK(native_method != NULL); |
| 641 | SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), |
| 642 | native_method, false); |
| 643 | } |
| 644 | |
| 645 | void Method::UnregisterNative() { |
| 646 | CHECK(IsNative()); |
| 647 | // restore stub to lookup native pointer via dlsym |
| 648 | RegisterNative(Runtime::Current()->GetJniStubArray()->GetData()); |
| 649 | } |
| 650 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 651 | void Class::SetStatus(Status new_status) { |
| 652 | CHECK(new_status > GetStatus() || new_status == kStatusError || |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 653 | !Runtime::Current()->IsStarted()) << GetDescriptor()->ToModifiedUtf8(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 654 | CHECK(sizeof(Status) == sizeof(uint32_t)); |
| 655 | return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), |
| 656 | new_status, false); |
| 657 | } |
| 658 | |
| 659 | DexCache* Class::GetDexCache() const { |
| 660 | return GetFieldObject<DexCache*>( |
| 661 | OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false); |
| 662 | } |
| 663 | |
| 664 | void Class::SetDexCache(DexCache* new_dex_cache) { |
| 665 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), |
| 666 | new_dex_cache, false); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 669 | Object* Class::AllocObjectFromCode(uint32_t type_idx, Method* method) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 670 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 671 | if (klass == NULL) { |
| 672 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 673 | if (klass == NULL) { |
| 674 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 675 | return NULL; |
| 676 | } |
| 677 | } |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 678 | return klass->AllocObject(); |
| 679 | } |
| 680 | |
| 681 | Object* Class::AllocObject() { |
| 682 | DCHECK(!IsAbstract()); |
| 683 | return Heap::AllocObject(this, this->object_size_); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 686 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
| 687 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 688 | // Sanity check that the number of bits set in the reference offset bitmap |
| 689 | // agrees with the number of references |
| 690 | Class* cur = this; |
| 691 | size_t cnt = 0; |
| 692 | while (cur) { |
| 693 | cnt += cur->NumReferenceInstanceFieldsDuringLinking(); |
| 694 | cur = cur->GetSuperClass(); |
| 695 | } |
| 696 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt); |
| 697 | } |
| 698 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
| 699 | new_reference_offsets, false); |
| 700 | } |
| 701 | |
| 702 | void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { |
| 703 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 704 | // Sanity check that the number of bits set in the reference offset bitmap |
| 705 | // agrees with the number of references |
| 706 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), |
| 707 | NumReferenceStaticFieldsDuringLinking()); |
| 708 | } |
| 709 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), |
| 710 | new_reference_offsets, false); |
| 711 | } |
| 712 | |
| 713 | size_t Class::PrimitiveSize() const { |
| 714 | switch (GetPrimitiveType()) { |
| 715 | case kPrimBoolean: |
| 716 | case kPrimByte: |
| 717 | case kPrimChar: |
| 718 | case kPrimShort: |
| 719 | case kPrimInt: |
| 720 | case kPrimFloat: |
| 721 | return sizeof(int32_t); |
| 722 | case kPrimLong: |
| 723 | case kPrimDouble: |
| 724 | return sizeof(int64_t); |
| 725 | default: |
| 726 | LOG(FATAL) << "Primitive type size calculation on invalid type " << this; |
| 727 | return 0; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | size_t Class::GetTypeSize(const String* descriptor) { |
| 732 | switch (descriptor->CharAt(0)) { |
| 733 | case 'B': return 1; // byte |
| 734 | case 'C': return 2; // char |
| 735 | case 'D': return 8; // double |
| 736 | case 'F': return 4; // float |
| 737 | case 'I': return 4; // int |
| 738 | case 'J': return 8; // long |
| 739 | case 'S': return 2; // short |
| 740 | case 'Z': return 1; // boolean |
| 741 | case 'L': return sizeof(Object*); |
| 742 | case '[': return sizeof(Array*); |
| 743 | default: |
| 744 | LOG(ERROR) << "Unknown type " << descriptor; |
| 745 | return 0; |
| 746 | } |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 749 | bool Class::Implements(const Class* klass) const { |
| 750 | DCHECK(klass != NULL); |
| 751 | DCHECK(klass->IsInterface()); |
| 752 | // All interfaces implemented directly and by our superclass, and |
| 753 | // recursively all super-interfaces of those interfaces, are listed |
| 754 | // in iftable_, so we can just do a linear scan through that. |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 755 | int32_t iftable_count = GetIfTableCount(); |
| 756 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 757 | for (int32_t i = 0; i < iftable_count; i++) { |
| 758 | if (iftable->Get(i)->GetInterface() == klass) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 759 | return true; |
| 760 | } |
| 761 | } |
| 762 | return false; |
| 763 | } |
| 764 | |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 765 | bool Class::CanPutArrayElement(const Class* object_class, const Class* array_class) { |
| 766 | if (object_class->IsArrayClass()) { |
| 767 | return array_class->IsArrayAssignableFromArray(object_class); |
| 768 | } else { |
| 769 | return array_class->GetComponentType()->IsAssignableFrom(object_class); |
| 770 | } |
| 771 | } |
| 772 | |
buzbee | 9a195c9 | 2011-09-16 13:26:02 -0700 | [diff] [blame] | 773 | void Class::CanPutArrayElementFromCode(const Object* element, const Class* array_class) { |
| 774 | if (element == NULL) { |
| 775 | return; |
| 776 | } |
| 777 | if (!CanPutArrayElement(element->GetClass(), array_class)) { |
| 778 | LOG(ERROR) << "Can't put a " << PrettyClass(element->GetClass()) |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 779 | << " into a " << PrettyClass(array_class); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 780 | UNIMPLEMENTED(FATAL) << "need to throw ArrayStoreException and unwind stack"; |
| 781 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 784 | // Determine whether "this" is assignable from "klazz", where both of these |
| 785 | // are array classes. |
| 786 | // |
| 787 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 788 | // Y[][] = Y[][] --> true (identity) |
| 789 | // X[][] = Y[][] --> true (element superclass) |
| 790 | // Y = Y[][] --> false |
| 791 | // Y[] = Y[][] --> false |
| 792 | // Object = Y[][] --> true (everything is an object) |
| 793 | // Object[] = Y[][] --> true |
| 794 | // Object[][] = Y[][] --> true |
| 795 | // Object[][][] = Y[][] --> false (too many []s) |
| 796 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 797 | // Serializable[] = Y[][] --> true |
| 798 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 799 | // |
| 800 | // Don't forget about primitive types. |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 801 | // Object[] = int[] --> false |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 802 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 803 | bool Class::IsArrayAssignableFromArray(const Class* src) const { |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 804 | DCHECK(IsArrayClass()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 805 | DCHECK(src->IsArrayClass()); |
| 806 | return GetComponentType()->IsAssignableFrom(src->GetComponentType()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 809 | bool Class::IsAssignableFromArray(const Class* src) const { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 810 | DCHECK(!IsInterface()); // handled first in IsAssignableFrom |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 811 | DCHECK(src->IsArrayClass()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 812 | if (!IsArrayClass()) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 813 | // If "this" is not also an array, it must be Object. |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 814 | // src's super should be java_lang_Object, since it is an array. |
| 815 | Class* java_lang_Object = src->GetSuperClass(); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 816 | DCHECK(java_lang_Object != NULL); |
| 817 | DCHECK(java_lang_Object->GetSuperClass() == NULL); |
| 818 | return this == java_lang_Object; |
| 819 | } |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 820 | return IsArrayAssignableFromArray(src); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | bool Class::IsSubClass(const Class* klass) const { |
| 824 | DCHECK(!IsInterface()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 825 | DCHECK(!IsArrayClass()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 826 | const Class* current = this; |
| 827 | do { |
| 828 | if (current == klass) { |
| 829 | return true; |
| 830 | } |
| 831 | current = current->GetSuperClass(); |
| 832 | } while (current != NULL); |
| 833 | return false; |
| 834 | } |
| 835 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 836 | bool Class::IsInSamePackage(const String* descriptor_string_1, |
| 837 | const String* descriptor_string_2) { |
| 838 | const std::string descriptor1(descriptor_string_1->ToModifiedUtf8()); |
| 839 | const std::string descriptor2(descriptor_string_2->ToModifiedUtf8()); |
| 840 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 841 | size_t i = 0; |
| 842 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 843 | ++i; |
| 844 | } |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 845 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 846 | descriptor2.find('/', i) != StringPiece::npos) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 847 | return false; |
| 848 | } else { |
| 849 | return true; |
| 850 | } |
| 851 | } |
| 852 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 853 | #if 0 |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 854 | bool Class::IsInSamePackage(const StringPiece& descriptor1, |
| 855 | const StringPiece& descriptor2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 856 | size_t size = std::min(descriptor1.size(), descriptor2.size()); |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 857 | std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 858 | pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size, |
| 859 | descriptor2.begin()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 860 | return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos); |
| 861 | } |
| 862 | #endif |
| 863 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 864 | bool Class::IsInSamePackage(const Class* that) const { |
| 865 | const Class* klass1 = this; |
| 866 | const Class* klass2 = that; |
| 867 | if (klass1 == klass2) { |
| 868 | return true; |
| 869 | } |
| 870 | // Class loaders must match. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 871 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 872 | return false; |
| 873 | } |
| 874 | // Arrays are in the same package when their element classes are. |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 875 | if (klass1->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 876 | klass1 = klass1->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 877 | } |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 878 | if (klass2->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 879 | klass2 = klass2->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 880 | } |
| 881 | // Compare the package part of the descriptor string. |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 882 | return IsInSamePackage(klass1->descriptor_, klass2->descriptor_); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 885 | const ClassLoader* Class::GetClassLoader() const { |
| 886 | return GetFieldObject<const ClassLoader*>( |
| 887 | OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 890 | void Class::SetClassLoader(const ClassLoader* new_cl) { |
| 891 | ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl); |
| 892 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), |
| 893 | new_class_loader, false); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 896 | Method* Class::FindVirtualMethodForInterface(Method* method) { |
| 897 | Class* declaring_class = method->GetDeclaringClass(); |
Brian Carlstrom | a5a97a2 | 2011-09-15 14:08:49 -0700 | [diff] [blame] | 898 | DCHECK(declaring_class != NULL); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 899 | DCHECK(declaring_class->IsInterface()); |
| 900 | // TODO cache to improve lookup speed |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 901 | int32_t iftable_count = GetIfTableCount(); |
| 902 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 903 | for (int32_t i = 0; i < iftable_count; i++) { |
| 904 | InterfaceEntry* interface_entry = iftable->Get(i); |
| 905 | if (interface_entry->GetInterface() == declaring_class) { |
| 906 | return interface_entry->GetMethodArray()->Get(method->GetMethodIndex()); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 909 | UNIMPLEMENTED(FATAL) << "Need to throw an error of some kind " << PrettyMethod(method); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 910 | return NULL; |
| 911 | } |
| 912 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 913 | Method* Class::FindInterfaceMethod(const StringPiece& name, |
| 914 | const StringPiece& signature) { |
| 915 | // Check the current class before checking the interfaces. |
| 916 | Method* method = FindVirtualMethod(name, signature); |
| 917 | if (method != NULL) { |
| 918 | return method; |
| 919 | } |
| 920 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 921 | int32_t iftable_count = GetIfTableCount(); |
| 922 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 923 | for (int32_t i = 0; i < iftable_count; i++) { |
| 924 | method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 925 | if (method != NULL) { |
| 926 | return method; |
| 927 | } |
| 928 | } |
| 929 | return NULL; |
| 930 | } |
| 931 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 932 | Method* Class::FindDeclaredDirectMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 933 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 934 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 935 | Method* method = GetDirectMethod(i); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 936 | if (method->GetName()->Equals(name) && |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 937 | method->GetSignature()->Equals(signature)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 938 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 941 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 944 | Method* Class::FindDirectMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 945 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 946 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 947 | Method* method = klass->FindDeclaredDirectMethod(name, signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 948 | if (method != NULL) { |
| 949 | return method; |
| 950 | } |
| 951 | } |
| 952 | return NULL; |
| 953 | } |
| 954 | |
| 955 | Method* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 956 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 957 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 958 | Method* method = GetVirtualMethod(i); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 959 | if (method->GetName()->Equals(name) && |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 960 | method->GetSignature()->Equals(signature)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 961 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 962 | } |
| 963 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 964 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 967 | Method* Class::FindVirtualMethod(const StringPiece& name, |
| 968 | const StringPiece& descriptor) { |
| 969 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 970 | Method* method = klass->FindDeclaredVirtualMethod(name, descriptor); |
| 971 | if (method != NULL) { |
| 972 | return method; |
| 973 | } |
| 974 | } |
| 975 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 978 | Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 979 | // Is the field in this class? |
| 980 | // Interfaces are not relevant because they can't contain instance fields. |
| 981 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 982 | Field* f = GetInstanceField(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 983 | if (f->GetName()->Equals(name) && type == f->GetType()) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 984 | return f; |
| 985 | } |
| 986 | } |
| 987 | return NULL; |
| 988 | } |
| 989 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 990 | Field* Class::FindInstanceField(const StringPiece& name, Class* type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 991 | // Is the field in this class, or any of its superclasses? |
| 992 | // Interfaces are not relevant because they can't contain instance fields. |
| 993 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 994 | Field* f = c->FindDeclaredInstanceField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 995 | if (f != NULL) { |
| 996 | return f; |
| 997 | } |
| 998 | } |
| 999 | return NULL; |
| 1000 | } |
| 1001 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1002 | Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) { |
| 1003 | DCHECK(type != NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1004 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 1005 | Field* f = GetStaticField(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1006 | if (f->GetName()->Equals(name) && f->GetType() == type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1007 | return f; |
| 1008 | } |
| 1009 | } |
| 1010 | return NULL; |
| 1011 | } |
| 1012 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1013 | Field* Class::FindStaticField(const StringPiece& name, Class* type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1014 | // Is the field in this class (or its interfaces), or any of its |
| 1015 | // superclasses (or their interfaces)? |
| 1016 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 1017 | // Is the field in this class? |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1018 | Field* f = c->FindDeclaredStaticField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1019 | if (f != NULL) { |
| 1020 | return f; |
| 1021 | } |
| 1022 | |
| 1023 | // Is this field in any of this class' interfaces? |
| 1024 | for (size_t i = 0; i < c->NumInterfaces(); ++i) { |
| 1025 | Class* interface = c->GetInterface(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1026 | f = interface->FindDeclaredStaticField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1027 | if (f != NULL) { |
| 1028 | return f; |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | return NULL; |
| 1033 | } |
| 1034 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1035 | 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] | 1036 | DCHECK(array_class != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1037 | DCHECK_GE(component_count, 0); |
| 1038 | DCHECK(array_class->IsArrayClass()); |
| 1039 | size_t size = SizeOf(component_count, component_size); |
| 1040 | Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size)); |
| 1041 | if (array != NULL) { |
| 1042 | DCHECK(array->IsArrayInstance()); |
| 1043 | array->SetLength(component_count); |
| 1044 | } |
| 1045 | return array; |
| 1046 | } |
| 1047 | |
| 1048 | Array* Array::Alloc(Class* array_class, int32_t component_count) { |
| 1049 | return Alloc(array_class, component_count, array_class->GetComponentSize()); |
| 1050 | } |
| 1051 | |
| 1052 | Array* Array::AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) { |
| 1053 | // TODO: throw on negative component_count |
| 1054 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 1055 | if (klass == NULL) { |
| 1056 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 1057 | if (klass == NULL || !klass->IsArrayClass()) { |
| 1058 | UNIMPLEMENTED(FATAL) << "throw an error"; |
| 1059 | return NULL; |
| 1060 | } |
| 1061 | } |
| 1062 | return Array::Alloc(klass, component_count); |
| 1063 | } |
| 1064 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1065 | template<typename T> |
| 1066 | PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1067 | DCHECK(array_class_ != NULL); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1068 | Array* raw_array = Array::Alloc(array_class_, length, sizeof(T)); |
| 1069 | return down_cast<PrimitiveArray<T>*>(raw_array); |
| 1070 | } |
| 1071 | |
| 1072 | template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL; |
| 1073 | |
| 1074 | // Explicitly instantiate all the primitive array types. |
| 1075 | template class PrimitiveArray<uint8_t>; // BooleanArray |
| 1076 | template class PrimitiveArray<int8_t>; // ByteArray |
| 1077 | template class PrimitiveArray<uint16_t>; // CharArray |
| 1078 | template class PrimitiveArray<double>; // DoubleArray |
| 1079 | template class PrimitiveArray<float>; // FloatArray |
| 1080 | template class PrimitiveArray<int32_t>; // IntArray |
| 1081 | template class PrimitiveArray<int64_t>; // LongArray |
| 1082 | template class PrimitiveArray<int16_t>; // ShortArray |
| 1083 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1084 | // TODO: get global references for these |
| 1085 | Class* String::java_lang_String_ = NULL; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1086 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1087 | void String::SetClass(Class* java_lang_String) { |
| 1088 | CHECK(java_lang_String_ == NULL); |
| 1089 | CHECK(java_lang_String != NULL); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1090 | java_lang_String_ = java_lang_String; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1091 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1092 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1093 | void String::ResetClass() { |
| 1094 | CHECK(java_lang_String_ != NULL); |
| 1095 | java_lang_String_ = NULL; |
| 1096 | } |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1097 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 1098 | String* String::Intern() { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1099 | return Runtime::Current()->GetInternTable()->InternWeak(this); |
| 1100 | } |
| 1101 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1102 | int32_t String::GetHashCode() const { |
| 1103 | int32_t result = GetField32( |
| 1104 | OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); |
| 1105 | DCHECK(result != 0 || |
| 1106 | ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0); |
| 1107 | return result; |
| 1108 | } |
| 1109 | |
| 1110 | int32_t String::GetLength() const { |
| 1111 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false); |
| 1112 | DCHECK(result >= 0 && result <= GetCharArray()->GetLength()); |
| 1113 | return result; |
| 1114 | } |
| 1115 | |
| 1116 | uint16_t String::CharAt(int32_t index) const { |
| 1117 | // TODO: do we need this? Equals is the only caller, and could |
| 1118 | // bounds check itself. |
| 1119 | if (index < 0 || index >= count_) { |
| 1120 | Thread* self = Thread::Current(); |
| 1121 | self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;", |
| 1122 | "length=%i; index=%i", count_, index); |
| 1123 | return 0; |
| 1124 | } |
| 1125 | return GetCharArray()->Get(index + GetOffset()); |
| 1126 | } |
| 1127 | |
| 1128 | String* String::AllocFromUtf16(int32_t utf16_length, |
| 1129 | const uint16_t* utf16_data_in, |
| 1130 | int32_t hash_code) { |
| 1131 | String* string = Alloc(GetJavaLangString(), utf16_length); |
| 1132 | // TODO: use 16-bit wide memset variant |
| 1133 | CharArray* array = const_cast<CharArray*>(string->GetCharArray()); |
| 1134 | for (int i = 0; i < utf16_length; i++) { |
| 1135 | array->Set(i, utf16_data_in[i]); |
| 1136 | } |
| 1137 | if (hash_code != 0) { |
| 1138 | string->SetHashCode(hash_code); |
| 1139 | } else { |
| 1140 | string->ComputeHashCode(); |
| 1141 | } |
| 1142 | return string; |
| 1143 | } |
| 1144 | |
| 1145 | String* String::AllocFromModifiedUtf8(const char* utf) { |
| 1146 | size_t char_count = CountModifiedUtf8Chars(utf); |
| 1147 | return AllocFromModifiedUtf8(char_count, utf); |
| 1148 | } |
| 1149 | |
| 1150 | String* String::AllocFromModifiedUtf8(int32_t utf16_length, |
| 1151 | const char* utf8_data_in) { |
| 1152 | String* string = Alloc(GetJavaLangString(), utf16_length); |
| 1153 | uint16_t* utf16_data_out = |
| 1154 | const_cast<uint16_t*>(string->GetCharArray()->GetData()); |
| 1155 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
| 1156 | string->ComputeHashCode(); |
| 1157 | return string; |
| 1158 | } |
| 1159 | |
| 1160 | String* String::Alloc(Class* java_lang_String, int32_t utf16_length) { |
| 1161 | return Alloc(java_lang_String, CharArray::Alloc(utf16_length)); |
| 1162 | } |
| 1163 | |
| 1164 | String* String::Alloc(Class* java_lang_String, CharArray* array) { |
| 1165 | String* string = down_cast<String*>(java_lang_String->AllocObject()); |
| 1166 | string->SetArray(array); |
| 1167 | string->SetCount(array->GetLength()); |
| 1168 | return string; |
| 1169 | } |
| 1170 | |
| 1171 | bool String::Equals(const String* that) const { |
| 1172 | if (this == that) { |
| 1173 | // Quick reference equality test |
| 1174 | return true; |
| 1175 | } else if (that == NULL) { |
| 1176 | // Null isn't an instanceof anything |
| 1177 | return false; |
| 1178 | } else if (this->GetLength() != that->GetLength()) { |
| 1179 | // Quick length inequality test |
| 1180 | return false; |
| 1181 | } else { |
| 1182 | // NB don't short circuit on hash code as we're presumably here as the |
| 1183 | // hash code was already equal |
| 1184 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
| 1185 | if (this->CharAt(i) != that->CharAt(i)) { |
| 1186 | return false; |
| 1187 | } |
| 1188 | } |
| 1189 | return true; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | bool String::Equals(const uint16_t* that_chars, int32_t that_offset, |
| 1194 | int32_t that_length) const { |
| 1195 | if (this->GetLength() != that_length) { |
| 1196 | return false; |
| 1197 | } else { |
| 1198 | for (int32_t i = 0; i < that_length; ++i) { |
| 1199 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
| 1200 | return false; |
| 1201 | } |
| 1202 | } |
| 1203 | return true; |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | bool String::Equals(const char* modified_utf8) const { |
| 1208 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 1209 | uint16_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 1210 | if (ch == '\0' || ch != CharAt(i)) { |
| 1211 | return false; |
| 1212 | } |
| 1213 | } |
| 1214 | return *modified_utf8 == '\0'; |
| 1215 | } |
| 1216 | |
| 1217 | bool String::Equals(const StringPiece& modified_utf8) const { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1218 | // TODO: do not assume C-string representation. For now DCHECK. |
| 1219 | DCHECK_EQ(modified_utf8.data()[modified_utf8.size()], 0); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1220 | return Equals(modified_utf8.data()); |
| 1221 | } |
| 1222 | |
| 1223 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 1224 | std::string String::ToModifiedUtf8() const { |
| 1225 | const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); |
| 1226 | size_t byte_count(CountUtf8Bytes(chars, GetLength())); |
| 1227 | std::string result(byte_count, char(0)); |
| 1228 | ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); |
| 1229 | return result; |
| 1230 | } |
| 1231 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1232 | Class* StackTraceElement::java_lang_StackTraceElement_ = NULL; |
| 1233 | |
| 1234 | void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) { |
| 1235 | CHECK(java_lang_StackTraceElement_ == NULL); |
| 1236 | CHECK(java_lang_StackTraceElement != NULL); |
| 1237 | java_lang_StackTraceElement_ = java_lang_StackTraceElement; |
| 1238 | } |
| 1239 | |
| 1240 | void StackTraceElement::ResetClass() { |
| 1241 | CHECK(java_lang_StackTraceElement_ != NULL); |
| 1242 | java_lang_StackTraceElement_ = NULL; |
| 1243 | } |
| 1244 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1245 | StackTraceElement* StackTraceElement::Alloc(const String* declaring_class, |
| 1246 | const String* method_name, |
| 1247 | const String* file_name, |
| 1248 | int32_t line_number) { |
| 1249 | StackTraceElement* trace = |
| 1250 | down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject()); |
| 1251 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), |
| 1252 | const_cast<String*>(declaring_class), false); |
| 1253 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), |
| 1254 | const_cast<String*>(method_name), false); |
| 1255 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), |
| 1256 | const_cast<String*>(file_name), false); |
| 1257 | trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), |
| 1258 | line_number, false); |
| 1259 | return trace; |
| 1260 | } |
| 1261 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1262 | static const char* kClassStatusNames[] = { |
| 1263 | "Error", |
| 1264 | "NotReady", |
| 1265 | "Idx", |
| 1266 | "Loaded", |
| 1267 | "Resolved", |
| 1268 | "Verifying", |
| 1269 | "Verified", |
| 1270 | "Initializing", |
| 1271 | "Initialized" |
| 1272 | }; |
| 1273 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) { |
| 1274 | if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) { |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 1275 | os << kClassStatusNames[rhs + 1]; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1276 | } else { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1277 | os << "Class::Status[" << static_cast<int>(rhs) << "]"; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1278 | } |
| 1279 | return os; |
| 1280 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1281 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1282 | } // namespace art |