blob: cb66b87ccc57a2f248c0b9493d9d77006bcaac1c [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
Bill Buzbee11f9d212012-03-03 20:03:18 -080032extern "C" int art_cmpl_float(float a, float b) {
33 if (a == b) {
34 return 0;
35 } else if (a < b) {
36 return -1;
37 } else if (a > b) {
38 return 1;
39 }
40 return -1;
41}
42
43extern "C" int art_cmpg_float(float a, float b) {
44 if (a == b) {
45 return 0;
46 } else if (a < b) {
47 return -1;
48 } else if (a > b) {
49 return 1;
50 }
51 return 1;
52}
53
54extern "C" int art_cmpl_double(double a, double b) {
55 if (a == b) {
56 return 0;
57 } else if (a < b) {
58 return -1;
59 } else if (a > b) {
60 return 1;
61 }
62 return -1;
63}
64
65extern "C" int art_cmpg_double(double a, double b) {
66 if (a == b) {
67 return 0;
68 } else if (a < b) {
69 return -1;
70 } else if (a > b) {
71 return 1;
72 }
73 return 1;
74}
75
buzbee44b412b2012-02-04 08:50:53 -080076/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080077 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -080078 * the method. However, because the offset alone cannot distinguish between
79 * method entry and offset 0 within the method, we'll use an offset of -1
80 * to denote method entry.
81 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080082extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -080083 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080084 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -080085}
86
Shih-wei Liao2d831012011-09-28 22:06:53 -070087// Temporary debugging hook for compiler.
88extern void DebugMe(Method* method, uint32_t info) {
89 LOG(INFO) << "DebugMe";
90 if (method != NULL) {
91 LOG(INFO) << PrettyMethod(method);
92 }
93 LOG(INFO) << "Info: " << info;
94}
95
96// Return value helper for jobject return types
97extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -070098 if (thread->IsExceptionPending()) {
99 return NULL;
100 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700101 return thread->DecodeJObject(obj);
102}
103
Ian Rogers60db5ab2012-02-20 17:02:00 -0800104extern void* FindNativeMethod(Thread* self) {
105 DCHECK(Thread::Current() == self);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700106
Ian Rogers60db5ab2012-02-20 17:02:00 -0800107 Method* method = const_cast<Method*>(self->GetCurrentMethod());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700108 DCHECK(method != NULL);
109
110 // Lookup symbol address for method, on failure we'll return NULL with an
111 // exception set, otherwise we return the address of the method we found.
Ian Rogers60db5ab2012-02-20 17:02:00 -0800112 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700113 if (native_code == NULL) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800114 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700115 return NULL;
116 } else {
117 // Register so that future calls don't come here
Ian Rogers60db5ab2012-02-20 17:02:00 -0800118 method->RegisterNative(self, native_code);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700119 return native_code;
120 }
121}
122
123// Called by generated call to throw an exception
124extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
125 /*
126 * exception may be NULL, in which case this routine should
127 * throw NPE. NOTE: this is a convenience for generated code,
128 * which previously did the null check inline and constructed
129 * and threw a NPE if NULL. This routine responsible for setting
130 * exception_ in thread and delivering the exception.
131 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700132 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700133 if (exception == NULL) {
134 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
135 } else {
136 thread->SetException(exception);
137 }
138 thread->DeliverException();
139}
140
141// Deliver an exception that's pending on thread helping set up a callee save frame on the way
142extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700143 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700144 thread->DeliverException();
145}
146
147// Called by generated call to throw a NPE exception
148extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700149 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700150 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700151 thread->DeliverException();
152}
153
154// Called by generated call to throw an arithmetic divide by zero exception
155extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700156 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700157 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
158 thread->DeliverException();
159}
160
161// Called by generated call to throw an arithmetic divide by zero exception
162extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700163 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
164 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
165 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700166 thread->DeliverException();
167}
168
169// Called by the AbstractMethodError stub (not runtime support)
170extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700171 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
172 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
173 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700174 thread->DeliverException();
175}
176
177extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700178 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800179 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800180 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800181 artTraceMethodUnwindFromCode(thread);
182 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700183 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700184 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
185 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700186 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700187 thread->ResetDefaultStackEnd(); // Return to default stack size
188 thread->DeliverException();
189}
190
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700191static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700192 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700193 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
194 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
195
196 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700197 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700198 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
199 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700200 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700201 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
202 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700203 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700204 type_idx = ref;
205 } else {
206 CHECK(false) << static_cast<int>(ref_type);
207 }
208
Ian Rogers0571d352011-11-03 19:51:38 -0700209 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700210 if (!access) {
211 return class_name;
212 }
213
214 std::string result;
215 result += "tried to access class ";
216 result += class_name;
217 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800218 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700219 return result;
220}
221
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700222static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700223 verifier::VerifyErrorRefType ref_type, bool access) {
224 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700225
226 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
227 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
228
229 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700230 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700231 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700232 if (!access) {
233 return class_name + "." + field_name;
234 }
235
236 std::string result;
237 result += "tried to access field ";
238 result += class_name + "." + field_name;
239 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800240 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700241 return result;
242}
243
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700244static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800245 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700246 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700247
248 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
249 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
250
251 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700252 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700253 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700254 if (!access) {
255 return class_name + "." + method_name;
256 }
257
258 std::string result;
259 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700260 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700261 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700262 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800263 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700264 return result;
265}
266
267extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700268 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
269 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700270 frame.Next();
271 Method* method = frame.GetMethod();
272
Ian Rogersd81871c2011-10-03 13:57:23 -0700273 verifier::VerifyErrorRefType ref_type =
274 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700275
276 const char* exception_class = "Ljava/lang/VerifyError;";
277 std::string msg;
278
Ian Rogersd81871c2011-10-03 13:57:23 -0700279 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
280 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700281 exception_class = "Ljava/lang/NoClassDefFoundError;";
282 msg = ClassNameFromIndex(method, ref, ref_type, false);
283 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700284 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700285 exception_class = "Ljava/lang/NoSuchFieldError;";
286 msg = FieldNameFromIndex(method, ref, ref_type, false);
287 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700288 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700289 exception_class = "Ljava/lang/NoSuchMethodError;";
290 msg = MethodNameFromIndex(method, ref, ref_type, false);
291 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700292 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700293 exception_class = "Ljava/lang/IllegalAccessError;";
294 msg = ClassNameFromIndex(method, ref, ref_type, true);
295 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700296 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700297 exception_class = "Ljava/lang/IllegalAccessError;";
298 msg = FieldNameFromIndex(method, ref, ref_type, true);
299 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700300 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700301 exception_class = "Ljava/lang/IllegalAccessError;";
302 msg = MethodNameFromIndex(method, ref, ref_type, true);
303 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700304 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700305 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
306 msg = ClassNameFromIndex(method, ref, ref_type, false);
307 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700308 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700309 exception_class = "Ljava/lang/InstantiationError;";
310 msg = ClassNameFromIndex(method, ref, ref_type, false);
311 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700312 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700313 // Generic VerifyError; use default exception, no message.
314 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700315 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700316 CHECK(false);
317 break;
318 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700319 self->ThrowNewException(exception_class, msg.c_str());
320 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700321}
322
323extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700324 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700325 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700326 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700327 thread->DeliverException();
328}
329
330extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700331 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700332 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700333 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700334 thread->DeliverException();
335}
336
Elliott Hughese1410a22011-10-04 12:10:24 -0700337extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700338 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
339 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700340 frame.Next();
341 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700342 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700343 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700344 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700345}
346
347extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700348 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700349 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700350 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700351 thread->DeliverException();
352}
353
Ian Rogers19846512012-02-24 11:42:47 -0800354const void* UnresolvedDirectMethodTrampolineFromCode(Method* called, Method** sp, Thread* thread,
355 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700356 // TODO: this code is specific to ARM
357 // On entry the stack pointed by sp is:
358 // | argN | |
359 // | ... | |
360 // | arg4 | |
361 // | arg3 spill | | Caller's frame
362 // | arg2 spill | |
363 // | arg1 spill | |
364 // | Method* | ---
365 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700366 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700367 // | R3 | arg3
368 // | R2 | arg2
369 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700370 // | R0 |
371 // | Method* | <- sp
372 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
373 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
374 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
375 uintptr_t caller_pc = regs[10];
376 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700377 // Start new JNI local reference state
378 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700379 ScopedJniEnvLocalRefState env_state(env);
Ian Rogers19846512012-02-24 11:42:47 -0800380
381 // Compute details about the called method (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700382 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers19846512012-02-24 11:42:47 -0800383 Method* caller = *caller_sp;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700384 bool is_static;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800385 bool is_virtual;
Ian Rogers19846512012-02-24 11:42:47 -0800386 uint32_t dex_method_idx;
387 const char* shorty;
388 uint32_t shorty_len;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700389 if (type == Runtime::kUnknownMethod) {
Ian Rogers19846512012-02-24 11:42:47 -0800390 DCHECK(called->IsRuntimeMethod());
Ian Rogersdf9a7822011-10-11 16:53:22 -0700391 // less two as return address may span into next dex instruction
392 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800393 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700394 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
395 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700396 Instruction::Code instr_code = instr->Opcode();
397 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
398 (instr_code == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800399 is_virtual = (instr_code == Instruction::INVOKE_VIRTUAL) ||
400 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700401 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
Ian Rogersfb6adba2012-03-04 21:51:51 -0800402 (instr_code == Instruction::INVOKE_DIRECT_RANGE) ||
403 (instr_code == Instruction::INVOKE_VIRTUAL) ||
404 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE));
Elliott Hughesadb8c672012-03-06 16:49:32 -0800405 DecodedInstruction dec_insn(instr);
406 dex_method_idx = dec_insn.vB;
Ian Rogers19846512012-02-24 11:42:47 -0800407 shorty = linker->MethodShorty(dex_method_idx, caller, &shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700408 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800409 DCHECK(!called->IsRuntimeMethod());
Ian Rogersea2a11d2011-10-11 16:48:51 -0700410 is_static = type == Runtime::kStaticMethod;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800411 is_virtual = false;
Ian Rogers19846512012-02-24 11:42:47 -0800412 dex_method_idx = called->GetDexMethodIndex();
413 MethodHelper mh(called);
414 shorty = mh.GetShorty();
415 shorty_len = mh.GetShortyLength();
416 }
417 // Discover shorty (avoid GCs)
418 size_t args_in_regs = 0;
419 for (size_t i = 1; i < shorty_len; i++) {
420 char c = shorty[i];
421 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
422 if (args_in_regs > 3) {
423 args_in_regs = 3;
424 break;
425 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700426 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700427 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700428 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700429 if (!is_static) {
430 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
431 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700432 if (args_in_regs < 3) {
433 // If we thought we had fewer than 3 arguments in registers, account for the receiver
434 args_in_regs++;
435 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700436 AddLocalReference<jobject>(env, obj);
437 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700438 size_t shorty_index = 1; // skip return value
439 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
440 // R0)
441 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
442 char c = shorty[shorty_index];
443 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700444 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700445 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
446 AddLocalReference<jobject>(env, obj);
447 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700448 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
449 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700450 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700451 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700452 while (shorty_index < shorty_len) {
453 char c = shorty[shorty_index];
454 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700455 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700456 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700457 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700458 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700459 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700460 }
461 // Resolve method filling in dex cache
Ian Rogers19846512012-02-24 11:42:47 -0800462 if (type == Runtime::kUnknownMethod) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800463 called = linker->ResolveMethod(dex_method_idx, caller, !is_virtual);
Ian Rogers19846512012-02-24 11:42:47 -0800464 }
465 const void* code = NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700466 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800467 if (LIKELY(called->IsDirect() == !is_virtual)) {
Ian Rogers19846512012-02-24 11:42:47 -0800468 // Ensure that the called method's class is initialized.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800469 Class* called_class = called->GetDeclaringClass();
470 linker->EnsureInitialized(called_class, true);
471 if (LIKELY(called_class->IsInitialized())) {
Ian Rogers19846512012-02-24 11:42:47 -0800472 code = called->GetCode();
473 } else if (called_class->IsInitializing()) {
474 // Class is still initializing, go to oat and grab code (trampoline must be left in place
475 // until class is initialized to stop races between threads).
476 code = linker->GetOatCodeFor(called);
477 } else {
478 DCHECK(called_class->IsErroneous());
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800479 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800480 } else {
481 // Direct method has been made virtual
482 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
483 "Expected direct method but found virtual: %s",
484 PrettyMethod(called, true).c_str());
485 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700486 }
Ian Rogers19846512012-02-24 11:42:47 -0800487 if (UNLIKELY(code == NULL)) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700488 // Something went wrong in ResolveMethod or EnsureInitialized,
489 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700490 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700491 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700492 thread->ClearException();
493 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800494 // Expect class to at least be initializing.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800495 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogers19846512012-02-24 11:42:47 -0800496 // Don't want infinite recursion.
497 DCHECK(code != Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod)->GetData());
Ian Rogersad25ac52011-10-04 19:13:33 -0700498 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700499 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700500 }
501 return code;
502}
503
Ian Rogers60db5ab2012-02-20 17:02:00 -0800504static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
505 intptr_t value = *arg_ptr;
506 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
507 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
508 CHECK(Heap::IsHeapAddress(value_as_work_around_rep));
509 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
510}
511
512extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
513 DCHECK(Thread::Current() == self);
514 // TODO: this code is specific to ARM
515 // On entry the stack pointed by sp is:
516 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
517 // | LR |
518 // | R3 | arg2
519 // | R2 | arg1
520 // | R1 | jclass/jobject
521 // | R0 | JNIEnv
522 // | unused |
523 // | unused |
524 // | unused | <- sp
525 Method* jni_method = self->GetTopOfStack().GetMethod();
526 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
527 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
528 // Fix up this/jclass argument
529 WorkAroundJniBugsForJobject(arg_ptr);
530 arg_ptr++;
531 // Fix up jobject arguments
532 MethodHelper mh(jni_method);
533 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800534 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800535 char shorty_char = mh.GetShorty()[i];
536 if (shorty_char == 'L') {
537 WorkAroundJniBugsForJobject(arg_ptr);
538 }
539 if (shorty_char == 'J' || shorty_char == 'D') {
540 if (reg_num == 2) {
541 arg_ptr = sp + 8; // skip to out arguments
542 reg_num = -1;
543 } else if (reg_num == 3) {
544 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
545 reg_num = -1;
546 } else {
547 DCHECK(reg_num == -1);
548 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
549 arg_ptr += 3; // unaligned, pad and move through stack arguments
550 } else {
551 arg_ptr += 2; // aligned, move through stack arguments
552 }
553 }
554 } else {
555 if (reg_num == 2) {
556 arg_ptr++; // move through register arguments
557 reg_num++;
558 } else if (reg_num == 3) {
559 arg_ptr = sp + 8; // skip to outgoing stack arguments
560 reg_num = -1;
561 } else {
562 DCHECK(reg_num == -1);
563 arg_ptr++; // move through stack arguments
564 }
565 }
566 }
567 // Load expected destination, see Method::RegisterNative
Ian Rogers19846512012-02-24 11:42:47 -0800568 const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
569 if (UNLIKELY(code == NULL)) {
570 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
571 jni_method->RegisterNative(self, code);
572 }
573 return code;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800574}
575
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800576extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
577 Thread* self, Method** sp) {
578 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
579 return AllocObjectFromCode(type_idx, method, self, false);
580}
581
Ian Rogers28ad40d2011-10-27 15:19:26 -0700582extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
583 Thread* self, Method** sp) {
584 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800585 return AllocObjectFromCode(type_idx, method, self, true);
586}
587
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800588extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
589 Thread* self, Method** sp) {
590 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
591 return AllocArrayFromCode(type_idx, method, component_count, self, false);
592}
593
594extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
595 int32_t component_count,
596 Thread* self, Method** sp) {
597 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
598 return AllocArrayFromCode(type_idx, method, component_count, self, true);
599}
600
Ian Rogersce9eca62011-10-07 17:11:03 -0700601extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
602 int32_t component_count, Thread* self, Method** sp) {
603 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800604 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700605}
606
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800607extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
608 int32_t component_count,
609 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700610 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800611 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700612}
613
Ian Rogerscaab8c42011-10-12 12:11:18 -0700614// Assignable test for code, won't throw. Null and equality tests already performed
615uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
616 DCHECK(klass != NULL);
617 DCHECK(ref_class != NULL);
618 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
619}
620
Shih-wei Liao2d831012011-09-28 22:06:53 -0700621// 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 -0700622extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700623 DCHECK(a->IsClass()) << PrettyClass(a);
624 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700625 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700626 return 0; // Success
627 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700628 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700629 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700630 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800631 PrettyDescriptor(a).c_str(),
632 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700633 return -1; // Failure
634 }
635}
636
637// Tests whether 'element' can be assigned into an array of type 'array_class'.
638// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700639extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
640 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700641 DCHECK(array_class != NULL);
642 // element can't be NULL as we catch this is screened in runtime_support
643 Class* element_class = element->GetClass();
644 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700645 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700646 return 0; // Success
647 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700648 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700649 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800650 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800651 PrettyDescriptor(element_class).c_str(),
652 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700653 return -1; // Failure
654 }
655}
656
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700657extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
658 Thread* self, Method** sp) {
659 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800660 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700661}
662
Ian Rogers28ad40d2011-10-27 15:19:26 -0700663extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
664 Method** sp) {
665 // Called when method->dex_cache_resolved_types_[] misses
666 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800667 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700668}
669
Ian Rogersb093c6b2011-10-31 16:19:55 -0700670extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
671 const Method* referrer, Thread* self,
672 Method** sp) {
673 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
674 // unpopulated
675 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800676 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700677}
678
Brian Carlstromaded5f72011-10-07 17:15:04 -0700679extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
680 Thread* self, Method** sp) {
681 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
682 return ResolveStringFromCode(referrer, string_idx);
683}
684
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700685extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
686 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700687 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700688 // MonitorExit may throw exception
689 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
690}
691
692extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
693 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
694 DCHECK(obj != NULL); // Assumed to have been checked before entry
695 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700696 DCHECK(thread->HoldsLock(obj));
697 // Only possible exception is NPE and is handled before entry
698 DCHECK(!thread->IsExceptionPending());
699}
700
Ian Rogers4a510d82011-10-09 14:30:24 -0700701void CheckSuspendFromCode(Thread* thread) {
702 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700703 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
704}
705
Ian Rogers4a510d82011-10-09 14:30:24 -0700706extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
707 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700708 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700709 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
710}
711
712/*
713 * Fill the array with predefined constant values, throwing exceptions if the array is null or
714 * not of sufficient length.
715 *
716 * NOTE: When dealing with a raw dex file, the data to be copied uses
717 * little-endian ordering. Require that oat2dex do any required swapping
718 * so this routine can get by with a memcpy().
719 *
720 * Format of the data:
721 * ushort ident = 0x0300 magic value
722 * ushort width width of each element in the table
723 * uint size number of elements in the table
724 * ubyte data[size*width] table of data values (may contain a single-byte
725 * padding at the end)
726 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700727extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
728 Thread* self, Method** sp) {
729 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700730 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700731 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700732 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
733 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700734 return -1; // Error
735 }
736 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
737 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700738 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700739 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
740 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700741 return -1; // Error
742 }
743 uint16_t width = table[1];
744 uint32_t size_in_bytes = size * width;
Ian Rogersa15e67d2012-02-28 13:51:55 -0800745 memcpy((char*)array + Array::DataOffset(width).Int32Value(), (char*)&table[4], size_in_bytes);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700746 return 0; // Success
747}
748
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800749// See comments in runtime_support_asm.S
750extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
751 Method* caller_method, Thread* self,
752 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800753 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800754}
755
756extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
757 Object* this_object,
758 Method* caller_method, Thread* self,
759 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800760 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
761}
762
763
764extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
765 Object* this_object,
766 Method* caller_method, Thread* self,
767 Method** sp) {
768 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
769}
770
771extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
772 Object* this_object,
773 Method* caller_method, Thread* self,
774 Method** sp) {
775 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800776}
777
778extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
779 Object* this_object,
780 Method* caller_method, Thread* self,
781 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800782 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800783}
784
785extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
786 Object* this_object,
787 Method* caller_method, Thread* self,
788 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800789 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800790}
791
Ian Rogers466bb252011-10-14 03:29:56 -0700792static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
793 ScopedLocalRef<jclass> jlr_UTE_class(env,
794 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
795 if (jlr_UTE_class.get() == NULL) {
796 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
797 } else {
798 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
799 "(Ljava/lang/Throwable;)V");
800 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
801 ScopedLocalRef<jthrowable> jlr_UTE(env,
802 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
803 jexception)));
804 int rc = env->Throw(jlr_UTE.get());
805 if (rc != JNI_OK) {
806 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
807 }
808 }
809 CHECK(self->IsExceptionPending());
810}
811
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700812// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
813// which is responsible for recording callee save registers. We explicitly handlerize incoming
814// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
815// the invocation handler which is a field within the proxy object receiver.
816extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -0700817 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700818 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -0700819 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
820 DCHECK_EQ(*proxy_sp, proxy_method);
821 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700822 // TODO: ARM specific
823 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
824 // Start new JNI local reference state
825 JNIEnvExt* env = self->GetJniEnv();
826 ScopedJniEnvLocalRefState env_state(env);
827 // Create local ref. copies of proxy method and the receiver
828 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
829 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
830
Ian Rogers14b1b242011-10-11 18:54:34 -0700831 // Placing into local references incoming arguments from the caller's register arguments,
832 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800833 MethodHelper proxy_mh(proxy_method);
834 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -0700835 size_t args_in_regs = 0;
836 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800837 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -0700838 if (args_in_regs > 2) {
839 args_in_regs = 2;
840 break;
841 }
842 }
843 size_t cur_arg = 0; // current stack location to read
844 size_t param_index = 1; // skip receiver
845 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800846 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -0700847 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700848 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -0700849 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700850 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800851 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -0700852 param_index++;
853 }
854 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -0700855 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -0700856 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800857 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -0700858 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
859 jobject jobj = AddLocalReference<jobject>(env, obj);
860 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700861 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800862 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -0700863 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700864 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700865 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
866 jvalue args_jobj[3];
867 args_jobj[0].l = rcvr_jobj;
868 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -0700869 // Args array, if no arguments then NULL (don't include receiver in argument count)
870 args_jobj[2].l = NULL;
871 ObjectArray<Object>* args = NULL;
872 if ((num_params - 1) > 0) {
873 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700874 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -0700875 CHECK(self->IsExceptionPending());
876 return;
877 }
878 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
879 }
880 // Convert proxy method into expected interface method
881 Method* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers19846512012-02-24 11:42:47 -0800882 DCHECK(interface_method != NULL);
883 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -0700884 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700885 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700886 cur_arg = 0; // reset stack location to read to start
887 // reset index, will index into param type array which doesn't include the receiver
888 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800889 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers19846512012-02-24 11:42:47 -0800890 DCHECK(param_types != NULL);
Ian Rogers14b1b242011-10-11 18:54:34 -0700891 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
Ian Rogers19846512012-02-24 11:42:47 -0800892 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
Ian Rogers14b1b242011-10-11 18:54:34 -0700893 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
894 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700895 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700896 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -0700897 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700898 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -0700899 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
900 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
901 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -0700902 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700903 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -0700904 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700905 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700906 if (self->IsExceptionPending()) {
907 return;
908 }
909 obj = val.l;
910 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700911 args->Set(param_index, obj);
912 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
913 param_index++;
914 }
915 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -0700916 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
917 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -0700918 Class* param_type = param_types->Get(param_index);
919 Object* obj;
920 if (!param_type->IsPrimitive()) {
921 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
922 } else {
923 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700924 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -0700925 if (self->IsExceptionPending()) {
926 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700927 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700928 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700929 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700930 args->Set(param_index, obj);
931 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
932 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700933 }
934 // Get the InvocationHandler method and the field that holds it within the Proxy object
935 static jmethodID inv_hand_invoke_mid = NULL;
936 static jfieldID proxy_inv_hand_fid = NULL;
937 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -0700938 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700939 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -0700940 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700941 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
942 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
943 }
Ian Rogers466bb252011-10-14 03:29:56 -0700944 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700945 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
946 // Call InvocationHandler.invoke
947 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
948 // Place result in stack args
949 if (!self->IsExceptionPending()) {
950 Object* result_ref = self->DecodeJObject(result);
951 if (result_ref != NULL) {
952 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800953 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700954 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
955 } else {
956 *reinterpret_cast<jobject*>(stack_args) = NULL;
957 }
Ian Rogers466bb252011-10-14 03:29:56 -0700958 } else {
959 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
960 // a UndeclaredThrowableException.
961 Throwable* exception = self->GetException();
962 self->ClearException();
963 if (!exception->IsCheckedException()) {
964 self->SetException(exception);
965 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800966 SynthesizedProxyClass* proxy_class =
967 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
968 int throws_index = -1;
969 size_t num_virt_methods = proxy_class->NumVirtualMethods();
970 for (size_t i = 0; i < num_virt_methods; i++) {
971 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
972 throws_index = i;
973 break;
974 }
975 }
976 CHECK_NE(throws_index, -1);
977 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -0700978 Class* exception_class = exception->GetClass();
979 bool declares_exception = false;
980 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
981 Class* declared_exception = declared_exceptions->Get(i);
982 declares_exception = declared_exception->IsAssignableFrom(exception_class);
983 }
984 if (declares_exception) {
985 self->SetException(exception);
986 } else {
987 ThrowNewUndeclaredThrowableException(self, env, exception);
988 }
989 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700990 }
991}
992
jeffhaoe343b762011-12-05 16:36:44 -0800993extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -0800994 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -0800995 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
996 self->PushTraceStackFrame(trace_frame);
997
jeffhao2692b572011-12-16 15:42:28 -0800998 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800999
jeffhao2692b572011-12-16 15:42:28 -08001000 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001001}
1002
1003extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001004 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001005 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1006 Method* method = trace_frame.method_;
1007 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001008
jeffhao2692b572011-12-16 15:42:28 -08001009 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001010
1011 return lr;
1012}
1013
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001014uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001015 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001016 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1017 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001018 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001019
jeffhao2692b572011-12-16 15:42:28 -08001020 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001021
1022 return lr;
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) {
1030 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}
1041
1042int64_t F2L(float f) {
1043 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1044 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1045 if (f >= kMaxLong)
1046 return (int64_t)0x7fffffffffffffffULL;
1047 else if (f <= kMinLong)
1048 return (int64_t)0x8000000000000000ULL;
1049 else if (f != f) // NaN case
1050 return 0;
1051 else
1052 return (int64_t)f;
1053}
1054
1055} // namespace art