blob: 3a9888eba78c2b10a2499c6e3d9b88d970c45155 [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 Carlstromae3ac012011-07-27 01:30:28 -0700265 return descriptor_[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700266 }
267
Brian Carlstromae3ac012011-07-27 01:30:28 -0700268 const StringPiece& GetDescriptor() const {
269 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700270 }
271
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700272 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400273 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
274 Class* java_declaring_class_;
275 Object* java_generic_type_;
276 uint32_t java_generic_types_are_initialized_;
277 String* java_name_;
278 uint32_t java_slot_;
279 Class* java_type_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700280
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700281 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700282 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700283
Brian Carlstromae3ac012011-07-27 01:30:28 -0700284 StringPiece name_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700285
286 // e.g. "I", "[C", "Landroid/os/Debug;"
Brian Carlstromae3ac012011-07-27 01:30:28 -0700287 StringPiece descriptor_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700288
289 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700290
291 private:
292 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700293};
294
295// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700296class InstanceField : public Field {
297 public:
298 uint32_t GetOffset() const {
299 return offset_;
300 }
301
302 void SetOffset(size_t num_bytes) {
303 offset_ = num_bytes;
304 }
305
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700306 private:
307 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700308 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700309};
310
311// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700312class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700313 public:
314 void SetBoolean(bool z) {
315 CHECK_EQ(GetType(), 'Z');
316 value_.z = z;
317 }
318
319 void SetByte(int8_t b) {
320 CHECK_EQ(GetType(), 'B');
321 value_.b = b;
322 }
323
324 void SetChar(uint16_t c) {
325 CHECK_EQ(GetType(), 'C');
326 value_.c = c;
327 }
328
329 void SetShort(uint16_t s) {
330 CHECK_EQ(GetType(), 'S');
331 value_.s = s;
332 }
333
334 void SetInt(int32_t i) {
335 CHECK_EQ(GetType(), 'I');
336 value_.i = i;
337 }
338
339 int64_t GetLong() {
340 CHECK_EQ(GetType(), 'J');
341 return value_.j;
342 }
343
344 void SetLong(int64_t j) {
345 CHECK_EQ(GetType(), 'J');
346 value_.j = j;
347 }
348
349 void SetFloat(float f) {
350 CHECK_EQ(GetType(), 'F');
351 value_.f = f;
352 }
353
354 void SetDouble(double d) {
355 CHECK_EQ(GetType(), 'D');
356 value_.d = d;
357 }
358
Carl Shapiro69759ea2011-07-21 18:13:35 -0700359 Object* GetObject() {
360 return value_.l;
361 }
362
363 const Object* GetObject() const {
364 return value_.l;
365 }
366
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700367 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700368 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700369 value_.l = l;
370 // TODO: write barrier
371 }
372
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700373 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700374 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700375 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700376};
377
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400378class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700379 public:
Brian Carlstromae3ac012011-07-27 01:30:28 -0700380 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700381 const String* GetName() const {
382 return java_name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700383 }
384
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700385 const String* GetDescriptor() const {
386 return descriptor_;
387 }
388
Brian Carlstroma0808032011-07-18 00:39:23 -0700389 Class* GetDeclaringClass() const {
390 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391 }
392
Ian Rogersb033c752011-07-20 12:22:35 -0700393 static MemberOffset ClassOffset() {
394 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
395 }
396
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700397 // const char* GetReturnTypeDescriptor() const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700398 // return FindDexFile(declaring_class_->GetDexCache()
Ian Rogersb033c752011-07-20 12:22:35 -0700399 // ->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700400 // }
401
402 // Returns true if the method is declared public.
403 bool IsPublic() const {
404 return (access_flags_ & kAccPublic) != 0;
405 }
406
407 // Returns true if the method is declared private.
408 bool IsPrivate() const {
409 return (access_flags_ & kAccPrivate) != 0;
410 }
411
412 // Returns true if the method is declared static.
413 bool IsStatic() const {
414 return (access_flags_ & kAccStatic) != 0;
415 }
416
417 // Returns true if the method is declared synchronized.
418 bool IsSynchronized() const {
419 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
420 return (access_flags_ & synchonized) != 0;
421 }
422
423 // Returns true if the method is declared final.
424 bool IsFinal() const {
425 return (access_flags_ & kAccFinal) != 0;
426 }
427
428 // Returns true if the method is declared native.
429 bool IsNative() const {
430 return (access_flags_ & kAccNative) != 0;
431 }
432
433 // Returns true if the method is declared abstract.
434 bool IsAbstract() const {
435 return (access_flags_ & kAccAbstract) != 0;
436 }
437
438 bool IsSynthetic() const {
439 return (access_flags_ & kAccSynthetic) != 0;
440 }
441
442 // Number of argument registers required by the prototype.
443 uint32_t NumArgRegisters();
444
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700445 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400446 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
447 Class* java_declaring_class_;
448 ObjectArray<Class>* java_exception_types_;
449 Object* java_formal_type_parameters_;
450 Object* java_generic_exception_types_;
451 Object* java_generic_parameter_types_;
452 Object* java_generic_return_type_;
453 Class* java_return_type_;
454 String* java_name_;
455 ObjectArray<Class>* java_parameter_types_;
456 uint32_t java_generic_types_are_initialized_;
457 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700458
Ian Rogersb033c752011-07-20 12:22:35 -0700459 bool IsReturnAReference() const {
460 return (shorty_[0] == 'L') || (shorty_[0] == '[');
461 }
462
463 bool IsReturnAFloatOrDouble() const {
464 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
465 }
466
467 bool IsReturnAFloat() const {
468 return shorty_[0] == 'F';
469 }
470
471 bool IsReturnADouble() const {
472 return shorty_[0] == 'D';
473 }
474
475 bool IsReturnALong() const {
476 return shorty_[0] == 'J';
477 }
478
Ian Rogers45a76cb2011-07-21 22:00:15 -0700479 bool IsReturnVoid() const {
480 return shorty_[0] == 'V';
481 }
482
Ian Rogersb033c752011-07-20 12:22:35 -0700483 // The number of arguments that should be supplied to this method
484 size_t NumArgs() const {
485 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
486 }
487
488 // The number of reference arguments to this method including implicit this
489 // pointer
490 size_t NumReferenceArgs() const;
491
492 // The number of long or double arguments
493 size_t NumLongOrDoubleArgs() const;
494
495 // The number of reference arguments to this method before the given
496 // parameter index
497 size_t NumReferenceArgsBefore(unsigned int param) const;
498
499 // Is the given method parameter a reference?
500 bool IsParamAReference(unsigned int param) const;
501
502 // Is the given method parameter a long or double?
503 bool IsParamALongOrDouble(unsigned int param) const;
504
Ian Rogersdf20fe02011-07-20 20:34:16 -0700505 // Size in bytes of the given parameter
506 size_t ParamSize(unsigned int param) const;
507
508 // Size in bytes of the return value
509 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700510
511 void SetCode(const void* code) {
512 code_ = code;
513 }
514
515 const void* GetCode() const {
516 return code_;
517 }
518
519 void RegisterNative(const void* native_method) {
520 native_method_ = native_method;
521 }
522
523 static MemberOffset NativeMethodOffset() {
524 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
525 }
526
527 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700528 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700529 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700530
531 // access flags; low 16 bits are defined by spec (could be uint16_t?)
532 uint32_t access_flags_;
533
534 // For concrete virtual methods, this is the offset of the method
535 // in "vtable".
536 //
537 // For abstract methods in an interface class, this is the offset
538 // of the method in "iftable[n]->methodIndexArray".
539 uint16_t method_index_;
540
541 // Method bounds; not needed for an abstract method.
542 //
543 // For a native method, we compute the size of the argument list, and
544 // set "insSize" and "registerSize" equal to it.
545 uint16_t num_registers_; // ins + locals
546 uint16_t num_outs_;
547 uint16_t num_ins_;
548
549 // method name, e.g. "<init>" or "eatLunch"
550 StringPiece name_;
551
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700552 // The method descriptor. This represents the parameters a method
553 // takes and value it returns. This string is a list of the type
554 // descriptors for the parameters enclosed in parenthesis followed
555 // by the return type descriptor. For example, for the method
556 //
557 // Object mymethod(int i, double d, Thread t)
558 //
559 // the method descriptor would be
560 //
561 // (IDLjava/lang/Thread;)Ljava/lang/Object;
562 String* descriptor_;
563
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700564 // Method prototype descriptor string (return and argument types).
565 uint32_t proto_idx_;
566
567 // The short-form method descriptor string.
568 StringPiece shorty_;
569
570 // A pointer to the memory-mapped DEX code.
571 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700572
573 private:
574 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700575
576 // Compiled code associated with this method
577 const void* code_;
578
579 // Any native method registered with this method
580 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700581};
582
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700583class Array : public Object {
584 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700585 static Array* Alloc(Class* array_class,
586 size_t component_count,
587 size_t component_size) {
588 size_t size = sizeof(Array) + component_count * component_size;
589 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
590 if (array != NULL) {
591 array->SetLength(component_count);
592 }
593 return array;
594 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700595 uint32_t GetLength() const {
596 return length_;
597 }
598 void SetLength(uint32_t length) {
599 length_ = length;
600 }
601 const void* GetData() const {
602 return &elements_;
603 }
604 void* GetData() {
605 return &elements_;
606 }
607
608 private:
609 // The number of array elements.
610 uint32_t length_;
611 // Location of first element.
612 uint32_t elements_[0];
613 Array();
614};
615
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700616template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700617class ObjectArray : public Array {
618 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700619 static ObjectArray<T>* Alloc(Class* object_array_class,
620 size_t length) {
621 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
622 length,
623 sizeof(uint32_t)));
624 }
625
626 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700627 CHECK_LT(i, GetLength());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700628 Object* const * data = reinterpret_cast<Object* const *>(GetData());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700629 return down_cast<T*>(data[i]);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700630 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700631 void Set(uint32_t i, T* object) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700632 CHECK_LT(i, GetLength());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700633 T** data = reinterpret_cast<T**>(GetData());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700634 data[i] = object;
635 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700636 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 -0700637 for (size_t i = 0; i < length; i++) {
638 dst->Set(dst_pos + i, src->Get(src_pos + i));
639 }
640 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700641 ObjectArray<T>* CopyOf(size_t new_length) {
642 ObjectArray<T>* new_array = Alloc(klass_, new_length);
643 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
644 return new_array;
645 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700646
647 private:
648 ObjectArray();
649};
650
Carl Shapiro1fb86202011-06-27 17:43:13 -0700651// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700652class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700653 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700654 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700655 kStatusError = -1,
656 kStatusNotReady = 0,
657 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
658 kStatusLoaded = 2, // DEX idx values resolved
659 kStatusResolved = 3, // part of linking
660 kStatusVerifying = 4, // in the process of being verified
661 kStatusVerified = 5, // logically part of linking; done pre-init
662 kStatusInitializing = 6, // class init in progress
663 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700664 };
665
666 enum PrimitiveType {
667 kPrimNot = -1
668 };
669
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700670 Class* GetSuperClass() const {
671 return super_class_;
672 }
673
674 uint32_t GetSuperClassIdx() const {
675 return super_class_idx_;
676 }
677
678 bool HasSuperClass() const {
679 return super_class_ != NULL;
680 }
681
682 Object* GetClassLoader() const {
683 return class_loader_;
684 }
685
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700686 DexCache* GetDexCache() const {
687 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700688 }
689
690 Class* GetComponentType() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700691 DCHECK(IsArray());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700692 return component_type_;
693 }
694
695 const StringPiece& GetDescriptor() const {
696 return descriptor_;
697 }
698
699 Status GetStatus() const {
700 return status_;
701 }
702
703 void SetStatus(Status new_status) {
704 // TODO: validate transition
705 status_ = new_status;
706 }
707
Carl Shapiro69759ea2011-07-21 18:13:35 -0700708 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700709 bool IsErroneous() const {
710 return GetStatus() == kStatusError;
711 }
712
Carl Shapiro69759ea2011-07-21 18:13:35 -0700713 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700714 bool IsVerified() const {
715 return GetStatus() >= kStatusVerified;
716 }
717
Carl Shapiro69759ea2011-07-21 18:13:35 -0700718 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700719 bool IsLinked() const {
720 return GetStatus() >= kStatusResolved;
721 }
722
Carl Shapiro69759ea2011-07-21 18:13:35 -0700723 bool IsLoaded() const {
724 return GetStatus() >= kStatusLoaded;
725 }
726
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700727 // Returns true if this class is in the same packages as that class.
728 bool IsInSamePackage(const Class* that) const;
729
Ian Rogersb033c752011-07-20 12:22:35 -0700730 static bool IsInSamePackage(const StringPiece& descriptor1,
731 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700732
733 // Returns true if this class represents an array class.
734 bool IsArray() const {
735 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
736 }
737
738 // Returns true if the class is an interface.
739 bool IsInterface() const {
740 return (access_flags_ & kAccInterface) != 0;
741 }
742
743 // Returns true if the class is declared public.
744 bool IsPublic() const {
745 return (access_flags_ & kAccPublic) != 0;
746 }
747
748 // Returns true if the class is declared final.
749 bool IsFinal() const {
750 return (access_flags_ & kAccFinal) != 0;
751 }
752
753 // Returns true if the class is abstract.
754 bool IsAbstract() const {
755 return (access_flags_ & kAccAbstract) != 0;
756 }
757
758 // Returns true if the class is an annotation.
759 bool IsAnnotation() const {
760 return (access_flags_ & kAccAnnotation) != 0;
761 }
762
763 // Returns true if the class is a primitive type.
764 bool IsPrimitive() const {
765 return primitive_type_ != kPrimNot;
766 }
767
Brian Carlstromae3ac012011-07-27 01:30:28 -0700768 // Returns true if the class is synthetic.
769 bool IsSynthetic() const {
770 return (access_flags_ & kAccSynthetic) != 0;
771 }
772
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700773 // Returns true if this class can access that class.
774 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700775 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700776 }
777
Carl Shapiro1fb86202011-06-27 17:43:13 -0700778 // Returns the size in bytes of a class object instance with the
779 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700780 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700781 // return OFFSETOF_MEMBER(Class, sfields_) +
782 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700783 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700784
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700785 // Returns the number of static, private, and constructor methods.
786 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700787 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700788 }
789
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700790 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700791 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700792 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700793 }
794
795 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700796 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700797 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700798 }
799
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700800 Method* FindDeclaredDirectMethod(const StringPiece& name,
801 const StringPiece& descriptor);
802
803 Method* FindDirectMethod(const StringPiece& name,
804 const StringPiece& descriptor);
805
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700806 // Returns the number of non-inherited virtual methods.
807 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700808 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700809 }
810
811 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700812 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700813 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700814 }
815
816 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700817 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700818 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700819 }
820
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700821 Method* FindDeclaredVirtualMethod(const StringPiece& name,
822 const StringPiece& descriptor);
823
824 Method* FindVirtualMethod(const StringPiece& name,
825 const StringPiece& descriptor);
826
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700827 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700828 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700829 }
830
Carl Shapiro69759ea2011-07-21 18:13:35 -0700831 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700832 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700833 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700834 }
835
836 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700837 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700838 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700839 }
840
841 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700842 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700843 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700844 }
845
846 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700847 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700848 }
849
Carl Shapiro69759ea2011-07-21 18:13:35 -0700850 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700851 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700852 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700853 }
854
855 void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700856 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700857 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700858 }
859
860 uint32_t GetReferenceOffsets() const {
861 return reference_offsets_;
862 }
863
864 void SetReferenceOffsets(uint32_t new_reference_offsets) {
865 reference_offsets_ = new_reference_offsets;
866 }
867
Carl Shapiro69759ea2011-07-21 18:13:35 -0700868 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700869 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700870 }
871
872 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700873 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700874 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700875 }
876
877 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700878 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700879 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700880 }
881
Ian Rogersb033c752011-07-20 12:22:35 -0700882 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700883 // leave space for instance data; we could access fields directly if
884 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700885#define CLASS_FIELD_SLOTS 1
886 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700887 uint32_t instance_data_[CLASS_FIELD_SLOTS];
888#undef CLASS_FIELD_SLOTS
889
890 // UTF-8 descriptor for the class from constant pool
891 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700892 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700893
894 // Proxy classes have their descriptor allocated on the native heap.
895 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700896 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700897
898 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700899 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700900
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700901 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700902 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700903 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700904
905 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700906 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700907
908 // if class verify fails, we must return same error on subsequent tries
909 Class* verify_error_class_;
910
911 // threadId, used to check for recursive <clinit> invocation
912 uint32_t clinit_thread_id_;
913
914 // Total object size; used when allocating storage on gc heap. (For
915 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700916 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700917
918 // For array classes, the class object for base element, for
919 // instanceof/checkcast (for String[][][], this will be String).
920 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700921 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700922
923 // For array classes, the number of array dimensions, e.g. int[][]
924 // is 2. Otherwise 0.
925 int32_t array_rank_;
926
927 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
928 PrimitiveType primitive_type_;
929
930 // The superclass, or NULL if this is java.lang.Object or a
931 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700932 Class* super_class_; // TODO: make an instance field
933 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700934
935 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700936 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700937
938 // initiating class loader list
939 // NOTE: for classes with low serialNumber, these are unused, and the
940 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700941 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700942
943 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700944 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700945 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700946
947 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700948 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700949
950 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700951 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700952
953 // Virtual method table (vtable), for use by "invoke-virtual". The
954 // vtable from the superclass is copied in, and virtual methods from
955 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700956 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700957
958 // Interface table (iftable), one entry per interface supported by
959 // this class. That means one entry for each interface we support
960 // directly, indirectly via superclass, or indirectly via
961 // superinterface. This will be null if neither we nor our
962 // superclass implement any interfaces.
963 //
964 // Why we need this: given "class Foo implements Face", declare
965 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
966 // is part of the Face interface. We can't easily use a single
967 // vtable.
968 //
969 // For every interface a concrete class implements, we create a list
970 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700971 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700972 InterfaceEntry* iftable_;
973
974 // The interface vtable indices for iftable get stored here. By
975 // placing them all in a single pool for each class that implements
976 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700977 size_t ifvi_pool_count_;
978 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700979
980 // instance fields
981 //
982 // These describe the layout of the contents of a
983 // DataObject-compatible Object. Note that only the fields directly
984 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700985 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700986 //
987 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700988 // the beginning of the field list. num_reference_instance_fields_
989 // specifies the number of reference fields.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700990 ObjectArray<InstanceField>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700991
992 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -0700993 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700994
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700995 // Bitmap of offsets of ifields.
996 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700997
998 // source file name, if known. Otherwise, NULL.
999 const char* source_file_;
1000
1001 // Static fields
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001002 ObjectArray<StaticField>* sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001003
1004 private:
1005 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -07001006};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001007std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001008
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001009inline Object* Object::Alloc(Class* klass) {
Jesse Wilson14150742011-07-29 19:04:44 -04001010 DCHECK(klass != NULL);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001011 return Heap::AllocObject(klass, klass->object_size_);
1012}
1013
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001014class DataObject : public Object {
1015 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001016 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001017 private:
1018 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001019};
1020
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001021class CharArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001022 public:
1023 static CharArray* Alloc(Class* char_array_class, size_t length) {
1024 return down_cast<CharArray*>(Array::Alloc(char_array_class,
1025 length,
1026 sizeof(uint16_t)));
1027 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001028
1029 uint16_t* GetChars() {
1030 return reinterpret_cast<uint16_t*>(GetData());
1031 }
1032
1033 const uint16_t* GetChars() const {
1034 return reinterpret_cast<const uint16_t*>(GetData());
1035 }
1036
1037 uint16_t GetChar(uint32_t i) const {
1038 CHECK_LT(i, GetLength());
1039 return GetChars()[i];
1040 }
1041
1042 void SetChar(uint32_t i, uint16_t ch) {
1043 CHECK_LT(i, GetLength());
1044 GetChars()[i] = ch;
1045 }
1046
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001047 private:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001048 CharArray();
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001049};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001050
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001051class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001052 public:
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001053 static String* AllocFromUtf16(Class* java_lang_String,
1054 Class* char_array,
1055 int32_t utf16_length,
1056 uint16_t* utf16_data_in) {
1057 String* string = Alloc(java_lang_String, char_array, utf16_length);
1058 uint16_t* utf16_data_out = string->array_->GetChars();
1059 // TODO use 16-bit wide memset variant
1060 for (int i = 0; i < utf16_length; i++ ) {
1061 utf16_data_out[i] = utf16_data_in[i];
1062 }
1063 string->hash_code_ = ComputeUtf16Hash(utf16_data_out, utf16_length);
1064 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001065 }
1066
1067 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1068 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001069 int32_t utf16_length,
1070 const char* utf8_data_in) {
1071 String* string = Alloc(java_lang_String, char_array, utf16_length);
1072 uint16_t* utf16_data_out = string->array_->GetChars();
1073 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1074 string->hash_code_ = ComputeUtf16Hash(utf16_data_out, utf16_length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001075 return string;
1076 }
1077
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001078 // Creates a String of the given ASCII characters. It is an error to call this
1079 // using non-ASCII characters as this function assumes one char per byte.
1080 static String* AllocFromAscii(const char* ascii_data_in) {
1081 DCHECK(java_lang_String_ != NULL);
1082 DCHECK(char_array_ != NULL);
1083 return AllocFromModifiedUtf8(java_lang_String_,
1084 char_array_,
1085 strlen(ascii_data_in),
1086 ascii_data_in);
1087 }
1088
Jesse Wilson8989d992011-08-02 13:39:42 -07001089 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1090 const char* utf8_data_in) {
1091 return AllocFromModifiedUtf8(java_lang_String_, char_array_, utf16_length, utf8_data_in);
1092 }
1093
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001094 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -04001095 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001096 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001097
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001098 int32_t hash_code_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001099
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001100 int32_t offset_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001101
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001102 int32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001103
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001104 static void InitClasses(Class* java_lang_String, Class* char_array);
1105
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001106 static String* Alloc(Class* java_lang_String,
1107 Class* char_array,
1108 int32_t utf16_length) {
1109 String* string = down_cast<String*>(Object::Alloc(java_lang_String));
1110 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1111 string->array_ = array;
1112 string->count_ = utf16_length;
1113 return string;
1114 }
1115
1116 // Convert Modified UTF-8 to UTF-16
1117 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1118 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1119 while (*utf8_data_in != '\0') {
1120 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1121 }
1122 }
1123
1124 // Retrieve the next UTF-16 character from a UTF-8 string.
1125 //
1126 // Advances "*pUtf8Ptr" to the start of the next character.
1127 //
1128 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1129 // of a 3-byte sequence, you can end up overrunning the buffer with
1130 // reads (and possibly with the writes if the length was computed and
1131 // cached before the damage). For performance reasons, this function
1132 // assumes that the string being parsed is known to be valid (e.g., by
1133 // already being verified). Most strings we process here are coming
1134 // out of dex files or other internal translations, so the only real
1135 // risk comes from the JNI NewStringUTF call.
1136 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1137 uint8_t one = *(*utf8_data_in)++;
1138 if ((one & 0x80) == 0) {
1139 /* one-byte encoding */
1140 return one;
1141 }
1142 /* two- or three-byte encoding */
1143 uint8_t two = *(*utf8_data_in)++;
1144 if ((one & 0x20) == 0) {
1145 /* two-byte encoding */
1146 return ((one & 0x1f) << 6) |
1147 (two & 0x3f);
1148 }
1149 /* three-byte encoding */
1150 uint8_t three = *(*utf8_data_in)++;
1151 return ((one & 0x0f) << 12) |
1152 ((two & 0x3f) << 6) |
1153 (three & 0x3f);
1154 }
1155
1156 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001157 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1158 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001159 while (string_length--) {
1160 hash = hash * 31 + *string_data++;
1161 }
1162 return hash;
1163 }
1164
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001165 static bool EqualsUtf8(const String* string, const char* other) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001166 uint16_t* chars = string->array_->GetChars();
1167 for (int32_t i = 0; i < string->count_; i++) {
1168 uint16_t c = GetUtf16FromUtf8(&other);
1169 if (c == '\0' || c != chars[string->offset_ + i]) {
1170 return false;
1171 }
1172 }
1173 return *other == '\0';
1174 }
1175
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001176 static bool Equals(const String* a, const String* b) {
1177 // TODO short circuit on hash_code_
1178 int32_t a_count = a->count_;
1179 if (a_count != b->count_) {
1180 return false;
1181 }
1182 int32_t a_offset = a->offset_;
1183 int32_t b_offset = b->offset_;
1184 uint16_t* a_chars = a->array_->GetChars();
1185 uint16_t* b_chars = b->array_->GetChars();
1186 for (int32_t i = 0; i < a_count; i++) {
1187 if (a_chars[a_offset + i] != b_chars[b_offset + i]) {
1188 return false;
1189 }
1190 }
1191 return true;
1192 }
1193
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001194 private:
1195 String();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001196
1197 static Class* java_lang_String_;
1198 static Class* char_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001199};
1200
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001202 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001203 Class* GetClass() const {
1204 return klass_;
1205 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001206
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001207 void SetClass(Class* klass) {
1208 klass_ = klass;
1209 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001210
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001211 private:
1212 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001213 Class* klass_;
1214
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001215 public: // TODO: private
1216 // Index into array of vtable offsets. This points into the
1217 // ifviPool, which holds the vtables for all interfaces declared by
1218 // this class.
1219 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001220};
1221
1222} // namespace art
1223
1224#endif // ART_SRC_OBJECT_H_