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