Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_OBJECT_H_ |
| 4 | #define ART_SRC_OBJECT_H_ |
| 5 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 6 | #include "constants.h" |
| 7 | #include "casts.h" |
| 8 | #include "globals.h" |
| 9 | #include "logging.h" |
| 10 | #include "macros.h" |
| 11 | #include "offsets.h" |
| 12 | #include "stringpiece.h" |
| 13 | #include "monitor.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 14 | |
| 15 | namespace art { |
| 16 | |
| 17 | class Array; |
| 18 | class Class; |
| 19 | class DexFile; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 20 | class InstanceField; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 21 | class InterfaceEntry; |
| 22 | class Monitor; |
| 23 | class Method; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 24 | class Object; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 25 | class ObjectArray; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 26 | class StaticField; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 27 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 28 | union JValue { |
| 29 | uint8_t z; |
| 30 | int8_t b; |
| 31 | uint16_t c; |
| 32 | int16_t s; |
| 33 | int32_t i; |
| 34 | int64_t j; |
| 35 | float f; |
| 36 | double d; |
| 37 | Object* l; |
| 38 | }; |
| 39 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 40 | static const uint32_t kAccPublic = 0x0001; // class, field, method, ic |
| 41 | static const uint32_t kAccPrivate = 0x0002; // field, method, ic |
| 42 | static const uint32_t kAccProtected = 0x0004; // field, method, ic |
| 43 | static const uint32_t kAccStatic = 0x0008; // field, method, ic |
| 44 | static const uint32_t kAccFinal = 0x0010; // class, field, method, ic |
| 45 | static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives) |
| 46 | static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik) |
| 47 | static const uint32_t kAccVolatile = 0x0040; // field |
| 48 | static const uint32_t kAccBridge = 0x0040; // method (1.5) |
| 49 | static const uint32_t kAccTransient = 0x0080; // field |
| 50 | static const uint32_t kAccVarargs = 0x0080; // method (1.5) |
| 51 | static const uint32_t kAccNative = 0x0100; // method |
| 52 | static const uint32_t kAccInterface = 0x0200; // class, ic |
| 53 | static const uint32_t kAccAbstract = 0x0400; // class, method, ic |
| 54 | static const uint32_t kAccStrict = 0x0800; // method |
| 55 | static const uint32_t kAccSynthetic = 0x1000; // field, method, ic |
| 56 | static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5) |
| 57 | static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5) |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 58 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 59 | static const uint32_t kAccMiranda = 0x8000; // method |
| 60 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 61 | static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16) |
| 62 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 63 | static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only) |
| 64 | static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only) |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 65 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 66 | /* |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 67 | * Definitions for packing refOffsets in Class. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 68 | */ |
| 69 | /* |
| 70 | * A magic value for refOffsets. Ignore the bits and walk the super |
| 71 | * chain when this is the value. |
| 72 | * [This is an unlikely "natural" value, since it would be 30 non-ref instance |
| 73 | * fields followed by 2 ref instance fields.] |
| 74 | */ |
| 75 | #define CLASS_WALK_SUPER ((unsigned int)(3)) |
| 76 | #define CLASS_SMALLEST_OFFSET (sizeof(struct Object)) |
| 77 | #define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8) |
| 78 | #define CLASS_OFFSET_ALIGNMENT 4 |
| 79 | #define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1)) |
| 80 | /* |
| 81 | * Given an offset, return the bit number which would encode that offset. |
| 82 | * Local use only. |
| 83 | */ |
| 84 | #define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \ |
| 85 | (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \ |
| 86 | CLASS_OFFSET_ALIGNMENT) |
| 87 | /* |
| 88 | * Is the given offset too large to be encoded? |
| 89 | */ |
| 90 | #define CLASS_CAN_ENCODE_OFFSET(byteOffset) \ |
| 91 | (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD) |
| 92 | /* |
| 93 | * Return a single bit, encoding the offset. |
| 94 | * Undefined if the offset is too large, as defined above. |
| 95 | */ |
| 96 | #define CLASS_BIT_FROM_OFFSET(byteOffset) \ |
| 97 | (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset)) |
| 98 | /* |
| 99 | * Return an offset, given a bit number as returned from CLZ. |
| 100 | */ |
| 101 | #define CLASS_OFFSET_FROM_CLZ(rshift) \ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 102 | ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 103 | |
| 104 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 105 | class Object { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 106 | public: |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 107 | Class* GetClass() const { |
| 108 | return klass_; |
| 109 | } |
| 110 | |
| 111 | void MonitorEnter() { |
| 112 | monitor_->Enter(); |
| 113 | } |
| 114 | |
| 115 | void MonitorExit() { |
| 116 | monitor_->Exit(); |
| 117 | } |
| 118 | |
| 119 | void Notify() { |
| 120 | monitor_->Notify(); |
| 121 | } |
| 122 | |
| 123 | void NotifyAll() { |
| 124 | monitor_->NotifyAll(); |
| 125 | } |
| 126 | |
| 127 | void Wait() { |
| 128 | monitor_->Wait(); |
| 129 | } |
| 130 | |
| 131 | void Wait(int64_t timeout) { |
| 132 | monitor_->Wait(timeout); |
| 133 | } |
| 134 | |
| 135 | void Wait(int64_t timeout, int32_t nanos) { |
| 136 | monitor_->Wait(timeout, nanos); |
| 137 | } |
| 138 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 139 | const Object* GetFieldObject(size_t field_offset) const { |
| 140 | const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset; |
| 141 | return *reinterpret_cast<Object* const*>(raw_addr); |
| 142 | } |
| 143 | |
| 144 | Object* GetFieldObject(size_t field_offset) { |
| 145 | return const_cast<Object*>(GetFieldObject(field_offset)); |
| 146 | } |
| 147 | |
| 148 | void SetFieldObject(size_t offset, Object* new_value) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 149 | byte* raw_addr = reinterpret_cast<byte*>(this) + offset; |
| 150 | *reinterpret_cast<Object**>(raw_addr) = new_value; |
| 151 | // TODO: write barrier |
| 152 | } |
| 153 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 154 | bool IsClass() const { |
| 155 | LOG(FATAL) << "Unimplemented"; |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | Class* AsClass() { |
| 160 | return down_cast<Class*>(this); |
| 161 | } |
| 162 | |
| 163 | const Class* AsClass() const { |
| 164 | return down_cast<const Class*>(this); |
| 165 | } |
| 166 | |
| 167 | bool IsObjectArray() const { |
| 168 | LOG(FATAL) << "Unimplemented"; |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | const ObjectArray* AsObjectArray() const { |
| 173 | return down_cast<const ObjectArray*>(this); |
| 174 | } |
| 175 | |
| 176 | bool IsReference() const { |
| 177 | LOG(FATAL) << "Unimplemented"; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | bool IsWeakReference() const { |
| 182 | LOG(FATAL) << "Unimplemented"; |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool IsSoftReference() const { |
| 187 | LOG(FATAL) << "Unimplemented"; |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | bool IsFinalizerReference() const { |
| 192 | LOG(FATAL) << "Unimplemented"; |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | bool IsPhantomReference() const { |
| 197 | LOG(FATAL) << "Unimplemented"; |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | bool IsArray() const { |
| 202 | LOG(FATAL) << "Unimplemented"; |
| 203 | return true; |
| 204 | } |
| 205 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 206 | public: |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 207 | Class* klass_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 208 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 209 | Monitor* monitor_; |
| 210 | |
| 211 | private: |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 212 | Object(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 213 | DISALLOW_COPY_AND_ASSIGN(Object); |
| 214 | }; |
| 215 | |
| 216 | class ObjectLock { |
| 217 | public: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 218 | explicit ObjectLock(Object* object) : obj_(object) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 219 | CHECK(object != NULL); |
| 220 | obj_->MonitorEnter(); |
| 221 | } |
| 222 | |
| 223 | ~ObjectLock() { |
| 224 | obj_->MonitorExit(); |
| 225 | } |
| 226 | |
| 227 | void Wait(int64_t millis = 0) { |
| 228 | return obj_->Wait(millis); |
| 229 | } |
| 230 | |
| 231 | void Notify() { |
| 232 | obj_->Notify(); |
| 233 | } |
| 234 | |
| 235 | void NotifyAll() { |
| 236 | obj_->NotifyAll(); |
| 237 | } |
| 238 | |
| 239 | private: |
| 240 | Object* obj_; |
| 241 | DISALLOW_COPY_AND_ASSIGN(ObjectLock); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 244 | class Field : public Object { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 245 | public: |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 246 | Class* GetDeclaringClass() const { |
| 247 | return declaring_class_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | const char* GetName() const { |
| 251 | return name_; |
| 252 | } |
| 253 | |
| 254 | char GetType() const { // TODO: return type |
| 255 | return signature_[0]; |
| 256 | } |
| 257 | |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 258 | const char* GetSignature() const { |
| 259 | return signature_; |
| 260 | } |
| 261 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 262 | public: // TODO: private |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 263 | #define FIELD_FIELD_SLOTS 1+6 |
| 264 | // AccessibleObject #0 flag |
| 265 | // Field #0 declaringClass |
| 266 | // Field #1 genericType |
| 267 | // Field #2 genericTypesAreInitialized |
| 268 | // Field #3 name |
| 269 | // Field #4 slot |
| 270 | // Field #5 type |
| 271 | uint32_t instance_data_[FIELD_FIELD_SLOTS]; |
| 272 | #undef FIELD_FIELD_SLOTS |
| 273 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 274 | // The class in which this field is declared. |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 275 | Class* declaring_class_; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 276 | |
| 277 | const char* name_; |
| 278 | |
| 279 | // e.g. "I", "[C", "Landroid/os/Debug;" |
| 280 | const char* signature_; |
| 281 | |
| 282 | uint32_t access_flags_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 283 | |
| 284 | private: |
| 285 | Field(); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | // Instance fields. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 289 | class InstanceField : public Field { |
| 290 | public: |
| 291 | uint32_t GetOffset() const { |
| 292 | return offset_; |
| 293 | } |
| 294 | |
| 295 | void SetOffset(size_t num_bytes) { |
| 296 | offset_ = num_bytes; |
| 297 | } |
| 298 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 299 | private: |
| 300 | size_t offset_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 301 | InstanceField(); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | // Static fields. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 305 | class StaticField : public Field { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 306 | public: |
| 307 | void SetBoolean(bool z) { |
| 308 | CHECK_EQ(GetType(), 'Z'); |
| 309 | value_.z = z; |
| 310 | } |
| 311 | |
| 312 | void SetByte(int8_t b) { |
| 313 | CHECK_EQ(GetType(), 'B'); |
| 314 | value_.b = b; |
| 315 | } |
| 316 | |
| 317 | void SetChar(uint16_t c) { |
| 318 | CHECK_EQ(GetType(), 'C'); |
| 319 | value_.c = c; |
| 320 | } |
| 321 | |
| 322 | void SetShort(uint16_t s) { |
| 323 | CHECK_EQ(GetType(), 'S'); |
| 324 | value_.s = s; |
| 325 | } |
| 326 | |
| 327 | void SetInt(int32_t i) { |
| 328 | CHECK_EQ(GetType(), 'I'); |
| 329 | value_.i = i; |
| 330 | } |
| 331 | |
| 332 | int64_t GetLong() { |
| 333 | CHECK_EQ(GetType(), 'J'); |
| 334 | return value_.j; |
| 335 | } |
| 336 | |
| 337 | void SetLong(int64_t j) { |
| 338 | CHECK_EQ(GetType(), 'J'); |
| 339 | value_.j = j; |
| 340 | } |
| 341 | |
| 342 | void SetFloat(float f) { |
| 343 | CHECK_EQ(GetType(), 'F'); |
| 344 | value_.f = f; |
| 345 | } |
| 346 | |
| 347 | void SetDouble(double d) { |
| 348 | CHECK_EQ(GetType(), 'D'); |
| 349 | value_.d = d; |
| 350 | } |
| 351 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 352 | Object* GetObject() { |
| 353 | return value_.l; |
| 354 | } |
| 355 | |
| 356 | const Object* GetObject() const { |
| 357 | return value_.l; |
| 358 | } |
| 359 | |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 360 | void SetObject(Object* l) { |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 361 | CHECK(GetType() == 'L' || GetType() == '['); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 362 | value_.l = l; |
| 363 | // TODO: write barrier |
| 364 | } |
| 365 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 366 | private: |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 367 | JValue value_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 368 | StaticField(); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 369 | }; |
| 370 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 371 | class Method : public Object { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 372 | public: |
| 373 | // Returns the method name. |
| 374 | // TODO: example |
| 375 | const StringPiece& GetName() const { |
| 376 | return name_; |
| 377 | } |
| 378 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 379 | Class* GetDeclaringClass() const { |
| 380 | return declaring_class_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 383 | static MemberOffset ClassOffset() { |
| 384 | return MemberOffset(OFFSETOF_MEMBER(Method, klass_)); |
| 385 | } |
| 386 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 387 | // const char* GetReturnTypeDescriptor() const { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 388 | // return declaring_class_->GetDexFile_->GetRaw() |
| 389 | // ->dexStringByTypeIdx(proto_id_.return_type_id_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 390 | // } |
| 391 | |
| 392 | // Returns true if the method is declared public. |
| 393 | bool IsPublic() const { |
| 394 | return (access_flags_ & kAccPublic) != 0; |
| 395 | } |
| 396 | |
| 397 | // Returns true if the method is declared private. |
| 398 | bool IsPrivate() const { |
| 399 | return (access_flags_ & kAccPrivate) != 0; |
| 400 | } |
| 401 | |
| 402 | // Returns true if the method is declared static. |
| 403 | bool IsStatic() const { |
| 404 | return (access_flags_ & kAccStatic) != 0; |
| 405 | } |
| 406 | |
| 407 | // Returns true if the method is declared synchronized. |
| 408 | bool IsSynchronized() const { |
| 409 | uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized; |
| 410 | return (access_flags_ & synchonized) != 0; |
| 411 | } |
| 412 | |
| 413 | // Returns true if the method is declared final. |
| 414 | bool IsFinal() const { |
| 415 | return (access_flags_ & kAccFinal) != 0; |
| 416 | } |
| 417 | |
| 418 | // Returns true if the method is declared native. |
| 419 | bool IsNative() const { |
| 420 | return (access_flags_ & kAccNative) != 0; |
| 421 | } |
| 422 | |
| 423 | // Returns true if the method is declared abstract. |
| 424 | bool IsAbstract() const { |
| 425 | return (access_flags_ & kAccAbstract) != 0; |
| 426 | } |
| 427 | |
| 428 | bool IsSynthetic() const { |
| 429 | return (access_flags_ & kAccSynthetic) != 0; |
| 430 | } |
| 431 | |
| 432 | // Number of argument registers required by the prototype. |
| 433 | uint32_t NumArgRegisters(); |
| 434 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 435 | public: // TODO: private |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 436 | #define METHOD_FIELD_SLOTS 1+11 |
| 437 | // AccessibleObject #0 flag |
| 438 | // Method #0 declaringClass |
| 439 | // Method #1 exceptionTypes |
| 440 | // Method #2 formalTypeParameters |
| 441 | // Method #3 genericExceptionTypes |
| 442 | // Method #4 genericParameterTypes |
| 443 | // Method #5 genericReturnType |
| 444 | // Method #6 genericTypesAreInitialized |
| 445 | // Method #7 name |
| 446 | // Method #8 parameterTypes |
| 447 | // Method #9 returnType |
| 448 | // Method #10 slot |
| 449 | uint32_t instance_data_[METHOD_FIELD_SLOTS]; |
| 450 | #undef METHOD_FIELD_SLOTS |
| 451 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 452 | bool IsReturnAReference() const { |
| 453 | return (shorty_[0] == 'L') || (shorty_[0] == '['); |
| 454 | } |
| 455 | |
| 456 | bool IsReturnAFloatOrDouble() const { |
| 457 | return (shorty_[0] == 'F') || (shorty_[0] == 'D'); |
| 458 | } |
| 459 | |
| 460 | bool IsReturnAFloat() const { |
| 461 | return shorty_[0] == 'F'; |
| 462 | } |
| 463 | |
| 464 | bool IsReturnADouble() const { |
| 465 | return shorty_[0] == 'D'; |
| 466 | } |
| 467 | |
| 468 | bool IsReturnALong() const { |
| 469 | return shorty_[0] == 'J'; |
| 470 | } |
| 471 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 472 | bool IsReturnVoid() const { |
| 473 | return shorty_[0] == 'V'; |
| 474 | } |
| 475 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 476 | // The number of arguments that should be supplied to this method |
| 477 | size_t NumArgs() const { |
| 478 | return (IsStatic() ? 0 : 1) + shorty_.length() - 1; |
| 479 | } |
| 480 | |
| 481 | // The number of reference arguments to this method including implicit this |
| 482 | // pointer |
| 483 | size_t NumReferenceArgs() const; |
| 484 | |
| 485 | // The number of long or double arguments |
| 486 | size_t NumLongOrDoubleArgs() const; |
| 487 | |
| 488 | // The number of reference arguments to this method before the given |
| 489 | // parameter index |
| 490 | size_t NumReferenceArgsBefore(unsigned int param) const; |
| 491 | |
| 492 | // Is the given method parameter a reference? |
| 493 | bool IsParamAReference(unsigned int param) const; |
| 494 | |
| 495 | // Is the given method parameter a long or double? |
| 496 | bool IsParamALongOrDouble(unsigned int param) const; |
| 497 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 498 | // Size in bytes of the given parameter |
| 499 | size_t ParamSize(unsigned int param) const; |
| 500 | |
| 501 | // Size in bytes of the return value |
| 502 | size_t ReturnSize() const; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 503 | |
| 504 | void SetCode(const void* code) { |
| 505 | code_ = code; |
| 506 | } |
| 507 | |
| 508 | const void* GetCode() const { |
| 509 | return code_; |
| 510 | } |
| 511 | |
| 512 | void RegisterNative(const void* native_method) { |
| 513 | native_method_ = native_method; |
| 514 | } |
| 515 | |
| 516 | static MemberOffset NativeMethodOffset() { |
| 517 | return MemberOffset(OFFSETOF_MEMBER(Method, native_method_)); |
| 518 | } |
| 519 | |
| 520 | public: // TODO: private/const |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 521 | // the class we are a part of |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 522 | Class* declaring_class_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 523 | |
| 524 | // access flags; low 16 bits are defined by spec (could be uint16_t?) |
| 525 | uint32_t access_flags_; |
| 526 | |
| 527 | // For concrete virtual methods, this is the offset of the method |
| 528 | // in "vtable". |
| 529 | // |
| 530 | // For abstract methods in an interface class, this is the offset |
| 531 | // of the method in "iftable[n]->methodIndexArray". |
| 532 | uint16_t method_index_; |
| 533 | |
| 534 | // Method bounds; not needed for an abstract method. |
| 535 | // |
| 536 | // For a native method, we compute the size of the argument list, and |
| 537 | // set "insSize" and "registerSize" equal to it. |
| 538 | uint16_t num_registers_; // ins + locals |
| 539 | uint16_t num_outs_; |
| 540 | uint16_t num_ins_; |
| 541 | |
| 542 | // method name, e.g. "<init>" or "eatLunch" |
| 543 | StringPiece name_; |
| 544 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 545 | // Method prototype descriptor string (return and argument types). |
| 546 | uint32_t proto_idx_; |
| 547 | |
| 548 | // The short-form method descriptor string. |
| 549 | StringPiece shorty_; |
| 550 | |
| 551 | // A pointer to the memory-mapped DEX code. |
| 552 | const uint16_t* insns_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 553 | |
| 554 | private: |
| 555 | Method(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 556 | |
| 557 | // Compiled code associated with this method |
| 558 | const void* code_; |
| 559 | |
| 560 | // Any native method registered with this method |
| 561 | const void* native_method_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 562 | }; |
| 563 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 564 | // Class objects. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 565 | class Class : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 566 | public: |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 567 | enum Status { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 568 | kStatusError = -1, |
| 569 | kStatusNotReady = 0, |
| 570 | kStatusIdx = 1, // loaded, DEX idx in super or ifaces |
| 571 | kStatusLoaded = 2, // DEX idx values resolved |
| 572 | kStatusResolved = 3, // part of linking |
| 573 | kStatusVerifying = 4, // in the process of being verified |
| 574 | kStatusVerified = 5, // logically part of linking; done pre-init |
| 575 | kStatusInitializing = 6, // class init in progress |
| 576 | kStatusInitialized = 7, // ready to go |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | enum PrimitiveType { |
| 580 | kPrimNot = -1 |
| 581 | }; |
| 582 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 583 | Class* GetSuperClass() const { |
| 584 | return super_class_; |
| 585 | } |
| 586 | |
| 587 | uint32_t GetSuperClassIdx() const { |
| 588 | return super_class_idx_; |
| 589 | } |
| 590 | |
| 591 | bool HasSuperClass() const { |
| 592 | return super_class_ != NULL; |
| 593 | } |
| 594 | |
| 595 | Object* GetClassLoader() const { |
| 596 | return class_loader_; |
| 597 | } |
| 598 | |
| 599 | DexFile* GetDexFile() const { |
| 600 | return dex_file_; |
| 601 | } |
| 602 | |
| 603 | Class* GetComponentType() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 604 | DCHECK(IsArray()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 605 | return component_type_; |
| 606 | } |
| 607 | |
| 608 | const StringPiece& GetDescriptor() const { |
| 609 | return descriptor_; |
| 610 | } |
| 611 | |
| 612 | Status GetStatus() const { |
| 613 | return status_; |
| 614 | } |
| 615 | |
| 616 | void SetStatus(Status new_status) { |
| 617 | // TODO: validate transition |
| 618 | status_ = new_status; |
| 619 | } |
| 620 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 621 | // Returns true if the class has failed to link. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 622 | bool IsErroneous() const { |
| 623 | return GetStatus() == kStatusError; |
| 624 | } |
| 625 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 626 | // Returns true if the class has been verified. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 627 | bool IsVerified() const { |
| 628 | return GetStatus() >= kStatusVerified; |
| 629 | } |
| 630 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 631 | // Returns true if the class has been linked. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 632 | bool IsLinked() const { |
| 633 | return GetStatus() >= kStatusResolved; |
| 634 | } |
| 635 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 636 | bool IsLoaded() const { |
| 637 | return GetStatus() >= kStatusLoaded; |
| 638 | } |
| 639 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 640 | // Returns true if this class is in the same packages as that class. |
| 641 | bool IsInSamePackage(const Class* that) const; |
| 642 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 643 | static bool IsInSamePackage(const StringPiece& descriptor1, |
| 644 | const StringPiece& descriptor2); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 645 | |
| 646 | // Returns true if this class represents an array class. |
| 647 | bool IsArray() const { |
| 648 | return descriptor_[0] == '['; // TODO: avoid parsing the descriptor |
| 649 | } |
| 650 | |
| 651 | // Returns true if the class is an interface. |
| 652 | bool IsInterface() const { |
| 653 | return (access_flags_ & kAccInterface) != 0; |
| 654 | } |
| 655 | |
| 656 | // Returns true if the class is declared public. |
| 657 | bool IsPublic() const { |
| 658 | return (access_flags_ & kAccPublic) != 0; |
| 659 | } |
| 660 | |
| 661 | // Returns true if the class is declared final. |
| 662 | bool IsFinal() const { |
| 663 | return (access_flags_ & kAccFinal) != 0; |
| 664 | } |
| 665 | |
| 666 | // Returns true if the class is abstract. |
| 667 | bool IsAbstract() const { |
| 668 | return (access_flags_ & kAccAbstract) != 0; |
| 669 | } |
| 670 | |
| 671 | // Returns true if the class is an annotation. |
| 672 | bool IsAnnotation() const { |
| 673 | return (access_flags_ & kAccAnnotation) != 0; |
| 674 | } |
| 675 | |
| 676 | // Returns true if the class is a primitive type. |
| 677 | bool IsPrimitive() const { |
| 678 | return primitive_type_ != kPrimNot; |
| 679 | } |
| 680 | |
| 681 | // Returns true if this class can access that class. |
| 682 | bool CanAccess(const Class* that) const { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 683 | return that->IsPublic() || this->IsInSamePackage(that); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 686 | // Returns the size in bytes of a class object instance with the |
| 687 | // given number of static fields. |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 688 | // static size_t Size(size_t num_sfields) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 689 | // return OFFSETOF_MEMBER(Class, sfields_) + |
| 690 | // sizeof(StaticField) * num_sfields; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 691 | // } |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 692 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 693 | // Returns the number of static, private, and constructor methods. |
| 694 | size_t NumDirectMethods() const { |
| 695 | return num_direct_methods_; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 698 | Method* GetDirectMethod(uint32_t i) const { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 699 | return direct_methods_[i]; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 702 | // Returns the number of non-inherited virtual methods. |
| 703 | size_t NumVirtualMethods() const { |
| 704 | return num_virtual_methods_; |
| 705 | } |
| 706 | |
| 707 | Method* GetVirtualMethod(uint32_t i) const { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 708 | return virtual_methods_[i]; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | size_t NumInstanceFields() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 712 | return num_instance_fields_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 715 | // Returns the number of instance fields containing reference types. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 716 | size_t NumReferenceInstanceFields() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 717 | return num_reference_instance_fields_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 721 | DCHECK_LT(i, num_instance_fields_); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 722 | return ifields_[i]; |
| 723 | } |
| 724 | |
| 725 | void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 726 | DCHECK_LT(i, num_instance_fields_); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 727 | ifields_[i] = f; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | size_t NumStaticFields() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 731 | return num_static_fields_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 734 | StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t |
| 735 | DCHECK_LT(i, num_static_fields_); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 736 | return sfields_[i]; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | uint32_t GetReferenceOffsets() const { |
| 740 | return reference_offsets_; |
| 741 | } |
| 742 | |
| 743 | void SetReferenceOffsets(uint32_t new_reference_offsets) { |
| 744 | reference_offsets_ = new_reference_offsets; |
| 745 | } |
| 746 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 747 | Method* FindDirectMethod(const StringPiece& name) const; |
| 748 | |
| 749 | Method* FindVirtualMethod(const StringPiece& name) const; |
| 750 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 751 | size_t NumInterfaces() const { |
| 752 | return interface_count_; |
| 753 | } |
| 754 | |
| 755 | Class* GetInterface(uint32_t i) const { |
| 756 | DCHECK_LT(i, interface_count_); |
| 757 | return interfaces_[i]; |
| 758 | } |
| 759 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 760 | Method* FindDirectMethodLocally(const StringPiece& name, |
| 761 | const StringPiece& descriptor) const; |
| 762 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 763 | public: // TODO: private |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 764 | // leave space for instance data; we could access fields directly if |
| 765 | // we freeze the definition of java/lang/Class |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 766 | #define CLASS_FIELD_SLOTS 1 |
| 767 | // Class.#0 name |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 768 | uint32_t instance_data_[CLASS_FIELD_SLOTS]; |
| 769 | #undef CLASS_FIELD_SLOTS |
| 770 | |
| 771 | // UTF-8 descriptor for the class from constant pool |
| 772 | // ("Ljava/lang/Class;"), or on heap if generated ("[C") |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 773 | StringPiece descriptor_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 774 | |
| 775 | // Proxy classes have their descriptor allocated on the native heap. |
| 776 | // When this field is non-NULL it must be explicitly freed. |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 777 | std::string* descriptor_alloc_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 778 | |
| 779 | // access flags; low 16 bits are defined by VM spec |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 780 | uint32_t access_flags_; // TODO: make an instance field? |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 781 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 782 | // DexFile from which we came; needed to resolve constant pool entries |
| 783 | // (will be NULL for VM-generated, e.g. arrays and primitive classes) |
| 784 | DexFile* dex_file_; |
| 785 | |
| 786 | // state of class initialization |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 787 | Status status_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 788 | |
| 789 | // if class verify fails, we must return same error on subsequent tries |
| 790 | Class* verify_error_class_; |
| 791 | |
| 792 | // threadId, used to check for recursive <clinit> invocation |
| 793 | uint32_t clinit_thread_id_; |
| 794 | |
| 795 | // Total object size; used when allocating storage on gc heap. (For |
| 796 | // interfaces and abstract classes this will be zero.) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 797 | size_t object_size_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 798 | |
| 799 | // For array classes, the class object for base element, for |
| 800 | // instanceof/checkcast (for String[][][], this will be String). |
| 801 | // Otherwise, NULL. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 802 | Class* component_type_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 803 | |
| 804 | // For array classes, the number of array dimensions, e.g. int[][] |
| 805 | // is 2. Otherwise 0. |
| 806 | int32_t array_rank_; |
| 807 | |
| 808 | // primitive type index, or PRIM_NOT (-1); set for generated prim classes |
| 809 | PrimitiveType primitive_type_; |
| 810 | |
| 811 | // The superclass, or NULL if this is java.lang.Object or a |
| 812 | // primitive type. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 813 | Class* super_class_; // TODO: make an instance field |
| 814 | uint32_t super_class_idx_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 815 | |
| 816 | // defining class loader, or NULL for the "bootstrap" system loader |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 817 | Object* class_loader_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 818 | |
| 819 | // initiating class loader list |
| 820 | // NOTE: for classes with low serialNumber, these are unused, and the |
| 821 | // values are kept in a table in gDvm. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 822 | // InitiatingLoaderList initiating_loader_list_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 823 | |
| 824 | // array of interfaces this class implements directly |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 825 | size_t interface_count_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 826 | Class** interfaces_; |
| 827 | |
| 828 | // static, private, and <init> methods |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 829 | size_t num_direct_methods_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 830 | Method** direct_methods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 831 | |
| 832 | // virtual methods defined in this class; invoked through vtable |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 833 | size_t num_virtual_methods_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 834 | Method** virtual_methods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 835 | |
| 836 | // Virtual method table (vtable), for use by "invoke-virtual". The |
| 837 | // vtable from the superclass is copied in, and virtual methods from |
| 838 | // our class either replace those from the super or are appended. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 839 | size_t vtable_count_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 840 | Method** vtable_; |
| 841 | |
| 842 | // Interface table (iftable), one entry per interface supported by |
| 843 | // this class. That means one entry for each interface we support |
| 844 | // directly, indirectly via superclass, or indirectly via |
| 845 | // superinterface. This will be null if neither we nor our |
| 846 | // superclass implement any interfaces. |
| 847 | // |
| 848 | // Why we need this: given "class Foo implements Face", declare |
| 849 | // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah" |
| 850 | // is part of the Face interface. We can't easily use a single |
| 851 | // vtable. |
| 852 | // |
| 853 | // For every interface a concrete class implements, we create a list |
| 854 | // of virtualMethod indices for the methods in the interface. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 855 | size_t iftable_count_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 856 | InterfaceEntry* iftable_; |
| 857 | |
| 858 | // The interface vtable indices for iftable get stored here. By |
| 859 | // placing them all in a single pool for each class that implements |
| 860 | // interfaces, we decrease the number of allocations. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 861 | size_t ifvi_pool_count_; |
| 862 | uint32_t* ifvi_pool_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 863 | |
| 864 | // instance fields |
| 865 | // |
| 866 | // These describe the layout of the contents of a |
| 867 | // DataObject-compatible Object. Note that only the fields directly |
| 868 | // declared by this class are listed in ifields; fields declared by |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 869 | // a superclass are listed in the superclass's Class.ifields. |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 870 | // |
| 871 | // All instance fields that refer to objects are guaranteed to be at |
| 872 | // the beginning of the field list. ifieldRefCount specifies the |
| 873 | // number of reference fields. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 874 | size_t num_instance_fields_; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 875 | |
| 876 | // number of fields that are object refs |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 877 | size_t num_reference_instance_fields_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 878 | InstanceField** ifields_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 879 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 880 | // Bitmap of offsets of ifields. |
| 881 | uint32_t reference_offsets_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 882 | |
| 883 | // source file name, if known. Otherwise, NULL. |
| 884 | const char* source_file_; |
| 885 | |
| 886 | // Static fields |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 887 | size_t num_static_fields_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 888 | StaticField** sfields_; |
| 889 | |
| 890 | private: |
| 891 | Class(); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 892 | }; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 893 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 894 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 895 | class DataObject : public Object { |
| 896 | public: |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 897 | uint32_t fields_[0]; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 898 | private: |
| 899 | DataObject(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 900 | }; |
| 901 | |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 902 | class Array : public Object { |
| 903 | public: |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 904 | uint32_t GetLength() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 905 | return length_; |
| 906 | } |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 907 | void SetLength(uint32_t length) { |
| 908 | length_ = length; |
| 909 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 910 | const void* GetData() const { |
| 911 | return &elements_; |
| 912 | } |
| 913 | void* GetData() { |
| 914 | return &elements_; |
| 915 | } |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 916 | |
| 917 | private: |
| 918 | // The number of array elements. |
| 919 | uint32_t length_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 920 | // Location of first element. |
| 921 | uint32_t elements_[0]; |
| 922 | Array(); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 923 | }; |
| 924 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 925 | class CharArray : public Array { |
| 926 | private: |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 927 | DISALLOW_IMPLICIT_CONSTRUCTORS(CharArray); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 928 | }; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 929 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 930 | class ObjectArray : public Array { |
| 931 | public: |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 932 | Object* Get(uint32_t i) const { |
| 933 | DCHECK_LT(i, GetLength()); |
| 934 | Object* const * data = reinterpret_cast<Object* const *>(GetData()); |
| 935 | return data[i]; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 936 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 937 | void Set(uint32_t i, Object* object) { |
| 938 | DCHECK_LT(i, GetLength()); |
| 939 | Object** data = reinterpret_cast<Object**>(GetData()); |
| 940 | data[i] = object; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 941 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 942 | private: |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 943 | ObjectArray(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 944 | }; |
| 945 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 946 | class String : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 947 | public: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 948 | CharArray* array_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 949 | |
| 950 | uint32_t hash_code_; |
| 951 | |
| 952 | uint32_t offset_; |
| 953 | |
| 954 | uint32_t count_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 955 | |
| 956 | private: |
| 957 | String(); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 958 | }; |
| 959 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 960 | class InterfaceEntry { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 961 | public: |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 962 | Class* GetClass() const { |
| 963 | return klass_; |
| 964 | }; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 965 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 966 | void SetClass(Class* klass) { |
| 967 | klass_ = klass; |
| 968 | }; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 969 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 970 | private: |
| 971 | // Points to the interface class. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 972 | Class* klass_; |
| 973 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 974 | public: // TODO: private |
| 975 | // Index into array of vtable offsets. This points into the |
| 976 | // ifviPool, which holds the vtables for all interfaces declared by |
| 977 | // this class. |
| 978 | uint32_t* method_index_array_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 979 | }; |
| 980 | |
| 981 | } // namespace art |
| 982 | |
| 983 | #endif // ART_SRC_OBJECT_H_ |