blob: 5d781e4f1eafd96e6703870e7434d4ff8f2a49d2 [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));
547 byte* code = code_area_->GetAddress();
548 memcpy(code, compiled_code, byte_count);
549 __builtin___clear_cache(code, code + byte_count);
550
551 uintptr_t address = reinterpret_cast<uintptr_t>(code);
552 if (code_instruction_set_ == kThumb2) {
553 // Set the low-order bit so a BLX will switch to Thumb mode
554 address |= 0x1;
555 }
556 code_ = reinterpret_cast<void*>(address);
557 }
558
559 void SetFrameSize(uint32_t frame_size) {
560 frame_size_ = frame_size;
561 }
562
563 void SetCoreSpillMask(uint32_t core_spill_mask) {
564 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700565 }
566
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700567 static size_t GetCodeOffset() {
568 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700569 }
570
buzbeec143c552011-08-20 17:38:58 -0700571 void SetFpSpillMask(uint32_t fp_spill_mask) {
572 fp_spill_mask_ = fp_spill_mask;
573 }
574
Ian Rogersb033c752011-07-20 12:22:35 -0700575 void RegisterNative(const void* native_method) {
576 native_method_ = native_method;
577 }
578
579 static MemberOffset NativeMethodOffset() {
580 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
581 }
582
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700583 InvokeStub* GetInvokeStub() const {
584 return invoke_stub_;
585 }
586
587 void SetInvokeStub(const InvokeStub* invoke_stub) {
588 invoke_stub_ = invoke_stub;
589 }
590
591 static size_t GetInvokeStubOffset() {
592 return OFFSETOF_MEMBER(Method, invoke_stub_);
593 }
594
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700595 bool HasSameNameAndDescriptor(const Method* that) const;
596
Ian Rogersb033c752011-07-20 12:22:35 -0700597 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700598 // access flags; low 16 bits are defined by spec (could be uint16_t?)
599 uint32_t access_flags_;
600
601 // For concrete virtual methods, this is the offset of the method
602 // in "vtable".
603 //
604 // For abstract methods in an interface class, this is the offset
605 // of the method in "iftable[n]->methodIndexArray".
606 uint16_t method_index_;
607
608 // Method bounds; not needed for an abstract method.
609 //
610 // For a native method, we compute the size of the argument list, and
611 // set "insSize" and "registerSize" equal to it.
612 uint16_t num_registers_; // ins + locals
613 uint16_t num_outs_;
614 uint16_t num_ins_;
615
buzbeec143c552011-08-20 17:38:58 -0700616 // Total size in bytes of the frame
617 uint32_t frame_size_;
618
619 // Architecture-dependent register spill masks
620 uint32_t core_spill_mask_;
621 uint32_t fp_spill_mask_;
622
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700623 // The method descriptor. This represents the parameters a method
624 // takes and value it returns. This string is a list of the type
625 // descriptors for the parameters enclosed in parenthesis followed
626 // by the return type descriptor. For example, for the method
627 //
628 // Object mymethod(int i, double d, Thread t)
629 //
630 // the method descriptor would be
631 //
632 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700633 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700634
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700635 // Method prototype descriptor string (return and argument types).
636 uint32_t proto_idx_;
637
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700638 // Offset to the CodeItem.
639 uint32_t code_off_;
640
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700641 // The short-form method descriptor string.
642 StringPiece shorty_;
643
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700644 // short cuts to declaring_class_->dex_cache_ members for fast compiled code access
645 ObjectArray<String>* dex_cache_strings_;
646 ObjectArray<Class>* dex_cache_classes_;
647 ObjectArray<Method>* dex_cache_methods_;
648 ObjectArray<Field>* dex_cache_fields_;
649
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700650 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700651 // Compiled code associated with this method
buzbeec143c552011-08-20 17:38:58 -0700652 scoped_ptr<MemMap> code_area_;
653 void* code_;
654 // Instruction set of the coompiled code
655 InstructionSet code_instruction_set_;
656
657 // Size in bytes of compiled code associated with this method
658 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700659
660 // Any native method registered with this method
661 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700662
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700663 // Native invocation stub entry point.
664 const InvokeStub* invoke_stub_;
665
Carl Shapirof88c9522011-08-06 15:47:38 -0700666 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700667};
668
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700669class Array : public Object {
670 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700671 static size_t SizeOf(size_t component_count,
672 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700673 return sizeof(Array) + component_count * component_size;
674 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700675
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700676 // A convenience for code that doesn't know the component size,
677 // and doesn't want to have to work it out itself.
678 static Array* Alloc(Class* array_class, size_t component_count);
679
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700680 static Array* Alloc(Class* array_class,
681 size_t component_count,
682 size_t component_size) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700683 size_t size = SizeOf(component_count, component_size);
684 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700685 if (array != NULL) {
686 array->SetLength(component_count);
687 }
688 return array;
689 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700690
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700691 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700692
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700693 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700694 return length_;
695 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700696
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700697 void SetLength(uint32_t length) {
698 length_ = length;
699 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700700
buzbeec143c552011-08-20 17:38:58 -0700701 static MemberOffset LengthOffset() {
702 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
703 }
704
705 static MemberOffset DataOffset() {
706 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
707 }
708
Elliott Hughes289da822011-08-16 10:11:20 -0700709 protected:
710 bool IsValidIndex(int32_t index) const {
711 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700712 Thread* self = Thread::Current();
713 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
714 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700715 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700716 }
717 return true;
718 }
719
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700720 private:
721 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700722 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400723 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
724 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700725 // Marker for the data (used by generated code)
726 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700727
Carl Shapirof88c9522011-08-06 15:47:38 -0700728 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700729};
730
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700731template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700732class ObjectArray : public Array {
733 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700734 static ObjectArray<T>* Alloc(Class* object_array_class,
735 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700736 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700737 }
738
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700739 T* const * GetData() const {
740 return reinterpret_cast<T* const *>(&elements_);
741 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400742
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700743 T** GetData() {
744 return reinterpret_cast<T**>(&elements_);
745 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400746
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700747 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700748 if (!IsValidIndex(i)) {
749 return NULL;
750 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700751 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700752 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700753
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700754 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700755 if (IsValidIndex(i)) {
756 // TODO: ArrayStoreException
757 GetData()[i] = object; // TODO: write barrier
758 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700759 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700760
761 static void Copy(ObjectArray<T>* src, int src_pos,
762 ObjectArray<T>* dst, int dst_pos,
763 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700764 for (size_t i = 0; i < length; i++) {
765 dst->Set(dst_pos + i, src->Get(src_pos + i));
766 }
767 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700768
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700769 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700770 ObjectArray<T>* new_array = Alloc(klass_, new_length);
771 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
772 return new_array;
773 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700774
775 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700776 // Location of first element.
777 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700778
779 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700780};
781
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700782// ClassLoader objects.
783class ClassLoader : public Object {
784 public:
785 std::vector<const DexFile*>& GetClassPath() {
786 return class_path_;
787 }
788 void SetClassPath(std::vector<const DexFile*>& class_path) {
789 DCHECK_EQ(0U, class_path_.size());
790 class_path_ = class_path;
791 }
792
793 private:
794 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
795 Object* packages_;
796 ClassLoader* parent_;
797
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700798 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700799 std::vector<const DexFile*> class_path_;
800
Carl Shapirof88c9522011-08-06 15:47:38 -0700801 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700802};
803
804class BaseDexClassLoader : public ClassLoader {
805 private:
806 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
807 String* original_path_;
808 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700809 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700810};
811
812class PathClassLoader : public BaseDexClassLoader {
813 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700814 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700815};
816
Carl Shapiro1fb86202011-06-27 17:43:13 -0700817// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700818class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700819 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700820
821 // Class Status
822 //
823 // kStatusNotReady: If a Class cannot be found in the class table by
824 // FindClass, it allocates an new one with AllocClass in the
825 // kStatusNotReady and calls LoadClass. Note if it does find a
826 // class, it may not be kStatusResolved and it will try to push it
827 // forward toward kStatusResolved.
828 //
829 // kStatusIdx: LoadClass populates with Class with information from
830 // the DexFile, moving the status to kStatusIdx, indicating that the
831 // Class values in super_class_ and interfaces_ have not been
832 // populated based on super_class_idx_ and interfaces_idx_. The new
833 // Class can then be inserted into the classes table.
834 //
835 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
836 // attempt to move a kStatusIdx class forward to kStatusLoaded by
837 // using ResolveClass to initialize the super_class_ and interfaces_.
838 //
839 // kStatusResolved: Still holding the lock on Class, the ClassLinker
840 // will use LinkClass to link all members, creating Field and Method
841 // objects, setting up the vtable, etc. On success, the class is
842 // marked kStatusResolved.
843
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700844 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700845 kStatusError = -1,
846 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700847 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700848 kStatusLoaded = 2, // DEX idx values resolved
849 kStatusResolved = 3, // part of linking
850 kStatusVerifying = 4, // in the process of being verified
851 kStatusVerified = 5, // logically part of linking; done pre-init
852 kStatusInitializing = 6, // class init in progress
853 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700854 };
855
856 enum PrimitiveType {
857 kPrimNot = -1
858 };
859
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700860 Object* NewInstance() {
861 return Heap::AllocObject(this, this->object_size_);
862 }
863
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700864 Class* GetSuperClass() const {
865 return super_class_;
866 }
867
868 uint32_t GetSuperClassIdx() const {
869 return super_class_idx_;
870 }
871
872 bool HasSuperClass() const {
873 return super_class_ != NULL;
874 }
875
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700876 bool IsAssignableFrom(const Class* klass) const {
877 DCHECK(klass != NULL);
878 if (this == klass) {
879 return true;
880 }
881 if (IsInterface()) {
882 return klass->Implements(this);
883 }
884 if (klass->IsArray()) {
885 return IsAssignableFromArray(klass);
886 }
887 return klass->IsSubClass(this);
888 }
889
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700890 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700891 return class_loader_;
892 }
893
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700894 DexCache* GetDexCache() const {
895 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700896 }
897
898 Class* GetComponentType() const {
899 return component_type_;
900 }
901
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700902 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700903
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700904 size_t GetComponentSize() const {
905 return GetTypeSize(component_type_->descriptor_);
906 }
907
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700908 const String* GetDescriptor() const {
909 DCHECK(descriptor_ != NULL);
910 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700911 return descriptor_;
912 }
913
914 Status GetStatus() const {
915 return status_;
916 }
917
918 void SetStatus(Status new_status) {
919 // TODO: validate transition
920 status_ = new_status;
921 }
922
Carl Shapiro69759ea2011-07-21 18:13:35 -0700923 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700924 bool IsErroneous() const {
925 return GetStatus() == kStatusError;
926 }
927
Carl Shapiro69759ea2011-07-21 18:13:35 -0700928 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700929 bool IsVerified() const {
930 return GetStatus() >= kStatusVerified;
931 }
932
Carl Shapiro69759ea2011-07-21 18:13:35 -0700933 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700934 bool IsLinked() const {
935 return GetStatus() >= kStatusResolved;
936 }
937
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700938 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700939 bool IsLoaded() const {
940 return GetStatus() >= kStatusLoaded;
941 }
942
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700943 // Returns true if the class is initialized.
944 bool IsInitialized() const {
945 return GetStatus() == kStatusInitialized;
946 }
947
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700948 // Returns true if this class is in the same packages as that class.
949 bool IsInSamePackage(const Class* that) const;
950
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700951 static bool IsInSamePackage(const String* descriptor1,
952 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700953
954 // Returns true if this class represents an array class.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700955 bool IsArray() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700956
957 // Returns true if the class is an interface.
958 bool IsInterface() const {
959 return (access_flags_ & kAccInterface) != 0;
960 }
961
962 // Returns true if the class is declared public.
963 bool IsPublic() const {
964 return (access_flags_ & kAccPublic) != 0;
965 }
966
967 // Returns true if the class is declared final.
968 bool IsFinal() const {
969 return (access_flags_ & kAccFinal) != 0;
970 }
971
972 // Returns true if the class is abstract.
973 bool IsAbstract() const {
974 return (access_flags_ & kAccAbstract) != 0;
975 }
976
977 // Returns true if the class is an annotation.
978 bool IsAnnotation() const {
979 return (access_flags_ & kAccAnnotation) != 0;
980 }
981
982 // Returns true if the class is a primitive type.
983 bool IsPrimitive() const {
984 return primitive_type_ != kPrimNot;
985 }
986
Brian Carlstromae3ac012011-07-27 01:30:28 -0700987 // Returns true if the class is synthetic.
988 bool IsSynthetic() const {
989 return (access_flags_ & kAccSynthetic) != 0;
990 }
991
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700992 // Returns true if this class can access that class.
993 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700994 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700995 }
996
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700997 // Returns the number of static, private, and constructor methods.
998 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700999 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001000 }
1001
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001002 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001003 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001004 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001005 }
1006
1007 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001008 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001009 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001010 }
1011
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001012 Method* FindDeclaredDirectMethod(const StringPiece& name,
1013 const StringPiece& descriptor);
1014
1015 Method* FindDirectMethod(const StringPiece& name,
1016 const StringPiece& descriptor);
1017
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001018 // Returns the number of non-inherited virtual methods.
1019 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001020 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001021 }
1022
1023 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001024 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001025 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001026 }
1027
1028 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001029 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001030 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001031 }
1032
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001033 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1034 const StringPiece& descriptor);
1035
1036 Method* FindVirtualMethod(const StringPiece& name,
1037 const StringPiece& descriptor);
1038
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001039 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001040 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001041 }
1042
Carl Shapiro69759ea2011-07-21 18:13:35 -07001043 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001044 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001045 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001046 }
1047
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 // Finds the given instance field in this class or a superclass.
1049 Field* FindInstanceField(const StringPiece& name,
1050 const StringPiece& descriptor);
1051
1052 Field* FindDeclaredInstanceField(const StringPiece& name,
1053 const StringPiece& descriptor);
1054
1055 // Finds the given static field in this class or a superclass.
1056 Field* FindStaticField(const StringPiece& name,
1057 const StringPiece& descriptor);
1058
1059 Field* FindDeclaredStaticField(const StringPiece& name,
1060 const StringPiece& descriptor);
1061
Jesse Wilson35baaab2011-08-10 16:18:03 -04001062 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001063 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001064 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001065 }
1066
Jesse Wilson35baaab2011-08-10 16:18:03 -04001067 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001068 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001069 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001070 }
1071
1072 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001073 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001074 }
1075
Jesse Wilson35baaab2011-08-10 16:18:03 -04001076 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001077 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001078 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001079 }
1080
Jesse Wilson35baaab2011-08-10 16:18:03 -04001081 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001082 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001083 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001084 }
1085
1086 uint32_t GetReferenceOffsets() const {
1087 return reference_offsets_;
1088 }
1089
1090 void SetReferenceOffsets(uint32_t new_reference_offsets) {
1091 reference_offsets_ = new_reference_offsets;
1092 }
1093
Carl Shapiro69759ea2011-07-21 18:13:35 -07001094 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001095 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001096 }
1097
1098 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001099 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001100 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001101 }
1102
1103 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001104 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001105 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001106 }
1107
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001108 void SetVerifyErrorClass(Class* klass) {
1109 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1110 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1111 klass->SetFieldObject(field_offset, klass);
1112 }
1113
1114 private:
1115 bool Implements(const Class* klass) const;
1116 bool IsArrayAssignableFromArray(const Class* klass) const;
1117 bool IsAssignableFromArray(const Class* klass) const;
1118 bool IsSubClass(const Class* klass) const;
1119
Ian Rogersb033c752011-07-20 12:22:35 -07001120 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001121 // descriptor for the class such as "java.lang.Class" or "[C"
1122 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001123
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001124 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1125 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001126
1127 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001128 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001129
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001130 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001131 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001132 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001133
1134 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001135 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001136
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001137 // If class verify fails, we must return same error on subsequent tries.
1138 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1139 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001140
1141 // threadId, used to check for recursive <clinit> invocation
1142 uint32_t clinit_thread_id_;
1143
1144 // Total object size; used when allocating storage on gc heap. (For
1145 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001146 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001147
1148 // For array classes, the class object for base element, for
1149 // instanceof/checkcast (for String[][][], this will be String).
1150 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001151 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001152
1153 // For array classes, the number of array dimensions, e.g. int[][]
1154 // is 2. Otherwise 0.
1155 int32_t array_rank_;
1156
1157 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1158 PrimitiveType primitive_type_;
1159
1160 // The superclass, or NULL if this is java.lang.Object or a
1161 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001162 Class* super_class_; // TODO: make an instance field
1163 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001164
1165 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001166 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001167
1168 // initiating class loader list
1169 // NOTE: for classes with low serialNumber, these are unused, and the
1170 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001171 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001172
1173 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001174 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001175 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001176
1177 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001178 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001179
1180 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001181 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001182
1183 // Virtual method table (vtable), for use by "invoke-virtual". The
1184 // vtable from the superclass is copied in, and virtual methods from
1185 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001186 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001187
1188 // Interface table (iftable), one entry per interface supported by
1189 // this class. That means one entry for each interface we support
1190 // directly, indirectly via superclass, or indirectly via
1191 // superinterface. This will be null if neither we nor our
1192 // superclass implement any interfaces.
1193 //
1194 // Why we need this: given "class Foo implements Face", declare
1195 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1196 // is part of the Face interface. We can't easily use a single
1197 // vtable.
1198 //
1199 // For every interface a concrete class implements, we create a list
1200 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001202 InterfaceEntry* iftable_;
1203
1204 // The interface vtable indices for iftable get stored here. By
1205 // placing them all in a single pool for each class that implements
1206 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001207 size_t ifvi_pool_count_;
1208 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001209
1210 // instance fields
1211 //
1212 // These describe the layout of the contents of a
1213 // DataObject-compatible Object. Note that only the fields directly
1214 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001215 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001216 //
1217 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001218 // the beginning of the field list. num_reference_instance_fields_
1219 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001220 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001221
1222 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001223 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001224
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001225 // Bitmap of offsets of ifields.
1226 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001227
1228 // source file name, if known. Otherwise, NULL.
1229 const char* source_file_;
1230
Jesse Wilson7833bd22011-08-09 18:31:44 -04001231 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001232 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001233
1234 // static field storage
1235 //
1236 // Each static field is stored in one of three arrays:
1237 // o references are stored in static_references_
1238 // o doubles and longs are stored in static_64bit_primitives_
1239 // o everything else is in static_32bit_primitives_
1240 // Static fields select their array using their type and their index using the
1241 // Field->slot_ member. Storing static fields in arrays avoids the need for a
1242 // special case in the GC.
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001243 ObjectArray<Object>* static_references_;
1244 IntArray* static_32bit_primitives_;
1245 LongArray* static_64bit_primitives_;
1246
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001247 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001248 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001249};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001250std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001251
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001252inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001253 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001254 DCHECK(klass_ != NULL);
1255 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001256}
1257
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001258inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001259 Class* java_lang_Class = klass_->klass_;
1260 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001261}
1262
1263inline bool Object::IsObjectArray() const {
1264 return IsArray() && !klass_->component_type_->IsPrimitive();
1265}
1266
1267inline bool Object::IsArray() const {
1268 return klass_->IsArray();
1269}
1270
Brian Carlstroma663ea52011-08-19 23:33:41 -07001271inline bool Object::IsField() const {
1272 Class* java_lang_Class = klass_->klass_;
1273 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1274 return klass_ == java_lang_reflect_Field;
1275}
1276
1277inline bool Object::IsMethod() const {
1278 Class* java_lang_Class = klass_->klass_;
1279 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1280 return klass_ == java_lang_reflect_Method;
1281}
1282
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001283inline size_t Object::SizeOf() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001284 if (IsArray()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001285 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001286 }
1287 return klass_->object_size_;
1288}
1289
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001290inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001291 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001292}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001293
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001294class DataObject : public Object {
1295 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001296 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001297 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001298 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001299};
1300
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001301template<class T>
1302class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001303 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001304 typedef T ElementType;
1305
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001306 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001307
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001308 const T* GetData() const {
1309 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001310 }
1311
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001312 T* GetData() {
1313 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001314 }
1315
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001316 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001317 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001318 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001319 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001320 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001321 }
1322
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001323 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001324 // TODO: ArrayStoreException
1325 if (IsValidIndex(i)) {
1326 GetData()[i] = value;
1327 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001328 }
1329
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001330 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001331 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001332 CHECK(array_class != NULL);
1333 array_class_ = array_class;
1334 }
1335
Brian Carlstroma663ea52011-08-19 23:33:41 -07001336 static void ResetArrayClass() {
1337 CHECK(array_class_ != NULL);
1338 array_class_ = NULL;
1339 }
1340
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001341 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001342 // Location of first element.
1343 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001344
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001345 static Class* array_class_;
1346
Carl Shapirof88c9522011-08-06 15:47:38 -07001347 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001348};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001349
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001350class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001351 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001352 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001353 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001354 return array_;
1355 }
1356
Carl Shapirof88c9522011-08-06 15:47:38 -07001357 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001358 return hash_code_;
1359 }
1360
Carl Shapirof88c9522011-08-06 15:47:38 -07001361 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001362 return offset_;
1363 }
1364
Carl Shapirof88c9522011-08-06 15:47:38 -07001365 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001366 return count_;
1367 }
1368
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001369 // TODO: do we need this? Equals is the only caller, and could
1370 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001371 uint16_t CharAt(int32_t index) const {
1372 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001373 Thread* self = Thread::Current();
1374 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1375 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001376 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001377 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001378 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001379 }
1380
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001381 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001382 const uint16_t* utf16_data_in,
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001383 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001384 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001385 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001386 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001387 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001388 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001389 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001390 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001391 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001392 }
1393
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001394 static String* AllocFromModifiedUtf8(const char* utf) {
1395 size_t char_count = ModifiedUtf8Len(utf);
1396 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001397 }
1398
Jesse Wilson8989d992011-08-02 13:39:42 -07001399 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1400 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001401 String* string = Alloc(GetJavaLangString(), utf16_length);
1402 uint16_t* utf16_data_out = string->array_->GetData();
1403 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1404 string->ComputeHashCode();
1405 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001406 }
1407
Brian Carlstroma663ea52011-08-19 23:33:41 -07001408 static void SetClass(Class* java_lang_String);
1409 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001410
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001411 static String* Alloc(Class* java_lang_String,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001412 int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001413 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1414 }
1415
1416 static String* Alloc(Class* java_lang_String,
1417 CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001418 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001419 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001420 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001421 return string;
1422 }
1423
1424 // Convert Modified UTF-8 to UTF-16
1425 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1426 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1427 while (*utf8_data_in != '\0') {
1428 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1429 }
1430 }
1431
1432 // Retrieve the next UTF-16 character from a UTF-8 string.
1433 //
1434 // Advances "*pUtf8Ptr" to the start of the next character.
1435 //
1436 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1437 // of a 3-byte sequence, you can end up overrunning the buffer with
1438 // reads (and possibly with the writes if the length was computed and
1439 // cached before the damage). For performance reasons, this function
1440 // assumes that the string being parsed is known to be valid (e.g., by
1441 // already being verified). Most strings we process here are coming
1442 // out of dex files or other internal translations, so the only real
1443 // risk comes from the JNI NewStringUTF call.
1444 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1445 uint8_t one = *(*utf8_data_in)++;
1446 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001447 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001448 return one;
1449 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001450 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001451 uint8_t two = *(*utf8_data_in)++;
1452 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001453 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001454 return ((one & 0x1f) << 6) |
1455 (two & 0x3f);
1456 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001457 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001458 uint8_t three = *(*utf8_data_in)++;
1459 return ((one & 0x0f) << 12) |
1460 ((two & 0x3f) << 6) |
1461 (three & 0x3f);
1462 }
1463
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001464 // Like "strlen", but for strings encoded with "modified" UTF-8.
1465 //
1466 // The value returned is the number of characters, which may or may not
1467 // be the same as the number of bytes.
1468 //
1469 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1470 // get increment {1-3} from table of 8 values.)
1471 static size_t ModifiedUtf8Len(const char* utf8) {
1472 size_t len = 0;
1473 int ic;
1474 while ((ic = *utf8++) != '\0') {
1475 len++;
1476 if ((ic & 0x80) == 0) {
1477 // one-byte encoding
1478 continue;
1479 }
1480 // two- or three-byte encoding
1481 utf8++;
1482 if ((ic & 0x20) == 0) {
1483 // two-byte encoding
1484 continue;
1485 }
1486 // three-byte encoding
1487 utf8++;
1488 }
1489 return len;
1490 }
1491
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001492 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001493 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1494 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001495 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001496 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001497 }
1498 return hash;
1499 }
1500
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001501 void ComputeHashCode() {
1502 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1503 }
1504
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001505 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001506 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001507 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001508 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1509 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001510 return false;
1511 }
1512 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001513 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001514 }
1515
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001516 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001517 bool Equals(const StringPiece& modified_utf8) const {
1518 // TODO: do not assume C-string representation.
1519 return Equals(modified_utf8.data());
1520 }
1521
1522 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001523 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001524 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001525 return false;
1526 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001527 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001528 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001529 return false;
1530 }
1531 }
1532 return true;
1533 }
1534
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001535 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapirof88c9522011-08-06 15:47:38 -07001536 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001537 if (this->GetLength() != that_length) {
1538 return false;
1539 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001540 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001541 if (this->CharAt(i) != that_chars[that_offset + i]) {
1542 return false;
1543 }
1544 }
1545 return true;
1546 }
1547
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001548 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1549 std::string ToModifiedUtf8() const {
1550 std::string result;
1551 for (uint32_t i = 0; i < GetLength(); i++) {
1552 uint16_t ch = CharAt(i);
1553 // The most common case is (ch > 0 && ch <= 0x7f).
1554 if (ch == 0 || ch > 0x7f) {
1555 if (ch > 0x07ff) {
1556 result.push_back((ch >> 12) | 0xe0);
1557 result.push_back(((ch >> 6) & 0x3f) | 0x80);
1558 result.push_back((ch & 0x3f) | 0x80);
1559 } else { // (ch > 0x7f || ch == 0)
1560 result.push_back((ch >> 6) | 0xc0);
1561 result.push_back((ch & 0x3f) | 0x80);
1562 }
1563 } else {
1564 result.push_back(ch);
1565 }
1566 }
1567 return result;
1568 }
1569
1570
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001571 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001572 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1573 CharArray* array_;
1574
Carl Shapirof88c9522011-08-06 15:47:38 -07001575 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001576
Elliott Hughes289da822011-08-16 10:11:20 -07001577 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001578
Elliott Hughes289da822011-08-16 10:11:20 -07001579 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001580
1581 static Class* GetJavaLangString() {
1582 DCHECK(java_lang_String_ != NULL);
1583 return java_lang_String_;
1584 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001585
1586 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001587
1588 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001589};
1590
Brian Carlstroma663ea52011-08-19 23:33:41 -07001591inline bool Object::IsString() const {
1592 // TODO use "klass_ == String::GetJavaLangString()" instead?
1593 return klass_ == klass_->descriptor_->klass_;
1594}
1595
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001596inline size_t Class::GetTypeSize(String* descriptor) {
1597 switch (descriptor->CharAt(0)) {
1598 case 'B': return 1; // byte
1599 case 'C': return 2; // char
1600 case 'D': return 8; // double
1601 case 'F': return 4; // float
1602 case 'I': return 4; // int
1603 case 'J': return 8; // long
1604 case 'S': return 2; // short
1605 case 'Z': return 1; // boolean
1606 case 'L': return sizeof(Object*);
1607 case '[': return sizeof(Array*);
1608 default:
1609 LOG(ERROR) << "Unknown type " << descriptor;
1610 return 0;
1611 }
1612}
1613
1614inline bool Class::IsArray() const {
1615 return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor
1616}
1617
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001618class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001619 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001620 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1621 }
1622
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001623 Class* GetClass() const {
1624 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001625 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001626
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001627 void SetClass(Class* klass) {
1628 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001629 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001630
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001631 private:
1632 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001633 Class* klass_;
1634
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001635 public: // TODO: private
1636 // Index into array of vtable offsets. This points into the
1637 // ifviPool, which holds the vtables for all interfaces declared by
1638 // this class.
1639 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001640
1641 private:
1642 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001643};
1644
1645} // namespace art
1646
1647#endif // ART_SRC_OBJECT_H_