blob: 9854151b9deaceb051fe356d603c94e0e72d4933 [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 Hughes91bf6cd2012-02-14 17:27:48 -080019#include "debugger.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070020#include "dex_cache.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070021#include "dex_verifier.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070022#include "macros.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080023#include "object.h"
24#include "object_utils.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070025#include "reflection.h"
Shih-wei Liaob0ee9d72012-03-07 16:39:26 -080026#include "runtime_support_common.h"
jeffhaoe343b762011-12-05 16:36:44 -080027#include "trace.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070028#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070029
Shih-wei Liao2d831012011-09-28 22:06:53 -070030namespace art {
31
buzbee44b412b2012-02-04 08:50:53 -080032/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080033 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -080034 * the method. However, because the offset alone cannot distinguish between
35 * method entry and offset 0 within the method, we'll use an offset of -1
36 * to denote method entry.
37 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080038extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -080039 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080040 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -080041}
42
Shih-wei Liao2d831012011-09-28 22:06:53 -070043// Temporary debugging hook for compiler.
44extern void DebugMe(Method* method, uint32_t info) {
45 LOG(INFO) << "DebugMe";
46 if (method != NULL) {
47 LOG(INFO) << PrettyMethod(method);
48 }
49 LOG(INFO) << "Info: " << info;
50}
51
52// Return value helper for jobject return types
53extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -070054 if (thread->IsExceptionPending()) {
55 return NULL;
56 }
Shih-wei Liao2d831012011-09-28 22:06:53 -070057 return thread->DecodeJObject(obj);
58}
59
Ian Rogers60db5ab2012-02-20 17:02:00 -080060extern void* FindNativeMethod(Thread* self) {
61 DCHECK(Thread::Current() == self);
Shih-wei Liao2d831012011-09-28 22:06:53 -070062
Ian Rogers60db5ab2012-02-20 17:02:00 -080063 Method* method = const_cast<Method*>(self->GetCurrentMethod());
Shih-wei Liao2d831012011-09-28 22:06:53 -070064 DCHECK(method != NULL);
65
66 // Lookup symbol address for method, on failure we'll return NULL with an
67 // exception set, otherwise we return the address of the method we found.
Ian Rogers60db5ab2012-02-20 17:02:00 -080068 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -070069 if (native_code == NULL) {
Ian Rogers60db5ab2012-02-20 17:02:00 -080070 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -070071 return NULL;
72 } else {
73 // Register so that future calls don't come here
Ian Rogers60db5ab2012-02-20 17:02:00 -080074 method->RegisterNative(self, native_code);
Shih-wei Liao2d831012011-09-28 22:06:53 -070075 return native_code;
76 }
77}
78
79// Called by generated call to throw an exception
80extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
81 /*
82 * exception may be NULL, in which case this routine should
83 * throw NPE. NOTE: this is a convenience for generated code,
84 * which previously did the null check inline and constructed
85 * and threw a NPE if NULL. This routine responsible for setting
86 * exception_ in thread and delivering the exception.
87 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -070088 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -070089 if (exception == NULL) {
90 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
91 } else {
92 thread->SetException(exception);
93 }
94 thread->DeliverException();
95}
96
97// Deliver an exception that's pending on thread helping set up a callee save frame on the way
98extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070099 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700100 thread->DeliverException();
101}
102
103// Called by generated call to throw a NPE exception
104extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700105 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700106 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700107 thread->DeliverException();
108}
109
110// Called by generated call to throw an arithmetic divide by zero exception
111extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700112 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700113 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
114 thread->DeliverException();
115}
116
117// Called by generated call to throw an arithmetic divide by zero exception
118extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700119 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
120 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
121 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700122 thread->DeliverException();
123}
124
125// Called by the AbstractMethodError stub (not runtime support)
126extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700127 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
128 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
129 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700130 thread->DeliverException();
131}
132
133extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700134 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800135 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800136 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800137 artTraceMethodUnwindFromCode(thread);
138 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700139 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700140 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
141 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700142 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700143 thread->ResetDefaultStackEnd(); // Return to default stack size
144 thread->DeliverException();
145}
146
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700147static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700148 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700149 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
150 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
151
152 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700153 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700154 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
155 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700156 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700157 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
158 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700159 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700160 type_idx = ref;
161 } else {
162 CHECK(false) << static_cast<int>(ref_type);
163 }
164
Ian Rogers0571d352011-11-03 19:51:38 -0700165 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700166 if (!access) {
167 return class_name;
168 }
169
170 std::string result;
171 result += "tried to access class ";
172 result += class_name;
173 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800174 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700175 return result;
176}
177
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700178static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700179 verifier::VerifyErrorRefType ref_type, bool access) {
180 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700181
182 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
183 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
184
185 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700186 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700187 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700188 if (!access) {
189 return class_name + "." + field_name;
190 }
191
192 std::string result;
193 result += "tried to access field ";
194 result += class_name + "." + field_name;
195 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800196 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700197 return result;
198}
199
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700200static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800201 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700202 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700203
204 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
205 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
206
207 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700208 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700209 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700210 if (!access) {
211 return class_name + "." + method_name;
212 }
213
214 std::string result;
215 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700216 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700217 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700218 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800219 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700220 return result;
221}
222
223extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700224 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
225 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700226 frame.Next();
227 Method* method = frame.GetMethod();
228
Ian Rogersd81871c2011-10-03 13:57:23 -0700229 verifier::VerifyErrorRefType ref_type =
230 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700231
232 const char* exception_class = "Ljava/lang/VerifyError;";
233 std::string msg;
234
Ian Rogersd81871c2011-10-03 13:57:23 -0700235 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
236 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700237 exception_class = "Ljava/lang/NoClassDefFoundError;";
238 msg = ClassNameFromIndex(method, ref, ref_type, false);
239 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700240 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700241 exception_class = "Ljava/lang/NoSuchFieldError;";
242 msg = FieldNameFromIndex(method, ref, ref_type, false);
243 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700244 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700245 exception_class = "Ljava/lang/NoSuchMethodError;";
246 msg = MethodNameFromIndex(method, ref, ref_type, false);
247 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700248 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700249 exception_class = "Ljava/lang/IllegalAccessError;";
250 msg = ClassNameFromIndex(method, ref, ref_type, true);
251 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700252 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700253 exception_class = "Ljava/lang/IllegalAccessError;";
254 msg = FieldNameFromIndex(method, ref, ref_type, true);
255 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700256 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700257 exception_class = "Ljava/lang/IllegalAccessError;";
258 msg = MethodNameFromIndex(method, ref, ref_type, true);
259 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700260 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700261 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
262 msg = ClassNameFromIndex(method, ref, ref_type, false);
263 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700264 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700265 exception_class = "Ljava/lang/InstantiationError;";
266 msg = ClassNameFromIndex(method, ref, ref_type, false);
267 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700268 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700269 // Generic VerifyError; use default exception, no message.
270 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700271 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700272 CHECK(false);
273 break;
274 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700275 self->ThrowNewException(exception_class, msg.c_str());
276 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700277}
278
279extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700280 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700281 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700282 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700283 thread->DeliverException();
284}
285
286extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700287 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700288 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700289 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700290 thread->DeliverException();
291}
292
Elliott Hughese1410a22011-10-04 12:10:24 -0700293extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700294 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
295 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700296 frame.Next();
297 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700298 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700299 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700300 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700301}
302
303extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700304 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700305 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700306 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700307 thread->DeliverException();
308}
309
Ian Rogers19846512012-02-24 11:42:47 -0800310const void* UnresolvedDirectMethodTrampolineFromCode(Method* called, Method** sp, Thread* thread,
311 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700312 // TODO: this code is specific to ARM
313 // On entry the stack pointed by sp is:
314 // | argN | |
315 // | ... | |
316 // | arg4 | |
317 // | arg3 spill | | Caller's frame
318 // | arg2 spill | |
319 // | arg1 spill | |
320 // | Method* | ---
321 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700322 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700323 // | R3 | arg3
324 // | R2 | arg2
325 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700326 // | R0 |
327 // | Method* | <- sp
328 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
329 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
330 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
331 uintptr_t caller_pc = regs[10];
332 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700333 // Start new JNI local reference state
334 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700335 ScopedJniEnvLocalRefState env_state(env);
Ian Rogers19846512012-02-24 11:42:47 -0800336
337 // Compute details about the called method (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700338 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers19846512012-02-24 11:42:47 -0800339 Method* caller = *caller_sp;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700340 bool is_static;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800341 bool is_virtual;
Ian Rogers19846512012-02-24 11:42:47 -0800342 uint32_t dex_method_idx;
343 const char* shorty;
344 uint32_t shorty_len;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700345 if (type == Runtime::kUnknownMethod) {
Ian Rogers19846512012-02-24 11:42:47 -0800346 DCHECK(called->IsRuntimeMethod());
Ian Rogersdf9a7822011-10-11 16:53:22 -0700347 // less two as return address may span into next dex instruction
348 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800349 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700350 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
351 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700352 Instruction::Code instr_code = instr->Opcode();
353 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
354 (instr_code == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800355 is_virtual = (instr_code == Instruction::INVOKE_VIRTUAL) ||
356 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700357 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
Ian Rogersfb6adba2012-03-04 21:51:51 -0800358 (instr_code == Instruction::INVOKE_DIRECT_RANGE) ||
359 (instr_code == Instruction::INVOKE_VIRTUAL) ||
360 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE));
Elliott Hughesadb8c672012-03-06 16:49:32 -0800361 DecodedInstruction dec_insn(instr);
362 dex_method_idx = dec_insn.vB;
Ian Rogers19846512012-02-24 11:42:47 -0800363 shorty = linker->MethodShorty(dex_method_idx, caller, &shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700364 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800365 DCHECK(!called->IsRuntimeMethod());
Ian Rogersea2a11d2011-10-11 16:48:51 -0700366 is_static = type == Runtime::kStaticMethod;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800367 is_virtual = false;
Ian Rogers19846512012-02-24 11:42:47 -0800368 dex_method_idx = called->GetDexMethodIndex();
369 MethodHelper mh(called);
370 shorty = mh.GetShorty();
371 shorty_len = mh.GetShortyLength();
372 }
373 // Discover shorty (avoid GCs)
374 size_t args_in_regs = 0;
375 for (size_t i = 1; i < shorty_len; i++) {
376 char c = shorty[i];
377 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
378 if (args_in_regs > 3) {
379 args_in_regs = 3;
380 break;
381 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700382 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700383 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700384 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700385 if (!is_static) {
386 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
387 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700388 if (args_in_regs < 3) {
389 // If we thought we had fewer than 3 arguments in registers, account for the receiver
390 args_in_regs++;
391 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700392 AddLocalReference<jobject>(env, obj);
393 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700394 size_t shorty_index = 1; // skip return value
395 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
396 // R0)
397 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
398 char c = shorty[shorty_index];
399 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700400 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700401 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
402 AddLocalReference<jobject>(env, obj);
403 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700404 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
405 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700406 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700407 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700408 while (shorty_index < shorty_len) {
409 char c = shorty[shorty_index];
410 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700411 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700412 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700413 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700414 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700415 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700416 }
417 // Resolve method filling in dex cache
Ian Rogers19846512012-02-24 11:42:47 -0800418 if (type == Runtime::kUnknownMethod) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800419 called = linker->ResolveMethod(dex_method_idx, caller, !is_virtual);
Ian Rogers19846512012-02-24 11:42:47 -0800420 }
421 const void* code = NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700422 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800423 if (LIKELY(called->IsDirect() == !is_virtual)) {
Ian Rogers19846512012-02-24 11:42:47 -0800424 // Ensure that the called method's class is initialized.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800425 Class* called_class = called->GetDeclaringClass();
426 linker->EnsureInitialized(called_class, true);
427 if (LIKELY(called_class->IsInitialized())) {
Ian Rogers19846512012-02-24 11:42:47 -0800428 code = called->GetCode();
429 } else if (called_class->IsInitializing()) {
430 // Class is still initializing, go to oat and grab code (trampoline must be left in place
431 // until class is initialized to stop races between threads).
432 code = linker->GetOatCodeFor(called);
433 } else {
434 DCHECK(called_class->IsErroneous());
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800435 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800436 } else {
437 // Direct method has been made virtual
438 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
439 "Expected direct method but found virtual: %s",
440 PrettyMethod(called, true).c_str());
441 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700442 }
Ian Rogers19846512012-02-24 11:42:47 -0800443 if (UNLIKELY(code == NULL)) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700444 // Something went wrong in ResolveMethod or EnsureInitialized,
445 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700446 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700447 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700448 thread->ClearException();
449 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800450 // Expect class to at least be initializing.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800451 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogers19846512012-02-24 11:42:47 -0800452 // Don't want infinite recursion.
453 DCHECK(code != Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod)->GetData());
Ian Rogersad25ac52011-10-04 19:13:33 -0700454 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700455 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700456 }
457 return code;
458}
459
Ian Rogers60db5ab2012-02-20 17:02:00 -0800460static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
461 intptr_t value = *arg_ptr;
462 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
463 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
464 CHECK(Heap::IsHeapAddress(value_as_work_around_rep));
465 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
466}
467
468extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
469 DCHECK(Thread::Current() == self);
470 // TODO: this code is specific to ARM
471 // On entry the stack pointed by sp is:
472 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
473 // | LR |
474 // | R3 | arg2
475 // | R2 | arg1
476 // | R1 | jclass/jobject
477 // | R0 | JNIEnv
478 // | unused |
479 // | unused |
480 // | unused | <- sp
481 Method* jni_method = self->GetTopOfStack().GetMethod();
482 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
483 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
484 // Fix up this/jclass argument
485 WorkAroundJniBugsForJobject(arg_ptr);
486 arg_ptr++;
487 // Fix up jobject arguments
488 MethodHelper mh(jni_method);
489 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800490 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800491 char shorty_char = mh.GetShorty()[i];
492 if (shorty_char == 'L') {
493 WorkAroundJniBugsForJobject(arg_ptr);
494 }
495 if (shorty_char == 'J' || shorty_char == 'D') {
496 if (reg_num == 2) {
497 arg_ptr = sp + 8; // skip to out arguments
498 reg_num = -1;
499 } else if (reg_num == 3) {
500 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
501 reg_num = -1;
502 } else {
503 DCHECK(reg_num == -1);
504 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
505 arg_ptr += 3; // unaligned, pad and move through stack arguments
506 } else {
507 arg_ptr += 2; // aligned, move through stack arguments
508 }
509 }
510 } else {
511 if (reg_num == 2) {
512 arg_ptr++; // move through register arguments
513 reg_num++;
514 } else if (reg_num == 3) {
515 arg_ptr = sp + 8; // skip to outgoing stack arguments
516 reg_num = -1;
517 } else {
518 DCHECK(reg_num == -1);
519 arg_ptr++; // move through stack arguments
520 }
521 }
522 }
523 // Load expected destination, see Method::RegisterNative
Ian Rogers19846512012-02-24 11:42:47 -0800524 const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
525 if (UNLIKELY(code == NULL)) {
526 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
527 jni_method->RegisterNative(self, code);
528 }
529 return code;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800530}
531
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800532extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
533 Thread* self, Method** sp) {
534 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
535 return AllocObjectFromCode(type_idx, method, self, false);
536}
537
Ian Rogers28ad40d2011-10-27 15:19:26 -0700538extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
539 Thread* self, Method** sp) {
540 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800541 return AllocObjectFromCode(type_idx, method, self, true);
542}
543
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800544extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
545 Thread* self, Method** sp) {
546 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
547 return AllocArrayFromCode(type_idx, method, component_count, self, false);
548}
549
550extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
551 int32_t component_count,
552 Thread* self, Method** sp) {
553 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
554 return AllocArrayFromCode(type_idx, method, component_count, self, true);
555}
556
Ian Rogersce9eca62011-10-07 17:11:03 -0700557extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
558 int32_t component_count, Thread* self, Method** sp) {
559 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800560 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700561}
562
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800563extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
564 int32_t component_count,
565 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700566 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800567 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700568}
569
Ian Rogerscaab8c42011-10-12 12:11:18 -0700570// Assignable test for code, won't throw. Null and equality tests already performed
571uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
572 DCHECK(klass != NULL);
573 DCHECK(ref_class != NULL);
574 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
575}
576
Shih-wei Liao2d831012011-09-28 22:06:53 -0700577// Check whether it is safe to cast one class to the other, throw exception and return -1 on failure
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700578extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700579 DCHECK(a->IsClass()) << PrettyClass(a);
580 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700581 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700582 return 0; // Success
583 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700584 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700585 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700586 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800587 PrettyDescriptor(a).c_str(),
588 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700589 return -1; // Failure
590 }
591}
592
593// Tests whether 'element' can be assigned into an array of type 'array_class'.
594// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700595extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
596 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700597 DCHECK(array_class != NULL);
598 // element can't be NULL as we catch this is screened in runtime_support
599 Class* element_class = element->GetClass();
600 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700601 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700602 return 0; // Success
603 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700604 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700605 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800606 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800607 PrettyDescriptor(element_class).c_str(),
608 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700609 return -1; // Failure
610 }
611}
612
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700613extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
614 Thread* self, Method** sp) {
615 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800616 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700617}
618
Ian Rogers28ad40d2011-10-27 15:19:26 -0700619extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
620 Method** sp) {
621 // Called when method->dex_cache_resolved_types_[] misses
622 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800623 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700624}
625
Ian Rogersb093c6b2011-10-31 16:19:55 -0700626extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
627 const Method* referrer, Thread* self,
628 Method** sp) {
629 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
630 // unpopulated
631 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800632 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700633}
634
Brian Carlstromaded5f72011-10-07 17:15:04 -0700635extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
636 Thread* self, Method** sp) {
637 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
638 return ResolveStringFromCode(referrer, string_idx);
639}
640
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700641extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
642 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700643 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700644 // MonitorExit may throw exception
645 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
646}
647
648extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
649 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
650 DCHECK(obj != NULL); // Assumed to have been checked before entry
651 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700652 DCHECK(thread->HoldsLock(obj));
653 // Only possible exception is NPE and is handled before entry
654 DCHECK(!thread->IsExceptionPending());
655}
656
Ian Rogers4a510d82011-10-09 14:30:24 -0700657void CheckSuspendFromCode(Thread* thread) {
658 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700659 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
660}
661
Ian Rogers4a510d82011-10-09 14:30:24 -0700662extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
663 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700664 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700665 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
666}
667
668/*
669 * Fill the array with predefined constant values, throwing exceptions if the array is null or
670 * not of sufficient length.
671 *
672 * NOTE: When dealing with a raw dex file, the data to be copied uses
673 * little-endian ordering. Require that oat2dex do any required swapping
674 * so this routine can get by with a memcpy().
675 *
676 * Format of the data:
677 * ushort ident = 0x0300 magic value
678 * ushort width width of each element in the table
679 * uint size number of elements in the table
680 * ubyte data[size*width] table of data values (may contain a single-byte
681 * padding at the end)
682 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700683extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
684 Thread* self, Method** sp) {
685 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700686 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700687 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700688 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
689 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700690 return -1; // Error
691 }
692 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
693 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700694 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700695 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
696 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700697 return -1; // Error
698 }
699 uint16_t width = table[1];
700 uint32_t size_in_bytes = size * width;
Ian Rogersa15e67d2012-02-28 13:51:55 -0800701 memcpy((char*)array + Array::DataOffset(width).Int32Value(), (char*)&table[4], size_in_bytes);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700702 return 0; // Success
703}
704
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800705// See comments in runtime_support_asm.S
706extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
707 Method* caller_method, Thread* self,
708 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800709 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800710}
711
712extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
713 Object* this_object,
714 Method* caller_method, Thread* self,
715 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800716 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
717}
718
719
720extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
721 Object* this_object,
722 Method* caller_method, Thread* self,
723 Method** sp) {
724 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
725}
726
727extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
728 Object* this_object,
729 Method* caller_method, Thread* self,
730 Method** sp) {
731 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800732}
733
734extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
735 Object* this_object,
736 Method* caller_method, Thread* self,
737 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800738 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800739}
740
741extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
742 Object* this_object,
743 Method* caller_method, Thread* self,
744 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800745 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800746}
747
Ian Rogers466bb252011-10-14 03:29:56 -0700748static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
749 ScopedLocalRef<jclass> jlr_UTE_class(env,
750 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
751 if (jlr_UTE_class.get() == NULL) {
752 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
753 } else {
754 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
755 "(Ljava/lang/Throwable;)V");
756 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
757 ScopedLocalRef<jthrowable> jlr_UTE(env,
758 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
759 jexception)));
760 int rc = env->Throw(jlr_UTE.get());
761 if (rc != JNI_OK) {
762 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
763 }
764 }
765 CHECK(self->IsExceptionPending());
766}
767
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700768// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
769// which is responsible for recording callee save registers. We explicitly handlerize incoming
770// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
771// the invocation handler which is a field within the proxy object receiver.
772extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -0700773 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700774 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -0700775 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
776 DCHECK_EQ(*proxy_sp, proxy_method);
777 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700778 // TODO: ARM specific
779 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
780 // Start new JNI local reference state
781 JNIEnvExt* env = self->GetJniEnv();
782 ScopedJniEnvLocalRefState env_state(env);
783 // Create local ref. copies of proxy method and the receiver
784 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
785 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
786
Ian Rogers14b1b242011-10-11 18:54:34 -0700787 // Placing into local references incoming arguments from the caller's register arguments,
788 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800789 MethodHelper proxy_mh(proxy_method);
790 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -0700791 size_t args_in_regs = 0;
792 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800793 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -0700794 if (args_in_regs > 2) {
795 args_in_regs = 2;
796 break;
797 }
798 }
799 size_t cur_arg = 0; // current stack location to read
800 size_t param_index = 1; // skip receiver
801 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800802 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -0700803 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700804 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -0700805 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700806 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800807 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -0700808 param_index++;
809 }
810 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -0700811 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -0700812 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800813 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -0700814 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
815 jobject jobj = AddLocalReference<jobject>(env, obj);
816 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700817 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800818 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -0700819 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700820 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700821 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
822 jvalue args_jobj[3];
823 args_jobj[0].l = rcvr_jobj;
824 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -0700825 // Args array, if no arguments then NULL (don't include receiver in argument count)
826 args_jobj[2].l = NULL;
827 ObjectArray<Object>* args = NULL;
828 if ((num_params - 1) > 0) {
829 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700830 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -0700831 CHECK(self->IsExceptionPending());
832 return;
833 }
834 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
835 }
836 // Convert proxy method into expected interface method
837 Method* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers19846512012-02-24 11:42:47 -0800838 DCHECK(interface_method != NULL);
839 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -0700840 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700841 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700842 cur_arg = 0; // reset stack location to read to start
843 // reset index, will index into param type array which doesn't include the receiver
844 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800845 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers19846512012-02-24 11:42:47 -0800846 DCHECK(param_types != NULL);
Ian Rogers14b1b242011-10-11 18:54:34 -0700847 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
Ian Rogers19846512012-02-24 11:42:47 -0800848 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
Ian Rogers14b1b242011-10-11 18:54:34 -0700849 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
850 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700851 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700852 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -0700853 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700854 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -0700855 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
856 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
857 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -0700858 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700859 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -0700860 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700861 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700862 if (self->IsExceptionPending()) {
863 return;
864 }
865 obj = val.l;
866 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700867 args->Set(param_index, obj);
868 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
869 param_index++;
870 }
871 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -0700872 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
873 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -0700874 Class* param_type = param_types->Get(param_index);
875 Object* obj;
876 if (!param_type->IsPrimitive()) {
877 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
878 } else {
879 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700880 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -0700881 if (self->IsExceptionPending()) {
882 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700883 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700884 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700885 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700886 args->Set(param_index, obj);
887 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
888 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700889 }
890 // Get the InvocationHandler method and the field that holds it within the Proxy object
891 static jmethodID inv_hand_invoke_mid = NULL;
892 static jfieldID proxy_inv_hand_fid = NULL;
893 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -0700894 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700895 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -0700896 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700897 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
898 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
899 }
Ian Rogers466bb252011-10-14 03:29:56 -0700900 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700901 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
902 // Call InvocationHandler.invoke
903 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
904 // Place result in stack args
905 if (!self->IsExceptionPending()) {
906 Object* result_ref = self->DecodeJObject(result);
907 if (result_ref != NULL) {
908 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700910 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
911 } else {
912 *reinterpret_cast<jobject*>(stack_args) = NULL;
913 }
Ian Rogers466bb252011-10-14 03:29:56 -0700914 } else {
915 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
916 // a UndeclaredThrowableException.
917 Throwable* exception = self->GetException();
918 self->ClearException();
919 if (!exception->IsCheckedException()) {
920 self->SetException(exception);
921 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800922 SynthesizedProxyClass* proxy_class =
923 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
924 int throws_index = -1;
925 size_t num_virt_methods = proxy_class->NumVirtualMethods();
926 for (size_t i = 0; i < num_virt_methods; i++) {
927 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
928 throws_index = i;
929 break;
930 }
931 }
932 CHECK_NE(throws_index, -1);
933 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -0700934 Class* exception_class = exception->GetClass();
935 bool declares_exception = false;
936 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
937 Class* declared_exception = declared_exceptions->Get(i);
938 declares_exception = declared_exception->IsAssignableFrom(exception_class);
939 }
940 if (declares_exception) {
941 self->SetException(exception);
942 } else {
943 ThrowNewUndeclaredThrowableException(self, env, exception);
944 }
945 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700946 }
947}
948
jeffhaoe343b762011-12-05 16:36:44 -0800949extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -0800950 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -0800951 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
952 self->PushTraceStackFrame(trace_frame);
953
jeffhao2692b572011-12-16 15:42:28 -0800954 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800955
jeffhao2692b572011-12-16 15:42:28 -0800956 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -0800957}
958
959extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -0800960 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -0800961 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
962 Method* method = trace_frame.method_;
963 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800964
jeffhao2692b572011-12-16 15:42:28 -0800965 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -0800966
967 return lr;
968}
969
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800970uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -0800971 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -0800972 TraceStackFrame trace_frame = self->PopTraceStackFrame();
973 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800974 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800975
jeffhao2692b572011-12-16 15:42:28 -0800976 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -0800977
978 return lr;
979}
980
Ian Rogersa433b2e2012-03-09 08:40:33 -0800981int artCmplFloat(float a, float b) {
982 if (a == b) {
983 return 0;
984 } else if (a < b) {
985 return -1;
986 } else if (a > b) {
987 return 1;
988 }
989 return -1;
990}
991
992int artCmpgFloat(float a, float b) {
993 if (a == b) {
994 return 0;
995 } else if (a < b) {
996 return -1;
997 } else if (a > b) {
998 return 1;
999 }
1000 return 1;
1001}
1002
1003int artCmpgDouble(double a, double b) {
1004 if (a == b) {
1005 return 0;
1006 } else if (a < b) {
1007 return -1;
1008 } else if (a > b) {
1009 return 1;
1010 }
1011 return 1;
1012}
1013
1014int artCmplDouble(double a, double b) {
1015 if (a == b) {
1016 return 0;
1017 } else if (a < b) {
1018 return -1;
1019 } else if (a > b) {
1020 return 1;
1021 }
1022 return -1;
1023}
1024
Shih-wei Liao2d831012011-09-28 22:06:53 -07001025/*
1026 * Float/double conversion requires clamping to min and max of integer form. If
1027 * target doesn't support this normally, use these.
1028 */
1029int64_t D2L(double d) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001030 static const double kMaxLong = (double) (int64_t) 0x7fffffffffffffffULL;
1031 static const double kMinLong = (double) (int64_t) 0x8000000000000000ULL;
1032 if (d >= kMaxLong) {
1033 return (int64_t) 0x7fffffffffffffffULL;
1034 } else if (d <= kMinLong) {
1035 return (int64_t) 0x8000000000000000ULL;
1036 } else if (d != d) { // NaN case
1037 return 0;
1038 } else {
1039 return (int64_t) d;
1040 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001041}
1042
1043int64_t F2L(float f) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001044 static const float kMaxLong = (float) (int64_t) 0x7fffffffffffffffULL;
1045 static const float kMinLong = (float) (int64_t) 0x8000000000000000ULL;
1046 if (f >= kMaxLong) {
1047 return (int64_t) 0x7fffffffffffffffULL;
1048 } else if (f <= kMinLong) {
1049 return (int64_t) 0x8000000000000000ULL;
1050 } else if (f != f) { // NaN case
1051 return 0;
1052 } else {
1053 return (int64_t) f;
1054 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001055}
1056
1057} // namespace art