blob: 3c10fc37afa669e6a1da53b94b99e5a27bed78bc [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 Rogersdfcdf1a2011-10-10 17:50:35 -070022#include "reflection.h"
23#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070024
Shih-wei Liao2d831012011-09-28 22:06:53 -070025namespace art {
26
Ian Rogers4f0d07c2011-10-06 23:38:47 -070027// Place a special frame at the TOS that will save the callee saves for the given type
28static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070029 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070030 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
31 self->SetTopOfStack(sp, 0);
32}
33
Shih-wei Liao2d831012011-09-28 22:06:53 -070034// Temporary debugging hook for compiler.
35extern void DebugMe(Method* method, uint32_t info) {
36 LOG(INFO) << "DebugMe";
37 if (method != NULL) {
38 LOG(INFO) << PrettyMethod(method);
39 }
40 LOG(INFO) << "Info: " << info;
41}
42
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -070043extern "C" uint32_t artObjectInitFromCode(Object* o, Thread* self, Method** sp) {
44 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -070045 Class* c = o->GetClass();
46 if (UNLIKELY(c->IsFinalizable())) {
47 Heap::AddFinalizerReference(o);
48 }
49 /*
50 * NOTE: once debugger/profiler support is added, we'll need to check
51 * here and branch to actual compiled object.<init> to handle any
52 * breakpoint/logging activites if either is active.
53 */
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -070054 return self->IsExceptionPending() ? -1 : 0;
Ian Rogerscaab8c42011-10-12 12:11:18 -070055}
56
Shih-wei Liao2d831012011-09-28 22:06:53 -070057// Return value helper for jobject return types
58extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -070059 if (thread->IsExceptionPending()) {
60 return NULL;
61 }
Shih-wei Liao2d831012011-09-28 22:06:53 -070062 return thread->DecodeJObject(obj);
63}
64
65extern void* FindNativeMethod(Thread* thread) {
66 DCHECK(Thread::Current() == thread);
67
68 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
69 DCHECK(method != NULL);
70
71 // Lookup symbol address for method, on failure we'll return NULL with an
72 // exception set, otherwise we return the address of the method we found.
73 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
74 if (native_code == NULL) {
75 DCHECK(thread->IsExceptionPending());
76 return NULL;
77 } else {
78 // Register so that future calls don't come here
79 method->RegisterNative(native_code);
80 return native_code;
81 }
82}
83
84// Called by generated call to throw an exception
85extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
86 /*
87 * exception may be NULL, in which case this routine should
88 * throw NPE. NOTE: this is a convenience for generated code,
89 * which previously did the null check inline and constructed
90 * and threw a NPE if NULL. This routine responsible for setting
91 * exception_ in thread and delivering the exception.
92 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -070093 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -070094 if (exception == NULL) {
95 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
96 } else {
97 thread->SetException(exception);
98 }
99 thread->DeliverException();
100}
101
102// Deliver an exception that's pending on thread helping set up a callee save frame on the way
103extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700104 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700105 thread->DeliverException();
106}
107
108// Called by generated call to throw a NPE exception
109extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700110 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700111 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700112 thread->DeliverException();
113}
114
115// Called by generated call to throw an arithmetic divide by zero exception
116extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700117 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700118 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
119 thread->DeliverException();
120}
121
122// Called by generated call to throw an arithmetic divide by zero exception
123extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700124 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
125 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
126 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700127 thread->DeliverException();
128}
129
130// Called by the AbstractMethodError stub (not runtime support)
131extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700132 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
133 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
134 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700135 thread->DeliverException();
136}
137
138extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700139 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700140 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700141 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
142 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700143 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700144 thread->ResetDefaultStackEnd(); // Return to default stack size
145 thread->DeliverException();
146}
147
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700148static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700149 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700150 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
151 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
152
153 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700154 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700155 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
156 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700157 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700158 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
159 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700160 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700161 type_idx = ref;
162 } else {
163 CHECK(false) << static_cast<int>(ref_type);
164 }
165
166 std::string class_name(PrettyDescriptor(dex_file.dexStringByTypeIdx(type_idx)));
167 if (!access) {
168 return class_name;
169 }
170
171 std::string result;
172 result += "tried to access class ";
173 result += class_name;
174 result += " from class ";
175 result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor());
176 return result;
177}
178
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700179static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700180 verifier::VerifyErrorRefType ref_type, bool access) {
181 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700182
183 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
184 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
185
186 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700187 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700188 const char* field_name = dex_file.dexStringById(id.name_idx_);
189 if (!access) {
190 return class_name + "." + field_name;
191 }
192
193 std::string result;
194 result += "tried to access field ";
195 result += class_name + "." + field_name;
196 result += " from class ";
197 result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor());
198 return result;
199}
200
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700201static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700202 verifier::VerifyErrorRefType ref_type, bool access) {
203 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700204
205 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
206 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
207
208 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700209 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700210 const char* method_name = dex_file.dexStringById(id.name_idx_);
211 if (!access) {
212 return class_name + "." + method_name;
213 }
214
215 std::string result;
216 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700217 result += class_name + "." + method_name + ":" +
218 dex_file.CreateMethodDescriptor(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700219 result += " from class ";
220 result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor());
221 return result;
222}
223
224extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700225 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
226 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700227 frame.Next();
228 Method* method = frame.GetMethod();
229
Ian Rogersd81871c2011-10-03 13:57:23 -0700230 verifier::VerifyErrorRefType ref_type =
231 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700232
233 const char* exception_class = "Ljava/lang/VerifyError;";
234 std::string msg;
235
Ian Rogersd81871c2011-10-03 13:57:23 -0700236 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
237 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700238 exception_class = "Ljava/lang/NoClassDefFoundError;";
239 msg = ClassNameFromIndex(method, ref, ref_type, false);
240 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700241 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700242 exception_class = "Ljava/lang/NoSuchFieldError;";
243 msg = FieldNameFromIndex(method, ref, ref_type, false);
244 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700245 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700246 exception_class = "Ljava/lang/NoSuchMethodError;";
247 msg = MethodNameFromIndex(method, ref, ref_type, false);
248 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700249 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700250 exception_class = "Ljava/lang/IllegalAccessError;";
251 msg = ClassNameFromIndex(method, ref, ref_type, true);
252 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700253 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700254 exception_class = "Ljava/lang/IllegalAccessError;";
255 msg = FieldNameFromIndex(method, ref, ref_type, true);
256 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700257 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700258 exception_class = "Ljava/lang/IllegalAccessError;";
259 msg = MethodNameFromIndex(method, ref, ref_type, true);
260 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700261 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700262 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
263 msg = ClassNameFromIndex(method, ref, ref_type, false);
264 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700265 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700266 exception_class = "Ljava/lang/InstantiationError;";
267 msg = ClassNameFromIndex(method, ref, ref_type, false);
268 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700269 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700270 // Generic VerifyError; use default exception, no message.
271 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700272 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700273 CHECK(false);
274 break;
275 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700276 self->ThrowNewException(exception_class, msg.c_str());
277 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700278}
279
280extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700281 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700282 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700283 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700284 thread->DeliverException();
285}
286
287extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700288 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700289 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700290 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700291 thread->DeliverException();
292}
293
Elliott Hughese1410a22011-10-04 12:10:24 -0700294extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700295 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
296 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700297 frame.Next();
298 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700299 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700300 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700301 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700302}
303
304extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700305 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700306 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700307 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700308 thread->DeliverException();
309}
310
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700311void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700312 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700313 // TODO: this code is specific to ARM
314 // On entry the stack pointed by sp is:
315 // | argN | |
316 // | ... | |
317 // | arg4 | |
318 // | arg3 spill | | Caller's frame
319 // | arg2 spill | |
320 // | arg1 spill | |
321 // | Method* | ---
322 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700323 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700324 // | R3 | arg3
325 // | R2 | arg2
326 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700327 // | R0 |
328 // | Method* | <- sp
329 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
330 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
331 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
332 uintptr_t caller_pc = regs[10];
333 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700334 // Start new JNI local reference state
335 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700336 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersad25ac52011-10-04 19:13:33 -0700337 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700338 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700339 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
340 size_t shorty_len = strlen(shorty);
Ian Rogers14b1b242011-10-11 18:54:34 -0700341 size_t args_in_regs = 0;
342 for (size_t i = 1; i < shorty_len; i++) {
343 char c = shorty[i];
344 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
345 if (args_in_regs > 3) {
346 args_in_regs = 3;
347 break;
348 }
349 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700350 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700351 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700352 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700353 // less two as return address may span into next dex instruction
354 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700355 const DexFile& dex_file = Runtime::Current()->GetClassLinker()
356 ->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
357 const DexFile::CodeItem* code = dex_file.GetCodeItem(caller->GetCodeItemOffset());
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);
buzbee1cb66f52011-10-18 14:00:10 -0700404 // Update CodeAndDirectMethod table
405 Method* caller = *caller_sp;
406 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
407 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700408 if (LIKELY(!thread->IsExceptionPending())) {
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())) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700414 // Something went wrong, go into deliver exception with the pending exception in r0
415 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
416 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
417 thread->ClearException();
418 } else {
419 // Expect class to at least be initializing
420 CHECK(called->GetDeclaringClass()->IsInitializing());
421 // Set up entry into main method
422 regs[0] = reinterpret_cast<uintptr_t>(called);
423 code = const_cast<void*>(called->GetCode());
424 }
425 return code;
426}
427
Ian Rogerscaab8c42011-10-12 12:11:18 -0700428// Fast path field resolution that can't throw exceptions
429static Field* FindFieldFast(uint32_t field_idx, const Method* referrer) {
430 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
431 if (UNLIKELY(resolved_field == NULL)) {
432 return NULL;
433 }
434 Class* fields_class = resolved_field->GetDeclaringClass();
435 // Check class is initilaized or initializing
436 if (UNLIKELY(!fields_class->IsInitializing())) {
437 return NULL;
438 }
439 return resolved_field;
440}
441
442// Slow path field resolution and declaring class initialization
Ian Rogersce9eca62011-10-07 17:11:03 -0700443Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static) {
444 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700445 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
446 if (LIKELY(resolved_field != NULL)) {
447 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogersce9eca62011-10-07 17:11:03 -0700448 // If the class is already initializing, we must be inside <clinit>, or
449 // we'd still be waiting for the lock.
Ian Rogerscaab8c42011-10-12 12:11:18 -0700450 if (fields_class->IsInitializing()) {
451 return resolved_field;
452 }
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700453 if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700454 return resolved_field;
Ian Rogersce9eca62011-10-07 17:11:03 -0700455 }
456 }
457 DCHECK(Thread::Current()->IsExceptionPending()); // Throw exception and unwind
458 return NULL;
459}
460
461extern "C" Field* artFindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer,
462 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700463 Field* resolved_field = FindFieldFast(field_idx, referrer);
464 if (UNLIKELY(resolved_field == NULL)) {
465 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
466 resolved_field = FindFieldFromCode(field_idx, referrer, false);
467 }
468 return resolved_field;
Ian Rogersce9eca62011-10-07 17:11:03 -0700469}
470
471extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
472 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700473 Field* field = FindFieldFast(field_idx, referrer);
474 if (LIKELY(field != NULL)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700475 if (LIKELY(field->IsPrimitiveType() && field->PrimitiveSize() == sizeof(int32_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700476 return field->Get32(NULL);
477 }
478 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700479 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700480 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700481 if (field != NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700482 if (!field->IsPrimitiveType() || field->PrimitiveSize() != sizeof(int32_t)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700483 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
484 "Attempted read of 32-bit primitive on field '%s'",
485 PrettyField(field, true).c_str());
486 } else {
487 return field->Get32(NULL);
488 }
489 }
490 return 0; // Will throw exception by checking with Thread::Current
491}
492
493extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
494 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700495 Field* field = FindFieldFast(field_idx, referrer);
496 if (LIKELY(field != NULL)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700497 if (LIKELY(field->IsPrimitiveType() && field->PrimitiveSize() == sizeof(int64_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700498 return field->Get64(NULL);
499 }
500 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700501 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700502 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700503 if (field != NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700504 if (!field->IsPrimitiveType() || field->PrimitiveSize() != sizeof(int64_t)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700505 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
506 "Attempted read of 64-bit primitive on field '%s'",
507 PrettyField(field, true).c_str());
508 } else {
509 return field->Get64(NULL);
510 }
511 }
512 return 0; // Will throw exception by checking with Thread::Current
513}
514
515extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
516 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700517 Field* field = FindFieldFast(field_idx, referrer);
518 if (LIKELY(field != NULL)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700519 if (LIKELY(!field->IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700520 return field->GetObj(NULL);
521 }
522 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700523 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700524 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700525 if (field != NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700526 if (field->IsPrimitiveType()) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700527 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
528 "Attempted read of reference on primitive field '%s'",
529 PrettyField(field, true).c_str());
530 } else {
531 return field->GetObj(NULL);
532 }
533 }
534 return NULL; // Will throw exception by checking with Thread::Current
535}
536
537extern "C" int artSet32StaticFromCode(uint32_t field_idx, const Method* referrer,
538 uint32_t new_value, Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700539 Field* field = FindFieldFast(field_idx, referrer);
540 if (LIKELY(field != NULL)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700541 if (LIKELY(field->IsPrimitiveType() && field->PrimitiveSize() == sizeof(int32_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700542 field->Set32(NULL, new_value);
543 return 0; // success
544 }
545 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700546 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700547 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700548 if (field != NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700549 if (!field->IsPrimitiveType() || field->PrimitiveSize() != sizeof(int32_t)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700550 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
551 "Attempted write of 32-bit primitive to field '%s'",
552 PrettyField(field, true).c_str());
553 } else {
554 field->Set32(NULL, new_value);
555 return 0; // success
556 }
557 }
558 return -1; // failure
559}
560
561extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
562 uint64_t new_value, Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700563 Field* field = FindFieldFast(field_idx, referrer);
564 if (LIKELY(field != NULL)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700565 if (LIKELY(field->IsPrimitiveType() && field->PrimitiveSize() == sizeof(int64_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700566 field->Set64(NULL, new_value);
567 return 0; // success
568 }
569 }
570 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
571 field = FindFieldFromCode(field_idx, referrer, true);
572 if (LIKELY(field != NULL)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700573 if (UNLIKELY(!field->IsPrimitiveType() || field->PrimitiveSize() != sizeof(int64_t))) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700574 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
575 "Attempted write of 64-bit primitive to field '%s'",
576 PrettyField(field, true).c_str());
577 } else {
578 field->Set64(NULL, new_value);
579 return 0; // success
580 }
581 }
582 return -1; // failure
583}
584
585extern "C" int artSetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
586 Object* new_value, Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700587 Field* field = FindFieldFast(field_idx, referrer);
588 if (LIKELY(field != NULL)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700589 if (LIKELY(!field->IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700590 field->SetObj(NULL, new_value);
591 return 0; // success
592 }
593 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700594 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700595 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700596 if (field != NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700597 if (field->IsPrimitiveType()) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700598 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
599 "Attempted write of reference to primitive field '%s'",
600 PrettyField(field, true).c_str());
601 } else {
602 field->SetObj(NULL, new_value);
603 return 0; // success
604 }
605 }
606 return -1; // failure
607}
608
Shih-wei Liao2d831012011-09-28 22:06:53 -0700609// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
610// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogerscaab8c42011-10-12 12:11:18 -0700611extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
612 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700613 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700614 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700615 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700616 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700617 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700618 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700619 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700620 return NULL; // Failure
621 }
622 }
buzbee33a129c2011-10-06 16:53:20 -0700623 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
624 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700625 return NULL; // Failure
626 }
627 return klass->AllocObject();
628}
629
Ian Rogers28ad40d2011-10-27 15:19:26 -0700630extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
631 Thread* self, Method** sp) {
632 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
633 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
634 Runtime* runtime = Runtime::Current();
635 if (UNLIKELY(klass == NULL)) {
636 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
637 if (klass == NULL) {
638 DCHECK(self->IsExceptionPending());
639 return NULL; // Failure
640 }
641 }
642 Class* referrer = method->GetDeclaringClass();
643 if (UNLIKELY(!referrer->CanAccess(klass))) {
644 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", "illegal class access: '%s' -> '%s'",
645 PrettyDescriptor(referrer->GetDescriptor()).c_str(),
646 PrettyDescriptor(klass->GetDescriptor()).c_str());
647 return NULL; // Failure
648 }
649 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
650 DCHECK(self->IsExceptionPending());
651 return NULL; // Failure
652 }
653 return klass->AllocObject();
buzbeecc4540e2011-10-27 13:06:03 -0700654}
655
Ian Rogersce9eca62011-10-07 17:11:03 -0700656Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
657 Thread* self) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700658 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700659 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700660 return NULL; // Failure
661 }
662 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700663 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700664 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
665 if (klass == NULL) { // Error
666 DCHECK(Thread::Current()->IsExceptionPending());
667 return NULL; // Failure
668 }
669 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700670 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700671 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700672 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700673 "Bad filled array request for type %s",
674 PrettyDescriptor(klass->GetDescriptor()).c_str());
675 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700676 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700677 "Found type %s; filled-new-array not implemented for anything but \'int\'",
678 PrettyDescriptor(klass->GetDescriptor()).c_str());
679 }
680 return NULL; // Failure
681 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700682 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700683 return Array::Alloc(klass, component_count);
684 }
685}
686
Ian Rogersce9eca62011-10-07 17:11:03 -0700687// Helper function to alloc array for OP_FILLED_NEW_ARRAY
688extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
689 int32_t component_count, Thread* self, Method** sp) {
690 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
691 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self);
692}
693
Shih-wei Liao2d831012011-09-28 22:06:53 -0700694// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
695// it cannot be resolved, throw an error. If it can, use it to create an array.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700696extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
697 Thread* self, Method** sp) {
698 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700699 if (UNLIKELY(component_count < 0)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700700 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700701 component_count);
702 return NULL; // Failure
703 }
704 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700705 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700706 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
707 if (klass == NULL) { // Error
708 DCHECK(Thread::Current()->IsExceptionPending());
709 return NULL; // Failure
710 }
711 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
712 }
713 return Array::Alloc(klass, component_count);
714}
715
Ian Rogerscaab8c42011-10-12 12:11:18 -0700716// Assignable test for code, won't throw. Null and equality tests already performed
717uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
718 DCHECK(klass != NULL);
719 DCHECK(ref_class != NULL);
720 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
721}
722
Shih-wei Liao2d831012011-09-28 22:06:53 -0700723// 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 -0700724extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700725 DCHECK(a->IsClass()) << PrettyClass(a);
726 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700727 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700728 return 0; // Success
729 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700730 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700731 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700732 "%s cannot be cast to %s",
733 PrettyDescriptor(a->GetDescriptor()).c_str(),
734 PrettyDescriptor(b->GetDescriptor()).c_str());
735 return -1; // Failure
736 }
737}
738
739// Tests whether 'element' can be assigned into an array of type 'array_class'.
740// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700741extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
742 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700743 DCHECK(array_class != NULL);
744 // element can't be NULL as we catch this is screened in runtime_support
745 Class* element_class = element->GetClass();
746 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700747 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700748 return 0; // Success
749 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700750 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700751 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700752 "Cannot store an object of type %s in to an array of type %s",
753 PrettyDescriptor(element_class->GetDescriptor()).c_str(),
754 PrettyDescriptor(array_class->GetDescriptor()).c_str());
755 return -1; // Failure
756 }
757}
758
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700759Class* InitializeStaticStorage(uint32_t type_idx, const Method* referrer, Thread* self) {
760 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
761 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700762 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700763 CHECK(self->IsExceptionPending());
764 return NULL; // Failure - Indicate to caller to deliver exception
765 }
Ian Rogersb093c6b2011-10-31 16:19:55 -0700766 DCHECK(referrer->GetDeclaringClass()->CanAccess(klass));
767 // If we are the <clinit> of this class, just return our storage.
768 //
769 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
770 // running.
771 if (klass == referrer->GetDeclaringClass() && referrer->IsClassInitializer()) {
772 return klass;
773 }
774 if (!class_linker->EnsureInitialized(klass, true)) {
775 CHECK(self->IsExceptionPending());
776 return NULL; // Failure - Indicate to caller to deliver exception
777 }
778 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
779 return klass;
780}
781
782Class* InitializeStaticStorageAndVerifyAccess(uint32_t type_idx, const Method* referrer,
783 Thread* self) {
784 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
785 Class* klass = class_linker->ResolveType(type_idx, referrer);
786 if (UNLIKELY(klass == NULL)) {
787 CHECK(self->IsExceptionPending());
788 return NULL; // Failure - Indicate to caller to deliver exception
789 }
790 // Perform access check
791 if (UNLIKELY(!referrer->GetDeclaringClass()->CanAccess(klass))) {
792 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
793 "Class %s is inaccessible to method %s",
794 PrettyDescriptor(klass->GetDescriptor()).c_str(),
795 PrettyMethod(referrer, true).c_str());
796 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700797 // If we are the <clinit> of this class, just return our storage.
798 //
799 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
800 // running.
801 if (klass == referrer->GetDeclaringClass() && referrer->IsClassInitializer()) {
802 return klass;
803 }
804 if (!class_linker->EnsureInitialized(klass, true)) {
805 CHECK(self->IsExceptionPending());
806 return NULL; // Failure - Indicate to caller to deliver exception
807 }
808 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
809 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700810}
811
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700812extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
813 Thread* self, Method** sp) {
814 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
815 return InitializeStaticStorage(type_idx, referrer, self);
816}
817
Ian Rogers28ad40d2011-10-27 15:19:26 -0700818extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
819 Method** sp) {
820 // Called when method->dex_cache_resolved_types_[] misses
821 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
822 return InitializeStaticStorage(type_idx, referrer, self);
823}
824
Ian Rogersb093c6b2011-10-31 16:19:55 -0700825extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
826 const Method* referrer, Thread* self,
827 Method** sp) {
828 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
829 // unpopulated
830 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
831 return InitializeStaticStorageAndVerifyAccess(type_idx, referrer, self);
832}
833
Ian Rogers28ad40d2011-10-27 15:19:26 -0700834// TODO: placeholder. Helper function to resolve virtual method
835void ResolveMethodFromCode(Method* method, uint32_t method_idx) {
836 /*
837 * Slow-path handler on invoke virtual method path in which
838 * base method is unresolved at compile-time. Doesn't need to
839 * return anything - just either ensure that
840 * method->dex_cache_resolved_methods_(method_idx) != NULL or
841 * throw and unwind. The caller will restart call sequence
842 * from the beginning.
843 */
844}
845
Brian Carlstromaded5f72011-10-07 17:15:04 -0700846String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
847 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
848 return class_linker->ResolveString(string_idx, referrer);
849}
850
851extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
852 Thread* self, Method** sp) {
853 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
854 return ResolveStringFromCode(referrer, string_idx);
855}
856
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700857extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
858 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700859 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700860 // MonitorExit may throw exception
861 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
862}
863
864extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
865 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
866 DCHECK(obj != NULL); // Assumed to have been checked before entry
867 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700868 DCHECK(thread->HoldsLock(obj));
869 // Only possible exception is NPE and is handled before entry
870 DCHECK(!thread->IsExceptionPending());
871}
872
Ian Rogers4a510d82011-10-09 14:30:24 -0700873void CheckSuspendFromCode(Thread* thread) {
874 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700875 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
876}
877
Ian Rogers4a510d82011-10-09 14:30:24 -0700878extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
879 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700880 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700881 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
882}
883
884/*
885 * Fill the array with predefined constant values, throwing exceptions if the array is null or
886 * not of sufficient length.
887 *
888 * NOTE: When dealing with a raw dex file, the data to be copied uses
889 * little-endian ordering. Require that oat2dex do any required swapping
890 * so this routine can get by with a memcpy().
891 *
892 * Format of the data:
893 * ushort ident = 0x0300 magic value
894 * ushort width width of each element in the table
895 * uint size number of elements in the table
896 * ubyte data[size*width] table of data values (may contain a single-byte
897 * padding at the end)
898 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700899extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
900 Thread* self, Method** sp) {
901 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700902 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700903 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700904 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
905 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700906 return -1; // Error
907 }
908 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
909 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700910 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700911 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
912 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700913 return -1; // Error
914 }
915 uint16_t width = table[1];
916 uint32_t size_in_bytes = size * width;
917 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
918 return 0; // Success
919}
920
921// See comments in runtime_support_asm.S
922extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx,
Ian Rogerscaab8c42011-10-12 12:11:18 -0700923 Object* this_object,
924 Method* caller_method,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700925 Thread* thread, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700926 Method* interface_method = caller_method->GetDexCacheResolvedMethods()->Get(method_idx);
927 Method* found_method = NULL; // The found method
928 if (LIKELY(interface_method != NULL && this_object != NULL)) {
Ian Rogersb04f69f2011-10-17 00:40:54 -0700929 found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, false);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700930 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700931 if (UNLIKELY(found_method == NULL)) {
932 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
933 if (this_object == NULL) {
934 thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
935 "null receiver during interface dispatch");
936 return 0;
937 }
938 if (interface_method == NULL) {
939 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
940 interface_method = class_linker->ResolveMethod(method_idx, caller_method, false);
941 if (interface_method == NULL) {
942 // Could not resolve interface method. Throw error and unwind
943 CHECK(thread->IsExceptionPending());
944 return 0;
945 }
946 }
Ian Rogersb04f69f2011-10-17 00:40:54 -0700947 found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700948 if (found_method == NULL) {
949 CHECK(thread->IsExceptionPending());
950 return 0;
951 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700952 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700953 const void* code = found_method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700954
Ian Rogerscaab8c42011-10-12 12:11:18 -0700955 uint32_t method_uint = reinterpret_cast<uint32_t>(found_method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700956 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
957 uint64_t result = ((code_uint << 32) | method_uint);
958 return result;
959}
960
Ian Rogers466bb252011-10-14 03:29:56 -0700961static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
962 ScopedLocalRef<jclass> jlr_UTE_class(env,
963 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
964 if (jlr_UTE_class.get() == NULL) {
965 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
966 } else {
967 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
968 "(Ljava/lang/Throwable;)V");
969 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
970 ScopedLocalRef<jthrowable> jlr_UTE(env,
971 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
972 jexception)));
973 int rc = env->Throw(jlr_UTE.get());
974 if (rc != JNI_OK) {
975 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
976 }
977 }
978 CHECK(self->IsExceptionPending());
979}
980
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700981// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
982// which is responsible for recording callee save registers. We explicitly handlerize incoming
983// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
984// the invocation handler which is a field within the proxy object receiver.
985extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -0700986 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700987 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -0700988 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
989 DCHECK_EQ(*proxy_sp, proxy_method);
990 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700991 // TODO: ARM specific
992 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
993 // Start new JNI local reference state
994 JNIEnvExt* env = self->GetJniEnv();
995 ScopedJniEnvLocalRefState env_state(env);
996 // Create local ref. copies of proxy method and the receiver
997 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
998 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
999
Ian Rogers14b1b242011-10-11 18:54:34 -07001000 // Placing into local references incoming arguments from the caller's register arguments,
1001 // replacing original Object* with jobject
1002 const size_t num_params = proxy_method->NumArgs();
1003 size_t args_in_regs = 0;
1004 for (size_t i = 1; i < num_params; i++) { // skip receiver
1005 args_in_regs = args_in_regs + (proxy_method->IsParamALongOrDouble(i) ? 2 : 1);
1006 if (args_in_regs > 2) {
1007 args_in_regs = 2;
1008 break;
1009 }
1010 }
1011 size_t cur_arg = 0; // current stack location to read
1012 size_t param_index = 1; // skip receiver
1013 while (cur_arg < args_in_regs && param_index < num_params) {
1014 if (proxy_method->IsParamAReference(param_index)) {
1015 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001016 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001017 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001018 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001019 cur_arg = cur_arg + (proxy_method->IsParamALongOrDouble(param_index) ? 2 : 1);
1020 param_index++;
1021 }
1022 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001023 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001024 while (param_index < num_params) {
1025 if (proxy_method->IsParamAReference(param_index)) {
1026 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1027 jobject jobj = AddLocalReference<jobject>(env, obj);
1028 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001029 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001030 cur_arg = cur_arg + (proxy_method->IsParamALongOrDouble(param_index) ? 2 : 1);
1031 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001032 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001033 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1034 jvalue args_jobj[3];
1035 args_jobj[0].l = rcvr_jobj;
1036 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001037 // Args array, if no arguments then NULL (don't include receiver in argument count)
1038 args_jobj[2].l = NULL;
1039 ObjectArray<Object>* args = NULL;
1040 if ((num_params - 1) > 0) {
1041 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001042 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001043 CHECK(self->IsExceptionPending());
1044 return;
1045 }
1046 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1047 }
1048 // Convert proxy method into expected interface method
1049 Method* interface_method = proxy_method->FindOverriddenMethod();
1050 CHECK(interface_method != NULL);
1051 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
1052 LOG(INFO) << "Interface method is " << PrettyMethod(interface_method, true);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001053 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001054 cur_arg = 0; // reset stack location to read to start
1055 // reset index, will index into param type array which doesn't include the receiver
1056 param_index = 0;
Ian Rogers466bb252011-10-14 03:29:56 -07001057 ObjectArray<Class>* param_types = interface_method->GetJavaParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001058 CHECK(param_types != NULL);
1059 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1060 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1061 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1062 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001063 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001064 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001065 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001066 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001067 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1068 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1069 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001070 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001071 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001072 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001073 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001074 if (self->IsExceptionPending()) {
1075 return;
1076 }
1077 obj = val.l;
1078 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001079 args->Set(param_index, obj);
1080 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1081 param_index++;
1082 }
1083 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001084 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1085 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001086 Class* param_type = param_types->Get(param_index);
1087 Object* obj;
1088 if (!param_type->IsPrimitive()) {
1089 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1090 } else {
1091 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001092 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001093 if (self->IsExceptionPending()) {
1094 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001095 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001096 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001097 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001098 args->Set(param_index, obj);
1099 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1100 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001101 }
1102 // Get the InvocationHandler method and the field that holds it within the Proxy object
1103 static jmethodID inv_hand_invoke_mid = NULL;
1104 static jfieldID proxy_inv_hand_fid = NULL;
1105 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001106 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001107 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001108 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001109 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1110 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1111 }
Ian Rogers466bb252011-10-14 03:29:56 -07001112 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001113 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1114 // Call InvocationHandler.invoke
1115 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1116 // Place result in stack args
1117 if (!self->IsExceptionPending()) {
1118 Object* result_ref = self->DecodeJObject(result);
1119 if (result_ref != NULL) {
1120 JValue result_unboxed;
Ian Rogers466bb252011-10-14 03:29:56 -07001121 UnboxPrimitive(env, result_ref, interface_method->GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001122 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1123 } else {
1124 *reinterpret_cast<jobject*>(stack_args) = NULL;
1125 }
Ian Rogers466bb252011-10-14 03:29:56 -07001126 } else {
1127 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1128 // a UndeclaredThrowableException.
1129 Throwable* exception = self->GetException();
1130 self->ClearException();
1131 if (!exception->IsCheckedException()) {
1132 self->SetException(exception);
1133 } else {
1134 ObjectArray<Class>* declared_exceptions = proxy_method->GetExceptionTypes();
1135 Class* exception_class = exception->GetClass();
1136 bool declares_exception = false;
1137 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1138 Class* declared_exception = declared_exceptions->Get(i);
1139 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1140 }
1141 if (declares_exception) {
1142 self->SetException(exception);
1143 } else {
1144 ThrowNewUndeclaredThrowableException(self, env, exception);
1145 }
1146 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001147 }
1148}
1149
Shih-wei Liao2d831012011-09-28 22:06:53 -07001150/*
1151 * Float/double conversion requires clamping to min and max of integer form. If
1152 * target doesn't support this normally, use these.
1153 */
1154int64_t D2L(double d) {
1155 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1156 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1157 if (d >= kMaxLong)
1158 return (int64_t)0x7fffffffffffffffULL;
1159 else if (d <= kMinLong)
1160 return (int64_t)0x8000000000000000ULL;
1161 else if (d != d) // NaN case
1162 return 0;
1163 else
1164 return (int64_t)d;
1165}
1166
1167int64_t F2L(float f) {
1168 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1169 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1170 if (f >= kMaxLong)
1171 return (int64_t)0x7fffffffffffffffULL;
1172 else if (f <= kMinLong)
1173 return (int64_t)0x8000000000000000ULL;
1174 else if (f != f) // NaN case
1175 return 0;
1176 else
1177 return (int64_t)f;
1178}
1179
1180} // namespace art