blob: af8ce22087543ed22ec872a0c02a728a9602049f [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 -080032// Place a special frame at the TOS that will save the callee saves for the given type
33static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
34 // Be aware the store below may well stomp on an incoming argument
35 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
36 self->SetTopOfStack(sp, 0);
37}
38
buzbee44b412b2012-02-04 08:50:53 -080039/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080040 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -080041 * the method. However, because the offset alone cannot distinguish between
42 * method entry and offset 0 within the method, we'll use an offset of -1
43 * to denote method entry.
44 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080045extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -080046 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080047 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -080048}
49
Shih-wei Liao2d831012011-09-28 22:06:53 -070050// Temporary debugging hook for compiler.
51extern void DebugMe(Method* method, uint32_t info) {
52 LOG(INFO) << "DebugMe";
53 if (method != NULL) {
54 LOG(INFO) << PrettyMethod(method);
55 }
56 LOG(INFO) << "Info: " << info;
57}
58
59// Return value helper for jobject return types
60extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -070061 if (thread->IsExceptionPending()) {
62 return NULL;
63 }
Shih-wei Liao2d831012011-09-28 22:06:53 -070064 return thread->DecodeJObject(obj);
65}
66
Ian Rogers60db5ab2012-02-20 17:02:00 -080067extern void* FindNativeMethod(Thread* self) {
68 DCHECK(Thread::Current() == self);
Shih-wei Liao2d831012011-09-28 22:06:53 -070069
Ian Rogers60db5ab2012-02-20 17:02:00 -080070 Method* method = const_cast<Method*>(self->GetCurrentMethod());
Shih-wei Liao2d831012011-09-28 22:06:53 -070071 DCHECK(method != NULL);
72
73 // Lookup symbol address for method, on failure we'll return NULL with an
74 // exception set, otherwise we return the address of the method we found.
Ian Rogers60db5ab2012-02-20 17:02:00 -080075 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -070076 if (native_code == NULL) {
Ian Rogers60db5ab2012-02-20 17:02:00 -080077 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -070078 return NULL;
79 } else {
80 // Register so that future calls don't come here
Ian Rogers60db5ab2012-02-20 17:02:00 -080081 method->RegisterNative(self, native_code);
Shih-wei Liao2d831012011-09-28 22:06:53 -070082 return native_code;
83 }
84}
85
86// Called by generated call to throw an exception
87extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
88 /*
89 * exception may be NULL, in which case this routine should
90 * throw NPE. NOTE: this is a convenience for generated code,
91 * which previously did the null check inline and constructed
92 * and threw a NPE if NULL. This routine responsible for setting
93 * exception_ in thread and delivering the exception.
94 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -070095 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -070096 if (exception == NULL) {
97 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
98 } else {
99 thread->SetException(exception);
100 }
101 thread->DeliverException();
102}
103
104// Deliver an exception that's pending on thread helping set up a callee save frame on the way
105extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700106 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700107 thread->DeliverException();
108}
109
110// Called by generated call to throw a NPE exception
Ian Rogers98d39882012-03-15 01:42:12 -0700111extern "C" void artThrowNullPointerExceptionFromCode(Thread* self, Method** sp) {
112 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
113 Frame fr = self->GetTopOfStack();
114 uintptr_t throw_native_pc = fr.GetReturnPC();
115 fr.Next();
116 Method* throw_method = fr.GetMethod();
117 uint32_t dex_pc = throw_method->ToDexPC(throw_native_pc - 2);
118 const DexFile::CodeItem* code = MethodHelper(throw_method).GetCodeItem();
119 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
120 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
121 DecodedInstruction dec_insn(instr);
122 switch (instr->Opcode()) {
123 case Instruction::INVOKE_DIRECT:
124 case Instruction::INVOKE_DIRECT_RANGE:
125 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kDirect);
126 break;
127 case Instruction::INVOKE_VIRTUAL:
128 case Instruction::INVOKE_VIRTUAL_RANGE:
129 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kVirtual);
130 break;
131 case Instruction::IGET:
132 case Instruction::IGET_WIDE:
133 case Instruction::IGET_OBJECT:
134 case Instruction::IGET_BOOLEAN:
135 case Instruction::IGET_BYTE:
136 case Instruction::IGET_CHAR:
137 case Instruction::IGET_SHORT: {
138 Field* field =
139 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
140 ThrowNullPointerExceptionForFieldAccess(self, field, true /* read */);
141 break;
142 }
143 case Instruction::IPUT:
144 case Instruction::IPUT_WIDE:
145 case Instruction::IPUT_OBJECT:
146 case Instruction::IPUT_BOOLEAN:
147 case Instruction::IPUT_BYTE:
148 case Instruction::IPUT_CHAR:
149 case Instruction::IPUT_SHORT: {
150 Field* field =
151 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
152 ThrowNullPointerExceptionForFieldAccess(self, field, false /* write */);
153 break;
154 }
155 case Instruction::AGET:
156 case Instruction::AGET_WIDE:
157 case Instruction::AGET_OBJECT:
158 case Instruction::AGET_BOOLEAN:
159 case Instruction::AGET_BYTE:
160 case Instruction::AGET_CHAR:
161 case Instruction::AGET_SHORT:
162 self->ThrowNewException("Ljava/lang/NullPointerException;",
163 "Attempt to read from null array");
164 break;
165 case Instruction::APUT:
166 case Instruction::APUT_WIDE:
167 case Instruction::APUT_OBJECT:
168 case Instruction::APUT_BOOLEAN:
169 case Instruction::APUT_BYTE:
170 case Instruction::APUT_CHAR:
171 case Instruction::APUT_SHORT:
172 self->ThrowNewException("Ljava/lang/NullPointerException;",
173 "Attempt to write to null array");
174 break;
175 default: {
176 const DexFile& dex_file = Runtime::Current()->GetClassLinker()
177 ->FindDexFile(throw_method->GetDeclaringClass()->GetDexCache());
178 std::string message("Null pointer exception during instruction '");
179 message += instr->DumpString(&dex_file);
180 message += "'";
181 self->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str());
182 break;
183 }
184 }
185 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700186}
187
188// Called by generated call to throw an arithmetic divide by zero exception
189extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700190 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700191 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
192 thread->DeliverException();
193}
194
195// Called by generated call to throw an arithmetic divide by zero exception
196extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700197 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
198 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
199 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700200 thread->DeliverException();
201}
202
203// Called by the AbstractMethodError stub (not runtime support)
204extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700205 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
206 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
207 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700208 thread->DeliverException();
209}
210
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700211extern "C" void artThrowStackOverflowFromCode(Method* /*method*/, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700212 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800213 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800214 if (Runtime::Current()->IsMethodTracingActive()) {
Elliott Hughes0ece7b92012-03-09 18:14:40 -0800215 TraceMethodUnwindFromCode(thread);
jeffhaoe343b762011-12-05 16:36:44 -0800216 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700217 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700218 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
219 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700220 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700221 thread->ResetDefaultStackEnd(); // Return to default stack size
222 thread->DeliverException();
223}
224
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700225static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700226 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700227 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
228 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
229
230 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700231 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700232 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
233 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700234 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700235 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
236 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700237 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700238 type_idx = ref;
239 } else {
240 CHECK(false) << static_cast<int>(ref_type);
241 }
242
Ian Rogers0571d352011-11-03 19:51:38 -0700243 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700244 if (!access) {
245 return class_name;
246 }
247
248 std::string result;
249 result += "tried to access class ";
250 result += class_name;
251 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800252 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700253 return result;
254}
255
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700256static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700257 verifier::VerifyErrorRefType ref_type, bool access) {
258 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700259
260 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
261 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
262
263 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700264 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700265 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700266 if (!access) {
267 return class_name + "." + field_name;
268 }
269
270 std::string result;
271 result += "tried to access field ";
272 result += class_name + "." + field_name;
273 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700275 return result;
276}
277
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700278static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800279 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700280 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700281
282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
283 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
284
285 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700286 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700287 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700288 if (!access) {
289 return class_name + "." + method_name;
290 }
291
292 std::string result;
293 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700294 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700295 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700296 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800297 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700298 return result;
299}
300
301extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700302 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
303 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700304 frame.Next();
305 Method* method = frame.GetMethod();
306
Ian Rogersd81871c2011-10-03 13:57:23 -0700307 verifier::VerifyErrorRefType ref_type =
308 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700309
310 const char* exception_class = "Ljava/lang/VerifyError;";
311 std::string msg;
312
Ian Rogersd81871c2011-10-03 13:57:23 -0700313 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
314 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700315 exception_class = "Ljava/lang/NoClassDefFoundError;";
316 msg = ClassNameFromIndex(method, ref, ref_type, false);
317 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700318 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700319 exception_class = "Ljava/lang/NoSuchFieldError;";
320 msg = FieldNameFromIndex(method, ref, ref_type, false);
321 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700322 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700323 exception_class = "Ljava/lang/NoSuchMethodError;";
324 msg = MethodNameFromIndex(method, ref, ref_type, false);
325 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700326 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700327 exception_class = "Ljava/lang/IllegalAccessError;";
328 msg = ClassNameFromIndex(method, ref, ref_type, true);
329 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700330 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700331 exception_class = "Ljava/lang/IllegalAccessError;";
332 msg = FieldNameFromIndex(method, ref, ref_type, true);
333 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700334 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700335 exception_class = "Ljava/lang/IllegalAccessError;";
336 msg = MethodNameFromIndex(method, ref, ref_type, true);
337 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700338 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700339 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
340 msg = ClassNameFromIndex(method, ref, ref_type, false);
341 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700342 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700343 exception_class = "Ljava/lang/InstantiationError;";
344 msg = ClassNameFromIndex(method, ref, ref_type, false);
345 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700346 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700347 // Generic VerifyError; use default exception, no message.
348 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700349 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700350 CHECK(false);
351 break;
352 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700353 self->ThrowNewException(exception_class, msg.c_str());
354 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700355}
356
357extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700358 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700359 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700360 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700361 thread->DeliverException();
362}
363
364extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700365 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700366 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700367 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700368 thread->DeliverException();
369}
370
Elliott Hughese1410a22011-10-04 12:10:24 -0700371extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700372 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
373 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700374 frame.Next();
375 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700376 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700378 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700379}
380
381extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700382 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700383 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700384 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700385 thread->DeliverException();
386}
387
Ian Rogers19846512012-02-24 11:42:47 -0800388const void* UnresolvedDirectMethodTrampolineFromCode(Method* called, Method** sp, Thread* thread,
389 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700390 // TODO: this code is specific to ARM
391 // On entry the stack pointed by sp is:
392 // | argN | |
393 // | ... | |
394 // | arg4 | |
395 // | arg3 spill | | Caller's frame
396 // | arg2 spill | |
397 // | arg1 spill | |
398 // | Method* | ---
399 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700400 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700401 // | R3 | arg3
402 // | R2 | arg2
403 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700404 // | R0 |
405 // | Method* | <- sp
406 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
407 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
408 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
409 uintptr_t caller_pc = regs[10];
410 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700411 // Start new JNI local reference state
412 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700413 ScopedJniEnvLocalRefState env_state(env);
Ian Rogers19846512012-02-24 11:42:47 -0800414
415 // Compute details about the called method (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700416 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers19846512012-02-24 11:42:47 -0800417 Method* caller = *caller_sp;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700418 bool is_static;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800419 bool is_virtual;
Ian Rogers19846512012-02-24 11:42:47 -0800420 uint32_t dex_method_idx;
421 const char* shorty;
422 uint32_t shorty_len;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700423 if (type == Runtime::kUnknownMethod) {
Ian Rogers19846512012-02-24 11:42:47 -0800424 DCHECK(called->IsRuntimeMethod());
Ian Rogersdf9a7822011-10-11 16:53:22 -0700425 // less two as return address may span into next dex instruction
426 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800427 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700428 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
429 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700430 Instruction::Code instr_code = instr->Opcode();
431 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
432 (instr_code == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800433 is_virtual = (instr_code == Instruction::INVOKE_VIRTUAL) ||
Ian Rogers2ed3b952012-03-17 11:49:39 -0700434 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE) ||
435 (instr_code == Instruction::INVOKE_SUPER) ||
436 (instr_code == Instruction::INVOKE_SUPER_RANGE);
437 DCHECK(is_static || is_virtual || (instr_code == Instruction::INVOKE_DIRECT) ||
438 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Elliott Hughesadb8c672012-03-06 16:49:32 -0800439 DecodedInstruction dec_insn(instr);
440 dex_method_idx = dec_insn.vB;
Ian Rogers19846512012-02-24 11:42:47 -0800441 shorty = linker->MethodShorty(dex_method_idx, caller, &shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700442 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800443 DCHECK(!called->IsRuntimeMethod());
Ian Rogersea2a11d2011-10-11 16:48:51 -0700444 is_static = type == Runtime::kStaticMethod;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800445 is_virtual = false;
Ian Rogers19846512012-02-24 11:42:47 -0800446 dex_method_idx = called->GetDexMethodIndex();
447 MethodHelper mh(called);
448 shorty = mh.GetShorty();
449 shorty_len = mh.GetShortyLength();
450 }
451 // Discover shorty (avoid GCs)
452 size_t args_in_regs = 0;
453 for (size_t i = 1; i < shorty_len; i++) {
454 char c = shorty[i];
455 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
456 if (args_in_regs > 3) {
457 args_in_regs = 3;
458 break;
459 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700460 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700461 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700462 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700463 if (!is_static) {
464 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
465 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700466 if (args_in_regs < 3) {
467 // If we thought we had fewer than 3 arguments in registers, account for the receiver
468 args_in_regs++;
469 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700470 AddLocalReference<jobject>(env, obj);
471 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700472 size_t shorty_index = 1; // skip return value
473 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
474 // R0)
475 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
476 char c = shorty[shorty_index];
477 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700478 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700479 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
480 AddLocalReference<jobject>(env, obj);
481 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700482 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
483 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700484 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700485 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700486 while (shorty_index < shorty_len) {
487 char c = shorty[shorty_index];
488 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700489 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700490 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700491 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700492 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700493 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700494 }
495 // Resolve method filling in dex cache
Ian Rogers19846512012-02-24 11:42:47 -0800496 if (type == Runtime::kUnknownMethod) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800497 called = linker->ResolveMethod(dex_method_idx, caller, !is_virtual);
Ian Rogers19846512012-02-24 11:42:47 -0800498 }
499 const void* code = NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700500 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800501 if (LIKELY(called->IsDirect() == !is_virtual)) {
Ian Rogers19846512012-02-24 11:42:47 -0800502 // Ensure that the called method's class is initialized.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800503 Class* called_class = called->GetDeclaringClass();
504 linker->EnsureInitialized(called_class, true);
505 if (LIKELY(called_class->IsInitialized())) {
Ian Rogers19846512012-02-24 11:42:47 -0800506 code = called->GetCode();
507 } else if (called_class->IsInitializing()) {
Ian Rogers2ed3b952012-03-17 11:49:39 -0700508 if (is_static) {
509 // Class is still initializing, go to oat and grab code (trampoline must be left in place
510 // until class is initialized to stop races between threads).
511 code = linker->GetOatCodeFor(called);
512 } else {
513 // No trampoline for non-static methods.
514 code = called->GetCode();
515 }
Ian Rogers19846512012-02-24 11:42:47 -0800516 } else {
517 DCHECK(called_class->IsErroneous());
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800518 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800519 } else {
520 // Direct method has been made virtual
521 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
522 "Expected direct method but found virtual: %s",
523 PrettyMethod(called, true).c_str());
524 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700525 }
Ian Rogers19846512012-02-24 11:42:47 -0800526 if (UNLIKELY(code == NULL)) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700527 // Something went wrong in ResolveMethod or EnsureInitialized,
528 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700529 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700530 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700531 thread->ClearException();
532 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800533 // Expect class to at least be initializing.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800534 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogers19846512012-02-24 11:42:47 -0800535 // Don't want infinite recursion.
536 DCHECK(code != Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod)->GetData());
Ian Rogersad25ac52011-10-04 19:13:33 -0700537 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700538 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700539 }
540 return code;
541}
542
Ian Rogers60db5ab2012-02-20 17:02:00 -0800543static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
544 intptr_t value = *arg_ptr;
545 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
546 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
Elliott Hughes88c5c352012-03-15 18:49:48 -0700547 CHECK(Runtime::Current()->GetHeap()->IsHeapAddress(value_as_work_around_rep)) << value_as_work_around_rep;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800548 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
549}
550
551extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
552 DCHECK(Thread::Current() == self);
553 // TODO: this code is specific to ARM
554 // On entry the stack pointed by sp is:
555 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
556 // | LR |
557 // | R3 | arg2
558 // | R2 | arg1
559 // | R1 | jclass/jobject
560 // | R0 | JNIEnv
561 // | unused |
562 // | unused |
563 // | unused | <- sp
564 Method* jni_method = self->GetTopOfStack().GetMethod();
565 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
566 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
567 // Fix up this/jclass argument
568 WorkAroundJniBugsForJobject(arg_ptr);
569 arg_ptr++;
570 // Fix up jobject arguments
571 MethodHelper mh(jni_method);
572 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800573 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800574 char shorty_char = mh.GetShorty()[i];
575 if (shorty_char == 'L') {
576 WorkAroundJniBugsForJobject(arg_ptr);
577 }
578 if (shorty_char == 'J' || shorty_char == 'D') {
579 if (reg_num == 2) {
580 arg_ptr = sp + 8; // skip to out arguments
581 reg_num = -1;
582 } else if (reg_num == 3) {
583 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
584 reg_num = -1;
585 } else {
586 DCHECK(reg_num == -1);
587 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
588 arg_ptr += 3; // unaligned, pad and move through stack arguments
589 } else {
590 arg_ptr += 2; // aligned, move through stack arguments
591 }
592 }
593 } else {
594 if (reg_num == 2) {
595 arg_ptr++; // move through register arguments
596 reg_num++;
597 } else if (reg_num == 3) {
598 arg_ptr = sp + 8; // skip to outgoing stack arguments
599 reg_num = -1;
600 } else {
601 DCHECK(reg_num == -1);
602 arg_ptr++; // move through stack arguments
603 }
604 }
605 }
606 // Load expected destination, see Method::RegisterNative
Ian Rogers19846512012-02-24 11:42:47 -0800607 const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
608 if (UNLIKELY(code == NULL)) {
609 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
610 jni_method->RegisterNative(self, code);
611 }
612 return code;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800613}
614
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -0800615
616extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
617 Thread* self, Method** sp) {
618 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
619 if (LIKELY(field != NULL)) {
620 return field->Get32(NULL);
621 }
622 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
623 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int32_t));
624 if (LIKELY(field != NULL)) {
625 return field->Get32(NULL);
626 }
627 return 0; // Will throw exception by checking with Thread::Current
628}
629
630extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
631 Thread* self, Method** sp) {
632 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
633 if (LIKELY(field != NULL)) {
634 return field->Get64(NULL);
635 }
636 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
637 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int64_t));
638 if (LIKELY(field != NULL)) {
639 return field->Get64(NULL);
640 }
641 return 0; // Will throw exception by checking with Thread::Current
642}
643
644extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
645 Thread* self, Method** sp) {
646 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
647 if (LIKELY(field != NULL)) {
648 return field->GetObj(NULL);
649 }
650 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
651 field = FindFieldFromCode(field_idx, referrer, self, true, false, false, sizeof(Object*));
652 if (LIKELY(field != NULL)) {
653 return field->GetObj(NULL);
654 }
655 return NULL; // Will throw exception by checking with Thread::Current
656}
657
658extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
659 const Method* referrer, Thread* self, Method** sp) {
660 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
661 if (LIKELY(field != NULL && obj != NULL)) {
662 return field->Get32(obj);
663 }
664 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
665 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int32_t));
666 if (LIKELY(field != NULL)) {
667 if (UNLIKELY(obj == NULL)) {
668 ThrowNullPointerExceptionForFieldAccess(self, field, true);
669 } else {
670 return field->Get32(obj);
671 }
672 }
673 return 0; // Will throw exception by checking with Thread::Current
674}
675
676extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
677 const Method* referrer, Thread* self, Method** sp) {
678 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
679 if (LIKELY(field != NULL && obj != NULL)) {
680 return field->Get64(obj);
681 }
682 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
683 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int64_t));
684 if (LIKELY(field != NULL)) {
685 if (UNLIKELY(obj == NULL)) {
686 ThrowNullPointerExceptionForFieldAccess(self, field, true);
687 } else {
688 return field->Get64(obj);
689 }
690 }
691 return 0; // Will throw exception by checking with Thread::Current
692}
693
694extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
695 const Method* referrer, Thread* self, Method** sp) {
696 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
697 if (LIKELY(field != NULL && obj != NULL)) {
698 return field->GetObj(obj);
699 }
700 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
701 field = FindFieldFromCode(field_idx, referrer, self, false, false, false, sizeof(Object*));
702 if (LIKELY(field != NULL)) {
703 if (UNLIKELY(obj == NULL)) {
704 ThrowNullPointerExceptionForFieldAccess(self, field, true);
705 } else {
706 return field->GetObj(obj);
707 }
708 }
709 return NULL; // Will throw exception by checking with Thread::Current
710}
711
712extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
713 const Method* referrer, Thread* self, Method** sp) {
714 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
715 if (LIKELY(field != NULL)) {
716 field->Set32(NULL, new_value);
717 return 0; // success
718 }
719 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
720 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int32_t));
721 if (LIKELY(field != NULL)) {
722 field->Set32(NULL, new_value);
723 return 0; // success
724 }
725 return -1; // failure
726}
727
728extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
729 uint64_t new_value, Thread* self, Method** sp) {
730 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
731 if (LIKELY(field != NULL)) {
732 field->Set64(NULL, new_value);
733 return 0; // success
734 }
735 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
736 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int64_t));
737 if (LIKELY(field != NULL)) {
738 field->Set64(NULL, new_value);
739 return 0; // success
740 }
741 return -1; // failure
742}
743
744extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
745 const Method* referrer, Thread* self, Method** sp) {
746 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
747 if (LIKELY(field != NULL)) {
748 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
749 field->SetObj(NULL, new_value);
750 return 0; // success
751 }
752 }
753 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
754 field = FindFieldFromCode(field_idx, referrer, self, true, false, true, sizeof(Object*));
755 if (LIKELY(field != NULL)) {
756 field->SetObj(NULL, new_value);
757 return 0; // success
758 }
759 return -1; // failure
760}
761
762extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
763 const Method* referrer, Thread* self, Method** sp) {
764 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
765 if (LIKELY(field != NULL && obj != NULL)) {
766 field->Set32(obj, new_value);
767 return 0; // success
768 }
769 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
770 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int32_t));
771 if (LIKELY(field != NULL)) {
772 if (UNLIKELY(obj == NULL)) {
773 ThrowNullPointerExceptionForFieldAccess(self, field, false);
774 } else {
775 field->Set32(obj, new_value);
776 return 0; // success
777 }
778 }
779 return -1; // failure
780}
781
782extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
783 Thread* self, Method** sp) {
784 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
785 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
786 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
787 if (LIKELY(field != NULL && obj != NULL)) {
788 field->Set64(obj, new_value);
789 return 0; // success
790 }
791 *sp = callee_save;
792 self->SetTopOfStack(sp, 0);
793 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int64_t));
794 if (LIKELY(field != NULL)) {
795 if (UNLIKELY(obj == NULL)) {
796 ThrowNullPointerExceptionForFieldAccess(self, field, false);
797 } else {
798 field->Set64(obj, new_value);
799 return 0; // success
800 }
801 }
802 return -1; // failure
803}
804
805extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
806 const Method* referrer, Thread* self, Method** sp) {
807 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
808 if (LIKELY(field != NULL && obj != NULL)) {
809 field->SetObj(obj, new_value);
810 return 0; // success
811 }
812 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
813 field = FindFieldFromCode(field_idx, referrer, self, false, false, true, sizeof(Object*));
814 if (LIKELY(field != NULL)) {
815 if (UNLIKELY(obj == NULL)) {
816 ThrowNullPointerExceptionForFieldAccess(self, field, false);
817 } else {
818 field->SetObj(obj, new_value);
819 return 0; // success
820 }
821 }
822 return -1; // failure
823}
824
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800825extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
826 Thread* self, Method** sp) {
827 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
828 return AllocObjectFromCode(type_idx, method, self, false);
829}
830
Ian Rogers28ad40d2011-10-27 15:19:26 -0700831extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
832 Thread* self, Method** sp) {
833 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800834 return AllocObjectFromCode(type_idx, method, self, true);
835}
836
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800837extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
838 Thread* self, Method** sp) {
839 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
840 return AllocArrayFromCode(type_idx, method, component_count, self, false);
841}
842
843extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
844 int32_t component_count,
845 Thread* self, Method** sp) {
846 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
847 return AllocArrayFromCode(type_idx, method, component_count, self, true);
848}
849
Ian Rogersce9eca62011-10-07 17:11:03 -0700850extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
851 int32_t component_count, Thread* self, Method** sp) {
852 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800853 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700854}
855
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800856extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
857 int32_t component_count,
858 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700859 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800860 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700861}
862
Ian Rogerscaab8c42011-10-12 12:11:18 -0700863// Assignable test for code, won't throw. Null and equality tests already performed
864uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
865 DCHECK(klass != NULL);
866 DCHECK(ref_class != NULL);
867 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
868}
869
Shih-wei Liao2d831012011-09-28 22:06:53 -0700870// 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 -0700871extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700872 DCHECK(a->IsClass()) << PrettyClass(a);
873 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700874 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700875 return 0; // Success
876 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700877 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700878 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700879 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800880 PrettyDescriptor(a).c_str(),
881 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700882 return -1; // Failure
883 }
884}
885
886// Tests whether 'element' can be assigned into an array of type 'array_class'.
887// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700888extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
889 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700890 DCHECK(array_class != NULL);
891 // element can't be NULL as we catch this is screened in runtime_support
892 Class* element_class = element->GetClass();
893 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700894 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700895 return 0; // Success
896 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700897 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700898 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800899 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800900 PrettyDescriptor(element_class).c_str(),
901 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700902 return -1; // Failure
903 }
904}
905
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700906extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
907 Thread* self, Method** sp) {
908 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800909 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700910}
911
Ian Rogers28ad40d2011-10-27 15:19:26 -0700912extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
913 Method** sp) {
914 // Called when method->dex_cache_resolved_types_[] misses
915 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800916 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700917}
918
Ian Rogersb093c6b2011-10-31 16:19:55 -0700919extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
920 const Method* referrer, Thread* self,
921 Method** sp) {
922 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
923 // unpopulated
924 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800925 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700926}
927
Brian Carlstromaded5f72011-10-07 17:15:04 -0700928extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
929 Thread* self, Method** sp) {
930 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
931 return ResolveStringFromCode(referrer, string_idx);
932}
933
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700934extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
935 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700936 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700937 // MonitorExit may throw exception
938 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
939}
940
941extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
942 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
943 DCHECK(obj != NULL); // Assumed to have been checked before entry
944 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700945 DCHECK(thread->HoldsLock(obj));
946 // Only possible exception is NPE and is handled before entry
947 DCHECK(!thread->IsExceptionPending());
948}
949
Ian Rogers4a510d82011-10-09 14:30:24 -0700950void CheckSuspendFromCode(Thread* thread) {
951 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700952 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
953}
954
Ian Rogers4a510d82011-10-09 14:30:24 -0700955extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
956 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700957 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700958 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
959}
960
961/*
962 * Fill the array with predefined constant values, throwing exceptions if the array is null or
963 * not of sufficient length.
964 *
965 * NOTE: When dealing with a raw dex file, the data to be copied uses
966 * little-endian ordering. Require that oat2dex do any required swapping
967 * so this routine can get by with a memcpy().
968 *
969 * Format of the data:
970 * ushort ident = 0x0300 magic value
971 * ushort width width of each element in the table
972 * uint size number of elements in the table
973 * ubyte data[size*width] table of data values (may contain a single-byte
974 * padding at the end)
975 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700976extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
977 Thread* self, Method** sp) {
978 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700979 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700980 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700981 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
982 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700983 return -1; // Error
984 }
985 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
986 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700987 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700988 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
989 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700990 return -1; // Error
991 }
992 uint16_t width = table[1];
993 uint32_t size_in_bytes = size * width;
Ian Rogersa15e67d2012-02-28 13:51:55 -0800994 memcpy((char*)array + Array::DataOffset(width).Int32Value(), (char*)&table[4], size_in_bytes);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700995 return 0; // Success
996}
997
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -0800998static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
999 Thread* self, Method** sp, bool access_check, InvokeType type){
1000 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
1001 if (UNLIKELY(method == NULL)) {
1002 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1003 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
1004 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type);
1005 return 0; // failure
1006 }
1007 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
1008 if (UNLIKELY(method == NULL)) {
1009 CHECK(self->IsExceptionPending());
1010 return 0; // failure
1011 }
1012 }
1013 DCHECK(!self->IsExceptionPending());
1014 const void* code = method->GetCode();
1015
Elliott Hughes634eb2e2012-03-22 16:06:28 -07001016 // When we return, the caller will branch to this address, so it had better not be 0!
1017 CHECK(code != NULL) << PrettyMethod(method);
1018
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -08001019 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
1020 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1021 uint64_t result = ((code_uint << 32) | method_uint);
1022 return result;
1023}
1024
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001025// See comments in runtime_support_asm.S
1026extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1027 Method* caller_method, Thread* self,
1028 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001029 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001030}
1031
1032extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1033 Object* this_object,
1034 Method* caller_method, Thread* self,
1035 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001036 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
1037}
1038
1039
1040extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1041 Object* this_object,
1042 Method* caller_method, Thread* self,
1043 Method** sp) {
1044 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
1045}
1046
1047extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1048 Object* this_object,
1049 Method* caller_method, Thread* self,
1050 Method** sp) {
1051 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001052}
1053
1054extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1055 Object* this_object,
1056 Method* caller_method, Thread* self,
1057 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001058 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001059}
1060
1061extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1062 Object* this_object,
1063 Method* caller_method, Thread* self,
1064 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001065 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001066}
1067
Ian Rogers466bb252011-10-14 03:29:56 -07001068static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1069 ScopedLocalRef<jclass> jlr_UTE_class(env,
1070 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1071 if (jlr_UTE_class.get() == NULL) {
1072 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1073 } else {
1074 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1075 "(Ljava/lang/Throwable;)V");
1076 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1077 ScopedLocalRef<jthrowable> jlr_UTE(env,
1078 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1079 jexception)));
1080 int rc = env->Throw(jlr_UTE.get());
1081 if (rc != JNI_OK) {
1082 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1083 }
1084 }
1085 CHECK(self->IsExceptionPending());
1086}
1087
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001088// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1089// which is responsible for recording callee save registers. We explicitly handlerize incoming
1090// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1091// the invocation handler which is a field within the proxy object receiver.
1092extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001093 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001094 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001095 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1096 DCHECK_EQ(*proxy_sp, proxy_method);
1097 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001098 // TODO: ARM specific
1099 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1100 // Start new JNI local reference state
1101 JNIEnvExt* env = self->GetJniEnv();
1102 ScopedJniEnvLocalRefState env_state(env);
1103 // Create local ref. copies of proxy method and the receiver
1104 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1105 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1106
Ian Rogers14b1b242011-10-11 18:54:34 -07001107 // Placing into local references incoming arguments from the caller's register arguments,
1108 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001109 MethodHelper proxy_mh(proxy_method);
1110 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001111 size_t args_in_regs = 0;
1112 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001113 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001114 if (args_in_regs > 2) {
1115 args_in_regs = 2;
1116 break;
1117 }
1118 }
1119 size_t cur_arg = 0; // current stack location to read
1120 size_t param_index = 1; // skip receiver
1121 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001122 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001123 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001124 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001125 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001126 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001127 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001128 param_index++;
1129 }
1130 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001131 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001132 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001133 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001134 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1135 jobject jobj = AddLocalReference<jobject>(env, obj);
1136 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001137 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001138 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001139 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001140 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001141 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1142 jvalue args_jobj[3];
1143 args_jobj[0].l = rcvr_jobj;
1144 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001145 // Args array, if no arguments then NULL (don't include receiver in argument count)
1146 args_jobj[2].l = NULL;
1147 ObjectArray<Object>* args = NULL;
1148 if ((num_params - 1) > 0) {
1149 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001150 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001151 CHECK(self->IsExceptionPending());
1152 return;
1153 }
1154 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1155 }
1156 // Convert proxy method into expected interface method
1157 Method* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers19846512012-02-24 11:42:47 -08001158 DCHECK(interface_method != NULL);
1159 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001160 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001161 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001162 cur_arg = 0; // reset stack location to read to start
1163 // reset index, will index into param type array which doesn't include the receiver
1164 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001165 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers19846512012-02-24 11:42:47 -08001166 DCHECK(param_types != NULL);
Ian Rogers14b1b242011-10-11 18:54:34 -07001167 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
Ian Rogers19846512012-02-24 11:42:47 -08001168 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001169 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1170 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001171 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001172 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001173 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001174 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001175 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1176 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1177 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001178 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001179 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001180 }
Elliott Hughesdbac3092012-03-16 18:00:30 -07001181 BoxPrimitive(param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001182 if (self->IsExceptionPending()) {
1183 return;
1184 }
1185 obj = val.l;
1186 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001187 args->Set(param_index, obj);
1188 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1189 param_index++;
1190 }
1191 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001192 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1193 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001194 Class* param_type = param_types->Get(param_index);
1195 Object* obj;
1196 if (!param_type->IsPrimitive()) {
1197 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1198 } else {
1199 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Elliott Hughesdbac3092012-03-16 18:00:30 -07001200 BoxPrimitive(param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001201 if (self->IsExceptionPending()) {
1202 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001203 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001204 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001205 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001206 args->Set(param_index, obj);
1207 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1208 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001209 }
1210 // Get the InvocationHandler method and the field that holds it within the Proxy object
1211 static jmethodID inv_hand_invoke_mid = NULL;
1212 static jfieldID proxy_inv_hand_fid = NULL;
1213 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001214 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001215 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001216 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001217 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1218 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1219 }
Ian Rogers466bb252011-10-14 03:29:56 -07001220 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001221 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1222 // Call InvocationHandler.invoke
1223 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1224 // Place result in stack args
1225 if (!self->IsExceptionPending()) {
1226 Object* result_ref = self->DecodeJObject(result);
1227 if (result_ref != NULL) {
1228 JValue result_unboxed;
Elliott Hughesdbac3092012-03-16 18:00:30 -07001229 bool unboxed_okay = UnboxPrimitive(result_ref, proxy_mh.GetReturnType(), result_unboxed, "result");
Elliott Hughes051c9fc2012-03-21 12:24:55 -07001230 if (!unboxed_okay) {
1231 self->ClearException();
1232 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
1233 "Couldn't convert result of type %s to %s",
1234 PrettyTypeOf(result_ref).c_str(),
1235 PrettyDescriptor(proxy_mh.GetReturnType()).c_str());
1236 return;
1237 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001238 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1239 } else {
1240 *reinterpret_cast<jobject*>(stack_args) = NULL;
1241 }
Ian Rogers466bb252011-10-14 03:29:56 -07001242 } else {
1243 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1244 // a UndeclaredThrowableException.
1245 Throwable* exception = self->GetException();
1246 self->ClearException();
1247 if (!exception->IsCheckedException()) {
1248 self->SetException(exception);
1249 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001250 SynthesizedProxyClass* proxy_class =
1251 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1252 int throws_index = -1;
1253 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1254 for (size_t i = 0; i < num_virt_methods; i++) {
1255 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1256 throws_index = i;
1257 break;
1258 }
1259 }
1260 CHECK_NE(throws_index, -1);
1261 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001262 Class* exception_class = exception->GetClass();
1263 bool declares_exception = false;
1264 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1265 Class* declared_exception = declared_exceptions->Get(i);
1266 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1267 }
1268 if (declares_exception) {
1269 self->SetException(exception);
1270 } else {
1271 ThrowNewUndeclaredThrowableException(self, env, exception);
1272 }
1273 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001274 }
1275}
1276
jeffhaoe343b762011-12-05 16:36:44 -08001277extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001278 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001279 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1280 self->PushTraceStackFrame(trace_frame);
1281
jeffhao2692b572011-12-16 15:42:28 -08001282 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001283
jeffhao2692b572011-12-16 15:42:28 -08001284 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001285}
1286
1287extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001288 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001289 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1290 Method* method = trace_frame.method_;
1291 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001292
jeffhao2692b572011-12-16 15:42:28 -08001293 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001294
1295 return lr;
1296}
1297
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001298uint32_t TraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001299 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001300 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1301 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001302 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001303
jeffhao2692b572011-12-16 15:42:28 -08001304 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001305
1306 return lr;
1307}
1308
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001309int CmplFloat(float a, float b) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001310 if (a == b) {
1311 return 0;
1312 } else if (a < b) {
1313 return -1;
1314 } else if (a > b) {
1315 return 1;
1316 }
1317 return -1;
1318}
1319
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001320int CmpgFloat(float a, float b) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001321 if (a == b) {
1322 return 0;
1323 } else if (a < b) {
1324 return -1;
1325 } else if (a > b) {
1326 return 1;
1327 }
1328 return 1;
1329}
1330
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001331int CmpgDouble(double a, double b) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001332 if (a == b) {
1333 return 0;
1334 } else if (a < b) {
1335 return -1;
1336 } else if (a > b) {
1337 return 1;
1338 }
1339 return 1;
1340}
1341
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001342int CmplDouble(double a, double b) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001343 if (a == b) {
1344 return 0;
1345 } else if (a < b) {
1346 return -1;
1347 } else if (a > b) {
1348 return 1;
1349 }
1350 return -1;
1351}
1352
Shih-wei Liao2d831012011-09-28 22:06:53 -07001353/*
1354 * Float/double conversion requires clamping to min and max of integer form. If
1355 * target doesn't support this normally, use these.
1356 */
1357int64_t D2L(double d) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001358 static const double kMaxLong = (double) (int64_t) 0x7fffffffffffffffULL;
1359 static const double kMinLong = (double) (int64_t) 0x8000000000000000ULL;
1360 if (d >= kMaxLong) {
1361 return (int64_t) 0x7fffffffffffffffULL;
1362 } else if (d <= kMinLong) {
1363 return (int64_t) 0x8000000000000000ULL;
1364 } else if (d != d) { // NaN case
1365 return 0;
1366 } else {
1367 return (int64_t) d;
1368 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001369}
1370
1371int64_t F2L(float f) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001372 static const float kMaxLong = (float) (int64_t) 0x7fffffffffffffffULL;
1373 static const float kMinLong = (float) (int64_t) 0x8000000000000000ULL;
1374 if (f >= kMaxLong) {
1375 return (int64_t) 0x7fffffffffffffffULL;
1376 } else if (f <= kMinLong) {
1377 return (int64_t) 0x8000000000000000ULL;
1378 } else if (f != f) { // NaN case
1379 return 0;
1380 } else {
1381 return (int64_t) f;
1382 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001383}
1384
1385} // namespace art