blob: 6531eb21f0e607f937edfed7065636066be1f77d [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
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -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
76// Place a special frame at the TOS that will save the callee saves for the given type
77static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
78 // Be aware the store below may well stomp on an incoming argument
79 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
80 self->SetTopOfStack(sp, 0);
81}
82
buzbee44b412b2012-02-04 08:50:53 -080083/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080084 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -080085 * the method. However, because the offset alone cannot distinguish between
86 * method entry and offset 0 within the method, we'll use an offset of -1
87 * to denote method entry.
88 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080089extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -080090 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080091 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -080092}
93
Shih-wei Liao2d831012011-09-28 22:06:53 -070094// Temporary debugging hook for compiler.
95extern void DebugMe(Method* method, uint32_t info) {
96 LOG(INFO) << "DebugMe";
97 if (method != NULL) {
98 LOG(INFO) << PrettyMethod(method);
99 }
100 LOG(INFO) << "Info: " << info;
101}
102
103// Return value helper for jobject return types
104extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -0700105 if (thread->IsExceptionPending()) {
106 return NULL;
107 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700108 return thread->DecodeJObject(obj);
109}
110
Ian Rogers60db5ab2012-02-20 17:02:00 -0800111extern void* FindNativeMethod(Thread* self) {
112 DCHECK(Thread::Current() == self);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700113
Ian Rogers60db5ab2012-02-20 17:02:00 -0800114 Method* method = const_cast<Method*>(self->GetCurrentMethod());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700115 DCHECK(method != NULL);
116
117 // Lookup symbol address for method, on failure we'll return NULL with an
118 // exception set, otherwise we return the address of the method we found.
Ian Rogers60db5ab2012-02-20 17:02:00 -0800119 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700120 if (native_code == NULL) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800121 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700122 return NULL;
123 } else {
124 // Register so that future calls don't come here
Ian Rogers60db5ab2012-02-20 17:02:00 -0800125 method->RegisterNative(self, native_code);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700126 return native_code;
127 }
128}
129
130// Called by generated call to throw an exception
131extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
132 /*
133 * exception may be NULL, in which case this routine should
134 * throw NPE. NOTE: this is a convenience for generated code,
135 * which previously did the null check inline and constructed
136 * and threw a NPE if NULL. This routine responsible for setting
137 * exception_ in thread and delivering the exception.
138 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700139 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700140 if (exception == NULL) {
141 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
142 } else {
143 thread->SetException(exception);
144 }
145 thread->DeliverException();
146}
147
148// Deliver an exception that's pending on thread helping set up a callee save frame on the way
149extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700150 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700151 thread->DeliverException();
152}
153
154// Called by generated call to throw a NPE exception
155extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700156 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700157 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700158 thread->DeliverException();
159}
160
161// Called by generated call to throw an arithmetic divide by zero exception
162extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700163 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700164 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
165 thread->DeliverException();
166}
167
168// Called by generated call to throw an arithmetic divide by zero exception
169extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700170 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
171 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
172 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700173 thread->DeliverException();
174}
175
176// Called by the AbstractMethodError stub (not runtime support)
177extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700178 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
179 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
180 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700181 thread->DeliverException();
182}
183
184extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700185 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800186 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800187 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800188 artTraceMethodUnwindFromCode(thread);
189 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700190 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700191 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
192 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700193 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700194 thread->ResetDefaultStackEnd(); // Return to default stack size
195 thread->DeliverException();
196}
197
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700198static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700199 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700200 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
201 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
202
203 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700204 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700205 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
206 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700207 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700208 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
209 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700210 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700211 type_idx = ref;
212 } else {
213 CHECK(false) << static_cast<int>(ref_type);
214 }
215
Ian Rogers0571d352011-11-03 19:51:38 -0700216 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700217 if (!access) {
218 return class_name;
219 }
220
221 std::string result;
222 result += "tried to access class ";
223 result += class_name;
224 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700226 return result;
227}
228
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700229static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700230 verifier::VerifyErrorRefType ref_type, bool access) {
231 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700232
233 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
234 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
235
236 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700237 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700238 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700239 if (!access) {
240 return class_name + "." + field_name;
241 }
242
243 std::string result;
244 result += "tried to access field ";
245 result += class_name + "." + field_name;
246 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700248 return result;
249}
250
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700251static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800252 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700253 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700254
255 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
256 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
257
258 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700259 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700260 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700261 if (!access) {
262 return class_name + "." + method_name;
263 }
264
265 std::string result;
266 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700267 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700268 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700269 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700271 return result;
272}
273
274extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700275 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
276 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700277 frame.Next();
278 Method* method = frame.GetMethod();
279
Ian Rogersd81871c2011-10-03 13:57:23 -0700280 verifier::VerifyErrorRefType ref_type =
281 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700282
283 const char* exception_class = "Ljava/lang/VerifyError;";
284 std::string msg;
285
Ian Rogersd81871c2011-10-03 13:57:23 -0700286 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
287 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700288 exception_class = "Ljava/lang/NoClassDefFoundError;";
289 msg = ClassNameFromIndex(method, ref, ref_type, false);
290 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700291 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700292 exception_class = "Ljava/lang/NoSuchFieldError;";
293 msg = FieldNameFromIndex(method, ref, ref_type, false);
294 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700295 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700296 exception_class = "Ljava/lang/NoSuchMethodError;";
297 msg = MethodNameFromIndex(method, ref, ref_type, false);
298 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700299 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700300 exception_class = "Ljava/lang/IllegalAccessError;";
301 msg = ClassNameFromIndex(method, ref, ref_type, true);
302 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700303 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700304 exception_class = "Ljava/lang/IllegalAccessError;";
305 msg = FieldNameFromIndex(method, ref, ref_type, true);
306 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700307 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700308 exception_class = "Ljava/lang/IllegalAccessError;";
309 msg = MethodNameFromIndex(method, ref, ref_type, true);
310 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700311 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700312 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
313 msg = ClassNameFromIndex(method, ref, ref_type, false);
314 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700315 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700316 exception_class = "Ljava/lang/InstantiationError;";
317 msg = ClassNameFromIndex(method, ref, ref_type, false);
318 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700320 // Generic VerifyError; use default exception, no message.
321 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700322 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700323 CHECK(false);
324 break;
325 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700326 self->ThrowNewException(exception_class, msg.c_str());
327 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700328}
329
330extern "C" void artThrowInternalErrorFromCode(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: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700333 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700334 thread->DeliverException();
335}
336
337extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700338 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700339 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700340 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700341 thread->DeliverException();
342}
343
Elliott Hughese1410a22011-10-04 12:10:24 -0700344extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700345 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
346 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700347 frame.Next();
348 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700349 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700350 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700351 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700352}
353
354extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700355 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700356 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700357 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700358 thread->DeliverException();
359}
360
Ian Rogers19846512012-02-24 11:42:47 -0800361const void* UnresolvedDirectMethodTrampolineFromCode(Method* called, Method** sp, Thread* thread,
362 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700363 // TODO: this code is specific to ARM
364 // On entry the stack pointed by sp is:
365 // | argN | |
366 // | ... | |
367 // | arg4 | |
368 // | arg3 spill | | Caller's frame
369 // | arg2 spill | |
370 // | arg1 spill | |
371 // | Method* | ---
372 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700373 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700374 // | R3 | arg3
375 // | R2 | arg2
376 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700377 // | R0 |
378 // | Method* | <- sp
379 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
380 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
381 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
382 uintptr_t caller_pc = regs[10];
383 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700384 // Start new JNI local reference state
385 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700386 ScopedJniEnvLocalRefState env_state(env);
Ian Rogers19846512012-02-24 11:42:47 -0800387
388 // Compute details about the called method (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700389 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers19846512012-02-24 11:42:47 -0800390 Method* caller = *caller_sp;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700391 bool is_static;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800392 bool is_virtual;
Ian Rogers19846512012-02-24 11:42:47 -0800393 uint32_t dex_method_idx;
394 const char* shorty;
395 uint32_t shorty_len;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700396 if (type == Runtime::kUnknownMethod) {
Ian Rogers19846512012-02-24 11:42:47 -0800397 DCHECK(called->IsRuntimeMethod());
Ian Rogersdf9a7822011-10-11 16:53:22 -0700398 // less two as return address may span into next dex instruction
399 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800400 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700401 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
402 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700403 Instruction::Code instr_code = instr->Opcode();
404 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
405 (instr_code == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800406 is_virtual = (instr_code == Instruction::INVOKE_VIRTUAL) ||
407 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700408 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
Ian Rogersfb6adba2012-03-04 21:51:51 -0800409 (instr_code == Instruction::INVOKE_DIRECT_RANGE) ||
410 (instr_code == Instruction::INVOKE_VIRTUAL) ||
411 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE));
Elliott Hughesadb8c672012-03-06 16:49:32 -0800412 DecodedInstruction dec_insn(instr);
413 dex_method_idx = dec_insn.vB;
Ian Rogers19846512012-02-24 11:42:47 -0800414 shorty = linker->MethodShorty(dex_method_idx, caller, &shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700415 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800416 DCHECK(!called->IsRuntimeMethod());
Ian Rogersea2a11d2011-10-11 16:48:51 -0700417 is_static = type == Runtime::kStaticMethod;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800418 is_virtual = false;
Ian Rogers19846512012-02-24 11:42:47 -0800419 dex_method_idx = called->GetDexMethodIndex();
420 MethodHelper mh(called);
421 shorty = mh.GetShorty();
422 shorty_len = mh.GetShortyLength();
423 }
424 // Discover shorty (avoid GCs)
425 size_t args_in_regs = 0;
426 for (size_t i = 1; i < shorty_len; i++) {
427 char c = shorty[i];
428 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
429 if (args_in_regs > 3) {
430 args_in_regs = 3;
431 break;
432 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700433 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700434 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700435 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700436 if (!is_static) {
437 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
438 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700439 if (args_in_regs < 3) {
440 // If we thought we had fewer than 3 arguments in registers, account for the receiver
441 args_in_regs++;
442 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700443 AddLocalReference<jobject>(env, obj);
444 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700445 size_t shorty_index = 1; // skip return value
446 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
447 // R0)
448 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
449 char c = shorty[shorty_index];
450 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700451 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700452 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
453 AddLocalReference<jobject>(env, obj);
454 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700455 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
456 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700457 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700458 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700459 while (shorty_index < shorty_len) {
460 char c = shorty[shorty_index];
461 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700462 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700463 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700464 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700465 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700466 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700467 }
468 // Resolve method filling in dex cache
Ian Rogers19846512012-02-24 11:42:47 -0800469 if (type == Runtime::kUnknownMethod) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800470 called = linker->ResolveMethod(dex_method_idx, caller, !is_virtual);
Ian Rogers19846512012-02-24 11:42:47 -0800471 }
472 const void* code = NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700473 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800474 if (LIKELY(called->IsDirect() == !is_virtual)) {
Ian Rogers19846512012-02-24 11:42:47 -0800475 // Ensure that the called method's class is initialized.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800476 Class* called_class = called->GetDeclaringClass();
477 linker->EnsureInitialized(called_class, true);
478 if (LIKELY(called_class->IsInitialized())) {
Ian Rogers19846512012-02-24 11:42:47 -0800479 code = called->GetCode();
480 } else if (called_class->IsInitializing()) {
481 // Class is still initializing, go to oat and grab code (trampoline must be left in place
482 // until class is initialized to stop races between threads).
483 code = linker->GetOatCodeFor(called);
484 } else {
485 DCHECK(called_class->IsErroneous());
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800486 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800487 } else {
488 // Direct method has been made virtual
489 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
490 "Expected direct method but found virtual: %s",
491 PrettyMethod(called, true).c_str());
492 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700493 }
Ian Rogers19846512012-02-24 11:42:47 -0800494 if (UNLIKELY(code == NULL)) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700495 // Something went wrong in ResolveMethod or EnsureInitialized,
496 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700497 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700498 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700499 thread->ClearException();
500 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800501 // Expect class to at least be initializing.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800502 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogers19846512012-02-24 11:42:47 -0800503 // Don't want infinite recursion.
504 DCHECK(code != Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod)->GetData());
Ian Rogersad25ac52011-10-04 19:13:33 -0700505 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700506 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700507 }
508 return code;
509}
510
Ian Rogers60db5ab2012-02-20 17:02:00 -0800511static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
512 intptr_t value = *arg_ptr;
513 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
514 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
515 CHECK(Heap::IsHeapAddress(value_as_work_around_rep));
516 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
517}
518
519extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
520 DCHECK(Thread::Current() == self);
521 // TODO: this code is specific to ARM
522 // On entry the stack pointed by sp is:
523 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
524 // | LR |
525 // | R3 | arg2
526 // | R2 | arg1
527 // | R1 | jclass/jobject
528 // | R0 | JNIEnv
529 // | unused |
530 // | unused |
531 // | unused | <- sp
532 Method* jni_method = self->GetTopOfStack().GetMethod();
533 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
534 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
535 // Fix up this/jclass argument
536 WorkAroundJniBugsForJobject(arg_ptr);
537 arg_ptr++;
538 // Fix up jobject arguments
539 MethodHelper mh(jni_method);
540 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800541 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800542 char shorty_char = mh.GetShorty()[i];
543 if (shorty_char == 'L') {
544 WorkAroundJniBugsForJobject(arg_ptr);
545 }
546 if (shorty_char == 'J' || shorty_char == 'D') {
547 if (reg_num == 2) {
548 arg_ptr = sp + 8; // skip to out arguments
549 reg_num = -1;
550 } else if (reg_num == 3) {
551 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
552 reg_num = -1;
553 } else {
554 DCHECK(reg_num == -1);
555 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
556 arg_ptr += 3; // unaligned, pad and move through stack arguments
557 } else {
558 arg_ptr += 2; // aligned, move through stack arguments
559 }
560 }
561 } else {
562 if (reg_num == 2) {
563 arg_ptr++; // move through register arguments
564 reg_num++;
565 } else if (reg_num == 3) {
566 arg_ptr = sp + 8; // skip to outgoing stack arguments
567 reg_num = -1;
568 } else {
569 DCHECK(reg_num == -1);
570 arg_ptr++; // move through stack arguments
571 }
572 }
573 }
574 // Load expected destination, see Method::RegisterNative
Ian Rogers19846512012-02-24 11:42:47 -0800575 const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
576 if (UNLIKELY(code == NULL)) {
577 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
578 jni_method->RegisterNative(self, code);
579 }
580 return code;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800581}
582
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -0800583
584extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
585 Thread* self, Method** sp) {
586 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
587 if (LIKELY(field != NULL)) {
588 return field->Get32(NULL);
589 }
590 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
591 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int32_t));
592 if (LIKELY(field != NULL)) {
593 return field->Get32(NULL);
594 }
595 return 0; // Will throw exception by checking with Thread::Current
596}
597
598extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
599 Thread* self, Method** sp) {
600 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
601 if (LIKELY(field != NULL)) {
602 return field->Get64(NULL);
603 }
604 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
605 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int64_t));
606 if (LIKELY(field != NULL)) {
607 return field->Get64(NULL);
608 }
609 return 0; // Will throw exception by checking with Thread::Current
610}
611
612extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
613 Thread* self, Method** sp) {
614 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
615 if (LIKELY(field != NULL)) {
616 return field->GetObj(NULL);
617 }
618 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
619 field = FindFieldFromCode(field_idx, referrer, self, true, false, false, sizeof(Object*));
620 if (LIKELY(field != NULL)) {
621 return field->GetObj(NULL);
622 }
623 return NULL; // Will throw exception by checking with Thread::Current
624}
625
626extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
627 const Method* referrer, Thread* self, Method** sp) {
628 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
629 if (LIKELY(field != NULL && obj != NULL)) {
630 return field->Get32(obj);
631 }
632 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
633 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int32_t));
634 if (LIKELY(field != NULL)) {
635 if (UNLIKELY(obj == NULL)) {
636 ThrowNullPointerExceptionForFieldAccess(self, field, true);
637 } else {
638 return field->Get32(obj);
639 }
640 }
641 return 0; // Will throw exception by checking with Thread::Current
642}
643
644extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
645 const Method* referrer, Thread* self, Method** sp) {
646 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
647 if (LIKELY(field != NULL && obj != NULL)) {
648 return field->Get64(obj);
649 }
650 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
651 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int64_t));
652 if (LIKELY(field != NULL)) {
653 if (UNLIKELY(obj == NULL)) {
654 ThrowNullPointerExceptionForFieldAccess(self, field, true);
655 } else {
656 return field->Get64(obj);
657 }
658 }
659 return 0; // Will throw exception by checking with Thread::Current
660}
661
662extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
663 const Method* referrer, Thread* self, Method** sp) {
664 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
665 if (LIKELY(field != NULL && obj != NULL)) {
666 return field->GetObj(obj);
667 }
668 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
669 field = FindFieldFromCode(field_idx, referrer, self, false, false, false, sizeof(Object*));
670 if (LIKELY(field != NULL)) {
671 if (UNLIKELY(obj == NULL)) {
672 ThrowNullPointerExceptionForFieldAccess(self, field, true);
673 } else {
674 return field->GetObj(obj);
675 }
676 }
677 return NULL; // Will throw exception by checking with Thread::Current
678}
679
680extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
681 const Method* referrer, Thread* self, Method** sp) {
682 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
683 if (LIKELY(field != NULL)) {
684 field->Set32(NULL, new_value);
685 return 0; // success
686 }
687 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
688 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int32_t));
689 if (LIKELY(field != NULL)) {
690 field->Set32(NULL, new_value);
691 return 0; // success
692 }
693 return -1; // failure
694}
695
696extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
697 uint64_t new_value, Thread* self, Method** sp) {
698 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
699 if (LIKELY(field != NULL)) {
700 field->Set64(NULL, new_value);
701 return 0; // success
702 }
703 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
704 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int64_t));
705 if (LIKELY(field != NULL)) {
706 field->Set64(NULL, new_value);
707 return 0; // success
708 }
709 return -1; // failure
710}
711
712extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
713 const Method* referrer, Thread* self, Method** sp) {
714 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
715 if (LIKELY(field != NULL)) {
716 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
717 field->SetObj(NULL, new_value);
718 return 0; // success
719 }
720 }
721 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
722 field = FindFieldFromCode(field_idx, referrer, self, true, false, true, sizeof(Object*));
723 if (LIKELY(field != NULL)) {
724 field->SetObj(NULL, new_value);
725 return 0; // success
726 }
727 return -1; // failure
728}
729
730extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
731 const Method* referrer, Thread* self, Method** sp) {
732 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
733 if (LIKELY(field != NULL && obj != NULL)) {
734 field->Set32(obj, new_value);
735 return 0; // success
736 }
737 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
738 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int32_t));
739 if (LIKELY(field != NULL)) {
740 if (UNLIKELY(obj == NULL)) {
741 ThrowNullPointerExceptionForFieldAccess(self, field, false);
742 } else {
743 field->Set32(obj, new_value);
744 return 0; // success
745 }
746 }
747 return -1; // failure
748}
749
750extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
751 Thread* self, Method** sp) {
752 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
753 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
754 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
755 if (LIKELY(field != NULL && obj != NULL)) {
756 field->Set64(obj, new_value);
757 return 0; // success
758 }
759 *sp = callee_save;
760 self->SetTopOfStack(sp, 0);
761 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int64_t));
762 if (LIKELY(field != NULL)) {
763 if (UNLIKELY(obj == NULL)) {
764 ThrowNullPointerExceptionForFieldAccess(self, field, false);
765 } else {
766 field->Set64(obj, new_value);
767 return 0; // success
768 }
769 }
770 return -1; // failure
771}
772
773extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
774 const Method* referrer, Thread* self, Method** sp) {
775 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
776 if (LIKELY(field != NULL && obj != NULL)) {
777 field->SetObj(obj, new_value);
778 return 0; // success
779 }
780 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
781 field = FindFieldFromCode(field_idx, referrer, self, false, false, true, sizeof(Object*));
782 if (LIKELY(field != NULL)) {
783 if (UNLIKELY(obj == NULL)) {
784 ThrowNullPointerExceptionForFieldAccess(self, field, false);
785 } else {
786 field->SetObj(obj, new_value);
787 return 0; // success
788 }
789 }
790 return -1; // failure
791}
792
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800793extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
794 Thread* self, Method** sp) {
795 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
796 return AllocObjectFromCode(type_idx, method, self, false);
797}
798
Ian Rogers28ad40d2011-10-27 15:19:26 -0700799extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
800 Thread* self, Method** sp) {
801 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800802 return AllocObjectFromCode(type_idx, method, self, true);
803}
804
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800805extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
806 Thread* self, Method** sp) {
807 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
808 return AllocArrayFromCode(type_idx, method, component_count, self, false);
809}
810
811extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
812 int32_t component_count,
813 Thread* self, Method** sp) {
814 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
815 return AllocArrayFromCode(type_idx, method, component_count, self, true);
816}
817
Ian Rogersce9eca62011-10-07 17:11:03 -0700818extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
819 int32_t component_count, Thread* self, Method** sp) {
820 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800821 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700822}
823
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800824extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
825 int32_t component_count,
826 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700827 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800828 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700829}
830
Ian Rogerscaab8c42011-10-12 12:11:18 -0700831// Assignable test for code, won't throw. Null and equality tests already performed
832uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
833 DCHECK(klass != NULL);
834 DCHECK(ref_class != NULL);
835 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
836}
837
Shih-wei Liao2d831012011-09-28 22:06:53 -0700838// 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 -0700839extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700840 DCHECK(a->IsClass()) << PrettyClass(a);
841 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700842 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700843 return 0; // Success
844 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700845 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700846 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700847 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800848 PrettyDescriptor(a).c_str(),
849 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700850 return -1; // Failure
851 }
852}
853
854// Tests whether 'element' can be assigned into an array of type 'array_class'.
855// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700856extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
857 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700858 DCHECK(array_class != NULL);
859 // element can't be NULL as we catch this is screened in runtime_support
860 Class* element_class = element->GetClass();
861 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700862 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700863 return 0; // Success
864 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700865 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700866 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800867 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800868 PrettyDescriptor(element_class).c_str(),
869 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700870 return -1; // Failure
871 }
872}
873
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700874extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
875 Thread* self, Method** sp) {
876 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800877 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700878}
879
Ian Rogers28ad40d2011-10-27 15:19:26 -0700880extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
881 Method** sp) {
882 // Called when method->dex_cache_resolved_types_[] misses
883 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800884 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700885}
886
Ian Rogersb093c6b2011-10-31 16:19:55 -0700887extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
888 const Method* referrer, Thread* self,
889 Method** sp) {
890 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
891 // unpopulated
892 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800893 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700894}
895
Brian Carlstromaded5f72011-10-07 17:15:04 -0700896extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
897 Thread* self, Method** sp) {
898 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
899 return ResolveStringFromCode(referrer, string_idx);
900}
901
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700902extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
903 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700904 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700905 // MonitorExit may throw exception
906 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
907}
908
909extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
910 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
911 DCHECK(obj != NULL); // Assumed to have been checked before entry
912 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700913 DCHECK(thread->HoldsLock(obj));
914 // Only possible exception is NPE and is handled before entry
915 DCHECK(!thread->IsExceptionPending());
916}
917
Ian Rogers4a510d82011-10-09 14:30:24 -0700918void CheckSuspendFromCode(Thread* thread) {
919 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700920 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
921}
922
Ian Rogers4a510d82011-10-09 14:30:24 -0700923extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
924 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700925 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700926 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
927}
928
929/*
930 * Fill the array with predefined constant values, throwing exceptions if the array is null or
931 * not of sufficient length.
932 *
933 * NOTE: When dealing with a raw dex file, the data to be copied uses
934 * little-endian ordering. Require that oat2dex do any required swapping
935 * so this routine can get by with a memcpy().
936 *
937 * Format of the data:
938 * ushort ident = 0x0300 magic value
939 * ushort width width of each element in the table
940 * uint size number of elements in the table
941 * ubyte data[size*width] table of data values (may contain a single-byte
942 * padding at the end)
943 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700944extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
945 Thread* self, Method** sp) {
946 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700947 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700948 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700949 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
950 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700951 return -1; // Error
952 }
953 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
954 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700955 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700956 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
957 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700958 return -1; // Error
959 }
960 uint16_t width = table[1];
961 uint32_t size_in_bytes = size * width;
Ian Rogersa15e67d2012-02-28 13:51:55 -0800962 memcpy((char*)array + Array::DataOffset(width).Int32Value(), (char*)&table[4], size_in_bytes);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700963 return 0; // Success
964}
965
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -0800966static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
967 Thread* self, Method** sp, bool access_check, InvokeType type){
968 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
969 if (UNLIKELY(method == NULL)) {
970 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
971 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
972 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type);
973 return 0; // failure
974 }
975 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
976 if (UNLIKELY(method == NULL)) {
977 CHECK(self->IsExceptionPending());
978 return 0; // failure
979 }
980 }
981 DCHECK(!self->IsExceptionPending());
982 const void* code = method->GetCode();
983
984 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
985 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
986 uint64_t result = ((code_uint << 32) | method_uint);
987 return result;
988}
989
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800990// See comments in runtime_support_asm.S
991extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
992 Method* caller_method, Thread* self,
993 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800994 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800995}
996
997extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
998 Object* this_object,
999 Method* caller_method, Thread* self,
1000 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001001 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
1002}
1003
1004
1005extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1006 Object* this_object,
1007 Method* caller_method, Thread* self,
1008 Method** sp) {
1009 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
1010}
1011
1012extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1013 Object* this_object,
1014 Method* caller_method, Thread* self,
1015 Method** sp) {
1016 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001017}
1018
1019extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1020 Object* this_object,
1021 Method* caller_method, Thread* self,
1022 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001023 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001024}
1025
1026extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1027 Object* this_object,
1028 Method* caller_method, Thread* self,
1029 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001030 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001031}
1032
Ian Rogers466bb252011-10-14 03:29:56 -07001033static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1034 ScopedLocalRef<jclass> jlr_UTE_class(env,
1035 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1036 if (jlr_UTE_class.get() == NULL) {
1037 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1038 } else {
1039 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1040 "(Ljava/lang/Throwable;)V");
1041 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1042 ScopedLocalRef<jthrowable> jlr_UTE(env,
1043 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1044 jexception)));
1045 int rc = env->Throw(jlr_UTE.get());
1046 if (rc != JNI_OK) {
1047 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1048 }
1049 }
1050 CHECK(self->IsExceptionPending());
1051}
1052
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001053// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1054// which is responsible for recording callee save registers. We explicitly handlerize incoming
1055// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1056// the invocation handler which is a field within the proxy object receiver.
1057extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001058 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001059 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001060 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1061 DCHECK_EQ(*proxy_sp, proxy_method);
1062 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001063 // TODO: ARM specific
1064 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1065 // Start new JNI local reference state
1066 JNIEnvExt* env = self->GetJniEnv();
1067 ScopedJniEnvLocalRefState env_state(env);
1068 // Create local ref. copies of proxy method and the receiver
1069 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1070 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1071
Ian Rogers14b1b242011-10-11 18:54:34 -07001072 // Placing into local references incoming arguments from the caller's register arguments,
1073 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001074 MethodHelper proxy_mh(proxy_method);
1075 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001076 size_t args_in_regs = 0;
1077 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001078 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001079 if (args_in_regs > 2) {
1080 args_in_regs = 2;
1081 break;
1082 }
1083 }
1084 size_t cur_arg = 0; // current stack location to read
1085 size_t param_index = 1; // skip receiver
1086 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001087 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001088 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001089 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001090 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001091 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001092 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001093 param_index++;
1094 }
1095 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001096 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001097 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001098 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001099 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1100 jobject jobj = AddLocalReference<jobject>(env, obj);
1101 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001102 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001103 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001104 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001105 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001106 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1107 jvalue args_jobj[3];
1108 args_jobj[0].l = rcvr_jobj;
1109 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001110 // Args array, if no arguments then NULL (don't include receiver in argument count)
1111 args_jobj[2].l = NULL;
1112 ObjectArray<Object>* args = NULL;
1113 if ((num_params - 1) > 0) {
1114 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001115 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001116 CHECK(self->IsExceptionPending());
1117 return;
1118 }
1119 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1120 }
1121 // Convert proxy method into expected interface method
1122 Method* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers19846512012-02-24 11:42:47 -08001123 DCHECK(interface_method != NULL);
1124 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001125 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001126 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001127 cur_arg = 0; // reset stack location to read to start
1128 // reset index, will index into param type array which doesn't include the receiver
1129 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001130 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers19846512012-02-24 11:42:47 -08001131 DCHECK(param_types != NULL);
Ian Rogers14b1b242011-10-11 18:54:34 -07001132 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
Ian Rogers19846512012-02-24 11:42:47 -08001133 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001134 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1135 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001136 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001137 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001138 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001139 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001140 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1141 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1142 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001143 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001144 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001145 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001146 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001147 if (self->IsExceptionPending()) {
1148 return;
1149 }
1150 obj = val.l;
1151 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001152 args->Set(param_index, obj);
1153 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1154 param_index++;
1155 }
1156 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001157 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1158 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001159 Class* param_type = param_types->Get(param_index);
1160 Object* obj;
1161 if (!param_type->IsPrimitive()) {
1162 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1163 } else {
1164 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001165 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001166 if (self->IsExceptionPending()) {
1167 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001168 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001169 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001170 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001171 args->Set(param_index, obj);
1172 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1173 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001174 }
1175 // Get the InvocationHandler method and the field that holds it within the Proxy object
1176 static jmethodID inv_hand_invoke_mid = NULL;
1177 static jfieldID proxy_inv_hand_fid = NULL;
1178 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001179 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001180 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001181 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001182 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1183 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1184 }
Ian Rogers466bb252011-10-14 03:29:56 -07001185 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001186 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1187 // Call InvocationHandler.invoke
1188 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1189 // Place result in stack args
1190 if (!self->IsExceptionPending()) {
1191 Object* result_ref = self->DecodeJObject(result);
1192 if (result_ref != NULL) {
1193 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001194 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001195 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1196 } else {
1197 *reinterpret_cast<jobject*>(stack_args) = NULL;
1198 }
Ian Rogers466bb252011-10-14 03:29:56 -07001199 } else {
1200 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1201 // a UndeclaredThrowableException.
1202 Throwable* exception = self->GetException();
1203 self->ClearException();
1204 if (!exception->IsCheckedException()) {
1205 self->SetException(exception);
1206 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001207 SynthesizedProxyClass* proxy_class =
1208 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1209 int throws_index = -1;
1210 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1211 for (size_t i = 0; i < num_virt_methods; i++) {
1212 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1213 throws_index = i;
1214 break;
1215 }
1216 }
1217 CHECK_NE(throws_index, -1);
1218 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001219 Class* exception_class = exception->GetClass();
1220 bool declares_exception = false;
1221 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1222 Class* declared_exception = declared_exceptions->Get(i);
1223 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1224 }
1225 if (declares_exception) {
1226 self->SetException(exception);
1227 } else {
1228 ThrowNewUndeclaredThrowableException(self, env, exception);
1229 }
1230 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001231 }
1232}
1233
jeffhaoe343b762011-12-05 16:36:44 -08001234extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001235 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001236 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1237 self->PushTraceStackFrame(trace_frame);
1238
jeffhao2692b572011-12-16 15:42:28 -08001239 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001240
jeffhao2692b572011-12-16 15:42:28 -08001241 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001242}
1243
1244extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001245 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001246 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1247 Method* method = trace_frame.method_;
1248 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001249
jeffhao2692b572011-12-16 15:42:28 -08001250 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001251
1252 return lr;
1253}
1254
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001255uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001256 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001257 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1258 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001259 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001260
jeffhao2692b572011-12-16 15:42:28 -08001261 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001262
1263 return lr;
1264}
1265
Ian Rogersa433b2e2012-03-09 08:40:33 -08001266int artCmplFloat(float a, float b) {
1267 if (a == b) {
1268 return 0;
1269 } else if (a < b) {
1270 return -1;
1271 } else if (a > b) {
1272 return 1;
1273 }
1274 return -1;
1275}
1276
1277int artCmpgFloat(float a, float b) {
1278 if (a == b) {
1279 return 0;
1280 } else if (a < b) {
1281 return -1;
1282 } else if (a > b) {
1283 return 1;
1284 }
1285 return 1;
1286}
1287
1288int artCmpgDouble(double a, double b) {
1289 if (a == b) {
1290 return 0;
1291 } else if (a < b) {
1292 return -1;
1293 } else if (a > b) {
1294 return 1;
1295 }
1296 return 1;
1297}
1298
1299int artCmplDouble(double a, double b) {
1300 if (a == b) {
1301 return 0;
1302 } else if (a < b) {
1303 return -1;
1304 } else if (a > b) {
1305 return 1;
1306 }
1307 return -1;
1308}
1309
Shih-wei Liao2d831012011-09-28 22:06:53 -07001310/*
1311 * Float/double conversion requires clamping to min and max of integer form. If
1312 * target doesn't support this normally, use these.
1313 */
1314int64_t D2L(double d) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001315 static const double kMaxLong = (double) (int64_t) 0x7fffffffffffffffULL;
1316 static const double kMinLong = (double) (int64_t) 0x8000000000000000ULL;
1317 if (d >= kMaxLong) {
1318 return (int64_t) 0x7fffffffffffffffULL;
1319 } else if (d <= kMinLong) {
1320 return (int64_t) 0x8000000000000000ULL;
1321 } else if (d != d) { // NaN case
1322 return 0;
1323 } else {
1324 return (int64_t) d;
1325 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001326}
1327
1328int64_t F2L(float f) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001329 static const float kMaxLong = (float) (int64_t) 0x7fffffffffffffffULL;
1330 static const float kMinLong = (float) (int64_t) 0x8000000000000000ULL;
1331 if (f >= kMaxLong) {
1332 return (int64_t) 0x7fffffffffffffffULL;
1333 } else if (f <= kMinLong) {
1334 return (int64_t) 0x8000000000000000ULL;
1335 } else if (f != f) { // NaN case
1336 return 0;
1337 } else {
1338 return (int64_t) f;
1339 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001340}
1341
1342} // namespace art