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