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" |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 8 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "globals.h" |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 10 | #include "heap.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "logging.h" |
| 12 | #include "macros.h" |
| 13 | #include "offsets.h" |
| 14 | #include "stringpiece.h" |
| 15 | #include "monitor.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
| 17 | namespace art { |
| 18 | |
| 19 | class Array; |
| 20 | class Class; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 21 | class DexCache; |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 22 | class Field; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 23 | class InterfaceEntry; |
| 24 | class Monitor; |
| 25 | class Method; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 26 | class Object; |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 27 | class String; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 28 | template<class T> class ObjectArray; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 29 | template<class T> class PrimitiveArray; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 30 | typedef PrimitiveArray<uint8_t> BooleanArray; |
| 31 | typedef PrimitiveArray<int8_t> ByteArray; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 32 | typedef PrimitiveArray<uint16_t> CharArray; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 33 | typedef PrimitiveArray<double> DoubleArray; |
| 34 | typedef PrimitiveArray<float> FloatArray; |
| 35 | typedef PrimitiveArray<int32_t> IntArray; |
| 36 | typedef PrimitiveArray<int64_t> LongArray; |
| 37 | typedef PrimitiveArray<int16_t> ShortArray; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 38 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 39 | union JValue { |
| 40 | uint8_t z; |
| 41 | int8_t b; |
| 42 | uint16_t c; |
| 43 | int16_t s; |
| 44 | int32_t i; |
| 45 | int64_t j; |
| 46 | float f; |
| 47 | double d; |
| 48 | Object* l; |
| 49 | }; |
| 50 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 51 | static const uint32_t kAccPublic = 0x0001; // class, field, method, ic |
| 52 | static const uint32_t kAccPrivate = 0x0002; // field, method, ic |
| 53 | static const uint32_t kAccProtected = 0x0004; // field, method, ic |
| 54 | static const uint32_t kAccStatic = 0x0008; // field, method, ic |
| 55 | static const uint32_t kAccFinal = 0x0010; // class, field, method, ic |
| 56 | static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives) |
| 57 | static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik) |
| 58 | static const uint32_t kAccVolatile = 0x0040; // field |
| 59 | static const uint32_t kAccBridge = 0x0040; // method (1.5) |
| 60 | static const uint32_t kAccTransient = 0x0080; // field |
| 61 | static const uint32_t kAccVarargs = 0x0080; // method (1.5) |
| 62 | static const uint32_t kAccNative = 0x0100; // method |
| 63 | static const uint32_t kAccInterface = 0x0200; // class, ic |
| 64 | static const uint32_t kAccAbstract = 0x0400; // class, method, ic |
| 65 | static const uint32_t kAccStrict = 0x0800; // method |
| 66 | static const uint32_t kAccSynthetic = 0x1000; // field, method, ic |
| 67 | static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5) |
| 68 | static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5) |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 69 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 70 | static const uint32_t kAccMiranda = 0x8000; // method |
| 71 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 72 | static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16) |
| 73 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 74 | static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only) |
| 75 | static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only) |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 76 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 77 | /* |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 78 | * Definitions for packing refOffsets in Class. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 79 | */ |
| 80 | /* |
| 81 | * A magic value for refOffsets. Ignore the bits and walk the super |
| 82 | * chain when this is the value. |
| 83 | * [This is an unlikely "natural" value, since it would be 30 non-ref instance |
| 84 | * fields followed by 2 ref instance fields.] |
| 85 | */ |
| 86 | #define CLASS_WALK_SUPER ((unsigned int)(3)) |
| 87 | #define CLASS_SMALLEST_OFFSET (sizeof(struct Object)) |
| 88 | #define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8) |
| 89 | #define CLASS_OFFSET_ALIGNMENT 4 |
| 90 | #define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1)) |
| 91 | /* |
| 92 | * Given an offset, return the bit number which would encode that offset. |
| 93 | * Local use only. |
| 94 | */ |
| 95 | #define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \ |
| 96 | (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \ |
| 97 | CLASS_OFFSET_ALIGNMENT) |
| 98 | /* |
| 99 | * Is the given offset too large to be encoded? |
| 100 | */ |
| 101 | #define CLASS_CAN_ENCODE_OFFSET(byteOffset) \ |
| 102 | (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD) |
| 103 | /* |
| 104 | * Return a single bit, encoding the offset. |
| 105 | * Undefined if the offset is too large, as defined above. |
| 106 | */ |
| 107 | #define CLASS_BIT_FROM_OFFSET(byteOffset) \ |
| 108 | (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset)) |
| 109 | /* |
| 110 | * Return an offset, given a bit number as returned from CLZ. |
| 111 | */ |
| 112 | #define CLASS_OFFSET_FROM_CLZ(rshift) \ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 113 | ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 114 | |
| 115 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 116 | class Object { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 117 | public: |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 118 | static bool InstanceOf(const Object* object, const Class* klass) { |
| 119 | if (object == NULL) { |
| 120 | return false; |
| 121 | } |
| 122 | return object->InstanceOf(klass); |
| 123 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 124 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 125 | Class* GetClass() const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 126 | DCHECK(klass_ != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 127 | return klass_; |
| 128 | } |
| 129 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 130 | bool InstanceOf(const Class* klass) const; |
| 131 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 132 | size_t SizeOf() const; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 133 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 134 | void MonitorEnter() { |
| 135 | monitor_->Enter(); |
| 136 | } |
| 137 | |
| 138 | void MonitorExit() { |
| 139 | monitor_->Exit(); |
| 140 | } |
| 141 | |
| 142 | void Notify() { |
| 143 | monitor_->Notify(); |
| 144 | } |
| 145 | |
| 146 | void NotifyAll() { |
| 147 | monitor_->NotifyAll(); |
| 148 | } |
| 149 | |
| 150 | void Wait() { |
| 151 | monitor_->Wait(); |
| 152 | } |
| 153 | |
| 154 | void Wait(int64_t timeout) { |
| 155 | monitor_->Wait(timeout); |
| 156 | } |
| 157 | |
| 158 | void Wait(int64_t timeout, int32_t nanos) { |
| 159 | monitor_->Wait(timeout, nanos); |
| 160 | } |
| 161 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 162 | const Object* GetFieldObject(size_t field_offset) const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 163 | Object* that = const_cast<Object*>(this); |
| 164 | Object* other = that->GetFieldObject(field_offset); |
| 165 | return const_cast<const Object*>(other); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | Object* GetFieldObject(size_t field_offset) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 169 | byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset; |
| 170 | return *reinterpret_cast<Object**>(raw_addr); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void SetFieldObject(size_t offset, Object* new_value) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 174 | byte* raw_addr = reinterpret_cast<byte*>(this) + offset; |
| 175 | *reinterpret_cast<Object**>(raw_addr) = new_value; |
| 176 | // TODO: write barrier |
| 177 | } |
| 178 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 179 | bool IsClass() const; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 180 | |
| 181 | Class* AsClass() { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 182 | DCHECK(IsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 183 | return down_cast<Class*>(this); |
| 184 | } |
| 185 | |
| 186 | const Class* AsClass() const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 187 | DCHECK(IsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 188 | return down_cast<const Class*>(this); |
| 189 | } |
| 190 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 191 | bool IsObjectArray() const; |
| 192 | |
| 193 | template<class T> |
| 194 | ObjectArray<T>* AsObjectArray() { |
| 195 | DCHECK(IsObjectArray()); |
| 196 | return down_cast<ObjectArray<T>*>(this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 199 | template<class T> |
| 200 | const ObjectArray<T>* AsObjectArray() const { |
| 201 | DCHECK(IsObjectArray()); |
| 202 | return down_cast<const ObjectArray<T>*>(this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bool IsReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 206 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | |
| 210 | bool IsWeakReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 211 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 212 | return true; |
| 213 | } |
| 214 | |
| 215 | bool IsSoftReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 216 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
| 220 | bool IsFinalizerReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 221 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
| 225 | bool IsPhantomReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 226 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 227 | return true; |
| 228 | } |
| 229 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 230 | bool IsArray() const; |
| 231 | |
| 232 | Array* AsArray() { |
| 233 | DCHECK(IsArray()); |
| 234 | return down_cast<Array*>(this); |
| 235 | } |
| 236 | |
| 237 | const Array* AsArray() const { |
| 238 | DCHECK(IsArray()); |
| 239 | return down_cast<const Array*>(this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 242 | public: |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 243 | Class* klass_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 244 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 245 | Monitor* monitor_; |
| 246 | |
| 247 | private: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 248 | DISALLOW_IMPLICIT_CONSTRUCTORS(Object); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | class ObjectLock { |
| 252 | public: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 253 | explicit ObjectLock(Object* object) : obj_(object) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 254 | CHECK(object != NULL); |
| 255 | obj_->MonitorEnter(); |
| 256 | } |
| 257 | |
| 258 | ~ObjectLock() { |
| 259 | obj_->MonitorExit(); |
| 260 | } |
| 261 | |
| 262 | void Wait(int64_t millis = 0) { |
| 263 | return obj_->Wait(millis); |
| 264 | } |
| 265 | |
| 266 | void Notify() { |
| 267 | obj_->Notify(); |
| 268 | } |
| 269 | |
| 270 | void NotifyAll() { |
| 271 | obj_->NotifyAll(); |
| 272 | } |
| 273 | |
| 274 | private: |
| 275 | Object* obj_; |
| 276 | DISALLOW_COPY_AND_ASSIGN(ObjectLock); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 277 | }; |
| 278 | |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 279 | class AccessibleObject : public Object { |
| 280 | private: |
| 281 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 282 | uint32_t java_flag_; |
| 283 | }; |
| 284 | |
| 285 | class Field : public AccessibleObject { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 286 | public: |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 287 | Class* GetDeclaringClass() const { |
| 288 | return declaring_class_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 291 | const String* GetName() const { |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 292 | return name_; |
| 293 | } |
| 294 | |
| 295 | bool IsStatic() const { |
| 296 | return (access_flags_ & kAccStatic) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | char GetType() const { // TODO: return type |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 300 | return GetDescriptor()[0]; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 303 | const StringPiece& GetDescriptor() const { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 304 | DCHECK_NE(0, descriptor_.size()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 305 | return descriptor_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 308 | uint32_t GetOffset() const { |
| 309 | return offset_; |
| 310 | } |
| 311 | |
| 312 | void SetOffset(size_t num_bytes) { |
| 313 | offset_ = num_bytes; |
| 314 | } |
| 315 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 316 | // static field access |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 317 | bool GetBoolean(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 318 | void SetBoolean(bool z); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 319 | int8_t GetByte(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 320 | void SetByte(int8_t b); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 321 | uint16_t GetChar(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 322 | void SetChar(uint16_t c); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 323 | uint16_t GetShort(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 324 | void SetShort(uint16_t s); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 325 | int32_t GetInt(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 326 | void SetInt(int32_t i); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 327 | int64_t GetLong(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 328 | void SetLong(int64_t j); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 329 | float GetFloat(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 330 | void SetFloat(float f); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 331 | double GetDouble(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 332 | void SetDouble(double d); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 333 | Object* GetObject(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 334 | const Object* GetObject() const; |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 335 | void SetObject(Object* l); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 336 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 337 | public: // TODO: private |
| 338 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 339 | // The class in which this field is declared. |
| 340 | Class* declaring_class_; |
| 341 | Object* generic_type_; |
| 342 | uint32_t generic_types_are_initialized_; |
| 343 | String* name_; |
| 344 | uint32_t offset_; |
| 345 | Class* type_; |
| 346 | |
| 347 | // e.g. "I", "[C", "Landroid/os/Debug;" |
| 348 | StringPiece descriptor_; |
| 349 | |
| 350 | uint32_t access_flags_; |
| 351 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 352 | private: |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 353 | DISALLOW_IMPLICIT_CONSTRUCTORS(Field); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 356 | class Method : public AccessibleObject { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 357 | public: |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 358 | // An function that invokes a method with an array of its arguments. |
| 359 | typedef void InvokeStub(Method* method, |
| 360 | Object* obj, |
| 361 | Thread* thread, |
| 362 | byte* args, |
| 363 | JValue* result); |
| 364 | |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 365 | // Returns the method name, e.g. "<init>" or "eatLunch" |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 366 | const String* GetName() const { |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 367 | return name_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 370 | const String* GetDescriptor() const { |
| 371 | return descriptor_; |
| 372 | } |
| 373 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 374 | Class* GetDeclaringClass() const { |
| 375 | return declaring_class_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 378 | static MemberOffset ClassOffset() { |
| 379 | return MemberOffset(OFFSETOF_MEMBER(Method, klass_)); |
| 380 | } |
| 381 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 382 | // Returns true if the method is declared public. |
| 383 | bool IsPublic() const { |
| 384 | return (access_flags_ & kAccPublic) != 0; |
| 385 | } |
| 386 | |
| 387 | // Returns true if the method is declared private. |
| 388 | bool IsPrivate() const { |
| 389 | return (access_flags_ & kAccPrivate) != 0; |
| 390 | } |
| 391 | |
| 392 | // Returns true if the method is declared static. |
| 393 | bool IsStatic() const { |
| 394 | return (access_flags_ & kAccStatic) != 0; |
| 395 | } |
| 396 | |
| 397 | // Returns true if the method is declared synchronized. |
| 398 | bool IsSynchronized() const { |
| 399 | uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized; |
| 400 | return (access_flags_ & synchonized) != 0; |
| 401 | } |
| 402 | |
| 403 | // Returns true if the method is declared final. |
| 404 | bool IsFinal() const { |
| 405 | return (access_flags_ & kAccFinal) != 0; |
| 406 | } |
| 407 | |
| 408 | // Returns true if the method is declared native. |
| 409 | bool IsNative() const { |
| 410 | return (access_flags_ & kAccNative) != 0; |
| 411 | } |
| 412 | |
| 413 | // Returns true if the method is declared abstract. |
| 414 | bool IsAbstract() const { |
| 415 | return (access_flags_ & kAccAbstract) != 0; |
| 416 | } |
| 417 | |
| 418 | bool IsSynthetic() const { |
| 419 | return (access_flags_ & kAccSynthetic) != 0; |
| 420 | } |
| 421 | |
| 422 | // Number of argument registers required by the prototype. |
| 423 | uint32_t NumArgRegisters(); |
| 424 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 425 | // Number of argument bytes required for densely packing the |
| 426 | // arguments into an array of arguments. |
| 427 | size_t NumArgArrayBytes(); |
| 428 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 429 | public: // TODO: private |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 430 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 431 | // the class we are a part of |
| 432 | Class* declaring_class_; |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 433 | ObjectArray<Class>* java_exception_types_; |
| 434 | Object* java_formal_type_parameters_; |
| 435 | Object* java_generic_exception_types_; |
| 436 | Object* java_generic_parameter_types_; |
| 437 | Object* java_generic_return_type_; |
| 438 | Class* java_return_type_; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 439 | String* name_; |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 440 | ObjectArray<Class>* java_parameter_types_; |
| 441 | uint32_t java_generic_types_are_initialized_; |
| 442 | uint32_t java_slot_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 443 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 444 | const StringPiece& GetShorty() const { |
| 445 | return shorty_; |
| 446 | } |
| 447 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 448 | bool IsReturnAReference() const { |
| 449 | return (shorty_[0] == 'L') || (shorty_[0] == '['); |
| 450 | } |
| 451 | |
| 452 | bool IsReturnAFloatOrDouble() const { |
| 453 | return (shorty_[0] == 'F') || (shorty_[0] == 'D'); |
| 454 | } |
| 455 | |
| 456 | bool IsReturnAFloat() const { |
| 457 | return shorty_[0] == 'F'; |
| 458 | } |
| 459 | |
| 460 | bool IsReturnADouble() const { |
| 461 | return shorty_[0] == 'D'; |
| 462 | } |
| 463 | |
| 464 | bool IsReturnALong() const { |
| 465 | return shorty_[0] == 'J'; |
| 466 | } |
| 467 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 468 | bool IsReturnVoid() const { |
| 469 | return shorty_[0] == 'V'; |
| 470 | } |
| 471 | |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 472 | // "Args" may refer to any of the 3 levels of "Args." |
| 473 | // To avoid confusion, our code will denote which "Args" clearly: |
| 474 | // 1. UserArgs: Args that a user see. |
| 475 | // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the |
| 476 | // receiver. |
| 477 | // 3. CConvArgs: Calling Convention Args, which is physical-level Args. |
| 478 | // E.g., the first in Args is Method* for both static and non-static |
| 479 | // methods. And CConvArgs doesn't deal with the receiver because |
| 480 | // receiver is hardwired in an implicit register, so CConvArgs doesn't |
| 481 | // need to deal with it. |
| 482 | // |
| 483 | // The number of Args that should be supplied to this method |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 484 | size_t NumArgs() const { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 485 | // "1 +" because the first in Args is the receiver. |
| 486 | // "- 1" because we don't count the return type. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 487 | return (IsStatic() ? 0 : 1) + shorty_.length() - 1; |
| 488 | } |
| 489 | |
| 490 | // The number of reference arguments to this method including implicit this |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 491 | // pointer. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 492 | size_t NumReferenceArgs() const; |
| 493 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 494 | // The number of long or double arguments. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 495 | size_t NumLongOrDoubleArgs() const; |
| 496 | |
| 497 | // The number of reference arguments to this method before the given |
| 498 | // parameter index |
| 499 | size_t NumReferenceArgsBefore(unsigned int param) const; |
| 500 | |
| 501 | // Is the given method parameter a reference? |
| 502 | bool IsParamAReference(unsigned int param) const; |
| 503 | |
| 504 | // Is the given method parameter a long or double? |
| 505 | bool IsParamALongOrDouble(unsigned int param) const; |
| 506 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 507 | // Size in bytes of the given parameter |
| 508 | size_t ParamSize(unsigned int param) const; |
| 509 | |
| 510 | // Size in bytes of the return value |
| 511 | size_t ReturnSize() const; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 512 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 513 | const void* GetCode() const { |
| 514 | return code_; |
| 515 | } |
| 516 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 517 | void SetCode(const void* code) { |
| 518 | code_ = code; |
| 519 | } |
| 520 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 521 | static size_t GetCodeOffset() { |
| 522 | return OFFSETOF_MEMBER(Method, code_); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void RegisterNative(const void* native_method) { |
| 526 | native_method_ = native_method; |
| 527 | } |
| 528 | |
| 529 | static MemberOffset NativeMethodOffset() { |
| 530 | return MemberOffset(OFFSETOF_MEMBER(Method, native_method_)); |
| 531 | } |
| 532 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 533 | InvokeStub* GetInvokeStub() const { |
| 534 | return invoke_stub_; |
| 535 | } |
| 536 | |
| 537 | void SetInvokeStub(const InvokeStub* invoke_stub) { |
| 538 | invoke_stub_ = invoke_stub; |
| 539 | } |
| 540 | |
| 541 | static size_t GetInvokeStubOffset() { |
| 542 | return OFFSETOF_MEMBER(Method, invoke_stub_); |
| 543 | } |
| 544 | |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 545 | bool HasSameNameAndDescriptor(const Method* that) const; |
| 546 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 547 | public: // TODO: private/const |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 548 | // access flags; low 16 bits are defined by spec (could be uint16_t?) |
| 549 | uint32_t access_flags_; |
| 550 | |
| 551 | // For concrete virtual methods, this is the offset of the method |
| 552 | // in "vtable". |
| 553 | // |
| 554 | // For abstract methods in an interface class, this is the offset |
| 555 | // of the method in "iftable[n]->methodIndexArray". |
| 556 | uint16_t method_index_; |
| 557 | |
| 558 | // Method bounds; not needed for an abstract method. |
| 559 | // |
| 560 | // For a native method, we compute the size of the argument list, and |
| 561 | // set "insSize" and "registerSize" equal to it. |
| 562 | uint16_t num_registers_; // ins + locals |
| 563 | uint16_t num_outs_; |
| 564 | uint16_t num_ins_; |
| 565 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 566 | // The method descriptor. This represents the parameters a method |
| 567 | // takes and value it returns. This string is a list of the type |
| 568 | // descriptors for the parameters enclosed in parenthesis followed |
| 569 | // by the return type descriptor. For example, for the method |
| 570 | // |
| 571 | // Object mymethod(int i, double d, Thread t) |
| 572 | // |
| 573 | // the method descriptor would be |
| 574 | // |
| 575 | // (IDLjava/lang/Thread;)Ljava/lang/Object; |
| 576 | String* descriptor_; |
| 577 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 578 | // Method prototype descriptor string (return and argument types). |
| 579 | uint32_t proto_idx_; |
| 580 | |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 581 | // Offset to the CodeItem. |
| 582 | uint32_t code_off_; |
| 583 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 584 | // The short-form method descriptor string. |
| 585 | StringPiece shorty_; |
| 586 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 587 | private: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 588 | // Compiled code associated with this method |
| 589 | const void* code_; |
| 590 | |
| 591 | // Any native method registered with this method |
| 592 | const void* native_method_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 593 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 594 | // Native invocation stub entry point. |
| 595 | const InvokeStub* invoke_stub_; |
| 596 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 597 | DISALLOW_IMPLICIT_CONSTRUCTORS(Method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 598 | }; |
| 599 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 600 | class Array : public Object { |
| 601 | public: |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 602 | static size_t Size(size_t component_count, |
| 603 | size_t component_size) { |
| 604 | return sizeof(Array) + component_count * component_size; |
| 605 | } |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 606 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 607 | static Array* Alloc(Class* array_class, |
| 608 | size_t component_count, |
| 609 | size_t component_size) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 610 | size_t size = Size(component_count, component_size); |
| 611 | Array* array = Heap::AllocObject(array_class, size)->AsArray(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 612 | if (array != NULL) { |
| 613 | array->SetLength(component_count); |
| 614 | } |
| 615 | return array; |
| 616 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 617 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 618 | size_t SizeOf() const; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 619 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 620 | int32_t GetLength() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 621 | return length_; |
| 622 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 623 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 624 | void SetLength(uint32_t length) { |
| 625 | length_ = length; |
| 626 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 627 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 628 | protected: |
| 629 | bool IsValidIndex(int32_t index) const { |
| 630 | if (index < 0 || index >= length_) { |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 631 | Thread* self = Thread::Current(); |
| 632 | self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 633 | "length=%i; index=%i", length_, index); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 634 | return false; |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 635 | } |
| 636 | return true; |
| 637 | } |
| 638 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 639 | private: |
| 640 | // The number of array elements. |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 641 | int32_t length_; |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 642 | // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary |
| 643 | int32_t padding_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 644 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 645 | DISALLOW_IMPLICIT_CONSTRUCTORS(Array); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 646 | }; |
| 647 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 648 | template<class T> |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 649 | class ObjectArray : public Array { |
| 650 | public: |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 651 | static ObjectArray<T>* Alloc(Class* object_array_class, |
| 652 | size_t length) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 653 | return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 656 | T* const * GetData() const { |
| 657 | return reinterpret_cast<T* const *>(&elements_); |
| 658 | } |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 659 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 660 | T** GetData() { |
| 661 | return reinterpret_cast<T**>(&elements_); |
| 662 | } |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 663 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 664 | T* Get(int32_t i) const { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 665 | if (!IsValidIndex(i)) { |
| 666 | return NULL; |
| 667 | } |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 668 | return GetData()[i]; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 669 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 670 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 671 | void Set(int32_t i, T* object) { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 672 | if (IsValidIndex(i)) { |
| 673 | // TODO: ArrayStoreException |
| 674 | GetData()[i] = object; // TODO: write barrier |
| 675 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 676 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 677 | |
| 678 | static void Copy(ObjectArray<T>* src, int src_pos, |
| 679 | ObjectArray<T>* dst, int dst_pos, |
| 680 | size_t length) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 681 | for (size_t i = 0; i < length; i++) { |
| 682 | dst->Set(dst_pos + i, src->Get(src_pos + i)); |
| 683 | } |
| 684 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 685 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 686 | ObjectArray<T>* CopyOf(int32_t new_length) { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 687 | ObjectArray<T>* new_array = Alloc(klass_, new_length); |
| 688 | Copy(this, 0, new_array, 0, std::min(GetLength(), new_length)); |
| 689 | return new_array; |
| 690 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 691 | |
| 692 | private: |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 693 | // Location of first element. |
| 694 | T* elements_[0]; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 695 | |
| 696 | DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 697 | }; |
| 698 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 699 | // ClassLoader objects. |
| 700 | class ClassLoader : public Object { |
| 701 | public: |
| 702 | std::vector<const DexFile*>& GetClassPath() { |
| 703 | return class_path_; |
| 704 | } |
| 705 | void SetClassPath(std::vector<const DexFile*>& class_path) { |
| 706 | DCHECK_EQ(0U, class_path_.size()); |
| 707 | class_path_ = class_path; |
| 708 | } |
| 709 | |
| 710 | private: |
| 711 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 712 | Object* packages_; |
| 713 | ClassLoader* parent_; |
| 714 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 715 | // TODO: remove once we can create a real PathClassLoader |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 716 | std::vector<const DexFile*> class_path_; |
| 717 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 718 | DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 719 | }; |
| 720 | |
| 721 | class BaseDexClassLoader : public ClassLoader { |
| 722 | private: |
| 723 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 724 | String* original_path_; |
| 725 | Object* path_list_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 726 | DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 727 | }; |
| 728 | |
| 729 | class PathClassLoader : public BaseDexClassLoader { |
| 730 | private: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 731 | DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 732 | }; |
| 733 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 734 | // Class objects. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 735 | class Class : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 736 | public: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 737 | |
| 738 | // Class Status |
| 739 | // |
| 740 | // kStatusNotReady: If a Class cannot be found in the class table by |
| 741 | // FindClass, it allocates an new one with AllocClass in the |
| 742 | // kStatusNotReady and calls LoadClass. Note if it does find a |
| 743 | // class, it may not be kStatusResolved and it will try to push it |
| 744 | // forward toward kStatusResolved. |
| 745 | // |
| 746 | // kStatusIdx: LoadClass populates with Class with information from |
| 747 | // the DexFile, moving the status to kStatusIdx, indicating that the |
| 748 | // Class values in super_class_ and interfaces_ have not been |
| 749 | // populated based on super_class_idx_ and interfaces_idx_. The new |
| 750 | // Class can then be inserted into the classes table. |
| 751 | // |
| 752 | // kStatusLoaded: After taking a lock on Class, the ClassLinker will |
| 753 | // attempt to move a kStatusIdx class forward to kStatusLoaded by |
| 754 | // using ResolveClass to initialize the super_class_ and interfaces_. |
| 755 | // |
| 756 | // kStatusResolved: Still holding the lock on Class, the ClassLinker |
| 757 | // will use LinkClass to link all members, creating Field and Method |
| 758 | // objects, setting up the vtable, etc. On success, the class is |
| 759 | // marked kStatusResolved. |
| 760 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 761 | enum Status { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 762 | kStatusError = -1, |
| 763 | kStatusNotReady = 0, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 764 | kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_ |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 765 | kStatusLoaded = 2, // DEX idx values resolved |
| 766 | kStatusResolved = 3, // part of linking |
| 767 | kStatusVerifying = 4, // in the process of being verified |
| 768 | kStatusVerified = 5, // logically part of linking; done pre-init |
| 769 | kStatusInitializing = 6, // class init in progress |
| 770 | kStatusInitialized = 7, // ready to go |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 771 | }; |
| 772 | |
| 773 | enum PrimitiveType { |
| 774 | kPrimNot = -1 |
| 775 | }; |
| 776 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 777 | Object* NewInstance() { |
| 778 | return Heap::AllocObject(this, this->object_size_); |
| 779 | } |
| 780 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 781 | Class* GetSuperClass() const { |
| 782 | return super_class_; |
| 783 | } |
| 784 | |
| 785 | uint32_t GetSuperClassIdx() const { |
| 786 | return super_class_idx_; |
| 787 | } |
| 788 | |
| 789 | bool HasSuperClass() const { |
| 790 | return super_class_ != NULL; |
| 791 | } |
| 792 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 793 | bool IsAssignableFrom(const Class* klass) const { |
| 794 | DCHECK(klass != NULL); |
| 795 | if (this == klass) { |
| 796 | return true; |
| 797 | } |
| 798 | if (IsInterface()) { |
| 799 | return klass->Implements(this); |
| 800 | } |
| 801 | if (klass->IsArray()) { |
| 802 | return IsAssignableFromArray(klass); |
| 803 | } |
| 804 | return klass->IsSubClass(this); |
| 805 | } |
| 806 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 807 | ClassLoader* GetClassLoader() const { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 808 | return class_loader_; |
| 809 | } |
| 810 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 811 | DexCache* GetDexCache() const { |
| 812 | return dex_cache_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | Class* GetComponentType() const { |
| 816 | return component_type_; |
| 817 | } |
| 818 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 819 | static size_t GetTypeSize(const StringPiece& descriptor) { |
| 820 | switch (descriptor[0]) { |
| 821 | case 'B': return 1; // byte |
| 822 | case 'C': return 2; // char |
| 823 | case 'D': return 8; // double |
| 824 | case 'F': return 4; // float |
| 825 | case 'I': return 4; // int |
| 826 | case 'J': return 8; // long |
| 827 | case 'S': return 2; // short |
| 828 | case 'Z': return 1; // boolean |
| 829 | case 'L': return sizeof(Object*); |
| 830 | case '[': return sizeof(Array*); |
| 831 | default: |
| 832 | LOG(ERROR) << "Unknown type " << descriptor; |
| 833 | return 0; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 837 | size_t GetComponentSize() const { |
| 838 | return GetTypeSize(component_type_->descriptor_); |
| 839 | } |
| 840 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 841 | const StringPiece& GetDescriptor() const { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 842 | DCHECK_NE(0, descriptor_.size()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 843 | return descriptor_; |
| 844 | } |
| 845 | |
| 846 | Status GetStatus() const { |
| 847 | return status_; |
| 848 | } |
| 849 | |
| 850 | void SetStatus(Status new_status) { |
| 851 | // TODO: validate transition |
| 852 | status_ = new_status; |
| 853 | } |
| 854 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 855 | // Returns true if the class has failed to link. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 856 | bool IsErroneous() const { |
| 857 | return GetStatus() == kStatusError; |
| 858 | } |
| 859 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 860 | // Returns true if the class has been verified. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 861 | bool IsVerified() const { |
| 862 | return GetStatus() >= kStatusVerified; |
| 863 | } |
| 864 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 865 | // Returns true if the class has been linked. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 866 | bool IsLinked() const { |
| 867 | return GetStatus() >= kStatusResolved; |
| 868 | } |
| 869 | |
Carl Shapiro | 83ab4f3 | 2011-08-15 20:21:39 -0700 | [diff] [blame] | 870 | // Returns true if the class has been loaded. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 871 | bool IsLoaded() const { |
| 872 | return GetStatus() >= kStatusLoaded; |
| 873 | } |
| 874 | |
Carl Shapiro | 83ab4f3 | 2011-08-15 20:21:39 -0700 | [diff] [blame] | 875 | // Returns true if the class is initialized. |
| 876 | bool IsInitialized() const { |
| 877 | return GetStatus() == kStatusInitialized; |
| 878 | } |
| 879 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 880 | // Returns true if this class is in the same packages as that class. |
| 881 | bool IsInSamePackage(const Class* that) const; |
| 882 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 883 | static bool IsInSamePackage(const StringPiece& descriptor1, |
| 884 | const StringPiece& descriptor2); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 885 | |
| 886 | // Returns true if this class represents an array class. |
| 887 | bool IsArray() const { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 888 | return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | // Returns true if the class is an interface. |
| 892 | bool IsInterface() const { |
| 893 | return (access_flags_ & kAccInterface) != 0; |
| 894 | } |
| 895 | |
| 896 | // Returns true if the class is declared public. |
| 897 | bool IsPublic() const { |
| 898 | return (access_flags_ & kAccPublic) != 0; |
| 899 | } |
| 900 | |
| 901 | // Returns true if the class is declared final. |
| 902 | bool IsFinal() const { |
| 903 | return (access_flags_ & kAccFinal) != 0; |
| 904 | } |
| 905 | |
| 906 | // Returns true if the class is abstract. |
| 907 | bool IsAbstract() const { |
| 908 | return (access_flags_ & kAccAbstract) != 0; |
| 909 | } |
| 910 | |
| 911 | // Returns true if the class is an annotation. |
| 912 | bool IsAnnotation() const { |
| 913 | return (access_flags_ & kAccAnnotation) != 0; |
| 914 | } |
| 915 | |
| 916 | // Returns true if the class is a primitive type. |
| 917 | bool IsPrimitive() const { |
| 918 | return primitive_type_ != kPrimNot; |
| 919 | } |
| 920 | |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 921 | // Returns true if the class is synthetic. |
| 922 | bool IsSynthetic() const { |
| 923 | return (access_flags_ & kAccSynthetic) != 0; |
| 924 | } |
| 925 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 926 | // Returns true if this class can access that class. |
| 927 | bool CanAccess(const Class* that) const { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 928 | return that->IsPublic() || this->IsInSamePackage(that); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 931 | // Returns the number of static, private, and constructor methods. |
| 932 | size_t NumDirectMethods() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 933 | return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 936 | Method* GetDirectMethod(int32_t i) const { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 937 | DCHECK_NE(NumDirectMethods(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 938 | return direct_methods_->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 942 | DCHECK_NE(NumDirectMethods(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 943 | direct_methods_->Set(i, f); |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 946 | Method* FindDeclaredDirectMethod(const StringPiece& name, |
| 947 | const StringPiece& descriptor); |
| 948 | |
| 949 | Method* FindDirectMethod(const StringPiece& name, |
| 950 | const StringPiece& descriptor); |
| 951 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 952 | // Returns the number of non-inherited virtual methods. |
| 953 | size_t NumVirtualMethods() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 954 | return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | Method* GetVirtualMethod(uint32_t i) const { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 958 | DCHECK_NE(NumVirtualMethods(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 959 | return virtual_methods_->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 963 | DCHECK_NE(NumVirtualMethods(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 964 | virtual_methods_->Set(i, f); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 967 | Method* FindDeclaredVirtualMethod(const StringPiece& name, |
| 968 | const StringPiece& descriptor); |
| 969 | |
| 970 | Method* FindVirtualMethod(const StringPiece& name, |
| 971 | const StringPiece& descriptor); |
| 972 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 973 | size_t NumInstanceFields() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 974 | return (ifields_ != NULL) ? ifields_->GetLength() : 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 977 | // Returns the number of instance fields containing reference types. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 978 | size_t NumReferenceInstanceFields() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 979 | return num_reference_instance_fields_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 982 | Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 983 | DCHECK_NE(NumInstanceFields(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 984 | return ifields_->Get(i); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 987 | void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 988 | DCHECK_NE(NumInstanceFields(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 989 | ifields_->Set(i, f); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | size_t NumStaticFields() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 993 | return (sfields_ != NULL) ? sfields_->GetLength() : 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 996 | Field* GetStaticField(uint32_t i) const { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 997 | DCHECK_NE(NumStaticFields(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 998 | return sfields_->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1001 | void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1002 | DCHECK_NE(NumStaticFields(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1003 | sfields_->Set(i, f); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | uint32_t GetReferenceOffsets() const { |
| 1007 | return reference_offsets_; |
| 1008 | } |
| 1009 | |
| 1010 | void SetReferenceOffsets(uint32_t new_reference_offsets) { |
| 1011 | reference_offsets_ = new_reference_offsets; |
| 1012 | } |
| 1013 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1014 | size_t NumInterfaces() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1015 | return (interfaces_ != NULL) ? interfaces_->GetLength() : 0; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | Class* GetInterface(uint32_t i) const { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1019 | DCHECK_NE(NumInterfaces(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1020 | return interfaces_->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1024 | DCHECK_NE(NumInterfaces(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1025 | interfaces_->Set(i, f); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1028 | void SetVerifyErrorClass(Class* klass) { |
| 1029 | // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier |
| 1030 | size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_); |
| 1031 | klass->SetFieldObject(field_offset, klass); |
| 1032 | } |
| 1033 | |
| 1034 | private: |
| 1035 | bool Implements(const Class* klass) const; |
| 1036 | bool IsArrayAssignableFromArray(const Class* klass) const; |
| 1037 | bool IsAssignableFromArray(const Class* klass) const; |
| 1038 | bool IsSubClass(const Class* klass) const; |
| 1039 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1040 | public: // TODO: private |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1041 | // leave space for instance data; we could access fields directly if |
| 1042 | // we freeze the definition of java/lang/Class |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1043 | #define CLASS_FIELD_SLOTS 1 |
| 1044 | // Class.#0 name |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1045 | uint32_t instance_data_[CLASS_FIELD_SLOTS]; |
| 1046 | #undef CLASS_FIELD_SLOTS |
| 1047 | |
| 1048 | // UTF-8 descriptor for the class from constant pool |
| 1049 | // ("Ljava/lang/Class;"), or on heap if generated ("[C") |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1050 | StringPiece descriptor_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1051 | |
| 1052 | // Proxy classes have their descriptor allocated on the native heap. |
| 1053 | // When this field is non-NULL it must be explicitly freed. |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1054 | std::string* descriptor_alloc_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1055 | |
| 1056 | // access flags; low 16 bits are defined by VM spec |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1057 | uint32_t access_flags_; // TODO: make an instance field? |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1058 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1059 | // DexCache of resolved constant pool entries |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1060 | // (will be NULL for VM-generated, e.g. arrays and primitive classes) |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1061 | DexCache* dex_cache_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1062 | |
| 1063 | // state of class initialization |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1064 | Status status_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1065 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1066 | // If class verify fails, we must return same error on subsequent tries. |
| 1067 | // Update with SetVerifyErrorClass to ensure a write barrier is used. |
| 1068 | const Class* verify_error_class_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1069 | |
| 1070 | // threadId, used to check for recursive <clinit> invocation |
| 1071 | uint32_t clinit_thread_id_; |
| 1072 | |
| 1073 | // Total object size; used when allocating storage on gc heap. (For |
| 1074 | // interfaces and abstract classes this will be zero.) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1075 | size_t object_size_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1076 | |
| 1077 | // For array classes, the class object for base element, for |
| 1078 | // instanceof/checkcast (for String[][][], this will be String). |
| 1079 | // Otherwise, NULL. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1080 | Class* component_type_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1081 | |
| 1082 | // For array classes, the number of array dimensions, e.g. int[][] |
| 1083 | // is 2. Otherwise 0. |
| 1084 | int32_t array_rank_; |
| 1085 | |
| 1086 | // primitive type index, or PRIM_NOT (-1); set for generated prim classes |
| 1087 | PrimitiveType primitive_type_; |
| 1088 | |
| 1089 | // The superclass, or NULL if this is java.lang.Object or a |
| 1090 | // primitive type. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1091 | Class* super_class_; // TODO: make an instance field |
| 1092 | uint32_t super_class_idx_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1093 | |
| 1094 | // defining class loader, or NULL for the "bootstrap" system loader |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1095 | ClassLoader* class_loader_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1096 | |
| 1097 | // initiating class loader list |
| 1098 | // NOTE: for classes with low serialNumber, these are unused, and the |
| 1099 | // values are kept in a table in gDvm. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1100 | // InitiatingLoaderList initiating_loader_list_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1101 | |
| 1102 | // array of interfaces this class implements directly |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1103 | ObjectArray<Class>* interfaces_; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1104 | uint32_t* interfaces_idx_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1105 | |
| 1106 | // static, private, and <init> methods |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1107 | ObjectArray<Method>* direct_methods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1108 | |
| 1109 | // virtual methods defined in this class; invoked through vtable |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1110 | ObjectArray<Method>* virtual_methods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1111 | |
| 1112 | // Virtual method table (vtable), for use by "invoke-virtual". The |
| 1113 | // vtable from the superclass is copied in, and virtual methods from |
| 1114 | // our class either replace those from the super or are appended. |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1115 | ObjectArray<Method>* vtable_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1116 | |
| 1117 | // Interface table (iftable), one entry per interface supported by |
| 1118 | // this class. That means one entry for each interface we support |
| 1119 | // directly, indirectly via superclass, or indirectly via |
| 1120 | // superinterface. This will be null if neither we nor our |
| 1121 | // superclass implement any interfaces. |
| 1122 | // |
| 1123 | // Why we need this: given "class Foo implements Face", declare |
| 1124 | // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah" |
| 1125 | // is part of the Face interface. We can't easily use a single |
| 1126 | // vtable. |
| 1127 | // |
| 1128 | // For every interface a concrete class implements, we create a list |
| 1129 | // of virtualMethod indices for the methods in the interface. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1130 | size_t iftable_count_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1131 | InterfaceEntry* iftable_; |
| 1132 | |
| 1133 | // The interface vtable indices for iftable get stored here. By |
| 1134 | // placing them all in a single pool for each class that implements |
| 1135 | // interfaces, we decrease the number of allocations. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1136 | size_t ifvi_pool_count_; |
| 1137 | uint32_t* ifvi_pool_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1138 | |
| 1139 | // instance fields |
| 1140 | // |
| 1141 | // These describe the layout of the contents of a |
| 1142 | // DataObject-compatible Object. Note that only the fields directly |
| 1143 | // declared by this class are listed in ifields; fields declared by |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1144 | // a superclass are listed in the superclass's Class.ifields. |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1145 | // |
| 1146 | // All instance fields that refer to objects are guaranteed to be at |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1147 | // the beginning of the field list. num_reference_instance_fields_ |
| 1148 | // specifies the number of reference fields. |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1149 | ObjectArray<Field>* ifields_; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1150 | |
| 1151 | // number of fields that are object refs |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1152 | size_t num_reference_instance_fields_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1153 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1154 | // Bitmap of offsets of ifields. |
| 1155 | uint32_t reference_offsets_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1156 | |
| 1157 | // source file name, if known. Otherwise, NULL. |
| 1158 | const char* source_file_; |
| 1159 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1160 | // Static fields |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1161 | ObjectArray<Field>* sfields_; |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1162 | |
| 1163 | // static field storage |
| 1164 | // |
| 1165 | // Each static field is stored in one of three arrays: |
| 1166 | // o references are stored in static_references_ |
| 1167 | // o doubles and longs are stored in static_64bit_primitives_ |
| 1168 | // o everything else is in static_32bit_primitives_ |
| 1169 | // Static fields select their array using their type and their index using the |
| 1170 | // Field->slot_ member. Storing static fields in arrays avoids the need for a |
| 1171 | // special case in the GC. |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1172 | ObjectArray<Object>* static_references_; |
| 1173 | IntArray* static_32bit_primitives_; |
| 1174 | LongArray* static_64bit_primitives_; |
| 1175 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1176 | private: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1177 | DISALLOW_IMPLICIT_CONSTRUCTORS(Class); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1178 | }; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1179 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1180 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1181 | inline bool Object::InstanceOf(const Class* klass) const { |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 1182 | DCHECK(klass != NULL); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1183 | DCHECK(klass_ != NULL); |
| 1184 | return klass->IsAssignableFrom(klass_); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1187 | inline bool Object::IsClass() const { |
| 1188 | return klass_ == klass_->klass_; |
| 1189 | } |
| 1190 | |
| 1191 | inline bool Object::IsObjectArray() const { |
| 1192 | return IsArray() && !klass_->component_type_->IsPrimitive(); |
| 1193 | } |
| 1194 | |
| 1195 | inline bool Object::IsArray() const { |
| 1196 | return klass_->IsArray(); |
| 1197 | } |
| 1198 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 1199 | inline size_t Object::SizeOf() const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1200 | if (IsArray()) { |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 1201 | return AsArray()->SizeOf(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1202 | } |
| 1203 | return klass_->object_size_; |
| 1204 | } |
| 1205 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 1206 | inline size_t Array::SizeOf() const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1207 | return Size(GetLength(), klass_->GetComponentSize()); |
| 1208 | } |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1209 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1210 | class DataObject : public Object { |
| 1211 | public: |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1212 | uint32_t fields_[0]; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1213 | private: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1214 | DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1215 | }; |
| 1216 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1217 | template<class T> |
| 1218 | class PrimitiveArray : public Array { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1219 | public: |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 1220 | typedef T ElementType; |
| 1221 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1222 | static PrimitiveArray<T>* Alloc(size_t length); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1223 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1224 | const T* GetData() const { |
| 1225 | return reinterpret_cast<const T*>(&elements_); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1228 | T* GetData() { |
| 1229 | return reinterpret_cast<T*>(&elements_); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1232 | T Get(int32_t i) const { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1233 | if (!IsValidIndex(i)) { |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 1234 | return T(0); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1235 | } |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1236 | return GetData()[i]; |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1239 | void Set(int32_t i, T value) { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1240 | // TODO: ArrayStoreException |
| 1241 | if (IsValidIndex(i)) { |
| 1242 | GetData()[i] = value; |
| 1243 | } |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1246 | static void SetArrayClass(Class* array_class) { |
| 1247 | CHECK(array_class != NULL); |
| 1248 | array_class_ = array_class; |
| 1249 | } |
| 1250 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1251 | private: |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1252 | // Location of first element. |
| 1253 | T elements_[0]; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1254 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1255 | static Class* array_class_; |
| 1256 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1257 | DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1258 | }; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1259 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1260 | class String : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1261 | public: |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1262 | const CharArray* GetCharArray() const { |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1263 | DCHECK(array_ != NULL); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1264 | return array_; |
| 1265 | } |
| 1266 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1267 | uint32_t GetHashCode() const { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1268 | return hash_code_; |
| 1269 | } |
| 1270 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1271 | uint32_t GetOffset() const { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1272 | return offset_; |
| 1273 | } |
| 1274 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1275 | uint32_t GetLength() const { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1276 | return count_; |
| 1277 | } |
| 1278 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1279 | // TODO: do we need this? Equals is the only caller, and could |
| 1280 | // bounds check itself. |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1281 | uint16_t CharAt(int32_t index) const { |
| 1282 | if (index < 0 || index >= count_) { |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1283 | Thread* self = Thread::Current(); |
| 1284 | self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;", |
| 1285 | "length=%i; index=%i", count_, index); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1286 | return 0; |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1287 | } |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 1288 | return GetCharArray()->Get(index + GetOffset()); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1291 | static String* AllocFromUtf16(int32_t utf16_length, |
| 1292 | uint16_t* utf16_data_in, |
| 1293 | int32_t hash_code) { |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1294 | String* string = Alloc(GetJavaLangString(), |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1295 | utf16_length); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 1296 | // TODO: use 16-bit wide memset variant |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1297 | for (int i = 0; i < utf16_length; i++ ) { |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1298 | string->array_->Set(i, utf16_data_in[i]); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1299 | } |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1300 | string->ComputeHashCode(); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1301 | return string; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Elliott Hughes | bfaadc8 | 2011-08-18 17:36:58 -0700 | [diff] [blame^] | 1304 | static String* AllocFromModifiedUtf8(const char* utf) { |
| 1305 | size_t char_count = ModifiedUtf8Len(utf); |
| 1306 | return AllocFromModifiedUtf8(char_count, utf); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 1309 | static String* AllocFromModifiedUtf8(int32_t utf16_length, |
| 1310 | const char* utf8_data_in) { |
Elliott Hughes | bfaadc8 | 2011-08-18 17:36:58 -0700 | [diff] [blame^] | 1311 | String* string = Alloc(GetJavaLangString(), utf16_length); |
| 1312 | uint16_t* utf16_data_out = string->array_->GetData(); |
| 1313 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
| 1314 | string->ComputeHashCode(); |
| 1315 | return string; |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1318 | static void InitClasses(Class* java_lang_String); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1319 | |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1320 | static String* Alloc(Class* java_lang_String, |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1321 | int32_t utf16_length) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1322 | String* string = down_cast<String*>(java_lang_String->NewInstance()); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1323 | CharArray* array = CharArray::Alloc(utf16_length); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1324 | string->array_ = array; |
| 1325 | string->count_ = utf16_length; |
| 1326 | return string; |
| 1327 | } |
| 1328 | |
| 1329 | // Convert Modified UTF-8 to UTF-16 |
| 1330 | // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 |
| 1331 | static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) { |
| 1332 | while (*utf8_data_in != '\0') { |
| 1333 | *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | // Retrieve the next UTF-16 character from a UTF-8 string. |
| 1338 | // |
| 1339 | // Advances "*pUtf8Ptr" to the start of the next character. |
| 1340 | // |
| 1341 | // WARNING: If a string is corrupted by dropping a '\0' in the middle |
| 1342 | // of a 3-byte sequence, you can end up overrunning the buffer with |
| 1343 | // reads (and possibly with the writes if the length was computed and |
| 1344 | // cached before the damage). For performance reasons, this function |
| 1345 | // assumes that the string being parsed is known to be valid (e.g., by |
| 1346 | // already being verified). Most strings we process here are coming |
| 1347 | // out of dex files or other internal translations, so the only real |
| 1348 | // risk comes from the JNI NewStringUTF call. |
| 1349 | static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) { |
| 1350 | uint8_t one = *(*utf8_data_in)++; |
| 1351 | if ((one & 0x80) == 0) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1352 | // one-byte encoding |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1353 | return one; |
| 1354 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1355 | // two- or three-byte encoding |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1356 | uint8_t two = *(*utf8_data_in)++; |
| 1357 | if ((one & 0x20) == 0) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1358 | // two-byte encoding |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1359 | return ((one & 0x1f) << 6) | |
| 1360 | (two & 0x3f); |
| 1361 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1362 | // three-byte encoding |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1363 | uint8_t three = *(*utf8_data_in)++; |
| 1364 | return ((one & 0x0f) << 12) | |
| 1365 | ((two & 0x3f) << 6) | |
| 1366 | (three & 0x3f); |
| 1367 | } |
| 1368 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1369 | // Like "strlen", but for strings encoded with "modified" UTF-8. |
| 1370 | // |
| 1371 | // The value returned is the number of characters, which may or may not |
| 1372 | // be the same as the number of bytes. |
| 1373 | // |
| 1374 | // (If this needs optimizing, try: mask against 0xa0, shift right 5, |
| 1375 | // get increment {1-3} from table of 8 values.) |
| 1376 | static size_t ModifiedUtf8Len(const char* utf8) { |
| 1377 | size_t len = 0; |
| 1378 | int ic; |
| 1379 | while ((ic = *utf8++) != '\0') { |
| 1380 | len++; |
| 1381 | if ((ic & 0x80) == 0) { |
| 1382 | // one-byte encoding |
| 1383 | continue; |
| 1384 | } |
| 1385 | // two- or three-byte encoding |
| 1386 | utf8++; |
| 1387 | if ((ic & 0x20) == 0) { |
| 1388 | // two-byte encoding |
| 1389 | continue; |
| 1390 | } |
| 1391 | // three-byte encoding |
| 1392 | utf8++; |
| 1393 | } |
| 1394 | return len; |
| 1395 | } |
| 1396 | |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1397 | // The java/lang/String.computeHashCode() algorithm |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 1398 | static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) { |
| 1399 | int32_t hash = 0; |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1400 | while (string_length--) { |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1401 | hash = hash * 31 + *string_data++; |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1402 | } |
| 1403 | return hash; |
| 1404 | } |
| 1405 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1406 | void ComputeHashCode() { |
| 1407 | hash_code_ = ComputeUtf16Hash(array_->GetData(), count_); |
| 1408 | } |
| 1409 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1410 | // TODO: do we need this overload? give it a more intention-revealing name. |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1411 | bool Equals(const char* modified_utf8) const { |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1412 | for (uint32_t i = 0; i < GetLength(); ++i) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1413 | uint16_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 1414 | if (ch == '\0' || ch != CharAt(i)) { |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 1415 | return false; |
| 1416 | } |
| 1417 | } |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1418 | return *modified_utf8 == '\0'; |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 1419 | } |
| 1420 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1421 | // TODO: do we need this overload? give it a more intention-revealing name. |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1422 | bool Equals(const StringPiece& modified_utf8) const { |
| 1423 | // TODO: do not assume C-string representation. |
| 1424 | return Equals(modified_utf8.data()); |
| 1425 | } |
| 1426 | |
| 1427 | bool Equals(const String* that) const { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 1428 | // TODO: short circuit on hash_code_ |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1429 | if (this->GetLength() != that->GetLength()) { |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1430 | return false; |
| 1431 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1432 | for (uint32_t i = 0; i < that->GetLength(); ++i) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1433 | if (this->CharAt(i) != that->CharAt(i)) { |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1434 | return false; |
| 1435 | } |
| 1436 | } |
| 1437 | return true; |
| 1438 | } |
| 1439 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1440 | // TODO: do we need this overload? give it a more intention-revealing name. |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1441 | bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const { |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1442 | if (this->GetLength() != that_length) { |
| 1443 | return false; |
| 1444 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1445 | for (uint32_t i = 0; i < that_length; ++i) { |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1446 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
| 1447 | return false; |
| 1448 | } |
| 1449 | } |
| 1450 | return true; |
| 1451 | } |
| 1452 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1453 | private: |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1454 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 1455 | CharArray* array_; |
| 1456 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1457 | uint32_t hash_code_; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1458 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1459 | int32_t offset_; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1460 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1461 | int32_t count_; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1462 | |
| 1463 | static Class* GetJavaLangString() { |
| 1464 | DCHECK(java_lang_String_ != NULL); |
| 1465 | return java_lang_String_; |
| 1466 | } |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1467 | |
| 1468 | static Class* java_lang_String_; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1469 | |
| 1470 | DISALLOW_IMPLICIT_CONSTRUCTORS(String); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1471 | }; |
| 1472 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1473 | class InterfaceEntry { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1474 | public: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1475 | InterfaceEntry() : klass_(NULL), method_index_array_(NULL) { |
| 1476 | } |
| 1477 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1478 | Class* GetClass() const { |
| 1479 | return klass_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1480 | } |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1481 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1482 | void SetClass(Class* klass) { |
| 1483 | klass_ = klass; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1484 | } |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1485 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1486 | private: |
| 1487 | // Points to the interface class. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1488 | Class* klass_; |
| 1489 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1490 | public: // TODO: private |
| 1491 | // Index into array of vtable offsets. This points into the |
| 1492 | // ifviPool, which holds the vtables for all interfaces declared by |
| 1493 | // this class. |
| 1494 | uint32_t* method_index_array_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1495 | |
| 1496 | private: |
| 1497 | DISALLOW_COPY_AND_ASSIGN(InterfaceEntry); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1498 | }; |
| 1499 | |
| 1500 | } // namespace art |
| 1501 | |
| 1502 | #endif // ART_SRC_OBJECT_H_ |