blob: 2c666908500c073c2b63bc964a9b0e08728c7dce [file] [log] [blame]
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "object.h"
4
Ian Rogersb033c752011-07-20 12:22:35 -07005#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07006
Ian Rogersdf20fe02011-07-20 20:34:16 -07007#include <algorithm>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07008#include <string>
9#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070011#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070012#include "class_loader.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070014#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070015#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "logging.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070017#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "dex_file.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070019#include "runtime.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070020
21namespace art {
22
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023bool Object::IsString() const {
24 // TODO use "klass_ == String::GetJavaLangString()" instead?
25 return GetClass() == GetClass()->GetDescriptor()->GetClass();
26}
27
28// TODO: get global references for these
29Class* Field::java_lang_reflect_Field_ = NULL;
30
31void Field::SetClass(Class* java_lang_reflect_Field) {
32 CHECK(java_lang_reflect_Field_ == NULL);
33 CHECK(java_lang_reflect_Field != NULL);
34 java_lang_reflect_Field_ = java_lang_reflect_Field;
35}
36
37void Field::ResetClass() {
38 CHECK(java_lang_reflect_Field_ != NULL);
39 java_lang_reflect_Field_ = NULL;
40}
41
42void Field::SetTypeIdx(uint32_t type_idx) {
43 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), type_idx, false);
44}
45
46Class* Field::GetTypeDuringLinking() const {
47 // We are assured that the necessary primitive types are in the dex cache
48 // early during class linking
49 return GetDeclaringClass()->GetDexCache()->GetResolvedType(GetTypeIdx());
50}
51
52Class* Field::GetType() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -070053 DCHECK(Runtime::Current()->IsStarted())
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070054 << "Can't call GetType without an initialized runtime";
55 // Do full linkage (which sets dex cache value to speed next call)
56 return Runtime::Current()->GetClassLinker()->ResolveType(GetTypeIdx(), this);
57}
58
buzbee34cd9e52011-09-08 14:31:52 -070059Field* Field::FindFieldFromCode(uint32_t field_idx, const Method* referrer) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
61 Field* f = class_linker->ResolveField(field_idx, referrer);
62 if (f != NULL) {
63 Class* c = f->GetDeclaringClass();
64 // If the class is already initializing, we must be inside <clinit>, or
65 // we'd still be waiting for the lock.
66 if (c->GetStatus() == Class::kStatusInitializing || class_linker->EnsureInitialized(c)) {
67 return f;
68 }
Brian Carlstromb63ec392011-08-27 17:38:27 -070069 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -070070 UNIMPLEMENTED(FATAL) << "throw an error and unwind";
71 return NULL;
72}
73
74uint32_t Field::Get32StaticFromCode(uint32_t field_idx, const Method* referrer) {
75 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070076 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t));
77 return field->Get32(NULL);
78}
79void Field::Set32StaticFromCode(uint32_t field_idx, const Method* referrer, uint32_t new_value) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070080 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070081 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int32_t));
82 field->Set32(NULL, new_value);
83}
84uint64_t Field::Get64StaticFromCode(uint32_t field_idx, const Method* referrer) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070085 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070086 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t));
87 return field->Get64(NULL);
88}
89void Field::Set64StaticFromCode(uint32_t field_idx, const Method* referrer, uint64_t new_value) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070090 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070091 DCHECK(field->GetType()->PrimitiveSize() == sizeof(int64_t));
92 field->Set64(NULL, new_value);
93}
94Object* Field::GetObjStaticFromCode(uint32_t field_idx, const Method* referrer) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -070095 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070096 DCHECK(!field->GetType()->IsPrimitive());
97 return field->GetObj(NULL);
98}
99void Field::SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, Object* new_value) {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700100 Field* field = FindFieldFromCode(field_idx, referrer);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700101 DCHECK(!field->GetType()->IsPrimitive());
102 field->SetObj(NULL, new_value);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700103}
104
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700105uint32_t Field::Get32(const Object* object) const {
106 CHECK((object == NULL) == IsStatic());
107 if (IsStatic()) {
108 object = declaring_class_;
109 }
110 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700111}
112
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700113void Field::Set32(Object* object, uint32_t new_value) const {
114 CHECK((object == NULL) == IsStatic());
115 if (IsStatic()) {
116 object = declaring_class_;
117 }
118 object->SetField32(GetOffset(), new_value, IsVolatile());
119}
120
121uint64_t Field::Get64(const Object* object) const {
122 CHECK((object == NULL) == IsStatic());
123 if (IsStatic()) {
124 object = declaring_class_;
125 }
126 return object->GetField64(GetOffset(), IsVolatile());
127}
128
129void Field::Set64(Object* object, uint64_t new_value) const {
130 CHECK((object == NULL) == IsStatic());
131 if (IsStatic()) {
132 object = declaring_class_;
133 }
134 object->SetField64(GetOffset(), new_value, IsVolatile());
135}
136
137Object* Field::GetObj(const Object* object) const {
138 CHECK((object == NULL) == IsStatic());
139 if (IsStatic()) {
140 object = declaring_class_;
141 }
142 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
143}
144
145void Field::SetObj(Object* object, const Object* new_value) const {
146 CHECK((object == NULL) == IsStatic());
147 if (IsStatic()) {
148 object = declaring_class_;
149 }
150 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
151}
152
153bool Field::GetBoolean(const Object* object) const {
154 DCHECK(GetType()->IsPrimitiveBoolean());
155 return Get32(object);
156}
157
158void Field::SetBoolean(Object* object, bool z) const {
159 DCHECK(GetType()->IsPrimitiveBoolean());
160 Set32(object, z);
161}
162
163int8_t Field::GetByte(const Object* object) const {
164 DCHECK(GetType()->IsPrimitiveByte());
165 return Get32(object);
166}
167
168void Field::SetByte(Object* object, int8_t b) const {
169 DCHECK(GetType()->IsPrimitiveByte());
170 Set32(object, b);
171}
172
173uint16_t Field::GetChar(const Object* object) const {
174 DCHECK(GetType()->IsPrimitiveChar());
175 return Get32(object);
176}
177
178void Field::SetChar(Object* object, uint16_t c) const {
179 DCHECK(GetType()->IsPrimitiveChar());
180 Set32(object, c);
181}
182
183uint16_t Field::GetShort(const Object* object) const {
184 DCHECK(GetType()->IsPrimitiveShort());
185 return Get32(object);
186}
187
188void Field::SetShort(Object* object, uint16_t s) const {
189 DCHECK(GetType()->IsPrimitiveShort());
190 Set32(object, s);
191}
192
193int32_t Field::GetInt(const Object* object) const {
194 DCHECK(GetType()->IsPrimitiveInt());
195 return Get32(object);
196}
197
198void Field::SetInt(Object* object, int32_t i) const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700199 DCHECK(GetType()->IsPrimitiveInt()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 Set32(object, i);
201}
202
203int64_t Field::GetLong(const Object* object) const {
204 DCHECK(GetType()->IsPrimitiveLong());
205 return Get64(object);
206}
207
208void Field::SetLong(Object* object, int64_t j) const {
209 DCHECK(GetType()->IsPrimitiveLong());
210 Set64(object, j);
211}
212
213float Field::GetFloat(const Object* object) const {
214 DCHECK(GetType()->IsPrimitiveFloat());
215 JValue float_bits;
216 float_bits.i = Get32(object);
217 return float_bits.f;
218}
219
220void Field::SetFloat(Object* object, float f) const {
221 DCHECK(GetType()->IsPrimitiveFloat());
222 JValue float_bits;
223 float_bits.f = f;
224 Set32(object, float_bits.i);
225}
226
227double Field::GetDouble(const Object* object) const {
228 DCHECK(GetType()->IsPrimitiveDouble());
229 JValue double_bits;
230 double_bits.j = Get64(object);
231 return double_bits.d;
232}
233
234void Field::SetDouble(Object* object, double d) const {
235 DCHECK(GetType()->IsPrimitiveDouble());
236 JValue double_bits;
237 double_bits.d = d;
238 Set64(object, double_bits.j);
239}
240
241Object* Field::GetObject(const Object* object) const {
242 CHECK(!GetType()->IsPrimitive());
243 return GetObj(object);
244}
245
246void Field::SetObject(Object* object, const Object* l) const {
247 CHECK(!GetType()->IsPrimitive());
248 SetObj(object, l);
249}
250
251// TODO: get global references for these
252Class* Method::java_lang_reflect_Method_ = NULL;
253
254void Method::SetClass(Class* java_lang_reflect_Method) {
255 CHECK(java_lang_reflect_Method_ == NULL);
256 CHECK(java_lang_reflect_Method != NULL);
257 java_lang_reflect_Method_ = java_lang_reflect_Method;
258}
259
260void Method::ResetClass() {
261 CHECK(java_lang_reflect_Method_ != NULL);
262 java_lang_reflect_Method_ = NULL;
263}
264
265ObjectArray<String>* Method::GetDexCacheStrings() const {
266 return GetFieldObject<ObjectArray<String>*>(
267 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
268}
269
270void Method::SetReturnTypeIdx(uint32_t new_return_type_idx) {
271 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
272 new_return_type_idx, false);
273}
274
275Class* Method::GetReturnType() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700276 DCHECK(GetDeclaringClass()->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 // Short-cut
278 Class* result = GetDexCacheResolvedTypes()->Get(GetReturnTypeIdx());
279 if (result == NULL) {
280 // Do full linkage and set cache value for next call
281 result = Runtime::Current()->GetClassLinker()->ResolveType(GetReturnTypeIdx(), this);
282 }
283 CHECK(result != NULL);
284 return result;
285}
286
287void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
288 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
289 new_dex_cache_strings, false);
290}
291
292ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
293 return GetFieldObject<ObjectArray<Class>*>(
294 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
295}
296
297void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
298 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
299 new_dex_cache_classes, false);
300}
301
302ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
303 return GetFieldObject<ObjectArray<Method>*>(
304 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
305}
306
307void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
308 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
309 new_dex_cache_methods, false);
310}
311
312ObjectArray<Field>* Method::GetDexCacheResolvedFields() const {
313 return GetFieldObject<ObjectArray<Field>*>(
314 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false);
315}
316
317void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) {
318 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_),
319 new_dex_cache_fields, false);
320}
321
322CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
323 return GetFieldPtr<CodeAndDirectMethods*>(
324 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
325 false);
326}
327
328void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
329 SetFieldPtr<CodeAndDirectMethods*>(
330 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
331 new_value, false);
332}
333
334ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
335 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
336 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
337 false);
338}
339
340void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
341 SetFieldObject(
342 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
343 new_value, false);
344
345}
346
347size_t Method::NumArgRegisters(const StringPiece& shorty) {
348 CHECK_LE(1, shorty.length());
349 uint32_t num_registers = 0;
350 for (int i = 1; i < shorty.length(); ++i) {
351 char ch = shorty[i];
352 if (ch == 'D' || ch == 'J') {
353 num_registers += 2;
354 } else {
355 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700356 }
357 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358 return num_registers;
359}
360
361size_t Method::NumArgArrayBytes() const {
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700362 String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363 size_t num_bytes = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700364 for (int i = 1; i < shorty->GetLength(); ++i) {
365 char ch = shorty->CharAt(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700366 if (ch == 'D' || ch == 'J') {
367 num_bytes += 8;
368 } else if (ch == 'L') {
369 // Argument is a reference or an array. The shorty descriptor
370 // does not distinguish between these types.
371 num_bytes += sizeof(Object*);
372 } else {
373 num_bytes += 4;
374 }
375 }
376 return num_bytes;
377}
378
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700379size_t Method::NumArgs() const {
380 // "1 +" because the first in Args is the receiver.
381 // "- 1" because we don't count the return type.
382 return (IsStatic() ? 0 : 1) + GetShorty()->GetLength() - 1;
383}
384
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700385// The number of reference arguments to this method including implicit this
386// pointer
387size_t Method::NumReferenceArgs() const {
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700388 String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389 size_t result = IsStatic() ? 0 : 1; // The implicit this pointer.
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700390 for (int i = 1; i < shorty->GetLength(); i++) {
391 char ch = shorty->CharAt(i);
392 if ((ch == 'L') || (ch == '[')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700393 result++;
394 }
395 }
396 return result;
397}
398
399// The number of long or double arguments
400size_t Method::NumLongOrDoubleArgs() const {
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700401 String* shorty = GetShorty();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700402 size_t result = 0;
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700403 for (int i = 1; i < shorty->GetLength(); i++) {
404 char ch = shorty->CharAt(i);
405 if ((ch == 'D') || (ch == 'J')) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700406 result++;
407 }
408 }
409 return result;
410}
411
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700412// Is the given method parameter a reference?
413bool Method::IsParamAReference(unsigned int param) const {
414 CHECK_LT(param, NumArgs());
415 if (IsStatic()) {
416 param++; // 0th argument must skip return value at start of the shorty
417 } else if (param == 0) {
418 return true; // this argument
419 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700420 return GetShorty()->CharAt(param) == 'L';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421}
422
423// Is the given method parameter a long or double?
424bool Method::IsParamALongOrDouble(unsigned int param) const {
425 CHECK_LT(param, NumArgs());
426 if (IsStatic()) {
427 param++; // 0th argument must skip return value at start of the shorty
428 } else if (param == 0) {
429 return false; // this argument
430 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700431 char ch = GetShorty()->CharAt(param);
432 return (ch == 'J' || ch == 'D');
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700433}
434
435static size_t ShortyCharToSize(char x) {
436 switch (x) {
437 case 'V': return 0;
438 case '[': return kPointerSize;
439 case 'L': return kPointerSize;
440 case 'D': return 8;
441 case 'J': return 8;
442 default: return 4;
443 }
444}
445
446size_t Method::ParamSize(unsigned int param) const {
447 CHECK_LT(param, NumArgs());
448 if (IsStatic()) {
449 param++; // 0th argument must skip return value at start of the shorty
450 } else if (param == 0) {
451 return kPointerSize; // this argument
452 }
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700453 return ShortyCharToSize(GetShorty()->CharAt(param));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700454}
455
456size_t Method::ReturnSize() const {
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700457 return ShortyCharToSize(GetShorty()->CharAt(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700458}
459
460bool Method::HasSameNameAndDescriptor(const Method* that) const {
461 return (this->GetName()->Equals(that->GetName()) &&
462 this->GetSignature()->Equals(that->GetSignature()));
463}
464
buzbee4ef76522011-09-08 10:00:32 -0700465void Method::SetCode(ByteArray* code_array, InstructionSet instruction_set,
466 ByteArray* mapping_table) {
Elliott Hughes1240dad2011-09-09 16:24:50 -0700467 CHECK(GetCode() == NULL || IsNative());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700468 SetFieldPtr<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), code_array, false);
buzbee4ef76522011-09-08 10:00:32 -0700469 SetFieldPtr<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
470 mapping_table, false);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700471 int8_t* code = code_array->GetData();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700472 uintptr_t address = reinterpret_cast<uintptr_t>(code);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700473 if (instruction_set == kThumb2) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700474 // Set the low-order bit so a BLX will switch to Thumb mode
475 address |= 0x1;
476 }
477 SetFieldPtr<uintptr_t>(OFFSET_OF_OBJECT_MEMBER(Method, code_), address, false);
478}
479
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700480void Method::SetInvokeStub(const ByteArray* invoke_stub_array) {
481 const InvokeStub* invoke_stub = reinterpret_cast<InvokeStub*>(invoke_stub_array->GetData());
482 SetFieldPtr<const ByteArray*>(
483 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), invoke_stub_array, false);
484 SetFieldPtr<const InvokeStub*>(
485 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), invoke_stub, false);
486}
487
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700488void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
489 // Push a transition back into managed code onto the linked list in thread.
490 CHECK_EQ(Thread::kRunnable, self->GetState());
491 NativeToManagedRecord record;
492 self->PushNativeToManagedRecord(&record);
493
494 // Call the invoke stub associated with the method.
495 // Pass everything as arguments.
496 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700497
498 bool have_executable_code = (GetCode() != NULL);
499#if !defined(__arm__)
500 // Currently we can only compile for ARM, so we can't execute
501 // code on other architectures even if we do have it.
502 have_executable_code = false;
503#endif
504
505 if (have_executable_code && stub != NULL) {
506 LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700507 (*stub)(this, receiver, self, args, result);
508 } else {
509 LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this);
510 if (result != NULL) {
511 result->j = 0;
512 }
513 }
514
515 // Pop transition.
516 self->PopNativeToManagedRecord(record);
517}
518
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700519void Class::SetStatus(Status new_status) {
520 CHECK(new_status > GetStatus() || new_status == kStatusError ||
Elliott Hughesdcc24742011-09-07 14:02:44 -0700521 !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700522 CHECK(sizeof(Status) == sizeof(uint32_t));
523 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_),
524 new_status, false);
525}
526
527DexCache* Class::GetDexCache() const {
528 return GetFieldObject<DexCache*>(
529 OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
530}
531
532void Class::SetDexCache(DexCache* new_dex_cache) {
533 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_),
534 new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700535}
536
Brian Carlstrom1f870082011-08-23 16:02:11 -0700537Object* Class::AllocObjectFromCode(uint32_t type_idx, Method* method) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700538 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700539 if (klass == NULL) {
540 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
541 if (klass == NULL) {
542 UNIMPLEMENTED(FATAL) << "throw an error";
543 return NULL;
544 }
545 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700546 return klass->AllocObject();
547}
548
549Object* Class::AllocObject() {
550 DCHECK(!IsAbstract());
551 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700552}
553
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700554void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
555 if (new_reference_offsets != CLASS_WALK_SUPER) {
556 // Sanity check that the number of bits set in the reference offset bitmap
557 // agrees with the number of references
558 Class* cur = this;
559 size_t cnt = 0;
560 while (cur) {
561 cnt += cur->NumReferenceInstanceFieldsDuringLinking();
562 cur = cur->GetSuperClass();
563 }
564 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt);
565 }
566 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
567 new_reference_offsets, false);
568}
569
570void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
571 if (new_reference_offsets != CLASS_WALK_SUPER) {
572 // Sanity check that the number of bits set in the reference offset bitmap
573 // agrees with the number of references
574 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
575 NumReferenceStaticFieldsDuringLinking());
576 }
577 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
578 new_reference_offsets, false);
579}
580
581size_t Class::PrimitiveSize() const {
582 switch (GetPrimitiveType()) {
583 case kPrimBoolean:
584 case kPrimByte:
585 case kPrimChar:
586 case kPrimShort:
587 case kPrimInt:
588 case kPrimFloat:
589 return sizeof(int32_t);
590 case kPrimLong:
591 case kPrimDouble:
592 return sizeof(int64_t);
593 default:
594 LOG(FATAL) << "Primitive type size calculation on invalid type " << this;
595 return 0;
596 }
597}
598
599size_t Class::GetTypeSize(const String* descriptor) {
600 switch (descriptor->CharAt(0)) {
601 case 'B': return 1; // byte
602 case 'C': return 2; // char
603 case 'D': return 8; // double
604 case 'F': return 4; // float
605 case 'I': return 4; // int
606 case 'J': return 8; // long
607 case 'S': return 2; // short
608 case 'Z': return 1; // boolean
609 case 'L': return sizeof(Object*);
610 case '[': return sizeof(Array*);
611 default:
612 LOG(ERROR) << "Unknown type " << descriptor;
613 return 0;
614 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700615}
616
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700617bool Class::Implements(const Class* klass) const {
618 DCHECK(klass != NULL);
619 DCHECK(klass->IsInterface());
620 // All interfaces implemented directly and by our superclass, and
621 // recursively all super-interfaces of those interfaces, are listed
622 // in iftable_, so we can just do a linear scan through that.
623 for (size_t i = 0; i < iftable_count_; i++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700624 if (iftable_[i].GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700625 return true;
626 }
627 }
628 return false;
629}
630
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700631bool Class::CanPutArrayElement(const Class* object_class, const Class* array_class) {
632 if (object_class->IsArrayClass()) {
633 return array_class->IsArrayAssignableFromArray(object_class);
634 } else {
635 return array_class->GetComponentType()->IsAssignableFrom(object_class);
636 }
637}
638
639void Class::CanPutArrayElementFromCode(const Class* object_class, const Class* array_class) {
640 if (!CanPutArrayElement(object_class, array_class)) {
641 LOG(ERROR) << "Can't put a " << PrettyDescriptor(object_class->GetDescriptor())
642 << " into a " << PrettyDescriptor(array_class->GetDescriptor());
643 UNIMPLEMENTED(FATAL) << "need to throw ArrayStoreException and unwind stack";
644 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700645}
646
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700647// Determine whether "this" is assignable from "klazz", where both of these
648// are array classes.
649//
650// Consider an array class, e.g. Y[][], where Y is a subclass of X.
651// Y[][] = Y[][] --> true (identity)
652// X[][] = Y[][] --> true (element superclass)
653// Y = Y[][] --> false
654// Y[] = Y[][] --> false
655// Object = Y[][] --> true (everything is an object)
656// Object[] = Y[][] --> true
657// Object[][] = Y[][] --> true
658// Object[][][] = Y[][] --> false (too many []s)
659// Serializable = Y[][] --> true (all arrays are Serializable)
660// Serializable[] = Y[][] --> true
661// Serializable[][] = Y[][] --> false (unless Y is Serializable)
662//
663// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700664// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700665//
666bool Class::IsArrayAssignableFromArray(const Class* klass) const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700667 DCHECK(IsArrayClass());
668 DCHECK(klass->IsArrayClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700669 DCHECK_GT(GetArrayRank(), 0);
670 DCHECK_GT(klass->GetArrayRank(), 0);
671 DCHECK(GetComponentType() != NULL);
672 DCHECK(klass->GetComponentType() != NULL);
673 if (GetArrayRank() > klass->GetArrayRank()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700674 // Too many []s.
675 return false;
676 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700677 if (GetArrayRank() == klass->GetArrayRank()) {
678 return GetComponentType()->IsAssignableFrom(klass->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700679 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700680 DCHECK_LT(GetArrayRank(), klass->GetArrayRank());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700681 // The thing we might be assignable from has more dimensions. We
682 // must be an Object or array of Object, or a standard array
683 // interface or array of standard array interfaces (the standard
684 // interfaces being java/lang/Cloneable and java/io/Serializable).
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700685 if (GetComponentType()->IsInterface()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700686 // See if we implement our component type. We know the
687 // base element is an interface; if the array class implements
688 // it, we know it's a standard array interface.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700689 return Implements(GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700690 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700691 // See if this is an array of Object, Object[], etc.
692 return GetComponentType()->IsObjectClass();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700693}
694
695bool Class::IsAssignableFromArray(const Class* klass) const {
696 DCHECK(!IsInterface()); // handled first in IsAssignableFrom
Brian Carlstromb63ec392011-08-27 17:38:27 -0700697 DCHECK(klass->IsArrayClass());
698 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700699 // If "this" is not also an array, it must be Object.
700 // klass's super should be java_lang_Object, since it is an array.
701 Class* java_lang_Object = klass->GetSuperClass();
702 DCHECK(java_lang_Object != NULL);
703 DCHECK(java_lang_Object->GetSuperClass() == NULL);
704 return this == java_lang_Object;
705 }
706 return IsArrayAssignableFromArray(klass);
707}
708
709bool Class::IsSubClass(const Class* klass) const {
710 DCHECK(!IsInterface());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700711 DCHECK(!klass->IsArrayClass());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700712 const Class* current = this;
713 do {
714 if (current == klass) {
715 return true;
716 }
717 current = current->GetSuperClass();
718 } while (current != NULL);
719 return false;
720}
721
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700722bool Class::IsInSamePackage(const String* descriptor_string_1,
723 const String* descriptor_string_2) {
724 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
725 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
726
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700727 size_t i = 0;
728 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
729 ++i;
730 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700731 if (descriptor1.find('/', i) != StringPiece::npos ||
732 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700733 return false;
734 } else {
735 return true;
736 }
737}
738
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700739#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700740bool Class::IsInSamePackage(const StringPiece& descriptor1,
741 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700742 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700743 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700744 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
745 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700746 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
747}
748#endif
749
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700750bool Class::IsInSamePackage(const Class* that) const {
751 const Class* klass1 = this;
752 const Class* klass2 = that;
753 if (klass1 == klass2) {
754 return true;
755 }
756 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700757 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700758 return false;
759 }
760 // Arrays are in the same package when their element classes are.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700761 if (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700762 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700763 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700764 if (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700765 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700766 }
767 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700768 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700769}
770
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700771const ClassLoader* Class::GetClassLoader() const {
772 return GetFieldObject<const ClassLoader*>(
773 OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700774}
775
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700776void Class::SetClassLoader(const ClassLoader* new_cl) {
777 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
778 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_),
779 new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700780}
781
Brian Carlstrom30b94452011-08-25 21:35:26 -0700782Method* Class::FindVirtualMethodForInterface(Method* method) {
783 Class* declaring_class = method->GetDeclaringClass();
784 DCHECK(declaring_class->IsInterface());
785 // TODO cache to improve lookup speed
786 for (size_t i = 0; i < iftable_count_; i++) {
787 InterfaceEntry& interface_entry = iftable_[i];
788 if (interface_entry.GetInterface() == declaring_class) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700789 return GetVTable()->Get(
790 interface_entry.GetMethodIndexArray()[method->GetMethodIndex()]);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700791 }
792 }
793 UNIMPLEMENTED(FATAL) << "Need to throw an error of some kind";
794 return NULL;
795}
796
jeffhaobdb76512011-09-07 11:43:16 -0700797Method* Class::FindInterfaceMethod(const StringPiece& name,
798 const StringPiece& signature) {
799 // Check the current class before checking the interfaces.
800 Method* method = FindVirtualMethod(name, signature);
801 if (method != NULL) {
802 return method;
803 }
804
805 InterfaceEntry* iftable = GetIFTable();
806 for (size_t i = 0; i < GetIFTableCount(); i++) {
807 method = iftable[i].GetInterface()->FindVirtualMethod(name, signature);
808 if (method != NULL) {
809 return method;
810 }
811 }
812 return NULL;
813}
814
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700815Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700816 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700817 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700818 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700819 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700820 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700821 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700822 }
823 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700824 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700825}
826
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700827Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700828 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700829 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700830 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700831 if (method != NULL) {
832 return method;
833 }
834 }
835 return NULL;
836}
837
838Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700839 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700840 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700841 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700842 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700843 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700844 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700845 }
846 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700847 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700848}
849
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700850Method* Class::FindVirtualMethod(const StringPiece& name,
851 const StringPiece& descriptor) {
852 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
853 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
854 if (method != NULL) {
855 return method;
856 }
857 }
858 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700859}
860
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700861Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 // Is the field in this class?
863 // Interfaces are not relevant because they can't contain instance fields.
864 for (size_t i = 0; i < NumInstanceFields(); ++i) {
865 Field* f = GetInstanceField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700866 if (f->GetName()->Equals(name) && type == f->GetType()) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 return f;
868 }
869 }
870 return NULL;
871}
872
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700873Field* Class::FindInstanceField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 // Is the field in this class, or any of its superclasses?
875 // Interfaces are not relevant because they can't contain instance fields.
876 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700877 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 if (f != NULL) {
879 return f;
880 }
881 }
882 return NULL;
883}
884
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700885Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) {
886 DCHECK(type != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 for (size_t i = 0; i < NumStaticFields(); ++i) {
888 Field* f = GetStaticField(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700889 if (f->GetName()->Equals(name) && f->GetType() == type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 return f;
891 }
892 }
893 return NULL;
894}
895
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700896Field* Class::FindStaticField(const StringPiece& name, Class* type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 // Is the field in this class (or its interfaces), or any of its
898 // superclasses (or their interfaces)?
899 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
900 // Is the field in this class?
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700901 Field* f = c->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 if (f != NULL) {
903 return f;
904 }
905
906 // Is this field in any of this class' interfaces?
907 for (size_t i = 0; i < c->NumInterfaces(); ++i) {
908 Class* interface = c->GetInterface(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700909 f = interface->FindDeclaredStaticField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 if (f != NULL) {
911 return f;
912 }
913 }
914 }
915 return NULL;
916}
917
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700918Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700919 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700920 DCHECK_GE(component_count, 0);
921 DCHECK(array_class->IsArrayClass());
922 size_t size = SizeOf(component_count, component_size);
923 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
924 if (array != NULL) {
925 DCHECK(array->IsArrayInstance());
926 array->SetLength(component_count);
927 }
928 return array;
929}
930
931Array* Array::Alloc(Class* array_class, int32_t component_count) {
932 return Alloc(array_class, component_count, array_class->GetComponentSize());
933}
934
935Array* Array::AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) {
936 // TODO: throw on negative component_count
937 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
938 if (klass == NULL) {
939 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
940 if (klass == NULL || !klass->IsArrayClass()) {
941 UNIMPLEMENTED(FATAL) << "throw an error";
942 return NULL;
943 }
944 }
945 return Array::Alloc(klass, component_count);
946}
947
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700948template<typename T>
949PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700950 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700951 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
952 return down_cast<PrimitiveArray<T>*>(raw_array);
953}
954
955template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
956
957// Explicitly instantiate all the primitive array types.
958template class PrimitiveArray<uint8_t>; // BooleanArray
959template class PrimitiveArray<int8_t>; // ByteArray
960template class PrimitiveArray<uint16_t>; // CharArray
961template class PrimitiveArray<double>; // DoubleArray
962template class PrimitiveArray<float>; // FloatArray
963template class PrimitiveArray<int32_t>; // IntArray
964template class PrimitiveArray<int64_t>; // LongArray
965template class PrimitiveArray<int16_t>; // ShortArray
966
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700967// TODO: get global references for these
968Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700969
Brian Carlstroma663ea52011-08-19 23:33:41 -0700970void String::SetClass(Class* java_lang_String) {
971 CHECK(java_lang_String_ == NULL);
972 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700973 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700974}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700975
Brian Carlstroma663ea52011-08-19 23:33:41 -0700976void String::ResetClass() {
977 CHECK(java_lang_String_ != NULL);
978 java_lang_String_ = NULL;
979}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700980
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700981const String* String::Intern() const {
982 return Runtime::Current()->GetInternTable()->InternWeak(this);
983}
984
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700985int32_t String::GetHashCode() const {
986 int32_t result = GetField32(
987 OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
988 DCHECK(result != 0 ||
989 ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0);
990 return result;
991}
992
993int32_t String::GetLength() const {
994 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
995 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
996 return result;
997}
998
999uint16_t String::CharAt(int32_t index) const {
1000 // TODO: do we need this? Equals is the only caller, and could
1001 // bounds check itself.
1002 if (index < 0 || index >= count_) {
1003 Thread* self = Thread::Current();
1004 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1005 "length=%i; index=%i", count_, index);
1006 return 0;
1007 }
1008 return GetCharArray()->Get(index + GetOffset());
1009}
1010
1011String* String::AllocFromUtf16(int32_t utf16_length,
1012 const uint16_t* utf16_data_in,
1013 int32_t hash_code) {
1014 String* string = Alloc(GetJavaLangString(), utf16_length);
1015 // TODO: use 16-bit wide memset variant
1016 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
1017 for (int i = 0; i < utf16_length; i++) {
1018 array->Set(i, utf16_data_in[i]);
1019 }
1020 if (hash_code != 0) {
1021 string->SetHashCode(hash_code);
1022 } else {
1023 string->ComputeHashCode();
1024 }
1025 return string;
1026}
1027
1028String* String::AllocFromModifiedUtf8(const char* utf) {
1029 size_t char_count = CountModifiedUtf8Chars(utf);
1030 return AllocFromModifiedUtf8(char_count, utf);
1031}
1032
1033String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1034 const char* utf8_data_in) {
1035 String* string = Alloc(GetJavaLangString(), utf16_length);
1036 uint16_t* utf16_data_out =
1037 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1038 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1039 string->ComputeHashCode();
1040 return string;
1041}
1042
1043String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
1044 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1045}
1046
1047String* String::Alloc(Class* java_lang_String, CharArray* array) {
1048 String* string = down_cast<String*>(java_lang_String->AllocObject());
1049 string->SetArray(array);
1050 string->SetCount(array->GetLength());
1051 return string;
1052}
1053
1054bool String::Equals(const String* that) const {
1055 if (this == that) {
1056 // Quick reference equality test
1057 return true;
1058 } else if (that == NULL) {
1059 // Null isn't an instanceof anything
1060 return false;
1061 } else if (this->GetLength() != that->GetLength()) {
1062 // Quick length inequality test
1063 return false;
1064 } else {
1065 // NB don't short circuit on hash code as we're presumably here as the
1066 // hash code was already equal
1067 for (int32_t i = 0; i < that->GetLength(); ++i) {
1068 if (this->CharAt(i) != that->CharAt(i)) {
1069 return false;
1070 }
1071 }
1072 return true;
1073 }
1074}
1075
1076bool String::Equals(const uint16_t* that_chars, int32_t that_offset,
1077 int32_t that_length) const {
1078 if (this->GetLength() != that_length) {
1079 return false;
1080 } else {
1081 for (int32_t i = 0; i < that_length; ++i) {
1082 if (this->CharAt(i) != that_chars[that_offset + i]) {
1083 return false;
1084 }
1085 }
1086 return true;
1087 }
1088}
1089
1090bool String::Equals(const char* modified_utf8) const {
1091 for (int32_t i = 0; i < GetLength(); ++i) {
1092 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1093 if (ch == '\0' || ch != CharAt(i)) {
1094 return false;
1095 }
1096 }
1097 return *modified_utf8 == '\0';
1098}
1099
1100bool String::Equals(const StringPiece& modified_utf8) const {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001101 // TODO: do not assume C-string representation. For now DCHECK.
1102 DCHECK_EQ(modified_utf8.data()[modified_utf8.size()], 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001103 return Equals(modified_utf8.data());
1104}
1105
1106// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1107std::string String::ToModifiedUtf8() const {
1108 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1109 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1110 std::string result(byte_count, char(0));
1111 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1112 return result;
1113}
1114
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001115Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1116
1117void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1118 CHECK(java_lang_StackTraceElement_ == NULL);
1119 CHECK(java_lang_StackTraceElement != NULL);
1120 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1121}
1122
1123void StackTraceElement::ResetClass() {
1124 CHECK(java_lang_StackTraceElement_ != NULL);
1125 java_lang_StackTraceElement_ = NULL;
1126}
1127
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001128StackTraceElement* StackTraceElement::Alloc(const String* declaring_class,
1129 const String* method_name,
1130 const String* file_name,
1131 int32_t line_number) {
1132 StackTraceElement* trace =
1133 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1134 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1135 const_cast<String*>(declaring_class), false);
1136 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1137 const_cast<String*>(method_name), false);
1138 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1139 const_cast<String*>(file_name), false);
1140 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1141 line_number, false);
1142 return trace;
1143}
1144
Elliott Hughes1f359b02011-07-17 14:27:17 -07001145static const char* kClassStatusNames[] = {
1146 "Error",
1147 "NotReady",
1148 "Idx",
1149 "Loaded",
1150 "Resolved",
1151 "Verifying",
1152 "Verified",
1153 "Initializing",
1154 "Initialized"
1155};
1156std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1157 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001158 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001159 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001160 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001161 }
1162 return os;
1163}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001164
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001165} // namespace art