blob: 03ed132c26c968e5adf280493ac7b39fe71426c2 [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
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 Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
26#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070027#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
29#include "macros.h"
30#include "offsets.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070031#include "primitive.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070032#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070035#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
37namespace art {
38
39class Array;
40class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070041class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070042class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040044class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045class InterfaceEntry;
46class Monitor;
47class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070049class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040050class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070051template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070052template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070053typedef PrimitiveArray<uint8_t> BooleanArray;
54typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070055typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070056typedef PrimitiveArray<double> DoubleArray;
57typedef PrimitiveArray<float> FloatArray;
58typedef PrimitiveArray<int32_t> IntArray;
59typedef PrimitiveArray<int64_t> LongArray;
60typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070061
Carl Shapiro3ee755d2011-06-28 12:11:04 -070062union JValue {
Elliott Hughes1d878f32012-04-11 15:17:54 -070063 // We default initialize JValue instances to all-zeros.
64 JValue() : j(0) {}
65
Elliott Hughesf24d3ce2012-04-11 17:43:37 -070066 int8_t GetB() const { return b; }
67 void SetB(int8_t new_b) {
68 i = ((static_cast<int32_t>(new_b) << 24) >> 24); // Sign-extend.
69 }
70
71 uint16_t GetC() const { return c; }
72 void SetC(uint16_t new_c) { c = new_c; }
73
74 double GetD() const { return d; }
75 void SetD(double new_d) { d = new_d; }
76
77 float GetF() const { return f; }
78 void SetF(float new_f) { f = new_f; }
79
80 int32_t GetI() const { return i; }
81 void SetI(int32_t new_i) { i = new_i; }
82
83 int64_t GetJ() const { return j; }
84 void SetJ(int64_t new_j) { j = new_j; }
85
86 Object* GetL() const { return l; }
87 void SetL(Object* new_l) { l = new_l; }
88
89 int16_t GetS() const { return s; }
90 void SetS(int16_t new_s) {
91 i = ((static_cast<int32_t>(new_s) << 16) >> 16); // Sign-extend.
92 }
93
94 uint8_t GetZ() const { return z; }
95 void SetZ(uint8_t new_z) { z = new_z; }
96
97 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -070098 uint8_t z;
99 int8_t b;
100 uint16_t c;
101 int16_t s;
102 int32_t i;
103 int64_t j;
104 float f;
105 double d;
106 Object* l;
107};
108
Logan Chienfca7e872011-12-20 20:08:22 +0800109#if defined(ART_USE_LLVM_COMPILER)
110namespace compiler_llvm {
111 class InferredRegCategoryMap;
Elliott Hughes48257562012-06-06 17:42:44 -0700112} // namespace compiler_llvm
Logan Chienfca7e872011-12-20 20:08:22 +0800113#endif
114
Brian Carlstrombe977852011-07-19 14:54:54 -0700115static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
116static const uint32_t kAccPrivate = 0x0002; // field, method, ic
117static const uint32_t kAccProtected = 0x0004; // field, method, ic
118static const uint32_t kAccStatic = 0x0008; // field, method, ic
119static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
120static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
Elliott Hughes582a7d12011-10-10 18:38:42 -0700121static const uint32_t kAccSuper = 0x0020; // class (not used in dex)
Brian Carlstrombe977852011-07-19 14:54:54 -0700122static const uint32_t kAccVolatile = 0x0040; // field
123static const uint32_t kAccBridge = 0x0040; // method (1.5)
124static const uint32_t kAccTransient = 0x0080; // field
125static const uint32_t kAccVarargs = 0x0080; // method (1.5)
126static const uint32_t kAccNative = 0x0100; // method
127static const uint32_t kAccInterface = 0x0200; // class, ic
128static const uint32_t kAccAbstract = 0x0400; // class, method, ic
129static const uint32_t kAccStrict = 0x0800; // method
130static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
131static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
132static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700133
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134static const uint32_t kAccMiranda = 0x8000; // method
135
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700136static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
137
Elliott Hughes582a7d12011-10-10 18:38:42 -0700138static const uint32_t kAccConstructor = 0x00010000; // method (dex only)
139static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (dex only)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140static const uint32_t kAccClassIsProxy = 0x00040000; // class (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700141
Elliott Hughes20cde902011-10-04 17:37:27 -0700142// Special runtime-only flags.
143// Note: if only kAccClassIsReference is set, we have a soft reference.
144static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
145static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
146static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
147static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
148static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700149
150static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
151 | kAccClassIsWeakReference
152 | kAccClassIsFinalizerReference
153 | kAccClassIsPhantomReference);
154
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700155/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700156 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157 */
158/*
159 * A magic value for refOffsets. Ignore the bits and walk the super
160 * chain when this is the value.
161 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
162 * fields followed by 2 ref instance fields.]
163 */
164#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700165#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
166#define CLASS_OFFSET_ALIGNMENT 4
167#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
168/*
169 * Given an offset, return the bit number which would encode that offset.
170 * Local use only.
171 */
172#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700173 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700174 CLASS_OFFSET_ALIGNMENT)
175/*
176 * Is the given offset too large to be encoded?
177 */
178#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
179 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
180/*
181 * Return a single bit, encoding the offset.
182 * Undefined if the offset is too large, as defined above.
183 */
184#define CLASS_BIT_FROM_OFFSET(byteOffset) \
185 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
186/*
187 * Return an offset, given a bit number as returned from CLZ.
188 */
189#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700190 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700191
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192#define OFFSET_OF_OBJECT_MEMBER(type, field) \
193 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700194
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700195// Classes shared with the managed side of the world need to be packed
196// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700197#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700198
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700200class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700201 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 static MemberOffset ClassOffset() {
203 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700204 }
205
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700207 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 }
209
210 void SetClass(Class* new_klass);
211
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700212 bool InstanceOf(const Class* klass) const
213 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700214
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 size_t SizeOf() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700216
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217 Object* Clone() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
218
219 int32_t IdentityHashCode() const {
220 #ifdef MOVING_GARBAGE_COLLECTOR
221 // TODO: we'll need to use the Object's internal concept of identity
222 UNIMPLEMENTED(FATAL);
223 #endif
224 return reinterpret_cast<int32_t>(this);
225 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700226
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 static MemberOffset MonitorOffset() {
228 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
229 }
230
Elliott Hughes5f791332011-09-15 17:45:30 -0700231 volatile int32_t* GetRawLockWordAddress() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 byte* raw_addr = reinterpret_cast<byte*>(this) +
233 OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
Elliott Hughes5f791332011-09-15 17:45:30 -0700234 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
235 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700236 }
237
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700238 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700239
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 void MonitorEnter(Thread* thread) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
241 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700242
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 bool MonitorExit(Thread* thread) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
244 UNLOCK_FUNCTION(monitor_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700245
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 void Notify() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700247
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 void NotifyAll() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 void Wait(int64_t timeout) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700251
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 void Wait(int64_t timeout, int32_t nanos)
253 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700254
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700255 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700256
257 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700258 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700259 return down_cast<Class*>(this);
260 }
261
262 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700263 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700264 return down_cast<const Class*>(this);
265 }
266
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700267 bool IsObjectArray() const;
268
269 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700270 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700271
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700272 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700273 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700274
Brian Carlstromb63ec392011-08-27 17:38:27 -0700275 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700276
277 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700278 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700279 return down_cast<Array*>(this);
280 }
281
282 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700283 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700284 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700285 }
286
Elliott Hughesdbb40792011-11-18 17:05:22 -0800287 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700288
289 bool IsMethod() const;
290
291 Method* AsMethod() {
292 DCHECK(IsMethod());
293 return down_cast<Method*>(this);
294 }
295
Brian Carlstrom4873d462011-08-21 15:23:39 -0700296 const Method* AsMethod() const {
297 DCHECK(IsMethod());
298 return down_cast<const Method*>(this);
299 }
300
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 bool IsField() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700302
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 Field* AsField() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700304 DCHECK(IsField());
305 return down_cast<Field*>(this);
306 }
307
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 const Field* AsField() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700309 DCHECK(IsField());
310 return down_cast<const Field*>(this);
311 }
312
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700313 bool IsReferenceInstance() const;
314
315 bool IsWeakReferenceInstance() const;
316
317 bool IsSoftReferenceInstance() const;
318
319 bool IsFinalizerReferenceInstance() const;
320
321 bool IsPhantomReferenceInstance() const;
322
323 // Accessors for Java type fields
324 template<class T>
325 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700326 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
327 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800328 Runtime::Current()->GetHeap()->VerifyObject(result);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 return result;
330 }
331
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700332 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800333 Runtime::Current()->GetHeap()->VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700334 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
335 if (new_value != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800336 Runtime::Current()->GetHeap()->WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700338 }
339
340 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800341 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700342 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
343 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700344 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700345 return android_atomic_acquire_load(word_addr);
346 } else {
347 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349 }
350
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700351 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
352 if (this_is_valid) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800353 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700354 }
355 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700356 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700357 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700358 /*
359 * TODO: add an android_atomic_synchronization_store() function and
360 * use it in the 32-bit volatile set handlers. On some platforms we
361 * can use a fast atomic instruction and avoid the barriers.
362 */
363 ANDROID_MEMBAR_STORE();
364 *word_addr = new_value;
365 ANDROID_MEMBAR_FULL();
366 } else {
367 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700368 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 }
370
371 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800372 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700373 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700374 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700375 if (UNLIKELY(is_volatile)) {
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700376 uint64_t result = QuasiAtomic::Read64(addr);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700377 ANDROID_MEMBAR_FULL();
378 return result;
379 } else {
380 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 }
383
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700384 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800385 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700386 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
387 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700388 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700389 ANDROID_MEMBAR_STORE();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700390 QuasiAtomic::Swap64(new_value, addr);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700391 // Post-store barrier not required due to use of atomic op or mutex.
392 } else {
393 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395 }
396
397 protected:
398 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700399 template<class T>
400 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700401 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700402 }
403
404 template<typename T>
Ian Rogers30fab402012-01-23 15:43:46 -0800405 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
406 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700407 }
408
409 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700410 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700411
Elliott Hughes5f791332011-09-15 17:45:30 -0700412 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700413
Elliott Hughes5f791332011-09-15 17:45:30 -0700414 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700415 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700416 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700417};
418
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700419// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500420class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700421 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700423
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 void SetDeclaringClass(Class *new_declaring_class);
425
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426 uint32_t GetAccessFlags() const;
427
428 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700429 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700430 }
431
Elliott Hughes80609252011-09-23 17:24:51 -0700432 bool IsPublic() const {
433 return (GetAccessFlags() & kAccPublic) != 0;
434 }
435
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700436 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700438 }
439
jeffhaobdb76512011-09-07 11:43:16 -0700440 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700441 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700442 }
443
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800444 uint32_t GetDexFieldIndex() const {
445 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
446 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700447
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800448 void SetDexFieldIndex(uint32_t new_idx) {
449 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
450 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700451
452 // Offset to field within an Object
453 MemberOffset GetOffset() const;
454
buzbee34cd9e52011-09-08 14:31:52 -0700455 static MemberOffset OffsetOffset() {
456 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
457 }
458
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700459 MemberOffset GetOffsetDuringLinking() const;
460
461 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700462
Brian Carlstrom4873d462011-08-21 15:23:39 -0700463 // field access, null object for static fields
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 bool GetBoolean(const Object* object) const
465 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
466 void SetBoolean(Object* object, bool z) const
467 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
468 int8_t GetByte(const Object* object) const
469 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
470 void SetByte(Object* object, int8_t b) const
471 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
472 uint16_t GetChar(const Object* object) const
473 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
474 void SetChar(Object* object, uint16_t c) const
475 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
476 int16_t GetShort(const Object* object) const
477 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
478 void SetShort(Object* object, int16_t s) const
479 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
480 int32_t GetInt(const Object* object) const
481 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
482 void SetInt(Object* object, int32_t i) const
483 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
484 int64_t GetLong(const Object* object) const
485 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
486 void SetLong(Object* object, int64_t j) const
487 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
488 float GetFloat(const Object* object) const
489 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
490 void SetFloat(Object* object, float f) const
491 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
492 double GetDouble(const Object* object) const
493 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
494 void SetDouble(Object* object, double d) const
495 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
496 Object* GetObject(const Object* object) const
497 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
498 void SetObject(Object* object, const Object* l) const
499 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700500
Ian Rogersce9eca62011-10-07 17:11:03 -0700501 // raw field accesses
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700502 uint32_t Get32(const Object* object) const
503 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
504 void Set32(Object* object, uint32_t new_value) const
505 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
506 uint64_t Get64(const Object* object) const
507 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
508 void Set64(Object* object, uint64_t new_value) const
509 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
510 Object* GetObj(const Object* object) const
511 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
512 void SetObj(Object* object, const Object* new_value) const
513 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700514
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700515 static Class* GetJavaLangReflectField() {
516 DCHECK(java_lang_reflect_Field_ != NULL);
517 return java_lang_reflect_Field_;
518 }
519
520 static void SetClass(Class* java_lang_reflect_Field);
521 static void ResetClass();
522
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700523 bool IsVolatile() const {
524 return (GetAccessFlags() & kAccVolatile) != 0;
525 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700526
buzbee1da522d2011-09-04 11:22:20 -0700527 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400528 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800529 // The class we are a part of
Jesse Wilson35baaab2011-08-10 16:18:03 -0400530 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700531
Jesse Wilson35baaab2011-08-10 16:18:03 -0400532 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700533
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800534 // Dex cache index of field id
535 uint32_t field_dex_idx_;
536
Brian Carlstrom693267a2011-09-06 09:25:34 -0700537 // Offset of field within an instance or in the Class' static fields
538 uint32_t offset_;
539
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700540 static Class* java_lang_reflect_Field_;
541
Brian Carlstrom693267a2011-09-06 09:25:34 -0700542 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400543 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700544};
545
Jesse Wilson6bf19152011-09-29 13:12:33 -0400546// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500547class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700548 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700549 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700550 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700551 Object* obj,
552 Thread* thread,
Elliott Hughes77405792012-03-15 15:22:12 -0700553 JValue* args,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700554 JValue* result);
555
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700556 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700558 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700559
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700560 static MemberOffset DeclaringClassOffset() {
561 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700562 }
563
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700564 uint32_t GetAccessFlags() const;
565
566 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700567 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700568 }
569
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700570 // Returns true if the method is declared public.
571 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700572 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700573 }
574
575 // Returns true if the method is declared private.
576 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700577 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700578 }
579
580 // Returns true if the method is declared static.
581 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700582 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700583 }
584
Brian Carlstrom1f870082011-08-23 16:02:11 -0700585 // Returns true if the method is a constructor.
586 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700587 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700588 }
589
590 // Returns true if the method is static, private, or a constructor.
591 bool IsDirect() const {
Brian Carlstromf5822582012-03-19 22:34:31 -0700592 return IsDirect(GetAccessFlags());
593 }
594
595 static bool IsDirect(uint32_t access_flags) {
596 return (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700597 }
598
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700599 // Returns true if the method is declared synchronized.
600 bool IsSynchronized() const {
601 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700602 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700603 }
604
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700605 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700606 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700607 }
608
Elliott Hughes80609252011-09-23 17:24:51 -0700609 bool IsMiranda() const {
610 return (GetAccessFlags() & kAccMiranda) != 0;
611 }
612
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700613 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700614 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700615 }
616
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700617 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700618 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700619 }
620
621 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700622 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700623 }
624
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800625 bool IsProxyMethod() const;
Ian Rogers0571d352011-11-03 19:51:38 -0700626
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700627 uint16_t GetMethodIndex() const;
628
629 size_t GetVtableIndex() const {
630 return GetMethodIndex();
631 }
632
633 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700634 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700635 }
636
637 static MemberOffset MethodIndexOffset() {
638 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
639 }
640
641 uint32_t GetCodeItemOffset() const {
642 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
643 }
644
645 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700646 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647 }
648
649 // Number of 32bit registers that would be required to hold all the arguments
650 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700651
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800652 uint32_t GetDexMethodIndex() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700653
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800654 void SetDexMethodIndex(uint32_t new_idx) {
655 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), new_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700656 }
657
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700658 ObjectArray<String>* GetDexCacheStrings() const;
659 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
660
661 static MemberOffset DexCacheStringsOffset() {
662 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
663 }
664
Ian Rogers19846512012-02-24 11:42:47 -0800665 static MemberOffset DexCacheResolvedMethodsOffset() {
666 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
667 }
668
buzbee2a475e72011-09-07 17:19:17 -0700669 static MemberOffset DexCacheResolvedTypesOffset() {
670 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
671 }
672
buzbee1da522d2011-09-04 11:22:20 -0700673 static MemberOffset DexCacheInitializedStaticStorageOffset() {
674 return OFFSET_OF_OBJECT_MEMBER(Method,
675 dex_cache_initialized_static_storage_);
676 }
677
Ian Rogers19846512012-02-24 11:42:47 -0800678 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
679 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
680
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700681 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
682 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
683
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700684 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
685 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
686
Ian Rogers466bb252011-10-14 03:29:56 -0700687 // Find the method that this method overrides
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700688 Method* FindOverriddenMethod() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers466bb252011-10-14 03:29:56 -0700689
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 void Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const
691 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700692
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700693 const void* GetCode() const {
694 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700695 }
696
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700697 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700698 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
699 }
700
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701 uint32_t GetCodeSize() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700702 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
703 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
704 if (code == 0) {
705 return 0;
706 }
707 // TODO: make this Thumb2 specific
708 code &= ~0x1;
709 return reinterpret_cast<uint32_t*>(code)[-1];
710 }
711
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 bool IsWithinCode(uintptr_t pc) const
713 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700714 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
715 if (code == 0) {
716 return pc == 0;
717 }
718 return (code <= pc && pc < code + GetCodeSize());
719 }
720
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 void AssertPcIsWithinCode(uintptr_t pc) const
722 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700723
Brian Carlstrome24fa612011-09-29 00:53:55 -0700724 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700725 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700726 return reinterpret_cast<uint32_t>(GetCode());
727 }
728
729 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700730 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700731 SetCode(reinterpret_cast<void*>(code_offset));
732 }
733
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700734 static MemberOffset GetCodeOffset() {
735 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700736 }
737
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700738 const uint32_t* GetMappingTable() const {
739 const uint32_t* map = GetMappingTableRaw();
740 if (map == NULL) {
741 return map;
742 }
743 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700744 }
745
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700746 uint32_t GetMappingTableLength() const {
747 const uint32_t* map = GetMappingTableRaw();
748 if (map == NULL) {
749 return 0;
750 }
751 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700752 }
753
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700754 const uint32_t* GetMappingTableRaw() const {
755 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700756 }
757
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700758 void SetMappingTable(const uint32_t* mapping_table) {
759 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
760 mapping_table, false);
761 }
762
763 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700764 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700765 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
766 }
767
768 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700769 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700770 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
771 }
772
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800773 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700774 const uint16_t* GetVmapTableRaw() const {
775 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
776 }
777
778 void SetVmapTable(const uint16_t* vmap_table) {
779 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
780 }
781
782 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700783 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700784 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
785 }
786
787 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700788 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700789 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700790 }
791
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800792 const uint8_t* GetGcMap() const {
793 const uint8_t* gc_map_raw = GetGcMapRaw();
794 if (gc_map_raw == NULL) {
795 return gc_map_raw;
796 }
797 return gc_map_raw + sizeof(uint32_t);
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 }
799
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800800 uint32_t GetGcMapLength() const {
801 const uint8_t* gc_map_raw = GetGcMapRaw();
802 if (gc_map_raw == NULL) {
803 return 0;
804 }
805 return static_cast<uint32_t>((gc_map_raw[0] << 24) |
806 (gc_map_raw[1] << 16) |
807 (gc_map_raw[2] << 8) |
808 (gc_map_raw[3] << 0));
809 }
810
811 const uint8_t* GetGcMapRaw() const {
812 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
813 }
814 void SetGcMap(const uint8_t* data) {
815 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
816 }
817
818 uint32_t GetOatGcMapOffset() const {
819 DCHECK(!Runtime::Current()->IsStarted());
820 return reinterpret_cast<uint32_t>(GetGcMapRaw());
821 }
822 void SetOatGcMapOffset(uint32_t gc_map_offset) {
823 DCHECK(!Runtime::Current()->IsStarted());
824 SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 }
826
Shih-wei Liaod11af152011-08-23 16:02:11 -0700827 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700828 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
829 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700830 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
831 return result;
832 }
833
834 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700835 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700836 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
837 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700838 }
839
Shih-wei Liaod11af152011-08-23 16:02:11 -0700840 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700842 }
843
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700844 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700845
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 void RegisterNative(Thread* self, const void* native_method)
847 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogersb033c752011-07-20 12:22:35 -0700848
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700849 void UnregisterNative(Thread* self) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes5174fe62011-08-23 15:12:35 -0700850
Ian Rogersb033c752011-07-20 12:22:35 -0700851 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700852 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700853 }
854
Brian Carlstrom78128a62011-09-15 17:21:19 -0700855 const void* GetNativeMethod() const {
856 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
857 }
858
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700859 // Native to managed invocation stub entry point
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700860 const InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700861 InvokeStub* result = GetFieldPtr<InvokeStub*>(
862 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
863 // TODO: DCHECK(result != NULL); should be ahead of time compiled
864 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700865 }
866
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700867 void SetInvokeStub(InvokeStub* invoke_stub) {
868 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
869 invoke_stub, false);
870 }
871
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700872 uint32_t GetInvokeStubSize() const {
Logan Chien4284bb92012-06-06 15:30:44 +0800873 uintptr_t invoke_stub = reinterpret_cast<uintptr_t>(GetInvokeStub());
874 if (invoke_stub == 0) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700875 return 0;
876 }
Logan Chien4284bb92012-06-06 15:30:44 +0800877 // TODO: make this Thumb2 specific
878 invoke_stub &= ~0x1;
879 return reinterpret_cast<const uint32_t*>(invoke_stub)[-1];
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700880 }
881
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700882 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700883 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700884 return reinterpret_cast<uint32_t>(GetInvokeStub());
885 }
886
887 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700888 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700889 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
890 }
891
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700892 static MemberOffset GetInvokeStubOffset() {
893 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700894 }
895
buzbee561227c2011-09-02 15:28:19 -0700896 static MemberOffset GetMethodIndexOffset() {
897 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
898 }
899
Ian Rogers90865722011-09-19 11:11:44 -0700900 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700901 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700902 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700903
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700904 void SetCoreSpillMask(uint32_t core_spill_mask) {
905 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700906 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700907 }
908
Ian Rogers90865722011-09-19 11:11:44 -0700909 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700910 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
911 }
912
913 void SetFpSpillMask(uint32_t fp_spill_mask) {
914 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700915 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700916 }
917
Ian Rogers57b86d42012-03-27 16:05:41 -0700918 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
Ian Rogers640495b2012-06-22 15:15:47 -0700919 // conventions for a method of managed code. Returns false for Proxy methods.
Ian Rogers57b86d42012-03-27 16:05:41 -0700920 bool IsRuntimeMethod() const {
921 return GetDexMethodIndex() == DexFile::kDexNoIndex16;
922 }
923
Ian Rogers90865722011-09-19 11:11:44 -0700924 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700925 bool IsCalleeSaveMethod() const {
Ian Rogers57b86d42012-03-27 16:05:41 -0700926 if (!IsRuntimeMethod()) {
927 return false;
928 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700929 Runtime* runtime = Runtime::Current();
930 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700931 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700932 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
933 result = true;
934 break;
935 }
936 }
Ian Rogers90865722011-09-19 11:11:44 -0700937 return result;
938 }
939
Ian Rogers19846512012-02-24 11:42:47 -0800940 bool IsResolutionMethod() const {
941 bool result = this == Runtime::Current()->GetResolutionMethod();
942 // Check that if we do think it is phony it looks like the resolution method
943 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
944 return result;
945 }
946
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700947 // Converts a native PC to a dex PC. TODO: this is a no-op
948 // until we associate a PC mapping table with each method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 uint32_t ToDexPC(const uintptr_t pc) const
950 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700951
952 // Converts a dex PC to a native PC. TODO: this is a no-op
953 // until we associate a PC mapping table with each method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 uintptr_t ToNativePC(const uint32_t dex_pc) const
955 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700956
957 // Find the catch block for the given exception type and dex_pc
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const
959 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700960
Elliott Hughes80609252011-09-23 17:24:51 -0700961 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
962
963 static Class* GetConstructorClass() {
964 return java_lang_reflect_Constructor_;
965 }
966
967 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700968 return java_lang_reflect_Method_;
969 }
970
Elliott Hughes80609252011-09-23 17:24:51 -0700971 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700972
973 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700974 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800975 // The class we are a part of
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700976 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700977
Brian Carlstrom693267a2011-09-06 09:25:34 -0700978 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800979 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700980
981 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800982 ObjectArray<Class>* dex_cache_resolved_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700983
984 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstromdbc05252011-09-09 01:59:59 -0700985 ObjectArray<Class>* dex_cache_resolved_types_;
986
987 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
988 ObjectArray<String>* dex_cache_strings_;
989
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700990 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -0700991 uint32_t access_flags_;
992
993 // Compiled code associated with this method for callers from managed code.
994 // May be compiled managed code or a bridge for invoking a native method.
995 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700996
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700997 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700998 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700999
Brian Carlstrom693267a2011-09-06 09:25:34 -07001000 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001001 uint32_t core_spill_mask_;
1002
1003 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001004 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001005
Brian Carlstrom693267a2011-09-06 09:25:34 -07001006 // Total size in bytes of the frame
1007 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001008
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001009 // Garbage collection map
1010 const uint8_t* gc_map_;
1011
Brian Carlstrom693267a2011-09-06 09:25:34 -07001012 // Native invocation stub entry point for calling from native to managed code.
1013 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001014
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001016 const uint32_t* mapping_table_;
1017
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001018 // Index into method_ids of the dex file associated with this method
1019 uint32_t method_dex_index_;
1020
Ian Rogers466bb252011-10-14 03:29:56 -07001021 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -07001022 //
Ian Rogers466bb252011-10-14 03:29:56 -07001023 // For abstract methods in an interface class, this is the offset of the method in
1024 // "iftable_->Get(n)->GetMethodArray()".
Ian Rogers19846512012-02-24 11:42:47 -08001025 //
1026 // For static and direct methods this is the index in the direct methods table.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001027 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001028
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001029 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001030 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001031
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 // When a register is promoted into a register, the spill mask holds which registers hold dex
1033 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
1034 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001035 const uint16_t* vmap_table_;
1036
Elliott Hughes80609252011-09-23 17:24:51 -07001037 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001038 static Class* java_lang_reflect_Method_;
1039
Brian Carlstrom693267a2011-09-06 09:25:34 -07001040 friend class ImageWriter; // for relocating code_ and invoke_stub_
1041 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001042 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001043};
1044
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001045class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001046 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001047 // A convenience for code that doesn't know the component size,
1048 // and doesn't want to have to work it out itself.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001049 static Array* Alloc(Class* array_class, int32_t component_count)
1050 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001051
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001052 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size)
1053 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapirof88c9522011-08-06 15:47:38 -07001054
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001055 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001056
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001057 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001058 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001059 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001060
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001061 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001062 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001063 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001064 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001065
buzbeec143c552011-08-20 17:38:58 -07001066 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001067 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001068 }
1069
Ian Rogersa15e67d2012-02-28 13:51:55 -08001070 static MemberOffset DataOffset(size_t component_size) {
1071 if (component_size != sizeof(int64_t)) {
1072 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
1073 } else {
1074 // Align longs and doubles.
1075 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
1076 }
buzbeec143c552011-08-20 17:38:58 -07001077 }
1078
Ian Rogersa15e67d2012-02-28 13:51:55 -08001079 void* GetRawData(size_t component_size) {
1080 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1081 return reinterpret_cast<void*>(data);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001082 }
1083
Elliott Hughesa21039c2012-06-21 12:09:25 -07001084 const void* GetRawData(size_t component_size) const {
1085 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1086 return reinterpret_cast<const void*>(data);
1087 }
1088
Elliott Hughes289da822011-08-16 10:11:20 -07001089 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001090 bool IsValidIndex(int32_t index) const
1091 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001092 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001093 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001094 }
1095 return true;
1096 }
1097
Elliott Hughes80609252011-09-23 17:24:51 -07001098 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001099 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const
1100 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
1101 bool ThrowArrayStoreException(Object* object) const
1102 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes80609252011-09-23 17:24:51 -07001103
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001104 private:
1105 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001106 int32_t length_;
buzbeec143c552011-08-20 17:38:58 -07001107 // Marker for the data (used by generated code)
1108 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001109
Carl Shapirof88c9522011-08-06 15:47:38 -07001110 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001111};
1112
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001113template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001114class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001115 public:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001116 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length)
1117 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001118
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 T* Get(int32_t i) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001120
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001121 void Set(int32_t i, T* object) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001122
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001123 // Set element without bound and element type checks, to be used in limited
1124 // circumstances, such as during boot image writing
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001125 void SetWithoutChecks(int32_t i, T* object)
1126 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapirof88c9522011-08-06 15:47:38 -07001127
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 T* GetWithoutChecks(int32_t i) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -07001129
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001130 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001131 ObjectArray<T>* dst, int dst_pos,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 size_t length)
1133 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapirof88c9522011-08-06 15:47:38 -07001134
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 ObjectArray<T>* CopyOf(int32_t new_length)
1136 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001137
1138 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001139 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001140};
1141
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001142template<class T>
1143ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001144 Array* array = Array::Alloc(object_array_class, length, sizeof(Object*));
Ian Rogersc8b306f2012-02-17 21:34:44 -08001145 if (UNLIKELY(array == NULL)) {
1146 return NULL;
1147 } else {
1148 return array->AsObjectArray<T>();
1149 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001150}
1151
1152template<class T>
1153T* ObjectArray<T>::Get(int32_t i) const {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001154 if (UNLIKELY(!IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001155 return NULL;
1156 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08001157 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001158 return GetFieldObject<T*>(data_offset, false);
1159}
1160
1161template<class T>
1162ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1163 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1164 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1165 return new_array;
1166}
1167
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001168// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001169// provides the static storage. However, this might change to an Array
1170// to improve image sharing, so we use this type to avoid assumptions
1171// on the current storage.
Elliott Hughes48257562012-06-06 17:42:44 -07001172class MANAGED StaticStorageBase : public Object {
1173};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001174
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001175// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001176class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001177 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001178 // Class Status
1179 //
1180 // kStatusNotReady: If a Class cannot be found in the class table by
1181 // FindClass, it allocates an new one with AllocClass in the
1182 // kStatusNotReady and calls LoadClass. Note if it does find a
1183 // class, it may not be kStatusResolved and it will try to push it
1184 // forward toward kStatusResolved.
1185 //
1186 // kStatusIdx: LoadClass populates with Class with information from
1187 // the DexFile, moving the status to kStatusIdx, indicating that the
Ian Rogersd418eda2012-01-30 12:14:28 -08001188 // Class value in super_class_ has not been populated. The new Class
1189 // can then be inserted into the classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001190 //
1191 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1192 // attempt to move a kStatusIdx class forward to kStatusLoaded by
Ian Rogersd418eda2012-01-30 12:14:28 -08001193 // using ResolveClass to initialize the super_class_ and ensuring the
1194 // interfaces are resolved.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001195 //
1196 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001197 // shows linking is complete and fields of the Class populated by making
1198 // it kStatusResolved. Java allows circularities of the form where a super
1199 // class has a field that is of the type of the sub class. We need to be able
1200 // to fully resolve super classes while resolving types for fields.
jeffhaof1e6b7c2012-06-05 18:33:30 -07001201 //
1202 // kStatusRetryVerificationAtRuntime: The verifier sets a class to
1203 // this state if it encounters a soft failure at compile time. This
1204 // often happens when there are unresolved classes in other dex
1205 // files, and this status marks a class as needing to be verified
1206 // again at runtime.
1207 //
1208 // TODO: Explain the other states
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001209 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001210 kStatusError = -1,
1211 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001212 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001213 kStatusLoaded = 2, // DEX idx values resolved
1214 kStatusResolved = 3, // part of linking
jeffhaoa9b3bf42012-06-06 17:18:39 -07001215 kStatusVerifying = 4, // in the process of being verified
1216 kStatusRetryVerificationAtRuntime = 5, // compile time verification failed, retry at runtime
jeffhaof1e6b7c2012-06-05 18:33:30 -07001217 kStatusVerified = 6, // logically part of linking; done pre-init
1218 kStatusInitializing = 7, // class init in progress
1219 kStatusInitialized = 8, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001220 };
1221
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001222 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001223 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1224 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001225 }
1226
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 void SetStatus(Status new_status) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001228
1229 // Returns true if the class has failed to link.
1230 bool IsErroneous() const {
1231 return GetStatus() == kStatusError;
1232 }
1233
1234 // Returns true if the class has been loaded.
1235 bool IsIdxLoaded() const {
1236 return GetStatus() >= kStatusIdx;
1237 }
1238
1239 // Returns true if the class has been loaded.
1240 bool IsLoaded() const {
1241 return GetStatus() >= kStatusLoaded;
1242 }
1243
1244 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001245 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001246 return GetStatus() >= kStatusResolved;
1247 }
1248
jeffhaof1e6b7c2012-06-05 18:33:30 -07001249 // Returns true if the class was compile-time verified.
1250 bool IsCompileTimeVerified() const {
1251 return GetStatus() >= kStatusRetryVerificationAtRuntime;
1252 }
1253
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001254 // Returns true if the class has been verified.
1255 bool IsVerified() const {
1256 return GetStatus() >= kStatusVerified;
1257 }
1258
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001259 // Returns true if the class is initializing.
1260 bool IsInitializing() const {
1261 return GetStatus() >= kStatusInitializing;
1262 }
1263
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001264 // Returns true if the class is initialized.
1265 bool IsInitialized() const {
1266 return GetStatus() == kStatusInitialized;
1267 }
1268
1269 uint32_t GetAccessFlags() const;
1270
1271 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001272 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001273 }
1274
1275 // Returns true if the class is an interface.
1276 bool IsInterface() const {
1277 return (GetAccessFlags() & kAccInterface) != 0;
1278 }
1279
1280 // Returns true if the class is declared public.
1281 bool IsPublic() const {
1282 return (GetAccessFlags() & kAccPublic) != 0;
1283 }
1284
1285 // Returns true if the class is declared final.
1286 bool IsFinal() const {
1287 return (GetAccessFlags() & kAccFinal) != 0;
1288 }
1289
Elliott Hughes20cde902011-10-04 17:37:27 -07001290 bool IsFinalizable() const {
1291 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1292 }
1293
1294 void SetFinalizable() {
1295 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1296 SetAccessFlags(flags | kAccClassIsFinalizable);
1297 }
1298
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001299 // Returns true if the class is abstract.
1300 bool IsAbstract() const {
1301 return (GetAccessFlags() & kAccAbstract) != 0;
1302 }
1303
1304 // Returns true if the class is an annotation.
1305 bool IsAnnotation() const {
1306 return (GetAccessFlags() & kAccAnnotation) != 0;
1307 }
1308
1309 // Returns true if the class is synthetic.
1310 bool IsSynthetic() const {
1311 return (GetAccessFlags() & kAccSynthetic) != 0;
1312 }
1313
1314 bool IsReferenceClass() const {
1315 return (GetAccessFlags() & kAccClassIsReference) != 0;
1316 }
1317
1318 bool IsWeakReferenceClass() const {
1319 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1320 }
1321
1322 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001323 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001324 }
1325
1326 bool IsFinalizerReferenceClass() const {
1327 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1328 }
1329
1330 bool IsPhantomReferenceClass() const {
1331 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1332 }
1333
Ian Rogersd418eda2012-01-30 12:14:28 -08001334
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001335 String* GetName() const; // Returns the cached name
Ian Rogersd418eda2012-01-30 12:14:28 -08001336 void SetName(String* name); // Sets the cached name
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001337 String* ComputeName() // Computes the name, then sets the cached value
1338 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001339
1340 bool IsProxyClass() const {
1341 // Read access flags without using getter as whether something is a proxy can be check in
1342 // any loaded state
1343 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
1344 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1345 return (access_flags & kAccClassIsProxy) != 0;
1346 }
1347
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001348 Primitive::Type GetPrimitiveType() const {
1349 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1350 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001351 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1352 }
1353
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001354 void SetPrimitiveType(Primitive::Type new_type) {
1355 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001356 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001357 }
1358
1359 // Returns true if the class is a primitive type.
1360 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001361 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001362 }
1363
1364 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001365 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001366 }
1367
1368 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001369 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001370 }
1371
1372 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001373 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001374 }
1375
1376 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001377 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001378 }
1379
1380 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001381 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001382 }
1383
1384 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001385 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001386 }
1387
1388 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001389 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001390 }
1391
1392 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001393 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001394 }
1395
1396 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001397 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001398 }
1399
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 // Depth of class from java.lang.Object
1401 size_t Depth() {
1402 size_t depth = 0;
Elliott Hughesff17f1f2012-01-24 18:12:29 -08001403 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 depth++;
1405 }
1406 return depth;
1407 }
1408
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001409 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001410 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001411 }
1412
Elliott Hughesdbb40792011-11-18 17:05:22 -08001413 bool IsClassClass() const;
1414
1415 bool IsStringClass() const;
1416
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 bool IsThrowableClass() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001418
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001419 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001420 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001421 }
1422
1423 void SetComponentType(Class* new_component_type) {
1424 DCHECK(GetComponentType() == NULL);
1425 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001426 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001427 }
1428
1429 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001430 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001431 }
1432
1433 bool IsObjectClass() const {
1434 return !IsPrimitive() && GetSuperClass() == NULL;
1435 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001436 bool IsInstantiable() const {
1437 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1438 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001439
Brian Carlstrom1f870082011-08-23 16:02:11 -07001440 // Creates a raw object instance but does not invoke the default constructor.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001441 Object* AllocObject() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001442
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001443 bool IsVariableSize() const {
1444 // Classes and arrays vary in size, and so the object_size_ field cannot
1445 // be used to get their instance size
1446 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 }
1448
Brian Carlstrom4873d462011-08-21 15:23:39 -07001449 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001450 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001451 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001452 }
1453
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001454 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001455 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001456 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001457 }
1458
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001459 void SetClassSize(size_t new_class_size)
1460 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001461
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001462 size_t GetObjectSize() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001463 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001464 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001465 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001466 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001467 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001468 }
1469
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001470 void SetObjectSize(size_t new_object_size) {
1471 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001472 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001473 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001474 }
1475
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001476 // Returns true if this class is in the same packages as that class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001477 bool IsInSamePackage(const Class* that) const
1478 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001479
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001480 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001481
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001482 // Returns true if this class can access that class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483 bool CanAccess(Class* that) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001484 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001485 }
1486
Ian Rogersf2391652011-12-14 12:50:52 -08001487 // Can this class access a member in the provided class with the provided member access flags?
Ian Rogersc2b44472011-12-14 21:17:17 -08001488 // Note that access to the class isn't checked in case the declaring class is protected and the
1489 // method has been exposed by a public sub-class
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001490 bool CanAccessMember(Class* access_to, uint32_t member_flags) const
1491 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogersf2391652011-12-14 12:50:52 -08001492 // Classes can access all of their own members
jeffhao4a801a42011-09-23 13:53:40 -07001493 if (this == access_to) {
1494 return true;
1495 }
Ian Rogersf2391652011-12-14 12:50:52 -08001496 // Public members are trivially accessible
1497 if (member_flags & kAccPublic) {
1498 return true;
1499 }
1500 // Private members are trivially not accessible
jeffhao4a801a42011-09-23 13:53:40 -07001501 if (member_flags & kAccPrivate) {
1502 return false;
1503 }
Ian Rogersf2391652011-12-14 12:50:52 -08001504 // Check for protected access from a sub-class, which may or may not be in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001505 if (member_flags & kAccProtected) {
1506 if (this->IsSubClass(access_to)) {
1507 return true;
1508 }
1509 }
Ian Rogersf2391652011-12-14 12:50:52 -08001510 // Allow protected access from other classes in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001511 return this->IsInSamePackage(access_to);
1512 }
1513
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001514 bool IsSubClass(const Class* klass) const
1515 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001516
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1518 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1519 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1520 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1521 // to themselves. Classes for primitive types may not assign to each other.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 bool IsAssignableFrom(const Class* src) const
1523 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001524 DCHECK(src != NULL);
1525 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001526 // Can always assign to things of the same type
1527 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001528 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001529 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001530 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001531 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001532 return src->Implements(this);
1533 } else if (src->IsArrayClass()) {
1534 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001535 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001537 }
1538 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001539
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001540 Class* GetSuperClass() const {
1541 // Can only get super class for loaded classes (hack for when runtime is
1542 // initializing)
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001543 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001544 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001545 }
1546
1547 void SetSuperClass(Class *new_super_class) {
1548 // super class is assigned once, except during class linker initialization
1549 Class* old_super_class = GetFieldObject<Class*>(
1550 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1551 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1552 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001553 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001554 }
1555
1556 bool HasSuperClass() const {
1557 return GetSuperClass() != NULL;
1558 }
1559
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001560 static MemberOffset SuperClassOffset() {
1561 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001562 }
1563
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001564 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001565
Ian Rogers365c1022012-06-22 15:05:28 -07001566 void SetClassLoader(ClassLoader* new_cl);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001567
1568 static MemberOffset DexCacheOffset() {
1569 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1570 }
1571
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001572 enum {
1573 kDumpClassFullDetail = 1,
1574 kDumpClassClassLoader = (1 << 1),
1575 kDumpClassInitialized = (1 << 2),
1576 };
1577
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001578 void DumpClass(std::ostream& os, int flags) const
1579 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001580
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001581 DexCache* GetDexCache() const;
1582
1583 void SetDexCache(DexCache* new_dex_cache);
1584
1585 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001586 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001587 return GetFieldObject<ObjectArray<Method>*>(
1588 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1589 }
1590
1591 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1592 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1593 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1594 DCHECK_NE(0, new_direct_methods->GetLength());
1595 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1596 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001597 }
1598
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001599 Method* GetDirectMethod(int32_t i) const
1600 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001601 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001602 }
1603
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001604 void SetDirectMethod(uint32_t i, Method* f) // TODO: uint16_t
1605 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_){
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001606 ObjectArray<Method>* direct_methods =
1607 GetFieldObject<ObjectArray<Method>*>(
1608 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1609 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001610 }
1611
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001612 // Returns the number of static, private, and constructor methods.
1613 size_t NumDirectMethods() const {
1614 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1615 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001616
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001617 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001618 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001619 return GetFieldObject<ObjectArray<Method>*>(
1620 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1621 }
1622
1623 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1624 // TODO: we reassign virtual methods to grow the table for miranda
1625 // methods.. they should really just be assigned once
1626 DCHECK_NE(0, new_virtual_methods->GetLength());
1627 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1628 new_virtual_methods, false);
1629 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001630
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001631 // Returns the number of non-inherited virtual methods.
1632 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001633 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001634 }
1635
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001636 Method* GetVirtualMethod(uint32_t i) const
1637 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001638 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001639 return GetVirtualMethods()->Get(i);
1640 }
1641
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001642 Method* GetVirtualMethodDuringLinking(uint32_t i) const
1643 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001644 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001645 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001646 }
1647
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001648 void SetVirtualMethod(uint32_t i, Method* f) // TODO: uint16_t
1649 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001650 ObjectArray<Method>* virtual_methods =
1651 GetFieldObject<ObjectArray<Method>*>(
1652 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1653 virtual_methods->Set(i, f);
1654 }
1655
1656 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001657 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001658 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001659 }
1660
1661 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001662 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001663 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001664 }
1665
1666 void SetVTable(ObjectArray<Method>* new_vtable) {
1667 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1668 }
1669
1670 static MemberOffset VTableOffset() {
1671 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001672 }
1673
Brian Carlstrom30b94452011-08-25 21:35:26 -07001674 // Given a method implemented by this class but potentially from a
1675 // super class, return the specific implementation
1676 // method for this class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001677 Method* FindVirtualMethodForVirtual(Method* method)
1678 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001679 DCHECK(!method->GetDeclaringClass()->IsInterface());
1680 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001681 // Use the index to a potentially overridden one for this instance's class.
1682 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001683 }
1684
1685 // Given a method implemented by this class, but potentially from a
1686 // super class or interface, return the specific implementation
1687 // method for this class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001688 Method* FindVirtualMethodForInterface(Method* method)
1689 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001690
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001691 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const
1692 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
jeffhaobdb76512011-09-07 11:43:16 -07001693
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
1695 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001696
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001697 Method* FindVirtualMethodForVirtualOrInterface(Method* method)
1698 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001699 if (method->IsDirect()) {
1700 return method;
1701 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001702 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001703 return FindVirtualMethodForInterface(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001704 }
1705 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 }
1707
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const
1709 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001710
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001711 Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
1712 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001713
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001714 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const
1715 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001716
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
1718 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001719
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001720 Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const
1721 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001722
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001723 Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
1724 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001725
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const
1727 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001728
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001729 Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
1730 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001731
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001732 int32_t GetIfTableCount() const {
1733 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1734 if (iftable == NULL) {
1735 return 0;
1736 }
1737 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001738 }
1739
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001740 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001741 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001742 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001743 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1744 }
1745
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001746 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001747 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001748 }
1749
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001750 // Get instance fields of the class (See also GetSFields).
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001751 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001752 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001753 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001754 }
1755
1756 void SetIFields(ObjectArray<Field>* new_ifields) {
1757 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1758 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001759 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001760 }
1761
1762 size_t NumInstanceFields() const {
1763 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1764 }
1765
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 Field* GetInstanceField(uint32_t i) const // TODO: uint16_t
1767 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_){
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001768 DCHECK_NE(NumInstanceFields(), 0U);
1769 return GetIFields()->Get(i);
1770 }
1771
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001772 void SetInstanceField(uint32_t i, Field* f) // TODO: uint16_t
1773 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_){
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001774 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1775 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1776 ifields->Set(i, f);
1777 }
1778
1779 // Returns the number of instance fields containing reference types.
1780 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001781 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001782 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001783 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001784 }
1785
1786 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001787 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001788 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001789 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001790 }
1791
1792 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001793 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001794 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001795 }
1796
1797 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001798 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001799 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001800 }
1801
1802 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1803
1804 // Beginning of static field data
1805 static MemberOffset FieldsOffset() {
1806 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1807 }
1808
1809 // Returns the number of static fields containing reference types.
1810 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001811 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001812 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001813 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001814 }
1815
1816 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001817 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001818 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001819 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001820 }
1821
1822 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001823 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001824 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001825 }
1826
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001827 // Gets the static fields of the class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001828 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001829 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001830 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001831 }
1832
1833 void SetSFields(ObjectArray<Field>* new_sfields) {
1834 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1835 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001836 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001837 }
1838
1839 size_t NumStaticFields() const {
1840 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1841 }
1842
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001843 Field* GetStaticField(uint32_t i) const // TODO: uint16_t
1844 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001845 return GetSFields()->Get(i);
1846 }
1847
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001848 void SetStaticField(uint32_t i, Field* f) // TODO: uint16_t
1849 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001850 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1851 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1852 sfields->Set(i, f);
1853 }
1854
1855 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001856 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001857 }
1858
1859 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1860
Ian Rogersb067ac22011-12-13 18:05:09 -08001861 // Find a static or instance field using the JLS resolution order
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 Field* FindField(const StringPiece& name, const StringPiece& type)
1863 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogersb067ac22011-12-13 18:05:09 -08001864
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001865 // Finds the given instance field in this class or a superclass.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 Field* FindInstanceField(const StringPiece& name, const StringPiece& type)
1867 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001868
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001869 // Finds the given instance field in this class or a superclass, only searches classes that
1870 // have the same dex cache.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
1872 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001873
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001874 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type)
1875 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001876
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001877 Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
1878 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001879
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001880 // Finds the given static field in this class or a superclass.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001881 Field* FindStaticField(const StringPiece& name, const StringPiece& type)
1882 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001883
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001884 // Finds the given static field in this class or superclass, only searches classes that
1885 // have the same dex cache.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
1887 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001888
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type)
1890 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001891
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
1893 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001894
Elliott Hughesdcc24742011-09-07 14:02:44 -07001895 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001896 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001897 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1898 }
1899
Elliott Hughesdcc24742011-09-07 14:02:44 -07001900 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001901 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001902 }
1903
1904 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001905 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001906 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001907 }
1908
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001909 uint16_t GetDexTypeIndex() const {
1910 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
jeffhao64155032011-11-03 17:56:34 -07001911 }
1912
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001913 void SetDexTypeIndex(uint16_t type_idx) {
1914 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
jeffhao64155032011-11-03 17:56:34 -07001915 }
1916
jeffhaobdb76512011-09-07 11:43:16 -07001917 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001918 void SetVerifyErrorClass(Class* klass)
1919 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001920 CHECK(klass != NULL) << PrettyClass(this);
1921 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
1922 }
1923
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 bool Implements(const Class* klass) const
1925 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
1926 bool IsArrayAssignableFromArray(const Class* klass) const
1927 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
1928 bool IsAssignableFromArray(const Class* klass) const
1929 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001930
Brian Carlstrom693267a2011-09-06 09:25:34 -07001931 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001932 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001933
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001934 // For array classes, the component class object for instanceof/checkcast
1935 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001936 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001937
Elliott Hughes81ff3182012-03-23 20:35:56 -07001938 // DexCache of resolved constant pool entries (will be NULL for classes generated by the
1939 // runtime such as arrays and primitive classes).
Brian Carlstrom693267a2011-09-06 09:25:34 -07001940 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001941
1942 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001943 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001944
Brian Carlstrom693267a2011-09-06 09:25:34 -07001945 // instance fields
1946 //
1947 // These describe the layout of the contents of an Object.
1948 // Note that only the fields directly declared by this class are
1949 // listed in ifields; fields declared by a superclass are listed in
1950 // the superclass's Class.ifields.
1951 //
1952 // All instance fields that refer to objects are guaranteed to be at
1953 // the beginning of the field list. num_reference_instance_fields_
1954 // specifies the number of reference fields.
1955 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001956
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001957 // Interface table (iftable_), one entry per interface supported by
1958 // this class. That means one entry for each interface we support
1959 // directly, indirectly via superclass, or indirectly via
1960 // superinterface. This will be null if neither we nor our
1961 // superclass implement any interfaces.
1962 //
1963 // Why we need this: given "class Foo implements Face", declare
1964 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1965 // is part of the Face interface. We can't easily use a single
1966 // vtable.
1967 //
1968 // For every interface a concrete class implements, we create an array
1969 // of the concrete vtable_ methods for the methods in the interface.
1970 ObjectArray<InterfaceEntry>* iftable_;
1971
Ian Rogersd418eda2012-01-30 12:14:28 -08001972 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
1973 String* name_;
1974
Brian Carlstromdbc05252011-09-09 01:59:59 -07001975 // Static fields
1976 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001977
Ian Rogersd418eda2012-01-30 12:14:28 -08001978 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001979 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001980
Brian Carlstromdbc05252011-09-09 01:59:59 -07001981 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001982 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001983
Brian Carlstromdbc05252011-09-09 01:59:59 -07001984 // virtual methods defined in this class; invoked through vtable
1985 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001986
Ian Rogers9074b992011-10-26 17:41:55 -07001987 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
1988 // copied in, and virtual methods from our class either replace those from the super or are
1989 // appended. For abstract classes, methods may be created in the vtable that aren't in
1990 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001991 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001992
Brian Carlstromdbc05252011-09-09 01:59:59 -07001993 // access flags; low 16 bits are defined by VM spec
1994 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001995
Jesse Wilson6bf19152011-09-29 13:12:33 -04001996 // Total size of the Class instance; used when allocating storage on gc heap.
1997 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001998 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001999
Elliott Hughes5f791332011-09-15 17:45:30 -07002000 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002001 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002002
Ian Rogersd418eda2012-01-30 12:14:28 -08002003 // type index from dex file
2004 // TODO: really 16bits
2005 uint32_t dex_type_idx_;
2006
Brian Carlstromdbc05252011-09-09 01:59:59 -07002007 // number of instance fields that are object refs
2008 size_t num_reference_instance_fields_;
2009
2010 // number of static fields that are object refs
2011 size_t num_reference_static_fields_;
2012
2013 // Total object size; used when allocating storage on gc heap.
2014 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04002015 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002016 size_t object_size_;
2017
Ian Rogersd418eda2012-01-30 12:14:28 -08002018 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002019 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002020
2021 // Bitmap of offsets of ifields.
2022 uint32_t reference_instance_offsets_;
2023
2024 // Bitmap of offsets of sfields.
2025 uint32_t reference_static_offsets_;
2026
Brian Carlstrom693267a2011-09-06 09:25:34 -07002027 // state of class initialization
2028 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002029
Brian Carlstrom693267a2011-09-06 09:25:34 -07002030 // TODO: ?
2031 // initiating class loader list
2032 // NOTE: for classes with low serialNumber, these are unused, and the
2033 // values are kept in a table in gDvm.
2034 // InitiatingLoaderList initiating_loader_list_;
2035
Brian Carlstrom4873d462011-08-21 15:23:39 -07002036 // Location of first static field.
2037 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002038
Brian Carlstrom693267a2011-09-06 09:25:34 -07002039 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002040 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002041};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002042
Elliott Hughes1f359b02011-07-17 14:27:17 -07002043std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002044
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002045inline void Object::SetClass(Class* new_klass) {
2046 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002047 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002048}
2049
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002050inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002051 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002052 DCHECK(GetClass() != NULL);
2053 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002054}
2055
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002056inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002057 Class* java_lang_Class = GetClass()->GetClass();
2058 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002059}
2060
2061inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002062 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002063}
2064
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002065template<class T>
2066inline ObjectArray<T>* Object::AsObjectArray() {
2067 DCHECK(IsObjectArray());
2068 return down_cast<ObjectArray<T>*>(this);
2069}
2070
2071template<class T>
2072inline const ObjectArray<T>* Object::AsObjectArray() const {
2073 DCHECK(IsObjectArray());
2074 return down_cast<const ObjectArray<T>*>(this);
2075}
2076
Brian Carlstromb63ec392011-08-27 17:38:27 -07002077inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002078 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002079}
2080
Brian Carlstroma663ea52011-08-19 23:33:41 -07002081inline bool Object::IsField() const {
2082 Class* java_lang_Class = klass_->klass_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002083 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002084 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002085}
2086
2087inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002088 Class* c = GetClass();
2089 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002090}
2091
2092inline bool Object::IsReferenceInstance() const {
2093 return GetClass()->IsReferenceClass();
2094}
2095
2096inline bool Object::IsWeakReferenceInstance() const {
2097 return GetClass()->IsWeakReferenceClass();
2098}
2099
2100inline bool Object::IsSoftReferenceInstance() const {
2101 return GetClass()->IsSoftReferenceClass();
2102}
2103
2104inline bool Object::IsFinalizerReferenceInstance() const {
2105 return GetClass()->IsFinalizerReferenceClass();
2106}
2107
2108inline bool Object::IsPhantomReferenceInstance() const {
2109 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002110}
2111
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002112inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002113 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002114 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002115 result = AsArray()->SizeOf();
2116 } else if (IsClass()) {
2117 result = AsClass()->SizeOf();
2118 } else {
2119 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002120 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002121 DCHECK(!IsField() || result == sizeof(Field));
2122 DCHECK(!IsMethod() || result == sizeof(Method));
2123 return result;
2124}
2125
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002126inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002127 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002128 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002129 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002130 return result;
2131}
2132
2133inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002134 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002135}
2136
2137inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002138 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08002139 DCHECK(result != NULL) << this;
2140 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002141 return result;
2142}
2143
2144inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002145 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002146}
2147
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002148inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002149 // This is safe from overflow because the array was already allocated, so we know it's sane.
Ian Rogersa15e67d2012-02-28 13:51:55 -08002150 size_t component_size = GetClass()->GetComponentSize();
2151 int32_t component_count = GetLength();
2152 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
2153 size_t data_size = component_count * component_size;
2154 return header_size + data_size;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002155}
2156
2157template<class T>
2158void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002159 if (LIKELY(IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002160 if (object != NULL) {
2161 Class* element_class = GetClass()->GetComponentType();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002162 if (UNLIKELY(!object->InstanceOf(element_class))) {
Elliott Hughes80609252011-09-23 17:24:51 -07002163 ThrowArrayStoreException(object);
2164 return;
2165 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002166 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08002167 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002168 SetFieldObject(data_offset, object, false);
2169 }
2170}
2171
2172template<class T>
2173void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2174 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002175 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002176 SetFieldObject(data_offset, object, false);
2177}
2178
2179template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002180T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2181 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002182 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers5d76c432011-10-31 21:42:49 -07002183 return GetFieldObject<T*>(data_offset, false);
2184}
2185
2186template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002187void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2188 ObjectArray<T>* dst, int dst_pos,
2189 size_t length) {
2190 if (src->IsValidIndex(src_pos) &&
2191 src->IsValidIndex(src_pos+length-1) &&
2192 dst->IsValidIndex(dst_pos) &&
2193 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002194 MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*));
2195 MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002196 Class* array_class = dst->GetClass();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002197 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002198 if (array_class == src->GetClass()) {
2199 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002200 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002201 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002202 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002203 // directly set field, we do a bulk write barrier at the end
2204 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002205 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2206 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2207 }
2208 } else {
2209 Class* element_class = array_class->GetComponentType();
2210 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002211 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002212 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002213 if (object != NULL && !object->InstanceOf(element_class)) {
2214 dst->ThrowArrayStoreException(object);
2215 return;
2216 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002217 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002218 // directly set field, we do a bulk write barrier at the end
2219 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002220 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2221 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2222 }
2223 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002224 heap->WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002225 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002226}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002227
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002228class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002229 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002230 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002231 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002232 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002233 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2234};
2235
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002236class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002237 private:
2238 CharArray* ASCII_;
2239 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002240 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002241 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002242 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002243 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2244};
2245
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002246class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002247 private:
2248 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002249 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002250 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2251};
2252
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002253class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002254 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002255 Object* ORDER_BY_SIGNATURE_;
2256 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002257 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2258};
2259
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002260template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002261class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002262 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002263 typedef T ElementType;
2264
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002265 static PrimitiveArray<T>* Alloc(size_t length)
2266 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002267
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002268 const T* GetData() const {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002269 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2270 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002271 }
2272
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002273 T* GetData() {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002274 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2275 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002276 }
2277
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002278 T Get(int32_t i) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes289da822011-08-16 10:11:20 -07002279 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002280 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002281 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002282 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002283 }
2284
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002285 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes289da822011-08-16 10:11:20 -07002286 // TODO: ArrayStoreException
2287 if (IsValidIndex(i)) {
2288 GetData()[i] = value;
2289 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002290 }
2291
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002292 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002293 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002294 CHECK(array_class != NULL);
2295 array_class_ = array_class;
2296 }
2297
Brian Carlstroma663ea52011-08-19 23:33:41 -07002298 static void ResetArrayClass() {
2299 CHECK(array_class_ != NULL);
2300 array_class_ = NULL;
2301 }
2302
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002303 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002304 static Class* array_class_;
2305
Carl Shapirof88c9522011-08-06 15:47:38 -07002306 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002307};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002308
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002309// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002310class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002311 public:
buzbeefc9e6fa2012-03-23 15:14:29 -07002312 static MemberOffset CountOffset() {
2313 return OFFSET_OF_OBJECT_MEMBER(String, count_);
2314 }
2315
2316 static MemberOffset ValueOffset() {
2317 return OFFSET_OF_OBJECT_MEMBER(String, array_);
2318 }
2319
2320 static MemberOffset OffsetOffset() {
2321 return OFFSET_OF_OBJECT_MEMBER(String, offset_);
2322 }
2323
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002324 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002325 const CharArray* result = GetFieldObject<const CharArray*>(
buzbeefc9e6fa2012-03-23 15:14:29 -07002326 ValueOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002327 DCHECK(result != NULL);
2328 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002329 }
2330
Elliott Hughes814e4032011-08-23 12:07:56 -07002331 int32_t GetOffset() const {
buzbeefc9e6fa2012-03-23 15:14:29 -07002332 int32_t result = GetField32(OffsetOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002333 DCHECK_LE(0, result);
2334 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002335 }
2336
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002337 int32_t GetLength() const;
2338
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002339 int32_t GetHashCode() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002340
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002341 void ComputeHashCode() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002342 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002343 }
2344
Elliott Hughes814e4032011-08-23 12:07:56 -07002345 int32_t GetUtfLength() const {
jeffhao0ce13152012-03-27 19:45:50 -07002346 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002347 }
2348
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002349 uint16_t CharAt(int32_t index) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002350
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002351 String* Intern() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002352
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002353 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002354 const uint16_t* utf16_data_in,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002355 int32_t hash_code = 0)
2356 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002357
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002358 static String* AllocFromModifiedUtf8(const char* utf)
2359 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002360
Jesse Wilson8989d992011-08-02 13:39:42 -07002361 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002362 const char* utf8_data_in)
2363 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002364
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002365 static String* Alloc(Class* java_lang_String, int32_t utf16_length)
2366 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002367
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002368 static String* Alloc(Class* java_lang_String, CharArray* array)
2369 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002370
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002371 bool Equals(const char* modified_utf8) const
2372 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002373
2374 // TODO: do we need this overload? give it a more intention-revealing name.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002375 bool Equals(const StringPiece& modified_utf8) const
2376 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002377
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002378 bool Equals(const String* that) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002379
Ian Rogers0571d352011-11-03 19:51:38 -07002380 // Compare UTF-16 code point values not in a locale-sensitive manner
2381 int Compare(int32_t utf16_length, const char* utf8_data_in);
2382
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002383 // TODO: do we need this overload? give it a more intention-revealing name.
2384 bool Equals(const uint16_t* that_chars, int32_t that_offset,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002385 int32_t that_length) const
2386 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002387
2388 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2389 std::string ToModifiedUtf8() const;
2390
2391 static Class* GetJavaLangString() {
2392 DCHECK(java_lang_String_ != NULL);
2393 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002394 }
2395
Brian Carlstroma663ea52011-08-19 23:33:41 -07002396 static void SetClass(Class* java_lang_String);
2397 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002398
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002399 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002400 void SetHashCode(int32_t new_hash_code) {
2401 DCHECK_EQ(0u,
2402 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2403 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2404 new_hash_code, false);
2405 }
2406
2407 void SetCount(int32_t new_count) {
2408 DCHECK_LE(0, new_count);
2409 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2410 }
2411
2412 void SetOffset(int32_t new_offset) {
2413 DCHECK_LE(0, new_offset);
2414 DCHECK_GE(GetLength(), new_offset);
2415 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2416 }
2417
2418 void SetArray(CharArray* new_array) {
2419 DCHECK(new_array != NULL);
2420 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2421 }
2422
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002423 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2424 CharArray* array_;
2425
Brian Carlstromdbc05252011-09-09 01:59:59 -07002426 int32_t count_;
2427
Carl Shapirof88c9522011-08-06 15:47:38 -07002428 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002429
Elliott Hughes289da822011-08-16 10:11:20 -07002430 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002431
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002432 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002433
Brian Carlstrom693267a2011-09-06 09:25:34 -07002434 friend struct StringOffsets; // for verifying offset information
jeffhao0ce13152012-03-27 19:45:50 -07002435 FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002436 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002437};
2438
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002439// TODO: remove? only used in a unit test of itself.
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002440struct StringHashCode {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08002441 int32_t operator()(String* string) const {
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002442 return string->GetHashCode();
2443 }
2444};
2445
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002446inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002447 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002448 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2449}
2450
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002451inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002452 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002453 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002454}
2455
2456inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002457 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002458 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002459}
2460
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002461inline uint32_t Class::GetAccessFlags() const {
2462 // Check class is loaded or this is java.lang.String that has a
2463 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002464 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002465 this == String::GetJavaLangString() ||
2466 this == Field::GetJavaLangReflectField() ||
2467 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002468 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002469 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2470}
2471
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002472inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002473 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002474 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2475}
2476
2477inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002478 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002479 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002480}
2481
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002482inline uint32_t Method::GetDexMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002483 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002484 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002485}
2486
Elliott Hughes76e36942012-03-16 13:44:56 -07002487inline void Method::AssertPcIsWithinCode(uintptr_t pc) const {
Elliott Hughes67d92002012-03-26 15:08:51 -07002488 if (!kIsDebugBuild) {
2489 return;
2490 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002491 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
2492 return;
2493 }
2494 Runtime* runtime = Runtime::Current();
2495 if (GetCode() == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
2496 return;
2497 }
2498 DCHECK(IsWithinCode(pc))
2499 << PrettyMethod(this)
2500 << " pc=" << std::hex << pc
2501 << " code=" << GetCode()
2502 << " size=" << GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002503}
2504
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002505inline String* Class::GetName() const {
2506 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002507}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002508inline void Class::SetName(String* name) {
2509 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002510}
2511
2512// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002513class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002514 public:
2515 void SetDetailMessage(String* new_detail_message) {
2516 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2517 new_detail_message, false);
2518 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002519 String* GetDetailMessage() const {
2520 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), false);
2521 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002522 std::string Dump() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002523
Ian Rogers1c5eb702012-02-01 09:18:34 -08002524 // This is a runtime version of initCause, you shouldn't use it if initCause may have been
2525 // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
2526 // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
2527 void SetCause(Throwable* cause);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002528 bool IsCheckedException() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers5167c972012-02-03 10:41:20 -08002529
2530 static Class* GetJavaLangThrowable() {
2531 DCHECK(java_lang_Throwable_ != NULL);
2532 return java_lang_Throwable_;
2533 }
2534
2535 static void SetClass(Class* java_lang_Throwable);
2536 static void ResetClass();
2537
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002538 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002539 Object* GetStackState() const {
2540 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2541 }
2542
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002543 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2544 Throwable* cause_;
2545 String* detail_message_;
2546 Object* stack_state_; // Note this is Java volatile:
2547 Object* stack_trace_;
2548 Object* suppressed_exceptions_;
2549
Ian Rogers5167c972012-02-03 10:41:20 -08002550 static Class* java_lang_Throwable_;
2551
Brian Carlstrom693267a2011-09-06 09:25:34 -07002552 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002553 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2554};
2555
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002556// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002557class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002558 public:
2559 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002560 return GetFieldObject<const String*>(
2561 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002562 }
2563
2564 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002565 return GetFieldObject<const String*>(
2566 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002567 }
2568
2569 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002570 return GetFieldObject<const String*>(
2571 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002572 }
2573
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002574 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002575 return GetField32(
2576 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002577 }
2578
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002579 static StackTraceElement* Alloc(String* declaring_class,
2580 String* method_name,
2581 String* file_name,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002582 int32_t line_number)
2583 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002584
2585 static void SetClass(Class* java_lang_StackTraceElement);
2586
2587 static void ResetClass();
2588
2589 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002590 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002591 String* declaring_class_;
2592 String* file_name_;
2593 String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002594 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002595
2596 static Class* GetStackTraceElement() {
2597 DCHECK(java_lang_StackTraceElement_ != NULL);
2598 return java_lang_StackTraceElement_;
2599 }
2600
2601 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002602
2603 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002604 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2605};
2606
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002607class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002608 public:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002609 Class* GetInterface() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002610 Class* interface = Get(kInterface)->AsClass();
2611 DCHECK(interface != NULL);
2612 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002613 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002614
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002615 void SetInterface(Class* interface) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002616 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002617 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002618 DCHECK(Get(kInterface) == NULL);
2619 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002620 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002621
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002622 size_t GetMethodArrayCount() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002623 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002624 if (method_array == NULL) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002625 return 0;
2626 }
2627 return method_array->GetLength();
2628 }
2629
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002630 ObjectArray<Method>* GetMethodArray() const
2631 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002632 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2633 DCHECK(method_array != NULL);
2634 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002635 }
2636
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002637 void SetMethodArray(ObjectArray<Method>* new_ma)
2638 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002639 DCHECK(new_ma != NULL);
2640 DCHECK(Get(kMethodArray) == NULL);
2641 Set(kMethodArray, new_ma);
2642 }
2643
2644 static size_t LengthAsArray() {
2645 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002646 }
2647
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002648 private:
Elliott Hughes48257562012-06-06 17:42:44 -07002649 enum {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002650 // Points to the interface class.
2651 kInterface = 0,
2652 // Method pointers into the vtable, allow fast map from interface
2653 // method index to concrete instance method.
2654 kMethodArray = 1,
2655 kMax = 2,
2656 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002657
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002658 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002659};
2660
Ian Rogersc2b44472011-12-14 21:17:17 -08002661class MANAGED SynthesizedProxyClass : public Class {
2662 public:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002663 ObjectArray<Class>* GetInterfaces() {
2664 return interfaces_;
2665 }
2666
Ian Rogersc2b44472011-12-14 21:17:17 -08002667 ObjectArray<ObjectArray<Class> >* GetThrows() {
2668 return throws_;
2669 }
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002670
Jesse Wilson95caa792011-10-12 18:14:17 -04002671 private:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002672 ObjectArray<Class>* interfaces_;
Ian Rogersc2b44472011-12-14 21:17:17 -08002673 ObjectArray<ObjectArray<Class> >* throws_;
2674 DISALLOW_IMPLICIT_CONSTRUCTORS(SynthesizedProxyClass);
Jesse Wilson95caa792011-10-12 18:14:17 -04002675};
2676
2677class MANAGED Proxy : public Object {
2678 private:
2679 Object* h_;
2680
2681 friend struct ProxyOffsets; // for verifying offset information
2682 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2683};
2684
Carl Shapiro1fb86202011-06-27 17:43:13 -07002685} // namespace art
2686
2687#endif // ART_SRC_OBJECT_H_