blob: 0b1c072182be98b0561a61a73ecc4f65925c905a [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
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700488 // Converts a native PC to a virtual PC. TODO: this is a no-op
489 // until we associate a PC mapping table with each method.
490 uintptr_t ToDexPC(const uintptr_t pc) const {
491 return pc;
492 }
493
494 // Converts a virtual PC to a native PC. TODO: this is a no-op
495 // until we associate a PC mapping table with each method.
496 uintptr_t ToNativePC(const uintptr_t pc) const {
497 return pc;
498 }
499
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700500 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400501 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400502 // the class we are a part of
503 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400504 ObjectArray<Class>* java_exception_types_;
505 Object* java_formal_type_parameters_;
506 Object* java_generic_exception_types_;
507 Object* java_generic_parameter_types_;
508 Object* java_generic_return_type_;
509 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700510 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400511 ObjectArray<Class>* java_parameter_types_;
512 uint32_t java_generic_types_are_initialized_;
513 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700514
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700515 const StringPiece& GetShorty() const {
516 return shorty_;
517 }
518
Ian Rogersb033c752011-07-20 12:22:35 -0700519 bool IsReturnAReference() const {
520 return (shorty_[0] == 'L') || (shorty_[0] == '[');
521 }
522
523 bool IsReturnAFloatOrDouble() const {
524 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
525 }
526
527 bool IsReturnAFloat() const {
528 return shorty_[0] == 'F';
529 }
530
531 bool IsReturnADouble() const {
532 return shorty_[0] == 'D';
533 }
534
535 bool IsReturnALong() const {
536 return shorty_[0] == 'J';
537 }
538
Ian Rogers45a76cb2011-07-21 22:00:15 -0700539 bool IsReturnVoid() const {
540 return shorty_[0] == 'V';
541 }
542
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700543 // "Args" may refer to any of the 3 levels of "Args."
544 // To avoid confusion, our code will denote which "Args" clearly:
545 // 1. UserArgs: Args that a user see.
546 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
547 // receiver.
548 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
549 // E.g., the first in Args is Method* for both static and non-static
550 // methods. And CConvArgs doesn't deal with the receiver because
551 // receiver is hardwired in an implicit register, so CConvArgs doesn't
552 // need to deal with it.
553 //
554 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700555 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700556 // "1 +" because the first in Args is the receiver.
557 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700558 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
559 }
560
561 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700562 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700563 size_t NumReferenceArgs() const;
564
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700565 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700566 size_t NumLongOrDoubleArgs() const;
567
568 // The number of reference arguments to this method before the given
569 // parameter index
570 size_t NumReferenceArgsBefore(unsigned int param) const;
571
572 // Is the given method parameter a reference?
573 bool IsParamAReference(unsigned int param) const;
574
575 // Is the given method parameter a long or double?
576 bool IsParamALongOrDouble(unsigned int param) const;
577
Ian Rogersdf20fe02011-07-20 20:34:16 -0700578 // Size in bytes of the given parameter
579 size_t ParamSize(unsigned int param) const;
580
581 // Size in bytes of the return value
582 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700583
buzbeec143c552011-08-20 17:38:58 -0700584 bool HasCode() {
585 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700586 }
587
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700588 void SetCode(const byte* compiled_code,
589 size_t byte_count,
590 InstructionSet set) {
buzbeec143c552011-08-20 17:38:58 -0700591 // Copy the code into an executable region.
592 code_instruction_set_ = set;
593 code_area_.reset(MemMap::Map(byte_count,
594 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700595 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700596 byte* code = code_area_->GetAddress();
597 memcpy(code, compiled_code, byte_count);
598 __builtin___clear_cache(code, code + byte_count);
599
600 uintptr_t address = reinterpret_cast<uintptr_t>(code);
601 if (code_instruction_set_ == kThumb2) {
602 // Set the low-order bit so a BLX will switch to Thumb mode
603 address |= 0x1;
604 }
605 code_ = reinterpret_cast<void*>(address);
606 }
607
608 void SetFrameSize(uint32_t frame_size) {
609 frame_size_ = frame_size;
610 }
611
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700612 void SetPcOffset(uint32_t pc_offset) {
613 pc_offset_ = pc_offset;
614 }
615
616 size_t GetFrameSize() const {
617 return frame_size_;
618 }
619
620 size_t GetPcOffset() const {
621 return pc_offset_;
622 }
623
buzbeec143c552011-08-20 17:38:58 -0700624 void SetCoreSpillMask(uint32_t core_spill_mask) {
625 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700626 }
627
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700628 static size_t GetCodeOffset() {
629 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700630 }
631
buzbeec143c552011-08-20 17:38:58 -0700632 void SetFpSpillMask(uint32_t fp_spill_mask) {
633 fp_spill_mask_ = fp_spill_mask;
634 }
635
Ian Rogersb033c752011-07-20 12:22:35 -0700636 void RegisterNative(const void* native_method) {
637 native_method_ = native_method;
638 }
639
640 static MemberOffset NativeMethodOffset() {
641 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
642 }
643
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700644 InvokeStub* GetInvokeStub() const {
645 return invoke_stub_;
646 }
647
648 void SetInvokeStub(const InvokeStub* invoke_stub) {
649 invoke_stub_ = invoke_stub;
650 }
651
652 static size_t GetInvokeStubOffset() {
653 return OFFSETOF_MEMBER(Method, invoke_stub_);
654 }
655
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700656 bool HasSameNameAndDescriptor(const Method* that) const;
657
Ian Rogersb033c752011-07-20 12:22:35 -0700658 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700659 // access flags; low 16 bits are defined by spec (could be uint16_t?)
660 uint32_t access_flags_;
661
662 // For concrete virtual methods, this is the offset of the method
663 // in "vtable".
664 //
665 // For abstract methods in an interface class, this is the offset
666 // of the method in "iftable[n]->methodIndexArray".
667 uint16_t method_index_;
668
669 // Method bounds; not needed for an abstract method.
670 //
671 // For a native method, we compute the size of the argument list, and
672 // set "insSize" and "registerSize" equal to it.
673 uint16_t num_registers_; // ins + locals
674 uint16_t num_outs_;
675 uint16_t num_ins_;
676
buzbeec143c552011-08-20 17:38:58 -0700677 // Total size in bytes of the frame
678 uint32_t frame_size_;
679
680 // Architecture-dependent register spill masks
681 uint32_t core_spill_mask_;
682 uint32_t fp_spill_mask_;
683
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700684 // The method descriptor. This represents the parameters a method
685 // takes and value it returns. This string is a list of the type
686 // descriptors for the parameters enclosed in parenthesis followed
687 // by the return type descriptor. For example, for the method
688 //
689 // Object mymethod(int i, double d, Thread t)
690 //
691 // the method descriptor would be
692 //
693 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700694 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700695
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700696 // Method prototype descriptor string (return and argument types).
697 uint32_t proto_idx_;
698
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700699 // Offset to the CodeItem.
700 uint32_t code_off_;
701
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700702 // The short-form method descriptor string.
703 StringPiece shorty_;
704
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700705 // short cuts to declaring_class_->dex_cache_ members for fast compiled code access
706 ObjectArray<String>* dex_cache_strings_;
707 ObjectArray<Class>* dex_cache_classes_;
708 ObjectArray<Method>* dex_cache_methods_;
709 ObjectArray<Field>* dex_cache_fields_;
710
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700711 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700712 // Compiled code associated with this method
buzbeec143c552011-08-20 17:38:58 -0700713 scoped_ptr<MemMap> code_area_;
714 void* code_;
715 // Instruction set of the coompiled code
716 InstructionSet code_instruction_set_;
717
718 // Size in bytes of compiled code associated with this method
719 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700720
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700721 // Offset of PC within compiled code (in bytes)
722 uint32_t pc_offset_;
723
Ian Rogersb033c752011-07-20 12:22:35 -0700724 // Any native method registered with this method
725 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700726
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700727 // Native invocation stub entry point.
728 const InvokeStub* invoke_stub_;
729
Carl Shapirof88c9522011-08-06 15:47:38 -0700730 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700731};
732
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700733class Array : public Object {
734 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700735 static size_t SizeOf(size_t component_count,
736 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700737 return sizeof(Array) + component_count * component_size;
738 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700739
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700740 // A convenience for code that doesn't know the component size,
741 // and doesn't want to have to work it out itself.
742 static Array* Alloc(Class* array_class, size_t component_count);
743
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700744 static Array* Alloc(Class* array_class,
745 size_t component_count,
746 size_t component_size) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700747 size_t size = SizeOf(component_count, component_size);
748 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700749 if (array != NULL) {
750 array->SetLength(component_count);
751 }
752 return array;
753 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700754
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700755 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700756
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700757 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700758 return length_;
759 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700760
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700761 void SetLength(uint32_t length) {
762 length_ = length;
763 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700764
buzbeec143c552011-08-20 17:38:58 -0700765 static MemberOffset LengthOffset() {
766 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
767 }
768
769 static MemberOffset DataOffset() {
770 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
771 }
772
Elliott Hughes289da822011-08-16 10:11:20 -0700773 protected:
774 bool IsValidIndex(int32_t index) const {
775 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700776 Thread* self = Thread::Current();
777 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
778 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700779 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700780 }
781 return true;
782 }
783
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700784 private:
785 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700786 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400787 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
788 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700789 // Marker for the data (used by generated code)
790 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700791
Carl Shapirof88c9522011-08-06 15:47:38 -0700792 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700793};
794
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700795template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700796class ObjectArray : public Array {
797 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700798 static ObjectArray<T>* Alloc(Class* object_array_class,
799 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700800 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700801 }
802
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700803 T* const * GetData() const {
804 return reinterpret_cast<T* const *>(&elements_);
805 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400806
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700807 T** GetData() {
808 return reinterpret_cast<T**>(&elements_);
809 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400810
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700811 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700812 if (!IsValidIndex(i)) {
813 return NULL;
814 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700815 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700816 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700817
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700818 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700819 if (IsValidIndex(i)) {
820 // TODO: ArrayStoreException
821 GetData()[i] = object; // TODO: write barrier
822 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700823 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700824
825 static void Copy(ObjectArray<T>* src, int src_pos,
826 ObjectArray<T>* dst, int dst_pos,
827 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700828 for (size_t i = 0; i < length; i++) {
829 dst->Set(dst_pos + i, src->Get(src_pos + i));
830 }
831 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700832
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700833 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700834 ObjectArray<T>* new_array = Alloc(klass_, new_length);
835 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
836 return new_array;
837 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700838
839 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700840 // Location of first element.
841 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700842
843 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700844};
845
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700846// ClassLoader objects.
847class ClassLoader : public Object {
848 public:
849 std::vector<const DexFile*>& GetClassPath() {
850 return class_path_;
851 }
852 void SetClassPath(std::vector<const DexFile*>& class_path) {
853 DCHECK_EQ(0U, class_path_.size());
854 class_path_ = class_path;
855 }
856
857 private:
858 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
859 Object* packages_;
860 ClassLoader* parent_;
861
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700862 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700863 std::vector<const DexFile*> class_path_;
864
Carl Shapirof88c9522011-08-06 15:47:38 -0700865 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700866};
867
868class BaseDexClassLoader : public ClassLoader {
869 private:
870 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
871 String* original_path_;
872 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700873 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700874};
875
876class PathClassLoader : public BaseDexClassLoader {
877 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700878 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700879};
880
Carl Shapiro1fb86202011-06-27 17:43:13 -0700881// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700882class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700883 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700884
885 // Class Status
886 //
887 // kStatusNotReady: If a Class cannot be found in the class table by
888 // FindClass, it allocates an new one with AllocClass in the
889 // kStatusNotReady and calls LoadClass. Note if it does find a
890 // class, it may not be kStatusResolved and it will try to push it
891 // forward toward kStatusResolved.
892 //
893 // kStatusIdx: LoadClass populates with Class with information from
894 // the DexFile, moving the status to kStatusIdx, indicating that the
895 // Class values in super_class_ and interfaces_ have not been
896 // populated based on super_class_idx_ and interfaces_idx_. The new
897 // Class can then be inserted into the classes table.
898 //
899 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
900 // attempt to move a kStatusIdx class forward to kStatusLoaded by
901 // using ResolveClass to initialize the super_class_ and interfaces_.
902 //
903 // kStatusResolved: Still holding the lock on Class, the ClassLinker
904 // will use LinkClass to link all members, creating Field and Method
905 // objects, setting up the vtable, etc. On success, the class is
906 // marked kStatusResolved.
907
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700908 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700909 kStatusError = -1,
910 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700911 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700912 kStatusLoaded = 2, // DEX idx values resolved
913 kStatusResolved = 3, // part of linking
914 kStatusVerifying = 4, // in the process of being verified
915 kStatusVerified = 5, // logically part of linking; done pre-init
916 kStatusInitializing = 6, // class init in progress
917 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700918 };
919
920 enum PrimitiveType {
921 kPrimNot = -1
922 };
923
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700924 Object* NewInstance() {
925 return Heap::AllocObject(this, this->object_size_);
926 }
927
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700928 Class* GetSuperClass() const {
929 return super_class_;
930 }
931
932 uint32_t GetSuperClassIdx() const {
933 return super_class_idx_;
934 }
935
936 bool HasSuperClass() const {
937 return super_class_ != NULL;
938 }
939
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700940 bool IsAssignableFrom(const Class* klass) const {
941 DCHECK(klass != NULL);
942 if (this == klass) {
943 return true;
944 }
945 if (IsInterface()) {
946 return klass->Implements(this);
947 }
948 if (klass->IsArray()) {
949 return IsAssignableFromArray(klass);
950 }
951 return klass->IsSubClass(this);
952 }
953
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700954 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700955 return class_loader_;
956 }
957
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700958 DexCache* GetDexCache() const {
959 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700960 }
961
962 Class* GetComponentType() const {
963 return component_type_;
964 }
965
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700966 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700967
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700968 size_t GetComponentSize() const {
969 return GetTypeSize(component_type_->descriptor_);
970 }
971
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700972 const String* GetDescriptor() const {
973 DCHECK(descriptor_ != NULL);
974 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700975 return descriptor_;
976 }
977
Brian Carlstrom4873d462011-08-21 15:23:39 -0700978 size_t SizeOf() const {
979 return class_size_;
980 }
981
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700982 Status GetStatus() const {
983 return status_;
984 }
985
986 void SetStatus(Status new_status) {
987 // TODO: validate transition
988 status_ = new_status;
989 }
990
Carl Shapiro69759ea2011-07-21 18:13:35 -0700991 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700992 bool IsErroneous() const {
993 return GetStatus() == kStatusError;
994 }
995
Carl Shapiro69759ea2011-07-21 18:13:35 -0700996 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700997 bool IsVerified() const {
998 return GetStatus() >= kStatusVerified;
999 }
1000
Carl Shapiro69759ea2011-07-21 18:13:35 -07001001 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001002 bool IsLinked() const {
1003 return GetStatus() >= kStatusResolved;
1004 }
1005
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001006 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001007 bool IsLoaded() const {
1008 return GetStatus() >= kStatusLoaded;
1009 }
1010
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001011 // Returns true if the class is initialized.
1012 bool IsInitialized() const {
1013 return GetStatus() == kStatusInitialized;
1014 }
1015
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001016 // Returns true if this class is in the same packages as that class.
1017 bool IsInSamePackage(const Class* that) const;
1018
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001019 static bool IsInSamePackage(const String* descriptor1,
1020 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001021
1022 // Returns true if this class represents an array class.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001023 bool IsArray() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001024
1025 // Returns true if the class is an interface.
1026 bool IsInterface() const {
1027 return (access_flags_ & kAccInterface) != 0;
1028 }
1029
1030 // Returns true if the class is declared public.
1031 bool IsPublic() const {
1032 return (access_flags_ & kAccPublic) != 0;
1033 }
1034
1035 // Returns true if the class is declared final.
1036 bool IsFinal() const {
1037 return (access_flags_ & kAccFinal) != 0;
1038 }
1039
1040 // Returns true if the class is abstract.
1041 bool IsAbstract() const {
1042 return (access_flags_ & kAccAbstract) != 0;
1043 }
1044
1045 // Returns true if the class is an annotation.
1046 bool IsAnnotation() const {
1047 return (access_flags_ & kAccAnnotation) != 0;
1048 }
1049
1050 // Returns true if the class is a primitive type.
1051 bool IsPrimitive() const {
1052 return primitive_type_ != kPrimNot;
1053 }
1054
Brian Carlstromae3ac012011-07-27 01:30:28 -07001055 // Returns true if the class is synthetic.
1056 bool IsSynthetic() const {
1057 return (access_flags_ & kAccSynthetic) != 0;
1058 }
1059
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001060 // Returns true if this class can access that class.
1061 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001062 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001063 }
1064
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001065 // Returns the number of static, private, and constructor methods.
1066 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001067 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001068 }
1069
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001070 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001071 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001072 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001073 }
1074
1075 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001076 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001077 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001078 }
1079
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001080 Method* FindDeclaredDirectMethod(const StringPiece& name,
1081 const StringPiece& descriptor);
1082
1083 Method* FindDirectMethod(const StringPiece& name,
1084 const StringPiece& descriptor);
1085
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001086 // Returns the number of non-inherited virtual methods.
1087 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001088 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001089 }
1090
1091 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001092 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001093 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001094 }
1095
1096 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001097 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001098 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001099 }
1100
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001101 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1102 const StringPiece& descriptor);
1103
1104 Method* FindVirtualMethod(const StringPiece& name,
1105 const StringPiece& descriptor);
1106
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001107 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001108 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001109 }
1110
Carl Shapiro69759ea2011-07-21 18:13:35 -07001111 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001112 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001113 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001114 }
1115
Brian Carlstrom4873d462011-08-21 15:23:39 -07001116 // Returns the number of static fields containing reference types.
1117 size_t NumReferenceStaticFields() const {
1118 return num_reference_static_fields_;
1119 }
1120
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 // Finds the given instance field in this class or a superclass.
1122 Field* FindInstanceField(const StringPiece& name,
1123 const StringPiece& descriptor);
1124
1125 Field* FindDeclaredInstanceField(const StringPiece& name,
1126 const StringPiece& descriptor);
1127
1128 // Finds the given static field in this class or a superclass.
1129 Field* FindStaticField(const StringPiece& name,
1130 const StringPiece& descriptor);
1131
1132 Field* FindDeclaredStaticField(const StringPiece& name,
1133 const StringPiece& descriptor);
1134
Jesse Wilson35baaab2011-08-10 16:18:03 -04001135 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001136 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001137 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001138 }
1139
Jesse Wilson35baaab2011-08-10 16:18:03 -04001140 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001141 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001142 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001143 }
1144
1145 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001146 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001147 }
1148
Jesse Wilson35baaab2011-08-10 16:18:03 -04001149 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001150 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001151 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001152 }
1153
Jesse Wilson35baaab2011-08-10 16:18:03 -04001154 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001155 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001156 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001157 }
1158
Brian Carlstrom4873d462011-08-21 15:23:39 -07001159 uint32_t GetReferenceInstanceOffsets() const {
1160 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001161 }
1162
Brian Carlstrom4873d462011-08-21 15:23:39 -07001163 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1164 reference_instance_offsets_ = new_reference_offsets;
1165 }
1166
1167 uint32_t GetReferenceStaticOffsets() const {
1168 return reference_static_offsets_;
1169 }
1170
1171 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1172 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001173 }
1174
Carl Shapiro69759ea2011-07-21 18:13:35 -07001175 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001176 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001177 }
1178
1179 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001180 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001181 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001182 }
1183
1184 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001185 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001186 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001187 }
1188
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001189 void SetVerifyErrorClass(Class* klass) {
1190 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1191 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1192 klass->SetFieldObject(field_offset, klass);
1193 }
1194
1195 private:
1196 bool Implements(const Class* klass) const;
1197 bool IsArrayAssignableFromArray(const Class* klass) const;
1198 bool IsAssignableFromArray(const Class* klass) const;
1199 bool IsSubClass(const Class* klass) const;
1200
Ian Rogersb033c752011-07-20 12:22:35 -07001201 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001202 // descriptor for the class such as "java.lang.Class" or "[C"
1203 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001204
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001205 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1206 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001207
1208 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001209 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001210
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001211 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001212 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001213 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001214
1215 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001216 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001217
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001218 // If class verify fails, we must return same error on subsequent tries.
1219 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1220 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001221
1222 // threadId, used to check for recursive <clinit> invocation
1223 uint32_t clinit_thread_id_;
1224
1225 // Total object size; used when allocating storage on gc heap. (For
1226 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001227 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001228
1229 // For array classes, the class object for base element, for
1230 // instanceof/checkcast (for String[][][], this will be String).
1231 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001232 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001233
1234 // For array classes, the number of array dimensions, e.g. int[][]
1235 // is 2. Otherwise 0.
1236 int32_t array_rank_;
1237
1238 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1239 PrimitiveType primitive_type_;
1240
1241 // The superclass, or NULL if this is java.lang.Object or a
1242 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001243 Class* super_class_; // TODO: make an instance field
1244 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001245
1246 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001247 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001248
1249 // initiating class loader list
1250 // NOTE: for classes with low serialNumber, these are unused, and the
1251 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001252 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001253
1254 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001255 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001256 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001257
1258 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001259 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001260
1261 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001262 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001263
1264 // Virtual method table (vtable), for use by "invoke-virtual". The
1265 // vtable from the superclass is copied in, and virtual methods from
1266 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001267 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001268
1269 // Interface table (iftable), one entry per interface supported by
1270 // this class. That means one entry for each interface we support
1271 // directly, indirectly via superclass, or indirectly via
1272 // superinterface. This will be null if neither we nor our
1273 // superclass implement any interfaces.
1274 //
1275 // Why we need this: given "class Foo implements Face", declare
1276 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1277 // is part of the Face interface. We can't easily use a single
1278 // vtable.
1279 //
1280 // For every interface a concrete class implements, we create a list
1281 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001282 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001283 InterfaceEntry* iftable_;
1284
1285 // The interface vtable indices for iftable get stored here. By
1286 // placing them all in a single pool for each class that implements
1287 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001288 size_t ifvi_pool_count_;
1289 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001290
1291 // instance fields
1292 //
1293 // These describe the layout of the contents of a
1294 // DataObject-compatible Object. Note that only the fields directly
1295 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001296 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001297 //
1298 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001299 // the beginning of the field list. num_reference_instance_fields_
1300 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001301 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001302
Brian Carlstrom4873d462011-08-21 15:23:39 -07001303 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001304 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001305
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001306 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001307 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001308
1309 // source file name, if known. Otherwise, NULL.
1310 const char* source_file_;
1311
Jesse Wilson7833bd22011-08-09 18:31:44 -04001312 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001313 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001314
Brian Carlstrom4873d462011-08-21 15:23:39 -07001315 // number of static fields that are object refs
1316 size_t num_reference_static_fields_;
1317
1318 // Bitmap of offsets of sfields.
1319 uint32_t reference_static_offsets_;
1320
1321 // Total class size; used when allocating storage on gc heap.
1322 size_t class_size_;
1323
1324 // Location of first static field.
1325 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001326
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001327 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001328 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001329};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001330std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001331
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001332inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001333 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001334 DCHECK(klass_ != NULL);
1335 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001336}
1337
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001338inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001339 Class* java_lang_Class = klass_->klass_;
1340 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001341}
1342
Brian Carlstrom4873d462011-08-21 15:23:39 -07001343inline bool Object::IsClassClass() const {
1344 Class* java_lang_Class = klass_->klass_;
1345 return this == java_lang_Class;
1346}
1347
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001348inline bool Object::IsObjectArray() const {
1349 return IsArray() && !klass_->component_type_->IsPrimitive();
1350}
1351
1352inline bool Object::IsArray() const {
1353 return klass_->IsArray();
1354}
1355
Brian Carlstroma663ea52011-08-19 23:33:41 -07001356inline bool Object::IsField() const {
1357 Class* java_lang_Class = klass_->klass_;
1358 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1359 return klass_ == java_lang_reflect_Field;
1360}
1361
1362inline bool Object::IsMethod() const {
1363 Class* java_lang_Class = klass_->klass_;
1364 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1365 return klass_ == java_lang_reflect_Method;
1366}
1367
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001368inline size_t Object::SizeOf() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001369 if (IsArray()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001370 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001371 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001372 if (IsClass()) {
1373 return AsClass()->SizeOf();
1374 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001375 return klass_->object_size_;
1376}
1377
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001378inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001379 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001380}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001381
Brian Carlstrom4873d462011-08-21 15:23:39 -07001382class ClassClass : public Class {
1383 private:
1384 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1385 int32_t padding_;
1386 int64_t serialVersionUID_;
1387 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1388};
1389
1390class StringClass : public Class {
1391 private:
1392 CharArray* ASCII_;
1393 Object* CASE_INSENSITIVE_ORDER_;
1394 uint32_t REPLACEMENT_CHAR_;
1395 int64_t serialVersionUID;
1396 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1397};
1398
1399class FieldClass : public Class {
1400 private:
1401 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1402 uint32_t TYPE_BOOLEAN_;
1403 uint32_t TYPE_BYTE_;
1404 uint32_t TYPE_CHAR_;
1405 uint32_t TYPE_DOUBLE_;
1406 uint32_t TYPE_FLOAT_;
1407 uint32_t TYPE_INTEGER_;
1408 uint32_t TYPE_LONG_;
1409 uint32_t TYPE_SHORT_;
1410 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1411};
1412
1413class MethodClass : public Class {
1414 private:
1415 int32_t DECLARED_;
1416 int32_t PUBLIC_;
1417 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1418};
1419
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001420class DataObject : public Object {
1421 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001422 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001423 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001424 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001425 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001426};
1427
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001428template<class T>
1429class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001430 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001431 typedef T ElementType;
1432
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001433 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001434
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001435 const T* GetData() const {
1436 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001437 }
1438
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001439 T* GetData() {
1440 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001441 }
1442
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001443 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001444 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001445 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001446 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001447 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001448 }
1449
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001450 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001451 // TODO: ArrayStoreException
1452 if (IsValidIndex(i)) {
1453 GetData()[i] = value;
1454 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001455 }
1456
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001457 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001458 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001459 CHECK(array_class != NULL);
1460 array_class_ = array_class;
1461 }
1462
Brian Carlstroma663ea52011-08-19 23:33:41 -07001463 static void ResetArrayClass() {
1464 CHECK(array_class_ != NULL);
1465 array_class_ = NULL;
1466 }
1467
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001468 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001469 // Location of first element.
1470 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001471
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001472 static Class* array_class_;
1473
Carl Shapirof88c9522011-08-06 15:47:38 -07001474 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001475};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001476
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001477class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001478 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001479 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001480 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001481 return array_;
1482 }
1483
Carl Shapirof88c9522011-08-06 15:47:38 -07001484 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001485 return hash_code_;
1486 }
1487
Carl Shapirof88c9522011-08-06 15:47:38 -07001488 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001489 return offset_;
1490 }
1491
Carl Shapirof88c9522011-08-06 15:47:38 -07001492 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001493 return count_;
1494 }
1495
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001496 // TODO: do we need this? Equals is the only caller, and could
1497 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001498 uint16_t CharAt(int32_t index) const {
1499 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001500 Thread* self = Thread::Current();
1501 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1502 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001503 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001504 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001505 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001506 }
1507
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001508 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001509 const uint16_t* utf16_data_in,
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001510 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001511 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001512 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001513 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001514 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001515 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001516 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001517 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001518 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001519 }
1520
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001521 static String* AllocFromModifiedUtf8(const char* utf) {
1522 size_t char_count = ModifiedUtf8Len(utf);
1523 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001524 }
1525
Jesse Wilson8989d992011-08-02 13:39:42 -07001526 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1527 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001528 String* string = Alloc(GetJavaLangString(), utf16_length);
1529 uint16_t* utf16_data_out = string->array_->GetData();
1530 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1531 string->ComputeHashCode();
1532 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001533 }
1534
Brian Carlstroma663ea52011-08-19 23:33:41 -07001535 static void SetClass(Class* java_lang_String);
1536 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001537
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001538 static String* Alloc(Class* java_lang_String,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001539 int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001540 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1541 }
1542
1543 static String* Alloc(Class* java_lang_String,
1544 CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001545 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001546 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001547 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001548 return string;
1549 }
1550
1551 // Convert Modified UTF-8 to UTF-16
1552 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1553 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1554 while (*utf8_data_in != '\0') {
1555 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1556 }
1557 }
1558
1559 // Retrieve the next UTF-16 character from a UTF-8 string.
1560 //
1561 // Advances "*pUtf8Ptr" to the start of the next character.
1562 //
1563 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1564 // of a 3-byte sequence, you can end up overrunning the buffer with
1565 // reads (and possibly with the writes if the length was computed and
1566 // cached before the damage). For performance reasons, this function
1567 // assumes that the string being parsed is known to be valid (e.g., by
1568 // already being verified). Most strings we process here are coming
1569 // out of dex files or other internal translations, so the only real
1570 // risk comes from the JNI NewStringUTF call.
1571 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1572 uint8_t one = *(*utf8_data_in)++;
1573 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001574 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001575 return one;
1576 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001577 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001578 uint8_t two = *(*utf8_data_in)++;
1579 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001580 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001581 return ((one & 0x1f) << 6) |
1582 (two & 0x3f);
1583 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001584 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001585 uint8_t three = *(*utf8_data_in)++;
1586 return ((one & 0x0f) << 12) |
1587 ((two & 0x3f) << 6) |
1588 (three & 0x3f);
1589 }
1590
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001591 // Like "strlen", but for strings encoded with "modified" UTF-8.
1592 //
1593 // The value returned is the number of characters, which may or may not
1594 // be the same as the number of bytes.
1595 //
1596 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1597 // get increment {1-3} from table of 8 values.)
1598 static size_t ModifiedUtf8Len(const char* utf8) {
1599 size_t len = 0;
1600 int ic;
1601 while ((ic = *utf8++) != '\0') {
1602 len++;
1603 if ((ic & 0x80) == 0) {
1604 // one-byte encoding
1605 continue;
1606 }
1607 // two- or three-byte encoding
1608 utf8++;
1609 if ((ic & 0x20) == 0) {
1610 // two-byte encoding
1611 continue;
1612 }
1613 // three-byte encoding
1614 utf8++;
1615 }
1616 return len;
1617 }
1618
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001619 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001620 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1621 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001622 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001623 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001624 }
1625 return hash;
1626 }
1627
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001628 void ComputeHashCode() {
1629 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1630 }
1631
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001632 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001633 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001634 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001635 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1636 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001637 return false;
1638 }
1639 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001640 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001641 }
1642
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001643 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001644 bool Equals(const StringPiece& modified_utf8) const {
1645 // TODO: do not assume C-string representation.
1646 return Equals(modified_utf8.data());
1647 }
1648
1649 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001650 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001651 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001652 return false;
1653 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001654 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001655 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001656 return false;
1657 }
1658 }
1659 return true;
1660 }
1661
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001662 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapirof88c9522011-08-06 15:47:38 -07001663 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001664 if (this->GetLength() != that_length) {
1665 return false;
1666 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001667 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001668 if (this->CharAt(i) != that_chars[that_offset + i]) {
1669 return false;
1670 }
1671 }
1672 return true;
1673 }
1674
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001675 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1676 std::string ToModifiedUtf8() const {
1677 std::string result;
1678 for (uint32_t i = 0; i < GetLength(); i++) {
1679 uint16_t ch = CharAt(i);
1680 // The most common case is (ch > 0 && ch <= 0x7f).
1681 if (ch == 0 || ch > 0x7f) {
1682 if (ch > 0x07ff) {
1683 result.push_back((ch >> 12) | 0xe0);
1684 result.push_back(((ch >> 6) & 0x3f) | 0x80);
1685 result.push_back((ch & 0x3f) | 0x80);
1686 } else { // (ch > 0x7f || ch == 0)
1687 result.push_back((ch >> 6) | 0xc0);
1688 result.push_back((ch & 0x3f) | 0x80);
1689 }
1690 } else {
1691 result.push_back(ch);
1692 }
1693 }
1694 return result;
1695 }
1696
1697
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001698 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001699 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1700 CharArray* array_;
1701
Carl Shapirof88c9522011-08-06 15:47:38 -07001702 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001703
Elliott Hughes289da822011-08-16 10:11:20 -07001704 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001705
Elliott Hughes289da822011-08-16 10:11:20 -07001706 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001707
1708 static Class* GetJavaLangString() {
1709 DCHECK(java_lang_String_ != NULL);
1710 return java_lang_String_;
1711 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001712
1713 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001714
1715 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001716};
1717
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001718class Throwable : public Object {
1719 private:
1720 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1721 Throwable* cause_;
1722 String* detail_message_;
1723 Object* stack_state_; // Note this is Java volatile:
1724 Object* stack_trace_;
1725 Object* suppressed_exceptions_;
1726
1727 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
1728};
1729
Brian Carlstroma663ea52011-08-19 23:33:41 -07001730inline bool Object::IsString() const {
1731 // TODO use "klass_ == String::GetJavaLangString()" instead?
1732 return klass_ == klass_->descriptor_->klass_;
1733}
1734
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001735inline size_t Class::GetTypeSize(String* descriptor) {
1736 switch (descriptor->CharAt(0)) {
1737 case 'B': return 1; // byte
1738 case 'C': return 2; // char
1739 case 'D': return 8; // double
1740 case 'F': return 4; // float
1741 case 'I': return 4; // int
1742 case 'J': return 8; // long
1743 case 'S': return 2; // short
1744 case 'Z': return 1; // boolean
1745 case 'L': return sizeof(Object*);
1746 case '[': return sizeof(Array*);
1747 default:
1748 LOG(ERROR) << "Unknown type " << descriptor;
1749 return 0;
1750 }
1751}
1752
1753inline bool Class::IsArray() const {
1754 return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor
1755}
1756
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001757class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001758 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001759 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1760 }
1761
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001762 Class* GetClass() const {
1763 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001764 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001765
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001766 void SetClass(Class* klass) {
1767 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001768 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001769
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001770 private:
1771 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001772 Class* klass_;
1773
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001774 public: // TODO: private
1775 // Index into array of vtable offsets. This points into the
1776 // ifviPool, which holds the vtables for all interfaces declared by
1777 // this class.
1778 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001779
1780 private:
1781 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001782};
1783
1784} // namespace art
1785
1786#endif // ART_SRC_OBJECT_H_