blob: 6322b8d9c95849e3e9632312e1f7178d79349a00 [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;
Jesse Wilson35baaab2011-08-10 16:18:03 -040021class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070022class InterfaceEntry;
23class Monitor;
24class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070025class Object;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040026class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070027template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070028template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070029typedef PrimitiveArray<uint8_t> BooleanArray;
30typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070031typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070032typedef PrimitiveArray<double> DoubleArray;
33typedef PrimitiveArray<float> FloatArray;
34typedef PrimitiveArray<int32_t> IntArray;
35typedef PrimitiveArray<int64_t> LongArray;
36typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070037
Carl Shapiro3ee755d2011-06-28 12:11:04 -070038union JValue {
39 uint8_t z;
40 int8_t b;
41 uint16_t c;
42 int16_t s;
43 int32_t i;
44 int64_t j;
45 float f;
46 double d;
47 Object* l;
48};
49
Brian Carlstrombe977852011-07-19 14:54:54 -070050static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
51static const uint32_t kAccPrivate = 0x0002; // field, method, ic
52static const uint32_t kAccProtected = 0x0004; // field, method, ic
53static const uint32_t kAccStatic = 0x0008; // field, method, ic
54static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
55static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
56static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
57static const uint32_t kAccVolatile = 0x0040; // field
58static const uint32_t kAccBridge = 0x0040; // method (1.5)
59static const uint32_t kAccTransient = 0x0080; // field
60static const uint32_t kAccVarargs = 0x0080; // method (1.5)
61static const uint32_t kAccNative = 0x0100; // method
62static const uint32_t kAccInterface = 0x0200; // class, ic
63static const uint32_t kAccAbstract = 0x0400; // class, method, ic
64static const uint32_t kAccStrict = 0x0800; // method
65static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
66static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
67static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070068
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070069static const uint32_t kAccMiranda = 0x8000; // method
70
Brian Carlstroma331b3c2011-07-18 17:47:56 -070071static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
72
Brian Carlstrombe977852011-07-19 14:54:54 -070073static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
74static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070075
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070076/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070077 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070078 */
79/*
80 * A magic value for refOffsets. Ignore the bits and walk the super
81 * chain when this is the value.
82 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
83 * fields followed by 2 ref instance fields.]
84 */
85#define CLASS_WALK_SUPER ((unsigned int)(3))
86#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
87#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
88#define CLASS_OFFSET_ALIGNMENT 4
89#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
90/*
91 * Given an offset, return the bit number which would encode that offset.
92 * Local use only.
93 */
94#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
95 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
96 CLASS_OFFSET_ALIGNMENT)
97/*
98 * Is the given offset too large to be encoded?
99 */
100#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
101 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
102/*
103 * Return a single bit, encoding the offset.
104 * Undefined if the offset is too large, as defined above.
105 */
106#define CLASS_BIT_FROM_OFFSET(byteOffset) \
107 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
108/*
109 * Return an offset, given a bit number as returned from CLZ.
110 */
111#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700112 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700113
114
Carl Shapiro1fb86202011-06-27 17:43:13 -0700115class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700116 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700117 static bool InstanceOf(const Object* object, const Class* klass) {
118 if (object == NULL) {
119 return false;
120 }
121 return object->InstanceOf(klass);
122 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700123
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700124 Class* GetClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700125 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700126 return klass_;
127 }
128
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700129 bool InstanceOf(const Class* klass) const;
130
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700131 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700132
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700133 void MonitorEnter() {
134 monitor_->Enter();
135 }
136
137 void MonitorExit() {
138 monitor_->Exit();
139 }
140
141 void Notify() {
142 monitor_->Notify();
143 }
144
145 void NotifyAll() {
146 monitor_->NotifyAll();
147 }
148
149 void Wait() {
150 monitor_->Wait();
151 }
152
153 void Wait(int64_t timeout) {
154 monitor_->Wait(timeout);
155 }
156
157 void Wait(int64_t timeout, int32_t nanos) {
158 monitor_->Wait(timeout, nanos);
159 }
160
Carl Shapiro69759ea2011-07-21 18:13:35 -0700161 const Object* GetFieldObject(size_t field_offset) const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700162 Object* that = const_cast<Object*>(this);
163 Object* other = that->GetFieldObject(field_offset);
164 return const_cast<const Object*>(other);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700165 }
166
167 Object* GetFieldObject(size_t field_offset) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700168 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset;
169 return *reinterpret_cast<Object**>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700170 }
171
172 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700173 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
174 *reinterpret_cast<Object**>(raw_addr) = new_value;
175 // TODO: write barrier
176 }
177
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700178 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700179
180 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700181 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700182 return down_cast<Class*>(this);
183 }
184
185 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700186 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700187 return down_cast<const Class*>(this);
188 }
189
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700190 bool IsObjectArray() const;
191
192 template<class T>
193 ObjectArray<T>* AsObjectArray() {
194 DCHECK(IsObjectArray());
195 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700196 }
197
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700198 template<class T>
199 const ObjectArray<T>* AsObjectArray() const {
200 DCHECK(IsObjectArray());
201 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700202 }
203
204 bool IsReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700205 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700206 return true;
207 }
208
209 bool IsWeakReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700210 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700211 return true;
212 }
213
214 bool IsSoftReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700215 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700216 return true;
217 }
218
219 bool IsFinalizerReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700220 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700221 return true;
222 }
223
224 bool IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700225 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700226 return true;
227 }
228
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700229 bool IsArray() const;
230
231 Array* AsArray() {
232 DCHECK(IsArray());
233 return down_cast<Array*>(this);
234 }
235
236 const Array* AsArray() const {
237 DCHECK(IsArray());
238 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700239 }
240
Brian Carlstroma663ea52011-08-19 23:33:41 -0700241 bool IsString() const;
242
243 String* AsString() {
244 DCHECK(IsString());
245 return down_cast<String*>(this);
246 }
247
248 bool IsMethod() const;
249
250 Method* AsMethod() {
251 DCHECK(IsMethod());
252 return down_cast<Method*>(this);
253 }
254
255 bool IsField() const;
256
257 Field* AsField() {
258 DCHECK(IsField());
259 return down_cast<Field*>(this);
260 }
261
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700262 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700263 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700264
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700265 Monitor* monitor_;
266
267 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700268 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700269};
270
271class ObjectLock {
272 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700273 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700274 CHECK(object != NULL);
275 obj_->MonitorEnter();
276 }
277
278 ~ObjectLock() {
279 obj_->MonitorExit();
280 }
281
282 void Wait(int64_t millis = 0) {
283 return obj_->Wait(millis);
284 }
285
286 void Notify() {
287 obj_->Notify();
288 }
289
290 void NotifyAll() {
291 obj_->NotifyAll();
292 }
293
294 private:
295 Object* obj_;
296 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700297};
298
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400299class AccessibleObject : public Object {
300 private:
301 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
302 uint32_t java_flag_;
303};
304
305class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700306 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700307 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700308 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700309 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700310 }
311
Jesse Wilson14150742011-07-29 19:04:44 -0400312 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700313 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700314 return name_;
315 }
316
317 bool IsStatic() const {
318 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700319 }
320
321 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700322 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700323 }
324
Brian Carlstromae3ac012011-07-27 01:30:28 -0700325 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700326 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700327 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700328 }
329
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700330 uint32_t GetOffset() const {
331 return offset_;
332 }
333
334 void SetOffset(size_t num_bytes) {
335 offset_ = num_bytes;
336 }
337
Jesse Wilson35baaab2011-08-10 16:18:03 -0400338 // static field access
Jesse Wilson7833bd22011-08-09 18:31:44 -0400339 bool GetBoolean();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400340 void SetBoolean(bool z);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400341 int8_t GetByte();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400342 void SetByte(int8_t b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400343 uint16_t GetChar();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400344 void SetChar(uint16_t c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400345 uint16_t GetShort();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400346 void SetShort(uint16_t s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400347 int32_t GetInt();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400348 void SetInt(int32_t i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400349 int64_t GetLong();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400350 void SetLong(int64_t j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400351 float GetFloat();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400352 void SetFloat(float f);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400353 double GetDouble();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400354 void SetDouble(double d);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400355 Object* GetObject();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400356 const Object* GetObject() const;
Jesse Wilson7833bd22011-08-09 18:31:44 -0400357 void SetObject(Object* l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700358
Jesse Wilson35baaab2011-08-10 16:18:03 -0400359 public: // TODO: private
360 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
361 // The class in which this field is declared.
362 Class* declaring_class_;
363 Object* generic_type_;
364 uint32_t generic_types_are_initialized_;
365 String* name_;
366 uint32_t offset_;
367 Class* type_;
368
369 // e.g. "I", "[C", "Landroid/os/Debug;"
370 StringPiece descriptor_;
371
372 uint32_t access_flags_;
373
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700374 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400375 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
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:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700380 // An function that invokes a method with an array of its arguments.
381 typedef void InvokeStub(Method* method,
382 Object* obj,
383 Thread* thread,
384 byte* args,
385 JValue* result);
386
Brian Carlstromae3ac012011-07-27 01:30:28 -0700387 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700388 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700389 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700390 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391 }
392
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700393 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700394 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700395 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700396 }
397
Brian Carlstroma0808032011-07-18 00:39:23 -0700398 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700399 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700400 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700401 }
402
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700403 static MemberOffset DeclaringClassOffset() {
404 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700405 }
406
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700407 // Returns true if the method is declared public.
408 bool IsPublic() const {
409 return (access_flags_ & kAccPublic) != 0;
410 }
411
412 // Returns true if the method is declared private.
413 bool IsPrivate() const {
414 return (access_flags_ & kAccPrivate) != 0;
415 }
416
417 // Returns true if the method is declared static.
418 bool IsStatic() const {
419 return (access_flags_ & kAccStatic) != 0;
420 }
421
422 // Returns true if the method is declared synchronized.
423 bool IsSynchronized() const {
424 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
425 return (access_flags_ & synchonized) != 0;
426 }
427
428 // Returns true if the method is declared final.
429 bool IsFinal() const {
430 return (access_flags_ & kAccFinal) != 0;
431 }
432
433 // Returns true if the method is declared native.
434 bool IsNative() const {
435 return (access_flags_ & kAccNative) != 0;
436 }
437
438 // Returns true if the method is declared abstract.
439 bool IsAbstract() const {
440 return (access_flags_ & kAccAbstract) != 0;
441 }
442
443 bool IsSynthetic() const {
444 return (access_flags_ & kAccSynthetic) != 0;
445 }
446
447 // Number of argument registers required by the prototype.
448 uint32_t NumArgRegisters();
449
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700450 // Number of argument bytes required for densely packing the
451 // arguments into an array of arguments.
452 size_t NumArgArrayBytes();
453
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700454 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400455 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400456 // the class we are a part of
457 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400458 ObjectArray<Class>* java_exception_types_;
459 Object* java_formal_type_parameters_;
460 Object* java_generic_exception_types_;
461 Object* java_generic_parameter_types_;
462 Object* java_generic_return_type_;
463 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700464 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400465 ObjectArray<Class>* java_parameter_types_;
466 uint32_t java_generic_types_are_initialized_;
467 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700468
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700469 const StringPiece& GetShorty() const {
470 return shorty_;
471 }
472
Ian Rogersb033c752011-07-20 12:22:35 -0700473 bool IsReturnAReference() const {
474 return (shorty_[0] == 'L') || (shorty_[0] == '[');
475 }
476
477 bool IsReturnAFloatOrDouble() const {
478 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
479 }
480
481 bool IsReturnAFloat() const {
482 return shorty_[0] == 'F';
483 }
484
485 bool IsReturnADouble() const {
486 return shorty_[0] == 'D';
487 }
488
489 bool IsReturnALong() const {
490 return shorty_[0] == 'J';
491 }
492
Ian Rogers45a76cb2011-07-21 22:00:15 -0700493 bool IsReturnVoid() const {
494 return shorty_[0] == 'V';
495 }
496
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700497 // "Args" may refer to any of the 3 levels of "Args."
498 // To avoid confusion, our code will denote which "Args" clearly:
499 // 1. UserArgs: Args that a user see.
500 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
501 // receiver.
502 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
503 // E.g., the first in Args is Method* for both static and non-static
504 // methods. And CConvArgs doesn't deal with the receiver because
505 // receiver is hardwired in an implicit register, so CConvArgs doesn't
506 // need to deal with it.
507 //
508 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700509 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700510 // "1 +" because the first in Args is the receiver.
511 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700512 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
513 }
514
515 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700516 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700517 size_t NumReferenceArgs() const;
518
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700519 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700520 size_t NumLongOrDoubleArgs() const;
521
522 // The number of reference arguments to this method before the given
523 // parameter index
524 size_t NumReferenceArgsBefore(unsigned int param) const;
525
526 // Is the given method parameter a reference?
527 bool IsParamAReference(unsigned int param) const;
528
529 // Is the given method parameter a long or double?
530 bool IsParamALongOrDouble(unsigned int param) const;
531
Ian Rogersdf20fe02011-07-20 20:34:16 -0700532 // Size in bytes of the given parameter
533 size_t ParamSize(unsigned int param) const;
534
535 // Size in bytes of the return value
536 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700537
buzbeec143c552011-08-20 17:38:58 -0700538 bool HasCode() {
539 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700540 }
541
buzbeec143c552011-08-20 17:38:58 -0700542 void SetCode(const byte* compiled_code, size_t byte_count, InstructionSet set) {
543 // Copy the code into an executable region.
544 code_instruction_set_ = set;
545 code_area_.reset(MemMap::Map(byte_count,
546 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700547 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700548 byte* code = code_area_->GetAddress();
549 memcpy(code, compiled_code, byte_count);
550 __builtin___clear_cache(code, code + byte_count);
551
552 uintptr_t address = reinterpret_cast<uintptr_t>(code);
553 if (code_instruction_set_ == kThumb2) {
554 // Set the low-order bit so a BLX will switch to Thumb mode
555 address |= 0x1;
556 }
557 code_ = reinterpret_cast<void*>(address);
558 }
559
560 void SetFrameSize(uint32_t frame_size) {
561 frame_size_ = frame_size;
562 }
563
564 void SetCoreSpillMask(uint32_t core_spill_mask) {
565 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700566 }
567
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700568 static size_t GetCodeOffset() {
569 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700570 }
571
buzbeec143c552011-08-20 17:38:58 -0700572 void SetFpSpillMask(uint32_t fp_spill_mask) {
573 fp_spill_mask_ = fp_spill_mask;
574 }
575
Ian Rogersb033c752011-07-20 12:22:35 -0700576 void RegisterNative(const void* native_method) {
577 native_method_ = native_method;
578 }
579
580 static MemberOffset NativeMethodOffset() {
581 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
582 }
583
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700584 InvokeStub* GetInvokeStub() const {
585 return invoke_stub_;
586 }
587
588 void SetInvokeStub(const InvokeStub* invoke_stub) {
589 invoke_stub_ = invoke_stub;
590 }
591
592 static size_t GetInvokeStubOffset() {
593 return OFFSETOF_MEMBER(Method, invoke_stub_);
594 }
595
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700596 bool HasSameNameAndDescriptor(const Method* that) const;
597
Ian Rogersb033c752011-07-20 12:22:35 -0700598 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700599 // access flags; low 16 bits are defined by spec (could be uint16_t?)
600 uint32_t access_flags_;
601
602 // For concrete virtual methods, this is the offset of the method
603 // in "vtable".
604 //
605 // For abstract methods in an interface class, this is the offset
606 // of the method in "iftable[n]->methodIndexArray".
607 uint16_t method_index_;
608
609 // Method bounds; not needed for an abstract method.
610 //
611 // For a native method, we compute the size of the argument list, and
612 // set "insSize" and "registerSize" equal to it.
613 uint16_t num_registers_; // ins + locals
614 uint16_t num_outs_;
615 uint16_t num_ins_;
616
buzbeec143c552011-08-20 17:38:58 -0700617 // Total size in bytes of the frame
618 uint32_t frame_size_;
619
620 // Architecture-dependent register spill masks
621 uint32_t core_spill_mask_;
622 uint32_t fp_spill_mask_;
623
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700624 // The method descriptor. This represents the parameters a method
625 // takes and value it returns. This string is a list of the type
626 // descriptors for the parameters enclosed in parenthesis followed
627 // by the return type descriptor. For example, for the method
628 //
629 // Object mymethod(int i, double d, Thread t)
630 //
631 // the method descriptor would be
632 //
633 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700634 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700635
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700636 // Method prototype descriptor string (return and argument types).
637 uint32_t proto_idx_;
638
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700639 // Offset to the CodeItem.
640 uint32_t code_off_;
641
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700642 // The short-form method descriptor string.
643 StringPiece shorty_;
644
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700645 // short cuts to declaring_class_->dex_cache_ members for fast compiled code access
646 ObjectArray<String>* dex_cache_strings_;
647 ObjectArray<Class>* dex_cache_classes_;
648 ObjectArray<Method>* dex_cache_methods_;
649 ObjectArray<Field>* dex_cache_fields_;
650
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700651 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700652 // Compiled code associated with this method
buzbeec143c552011-08-20 17:38:58 -0700653 scoped_ptr<MemMap> code_area_;
654 void* code_;
655 // Instruction set of the coompiled code
656 InstructionSet code_instruction_set_;
657
658 // Size in bytes of compiled code associated with this method
659 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700660
661 // Any native method registered with this method
662 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700663
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700664 // Native invocation stub entry point.
665 const InvokeStub* invoke_stub_;
666
Carl Shapirof88c9522011-08-06 15:47:38 -0700667 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700668};
669
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700670class Array : public Object {
671 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700672 static size_t SizeOf(size_t component_count,
673 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700674 return sizeof(Array) + component_count * component_size;
675 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700676
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700677 // A convenience for code that doesn't know the component size,
678 // and doesn't want to have to work it out itself.
679 static Array* Alloc(Class* array_class, size_t component_count);
680
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700681 static Array* Alloc(Class* array_class,
682 size_t component_count,
683 size_t component_size) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700684 size_t size = SizeOf(component_count, component_size);
685 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700686 if (array != NULL) {
687 array->SetLength(component_count);
688 }
689 return array;
690 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700691
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700692 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700693
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700694 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700695 return length_;
696 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700697
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700698 void SetLength(uint32_t length) {
699 length_ = length;
700 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700701
buzbeec143c552011-08-20 17:38:58 -0700702 static MemberOffset LengthOffset() {
703 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
704 }
705
706 static MemberOffset DataOffset() {
707 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
708 }
709
Elliott Hughes289da822011-08-16 10:11:20 -0700710 protected:
711 bool IsValidIndex(int32_t index) const {
712 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700713 Thread* self = Thread::Current();
714 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
715 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700716 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700717 }
718 return true;
719 }
720
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700721 private:
722 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700723 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400724 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
725 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700726 // Marker for the data (used by generated code)
727 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700728
Carl Shapirof88c9522011-08-06 15:47:38 -0700729 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700730};
731
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700732template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700733class ObjectArray : public Array {
734 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700735 static ObjectArray<T>* Alloc(Class* object_array_class,
736 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700737 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700738 }
739
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700740 T* const * GetData() const {
741 return reinterpret_cast<T* const *>(&elements_);
742 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400743
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700744 T** GetData() {
745 return reinterpret_cast<T**>(&elements_);
746 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400747
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700748 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700749 if (!IsValidIndex(i)) {
750 return NULL;
751 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700752 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700753 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700754
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700755 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700756 if (IsValidIndex(i)) {
757 // TODO: ArrayStoreException
758 GetData()[i] = object; // TODO: write barrier
759 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700760 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700761
762 static void Copy(ObjectArray<T>* src, int src_pos,
763 ObjectArray<T>* dst, int dst_pos,
764 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700765 for (size_t i = 0; i < length; i++) {
766 dst->Set(dst_pos + i, src->Get(src_pos + i));
767 }
768 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700769
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700770 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700771 ObjectArray<T>* new_array = Alloc(klass_, new_length);
772 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
773 return new_array;
774 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700775
776 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700777 // Location of first element.
778 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700779
780 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700781};
782
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700783// ClassLoader objects.
784class ClassLoader : public Object {
785 public:
786 std::vector<const DexFile*>& GetClassPath() {
787 return class_path_;
788 }
789 void SetClassPath(std::vector<const DexFile*>& class_path) {
790 DCHECK_EQ(0U, class_path_.size());
791 class_path_ = class_path;
792 }
793
794 private:
795 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
796 Object* packages_;
797 ClassLoader* parent_;
798
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700799 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700800 std::vector<const DexFile*> class_path_;
801
Carl Shapirof88c9522011-08-06 15:47:38 -0700802 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700803};
804
805class BaseDexClassLoader : public ClassLoader {
806 private:
807 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
808 String* original_path_;
809 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700810 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700811};
812
813class PathClassLoader : public BaseDexClassLoader {
814 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700815 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700816};
817
Carl Shapiro1fb86202011-06-27 17:43:13 -0700818// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700819class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700820 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700821
822 // Class Status
823 //
824 // kStatusNotReady: If a Class cannot be found in the class table by
825 // FindClass, it allocates an new one with AllocClass in the
826 // kStatusNotReady and calls LoadClass. Note if it does find a
827 // class, it may not be kStatusResolved and it will try to push it
828 // forward toward kStatusResolved.
829 //
830 // kStatusIdx: LoadClass populates with Class with information from
831 // the DexFile, moving the status to kStatusIdx, indicating that the
832 // Class values in super_class_ and interfaces_ have not been
833 // populated based on super_class_idx_ and interfaces_idx_. The new
834 // Class can then be inserted into the classes table.
835 //
836 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
837 // attempt to move a kStatusIdx class forward to kStatusLoaded by
838 // using ResolveClass to initialize the super_class_ and interfaces_.
839 //
840 // kStatusResolved: Still holding the lock on Class, the ClassLinker
841 // will use LinkClass to link all members, creating Field and Method
842 // objects, setting up the vtable, etc. On success, the class is
843 // marked kStatusResolved.
844
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700845 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700846 kStatusError = -1,
847 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700848 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700849 kStatusLoaded = 2, // DEX idx values resolved
850 kStatusResolved = 3, // part of linking
851 kStatusVerifying = 4, // in the process of being verified
852 kStatusVerified = 5, // logically part of linking; done pre-init
853 kStatusInitializing = 6, // class init in progress
854 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700855 };
856
857 enum PrimitiveType {
858 kPrimNot = -1
859 };
860
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700861 Object* NewInstance() {
862 return Heap::AllocObject(this, this->object_size_);
863 }
864
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700865 Class* GetSuperClass() const {
866 return super_class_;
867 }
868
869 uint32_t GetSuperClassIdx() const {
870 return super_class_idx_;
871 }
872
873 bool HasSuperClass() const {
874 return super_class_ != NULL;
875 }
876
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700877 bool IsAssignableFrom(const Class* klass) const {
878 DCHECK(klass != NULL);
879 if (this == klass) {
880 return true;
881 }
882 if (IsInterface()) {
883 return klass->Implements(this);
884 }
885 if (klass->IsArray()) {
886 return IsAssignableFromArray(klass);
887 }
888 return klass->IsSubClass(this);
889 }
890
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700891 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700892 return class_loader_;
893 }
894
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700895 DexCache* GetDexCache() const {
896 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700897 }
898
899 Class* GetComponentType() const {
900 return component_type_;
901 }
902
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700903 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700904
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700905 size_t GetComponentSize() const {
906 return GetTypeSize(component_type_->descriptor_);
907 }
908
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700909 const String* GetDescriptor() const {
910 DCHECK(descriptor_ != NULL);
911 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700912 return descriptor_;
913 }
914
915 Status GetStatus() const {
916 return status_;
917 }
918
919 void SetStatus(Status new_status) {
920 // TODO: validate transition
921 status_ = new_status;
922 }
923
Carl Shapiro69759ea2011-07-21 18:13:35 -0700924 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700925 bool IsErroneous() const {
926 return GetStatus() == kStatusError;
927 }
928
Carl Shapiro69759ea2011-07-21 18:13:35 -0700929 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700930 bool IsVerified() const {
931 return GetStatus() >= kStatusVerified;
932 }
933
Carl Shapiro69759ea2011-07-21 18:13:35 -0700934 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700935 bool IsLinked() const {
936 return GetStatus() >= kStatusResolved;
937 }
938
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700939 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700940 bool IsLoaded() const {
941 return GetStatus() >= kStatusLoaded;
942 }
943
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700944 // Returns true if the class is initialized.
945 bool IsInitialized() const {
946 return GetStatus() == kStatusInitialized;
947 }
948
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700949 // Returns true if this class is in the same packages as that class.
950 bool IsInSamePackage(const Class* that) const;
951
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700952 static bool IsInSamePackage(const String* descriptor1,
953 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700954
955 // Returns true if this class represents an array class.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700956 bool IsArray() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700957
958 // Returns true if the class is an interface.
959 bool IsInterface() const {
960 return (access_flags_ & kAccInterface) != 0;
961 }
962
963 // Returns true if the class is declared public.
964 bool IsPublic() const {
965 return (access_flags_ & kAccPublic) != 0;
966 }
967
968 // Returns true if the class is declared final.
969 bool IsFinal() const {
970 return (access_flags_ & kAccFinal) != 0;
971 }
972
973 // Returns true if the class is abstract.
974 bool IsAbstract() const {
975 return (access_flags_ & kAccAbstract) != 0;
976 }
977
978 // Returns true if the class is an annotation.
979 bool IsAnnotation() const {
980 return (access_flags_ & kAccAnnotation) != 0;
981 }
982
983 // Returns true if the class is a primitive type.
984 bool IsPrimitive() const {
985 return primitive_type_ != kPrimNot;
986 }
987
Brian Carlstromae3ac012011-07-27 01:30:28 -0700988 // Returns true if the class is synthetic.
989 bool IsSynthetic() const {
990 return (access_flags_ & kAccSynthetic) != 0;
991 }
992
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700993 // Returns true if this class can access that class.
994 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700995 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700996 }
997
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700998 // Returns the number of static, private, and constructor methods.
999 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001000 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001001 }
1002
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001003 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001004 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001005 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001006 }
1007
1008 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001009 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001010 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001011 }
1012
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001013 Method* FindDeclaredDirectMethod(const StringPiece& name,
1014 const StringPiece& descriptor);
1015
1016 Method* FindDirectMethod(const StringPiece& name,
1017 const StringPiece& descriptor);
1018
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001019 // Returns the number of non-inherited virtual methods.
1020 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001021 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001022 }
1023
1024 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001025 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001026 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001027 }
1028
1029 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001030 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001031 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001032 }
1033
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001034 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1035 const StringPiece& descriptor);
1036
1037 Method* FindVirtualMethod(const StringPiece& name,
1038 const StringPiece& descriptor);
1039
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001040 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001041 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001042 }
1043
Carl Shapiro69759ea2011-07-21 18:13:35 -07001044 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001045 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001046 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001047 }
1048
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 // Finds the given instance field in this class or a superclass.
1050 Field* FindInstanceField(const StringPiece& name,
1051 const StringPiece& descriptor);
1052
1053 Field* FindDeclaredInstanceField(const StringPiece& name,
1054 const StringPiece& descriptor);
1055
1056 // Finds the given static field in this class or a superclass.
1057 Field* FindStaticField(const StringPiece& name,
1058 const StringPiece& descriptor);
1059
1060 Field* FindDeclaredStaticField(const StringPiece& name,
1061 const StringPiece& descriptor);
1062
Jesse Wilson35baaab2011-08-10 16:18:03 -04001063 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001064 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001065 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001066 }
1067
Jesse Wilson35baaab2011-08-10 16:18:03 -04001068 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001069 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001070 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001071 }
1072
1073 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001074 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001075 }
1076
Jesse Wilson35baaab2011-08-10 16:18:03 -04001077 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001078 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001079 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001080 }
1081
Jesse Wilson35baaab2011-08-10 16:18:03 -04001082 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001083 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001084 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001085 }
1086
1087 uint32_t GetReferenceOffsets() const {
1088 return reference_offsets_;
1089 }
1090
1091 void SetReferenceOffsets(uint32_t new_reference_offsets) {
1092 reference_offsets_ = new_reference_offsets;
1093 }
1094
Carl Shapiro69759ea2011-07-21 18:13:35 -07001095 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001096 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001097 }
1098
1099 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001100 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001101 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001102 }
1103
1104 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001105 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001106 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001107 }
1108
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001109 void SetVerifyErrorClass(Class* klass) {
1110 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1111 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1112 klass->SetFieldObject(field_offset, klass);
1113 }
1114
1115 private:
1116 bool Implements(const Class* klass) const;
1117 bool IsArrayAssignableFromArray(const Class* klass) const;
1118 bool IsAssignableFromArray(const Class* klass) const;
1119 bool IsSubClass(const Class* klass) const;
1120
Ian Rogersb033c752011-07-20 12:22:35 -07001121 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001122 // descriptor for the class such as "java.lang.Class" or "[C"
1123 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001124
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001125 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1126 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001127
1128 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001129 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001130
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001131 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001132 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001133 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001134
1135 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001136 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001137
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001138 // If class verify fails, we must return same error on subsequent tries.
1139 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1140 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001141
1142 // threadId, used to check for recursive <clinit> invocation
1143 uint32_t clinit_thread_id_;
1144
1145 // Total object size; used when allocating storage on gc heap. (For
1146 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001147 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001148
1149 // For array classes, the class object for base element, for
1150 // instanceof/checkcast (for String[][][], this will be String).
1151 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001152 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001153
1154 // For array classes, the number of array dimensions, e.g. int[][]
1155 // is 2. Otherwise 0.
1156 int32_t array_rank_;
1157
1158 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1159 PrimitiveType primitive_type_;
1160
1161 // The superclass, or NULL if this is java.lang.Object or a
1162 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001163 Class* super_class_; // TODO: make an instance field
1164 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001165
1166 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001167 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001168
1169 // initiating class loader list
1170 // NOTE: for classes with low serialNumber, these are unused, and the
1171 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001172 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001173
1174 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001175 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001176 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001177
1178 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001179 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001180
1181 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001182 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001183
1184 // Virtual method table (vtable), for use by "invoke-virtual". The
1185 // vtable from the superclass is copied in, and virtual methods from
1186 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001187 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001188
1189 // Interface table (iftable), one entry per interface supported by
1190 // this class. That means one entry for each interface we support
1191 // directly, indirectly via superclass, or indirectly via
1192 // superinterface. This will be null if neither we nor our
1193 // superclass implement any interfaces.
1194 //
1195 // Why we need this: given "class Foo implements Face", declare
1196 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1197 // is part of the Face interface. We can't easily use a single
1198 // vtable.
1199 //
1200 // For every interface a concrete class implements, we create a list
1201 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001202 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001203 InterfaceEntry* iftable_;
1204
1205 // The interface vtable indices for iftable get stored here. By
1206 // placing them all in a single pool for each class that implements
1207 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001208 size_t ifvi_pool_count_;
1209 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001210
1211 // instance fields
1212 //
1213 // These describe the layout of the contents of a
1214 // DataObject-compatible Object. Note that only the fields directly
1215 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001216 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001217 //
1218 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001219 // the beginning of the field list. num_reference_instance_fields_
1220 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001221 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001222
1223 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001224 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001225
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001226 // Bitmap of offsets of ifields.
1227 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001228
1229 // source file name, if known. Otherwise, NULL.
1230 const char* source_file_;
1231
Jesse Wilson7833bd22011-08-09 18:31:44 -04001232 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001233 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001234
1235 // static field storage
1236 //
1237 // Each static field is stored in one of three arrays:
1238 // o references are stored in static_references_
1239 // o doubles and longs are stored in static_64bit_primitives_
1240 // o everything else is in static_32bit_primitives_
1241 // Static fields select their array using their type and their index using the
1242 // Field->slot_ member. Storing static fields in arrays avoids the need for a
1243 // special case in the GC.
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001244 ObjectArray<Object>* static_references_;
1245 IntArray* static_32bit_primitives_;
1246 LongArray* static_64bit_primitives_;
1247
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001248 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001249 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001250};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001251std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001252
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001253inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001254 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001255 DCHECK(klass_ != NULL);
1256 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001257}
1258
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001259inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001260 Class* java_lang_Class = klass_->klass_;
1261 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001262}
1263
1264inline bool Object::IsObjectArray() const {
1265 return IsArray() && !klass_->component_type_->IsPrimitive();
1266}
1267
1268inline bool Object::IsArray() const {
1269 return klass_->IsArray();
1270}
1271
Brian Carlstroma663ea52011-08-19 23:33:41 -07001272inline bool Object::IsField() const {
1273 Class* java_lang_Class = klass_->klass_;
1274 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1275 return klass_ == java_lang_reflect_Field;
1276}
1277
1278inline bool Object::IsMethod() const {
1279 Class* java_lang_Class = klass_->klass_;
1280 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1281 return klass_ == java_lang_reflect_Method;
1282}
1283
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001284inline size_t Object::SizeOf() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001285 if (IsArray()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001286 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001287 }
1288 return klass_->object_size_;
1289}
1290
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001291inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001292 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001293}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001294
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001295class DataObject : public Object {
1296 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001297 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001298 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001299 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001300};
1301
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001302template<class T>
1303class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001304 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001305 typedef T ElementType;
1306
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001307 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001308
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001309 const T* GetData() const {
1310 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001311 }
1312
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001313 T* GetData() {
1314 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001315 }
1316
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001317 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001318 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001319 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001320 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001321 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001322 }
1323
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001324 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001325 // TODO: ArrayStoreException
1326 if (IsValidIndex(i)) {
1327 GetData()[i] = value;
1328 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001329 }
1330
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001331 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001332 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001333 CHECK(array_class != NULL);
1334 array_class_ = array_class;
1335 }
1336
Brian Carlstroma663ea52011-08-19 23:33:41 -07001337 static void ResetArrayClass() {
1338 CHECK(array_class_ != NULL);
1339 array_class_ = NULL;
1340 }
1341
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001342 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001343 // Location of first element.
1344 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001345
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001346 static Class* array_class_;
1347
Carl Shapirof88c9522011-08-06 15:47:38 -07001348 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001349};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001350
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001352 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001353 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001354 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001355 return array_;
1356 }
1357
Carl Shapirof88c9522011-08-06 15:47:38 -07001358 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001359 return hash_code_;
1360 }
1361
Carl Shapirof88c9522011-08-06 15:47:38 -07001362 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001363 return offset_;
1364 }
1365
Carl Shapirof88c9522011-08-06 15:47:38 -07001366 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001367 return count_;
1368 }
1369
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001370 // TODO: do we need this? Equals is the only caller, and could
1371 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001372 uint16_t CharAt(int32_t index) const {
1373 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001374 Thread* self = Thread::Current();
1375 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1376 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001377 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001378 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001379 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001380 }
1381
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001382 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001383 const uint16_t* utf16_data_in,
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001384 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001385 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001386 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001387 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001388 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001389 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001390 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001391 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001392 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001393 }
1394
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001395 static String* AllocFromModifiedUtf8(const char* utf) {
1396 size_t char_count = ModifiedUtf8Len(utf);
1397 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001398 }
1399
Jesse Wilson8989d992011-08-02 13:39:42 -07001400 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1401 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001402 String* string = Alloc(GetJavaLangString(), utf16_length);
1403 uint16_t* utf16_data_out = string->array_->GetData();
1404 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1405 string->ComputeHashCode();
1406 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001407 }
1408
Brian Carlstroma663ea52011-08-19 23:33:41 -07001409 static void SetClass(Class* java_lang_String);
1410 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001411
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001412 static String* Alloc(Class* java_lang_String,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001413 int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001414 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1415 }
1416
1417 static String* Alloc(Class* java_lang_String,
1418 CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001419 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001420 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001421 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001422 return string;
1423 }
1424
1425 // Convert Modified UTF-8 to UTF-16
1426 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1427 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1428 while (*utf8_data_in != '\0') {
1429 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1430 }
1431 }
1432
1433 // Retrieve the next UTF-16 character from a UTF-8 string.
1434 //
1435 // Advances "*pUtf8Ptr" to the start of the next character.
1436 //
1437 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1438 // of a 3-byte sequence, you can end up overrunning the buffer with
1439 // reads (and possibly with the writes if the length was computed and
1440 // cached before the damage). For performance reasons, this function
1441 // assumes that the string being parsed is known to be valid (e.g., by
1442 // already being verified). Most strings we process here are coming
1443 // out of dex files or other internal translations, so the only real
1444 // risk comes from the JNI NewStringUTF call.
1445 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1446 uint8_t one = *(*utf8_data_in)++;
1447 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001448 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001449 return one;
1450 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001451 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001452 uint8_t two = *(*utf8_data_in)++;
1453 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001454 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001455 return ((one & 0x1f) << 6) |
1456 (two & 0x3f);
1457 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001458 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001459 uint8_t three = *(*utf8_data_in)++;
1460 return ((one & 0x0f) << 12) |
1461 ((two & 0x3f) << 6) |
1462 (three & 0x3f);
1463 }
1464
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001465 // Like "strlen", but for strings encoded with "modified" UTF-8.
1466 //
1467 // The value returned is the number of characters, which may or may not
1468 // be the same as the number of bytes.
1469 //
1470 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1471 // get increment {1-3} from table of 8 values.)
1472 static size_t ModifiedUtf8Len(const char* utf8) {
1473 size_t len = 0;
1474 int ic;
1475 while ((ic = *utf8++) != '\0') {
1476 len++;
1477 if ((ic & 0x80) == 0) {
1478 // one-byte encoding
1479 continue;
1480 }
1481 // two- or three-byte encoding
1482 utf8++;
1483 if ((ic & 0x20) == 0) {
1484 // two-byte encoding
1485 continue;
1486 }
1487 // three-byte encoding
1488 utf8++;
1489 }
1490 return len;
1491 }
1492
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001493 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001494 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1495 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001496 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001497 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001498 }
1499 return hash;
1500 }
1501
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001502 void ComputeHashCode() {
1503 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1504 }
1505
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001506 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001507 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001508 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001509 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1510 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001511 return false;
1512 }
1513 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001514 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001515 }
1516
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001517 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001518 bool Equals(const StringPiece& modified_utf8) const {
1519 // TODO: do not assume C-string representation.
1520 return Equals(modified_utf8.data());
1521 }
1522
1523 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001524 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001525 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001526 return false;
1527 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001528 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001529 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001530 return false;
1531 }
1532 }
1533 return true;
1534 }
1535
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001536 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapirof88c9522011-08-06 15:47:38 -07001537 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001538 if (this->GetLength() != that_length) {
1539 return false;
1540 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001541 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001542 if (this->CharAt(i) != that_chars[that_offset + i]) {
1543 return false;
1544 }
1545 }
1546 return true;
1547 }
1548
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001549 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1550 std::string ToModifiedUtf8() const {
1551 std::string result;
1552 for (uint32_t i = 0; i < GetLength(); i++) {
1553 uint16_t ch = CharAt(i);
1554 // The most common case is (ch > 0 && ch <= 0x7f).
1555 if (ch == 0 || ch > 0x7f) {
1556 if (ch > 0x07ff) {
1557 result.push_back((ch >> 12) | 0xe0);
1558 result.push_back(((ch >> 6) & 0x3f) | 0x80);
1559 result.push_back((ch & 0x3f) | 0x80);
1560 } else { // (ch > 0x7f || ch == 0)
1561 result.push_back((ch >> 6) | 0xc0);
1562 result.push_back((ch & 0x3f) | 0x80);
1563 }
1564 } else {
1565 result.push_back(ch);
1566 }
1567 }
1568 return result;
1569 }
1570
1571
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001572 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001573 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1574 CharArray* array_;
1575
Carl Shapirof88c9522011-08-06 15:47:38 -07001576 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001577
Elliott Hughes289da822011-08-16 10:11:20 -07001578 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001579
Elliott Hughes289da822011-08-16 10:11:20 -07001580 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001581
1582 static Class* GetJavaLangString() {
1583 DCHECK(java_lang_String_ != NULL);
1584 return java_lang_String_;
1585 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001586
1587 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001588
1589 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001590};
1591
Brian Carlstroma663ea52011-08-19 23:33:41 -07001592inline bool Object::IsString() const {
1593 // TODO use "klass_ == String::GetJavaLangString()" instead?
1594 return klass_ == klass_->descriptor_->klass_;
1595}
1596
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001597inline size_t Class::GetTypeSize(String* descriptor) {
1598 switch (descriptor->CharAt(0)) {
1599 case 'B': return 1; // byte
1600 case 'C': return 2; // char
1601 case 'D': return 8; // double
1602 case 'F': return 4; // float
1603 case 'I': return 4; // int
1604 case 'J': return 8; // long
1605 case 'S': return 2; // short
1606 case 'Z': return 1; // boolean
1607 case 'L': return sizeof(Object*);
1608 case '[': return sizeof(Array*);
1609 default:
1610 LOG(ERROR) << "Unknown type " << descriptor;
1611 return 0;
1612 }
1613}
1614
1615inline bool Class::IsArray() const {
1616 return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor
1617}
1618
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001619class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001620 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001621 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1622 }
1623
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001624 Class* GetClass() const {
1625 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001626 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001627
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001628 void SetClass(Class* klass) {
1629 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001630 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001631
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001632 private:
1633 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001634 Class* klass_;
1635
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001636 public: // TODO: private
1637 // Index into array of vtable offsets. This points into the
1638 // ifviPool, which holds the vtables for all interfaces declared by
1639 // this class.
1640 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001641
1642 private:
1643 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001644};
1645
1646} // namespace art
1647
1648#endif // ART_SRC_OBJECT_H_