blob: 0bbb4f46ba0aa35abda4b70a15320ed67a981b28 [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"
Shih-wei Liao2fb97532011-08-11 16:17:23 -07008#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070010#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "logging.h"
12#include "macros.h"
13#include "offsets.h"
14#include "stringpiece.h"
15#include "monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17namespace art {
18
19class Array;
20class Class;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070021class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040022class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070023class InterfaceEntry;
24class Monitor;
25class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070026class Object;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040027class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070028template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070029template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070030typedef PrimitiveArray<uint8_t> BooleanArray;
31typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070032typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070033typedef PrimitiveArray<double> DoubleArray;
34typedef PrimitiveArray<float> FloatArray;
35typedef PrimitiveArray<int32_t> IntArray;
36typedef PrimitiveArray<int64_t> LongArray;
37typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070038
Carl Shapiro3ee755d2011-06-28 12:11:04 -070039union JValue {
40 uint8_t z;
41 int8_t b;
42 uint16_t c;
43 int16_t s;
44 int32_t i;
45 int64_t j;
46 float f;
47 double d;
48 Object* l;
49};
50
Brian Carlstrombe977852011-07-19 14:54:54 -070051static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
52static const uint32_t kAccPrivate = 0x0002; // field, method, ic
53static const uint32_t kAccProtected = 0x0004; // field, method, ic
54static const uint32_t kAccStatic = 0x0008; // field, method, ic
55static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
56static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
57static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
58static const uint32_t kAccVolatile = 0x0040; // field
59static const uint32_t kAccBridge = 0x0040; // method (1.5)
60static const uint32_t kAccTransient = 0x0080; // field
61static const uint32_t kAccVarargs = 0x0080; // method (1.5)
62static const uint32_t kAccNative = 0x0100; // method
63static const uint32_t kAccInterface = 0x0200; // class, ic
64static const uint32_t kAccAbstract = 0x0400; // class, method, ic
65static const uint32_t kAccStrict = 0x0800; // method
66static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
67static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
68static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070069
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070070static const uint32_t kAccMiranda = 0x8000; // method
71
Brian Carlstroma331b3c2011-07-18 17:47:56 -070072static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
73
Brian Carlstrombe977852011-07-19 14:54:54 -070074static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
75static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070076
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070077/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070078 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070079 */
80/*
81 * A magic value for refOffsets. Ignore the bits and walk the super
82 * chain when this is the value.
83 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
84 * fields followed by 2 ref instance fields.]
85 */
86#define CLASS_WALK_SUPER ((unsigned int)(3))
87#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
88#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
89#define CLASS_OFFSET_ALIGNMENT 4
90#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
91/*
92 * Given an offset, return the bit number which would encode that offset.
93 * Local use only.
94 */
95#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
96 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
97 CLASS_OFFSET_ALIGNMENT)
98/*
99 * Is the given offset too large to be encoded?
100 */
101#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
102 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
103/*
104 * Return a single bit, encoding the offset.
105 * Undefined if the offset is too large, as defined above.
106 */
107#define CLASS_BIT_FROM_OFFSET(byteOffset) \
108 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
109/*
110 * Return an offset, given a bit number as returned from CLZ.
111 */
112#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700113 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700114
115
Carl Shapiro1fb86202011-06-27 17:43:13 -0700116class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700117 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700118 static bool InstanceOf(const Object* object, const Class* klass) {
119 if (object == NULL) {
120 return false;
121 }
122 return object->InstanceOf(klass);
123 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700124
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700125 Class* GetClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700126 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700127 return klass_;
128 }
129
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700130 bool InstanceOf(const Class* klass) const;
131
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700132 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700133
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134 void MonitorEnter() {
135 monitor_->Enter();
136 }
137
138 void MonitorExit() {
139 monitor_->Exit();
140 }
141
142 void Notify() {
143 monitor_->Notify();
144 }
145
146 void NotifyAll() {
147 monitor_->NotifyAll();
148 }
149
150 void Wait() {
151 monitor_->Wait();
152 }
153
154 void Wait(int64_t timeout) {
155 monitor_->Wait(timeout);
156 }
157
158 void Wait(int64_t timeout, int32_t nanos) {
159 monitor_->Wait(timeout, nanos);
160 }
161
Carl Shapiro69759ea2011-07-21 18:13:35 -0700162 const Object* GetFieldObject(size_t field_offset) const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700163 Object* that = const_cast<Object*>(this);
164 Object* other = that->GetFieldObject(field_offset);
165 return const_cast<const Object*>(other);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700166 }
167
168 Object* GetFieldObject(size_t field_offset) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700169 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset;
170 return *reinterpret_cast<Object**>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171 }
172
173 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700174 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
175 *reinterpret_cast<Object**>(raw_addr) = new_value;
176 // TODO: write barrier
177 }
178
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700179 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700180
181 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700182 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700183 return down_cast<Class*>(this);
184 }
185
186 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700187 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700188 return down_cast<const Class*>(this);
189 }
190
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700191 bool IsObjectArray() const;
192
193 template<class T>
194 ObjectArray<T>* AsObjectArray() {
195 DCHECK(IsObjectArray());
196 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700197 }
198
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700199 template<class T>
200 const ObjectArray<T>* AsObjectArray() const {
201 DCHECK(IsObjectArray());
202 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700203 }
204
205 bool IsReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700206 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700207 return true;
208 }
209
210 bool IsWeakReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700211 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212 return true;
213 }
214
215 bool IsSoftReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700216 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700217 return true;
218 }
219
220 bool IsFinalizerReference() 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 IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700226 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700227 return true;
228 }
229
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700230 bool IsArray() const;
231
232 Array* AsArray() {
233 DCHECK(IsArray());
234 return down_cast<Array*>(this);
235 }
236
237 const Array* AsArray() const {
238 DCHECK(IsArray());
239 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240 }
241
Brian Carlstroma663ea52011-08-19 23:33:41 -0700242 bool IsString() const;
243
244 String* AsString() {
245 DCHECK(IsString());
246 return down_cast<String*>(this);
247 }
248
249 bool IsMethod() const;
250
251 Method* AsMethod() {
252 DCHECK(IsMethod());
253 return down_cast<Method*>(this);
254 }
255
256 bool IsField() const;
257
258 Field* AsField() {
259 DCHECK(IsField());
260 return down_cast<Field*>(this);
261 }
262
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700263 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700264 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700265
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700266 Monitor* monitor_;
267
268 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700269 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700270};
271
272class ObjectLock {
273 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700274 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700275 CHECK(object != NULL);
276 obj_->MonitorEnter();
277 }
278
279 ~ObjectLock() {
280 obj_->MonitorExit();
281 }
282
283 void Wait(int64_t millis = 0) {
284 return obj_->Wait(millis);
285 }
286
287 void Notify() {
288 obj_->Notify();
289 }
290
291 void NotifyAll() {
292 obj_->NotifyAll();
293 }
294
295 private:
296 Object* obj_;
297 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700298};
299
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400300class AccessibleObject : public Object {
301 private:
302 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
303 uint32_t java_flag_;
304};
305
306class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700307 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700308 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700309 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700310 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700311 }
312
Jesse Wilson14150742011-07-29 19:04:44 -0400313 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700314 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700315 return name_;
316 }
317
318 bool IsStatic() const {
319 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700320 }
321
322 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700323 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700324 }
325
Brian Carlstromae3ac012011-07-27 01:30:28 -0700326 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700327 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700328 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700329 }
330
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700331 uint32_t GetOffset() const {
332 return offset_;
333 }
334
335 void SetOffset(size_t num_bytes) {
336 offset_ = num_bytes;
337 }
338
Jesse Wilson35baaab2011-08-10 16:18:03 -0400339 // static field access
Jesse Wilson7833bd22011-08-09 18:31:44 -0400340 bool GetBoolean();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400341 void SetBoolean(bool z);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400342 int8_t GetByte();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400343 void SetByte(int8_t b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400344 uint16_t GetChar();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400345 void SetChar(uint16_t c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400346 uint16_t GetShort();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400347 void SetShort(uint16_t s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400348 int32_t GetInt();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400349 void SetInt(int32_t i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400350 int64_t GetLong();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400351 void SetLong(int64_t j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400352 float GetFloat();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400353 void SetFloat(float f);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400354 double GetDouble();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400355 void SetDouble(double d);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400356 Object* GetObject();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400357 const Object* GetObject() const;
Jesse Wilson7833bd22011-08-09 18:31:44 -0400358 void SetObject(Object* l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700359
Jesse Wilson35baaab2011-08-10 16:18:03 -0400360 public: // TODO: private
361 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
362 // The class in which this field is declared.
363 Class* declaring_class_;
364 Object* generic_type_;
365 uint32_t generic_types_are_initialized_;
366 String* name_;
367 uint32_t offset_;
368 Class* type_;
369
370 // e.g. "I", "[C", "Landroid/os/Debug;"
371 StringPiece descriptor_;
372
373 uint32_t access_flags_;
374
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700375 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400376 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700377};
378
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400379class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700381 // An function that invokes a method with an array of its arguments.
382 typedef void InvokeStub(Method* method,
383 Object* obj,
384 Thread* thread,
385 byte* args,
386 JValue* result);
387
Brian Carlstromae3ac012011-07-27 01:30:28 -0700388 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700389 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700390 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700391 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700392 }
393
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700394 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700395 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700396 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700397 }
398
Brian Carlstroma0808032011-07-18 00:39:23 -0700399 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700400 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700401 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700402 }
403
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700404 static MemberOffset DeclaringClassOffset() {
405 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700406 }
407
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700408 // Returns true if the method is declared public.
409 bool IsPublic() const {
410 return (access_flags_ & kAccPublic) != 0;
411 }
412
413 // Returns true if the method is declared private.
414 bool IsPrivate() const {
415 return (access_flags_ & kAccPrivate) != 0;
416 }
417
418 // Returns true if the method is declared static.
419 bool IsStatic() const {
420 return (access_flags_ & kAccStatic) != 0;
421 }
422
423 // Returns true if the method is declared synchronized.
424 bool IsSynchronized() const {
425 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
426 return (access_flags_ & synchonized) != 0;
427 }
428
429 // Returns true if the method is declared final.
430 bool IsFinal() const {
431 return (access_flags_ & kAccFinal) != 0;
432 }
433
434 // Returns true if the method is declared native.
435 bool IsNative() const {
436 return (access_flags_ & kAccNative) != 0;
437 }
438
439 // Returns true if the method is declared abstract.
440 bool IsAbstract() const {
441 return (access_flags_ & kAccAbstract) != 0;
442 }
443
444 bool IsSynthetic() const {
445 return (access_flags_ & kAccSynthetic) != 0;
446 }
447
448 // Number of argument registers required by the prototype.
449 uint32_t NumArgRegisters();
450
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700451 // Number of argument bytes required for densely packing the
452 // arguments into an array of arguments.
453 size_t NumArgArrayBytes();
454
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700455 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400456 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400457 // the class we are a part of
458 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400459 ObjectArray<Class>* java_exception_types_;
460 Object* java_formal_type_parameters_;
461 Object* java_generic_exception_types_;
462 Object* java_generic_parameter_types_;
463 Object* java_generic_return_type_;
464 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700465 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400466 ObjectArray<Class>* java_parameter_types_;
467 uint32_t java_generic_types_are_initialized_;
468 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700469
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700470 const StringPiece& GetShorty() const {
471 return shorty_;
472 }
473
Ian Rogersb033c752011-07-20 12:22:35 -0700474 bool IsReturnAReference() const {
475 return (shorty_[0] == 'L') || (shorty_[0] == '[');
476 }
477
478 bool IsReturnAFloatOrDouble() const {
479 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
480 }
481
482 bool IsReturnAFloat() const {
483 return shorty_[0] == 'F';
484 }
485
486 bool IsReturnADouble() const {
487 return shorty_[0] == 'D';
488 }
489
490 bool IsReturnALong() const {
491 return shorty_[0] == 'J';
492 }
493
Ian Rogers45a76cb2011-07-21 22:00:15 -0700494 bool IsReturnVoid() const {
495 return shorty_[0] == 'V';
496 }
497
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700498 // "Args" may refer to any of the 3 levels of "Args."
499 // To avoid confusion, our code will denote which "Args" clearly:
500 // 1. UserArgs: Args that a user see.
501 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
502 // receiver.
503 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
504 // E.g., the first in Args is Method* for both static and non-static
505 // methods. And CConvArgs doesn't deal with the receiver because
506 // receiver is hardwired in an implicit register, so CConvArgs doesn't
507 // need to deal with it.
508 //
509 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700510 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700511 // "1 +" because the first in Args is the receiver.
512 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700513 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
514 }
515
516 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700517 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700518 size_t NumReferenceArgs() const;
519
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700520 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700521 size_t NumLongOrDoubleArgs() const;
522
523 // The number of reference arguments to this method before the given
524 // parameter index
525 size_t NumReferenceArgsBefore(unsigned int param) const;
526
527 // Is the given method parameter a reference?
528 bool IsParamAReference(unsigned int param) const;
529
530 // Is the given method parameter a long or double?
531 bool IsParamALongOrDouble(unsigned int param) const;
532
Ian Rogersdf20fe02011-07-20 20:34:16 -0700533 // Size in bytes of the given parameter
534 size_t ParamSize(unsigned int param) const;
535
536 // Size in bytes of the return value
537 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700538
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700539 const void* GetCode() const {
540 return code_;
541 }
542
Ian Rogersb033c752011-07-20 12:22:35 -0700543 void SetCode(const void* code) {
544 code_ = code;
545 }
546
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700547 static size_t GetCodeOffset() {
548 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700549 }
550
551 void RegisterNative(const void* native_method) {
552 native_method_ = native_method;
553 }
554
555 static MemberOffset NativeMethodOffset() {
556 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
557 }
558
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700559 InvokeStub* GetInvokeStub() const {
560 return invoke_stub_;
561 }
562
563 void SetInvokeStub(const InvokeStub* invoke_stub) {
564 invoke_stub_ = invoke_stub;
565 }
566
567 static size_t GetInvokeStubOffset() {
568 return OFFSETOF_MEMBER(Method, invoke_stub_);
569 }
570
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700571 bool HasSameNameAndDescriptor(const Method* that) const;
572
Ian Rogersb033c752011-07-20 12:22:35 -0700573 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700574 // access flags; low 16 bits are defined by spec (could be uint16_t?)
575 uint32_t access_flags_;
576
577 // For concrete virtual methods, this is the offset of the method
578 // in "vtable".
579 //
580 // For abstract methods in an interface class, this is the offset
581 // of the method in "iftable[n]->methodIndexArray".
582 uint16_t method_index_;
583
584 // Method bounds; not needed for an abstract method.
585 //
586 // For a native method, we compute the size of the argument list, and
587 // set "insSize" and "registerSize" equal to it.
588 uint16_t num_registers_; // ins + locals
589 uint16_t num_outs_;
590 uint16_t num_ins_;
591
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700592 // The method descriptor. This represents the parameters a method
593 // takes and value it returns. This string is a list of the type
594 // descriptors for the parameters enclosed in parenthesis followed
595 // by the return type descriptor. For example, for the method
596 //
597 // Object mymethod(int i, double d, Thread t)
598 //
599 // the method descriptor would be
600 //
601 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700602 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700603
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700604 // Method prototype descriptor string (return and argument types).
605 uint32_t proto_idx_;
606
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700607 // Offset to the CodeItem.
608 uint32_t code_off_;
609
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700610 // The short-form method descriptor string.
611 StringPiece shorty_;
612
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700613 // short cuts to declaring_class_->dex_cache_ members for fast compiled code access
614 ObjectArray<String>* dex_cache_strings_;
615 ObjectArray<Class>* dex_cache_classes_;
616 ObjectArray<Method>* dex_cache_methods_;
617 ObjectArray<Field>* dex_cache_fields_;
618
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700619 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700620 // Compiled code associated with this method
621 const void* code_;
622
623 // Any native method registered with this method
624 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700625
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700626 // Native invocation stub entry point.
627 const InvokeStub* invoke_stub_;
628
Carl Shapirof88c9522011-08-06 15:47:38 -0700629 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700630};
631
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700632class Array : public Object {
633 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700634 static size_t SizeOf(size_t component_count,
635 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700636 return sizeof(Array) + component_count * component_size;
637 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700638
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700639 // A convenience for code that doesn't know the component size,
640 // and doesn't want to have to work it out itself.
641 static Array* Alloc(Class* array_class, size_t component_count);
642
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700643 static Array* Alloc(Class* array_class,
644 size_t component_count,
645 size_t component_size) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700646 size_t size = SizeOf(component_count, component_size);
647 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700648 if (array != NULL) {
649 array->SetLength(component_count);
650 }
651 return array;
652 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700653
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700654 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700655
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700656 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700657 return length_;
658 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700659
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700660 void SetLength(uint32_t length) {
661 length_ = length;
662 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700663
Elliott Hughes289da822011-08-16 10:11:20 -0700664 protected:
665 bool IsValidIndex(int32_t index) const {
666 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700667 Thread* self = Thread::Current();
668 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
669 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700670 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700671 }
672 return true;
673 }
674
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700675 private:
676 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700677 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400678 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
679 int32_t padding_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700680
Carl Shapirof88c9522011-08-06 15:47:38 -0700681 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700682};
683
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700684template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700685class ObjectArray : public Array {
686 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700687 static ObjectArray<T>* Alloc(Class* object_array_class,
688 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700689 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700690 }
691
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700692 T* const * GetData() const {
693 return reinterpret_cast<T* const *>(&elements_);
694 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400695
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700696 T** GetData() {
697 return reinterpret_cast<T**>(&elements_);
698 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400699
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700700 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700701 if (!IsValidIndex(i)) {
702 return NULL;
703 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700704 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700705 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700706
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700707 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700708 if (IsValidIndex(i)) {
709 // TODO: ArrayStoreException
710 GetData()[i] = object; // TODO: write barrier
711 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700712 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700713
714 static void Copy(ObjectArray<T>* src, int src_pos,
715 ObjectArray<T>* dst, int dst_pos,
716 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700717 for (size_t i = 0; i < length; i++) {
718 dst->Set(dst_pos + i, src->Get(src_pos + i));
719 }
720 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700721
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700722 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700723 ObjectArray<T>* new_array = Alloc(klass_, new_length);
724 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
725 return new_array;
726 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700727
728 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700729 // Location of first element.
730 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700731
732 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700733};
734
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700735// ClassLoader objects.
736class ClassLoader : public Object {
737 public:
738 std::vector<const DexFile*>& GetClassPath() {
739 return class_path_;
740 }
741 void SetClassPath(std::vector<const DexFile*>& class_path) {
742 DCHECK_EQ(0U, class_path_.size());
743 class_path_ = class_path;
744 }
745
746 private:
747 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
748 Object* packages_;
749 ClassLoader* parent_;
750
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700751 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700752 std::vector<const DexFile*> class_path_;
753
Carl Shapirof88c9522011-08-06 15:47:38 -0700754 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700755};
756
757class BaseDexClassLoader : public ClassLoader {
758 private:
759 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
760 String* original_path_;
761 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700762 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700763};
764
765class PathClassLoader : public BaseDexClassLoader {
766 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700767 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700768};
769
Carl Shapiro1fb86202011-06-27 17:43:13 -0700770// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700771class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700772 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700773
774 // Class Status
775 //
776 // kStatusNotReady: If a Class cannot be found in the class table by
777 // FindClass, it allocates an new one with AllocClass in the
778 // kStatusNotReady and calls LoadClass. Note if it does find a
779 // class, it may not be kStatusResolved and it will try to push it
780 // forward toward kStatusResolved.
781 //
782 // kStatusIdx: LoadClass populates with Class with information from
783 // the DexFile, moving the status to kStatusIdx, indicating that the
784 // Class values in super_class_ and interfaces_ have not been
785 // populated based on super_class_idx_ and interfaces_idx_. The new
786 // Class can then be inserted into the classes table.
787 //
788 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
789 // attempt to move a kStatusIdx class forward to kStatusLoaded by
790 // using ResolveClass to initialize the super_class_ and interfaces_.
791 //
792 // kStatusResolved: Still holding the lock on Class, the ClassLinker
793 // will use LinkClass to link all members, creating Field and Method
794 // objects, setting up the vtable, etc. On success, the class is
795 // marked kStatusResolved.
796
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700797 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700798 kStatusError = -1,
799 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700800 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700801 kStatusLoaded = 2, // DEX idx values resolved
802 kStatusResolved = 3, // part of linking
803 kStatusVerifying = 4, // in the process of being verified
804 kStatusVerified = 5, // logically part of linking; done pre-init
805 kStatusInitializing = 6, // class init in progress
806 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700807 };
808
809 enum PrimitiveType {
810 kPrimNot = -1
811 };
812
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700813 Object* NewInstance() {
814 return Heap::AllocObject(this, this->object_size_);
815 }
816
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700817 Class* GetSuperClass() const {
818 return super_class_;
819 }
820
821 uint32_t GetSuperClassIdx() const {
822 return super_class_idx_;
823 }
824
825 bool HasSuperClass() const {
826 return super_class_ != NULL;
827 }
828
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700829 bool IsAssignableFrom(const Class* klass) const {
830 DCHECK(klass != NULL);
831 if (this == klass) {
832 return true;
833 }
834 if (IsInterface()) {
835 return klass->Implements(this);
836 }
837 if (klass->IsArray()) {
838 return IsAssignableFromArray(klass);
839 }
840 return klass->IsSubClass(this);
841 }
842
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700843 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700844 return class_loader_;
845 }
846
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700847 DexCache* GetDexCache() const {
848 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700849 }
850
851 Class* GetComponentType() const {
852 return component_type_;
853 }
854
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700855 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700856
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700857 size_t GetComponentSize() const {
858 return GetTypeSize(component_type_->descriptor_);
859 }
860
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700861 const String* GetDescriptor() const {
862 DCHECK(descriptor_ != NULL);
863 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700864 return descriptor_;
865 }
866
867 Status GetStatus() const {
868 return status_;
869 }
870
871 void SetStatus(Status new_status) {
872 // TODO: validate transition
873 status_ = new_status;
874 }
875
Carl Shapiro69759ea2011-07-21 18:13:35 -0700876 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700877 bool IsErroneous() const {
878 return GetStatus() == kStatusError;
879 }
880
Carl Shapiro69759ea2011-07-21 18:13:35 -0700881 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700882 bool IsVerified() const {
883 return GetStatus() >= kStatusVerified;
884 }
885
Carl Shapiro69759ea2011-07-21 18:13:35 -0700886 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700887 bool IsLinked() const {
888 return GetStatus() >= kStatusResolved;
889 }
890
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700891 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700892 bool IsLoaded() const {
893 return GetStatus() >= kStatusLoaded;
894 }
895
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700896 // Returns true if the class is initialized.
897 bool IsInitialized() const {
898 return GetStatus() == kStatusInitialized;
899 }
900
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700901 // Returns true if this class is in the same packages as that class.
902 bool IsInSamePackage(const Class* that) const;
903
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700904 static bool IsInSamePackage(const String* descriptor1,
905 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700906
907 // Returns true if this class represents an array class.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700908 bool IsArray() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700909
910 // Returns true if the class is an interface.
911 bool IsInterface() const {
912 return (access_flags_ & kAccInterface) != 0;
913 }
914
915 // Returns true if the class is declared public.
916 bool IsPublic() const {
917 return (access_flags_ & kAccPublic) != 0;
918 }
919
920 // Returns true if the class is declared final.
921 bool IsFinal() const {
922 return (access_flags_ & kAccFinal) != 0;
923 }
924
925 // Returns true if the class is abstract.
926 bool IsAbstract() const {
927 return (access_flags_ & kAccAbstract) != 0;
928 }
929
930 // Returns true if the class is an annotation.
931 bool IsAnnotation() const {
932 return (access_flags_ & kAccAnnotation) != 0;
933 }
934
935 // Returns true if the class is a primitive type.
936 bool IsPrimitive() const {
937 return primitive_type_ != kPrimNot;
938 }
939
Brian Carlstromae3ac012011-07-27 01:30:28 -0700940 // Returns true if the class is synthetic.
941 bool IsSynthetic() const {
942 return (access_flags_ & kAccSynthetic) != 0;
943 }
944
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700945 // Returns true if this class can access that class.
946 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700947 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700948 }
949
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700950 // Returns the number of static, private, and constructor methods.
951 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700952 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700953 }
954
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700955 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700956 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700957 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700958 }
959
960 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700961 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700962 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700963 }
964
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700965 Method* FindDeclaredDirectMethod(const StringPiece& name,
966 const StringPiece& descriptor);
967
968 Method* FindDirectMethod(const StringPiece& name,
969 const StringPiece& descriptor);
970
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700971 // Returns the number of non-inherited virtual methods.
972 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700973 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700974 }
975
976 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700977 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700978 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700979 }
980
981 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700982 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700983 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700984 }
985
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700986 Method* FindDeclaredVirtualMethod(const StringPiece& name,
987 const StringPiece& descriptor);
988
989 Method* FindVirtualMethod(const StringPiece& name,
990 const StringPiece& descriptor);
991
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700992 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700993 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700994 }
995
Carl Shapiro69759ea2011-07-21 18:13:35 -0700996 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700997 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700998 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700999 }
1000
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 // Finds the given instance field in this class or a superclass.
1002 Field* FindInstanceField(const StringPiece& name,
1003 const StringPiece& descriptor);
1004
1005 Field* FindDeclaredInstanceField(const StringPiece& name,
1006 const StringPiece& descriptor);
1007
1008 // Finds the given static field in this class or a superclass.
1009 Field* FindStaticField(const StringPiece& name,
1010 const StringPiece& descriptor);
1011
1012 Field* FindDeclaredStaticField(const StringPiece& name,
1013 const StringPiece& descriptor);
1014
Jesse Wilson35baaab2011-08-10 16:18:03 -04001015 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001016 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001017 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001018 }
1019
Jesse Wilson35baaab2011-08-10 16:18:03 -04001020 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001021 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001022 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001023 }
1024
1025 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001026 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001027 }
1028
Jesse Wilson35baaab2011-08-10 16:18:03 -04001029 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001030 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001031 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001032 }
1033
Jesse Wilson35baaab2011-08-10 16:18:03 -04001034 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001035 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001036 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001037 }
1038
1039 uint32_t GetReferenceOffsets() const {
1040 return reference_offsets_;
1041 }
1042
1043 void SetReferenceOffsets(uint32_t new_reference_offsets) {
1044 reference_offsets_ = new_reference_offsets;
1045 }
1046
Carl Shapiro69759ea2011-07-21 18:13:35 -07001047 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001048 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001049 }
1050
1051 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001052 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001053 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001054 }
1055
1056 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001057 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001058 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001059 }
1060
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001061 void SetVerifyErrorClass(Class* klass) {
1062 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1063 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1064 klass->SetFieldObject(field_offset, klass);
1065 }
1066
1067 private:
1068 bool Implements(const Class* klass) const;
1069 bool IsArrayAssignableFromArray(const Class* klass) const;
1070 bool IsAssignableFromArray(const Class* klass) const;
1071 bool IsSubClass(const Class* klass) const;
1072
Ian Rogersb033c752011-07-20 12:22:35 -07001073 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001074 // descriptor for the class such as "java.lang.Class" or "[C"
1075 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001076
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001077 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1078 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001079
1080 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001081 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001082
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001083 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001084 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001085 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001086
1087 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001088 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001089
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001090 // If class verify fails, we must return same error on subsequent tries.
1091 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1092 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001093
1094 // threadId, used to check for recursive <clinit> invocation
1095 uint32_t clinit_thread_id_;
1096
1097 // Total object size; used when allocating storage on gc heap. (For
1098 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001099 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001100
1101 // For array classes, the class object for base element, for
1102 // instanceof/checkcast (for String[][][], this will be String).
1103 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001104 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001105
1106 // For array classes, the number of array dimensions, e.g. int[][]
1107 // is 2. Otherwise 0.
1108 int32_t array_rank_;
1109
1110 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1111 PrimitiveType primitive_type_;
1112
1113 // The superclass, or NULL if this is java.lang.Object or a
1114 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001115 Class* super_class_; // TODO: make an instance field
1116 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001117
1118 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001119 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001120
1121 // initiating class loader list
1122 // NOTE: for classes with low serialNumber, these are unused, and the
1123 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001124 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001125
1126 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001127 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001128 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001129
1130 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001131 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001132
1133 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001134 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001135
1136 // Virtual method table (vtable), for use by "invoke-virtual". The
1137 // vtable from the superclass is copied in, and virtual methods from
1138 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001139 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001140
1141 // Interface table (iftable), one entry per interface supported by
1142 // this class. That means one entry for each interface we support
1143 // directly, indirectly via superclass, or indirectly via
1144 // superinterface. This will be null if neither we nor our
1145 // superclass implement any interfaces.
1146 //
1147 // Why we need this: given "class Foo implements Face", declare
1148 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1149 // is part of the Face interface. We can't easily use a single
1150 // vtable.
1151 //
1152 // For every interface a concrete class implements, we create a list
1153 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001154 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001155 InterfaceEntry* iftable_;
1156
1157 // The interface vtable indices for iftable get stored here. By
1158 // placing them all in a single pool for each class that implements
1159 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001160 size_t ifvi_pool_count_;
1161 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001162
1163 // instance fields
1164 //
1165 // These describe the layout of the contents of a
1166 // DataObject-compatible Object. Note that only the fields directly
1167 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001168 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001169 //
1170 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001171 // the beginning of the field list. num_reference_instance_fields_
1172 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001173 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001174
1175 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001176 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001177
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001178 // Bitmap of offsets of ifields.
1179 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001180
1181 // source file name, if known. Otherwise, NULL.
1182 const char* source_file_;
1183
Jesse Wilson7833bd22011-08-09 18:31:44 -04001184 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001185 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001186
1187 // static field storage
1188 //
1189 // Each static field is stored in one of three arrays:
1190 // o references are stored in static_references_
1191 // o doubles and longs are stored in static_64bit_primitives_
1192 // o everything else is in static_32bit_primitives_
1193 // Static fields select their array using their type and their index using the
1194 // Field->slot_ member. Storing static fields in arrays avoids the need for a
1195 // special case in the GC.
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001196 ObjectArray<Object>* static_references_;
1197 IntArray* static_32bit_primitives_;
1198 LongArray* static_64bit_primitives_;
1199
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001200 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001201 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001202};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001203std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001204
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001205inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001206 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001207 DCHECK(klass_ != NULL);
1208 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001209}
1210
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001211inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001212 Class* java_lang_Class = klass_->klass_;
1213 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001214}
1215
1216inline bool Object::IsObjectArray() const {
1217 return IsArray() && !klass_->component_type_->IsPrimitive();
1218}
1219
1220inline bool Object::IsArray() const {
1221 return klass_->IsArray();
1222}
1223
Brian Carlstroma663ea52011-08-19 23:33:41 -07001224inline bool Object::IsField() const {
1225 Class* java_lang_Class = klass_->klass_;
1226 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1227 return klass_ == java_lang_reflect_Field;
1228}
1229
1230inline bool Object::IsMethod() const {
1231 Class* java_lang_Class = klass_->klass_;
1232 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1233 return klass_ == java_lang_reflect_Method;
1234}
1235
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001236inline size_t Object::SizeOf() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001237 if (IsArray()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001238 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001239 }
1240 return klass_->object_size_;
1241}
1242
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001243inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001244 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001245}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001246
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001247class DataObject : public Object {
1248 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001249 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001250 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001251 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001252};
1253
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001254template<class T>
1255class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001256 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001257 typedef T ElementType;
1258
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001259 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001260
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001261 const T* GetData() const {
1262 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001263 }
1264
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001265 T* GetData() {
1266 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001267 }
1268
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001269 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001270 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001271 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001272 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001273 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001274 }
1275
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001276 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001277 // TODO: ArrayStoreException
1278 if (IsValidIndex(i)) {
1279 GetData()[i] = value;
1280 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001281 }
1282
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001283 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001284 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001285 CHECK(array_class != NULL);
1286 array_class_ = array_class;
1287 }
1288
Brian Carlstroma663ea52011-08-19 23:33:41 -07001289 static void ResetArrayClass() {
1290 CHECK(array_class_ != NULL);
1291 array_class_ = NULL;
1292 }
1293
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001294 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001295 // Location of first element.
1296 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001297
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001298 static Class* array_class_;
1299
Carl Shapirof88c9522011-08-06 15:47:38 -07001300 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001301};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001302
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001303class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001304 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001305 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001306 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001307 return array_;
1308 }
1309
Carl Shapirof88c9522011-08-06 15:47:38 -07001310 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001311 return hash_code_;
1312 }
1313
Carl Shapirof88c9522011-08-06 15:47:38 -07001314 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001315 return offset_;
1316 }
1317
Carl Shapirof88c9522011-08-06 15:47:38 -07001318 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001319 return count_;
1320 }
1321
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001322 // TODO: do we need this? Equals is the only caller, and could
1323 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001324 uint16_t CharAt(int32_t index) const {
1325 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001326 Thread* self = Thread::Current();
1327 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1328 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001329 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001330 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001331 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001332 }
1333
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001334 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001335 const uint16_t* utf16_data_in,
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001336 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001337 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001338 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001339 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001340 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001341 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001342 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001343 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001344 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001345 }
1346
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001347 static String* AllocFromModifiedUtf8(const char* utf) {
1348 size_t char_count = ModifiedUtf8Len(utf);
1349 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001350 }
1351
Jesse Wilson8989d992011-08-02 13:39:42 -07001352 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1353 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001354 String* string = Alloc(GetJavaLangString(), utf16_length);
1355 uint16_t* utf16_data_out = string->array_->GetData();
1356 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1357 string->ComputeHashCode();
1358 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001359 }
1360
Brian Carlstroma663ea52011-08-19 23:33:41 -07001361 static void SetClass(Class* java_lang_String);
1362 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001363
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001364 static String* Alloc(Class* java_lang_String,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001365 int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001366 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1367 }
1368
1369 static String* Alloc(Class* java_lang_String,
1370 CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001371 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001372 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001373 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001374 return string;
1375 }
1376
1377 // Convert Modified UTF-8 to UTF-16
1378 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1379 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1380 while (*utf8_data_in != '\0') {
1381 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1382 }
1383 }
1384
1385 // Retrieve the next UTF-16 character from a UTF-8 string.
1386 //
1387 // Advances "*pUtf8Ptr" to the start of the next character.
1388 //
1389 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1390 // of a 3-byte sequence, you can end up overrunning the buffer with
1391 // reads (and possibly with the writes if the length was computed and
1392 // cached before the damage). For performance reasons, this function
1393 // assumes that the string being parsed is known to be valid (e.g., by
1394 // already being verified). Most strings we process here are coming
1395 // out of dex files or other internal translations, so the only real
1396 // risk comes from the JNI NewStringUTF call.
1397 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1398 uint8_t one = *(*utf8_data_in)++;
1399 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001400 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001401 return one;
1402 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001403 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001404 uint8_t two = *(*utf8_data_in)++;
1405 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001406 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001407 return ((one & 0x1f) << 6) |
1408 (two & 0x3f);
1409 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001410 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001411 uint8_t three = *(*utf8_data_in)++;
1412 return ((one & 0x0f) << 12) |
1413 ((two & 0x3f) << 6) |
1414 (three & 0x3f);
1415 }
1416
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001417 // Like "strlen", but for strings encoded with "modified" UTF-8.
1418 //
1419 // The value returned is the number of characters, which may or may not
1420 // be the same as the number of bytes.
1421 //
1422 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1423 // get increment {1-3} from table of 8 values.)
1424 static size_t ModifiedUtf8Len(const char* utf8) {
1425 size_t len = 0;
1426 int ic;
1427 while ((ic = *utf8++) != '\0') {
1428 len++;
1429 if ((ic & 0x80) == 0) {
1430 // one-byte encoding
1431 continue;
1432 }
1433 // two- or three-byte encoding
1434 utf8++;
1435 if ((ic & 0x20) == 0) {
1436 // two-byte encoding
1437 continue;
1438 }
1439 // three-byte encoding
1440 utf8++;
1441 }
1442 return len;
1443 }
1444
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001445 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001446 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1447 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001448 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001449 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001450 }
1451 return hash;
1452 }
1453
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001454 void ComputeHashCode() {
1455 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1456 }
1457
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001458 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001459 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001460 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001461 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1462 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001463 return false;
1464 }
1465 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001466 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001467 }
1468
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001469 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001470 bool Equals(const StringPiece& modified_utf8) const {
1471 // TODO: do not assume C-string representation.
1472 return Equals(modified_utf8.data());
1473 }
1474
1475 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001476 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001477 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001478 return false;
1479 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001480 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001481 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001482 return false;
1483 }
1484 }
1485 return true;
1486 }
1487
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001488 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapirof88c9522011-08-06 15:47:38 -07001489 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001490 if (this->GetLength() != that_length) {
1491 return false;
1492 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001493 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001494 if (this->CharAt(i) != that_chars[that_offset + i]) {
1495 return false;
1496 }
1497 }
1498 return true;
1499 }
1500
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001501 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1502 std::string ToModifiedUtf8() const {
1503 std::string result;
1504 for (uint32_t i = 0; i < GetLength(); i++) {
1505 uint16_t ch = CharAt(i);
1506 // The most common case is (ch > 0 && ch <= 0x7f).
1507 if (ch == 0 || ch > 0x7f) {
1508 if (ch > 0x07ff) {
1509 result.push_back((ch >> 12) | 0xe0);
1510 result.push_back(((ch >> 6) & 0x3f) | 0x80);
1511 result.push_back((ch & 0x3f) | 0x80);
1512 } else { // (ch > 0x7f || ch == 0)
1513 result.push_back((ch >> 6) | 0xc0);
1514 result.push_back((ch & 0x3f) | 0x80);
1515 }
1516 } else {
1517 result.push_back(ch);
1518 }
1519 }
1520 return result;
1521 }
1522
1523
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001524 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001525 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1526 CharArray* array_;
1527
Carl Shapirof88c9522011-08-06 15:47:38 -07001528 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001529
Elliott Hughes289da822011-08-16 10:11:20 -07001530 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001531
Elliott Hughes289da822011-08-16 10:11:20 -07001532 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001533
1534 static Class* GetJavaLangString() {
1535 DCHECK(java_lang_String_ != NULL);
1536 return java_lang_String_;
1537 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001538
1539 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001540
1541 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001542};
1543
Brian Carlstroma663ea52011-08-19 23:33:41 -07001544inline bool Object::IsString() const {
1545 // TODO use "klass_ == String::GetJavaLangString()" instead?
1546 return klass_ == klass_->descriptor_->klass_;
1547}
1548
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001549inline size_t Class::GetTypeSize(String* descriptor) {
1550 switch (descriptor->CharAt(0)) {
1551 case 'B': return 1; // byte
1552 case 'C': return 2; // char
1553 case 'D': return 8; // double
1554 case 'F': return 4; // float
1555 case 'I': return 4; // int
1556 case 'J': return 8; // long
1557 case 'S': return 2; // short
1558 case 'Z': return 1; // boolean
1559 case 'L': return sizeof(Object*);
1560 case '[': return sizeof(Array*);
1561 default:
1562 LOG(ERROR) << "Unknown type " << descriptor;
1563 return 0;
1564 }
1565}
1566
1567inline bool Class::IsArray() const {
1568 return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor
1569}
1570
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001571class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001572 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001573 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1574 }
1575
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001576 Class* GetClass() const {
1577 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001578 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001579
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001580 void SetClass(Class* klass) {
1581 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001582 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001583
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001584 private:
1585 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001586 Class* klass_;
1587
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001588 public: // TODO: private
1589 // Index into array of vtable offsets. This points into the
1590 // ifviPool, which holds the vtables for all interfaces declared by
1591 // this class.
1592 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001593
1594 private:
1595 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001596};
1597
1598} // namespace art
1599
1600#endif // ART_SRC_OBJECT_H_