blob: 0e0728decef4fdfa4de92f6e14d5b410828d69a8 [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;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070028class StaticField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070029
Carl Shapiro3ee755d2011-06-28 12:11:04 -070030union JValue {
31 uint8_t z;
32 int8_t b;
33 uint16_t c;
34 int16_t s;
35 int32_t i;
36 int64_t j;
37 float f;
38 double d;
39 Object* l;
40};
41
Brian Carlstrombe977852011-07-19 14:54:54 -070042static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
43static const uint32_t kAccPrivate = 0x0002; // field, method, ic
44static const uint32_t kAccProtected = 0x0004; // field, method, ic
45static const uint32_t kAccStatic = 0x0008; // field, method, ic
46static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
47static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
48static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
49static const uint32_t kAccVolatile = 0x0040; // field
50static const uint32_t kAccBridge = 0x0040; // method (1.5)
51static const uint32_t kAccTransient = 0x0080; // field
52static const uint32_t kAccVarargs = 0x0080; // method (1.5)
53static const uint32_t kAccNative = 0x0100; // method
54static const uint32_t kAccInterface = 0x0200; // class, ic
55static const uint32_t kAccAbstract = 0x0400; // class, method, ic
56static const uint32_t kAccStrict = 0x0800; // method
57static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
58static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
59static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070060
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070061static const uint32_t kAccMiranda = 0x8000; // method
62
Brian Carlstroma331b3c2011-07-18 17:47:56 -070063static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
64
Brian Carlstrombe977852011-07-19 14:54:54 -070065static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
66static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070067
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070069 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070070 */
71/*
72 * A magic value for refOffsets. Ignore the bits and walk the super
73 * chain when this is the value.
74 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
75 * fields followed by 2 ref instance fields.]
76 */
77#define CLASS_WALK_SUPER ((unsigned int)(3))
78#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
79#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
80#define CLASS_OFFSET_ALIGNMENT 4
81#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
82/*
83 * Given an offset, return the bit number which would encode that offset.
84 * Local use only.
85 */
86#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
87 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
88 CLASS_OFFSET_ALIGNMENT)
89/*
90 * Is the given offset too large to be encoded?
91 */
92#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
93 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
94/*
95 * Return a single bit, encoding the offset.
96 * Undefined if the offset is too large, as defined above.
97 */
98#define CLASS_BIT_FROM_OFFSET(byteOffset) \
99 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
100/*
101 * Return an offset, given a bit number as returned from CLZ.
102 */
103#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700104 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700105
106
Carl Shapiro1fb86202011-06-27 17:43:13 -0700107class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700108 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700109 static Object* Alloc(Class* klass);
110
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700111 Class* GetClass() const {
112 return klass_;
113 }
114
115 void MonitorEnter() {
116 monitor_->Enter();
117 }
118
119 void MonitorExit() {
120 monitor_->Exit();
121 }
122
123 void Notify() {
124 monitor_->Notify();
125 }
126
127 void NotifyAll() {
128 monitor_->NotifyAll();
129 }
130
131 void Wait() {
132 monitor_->Wait();
133 }
134
135 void Wait(int64_t timeout) {
136 monitor_->Wait(timeout);
137 }
138
139 void Wait(int64_t timeout, int32_t nanos) {
140 monitor_->Wait(timeout, nanos);
141 }
142
Carl Shapiro69759ea2011-07-21 18:13:35 -0700143 const Object* GetFieldObject(size_t field_offset) const {
144 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
145 return *reinterpret_cast<Object* const*>(raw_addr);
146 }
147
148 Object* GetFieldObject(size_t field_offset) {
149 return const_cast<Object*>(GetFieldObject(field_offset));
150 }
151
152 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700153 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
154 *reinterpret_cast<Object**>(raw_addr) = new_value;
155 // TODO: write barrier
156 }
157
Carl Shapiro69759ea2011-07-21 18:13:35 -0700158 bool IsClass() const {
159 LOG(FATAL) << "Unimplemented";
160 return true;
161 }
162
163 Class* AsClass() {
164 return down_cast<Class*>(this);
165 }
166
167 const Class* AsClass() const {
168 return down_cast<const Class*>(this);
169 }
170
171 bool IsObjectArray() const {
172 LOG(FATAL) << "Unimplemented";
173 return true;
174 }
175
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700176 const ObjectArray<Object>* AsObjectArray() const {
177 return down_cast<const ObjectArray<Object>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700178 }
179
180 bool IsReference() const {
181 LOG(FATAL) << "Unimplemented";
182 return true;
183 }
184
185 bool IsWeakReference() const {
186 LOG(FATAL) << "Unimplemented";
187 return true;
188 }
189
190 bool IsSoftReference() const {
191 LOG(FATAL) << "Unimplemented";
192 return true;
193 }
194
195 bool IsFinalizerReference() const {
196 LOG(FATAL) << "Unimplemented";
197 return true;
198 }
199
200 bool IsPhantomReference() const {
201 LOG(FATAL) << "Unimplemented";
202 return true;
203 }
204
205 bool IsArray() const {
206 LOG(FATAL) << "Unimplemented";
207 return true;
208 }
209
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700210 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700211 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700213 Monitor* monitor_;
214
215 private:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700216 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700217 DISALLOW_COPY_AND_ASSIGN(Object);
218};
219
220class ObjectLock {
221 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700222 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223 CHECK(object != NULL);
224 obj_->MonitorEnter();
225 }
226
227 ~ObjectLock() {
228 obj_->MonitorExit();
229 }
230
231 void Wait(int64_t millis = 0) {
232 return obj_->Wait(millis);
233 }
234
235 void Notify() {
236 obj_->Notify();
237 }
238
239 void NotifyAll() {
240 obj_->NotifyAll();
241 }
242
243 private:
244 Object* obj_;
245 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700246};
247
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400248class AccessibleObject : public Object {
249 private:
250 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
251 uint32_t java_flag_;
252};
253
254class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700255 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700256 Class* GetDeclaringClass() const {
257 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700258 }
259
Jesse Wilson14150742011-07-29 19:04:44 -0400260 const String* GetName() const {
261 return java_name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700262 }
263
264 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700265 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700266 }
267
Brian Carlstromae3ac012011-07-27 01:30:28 -0700268 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700269 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700270 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700271 }
272
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700273 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400274 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
275 Class* java_declaring_class_;
276 Object* java_generic_type_;
277 uint32_t java_generic_types_are_initialized_;
278 String* java_name_;
279 uint32_t java_slot_;
280 Class* java_type_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700281
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700282 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700283 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700284
Brian Carlstromae3ac012011-07-27 01:30:28 -0700285 StringPiece name_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700286
287 // e.g. "I", "[C", "Landroid/os/Debug;"
Brian Carlstromae3ac012011-07-27 01:30:28 -0700288 StringPiece descriptor_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700289
290 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700291
292 private:
293 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700294};
295
296// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700297class InstanceField : public Field {
298 public:
299 uint32_t GetOffset() const {
300 return offset_;
301 }
302
303 void SetOffset(size_t num_bytes) {
304 offset_ = num_bytes;
305 }
306
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700307 private:
308 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700309 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700310};
311
312// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700313class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700314 public:
315 void SetBoolean(bool z) {
316 CHECK_EQ(GetType(), 'Z');
317 value_.z = z;
318 }
319
320 void SetByte(int8_t b) {
321 CHECK_EQ(GetType(), 'B');
322 value_.b = b;
323 }
324
325 void SetChar(uint16_t c) {
326 CHECK_EQ(GetType(), 'C');
327 value_.c = c;
328 }
329
330 void SetShort(uint16_t s) {
331 CHECK_EQ(GetType(), 'S');
332 value_.s = s;
333 }
334
335 void SetInt(int32_t i) {
336 CHECK_EQ(GetType(), 'I');
337 value_.i = i;
338 }
339
340 int64_t GetLong() {
341 CHECK_EQ(GetType(), 'J');
342 return value_.j;
343 }
344
345 void SetLong(int64_t j) {
346 CHECK_EQ(GetType(), 'J');
347 value_.j = j;
348 }
349
350 void SetFloat(float f) {
351 CHECK_EQ(GetType(), 'F');
352 value_.f = f;
353 }
354
355 void SetDouble(double d) {
356 CHECK_EQ(GetType(), 'D');
357 value_.d = d;
358 }
359
Carl Shapiro69759ea2011-07-21 18:13:35 -0700360 Object* GetObject() {
361 return value_.l;
362 }
363
364 const Object* GetObject() const {
365 return value_.l;
366 }
367
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700368 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700369 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700370 value_.l = l;
371 // TODO: write barrier
372 }
373
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700374 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700375 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700376 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700377};
378
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400379class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380 public:
Brian Carlstromae3ac012011-07-27 01:30:28 -0700381 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700382 const String* GetName() const {
383 return java_name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700384 }
385
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700386 const String* GetDescriptor() const {
387 return descriptor_;
388 }
389
Brian Carlstroma0808032011-07-18 00:39:23 -0700390 Class* GetDeclaringClass() const {
391 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700392 }
393
Ian Rogersb033c752011-07-20 12:22:35 -0700394 static MemberOffset ClassOffset() {
395 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
396 }
397
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700398 // Returns true if the method is declared public.
399 bool IsPublic() const {
400 return (access_flags_ & kAccPublic) != 0;
401 }
402
403 // Returns true if the method is declared private.
404 bool IsPrivate() const {
405 return (access_flags_ & kAccPrivate) != 0;
406 }
407
408 // Returns true if the method is declared static.
409 bool IsStatic() const {
410 return (access_flags_ & kAccStatic) != 0;
411 }
412
413 // Returns true if the method is declared synchronized.
414 bool IsSynchronized() const {
415 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
416 return (access_flags_ & synchonized) != 0;
417 }
418
419 // Returns true if the method is declared final.
420 bool IsFinal() const {
421 return (access_flags_ & kAccFinal) != 0;
422 }
423
424 // Returns true if the method is declared native.
425 bool IsNative() const {
426 return (access_flags_ & kAccNative) != 0;
427 }
428
429 // Returns true if the method is declared abstract.
430 bool IsAbstract() const {
431 return (access_flags_ & kAccAbstract) != 0;
432 }
433
434 bool IsSynthetic() const {
435 return (access_flags_ & kAccSynthetic) != 0;
436 }
437
438 // Number of argument registers required by the prototype.
439 uint32_t NumArgRegisters();
440
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700441 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400442 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
443 Class* java_declaring_class_;
444 ObjectArray<Class>* java_exception_types_;
445 Object* java_formal_type_parameters_;
446 Object* java_generic_exception_types_;
447 Object* java_generic_parameter_types_;
448 Object* java_generic_return_type_;
449 Class* java_return_type_;
450 String* java_name_;
451 ObjectArray<Class>* java_parameter_types_;
452 uint32_t java_generic_types_are_initialized_;
453 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700454
Ian Rogersb033c752011-07-20 12:22:35 -0700455 bool IsReturnAReference() const {
456 return (shorty_[0] == 'L') || (shorty_[0] == '[');
457 }
458
459 bool IsReturnAFloatOrDouble() const {
460 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
461 }
462
463 bool IsReturnAFloat() const {
464 return shorty_[0] == 'F';
465 }
466
467 bool IsReturnADouble() const {
468 return shorty_[0] == 'D';
469 }
470
471 bool IsReturnALong() const {
472 return shorty_[0] == 'J';
473 }
474
Ian Rogers45a76cb2011-07-21 22:00:15 -0700475 bool IsReturnVoid() const {
476 return shorty_[0] == 'V';
477 }
478
Ian Rogersb033c752011-07-20 12:22:35 -0700479 // The number of arguments that should be supplied to this method
480 size_t NumArgs() const {
481 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
482 }
483
484 // The number of reference arguments to this method including implicit this
485 // pointer
486 size_t NumReferenceArgs() const;
487
488 // The number of long or double arguments
489 size_t NumLongOrDoubleArgs() const;
490
491 // The number of reference arguments to this method before the given
492 // parameter index
493 size_t NumReferenceArgsBefore(unsigned int param) const;
494
495 // Is the given method parameter a reference?
496 bool IsParamAReference(unsigned int param) const;
497
498 // Is the given method parameter a long or double?
499 bool IsParamALongOrDouble(unsigned int param) const;
500
Ian Rogersdf20fe02011-07-20 20:34:16 -0700501 // Size in bytes of the given parameter
502 size_t ParamSize(unsigned int param) const;
503
504 // Size in bytes of the return value
505 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700506
507 void SetCode(const void* code) {
508 code_ = code;
509 }
510
511 const void* GetCode() const {
512 return code_;
513 }
514
515 void RegisterNative(const void* native_method) {
516 native_method_ = native_method;
517 }
518
519 static MemberOffset NativeMethodOffset() {
520 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
521 }
522
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700523 bool HasSameNameAndDescriptor(const Method* that) const;
524
Ian Rogersb033c752011-07-20 12:22:35 -0700525 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700526 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700527 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700528
529 // access flags; low 16 bits are defined by spec (could be uint16_t?)
530 uint32_t access_flags_;
531
532 // For concrete virtual methods, this is the offset of the method
533 // in "vtable".
534 //
535 // For abstract methods in an interface class, this is the offset
536 // of the method in "iftable[n]->methodIndexArray".
537 uint16_t method_index_;
538
539 // Method bounds; not needed for an abstract method.
540 //
541 // For a native method, we compute the size of the argument list, and
542 // set "insSize" and "registerSize" equal to it.
543 uint16_t num_registers_; // ins + locals
544 uint16_t num_outs_;
545 uint16_t num_ins_;
546
547 // method name, e.g. "<init>" or "eatLunch"
548 StringPiece name_;
549
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700550 // The method descriptor. This represents the parameters a method
551 // takes and value it returns. This string is a list of the type
552 // descriptors for the parameters enclosed in parenthesis followed
553 // by the return type descriptor. For example, for the method
554 //
555 // Object mymethod(int i, double d, Thread t)
556 //
557 // the method descriptor would be
558 //
559 // (IDLjava/lang/Thread;)Ljava/lang/Object;
560 String* descriptor_;
561
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700562 // Method prototype descriptor string (return and argument types).
563 uint32_t proto_idx_;
564
565 // The short-form method descriptor string.
566 StringPiece shorty_;
567
568 // A pointer to the memory-mapped DEX code.
569 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700570
571 private:
572 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700573
574 // Compiled code associated with this method
575 const void* code_;
576
577 // Any native method registered with this method
578 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700579};
580
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700581class Array : public Object {
582 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700583 static Array* Alloc(Class* array_class,
584 size_t component_count,
585 size_t component_size) {
586 size_t size = sizeof(Array) + component_count * component_size;
587 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
588 if (array != NULL) {
589 array->SetLength(component_count);
590 }
591 return array;
592 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700593 uint32_t GetLength() const {
594 return length_;
595 }
596 void SetLength(uint32_t length) {
597 length_ = length;
598 }
599 const void* GetData() const {
600 return &elements_;
601 }
602 void* GetData() {
603 return &elements_;
604 }
605
606 private:
607 // The number of array elements.
608 uint32_t length_;
609 // Location of first element.
610 uint32_t elements_[0];
611 Array();
612};
613
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700614template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700615class ObjectArray : public Array {
616 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700617 static ObjectArray<T>* Alloc(Class* object_array_class,
618 size_t length) {
619 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
620 length,
621 sizeof(uint32_t)));
622 }
623
624 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700625 CHECK_LT(i, GetLength());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700626 Object* const * data = reinterpret_cast<Object* const *>(GetData());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700627 return down_cast<T*>(data[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());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700631 T** data = reinterpret_cast<T**>(GetData());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700632 data[i] = object;
633 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700634 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 -0700635 for (size_t i = 0; i < length; i++) {
636 dst->Set(dst_pos + i, src->Get(src_pos + i));
637 }
638 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700639 ObjectArray<T>* CopyOf(size_t new_length) {
640 ObjectArray<T>* new_array = Alloc(klass_, new_length);
641 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
642 return new_array;
643 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700644
645 private:
646 ObjectArray();
647};
648
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700649// ClassLoader objects.
650class ClassLoader : public Object {
651 public:
652 std::vector<const DexFile*>& GetClassPath() {
653 return class_path_;
654 }
655 void SetClassPath(std::vector<const DexFile*>& class_path) {
656 DCHECK_EQ(0U, class_path_.size());
657 class_path_ = class_path;
658 }
659
660 private:
661 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
662 Object* packages_;
663 ClassLoader* parent_;
664
665 // TODO remove once we can create a real PathClassLoader
666 std::vector<const DexFile*> class_path_;
667
668 ClassLoader();
669};
670
671class BaseDexClassLoader : public ClassLoader {
672 private:
673 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
674 String* original_path_;
675 Object* path_list_;
676 BaseDexClassLoader();
677};
678
679class PathClassLoader : public BaseDexClassLoader {
680 private:
681 PathClassLoader();
682};
683
Carl Shapiro1fb86202011-06-27 17:43:13 -0700684// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700685class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700686 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700687
688 // Class Status
689 //
690 // kStatusNotReady: If a Class cannot be found in the class table by
691 // FindClass, it allocates an new one with AllocClass in the
692 // kStatusNotReady and calls LoadClass. Note if it does find a
693 // class, it may not be kStatusResolved and it will try to push it
694 // forward toward kStatusResolved.
695 //
696 // kStatusIdx: LoadClass populates with Class with information from
697 // the DexFile, moving the status to kStatusIdx, indicating that the
698 // Class values in super_class_ and interfaces_ have not been
699 // populated based on super_class_idx_ and interfaces_idx_. The new
700 // Class can then be inserted into the classes table.
701 //
702 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
703 // attempt to move a kStatusIdx class forward to kStatusLoaded by
704 // using ResolveClass to initialize the super_class_ and interfaces_.
705 //
706 // kStatusResolved: Still holding the lock on Class, the ClassLinker
707 // will use LinkClass to link all members, creating Field and Method
708 // objects, setting up the vtable, etc. On success, the class is
709 // marked kStatusResolved.
710
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700711 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700712 kStatusError = -1,
713 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700714 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700715 kStatusLoaded = 2, // DEX idx values resolved
716 kStatusResolved = 3, // part of linking
717 kStatusVerifying = 4, // in the process of being verified
718 kStatusVerified = 5, // logically part of linking; done pre-init
719 kStatusInitializing = 6, // class init in progress
720 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700721 };
722
723 enum PrimitiveType {
724 kPrimNot = -1
725 };
726
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700727 Class* GetSuperClass() const {
728 return super_class_;
729 }
730
731 uint32_t GetSuperClassIdx() const {
732 return super_class_idx_;
733 }
734
735 bool HasSuperClass() const {
736 return super_class_ != NULL;
737 }
738
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700739 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700740 return class_loader_;
741 }
742
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700743 DexCache* GetDexCache() const {
744 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700745 }
746
747 Class* GetComponentType() const {
748 return component_type_;
749 }
750
751 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700752 DCHECK_NE(0, descriptor_.size());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700753 return descriptor_;
754 }
755
756 Status GetStatus() const {
757 return status_;
758 }
759
760 void SetStatus(Status new_status) {
761 // TODO: validate transition
762 status_ = new_status;
763 }
764
Carl Shapiro69759ea2011-07-21 18:13:35 -0700765 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700766 bool IsErroneous() const {
767 return GetStatus() == kStatusError;
768 }
769
Carl Shapiro69759ea2011-07-21 18:13:35 -0700770 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700771 bool IsVerified() const {
772 return GetStatus() >= kStatusVerified;
773 }
774
Carl Shapiro69759ea2011-07-21 18:13:35 -0700775 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700776 bool IsLinked() const {
777 return GetStatus() >= kStatusResolved;
778 }
779
Carl Shapiro69759ea2011-07-21 18:13:35 -0700780 bool IsLoaded() const {
781 return GetStatus() >= kStatusLoaded;
782 }
783
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700784 // Returns true if this class is in the same packages as that class.
785 bool IsInSamePackage(const Class* that) const;
786
Ian Rogersb033c752011-07-20 12:22:35 -0700787 static bool IsInSamePackage(const StringPiece& descriptor1,
788 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700789
790 // Returns true if this class represents an array class.
791 bool IsArray() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700792 return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700793 }
794
795 // Returns true if the class is an interface.
796 bool IsInterface() const {
797 return (access_flags_ & kAccInterface) != 0;
798 }
799
800 // Returns true if the class is declared public.
801 bool IsPublic() const {
802 return (access_flags_ & kAccPublic) != 0;
803 }
804
805 // Returns true if the class is declared final.
806 bool IsFinal() const {
807 return (access_flags_ & kAccFinal) != 0;
808 }
809
810 // Returns true if the class is abstract.
811 bool IsAbstract() const {
812 return (access_flags_ & kAccAbstract) != 0;
813 }
814
815 // Returns true if the class is an annotation.
816 bool IsAnnotation() const {
817 return (access_flags_ & kAccAnnotation) != 0;
818 }
819
820 // Returns true if the class is a primitive type.
821 bool IsPrimitive() const {
822 return primitive_type_ != kPrimNot;
823 }
824
Brian Carlstromae3ac012011-07-27 01:30:28 -0700825 // Returns true if the class is synthetic.
826 bool IsSynthetic() const {
827 return (access_flags_ & kAccSynthetic) != 0;
828 }
829
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700830 // Returns true if this class can access that class.
831 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700832 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700833 }
834
Carl Shapiro1fb86202011-06-27 17:43:13 -0700835 // Returns the size in bytes of a class object instance with the
836 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700837 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700838 // return OFFSETOF_MEMBER(Class, sfields_) +
839 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700840 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700841
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700842 // Returns the number of static, private, and constructor methods.
843 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700844 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700845 }
846
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700847 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700848 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700849 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700850 }
851
852 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700853 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700854 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700855 }
856
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700857 Method* FindDeclaredDirectMethod(const StringPiece& name,
858 const StringPiece& descriptor);
859
860 Method* FindDirectMethod(const StringPiece& name,
861 const StringPiece& descriptor);
862
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700863 // Returns the number of non-inherited virtual methods.
864 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700865 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700866 }
867
868 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700869 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700870 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700871 }
872
873 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700874 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700875 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700876 }
877
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700878 Method* FindDeclaredVirtualMethod(const StringPiece& name,
879 const StringPiece& descriptor);
880
881 Method* FindVirtualMethod(const StringPiece& name,
882 const StringPiece& descriptor);
883
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700884 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700885 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700886 }
887
Carl Shapiro69759ea2011-07-21 18:13:35 -0700888 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700889 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700890 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700891 }
892
893 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700894 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700895 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700896 }
897
898 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700899 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700900 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700901 }
902
903 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700904 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700905 }
906
Carl Shapiro69759ea2011-07-21 18:13:35 -0700907 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700908 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700909 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700910 }
911
912 void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700913 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700914 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700915 }
916
917 uint32_t GetReferenceOffsets() const {
918 return reference_offsets_;
919 }
920
921 void SetReferenceOffsets(uint32_t new_reference_offsets) {
922 reference_offsets_ = new_reference_offsets;
923 }
924
Carl Shapiro69759ea2011-07-21 18:13:35 -0700925 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700926 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700927 }
928
929 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700930 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700931 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700932 }
933
934 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700935 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700936 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700937 }
938
Ian Rogersb033c752011-07-20 12:22:35 -0700939 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700940 // leave space for instance data; we could access fields directly if
941 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700942#define CLASS_FIELD_SLOTS 1
943 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700944 uint32_t instance_data_[CLASS_FIELD_SLOTS];
945#undef CLASS_FIELD_SLOTS
946
947 // UTF-8 descriptor for the class from constant pool
948 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700949 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700950
951 // Proxy classes have their descriptor allocated on the native heap.
952 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700953 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700954
955 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700956 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700957
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700958 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700959 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700960 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700961
962 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700963 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700964
965 // if class verify fails, we must return same error on subsequent tries
966 Class* verify_error_class_;
967
968 // threadId, used to check for recursive <clinit> invocation
969 uint32_t clinit_thread_id_;
970
971 // Total object size; used when allocating storage on gc heap. (For
972 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700973 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700974
975 // For array classes, the class object for base element, for
976 // instanceof/checkcast (for String[][][], this will be String).
977 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700978 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700979
980 // For array classes, the number of array dimensions, e.g. int[][]
981 // is 2. Otherwise 0.
982 int32_t array_rank_;
983
984 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
985 PrimitiveType primitive_type_;
986
987 // The superclass, or NULL if this is java.lang.Object or a
988 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700989 Class* super_class_; // TODO: make an instance field
990 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700991
992 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700993 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700994
995 // initiating class loader list
996 // NOTE: for classes with low serialNumber, these are unused, and the
997 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700998 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700999
1000 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001001 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001002 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001003
1004 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001005 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001006
1007 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001008 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001009
1010 // Virtual method table (vtable), for use by "invoke-virtual". The
1011 // vtable from the superclass is copied in, and virtual methods from
1012 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001013 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001014
1015 // Interface table (iftable), one entry per interface supported by
1016 // this class. That means one entry for each interface we support
1017 // directly, indirectly via superclass, or indirectly via
1018 // superinterface. This will be null if neither we nor our
1019 // superclass implement any interfaces.
1020 //
1021 // Why we need this: given "class Foo implements Face", declare
1022 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1023 // is part of the Face interface. We can't easily use a single
1024 // vtable.
1025 //
1026 // For every interface a concrete class implements, we create a list
1027 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001028 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001029 InterfaceEntry* iftable_;
1030
1031 // The interface vtable indices for iftable get stored here. By
1032 // placing them all in a single pool for each class that implements
1033 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001034 size_t ifvi_pool_count_;
1035 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001036
1037 // instance fields
1038 //
1039 // These describe the layout of the contents of a
1040 // DataObject-compatible Object. Note that only the fields directly
1041 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001042 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001043 //
1044 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001045 // the beginning of the field list. num_reference_instance_fields_
1046 // specifies the number of reference fields.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001047 ObjectArray<InstanceField>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001048
1049 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001050 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001051
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001052 // Bitmap of offsets of ifields.
1053 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001054
1055 // source file name, if known. Otherwise, NULL.
1056 const char* source_file_;
1057
1058 // Static fields
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001059 ObjectArray<StaticField>* sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001060
1061 private:
1062 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -07001063};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001064std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001065
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001066inline Object* Object::Alloc(Class* klass) {
Jesse Wilson14150742011-07-29 19:04:44 -04001067 DCHECK(klass != NULL);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001068 return Heap::AllocObject(klass, klass->object_size_);
1069}
1070
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001071class DataObject : public Object {
1072 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001073 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001074 private:
1075 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001076};
1077
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001078class CharArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001079 public:
1080 static CharArray* Alloc(Class* char_array_class, size_t length) {
1081 return down_cast<CharArray*>(Array::Alloc(char_array_class,
1082 length,
1083 sizeof(uint16_t)));
1084 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001085
1086 uint16_t* GetChars() {
1087 return reinterpret_cast<uint16_t*>(GetData());
1088 }
1089
1090 const uint16_t* GetChars() const {
1091 return reinterpret_cast<const uint16_t*>(GetData());
1092 }
1093
1094 uint16_t GetChar(uint32_t i) const {
1095 CHECK_LT(i, GetLength());
1096 return GetChars()[i];
1097 }
1098
1099 void SetChar(uint32_t i, uint16_t ch) {
1100 CHECK_LT(i, GetLength());
1101 GetChars()[i] = ch;
1102 }
1103
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001104 private:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001105 CharArray();
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001106};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001107
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001108class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001109 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001110 const CharArray* GetCharArray() const {
1111 return array_;
1112 }
1113
1114 uint32_t GetHashCode() const {
1115 return hash_code_;
1116 }
1117
1118 uint32_t GetOffset() const {
1119 return offset_;
1120 }
1121
1122 uint32_t GetLength() const {
1123 return count_;
1124 }
1125
1126 uint16_t CharAt(uint32_t index) const {
1127 return GetCharArray()->GetChar(index + GetOffset());
1128 }
1129
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001130 static String* AllocFromUtf16(Class* java_lang_String,
1131 Class* char_array,
1132 int32_t utf16_length,
1133 uint16_t* utf16_data_in) {
1134 String* string = Alloc(java_lang_String, char_array, utf16_length);
1135 uint16_t* utf16_data_out = string->array_->GetChars();
1136 // TODO use 16-bit wide memset variant
1137 for (int i = 0; i < utf16_length; i++ ) {
1138 utf16_data_out[i] = utf16_data_in[i];
1139 }
1140 string->hash_code_ = ComputeUtf16Hash(utf16_data_out, utf16_length);
1141 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001142 }
1143
1144 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1145 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001146 int32_t utf16_length,
1147 const char* utf8_data_in) {
1148 String* string = Alloc(java_lang_String, char_array, utf16_length);
1149 uint16_t* utf16_data_out = string->array_->GetChars();
1150 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1151 string->hash_code_ = ComputeUtf16Hash(utf16_data_out, utf16_length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001152 return string;
1153 }
1154
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001155 // Creates a String of the given ASCII characters. It is an error to call this
1156 // using non-ASCII characters as this function assumes one char per byte.
1157 static String* AllocFromAscii(const char* ascii_data_in) {
1158 DCHECK(java_lang_String_ != NULL);
1159 DCHECK(char_array_ != NULL);
1160 return AllocFromModifiedUtf8(java_lang_String_,
1161 char_array_,
1162 strlen(ascii_data_in),
1163 ascii_data_in);
1164 }
1165
Jesse Wilson8989d992011-08-02 13:39:42 -07001166 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1167 const char* utf8_data_in) {
1168 return AllocFromModifiedUtf8(java_lang_String_, char_array_, utf16_length, utf8_data_in);
1169 }
1170
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001171 static void InitClasses(Class* java_lang_String, Class* char_array);
1172
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001173 static String* Alloc(Class* java_lang_String,
1174 Class* char_array,
1175 int32_t utf16_length) {
1176 String* string = down_cast<String*>(Object::Alloc(java_lang_String));
1177 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1178 string->array_ = array;
1179 string->count_ = utf16_length;
1180 return string;
1181 }
1182
1183 // Convert Modified UTF-8 to UTF-16
1184 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1185 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1186 while (*utf8_data_in != '\0') {
1187 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1188 }
1189 }
1190
1191 // Retrieve the next UTF-16 character from a UTF-8 string.
1192 //
1193 // Advances "*pUtf8Ptr" to the start of the next character.
1194 //
1195 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1196 // of a 3-byte sequence, you can end up overrunning the buffer with
1197 // reads (and possibly with the writes if the length was computed and
1198 // cached before the damage). For performance reasons, this function
1199 // assumes that the string being parsed is known to be valid (e.g., by
1200 // already being verified). Most strings we process here are coming
1201 // out of dex files or other internal translations, so the only real
1202 // risk comes from the JNI NewStringUTF call.
1203 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1204 uint8_t one = *(*utf8_data_in)++;
1205 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001206 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001207 return one;
1208 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001209 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001210 uint8_t two = *(*utf8_data_in)++;
1211 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001212 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001213 return ((one & 0x1f) << 6) |
1214 (two & 0x3f);
1215 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001216 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001217 uint8_t three = *(*utf8_data_in)++;
1218 return ((one & 0x0f) << 12) |
1219 ((two & 0x3f) << 6) |
1220 (three & 0x3f);
1221 }
1222
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001223 // Like "strlen", but for strings encoded with "modified" UTF-8.
1224 //
1225 // The value returned is the number of characters, which may or may not
1226 // be the same as the number of bytes.
1227 //
1228 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1229 // get increment {1-3} from table of 8 values.)
1230 static size_t ModifiedUtf8Len(const char* utf8) {
1231 size_t len = 0;
1232 int ic;
1233 while ((ic = *utf8++) != '\0') {
1234 len++;
1235 if ((ic & 0x80) == 0) {
1236 // one-byte encoding
1237 continue;
1238 }
1239 // two- or three-byte encoding
1240 utf8++;
1241 if ((ic & 0x20) == 0) {
1242 // two-byte encoding
1243 continue;
1244 }
1245 // three-byte encoding
1246 utf8++;
1247 }
1248 return len;
1249 }
1250
1251
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001252 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001253 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1254 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001255 while (string_length--) {
1256 hash = hash * 31 + *string_data++;
1257 }
1258 return hash;
1259 }
1260
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001261 bool Equals(const char* modified_utf8) const {
1262 for (size_t i = 0; i < GetLength(); ++i) {
1263 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1264 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001265 return false;
1266 }
1267 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001268 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001269 }
1270
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001271 bool Equals(const StringPiece& modified_utf8) const {
1272 // TODO: do not assume C-string representation.
1273 return Equals(modified_utf8.data());
1274 }
1275
1276 bool Equals(const String* that) const {
1277 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001278 return false;
1279 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001280 for (size_t i = 0; i < that->GetLength(); ++i) {
1281 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001282 return false;
1283 }
1284 }
1285 return true;
1286 }
1287
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001288 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001289 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1290 CharArray* array_;
1291
1292 uint32_t hash_code_;
1293
1294 uint32_t offset_;
1295
1296 uint32_t count_;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001297
1298 static Class* java_lang_String_;
1299 static Class* char_array_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001300
1301 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001302};
1303
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001304class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001305 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001306 Class* GetClass() const {
1307 return klass_;
1308 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001309
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001310 void SetClass(Class* klass) {
1311 klass_ = klass;
1312 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001313
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001314 private:
1315 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001316 Class* klass_;
1317
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001318 public: // TODO: private
1319 // Index into array of vtable offsets. This points into the
1320 // ifviPool, which holds the vtables for all interfaces declared by
1321 // this class.
1322 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001323};
1324
1325} // namespace art
1326
1327#endif // ART_SRC_OBJECT_H_