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