blob: 57f216d461cbd2545d90cc6588593a57d215ae59 [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
2 * Copyright 2011 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "runtime_support.h"
18
Elliott Hughes6c8867d2011-10-03 16:34:05 -070019#include "dex_verifier.h"
20
Shih-wei Liao2d831012011-09-28 22:06:53 -070021namespace art {
22
23// Temporary debugging hook for compiler.
24extern void DebugMe(Method* method, uint32_t info) {
25 LOG(INFO) << "DebugMe";
26 if (method != NULL) {
27 LOG(INFO) << PrettyMethod(method);
28 }
29 LOG(INFO) << "Info: " << info;
30}
31
32// Return value helper for jobject return types
33extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
34 return thread->DecodeJObject(obj);
35}
36
37extern void* FindNativeMethod(Thread* thread) {
38 DCHECK(Thread::Current() == thread);
39
40 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
41 DCHECK(method != NULL);
42
43 // Lookup symbol address for method, on failure we'll return NULL with an
44 // exception set, otherwise we return the address of the method we found.
45 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
46 if (native_code == NULL) {
47 DCHECK(thread->IsExceptionPending());
48 return NULL;
49 } else {
50 // Register so that future calls don't come here
51 method->RegisterNative(native_code);
52 return native_code;
53 }
54}
55
56// Called by generated call to throw an exception
57extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
58 /*
59 * exception may be NULL, in which case this routine should
60 * throw NPE. NOTE: this is a convenience for generated code,
61 * which previously did the null check inline and constructed
62 * and threw a NPE if NULL. This routine responsible for setting
63 * exception_ in thread and delivering the exception.
64 */
65 // Place a special frame at the TOS that will save all callee saves
66 *sp = Runtime::Current()->GetCalleeSaveMethod();
67 thread->SetTopOfStack(sp, 0);
68 if (exception == NULL) {
69 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
70 } else {
71 thread->SetException(exception);
72 }
73 thread->DeliverException();
74}
75
76// Deliver an exception that's pending on thread helping set up a callee save frame on the way
77extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
78 *sp = Runtime::Current()->GetCalleeSaveMethod();
79 thread->SetTopOfStack(sp, 0);
80 thread->DeliverException();
81}
82
83// Called by generated call to throw a NPE exception
84extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
85 // Place a special frame at the TOS that will save all callee saves
86 *sp = Runtime::Current()->GetCalleeSaveMethod();
87 thread->SetTopOfStack(sp, 0);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070088 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -070089 thread->DeliverException();
90}
91
92// Called by generated call to throw an arithmetic divide by zero exception
93extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
94 // Place a special frame at the TOS that will save all callee saves
95 *sp = Runtime::Current()->GetCalleeSaveMethod();
96 thread->SetTopOfStack(sp, 0);
97 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
98 thread->DeliverException();
99}
100
101// Called by generated call to throw an arithmetic divide by zero exception
102extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
103 // Place a special frame at the TOS that will save all callee saves
104 *sp = Runtime::Current()->GetCalleeSaveMethod();
105 thread->SetTopOfStack(sp, 0);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700106 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", "length=%d; index=%d",
107 limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700108 thread->DeliverException();
109}
110
111// Called by the AbstractMethodError stub (not runtime support)
112extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
113 *sp = Runtime::Current()->GetCalleeSaveMethod();
114 thread->SetTopOfStack(sp, 0);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700115 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;", "abstract method \"%s\"",
116 PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700117 thread->DeliverException();
118}
119
120extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
121 // Place a special frame at the TOS that will save all callee saves
122 Runtime* runtime = Runtime::Current();
123 *sp = runtime->GetCalleeSaveMethod();
124 thread->SetTopOfStack(sp, 0);
125 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700126 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
127 "stack size %zdkb; default stack size: %zdkb",
128 thread->GetStackSize() / KB, runtime->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700129 thread->ResetDefaultStackEnd(); // Return to default stack size
130 thread->DeliverException();
131}
132
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700133std::string ClassNameFromIndex(Method* method, uint32_t ref, DexVerifier::VerifyErrorRefType ref_type, bool access) {
134 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
135 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
136
137 uint16_t type_idx = 0;
138 if (ref_type == DexVerifier::VERIFY_ERROR_REF_FIELD) {
139 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
140 type_idx = id.class_idx_;
141 } else if (ref_type == DexVerifier::VERIFY_ERROR_REF_METHOD) {
142 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
143 type_idx = id.class_idx_;
144 } else if (ref_type == DexVerifier::VERIFY_ERROR_REF_CLASS) {
145 type_idx = ref;
146 } else {
147 CHECK(false) << static_cast<int>(ref_type);
148 }
149
150 std::string class_name(PrettyDescriptor(dex_file.dexStringByTypeIdx(type_idx)));
151 if (!access) {
152 return class_name;
153 }
154
155 std::string result;
156 result += "tried to access class ";
157 result += class_name;
158 result += " from class ";
159 result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor());
160 return result;
161}
162
163std::string FieldNameFromIndex(const Method* method, uint32_t ref, DexVerifier::VerifyErrorRefType ref_type, bool access) {
164 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(DexVerifier::VERIFY_ERROR_REF_FIELD));
165
166 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
167 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
168
169 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
170 std::string class_name(PrettyDescriptor(dex_file.dexStringByTypeIdx(id.class_idx_)));
171 const char* field_name = dex_file.dexStringById(id.name_idx_);
172 if (!access) {
173 return class_name + "." + field_name;
174 }
175
176 std::string result;
177 result += "tried to access field ";
178 result += class_name + "." + field_name;
179 result += " from class ";
180 result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor());
181 return result;
182}
183
184std::string MethodNameFromIndex(const Method* method, uint32_t ref, DexVerifier::VerifyErrorRefType ref_type, bool access) {
185 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(DexVerifier::VERIFY_ERROR_REF_METHOD));
186
187 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
188 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
189
190 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
191 std::string class_name(PrettyDescriptor(dex_file.dexStringByTypeIdx(id.class_idx_)));
192 const char* method_name = dex_file.dexStringById(id.name_idx_);
193 if (!access) {
194 return class_name + "." + method_name;
195 }
196
197 std::string result;
198 result += "tried to access method ";
199 result += class_name + "." + method_name + ":" + dex_file.CreateMethodDescriptor(id.proto_idx_, NULL);
200 result += " from class ";
201 result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor());
202 return result;
203}
204
205extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700206 // Place a special frame at the TOS that will save all callee saves
207 Runtime* runtime = Runtime::Current();
208 *sp = runtime->GetCalleeSaveMethod();
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700209 self->SetTopOfStack(sp, 0);
210
211 Frame frame = self->GetTopOfStack();
212 frame.Next();
213 Method* method = frame.GetMethod();
214
215 DexVerifier::VerifyErrorRefType ref_type = static_cast<DexVerifier::VerifyErrorRefType>(kind >> kVerifyErrorRefTypeShift);
216
217 const char* exception_class = "Ljava/lang/VerifyError;";
218 std::string msg;
219
220 switch (static_cast<DexVerifier::VerifyError>(kind & ~(0xff << kVerifyErrorRefTypeShift))) {
221 case DexVerifier::VERIFY_ERROR_NO_CLASS:
222 exception_class = "Ljava/lang/NoClassDefFoundError;";
223 msg = ClassNameFromIndex(method, ref, ref_type, false);
224 break;
225 case DexVerifier::VERIFY_ERROR_NO_FIELD:
226 exception_class = "Ljava/lang/NoSuchFieldError;";
227 msg = FieldNameFromIndex(method, ref, ref_type, false);
228 break;
229 case DexVerifier::VERIFY_ERROR_NO_METHOD:
230 exception_class = "Ljava/lang/NoSuchMethodError;";
231 msg = MethodNameFromIndex(method, ref, ref_type, false);
232 break;
233 case DexVerifier::VERIFY_ERROR_ACCESS_CLASS:
234 exception_class = "Ljava/lang/IllegalAccessError;";
235 msg = ClassNameFromIndex(method, ref, ref_type, true);
236 break;
237 case DexVerifier::VERIFY_ERROR_ACCESS_FIELD:
238 exception_class = "Ljava/lang/IllegalAccessError;";
239 msg = FieldNameFromIndex(method, ref, ref_type, true);
240 break;
241 case DexVerifier::VERIFY_ERROR_ACCESS_METHOD:
242 exception_class = "Ljava/lang/IllegalAccessError;";
243 msg = MethodNameFromIndex(method, ref, ref_type, true);
244 break;
245 case DexVerifier::VERIFY_ERROR_CLASS_CHANGE:
246 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
247 msg = ClassNameFromIndex(method, ref, ref_type, false);
248 break;
249 case DexVerifier::VERIFY_ERROR_INSTANTIATION:
250 exception_class = "Ljava/lang/InstantiationError;";
251 msg = ClassNameFromIndex(method, ref, ref_type, false);
252 break;
253 case DexVerifier::VERIFY_ERROR_GENERIC:
254 // Generic VerifyError; use default exception, no message.
255 break;
256 case DexVerifier::VERIFY_ERROR_NONE:
257 CHECK(false);
258 break;
259 }
260
261 self->ThrowNewException(exception_class, msg.c_str());
262 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700263}
264
265extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
266 // Place a special frame at the TOS that will save all callee saves
267 Runtime* runtime = Runtime::Current();
268 *sp = runtime->GetCalleeSaveMethod();
269 thread->SetTopOfStack(sp, 0);
270 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700271 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700272 thread->DeliverException();
273}
274
275extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
276 // Place a special frame at the TOS that will save all callee saves
277 Runtime* runtime = Runtime::Current();
278 *sp = runtime->GetCalleeSaveMethod();
279 thread->SetTopOfStack(sp, 0);
280 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700281 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700282 thread->DeliverException();
283}
284
285extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* thread, Method** sp) {
286 // Place a special frame at the TOS that will save all callee saves
287 Runtime* runtime = Runtime::Current();
288 *sp = runtime->GetCalleeSaveMethod();
289 thread->SetTopOfStack(sp, 0);
290 LOG(WARNING) << "TODO: no such method exception detail message. method_idx=" << method_idx;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700291 thread->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;", "method_idx=%d", method_idx);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700292 thread->DeliverException();
293}
294
295extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
296 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
297 // Place a special frame at the TOS that will save all callee saves
298 Runtime* runtime = Runtime::Current();
299 *sp = runtime->GetCalleeSaveMethod();
300 thread->SetTopOfStack(sp, 0);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700301 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700302 thread->DeliverException();
303}
304
305// TODO: placeholder. Helper function to type
306Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) {
307 /*
308 * Should initialize & fix up method->dex_cache_resolved_types_[].
309 * Returns initialized type. Does not return normally if an exception
310 * is thrown, but instead initiates the catch. Should be similar to
311 * ClassLinker::InitializeStaticStorageFromCode.
312 */
313 UNIMPLEMENTED(FATAL);
314 return NULL;
315}
316
317// TODO: placeholder. Helper function to resolve virtual method
318void ResolveMethodFromCode(Method* method, uint32_t method_idx) {
319 /*
320 * Slow-path handler on invoke virtual method path in which
321 * base method is unresolved at compile-time. Doesn't need to
322 * return anything - just either ensure that
323 * method->dex_cache_resolved_methods_(method_idx) != NULL or
324 * throw and unwind. The caller will restart call sequence
325 * from the beginning.
326 */
327}
328
329// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
330// cannot be resolved, throw an error. If it can, use it to create an instance.
331extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method) {
332 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
333 if (klass == NULL) {
334 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
335 if (klass == NULL) {
336 DCHECK(Thread::Current()->IsExceptionPending());
337 return NULL; // Failure
338 }
339 }
340 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(klass, true)) {
341 DCHECK(Thread::Current()->IsExceptionPending());
342 return NULL; // Failure
343 }
344 return klass->AllocObject();
345}
346
347// Helper function to alloc array for OP_FILLED_NEW_ARRAY
348extern "C" Array* artCheckAndArrayAllocFromCode(uint32_t type_idx, Method* method,
349 int32_t component_count) {
350 if (component_count < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700351 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700352 component_count);
353 return NULL; // Failure
354 }
355 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
356 if (klass == NULL) { // Not in dex cache so try to resolve
357 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
358 if (klass == NULL) { // Error
359 DCHECK(Thread::Current()->IsExceptionPending());
360 return NULL; // Failure
361 }
362 }
363 if (klass->IsPrimitive() && !klass->IsPrimitiveInt()) {
364 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700365 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700366 "Bad filled array request for type %s",
367 PrettyDescriptor(klass->GetDescriptor()).c_str());
368 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700369 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700370 "Found type %s; filled-new-array not implemented for anything but \'int\'",
371 PrettyDescriptor(klass->GetDescriptor()).c_str());
372 }
373 return NULL; // Failure
374 } else {
375 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
376 return Array::Alloc(klass, component_count);
377 }
378}
379
380// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
381// it cannot be resolved, throw an error. If it can, use it to create an array.
382extern "C" Array* artArrayAllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) {
383 if (component_count < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700384 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700385 component_count);
386 return NULL; // Failure
387 }
388 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
389 if (klass == NULL) { // Not in dex cache so try to resolve
390 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
391 if (klass == NULL) { // Error
392 DCHECK(Thread::Current()->IsExceptionPending());
393 return NULL; // Failure
394 }
395 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
396 }
397 return Array::Alloc(klass, component_count);
398}
399
400// Check whether it is safe to cast one class to the other, throw exception and return -1 on failure
401extern "C" int artCheckCastFromCode(const Class* a, const Class* b) {
402 DCHECK(a->IsClass()) << PrettyClass(a);
403 DCHECK(b->IsClass()) << PrettyClass(b);
404 if (b->IsAssignableFrom(a)) {
405 return 0; // Success
406 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700407 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700408 "%s cannot be cast to %s",
409 PrettyDescriptor(a->GetDescriptor()).c_str(),
410 PrettyDescriptor(b->GetDescriptor()).c_str());
411 return -1; // Failure
412 }
413}
414
415// Tests whether 'element' can be assigned into an array of type 'array_class'.
416// Returns 0 on success and -1 if an exception is pending.
417extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class) {
418 DCHECK(array_class != NULL);
419 // element can't be NULL as we catch this is screened in runtime_support
420 Class* element_class = element->GetClass();
421 Class* component_type = array_class->GetComponentType();
422 if (component_type->IsAssignableFrom(element_class)) {
423 return 0; // Success
424 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700425 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700426 "Cannot store an object of type %s in to an array of type %s",
427 PrettyDescriptor(element_class->GetDescriptor()).c_str(),
428 PrettyDescriptor(array_class->GetDescriptor()).c_str());
429 return -1; // Failure
430 }
431}
432
433extern "C" int artUnlockObjectFromCode(Thread* thread, Object* obj) {
434 DCHECK(obj != NULL); // Assumed to have been checked before entry
435 return obj->MonitorExit(thread) ? 0 /* Success */ : -1 /* Failure */;
436}
437
438void LockObjectFromCode(Thread* thread, Object* obj) {
439 DCHECK(obj != NULL); // Assumed to have been checked before entry
440 obj->MonitorEnter(thread);
441 DCHECK(thread->HoldsLock(obj));
442 // Only possible exception is NPE and is handled before entry
443 DCHECK(!thread->IsExceptionPending());
444}
445
446extern "C" void artCheckSuspendFromCode(Thread* thread) {
447 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
448}
449
450/*
451 * Fill the array with predefined constant values, throwing exceptions if the array is null or
452 * not of sufficient length.
453 *
454 * NOTE: When dealing with a raw dex file, the data to be copied uses
455 * little-endian ordering. Require that oat2dex do any required swapping
456 * so this routine can get by with a memcpy().
457 *
458 * Format of the data:
459 * ushort ident = 0x0300 magic value
460 * ushort width width of each element in the table
461 * uint size number of elements in the table
462 * ubyte data[size*width] table of data values (may contain a single-byte
463 * padding at the end)
464 */
465extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table) {
466 DCHECK_EQ(table[0], 0x0300);
467 if (array == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700468 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
469 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700470 return -1; // Error
471 }
472 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
473 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
474 if (static_cast<int32_t>(size) > array->GetLength()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700475 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
476 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700477 return -1; // Error
478 }
479 uint16_t width = table[1];
480 uint32_t size_in_bytes = size * width;
481 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
482 return 0; // Success
483}
484
485// See comments in runtime_support_asm.S
486extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx,
487 Object* this_object ,
488 Method* caller_method) {
489 Thread* thread = Thread::Current();
490 if (this_object == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700491 thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
492 "null receiver during interface dispatch");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700493 return 0;
494 }
495 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
496 Method* interface_method = class_linker->ResolveMethod(method_idx, caller_method, false);
497 if (interface_method == NULL) {
498 // Could not resolve interface method. Throw error and unwind
499 CHECK(thread->IsExceptionPending());
500 return 0;
501 }
502 Method* method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
503 if (method == NULL) {
504 CHECK(thread->IsExceptionPending());
505 return 0;
506 }
507 const void* code = method->GetCode();
508
509 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
510 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
511 uint64_t result = ((code_uint << 32) | method_uint);
512 return result;
513}
514
515/*
516 * Float/double conversion requires clamping to min and max of integer form. If
517 * target doesn't support this normally, use these.
518 */
519int64_t D2L(double d) {
520 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
521 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
522 if (d >= kMaxLong)
523 return (int64_t)0x7fffffffffffffffULL;
524 else if (d <= kMinLong)
525 return (int64_t)0x8000000000000000ULL;
526 else if (d != d) // NaN case
527 return 0;
528 else
529 return (int64_t)d;
530}
531
532int64_t F2L(float f) {
533 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
534 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
535 if (f >= kMaxLong)
536 return (int64_t)0x7fffffffffffffffULL;
537 else if (f <= kMinLong)
538 return (int64_t)0x8000000000000000ULL;
539 else if (f != f) // NaN case
540 return 0;
541 else
542 return (int64_t)f;
543}
544
545} // namespace art