blob: 5a47c46d73353b9ea68a247e3a38cddbaba19908 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OBJECT_H_
4#define ART_SRC_OBJECT_H_
5
Brian Carlstrom1f870082011-08-23 16:02:11 -07006#include <vector>
7
Elliott Hughes90a33692011-08-30 13:27:07 -07008#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070010#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070012#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "logging.h"
14#include "macros.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070015#include "monitor.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070016#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "offsets.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070018#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070021#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070022
23namespace art {
24
25class Array;
26class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070027class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070028class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040030class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070031class InterfaceEntry;
32class Monitor;
33class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070034class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070035class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040036class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070037template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070038template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070039typedef PrimitiveArray<uint8_t> BooleanArray;
40typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070041typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070042typedef PrimitiveArray<double> DoubleArray;
43typedef PrimitiveArray<float> FloatArray;
44typedef PrimitiveArray<int32_t> IntArray;
45typedef PrimitiveArray<int64_t> LongArray;
46typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070047
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048union JValue {
49 uint8_t z;
50 int8_t b;
51 uint16_t c;
52 int16_t s;
53 int32_t i;
54 int64_t j;
55 float f;
56 double d;
57 Object* l;
58};
59
Brian Carlstrombe977852011-07-19 14:54:54 -070060static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
61static const uint32_t kAccPrivate = 0x0002; // field, method, ic
62static const uint32_t kAccProtected = 0x0004; // field, method, ic
63static const uint32_t kAccStatic = 0x0008; // field, method, ic
64static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
65static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
66static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
67static const uint32_t kAccVolatile = 0x0040; // field
68static const uint32_t kAccBridge = 0x0040; // method (1.5)
69static const uint32_t kAccTransient = 0x0080; // field
70static const uint32_t kAccVarargs = 0x0080; // method (1.5)
71static const uint32_t kAccNative = 0x0100; // method
72static const uint32_t kAccInterface = 0x0200; // class, ic
73static const uint32_t kAccAbstract = 0x0400; // class, method, ic
74static const uint32_t kAccStrict = 0x0800; // method
75static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
76static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
77static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070078
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070079static const uint32_t kAccMiranda = 0x8000; // method
80
Brian Carlstroma331b3c2011-07-18 17:47:56 -070081static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
82
Brian Carlstrombe977852011-07-19 14:54:54 -070083static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
84static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070085
Brian Carlstrom1f870082011-08-23 16:02:11 -070086static const uint32_t kAccClassFlagsMask = (kAccPublic
87 | kAccFinal
88 | kAccInterface
89 | kAccAbstract
90 | kAccSynthetic
91 | kAccAnnotation
92 | kAccEnum);
93static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
94 | kAccPrivate
95 | kAccProtected
96 | kAccStatic);
97static const uint32_t kAccFieldFlagsMask = (kAccPublic
98 | kAccPrivate
99 | kAccProtected
100 | kAccStatic
101 | kAccFinal
102 | kAccVolatile
103 | kAccTransient
104 | kAccSynthetic
105 | kAccEnum);
106static const uint32_t kAccMethodFlagsMask = (kAccPublic
107 | kAccPrivate
108 | kAccProtected
109 | kAccStatic
110 | kAccFinal
111 | kAccSynchronized
112 | kAccBridge
113 | kAccVarargs
114 | kAccNative
115 | kAccAbstract
116 | kAccStrict
117 | kAccSynthetic
118 | kAccConstructor
119 | kAccDeclaredSynchronized);
120
121// if only kAccClassIsReference is set, we have a soft reference
122static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref
123static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference
124static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference
125static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference
126
127static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
128 | kAccClassIsWeakReference
129 | kAccClassIsFinalizerReference
130 | kAccClassIsPhantomReference);
131
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700132/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700133 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134 */
135/*
136 * A magic value for refOffsets. Ignore the bits and walk the super
137 * chain when this is the value.
138 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
139 * fields followed by 2 ref instance fields.]
140 */
141#define CLASS_WALK_SUPER ((unsigned int)(3))
142#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
143#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
144#define CLASS_OFFSET_ALIGNMENT 4
145#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
146/*
147 * Given an offset, return the bit number which would encode that offset.
148 * Local use only.
149 */
150#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
151 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
152 CLASS_OFFSET_ALIGNMENT)
153/*
154 * Is the given offset too large to be encoded?
155 */
156#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
157 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
158/*
159 * Return a single bit, encoding the offset.
160 * Undefined if the offset is too large, as defined above.
161 */
162#define CLASS_BIT_FROM_OFFSET(byteOffset) \
163 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
164/*
165 * Return an offset, given a bit number as returned from CLZ.
166 */
167#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700168 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + \
169 CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700170
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700171#define OFFSET_OF_OBJECT_MEMBER(type, field) \
172 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700173
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700174// C++ mirror of java.lang.Object
Carl Shapiro1fb86202011-06-27 17:43:13 -0700175class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700176 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700177 static bool InstanceOf(const Object* object, const Class* klass) {
178 if (object == NULL) {
179 return false;
180 }
181 return object->InstanceOf(klass);
182 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700183
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700184 static MemberOffset ClassOffset() {
185 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700186 }
187
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700188 Class* GetClass() const {
189 return
190 GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
191 }
192
193 void SetClass(Class* new_klass);
194
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700195 bool InstanceOf(const Class* klass) const;
196
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700197 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700198
Elliott Hughesbf86d042011-08-31 17:53:14 -0700199 Object* Clone() {
200 UNIMPLEMENTED(FATAL);
201 return NULL;
202 }
203
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 static MemberOffset MonitorOffset() {
205 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
206 }
207
208 Monitor* GetMonitor() const {
209 return GetFieldPtr<Monitor*>(
210 OFFSET_OF_OBJECT_MEMBER(Object, monitor_), false);
211 }
212
213 void SetMonitor(Monitor* monitor) {
214 // TODO: threading - compare-and-set
215 SetFieldPtr(OFFSET_OF_OBJECT_MEMBER(Object, monitor_), monitor, false);
216 }
217
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700218 void MonitorEnter() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 GetMonitor()->Enter();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220 }
221
222 void MonitorExit() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 GetMonitor()->Exit();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700224 }
225
226 void Notify() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 GetMonitor()->Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700228 }
229
230 void NotifyAll() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 GetMonitor()->NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232 }
233
234 void Wait() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700235 GetMonitor()->Wait();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236 }
237
238 void Wait(int64_t timeout) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700239 GetMonitor()->Wait(timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700240 }
241
242 void Wait(int64_t timeout, int32_t nanos) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 GetMonitor()->Wait(timeout, nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700244 }
245
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700246 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247
248 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700249 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700250 return down_cast<Class*>(this);
251 }
252
253 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700254 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700255 return down_cast<const Class*>(this);
256 }
257
Brian Carlstrom4873d462011-08-21 15:23:39 -0700258 bool IsClassClass() const;
259
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700260 bool IsObjectArray() const;
261
262 template<class T>
263 ObjectArray<T>* AsObjectArray() {
264 DCHECK(IsObjectArray());
265 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700266 }
267
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700268 template<class T>
269 const ObjectArray<T>* AsObjectArray() const {
270 DCHECK(IsObjectArray());
271 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700272 }
273
Brian Carlstromb63ec392011-08-27 17:38:27 -0700274 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700275
276 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700277 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700278 return down_cast<Array*>(this);
279 }
280
281 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700282 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700283 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700284 }
285
Brian Carlstroma663ea52011-08-19 23:33:41 -0700286 bool IsString() const;
287
288 String* AsString() {
289 DCHECK(IsString());
290 return down_cast<String*>(this);
291 }
292
293 bool IsMethod() const;
294
295 Method* AsMethod() {
296 DCHECK(IsMethod());
297 return down_cast<Method*>(this);
298 }
299
Brian Carlstrom4873d462011-08-21 15:23:39 -0700300 const Method* AsMethod() const {
301 DCHECK(IsMethod());
302 return down_cast<const Method*>(this);
303 }
304
Brian Carlstroma663ea52011-08-19 23:33:41 -0700305 bool IsField() const;
306
307 Field* AsField() {
308 DCHECK(IsField());
309 return down_cast<Field*>(this);
310 }
311
Brian Carlstrom4873d462011-08-21 15:23:39 -0700312 const Field* AsField() const {
313 DCHECK(IsField());
314 return down_cast<const Field*>(this);
315 }
316
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700317 bool IsReferenceInstance() const;
318
319 bool IsWeakReferenceInstance() const;
320
321 bool IsSoftReferenceInstance() const;
322
323 bool IsFinalizerReferenceInstance() const;
324
325 bool IsPhantomReferenceInstance() const;
326
327 // Accessors for Java type fields
328 template<class T>
329 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
330 Heap::VerifyObject(this);
331 DCHECK(Thread::Current() == NULL ||
332 Thread::Current()->CanAccessDirectReferences());
333 const byte* raw_addr = reinterpret_cast<const byte*>(this) +
334 field_offset.Int32Value();
335 if (is_volatile) {
336 UNIMPLEMENTED(WARNING);
337 }
338 T result = *reinterpret_cast<T const *>(raw_addr);
339 Heap::VerifyObject(result);
340 return result;
341 }
342
343 void SetFieldObject(MemberOffset offset, const Object* new_value,
344 bool is_volatile) {
345 // Avoid verifying this when initializing the Class*
346 if (offset.Int32Value() != ClassOffset().Int32Value()) {
347 Heap::VerifyObject(this);
348 }
349 Heap::VerifyObject(new_value);
350 byte* raw_addr = reinterpret_cast<byte*>(this) + offset.Int32Value();
351 if (is_volatile) {
352 UNIMPLEMENTED(WARNING);
353 }
354 *reinterpret_cast<const Object**>(raw_addr) = new_value;
355 // TODO: write barrier
356 }
357
358 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
359 Heap::VerifyObject(this);
360 const byte* raw_addr = reinterpret_cast<const byte*>(this) +
361 field_offset.Int32Value();
362 if (is_volatile) {
363 UNIMPLEMENTED(WARNING);
364 }
365 return *reinterpret_cast<const uint32_t*>(raw_addr);
366 }
367
368 void SetField32(MemberOffset offset, uint32_t new_value, bool is_volatile) {
369 Heap::VerifyObject(this);
370 byte* raw_addr = reinterpret_cast<byte*>(this) + offset.Int32Value();
371 if (is_volatile) {
372 UNIMPLEMENTED(WARNING);
373 }
374 *reinterpret_cast<uint32_t*>(raw_addr) = new_value;
375 }
376
377 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
378 Heap::VerifyObject(this);
379 const byte* raw_addr = reinterpret_cast<const byte*>(this) +
380 field_offset.Int32Value();
381 if (is_volatile) {
382 UNIMPLEMENTED(WARNING);
383 }
384 return *reinterpret_cast<const uint64_t*>(raw_addr);
385 }
386
387 void SetField64(MemberOffset offset, uint64_t new_value,
388 bool is_volatile = false) {
389 Heap::VerifyObject(this);
390 byte* raw_addr = reinterpret_cast<byte*>(this) + offset.Int32Value();
391 if (is_volatile) {
392 UNIMPLEMENTED(WARNING);
393 }
394 *reinterpret_cast<uint64_t*>(raw_addr) = new_value;
395 }
396
397 protected:
398 // Accessors for non-Java type fields
399 uint16_t GetField16(MemberOffset field_offset, bool is_volatile) const {
400 Heap::VerifyObject(this);
401 const byte* raw_addr = reinterpret_cast<const byte*>(this) +
402 field_offset.Int32Value();
403 if (is_volatile) {
404 UNIMPLEMENTED(WARNING);
405 }
406 return *reinterpret_cast<const uint16_t*>(raw_addr);
407 }
408
409 void SetField16(MemberOffset offset, uint16_t new_value, bool is_volatile) {
410 Heap::VerifyObject(this);
411 byte* raw_addr = reinterpret_cast<byte*>(this) + offset.Int32Value();
412 if (is_volatile) {
413 UNIMPLEMENTED(WARNING);
414 }
415 *reinterpret_cast<uint16_t*>(raw_addr) = new_value;
416 }
417
418 template<class T>
419 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
420 Heap::VerifyObject(this);
421 const byte* raw_addr = reinterpret_cast<const byte*>(this) +
422 field_offset.Int32Value();
423 if (is_volatile) {
424 UNIMPLEMENTED(WARNING);
425 }
426 return *reinterpret_cast<T const *>(raw_addr);
427 }
428
429 template<typename T>
430 void SetFieldPtr(MemberOffset offset, T new_value, bool is_volatile) {
431 Heap::VerifyObject(this);
432 byte* raw_addr = reinterpret_cast<byte*>(this) + offset.Int32Value();
433 if (is_volatile) {
434 UNIMPLEMENTED(WARNING);
435 }
436 *reinterpret_cast<T*>(raw_addr) = new_value;
437 }
438
439 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700440 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700441
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700442 Monitor* monitor_;
443
Carl Shapirof88c9522011-08-06 15:47:38 -0700444 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700445};
446
447class ObjectLock {
448 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700449 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700450 CHECK(object != NULL);
451 obj_->MonitorEnter();
452 }
453
454 ~ObjectLock() {
455 obj_->MonitorExit();
456 }
457
458 void Wait(int64_t millis = 0) {
459 return obj_->Wait(millis);
460 }
461
462 void Notify() {
463 obj_->Notify();
464 }
465
466 void NotifyAll() {
467 obj_->NotifyAll();
468 }
469
470 private:
471 Object* obj_;
472 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700473};
474
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700475// C++ mirror of java.lang.reflect.AccessibleObject
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400476class AccessibleObject : public Object {
477 private:
478 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700479 uint32_t java_flag_; // can accessibility checks be bypassed
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400480};
481
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700482// C++ mirror of java.lang.reflect.Field
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400483class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700484 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700485 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700486
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700487 void SetDeclaringClass(Class *new_declaring_class);
488
489 const String* GetName() const;
490
491 void SetName(String* new_name);
492
493 uint32_t GetAccessFlags() const;
494
495 void SetAccessFlags(uint32_t new_access_flags) {
496 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags,
497 false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700498 }
499
500 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700501 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700502 }
503
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700504 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700505
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700506 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700507
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700508 // Gets type using type index and resolved types in the dex cache, may be null
509 // if type isn't yet resolved
510 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700511
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700512 // Performs full resolution, may return null and set exceptions if type cannot
513 // be resolved
514 Class* GetType() const;
515
516 // Offset to field within an Object
517 MemberOffset GetOffset() const;
518
519 MemberOffset GetOffsetDuringLinking() const;
520
521 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700522
Brian Carlstrom4873d462011-08-21 15:23:39 -0700523 // field access, null object for static fields
524 bool GetBoolean(const Object* object) const;
525 void SetBoolean(Object* object, bool z) const;
526 int8_t GetByte(const Object* object) const;
527 void SetByte(Object* object, int8_t b) const;
528 uint16_t GetChar(const Object* object) const;
529 void SetChar(Object* object, uint16_t c) const;
530 uint16_t GetShort(const Object* object) const;
531 void SetShort(Object* object, uint16_t s) const;
532 int32_t GetInt(const Object* object) const;
533 void SetInt(Object* object, int32_t i) const;
534 int64_t GetLong(const Object* object) const;
535 void SetLong(Object* object, int64_t j) const;
536 float GetFloat(const Object* object) const;
537 void SetFloat(Object* object, float f) const;
538 double GetDouble(const Object* object) const;
539 void SetDouble(Object* object, double d) const;
540 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700541 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700542
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700543 // Slow path routines for static field access when field was unresolved at
544 // compile time
545 static uint32_t Get32StaticFromCode(uint32_t field_idx,
546 const Method* referrer);
547 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer,
548 uint32_t new_value);
549 static uint64_t Get64StaticFromCode(uint32_t field_idx,
550 const Method* referrer);
551 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer,
552 uint64_t new_value);
553 static Object* GetObjStaticFromCode(uint32_t field_idx,
554 const Method* referrer);
555 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
556 Object* new_value);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700557
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700558 static Class* GetJavaLangReflectField() {
559 DCHECK(java_lang_reflect_Field_ != NULL);
560 return java_lang_reflect_Field_;
561 }
562
563 static void SetClass(Class* java_lang_reflect_Field);
564 static void ResetClass();
565
566 private:
567 bool IsVolatile() const {
568 return (GetAccessFlags() & kAccVolatile) != 0;
569 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700570
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700571 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700572 uint32_t Get32(const Object* object) const;
573 void Set32(Object* object, uint32_t new_value) const;
574 uint64_t Get64(const Object* object) const;
575 void Set64(Object* object, uint64_t new_value) const;
576 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700577 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700578
Jesse Wilson35baaab2011-08-10 16:18:03 -0400579 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
580 // The class in which this field is declared.
581 Class* declaring_class_;
582 Object* generic_type_;
583 uint32_t generic_types_are_initialized_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700584 const String* name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700585 // Offset of field within an instance or in the Class' static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -0400586 uint32_t offset_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700587 // Type of the field
588 // TODO: unused by ART (which uses the type_idx below), remove
Jesse Wilson35baaab2011-08-10 16:18:03 -0400589 Class* type_;
590
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700591 // TODO: expose these fields in the Java version of this Object
Jesse Wilson35baaab2011-08-10 16:18:03 -0400592 uint32_t access_flags_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700593 // Dex cache index of resolved type
594 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400595
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700596 static Class* java_lang_reflect_Field_;
597
Jesse Wilson35baaab2011-08-10 16:18:03 -0400598 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700599};
600
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700601// C++ mirror of java.lang.reflect.Method
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400602class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700603 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700604 // An function that invokes a method with an array of its arguments.
605 typedef void InvokeStub(Method* method,
606 Object* obj,
607 Thread* thread,
608 byte* args,
609 JValue* result);
610
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700611 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700612
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700613 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700614
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700615 static MemberOffset DeclaringClassOffset() {
616 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700617 }
618
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700619 // Returns the method name, e.g. "<init>" or "eatLunch"
620 const String* GetName() const;
621
622 void SetName(String* new_name);
623
624 const char* GetShorty() const;
625
626 void SetShorty(const char* new_shorty) {
627 DCHECK(NULL == GetFieldPtr<const char*>(
628 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
629 DCHECK_LE(1u, strlen(new_shorty));
630 SetFieldPtr(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
631 }
632
633 const String* GetSignature() const;
634
635 void SetSignature(String* new_signature);
636
637 bool HasSameNameAndDescriptor(const Method* that) const;
638
639 uint32_t GetAccessFlags() const;
640
641 void SetAccessFlags(uint32_t new_access_flags) {
642 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags,
643 false);
644 }
645
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700646 // Returns true if the method is declared public.
647 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700648 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700649 }
650
651 // Returns true if the method is declared private.
652 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700653 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700654 }
655
656 // Returns true if the method is declared static.
657 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700658 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700659 }
660
Brian Carlstrom1f870082011-08-23 16:02:11 -0700661 // Returns true if the method is a constructor.
662 bool IsConstructor() const {
663 return (access_flags_ & kAccConstructor) != 0;
664 }
665
666 // Returns true if the method is static, private, or a constructor.
667 bool IsDirect() const {
668 return IsStatic() || IsPrivate() || IsConstructor();
669 }
670
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700671 // Returns true if the method is declared synchronized.
672 bool IsSynchronized() const {
673 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700674 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700675 }
676
677 // Returns true if the method is declared final.
678 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700679 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700680 }
681
682 // Returns true if the method is declared native.
683 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700684 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700685 }
686
687 // Returns true if the method is declared abstract.
688 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700689 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700690 }
691
692 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700693 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700694 }
695
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700696 uint16_t GetMethodIndex() const;
697
698 size_t GetVtableIndex() const {
699 return GetMethodIndex();
700 }
701
702 void SetMethodIndex(uint16_t new_method_index) {
703 SetField16(OFFSET_OF_OBJECT_MEMBER(Method, method_index_),
704 new_method_index, false);
705 }
706
707 static MemberOffset MethodIndexOffset() {
708 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
709 }
710
711 uint32_t GetCodeItemOffset() const {
712 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
713 }
714
715 void SetCodeItemOffset(uint32_t new_code_off) {
716 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_),
717 new_code_off, false);
718 }
719
720 // Number of 32bit registers that would be required to hold all the arguments
721 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700722
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700723 // Number of argument bytes required for densely packing the
724 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700725 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700726
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700727 uint16_t NumRegisters() const;
728
729 void SetNumRegisters(uint16_t new_num_registers) {
730 SetField16(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_),
731 new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700732 }
733
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700734 uint16_t NumIns() const;
735
736 void SetNumIns(uint16_t new_num_ins) {
737 SetField16(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_),
738 new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700739 }
740
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700741 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700742
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700743 void SetNumOuts(uint16_t new_num_outs) {
744 SetField16(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_),
745 new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700746 }
747
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700748 uint32_t GetProtoIdx() const;
749
750 void SetProtoIdx(uint32_t new_proto_idx) {
751 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700752 }
753
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700754 ObjectArray<String>* GetDexCacheStrings() const;
755 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
756
757 static MemberOffset DexCacheStringsOffset() {
758 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
759 }
760
761 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
762 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
763
764 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
765 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
766
767 ObjectArray<Field>* GetDexCacheResolvedFields() const;
768 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
769
770 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
771 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
772
773 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
774 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
775
776 void SetReturnTypeIdx(uint32_t new_return_type_idx);
777
778 Class* GetReturnType() const;
779
780 bool IsReturnAReference() const;
781
782 bool IsReturnAFloat() const;
783
784 bool IsReturnADouble() const;
785
Ian Rogersb033c752011-07-20 12:22:35 -0700786 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700787 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700788 }
789
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700790 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700791
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700792 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700793
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700794 // "Args" may refer to any of the 3 levels of "Args."
795 // To avoid confusion, our code will denote which "Args" clearly:
796 // 1. UserArgs: Args that a user see.
797 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
798 // receiver.
799 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
800 // E.g., the first in Args is Method* for both static and non-static
801 // methods. And CConvArgs doesn't deal with the receiver because
802 // receiver is hardwired in an implicit register, so CConvArgs doesn't
803 // need to deal with it.
804 //
805 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700806 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700807 // "1 +" because the first in Args is the receiver.
808 // "- 1" because we don't count the return type.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700809 return (IsStatic() ? 0 : 1) + strlen(GetShorty()) - 1;
Ian Rogersb033c752011-07-20 12:22:35 -0700810 }
811
812 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700813 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700814 size_t NumReferenceArgs() const;
815
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700816 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700817 size_t NumLongOrDoubleArgs() const;
818
819 // The number of reference arguments to this method before the given
820 // parameter index
821 size_t NumReferenceArgsBefore(unsigned int param) const;
822
823 // Is the given method parameter a reference?
824 bool IsParamAReference(unsigned int param) const;
825
826 // Is the given method parameter a long or double?
827 bool IsParamALongOrDouble(unsigned int param) const;
828
Ian Rogersdf20fe02011-07-20 20:34:16 -0700829 // Size in bytes of the given parameter
830 size_t ParamSize(unsigned int param) const;
831
832 // Size in bytes of the return value
833 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700834
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700835 const void* GetCode() const {
836 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700837 }
838
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700839 bool HasCode() const {
840 return GetCode() != NULL;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700841 }
842
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700843 void SetCode(const byte* compiled_code,
844 size_t byte_count,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700845 InstructionSet set);
buzbeec143c552011-08-20 17:38:58 -0700846
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700847 static MemberOffset GetCodeOffset() {
848 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700849 }
850
Shih-wei Liaod11af152011-08-23 16:02:11 -0700851 size_t GetFrameSizeInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700852 DCHECK(sizeof(size_t) == sizeof(uint32_t));
853 size_t result = GetField32(
854 OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
855 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
856 return result;
857 }
858
859 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
860 DCHECK(sizeof(size_t) == sizeof(uint32_t));
861 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
862 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
863 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700864 }
865
Shih-wei Liaod11af152011-08-23 16:02:11 -0700866 size_t GetReturnPcOffsetInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700867 DCHECK(sizeof(size_t) == sizeof(uint32_t));
868 return GetField32(
869 OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700870 }
871
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700872 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
873 DCHECK(sizeof(size_t) == sizeof(uint32_t));
874 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
875 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
876 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700877 }
878
Ian Rogersb033c752011-07-20 12:22:35 -0700879 void RegisterNative(const void* native_method) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700880 CHECK(native_method != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700881 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
882 native_method, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700883 }
884
Elliott Hughes5174fe62011-08-23 15:12:35 -0700885 void UnregisterNative() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700886 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
887 NULL, false);
Elliott Hughes5174fe62011-08-23 15:12:35 -0700888 }
889
Ian Rogersb033c752011-07-20 12:22:35 -0700890 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700891 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700892 }
893
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700894 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700895 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700896 InvokeStub* result = GetFieldPtr<InvokeStub*>(
897 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
898 // TODO: DCHECK(result != NULL); should be ahead of time compiled
899 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700900 }
901
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700902 static MemberOffset GetInvokeStubOffset() {
903 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700904 }
905
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700906 void SetInvokeStub(const InvokeStub* new_invoke_stub) {
907 SetFieldPtr<const InvokeStub*>(
908 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), new_invoke_stub, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700909 }
910
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700911 void SetFpSpillMask(uint32_t fp_spill_mask) {
912 // Computed during compilation
913 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_),
914 fp_spill_mask, false);
915 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700916
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700917 void SetCoreSpillMask(uint32_t core_spill_mask) {
918 // Computed during compilation
919 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_),
920 core_spill_mask, false);
921 }
922
923 // Converts a native PC to a dex PC. TODO: this is a no-op
924 // until we associate a PC mapping table with each method.
925 uintptr_t ToDexPC(const uintptr_t pc) const {
926 return pc;
927 }
928
929 // Converts a dex PC to a native PC. TODO: this is a no-op
930 // until we associate a PC mapping table with each method.
931 uintptr_t ToNativePC(const uintptr_t pc) const {
932 return pc;
933 }
934
935 static Class* GetJavaLangReflectMethod() {
936 DCHECK(java_lang_reflect_Method_ != NULL);
937 return java_lang_reflect_Method_;
938 }
939
940 static void SetClass(Class* java_lang_reflect_Method);
941 static void ResetClass();
942
943 private:
944 uint32_t GetReturnTypeIdx() const;
945
946 // TODO: the image writer should know the offsets of these fields as they
947 // should appear in the libcore Java mirror
948 friend class ImageWriter;
949
950 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
951 // the class we are a part of
952 Class* declaring_class_;
953 ObjectArray<Class>* java_exception_types_;
954 Object* java_formal_type_parameters_;
955 Object* java_generic_exception_types_;
956 Object* java_generic_parameter_types_;
957 Object* java_generic_return_type_;
958 Class* java_return_type_; // Unused by ART
959 String* name_;
960 ObjectArray<Class>* java_parameter_types_;
961 uint32_t java_generic_types_are_initialized_;
962 uint32_t java_slot_;
963
964 // TODO: start of non-Java mirror fields, place these in the Java piece
965
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700966 // access flags; low 16 bits are defined by spec (could be uint16_t?)
967 uint32_t access_flags_;
968
969 // For concrete virtual methods, this is the offset of the method
Brian Carlstrom30b94452011-08-25 21:35:26 -0700970 // in Class::vtable_.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700971 //
972 // For abstract methods in an interface class, this is the offset
Brian Carlstrom30b94452011-08-25 21:35:26 -0700973 // of the method in "iftable_[n]->method_index_array_".
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700974 uint16_t method_index_;
975
976 // Method bounds; not needed for an abstract method.
977 //
978 // For a native method, we compute the size of the argument list, and
979 // set "insSize" and "registerSize" equal to it.
980 uint16_t num_registers_; // ins + locals
981 uint16_t num_outs_;
982 uint16_t num_ins_;
983
buzbeec143c552011-08-20 17:38:58 -0700984 // Total size in bytes of the frame
Shih-wei Liaod11af152011-08-23 16:02:11 -0700985 size_t frame_size_in_bytes_;
buzbeec143c552011-08-20 17:38:58 -0700986
987 // Architecture-dependent register spill masks
988 uint32_t core_spill_mask_;
989 uint32_t fp_spill_mask_;
990
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700991 // The method descriptor. This represents the parameters a method
992 // takes and value it returns. This string is a list of the type
993 // descriptors for the parameters enclosed in parenthesis followed
994 // by the return type descriptor. For example, for the method
995 //
996 // Object mymethod(int i, double d, Thread t)
997 //
998 // the method descriptor would be
999 //
1000 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001001 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001002
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001003 // Method prototype descriptor string (return and argument types).
1004 uint32_t proto_idx_;
1005
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001006 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001007 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001008
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001009 // Index of the return type
1010 uint32_t java_return_type_idx_;
1011
1012 // The short-form method descriptor string. TODO: make String*
1013 const char* shorty_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001014
Ian Rogers762400c2011-08-23 12:14:16 -07001015 // short cuts to declaring_class_->dex_cache_ members for fast compiled code
1016 // access
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001017 ObjectArray<String>* dex_cache_strings_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001018 ObjectArray<Class>* dex_cache_resolved_types_;
1019 ObjectArray<Method>* dex_cache_resolved_methods_;
1020 ObjectArray<Field>* dex_cache_resolved_fields_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001021 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001022 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -07001023
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001024 private:
Ian Rogersb033c752011-07-20 12:22:35 -07001025 // Compiled code associated with this method
Elliott Hughes90a33692011-08-30 13:27:07 -07001026 UniquePtr<MemMap> code_area_;
Brian Carlstrom83db7722011-08-26 17:32:56 -07001027 const void* code_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001028
Brian Carlstrom83db7722011-08-26 17:32:56 -07001029 // Instruction set of the compiled code
buzbeec143c552011-08-20 17:38:58 -07001030 InstructionSet code_instruction_set_;
1031
1032 // Size in bytes of compiled code associated with this method
1033 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -07001034
Ian Rogers762400c2011-08-23 12:14:16 -07001035 // Offset of return PC within frame for compiled code (in bytes)
Shih-wei Liaod11af152011-08-23 16:02:11 -07001036 // Offset of PC within compiled code (in bytes)
1037 size_t return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001038
Ian Rogersb033c752011-07-20 12:22:35 -07001039 // Any native method registered with this method
1040 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001041
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001042 // Native invocation stub entry point.
1043 const InvokeStub* invoke_stub_;
1044
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001045 static Class* java_lang_reflect_Method_;
1046
Carl Shapirof88c9522011-08-06 15:47:38 -07001047 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001048};
1049
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001050class Array : public Object {
1051 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001052 static size_t SizeOf(size_t component_count,
1053 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001054 return sizeof(Array) + component_count * component_size;
1055 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001056
Brian Carlstromb63ec392011-08-27 17:38:27 -07001057 // Given the context of a calling Method, use its DexCache to
1058 // resolve a type to an array Class. If it cannot be resolved, throw
1059 // an error. If it can, use it to create an array.
1060 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
1061
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001062 // A convenience for code that doesn't know the component size,
1063 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001064 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001065
Brian Carlstromb63ec392011-08-27 17:38:27 -07001066 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001067
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001068 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001069
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001070 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001071 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001072 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001073
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001074 void SetLength(int32_t length) {
1075 CHECK_LE(0, length);
1076 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001077 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001078
buzbeec143c552011-08-20 17:38:58 -07001079 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001080 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001081 }
1082
1083 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001084 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001085 }
1086
Elliott Hughesbf86d042011-08-31 17:53:14 -07001087 void* GetRawData() {
1088 return reinterpret_cast<void*>(first_element_);
1089 }
1090
Elliott Hughes289da822011-08-16 10:11:20 -07001091 protected:
1092 bool IsValidIndex(int32_t index) const {
1093 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001094 Thread* self = Thread::Current();
1095 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
1096 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001097 return false;
Elliott Hughes289da822011-08-16 10:11:20 -07001098 }
1099 return true;
1100 }
1101
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001102 private:
1103 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001104 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001105 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1106 int32_t padding_;
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 Carlstrom913af1b2011-07-23 21:41:13 -07001114class ObjectArray : public Array {
1115 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001116 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001117
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001118 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001119
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001120 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001121
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001122 // Set element without bound and element type checks, to be used in limited
1123 // circumstances, such as during boot image writing
1124 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001125
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001126 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001127 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001128 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001129
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001130 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001131
1132 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001133 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001134};
1135
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001136template<class T>
1137ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1138 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1139}
1140
1141template<class T>
1142T* ObjectArray<T>::Get(int32_t i) const {
1143 if (!IsValidIndex(i)) {
1144 return NULL;
1145 }
1146 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1147 return GetFieldObject<T*>(data_offset, false);
1148}
1149
1150template<class T>
1151ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1152 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1153 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1154 return new_array;
1155}
1156
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001157// Type for the InitializedStaticStorage table. Currently the Class
1158// provides the static storage. However, this might change to improve
1159// image sharing, so we use this type to avoid assumptions on the
1160// current storage.
1161class StaticStorageBase {};
1162
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001163// C++ mirror of java.lang.Class
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001164class Class : public Object, public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001165 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001166
1167 // Class Status
1168 //
1169 // kStatusNotReady: If a Class cannot be found in the class table by
1170 // FindClass, it allocates an new one with AllocClass in the
1171 // kStatusNotReady and calls LoadClass. Note if it does find a
1172 // class, it may not be kStatusResolved and it will try to push it
1173 // forward toward kStatusResolved.
1174 //
1175 // kStatusIdx: LoadClass populates with Class with information from
1176 // the DexFile, moving the status to kStatusIdx, indicating that the
1177 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001178 // populated based on super_class_type_idx_ and
1179 // interfaces_type_idx_. The new Class can then be inserted into the
1180 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001181 //
1182 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1183 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1184 // using ResolveClass to initialize the super_class_ and interfaces_.
1185 //
1186 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001187 // shows linking is complete and fields of the Class populated by making
1188 // it kStatusResolved. Java allows circularities of the form where a super
1189 // class has a field that is of the type of the sub class. We need to be able
1190 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001191
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001192 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001193 kStatusError = -1,
1194 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001195 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001196 kStatusLoaded = 2, // DEX idx values resolved
1197 kStatusResolved = 3, // part of linking
1198 kStatusVerifying = 4, // in the process of being verified
1199 kStatusVerified = 5, // logically part of linking; done pre-init
1200 kStatusInitializing = 6, // class init in progress
1201 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001202 };
1203
1204 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001205 kPrimNot = 0,
1206 kPrimBoolean,
1207 kPrimByte,
1208 kPrimChar,
1209 kPrimShort,
1210 kPrimInt,
1211 kPrimLong,
1212 kPrimFloat,
1213 kPrimDouble,
1214 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001215 };
1216
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001217 Status GetStatus() const {
1218 CHECK(sizeof(Status) == sizeof(uint32_t));
1219 return static_cast<Status>(
1220 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
1221 }
1222
1223 void SetStatus(Status new_status);
1224
1225 // Returns true if the class has failed to link.
1226 bool IsErroneous() const {
1227 return GetStatus() == kStatusError;
1228 }
1229
1230 // Returns true if the class has been loaded.
1231 bool IsIdxLoaded() const {
1232 return GetStatus() >= kStatusIdx;
1233 }
1234
1235 // Returns true if the class has been loaded.
1236 bool IsLoaded() const {
1237 return GetStatus() >= kStatusLoaded;
1238 }
1239
1240 // Returns true if the class has been linked.
1241 bool IsLinked() const {
1242 return GetStatus() >= kStatusResolved;
1243 }
1244
1245 // Returns true if the class has been verified.
1246 bool IsVerified() const {
1247 return GetStatus() >= kStatusVerified;
1248 }
1249
1250 // Returns true if the class is initialized.
1251 bool IsInitialized() const {
1252 return GetStatus() == kStatusInitialized;
1253 }
1254
1255 uint32_t GetAccessFlags() const;
1256
1257 void SetAccessFlags(uint32_t new_access_flags) {
1258 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags,
1259 false);
1260 }
1261
1262 // Returns true if the class is an interface.
1263 bool IsInterface() const {
1264 return (GetAccessFlags() & kAccInterface) != 0;
1265 }
1266
1267 // Returns true if the class is declared public.
1268 bool IsPublic() const {
1269 return (GetAccessFlags() & kAccPublic) != 0;
1270 }
1271
1272 // Returns true if the class is declared final.
1273 bool IsFinal() const {
1274 return (GetAccessFlags() & kAccFinal) != 0;
1275 }
1276
1277 // Returns true if the class is abstract.
1278 bool IsAbstract() const {
1279 return (GetAccessFlags() & kAccAbstract) != 0;
1280 }
1281
1282 // Returns true if the class is an annotation.
1283 bool IsAnnotation() const {
1284 return (GetAccessFlags() & kAccAnnotation) != 0;
1285 }
1286
1287 // Returns true if the class is synthetic.
1288 bool IsSynthetic() const {
1289 return (GetAccessFlags() & kAccSynthetic) != 0;
1290 }
1291
1292 bool IsReferenceClass() const {
1293 return (GetAccessFlags() & kAccClassIsReference) != 0;
1294 }
1295
1296 bool IsWeakReferenceClass() const {
1297 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1298 }
1299
1300 bool IsSoftReferenceClass() const {
1301 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1302 }
1303
1304 bool IsFinalizerReferenceClass() const {
1305 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1306 }
1307
1308 bool IsPhantomReferenceClass() const {
1309 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1310 }
1311
1312 PrimitiveType GetPrimitiveType() const {
1313 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1314 return static_cast<PrimitiveType>(
1315 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1316 }
1317
1318 void SetPrimitiveType(PrimitiveType new_type) {
1319 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1320 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type,
1321 false);
1322 }
1323
1324 // Returns true if the class is a primitive type.
1325 bool IsPrimitive() const {
1326 return GetPrimitiveType() != kPrimNot;
1327 }
1328
1329 bool IsPrimitiveBoolean() const {
1330 return GetPrimitiveType() == kPrimBoolean;
1331 }
1332
1333 bool IsPrimitiveByte() const {
1334 return GetPrimitiveType() == kPrimByte;
1335 }
1336
1337 bool IsPrimitiveChar() const {
1338 return GetPrimitiveType() == kPrimChar;
1339 }
1340
1341 bool IsPrimitiveShort() const {
1342 return GetPrimitiveType() == kPrimShort;
1343 }
1344
1345 bool IsPrimitiveInt() const {
1346 return GetPrimitiveType() == kPrimInt;
1347 }
1348
1349 bool IsPrimitiveLong() const {
1350 return GetPrimitiveType() == kPrimLong;
1351 }
1352
1353 bool IsPrimitiveFloat() const {
1354 return GetPrimitiveType() == kPrimFloat;
1355 }
1356
1357 bool IsPrimitiveDouble() const {
1358 return GetPrimitiveType() == kPrimDouble;
1359 }
1360
1361 bool IsPrimitiveVoid() const {
1362 return GetPrimitiveType() == kPrimVoid;
1363 }
1364
1365 size_t PrimitiveSize() const;
1366
1367 bool IsArrayClass() const {
1368 return GetArrayRank() != 0;
1369 }
1370
1371 int32_t GetArrayRank() const {
1372 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, array_rank_),
1373 false);
1374 return result;
1375 }
1376
1377 void SetArrayRank(int32_t new_array_rank) {
1378 DCHECK_EQ(0, GetArrayRank());
1379 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, array_rank_), new_array_rank,
1380 false);
1381 }
1382
1383 Class* GetComponentType() const {
1384 DCHECK(IsArrayClass());
1385 return GetFieldObject<Class*>(
1386 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1387 }
1388
1389 void SetComponentType(Class* new_component_type) {
1390 DCHECK(GetComponentType() == NULL);
1391 DCHECK(new_component_type != NULL);
1392 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_),
1393 new_component_type, false);
1394 }
1395
1396 size_t GetComponentSize() const {
1397 return GetTypeSize(GetComponentType()->GetDescriptor());
1398 }
1399
1400 bool IsObjectClass() const {
1401 return !IsPrimitive() && GetSuperClass() == NULL;
1402 }
1403
1404 static bool CanPutArrayElementFromCode(const Class* elementClass, const Class* arrayClass);
1405
Brian Carlstromb63ec392011-08-27 17:38:27 -07001406 // Given the context of a calling Method, use its DexCache to
1407 // resolve a type to a Class. If it cannot be resolved, throw an
1408 // error. If it can, use it to create an instance.
Brian Carlstrom1f870082011-08-23 16:02:11 -07001409 static Object* AllocObjectFromCode(uint32_t type_idx, Method* method);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001410
Brian Carlstrom1f870082011-08-23 16:02:11 -07001411 // Creates a raw object instance but does not invoke the default constructor.
1412 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001413
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001414 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001415
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001416 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001417 const String* result = GetFieldObject<const String*>(
1418 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1419 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1420 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1421 return result;
1422 }
1423
1424 void SetDescriptor(String* new_descriptor);
1425
1426 bool IsVariableSize() const {
1427 // Classes and arrays vary in size, and so the object_size_ field cannot
1428 // be used to get their instance size
1429 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001430 }
1431
Brian Carlstrom4873d462011-08-21 15:23:39 -07001432 size_t SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001433 CHECK(sizeof(size_t) == sizeof(int32_t));
1434 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001435 }
1436
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001437 size_t GetClassSize() const {
1438 CHECK(sizeof(size_t) == sizeof(uint32_t));
1439 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001440 }
1441
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001442 void SetClassSize(size_t new_class_size) {
1443 DCHECK_GE(new_class_size, GetClassSize());
1444 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size,
1445 false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001446 }
1447
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001448 size_t GetObjectSize() const {
1449 CHECK(!IsVariableSize());
1450 CHECK(sizeof(size_t) == sizeof(int32_t));
1451 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_),
1452 false);
1453 CHECK_GE(result, sizeof(Object));
1454 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001455 }
1456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001457 void SetObjectSize(size_t new_object_size) {
1458 DCHECK(!IsVariableSize());
1459 CHECK(sizeof(size_t) == sizeof(int32_t));
1460 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_),
1461 new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001462 }
1463
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001464 // Returns true if this class is in the same packages as that class.
1465 bool IsInSamePackage(const Class* that) const;
1466
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001467 static bool IsInSamePackage(const String* descriptor1,
1468 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001469
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001470 // Returns true if this class can access that class.
1471 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001472 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001473 }
1474
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001475 bool IsAssignableFrom(const Class* klass) const {
1476 DCHECK(klass != NULL);
1477 if (this == klass) {
1478 // Can always assign to things of the same type
1479 return true;
1480 } else if(IsObjectClass()) {
1481 // Can assign any reference to java.lang.Object
1482 return !klass->IsPrimitive();
1483 } else if (IsInterface()) {
1484 return klass->Implements(this);
1485 } else if (klass->IsArrayClass()) {
1486 return IsAssignableFromArray(klass);
1487 } else {
1488 return klass->IsSubClass(this);
1489 }
1490 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001491
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001492 Class* GetSuperClass() const {
1493 // Can only get super class for loaded classes (hack for when runtime is
1494 // initializing)
1495 DCHECK(IsLoaded() || Runtime::Current() == NULL);
1496 return GetFieldObject<Class*>(
1497 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1498 }
1499
1500 void SetSuperClass(Class *new_super_class) {
1501 // super class is assigned once, except during class linker initialization
1502 Class* old_super_class = GetFieldObject<Class*>(
1503 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1504 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1505 DCHECK(new_super_class != NULL);
1506 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_),
1507 new_super_class, false);
1508 }
1509
1510 bool HasSuperClass() const {
1511 return GetSuperClass() != NULL;
1512 }
1513
1514 uint32_t GetSuperClassTypeIdx() const {
1515 DCHECK(IsIdxLoaded());
1516 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1517 false);
1518 }
1519
1520 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
1521 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1522 new_super_class_idx, false);
1523 }
1524
1525 const ClassLoader* GetClassLoader() const;
1526
1527 void SetClassLoader(const ClassLoader* new_cl);
1528
1529 static MemberOffset DexCacheOffset() {
1530 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1531 }
1532
1533 DexCache* GetDexCache() const;
1534
1535 void SetDexCache(DexCache* new_dex_cache);
1536
1537 ObjectArray<Method>* GetDirectMethods() const {
1538 DCHECK(IsLoaded());
1539 return GetFieldObject<ObjectArray<Method>*>(
1540 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1541 }
1542
1543 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1544 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1545 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1546 DCHECK_NE(0, new_direct_methods->GetLength());
1547 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1548 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001549 }
1550
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001551 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001552 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001553 }
1554
1555 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001556 ObjectArray<Method>* direct_methods =
1557 GetFieldObject<ObjectArray<Method>*>(
1558 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1559 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001560 }
1561
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001562 // Returns the number of static, private, and constructor methods.
1563 size_t NumDirectMethods() const {
1564 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1565 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001566
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001567 ObjectArray<Method>* GetVirtualMethods() const {
1568 DCHECK(IsLoaded());
1569 return GetFieldObject<ObjectArray<Method>*>(
1570 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1571 }
1572
1573 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1574 // TODO: we reassign virtual methods to grow the table for miranda
1575 // methods.. they should really just be assigned once
1576 DCHECK_NE(0, new_virtual_methods->GetLength());
1577 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1578 new_virtual_methods, false);
1579 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001580
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001581 // Returns the number of non-inherited virtual methods.
1582 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001583 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001584 }
1585
1586 Method* GetVirtualMethod(uint32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001587 DCHECK(IsLinked());
1588 return GetVirtualMethods()->Get(i);
1589 }
1590
1591 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
1592 DCHECK(IsLoaded());
1593 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001594 }
1595
1596 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001597 ObjectArray<Method>* virtual_methods =
1598 GetFieldObject<ObjectArray<Method>*>(
1599 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1600 virtual_methods->Set(i, f);
1601 }
1602
1603 ObjectArray<Method>* GetVTable() const {
1604 DCHECK(IsLinked());
1605 return GetFieldObject<ObjectArray<Method>*>(
1606 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1607 }
1608
1609 ObjectArray<Method>* GetVTableDuringLinking() const {
1610 DCHECK(IsLoaded());
1611 return GetFieldObject<ObjectArray<Method>*>(
1612 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1613 }
1614
1615 void SetVTable(ObjectArray<Method>* new_vtable) {
1616 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1617 }
1618
1619 static MemberOffset VTableOffset() {
1620 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001621 }
1622
Brian Carlstrom30b94452011-08-25 21:35:26 -07001623 // Given a method implemented by this class but potentially from a
1624 // super class, return the specific implementation
1625 // method for this class.
1626 Method* FindVirtualMethodForVirtual(Method* method) {
1627 DCHECK(!method->GetDeclaringClass()->IsInterface());
1628 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001629 // Use the index to a potentially overridden one for this instance's class.
1630 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001631 }
1632
1633 // Given a method implemented by this class, but potentially from a
1634 // super class or interface, return the specific implementation
1635 // method for this class.
1636 Method* FindVirtualMethodForInterface(Method* method);
1637
1638 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1639 if (method->GetDeclaringClass()->IsInterface()) {
1640 return FindVirtualMethodForInterface(method);
1641 }
1642 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 }
1644
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001645 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1646 const StringPiece& descriptor);
1647
1648 Method* FindVirtualMethod(const StringPiece& name,
1649 const StringPiece& descriptor);
1650
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001651 Method* FindDeclaredDirectMethod(const StringPiece& name,
1652 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001653
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001654 Method* FindDirectMethod(const StringPiece& name,
1655 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001656
Carl Shapiro69759ea2011-07-21 18:13:35 -07001657 size_t NumInterfaces() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001658 CHECK(IsIdxLoaded()); // used during loading
1659 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1660 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1661 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001662 }
1663
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001664 IntArray* GetInterfacesTypeIdx() const {
1665 CHECK(IsIdxLoaded());
1666 return GetFieldObject<IntArray*>(
1667 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
1668 }
1669
1670 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1671
1672 ObjectArray<Class>* GetInterfaces() const {
1673 CHECK(IsLoaded());
1674 return GetFieldObject<ObjectArray<Class>*>(
1675 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1676 }
1677
1678 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
1679 DCHECK(NULL == GetFieldObject<Object*>(
1680 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
1681 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_),
1682 new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001683 }
1684
1685 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001686 DCHECK_NE(NumInterfaces(), 0U);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001687 ObjectArray<Class>* interfaces =
1688 GetFieldObject<ObjectArray<Class>*>(
1689 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1690 interfaces->Set(i, f);
1691 }
1692
1693 Class* GetInterface(uint32_t i) const {
1694 DCHECK_NE(NumInterfaces(), 0U);
1695 return GetInterfaces()->Get(i);
1696 }
1697
1698 size_t GetIFTableCount() const {
1699 DCHECK(IsLinked());
1700 DCHECK(sizeof(size_t) == sizeof(int32_t));
1701 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, iftable_count_), false);
1702 }
1703
1704 void SetIFTableCount(size_t new_iftable_count) {
1705 DCHECK(sizeof(size_t) == sizeof(int32_t));
1706 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, iftable_count_),
1707 new_iftable_count, false);
1708 }
1709
1710 InterfaceEntry* GetIFTable() const {
1711 DCHECK(IsLinked());
1712 return GetFieldPtr<InterfaceEntry*>(
1713 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1714 }
1715
1716 void SetIFTable(InterfaceEntry* new_iftable) {
1717 SetFieldPtr<InterfaceEntry*>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_),
1718 new_iftable, false);
1719 }
1720
1721 size_t GetIfviPoolCount() const {
1722 DCHECK(IsLinked());
1723 CHECK(sizeof(size_t) == sizeof(int32_t));
1724 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, ifvi_pool_count_), false);
1725 }
1726
1727 void SetIfviPoolCount(size_t new_count) {
1728 CHECK(sizeof(size_t) == sizeof(int32_t));
1729 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, ifvi_pool_count_), new_count,
1730 false);
1731 }
1732
1733 uint32_t* GetIfviPool() const {
1734 DCHECK(IsLinked());
1735 return GetFieldPtr<uint32_t*>(
1736 OFFSET_OF_OBJECT_MEMBER(Class, ifvi_pool_), false);
1737 }
1738
1739 void SetIfviPool(uint32_t* new_pool) {
1740 SetFieldPtr(OFFSET_OF_OBJECT_MEMBER(Class, ifvi_pool_), new_pool, false);
1741 }
1742
1743 // Get instance fields
1744 ObjectArray<Field>* GetIFields() const {
1745 DCHECK(IsLoaded());
1746 return GetFieldObject<ObjectArray<Field>*>(
1747 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1748 }
1749
1750 void SetIFields(ObjectArray<Field>* new_ifields) {
1751 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1752 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
1753 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_),
1754 new_ifields, false);
1755 }
1756
1757 size_t NumInstanceFields() const {
1758 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1759 }
1760
1761 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1762 DCHECK_NE(NumInstanceFields(), 0U);
1763 return GetIFields()->Get(i);
1764 }
1765
1766 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1767 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1768 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1769 ifields->Set(i, f);
1770 }
1771
1772 // Returns the number of instance fields containing reference types.
1773 size_t NumReferenceInstanceFields() const {
1774 DCHECK(IsLinked());
1775 DCHECK(sizeof(size_t) == sizeof(int32_t));
1776 return GetField32(
1777 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1778 }
1779
1780 size_t NumReferenceInstanceFieldsDuringLinking() const {
1781 DCHECK(IsLoaded());
1782 DCHECK(sizeof(size_t) == sizeof(int32_t));
1783 return GetField32(
1784 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1785 }
1786
1787 void SetNumReferenceInstanceFields(size_t new_num) {
1788 DCHECK(sizeof(size_t) == sizeof(int32_t));
1789 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_),
1790 new_num, false);
1791 }
1792
1793 uint32_t GetReferenceInstanceOffsets() const {
1794 DCHECK(IsLinked());
1795 return GetField32(
1796 OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
1797 }
1798
1799 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1800
1801 // Beginning of static field data
1802 static MemberOffset FieldsOffset() {
1803 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1804 }
1805
1806 // Returns the number of static fields containing reference types.
1807 size_t NumReferenceStaticFields() const {
1808 DCHECK(IsLinked());
1809 DCHECK(sizeof(size_t) == sizeof(int32_t));
1810 return GetField32(
1811 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1812 }
1813
1814 size_t NumReferenceStaticFieldsDuringLinking() const {
1815 DCHECK(IsLoaded());
1816 DCHECK(sizeof(size_t) == sizeof(int32_t));
1817 return GetField32(
1818 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1819 }
1820
1821 void SetNumReferenceStaticFields(size_t new_num) {
1822 DCHECK(sizeof(size_t) == sizeof(int32_t));
1823 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_),
1824 new_num, false);
1825 }
1826
1827 ObjectArray<Field>* GetSFields() const {
1828 DCHECK(IsLoaded());
1829 return GetFieldObject<ObjectArray<Field>*>(
1830 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1831 }
1832
1833 void SetSFields(ObjectArray<Field>* new_sfields) {
1834 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1835 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
1836 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_),
1837 new_sfields, false);
1838 }
1839
1840 size_t NumStaticFields() const {
1841 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1842 }
1843
1844 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1845 return GetSFields()->Get(i);
1846 }
1847
1848 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1849 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1850 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1851 sfields->Set(i, f);
1852 }
1853
1854 uint32_t GetReferenceStaticOffsets() const {
1855 return GetField32(
1856 OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
1857 }
1858
1859 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1860
1861 // Finds the given instance field in this class or a superclass.
1862 Field* FindInstanceField(const StringPiece& name, Class* type);
1863
1864 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1865
1866 // Finds the given static field in this class or a superclass.
1867 Field* FindStaticField(const StringPiece& name, Class* type);
1868
1869 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1870
1871 uint32_t GetClinitThreadId() const {
1872 DCHECK(IsIdxLoaded());
1873 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1874 }
1875
1876 void SetClinitThreadId(uint32_t new_clinit_thread_id) {
1877 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_),
1878 new_clinit_thread_id, false);
1879 }
1880
1881 Class* GetVerifyErrorClass() const {
1882 DCHECK(IsErroneous());
1883 return GetFieldObject<Class*>(
1884 OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001885 }
1886
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001887 void SetVerifyErrorClass(Class* klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001888 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_),
1889 klass, false);
1890 }
1891
1892 const char* GetSourceFile() const {
1893 DCHECK(IsLoaded());
1894 return GetFieldPtr<const char*>(
1895 OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
1896 }
1897
1898 void SetSourceFile(const char* new_source_file) {
1899 SetFieldPtr<const char*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_),
1900 new_source_file, false);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001901 }
1902
1903 private:
1904 bool Implements(const Class* klass) const;
1905 bool IsArrayAssignableFromArray(const Class* klass) const;
1906 bool IsAssignableFromArray(const Class* klass) const;
1907 bool IsSubClass(const Class* klass) const;
1908
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001909 // TODO: the image writer should know the offsets of these fields as they
1910 // should appear in the libcore Java mirror
1911 friend class ImageWriter;
1912
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001913 // descriptor for the class such as "java.lang.Class" or "[C"
1914 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001915
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001916 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1917 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001918
1919 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001920 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001921
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001922 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001923 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001924 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001925
1926 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001927 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001928
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001929 // If class verify fails, we must return same error on subsequent tries.
1930 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1931 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001932
1933 // threadId, used to check for recursive <clinit> invocation
1934 uint32_t clinit_thread_id_;
1935
1936 // Total object size; used when allocating storage on gc heap. (For
1937 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001938 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001939
1940 // For array classes, the class object for base element, for
1941 // instanceof/checkcast (for String[][][], this will be String).
1942 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001943 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001944
1945 // For array classes, the number of array dimensions, e.g. int[][]
1946 // is 2. Otherwise 0.
1947 int32_t array_rank_;
1948
1949 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1950 PrimitiveType primitive_type_;
1951
1952 // The superclass, or NULL if this is java.lang.Object or a
1953 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001954 Class* super_class_; // TODO: make an instance field
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001955 uint32_t super_class_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001956
1957 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001958 const ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001959
1960 // initiating class loader list
1961 // NOTE: for classes with low serialNumber, these are unused, and the
1962 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001963 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001964
1965 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001966 ObjectArray<Class>* interfaces_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001967 IntArray* interfaces_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001968
1969 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001970 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001971
1972 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001973 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001974
1975 // Virtual method table (vtable), for use by "invoke-virtual". The
1976 // vtable from the superclass is copied in, and virtual methods from
1977 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001978 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001979
Brian Carlstrom30b94452011-08-25 21:35:26 -07001980 // Interface table (iftable_), one entry per interface supported by
Carl Shapiro1fb86202011-06-27 17:43:13 -07001981 // this class. That means one entry for each interface we support
1982 // directly, indirectly via superclass, or indirectly via
1983 // superinterface. This will be null if neither we nor our
1984 // superclass implement any interfaces.
1985 //
1986 // Why we need this: given "class Foo implements Face", declare
1987 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1988 // is part of the Face interface. We can't easily use a single
1989 // vtable.
1990 //
1991 // For every interface a concrete class implements, we create a list
1992 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001993 size_t iftable_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001994 // TODO convert to ObjectArray<?>
Carl Shapiro1fb86202011-06-27 17:43:13 -07001995 InterfaceEntry* iftable_;
1996
1997 // The interface vtable indices for iftable get stored here. By
1998 // placing them all in a single pool for each class that implements
1999 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002000 size_t ifvi_pool_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07002001 // TODO convert to IntArray
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002002 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002003
2004 // instance fields
2005 //
2006 // These describe the layout of the contents of a
2007 // DataObject-compatible Object. Note that only the fields directly
2008 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07002009 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07002010 //
2011 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07002012 // the beginning of the field list. num_reference_instance_fields_
2013 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04002014 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002015
Brian Carlstrom4873d462011-08-21 15:23:39 -07002016 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07002017 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002018
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002019 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07002020 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002021
2022 // source file name, if known. Otherwise, NULL.
2023 const char* source_file_;
2024
Jesse Wilson7833bd22011-08-09 18:31:44 -04002025 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04002026 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002027
Brian Carlstrom4873d462011-08-21 15:23:39 -07002028 // number of static fields that are object refs
2029 size_t num_reference_static_fields_;
2030
2031 // Bitmap of offsets of sfields.
2032 uint32_t reference_static_offsets_;
2033
2034 // Total class size; used when allocating storage on gc heap.
2035 size_t class_size_;
2036
2037 // Location of first static field.
2038 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002039
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002040 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07002041 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002042};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002043
Elliott Hughes1f359b02011-07-17 14:27:17 -07002044std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002045
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002046inline void Object::SetClass(Class* new_klass) {
2047 // new_klass may be NULL prior to class linker initialization
2048 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false);
2049}
2050
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002051inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002052 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002053 DCHECK(GetClass() != NULL);
2054 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002055}
2056
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002057inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002058 Class* java_lang_Class = GetClass()->GetClass();
2059 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002060}
2061
Brian Carlstrom4873d462011-08-21 15:23:39 -07002062inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002063 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002064 return this == java_lang_Class;
2065}
2066
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002067inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002068 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002069}
2070
Brian Carlstromb63ec392011-08-27 17:38:27 -07002071inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002072 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002073}
2074
Brian Carlstroma663ea52011-08-19 23:33:41 -07002075inline bool Object::IsField() const {
2076 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002077 Class* java_lang_reflect_Field =
2078 java_lang_Class->GetInstanceField(0)->GetClass();
2079 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002080}
2081
2082inline bool Object::IsMethod() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002083 Class* java_lang_Class = GetClass()->GetClass();
2084 Class* java_lang_reflect_Method =
2085 java_lang_Class->GetDirectMethod(0)->GetClass();
2086 return GetClass() == java_lang_reflect_Method;
2087}
2088
2089inline bool Object::IsReferenceInstance() const {
2090 return GetClass()->IsReferenceClass();
2091}
2092
2093inline bool Object::IsWeakReferenceInstance() const {
2094 return GetClass()->IsWeakReferenceClass();
2095}
2096
2097inline bool Object::IsSoftReferenceInstance() const {
2098 return GetClass()->IsSoftReferenceClass();
2099}
2100
2101inline bool Object::IsFinalizerReferenceInstance() const {
2102 return GetClass()->IsFinalizerReferenceClass();
2103}
2104
2105inline bool Object::IsPhantomReferenceInstance() const {
2106 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002107}
2108
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002109inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002110 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002111 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002112 result = AsArray()->SizeOf();
2113 } else if (IsClass()) {
2114 result = AsClass()->SizeOf();
2115 } else {
2116 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002117 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002118 DCHECK(!IsField() || result == sizeof(Field));
2119 DCHECK(!IsMethod() || result == sizeof(Method));
2120 return result;
2121}
2122
2123inline void Field::SetOffset(MemberOffset num_bytes) {
2124 DCHECK(GetDeclaringClass()->IsLoaded());
2125 DCHECK_LE(CLASS_SMALLEST_OFFSET, num_bytes.Uint32Value());
2126 Class* type = GetTypeDuringLinking();
2127 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2128 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002129 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002130 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2131 num_bytes.Uint32Value(), false);
2132}
2133
2134inline Class* Field::GetDeclaringClass() const {
2135 Class* result = GetFieldObject<Class*>(
2136 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2137 DCHECK(result != NULL);
2138 DCHECK(result->IsLoaded());
2139 return result;
2140}
2141
2142inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2143 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2144 new_declaring_class, false);
2145}
2146
2147inline Class* Method::GetDeclaringClass() const {
2148 Class* result =
2149 GetFieldObject<Class*>(
2150 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2151 DCHECK(result != NULL);
2152 DCHECK(result->IsLoaded());
2153 return result;
2154}
2155
2156inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2157 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2158 new_declaring_class, false);
2159}
2160
2161inline uint32_t Method::GetReturnTypeIdx() const {
2162 DCHECK(GetDeclaringClass()->IsLinked());
2163 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2164 false);
2165}
2166
2167inline bool Method::IsReturnAReference() const {
2168 return !GetReturnType()->IsPrimitive();
2169}
2170
2171inline bool Method::IsReturnAFloat() const {
2172 return GetReturnType()->IsPrimitiveFloat();
2173}
2174
2175inline bool Method::IsReturnADouble() const {
2176 return GetReturnType()->IsPrimitiveDouble();
2177}
2178
2179inline bool Method::IsReturnALong() const {
2180 return GetReturnType()->IsPrimitiveLong();
2181}
2182
2183inline bool Method::IsReturnVoid() const {
2184 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002185}
2186
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002187inline size_t Array::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002188 return SizeOf(GetLength(), GetClass()->GetComponentSize());
2189}
2190
2191template<class T>
2192void ObjectArray<T>::Set(int32_t i, T* object) {
2193 if (IsValidIndex(i)) {
2194 if (object != NULL) {
2195 Class* element_class = GetClass()->GetComponentType();
2196 DCHECK(!element_class->IsPrimitive());
2197 // TODO: ArrayStoreException
2198 CHECK(object->InstanceOf(element_class));
2199 }
2200 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2201 SetFieldObject(data_offset, object, false);
2202 }
2203}
2204
2205template<class T>
2206void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2207 DCHECK(IsValidIndex(i));
2208 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2209 SetFieldObject(data_offset, object, false);
2210}
2211
2212template<class T>
2213void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2214 ObjectArray<T>* dst, int dst_pos,
2215 size_t length) {
2216 if (src->IsValidIndex(src_pos) &&
2217 src->IsValidIndex(src_pos+length-1) &&
2218 dst->IsValidIndex(dst_pos) &&
2219 dst->IsValidIndex(dst_pos+length-1)) {
2220 MemberOffset src_offset(DataOffset().Int32Value() +
2221 src_pos * sizeof(Object*));
2222 MemberOffset dst_offset(DataOffset().Int32Value() +
2223 dst_pos * sizeof(Object*));
2224 Class* array_class = dst->GetClass();
2225 if (array_class == src->GetClass()) {
2226 // No need for array store checks if arrays are of the same type
2227 for (size_t i=0; i < length; i++) {
2228 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2229 dst->SetFieldObject(dst_offset, object, false);
2230 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2231 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2232 }
2233 } else {
2234 Class* element_class = array_class->GetComponentType();
2235 CHECK(!element_class->IsPrimitive());
2236 for (size_t i=0; i < length; i++) {
2237 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2238 // TODO: ArrayStoreException
2239 CHECK(object == NULL || object->InstanceOf(element_class));
2240 dst->SetFieldObject(dst_offset, object, false);
2241 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2242 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2243 }
2244 }
2245 // TODO: bulk write barrier
2246 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002247}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002248
Brian Carlstrom4873d462011-08-21 15:23:39 -07002249class ClassClass : public Class {
2250 private:
2251 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
2252 int32_t padding_;
2253 int64_t serialVersionUID_;
2254 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2255};
2256
2257class StringClass : public Class {
2258 private:
2259 CharArray* ASCII_;
2260 Object* CASE_INSENSITIVE_ORDER_;
2261 uint32_t REPLACEMENT_CHAR_;
2262 int64_t serialVersionUID;
2263 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2264};
2265
2266class FieldClass : public Class {
2267 private:
2268 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2269 uint32_t TYPE_BOOLEAN_;
2270 uint32_t TYPE_BYTE_;
2271 uint32_t TYPE_CHAR_;
2272 uint32_t TYPE_DOUBLE_;
2273 uint32_t TYPE_FLOAT_;
2274 uint32_t TYPE_INTEGER_;
2275 uint32_t TYPE_LONG_;
2276 uint32_t TYPE_SHORT_;
2277 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2278};
2279
2280class MethodClass : public Class {
2281 private:
2282 int32_t DECLARED_;
2283 int32_t PUBLIC_;
2284 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2285};
2286
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002287class DataObject : public Object {
2288 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07002289 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07002290 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002291 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07002292 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002293};
2294
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002295template<class T>
2296class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002297 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002298 typedef T ElementType;
2299
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002300 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002301
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002302 const T* GetData() const {
2303 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002304 }
2305
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002306 T* GetData() {
2307 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002308 }
2309
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002310 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002311 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002312 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002313 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002314 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002315 }
2316
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002317 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002318 // TODO: ArrayStoreException
2319 if (IsValidIndex(i)) {
2320 GetData()[i] = value;
2321 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002322 }
2323
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002324 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002325 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002326 CHECK(array_class != NULL);
2327 array_class_ = array_class;
2328 }
2329
Brian Carlstroma663ea52011-08-19 23:33:41 -07002330 static void ResetArrayClass() {
2331 CHECK(array_class_ != NULL);
2332 array_class_ = NULL;
2333 }
2334
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002335 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002336 // Location of first element.
2337 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002338
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002339 static Class* array_class_;
2340
Carl Shapirof88c9522011-08-06 15:47:38 -07002341 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002342};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002343
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002344inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2345 DCHECK(NULL == GetFieldObject<IntArray*>(
2346 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2347 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2348 new_interfaces_idx, false);
2349}
2350
2351// C++ mirror of java.lang.String
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002352class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002353 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002354 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002355 const CharArray* result = GetFieldObject<const CharArray*>(
2356 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2357 DCHECK(result != NULL);
2358 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002359 }
2360
Elliott Hughes814e4032011-08-23 12:07:56 -07002361 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002362 int32_t result = GetField32(
2363 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2364 DCHECK_LE(0, result);
2365 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002366 }
2367
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002368 int32_t GetLength() const;
2369
2370 int32_t GetHashCode() const;
2371
2372 void ComputeHashCode() {
2373 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002374 }
2375
Elliott Hughes814e4032011-08-23 12:07:56 -07002376 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002377 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002378 }
2379
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002380 uint16_t CharAt(int32_t index) const;
2381
2382 const String* Intern() const;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002383
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002384 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002385 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002386 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002387
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002388 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002389
Jesse Wilson8989d992011-08-02 13:39:42 -07002390 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002391 const char* utf8_data_in);
2392
2393 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2394
2395 static String* Alloc(Class* java_lang_String, CharArray* array);
2396
2397 bool Equals(const char* modified_utf8) const;
2398
2399 // TODO: do we need this overload? give it a more intention-revealing name.
2400 bool Equals(const StringPiece& modified_utf8) const;
2401
2402 bool Equals(const String* that) const;
2403
2404 // TODO: do we need this overload? give it a more intention-revealing name.
2405 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2406 int32_t that_length) const;
2407
2408 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2409 std::string ToModifiedUtf8() const;
2410
2411 static Class* GetJavaLangString() {
2412 DCHECK(java_lang_String_ != NULL);
2413 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002414 }
2415
Brian Carlstroma663ea52011-08-19 23:33:41 -07002416 static void SetClass(Class* java_lang_String);
2417 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002418
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002419 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002420 void SetHashCode(int32_t new_hash_code) {
2421 DCHECK_EQ(0u,
2422 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2423 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2424 new_hash_code, false);
2425 }
2426
2427 void SetCount(int32_t new_count) {
2428 DCHECK_LE(0, new_count);
2429 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2430 }
2431
2432 void SetOffset(int32_t new_offset) {
2433 DCHECK_LE(0, new_offset);
2434 DCHECK_GE(GetLength(), new_offset);
2435 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2436 }
2437
2438 void SetArray(CharArray* new_array) {
2439 DCHECK(new_array != NULL);
2440 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2441 }
2442
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002443 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2444 CharArray* array_;
2445
Carl Shapirof88c9522011-08-06 15:47:38 -07002446 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002447
Elliott Hughes289da822011-08-16 10:11:20 -07002448 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002449
Elliott Hughes289da822011-08-16 10:11:20 -07002450 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002451
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002452 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002453
2454 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002455};
2456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002457inline const String* Field::GetName() const {
2458 DCHECK(GetDeclaringClass()->IsLoaded());
2459 String* result =
2460 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2461 DCHECK(result != NULL);
2462 return result;
2463}
2464
2465inline void Field::SetName(String* new_name) {
2466 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2467 new_name, false);
2468
2469}
2470
2471inline uint32_t Field::GetAccessFlags() const {
2472 DCHECK(GetDeclaringClass()->IsLoaded());
2473 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2474}
2475
2476inline uint32_t Field::GetTypeIdx() const {
2477 DCHECK(GetDeclaringClass()->IsIdxLoaded());
2478 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2479}
2480
2481inline MemberOffset Field::GetOffset() const {
2482 DCHECK(GetDeclaringClass()->IsLinked());
2483 return MemberOffset(
2484 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2485}
2486
2487inline MemberOffset Field::GetOffsetDuringLinking() const {
2488 DCHECK(GetDeclaringClass()->IsLoaded());
2489 return MemberOffset(
2490 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2491}
2492
2493inline const String* Method::GetName() const {
2494 DCHECK(GetDeclaringClass()->IsLoaded());
2495 const String* result =
2496 GetFieldObject<const String*>(
2497 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2498 DCHECK(result != NULL);
2499 return result;
2500}
2501
2502inline void Method::SetName(String* new_name) {
2503 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2504 new_name, false);
2505
2506}
2507
2508inline const char* Method::GetShorty() const {
2509 DCHECK(GetDeclaringClass()->IsLoaded());
2510 return GetFieldPtr<const char*>(
2511 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2512}
2513
2514inline const String* Method::GetSignature() const {
2515 DCHECK(GetDeclaringClass()->IsLoaded());
2516 const String* result =
2517 GetFieldObject<const String*>(
2518 OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
2519 DCHECK(result != NULL);
2520 return result;
2521}
2522
2523inline void Method::SetSignature(String* new_signature) {
2524 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2525 new_signature, false);
2526}
2527
2528inline uint32_t Class::GetAccessFlags() const {
2529 // Check class is loaded or this is java.lang.String that has a
2530 // circularity issue during loading the names of its members
2531 DCHECK(IsLoaded() || this == String::GetJavaLangString() ||
2532 this == Field::GetJavaLangReflectField() ||
2533 this == Method::GetJavaLangReflectMethod());
2534 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2535}
2536
2537inline void Class::SetDescriptor(String* new_descriptor) {
2538 DCHECK(new_descriptor != NULL);
2539 DCHECK_NE(0, new_descriptor->GetLength());
2540 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2541 new_descriptor, false);
2542}
2543
2544inline uint32_t Method::GetAccessFlags() const {
2545 DCHECK(GetDeclaringClass()->IsLoaded());
2546 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2547}
2548
2549inline uint16_t Method::GetMethodIndex() const {
2550 DCHECK(GetDeclaringClass()->IsLinked());
2551 return GetField16(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
2552}
2553
2554inline uint16_t Method::NumRegisters() const {
2555 DCHECK(GetDeclaringClass()->IsLoaded());
2556 return GetField16(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
2557}
2558
2559inline uint16_t Method::NumIns() const {
2560 DCHECK(GetDeclaringClass()->IsLoaded());
2561 return GetField16(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
2562}
2563
2564inline uint16_t Method::NumOuts() const {
2565 DCHECK(GetDeclaringClass()->IsLoaded());
2566 return GetField16(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
2567}
2568
2569inline uint32_t Method::GetProtoIdx() const {
2570 DCHECK(GetDeclaringClass()->IsLoaded());
2571 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2572}
2573
2574// C++ mirror of java.lang.Throwable
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002575class Throwable : public Object {
2576 private:
2577 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2578 Throwable* cause_;
2579 String* detail_message_;
2580 Object* stack_state_; // Note this is Java volatile:
2581 Object* stack_trace_;
2582 Object* suppressed_exceptions_;
2583
2584 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2585};
2586
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002587// C++ mirror of java.lang.StackTraceElement
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002588class StackTraceElement : public Object {
2589 public:
2590 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002591 return GetFieldObject<const String*>(
2592 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002593 }
2594
2595 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002596 return GetFieldObject<const String*>(
2597 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002598 }
2599
2600 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002601 return GetFieldObject<const String*>(
2602 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002603 }
2604
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002605 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002606 return GetField32(
2607 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002608 }
2609
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002610 static StackTraceElement* Alloc(const String* declaring_class,
2611 const String* method_name,
2612 const String* file_name,
2613 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002614
2615 static void SetClass(Class* java_lang_StackTraceElement);
2616
2617 static void ResetClass();
2618
2619 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002620 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002621 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002622 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002623 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002624 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002625
2626 static Class* GetStackTraceElement() {
2627 DCHECK(java_lang_StackTraceElement_ != NULL);
2628 return java_lang_StackTraceElement_;
2629 }
2630
2631 static Class* java_lang_StackTraceElement_;
2632 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2633};
2634
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002635class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002636 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002637 InterfaceEntry() : interface_(NULL), method_index_array_(NULL) {
Carl Shapirof88c9522011-08-06 15:47:38 -07002638 }
2639
Brian Carlstrom30b94452011-08-25 21:35:26 -07002640 Class* GetInterface() const {
2641 DCHECK(interface_ != NULL);
2642 return interface_;
Carl Shapirof88c9522011-08-06 15:47:38 -07002643 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002644
Brian Carlstrom30b94452011-08-25 21:35:26 -07002645 void SetInterface(Class* interface) {
2646 DCHECK(interface->IsInterface());
2647 interface_ = interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002648 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002649
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002650 uint32_t* GetMethodIndexArray() const {
2651 return method_index_array_;
2652 }
2653
2654 void SetMethodIndexArray(uint32_t* new_mia) {
2655 method_index_array_ = new_mia;
2656 }
2657
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002658 private:
2659 // Points to the interface class.
Brian Carlstrom30b94452011-08-25 21:35:26 -07002660 Class* interface_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002661
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002662 // Index into array of vtable offsets. This points into the
Brian Carlstrom30b94452011-08-25 21:35:26 -07002663 // ifvi_pool_, which holds the vtables for all interfaces declared by
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002664 // this class.
2665 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07002666
Carl Shapirof88c9522011-08-06 15:47:38 -07002667 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002668};
2669
2670} // namespace art
2671
2672#endif // ART_SRC_OBJECT_H_