Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_OBJECT_H_ |
| 18 | #define ART_SRC_OBJECT_H_ |
| 19 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 20 | #include <iosfwd> |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 23 | #include "UniquePtr.h" |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 24 | #include "atomic.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | #include "casts.h" |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 26 | #include "constants.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 27 | #include "globals.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 28 | #include "heap.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 29 | #include "logging.h" |
| 30 | #include "macros.h" |
| 31 | #include "offsets.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 33 | #include "stringpiece.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 34 | #include "thread.h" |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 35 | #include "utf.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | |
| 39 | class Array; |
| 40 | class Class; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 41 | class ClassLoader; |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 42 | class CodeAndDirectMethods; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 43 | class DexCache; |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 44 | class Field; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 45 | class InterfaceEntry; |
| 46 | class Monitor; |
| 47 | class Method; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 48 | class Object; |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 49 | class StaticStorageBase; |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 50 | class String; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 51 | template<class T> class ObjectArray; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 52 | template<class T> class PrimitiveArray; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 53 | typedef PrimitiveArray<uint8_t> BooleanArray; |
| 54 | typedef PrimitiveArray<int8_t> ByteArray; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 55 | typedef PrimitiveArray<uint16_t> CharArray; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 56 | typedef PrimitiveArray<double> DoubleArray; |
| 57 | typedef PrimitiveArray<float> FloatArray; |
| 58 | typedef PrimitiveArray<int32_t> IntArray; |
| 59 | typedef PrimitiveArray<int64_t> LongArray; |
| 60 | typedef PrimitiveArray<int16_t> ShortArray; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 61 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 62 | union JValue { |
| 63 | uint8_t z; |
| 64 | int8_t b; |
| 65 | uint16_t c; |
| 66 | int16_t s; |
| 67 | int32_t i; |
| 68 | int64_t j; |
| 69 | float f; |
| 70 | double d; |
| 71 | Object* l; |
| 72 | }; |
| 73 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 74 | static const uint32_t kAccPublic = 0x0001; // class, field, method, ic |
| 75 | static const uint32_t kAccPrivate = 0x0002; // field, method, ic |
| 76 | static const uint32_t kAccProtected = 0x0004; // field, method, ic |
| 77 | static const uint32_t kAccStatic = 0x0008; // field, method, ic |
| 78 | static const uint32_t kAccFinal = 0x0010; // class, field, method, ic |
| 79 | static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives) |
| 80 | static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik) |
| 81 | static const uint32_t kAccVolatile = 0x0040; // field |
| 82 | static const uint32_t kAccBridge = 0x0040; // method (1.5) |
| 83 | static const uint32_t kAccTransient = 0x0080; // field |
| 84 | static const uint32_t kAccVarargs = 0x0080; // method (1.5) |
| 85 | static const uint32_t kAccNative = 0x0100; // method |
| 86 | static const uint32_t kAccInterface = 0x0200; // class, ic |
| 87 | static const uint32_t kAccAbstract = 0x0400; // class, method, ic |
| 88 | static const uint32_t kAccStrict = 0x0800; // method |
| 89 | static const uint32_t kAccSynthetic = 0x1000; // field, method, ic |
| 90 | static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5) |
| 91 | static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5) |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 92 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 93 | static const uint32_t kAccMiranda = 0x8000; // method |
| 94 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 95 | static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16) |
| 96 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 97 | static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only) |
| 98 | static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 99 | static const uint32_t kAccWritable = 0x80000000; // method (Dalvik only) |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 100 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 101 | static const uint32_t kAccClassFlagsMask = (kAccPublic |
| 102 | | kAccFinal |
| 103 | | kAccInterface |
| 104 | | kAccAbstract |
| 105 | | kAccSynthetic |
| 106 | | kAccAnnotation |
| 107 | | kAccEnum); |
| 108 | static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask |
| 109 | | kAccPrivate |
| 110 | | kAccProtected |
| 111 | | kAccStatic); |
| 112 | static const uint32_t kAccFieldFlagsMask = (kAccPublic |
| 113 | | kAccPrivate |
| 114 | | kAccProtected |
| 115 | | kAccStatic |
| 116 | | kAccFinal |
| 117 | | kAccVolatile |
| 118 | | kAccTransient |
| 119 | | kAccSynthetic |
| 120 | | kAccEnum); |
| 121 | static const uint32_t kAccMethodFlagsMask = (kAccPublic |
| 122 | | kAccPrivate |
| 123 | | kAccProtected |
| 124 | | kAccStatic |
| 125 | | kAccFinal |
| 126 | | kAccSynchronized |
| 127 | | kAccBridge |
| 128 | | kAccVarargs |
| 129 | | kAccNative |
| 130 | | kAccAbstract |
| 131 | | kAccStrict |
| 132 | | kAccSynthetic |
| 133 | | kAccConstructor |
| 134 | | kAccDeclaredSynchronized); |
| 135 | |
| 136 | // if only kAccClassIsReference is set, we have a soft reference |
| 137 | static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref |
| 138 | static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference |
| 139 | static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference |
| 140 | static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference |
| 141 | |
| 142 | static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference |
| 143 | | kAccClassIsWeakReference |
| 144 | | kAccClassIsFinalizerReference |
| 145 | | kAccClassIsPhantomReference); |
| 146 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 147 | /* |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 148 | * Definitions for packing refOffsets in Class. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 149 | */ |
| 150 | /* |
| 151 | * A magic value for refOffsets. Ignore the bits and walk the super |
| 152 | * chain when this is the value. |
| 153 | * [This is an unlikely "natural" value, since it would be 30 non-ref instance |
| 154 | * fields followed by 2 ref instance fields.] |
| 155 | */ |
| 156 | #define CLASS_WALK_SUPER ((unsigned int)(3)) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 157 | #define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8) |
| 158 | #define CLASS_OFFSET_ALIGNMENT 4 |
| 159 | #define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1)) |
| 160 | /* |
| 161 | * Given an offset, return the bit number which would encode that offset. |
| 162 | * Local use only. |
| 163 | */ |
| 164 | #define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \ |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 165 | ((unsigned int)(byteOffset) / \ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 166 | CLASS_OFFSET_ALIGNMENT) |
| 167 | /* |
| 168 | * Is the given offset too large to be encoded? |
| 169 | */ |
| 170 | #define CLASS_CAN_ENCODE_OFFSET(byteOffset) \ |
| 171 | (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD) |
| 172 | /* |
| 173 | * Return a single bit, encoding the offset. |
| 174 | * Undefined if the offset is too large, as defined above. |
| 175 | */ |
| 176 | #define CLASS_BIT_FROM_OFFSET(byteOffset) \ |
| 177 | (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset)) |
| 178 | /* |
| 179 | * Return an offset, given a bit number as returned from CLZ. |
| 180 | */ |
| 181 | #define CLASS_OFFSET_FROM_CLZ(rshift) \ |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 182 | MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT)) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 183 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 184 | #define OFFSET_OF_OBJECT_MEMBER(type, field) \ |
| 185 | MemberOffset(OFFSETOF_MEMBER(type, field)) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 186 | |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 187 | // Classes shared with the managed side of the world need to be packed |
| 188 | // so that they don't have extra platform specific padding. |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 189 | #define MANAGED PACKED |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 190 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 191 | // C++ mirror of java.lang.Object |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 192 | class MANAGED Object { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 193 | public: |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 194 | static uint32_t InstanceOfFromCode(const Object* object, const Class* klass) { |
| 195 | DCHECK(object != NULL); |
| 196 | return object->InstanceOf(klass) ? 1 : 0; |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 197 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 198 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 199 | static MemberOffset ClassOffset() { |
| 200 | return OFFSET_OF_OBJECT_MEMBER(Object, klass_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 203 | Class* GetClass() const { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 204 | return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void SetClass(Class* new_klass); |
| 208 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 209 | bool InstanceOf(const Class* klass) const; |
| 210 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 211 | size_t SizeOf() const; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 212 | |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 213 | Object* Clone(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 214 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 215 | static MemberOffset MonitorOffset() { |
| 216 | return OFFSET_OF_OBJECT_MEMBER(Object, monitor_); |
| 217 | } |
| 218 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 219 | volatile int32_t* GetRawLockWordAddress() { |
| 220 | byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value(); |
| 221 | int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr); |
| 222 | return const_cast<volatile int32_t*>(word_addr); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 225 | uint32_t GetLockOwner(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 226 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 227 | void MonitorEnter(Thread* thread); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 228 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 229 | bool MonitorExit(Thread* thread); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 230 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 231 | void Notify(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 232 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 233 | void NotifyAll(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 234 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 235 | void Wait(int64_t timeout); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 236 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 237 | void Wait(int64_t timeout, int32_t nanos); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 238 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 239 | bool IsClass() const; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 240 | |
| 241 | Class* AsClass() { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 242 | DCHECK(IsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 243 | return down_cast<Class*>(this); |
| 244 | } |
| 245 | |
| 246 | const Class* AsClass() const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 247 | DCHECK(IsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 248 | return down_cast<const Class*>(this); |
| 249 | } |
| 250 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 251 | bool IsClassClass() const; |
| 252 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 253 | bool IsObjectArray() const; |
| 254 | |
| 255 | template<class T> |
| 256 | ObjectArray<T>* AsObjectArray() { |
| 257 | DCHECK(IsObjectArray()); |
| 258 | return down_cast<ObjectArray<T>*>(this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 261 | template<class T> |
| 262 | const ObjectArray<T>* AsObjectArray() const { |
| 263 | DCHECK(IsObjectArray()); |
| 264 | return down_cast<const ObjectArray<T>*>(this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 267 | bool IsArrayInstance() const; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 268 | |
| 269 | Array* AsArray() { |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 270 | DCHECK(IsArrayInstance()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 271 | return down_cast<Array*>(this); |
| 272 | } |
| 273 | |
| 274 | const Array* AsArray() const { |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 275 | DCHECK(IsArrayInstance()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 276 | return down_cast<const Array*>(this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 279 | bool IsString() const; |
| 280 | |
| 281 | String* AsString() { |
| 282 | DCHECK(IsString()); |
| 283 | return down_cast<String*>(this); |
| 284 | } |
| 285 | |
| 286 | bool IsMethod() const; |
| 287 | |
| 288 | Method* AsMethod() { |
| 289 | DCHECK(IsMethod()); |
| 290 | return down_cast<Method*>(this); |
| 291 | } |
| 292 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 293 | const Method* AsMethod() const { |
| 294 | DCHECK(IsMethod()); |
| 295 | return down_cast<const Method*>(this); |
| 296 | } |
| 297 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 298 | bool IsField() const; |
| 299 | |
| 300 | Field* AsField() { |
| 301 | DCHECK(IsField()); |
| 302 | return down_cast<Field*>(this); |
| 303 | } |
| 304 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 305 | const Field* AsField() const { |
| 306 | DCHECK(IsField()); |
| 307 | return down_cast<const Field*>(this); |
| 308 | } |
| 309 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 310 | bool IsReferenceInstance() const; |
| 311 | |
| 312 | bool IsWeakReferenceInstance() const; |
| 313 | |
| 314 | bool IsSoftReferenceInstance() const; |
| 315 | |
| 316 | bool IsFinalizerReferenceInstance() const; |
| 317 | |
| 318 | bool IsPhantomReferenceInstance() const; |
| 319 | |
| 320 | // Accessors for Java type fields |
| 321 | template<class T> |
| 322 | T GetFieldObject(MemberOffset field_offset, bool is_volatile) const { |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 323 | DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences()); |
| 324 | T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 325 | Heap::VerifyObject(result); |
| 326 | return result; |
| 327 | } |
| 328 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 329 | void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 330 | Heap::VerifyObject(new_value); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 331 | SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid); |
| 332 | if (new_value != NULL) { |
| 333 | Heap::WriteBarrier(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 334 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const { |
| 338 | Heap::VerifyObject(this); |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 339 | const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value(); |
| 340 | const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 341 | if (is_volatile) { |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 342 | return android_atomic_acquire_load(word_addr); |
| 343 | } else { |
| 344 | return *word_addr; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 345 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 348 | void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) { |
| 349 | if (this_is_valid) { |
| 350 | Heap::VerifyObject(this); |
| 351 | } |
| 352 | byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value(); |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 353 | uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 354 | if (is_volatile) { |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 355 | /* |
| 356 | * TODO: add an android_atomic_synchronization_store() function and |
| 357 | * use it in the 32-bit volatile set handlers. On some platforms we |
| 358 | * can use a fast atomic instruction and avoid the barriers. |
| 359 | */ |
| 360 | ANDROID_MEMBAR_STORE(); |
| 361 | *word_addr = new_value; |
| 362 | ANDROID_MEMBAR_FULL(); |
| 363 | } else { |
| 364 | *word_addr = new_value; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 365 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const { |
| 369 | Heap::VerifyObject(this); |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 370 | const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value(); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 371 | const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 372 | if (is_volatile) { |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 373 | uint64_t result = QuasiAtomicRead64(addr); |
| 374 | ANDROID_MEMBAR_FULL(); |
| 375 | return result; |
| 376 | } else { |
| 377 | return *addr; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 378 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 381 | void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 382 | Heap::VerifyObject(this); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 383 | byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value(); |
| 384 | int64_t* addr = reinterpret_cast<int64_t*>(raw_addr); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 385 | if (is_volatile) { |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 386 | ANDROID_MEMBAR_STORE(); |
| 387 | QuasiAtomicSwap64(new_value, addr); |
| 388 | // Post-store barrier not required due to use of atomic op or mutex. |
| 389 | } else { |
| 390 | *addr = new_value; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 391 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | protected: |
| 395 | // Accessors for non-Java type fields |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 396 | template<class T> |
| 397 | T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const { |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 398 | return reinterpret_cast<T>(GetField32(field_offset, is_volatile)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | template<typename T> |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 402 | void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) { |
| 403 | SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | private: |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 407 | Class* klass_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 408 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 409 | uint32_t monitor_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 410 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 411 | friend class ImageWriter; // for abusing monitor_ directly |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 412 | friend struct ObjectOffsets; // for verifying offset information |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 413 | DISALLOW_IMPLICIT_CONSTRUCTORS(Object); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 414 | }; |
| 415 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 416 | // C++ mirror of java.lang.reflect.AccessibleObject |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 417 | class MANAGED AccessibleObject : public Object { |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 418 | private: |
| 419 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 420 | uint32_t java_flag_; // can accessibility checks be bypassed |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 421 | friend struct AccessibleObjectOffsets; // for verifying offset information |
| 422 | DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject); |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 423 | }; |
| 424 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 425 | // C++ mirror of java.lang.reflect.Field |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 426 | class MANAGED Field : public AccessibleObject { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 427 | public: |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 428 | Class* GetDeclaringClass() const; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 429 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 430 | void SetDeclaringClass(Class *new_declaring_class); |
| 431 | |
| 432 | const String* GetName() const; |
| 433 | |
| 434 | void SetName(String* new_name); |
| 435 | |
| 436 | uint32_t GetAccessFlags() const; |
| 437 | |
| 438 | void SetAccessFlags(uint32_t new_access_flags) { |
| 439 | SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, |
| 440 | false); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 443 | bool IsPublic() const { |
| 444 | return (GetAccessFlags() & kAccPublic) != 0; |
| 445 | } |
| 446 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 447 | bool IsStatic() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 448 | return (GetAccessFlags() & kAccStatic) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 449 | } |
| 450 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 451 | bool IsFinal() const { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 452 | return (GetAccessFlags() & kAccFinal) != 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 455 | uint32_t GetTypeIdx() const; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 456 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 457 | void SetTypeIdx(uint32_t type_idx); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 458 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 459 | // Gets type using type index and resolved types in the dex cache, may be null |
| 460 | // if type isn't yet resolved |
| 461 | Class* GetTypeDuringLinking() const; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 462 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 463 | // Performs full resolution, may return null and set exceptions if type cannot |
| 464 | // be resolved |
| 465 | Class* GetType() const; |
| 466 | |
| 467 | // Offset to field within an Object |
| 468 | MemberOffset GetOffset() const; |
| 469 | |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 470 | static MemberOffset OffsetOffset() { |
| 471 | return MemberOffset(OFFSETOF_MEMBER(Field, offset_)); |
| 472 | } |
| 473 | |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 474 | static Field* FindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer); |
| 475 | static Field* FindStaticFieldFromCode(uint32_t field_idx, const Method* referrer); |
| 476 | static Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static); |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 477 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 478 | MemberOffset GetOffsetDuringLinking() const; |
| 479 | |
| 480 | void SetOffset(MemberOffset num_bytes); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 481 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 482 | // field access, null object for static fields |
| 483 | bool GetBoolean(const Object* object) const; |
| 484 | void SetBoolean(Object* object, bool z) const; |
| 485 | int8_t GetByte(const Object* object) const; |
| 486 | void SetByte(Object* object, int8_t b) const; |
| 487 | uint16_t GetChar(const Object* object) const; |
| 488 | void SetChar(Object* object, uint16_t c) const; |
| 489 | uint16_t GetShort(const Object* object) const; |
| 490 | void SetShort(Object* object, uint16_t s) const; |
| 491 | int32_t GetInt(const Object* object) const; |
| 492 | void SetInt(Object* object, int32_t i) const; |
| 493 | int64_t GetLong(const Object* object) const; |
| 494 | void SetLong(Object* object, int64_t j) const; |
| 495 | float GetFloat(const Object* object) const; |
| 496 | void SetFloat(Object* object, float f) const; |
| 497 | double GetDouble(const Object* object) const; |
| 498 | void SetDouble(Object* object, double d) const; |
| 499 | Object* GetObject(const Object* object) const; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 500 | void SetObject(Object* object, const Object* l) const; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 501 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 502 | // Slow path routines for static field access when field was unresolved at |
| 503 | // compile time |
| 504 | static uint32_t Get32StaticFromCode(uint32_t field_idx, |
| 505 | const Method* referrer); |
| 506 | static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 507 | uint32_t new_value); |
| 508 | static uint64_t Get64StaticFromCode(uint32_t field_idx, |
| 509 | const Method* referrer); |
| 510 | static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 511 | uint64_t new_value); |
| 512 | static Object* GetObjStaticFromCode(uint32_t field_idx, |
| 513 | const Method* referrer); |
| 514 | static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, |
| 515 | Object* new_value); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 516 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 517 | static Class* GetJavaLangReflectField() { |
| 518 | DCHECK(java_lang_reflect_Field_ != NULL); |
| 519 | return java_lang_reflect_Field_; |
| 520 | } |
| 521 | |
| 522 | static void SetClass(Class* java_lang_reflect_Field); |
| 523 | static void ResetClass(); |
| 524 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 525 | bool IsVolatile() const { |
| 526 | return (GetAccessFlags() & kAccVolatile) != 0; |
| 527 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 528 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 529 | void InitJavaFields(); |
| 530 | |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 531 | private: |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 532 | // private implementation of field access using raw data |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 533 | uint32_t Get32(const Object* object) const; |
| 534 | void Set32(Object* object, uint32_t new_value) const; |
| 535 | uint64_t Get64(const Object* object) const; |
| 536 | void Set64(Object* object, uint64_t new_value) const; |
| 537 | Object* GetObj(const Object* object) const; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 538 | void SetObj(Object* object, const Object* new_value) const; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 539 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 540 | void InitJavaFieldsLocked(); |
| 541 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 542 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 543 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 544 | // The class in which this field is declared. |
| 545 | Class* declaring_class_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 546 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 547 | Object* generic_type_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 548 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 549 | const String* name_; |
| 550 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 551 | // Type of the field |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 552 | mutable Class* type_; |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 553 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 554 | uint32_t generic_types_are_initialized_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 555 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 556 | uint32_t access_flags_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 557 | |
| 558 | // Offset of field within an instance or in the Class' static fields |
| 559 | uint32_t offset_; |
| 560 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 561 | // Dex cache index of resolved type |
| 562 | uint32_t type_idx_; |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 563 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 564 | int32_t slot_; |
| 565 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 566 | static Class* java_lang_reflect_Field_; |
| 567 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 568 | friend struct FieldOffsets; // for verifying offset information |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 569 | DISALLOW_IMPLICIT_CONSTRUCTORS(Field); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 570 | }; |
| 571 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 572 | // C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 573 | class MANAGED Method : public AccessibleObject { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 574 | public: |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 575 | // An function that invokes a method with an array of its arguments. |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 576 | typedef void InvokeStub(const Method* method, |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 577 | Object* obj, |
| 578 | Thread* thread, |
| 579 | byte* args, |
| 580 | JValue* result); |
| 581 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 582 | Class* GetDeclaringClass() const; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 583 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 584 | void SetDeclaringClass(Class *new_declaring_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 585 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 586 | static MemberOffset DeclaringClassOffset() { |
| 587 | return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 590 | // Returns the method name, e.g. "<init>" or "eatLunch" |
| 591 | const String* GetName() const; |
| 592 | |
| 593 | void SetName(String* new_name); |
| 594 | |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 595 | ByteArray* GetRegisterMapData() const; |
| 596 | |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 597 | void SetRegisterMapData(ByteArray* data); |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 598 | |
| 599 | ByteArray* GetRegisterMapHeader() const; |
| 600 | |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 601 | void SetRegisterMapHeader(ByteArray* header); |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 602 | |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 603 | String* GetShorty() const; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 604 | |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 605 | void SetShorty(String* new_shorty); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 606 | |
| 607 | const String* GetSignature() const; |
| 608 | |
| 609 | void SetSignature(String* new_signature); |
| 610 | |
| 611 | bool HasSameNameAndDescriptor(const Method* that) const; |
| 612 | |
| 613 | uint32_t GetAccessFlags() const; |
| 614 | |
| 615 | void SetAccessFlags(uint32_t new_access_flags) { |
| 616 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, |
| 617 | false); |
| 618 | } |
| 619 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 620 | // Returns true if the method is declared public. |
| 621 | bool IsPublic() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 622 | return (GetAccessFlags() & kAccPublic) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // Returns true if the method is declared private. |
| 626 | bool IsPrivate() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 627 | return (GetAccessFlags() & kAccPrivate) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | // Returns true if the method is declared static. |
| 631 | bool IsStatic() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 632 | return (GetAccessFlags() & kAccStatic) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 635 | // Returns true if the method is a constructor. |
| 636 | bool IsConstructor() const { |
| 637 | return (access_flags_ & kAccConstructor) != 0; |
| 638 | } |
| 639 | |
| 640 | // Returns true if the method is static, private, or a constructor. |
| 641 | bool IsDirect() const { |
| 642 | return IsStatic() || IsPrivate() || IsConstructor(); |
| 643 | } |
| 644 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 645 | // Returns true if the method is declared synchronized. |
| 646 | bool IsSynchronized() const { |
| 647 | uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 648 | return (GetAccessFlags() & synchonized) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 651 | bool IsFinal() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 652 | return (GetAccessFlags() & kAccFinal) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 655 | bool IsMiranda() const { |
| 656 | return (GetAccessFlags() & kAccMiranda) != 0; |
| 657 | } |
| 658 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 659 | bool IsNative() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 660 | return (GetAccessFlags() & kAccNative) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 663 | bool IsAbstract() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 664 | return (GetAccessFlags() & kAccAbstract) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | bool IsSynthetic() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 668 | return (GetAccessFlags() & kAccSynthetic) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 671 | uint16_t GetMethodIndex() const; |
| 672 | |
| 673 | size_t GetVtableIndex() const { |
| 674 | return GetMethodIndex(); |
| 675 | } |
| 676 | |
| 677 | void SetMethodIndex(uint16_t new_method_index) { |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 678 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 679 | new_method_index, false); |
| 680 | } |
| 681 | |
| 682 | static MemberOffset MethodIndexOffset() { |
| 683 | return OFFSET_OF_OBJECT_MEMBER(Method, method_index_); |
| 684 | } |
| 685 | |
| 686 | uint32_t GetCodeItemOffset() const { |
| 687 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false); |
| 688 | } |
| 689 | |
| 690 | void SetCodeItemOffset(uint32_t new_code_off) { |
| 691 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), |
| 692 | new_code_off, false); |
| 693 | } |
| 694 | |
| 695 | // Number of 32bit registers that would be required to hold all the arguments |
| 696 | static size_t NumArgRegisters(const StringPiece& shorty); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 697 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 698 | // Number of argument bytes required for densely packing the |
| 699 | // arguments into an array of arguments. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 700 | size_t NumArgArrayBytes() const; |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 701 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 702 | uint16_t NumRegisters() const; |
| 703 | |
| 704 | void SetNumRegisters(uint16_t new_num_registers) { |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 705 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 706 | new_num_registers, false); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 709 | uint16_t NumIns() const; |
| 710 | |
| 711 | void SetNumIns(uint16_t new_num_ins) { |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 712 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 713 | new_num_ins, false); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 716 | uint16_t NumOuts() const; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 717 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 718 | void SetNumOuts(uint16_t new_num_outs) { |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 719 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 720 | new_num_outs, false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 723 | uint32_t GetProtoIdx() const; |
| 724 | |
| 725 | void SetProtoIdx(uint32_t new_proto_idx) { |
| 726 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 729 | ObjectArray<String>* GetDexCacheStrings() const; |
| 730 | void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings); |
| 731 | |
| 732 | static MemberOffset DexCacheStringsOffset() { |
| 733 | return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_); |
| 734 | } |
| 735 | |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 736 | static MemberOffset DexCacheResolvedTypesOffset() { |
| 737 | return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_); |
| 738 | } |
| 739 | |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 740 | static MemberOffset DexCacheResolvedFieldsOffset() { |
| 741 | return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_); |
| 742 | } |
| 743 | |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 744 | static MemberOffset DexCacheInitializedStaticStorageOffset() { |
| 745 | return OFFSET_OF_OBJECT_MEMBER(Method, |
| 746 | dex_cache_initialized_static_storage_); |
| 747 | } |
| 748 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 749 | ObjectArray<Class>* GetDexCacheResolvedTypes() const; |
| 750 | void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types); |
| 751 | |
| 752 | ObjectArray<Method>* GetDexCacheResolvedMethods() const; |
| 753 | void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods); |
| 754 | |
| 755 | ObjectArray<Field>* GetDexCacheResolvedFields() const; |
| 756 | void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields); |
| 757 | |
| 758 | CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const; |
| 759 | void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value); |
| 760 | |
| 761 | ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const; |
| 762 | void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value); |
| 763 | |
| 764 | void SetReturnTypeIdx(uint32_t new_return_type_idx); |
| 765 | |
| 766 | Class* GetReturnType() const; |
| 767 | |
| 768 | bool IsReturnAReference() const; |
| 769 | |
| 770 | bool IsReturnAFloat() const; |
| 771 | |
| 772 | bool IsReturnADouble() const; |
| 773 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 774 | bool IsReturnAFloatOrDouble() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 775 | return IsReturnAFloat() || IsReturnADouble(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 778 | bool IsReturnALong() const; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 779 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 780 | bool IsReturnVoid() const; |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 781 | |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 782 | // "Args" may refer to any of the 3 levels of "Args." |
| 783 | // To avoid confusion, our code will denote which "Args" clearly: |
| 784 | // 1. UserArgs: Args that a user see. |
| 785 | // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the |
| 786 | // receiver. |
| 787 | // 3. CConvArgs: Calling Convention Args, which is physical-level Args. |
| 788 | // E.g., the first in Args is Method* for both static and non-static |
| 789 | // methods. And CConvArgs doesn't deal with the receiver because |
| 790 | // receiver is hardwired in an implicit register, so CConvArgs doesn't |
| 791 | // need to deal with it. |
| 792 | // |
| 793 | // The number of Args that should be supplied to this method |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 794 | size_t NumArgs() const; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 795 | |
| 796 | // The number of reference arguments to this method including implicit this |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 797 | // pointer. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 798 | size_t NumReferenceArgs() const; |
| 799 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 800 | // The number of long or double arguments. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 801 | size_t NumLongOrDoubleArgs() const; |
| 802 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 803 | // Is the given method parameter a reference? |
| 804 | bool IsParamAReference(unsigned int param) const; |
| 805 | |
| 806 | // Is the given method parameter a long or double? |
| 807 | bool IsParamALongOrDouble(unsigned int param) const; |
| 808 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 809 | // Size in bytes of the given parameter |
| 810 | size_t ParamSize(unsigned int param) const; |
| 811 | |
| 812 | // Size in bytes of the return value |
| 813 | size_t ReturnSize() const; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 814 | |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 815 | void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const; |
| 816 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 817 | const ByteArray* GetCodeArray() const { |
| 818 | return GetFieldPtr<const ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), false); |
| 819 | } |
| 820 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 821 | const void* GetCode() const { |
| 822 | return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 823 | } |
| 824 | |
buzbee | 4ef7652 | 2011-09-08 10:00:32 -0700 | [diff] [blame] | 825 | void SetCode(ByteArray* code_array, InstructionSet instruction_set, |
buzbee | c41e5b5 | 2011-09-23 12:46:19 -0700 | [diff] [blame] | 826 | IntArray* mapping_table = NULL, ShortArray* vmap_table = NULL); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 827 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 828 | static MemberOffset GetCodeOffset() { |
| 829 | return OFFSET_OF_OBJECT_MEMBER(Method, code_); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 832 | // Is the given PC within the code array? |
| 833 | bool IsWithinCode(uintptr_t pc) const; |
| 834 | |
| 835 | IntArray* GetMappingTable() const { |
| 836 | return GetFieldObject<IntArray*>( |
| 837 | OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false); |
| 838 | } |
| 839 | |
buzbee | c41e5b5 | 2011-09-23 12:46:19 -0700 | [diff] [blame] | 840 | ShortArray* GetVMapTable() const { |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 841 | return GetFieldObject<ShortArray*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false); |
buzbee | c41e5b5 | 2011-09-23 12:46:19 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 844 | size_t GetFrameSizeInBytes() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 845 | DCHECK(sizeof(size_t) == sizeof(uint32_t)); |
| 846 | size_t result = GetField32( |
| 847 | OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false); |
| 848 | DCHECK_LE(static_cast<size_t>(kStackAlignment), result); |
| 849 | return result; |
| 850 | } |
| 851 | |
| 852 | void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) { |
| 853 | DCHECK(sizeof(size_t) == sizeof(uint32_t)); |
| 854 | DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes); |
| 855 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), |
| 856 | new_frame_size_in_bytes, false); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 859 | size_t GetReturnPcOffsetInBytes() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 860 | DCHECK(sizeof(size_t) == sizeof(uint32_t)); |
| 861 | return GetField32( |
| 862 | OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 865 | void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) { |
| 866 | DCHECK(sizeof(size_t) == sizeof(uint32_t)); |
| 867 | DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes()); |
| 868 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), |
| 869 | return_pc_offset_in_bytes, false); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 872 | bool IsRegistered(); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 873 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 874 | void RegisterNative(const void* native_method); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 875 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 876 | void UnregisterNative(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 877 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 878 | static MemberOffset NativeMethodOffset() { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 879 | return OFFSET_OF_OBJECT_MEMBER(Method, native_method_); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 882 | const void* GetNativeMethod() const { |
| 883 | return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false)); |
| 884 | } |
| 885 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 886 | ByteArray* GetInvokeStubArray() const { |
| 887 | ByteArray* result = GetFieldPtr<ByteArray*>( |
| 888 | OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), false); |
| 889 | // TODO: DCHECK(result != NULL); should be ahead of time compiled |
| 890 | return result; |
| 891 | } |
| 892 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 893 | // Native to managed invocation stub entry point |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 894 | InvokeStub* GetInvokeStub() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 895 | InvokeStub* result = GetFieldPtr<InvokeStub*>( |
| 896 | OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false); |
| 897 | // TODO: DCHECK(result != NULL); should be ahead of time compiled |
| 898 | return result; |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 901 | static MemberOffset GetInvokeStubOffset() { |
| 902 | return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 903 | } |
| 904 | |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 905 | static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() { |
| 906 | return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_); |
| 907 | } |
| 908 | |
| 909 | static MemberOffset GetDexCacheResolvedMethodsOffset() { |
| 910 | return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_); |
| 911 | } |
| 912 | |
| 913 | static MemberOffset GetMethodIndexOffset() { |
| 914 | return OFFSET_OF_OBJECT_MEMBER(Method, method_index_); |
| 915 | } |
| 916 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 917 | void SetInvokeStub(const ByteArray* invoke_stub_array); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 918 | |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 919 | uint32_t GetCoreSpillMask() const { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 920 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 921 | } |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 922 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 923 | void SetCoreSpillMask(uint32_t core_spill_mask) { |
| 924 | // Computed during compilation |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 925 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 928 | uint32_t GetFpSpillMask() const { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 929 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false); |
| 930 | } |
| 931 | |
| 932 | void SetFpSpillMask(uint32_t fp_spill_mask) { |
| 933 | // Computed during compilation |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 934 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 937 | // Is this a hand crafted method used for something like describing callee saves? |
| 938 | bool IsPhony() const { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 939 | bool result = this == Runtime::Current()->GetCalleeSaveMethod(); |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 940 | // Check that if we do think it is phony it looks like the callee save method |
| 941 | DCHECK(!result || GetCoreSpillMask() != 0); |
| 942 | return result; |
| 943 | } |
| 944 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 945 | // Converts a native PC to a dex PC. TODO: this is a no-op |
| 946 | // until we associate a PC mapping table with each method. |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 947 | uint32_t ToDexPC(const uintptr_t pc) const; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 948 | |
| 949 | // Converts a dex PC to a native PC. TODO: this is a no-op |
| 950 | // until we associate a PC mapping table with each method. |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 951 | uintptr_t ToNativePC(const uint32_t dex_pc) const; |
| 952 | |
| 953 | // Find the catch block for the given exception type and dex_pc |
| 954 | uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 955 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 956 | static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method); |
| 957 | |
| 958 | static Class* GetConstructorClass() { |
| 959 | return java_lang_reflect_Constructor_; |
| 960 | } |
| 961 | |
| 962 | static Class* GetMethodClass() { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 963 | return java_lang_reflect_Method_; |
| 964 | } |
| 965 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 966 | static void ResetClasses(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 967 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 968 | void InitJavaFields(); |
| 969 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 970 | private: |
| 971 | uint32_t GetReturnTypeIdx() const; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 972 | void InitJavaFieldsLocked(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 973 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 974 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 975 | // the class we are a part of |
| 976 | Class* declaring_class_; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 977 | ObjectArray<Class>* java_exception_types_; // TODO |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 978 | Object* java_formal_type_parameters_; |
| 979 | Object* java_generic_exception_types_; |
| 980 | Object* java_generic_parameter_types_; |
| 981 | Object* java_generic_return_type_; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 982 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 983 | String* name_; |
| 984 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 985 | // Initialized by InitJavaFields. |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 986 | ObjectArray<Class>* java_parameter_types_; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 987 | Class* java_return_type_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 988 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 989 | // Storage for code_ |
| 990 | const ByteArray* code_array_; |
| 991 | |
| 992 | // short cuts to declaring_class_->dex_cache_ member for fast compiled code access |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 993 | CodeAndDirectMethods* dex_cache_code_and_direct_methods_; |
| 994 | |
| 995 | // short cuts to declaring_class_->dex_cache_ member for fast compiled code access |
| 996 | ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_; |
| 997 | |
| 998 | // short cuts to declaring_class_->dex_cache_ member for fast compiled code access |
| 999 | ObjectArray<Field>* dex_cache_resolved_fields_; |
| 1000 | |
| 1001 | // short cuts to declaring_class_->dex_cache_ member for fast compiled code access |
| 1002 | ObjectArray<Method>* dex_cache_resolved_methods_; |
| 1003 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1004 | // short cuts to declaring_class_->dex_cache_ member for fast compiled code access |
| 1005 | ObjectArray<Class>* dex_cache_resolved_types_; |
| 1006 | |
| 1007 | // short cuts to declaring_class_->dex_cache_ member for fast compiled code access |
| 1008 | ObjectArray<String>* dex_cache_strings_; |
| 1009 | |
| 1010 | // Storage for invoke_stub_ |
| 1011 | const ByteArray* invoke_stub_array_; |
| 1012 | |
| 1013 | // Storage for mapping_table_ |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1014 | IntArray* mapping_table_; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1015 | |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 1016 | // Byte arrays that hold data for the register maps |
| 1017 | const ByteArray* register_map_data_; |
| 1018 | const ByteArray* register_map_header_; |
| 1019 | |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 1020 | // The short-form method descriptor string. |
| 1021 | String* shorty_; |
| 1022 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1023 | // The method descriptor. This represents the parameters a method |
| 1024 | // takes and value it returns. This string is a list of the type |
| 1025 | // descriptors for the parameters enclosed in parenthesis followed |
| 1026 | // by the return type descriptor. For example, for the method |
| 1027 | // |
| 1028 | // Object mymethod(int i, double d, Thread t) |
| 1029 | // |
| 1030 | // the method descriptor would be |
| 1031 | // |
| 1032 | // (IDLjava/lang/Thread;)Ljava/lang/Object; |
| 1033 | String* signature_; |
| 1034 | |
buzbee | c41e5b5 | 2011-09-23 12:46:19 -0700 | [diff] [blame] | 1035 | // Storage for Dalvik virtual register mapping_table_ |
| 1036 | ShortArray* vmap_table_; |
| 1037 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1038 | uint32_t java_generic_types_are_initialized_; |
| 1039 | |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 1040 | // Access flags; low 16 bits are defined by spec. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1041 | uint32_t access_flags_; |
| 1042 | |
| 1043 | // Compiled code associated with this method for callers from managed code. |
| 1044 | // May be compiled managed code or a bridge for invoking a native method. |
| 1045 | const void* code_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1046 | |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 1047 | // Offset to the CodeItem. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1048 | uint32_t code_item_offset_; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 1049 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1050 | // Architecture-dependent register spill mask |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1051 | uint32_t core_spill_mask_; |
| 1052 | |
| 1053 | // Architecture-dependent register spill mask |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1054 | uint32_t fp_spill_mask_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1055 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1056 | // Total size in bytes of the frame |
| 1057 | size_t frame_size_in_bytes_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1058 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1059 | // Native invocation stub entry point for calling from native to managed code. |
| 1060 | const InvokeStub* invoke_stub_; |
buzbee | 4ef7652 | 2011-09-08 10:00:32 -0700 | [diff] [blame] | 1061 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1062 | // Index of the return type |
| 1063 | uint32_t java_return_type_idx_; |
| 1064 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1065 | // For concrete virtual methods, this is the offset of the method |
| 1066 | // in Class::vtable_. |
| 1067 | // |
| 1068 | // For abstract methods in an interface class, this is the offset |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1069 | // of the method in "iftable_->Get(n)->GetMethodArray()". |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 1070 | uint32_t method_index_; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1071 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1072 | // The target native method registered with this method |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1073 | const void* native_method_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1074 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1075 | // Method bounds; not needed for an abstract method. |
| 1076 | // |
| 1077 | // For a native method, we compute the size of the argument list, and |
| 1078 | // set "insSize" and "registerSize" equal to it. |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 1079 | uint32_t num_ins_; |
| 1080 | uint32_t num_outs_; |
| 1081 | uint32_t num_registers_; // ins + locals |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1082 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1083 | // Method prototype descriptor string (return and argument types). |
| 1084 | uint32_t proto_idx_; |
| 1085 | |
| 1086 | // Offset of return PC within frame for compiled code (in bytes) |
| 1087 | size_t return_pc_offset_in_bytes_; |
| 1088 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1089 | uint32_t java_slot_; |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1090 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1091 | static Class* java_lang_reflect_Constructor_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1092 | static Class* java_lang_reflect_Method_; |
| 1093 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1094 | friend class ImageWriter; // for relocating code_ and invoke_stub_ |
| 1095 | friend struct MethodOffsets; // for verifying offset information |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1096 | DISALLOW_IMPLICIT_CONSTRUCTORS(Method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1097 | }; |
| 1098 | |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 1099 | class MANAGED Array : public Object { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1100 | public: |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1101 | static size_t SizeOf(size_t component_count, |
| 1102 | size_t component_size) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1103 | return sizeof(Array) + component_count * component_size; |
| 1104 | } |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 1105 | |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 1106 | // A convenience for code that doesn't know the component size, |
| 1107 | // and doesn't want to have to work it out itself. |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 1108 | static Array* Alloc(Class* array_class, int32_t component_count); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 1109 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 1110 | static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size); |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1111 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 1112 | size_t SizeOf() const; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1113 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1114 | int32_t GetLength() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1115 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1116 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1117 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1118 | void SetLength(int32_t length) { |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 1119 | CHECK_GE(length, 0); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1120 | SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1121 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1122 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1123 | static MemberOffset LengthOffset() { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1124 | return OFFSET_OF_OBJECT_MEMBER(Array, length_); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static MemberOffset DataOffset() { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1128 | return OFFSET_OF_OBJECT_MEMBER(Array, first_element_); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 1131 | void* GetRawData() { |
| 1132 | return reinterpret_cast<void*>(first_element_); |
| 1133 | } |
| 1134 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1135 | protected: |
| 1136 | bool IsValidIndex(int32_t index) const { |
| 1137 | if (index < 0 || index >= length_) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1138 | return ThrowArrayIndexOutOfBoundsException(index); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1139 | } |
| 1140 | return true; |
| 1141 | } |
| 1142 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1143 | protected: |
| 1144 | bool ThrowArrayIndexOutOfBoundsException(int32_t index) const; |
| 1145 | bool ThrowArrayStoreException(Object* object) const; |
| 1146 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1147 | private: |
| 1148 | // The number of array elements. |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1149 | int32_t length_; |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 1150 | // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary |
| 1151 | int32_t padding_; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1152 | // Marker for the data (used by generated code) |
| 1153 | uint32_t first_element_[0]; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1154 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1155 | DISALLOW_IMPLICIT_CONSTRUCTORS(Array); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1156 | }; |
| 1157 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1158 | template<class T> |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 1159 | class MANAGED ObjectArray : public Array { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1160 | public: |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1161 | static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1162 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1163 | T* Get(int32_t i) const; |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 1164 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1165 | void Set(int32_t i, T* object); |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 1166 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1167 | // Set element without bound and element type checks, to be used in limited |
| 1168 | // circumstances, such as during boot image writing |
| 1169 | void SetWithoutChecks(int32_t i, T* object); |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1170 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1171 | static void Copy(const ObjectArray<T>* src, int src_pos, |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1172 | ObjectArray<T>* dst, int dst_pos, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1173 | size_t length); |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1174 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1175 | ObjectArray<T>* CopyOf(int32_t new_length); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1176 | |
| 1177 | private: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1178 | DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1179 | }; |
| 1180 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1181 | template<class T> |
| 1182 | ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) { |
| 1183 | return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>(); |
| 1184 | } |
| 1185 | |
| 1186 | template<class T> |
| 1187 | T* ObjectArray<T>::Get(int32_t i) const { |
| 1188 | if (!IsValidIndex(i)) { |
| 1189 | return NULL; |
| 1190 | } |
| 1191 | MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*)); |
| 1192 | return GetFieldObject<T*>(data_offset, false); |
| 1193 | } |
| 1194 | |
| 1195 | template<class T> |
| 1196 | ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) { |
| 1197 | ObjectArray<T>* new_array = Alloc(GetClass(), new_length); |
| 1198 | Copy(this, 0, new_array, 0, std::min(GetLength(), new_length)); |
| 1199 | return new_array; |
| 1200 | } |
| 1201 | |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1202 | // Type for the InitializedStaticStorage table. Currently the Class |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 1203 | // provides the static storage. However, this might change to an Array |
| 1204 | // to improve image sharing, so we use this type to avoid assumptions |
| 1205 | // on the current storage. |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 1206 | class MANAGED StaticStorageBase : public Object {}; |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1207 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1208 | // C++ mirror of java.lang.Class |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 1209 | class MANAGED Class : public StaticStorageBase { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1210 | public: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1211 | |
| 1212 | // Class Status |
| 1213 | // |
| 1214 | // kStatusNotReady: If a Class cannot be found in the class table by |
| 1215 | // FindClass, it allocates an new one with AllocClass in the |
| 1216 | // kStatusNotReady and calls LoadClass. Note if it does find a |
| 1217 | // class, it may not be kStatusResolved and it will try to push it |
| 1218 | // forward toward kStatusResolved. |
| 1219 | // |
| 1220 | // kStatusIdx: LoadClass populates with Class with information from |
| 1221 | // the DexFile, moving the status to kStatusIdx, indicating that the |
| 1222 | // Class values in super_class_ and interfaces_ have not been |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1223 | // populated based on super_class_type_idx_ and |
| 1224 | // interfaces_type_idx_. The new Class can then be inserted into the |
| 1225 | // classes table. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1226 | // |
| 1227 | // kStatusLoaded: After taking a lock on Class, the ClassLinker will |
| 1228 | // attempt to move a kStatusIdx class forward to kStatusLoaded by |
| 1229 | // using ResolveClass to initialize the super_class_ and interfaces_. |
| 1230 | // |
| 1231 | // kStatusResolved: Still holding the lock on Class, the ClassLinker |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1232 | // shows linking is complete and fields of the Class populated by making |
| 1233 | // it kStatusResolved. Java allows circularities of the form where a super |
| 1234 | // class has a field that is of the type of the sub class. We need to be able |
| 1235 | // to fully resolve super classes while resolving types for fields. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1236 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1237 | enum Status { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 1238 | kStatusError = -1, |
| 1239 | kStatusNotReady = 0, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1240 | kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_ |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 1241 | kStatusLoaded = 2, // DEX idx values resolved |
| 1242 | kStatusResolved = 3, // part of linking |
| 1243 | kStatusVerifying = 4, // in the process of being verified |
| 1244 | kStatusVerified = 5, // logically part of linking; done pre-init |
| 1245 | kStatusInitializing = 6, // class init in progress |
| 1246 | kStatusInitialized = 7, // ready to go |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1247 | }; |
| 1248 | |
| 1249 | enum PrimitiveType { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1250 | kPrimNot = 0, |
| 1251 | kPrimBoolean, |
| 1252 | kPrimByte, |
| 1253 | kPrimChar, |
| 1254 | kPrimShort, |
| 1255 | kPrimInt, |
| 1256 | kPrimLong, |
| 1257 | kPrimFloat, |
| 1258 | kPrimDouble, |
| 1259 | kPrimVoid, |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1260 | }; |
| 1261 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1262 | Status GetStatus() const { |
| 1263 | CHECK(sizeof(Status) == sizeof(uint32_t)); |
| 1264 | return static_cast<Status>( |
| 1265 | GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false)); |
| 1266 | } |
| 1267 | |
| 1268 | void SetStatus(Status new_status); |
| 1269 | |
| 1270 | // Returns true if the class has failed to link. |
| 1271 | bool IsErroneous() const { |
| 1272 | return GetStatus() == kStatusError; |
| 1273 | } |
| 1274 | |
| 1275 | // Returns true if the class has been loaded. |
| 1276 | bool IsIdxLoaded() const { |
| 1277 | return GetStatus() >= kStatusIdx; |
| 1278 | } |
| 1279 | |
| 1280 | // Returns true if the class has been loaded. |
| 1281 | bool IsLoaded() const { |
| 1282 | return GetStatus() >= kStatusLoaded; |
| 1283 | } |
| 1284 | |
| 1285 | // Returns true if the class has been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1286 | bool IsResolved() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1287 | return GetStatus() >= kStatusResolved; |
| 1288 | } |
| 1289 | |
| 1290 | // Returns true if the class has been verified. |
| 1291 | bool IsVerified() const { |
| 1292 | return GetStatus() >= kStatusVerified; |
| 1293 | } |
| 1294 | |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 1295 | // Returns true if the class is initializing. |
| 1296 | bool IsInitializing() const { |
| 1297 | return GetStatus() >= kStatusInitializing; |
| 1298 | } |
| 1299 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1300 | // Returns true if the class is initialized. |
| 1301 | bool IsInitialized() const { |
| 1302 | return GetStatus() == kStatusInitialized; |
| 1303 | } |
| 1304 | |
| 1305 | uint32_t GetAccessFlags() const; |
| 1306 | |
| 1307 | void SetAccessFlags(uint32_t new_access_flags) { |
| 1308 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, |
| 1309 | false); |
| 1310 | } |
| 1311 | |
| 1312 | // Returns true if the class is an interface. |
| 1313 | bool IsInterface() const { |
| 1314 | return (GetAccessFlags() & kAccInterface) != 0; |
| 1315 | } |
| 1316 | |
| 1317 | // Returns true if the class is declared public. |
| 1318 | bool IsPublic() const { |
| 1319 | return (GetAccessFlags() & kAccPublic) != 0; |
| 1320 | } |
| 1321 | |
| 1322 | // Returns true if the class is declared final. |
| 1323 | bool IsFinal() const { |
| 1324 | return (GetAccessFlags() & kAccFinal) != 0; |
| 1325 | } |
| 1326 | |
| 1327 | // Returns true if the class is abstract. |
| 1328 | bool IsAbstract() const { |
| 1329 | return (GetAccessFlags() & kAccAbstract) != 0; |
| 1330 | } |
| 1331 | |
| 1332 | // Returns true if the class is an annotation. |
| 1333 | bool IsAnnotation() const { |
| 1334 | return (GetAccessFlags() & kAccAnnotation) != 0; |
| 1335 | } |
| 1336 | |
| 1337 | // Returns true if the class is synthetic. |
| 1338 | bool IsSynthetic() const { |
| 1339 | return (GetAccessFlags() & kAccSynthetic) != 0; |
| 1340 | } |
| 1341 | |
| 1342 | bool IsReferenceClass() const { |
| 1343 | return (GetAccessFlags() & kAccClassIsReference) != 0; |
| 1344 | } |
| 1345 | |
| 1346 | bool IsWeakReferenceClass() const { |
| 1347 | return (GetAccessFlags() & kAccClassIsWeakReference) != 0; |
| 1348 | } |
| 1349 | |
| 1350 | bool IsSoftReferenceClass() const { |
| 1351 | return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference; |
| 1352 | } |
| 1353 | |
| 1354 | bool IsFinalizerReferenceClass() const { |
| 1355 | return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0; |
| 1356 | } |
| 1357 | |
| 1358 | bool IsPhantomReferenceClass() const { |
| 1359 | return (GetAccessFlags() & kAccClassIsPhantomReference) != 0; |
| 1360 | } |
| 1361 | |
| 1362 | PrimitiveType GetPrimitiveType() const { |
| 1363 | CHECK(sizeof(PrimitiveType) == sizeof(int32_t)); |
| 1364 | return static_cast<PrimitiveType>( |
| 1365 | GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false)); |
| 1366 | } |
| 1367 | |
| 1368 | void SetPrimitiveType(PrimitiveType new_type) { |
| 1369 | CHECK(sizeof(PrimitiveType) == sizeof(int32_t)); |
| 1370 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, |
| 1371 | false); |
| 1372 | } |
| 1373 | |
| 1374 | // Returns true if the class is a primitive type. |
| 1375 | bool IsPrimitive() const { |
| 1376 | return GetPrimitiveType() != kPrimNot; |
| 1377 | } |
| 1378 | |
| 1379 | bool IsPrimitiveBoolean() const { |
| 1380 | return GetPrimitiveType() == kPrimBoolean; |
| 1381 | } |
| 1382 | |
| 1383 | bool IsPrimitiveByte() const { |
| 1384 | return GetPrimitiveType() == kPrimByte; |
| 1385 | } |
| 1386 | |
| 1387 | bool IsPrimitiveChar() const { |
| 1388 | return GetPrimitiveType() == kPrimChar; |
| 1389 | } |
| 1390 | |
| 1391 | bool IsPrimitiveShort() const { |
| 1392 | return GetPrimitiveType() == kPrimShort; |
| 1393 | } |
| 1394 | |
| 1395 | bool IsPrimitiveInt() const { |
| 1396 | return GetPrimitiveType() == kPrimInt; |
| 1397 | } |
| 1398 | |
| 1399 | bool IsPrimitiveLong() const { |
| 1400 | return GetPrimitiveType() == kPrimLong; |
| 1401 | } |
| 1402 | |
| 1403 | bool IsPrimitiveFloat() const { |
| 1404 | return GetPrimitiveType() == kPrimFloat; |
| 1405 | } |
| 1406 | |
| 1407 | bool IsPrimitiveDouble() const { |
| 1408 | return GetPrimitiveType() == kPrimDouble; |
| 1409 | } |
| 1410 | |
| 1411 | bool IsPrimitiveVoid() const { |
| 1412 | return GetPrimitiveType() == kPrimVoid; |
| 1413 | } |
| 1414 | |
| 1415 | size_t PrimitiveSize() const; |
| 1416 | |
| 1417 | bool IsArrayClass() const { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1418 | return GetComponentType() != NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | Class* GetComponentType() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1422 | return GetFieldObject<Class*>( |
| 1423 | OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false); |
| 1424 | } |
| 1425 | |
| 1426 | void SetComponentType(Class* new_component_type) { |
| 1427 | DCHECK(GetComponentType() == NULL); |
| 1428 | DCHECK(new_component_type != NULL); |
| 1429 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), |
| 1430 | new_component_type, false); |
| 1431 | } |
| 1432 | |
| 1433 | size_t GetComponentSize() const { |
| 1434 | return GetTypeSize(GetComponentType()->GetDescriptor()); |
| 1435 | } |
| 1436 | |
| 1437 | bool IsObjectClass() const { |
| 1438 | return !IsPrimitive() && GetSuperClass() == NULL; |
| 1439 | } |
| 1440 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 1441 | // Creates a raw object instance but does not invoke the default constructor. |
| 1442 | Object* AllocObject(); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1443 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1444 | static size_t GetTypeSize(const String* descriptor); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1445 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1446 | const String* GetDescriptor() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1447 | const String* result = GetFieldObject<const String*>( |
| 1448 | OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false); |
| 1449 | // DCHECK(result != NULL); // may be NULL prior to class linker initialization |
| 1450 | // DCHECK_NE(0, result->GetLength()); // TODO: keep? |
| 1451 | return result; |
| 1452 | } |
| 1453 | |
| 1454 | void SetDescriptor(String* new_descriptor); |
| 1455 | |
| 1456 | bool IsVariableSize() const { |
| 1457 | // Classes and arrays vary in size, and so the object_size_ field cannot |
| 1458 | // be used to get their instance size |
| 1459 | return IsClassClass() || IsArrayClass(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1462 | size_t SizeOf() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1463 | CHECK(sizeof(size_t) == sizeof(int32_t)); |
| 1464 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1467 | size_t GetClassSize() const { |
| 1468 | CHECK(sizeof(size_t) == sizeof(uint32_t)); |
| 1469 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1472 | void SetClassSize(size_t new_class_size) { |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 1473 | DCHECK(new_class_size >= GetClassSize()) |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 1474 | << " class=" << PrettyTypeOf(this) |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 1475 | << " new_class_size=" << new_class_size |
| 1476 | << " GetClassSize=" << GetClassSize(); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 1477 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1480 | size_t GetObjectSize() const { |
| 1481 | CHECK(!IsVariableSize()); |
| 1482 | CHECK(sizeof(size_t) == sizeof(int32_t)); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 1483 | size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1484 | CHECK_GE(result, sizeof(Object)); |
| 1485 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1488 | void SetObjectSize(size_t new_object_size) { |
| 1489 | DCHECK(!IsVariableSize()); |
| 1490 | CHECK(sizeof(size_t) == sizeof(int32_t)); |
| 1491 | return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), |
| 1492 | new_object_size, false); |
Carl Shapiro | 83ab4f3 | 2011-08-15 20:21:39 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 1495 | // Returns true if this class is in the same packages as that class. |
| 1496 | bool IsInSamePackage(const Class* that) const; |
| 1497 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1498 | static bool IsInSamePackage(const String* descriptor1, |
| 1499 | const String* descriptor2); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 1500 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 1501 | // Returns true if this class can access that class. |
| 1502 | bool CanAccess(const Class* that) const { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1503 | return that->IsPublic() || this->IsInSamePackage(that); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
jeffhao | 4a801a4 | 2011-09-23 13:53:40 -0700 | [diff] [blame] | 1506 | // Validate method/field access. |
| 1507 | bool CanAccessMember(const Class* access_to, uint32_t member_flags) const { |
| 1508 | // quick accept for public access |
| 1509 | if (member_flags & kAccPublic) { |
| 1510 | return true; |
| 1511 | } |
| 1512 | |
| 1513 | // quick accept for access from same class |
| 1514 | if (this == access_to) { |
| 1515 | return true; |
| 1516 | } |
| 1517 | |
| 1518 | // quick reject for private access from another class |
| 1519 | if (member_flags & kAccPrivate) { |
| 1520 | return false; |
| 1521 | } |
| 1522 | |
| 1523 | // Semi-quick test for protected access from a sub-class, which may or |
| 1524 | // may not be in the same package. |
| 1525 | if (member_flags & kAccProtected) { |
| 1526 | if (this->IsSubClass(access_to)) { |
| 1527 | return true; |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | // Allow protected and private access from other classes in the same package. |
| 1532 | return this->IsInSamePackage(access_to); |
| 1533 | } |
| 1534 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 1535 | bool IsSubClass(const Class* klass) const; |
| 1536 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1537 | bool IsAssignableFrom(const Class* src) const { |
| 1538 | DCHECK(src != NULL); |
| 1539 | if (this == src) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1540 | // Can always assign to things of the same type |
| 1541 | return true; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1542 | } else if (IsObjectClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1543 | // Can assign any reference to java.lang.Object |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1544 | return !src->IsPrimitive(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1545 | } else if (IsInterface()) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1546 | return src->Implements(this); |
| 1547 | } else if (src->IsArrayClass()) { |
| 1548 | return IsAssignableFromArray(src); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1549 | } else { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1550 | return src->IsSubClass(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1551 | } |
| 1552 | } |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 1553 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1554 | Class* GetSuperClass() const { |
| 1555 | // Can only get super class for loaded classes (hack for when runtime is |
| 1556 | // initializing) |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1557 | DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1558 | return GetFieldObject<Class*>( |
| 1559 | OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false); |
| 1560 | } |
| 1561 | |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 1562 | static MemberOffset SuperClassOffset() { |
| 1563 | return MemberOffset(OFFSETOF_MEMBER(Class, super_class_)); |
| 1564 | } |
| 1565 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1566 | void SetSuperClass(Class *new_super_class) { |
| 1567 | // super class is assigned once, except during class linker initialization |
| 1568 | Class* old_super_class = GetFieldObject<Class*>( |
| 1569 | OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false); |
| 1570 | DCHECK(old_super_class == NULL || old_super_class == new_super_class); |
| 1571 | DCHECK(new_super_class != NULL); |
| 1572 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), |
| 1573 | new_super_class, false); |
| 1574 | } |
| 1575 | |
| 1576 | bool HasSuperClass() const { |
| 1577 | return GetSuperClass() != NULL; |
| 1578 | } |
| 1579 | |
| 1580 | uint32_t GetSuperClassTypeIdx() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1581 | DCHECK(IsIdxLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1582 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), |
| 1583 | false); |
| 1584 | } |
| 1585 | |
| 1586 | void SetSuperClassTypeIdx(int32_t new_super_class_idx) { |
| 1587 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), |
| 1588 | new_super_class_idx, false); |
| 1589 | } |
| 1590 | |
| 1591 | const ClassLoader* GetClassLoader() const; |
| 1592 | |
| 1593 | void SetClassLoader(const ClassLoader* new_cl); |
| 1594 | |
| 1595 | static MemberOffset DexCacheOffset() { |
| 1596 | return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_)); |
| 1597 | } |
| 1598 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1599 | enum { |
| 1600 | kDumpClassFullDetail = 1, |
| 1601 | kDumpClassClassLoader = (1 << 1), |
| 1602 | kDumpClassInitialized = (1 << 2), |
| 1603 | }; |
| 1604 | |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 1605 | void DumpClass(std::ostream& os, int flags) const; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1606 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1607 | DexCache* GetDexCache() const; |
| 1608 | |
| 1609 | void SetDexCache(DexCache* new_dex_cache); |
| 1610 | |
| 1611 | ObjectArray<Method>* GetDirectMethods() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1612 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1613 | return GetFieldObject<ObjectArray<Method>*>( |
| 1614 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false); |
| 1615 | } |
| 1616 | |
| 1617 | void SetDirectMethods(ObjectArray<Method>* new_direct_methods) { |
| 1618 | DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>( |
| 1619 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false)); |
| 1620 | DCHECK_NE(0, new_direct_methods->GetLength()); |
| 1621 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), |
| 1622 | new_direct_methods, false); |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1625 | Method* GetDirectMethod(int32_t i) const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1626 | return GetDirectMethods()->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1630 | ObjectArray<Method>* direct_methods = |
| 1631 | GetFieldObject<ObjectArray<Method>*>( |
| 1632 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false); |
| 1633 | direct_methods->Set(i, f); |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1636 | // Returns the number of static, private, and constructor methods. |
| 1637 | size_t NumDirectMethods() const { |
| 1638 | return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0; |
| 1639 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1640 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1641 | ObjectArray<Method>* GetVirtualMethods() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1642 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1643 | return GetFieldObject<ObjectArray<Method>*>( |
| 1644 | OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false); |
| 1645 | } |
| 1646 | |
| 1647 | void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) { |
| 1648 | // TODO: we reassign virtual methods to grow the table for miranda |
| 1649 | // methods.. they should really just be assigned once |
| 1650 | DCHECK_NE(0, new_virtual_methods->GetLength()); |
| 1651 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), |
| 1652 | new_virtual_methods, false); |
| 1653 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1654 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1655 | // Returns the number of non-inherited virtual methods. |
| 1656 | size_t NumVirtualMethods() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1657 | return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | Method* GetVirtualMethod(uint32_t i) const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1661 | DCHECK(IsResolved() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1662 | return GetVirtualMethods()->Get(i); |
| 1663 | } |
| 1664 | |
| 1665 | Method* GetVirtualMethodDuringLinking(uint32_t i) const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1666 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1667 | return GetVirtualMethods()->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1671 | ObjectArray<Method>* virtual_methods = |
| 1672 | GetFieldObject<ObjectArray<Method>*>( |
| 1673 | OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false); |
| 1674 | virtual_methods->Set(i, f); |
| 1675 | } |
| 1676 | |
| 1677 | ObjectArray<Method>* GetVTable() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1678 | DCHECK(IsResolved() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1679 | return GetFieldObject<ObjectArray<Method>*>( |
| 1680 | OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false); |
| 1681 | } |
| 1682 | |
| 1683 | ObjectArray<Method>* GetVTableDuringLinking() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1684 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1685 | return GetFieldObject<ObjectArray<Method>*>( |
| 1686 | OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false); |
| 1687 | } |
| 1688 | |
| 1689 | void SetVTable(ObjectArray<Method>* new_vtable) { |
| 1690 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false); |
| 1691 | } |
| 1692 | |
| 1693 | static MemberOffset VTableOffset() { |
| 1694 | return OFFSET_OF_OBJECT_MEMBER(Class, vtable_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1697 | // Given a method implemented by this class but potentially from a |
| 1698 | // super class, return the specific implementation |
| 1699 | // method for this class. |
| 1700 | Method* FindVirtualMethodForVirtual(Method* method) { |
| 1701 | DCHECK(!method->GetDeclaringClass()->IsInterface()); |
| 1702 | // The argument method may from a super class. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1703 | // Use the index to a potentially overridden one for this instance's class. |
| 1704 | return GetVTable()->Get(method->GetMethodIndex()); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | // Given a method implemented by this class, but potentially from a |
| 1708 | // super class or interface, return the specific implementation |
| 1709 | // method for this class. |
| 1710 | Method* FindVirtualMethodForInterface(Method* method); |
| 1711 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1712 | Method* FindInterfaceMethod(const StringPiece& name, |
| 1713 | const StringPiece& descriptor); |
| 1714 | |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1715 | Method* FindVirtualMethodForVirtualOrInterface(Method* method) { |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 1716 | if (method->IsDirect()) { |
| 1717 | return method; |
| 1718 | } |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1719 | if (method->GetDeclaringClass()->IsInterface()) { |
| 1720 | return FindVirtualMethodForInterface(method); |
| 1721 | } |
| 1722 | return FindVirtualMethodForVirtual(method); |
Elliott Hughes | 72025e5 | 2011-08-23 17:50:30 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1725 | Method* FindDeclaredVirtualMethod(const StringPiece& name, |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 1726 | const StringPiece& signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1727 | |
| 1728 | Method* FindVirtualMethod(const StringPiece& name, |
| 1729 | const StringPiece& descriptor); |
| 1730 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1731 | Method* FindDeclaredDirectMethod(const StringPiece& name, |
| 1732 | const StringPiece& signature); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1733 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1734 | Method* FindDirectMethod(const StringPiece& name, |
| 1735 | const StringPiece& signature); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1736 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1737 | size_t NumInterfaces() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1738 | CHECK(IsIdxLoaded() || IsErroneous()); // used during loading |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1739 | ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>( |
| 1740 | OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false); |
| 1741 | return (interfaces != NULL) ? interfaces->GetLength() : 0; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1744 | IntArray* GetInterfacesTypeIdx() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1745 | CHECK(IsIdxLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1746 | return GetFieldObject<IntArray*>( |
| 1747 | OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false); |
| 1748 | } |
| 1749 | |
| 1750 | void SetInterfacesTypeIdx(IntArray* new_interfaces_idx); |
| 1751 | |
| 1752 | ObjectArray<Class>* GetInterfaces() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1753 | CHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1754 | return GetFieldObject<ObjectArray<Class>*>( |
| 1755 | OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false); |
| 1756 | } |
| 1757 | |
| 1758 | void SetInterfaces(ObjectArray<Class>* new_interfaces) { |
| 1759 | DCHECK(NULL == GetFieldObject<Object*>( |
| 1760 | OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false)); |
| 1761 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), |
| 1762 | new_interfaces, false); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1766 | DCHECK_LT(i, NumInterfaces()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1767 | ObjectArray<Class>* interfaces = |
| 1768 | GetFieldObject<ObjectArray<Class>*>( |
| 1769 | OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false); |
| 1770 | interfaces->Set(i, f); |
| 1771 | } |
| 1772 | |
| 1773 | Class* GetInterface(uint32_t i) const { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1774 | DCHECK_LT(i, NumInterfaces()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1775 | return GetInterfaces()->Get(i); |
| 1776 | } |
| 1777 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1778 | int32_t GetIfTableCount() const { |
| 1779 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 1780 | if (iftable == NULL) { |
| 1781 | return 0; |
| 1782 | } |
| 1783 | return iftable->GetLength(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1786 | ObjectArray<InterfaceEntry>* GetIfTable() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1787 | DCHECK(IsResolved() || IsErroneous()); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1788 | return GetFieldObject<ObjectArray<InterfaceEntry>*>( |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1789 | OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false); |
| 1790 | } |
| 1791 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1792 | void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) { |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 1793 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | // Get instance fields |
| 1797 | ObjectArray<Field>* GetIFields() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1798 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1799 | return GetFieldObject<ObjectArray<Field>*>( |
| 1800 | OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false); |
| 1801 | } |
| 1802 | |
| 1803 | void SetIFields(ObjectArray<Field>* new_ifields) { |
| 1804 | DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>( |
| 1805 | OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false)); |
| 1806 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), |
| 1807 | new_ifields, false); |
| 1808 | } |
| 1809 | |
| 1810 | size_t NumInstanceFields() const { |
| 1811 | return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0; |
| 1812 | } |
| 1813 | |
| 1814 | Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t |
| 1815 | DCHECK_NE(NumInstanceFields(), 0U); |
| 1816 | return GetIFields()->Get(i); |
| 1817 | } |
| 1818 | |
| 1819 | void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t |
| 1820 | ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>( |
| 1821 | OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false); |
| 1822 | ifields->Set(i, f); |
| 1823 | } |
| 1824 | |
| 1825 | // Returns the number of instance fields containing reference types. |
| 1826 | size_t NumReferenceInstanceFields() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1827 | DCHECK(IsResolved() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1828 | DCHECK(sizeof(size_t) == sizeof(int32_t)); |
| 1829 | return GetField32( |
| 1830 | OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false); |
| 1831 | } |
| 1832 | |
| 1833 | size_t NumReferenceInstanceFieldsDuringLinking() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1834 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1835 | DCHECK(sizeof(size_t) == sizeof(int32_t)); |
| 1836 | return GetField32( |
| 1837 | OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false); |
| 1838 | } |
| 1839 | |
| 1840 | void SetNumReferenceInstanceFields(size_t new_num) { |
| 1841 | DCHECK(sizeof(size_t) == sizeof(int32_t)); |
| 1842 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), |
| 1843 | new_num, false); |
| 1844 | } |
| 1845 | |
| 1846 | uint32_t GetReferenceInstanceOffsets() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1847 | DCHECK(IsResolved() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1848 | return GetField32( |
| 1849 | OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false); |
| 1850 | } |
| 1851 | |
| 1852 | void SetReferenceInstanceOffsets(uint32_t new_reference_offsets); |
| 1853 | |
| 1854 | // Beginning of static field data |
| 1855 | static MemberOffset FieldsOffset() { |
| 1856 | return OFFSET_OF_OBJECT_MEMBER(Class, fields_); |
| 1857 | } |
| 1858 | |
| 1859 | // Returns the number of static fields containing reference types. |
| 1860 | size_t NumReferenceStaticFields() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1861 | DCHECK(IsResolved() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1862 | DCHECK(sizeof(size_t) == sizeof(int32_t)); |
| 1863 | return GetField32( |
| 1864 | OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false); |
| 1865 | } |
| 1866 | |
| 1867 | size_t NumReferenceStaticFieldsDuringLinking() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1868 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1869 | DCHECK(sizeof(size_t) == sizeof(int32_t)); |
| 1870 | return GetField32( |
| 1871 | OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false); |
| 1872 | } |
| 1873 | |
| 1874 | void SetNumReferenceStaticFields(size_t new_num) { |
| 1875 | DCHECK(sizeof(size_t) == sizeof(int32_t)); |
| 1876 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), |
| 1877 | new_num, false); |
| 1878 | } |
| 1879 | |
| 1880 | ObjectArray<Field>* GetSFields() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1881 | DCHECK(IsLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1882 | return GetFieldObject<ObjectArray<Field>*>( |
| 1883 | OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false); |
| 1884 | } |
| 1885 | |
| 1886 | void SetSFields(ObjectArray<Field>* new_sfields) { |
| 1887 | DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>( |
| 1888 | OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false)); |
| 1889 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), |
| 1890 | new_sfields, false); |
| 1891 | } |
| 1892 | |
| 1893 | size_t NumStaticFields() const { |
| 1894 | return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0; |
| 1895 | } |
| 1896 | |
| 1897 | Field* GetStaticField(uint32_t i) const { // TODO: uint16_t |
| 1898 | return GetSFields()->Get(i); |
| 1899 | } |
| 1900 | |
| 1901 | void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t |
| 1902 | ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>( |
| 1903 | OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false); |
| 1904 | sfields->Set(i, f); |
| 1905 | } |
| 1906 | |
| 1907 | uint32_t GetReferenceStaticOffsets() const { |
| 1908 | return GetField32( |
| 1909 | OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false); |
| 1910 | } |
| 1911 | |
| 1912 | void SetReferenceStaticOffsets(uint32_t new_reference_offsets); |
| 1913 | |
| 1914 | // Finds the given instance field in this class or a superclass. |
| 1915 | Field* FindInstanceField(const StringPiece& name, Class* type); |
| 1916 | |
| 1917 | Field* FindDeclaredInstanceField(const StringPiece& name, Class* type); |
| 1918 | |
| 1919 | // Finds the given static field in this class or a superclass. |
| 1920 | Field* FindStaticField(const StringPiece& name, Class* type); |
| 1921 | |
| 1922 | Field* FindDeclaredStaticField(const StringPiece& name, Class* type); |
| 1923 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1924 | pid_t GetClinitThreadId() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1925 | DCHECK(IsIdxLoaded() || IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1926 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false); |
| 1927 | } |
| 1928 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1929 | void SetClinitThreadId(pid_t new_clinit_thread_id) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1930 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), |
| 1931 | new_clinit_thread_id, false); |
| 1932 | } |
| 1933 | |
| 1934 | Class* GetVerifyErrorClass() const { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1935 | // DCHECK(IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1936 | return GetFieldObject<Class*>( |
| 1937 | OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1940 | void SetVerifyErrorClass(Class* klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1941 | klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), |
| 1942 | klass, false); |
| 1943 | } |
| 1944 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 1945 | String* GetSourceFile() const; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1946 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 1947 | void SetSourceFile(String* new_source_file); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1948 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1949 | private: |
jeffhao | 5dbddee | 2011-09-07 16:38:26 -0700 | [diff] [blame] | 1950 | bool Implements(const Class* klass) const; |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1951 | bool IsArrayAssignableFromArray(const Class* klass) const; |
| 1952 | bool IsAssignableFromArray(const Class* klass) const; |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1953 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1954 | // descriptor for the class such as "java.lang.Class" or "[C" |
| 1955 | String* name_; // TODO initialize |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1956 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1957 | // defining class loader, or NULL for the "bootstrap" system loader |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1958 | const ClassLoader* class_loader_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1959 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1960 | // For array classes, the component class object for instanceof/checkcast |
| 1961 | // (for String[][][], this will be String[][]). NULL for non-array classes. |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1962 | Class* component_type_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1963 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1964 | // descriptor for the class such as "Ljava/lang/Class;" or "[C" |
| 1965 | String* descriptor_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1966 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1967 | // DexCache of resolved constant pool entries |
| 1968 | // (will be NULL for VM-generated, e.g. arrays and primitive classes) |
| 1969 | DexCache* dex_cache_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1970 | |
| 1971 | // static, private, and <init> methods |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1972 | ObjectArray<Method>* direct_methods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1973 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1974 | // instance fields |
| 1975 | // |
| 1976 | // These describe the layout of the contents of an Object. |
| 1977 | // Note that only the fields directly declared by this class are |
| 1978 | // listed in ifields; fields declared by a superclass are listed in |
| 1979 | // the superclass's Class.ifields. |
| 1980 | // |
| 1981 | // All instance fields that refer to objects are guaranteed to be at |
| 1982 | // the beginning of the field list. num_reference_instance_fields_ |
| 1983 | // specifies the number of reference fields. |
| 1984 | ObjectArray<Field>* ifields_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1985 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1986 | // Interface table (iftable_), one entry per interface supported by |
| 1987 | // this class. That means one entry for each interface we support |
| 1988 | // directly, indirectly via superclass, or indirectly via |
| 1989 | // superinterface. This will be null if neither we nor our |
| 1990 | // superclass implement any interfaces. |
| 1991 | // |
| 1992 | // Why we need this: given "class Foo implements Face", declare |
| 1993 | // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah" |
| 1994 | // is part of the Face interface. We can't easily use a single |
| 1995 | // vtable. |
| 1996 | // |
| 1997 | // For every interface a concrete class implements, we create an array |
| 1998 | // of the concrete vtable_ methods for the methods in the interface. |
| 1999 | ObjectArray<InterfaceEntry>* iftable_; |
| 2000 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2001 | // array of interfaces this class implements directly |
| 2002 | // see also interfaces_type_idx_ |
| 2003 | ObjectArray<Class>* interfaces_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2004 | |
| 2005 | // array of type_idx's for interfaces this class implements directly |
| 2006 | // see also interfaces_ |
| 2007 | IntArray* interfaces_type_idx_; |
| 2008 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2009 | // Static fields |
| 2010 | ObjectArray<Field>* sfields_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2011 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2012 | // source file name, if known. Otherwise, NULL. |
| 2013 | String* source_file_; |
| 2014 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2015 | // The superclass, or NULL if this is java.lang.Object or a |
| 2016 | // primitive type. |
| 2017 | // see also super_class_type_idx_; |
| 2018 | Class* super_class_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2019 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2020 | // If class verify fails, we must return same error on subsequent tries. |
| 2021 | // Update with SetVerifyErrorClass to ensure a write barrier is used. |
| 2022 | const Class* verify_error_class_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2023 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2024 | // virtual methods defined in this class; invoked through vtable |
| 2025 | ObjectArray<Method>* virtual_methods_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2026 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2027 | // Virtual method table (vtable), for use by "invoke-virtual". The |
| 2028 | // vtable from the superclass is copied in, and virtual methods from |
| 2029 | // our class either replace those from the super or are appended. |
| 2030 | ObjectArray<Method>* vtable_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2031 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2032 | // access flags; low 16 bits are defined by VM spec |
| 2033 | uint32_t access_flags_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2034 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 2035 | // Total size of the Class instance; used when allocating storage on gc heap. |
| 2036 | // See also object_size_. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2037 | size_t class_size_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2038 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 2039 | // tid used to check for recursive <clinit> invocation |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2040 | pid_t clinit_thread_id_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 2041 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2042 | // number of instance fields that are object refs |
| 2043 | size_t num_reference_instance_fields_; |
| 2044 | |
| 2045 | // number of static fields that are object refs |
| 2046 | size_t num_reference_static_fields_; |
| 2047 | |
| 2048 | // Total object size; used when allocating storage on gc heap. |
| 2049 | // (For interfaces and abstract classes this will be zero.) |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 2050 | // See also class_size_. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2051 | size_t object_size_; |
| 2052 | |
| 2053 | // primitive type index, or kPrimNot (0); set for generated prim classes |
| 2054 | PrimitiveType primitive_type_; |
| 2055 | |
| 2056 | // Bitmap of offsets of ifields. |
| 2057 | uint32_t reference_instance_offsets_; |
| 2058 | |
| 2059 | // Bitmap of offsets of sfields. |
| 2060 | uint32_t reference_static_offsets_; |
| 2061 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2062 | // state of class initialization |
| 2063 | Status status_; |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 2064 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2065 | // Set in LoadClass, used to LinkClass |
| 2066 | // see also super_class_ |
| 2067 | uint32_t super_class_type_idx_; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2068 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2069 | // TODO: ? |
| 2070 | // initiating class loader list |
| 2071 | // NOTE: for classes with low serialNumber, these are unused, and the |
| 2072 | // values are kept in a table in gDvm. |
| 2073 | // InitiatingLoaderList initiating_loader_list_; |
| 2074 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2075 | // Location of first static field. |
| 2076 | uint32_t fields_[0]; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 2077 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2078 | friend struct ClassOffsets; // for verifying offset information |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 2079 | DISALLOW_IMPLICIT_CONSTRUCTORS(Class); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 2080 | }; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2081 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 2082 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 2083 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2084 | inline void Object::SetClass(Class* new_klass) { |
| 2085 | // new_klass may be NULL prior to class linker initialization |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 2086 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 2089 | inline bool Object::InstanceOf(const Class* klass) const { |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 2090 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2091 | DCHECK(GetClass() != NULL); |
| 2092 | return klass->IsAssignableFrom(GetClass()); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 2093 | } |
| 2094 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 2095 | inline bool Object::IsClass() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2096 | Class* java_lang_Class = GetClass()->GetClass(); |
| 2097 | return GetClass() == java_lang_Class; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2100 | inline bool Object::IsClassClass() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2101 | Class* java_lang_Class = GetClass()->GetClass(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2102 | return this == java_lang_Class; |
| 2103 | } |
| 2104 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 2105 | inline bool Object::IsObjectArray() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2106 | return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 2109 | inline bool Object::IsArrayInstance() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2110 | return GetClass()->IsArrayClass(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 2111 | } |
| 2112 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2113 | inline bool Object::IsField() const { |
| 2114 | Class* java_lang_Class = klass_->klass_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2115 | Class* java_lang_reflect_Field = |
| 2116 | java_lang_Class->GetInstanceField(0)->GetClass(); |
| 2117 | return GetClass() == java_lang_reflect_Field; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | inline bool Object::IsMethod() const { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 2121 | Class* c = GetClass(); |
| 2122 | return c == Method::GetMethodClass() || c == Method::GetConstructorClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | inline bool Object::IsReferenceInstance() const { |
| 2126 | return GetClass()->IsReferenceClass(); |
| 2127 | } |
| 2128 | |
| 2129 | inline bool Object::IsWeakReferenceInstance() const { |
| 2130 | return GetClass()->IsWeakReferenceClass(); |
| 2131 | } |
| 2132 | |
| 2133 | inline bool Object::IsSoftReferenceInstance() const { |
| 2134 | return GetClass()->IsSoftReferenceClass(); |
| 2135 | } |
| 2136 | |
| 2137 | inline bool Object::IsFinalizerReferenceInstance() const { |
| 2138 | return GetClass()->IsFinalizerReferenceClass(); |
| 2139 | } |
| 2140 | |
| 2141 | inline bool Object::IsPhantomReferenceInstance() const { |
| 2142 | return GetClass()->IsPhantomReferenceClass(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 2145 | inline size_t Object::SizeOf() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2146 | size_t result; |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 2147 | if (IsArrayInstance()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2148 | result = AsArray()->SizeOf(); |
| 2149 | } else if (IsClass()) { |
| 2150 | result = AsClass()->SizeOf(); |
| 2151 | } else { |
| 2152 | result = GetClass()->GetObjectSize(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 2153 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2154 | DCHECK(!IsField() || result == sizeof(Field)); |
| 2155 | DCHECK(!IsMethod() || result == sizeof(Method)); |
| 2156 | return result; |
| 2157 | } |
| 2158 | |
| 2159 | inline void Field::SetOffset(MemberOffset num_bytes) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2160 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2161 | Class* type = GetTypeDuringLinking(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2162 | if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) { |
| 2163 | DCHECK(IsAligned(num_bytes.Uint32Value(), 8)); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2164 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2165 | SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), |
| 2166 | num_bytes.Uint32Value(), false); |
| 2167 | } |
| 2168 | |
| 2169 | inline Class* Field::GetDeclaringClass() const { |
| 2170 | Class* result = GetFieldObject<Class*>( |
| 2171 | OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false); |
| 2172 | DCHECK(result != NULL); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2173 | DCHECK(result->IsLoaded() || result->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2174 | return result; |
| 2175 | } |
| 2176 | |
| 2177 | inline void Field::SetDeclaringClass(Class *new_declaring_class) { |
| 2178 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), |
| 2179 | new_declaring_class, false); |
| 2180 | } |
| 2181 | |
| 2182 | inline Class* Method::GetDeclaringClass() const { |
| 2183 | Class* result = |
| 2184 | GetFieldObject<Class*>( |
| 2185 | OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false); |
| 2186 | DCHECK(result != NULL); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 2187 | DCHECK(result->IsIdxLoaded() || result->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2188 | return result; |
| 2189 | } |
| 2190 | |
| 2191 | inline void Method::SetDeclaringClass(Class *new_declaring_class) { |
| 2192 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), |
| 2193 | new_declaring_class, false); |
| 2194 | } |
| 2195 | |
| 2196 | inline uint32_t Method::GetReturnTypeIdx() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2197 | DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2198 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_), |
| 2199 | false); |
| 2200 | } |
| 2201 | |
| 2202 | inline bool Method::IsReturnAReference() const { |
| 2203 | return !GetReturnType()->IsPrimitive(); |
| 2204 | } |
| 2205 | |
| 2206 | inline bool Method::IsReturnAFloat() const { |
| 2207 | return GetReturnType()->IsPrimitiveFloat(); |
| 2208 | } |
| 2209 | |
| 2210 | inline bool Method::IsReturnADouble() const { |
| 2211 | return GetReturnType()->IsPrimitiveDouble(); |
| 2212 | } |
| 2213 | |
| 2214 | inline bool Method::IsReturnALong() const { |
| 2215 | return GetReturnType()->IsPrimitiveLong(); |
| 2216 | } |
| 2217 | |
| 2218 | inline bool Method::IsReturnVoid() const { |
| 2219 | return GetReturnType()->IsPrimitiveVoid(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 2222 | inline size_t Array::SizeOf() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2223 | return SizeOf(GetLength(), GetClass()->GetComponentSize()); |
| 2224 | } |
| 2225 | |
| 2226 | template<class T> |
| 2227 | void ObjectArray<T>::Set(int32_t i, T* object) { |
| 2228 | if (IsValidIndex(i)) { |
| 2229 | if (object != NULL) { |
| 2230 | Class* element_class = GetClass()->GetComponentType(); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 2231 | if (!object->InstanceOf(element_class)) { |
| 2232 | ThrowArrayStoreException(object); |
| 2233 | return; |
| 2234 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2235 | } |
| 2236 | MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*)); |
| 2237 | SetFieldObject(data_offset, object, false); |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | template<class T> |
| 2242 | void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) { |
| 2243 | DCHECK(IsValidIndex(i)); |
| 2244 | MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*)); |
| 2245 | SetFieldObject(data_offset, object, false); |
| 2246 | } |
| 2247 | |
| 2248 | template<class T> |
| 2249 | void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos, |
| 2250 | ObjectArray<T>* dst, int dst_pos, |
| 2251 | size_t length) { |
| 2252 | if (src->IsValidIndex(src_pos) && |
| 2253 | src->IsValidIndex(src_pos+length-1) && |
| 2254 | dst->IsValidIndex(dst_pos) && |
| 2255 | dst->IsValidIndex(dst_pos+length-1)) { |
| 2256 | MemberOffset src_offset(DataOffset().Int32Value() + |
| 2257 | src_pos * sizeof(Object*)); |
| 2258 | MemberOffset dst_offset(DataOffset().Int32Value() + |
| 2259 | dst_pos * sizeof(Object*)); |
| 2260 | Class* array_class = dst->GetClass(); |
| 2261 | if (array_class == src->GetClass()) { |
| 2262 | // No need for array store checks if arrays are of the same type |
| 2263 | for (size_t i=0; i < length; i++) { |
| 2264 | Object* object = src->GetFieldObject<Object*>(src_offset, false); |
| 2265 | dst->SetFieldObject(dst_offset, object, false); |
| 2266 | src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*)); |
| 2267 | dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*)); |
| 2268 | } |
| 2269 | } else { |
| 2270 | Class* element_class = array_class->GetComponentType(); |
| 2271 | CHECK(!element_class->IsPrimitive()); |
| 2272 | for (size_t i=0; i < length; i++) { |
| 2273 | Object* object = src->GetFieldObject<Object*>(src_offset, false); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 2274 | if (object != NULL && !object->InstanceOf(element_class)) { |
| 2275 | dst->ThrowArrayStoreException(object); |
| 2276 | return; |
| 2277 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2278 | dst->SetFieldObject(dst_offset, object, false); |
| 2279 | src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*)); |
| 2280 | dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*)); |
| 2281 | } |
| 2282 | } |
Elliott Hughes | 3a4f8df | 2011-09-13 15:22:36 -0700 | [diff] [blame] | 2283 | Heap::WriteBarrier(dst); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2284 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 2285 | } |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 2286 | |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 2287 | class MANAGED ClassClass : public Class { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2288 | private: |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2289 | int32_t padding_; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2290 | int64_t serialVersionUID_; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2291 | friend struct ClassClassOffsets; // for verifying offset information |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2292 | DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass); |
| 2293 | }; |
| 2294 | |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 2295 | class MANAGED StringClass : public Class { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2296 | private: |
| 2297 | CharArray* ASCII_; |
| 2298 | Object* CASE_INSENSITIVE_ORDER_; |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2299 | uint32_t REPLACEMENT_CHAR_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2300 | int64_t serialVersionUID_; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2301 | friend struct StringClassOffsets; // for verifying offset information |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2302 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass); |
| 2303 | }; |
| 2304 | |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 2305 | class MANAGED FieldClass : public Class { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2306 | private: |
| 2307 | Object* ORDER_BY_NAME_AND_DECLARING_CLASS_; |
| 2308 | uint32_t TYPE_BOOLEAN_; |
| 2309 | uint32_t TYPE_BYTE_; |
| 2310 | uint32_t TYPE_CHAR_; |
| 2311 | uint32_t TYPE_DOUBLE_; |
| 2312 | uint32_t TYPE_FLOAT_; |
| 2313 | uint32_t TYPE_INTEGER_; |
| 2314 | uint32_t TYPE_LONG_; |
| 2315 | uint32_t TYPE_SHORT_; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2316 | friend struct FieldClassOffsets; // for verifying offset information |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2317 | DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass); |
| 2318 | }; |
| 2319 | |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 2320 | class MANAGED MethodClass : public Class { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2321 | private: |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2322 | Object* ORDER_BY_SIGNATURE_; |
| 2323 | friend struct MethodClassOffsets; // for verifying offset information |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2324 | DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass); |
| 2325 | }; |
| 2326 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 2327 | template<class T> |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 2328 | class MANAGED PrimitiveArray : public Array { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 2329 | public: |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 2330 | typedef T ElementType; |
| 2331 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 2332 | static PrimitiveArray<T>* Alloc(size_t length); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 2333 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 2334 | const T* GetData() const { |
| 2335 | return reinterpret_cast<const T*>(&elements_); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 2336 | } |
| 2337 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 2338 | T* GetData() { |
| 2339 | return reinterpret_cast<T*>(&elements_); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 2340 | } |
| 2341 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 2342 | T Get(int32_t i) const { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 2343 | if (!IsValidIndex(i)) { |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 2344 | return T(0); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 2345 | } |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 2346 | return GetData()[i]; |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 2349 | void Set(int32_t i, T value) { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 2350 | // TODO: ArrayStoreException |
| 2351 | if (IsValidIndex(i)) { |
| 2352 | GetData()[i] = value; |
| 2353 | } |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 2356 | static void SetArrayClass(Class* array_class) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2357 | CHECK(array_class_ == NULL); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 2358 | CHECK(array_class != NULL); |
| 2359 | array_class_ = array_class; |
| 2360 | } |
| 2361 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2362 | static void ResetArrayClass() { |
| 2363 | CHECK(array_class_ != NULL); |
| 2364 | array_class_ = NULL; |
| 2365 | } |
| 2366 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 2367 | private: |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 2368 | // Location of first element. |
| 2369 | T elements_[0]; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 2370 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 2371 | static Class* array_class_; |
| 2372 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 2373 | DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 2374 | }; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 2375 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2376 | inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) { |
| 2377 | DCHECK(NULL == GetFieldObject<IntArray*>( |
| 2378 | OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false)); |
| 2379 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), |
| 2380 | new_interfaces_idx, false); |
| 2381 | } |
| 2382 | |
| 2383 | // C++ mirror of java.lang.String |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 2384 | class MANAGED String : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 2385 | public: |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2386 | const CharArray* GetCharArray() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2387 | const CharArray* result = GetFieldObject<const CharArray*>( |
| 2388 | OFFSET_OF_OBJECT_MEMBER(String, array_), false); |
| 2389 | DCHECK(result != NULL); |
| 2390 | return result; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2391 | } |
| 2392 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2393 | int32_t GetOffset() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2394 | int32_t result = GetField32( |
| 2395 | OFFSET_OF_OBJECT_MEMBER(String, offset_), false); |
| 2396 | DCHECK_LE(0, result); |
| 2397 | return result; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2400 | int32_t GetLength() const; |
| 2401 | |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 2402 | int32_t GetHashCode(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2403 | |
| 2404 | void ComputeHashCode() { |
| 2405 | SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength())); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2406 | } |
| 2407 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2408 | int32_t GetUtfLength() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2409 | return CountUtf8Bytes(GetCharArray()->GetData(), GetLength()); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 2410 | } |
| 2411 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2412 | uint16_t CharAt(int32_t index) const; |
| 2413 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 2414 | String* Intern(); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2415 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 2416 | static String* AllocFromUtf16(int32_t utf16_length, |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2417 | const uint16_t* utf16_data_in, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2418 | int32_t hash_code = 0); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 2419 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2420 | static String* AllocFromModifiedUtf8(const char* utf); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 2421 | |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 2422 | static String* AllocFromModifiedUtf8(int32_t utf16_length, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2423 | const char* utf8_data_in); |
| 2424 | |
| 2425 | static String* Alloc(Class* java_lang_String, int32_t utf16_length); |
| 2426 | |
| 2427 | static String* Alloc(Class* java_lang_String, CharArray* array); |
| 2428 | |
| 2429 | bool Equals(const char* modified_utf8) const; |
| 2430 | |
| 2431 | // TODO: do we need this overload? give it a more intention-revealing name. |
| 2432 | bool Equals(const StringPiece& modified_utf8) const; |
| 2433 | |
| 2434 | bool Equals(const String* that) const; |
| 2435 | |
| 2436 | // TODO: do we need this overload? give it a more intention-revealing name. |
| 2437 | bool Equals(const uint16_t* that_chars, int32_t that_offset, |
| 2438 | int32_t that_length) const; |
| 2439 | |
| 2440 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 2441 | std::string ToModifiedUtf8() const; |
| 2442 | |
| 2443 | static Class* GetJavaLangString() { |
| 2444 | DCHECK(java_lang_String_ != NULL); |
| 2445 | return java_lang_String_; |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 2446 | } |
| 2447 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2448 | static void SetClass(Class* java_lang_String); |
| 2449 | static void ResetClass(); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 2450 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 2451 | private: |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2452 | void SetHashCode(int32_t new_hash_code) { |
| 2453 | DCHECK_EQ(0u, |
| 2454 | GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false)); |
| 2455 | SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), |
| 2456 | new_hash_code, false); |
| 2457 | } |
| 2458 | |
| 2459 | void SetCount(int32_t new_count) { |
| 2460 | DCHECK_LE(0, new_count); |
| 2461 | SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false); |
| 2462 | } |
| 2463 | |
| 2464 | void SetOffset(int32_t new_offset) { |
| 2465 | DCHECK_LE(0, new_offset); |
| 2466 | DCHECK_GE(GetLength(), new_offset); |
| 2467 | SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false); |
| 2468 | } |
| 2469 | |
| 2470 | void SetArray(CharArray* new_array) { |
| 2471 | DCHECK(new_array != NULL); |
| 2472 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false); |
| 2473 | } |
| 2474 | |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2475 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 2476 | CharArray* array_; |
| 2477 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2478 | int32_t count_; |
| 2479 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 2480 | uint32_t hash_code_; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2481 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 2482 | int32_t offset_; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2483 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 2484 | static Class* java_lang_String_; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2485 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2486 | friend struct StringOffsets; // for verifying offset information |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2487 | DISALLOW_IMPLICIT_CONSTRUCTORS(String); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 2488 | }; |
| 2489 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2490 | inline const String* Field::GetName() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2491 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2492 | String* result = |
| 2493 | GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false); |
| 2494 | DCHECK(result != NULL); |
| 2495 | return result; |
| 2496 | } |
| 2497 | |
| 2498 | inline void Field::SetName(String* new_name) { |
| 2499 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_), |
| 2500 | new_name, false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | inline uint32_t Field::GetAccessFlags() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2504 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2505 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false); |
| 2506 | } |
| 2507 | |
| 2508 | inline uint32_t Field::GetTypeIdx() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2509 | DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2510 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false); |
| 2511 | } |
| 2512 | |
| 2513 | inline MemberOffset Field::GetOffset() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2514 | DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2515 | return MemberOffset( |
| 2516 | GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false)); |
| 2517 | } |
| 2518 | |
| 2519 | inline MemberOffset Field::GetOffsetDuringLinking() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2520 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2521 | return MemberOffset( |
| 2522 | GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false)); |
| 2523 | } |
| 2524 | |
| 2525 | inline const String* Method::GetName() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2526 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2527 | const String* result = |
| 2528 | GetFieldObject<const String*>( |
| 2529 | OFFSET_OF_OBJECT_MEMBER(Method, name_), false); |
| 2530 | DCHECK(result != NULL); |
| 2531 | return result; |
| 2532 | } |
| 2533 | |
| 2534 | inline void Method::SetName(String* new_name) { |
| 2535 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_), |
| 2536 | new_name, false); |
| 2537 | |
| 2538 | } |
| 2539 | |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 2540 | inline ByteArray* Method::GetRegisterMapData() const { |
| 2541 | return GetFieldObject<ByteArray*>( |
| 2542 | OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false); |
| 2543 | } |
| 2544 | |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 2545 | inline void Method::SetRegisterMapData(ByteArray* data) { |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 2546 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 2547 | data, false); |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | inline ByteArray* Method::GetRegisterMapHeader() const { |
| 2551 | return GetFieldObject<ByteArray*>( |
| 2552 | OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false); |
| 2553 | } |
| 2554 | |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 2555 | inline void Method::SetRegisterMapHeader(ByteArray* header) { |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 2556 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 2557 | header, false); |
jeffhao | e23d93c | 2011-09-15 14:48:43 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 2560 | inline String* Method::GetShorty() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2561 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 2562 | return GetFieldObject<String*>( |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2563 | OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false); |
| 2564 | } |
| 2565 | |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 2566 | inline void Method::SetShorty(String* new_shorty) { |
| 2567 | DCHECK(NULL == GetFieldObject<String*>( |
| 2568 | OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false)); |
| 2569 | DCHECK_LE(1, new_shorty->GetLength()); |
| 2570 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false); |
| 2571 | } |
| 2572 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2573 | inline const String* Method::GetSignature() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2574 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2575 | const String* result = |
| 2576 | GetFieldObject<const String*>( |
| 2577 | OFFSET_OF_OBJECT_MEMBER(Method, signature_), false); |
| 2578 | DCHECK(result != NULL); |
| 2579 | return result; |
| 2580 | } |
| 2581 | |
| 2582 | inline void Method::SetSignature(String* new_signature) { |
| 2583 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_), |
| 2584 | new_signature, false); |
| 2585 | } |
| 2586 | |
| 2587 | inline uint32_t Class::GetAccessFlags() const { |
| 2588 | // Check class is loaded or this is java.lang.String that has a |
| 2589 | // circularity issue during loading the names of its members |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2590 | DCHECK(IsLoaded() || IsErroneous() || |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 2591 | this == String::GetJavaLangString() || |
| 2592 | this == Field::GetJavaLangReflectField() || |
| 2593 | this == Method::GetConstructorClass() || |
| 2594 | this == Method::GetMethodClass()) << PrettyClass(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2595 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false); |
| 2596 | } |
| 2597 | |
| 2598 | inline void Class::SetDescriptor(String* new_descriptor) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2599 | DCHECK(GetDescriptor() == NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2600 | DCHECK(new_descriptor != NULL); |
| 2601 | DCHECK_NE(0, new_descriptor->GetLength()); |
| 2602 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), |
| 2603 | new_descriptor, false); |
| 2604 | } |
| 2605 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 2606 | inline String* Class::GetSourceFile() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2607 | DCHECK(IsLoaded() || IsErroneous()); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 2608 | return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false); |
| 2609 | } |
| 2610 | |
| 2611 | inline void Class::SetSourceFile(String* new_source_file) { |
| 2612 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false); |
| 2613 | } |
| 2614 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2615 | inline uint32_t Method::GetAccessFlags() const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 2616 | DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2617 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false); |
| 2618 | } |
| 2619 | |
| 2620 | inline uint16_t Method::GetMethodIndex() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2621 | DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous()); |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 2622 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | inline uint16_t Method::NumRegisters() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2626 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 2627 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | inline uint16_t Method::NumIns() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2631 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 2632 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | inline uint16_t Method::NumOuts() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2636 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Elliott Hughes | 1d3f114 | 2011-09-13 12:00:00 -0700 | [diff] [blame] | 2637 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | inline uint32_t Method::GetProtoIdx() const { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2641 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2642 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false); |
| 2643 | } |
| 2644 | |
| 2645 | // C++ mirror of java.lang.Throwable |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 2646 | class MANAGED Throwable : public Object { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 2647 | public: |
| 2648 | void SetDetailMessage(String* new_detail_message) { |
| 2649 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), |
| 2650 | new_detail_message, false); |
| 2651 | } |
| 2652 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 2653 | private: |
| 2654 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 2655 | Throwable* cause_; |
| 2656 | String* detail_message_; |
| 2657 | Object* stack_state_; // Note this is Java volatile: |
| 2658 | Object* stack_trace_; |
| 2659 | Object* suppressed_exceptions_; |
| 2660 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2661 | friend struct ThrowableOffsets; // for verifying offset information |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 2662 | DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable); |
| 2663 | }; |
| 2664 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2665 | // C++ mirror of java.lang.StackTraceElement |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 2666 | class MANAGED StackTraceElement : public Object { |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2667 | public: |
| 2668 | const String* GetDeclaringClass() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2669 | return GetFieldObject<const String*>( |
| 2670 | OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | const String* GetMethodName() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2674 | return GetFieldObject<const String*>( |
| 2675 | OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | const String* GetFileName() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2679 | return GetFieldObject<const String*>( |
| 2680 | OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2681 | } |
| 2682 | |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 2683 | int32_t GetLineNumber() const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2684 | return GetField32( |
| 2685 | OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2688 | static StackTraceElement* Alloc(const String* declaring_class, |
| 2689 | const String* method_name, |
| 2690 | const String* file_name, |
| 2691 | int32_t line_number); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2692 | |
| 2693 | static void SetClass(Class* java_lang_StackTraceElement); |
| 2694 | |
| 2695 | static void ResetClass(); |
| 2696 | |
| 2697 | private: |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2698 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2699 | const String* declaring_class_; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2700 | const String* file_name_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2701 | const String* method_name_; |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 2702 | int32_t line_number_; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2703 | |
| 2704 | static Class* GetStackTraceElement() { |
| 2705 | DCHECK(java_lang_StackTraceElement_ != NULL); |
| 2706 | return java_lang_StackTraceElement_; |
| 2707 | } |
| 2708 | |
| 2709 | static Class* java_lang_StackTraceElement_; |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2710 | |
| 2711 | friend struct StackTraceElementOffsets; // for verifying offset information |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2712 | DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement); |
| 2713 | }; |
| 2714 | |
Brian Carlstrom | e4f7e1d | 2011-09-11 13:21:12 -0700 | [diff] [blame] | 2715 | class MANAGED InterfaceEntry : public ObjectArray<Object> { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 2716 | public: |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 2717 | Class* GetInterface() const { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2718 | Class* interface = Get(kInterface)->AsClass(); |
| 2719 | DCHECK(interface != NULL); |
| 2720 | return interface; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 2721 | } |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 2722 | |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 2723 | void SetInterface(Class* interface) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2724 | DCHECK(interface != NULL); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 2725 | DCHECK(interface->IsInterface()); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2726 | DCHECK(Get(kInterface) == NULL); |
| 2727 | Set(kInterface, interface); |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 2728 | } |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 2729 | |
Brian Carlstrom | 8692721 | 2011-09-15 11:31:11 -0700 | [diff] [blame] | 2730 | size_t GetMethodArrayCount() const { |
| 2731 | ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray)); |
| 2732 | if (method_array == 0) { |
| 2733 | return 0; |
| 2734 | } |
| 2735 | return method_array->GetLength(); |
| 2736 | } |
| 2737 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2738 | ObjectArray<Method>* GetMethodArray() const { |
| 2739 | ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray)); |
| 2740 | DCHECK(method_array != NULL); |
| 2741 | return method_array; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2742 | } |
| 2743 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2744 | void SetMethodArray(ObjectArray<Method>* new_ma) { |
| 2745 | DCHECK(new_ma != NULL); |
| 2746 | DCHECK(Get(kMethodArray) == NULL); |
| 2747 | Set(kMethodArray, new_ma); |
| 2748 | } |
| 2749 | |
| 2750 | static size_t LengthAsArray() { |
| 2751 | return kMax; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2752 | } |
| 2753 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2754 | private: |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 2755 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2756 | enum ArrayIndex { |
| 2757 | // Points to the interface class. |
| 2758 | kInterface = 0, |
| 2759 | // Method pointers into the vtable, allow fast map from interface |
| 2760 | // method index to concrete instance method. |
| 2761 | kMethodArray = 1, |
| 2762 | kMax = 2, |
| 2763 | }; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 2764 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2765 | DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 2766 | }; |
| 2767 | |
| 2768 | } // namespace art |
| 2769 | |
| 2770 | #endif // ART_SRC_OBJECT_H_ |