blob: 6871dfb75e820dd8475a0efd6c9184c25aa1bd91 [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;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040026class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070027template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070028template<class T> class PrimitiveArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070029class StaticField;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070030typedef PrimitiveArray<uint16_t> CharArray;
31typedef PrimitiveArray<uint32_t> IntArray;
32typedef PrimitiveArray<uint64_t> LongArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070033
Carl Shapiro3ee755d2011-06-28 12:11:04 -070034union JValue {
35 uint8_t z;
36 int8_t b;
37 uint16_t c;
38 int16_t s;
39 int32_t i;
40 int64_t j;
41 float f;
42 double d;
43 Object* l;
44};
45
Brian Carlstrombe977852011-07-19 14:54:54 -070046static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
47static const uint32_t kAccPrivate = 0x0002; // field, method, ic
48static const uint32_t kAccProtected = 0x0004; // field, method, ic
49static const uint32_t kAccStatic = 0x0008; // field, method, ic
50static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
51static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
52static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
53static const uint32_t kAccVolatile = 0x0040; // field
54static const uint32_t kAccBridge = 0x0040; // method (1.5)
55static const uint32_t kAccTransient = 0x0080; // field
56static const uint32_t kAccVarargs = 0x0080; // method (1.5)
57static const uint32_t kAccNative = 0x0100; // method
58static const uint32_t kAccInterface = 0x0200; // class, ic
59static const uint32_t kAccAbstract = 0x0400; // class, method, ic
60static const uint32_t kAccStrict = 0x0800; // method
61static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
62static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
63static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070064
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070065static const uint32_t kAccMiranda = 0x8000; // method
66
Brian Carlstroma331b3c2011-07-18 17:47:56 -070067static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
68
Brian Carlstrombe977852011-07-19 14:54:54 -070069static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
70static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070071
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070072/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070073 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070074 */
75/*
76 * A magic value for refOffsets. Ignore the bits and walk the super
77 * chain when this is the value.
78 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
79 * fields followed by 2 ref instance fields.]
80 */
81#define CLASS_WALK_SUPER ((unsigned int)(3))
82#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
83#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
84#define CLASS_OFFSET_ALIGNMENT 4
85#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
86/*
87 * Given an offset, return the bit number which would encode that offset.
88 * Local use only.
89 */
90#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
91 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
92 CLASS_OFFSET_ALIGNMENT)
93/*
94 * Is the given offset too large to be encoded?
95 */
96#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
97 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
98/*
99 * Return a single bit, encoding the offset.
100 * Undefined if the offset is too large, as defined above.
101 */
102#define CLASS_BIT_FROM_OFFSET(byteOffset) \
103 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
104/*
105 * Return an offset, given a bit number as returned from CLZ.
106 */
107#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700108 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700109
110
Carl Shapiro1fb86202011-06-27 17:43:13 -0700111class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700112 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700113 static Object* Alloc(Class* klass);
114
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700115 Class* GetClass() const {
116 return klass_;
117 }
118
119 void MonitorEnter() {
120 monitor_->Enter();
121 }
122
123 void MonitorExit() {
124 monitor_->Exit();
125 }
126
127 void Notify() {
128 monitor_->Notify();
129 }
130
131 void NotifyAll() {
132 monitor_->NotifyAll();
133 }
134
135 void Wait() {
136 monitor_->Wait();
137 }
138
139 void Wait(int64_t timeout) {
140 monitor_->Wait(timeout);
141 }
142
143 void Wait(int64_t timeout, int32_t nanos) {
144 monitor_->Wait(timeout, nanos);
145 }
146
Carl Shapiro69759ea2011-07-21 18:13:35 -0700147 const Object* GetFieldObject(size_t field_offset) const {
148 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
149 return *reinterpret_cast<Object* const*>(raw_addr);
150 }
151
152 Object* GetFieldObject(size_t field_offset) {
153 return const_cast<Object*>(GetFieldObject(field_offset));
154 }
155
156 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
158 *reinterpret_cast<Object**>(raw_addr) = new_value;
159 // TODO: write barrier
160 }
161
Carl Shapiro69759ea2011-07-21 18:13:35 -0700162 bool IsClass() const {
163 LOG(FATAL) << "Unimplemented";
164 return true;
165 }
166
167 Class* AsClass() {
168 return down_cast<Class*>(this);
169 }
170
171 const Class* AsClass() const {
172 return down_cast<const Class*>(this);
173 }
174
175 bool IsObjectArray() const {
176 LOG(FATAL) << "Unimplemented";
177 return true;
178 }
179
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700180 const ObjectArray<Object>* AsObjectArray() const {
181 return down_cast<const ObjectArray<Object>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700182 }
183
184 bool IsReference() const {
185 LOG(FATAL) << "Unimplemented";
186 return true;
187 }
188
189 bool IsWeakReference() const {
190 LOG(FATAL) << "Unimplemented";
191 return true;
192 }
193
194 bool IsSoftReference() const {
195 LOG(FATAL) << "Unimplemented";
196 return true;
197 }
198
199 bool IsFinalizerReference() const {
200 LOG(FATAL) << "Unimplemented";
201 return true;
202 }
203
204 bool IsPhantomReference() const {
205 LOG(FATAL) << "Unimplemented";
206 return true;
207 }
208
209 bool IsArray() const {
210 LOG(FATAL) << "Unimplemented";
211 return true;
212 }
213
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700214 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700215 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700216
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700217 Monitor* monitor_;
218
219 private:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700220 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700221 DISALLOW_COPY_AND_ASSIGN(Object);
222};
223
224class ObjectLock {
225 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700226 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227 CHECK(object != NULL);
228 obj_->MonitorEnter();
229 }
230
231 ~ObjectLock() {
232 obj_->MonitorExit();
233 }
234
235 void Wait(int64_t millis = 0) {
236 return obj_->Wait(millis);
237 }
238
239 void Notify() {
240 obj_->Notify();
241 }
242
243 void NotifyAll() {
244 obj_->NotifyAll();
245 }
246
247 private:
248 Object* obj_;
249 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700250};
251
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400252class AccessibleObject : public Object {
253 private:
254 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
255 uint32_t java_flag_;
256};
257
258class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700259 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700260 Class* GetDeclaringClass() const {
261 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700262 }
263
Jesse Wilson14150742011-07-29 19:04:44 -0400264 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700265 return name_;
266 }
267
268 bool IsStatic() const {
269 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700270 }
271
272 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700273 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700274 }
275
Brian Carlstromae3ac012011-07-27 01:30:28 -0700276 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700277 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700278 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700279 }
280
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700281 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400282 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
283 Class* java_declaring_class_;
284 Object* java_generic_type_;
285 uint32_t java_generic_types_are_initialized_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700286 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400287 uint32_t java_slot_;
288 Class* java_type_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700289
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700290 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700291 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700292
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700293 // e.g. "I", "[C", "Landroid/os/Debug;"
Brian Carlstromae3ac012011-07-27 01:30:28 -0700294 StringPiece descriptor_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700295
296 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700297
298 private:
299 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700300};
301
302// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700303class InstanceField : public Field {
304 public:
305 uint32_t GetOffset() const {
306 return offset_;
307 }
308
309 void SetOffset(size_t num_bytes) {
310 offset_ = num_bytes;
311 }
312
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700313 private:
314 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700315 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700316};
317
318// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700319class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700320 public:
321 void SetBoolean(bool z) {
322 CHECK_EQ(GetType(), 'Z');
323 value_.z = z;
324 }
325
326 void SetByte(int8_t b) {
327 CHECK_EQ(GetType(), 'B');
328 value_.b = b;
329 }
330
331 void SetChar(uint16_t c) {
332 CHECK_EQ(GetType(), 'C');
333 value_.c = c;
334 }
335
336 void SetShort(uint16_t s) {
337 CHECK_EQ(GetType(), 'S');
338 value_.s = s;
339 }
340
341 void SetInt(int32_t i) {
342 CHECK_EQ(GetType(), 'I');
343 value_.i = i;
344 }
345
346 int64_t GetLong() {
347 CHECK_EQ(GetType(), 'J');
348 return value_.j;
349 }
350
351 void SetLong(int64_t j) {
352 CHECK_EQ(GetType(), 'J');
353 value_.j = j;
354 }
355
356 void SetFloat(float f) {
357 CHECK_EQ(GetType(), 'F');
358 value_.f = f;
359 }
360
361 void SetDouble(double d) {
362 CHECK_EQ(GetType(), 'D');
363 value_.d = d;
364 }
365
Carl Shapiro69759ea2011-07-21 18:13:35 -0700366 Object* GetObject() {
367 return value_.l;
368 }
369
370 const Object* GetObject() const {
371 return value_.l;
372 }
373
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700374 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700375 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700376 value_.l = l;
377 // TODO: write barrier
378 }
379
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700381 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700382 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700383};
384
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400385class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700386 public:
Brian Carlstromae3ac012011-07-27 01:30:28 -0700387 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700388 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700389 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700390 }
391
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700392 const String* GetDescriptor() const {
393 return descriptor_;
394 }
395
Brian Carlstroma0808032011-07-18 00:39:23 -0700396 Class* GetDeclaringClass() const {
397 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700398 }
399
Ian Rogersb033c752011-07-20 12:22:35 -0700400 static MemberOffset ClassOffset() {
401 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
402 }
403
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700404 // Returns true if the method is declared public.
405 bool IsPublic() const {
406 return (access_flags_ & kAccPublic) != 0;
407 }
408
409 // Returns true if the method is declared private.
410 bool IsPrivate() const {
411 return (access_flags_ & kAccPrivate) != 0;
412 }
413
414 // Returns true if the method is declared static.
415 bool IsStatic() const {
416 return (access_flags_ & kAccStatic) != 0;
417 }
418
419 // Returns true if the method is declared synchronized.
420 bool IsSynchronized() const {
421 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
422 return (access_flags_ & synchonized) != 0;
423 }
424
425 // Returns true if the method is declared final.
426 bool IsFinal() const {
427 return (access_flags_ & kAccFinal) != 0;
428 }
429
430 // Returns true if the method is declared native.
431 bool IsNative() const {
432 return (access_flags_ & kAccNative) != 0;
433 }
434
435 // Returns true if the method is declared abstract.
436 bool IsAbstract() const {
437 return (access_flags_ & kAccAbstract) != 0;
438 }
439
440 bool IsSynthetic() const {
441 return (access_flags_ & kAccSynthetic) != 0;
442 }
443
444 // Number of argument registers required by the prototype.
445 uint32_t NumArgRegisters();
446
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700447 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400448 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
449 Class* java_declaring_class_;
450 ObjectArray<Class>* java_exception_types_;
451 Object* java_formal_type_parameters_;
452 Object* java_generic_exception_types_;
453 Object* java_generic_parameter_types_;
454 Object* java_generic_return_type_;
455 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700456 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400457 ObjectArray<Class>* java_parameter_types_;
458 uint32_t java_generic_types_are_initialized_;
459 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700460
Ian Rogersb033c752011-07-20 12:22:35 -0700461 bool IsReturnAReference() const {
462 return (shorty_[0] == 'L') || (shorty_[0] == '[');
463 }
464
465 bool IsReturnAFloatOrDouble() const {
466 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
467 }
468
469 bool IsReturnAFloat() const {
470 return shorty_[0] == 'F';
471 }
472
473 bool IsReturnADouble() const {
474 return shorty_[0] == 'D';
475 }
476
477 bool IsReturnALong() const {
478 return shorty_[0] == 'J';
479 }
480
Ian Rogers45a76cb2011-07-21 22:00:15 -0700481 bool IsReturnVoid() const {
482 return shorty_[0] == 'V';
483 }
484
Ian Rogersb033c752011-07-20 12:22:35 -0700485 // The number of arguments that should be supplied to this method
486 size_t NumArgs() const {
487 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
488 }
489
490 // The number of reference arguments to this method including implicit this
491 // pointer
492 size_t NumReferenceArgs() const;
493
494 // The number of long or double arguments
495 size_t NumLongOrDoubleArgs() const;
496
497 // The number of reference arguments to this method before the given
498 // parameter index
499 size_t NumReferenceArgsBefore(unsigned int param) const;
500
501 // Is the given method parameter a reference?
502 bool IsParamAReference(unsigned int param) const;
503
504 // Is the given method parameter a long or double?
505 bool IsParamALongOrDouble(unsigned int param) const;
506
Ian Rogersdf20fe02011-07-20 20:34:16 -0700507 // Size in bytes of the given parameter
508 size_t ParamSize(unsigned int param) const;
509
510 // Size in bytes of the return value
511 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700512
513 void SetCode(const void* code) {
514 code_ = code;
515 }
516
517 const void* GetCode() const {
518 return code_;
519 }
520
521 void RegisterNative(const void* native_method) {
522 native_method_ = native_method;
523 }
524
525 static MemberOffset NativeMethodOffset() {
526 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
527 }
528
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700529 bool HasSameNameAndDescriptor(const Method* that) const;
530
Ian Rogersb033c752011-07-20 12:22:35 -0700531 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700532 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700533 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700534
535 // access flags; low 16 bits are defined by spec (could be uint16_t?)
536 uint32_t access_flags_;
537
538 // For concrete virtual methods, this is the offset of the method
539 // in "vtable".
540 //
541 // For abstract methods in an interface class, this is the offset
542 // of the method in "iftable[n]->methodIndexArray".
543 uint16_t method_index_;
544
545 // Method bounds; not needed for an abstract method.
546 //
547 // For a native method, we compute the size of the argument list, and
548 // set "insSize" and "registerSize" equal to it.
549 uint16_t num_registers_; // ins + locals
550 uint16_t num_outs_;
551 uint16_t num_ins_;
552
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700553 // The method descriptor. This represents the parameters a method
554 // takes and value it returns. This string is a list of the type
555 // descriptors for the parameters enclosed in parenthesis followed
556 // by the return type descriptor. For example, for the method
557 //
558 // Object mymethod(int i, double d, Thread t)
559 //
560 // the method descriptor would be
561 //
562 // (IDLjava/lang/Thread;)Ljava/lang/Object;
563 String* descriptor_;
564
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700565 // Method prototype descriptor string (return and argument types).
566 uint32_t proto_idx_;
567
568 // The short-form method descriptor string.
569 StringPiece shorty_;
570
571 // A pointer to the memory-mapped DEX code.
572 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700573
574 private:
575 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700576
577 // Compiled code associated with this method
578 const void* code_;
579
580 // Any native method registered with this method
581 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700582};
583
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700584class Array : public Object {
585 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700586 static Array* Alloc(Class* array_class,
587 size_t component_count,
588 size_t component_size) {
589 size_t size = sizeof(Array) + component_count * component_size;
590 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
591 if (array != NULL) {
592 array->SetLength(component_count);
593 }
594 return array;
595 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700596 uint32_t GetLength() const {
597 return length_;
598 }
599 void SetLength(uint32_t length) {
600 length_ = length;
601 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700602
603 private:
604 // The number of array elements.
605 uint32_t length_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700606 Array();
607};
608
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700609template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700610class ObjectArray : public Array {
611 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700612 static ObjectArray<T>* Alloc(Class* object_array_class,
613 size_t length) {
614 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
615 length,
616 sizeof(uint32_t)));
617 }
618
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700619 T* const * GetData() const {
620 return reinterpret_cast<T* const *>(&elements_);
621 }
622 T** GetData() {
623 return reinterpret_cast<T**>(&elements_);
624 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700625 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700626 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700627 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700628 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700629 void Set(uint32_t i, T* object) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700630 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700631 GetData()[i] = object;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700632 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700633 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 -0700634 for (size_t i = 0; i < length; i++) {
635 dst->Set(dst_pos + i, src->Get(src_pos + i));
636 }
637 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700638 ObjectArray<T>* CopyOf(size_t new_length) {
639 ObjectArray<T>* new_array = Alloc(klass_, new_length);
640 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
641 return new_array;
642 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700643
644 private:
645 ObjectArray();
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700646 // Location of first element.
647 T* elements_[0];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700648};
649
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700650// ClassLoader objects.
651class ClassLoader : public Object {
652 public:
653 std::vector<const DexFile*>& GetClassPath() {
654 return class_path_;
655 }
656 void SetClassPath(std::vector<const DexFile*>& class_path) {
657 DCHECK_EQ(0U, class_path_.size());
658 class_path_ = class_path;
659 }
660
661 private:
662 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
663 Object* packages_;
664 ClassLoader* parent_;
665
666 // TODO remove once we can create a real PathClassLoader
667 std::vector<const DexFile*> class_path_;
668
669 ClassLoader();
670};
671
672class BaseDexClassLoader : public ClassLoader {
673 private:
674 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
675 String* original_path_;
676 Object* path_list_;
677 BaseDexClassLoader();
678};
679
680class PathClassLoader : public BaseDexClassLoader {
681 private:
682 PathClassLoader();
683};
684
Carl Shapiro1fb86202011-06-27 17:43:13 -0700685// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700686class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700687 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700688
689 // Class Status
690 //
691 // kStatusNotReady: If a Class cannot be found in the class table by
692 // FindClass, it allocates an new one with AllocClass in the
693 // kStatusNotReady and calls LoadClass. Note if it does find a
694 // class, it may not be kStatusResolved and it will try to push it
695 // forward toward kStatusResolved.
696 //
697 // kStatusIdx: LoadClass populates with Class with information from
698 // the DexFile, moving the status to kStatusIdx, indicating that the
699 // Class values in super_class_ and interfaces_ have not been
700 // populated based on super_class_idx_ and interfaces_idx_. The new
701 // Class can then be inserted into the classes table.
702 //
703 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
704 // attempt to move a kStatusIdx class forward to kStatusLoaded by
705 // using ResolveClass to initialize the super_class_ and interfaces_.
706 //
707 // kStatusResolved: Still holding the lock on Class, the ClassLinker
708 // will use LinkClass to link all members, creating Field and Method
709 // objects, setting up the vtable, etc. On success, the class is
710 // marked kStatusResolved.
711
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700712 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700713 kStatusError = -1,
714 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700715 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700716 kStatusLoaded = 2, // DEX idx values resolved
717 kStatusResolved = 3, // part of linking
718 kStatusVerifying = 4, // in the process of being verified
719 kStatusVerified = 5, // logically part of linking; done pre-init
720 kStatusInitializing = 6, // class init in progress
721 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700722 };
723
724 enum PrimitiveType {
725 kPrimNot = -1
726 };
727
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700728 Class* GetSuperClass() const {
729 return super_class_;
730 }
731
732 uint32_t GetSuperClassIdx() const {
733 return super_class_idx_;
734 }
735
736 bool HasSuperClass() const {
737 return super_class_ != NULL;
738 }
739
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700740 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700741 return class_loader_;
742 }
743
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700744 DexCache* GetDexCache() const {
745 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700746 }
747
748 Class* GetComponentType() const {
749 return component_type_;
750 }
751
752 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700753 DCHECK_NE(0, descriptor_.size());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700754 return descriptor_;
755 }
756
757 Status GetStatus() const {
758 return status_;
759 }
760
761 void SetStatus(Status new_status) {
762 // TODO: validate transition
763 status_ = new_status;
764 }
765
Carl Shapiro69759ea2011-07-21 18:13:35 -0700766 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700767 bool IsErroneous() const {
768 return GetStatus() == kStatusError;
769 }
770
Carl Shapiro69759ea2011-07-21 18:13:35 -0700771 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700772 bool IsVerified() const {
773 return GetStatus() >= kStatusVerified;
774 }
775
Carl Shapiro69759ea2011-07-21 18:13:35 -0700776 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700777 bool IsLinked() const {
778 return GetStatus() >= kStatusResolved;
779 }
780
Carl Shapiro69759ea2011-07-21 18:13:35 -0700781 bool IsLoaded() const {
782 return GetStatus() >= kStatusLoaded;
783 }
784
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700785 // Returns true if this class is in the same packages as that class.
786 bool IsInSamePackage(const Class* that) const;
787
Ian Rogersb033c752011-07-20 12:22:35 -0700788 static bool IsInSamePackage(const StringPiece& descriptor1,
789 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700790
791 // Returns true if this class represents an array class.
792 bool IsArray() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700793 return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700794 }
795
796 // Returns true if the class is an interface.
797 bool IsInterface() const {
798 return (access_flags_ & kAccInterface) != 0;
799 }
800
801 // Returns true if the class is declared public.
802 bool IsPublic() const {
803 return (access_flags_ & kAccPublic) != 0;
804 }
805
806 // Returns true if the class is declared final.
807 bool IsFinal() const {
808 return (access_flags_ & kAccFinal) != 0;
809 }
810
811 // Returns true if the class is abstract.
812 bool IsAbstract() const {
813 return (access_flags_ & kAccAbstract) != 0;
814 }
815
816 // Returns true if the class is an annotation.
817 bool IsAnnotation() const {
818 return (access_flags_ & kAccAnnotation) != 0;
819 }
820
821 // Returns true if the class is a primitive type.
822 bool IsPrimitive() const {
823 return primitive_type_ != kPrimNot;
824 }
825
Brian Carlstromae3ac012011-07-27 01:30:28 -0700826 // Returns true if the class is synthetic.
827 bool IsSynthetic() const {
828 return (access_flags_ & kAccSynthetic) != 0;
829 }
830
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700831 // Returns true if this class can access that class.
832 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700833 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700834 }
835
Carl Shapiro1fb86202011-06-27 17:43:13 -0700836 // Returns the size in bytes of a class object instance with the
837 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700838 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700839 // return OFFSETOF_MEMBER(Class, sfields_) +
840 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700841 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700842
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700843 // Returns the number of static, private, and constructor methods.
844 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700845 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700846 }
847
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700848 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700849 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700850 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700851 }
852
853 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700854 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700855 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700856 }
857
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700858 Method* FindDeclaredDirectMethod(const StringPiece& name,
859 const StringPiece& descriptor);
860
861 Method* FindDirectMethod(const StringPiece& name,
862 const StringPiece& descriptor);
863
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700864 // Returns the number of non-inherited virtual methods.
865 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700866 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700867 }
868
869 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700870 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700871 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700872 }
873
874 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700875 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700876 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700877 }
878
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700879 Method* FindDeclaredVirtualMethod(const StringPiece& name,
880 const StringPiece& descriptor);
881
882 Method* FindVirtualMethod(const StringPiece& name,
883 const StringPiece& descriptor);
884
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700885 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700886 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700887 }
888
Carl Shapiro69759ea2011-07-21 18:13:35 -0700889 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700890 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700891 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700892 }
893
894 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700895 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700896 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700897 }
898
899 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700900 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700901 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700902 }
903
904 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700905 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700906 }
907
Carl Shapiro69759ea2011-07-21 18:13:35 -0700908 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700909 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700910 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700911 }
912
913 void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700914 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700915 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700916 }
917
918 uint32_t GetReferenceOffsets() const {
919 return reference_offsets_;
920 }
921
922 void SetReferenceOffsets(uint32_t new_reference_offsets) {
923 reference_offsets_ = new_reference_offsets;
924 }
925
Carl Shapiro69759ea2011-07-21 18:13:35 -0700926 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700927 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700928 }
929
930 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700931 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700932 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700933 }
934
935 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700936 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700937 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700938 }
939
Ian Rogersb033c752011-07-20 12:22:35 -0700940 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700941 // leave space for instance data; we could access fields directly if
942 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700943#define CLASS_FIELD_SLOTS 1
944 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700945 uint32_t instance_data_[CLASS_FIELD_SLOTS];
946#undef CLASS_FIELD_SLOTS
947
948 // UTF-8 descriptor for the class from constant pool
949 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700950 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700951
952 // Proxy classes have their descriptor allocated on the native heap.
953 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700954 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700955
956 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700957 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700958
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700959 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700960 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700961 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700962
963 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700964 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700965
966 // if class verify fails, we must return same error on subsequent tries
967 Class* verify_error_class_;
968
969 // threadId, used to check for recursive <clinit> invocation
970 uint32_t clinit_thread_id_;
971
972 // Total object size; used when allocating storage on gc heap. (For
973 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700974 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700975
976 // For array classes, the class object for base element, for
977 // instanceof/checkcast (for String[][][], this will be String).
978 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700979 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700980
981 // For array classes, the number of array dimensions, e.g. int[][]
982 // is 2. Otherwise 0.
983 int32_t array_rank_;
984
985 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
986 PrimitiveType primitive_type_;
987
988 // The superclass, or NULL if this is java.lang.Object or a
989 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700990 Class* super_class_; // TODO: make an instance field
991 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700992
993 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700994 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700995
996 // initiating class loader list
997 // NOTE: for classes with low serialNumber, these are unused, and the
998 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700999 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001000
1001 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001002 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001003 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001004
1005 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001006 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001007
1008 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001009 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001010
1011 // Virtual method table (vtable), for use by "invoke-virtual". The
1012 // vtable from the superclass is copied in, and virtual methods from
1013 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001014 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001015
1016 // Interface table (iftable), one entry per interface supported by
1017 // this class. That means one entry for each interface we support
1018 // directly, indirectly via superclass, or indirectly via
1019 // superinterface. This will be null if neither we nor our
1020 // superclass implement any interfaces.
1021 //
1022 // Why we need this: given "class Foo implements Face", declare
1023 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1024 // is part of the Face interface. We can't easily use a single
1025 // vtable.
1026 //
1027 // For every interface a concrete class implements, we create a list
1028 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001029 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001030 InterfaceEntry* iftable_;
1031
1032 // The interface vtable indices for iftable get stored here. By
1033 // placing them all in a single pool for each class that implements
1034 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001035 size_t ifvi_pool_count_;
1036 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001037
1038 // instance fields
1039 //
1040 // These describe the layout of the contents of a
1041 // DataObject-compatible Object. Note that only the fields directly
1042 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001043 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001044 //
1045 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001046 // the beginning of the field list. num_reference_instance_fields_
1047 // specifies the number of reference fields.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001048 ObjectArray<InstanceField>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001049
1050 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001051 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001052
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001053 // Bitmap of offsets of ifields.
1054 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001055
1056 // source file name, if known. Otherwise, NULL.
1057 const char* source_file_;
1058
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001059 ObjectArray<Object>* static_references_;
1060 IntArray* static_32bit_primitives_;
1061 LongArray* static_64bit_primitives_;
1062
Carl Shapiro1fb86202011-06-27 17:43:13 -07001063 // Static fields
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001064 ObjectArray<StaticField>* sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001065
1066 private:
1067 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -07001068};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001069std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001070
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001071inline Object* Object::Alloc(Class* klass) {
Jesse Wilson14150742011-07-29 19:04:44 -04001072 DCHECK(klass != NULL);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001073 return Heap::AllocObject(klass, klass->object_size_);
1074}
1075
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001076class DataObject : public Object {
1077 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001078 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001079 private:
1080 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001081};
1082
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001083template<class T>
1084class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001085 public:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001086 static PrimitiveArray<T>* Alloc(Class* element_class, size_t length) {
1087 return down_cast<PrimitiveArray<T>*>(Array::Alloc(element_class,
1088 length,
1089 sizeof(T)));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001090 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001091
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001092 const T* GetData() const {
1093 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001094 }
1095
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001096 T* GetData() {
1097 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001098 }
1099
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001100 T Get(T i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001101 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001102 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001103 }
1104
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001105 void Set(uint32_t i, T value) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001106 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001107 GetData()[i] = value;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001108 }
1109
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001110 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001111 PrimitiveArray();
1112 // Location of first element.
1113 T elements_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001114};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001115
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001116class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001117 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001118 const CharArray* GetCharArray() const {
1119 return array_;
1120 }
1121
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001122 int32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001123 return hash_code_;
1124 }
1125
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001126 int32_t GetOffset() const {
1127 DCHECK_LE(0, offset_);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001128 return offset_;
1129 }
1130
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001131 int32_t GetLength() const {
1132 DCHECK_LE(0, count_);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001133 return count_;
1134 }
1135
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001136 uint16_t CharAt(int32_t index) const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001137 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001138 }
1139
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001140 static String* AllocFromUtf16(int32_t utf16_length,
1141 uint16_t* utf16_data_in,
1142 int32_t hash_code) {
1143 String* string = Alloc(GetJavaLangString(), GetCharArrayClass(), utf16_length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001144 // TODO use 16-bit wide memset variant
1145 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001146 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001147 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001148 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001149 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001150 }
1151
1152 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1153 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001154 int32_t utf16_length,
1155 const char* utf8_data_in) {
1156 String* string = Alloc(java_lang_String, char_array, utf16_length);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001157 uint16_t* utf16_data_out = string->array_->GetData();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001158 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001159 string->ComputeHashCode();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001160 return string;
1161 }
1162
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001163 // Creates a String of the given ASCII characters. It is an error to call this
1164 // using non-ASCII characters as this function assumes one char per byte.
1165 static String* AllocFromAscii(const char* ascii_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001166 return AllocFromModifiedUtf8(GetJavaLangString(),
1167 GetCharArrayClass(),
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001168 strlen(ascii_data_in),
1169 ascii_data_in);
1170 }
1171
Jesse Wilson8989d992011-08-02 13:39:42 -07001172 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1173 const char* utf8_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001174 return AllocFromModifiedUtf8(GetJavaLangString(), GetCharArrayClass(),
1175 utf16_length, utf8_data_in);
Jesse Wilson8989d992011-08-02 13:39:42 -07001176 }
1177
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001178 static void InitClasses(Class* java_lang_String, Class* char_array);
1179
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001180 static String* Alloc(Class* java_lang_String,
1181 Class* char_array,
1182 int32_t utf16_length) {
1183 String* string = down_cast<String*>(Object::Alloc(java_lang_String));
1184 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1185 string->array_ = array;
1186 string->count_ = utf16_length;
1187 return string;
1188 }
1189
1190 // Convert Modified UTF-8 to UTF-16
1191 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1192 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1193 while (*utf8_data_in != '\0') {
1194 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1195 }
1196 }
1197
1198 // Retrieve the next UTF-16 character from a UTF-8 string.
1199 //
1200 // Advances "*pUtf8Ptr" to the start of the next character.
1201 //
1202 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1203 // of a 3-byte sequence, you can end up overrunning the buffer with
1204 // reads (and possibly with the writes if the length was computed and
1205 // cached before the damage). For performance reasons, this function
1206 // assumes that the string being parsed is known to be valid (e.g., by
1207 // already being verified). Most strings we process here are coming
1208 // out of dex files or other internal translations, so the only real
1209 // risk comes from the JNI NewStringUTF call.
1210 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1211 uint8_t one = *(*utf8_data_in)++;
1212 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001213 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001214 return one;
1215 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001216 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001217 uint8_t two = *(*utf8_data_in)++;
1218 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001219 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001220 return ((one & 0x1f) << 6) |
1221 (two & 0x3f);
1222 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001223 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001224 uint8_t three = *(*utf8_data_in)++;
1225 return ((one & 0x0f) << 12) |
1226 ((two & 0x3f) << 6) |
1227 (three & 0x3f);
1228 }
1229
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001230 // Like "strlen", but for strings encoded with "modified" UTF-8.
1231 //
1232 // The value returned is the number of characters, which may or may not
1233 // be the same as the number of bytes.
1234 //
1235 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1236 // get increment {1-3} from table of 8 values.)
1237 static size_t ModifiedUtf8Len(const char* utf8) {
1238 size_t len = 0;
1239 int ic;
1240 while ((ic = *utf8++) != '\0') {
1241 len++;
1242 if ((ic & 0x80) == 0) {
1243 // one-byte encoding
1244 continue;
1245 }
1246 // two- or three-byte encoding
1247 utf8++;
1248 if ((ic & 0x20) == 0) {
1249 // two-byte encoding
1250 continue;
1251 }
1252 // three-byte encoding
1253 utf8++;
1254 }
1255 return len;
1256 }
1257
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001258 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001259 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1260 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001261 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001262 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001263 }
1264 return hash;
1265 }
1266
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001267 void ComputeHashCode() {
1268 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1269 }
1270
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001271 bool Equals(const char* modified_utf8) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001272 for (int32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001273 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1274 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001275 return false;
1276 }
1277 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001278 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001279 }
1280
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001281 bool Equals(const StringPiece& modified_utf8) const {
1282 // TODO: do not assume C-string representation.
1283 return Equals(modified_utf8.data());
1284 }
1285
1286 bool Equals(const String* that) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001287 // TODO short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001288 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001289 return false;
1290 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001291 for (int32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001292 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001293 return false;
1294 }
1295 }
1296 return true;
1297 }
1298
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001299 bool Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
1300 if (this->GetLength() != that_length) {
1301 return false;
1302 }
1303 for (int32_t i = 0; i < that_length; ++i) {
1304 if (this->CharAt(i) != that_chars[that_offset + i]) {
1305 return false;
1306 }
1307 }
1308 return true;
1309 }
1310
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001311 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001312 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1313 CharArray* array_;
1314
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001315 int32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001316
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001317 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001318
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001319 int32_t count_;
1320
1321 static Class* GetJavaLangString() {
1322 DCHECK(java_lang_String_ != NULL);
1323 return java_lang_String_;
1324 }
1325 static Class* GetCharArrayClass() {
1326 DCHECK(char_array_ != NULL);
1327 return char_array_;
1328 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001329
1330 static Class* java_lang_String_;
1331 static Class* char_array_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001332
1333 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001334};
1335
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001336class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001337 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001338 Class* GetClass() const {
1339 return klass_;
1340 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001341
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001342 void SetClass(Class* klass) {
1343 klass_ = klass;
1344 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001345
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001346 private:
1347 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001348 Class* klass_;
1349
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001350 public: // TODO: private
1351 // Index into array of vtable offsets. This points into the
1352 // ifviPool, which holds the vtables for all interfaces declared by
1353 // this class.
1354 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001355};
1356
1357} // namespace art
1358
1359#endif // ART_SRC_OBJECT_H_