blob: d0f4a6379e5306278376ceaea3e97b7c2289288f [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 // const char* GetReturnTypeDescriptor() const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700399 // return FindDexFile(declaring_class_->GetDexCache()
Ian Rogersb033c752011-07-20 12:22:35 -0700400 // ->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700401 // }
402
403 // Returns true if the method is declared public.
404 bool IsPublic() const {
405 return (access_flags_ & kAccPublic) != 0;
406 }
407
408 // Returns true if the method is declared private.
409 bool IsPrivate() const {
410 return (access_flags_ & kAccPrivate) != 0;
411 }
412
413 // Returns true if the method is declared static.
414 bool IsStatic() const {
415 return (access_flags_ & kAccStatic) != 0;
416 }
417
418 // Returns true if the method is declared synchronized.
419 bool IsSynchronized() const {
420 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
421 return (access_flags_ & synchonized) != 0;
422 }
423
424 // Returns true if the method is declared final.
425 bool IsFinal() const {
426 return (access_flags_ & kAccFinal) != 0;
427 }
428
429 // Returns true if the method is declared native.
430 bool IsNative() const {
431 return (access_flags_ & kAccNative) != 0;
432 }
433
434 // Returns true if the method is declared abstract.
435 bool IsAbstract() const {
436 return (access_flags_ & kAccAbstract) != 0;
437 }
438
439 bool IsSynthetic() const {
440 return (access_flags_ & kAccSynthetic) != 0;
441 }
442
443 // Number of argument registers required by the prototype.
444 uint32_t NumArgRegisters();
445
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700446 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400447 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
448 Class* java_declaring_class_;
449 ObjectArray<Class>* java_exception_types_;
450 Object* java_formal_type_parameters_;
451 Object* java_generic_exception_types_;
452 Object* java_generic_parameter_types_;
453 Object* java_generic_return_type_;
454 Class* java_return_type_;
455 String* java_name_;
456 ObjectArray<Class>* java_parameter_types_;
457 uint32_t java_generic_types_are_initialized_;
458 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700459
Ian Rogersb033c752011-07-20 12:22:35 -0700460 bool IsReturnAReference() const {
461 return (shorty_[0] == 'L') || (shorty_[0] == '[');
462 }
463
464 bool IsReturnAFloatOrDouble() const {
465 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
466 }
467
468 bool IsReturnAFloat() const {
469 return shorty_[0] == 'F';
470 }
471
472 bool IsReturnADouble() const {
473 return shorty_[0] == 'D';
474 }
475
476 bool IsReturnALong() const {
477 return shorty_[0] == 'J';
478 }
479
Ian Rogers45a76cb2011-07-21 22:00:15 -0700480 bool IsReturnVoid() const {
481 return shorty_[0] == 'V';
482 }
483
Ian Rogersb033c752011-07-20 12:22:35 -0700484 // The number of arguments that should be supplied to this method
485 size_t NumArgs() const {
486 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
487 }
488
489 // The number of reference arguments to this method including implicit this
490 // pointer
491 size_t NumReferenceArgs() const;
492
493 // The number of long or double arguments
494 size_t NumLongOrDoubleArgs() const;
495
496 // The number of reference arguments to this method before the given
497 // parameter index
498 size_t NumReferenceArgsBefore(unsigned int param) const;
499
500 // Is the given method parameter a reference?
501 bool IsParamAReference(unsigned int param) const;
502
503 // Is the given method parameter a long or double?
504 bool IsParamALongOrDouble(unsigned int param) const;
505
Ian Rogersdf20fe02011-07-20 20:34:16 -0700506 // Size in bytes of the given parameter
507 size_t ParamSize(unsigned int param) const;
508
509 // Size in bytes of the return value
510 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700511
512 void SetCode(const void* code) {
513 code_ = code;
514 }
515
516 const void* GetCode() const {
517 return code_;
518 }
519
520 void RegisterNative(const void* native_method) {
521 native_method_ = native_method;
522 }
523
524 static MemberOffset NativeMethodOffset() {
525 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
526 }
527
528 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700529 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700530 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700531
532 // access flags; low 16 bits are defined by spec (could be uint16_t?)
533 uint32_t access_flags_;
534
535 // For concrete virtual methods, this is the offset of the method
536 // in "vtable".
537 //
538 // For abstract methods in an interface class, this is the offset
539 // of the method in "iftable[n]->methodIndexArray".
540 uint16_t method_index_;
541
542 // Method bounds; not needed for an abstract method.
543 //
544 // For a native method, we compute the size of the argument list, and
545 // set "insSize" and "registerSize" equal to it.
546 uint16_t num_registers_; // ins + locals
547 uint16_t num_outs_;
548 uint16_t num_ins_;
549
550 // method name, e.g. "<init>" or "eatLunch"
551 StringPiece name_;
552
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700553 // The method descriptor. This represents the parameters a method
554 // takes and value it returns. This string is a list of the type
555 // descriptors for the parameters enclosed in parenthesis followed
556 // by the return type descriptor. For example, for the method
557 //
558 // Object mymethod(int i, double d, Thread t)
559 //
560 // the method descriptor would be
561 //
562 // (IDLjava/lang/Thread;)Ljava/lang/Object;
563 String* descriptor_;
564
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700565 // Method prototype descriptor string (return and argument types).
566 uint32_t proto_idx_;
567
568 // The short-form method descriptor string.
569 StringPiece shorty_;
570
571 // A pointer to the memory-mapped DEX code.
572 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700573
574 private:
575 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700576
577 // Compiled code associated with this method
578 const void* code_;
579
580 // Any native method registered with this method
581 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700582};
583
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700584class Array : public Object {
585 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700586 static Array* Alloc(Class* array_class,
587 size_t component_count,
588 size_t component_size) {
589 size_t size = sizeof(Array) + component_count * component_size;
590 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
591 if (array != NULL) {
592 array->SetLength(component_count);
593 }
594 return array;
595 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700596 uint32_t GetLength() const {
597 return length_;
598 }
599 void SetLength(uint32_t length) {
600 length_ = length;
601 }
602 const void* GetData() const {
603 return &elements_;
604 }
605 void* GetData() {
606 return &elements_;
607 }
608
609 private:
610 // The number of array elements.
611 uint32_t length_;
612 // Location of first element.
613 uint32_t elements_[0];
614 Array();
615};
616
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700617template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700618class ObjectArray : public Array {
619 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700620 static ObjectArray<T>* Alloc(Class* object_array_class,
621 size_t length) {
622 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
623 length,
624 sizeof(uint32_t)));
625 }
626
627 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700628 CHECK_LT(i, GetLength());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700629 Object* const * data = reinterpret_cast<Object* const *>(GetData());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700630 return down_cast<T*>(data[i]);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700631 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700632 void Set(uint32_t i, T* object) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700633 CHECK_LT(i, GetLength());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700634 T** data = reinterpret_cast<T**>(GetData());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700635 data[i] = object;
636 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700637 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 -0700638 for (size_t i = 0; i < length; i++) {
639 dst->Set(dst_pos + i, src->Get(src_pos + i));
640 }
641 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700642 ObjectArray<T>* CopyOf(size_t new_length) {
643 ObjectArray<T>* new_array = Alloc(klass_, new_length);
644 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
645 return new_array;
646 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700647
648 private:
649 ObjectArray();
650};
651
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700652// ClassLoader objects.
653class ClassLoader : public Object {
654 public:
655 std::vector<const DexFile*>& GetClassPath() {
656 return class_path_;
657 }
658 void SetClassPath(std::vector<const DexFile*>& class_path) {
659 DCHECK_EQ(0U, class_path_.size());
660 class_path_ = class_path;
661 }
662
663 private:
664 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
665 Object* packages_;
666 ClassLoader* parent_;
667
668 // TODO remove once we can create a real PathClassLoader
669 std::vector<const DexFile*> class_path_;
670
671 ClassLoader();
672};
673
674class BaseDexClassLoader : public ClassLoader {
675 private:
676 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
677 String* original_path_;
678 Object* path_list_;
679 BaseDexClassLoader();
680};
681
682class PathClassLoader : public BaseDexClassLoader {
683 private:
684 PathClassLoader();
685};
686
Carl Shapiro1fb86202011-06-27 17:43:13 -0700687// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700688class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700689 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700690
691 // Class Status
692 //
693 // kStatusNotReady: If a Class cannot be found in the class table by
694 // FindClass, it allocates an new one with AllocClass in the
695 // kStatusNotReady and calls LoadClass. Note if it does find a
696 // class, it may not be kStatusResolved and it will try to push it
697 // forward toward kStatusResolved.
698 //
699 // kStatusIdx: LoadClass populates with Class with information from
700 // the DexFile, moving the status to kStatusIdx, indicating that the
701 // Class values in super_class_ and interfaces_ have not been
702 // populated based on super_class_idx_ and interfaces_idx_. The new
703 // Class can then be inserted into the classes table.
704 //
705 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
706 // attempt to move a kStatusIdx class forward to kStatusLoaded by
707 // using ResolveClass to initialize the super_class_ and interfaces_.
708 //
709 // kStatusResolved: Still holding the lock on Class, the ClassLinker
710 // will use LinkClass to link all members, creating Field and Method
711 // objects, setting up the vtable, etc. On success, the class is
712 // marked kStatusResolved.
713
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700714 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700715 kStatusError = -1,
716 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700717 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700718 kStatusLoaded = 2, // DEX idx values resolved
719 kStatusResolved = 3, // part of linking
720 kStatusVerifying = 4, // in the process of being verified
721 kStatusVerified = 5, // logically part of linking; done pre-init
722 kStatusInitializing = 6, // class init in progress
723 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700724 };
725
726 enum PrimitiveType {
727 kPrimNot = -1
728 };
729
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700730 Class* GetSuperClass() const {
731 return super_class_;
732 }
733
734 uint32_t GetSuperClassIdx() const {
735 return super_class_idx_;
736 }
737
738 bool HasSuperClass() const {
739 return super_class_ != NULL;
740 }
741
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700742 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700743 return class_loader_;
744 }
745
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700746 DexCache* GetDexCache() const {
747 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700748 }
749
750 Class* GetComponentType() const {
751 return component_type_;
752 }
753
754 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700755 DCHECK_NE(0, descriptor_.size());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700756 return descriptor_;
757 }
758
759 Status GetStatus() const {
760 return status_;
761 }
762
763 void SetStatus(Status new_status) {
764 // TODO: validate transition
765 status_ = new_status;
766 }
767
Carl Shapiro69759ea2011-07-21 18:13:35 -0700768 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700769 bool IsErroneous() const {
770 return GetStatus() == kStatusError;
771 }
772
Carl Shapiro69759ea2011-07-21 18:13:35 -0700773 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700774 bool IsVerified() const {
775 return GetStatus() >= kStatusVerified;
776 }
777
Carl Shapiro69759ea2011-07-21 18:13:35 -0700778 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700779 bool IsLinked() const {
780 return GetStatus() >= kStatusResolved;
781 }
782
Carl Shapiro69759ea2011-07-21 18:13:35 -0700783 bool IsLoaded() const {
784 return GetStatus() >= kStatusLoaded;
785 }
786
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700787 // Returns true if this class is in the same packages as that class.
788 bool IsInSamePackage(const Class* that) const;
789
Ian Rogersb033c752011-07-20 12:22:35 -0700790 static bool IsInSamePackage(const StringPiece& descriptor1,
791 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700792
793 // Returns true if this class represents an array class.
794 bool IsArray() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700795 return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700796 }
797
798 // Returns true if the class is an interface.
799 bool IsInterface() const {
800 return (access_flags_ & kAccInterface) != 0;
801 }
802
803 // Returns true if the class is declared public.
804 bool IsPublic() const {
805 return (access_flags_ & kAccPublic) != 0;
806 }
807
808 // Returns true if the class is declared final.
809 bool IsFinal() const {
810 return (access_flags_ & kAccFinal) != 0;
811 }
812
813 // Returns true if the class is abstract.
814 bool IsAbstract() const {
815 return (access_flags_ & kAccAbstract) != 0;
816 }
817
818 // Returns true if the class is an annotation.
819 bool IsAnnotation() const {
820 return (access_flags_ & kAccAnnotation) != 0;
821 }
822
823 // Returns true if the class is a primitive type.
824 bool IsPrimitive() const {
825 return primitive_type_ != kPrimNot;
826 }
827
Brian Carlstromae3ac012011-07-27 01:30:28 -0700828 // Returns true if the class is synthetic.
829 bool IsSynthetic() const {
830 return (access_flags_ & kAccSynthetic) != 0;
831 }
832
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700833 // Returns true if this class can access that class.
834 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700835 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700836 }
837
Carl Shapiro1fb86202011-06-27 17:43:13 -0700838 // Returns the size in bytes of a class object instance with the
839 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700840 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700841 // return OFFSETOF_MEMBER(Class, sfields_) +
842 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700843 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700844
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700845 // Returns the number of static, private, and constructor methods.
846 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700847 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700848 }
849
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700850 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700851 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700852 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700853 }
854
855 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700856 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700857 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700858 }
859
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700860 Method* FindDeclaredDirectMethod(const StringPiece& name,
861 const StringPiece& descriptor);
862
863 Method* FindDirectMethod(const StringPiece& name,
864 const StringPiece& descriptor);
865
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700866 // Returns the number of non-inherited virtual methods.
867 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700868 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700869 }
870
871 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700872 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700873 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700874 }
875
876 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700877 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700878 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700879 }
880
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700881 Method* FindDeclaredVirtualMethod(const StringPiece& name,
882 const StringPiece& descriptor);
883
884 Method* FindVirtualMethod(const StringPiece& name,
885 const StringPiece& descriptor);
886
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700887 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700888 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700889 }
890
Carl Shapiro69759ea2011-07-21 18:13:35 -0700891 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700892 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700893 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700894 }
895
896 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700897 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700898 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700899 }
900
901 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700902 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700903 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700904 }
905
906 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700907 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700908 }
909
Carl Shapiro69759ea2011-07-21 18:13:35 -0700910 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700911 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700912 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700913 }
914
915 void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700916 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700917 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700918 }
919
920 uint32_t GetReferenceOffsets() const {
921 return reference_offsets_;
922 }
923
924 void SetReferenceOffsets(uint32_t new_reference_offsets) {
925 reference_offsets_ = new_reference_offsets;
926 }
927
Carl Shapiro69759ea2011-07-21 18:13:35 -0700928 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700929 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700930 }
931
932 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700933 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700934 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700935 }
936
937 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700938 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700939 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700940 }
941
Ian Rogersb033c752011-07-20 12:22:35 -0700942 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700943 // leave space for instance data; we could access fields directly if
944 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700945#define CLASS_FIELD_SLOTS 1
946 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700947 uint32_t instance_data_[CLASS_FIELD_SLOTS];
948#undef CLASS_FIELD_SLOTS
949
950 // UTF-8 descriptor for the class from constant pool
951 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700952 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700953
954 // Proxy classes have their descriptor allocated on the native heap.
955 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700956 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700957
958 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700959 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700960
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700961 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700962 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700963 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700964
965 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700966 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700967
968 // if class verify fails, we must return same error on subsequent tries
969 Class* verify_error_class_;
970
971 // threadId, used to check for recursive <clinit> invocation
972 uint32_t clinit_thread_id_;
973
974 // Total object size; used when allocating storage on gc heap. (For
975 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700976 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700977
978 // For array classes, the class object for base element, for
979 // instanceof/checkcast (for String[][][], this will be String).
980 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700981 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700982
983 // For array classes, the number of array dimensions, e.g. int[][]
984 // is 2. Otherwise 0.
985 int32_t array_rank_;
986
987 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
988 PrimitiveType primitive_type_;
989
990 // The superclass, or NULL if this is java.lang.Object or a
991 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700992 Class* super_class_; // TODO: make an instance field
993 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700994
995 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700996 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700997
998 // initiating class loader list
999 // NOTE: for classes with low serialNumber, these are unused, and the
1000 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001001 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001002
1003 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001004 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001005 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001006
1007 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001008 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001009
1010 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001011 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001012
1013 // Virtual method table (vtable), for use by "invoke-virtual". The
1014 // vtable from the superclass is copied in, and virtual methods from
1015 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001016 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001017
1018 // Interface table (iftable), one entry per interface supported by
1019 // this class. That means one entry for each interface we support
1020 // directly, indirectly via superclass, or indirectly via
1021 // superinterface. This will be null if neither we nor our
1022 // superclass implement any interfaces.
1023 //
1024 // Why we need this: given "class Foo implements Face", declare
1025 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1026 // is part of the Face interface. We can't easily use a single
1027 // vtable.
1028 //
1029 // For every interface a concrete class implements, we create a list
1030 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001031 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001032 InterfaceEntry* iftable_;
1033
1034 // The interface vtable indices for iftable get stored here. By
1035 // placing them all in a single pool for each class that implements
1036 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001037 size_t ifvi_pool_count_;
1038 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001039
1040 // instance fields
1041 //
1042 // These describe the layout of the contents of a
1043 // DataObject-compatible Object. Note that only the fields directly
1044 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001045 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001046 //
1047 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001048 // the beginning of the field list. num_reference_instance_fields_
1049 // specifies the number of reference fields.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001050 ObjectArray<InstanceField>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001051
1052 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001053 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001054
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001055 // Bitmap of offsets of ifields.
1056 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001057
1058 // source file name, if known. Otherwise, NULL.
1059 const char* source_file_;
1060
1061 // Static fields
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001062 ObjectArray<StaticField>* sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001063
1064 private:
1065 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -07001066};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001067std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001068
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001069inline Object* Object::Alloc(Class* klass) {
Jesse Wilson14150742011-07-29 19:04:44 -04001070 DCHECK(klass != NULL);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001071 return Heap::AllocObject(klass, klass->object_size_);
1072}
1073
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001074class DataObject : public Object {
1075 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001076 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001077 private:
1078 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001079};
1080
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001081class CharArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001082 public:
1083 static CharArray* Alloc(Class* char_array_class, size_t length) {
1084 return down_cast<CharArray*>(Array::Alloc(char_array_class,
1085 length,
1086 sizeof(uint16_t)));
1087 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001088
1089 uint16_t* GetChars() {
1090 return reinterpret_cast<uint16_t*>(GetData());
1091 }
1092
1093 const uint16_t* GetChars() const {
1094 return reinterpret_cast<const uint16_t*>(GetData());
1095 }
1096
1097 uint16_t GetChar(uint32_t i) const {
1098 CHECK_LT(i, GetLength());
1099 return GetChars()[i];
1100 }
1101
1102 void SetChar(uint32_t i, uint16_t ch) {
1103 CHECK_LT(i, GetLength());
1104 GetChars()[i] = ch;
1105 }
1106
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001107 private:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001108 CharArray();
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001109};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001110
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001111class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001112 public:
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001113 static String* AllocFromUtf16(Class* java_lang_String,
1114 Class* char_array,
1115 int32_t utf16_length,
1116 uint16_t* utf16_data_in) {
1117 String* string = Alloc(java_lang_String, char_array, utf16_length);
1118 uint16_t* utf16_data_out = string->array_->GetChars();
1119 // TODO use 16-bit wide memset variant
1120 for (int i = 0; i < utf16_length; i++ ) {
1121 utf16_data_out[i] = utf16_data_in[i];
1122 }
1123 string->hash_code_ = ComputeUtf16Hash(utf16_data_out, utf16_length);
1124 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001125 }
1126
1127 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1128 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001129 int32_t utf16_length,
1130 const char* utf8_data_in) {
1131 String* string = Alloc(java_lang_String, char_array, utf16_length);
1132 uint16_t* utf16_data_out = string->array_->GetChars();
1133 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1134 string->hash_code_ = ComputeUtf16Hash(utf16_data_out, utf16_length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001135 return string;
1136 }
1137
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001138 // Creates a String of the given ASCII characters. It is an error to call this
1139 // using non-ASCII characters as this function assumes one char per byte.
1140 static String* AllocFromAscii(const char* ascii_data_in) {
1141 DCHECK(java_lang_String_ != NULL);
1142 DCHECK(char_array_ != NULL);
1143 return AllocFromModifiedUtf8(java_lang_String_,
1144 char_array_,
1145 strlen(ascii_data_in),
1146 ascii_data_in);
1147 }
1148
Jesse Wilson8989d992011-08-02 13:39:42 -07001149 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1150 const char* utf8_data_in) {
1151 return AllocFromModifiedUtf8(java_lang_String_, char_array_, utf16_length, utf8_data_in);
1152 }
1153
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001154 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -04001155 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001156 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001157
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001158 int32_t hash_code_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001159
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001160 int32_t offset_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001161
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001162 int32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001163
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001164 static void InitClasses(Class* java_lang_String, Class* char_array);
1165
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001166 static String* Alloc(Class* java_lang_String,
1167 Class* char_array,
1168 int32_t utf16_length) {
1169 String* string = down_cast<String*>(Object::Alloc(java_lang_String));
1170 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1171 string->array_ = array;
1172 string->count_ = utf16_length;
1173 return string;
1174 }
1175
1176 // Convert Modified UTF-8 to UTF-16
1177 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1178 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1179 while (*utf8_data_in != '\0') {
1180 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1181 }
1182 }
1183
1184 // Retrieve the next UTF-16 character from a UTF-8 string.
1185 //
1186 // Advances "*pUtf8Ptr" to the start of the next character.
1187 //
1188 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1189 // of a 3-byte sequence, you can end up overrunning the buffer with
1190 // reads (and possibly with the writes if the length was computed and
1191 // cached before the damage). For performance reasons, this function
1192 // assumes that the string being parsed is known to be valid (e.g., by
1193 // already being verified). Most strings we process here are coming
1194 // out of dex files or other internal translations, so the only real
1195 // risk comes from the JNI NewStringUTF call.
1196 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1197 uint8_t one = *(*utf8_data_in)++;
1198 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001199 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001200 return one;
1201 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001202 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001203 uint8_t two = *(*utf8_data_in)++;
1204 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001205 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001206 return ((one & 0x1f) << 6) |
1207 (two & 0x3f);
1208 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001209 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001210 uint8_t three = *(*utf8_data_in)++;
1211 return ((one & 0x0f) << 12) |
1212 ((two & 0x3f) << 6) |
1213 (three & 0x3f);
1214 }
1215
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001216 // Like "strlen", but for strings encoded with "modified" UTF-8.
1217 //
1218 // The value returned is the number of characters, which may or may not
1219 // be the same as the number of bytes.
1220 //
1221 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1222 // get increment {1-3} from table of 8 values.)
1223 static size_t ModifiedUtf8Len(const char* utf8) {
1224 size_t len = 0;
1225 int ic;
1226 while ((ic = *utf8++) != '\0') {
1227 len++;
1228 if ((ic & 0x80) == 0) {
1229 // one-byte encoding
1230 continue;
1231 }
1232 // two- or three-byte encoding
1233 utf8++;
1234 if ((ic & 0x20) == 0) {
1235 // two-byte encoding
1236 continue;
1237 }
1238 // three-byte encoding
1239 utf8++;
1240 }
1241 return len;
1242 }
1243
1244
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001245 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001246 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1247 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001248 while (string_length--) {
1249 hash = hash * 31 + *string_data++;
1250 }
1251 return hash;
1252 }
1253
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001254 static bool EqualsUtf8(const String* string, const char* other) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001255 uint16_t* chars = string->array_->GetChars();
1256 for (int32_t i = 0; i < string->count_; i++) {
1257 uint16_t c = GetUtf16FromUtf8(&other);
1258 if (c == '\0' || c != chars[string->offset_ + i]) {
1259 return false;
1260 }
1261 }
1262 return *other == '\0';
1263 }
1264
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001265 static bool Equals(const String* a, const String* b) {
1266 // TODO short circuit on hash_code_
1267 int32_t a_count = a->count_;
1268 if (a_count != b->count_) {
1269 return false;
1270 }
1271 int32_t a_offset = a->offset_;
1272 int32_t b_offset = b->offset_;
1273 uint16_t* a_chars = a->array_->GetChars();
1274 uint16_t* b_chars = b->array_->GetChars();
1275 for (int32_t i = 0; i < a_count; i++) {
1276 if (a_chars[a_offset + i] != b_chars[b_offset + i]) {
1277 return false;
1278 }
1279 }
1280 return true;
1281 }
1282
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001283 private:
1284 String();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001285
1286 static Class* java_lang_String_;
1287 static Class* char_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001288};
1289
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001290class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001291 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001292 Class* GetClass() const {
1293 return klass_;
1294 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001295
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001296 void SetClass(Class* klass) {
1297 klass_ = klass;
1298 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001299
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001300 private:
1301 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001302 Class* klass_;
1303
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001304 public: // TODO: private
1305 // Index into array of vtable offsets. This points into the
1306 // ifviPool, which holds the vtables for all interfaces declared by
1307 // this class.
1308 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001309};
1310
1311} // namespace art
1312
1313#endif // ART_SRC_OBJECT_H_