blob: a1549595dad734045581d07d3b5f5621c2a05a4d [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 Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "constants.h"
7#include "casts.h"
8#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07009#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "logging.h"
11#include "macros.h"
12#include "offsets.h"
13#include "stringpiece.h"
14#include "monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070015
16namespace art {
17
18class Array;
19class Class;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070020class DexCache;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070021class InstanceField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070022class InterfaceEntry;
23class Monitor;
24class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070025class Object;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070026template<class T> class ObjectArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070027class StaticField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070028
Carl Shapiro3ee755d2011-06-28 12:11:04 -070029union JValue {
30 uint8_t z;
31 int8_t b;
32 uint16_t c;
33 int16_t s;
34 int32_t i;
35 int64_t j;
36 float f;
37 double d;
38 Object* l;
39};
40
Brian Carlstrombe977852011-07-19 14:54:54 -070041static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
42static const uint32_t kAccPrivate = 0x0002; // field, method, ic
43static const uint32_t kAccProtected = 0x0004; // field, method, ic
44static const uint32_t kAccStatic = 0x0008; // field, method, ic
45static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
46static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
47static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
48static const uint32_t kAccVolatile = 0x0040; // field
49static const uint32_t kAccBridge = 0x0040; // method (1.5)
50static const uint32_t kAccTransient = 0x0080; // field
51static const uint32_t kAccVarargs = 0x0080; // method (1.5)
52static const uint32_t kAccNative = 0x0100; // method
53static const uint32_t kAccInterface = 0x0200; // class, ic
54static const uint32_t kAccAbstract = 0x0400; // class, method, ic
55static const uint32_t kAccStrict = 0x0800; // method
56static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
57static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
58static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070059
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060static const uint32_t kAccMiranda = 0x8000; // method
61
Brian Carlstroma331b3c2011-07-18 17:47:56 -070062static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
63
Brian Carlstrombe977852011-07-19 14:54:54 -070064static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
65static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070066
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070067/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070068 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070069 */
70/*
71 * A magic value for refOffsets. Ignore the bits and walk the super
72 * chain when this is the value.
73 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
74 * fields followed by 2 ref instance fields.]
75 */
76#define CLASS_WALK_SUPER ((unsigned int)(3))
77#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
78#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
79#define CLASS_OFFSET_ALIGNMENT 4
80#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
81/*
82 * Given an offset, return the bit number which would encode that offset.
83 * Local use only.
84 */
85#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
86 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
87 CLASS_OFFSET_ALIGNMENT)
88/*
89 * Is the given offset too large to be encoded?
90 */
91#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
92 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
93/*
94 * Return a single bit, encoding the offset.
95 * Undefined if the offset is too large, as defined above.
96 */
97#define CLASS_BIT_FROM_OFFSET(byteOffset) \
98 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
99/*
100 * Return an offset, given a bit number as returned from CLZ.
101 */
102#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700103 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700104
105
Carl Shapiro1fb86202011-06-27 17:43:13 -0700106class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700107 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700108 static Object* Alloc(Class* klass);
109
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700110 Class* GetClass() const {
111 return klass_;
112 }
113
114 void MonitorEnter() {
115 monitor_->Enter();
116 }
117
118 void MonitorExit() {
119 monitor_->Exit();
120 }
121
122 void Notify() {
123 monitor_->Notify();
124 }
125
126 void NotifyAll() {
127 monitor_->NotifyAll();
128 }
129
130 void Wait() {
131 monitor_->Wait();
132 }
133
134 void Wait(int64_t timeout) {
135 monitor_->Wait(timeout);
136 }
137
138 void Wait(int64_t timeout, int32_t nanos) {
139 monitor_->Wait(timeout, nanos);
140 }
141
Carl Shapiro69759ea2011-07-21 18:13:35 -0700142 const Object* GetFieldObject(size_t field_offset) const {
143 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
144 return *reinterpret_cast<Object* const*>(raw_addr);
145 }
146
147 Object* GetFieldObject(size_t field_offset) {
148 return const_cast<Object*>(GetFieldObject(field_offset));
149 }
150
151 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700152 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
153 *reinterpret_cast<Object**>(raw_addr) = new_value;
154 // TODO: write barrier
155 }
156
Carl Shapiro69759ea2011-07-21 18:13:35 -0700157 bool IsClass() const {
158 LOG(FATAL) << "Unimplemented";
159 return true;
160 }
161
162 Class* AsClass() {
163 return down_cast<Class*>(this);
164 }
165
166 const Class* AsClass() const {
167 return down_cast<const Class*>(this);
168 }
169
170 bool IsObjectArray() const {
171 LOG(FATAL) << "Unimplemented";
172 return true;
173 }
174
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700175 const ObjectArray<Object>* AsObjectArray() const {
176 return down_cast<const ObjectArray<Object>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700177 }
178
179 bool IsReference() const {
180 LOG(FATAL) << "Unimplemented";
181 return true;
182 }
183
184 bool IsWeakReference() const {
185 LOG(FATAL) << "Unimplemented";
186 return true;
187 }
188
189 bool IsSoftReference() const {
190 LOG(FATAL) << "Unimplemented";
191 return true;
192 }
193
194 bool IsFinalizerReference() const {
195 LOG(FATAL) << "Unimplemented";
196 return true;
197 }
198
199 bool IsPhantomReference() const {
200 LOG(FATAL) << "Unimplemented";
201 return true;
202 }
203
204 bool IsArray() const {
205 LOG(FATAL) << "Unimplemented";
206 return true;
207 }
208
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700210 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700211
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700212 Monitor* monitor_;
213
214 private:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700215 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700216 DISALLOW_COPY_AND_ASSIGN(Object);
217};
218
219class ObjectLock {
220 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700221 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700222 CHECK(object != NULL);
223 obj_->MonitorEnter();
224 }
225
226 ~ObjectLock() {
227 obj_->MonitorExit();
228 }
229
230 void Wait(int64_t millis = 0) {
231 return obj_->Wait(millis);
232 }
233
234 void Notify() {
235 obj_->Notify();
236 }
237
238 void NotifyAll() {
239 obj_->NotifyAll();
240 }
241
242 private:
243 Object* obj_;
244 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700245};
246
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700247class Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700248 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700249 Class* GetDeclaringClass() const {
250 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700251 }
252
253 const char* GetName() const {
254 return name_;
255 }
256
257 char GetType() const { // TODO: return type
258 return signature_[0];
259 }
260
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700261 const char* GetSignature() const {
262 return signature_;
263 }
264
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700265 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700266#define FIELD_FIELD_SLOTS 1+6
267 // AccessibleObject #0 flag
268 // Field #0 declaringClass
269 // Field #1 genericType
270 // Field #2 genericTypesAreInitialized
271 // Field #3 name
272 // Field #4 slot
273 // Field #5 type
274 uint32_t instance_data_[FIELD_FIELD_SLOTS];
275#undef FIELD_FIELD_SLOTS
276
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700277 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700278 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700279
280 const char* name_;
281
282 // e.g. "I", "[C", "Landroid/os/Debug;"
283 const char* signature_;
284
285 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700286
287 private:
288 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700289};
290
291// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700292class InstanceField : public Field {
293 public:
294 uint32_t GetOffset() const {
295 return offset_;
296 }
297
298 void SetOffset(size_t num_bytes) {
299 offset_ = num_bytes;
300 }
301
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700302 private:
303 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700304 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700305};
306
307// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700308class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700309 public:
310 void SetBoolean(bool z) {
311 CHECK_EQ(GetType(), 'Z');
312 value_.z = z;
313 }
314
315 void SetByte(int8_t b) {
316 CHECK_EQ(GetType(), 'B');
317 value_.b = b;
318 }
319
320 void SetChar(uint16_t c) {
321 CHECK_EQ(GetType(), 'C');
322 value_.c = c;
323 }
324
325 void SetShort(uint16_t s) {
326 CHECK_EQ(GetType(), 'S');
327 value_.s = s;
328 }
329
330 void SetInt(int32_t i) {
331 CHECK_EQ(GetType(), 'I');
332 value_.i = i;
333 }
334
335 int64_t GetLong() {
336 CHECK_EQ(GetType(), 'J');
337 return value_.j;
338 }
339
340 void SetLong(int64_t j) {
341 CHECK_EQ(GetType(), 'J');
342 value_.j = j;
343 }
344
345 void SetFloat(float f) {
346 CHECK_EQ(GetType(), 'F');
347 value_.f = f;
348 }
349
350 void SetDouble(double d) {
351 CHECK_EQ(GetType(), 'D');
352 value_.d = d;
353 }
354
Carl Shapiro69759ea2011-07-21 18:13:35 -0700355 Object* GetObject() {
356 return value_.l;
357 }
358
359 const Object* GetObject() const {
360 return value_.l;
361 }
362
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700363 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700364 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700365 value_.l = l;
366 // TODO: write barrier
367 }
368
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700369 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700370 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700371 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700372};
373
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700374class Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700375 public:
376 // Returns the method name.
377 // TODO: example
378 const StringPiece& GetName() const {
379 return name_;
380 }
381
Brian Carlstroma0808032011-07-18 00:39:23 -0700382 Class* GetDeclaringClass() const {
383 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700384 }
385
Ian Rogersb033c752011-07-20 12:22:35 -0700386 static MemberOffset ClassOffset() {
387 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
388 }
389
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700390 // const char* GetReturnTypeDescriptor() const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700391 // return FindDexFile(declaring_class_->GetDexCache()
Ian Rogersb033c752011-07-20 12:22:35 -0700392 // ->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700393 // }
394
395 // Returns true if the method is declared public.
396 bool IsPublic() const {
397 return (access_flags_ & kAccPublic) != 0;
398 }
399
400 // Returns true if the method is declared private.
401 bool IsPrivate() const {
402 return (access_flags_ & kAccPrivate) != 0;
403 }
404
405 // Returns true if the method is declared static.
406 bool IsStatic() const {
407 return (access_flags_ & kAccStatic) != 0;
408 }
409
410 // Returns true if the method is declared synchronized.
411 bool IsSynchronized() const {
412 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
413 return (access_flags_ & synchonized) != 0;
414 }
415
416 // Returns true if the method is declared final.
417 bool IsFinal() const {
418 return (access_flags_ & kAccFinal) != 0;
419 }
420
421 // Returns true if the method is declared native.
422 bool IsNative() const {
423 return (access_flags_ & kAccNative) != 0;
424 }
425
426 // Returns true if the method is declared abstract.
427 bool IsAbstract() const {
428 return (access_flags_ & kAccAbstract) != 0;
429 }
430
431 bool IsSynthetic() const {
432 return (access_flags_ & kAccSynthetic) != 0;
433 }
434
435 // Number of argument registers required by the prototype.
436 uint32_t NumArgRegisters();
437
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700438 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700439#define METHOD_FIELD_SLOTS 1+11
440 // AccessibleObject #0 flag
441 // Method #0 declaringClass
442 // Method #1 exceptionTypes
443 // Method #2 formalTypeParameters
444 // Method #3 genericExceptionTypes
445 // Method #4 genericParameterTypes
446 // Method #5 genericReturnType
447 // Method #6 genericTypesAreInitialized
448 // Method #7 name
449 // Method #8 parameterTypes
450 // Method #9 returnType
451 // Method #10 slot
452 uint32_t instance_data_[METHOD_FIELD_SLOTS];
453#undef METHOD_FIELD_SLOTS
454
Ian Rogersb033c752011-07-20 12:22:35 -0700455 bool IsReturnAReference() const {
456 return (shorty_[0] == 'L') || (shorty_[0] == '[');
457 }
458
459 bool IsReturnAFloatOrDouble() const {
460 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
461 }
462
463 bool IsReturnAFloat() const {
464 return shorty_[0] == 'F';
465 }
466
467 bool IsReturnADouble() const {
468 return shorty_[0] == 'D';
469 }
470
471 bool IsReturnALong() const {
472 return shorty_[0] == 'J';
473 }
474
Ian Rogers45a76cb2011-07-21 22:00:15 -0700475 bool IsReturnVoid() const {
476 return shorty_[0] == 'V';
477 }
478
Ian Rogersb033c752011-07-20 12:22:35 -0700479 // The number of arguments that should be supplied to this method
480 size_t NumArgs() const {
481 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
482 }
483
484 // The number of reference arguments to this method including implicit this
485 // pointer
486 size_t NumReferenceArgs() const;
487
488 // The number of long or double arguments
489 size_t NumLongOrDoubleArgs() const;
490
491 // The number of reference arguments to this method before the given
492 // parameter index
493 size_t NumReferenceArgsBefore(unsigned int param) const;
494
495 // Is the given method parameter a reference?
496 bool IsParamAReference(unsigned int param) const;
497
498 // Is the given method parameter a long or double?
499 bool IsParamALongOrDouble(unsigned int param) const;
500
Ian Rogersdf20fe02011-07-20 20:34:16 -0700501 // Size in bytes of the given parameter
502 size_t ParamSize(unsigned int param) const;
503
504 // Size in bytes of the return value
505 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700506
507 void SetCode(const void* code) {
508 code_ = code;
509 }
510
511 const void* GetCode() const {
512 return code_;
513 }
514
515 void RegisterNative(const void* native_method) {
516 native_method_ = native_method;
517 }
518
519 static MemberOffset NativeMethodOffset() {
520 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
521 }
522
523 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700524 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700525 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700526
527 // access flags; low 16 bits are defined by spec (could be uint16_t?)
528 uint32_t access_flags_;
529
530 // For concrete virtual methods, this is the offset of the method
531 // in "vtable".
532 //
533 // For abstract methods in an interface class, this is the offset
534 // of the method in "iftable[n]->methodIndexArray".
535 uint16_t method_index_;
536
537 // Method bounds; not needed for an abstract method.
538 //
539 // For a native method, we compute the size of the argument list, and
540 // set "insSize" and "registerSize" equal to it.
541 uint16_t num_registers_; // ins + locals
542 uint16_t num_outs_;
543 uint16_t num_ins_;
544
545 // method name, e.g. "<init>" or "eatLunch"
546 StringPiece name_;
547
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700548 // Method prototype descriptor string (return and argument types).
549 uint32_t proto_idx_;
550
551 // The short-form method descriptor string.
552 StringPiece shorty_;
553
554 // A pointer to the memory-mapped DEX code.
555 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700556
557 private:
558 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700559
560 // Compiled code associated with this method
561 const void* code_;
562
563 // Any native method registered with this method
564 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700565};
566
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700567class Array : public Object {
568 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700569 static Array* Alloc(Class* array_class,
570 size_t component_count,
571 size_t component_size) {
572 size_t size = sizeof(Array) + component_count * component_size;
573 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
574 if (array != NULL) {
575 array->SetLength(component_count);
576 }
577 return array;
578 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700579 uint32_t GetLength() const {
580 return length_;
581 }
582 void SetLength(uint32_t length) {
583 length_ = length;
584 }
585 const void* GetData() const {
586 return &elements_;
587 }
588 void* GetData() {
589 return &elements_;
590 }
591
592 private:
593 // The number of array elements.
594 uint32_t length_;
595 // Location of first element.
596 uint32_t elements_[0];
597 Array();
598};
599
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700600template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700601class ObjectArray : public Array {
602 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700603 static ObjectArray<T>* Alloc(Class* object_array_class,
604 size_t length) {
605 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
606 length,
607 sizeof(uint32_t)));
608 }
609
610 T* Get(uint32_t i) const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700611 DCHECK_LT(i, GetLength());
612 Object* const * data = reinterpret_cast<Object* const *>(GetData());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700613 return down_cast<T*>(data[i]);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700614 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700615 void Set(uint32_t i, T* object) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700616 DCHECK_LT(i, GetLength());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700617 T** data = reinterpret_cast<T**>(GetData());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700618 data[i] = object;
619 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700620 static void Copy(ObjectArray<T>* src, int src_pos, ObjectArray<T>* dst, int dst_pos, size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700621 for (size_t i = 0; i < length; i++) {
622 dst->Set(dst_pos + i, src->Get(src_pos + i));
623 }
624 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700625 ObjectArray<T>* CopyOf(size_t new_length) {
626 ObjectArray<T>* new_array = Alloc(klass_, new_length);
627 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
628 return new_array;
629 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700630
631 private:
632 ObjectArray();
633};
634
Carl Shapiro1fb86202011-06-27 17:43:13 -0700635// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700636class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700637 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700638 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700639 kStatusError = -1,
640 kStatusNotReady = 0,
641 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
642 kStatusLoaded = 2, // DEX idx values resolved
643 kStatusResolved = 3, // part of linking
644 kStatusVerifying = 4, // in the process of being verified
645 kStatusVerified = 5, // logically part of linking; done pre-init
646 kStatusInitializing = 6, // class init in progress
647 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700648 };
649
650 enum PrimitiveType {
651 kPrimNot = -1
652 };
653
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700654 Class* GetSuperClass() const {
655 return super_class_;
656 }
657
658 uint32_t GetSuperClassIdx() const {
659 return super_class_idx_;
660 }
661
662 bool HasSuperClass() const {
663 return super_class_ != NULL;
664 }
665
666 Object* GetClassLoader() const {
667 return class_loader_;
668 }
669
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700670 DexCache* GetDexCache() const {
671 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700672 }
673
674 Class* GetComponentType() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700675 DCHECK(IsArray());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700676 return component_type_;
677 }
678
679 const StringPiece& GetDescriptor() const {
680 return descriptor_;
681 }
682
683 Status GetStatus() const {
684 return status_;
685 }
686
687 void SetStatus(Status new_status) {
688 // TODO: validate transition
689 status_ = new_status;
690 }
691
Carl Shapiro69759ea2011-07-21 18:13:35 -0700692 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700693 bool IsErroneous() const {
694 return GetStatus() == kStatusError;
695 }
696
Carl Shapiro69759ea2011-07-21 18:13:35 -0700697 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700698 bool IsVerified() const {
699 return GetStatus() >= kStatusVerified;
700 }
701
Carl Shapiro69759ea2011-07-21 18:13:35 -0700702 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700703 bool IsLinked() const {
704 return GetStatus() >= kStatusResolved;
705 }
706
Carl Shapiro69759ea2011-07-21 18:13:35 -0700707 bool IsLoaded() const {
708 return GetStatus() >= kStatusLoaded;
709 }
710
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700711 // Returns true if this class is in the same packages as that class.
712 bool IsInSamePackage(const Class* that) const;
713
Ian Rogersb033c752011-07-20 12:22:35 -0700714 static bool IsInSamePackage(const StringPiece& descriptor1,
715 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700716
717 // Returns true if this class represents an array class.
718 bool IsArray() const {
719 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
720 }
721
722 // Returns true if the class is an interface.
723 bool IsInterface() const {
724 return (access_flags_ & kAccInterface) != 0;
725 }
726
727 // Returns true if the class is declared public.
728 bool IsPublic() const {
729 return (access_flags_ & kAccPublic) != 0;
730 }
731
732 // Returns true if the class is declared final.
733 bool IsFinal() const {
734 return (access_flags_ & kAccFinal) != 0;
735 }
736
737 // Returns true if the class is abstract.
738 bool IsAbstract() const {
739 return (access_flags_ & kAccAbstract) != 0;
740 }
741
742 // Returns true if the class is an annotation.
743 bool IsAnnotation() const {
744 return (access_flags_ & kAccAnnotation) != 0;
745 }
746
747 // Returns true if the class is a primitive type.
748 bool IsPrimitive() const {
749 return primitive_type_ != kPrimNot;
750 }
751
752 // Returns true if this class can access that class.
753 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700754 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700755 }
756
Carl Shapiro1fb86202011-06-27 17:43:13 -0700757 // Returns the size in bytes of a class object instance with the
758 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700759 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700760 // return OFFSETOF_MEMBER(Class, sfields_) +
761 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700762 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700763
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700764 // Returns the number of static, private, and constructor methods.
765 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700766 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700767 }
768
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700769 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700770 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700771 }
772
773 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
774 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700775 }
776
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700777 // Returns the number of non-inherited virtual methods.
778 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700779 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780 }
781
782 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700783 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700784 }
785
786 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
787 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700788 }
789
790 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700791 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700792 }
793
Carl Shapiro69759ea2011-07-21 18:13:35 -0700794 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700795 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700796 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700797 }
798
799 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700800 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700801 }
802
803 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700804 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700805 }
806
807 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700808 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700809 }
810
Carl Shapiro69759ea2011-07-21 18:13:35 -0700811 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700812 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700813 }
814
815 void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
816 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700817 }
818
819 uint32_t GetReferenceOffsets() const {
820 return reference_offsets_;
821 }
822
823 void SetReferenceOffsets(uint32_t new_reference_offsets) {
824 reference_offsets_ = new_reference_offsets;
825 }
826
Ian Rogersb033c752011-07-20 12:22:35 -0700827 Method* FindDirectMethod(const StringPiece& name) const;
828
829 Method* FindVirtualMethod(const StringPiece& name) const;
830
Carl Shapiro69759ea2011-07-21 18:13:35 -0700831 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700832 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700833 }
834
835 Class* GetInterface(uint32_t i) const {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700836 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700837 }
838
839 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
840 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700841 }
842
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700843 Method* FindDirectMethodLocally(const StringPiece& name,
844 const StringPiece& descriptor) const;
845
Ian Rogersb033c752011-07-20 12:22:35 -0700846 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700847 // leave space for instance data; we could access fields directly if
848 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700849#define CLASS_FIELD_SLOTS 1
850 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700851 uint32_t instance_data_[CLASS_FIELD_SLOTS];
852#undef CLASS_FIELD_SLOTS
853
854 // UTF-8 descriptor for the class from constant pool
855 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700856 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700857
858 // Proxy classes have their descriptor allocated on the native heap.
859 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700860 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700861
862 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700863 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700864
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700865 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700866 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700867 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700868
869 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700870 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700871
872 // if class verify fails, we must return same error on subsequent tries
873 Class* verify_error_class_;
874
875 // threadId, used to check for recursive <clinit> invocation
876 uint32_t clinit_thread_id_;
877
878 // Total object size; used when allocating storage on gc heap. (For
879 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700880 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700881
882 // For array classes, the class object for base element, for
883 // instanceof/checkcast (for String[][][], this will be String).
884 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700885 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700886
887 // For array classes, the number of array dimensions, e.g. int[][]
888 // is 2. Otherwise 0.
889 int32_t array_rank_;
890
891 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
892 PrimitiveType primitive_type_;
893
894 // The superclass, or NULL if this is java.lang.Object or a
895 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700896 Class* super_class_; // TODO: make an instance field
897 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700898
899 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700900 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700901
902 // initiating class loader list
903 // NOTE: for classes with low serialNumber, these are unused, and the
904 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700905 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700906
907 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700908 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700909 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700910
911 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700912 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700913
914 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700915 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700916
917 // Virtual method table (vtable), for use by "invoke-virtual". The
918 // vtable from the superclass is copied in, and virtual methods from
919 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700920 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700921
922 // Interface table (iftable), one entry per interface supported by
923 // this class. That means one entry for each interface we support
924 // directly, indirectly via superclass, or indirectly via
925 // superinterface. This will be null if neither we nor our
926 // superclass implement any interfaces.
927 //
928 // Why we need this: given "class Foo implements Face", declare
929 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
930 // is part of the Face interface. We can't easily use a single
931 // vtable.
932 //
933 // For every interface a concrete class implements, we create a list
934 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700935 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700936 InterfaceEntry* iftable_;
937
938 // The interface vtable indices for iftable get stored here. By
939 // placing them all in a single pool for each class that implements
940 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700941 size_t ifvi_pool_count_;
942 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700943
944 // instance fields
945 //
946 // These describe the layout of the contents of a
947 // DataObject-compatible Object. Note that only the fields directly
948 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700949 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700950 //
951 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700952 // the beginning of the field list. num_reference_instance_fields_
953 // specifies the number of reference fields.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700954 ObjectArray<InstanceField>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700955
956 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -0700957 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700958
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700959 // Bitmap of offsets of ifields.
960 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700961
962 // source file name, if known. Otherwise, NULL.
963 const char* source_file_;
964
965 // Static fields
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700966 ObjectArray<StaticField>* sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700967
968 private:
969 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700970};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700971std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700972
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700973inline Object* Object::Alloc(Class* klass) {
974 return Heap::AllocObject(klass, klass->object_size_);
975}
976
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700977class DataObject : public Object {
978 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700979 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700980 private:
981 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700982};
983
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700984class CharArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700985 public:
986 static CharArray* Alloc(Class* char_array_class, size_t length) {
987 return down_cast<CharArray*>(Array::Alloc(char_array_class,
988 length,
989 sizeof(uint16_t)));
990 }
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700991 private:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700992 CharArray();
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700993};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700994
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700995class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700996 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700997 static String* Alloc(Class* java_lang_String) {
998 return down_cast<String*>(Object::Alloc(java_lang_String));
999 }
1000
1001 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1002 Class* char_array,
1003 const char* data) {
1004 String* string = Alloc(java_lang_String);
1005 uint32_t count = strlen(data); // TODO
1006 CharArray* array = CharArray::Alloc(char_array, count);
1007 string->array_ = array;
1008 string->count_ = count;
1009 return string;
1010 }
1011
1012 public: // TODO: private
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001013 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001014
1015 uint32_t hash_code_;
1016
1017 uint32_t offset_;
1018
1019 uint32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001020
1021 private:
1022 String();
Carl Shapiro1fb86202011-06-27 17:43:13 -07001023};
1024
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001025class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001026 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001027 Class* GetClass() const {
1028 return klass_;
1029 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001030
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001031 void SetClass(Class* klass) {
1032 klass_ = klass;
1033 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001034
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001035 private:
1036 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001037 Class* klass_;
1038
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001039 public: // TODO: private
1040 // Index into array of vtable offsets. This points into the
1041 // ifviPool, which holds the vtables for all interfaces declared by
1042 // this class.
1043 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001044};
1045
1046} // namespace art
1047
1048#endif // ART_SRC_OBJECT_H_