blob: ce956bb3fd738c9fa475ca66f439674ce1bee7a6 [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:
Carl Shapirof88c9522011-08-06 15:47:38 -0700220 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700221};
222
223class ObjectLock {
224 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700225 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700226 CHECK(object != NULL);
227 obj_->MonitorEnter();
228 }
229
230 ~ObjectLock() {
231 obj_->MonitorExit();
232 }
233
234 void Wait(int64_t millis = 0) {
235 return obj_->Wait(millis);
236 }
237
238 void Notify() {
239 obj_->Notify();
240 }
241
242 void NotifyAll() {
243 obj_->NotifyAll();
244 }
245
246 private:
247 Object* obj_;
248 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700249};
250
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400251class AccessibleObject : public Object {
252 private:
253 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
254 uint32_t java_flag_;
255};
256
257class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700258 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700259 Class* GetDeclaringClass() const {
260 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700261 }
262
Jesse Wilson14150742011-07-29 19:04:44 -0400263 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700264 return name_;
265 }
266
267 bool IsStatic() const {
268 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700269 }
270
271 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700272 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700273 }
274
Brian Carlstromae3ac012011-07-27 01:30:28 -0700275 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700276 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700277 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700278 }
279
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700280 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400281 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
282 Class* java_declaring_class_;
283 Object* java_generic_type_;
284 uint32_t java_generic_types_are_initialized_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700285 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400286 uint32_t java_slot_;
287 Class* java_type_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700288
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700289 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700290 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700291
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700292 // e.g. "I", "[C", "Landroid/os/Debug;"
Brian Carlstromae3ac012011-07-27 01:30:28 -0700293 StringPiece descriptor_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700294
295 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700296
297 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700298 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700299};
300
301// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700302class InstanceField : public Field {
303 public:
304 uint32_t GetOffset() const {
305 return offset_;
306 }
307
308 void SetOffset(size_t num_bytes) {
309 offset_ = num_bytes;
310 }
311
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700312 private:
313 size_t offset_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700314
315 DISALLOW_IMPLICIT_CONSTRUCTORS(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_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700382
383 DISALLOW_IMPLICIT_CONSTRUCTORS(StaticField);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700384};
385
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400386class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700387 public:
Brian Carlstromae3ac012011-07-27 01:30:28 -0700388 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700389 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700390 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391 }
392
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700393 const String* GetDescriptor() const {
394 return descriptor_;
395 }
396
Brian Carlstroma0808032011-07-18 00:39:23 -0700397 Class* GetDeclaringClass() const {
398 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700399 }
400
Ian Rogersb033c752011-07-20 12:22:35 -0700401 static MemberOffset ClassOffset() {
402 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
403 }
404
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700405 // Returns true if the method is declared public.
406 bool IsPublic() const {
407 return (access_flags_ & kAccPublic) != 0;
408 }
409
410 // Returns true if the method is declared private.
411 bool IsPrivate() const {
412 return (access_flags_ & kAccPrivate) != 0;
413 }
414
415 // Returns true if the method is declared static.
416 bool IsStatic() const {
417 return (access_flags_ & kAccStatic) != 0;
418 }
419
420 // Returns true if the method is declared synchronized.
421 bool IsSynchronized() const {
422 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
423 return (access_flags_ & synchonized) != 0;
424 }
425
426 // Returns true if the method is declared final.
427 bool IsFinal() const {
428 return (access_flags_ & kAccFinal) != 0;
429 }
430
431 // Returns true if the method is declared native.
432 bool IsNative() const {
433 return (access_flags_ & kAccNative) != 0;
434 }
435
436 // Returns true if the method is declared abstract.
437 bool IsAbstract() const {
438 return (access_flags_ & kAccAbstract) != 0;
439 }
440
441 bool IsSynthetic() const {
442 return (access_flags_ & kAccSynthetic) != 0;
443 }
444
445 // Number of argument registers required by the prototype.
446 uint32_t NumArgRegisters();
447
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700448 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400449 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
450 Class* java_declaring_class_;
451 ObjectArray<Class>* java_exception_types_;
452 Object* java_formal_type_parameters_;
453 Object* java_generic_exception_types_;
454 Object* java_generic_parameter_types_;
455 Object* java_generic_return_type_;
456 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700457 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400458 ObjectArray<Class>* java_parameter_types_;
459 uint32_t java_generic_types_are_initialized_;
460 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700461
Ian Rogersb033c752011-07-20 12:22:35 -0700462 bool IsReturnAReference() const {
463 return (shorty_[0] == 'L') || (shorty_[0] == '[');
464 }
465
466 bool IsReturnAFloatOrDouble() const {
467 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
468 }
469
470 bool IsReturnAFloat() const {
471 return shorty_[0] == 'F';
472 }
473
474 bool IsReturnADouble() const {
475 return shorty_[0] == 'D';
476 }
477
478 bool IsReturnALong() const {
479 return shorty_[0] == 'J';
480 }
481
Ian Rogers45a76cb2011-07-21 22:00:15 -0700482 bool IsReturnVoid() const {
483 return shorty_[0] == 'V';
484 }
485
Ian Rogersb033c752011-07-20 12:22:35 -0700486 // The number of arguments that should be supplied to this method
487 size_t NumArgs() const {
488 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
489 }
490
491 // The number of reference arguments to this method including implicit this
492 // pointer
493 size_t NumReferenceArgs() const;
494
495 // The number of long or double arguments
496 size_t NumLongOrDoubleArgs() const;
497
498 // The number of reference arguments to this method before the given
499 // parameter index
500 size_t NumReferenceArgsBefore(unsigned int param) const;
501
502 // Is the given method parameter a reference?
503 bool IsParamAReference(unsigned int param) const;
504
505 // Is the given method parameter a long or double?
506 bool IsParamALongOrDouble(unsigned int param) const;
507
Ian Rogersdf20fe02011-07-20 20:34:16 -0700508 // Size in bytes of the given parameter
509 size_t ParamSize(unsigned int param) const;
510
511 // Size in bytes of the return value
512 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700513
514 void SetCode(const void* code) {
515 code_ = code;
516 }
517
518 const void* GetCode() const {
519 return code_;
520 }
521
522 void RegisterNative(const void* native_method) {
523 native_method_ = native_method;
524 }
525
526 static MemberOffset NativeMethodOffset() {
527 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
528 }
529
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700530 bool HasSameNameAndDescriptor(const Method* that) const;
531
Ian Rogersb033c752011-07-20 12:22:35 -0700532 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700533 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700534 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535
536 // access flags; low 16 bits are defined by spec (could be uint16_t?)
537 uint32_t access_flags_;
538
539 // For concrete virtual methods, this is the offset of the method
540 // in "vtable".
541 //
542 // For abstract methods in an interface class, this is the offset
543 // of the method in "iftable[n]->methodIndexArray".
544 uint16_t method_index_;
545
546 // Method bounds; not needed for an abstract method.
547 //
548 // For a native method, we compute the size of the argument list, and
549 // set "insSize" and "registerSize" equal to it.
550 uint16_t num_registers_; // ins + locals
551 uint16_t num_outs_;
552 uint16_t num_ins_;
553
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700554 // The method descriptor. This represents the parameters a method
555 // takes and value it returns. This string is a list of the type
556 // descriptors for the parameters enclosed in parenthesis followed
557 // by the return type descriptor. For example, for the method
558 //
559 // Object mymethod(int i, double d, Thread t)
560 //
561 // the method descriptor would be
562 //
563 // (IDLjava/lang/Thread;)Ljava/lang/Object;
564 String* descriptor_;
565
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700566 // Method prototype descriptor string (return and argument types).
567 uint32_t proto_idx_;
568
569 // The short-form method descriptor string.
570 StringPiece shorty_;
571
572 // A pointer to the memory-mapped DEX code.
573 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700574
575 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700576 // Compiled code associated with this method
577 const void* code_;
578
579 // Any native method registered with this method
580 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700581
582 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700583};
584
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700585class Array : public Object {
586 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700587 static Array* Alloc(Class* array_class,
588 size_t component_count,
589 size_t component_size) {
590 size_t size = sizeof(Array) + component_count * component_size;
591 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
592 if (array != NULL) {
593 array->SetLength(component_count);
594 }
595 return array;
596 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700597
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700598 uint32_t GetLength() const {
599 return length_;
600 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700601
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700602 void SetLength(uint32_t length) {
603 length_ = length;
604 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700605
606 private:
607 // The number of array elements.
608 uint32_t length_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700609
Carl Shapirof88c9522011-08-06 15:47:38 -0700610 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700611};
612
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700613template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700614class ObjectArray : public Array {
615 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700616 static ObjectArray<T>* Alloc(Class* object_array_class,
617 size_t length) {
618 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
619 length,
620 sizeof(uint32_t)));
621 }
622
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700623 T* const * GetData() const {
624 return reinterpret_cast<T* const *>(&elements_);
625 }
626 T** GetData() {
627 return reinterpret_cast<T**>(&elements_);
628 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700629 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700630 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700631 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700632 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700633
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700634 void Set(uint32_t i, T* object) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700635 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700636 GetData()[i] = object;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700637 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700638
639 static void Copy(ObjectArray<T>* src, int src_pos,
640 ObjectArray<T>* dst, int dst_pos,
641 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700642 for (size_t i = 0; i < length; i++) {
643 dst->Set(dst_pos + i, src->Get(src_pos + i));
644 }
645 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700646
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700647 ObjectArray<T>* CopyOf(size_t new_length) {
648 ObjectArray<T>* new_array = Alloc(klass_, new_length);
649 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
650 return new_array;
651 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700652
653 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700654 // Location of first element.
655 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700656
657 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700658};
659
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700660// ClassLoader objects.
661class ClassLoader : public Object {
662 public:
663 std::vector<const DexFile*>& GetClassPath() {
664 return class_path_;
665 }
666 void SetClassPath(std::vector<const DexFile*>& class_path) {
667 DCHECK_EQ(0U, class_path_.size());
668 class_path_ = class_path;
669 }
670
671 private:
672 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
673 Object* packages_;
674 ClassLoader* parent_;
675
676 // TODO remove once we can create a real PathClassLoader
677 std::vector<const DexFile*> class_path_;
678
Carl Shapirof88c9522011-08-06 15:47:38 -0700679 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700680};
681
682class BaseDexClassLoader : public ClassLoader {
683 private:
684 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
685 String* original_path_;
686 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700687 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700688};
689
690class PathClassLoader : public BaseDexClassLoader {
691 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700692 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700693};
694
Carl Shapiro1fb86202011-06-27 17:43:13 -0700695// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700696class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700697 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700698
699 // Class Status
700 //
701 // kStatusNotReady: If a Class cannot be found in the class table by
702 // FindClass, it allocates an new one with AllocClass in the
703 // kStatusNotReady and calls LoadClass. Note if it does find a
704 // class, it may not be kStatusResolved and it will try to push it
705 // forward toward kStatusResolved.
706 //
707 // kStatusIdx: LoadClass populates with Class with information from
708 // the DexFile, moving the status to kStatusIdx, indicating that the
709 // Class values in super_class_ and interfaces_ have not been
710 // populated based on super_class_idx_ and interfaces_idx_. The new
711 // Class can then be inserted into the classes table.
712 //
713 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
714 // attempt to move a kStatusIdx class forward to kStatusLoaded by
715 // using ResolveClass to initialize the super_class_ and interfaces_.
716 //
717 // kStatusResolved: Still holding the lock on Class, the ClassLinker
718 // will use LinkClass to link all members, creating Field and Method
719 // objects, setting up the vtable, etc. On success, the class is
720 // marked kStatusResolved.
721
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700722 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700723 kStatusError = -1,
724 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700725 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700726 kStatusLoaded = 2, // DEX idx values resolved
727 kStatusResolved = 3, // part of linking
728 kStatusVerifying = 4, // in the process of being verified
729 kStatusVerified = 5, // logically part of linking; done pre-init
730 kStatusInitializing = 6, // class init in progress
731 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700732 };
733
734 enum PrimitiveType {
735 kPrimNot = -1
736 };
737
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700738 Class* GetSuperClass() const {
739 return super_class_;
740 }
741
742 uint32_t GetSuperClassIdx() const {
743 return super_class_idx_;
744 }
745
746 bool HasSuperClass() const {
747 return super_class_ != NULL;
748 }
749
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700750 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700751 return class_loader_;
752 }
753
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700754 DexCache* GetDexCache() const {
755 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700756 }
757
758 Class* GetComponentType() const {
759 return component_type_;
760 }
761
762 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700763 DCHECK_NE(0, descriptor_.size());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700764 return descriptor_;
765 }
766
767 Status GetStatus() const {
768 return status_;
769 }
770
771 void SetStatus(Status new_status) {
772 // TODO: validate transition
773 status_ = new_status;
774 }
775
Carl Shapiro69759ea2011-07-21 18:13:35 -0700776 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700777 bool IsErroneous() const {
778 return GetStatus() == kStatusError;
779 }
780
Carl Shapiro69759ea2011-07-21 18:13:35 -0700781 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700782 bool IsVerified() const {
783 return GetStatus() >= kStatusVerified;
784 }
785
Carl Shapiro69759ea2011-07-21 18:13:35 -0700786 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700787 bool IsLinked() const {
788 return GetStatus() >= kStatusResolved;
789 }
790
Carl Shapiro69759ea2011-07-21 18:13:35 -0700791 bool IsLoaded() const {
792 return GetStatus() >= kStatusLoaded;
793 }
794
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700795 // Returns true if this class is in the same packages as that class.
796 bool IsInSamePackage(const Class* that) const;
797
Ian Rogersb033c752011-07-20 12:22:35 -0700798 static bool IsInSamePackage(const StringPiece& descriptor1,
799 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700800
801 // Returns true if this class represents an array class.
802 bool IsArray() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700803 return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700804 }
805
806 // Returns true if the class is an interface.
807 bool IsInterface() const {
808 return (access_flags_ & kAccInterface) != 0;
809 }
810
811 // Returns true if the class is declared public.
812 bool IsPublic() const {
813 return (access_flags_ & kAccPublic) != 0;
814 }
815
816 // Returns true if the class is declared final.
817 bool IsFinal() const {
818 return (access_flags_ & kAccFinal) != 0;
819 }
820
821 // Returns true if the class is abstract.
822 bool IsAbstract() const {
823 return (access_flags_ & kAccAbstract) != 0;
824 }
825
826 // Returns true if the class is an annotation.
827 bool IsAnnotation() const {
828 return (access_flags_ & kAccAnnotation) != 0;
829 }
830
831 // Returns true if the class is a primitive type.
832 bool IsPrimitive() const {
833 return primitive_type_ != kPrimNot;
834 }
835
Brian Carlstromae3ac012011-07-27 01:30:28 -0700836 // Returns true if the class is synthetic.
837 bool IsSynthetic() const {
838 return (access_flags_ & kAccSynthetic) != 0;
839 }
840
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700841 // Returns true if this class can access that class.
842 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700843 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700844 }
845
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700846 // Returns the number of static, private, and constructor methods.
847 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700848 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700849 }
850
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700851 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700852 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700853 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700854 }
855
856 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700857 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700858 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700859 }
860
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700861 Method* FindDeclaredDirectMethod(const StringPiece& name,
862 const StringPiece& descriptor);
863
864 Method* FindDirectMethod(const StringPiece& name,
865 const StringPiece& descriptor);
866
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700867 // Returns the number of non-inherited virtual methods.
868 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700869 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700870 }
871
872 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700873 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700874 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700875 }
876
877 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700878 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700879 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700880 }
881
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700882 Method* FindDeclaredVirtualMethod(const StringPiece& name,
883 const StringPiece& descriptor);
884
885 Method* FindVirtualMethod(const StringPiece& name,
886 const StringPiece& descriptor);
887
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700888 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700889 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700890 }
891
Carl Shapiro69759ea2011-07-21 18:13:35 -0700892 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700893 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700894 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700895 }
896
897 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700898 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700899 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700900 }
901
902 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700903 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700904 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700905 }
906
907 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700908 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700909 }
910
Carl Shapiro69759ea2011-07-21 18:13:35 -0700911 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700912 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700913 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700914 }
915
916 void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700917 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700918 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700919 }
920
921 uint32_t GetReferenceOffsets() const {
922 return reference_offsets_;
923 }
924
925 void SetReferenceOffsets(uint32_t new_reference_offsets) {
926 reference_offsets_ = new_reference_offsets;
927 }
928
Carl Shapiro69759ea2011-07-21 18:13:35 -0700929 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700930 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700931 }
932
933 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700934 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700935 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700936 }
937
938 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700939 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700940 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700941 }
942
Ian Rogersb033c752011-07-20 12:22:35 -0700943 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700944 // leave space for instance data; we could access fields directly if
945 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700946#define CLASS_FIELD_SLOTS 1
947 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700948 uint32_t instance_data_[CLASS_FIELD_SLOTS];
949#undef CLASS_FIELD_SLOTS
950
951 // UTF-8 descriptor for the class from constant pool
952 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700953 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700954
955 // Proxy classes have their descriptor allocated on the native heap.
956 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700957 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700958
959 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700960 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700961
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700962 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700963 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700964 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700965
966 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700967 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700968
969 // if class verify fails, we must return same error on subsequent tries
970 Class* verify_error_class_;
971
972 // threadId, used to check for recursive <clinit> invocation
973 uint32_t clinit_thread_id_;
974
975 // Total object size; used when allocating storage on gc heap. (For
976 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700977 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700978
979 // For array classes, the class object for base element, for
980 // instanceof/checkcast (for String[][][], this will be String).
981 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700982 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700983
984 // For array classes, the number of array dimensions, e.g. int[][]
985 // is 2. Otherwise 0.
986 int32_t array_rank_;
987
988 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
989 PrimitiveType primitive_type_;
990
991 // The superclass, or NULL if this is java.lang.Object or a
992 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700993 Class* super_class_; // TODO: make an instance field
994 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700995
996 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700997 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700998
999 // initiating class loader list
1000 // NOTE: for classes with low serialNumber, these are unused, and the
1001 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001002 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001003
1004 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001005 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001006 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001007
1008 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001009 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001010
1011 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001012 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001013
1014 // Virtual method table (vtable), for use by "invoke-virtual". The
1015 // vtable from the superclass is copied in, and virtual methods from
1016 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001017 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001018
1019 // Interface table (iftable), one entry per interface supported by
1020 // this class. That means one entry for each interface we support
1021 // directly, indirectly via superclass, or indirectly via
1022 // superinterface. This will be null if neither we nor our
1023 // superclass implement any interfaces.
1024 //
1025 // Why we need this: given "class Foo implements Face", declare
1026 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1027 // is part of the Face interface. We can't easily use a single
1028 // vtable.
1029 //
1030 // For every interface a concrete class implements, we create a list
1031 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001032 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001033 InterfaceEntry* iftable_;
1034
1035 // The interface vtable indices for iftable get stored here. By
1036 // placing them all in a single pool for each class that implements
1037 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001038 size_t ifvi_pool_count_;
1039 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001040
1041 // instance fields
1042 //
1043 // These describe the layout of the contents of a
1044 // DataObject-compatible Object. Note that only the fields directly
1045 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001046 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001047 //
1048 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001049 // the beginning of the field list. num_reference_instance_fields_
1050 // specifies the number of reference fields.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001051 ObjectArray<InstanceField>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001052
1053 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001054 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001055
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001056 // Bitmap of offsets of ifields.
1057 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001058
1059 // source file name, if known. Otherwise, NULL.
1060 const char* source_file_;
1061
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001062 ObjectArray<Object>* static_references_;
1063 IntArray* static_32bit_primitives_;
1064 LongArray* static_64bit_primitives_;
1065
Carl Shapiro1fb86202011-06-27 17:43:13 -07001066 // Static fields
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001067 ObjectArray<StaticField>* sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001068
1069 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001070 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001071};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001072std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001073
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001074inline Object* Object::Alloc(Class* klass) {
Jesse Wilson14150742011-07-29 19:04:44 -04001075 DCHECK(klass != NULL);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001076 return Heap::AllocObject(klass, klass->object_size_);
1077}
1078
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001079class DataObject : public Object {
1080 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001081 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001082 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001083 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001084};
1085
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001086template<class T>
1087class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001088 public:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001089 static PrimitiveArray<T>* Alloc(Class* element_class, size_t length) {
1090 return down_cast<PrimitiveArray<T>*>(Array::Alloc(element_class,
1091 length,
1092 sizeof(T)));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001093 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001094
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001095 const T* GetData() const {
1096 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001097 }
1098
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001099 T* GetData() {
1100 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001101 }
1102
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001103 T Get(T i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001104 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001105 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001106 }
1107
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001108 void Set(uint32_t i, T value) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001109 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001110 GetData()[i] = value;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001111 }
1112
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001113 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001114 // Location of first element.
1115 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001116
1117 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001118};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001119
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001120class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001121 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001122 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001123 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001124 return array_;
1125 }
1126
Carl Shapirof88c9522011-08-06 15:47:38 -07001127 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001128 return hash_code_;
1129 }
1130
Carl Shapirof88c9522011-08-06 15:47:38 -07001131 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001132 return offset_;
1133 }
1134
Carl Shapirof88c9522011-08-06 15:47:38 -07001135 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001136 return count_;
1137 }
1138
Carl Shapirof88c9522011-08-06 15:47:38 -07001139 uint16_t CharAt(uint32_t index) const {
1140 DCHECK_LE(index, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001141 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001142 }
1143
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001144 static String* AllocFromUtf16(int32_t utf16_length,
1145 uint16_t* utf16_data_in,
1146 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001147 String* string = Alloc(GetJavaLangString(),
1148 GetCharArrayClass(),
1149 utf16_length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001150 // TODO use 16-bit wide memset variant
1151 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001152 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001153 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001154 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001155 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001156 }
1157
1158 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1159 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001160 int32_t utf16_length,
1161 const char* utf8_data_in) {
1162 String* string = Alloc(java_lang_String, char_array, utf16_length);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001163 uint16_t* utf16_data_out = string->array_->GetData();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001164 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001165 string->ComputeHashCode();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001166 return string;
1167 }
1168
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001169 // Creates a String of the given ASCII characters. It is an error to call this
1170 // using non-ASCII characters as this function assumes one char per byte.
1171 static String* AllocFromAscii(const char* ascii_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001172 return AllocFromModifiedUtf8(GetJavaLangString(),
1173 GetCharArrayClass(),
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001174 strlen(ascii_data_in),
1175 ascii_data_in);
1176 }
1177
Jesse Wilson8989d992011-08-02 13:39:42 -07001178 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1179 const char* utf8_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001180 return AllocFromModifiedUtf8(GetJavaLangString(), GetCharArrayClass(),
1181 utf16_length, utf8_data_in);
Jesse Wilson8989d992011-08-02 13:39:42 -07001182 }
1183
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001184 static void InitClasses(Class* java_lang_String, Class* char_array);
1185
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001186 static String* Alloc(Class* java_lang_String,
1187 Class* char_array,
1188 int32_t utf16_length) {
1189 String* string = down_cast<String*>(Object::Alloc(java_lang_String));
1190 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1191 string->array_ = array;
1192 string->count_ = utf16_length;
1193 return string;
1194 }
1195
1196 // Convert Modified UTF-8 to UTF-16
1197 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1198 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1199 while (*utf8_data_in != '\0') {
1200 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1201 }
1202 }
1203
1204 // Retrieve the next UTF-16 character from a UTF-8 string.
1205 //
1206 // Advances "*pUtf8Ptr" to the start of the next character.
1207 //
1208 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1209 // of a 3-byte sequence, you can end up overrunning the buffer with
1210 // reads (and possibly with the writes if the length was computed and
1211 // cached before the damage). For performance reasons, this function
1212 // assumes that the string being parsed is known to be valid (e.g., by
1213 // already being verified). Most strings we process here are coming
1214 // out of dex files or other internal translations, so the only real
1215 // risk comes from the JNI NewStringUTF call.
1216 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1217 uint8_t one = *(*utf8_data_in)++;
1218 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001219 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001220 return one;
1221 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001222 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001223 uint8_t two = *(*utf8_data_in)++;
1224 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001225 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001226 return ((one & 0x1f) << 6) |
1227 (two & 0x3f);
1228 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001229 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001230 uint8_t three = *(*utf8_data_in)++;
1231 return ((one & 0x0f) << 12) |
1232 ((two & 0x3f) << 6) |
1233 (three & 0x3f);
1234 }
1235
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001236 // Like "strlen", but for strings encoded with "modified" UTF-8.
1237 //
1238 // The value returned is the number of characters, which may or may not
1239 // be the same as the number of bytes.
1240 //
1241 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1242 // get increment {1-3} from table of 8 values.)
1243 static size_t ModifiedUtf8Len(const char* utf8) {
1244 size_t len = 0;
1245 int ic;
1246 while ((ic = *utf8++) != '\0') {
1247 len++;
1248 if ((ic & 0x80) == 0) {
1249 // one-byte encoding
1250 continue;
1251 }
1252 // two- or three-byte encoding
1253 utf8++;
1254 if ((ic & 0x20) == 0) {
1255 // two-byte encoding
1256 continue;
1257 }
1258 // three-byte encoding
1259 utf8++;
1260 }
1261 return len;
1262 }
1263
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001264 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001265 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1266 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001267 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001268 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001269 }
1270 return hash;
1271 }
1272
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001273 void ComputeHashCode() {
1274 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1275 }
1276
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001277 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001278 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001279 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1280 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001281 return false;
1282 }
1283 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001284 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001285 }
1286
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001287 bool Equals(const StringPiece& modified_utf8) const {
1288 // TODO: do not assume C-string representation.
1289 return Equals(modified_utf8.data());
1290 }
1291
1292 bool Equals(const String* that) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001293 // TODO short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001294 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001295 return false;
1296 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001297 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001298 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001299 return false;
1300 }
1301 }
1302 return true;
1303 }
1304
Carl Shapirof88c9522011-08-06 15:47:38 -07001305 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001306 if (this->GetLength() != that_length) {
1307 return false;
1308 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001309 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001310 if (this->CharAt(i) != that_chars[that_offset + i]) {
1311 return false;
1312 }
1313 }
1314 return true;
1315 }
1316
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001317 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001318 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1319 CharArray* array_;
1320
Carl Shapirof88c9522011-08-06 15:47:38 -07001321 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001322
Carl Shapirof88c9522011-08-06 15:47:38 -07001323 uint32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001324
Carl Shapirof88c9522011-08-06 15:47:38 -07001325 uint32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001326
1327 static Class* GetJavaLangString() {
1328 DCHECK(java_lang_String_ != NULL);
1329 return java_lang_String_;
1330 }
1331 static Class* GetCharArrayClass() {
1332 DCHECK(char_array_ != NULL);
1333 return char_array_;
1334 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001335
1336 static Class* java_lang_String_;
1337 static Class* char_array_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001338
1339 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001340};
1341
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001342class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001343 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001344 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1345 }
1346
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001347 Class* GetClass() const {
1348 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001349 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001350
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 void SetClass(Class* klass) {
1352 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001353 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001354
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355 private:
1356 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001357 Class* klass_;
1358
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001359 public: // TODO: private
1360 // Index into array of vtable offsets. This points into the
1361 // ifviPool, which holds the vtables for all interfaces declared by
1362 // this class.
1363 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001364
1365 private:
1366 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001367};
1368
1369} // namespace art
1370
1371#endif // ART_SRC_OBJECT_H_