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" |
| 7 | |
| 8 | namespace art { |
| 9 | |
| 10 | class Array; |
| 11 | class Class; |
| 12 | class DexFile; |
| 13 | class IField; |
| 14 | class InterfaceEntry; |
| 15 | class Monitor; |
| 16 | class Method; |
| 17 | class SField; |
| 18 | |
| 19 | class Object { |
| 20 | Class* klass_; |
| 21 | Monitor* lock_; |
| 22 | }; |
| 23 | |
| 24 | class Field { |
| 25 | Class* klass_; |
| 26 | }; |
| 27 | |
| 28 | // Instance fields. |
| 29 | class IField : Field { |
| 30 | // TODO |
| 31 | }; |
| 32 | |
| 33 | // Static fields. |
| 34 | class SField : Field { |
| 35 | // TODO |
| 36 | }; |
| 37 | |
| 38 | // Class objects. |
| 39 | class Class : Object { |
| 40 | public: |
| 41 | enum ClassStatus { |
| 42 | kClassError = -1, |
| 43 | kClassNotReady = 0, |
| 44 | kClassIdx = 1, // loaded, DEX idx in super or ifaces |
| 45 | kClassLoaded = 2, // DEX idx values resolved |
| 46 | kClassResolved = 3, // part of linking |
| 47 | kClassVerifying = 4, // in the process of being verified |
| 48 | kClassVerified = 5, // logically part of linking; done pre-init |
| 49 | kClassInitializing = 6, // class init in progress |
| 50 | kClassInitialized = 7, // ready to go |
| 51 | }; |
| 52 | |
| 53 | enum PrimitiveType { |
| 54 | kPrimNot = -1 |
| 55 | }; |
| 56 | |
| 57 | // Returns the size in bytes of a class object instance with the |
| 58 | // given number of static fields. |
| 59 | static size_t Size(size_t num_sfields) { |
| 60 | return OFFSETOF_MEMBER(Class, sfields_) + sizeof(SField) * num_sfields; |
| 61 | } |
| 62 | |
| 63 | public: // TODO: private |
| 64 | // leave space for instance data; we could access fields directly if |
| 65 | // we freeze the definition of java/lang/Class |
| 66 | #define CLASS_FIELD_SLOTS 4 |
| 67 | uint32_t instance_data_[CLASS_FIELD_SLOTS]; |
| 68 | #undef CLASS_FIELD_SLOTS |
| 69 | |
| 70 | // UTF-8 descriptor for the class from constant pool |
| 71 | // ("Ljava/lang/Class;"), or on heap if generated ("[C") |
| 72 | const char* descriptor_; |
| 73 | |
| 74 | // Proxy classes have their descriptor allocated on the native heap. |
| 75 | // When this field is non-NULL it must be explicitly freed. |
| 76 | char* descriptor_alloc_; |
| 77 | |
| 78 | // access flags; low 16 bits are defined by VM spec |
| 79 | uint32_t access_flags_; |
| 80 | |
| 81 | // VM-unique class serial number, nonzero, set very early |
| 82 | //uint32_t serial_number_; |
| 83 | |
| 84 | // DexFile from which we came; needed to resolve constant pool entries |
| 85 | // (will be NULL for VM-generated, e.g. arrays and primitive classes) |
| 86 | DexFile* dex_file_; |
| 87 | |
| 88 | // state of class initialization |
| 89 | ClassStatus status_; |
| 90 | |
| 91 | // if class verify fails, we must return same error on subsequent tries |
| 92 | Class* verify_error_class_; |
| 93 | |
| 94 | // threadId, used to check for recursive <clinit> invocation |
| 95 | uint32_t clinit_thread_id_; |
| 96 | |
| 97 | // Total object size; used when allocating storage on gc heap. (For |
| 98 | // interfaces and abstract classes this will be zero.) |
| 99 | uint32_t object_size_; |
| 100 | |
| 101 | // For array classes, the class object for base element, for |
| 102 | // instanceof/checkcast (for String[][][], this will be String). |
| 103 | // Otherwise, NULL. |
| 104 | Class* array_element_class_; |
| 105 | |
| 106 | // For array classes, the number of array dimensions, e.g. int[][] |
| 107 | // is 2. Otherwise 0. |
| 108 | int32_t array_rank_; |
| 109 | |
| 110 | // primitive type index, or PRIM_NOT (-1); set for generated prim classes |
| 111 | PrimitiveType primitive_type_; |
| 112 | |
| 113 | // The superclass, or NULL if this is java.lang.Object or a |
| 114 | // primitive type. |
| 115 | Class* super_; |
| 116 | |
| 117 | // defining class loader, or NULL for the "bootstrap" system loader |
| 118 | Object* class_loader_; |
| 119 | |
| 120 | // initiating class loader list |
| 121 | // NOTE: for classes with low serialNumber, these are unused, and the |
| 122 | // values are kept in a table in gDvm. |
| 123 | //InitiatingLoaderList initiating_loader_list_; |
| 124 | |
| 125 | // array of interfaces this class implements directly |
| 126 | int interface_count_; |
| 127 | Class** interfaces_; |
| 128 | |
| 129 | // static, private, and <init> methods |
| 130 | int direct_method_count_; |
| 131 | Method* direct_methods_; |
| 132 | |
| 133 | // virtual methods defined in this class; invoked through vtable |
| 134 | int virtual_method_count_; |
| 135 | Method* virtual_methods_; |
| 136 | |
| 137 | // Virtual method table (vtable), for use by "invoke-virtual". The |
| 138 | // vtable from the superclass is copied in, and virtual methods from |
| 139 | // our class either replace those from the super or are appended. |
| 140 | int32_t vtable_count_; |
| 141 | Method** vtable_; |
| 142 | |
| 143 | // Interface table (iftable), one entry per interface supported by |
| 144 | // this class. That means one entry for each interface we support |
| 145 | // directly, indirectly via superclass, or indirectly via |
| 146 | // superinterface. This will be null if neither we nor our |
| 147 | // superclass implement any interfaces. |
| 148 | // |
| 149 | // Why we need this: given "class Foo implements Face", declare |
| 150 | // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah" |
| 151 | // is part of the Face interface. We can't easily use a single |
| 152 | // vtable. |
| 153 | // |
| 154 | // For every interface a concrete class implements, we create a list |
| 155 | // of virtualMethod indices for the methods in the interface. |
| 156 | int iftable_count_; |
| 157 | InterfaceEntry* iftable_; |
| 158 | |
| 159 | // The interface vtable indices for iftable get stored here. By |
| 160 | // placing them all in a single pool for each class that implements |
| 161 | // interfaces, we decrease the number of allocations. |
| 162 | int ifvipool_count; |
| 163 | int* ifvipool_; |
| 164 | |
| 165 | // instance fields |
| 166 | // |
| 167 | // These describe the layout of the contents of a |
| 168 | // DataObject-compatible Object. Note that only the fields directly |
| 169 | // declared by this class are listed in ifields; fields declared by |
| 170 | // a superclass are listed in the superclass's ClassObject.ifields. |
| 171 | // |
| 172 | // All instance fields that refer to objects are guaranteed to be at |
| 173 | // the beginning of the field list. ifieldRefCount specifies the |
| 174 | // number of reference fields. |
| 175 | int ifield_count_; |
| 176 | int ifield_ref_count_; // number of fields that are object refs |
| 177 | IField* ifields_; |
| 178 | |
| 179 | // bitmap of offsets of ifields |
| 180 | uint32_t ref_offsets_; |
| 181 | |
| 182 | // source file name, if known. Otherwise, NULL. |
| 183 | const char* source_file_; |
| 184 | |
| 185 | // Static fields |
| 186 | uint16_t num_sfields_; |
| 187 | SField sfields_[]; // MUST be last item |
| 188 | }; |
| 189 | |
| 190 | class String : Object { |
| 191 | public: |
| 192 | Array* array_; |
| 193 | |
| 194 | uint32_t hash_code_; |
| 195 | |
| 196 | uint32_t offset_; |
| 197 | |
| 198 | uint32_t count_; |
| 199 | }; |
| 200 | |
| 201 | class Array : Object { |
| 202 | public: |
| 203 | // The number of array elements. |
| 204 | uint32_t length_; |
| 205 | }; |
| 206 | |
| 207 | class Method { |
| 208 | public: |
| 209 | Class* klass; |
| 210 | }; |
| 211 | |
| 212 | } // namespace art |
| 213 | |
| 214 | #endif // ART_SRC_OBJECT_H_ |