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