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