blob: 1d0552709e6a4f930b33110127f61ff36c5c72d2 [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
Ian Rogerscaab8c42011-10-12 12:11:18 -070019#include "dex_cache.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070020#include "dex_verifier.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070021#include "macros.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object.h"
23#include "object_utils.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070024#include "reflection.h"
25#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070026
Shih-wei Liao2d831012011-09-28 22:06:53 -070027namespace art {
28
Ian Rogers4f0d07c2011-10-06 23:38:47 -070029// Place a special frame at the TOS that will save the callee saves for the given type
30static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070031 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070032 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
33 self->SetTopOfStack(sp, 0);
34}
35
Shih-wei Liao2d831012011-09-28 22:06:53 -070036// Temporary debugging hook for compiler.
37extern void DebugMe(Method* method, uint32_t info) {
38 LOG(INFO) << "DebugMe";
39 if (method != NULL) {
40 LOG(INFO) << PrettyMethod(method);
41 }
42 LOG(INFO) << "Info: " << info;
43}
44
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -070045extern "C" uint32_t artObjectInitFromCode(Object* o, Thread* self, Method** sp) {
46 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -070047 Class* c = o->GetClass();
48 if (UNLIKELY(c->IsFinalizable())) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -070049 Heap::AddFinalizerReference(self, o);
Ian Rogerscaab8c42011-10-12 12:11:18 -070050 }
51 /*
52 * NOTE: once debugger/profiler support is added, we'll need to check
53 * here and branch to actual compiled object.<init> to handle any
Ian Rogers0571d352011-11-03 19:51:38 -070054 * breakpoint/logging activities if either is active.
Ian Rogerscaab8c42011-10-12 12:11:18 -070055 */
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -070056 return self->IsExceptionPending() ? -1 : 0;
Ian Rogerscaab8c42011-10-12 12:11:18 -070057}
58
Shih-wei Liao2d831012011-09-28 22:06:53 -070059// 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
67extern void* FindNativeMethod(Thread* thread) {
68 DCHECK(Thread::Current() == thread);
69
70 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
71 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.
75 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
76 if (native_code == NULL) {
77 DCHECK(thread->IsExceptionPending());
78 return NULL;
79 } else {
80 // Register so that future calls don't come here
81 method->RegisterNative(native_code);
82 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
111extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700112 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700113 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700114 thread->DeliverException();
115}
116
117// Called by generated call to throw an arithmetic divide by zero exception
118extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700119 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700120 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
121 thread->DeliverException();
122}
123
124// Called by generated call to throw an arithmetic divide by zero exception
125extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700126 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
127 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
128 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700129 thread->DeliverException();
130}
131
132// Called by the AbstractMethodError stub (not runtime support)
133extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700134 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
135 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
136 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700137 thread->DeliverException();
138}
139
140extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700141 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700142 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700143 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
144 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700145 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700146 thread->ResetDefaultStackEnd(); // Return to default stack size
147 thread->DeliverException();
148}
149
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700150static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700151 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700152 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
153 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
154
155 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700156 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700157 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
158 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700159 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700160 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
161 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700162 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700163 type_idx = ref;
164 } else {
165 CHECK(false) << static_cast<int>(ref_type);
166 }
167
Ian Rogers0571d352011-11-03 19:51:38 -0700168 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700169 if (!access) {
170 return class_name;
171 }
172
173 std::string result;
174 result += "tried to access class ";
175 result += class_name;
176 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800177 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700178 return result;
179}
180
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700181static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700182 verifier::VerifyErrorRefType ref_type, bool access) {
183 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700184
185 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
186 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
187
188 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700189 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700190 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700191 if (!access) {
192 return class_name + "." + field_name;
193 }
194
195 std::string result;
196 result += "tried to access field ";
197 result += class_name + "." + field_name;
198 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800199 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700200 return result;
201}
202
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700203static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700204 verifier::VerifyErrorRefType ref_type, bool access) {
205 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700206
207 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
208 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
209
210 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700211 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700212 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700213 if (!access) {
214 return class_name + "." + method_name;
215 }
216
217 std::string result;
218 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700219 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700220 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700221 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700223 return result;
224}
225
226extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700227 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
228 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700229 frame.Next();
230 Method* method = frame.GetMethod();
231
Ian Rogersd81871c2011-10-03 13:57:23 -0700232 verifier::VerifyErrorRefType ref_type =
233 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700234
235 const char* exception_class = "Ljava/lang/VerifyError;";
236 std::string msg;
237
Ian Rogersd81871c2011-10-03 13:57:23 -0700238 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
239 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700240 exception_class = "Ljava/lang/NoClassDefFoundError;";
241 msg = ClassNameFromIndex(method, ref, ref_type, false);
242 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700243 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700244 exception_class = "Ljava/lang/NoSuchFieldError;";
245 msg = FieldNameFromIndex(method, ref, ref_type, false);
246 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700247 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700248 exception_class = "Ljava/lang/NoSuchMethodError;";
249 msg = MethodNameFromIndex(method, ref, ref_type, false);
250 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700251 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700252 exception_class = "Ljava/lang/IllegalAccessError;";
253 msg = ClassNameFromIndex(method, ref, ref_type, true);
254 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700255 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700256 exception_class = "Ljava/lang/IllegalAccessError;";
257 msg = FieldNameFromIndex(method, ref, ref_type, true);
258 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700259 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700260 exception_class = "Ljava/lang/IllegalAccessError;";
261 msg = MethodNameFromIndex(method, ref, ref_type, true);
262 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700263 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700264 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
265 msg = ClassNameFromIndex(method, ref, ref_type, false);
266 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700267 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700268 exception_class = "Ljava/lang/InstantiationError;";
269 msg = ClassNameFromIndex(method, ref, ref_type, false);
270 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700271 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700272 // Generic VerifyError; use default exception, no message.
273 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700274 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700275 CHECK(false);
276 break;
277 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700278 self->ThrowNewException(exception_class, msg.c_str());
279 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700280}
281
282extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700283 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700284 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700285 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700286 thread->DeliverException();
287}
288
289extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700290 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700291 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700292 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700293 thread->DeliverException();
294}
295
Elliott Hughese1410a22011-10-04 12:10:24 -0700296extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700297 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
298 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700299 frame.Next();
300 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700301 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700302 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700303 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700304}
305
306extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700307 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700308 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700309 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700310 thread->DeliverException();
311}
312
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700313void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700314 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700315 // TODO: this code is specific to ARM
316 // On entry the stack pointed by sp is:
317 // | argN | |
318 // | ... | |
319 // | arg4 | |
320 // | arg3 spill | | Caller's frame
321 // | arg2 spill | |
322 // | arg1 spill | |
323 // | Method* | ---
324 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700325 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700326 // | R3 | arg3
327 // | R2 | arg2
328 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700329 // | R0 |
330 // | Method* | <- sp
331 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
332 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
333 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
334 uintptr_t caller_pc = regs[10];
335 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700336 // Start new JNI local reference state
337 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700338 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersad25ac52011-10-04 19:13:33 -0700339 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700340 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700341 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
342 size_t shorty_len = strlen(shorty);
Ian Rogers14b1b242011-10-11 18:54:34 -0700343 size_t args_in_regs = 0;
344 for (size_t i = 1; i < shorty_len; i++) {
345 char c = shorty[i];
346 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
347 if (args_in_regs > 3) {
348 args_in_regs = 3;
349 break;
350 }
351 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700352 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700353 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700354 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700355 // less two as return address may span into next dex instruction
356 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800357 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700358 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
359 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700360 Instruction::Code instr_code = instr->Opcode();
361 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
362 (instr_code == Instruction::INVOKE_STATIC_RANGE);
363 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
364 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700365 } else {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700366 is_static = type == Runtime::kStaticMethod;
367 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700368 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700369 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700370 if (!is_static) {
371 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
372 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700373 if (args_in_regs < 3) {
374 // If we thought we had fewer than 3 arguments in registers, account for the receiver
375 args_in_regs++;
376 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700377 AddLocalReference<jobject>(env, obj);
378 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700379 size_t shorty_index = 1; // skip return value
380 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
381 // R0)
382 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
383 char c = shorty[shorty_index];
384 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700385 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700386 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
387 AddLocalReference<jobject>(env, obj);
388 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700389 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
390 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700391 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700392 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700393 while (shorty_index < shorty_len) {
394 char c = shorty[shorty_index];
395 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700396 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700397 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700398 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700399 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700400 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700401 }
402 // Resolve method filling in dex cache
403 Method* called = linker->ResolveMethod(method_idx, *caller_sp, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700404 if (LIKELY(!thread->IsExceptionPending())) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700405 // Update CodeAndDirectMethod table
406 Method* caller = *caller_sp;
407 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
408 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700409 // We got this far, ensure that the declaring class is initialized
410 linker->EnsureInitialized(called->GetDeclaringClass(), true);
411 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700412 void* code;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700413 if (UNLIKELY(thread->IsExceptionPending())) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700414 // Something went wrong in ResolveMethod or EnsureInitialized,
415 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700416 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700417 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700418 thread->ClearException();
419 } else {
420 // Expect class to at least be initializing
421 CHECK(called->GetDeclaringClass()->IsInitializing());
422 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700423 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700424 code = const_cast<void*>(called->GetCode());
425 }
426 return code;
427}
428
Ian Rogerscaab8c42011-10-12 12:11:18 -0700429// Fast path field resolution that can't throw exceptions
430static Field* FindFieldFast(uint32_t field_idx, const Method* referrer) {
431 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
432 if (UNLIKELY(resolved_field == NULL)) {
433 return NULL;
434 }
435 Class* fields_class = resolved_field->GetDeclaringClass();
436 // Check class is initilaized or initializing
437 if (UNLIKELY(!fields_class->IsInitializing())) {
438 return NULL;
439 }
440 return resolved_field;
441}
442
443// Slow path field resolution and declaring class initialization
Ian Rogersce9eca62011-10-07 17:11:03 -0700444Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static) {
445 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700446 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
447 if (LIKELY(resolved_field != NULL)) {
448 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogersce9eca62011-10-07 17:11:03 -0700449 // If the class is already initializing, we must be inside <clinit>, or
450 // we'd still be waiting for the lock.
Ian Rogerscaab8c42011-10-12 12:11:18 -0700451 if (fields_class->IsInitializing()) {
452 return resolved_field;
453 }
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700454 if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700455 return resolved_field;
Ian Rogersce9eca62011-10-07 17:11:03 -0700456 }
457 }
458 DCHECK(Thread::Current()->IsExceptionPending()); // Throw exception and unwind
459 return NULL;
460}
461
462extern "C" Field* artFindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer,
463 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700464 Field* resolved_field = FindFieldFast(field_idx, referrer);
465 if (UNLIKELY(resolved_field == NULL)) {
466 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
467 resolved_field = FindFieldFromCode(field_idx, referrer, false);
468 }
469 return resolved_field;
Ian Rogersce9eca62011-10-07 17:11:03 -0700470}
471
472extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
473 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700474 Field* field = FindFieldFast(field_idx, referrer);
475 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800476 FieldHelper fh(field);
477 if (LIKELY(fh.IsPrimitiveType() && fh.FieldSize() == sizeof(int32_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700478 return field->Get32(NULL);
479 }
480 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700481 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700482 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700483 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800484 FieldHelper fh(field);
485 if (!fh.IsPrimitiveType() || fh.FieldSize() != sizeof(int32_t)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700486 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
487 "Attempted read of 32-bit primitive on field '%s'",
488 PrettyField(field, true).c_str());
489 } else {
490 return field->Get32(NULL);
491 }
492 }
493 return 0; // Will throw exception by checking with Thread::Current
494}
495
496extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
497 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700498 Field* field = FindFieldFast(field_idx, referrer);
499 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800500 FieldHelper fh(field);
501 if (LIKELY(fh.IsPrimitiveType() && fh.FieldSize() == sizeof(int64_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700502 return field->Get64(NULL);
503 }
504 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700505 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700506 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700507 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 FieldHelper fh(field);
509 if (!fh.IsPrimitiveType() || fh.FieldSize() != sizeof(int64_t)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700510 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
511 "Attempted read of 64-bit primitive on field '%s'",
512 PrettyField(field, true).c_str());
513 } else {
514 return field->Get64(NULL);
515 }
516 }
517 return 0; // Will throw exception by checking with Thread::Current
518}
519
520extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
521 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700522 Field* field = FindFieldFast(field_idx, referrer);
523 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800524 FieldHelper fh(field);
525 if (LIKELY(!fh.IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700526 return field->GetObj(NULL);
527 }
528 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700529 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700530 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700531 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800532 FieldHelper fh(field);
533 if (fh.IsPrimitiveType()) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700534 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
535 "Attempted read of reference on primitive field '%s'",
536 PrettyField(field, true).c_str());
537 } else {
538 return field->GetObj(NULL);
539 }
540 }
541 return NULL; // Will throw exception by checking with Thread::Current
542}
543
544extern "C" int artSet32StaticFromCode(uint32_t field_idx, const Method* referrer,
545 uint32_t new_value, Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700546 Field* field = FindFieldFast(field_idx, referrer);
547 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800548 FieldHelper fh(field);
549 if (LIKELY(fh.IsPrimitiveType() && fh.FieldSize() == sizeof(int32_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700550 field->Set32(NULL, new_value);
551 return 0; // success
552 }
553 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700554 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700555 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700556 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800557 FieldHelper fh(field);
558 if (!fh.IsPrimitiveType() || fh.FieldSize() != sizeof(int32_t)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700559 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
560 "Attempted write of 32-bit primitive to field '%s'",
561 PrettyField(field, true).c_str());
562 } else {
563 field->Set32(NULL, new_value);
564 return 0; // success
565 }
566 }
567 return -1; // failure
568}
569
570extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
571 uint64_t new_value, Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700572 Field* field = FindFieldFast(field_idx, referrer);
573 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800574 FieldHelper fh(field);
575 if (LIKELY(fh.IsPrimitiveType() && fh.FieldSize() == sizeof(int64_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700576 field->Set64(NULL, new_value);
577 return 0; // success
578 }
579 }
580 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
581 field = FindFieldFromCode(field_idx, referrer, true);
582 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800583 FieldHelper fh(field);
584 if (UNLIKELY(!fh.IsPrimitiveType() || fh.FieldSize() != sizeof(int64_t))) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700585 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
586 "Attempted write of 64-bit primitive to field '%s'",
587 PrettyField(field, true).c_str());
588 } else {
589 field->Set64(NULL, new_value);
590 return 0; // success
591 }
592 }
593 return -1; // failure
594}
595
596extern "C" int artSetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
597 Object* new_value, Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700598 Field* field = FindFieldFast(field_idx, referrer);
599 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800600 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700601 field->SetObj(NULL, new_value);
602 return 0; // success
603 }
604 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700605 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700606 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700607 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800608 if (FieldHelper(field).IsPrimitiveType()) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700609 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
610 "Attempted write of reference to primitive field '%s'",
611 PrettyField(field, true).c_str());
612 } else {
613 field->SetObj(NULL, new_value);
614 return 0; // success
615 }
616 }
617 return -1; // failure
618}
619
Shih-wei Liao2d831012011-09-28 22:06:53 -0700620// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
621// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogerscaab8c42011-10-12 12:11:18 -0700622extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
623 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700624 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700625 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700626 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700627 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700628 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700629 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700630 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700631 return NULL; // Failure
632 }
633 }
buzbee33a129c2011-10-06 16:53:20 -0700634 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
635 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700636 return NULL; // Failure
637 }
638 return klass->AllocObject();
639}
640
Ian Rogers28ad40d2011-10-27 15:19:26 -0700641extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
642 Thread* self, Method** sp) {
643 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
644 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
645 Runtime* runtime = Runtime::Current();
646 if (UNLIKELY(klass == NULL)) {
647 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
648 if (klass == NULL) {
649 DCHECK(self->IsExceptionPending());
650 return NULL; // Failure
651 }
652 }
653 Class* referrer = method->GetDeclaringClass();
654 if (UNLIKELY(!referrer->CanAccess(klass))) {
655 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", "illegal class access: '%s' -> '%s'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800656 PrettyDescriptor(referrer).c_str(),
657 PrettyDescriptor(klass).c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700658 return NULL; // Failure
659 }
660 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
661 DCHECK(self->IsExceptionPending());
662 return NULL; // Failure
663 }
664 return klass->AllocObject();
buzbeecc4540e2011-10-27 13:06:03 -0700665}
666
Ian Rogersce9eca62011-10-07 17:11:03 -0700667Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
668 Thread* self) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700669 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700670 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700671 return NULL; // Failure
672 }
673 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700674 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700675 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
676 if (klass == NULL) { // Error
677 DCHECK(Thread::Current()->IsExceptionPending());
678 return NULL; // Failure
679 }
680 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700681 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700682 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700683 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700684 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800685 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700686 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700687 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700688 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800689 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700690 }
691 return NULL; // Failure
692 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700693 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700694 return Array::Alloc(klass, component_count);
695 }
696}
697
Ian Rogersce9eca62011-10-07 17:11:03 -0700698// Helper function to alloc array for OP_FILLED_NEW_ARRAY
699extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
700 int32_t component_count, Thread* self, Method** sp) {
701 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
702 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self);
703}
704
Shih-wei Liao2d831012011-09-28 22:06:53 -0700705// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
706// it cannot be resolved, throw an error. If it can, use it to create an array.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700707extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
708 Thread* self, Method** sp) {
709 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700710 if (UNLIKELY(component_count < 0)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700711 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700712 component_count);
713 return NULL; // Failure
714 }
715 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700716 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700717 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
718 if (klass == NULL) { // Error
719 DCHECK(Thread::Current()->IsExceptionPending());
720 return NULL; // Failure
721 }
722 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
723 }
724 return Array::Alloc(klass, component_count);
725}
726
Ian Rogerscaab8c42011-10-12 12:11:18 -0700727// Assignable test for code, won't throw. Null and equality tests already performed
728uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
729 DCHECK(klass != NULL);
730 DCHECK(ref_class != NULL);
731 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
732}
733
Shih-wei Liao2d831012011-09-28 22:06:53 -0700734// 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 -0700735extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700736 DCHECK(a->IsClass()) << PrettyClass(a);
737 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700738 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700739 return 0; // Success
740 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700741 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700742 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700743 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800744 PrettyDescriptor(a).c_str(),
745 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700746 return -1; // Failure
747 }
748}
749
750// Tests whether 'element' can be assigned into an array of type 'array_class'.
751// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700752extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
753 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700754 DCHECK(array_class != NULL);
755 // element can't be NULL as we catch this is screened in runtime_support
756 Class* element_class = element->GetClass();
757 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700758 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700759 return 0; // Success
760 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700761 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700762 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700763 "Cannot store an object of type %s in to an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800764 PrettyDescriptor(element_class).c_str(),
765 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700766 return -1; // Failure
767 }
768}
769
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700770Class* InitializeStaticStorage(uint32_t type_idx, const Method* referrer, Thread* self) {
771 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
772 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700773 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700774 CHECK(self->IsExceptionPending());
775 return NULL; // Failure - Indicate to caller to deliver exception
776 }
Ian Rogersb093c6b2011-10-31 16:19:55 -0700777 DCHECK(referrer->GetDeclaringClass()->CanAccess(klass));
778 // If we are the <clinit> of this class, just return our storage.
779 //
780 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
781 // running.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800782 if (klass == referrer->GetDeclaringClass() && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogersb093c6b2011-10-31 16:19:55 -0700783 return klass;
784 }
785 if (!class_linker->EnsureInitialized(klass, true)) {
786 CHECK(self->IsExceptionPending());
787 return NULL; // Failure - Indicate to caller to deliver exception
788 }
789 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
790 return klass;
791}
792
793Class* InitializeStaticStorageAndVerifyAccess(uint32_t type_idx, const Method* referrer,
794 Thread* self) {
795 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
796 Class* klass = class_linker->ResolveType(type_idx, referrer);
797 if (UNLIKELY(klass == NULL)) {
798 CHECK(self->IsExceptionPending());
799 return NULL; // Failure - Indicate to caller to deliver exception
800 }
801 // Perform access check
802 if (UNLIKELY(!referrer->GetDeclaringClass()->CanAccess(klass))) {
803 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
804 "Class %s is inaccessible to method %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800805 PrettyDescriptor(klass).c_str(),
Ian Rogersb093c6b2011-10-31 16:19:55 -0700806 PrettyMethod(referrer, true).c_str());
807 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700808 // If we are the <clinit> of this class, just return our storage.
809 //
810 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
811 // running.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800812 if (klass == referrer->GetDeclaringClass() && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700813 return klass;
814 }
815 if (!class_linker->EnsureInitialized(klass, true)) {
816 CHECK(self->IsExceptionPending());
817 return NULL; // Failure - Indicate to caller to deliver exception
818 }
819 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
820 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700821}
822
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700823extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
824 Thread* self, Method** sp) {
825 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
826 return InitializeStaticStorage(type_idx, referrer, self);
827}
828
Ian Rogers28ad40d2011-10-27 15:19:26 -0700829extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
830 Method** sp) {
831 // Called when method->dex_cache_resolved_types_[] misses
832 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
833 return InitializeStaticStorage(type_idx, referrer, self);
834}
835
Ian Rogersb093c6b2011-10-31 16:19:55 -0700836extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
837 const Method* referrer, Thread* self,
838 Method** sp) {
839 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
840 // unpopulated
841 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
842 return InitializeStaticStorageAndVerifyAccess(type_idx, referrer, self);
843}
844
Ian Rogers28ad40d2011-10-27 15:19:26 -0700845// TODO: placeholder. Helper function to resolve virtual method
846void ResolveMethodFromCode(Method* method, uint32_t method_idx) {
847 /*
848 * Slow-path handler on invoke virtual method path in which
849 * base method is unresolved at compile-time. Doesn't need to
850 * return anything - just either ensure that
851 * method->dex_cache_resolved_methods_(method_idx) != NULL or
852 * throw and unwind. The caller will restart call sequence
853 * from the beginning.
854 */
855}
856
Brian Carlstromaded5f72011-10-07 17:15:04 -0700857String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
858 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
859 return class_linker->ResolveString(string_idx, referrer);
860}
861
862extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
863 Thread* self, Method** sp) {
864 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
865 return ResolveStringFromCode(referrer, string_idx);
866}
867
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700868extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
869 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700870 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700871 // MonitorExit may throw exception
872 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
873}
874
875extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
876 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
877 DCHECK(obj != NULL); // Assumed to have been checked before entry
878 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700879 DCHECK(thread->HoldsLock(obj));
880 // Only possible exception is NPE and is handled before entry
881 DCHECK(!thread->IsExceptionPending());
882}
883
Ian Rogers4a510d82011-10-09 14:30:24 -0700884void CheckSuspendFromCode(Thread* thread) {
885 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700886 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
887}
888
Ian Rogers4a510d82011-10-09 14:30:24 -0700889extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
890 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700891 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700892 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
893}
894
895/*
896 * Fill the array with predefined constant values, throwing exceptions if the array is null or
897 * not of sufficient length.
898 *
899 * NOTE: When dealing with a raw dex file, the data to be copied uses
900 * little-endian ordering. Require that oat2dex do any required swapping
901 * so this routine can get by with a memcpy().
902 *
903 * Format of the data:
904 * ushort ident = 0x0300 magic value
905 * ushort width width of each element in the table
906 * uint size number of elements in the table
907 * ubyte data[size*width] table of data values (may contain a single-byte
908 * padding at the end)
909 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700910extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
911 Thread* self, Method** sp) {
912 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700913 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700914 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700915 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
916 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700917 return -1; // Error
918 }
919 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
920 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700921 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700922 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
923 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700924 return -1; // Error
925 }
926 uint16_t width = table[1];
927 uint32_t size_in_bytes = size * width;
928 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
929 return 0; // Success
930}
931
932// See comments in runtime_support_asm.S
933extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx,
Ian Rogerscaab8c42011-10-12 12:11:18 -0700934 Object* this_object,
935 Method* caller_method,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700936 Thread* thread, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700937 Method* interface_method = caller_method->GetDexCacheResolvedMethods()->Get(method_idx);
938 Method* found_method = NULL; // The found method
939 if (LIKELY(interface_method != NULL && this_object != NULL)) {
Ian Rogersb04f69f2011-10-17 00:40:54 -0700940 found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, false);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700941 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700942 if (UNLIKELY(found_method == NULL)) {
943 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
944 if (this_object == NULL) {
945 thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
946 "null receiver during interface dispatch");
947 return 0;
948 }
949 if (interface_method == NULL) {
950 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
951 interface_method = class_linker->ResolveMethod(method_idx, caller_method, false);
952 if (interface_method == NULL) {
953 // Could not resolve interface method. Throw error and unwind
954 CHECK(thread->IsExceptionPending());
955 return 0;
956 }
957 }
Ian Rogersb04f69f2011-10-17 00:40:54 -0700958 found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700959 if (found_method == NULL) {
960 CHECK(thread->IsExceptionPending());
961 return 0;
962 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700963 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700964 const void* code = found_method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700965
Ian Rogerscaab8c42011-10-12 12:11:18 -0700966 uint32_t method_uint = reinterpret_cast<uint32_t>(found_method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700967 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
968 uint64_t result = ((code_uint << 32) | method_uint);
969 return result;
970}
971
Ian Rogers466bb252011-10-14 03:29:56 -0700972static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
973 ScopedLocalRef<jclass> jlr_UTE_class(env,
974 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
975 if (jlr_UTE_class.get() == NULL) {
976 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
977 } else {
978 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
979 "(Ljava/lang/Throwable;)V");
980 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
981 ScopedLocalRef<jthrowable> jlr_UTE(env,
982 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
983 jexception)));
984 int rc = env->Throw(jlr_UTE.get());
985 if (rc != JNI_OK) {
986 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
987 }
988 }
989 CHECK(self->IsExceptionPending());
990}
991
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700992// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
993// which is responsible for recording callee save registers. We explicitly handlerize incoming
994// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
995// the invocation handler which is a field within the proxy object receiver.
996extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -0700997 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700998 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -0700999 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1000 DCHECK_EQ(*proxy_sp, proxy_method);
1001 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001002 // TODO: ARM specific
1003 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1004 // Start new JNI local reference state
1005 JNIEnvExt* env = self->GetJniEnv();
1006 ScopedJniEnvLocalRefState env_state(env);
1007 // Create local ref. copies of proxy method and the receiver
1008 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1009 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1010
Ian Rogers14b1b242011-10-11 18:54:34 -07001011 // Placing into local references incoming arguments from the caller's register arguments,
1012 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001013 MethodHelper proxy_mh(proxy_method);
1014 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001015 size_t args_in_regs = 0;
1016 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001017 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001018 if (args_in_regs > 2) {
1019 args_in_regs = 2;
1020 break;
1021 }
1022 }
1023 size_t cur_arg = 0; // current stack location to read
1024 size_t param_index = 1; // skip receiver
1025 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001026 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001027 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001028 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001029 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001030 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001031 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001032 param_index++;
1033 }
1034 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001035 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001036 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001037 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001038 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1039 jobject jobj = AddLocalReference<jobject>(env, obj);
1040 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001041 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001042 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001043 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001044 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001045 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1046 jvalue args_jobj[3];
1047 args_jobj[0].l = rcvr_jobj;
1048 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001049 // Args array, if no arguments then NULL (don't include receiver in argument count)
1050 args_jobj[2].l = NULL;
1051 ObjectArray<Object>* args = NULL;
1052 if ((num_params - 1) > 0) {
1053 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001054 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001055 CHECK(self->IsExceptionPending());
1056 return;
1057 }
1058 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1059 }
1060 // Convert proxy method into expected interface method
1061 Method* interface_method = proxy_method->FindOverriddenMethod();
1062 CHECK(interface_method != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001063 CHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001064 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
1065 LOG(INFO) << "Interface method is " << PrettyMethod(interface_method, true);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001066 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001067 cur_arg = 0; // reset stack location to read to start
1068 // reset index, will index into param type array which doesn't include the receiver
1069 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001070 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001071 CHECK(param_types != NULL);
1072 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1073 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1074 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1075 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001076 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001077 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001078 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001079 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001080 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1081 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1082 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001083 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001084 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001085 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001086 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001087 if (self->IsExceptionPending()) {
1088 return;
1089 }
1090 obj = val.l;
1091 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001092 args->Set(param_index, obj);
1093 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1094 param_index++;
1095 }
1096 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001097 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1098 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001099 Class* param_type = param_types->Get(param_index);
1100 Object* obj;
1101 if (!param_type->IsPrimitive()) {
1102 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1103 } else {
1104 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001105 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001106 if (self->IsExceptionPending()) {
1107 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001108 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001109 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001110 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001111 args->Set(param_index, obj);
1112 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1113 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001114 }
1115 // Get the InvocationHandler method and the field that holds it within the Proxy object
1116 static jmethodID inv_hand_invoke_mid = NULL;
1117 static jfieldID proxy_inv_hand_fid = NULL;
1118 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001119 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001120 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001121 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001122 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1123 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1124 }
Ian Rogers466bb252011-10-14 03:29:56 -07001125 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001126 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1127 // Call InvocationHandler.invoke
1128 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1129 // Place result in stack args
1130 if (!self->IsExceptionPending()) {
1131 Object* result_ref = self->DecodeJObject(result);
1132 if (result_ref != NULL) {
1133 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001134 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001135 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1136 } else {
1137 *reinterpret_cast<jobject*>(stack_args) = NULL;
1138 }
Ian Rogers466bb252011-10-14 03:29:56 -07001139 } else {
1140 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1141 // a UndeclaredThrowableException.
1142 Throwable* exception = self->GetException();
1143 self->ClearException();
1144 if (!exception->IsCheckedException()) {
1145 self->SetException(exception);
1146 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001147 // TODO: get the correct intersection of exceptions as passed to the class linker's create
1148 // proxy code.
1149 UNIMPLEMENTED(FATAL);
1150 ObjectArray<Class>* declared_exceptions = NULL; // proxy_mh.GetExceptionTypes();
Ian Rogers466bb252011-10-14 03:29:56 -07001151 Class* exception_class = exception->GetClass();
1152 bool declares_exception = false;
1153 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1154 Class* declared_exception = declared_exceptions->Get(i);
1155 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1156 }
1157 if (declares_exception) {
1158 self->SetException(exception);
1159 } else {
1160 ThrowNewUndeclaredThrowableException(self, env, exception);
1161 }
1162 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001163 }
1164}
1165
Shih-wei Liao2d831012011-09-28 22:06:53 -07001166/*
1167 * Float/double conversion requires clamping to min and max of integer form. If
1168 * target doesn't support this normally, use these.
1169 */
1170int64_t D2L(double d) {
1171 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1172 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1173 if (d >= kMaxLong)
1174 return (int64_t)0x7fffffffffffffffULL;
1175 else if (d <= kMinLong)
1176 return (int64_t)0x8000000000000000ULL;
1177 else if (d != d) // NaN case
1178 return 0;
1179 else
1180 return (int64_t)d;
1181}
1182
1183int64_t F2L(float f) {
1184 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1185 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1186 if (f >= kMaxLong)
1187 return (int64_t)0x7fffffffffffffffULL;
1188 else if (f <= kMinLong)
1189 return (int64_t)0x8000000000000000ULL;
1190 else if (f != f) // NaN case
1191 return 0;
1192 else
1193 return (int64_t)f;
1194}
1195
1196} // namespace art