blob: 840c9665d9d148e09ec5a4ca2b0b7d2ec48f9449 [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
Brian Carlstrom4873d462011-08-21 15:23:39 -0700161 Object* GetFieldObject(size_t field_offset) const {
162 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
163 return *reinterpret_cast<Object* const *>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700164 }
165
166 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700167 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
168 *reinterpret_cast<Object**>(raw_addr) = new_value;
169 // TODO: write barrier
170 }
171
Brian Carlstrom4873d462011-08-21 15:23:39 -0700172 uint32_t GetField32(size_t field_offset) const {
173 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
174 return *reinterpret_cast<const uint32_t*>(raw_addr);
175 }
176
177 void SetField32(size_t offset, uint32_t new_value) {
178 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
179 *reinterpret_cast<uint32_t*>(raw_addr) = new_value;
180 }
181
182 uint64_t GetField64(size_t field_offset) const {
183 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
184 return *reinterpret_cast<const uint64_t*>(raw_addr);
185 }
186
187 void SetField64(size_t offset, uint64_t new_value) {
188 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
189 *reinterpret_cast<uint64_t*>(raw_addr) = new_value;
190 }
191
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700192 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700193
194 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700195 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700196 return down_cast<Class*>(this);
197 }
198
199 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700200 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700201 return down_cast<const Class*>(this);
202 }
203
Brian Carlstrom4873d462011-08-21 15:23:39 -0700204 bool IsClassClass() const;
205
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700206 bool IsObjectArray() const;
207
208 template<class T>
209 ObjectArray<T>* AsObjectArray() {
210 DCHECK(IsObjectArray());
211 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212 }
213
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700214 template<class T>
215 const ObjectArray<T>* AsObjectArray() const {
216 DCHECK(IsObjectArray());
217 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218 }
219
220 bool IsReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700221 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222 return true;
223 }
224
225 bool IsWeakReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700226 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700227 return true;
228 }
229
230 bool IsSoftReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700231 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700232 return true;
233 }
234
235 bool IsFinalizerReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700236 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700237 return true;
238 }
239
240 bool IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700241 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242 return true;
243 }
244
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700245 bool IsArray() const;
246
247 Array* AsArray() {
248 DCHECK(IsArray());
249 return down_cast<Array*>(this);
250 }
251
252 const Array* AsArray() const {
253 DCHECK(IsArray());
254 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700255 }
256
Brian Carlstroma663ea52011-08-19 23:33:41 -0700257 bool IsString() const;
258
259 String* AsString() {
260 DCHECK(IsString());
261 return down_cast<String*>(this);
262 }
263
264 bool IsMethod() const;
265
266 Method* AsMethod() {
267 DCHECK(IsMethod());
268 return down_cast<Method*>(this);
269 }
270
Brian Carlstrom4873d462011-08-21 15:23:39 -0700271 const Method* AsMethod() const {
272 DCHECK(IsMethod());
273 return down_cast<const Method*>(this);
274 }
275
Brian Carlstroma663ea52011-08-19 23:33:41 -0700276 bool IsField() const;
277
278 Field* AsField() {
279 DCHECK(IsField());
280 return down_cast<Field*>(this);
281 }
282
Brian Carlstrom4873d462011-08-21 15:23:39 -0700283 const Field* AsField() const {
284 DCHECK(IsField());
285 return down_cast<const Field*>(this);
286 }
287
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700288 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700289 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700290
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700291 Monitor* monitor_;
292
293 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700294 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700295};
296
297class ObjectLock {
298 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700299 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700300 CHECK(object != NULL);
301 obj_->MonitorEnter();
302 }
303
304 ~ObjectLock() {
305 obj_->MonitorExit();
306 }
307
308 void Wait(int64_t millis = 0) {
309 return obj_->Wait(millis);
310 }
311
312 void Notify() {
313 obj_->Notify();
314 }
315
316 void NotifyAll() {
317 obj_->NotifyAll();
318 }
319
320 private:
321 Object* obj_;
322 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700323};
324
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400325class AccessibleObject : public Object {
326 private:
327 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
328 uint32_t java_flag_;
329};
330
331class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700332 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700333 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700334 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700335 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700336 }
337
Jesse Wilson14150742011-07-29 19:04:44 -0400338 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700339 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700340 return name_;
341 }
342
343 bool IsStatic() const {
344 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700345 }
346
347 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700348 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700349 }
350
Brian Carlstromae3ac012011-07-27 01:30:28 -0700351 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700352 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700353 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700354 }
355
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700356 uint32_t GetOffset() const {
357 return offset_;
358 }
359
360 void SetOffset(size_t num_bytes) {
361 offset_ = num_bytes;
362 }
363
Brian Carlstrom4873d462011-08-21 15:23:39 -0700364 // field access, null object for static fields
365 bool GetBoolean(const Object* object) const;
366 void SetBoolean(Object* object, bool z) const;
367 int8_t GetByte(const Object* object) const;
368 void SetByte(Object* object, int8_t b) const;
369 uint16_t GetChar(const Object* object) const;
370 void SetChar(Object* object, uint16_t c) const;
371 uint16_t GetShort(const Object* object) const;
372 void SetShort(Object* object, uint16_t s) const;
373 int32_t GetInt(const Object* object) const;
374 void SetInt(Object* object, int32_t i) const;
375 int64_t GetLong(const Object* object) const;
376 void SetLong(Object* object, int64_t j) const;
377 float GetFloat(const Object* object) const;
378 void SetFloat(Object* object, float f) const;
379 double GetDouble(const Object* object) const;
380 void SetDouble(Object* object, double d) const;
381 Object* GetObject(const Object* object) const;
382 void SetObject(Object* object, Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700383
Jesse Wilson35baaab2011-08-10 16:18:03 -0400384 public: // TODO: private
Brian Carlstrom4873d462011-08-21 15:23:39 -0700385
386 // private implemention of field access using raw data
387 uint32_t Get32(const Object* object) const;
388 void Set32(Object* object, uint32_t new_value) const;
389 uint64_t Get64(const Object* object) const;
390 void Set64(Object* object, uint64_t new_value) const;
391 Object* GetObj(const Object* object) const;
392 void SetObj(Object* object, Object* new_value) const;
393
Jesse Wilson35baaab2011-08-10 16:18:03 -0400394 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
395 // The class in which this field is declared.
396 Class* declaring_class_;
397 Object* generic_type_;
398 uint32_t generic_types_are_initialized_;
399 String* name_;
400 uint32_t offset_;
401 Class* type_;
402
403 // e.g. "I", "[C", "Landroid/os/Debug;"
404 StringPiece descriptor_;
405
406 uint32_t access_flags_;
407
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700408 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400409 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700410};
411
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400412class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700413 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700414 // An function that invokes a method with an array of its arguments.
415 typedef void InvokeStub(Method* method,
416 Object* obj,
417 Thread* thread,
418 byte* args,
419 JValue* result);
420
Brian Carlstromae3ac012011-07-27 01:30:28 -0700421 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700422 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700423 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700424 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700425 }
426
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700427 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700428 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700429 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700430 }
431
Brian Carlstroma0808032011-07-18 00:39:23 -0700432 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700433 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700434 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700435 }
436
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700437 static MemberOffset DeclaringClassOffset() {
438 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700439 }
440
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700441 // Returns true if the method is declared public.
442 bool IsPublic() const {
443 return (access_flags_ & kAccPublic) != 0;
444 }
445
446 // Returns true if the method is declared private.
447 bool IsPrivate() const {
448 return (access_flags_ & kAccPrivate) != 0;
449 }
450
451 // Returns true if the method is declared static.
452 bool IsStatic() const {
453 return (access_flags_ & kAccStatic) != 0;
454 }
455
456 // Returns true if the method is declared synchronized.
457 bool IsSynchronized() const {
458 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
459 return (access_flags_ & synchonized) != 0;
460 }
461
462 // Returns true if the method is declared final.
463 bool IsFinal() const {
464 return (access_flags_ & kAccFinal) != 0;
465 }
466
467 // Returns true if the method is declared native.
468 bool IsNative() const {
469 return (access_flags_ & kAccNative) != 0;
470 }
471
472 // Returns true if the method is declared abstract.
473 bool IsAbstract() const {
474 return (access_flags_ & kAccAbstract) != 0;
475 }
476
477 bool IsSynthetic() const {
478 return (access_flags_ & kAccSynthetic) != 0;
479 }
480
481 // Number of argument registers required by the prototype.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700482 uint32_t NumArgRegisters() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700483
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700484 // Number of argument bytes required for densely packing the
485 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700486 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700487
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700488 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400489 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400490 // the class we are a part of
491 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400492 ObjectArray<Class>* java_exception_types_;
493 Object* java_formal_type_parameters_;
494 Object* java_generic_exception_types_;
495 Object* java_generic_parameter_types_;
496 Object* java_generic_return_type_;
497 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700498 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400499 ObjectArray<Class>* java_parameter_types_;
500 uint32_t java_generic_types_are_initialized_;
501 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700502
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700503 const StringPiece& GetShorty() const {
504 return shorty_;
505 }
506
Ian Rogersb033c752011-07-20 12:22:35 -0700507 bool IsReturnAReference() const {
508 return (shorty_[0] == 'L') || (shorty_[0] == '[');
509 }
510
511 bool IsReturnAFloatOrDouble() const {
512 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
513 }
514
515 bool IsReturnAFloat() const {
516 return shorty_[0] == 'F';
517 }
518
519 bool IsReturnADouble() const {
520 return shorty_[0] == 'D';
521 }
522
523 bool IsReturnALong() const {
524 return shorty_[0] == 'J';
525 }
526
Ian Rogers45a76cb2011-07-21 22:00:15 -0700527 bool IsReturnVoid() const {
528 return shorty_[0] == 'V';
529 }
530
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700531 // "Args" may refer to any of the 3 levels of "Args."
532 // To avoid confusion, our code will denote which "Args" clearly:
533 // 1. UserArgs: Args that a user see.
534 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
535 // receiver.
536 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
537 // E.g., the first in Args is Method* for both static and non-static
538 // methods. And CConvArgs doesn't deal with the receiver because
539 // receiver is hardwired in an implicit register, so CConvArgs doesn't
540 // need to deal with it.
541 //
542 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700543 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700544 // "1 +" because the first in Args is the receiver.
545 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700546 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
547 }
548
549 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700550 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700551 size_t NumReferenceArgs() const;
552
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700553 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700554 size_t NumLongOrDoubleArgs() const;
555
556 // The number of reference arguments to this method before the given
557 // parameter index
558 size_t NumReferenceArgsBefore(unsigned int param) const;
559
560 // Is the given method parameter a reference?
561 bool IsParamAReference(unsigned int param) const;
562
563 // Is the given method parameter a long or double?
564 bool IsParamALongOrDouble(unsigned int param) const;
565
Ian Rogersdf20fe02011-07-20 20:34:16 -0700566 // Size in bytes of the given parameter
567 size_t ParamSize(unsigned int param) const;
568
569 // Size in bytes of the return value
570 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700571
buzbeec143c552011-08-20 17:38:58 -0700572 bool HasCode() {
573 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700574 }
575
buzbeec143c552011-08-20 17:38:58 -0700576 void SetCode(const byte* compiled_code, size_t byte_count, InstructionSet set) {
577 // Copy the code into an executable region.
578 code_instruction_set_ = set;
579 code_area_.reset(MemMap::Map(byte_count,
580 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700581 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700582 byte* code = code_area_->GetAddress();
583 memcpy(code, compiled_code, byte_count);
584 __builtin___clear_cache(code, code + byte_count);
585
586 uintptr_t address = reinterpret_cast<uintptr_t>(code);
587 if (code_instruction_set_ == kThumb2) {
588 // Set the low-order bit so a BLX will switch to Thumb mode
589 address |= 0x1;
590 }
591 code_ = reinterpret_cast<void*>(address);
592 }
593
594 void SetFrameSize(uint32_t frame_size) {
595 frame_size_ = frame_size;
596 }
597
598 void SetCoreSpillMask(uint32_t core_spill_mask) {
599 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700600 }
601
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700602 static size_t GetCodeOffset() {
603 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700604 }
605
buzbeec143c552011-08-20 17:38:58 -0700606 void SetFpSpillMask(uint32_t fp_spill_mask) {
607 fp_spill_mask_ = fp_spill_mask;
608 }
609
Ian Rogersb033c752011-07-20 12:22:35 -0700610 void RegisterNative(const void* native_method) {
611 native_method_ = native_method;
612 }
613
614 static MemberOffset NativeMethodOffset() {
615 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
616 }
617
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700618 InvokeStub* GetInvokeStub() const {
619 return invoke_stub_;
620 }
621
622 void SetInvokeStub(const InvokeStub* invoke_stub) {
623 invoke_stub_ = invoke_stub;
624 }
625
626 static size_t GetInvokeStubOffset() {
627 return OFFSETOF_MEMBER(Method, invoke_stub_);
628 }
629
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700630 bool HasSameNameAndDescriptor(const Method* that) const;
631
Ian Rogersb033c752011-07-20 12:22:35 -0700632 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700633 // access flags; low 16 bits are defined by spec (could be uint16_t?)
634 uint32_t access_flags_;
635
636 // For concrete virtual methods, this is the offset of the method
637 // in "vtable".
638 //
639 // For abstract methods in an interface class, this is the offset
640 // of the method in "iftable[n]->methodIndexArray".
641 uint16_t method_index_;
642
643 // Method bounds; not needed for an abstract method.
644 //
645 // For a native method, we compute the size of the argument list, and
646 // set "insSize" and "registerSize" equal to it.
647 uint16_t num_registers_; // ins + locals
648 uint16_t num_outs_;
649 uint16_t num_ins_;
650
buzbeec143c552011-08-20 17:38:58 -0700651 // Total size in bytes of the frame
652 uint32_t frame_size_;
653
654 // Architecture-dependent register spill masks
655 uint32_t core_spill_mask_;
656 uint32_t fp_spill_mask_;
657
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700658 // The method descriptor. This represents the parameters a method
659 // takes and value it returns. This string is a list of the type
660 // descriptors for the parameters enclosed in parenthesis followed
661 // by the return type descriptor. For example, for the method
662 //
663 // Object mymethod(int i, double d, Thread t)
664 //
665 // the method descriptor would be
666 //
667 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700668 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700669
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700670 // Method prototype descriptor string (return and argument types).
671 uint32_t proto_idx_;
672
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700673 // Offset to the CodeItem.
674 uint32_t code_off_;
675
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700676 // The short-form method descriptor string.
677 StringPiece shorty_;
678
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700679 // short cuts to declaring_class_->dex_cache_ members for fast compiled code access
680 ObjectArray<String>* dex_cache_strings_;
681 ObjectArray<Class>* dex_cache_classes_;
682 ObjectArray<Method>* dex_cache_methods_;
683 ObjectArray<Field>* dex_cache_fields_;
684
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700685 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700686 // Compiled code associated with this method
buzbeec143c552011-08-20 17:38:58 -0700687 scoped_ptr<MemMap> code_area_;
688 void* code_;
689 // Instruction set of the coompiled code
690 InstructionSet code_instruction_set_;
691
692 // Size in bytes of compiled code associated with this method
693 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700694
695 // Any native method registered with this method
696 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700697
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700698 // Native invocation stub entry point.
699 const InvokeStub* invoke_stub_;
700
Carl Shapirof88c9522011-08-06 15:47:38 -0700701 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700702};
703
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700704class Array : public Object {
705 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700706 static size_t SizeOf(size_t component_count,
707 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700708 return sizeof(Array) + component_count * component_size;
709 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700710
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700711 // A convenience for code that doesn't know the component size,
712 // and doesn't want to have to work it out itself.
713 static Array* Alloc(Class* array_class, size_t component_count);
714
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700715 static Array* Alloc(Class* array_class,
716 size_t component_count,
717 size_t component_size) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700718 size_t size = SizeOf(component_count, component_size);
719 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700720 if (array != NULL) {
721 array->SetLength(component_count);
722 }
723 return array;
724 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700725
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700726 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700727
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700728 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700729 return length_;
730 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700731
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700732 void SetLength(uint32_t length) {
733 length_ = length;
734 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700735
buzbeec143c552011-08-20 17:38:58 -0700736 static MemberOffset LengthOffset() {
737 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
738 }
739
740 static MemberOffset DataOffset() {
741 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
742 }
743
Elliott Hughes289da822011-08-16 10:11:20 -0700744 protected:
745 bool IsValidIndex(int32_t index) const {
746 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700747 Thread* self = Thread::Current();
748 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
749 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700750 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700751 }
752 return true;
753 }
754
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700755 private:
756 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700757 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400758 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
759 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700760 // Marker for the data (used by generated code)
761 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700762
Carl Shapirof88c9522011-08-06 15:47:38 -0700763 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700764};
765
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700766template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700767class ObjectArray : public Array {
768 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700769 static ObjectArray<T>* Alloc(Class* object_array_class,
770 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700771 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700772 }
773
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700774 T* const * GetData() const {
775 return reinterpret_cast<T* const *>(&elements_);
776 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400777
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700778 T** GetData() {
779 return reinterpret_cast<T**>(&elements_);
780 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400781
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700782 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700783 if (!IsValidIndex(i)) {
784 return NULL;
785 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700786 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700787 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700788
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700789 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700790 if (IsValidIndex(i)) {
791 // TODO: ArrayStoreException
792 GetData()[i] = object; // TODO: write barrier
793 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700794 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700795
796 static void Copy(ObjectArray<T>* src, int src_pos,
797 ObjectArray<T>* dst, int dst_pos,
798 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700799 for (size_t i = 0; i < length; i++) {
800 dst->Set(dst_pos + i, src->Get(src_pos + i));
801 }
802 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700803
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700804 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700805 ObjectArray<T>* new_array = Alloc(klass_, new_length);
806 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
807 return new_array;
808 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700809
810 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700811 // Location of first element.
812 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700813
814 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700815};
816
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700817// ClassLoader objects.
818class ClassLoader : public Object {
819 public:
820 std::vector<const DexFile*>& GetClassPath() {
821 return class_path_;
822 }
823 void SetClassPath(std::vector<const DexFile*>& class_path) {
824 DCHECK_EQ(0U, class_path_.size());
825 class_path_ = class_path;
826 }
827
828 private:
829 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
830 Object* packages_;
831 ClassLoader* parent_;
832
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700833 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700834 std::vector<const DexFile*> class_path_;
835
Carl Shapirof88c9522011-08-06 15:47:38 -0700836 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700837};
838
839class BaseDexClassLoader : public ClassLoader {
840 private:
841 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
842 String* original_path_;
843 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700844 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700845};
846
847class PathClassLoader : public BaseDexClassLoader {
848 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700849 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700850};
851
Carl Shapiro1fb86202011-06-27 17:43:13 -0700852// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700853class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700854 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700855
856 // Class Status
857 //
858 // kStatusNotReady: If a Class cannot be found in the class table by
859 // FindClass, it allocates an new one with AllocClass in the
860 // kStatusNotReady and calls LoadClass. Note if it does find a
861 // class, it may not be kStatusResolved and it will try to push it
862 // forward toward kStatusResolved.
863 //
864 // kStatusIdx: LoadClass populates with Class with information from
865 // the DexFile, moving the status to kStatusIdx, indicating that the
866 // Class values in super_class_ and interfaces_ have not been
867 // populated based on super_class_idx_ and interfaces_idx_. The new
868 // Class can then be inserted into the classes table.
869 //
870 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
871 // attempt to move a kStatusIdx class forward to kStatusLoaded by
872 // using ResolveClass to initialize the super_class_ and interfaces_.
873 //
874 // kStatusResolved: Still holding the lock on Class, the ClassLinker
875 // will use LinkClass to link all members, creating Field and Method
876 // objects, setting up the vtable, etc. On success, the class is
877 // marked kStatusResolved.
878
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700879 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700880 kStatusError = -1,
881 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700882 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700883 kStatusLoaded = 2, // DEX idx values resolved
884 kStatusResolved = 3, // part of linking
885 kStatusVerifying = 4, // in the process of being verified
886 kStatusVerified = 5, // logically part of linking; done pre-init
887 kStatusInitializing = 6, // class init in progress
888 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700889 };
890
891 enum PrimitiveType {
892 kPrimNot = -1
893 };
894
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700895 Object* NewInstance() {
896 return Heap::AllocObject(this, this->object_size_);
897 }
898
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700899 Class* GetSuperClass() const {
900 return super_class_;
901 }
902
903 uint32_t GetSuperClassIdx() const {
904 return super_class_idx_;
905 }
906
907 bool HasSuperClass() const {
908 return super_class_ != NULL;
909 }
910
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700911 bool IsAssignableFrom(const Class* klass) const {
912 DCHECK(klass != NULL);
913 if (this == klass) {
914 return true;
915 }
916 if (IsInterface()) {
917 return klass->Implements(this);
918 }
919 if (klass->IsArray()) {
920 return IsAssignableFromArray(klass);
921 }
922 return klass->IsSubClass(this);
923 }
924
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700925 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700926 return class_loader_;
927 }
928
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700929 DexCache* GetDexCache() const {
930 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700931 }
932
933 Class* GetComponentType() const {
934 return component_type_;
935 }
936
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700937 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700938
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700939 size_t GetComponentSize() const {
940 return GetTypeSize(component_type_->descriptor_);
941 }
942
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700943 const String* GetDescriptor() const {
944 DCHECK(descriptor_ != NULL);
945 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700946 return descriptor_;
947 }
948
Brian Carlstrom4873d462011-08-21 15:23:39 -0700949 size_t SizeOf() const {
950 return class_size_;
951 }
952
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700953 Status GetStatus() const {
954 return status_;
955 }
956
957 void SetStatus(Status new_status) {
958 // TODO: validate transition
959 status_ = new_status;
960 }
961
Carl Shapiro69759ea2011-07-21 18:13:35 -0700962 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700963 bool IsErroneous() const {
964 return GetStatus() == kStatusError;
965 }
966
Carl Shapiro69759ea2011-07-21 18:13:35 -0700967 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700968 bool IsVerified() const {
969 return GetStatus() >= kStatusVerified;
970 }
971
Carl Shapiro69759ea2011-07-21 18:13:35 -0700972 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700973 bool IsLinked() const {
974 return GetStatus() >= kStatusResolved;
975 }
976
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700977 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700978 bool IsLoaded() const {
979 return GetStatus() >= kStatusLoaded;
980 }
981
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700982 // Returns true if the class is initialized.
983 bool IsInitialized() const {
984 return GetStatus() == kStatusInitialized;
985 }
986
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700987 // Returns true if this class is in the same packages as that class.
988 bool IsInSamePackage(const Class* that) const;
989
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700990 static bool IsInSamePackage(const String* descriptor1,
991 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700992
993 // Returns true if this class represents an array class.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700994 bool IsArray() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700995
996 // Returns true if the class is an interface.
997 bool IsInterface() const {
998 return (access_flags_ & kAccInterface) != 0;
999 }
1000
1001 // Returns true if the class is declared public.
1002 bool IsPublic() const {
1003 return (access_flags_ & kAccPublic) != 0;
1004 }
1005
1006 // Returns true if the class is declared final.
1007 bool IsFinal() const {
1008 return (access_flags_ & kAccFinal) != 0;
1009 }
1010
1011 // Returns true if the class is abstract.
1012 bool IsAbstract() const {
1013 return (access_flags_ & kAccAbstract) != 0;
1014 }
1015
1016 // Returns true if the class is an annotation.
1017 bool IsAnnotation() const {
1018 return (access_flags_ & kAccAnnotation) != 0;
1019 }
1020
1021 // Returns true if the class is a primitive type.
1022 bool IsPrimitive() const {
1023 return primitive_type_ != kPrimNot;
1024 }
1025
Brian Carlstromae3ac012011-07-27 01:30:28 -07001026 // Returns true if the class is synthetic.
1027 bool IsSynthetic() const {
1028 return (access_flags_ & kAccSynthetic) != 0;
1029 }
1030
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001031 // Returns true if this class can access that class.
1032 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001033 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001034 }
1035
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001036 // Returns the number of static, private, and constructor methods.
1037 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001038 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001039 }
1040
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001041 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001042 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001043 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001044 }
1045
1046 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001047 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001048 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001049 }
1050
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001051 Method* FindDeclaredDirectMethod(const StringPiece& name,
1052 const StringPiece& descriptor);
1053
1054 Method* FindDirectMethod(const StringPiece& name,
1055 const StringPiece& descriptor);
1056
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001057 // Returns the number of non-inherited virtual methods.
1058 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001059 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001060 }
1061
1062 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001063 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001064 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001065 }
1066
1067 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001068 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001069 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001070 }
1071
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001072 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1073 const StringPiece& descriptor);
1074
1075 Method* FindVirtualMethod(const StringPiece& name,
1076 const StringPiece& descriptor);
1077
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001078 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001079 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001080 }
1081
Carl Shapiro69759ea2011-07-21 18:13:35 -07001082 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001083 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001084 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001085 }
1086
Brian Carlstrom4873d462011-08-21 15:23:39 -07001087 // Returns the number of static fields containing reference types.
1088 size_t NumReferenceStaticFields() const {
1089 return num_reference_static_fields_;
1090 }
1091
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 // Finds the given instance field in this class or a superclass.
1093 Field* FindInstanceField(const StringPiece& name,
1094 const StringPiece& descriptor);
1095
1096 Field* FindDeclaredInstanceField(const StringPiece& name,
1097 const StringPiece& descriptor);
1098
1099 // Finds the given static field in this class or a superclass.
1100 Field* FindStaticField(const StringPiece& name,
1101 const StringPiece& descriptor);
1102
1103 Field* FindDeclaredStaticField(const StringPiece& name,
1104 const StringPiece& descriptor);
1105
Jesse Wilson35baaab2011-08-10 16:18:03 -04001106 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001107 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001108 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001109 }
1110
Jesse Wilson35baaab2011-08-10 16:18:03 -04001111 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001112 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001113 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001114 }
1115
1116 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001117 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001118 }
1119
Jesse Wilson35baaab2011-08-10 16:18:03 -04001120 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001121 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001122 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001123 }
1124
Jesse Wilson35baaab2011-08-10 16:18:03 -04001125 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001126 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001127 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001128 }
1129
Brian Carlstrom4873d462011-08-21 15:23:39 -07001130 uint32_t GetReferenceInstanceOffsets() const {
1131 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001132 }
1133
Brian Carlstrom4873d462011-08-21 15:23:39 -07001134 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1135 reference_instance_offsets_ = new_reference_offsets;
1136 }
1137
1138 uint32_t GetReferenceStaticOffsets() const {
1139 return reference_static_offsets_;
1140 }
1141
1142 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1143 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001144 }
1145
Carl Shapiro69759ea2011-07-21 18:13:35 -07001146 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001147 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001148 }
1149
1150 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001151 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001152 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001153 }
1154
1155 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001156 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001157 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001158 }
1159
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001160 void SetVerifyErrorClass(Class* klass) {
1161 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1162 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1163 klass->SetFieldObject(field_offset, klass);
1164 }
1165
1166 private:
1167 bool Implements(const Class* klass) const;
1168 bool IsArrayAssignableFromArray(const Class* klass) const;
1169 bool IsAssignableFromArray(const Class* klass) const;
1170 bool IsSubClass(const Class* klass) const;
1171
Ian Rogersb033c752011-07-20 12:22:35 -07001172 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001173 // descriptor for the class such as "java.lang.Class" or "[C"
1174 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001175
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001176 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1177 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001178
1179 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001180 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001181
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001182 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001183 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001184 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001185
1186 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001187 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001188
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001189 // If class verify fails, we must return same error on subsequent tries.
1190 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1191 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001192
1193 // threadId, used to check for recursive <clinit> invocation
1194 uint32_t clinit_thread_id_;
1195
1196 // Total object size; used when allocating storage on gc heap. (For
1197 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001198 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001199
1200 // For array classes, the class object for base element, for
1201 // instanceof/checkcast (for String[][][], this will be String).
1202 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001203 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001204
1205 // For array classes, the number of array dimensions, e.g. int[][]
1206 // is 2. Otherwise 0.
1207 int32_t array_rank_;
1208
1209 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1210 PrimitiveType primitive_type_;
1211
1212 // The superclass, or NULL if this is java.lang.Object or a
1213 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001214 Class* super_class_; // TODO: make an instance field
1215 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001216
1217 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001218 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001219
1220 // initiating class loader list
1221 // NOTE: for classes with low serialNumber, these are unused, and the
1222 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001223 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001224
1225 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001226 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001227 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001228
1229 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001230 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001231
1232 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001233 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001234
1235 // Virtual method table (vtable), for use by "invoke-virtual". The
1236 // vtable from the superclass is copied in, and virtual methods from
1237 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001238 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001239
1240 // Interface table (iftable), one entry per interface supported by
1241 // this class. That means one entry for each interface we support
1242 // directly, indirectly via superclass, or indirectly via
1243 // superinterface. This will be null if neither we nor our
1244 // superclass implement any interfaces.
1245 //
1246 // Why we need this: given "class Foo implements Face", declare
1247 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1248 // is part of the Face interface. We can't easily use a single
1249 // vtable.
1250 //
1251 // For every interface a concrete class implements, we create a list
1252 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001253 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001254 InterfaceEntry* iftable_;
1255
1256 // The interface vtable indices for iftable get stored here. By
1257 // placing them all in a single pool for each class that implements
1258 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001259 size_t ifvi_pool_count_;
1260 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001261
1262 // instance fields
1263 //
1264 // These describe the layout of the contents of a
1265 // DataObject-compatible Object. Note that only the fields directly
1266 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001267 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001268 //
1269 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001270 // the beginning of the field list. num_reference_instance_fields_
1271 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001272 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001273
Brian Carlstrom4873d462011-08-21 15:23:39 -07001274 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001275 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001276
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001277 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001278 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001279
1280 // source file name, if known. Otherwise, NULL.
1281 const char* source_file_;
1282
Jesse Wilson7833bd22011-08-09 18:31:44 -04001283 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001284 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001285
Brian Carlstrom4873d462011-08-21 15:23:39 -07001286 // number of static fields that are object refs
1287 size_t num_reference_static_fields_;
1288
1289 // Bitmap of offsets of sfields.
1290 uint32_t reference_static_offsets_;
1291
1292 // Total class size; used when allocating storage on gc heap.
1293 size_t class_size_;
1294
1295 // Location of first static field.
1296 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001297
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001298 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001299 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001300};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001301std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001302
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001303inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001304 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001305 DCHECK(klass_ != NULL);
1306 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001307}
1308
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001309inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001310 Class* java_lang_Class = klass_->klass_;
1311 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001312}
1313
Brian Carlstrom4873d462011-08-21 15:23:39 -07001314inline bool Object::IsClassClass() const {
1315 Class* java_lang_Class = klass_->klass_;
1316 return this == java_lang_Class;
1317}
1318
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001319inline bool Object::IsObjectArray() const {
1320 return IsArray() && !klass_->component_type_->IsPrimitive();
1321}
1322
1323inline bool Object::IsArray() const {
1324 return klass_->IsArray();
1325}
1326
Brian Carlstroma663ea52011-08-19 23:33:41 -07001327inline bool Object::IsField() const {
1328 Class* java_lang_Class = klass_->klass_;
1329 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1330 return klass_ == java_lang_reflect_Field;
1331}
1332
1333inline bool Object::IsMethod() const {
1334 Class* java_lang_Class = klass_->klass_;
1335 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1336 return klass_ == java_lang_reflect_Method;
1337}
1338
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001339inline size_t Object::SizeOf() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001340 if (IsArray()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001341 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001342 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001343 if (IsClass()) {
1344 return AsClass()->SizeOf();
1345 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001346 return klass_->object_size_;
1347}
1348
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001349inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001350 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001351}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001352
Brian Carlstrom4873d462011-08-21 15:23:39 -07001353class ClassClass : public Class {
1354 private:
1355 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1356 int32_t padding_;
1357 int64_t serialVersionUID_;
1358 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1359};
1360
1361class StringClass : public Class {
1362 private:
1363 CharArray* ASCII_;
1364 Object* CASE_INSENSITIVE_ORDER_;
1365 uint32_t REPLACEMENT_CHAR_;
1366 int64_t serialVersionUID;
1367 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1368};
1369
1370class FieldClass : public Class {
1371 private:
1372 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1373 uint32_t TYPE_BOOLEAN_;
1374 uint32_t TYPE_BYTE_;
1375 uint32_t TYPE_CHAR_;
1376 uint32_t TYPE_DOUBLE_;
1377 uint32_t TYPE_FLOAT_;
1378 uint32_t TYPE_INTEGER_;
1379 uint32_t TYPE_LONG_;
1380 uint32_t TYPE_SHORT_;
1381 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1382};
1383
1384class MethodClass : public Class {
1385 private:
1386 int32_t DECLARED_;
1387 int32_t PUBLIC_;
1388 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1389};
1390
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001391class DataObject : public Object {
1392 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001393 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001394 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001395 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001396 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001397};
1398
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001399template<class T>
1400class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001401 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001402 typedef T ElementType;
1403
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001404 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001405
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001406 const T* GetData() const {
1407 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001408 }
1409
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001410 T* GetData() {
1411 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001412 }
1413
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001414 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001415 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001416 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001417 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001418 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001419 }
1420
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001421 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001422 // TODO: ArrayStoreException
1423 if (IsValidIndex(i)) {
1424 GetData()[i] = value;
1425 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001426 }
1427
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001428 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001429 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001430 CHECK(array_class != NULL);
1431 array_class_ = array_class;
1432 }
1433
Brian Carlstroma663ea52011-08-19 23:33:41 -07001434 static void ResetArrayClass() {
1435 CHECK(array_class_ != NULL);
1436 array_class_ = NULL;
1437 }
1438
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001439 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001440 // Location of first element.
1441 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001442
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001443 static Class* array_class_;
1444
Carl Shapirof88c9522011-08-06 15:47:38 -07001445 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001446};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001447
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001448class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001449 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001450 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001451 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001452 return array_;
1453 }
1454
Carl Shapirof88c9522011-08-06 15:47:38 -07001455 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001456 return hash_code_;
1457 }
1458
Carl Shapirof88c9522011-08-06 15:47:38 -07001459 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001460 return offset_;
1461 }
1462
Carl Shapirof88c9522011-08-06 15:47:38 -07001463 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001464 return count_;
1465 }
1466
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001467 // TODO: do we need this? Equals is the only caller, and could
1468 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001469 uint16_t CharAt(int32_t index) const {
1470 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001471 Thread* self = Thread::Current();
1472 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1473 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001474 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001475 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001476 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001477 }
1478
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001479 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001480 const uint16_t* utf16_data_in,
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001481 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001482 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001483 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001484 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001485 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001486 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001487 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001488 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001489 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001490 }
1491
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001492 static String* AllocFromModifiedUtf8(const char* utf) {
1493 size_t char_count = ModifiedUtf8Len(utf);
1494 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001495 }
1496
Jesse Wilson8989d992011-08-02 13:39:42 -07001497 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1498 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001499 String* string = Alloc(GetJavaLangString(), utf16_length);
1500 uint16_t* utf16_data_out = string->array_->GetData();
1501 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1502 string->ComputeHashCode();
1503 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001504 }
1505
Brian Carlstroma663ea52011-08-19 23:33:41 -07001506 static void SetClass(Class* java_lang_String);
1507 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001508
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001509 static String* Alloc(Class* java_lang_String,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001510 int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001511 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1512 }
1513
1514 static String* Alloc(Class* java_lang_String,
1515 CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001516 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001517 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001518 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001519 return string;
1520 }
1521
1522 // Convert Modified UTF-8 to UTF-16
1523 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1524 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1525 while (*utf8_data_in != '\0') {
1526 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1527 }
1528 }
1529
1530 // Retrieve the next UTF-16 character from a UTF-8 string.
1531 //
1532 // Advances "*pUtf8Ptr" to the start of the next character.
1533 //
1534 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1535 // of a 3-byte sequence, you can end up overrunning the buffer with
1536 // reads (and possibly with the writes if the length was computed and
1537 // cached before the damage). For performance reasons, this function
1538 // assumes that the string being parsed is known to be valid (e.g., by
1539 // already being verified). Most strings we process here are coming
1540 // out of dex files or other internal translations, so the only real
1541 // risk comes from the JNI NewStringUTF call.
1542 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1543 uint8_t one = *(*utf8_data_in)++;
1544 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001545 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001546 return one;
1547 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001548 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001549 uint8_t two = *(*utf8_data_in)++;
1550 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001551 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001552 return ((one & 0x1f) << 6) |
1553 (two & 0x3f);
1554 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001555 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001556 uint8_t three = *(*utf8_data_in)++;
1557 return ((one & 0x0f) << 12) |
1558 ((two & 0x3f) << 6) |
1559 (three & 0x3f);
1560 }
1561
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001562 // Like "strlen", but for strings encoded with "modified" UTF-8.
1563 //
1564 // The value returned is the number of characters, which may or may not
1565 // be the same as the number of bytes.
1566 //
1567 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1568 // get increment {1-3} from table of 8 values.)
1569 static size_t ModifiedUtf8Len(const char* utf8) {
1570 size_t len = 0;
1571 int ic;
1572 while ((ic = *utf8++) != '\0') {
1573 len++;
1574 if ((ic & 0x80) == 0) {
1575 // one-byte encoding
1576 continue;
1577 }
1578 // two- or three-byte encoding
1579 utf8++;
1580 if ((ic & 0x20) == 0) {
1581 // two-byte encoding
1582 continue;
1583 }
1584 // three-byte encoding
1585 utf8++;
1586 }
1587 return len;
1588 }
1589
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001590 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001591 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1592 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001593 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001594 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001595 }
1596 return hash;
1597 }
1598
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001599 void ComputeHashCode() {
1600 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1601 }
1602
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001603 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001604 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001605 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001606 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1607 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001608 return false;
1609 }
1610 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001611 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001612 }
1613
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001614 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001615 bool Equals(const StringPiece& modified_utf8) const {
1616 // TODO: do not assume C-string representation.
1617 return Equals(modified_utf8.data());
1618 }
1619
1620 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001621 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001622 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001623 return false;
1624 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001625 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001626 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001627 return false;
1628 }
1629 }
1630 return true;
1631 }
1632
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001633 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapirof88c9522011-08-06 15:47:38 -07001634 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001635 if (this->GetLength() != that_length) {
1636 return false;
1637 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001638 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001639 if (this->CharAt(i) != that_chars[that_offset + i]) {
1640 return false;
1641 }
1642 }
1643 return true;
1644 }
1645
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001646 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1647 std::string ToModifiedUtf8() const {
1648 std::string result;
1649 for (uint32_t i = 0; i < GetLength(); i++) {
1650 uint16_t ch = CharAt(i);
1651 // The most common case is (ch > 0 && ch <= 0x7f).
1652 if (ch == 0 || ch > 0x7f) {
1653 if (ch > 0x07ff) {
1654 result.push_back((ch >> 12) | 0xe0);
1655 result.push_back(((ch >> 6) & 0x3f) | 0x80);
1656 result.push_back((ch & 0x3f) | 0x80);
1657 } else { // (ch > 0x7f || ch == 0)
1658 result.push_back((ch >> 6) | 0xc0);
1659 result.push_back((ch & 0x3f) | 0x80);
1660 }
1661 } else {
1662 result.push_back(ch);
1663 }
1664 }
1665 return result;
1666 }
1667
1668
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001669 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001670 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1671 CharArray* array_;
1672
Carl Shapirof88c9522011-08-06 15:47:38 -07001673 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001674
Elliott Hughes289da822011-08-16 10:11:20 -07001675 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001676
Elliott Hughes289da822011-08-16 10:11:20 -07001677 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001678
1679 static Class* GetJavaLangString() {
1680 DCHECK(java_lang_String_ != NULL);
1681 return java_lang_String_;
1682 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001683
1684 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001685
1686 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001687};
1688
Brian Carlstroma663ea52011-08-19 23:33:41 -07001689inline bool Object::IsString() const {
1690 // TODO use "klass_ == String::GetJavaLangString()" instead?
1691 return klass_ == klass_->descriptor_->klass_;
1692}
1693
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001694inline size_t Class::GetTypeSize(String* descriptor) {
1695 switch (descriptor->CharAt(0)) {
1696 case 'B': return 1; // byte
1697 case 'C': return 2; // char
1698 case 'D': return 8; // double
1699 case 'F': return 4; // float
1700 case 'I': return 4; // int
1701 case 'J': return 8; // long
1702 case 'S': return 2; // short
1703 case 'Z': return 1; // boolean
1704 case 'L': return sizeof(Object*);
1705 case '[': return sizeof(Array*);
1706 default:
1707 LOG(ERROR) << "Unknown type " << descriptor;
1708 return 0;
1709 }
1710}
1711
1712inline bool Class::IsArray() const {
1713 return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor
1714}
1715
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001716class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001717 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001718 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1719 }
1720
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001721 Class* GetClass() const {
1722 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001723 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001724
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001725 void SetClass(Class* klass) {
1726 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001727 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001728
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001729 private:
1730 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001731 Class* klass_;
1732
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001733 public: // TODO: private
1734 // Index into array of vtable offsets. This points into the
1735 // ifviPool, which holds the vtables for all interfaces declared by
1736 // this class.
1737 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001738
1739 private:
1740 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001741};
1742
1743} // namespace art
1744
1745#endif // ART_SRC_OBJECT_H_