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 | |
| 6 | #include "src/globals.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 7 | #include "src/macros.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 8 | |
| 9 | namespace art { |
| 10 | |
| 11 | class Array; |
| 12 | class Class; |
| 13 | class DexFile; |
| 14 | class IField; |
| 15 | class InterfaceEntry; |
| 16 | class Monitor; |
| 17 | class Method; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 18 | class Object; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 19 | class SField; |
| 20 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 21 | union JValue { |
| 22 | uint8_t z; |
| 23 | int8_t b; |
| 24 | uint16_t c; |
| 25 | int16_t s; |
| 26 | int32_t i; |
| 27 | int64_t j; |
| 28 | float f; |
| 29 | double d; |
| 30 | Object* l; |
| 31 | }; |
| 32 | |
| 33 | static const uint32_t kAccPublic = 0x0001; // class, field, method, ic |
| 34 | static const uint32_t kAccPrivate = 0x0002; // field, method, ic |
| 35 | static const uint32_t kAccProtected = 0x0004; // field, method, ic |
| 36 | static const uint32_t kAccStatic = 0x0008; // field, method, ic |
| 37 | static const uint32_t kAccFinal = 0x0010; // class, field, method, ic |
| 38 | static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives) |
| 39 | static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik) |
| 40 | static const uint32_t kAccVolatile = 0x0040; // field |
| 41 | static const uint32_t kAccBridge = 0x0040; // method (1.5) |
| 42 | static const uint32_t kAccTransient = 0x0080; // field |
| 43 | static const uint32_t kAccVarargs = 0x0080; // method (1.5) |
| 44 | static const uint32_t kAccNative = 0x0100; // method |
| 45 | static const uint32_t kAccInterface = 0x0200; // class, ic |
| 46 | static const uint32_t kAccAbstract = 0x0400; // class, method, ic |
| 47 | static const uint32_t kAccStrict = 0x0800; // method |
| 48 | static const uint32_t kAccSynthetic = 0x1000; // field, method, ic |
| 49 | static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5) |
| 50 | static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5) |
| 51 | |
| 52 | static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only) |
| 53 | static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only) |
| 54 | |
| 55 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 56 | class Object { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 57 | public: |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 58 | Class* klass_; |
| 59 | Monitor* lock_; |
| 60 | }; |
| 61 | |
| 62 | class Field { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 63 | public: |
| 64 | // The class in which this field is declared. |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 65 | Class* klass_; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 66 | |
| 67 | const char* name_; |
| 68 | |
| 69 | // e.g. "I", "[C", "Landroid/os/Debug;" |
| 70 | const char* signature_; |
| 71 | |
| 72 | uint32_t access_flags_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | // Instance fields. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 76 | class IField : public Field { |
| 77 | uint32_t offset_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | // Static fields. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 81 | class SField : public Field { |
| 82 | JValue value_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | // Class objects. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 86 | class Class : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 87 | public: |
| 88 | enum ClassStatus { |
| 89 | kClassError = -1, |
| 90 | kClassNotReady = 0, |
| 91 | kClassIdx = 1, // loaded, DEX idx in super or ifaces |
| 92 | kClassLoaded = 2, // DEX idx values resolved |
| 93 | kClassResolved = 3, // part of linking |
| 94 | kClassVerifying = 4, // in the process of being verified |
| 95 | kClassVerified = 5, // logically part of linking; done pre-init |
| 96 | kClassInitializing = 6, // class init in progress |
| 97 | kClassInitialized = 7, // ready to go |
| 98 | }; |
| 99 | |
| 100 | enum PrimitiveType { |
| 101 | kPrimNot = -1 |
| 102 | }; |
| 103 | |
| 104 | // Returns the size in bytes of a class object instance with the |
| 105 | // given number of static fields. |
| 106 | static size_t Size(size_t num_sfields) { |
| 107 | return OFFSETOF_MEMBER(Class, sfields_) + sizeof(SField) * num_sfields; |
| 108 | } |
| 109 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 110 | uint32_t NumDirectMethods() { |
| 111 | return num_dmethods_; |
| 112 | } |
| 113 | |
| 114 | uint32_t NumVirtualMethods() { |
| 115 | return num_vmethods_; |
| 116 | } |
| 117 | |
| 118 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 119 | public: // TODO: private |
| 120 | // leave space for instance data; we could access fields directly if |
| 121 | // we freeze the definition of java/lang/Class |
| 122 | #define CLASS_FIELD_SLOTS 4 |
| 123 | uint32_t instance_data_[CLASS_FIELD_SLOTS]; |
| 124 | #undef CLASS_FIELD_SLOTS |
| 125 | |
| 126 | // UTF-8 descriptor for the class from constant pool |
| 127 | // ("Ljava/lang/Class;"), or on heap if generated ("[C") |
| 128 | const char* descriptor_; |
| 129 | |
| 130 | // Proxy classes have their descriptor allocated on the native heap. |
| 131 | // When this field is non-NULL it must be explicitly freed. |
| 132 | char* descriptor_alloc_; |
| 133 | |
| 134 | // access flags; low 16 bits are defined by VM spec |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 135 | uint32_t access_flags_; // TODO: make an instance field? |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 136 | |
| 137 | // VM-unique class serial number, nonzero, set very early |
| 138 | //uint32_t serial_number_; |
| 139 | |
| 140 | // DexFile from which we came; needed to resolve constant pool entries |
| 141 | // (will be NULL for VM-generated, e.g. arrays and primitive classes) |
| 142 | DexFile* dex_file_; |
| 143 | |
| 144 | // state of class initialization |
| 145 | ClassStatus status_; |
| 146 | |
| 147 | // if class verify fails, we must return same error on subsequent tries |
| 148 | Class* verify_error_class_; |
| 149 | |
| 150 | // threadId, used to check for recursive <clinit> invocation |
| 151 | uint32_t clinit_thread_id_; |
| 152 | |
| 153 | // Total object size; used when allocating storage on gc heap. (For |
| 154 | // interfaces and abstract classes this will be zero.) |
| 155 | uint32_t object_size_; |
| 156 | |
| 157 | // For array classes, the class object for base element, for |
| 158 | // instanceof/checkcast (for String[][][], this will be String). |
| 159 | // Otherwise, NULL. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 160 | Class* array_element_class_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 161 | |
| 162 | // For array classes, the number of array dimensions, e.g. int[][] |
| 163 | // is 2. Otherwise 0. |
| 164 | int32_t array_rank_; |
| 165 | |
| 166 | // primitive type index, or PRIM_NOT (-1); set for generated prim classes |
| 167 | PrimitiveType primitive_type_; |
| 168 | |
| 169 | // The superclass, or NULL if this is java.lang.Object or a |
| 170 | // primitive type. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 171 | Class* super_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 172 | |
| 173 | // defining class loader, or NULL for the "bootstrap" system loader |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 174 | Object* class_loader_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 175 | |
| 176 | // initiating class loader list |
| 177 | // NOTE: for classes with low serialNumber, these are unused, and the |
| 178 | // values are kept in a table in gDvm. |
| 179 | //InitiatingLoaderList initiating_loader_list_; |
| 180 | |
| 181 | // array of interfaces this class implements directly |
| 182 | int interface_count_; |
| 183 | Class** interfaces_; |
| 184 | |
| 185 | // static, private, and <init> methods |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 186 | uint32_t num_dmethods_; |
| 187 | Method* dmethods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 188 | |
| 189 | // virtual methods defined in this class; invoked through vtable |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 190 | uint32_t num_vmethods_; |
| 191 | Method* vmethods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 192 | |
| 193 | // Virtual method table (vtable), for use by "invoke-virtual". The |
| 194 | // vtable from the superclass is copied in, and virtual methods from |
| 195 | // our class either replace those from the super or are appended. |
| 196 | int32_t vtable_count_; |
| 197 | Method** vtable_; |
| 198 | |
| 199 | // Interface table (iftable), one entry per interface supported by |
| 200 | // this class. That means one entry for each interface we support |
| 201 | // directly, indirectly via superclass, or indirectly via |
| 202 | // superinterface. This will be null if neither we nor our |
| 203 | // superclass implement any interfaces. |
| 204 | // |
| 205 | // Why we need this: given "class Foo implements Face", declare |
| 206 | // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah" |
| 207 | // is part of the Face interface. We can't easily use a single |
| 208 | // vtable. |
| 209 | // |
| 210 | // For every interface a concrete class implements, we create a list |
| 211 | // of virtualMethod indices for the methods in the interface. |
| 212 | int iftable_count_; |
| 213 | InterfaceEntry* iftable_; |
| 214 | |
| 215 | // The interface vtable indices for iftable get stored here. By |
| 216 | // placing them all in a single pool for each class that implements |
| 217 | // interfaces, we decrease the number of allocations. |
| 218 | int ifvipool_count; |
| 219 | int* ifvipool_; |
| 220 | |
| 221 | // instance fields |
| 222 | // |
| 223 | // These describe the layout of the contents of a |
| 224 | // DataObject-compatible Object. Note that only the fields directly |
| 225 | // declared by this class are listed in ifields; fields declared by |
| 226 | // a superclass are listed in the superclass's ClassObject.ifields. |
| 227 | // |
| 228 | // All instance fields that refer to objects are guaranteed to be at |
| 229 | // the beginning of the field list. ifieldRefCount specifies the |
| 230 | // number of reference fields. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 231 | uint32_t num_ifields_; |
| 232 | |
| 233 | // number of fields that are object refs |
| 234 | uint32_t num_reference_ifields_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 235 | IField* ifields_; |
| 236 | |
| 237 | // bitmap of offsets of ifields |
| 238 | uint32_t ref_offsets_; |
| 239 | |
| 240 | // source file name, if known. Otherwise, NULL. |
| 241 | const char* source_file_; |
| 242 | |
| 243 | // Static fields |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 244 | uint32_t num_sfields_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 245 | SField sfields_[]; // MUST be last item |
| 246 | }; |
| 247 | |
| 248 | class String : Object { |
| 249 | public: |
| 250 | Array* array_; |
| 251 | |
| 252 | uint32_t hash_code_; |
| 253 | |
| 254 | uint32_t offset_; |
| 255 | |
| 256 | uint32_t count_; |
| 257 | }; |
| 258 | |
| 259 | class Array : Object { |
| 260 | public: |
| 261 | // The number of array elements. |
| 262 | uint32_t length_; |
| 263 | }; |
| 264 | |
| 265 | class Method { |
| 266 | public: |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame^] | 267 | // Returns true if the method is declared public. |
| 268 | bool IsPublic() { |
| 269 | return (access_flags_ & kAccPublic) != 0; |
| 270 | } |
| 271 | |
| 272 | // Returns true if the method is declared private. |
| 273 | bool IsPrivate() { |
| 274 | return (access_flags_ & kAccPrivate) != 0; |
| 275 | } |
| 276 | |
| 277 | // Returns true if the method is declared static. |
| 278 | bool IsStatic() { |
| 279 | return (access_flags_ & kAccStatic) != 0; |
| 280 | } |
| 281 | |
| 282 | // Returns true if the method is declared native and synchronized. |
| 283 | bool IsSynchronized() { |
| 284 | return (access_flags_ & kAccSynchronized) != 0; |
| 285 | } |
| 286 | |
| 287 | // Returns true if the method is not native and declared synchronized. |
| 288 | bool IsDeclaredSynchronized() { |
| 289 | return (access_flags_ & kAccDeclaredSynchronized) != 0; |
| 290 | } |
| 291 | |
| 292 | // Returns true if the method is declared final. |
| 293 | bool IsFinal() { |
| 294 | return (access_flags_ & kAccFinal) != 0; |
| 295 | } |
| 296 | |
| 297 | // Returns true if the method is declared native. |
| 298 | bool IsNative() { |
| 299 | return (access_flags_ & kAccNative) != 0; |
| 300 | } |
| 301 | |
| 302 | // Returns true if the method is declared abstract. |
| 303 | bool IsAbstract() { |
| 304 | return (access_flags_ & kAccAbstract) != 0; |
| 305 | } |
| 306 | |
| 307 | bool IsSynthetic() { |
| 308 | return (access_flags_ & kAccSynthetic) != 0; |
| 309 | } |
| 310 | |
| 311 | // Number of argument registers required by the prototype. |
| 312 | uint32_t NumArgRegisters(); |
| 313 | |
| 314 | public: // TODO: private |
| 315 | // the class we are a part of |
| 316 | Class* klass_; |
| 317 | |
| 318 | // access flags; low 16 bits are defined by spec (could be uint16_t?) |
| 319 | uint32_t access_flags_; |
| 320 | |
| 321 | // For concrete virtual methods, this is the offset of the method |
| 322 | // in "vtable". |
| 323 | // |
| 324 | // For abstract methods in an interface class, this is the offset |
| 325 | // of the method in "iftable[n]->methodIndexArray". |
| 326 | uint16_t method_index_; |
| 327 | |
| 328 | // Method bounds; not needed for an abstract method. |
| 329 | // |
| 330 | // For a native method, we compute the size of the argument list, and |
| 331 | // set "insSize" and "registerSize" equal to it. |
| 332 | uint16_t num_registers_; // ins + locals |
| 333 | uint16_t num_outs_; |
| 334 | uint16_t num_ins_; |
| 335 | |
| 336 | // method name, e.g. "<init>" or "eatLunch" |
| 337 | const char* name_; |
| 338 | |
| 339 | // A pointer to the DEX file this class was loaded from or NULL for |
| 340 | // proxy objects. |
| 341 | DexFile* dex_file_; |
| 342 | |
| 343 | // Method prototype descriptor string (return and argument types). |
| 344 | uint32_t proto_idx_; |
| 345 | |
| 346 | // The short-form method descriptor string. |
| 347 | const char* shorty_; |
| 348 | |
| 349 | // A pointer to the memory-mapped DEX code. |
| 350 | const uint16_t* insns_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | } // namespace art |
| 354 | |
| 355 | #endif // ART_SRC_OBJECT_H_ |