blob: 4230cd35e6d798baa3c323897a8aee772197588a [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
2 * Copyright 2011 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "runtime_support.h"
18
Elliott Hughes6c8867d2011-10-03 16:34:05 -070019#include "dex_verifier.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070020#include "reflection.h"
21#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070022
Shih-wei Liao2d831012011-09-28 22:06:53 -070023namespace art {
24
Ian Rogers4f0d07c2011-10-06 23:38:47 -070025// Place a special frame at the TOS that will save the callee saves for the given type
26static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070027 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070028 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
29 self->SetTopOfStack(sp, 0);
30}
31
Shih-wei Liao2d831012011-09-28 22:06:53 -070032// Temporary debugging hook for compiler.
33extern void DebugMe(Method* method, uint32_t info) {
34 LOG(INFO) << "DebugMe";
35 if (method != NULL) {
36 LOG(INFO) << PrettyMethod(method);
37 }
38 LOG(INFO) << "Info: " << info;
39}
40
41// Return value helper for jobject return types
42extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -070043 if (thread->IsExceptionPending()) {
44 return NULL;
45 }
Shih-wei Liao2d831012011-09-28 22:06:53 -070046 return thread->DecodeJObject(obj);
47}
48
49extern void* FindNativeMethod(Thread* thread) {
50 DCHECK(Thread::Current() == thread);
51
52 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
53 DCHECK(method != NULL);
54
55 // Lookup symbol address for method, on failure we'll return NULL with an
56 // exception set, otherwise we return the address of the method we found.
57 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
58 if (native_code == NULL) {
59 DCHECK(thread->IsExceptionPending());
60 return NULL;
61 } else {
62 // Register so that future calls don't come here
63 method->RegisterNative(native_code);
64 return native_code;
65 }
66}
67
68// Called by generated call to throw an exception
69extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
70 /*
71 * exception may be NULL, in which case this routine should
72 * throw NPE. NOTE: this is a convenience for generated code,
73 * which previously did the null check inline and constructed
74 * and threw a NPE if NULL. This routine responsible for setting
75 * exception_ in thread and delivering the exception.
76 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -070077 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -070078 if (exception == NULL) {
79 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
80 } else {
81 thread->SetException(exception);
82 }
83 thread->DeliverException();
84}
85
86// Deliver an exception that's pending on thread helping set up a callee save frame on the way
87extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070088 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -070089 thread->DeliverException();
90}
91
92// Called by generated call to throw a NPE exception
93extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070094 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070095 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -070096 thread->DeliverException();
97}
98
99// Called by generated call to throw an arithmetic divide by zero exception
100extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700101 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700102 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
103 thread->DeliverException();
104}
105
106// Called by generated call to throw an arithmetic divide by zero exception
107extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700108 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
109 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
110 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700111 thread->DeliverException();
112}
113
114// Called by the AbstractMethodError stub (not runtime support)
115extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700116 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
117 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
118 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700119 thread->DeliverException();
120}
121
122extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700123 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700124 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700125 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
126 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700127 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700128 thread->ResetDefaultStackEnd(); // Return to default stack size
129 thread->DeliverException();
130}
131
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700132static std::string ClassNameFromIndex(Method* method, uint32_t ref,
133 DexVerifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700134 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
135 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
136
137 uint16_t type_idx = 0;
138 if (ref_type == DexVerifier::VERIFY_ERROR_REF_FIELD) {
139 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
140 type_idx = id.class_idx_;
141 } else if (ref_type == DexVerifier::VERIFY_ERROR_REF_METHOD) {
142 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
143 type_idx = id.class_idx_;
144 } else if (ref_type == DexVerifier::VERIFY_ERROR_REF_CLASS) {
145 type_idx = ref;
146 } else {
147 CHECK(false) << static_cast<int>(ref_type);
148 }
149
150 std::string class_name(PrettyDescriptor(dex_file.dexStringByTypeIdx(type_idx)));
151 if (!access) {
152 return class_name;
153 }
154
155 std::string result;
156 result += "tried to access class ";
157 result += class_name;
158 result += " from class ";
159 result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor());
160 return result;
161}
162
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700163static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
164 DexVerifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700165 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(DexVerifier::VERIFY_ERROR_REF_FIELD));
166
167 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
168 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
169
170 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
171 std::string class_name(PrettyDescriptor(dex_file.dexStringByTypeIdx(id.class_idx_)));
172 const char* field_name = dex_file.dexStringById(id.name_idx_);
173 if (!access) {
174 return class_name + "." + field_name;
175 }
176
177 std::string result;
178 result += "tried to access field ";
179 result += class_name + "." + field_name;
180 result += " from class ";
181 result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor());
182 return result;
183}
184
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700185static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
186 DexVerifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700187 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(DexVerifier::VERIFY_ERROR_REF_METHOD));
188
189 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
190 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
191
192 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
193 std::string class_name(PrettyDescriptor(dex_file.dexStringByTypeIdx(id.class_idx_)));
194 const char* method_name = dex_file.dexStringById(id.name_idx_);
195 if (!access) {
196 return class_name + "." + method_name;
197 }
198
199 std::string result;
200 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700201 result += class_name + "." + method_name + ":" +
202 dex_file.CreateMethodDescriptor(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700203 result += " from class ";
204 result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor());
205 return result;
206}
207
208extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700209 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
210 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700211 frame.Next();
212 Method* method = frame.GetMethod();
213
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700214 DexVerifier::VerifyErrorRefType ref_type =
215 static_cast<DexVerifier::VerifyErrorRefType>(kind >> kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700216
217 const char* exception_class = "Ljava/lang/VerifyError;";
218 std::string msg;
219
220 switch (static_cast<DexVerifier::VerifyError>(kind & ~(0xff << kVerifyErrorRefTypeShift))) {
221 case DexVerifier::VERIFY_ERROR_NO_CLASS:
222 exception_class = "Ljava/lang/NoClassDefFoundError;";
223 msg = ClassNameFromIndex(method, ref, ref_type, false);
224 break;
225 case DexVerifier::VERIFY_ERROR_NO_FIELD:
226 exception_class = "Ljava/lang/NoSuchFieldError;";
227 msg = FieldNameFromIndex(method, ref, ref_type, false);
228 break;
229 case DexVerifier::VERIFY_ERROR_NO_METHOD:
230 exception_class = "Ljava/lang/NoSuchMethodError;";
231 msg = MethodNameFromIndex(method, ref, ref_type, false);
232 break;
233 case DexVerifier::VERIFY_ERROR_ACCESS_CLASS:
234 exception_class = "Ljava/lang/IllegalAccessError;";
235 msg = ClassNameFromIndex(method, ref, ref_type, true);
236 break;
237 case DexVerifier::VERIFY_ERROR_ACCESS_FIELD:
238 exception_class = "Ljava/lang/IllegalAccessError;";
239 msg = FieldNameFromIndex(method, ref, ref_type, true);
240 break;
241 case DexVerifier::VERIFY_ERROR_ACCESS_METHOD:
242 exception_class = "Ljava/lang/IllegalAccessError;";
243 msg = MethodNameFromIndex(method, ref, ref_type, true);
244 break;
245 case DexVerifier::VERIFY_ERROR_CLASS_CHANGE:
246 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
247 msg = ClassNameFromIndex(method, ref, ref_type, false);
248 break;
249 case DexVerifier::VERIFY_ERROR_INSTANTIATION:
250 exception_class = "Ljava/lang/InstantiationError;";
251 msg = ClassNameFromIndex(method, ref, ref_type, false);
252 break;
253 case DexVerifier::VERIFY_ERROR_GENERIC:
254 // Generic VerifyError; use default exception, no message.
255 break;
256 case DexVerifier::VERIFY_ERROR_NONE:
257 CHECK(false);
258 break;
259 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700260 self->ThrowNewException(exception_class, msg.c_str());
261 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700262}
263
264extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700265 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700266 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700267 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700268 thread->DeliverException();
269}
270
271extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700272 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700273 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700274 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700275 thread->DeliverException();
276}
277
Elliott Hughese1410a22011-10-04 12:10:24 -0700278extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700279 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
280 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700281 frame.Next();
282 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700283 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
284 MethodNameFromIndex(method, method_idx, DexVerifier::VERIFY_ERROR_REF_METHOD, false).c_str());
285 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700286}
287
288extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700289 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700290 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700291 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700292 thread->DeliverException();
293}
294
Ian Rogersad25ac52011-10-04 19:13:33 -0700295void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, void* sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700296 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700297 // TODO: this code is specific to ARM
298 // On entry the stack pointed by sp is:
299 // | argN | |
300 // | ... | |
301 // | arg4 | |
302 // | arg3 spill | | Caller's frame
303 // | arg2 spill | |
304 // | arg1 spill | |
305 // | Method* | ---
306 // | LR |
307 // | R3 | arg3
308 // | R2 | arg2
309 // | R1 | arg1
310 // | R0 | <- sp
311 uintptr_t* regs = reinterpret_cast<uintptr_t*>(sp);
312 Method** caller_sp = reinterpret_cast<Method**>(&regs[5]);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700313 uintptr_t caller_pc = regs[4];
Ian Rogersad25ac52011-10-04 19:13:33 -0700314 // Record the last top of the managed stack
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700315 thread->SetTopOfStack(caller_sp, caller_pc);
Ian Rogersad25ac52011-10-04 19:13:33 -0700316 // Start new JNI local reference state
317 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700318 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersad25ac52011-10-04 19:13:33 -0700319 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700320 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700321 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
322 size_t shorty_len = strlen(shorty);
323 size_t args_in_regs = shorty_len < 3 ? shorty_len : 3;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700324 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700325 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700326 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700327 // less two as return address may span into next dex instruction
328 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700329 const DexFile& dex_file = Runtime::Current()->GetClassLinker()
330 ->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
331 const DexFile::CodeItem* code = dex_file.GetCodeItem(caller->GetCodeItemOffset());
332 CHECK_LT(dex_pc, code->insns_size_);
333 const Instruction* instr = Instruction::At(reinterpret_cast<const byte*>(&code->insns_[dex_pc]));
334 Instruction::Code instr_code = instr->Opcode();
335 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
336 (instr_code == Instruction::INVOKE_STATIC_RANGE);
337 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
338 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700339 } else {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700340 is_static = type == Runtime::kStaticMethod;
341 }
342 // Handlerize references in registers
343 int cur_arg = 1; // skip method_idx in R0, first arg is in R1
344 if (!is_static) {
345 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
346 cur_arg++;
347 AddLocalReference<jobject>(env, obj);
348 }
349 for(size_t i = 0; i < args_in_regs; i++) {
350 char c = shorty[i + 1]; // offset to skip return value
351 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700352 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
353 AddLocalReference<jobject>(env, obj);
354 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700355 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
356 }
357 // Handlerize references in out going arguments
358 for(size_t i = 3; i < (shorty_len - 1); i++) {
359 char c = shorty[i + 1]; // offset to skip return value
360 if (c == 'L') {
361 // Plus 6 to skip args 1 to 3, LR and Method* plus the start offset of 3 to skip the spills
362 Object* obj = reinterpret_cast<Object*>(regs[i + 6]);
363 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700364 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700365 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700366 }
367 // Resolve method filling in dex cache
368 Method* called = linker->ResolveMethod(method_idx, *caller_sp, true);
369 if (!thread->IsExceptionPending()) {
370 // We got this far, ensure that the declaring class is initialized
371 linker->EnsureInitialized(called->GetDeclaringClass(), true);
372 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700373 void* code;
374 if (thread->IsExceptionPending()) {
375 // Something went wrong, go into deliver exception with the pending exception in r0
376 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
377 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
378 thread->ClearException();
379 } else {
380 // Expect class to at least be initializing
381 CHECK(called->GetDeclaringClass()->IsInitializing());
382 // Set up entry into main method
383 regs[0] = reinterpret_cast<uintptr_t>(called);
384 code = const_cast<void*>(called->GetCode());
385 }
386 return code;
387}
388
Shih-wei Liao2d831012011-09-28 22:06:53 -0700389// TODO: placeholder. Helper function to type
390Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) {
391 /*
392 * Should initialize & fix up method->dex_cache_resolved_types_[].
393 * Returns initialized type. Does not return normally if an exception
394 * is thrown, but instead initiates the catch. Should be similar to
395 * ClassLinker::InitializeStaticStorageFromCode.
396 */
397 UNIMPLEMENTED(FATAL);
398 return NULL;
399}
400
401// TODO: placeholder. Helper function to resolve virtual method
402void ResolveMethodFromCode(Method* method, uint32_t method_idx) {
403 /*
404 * Slow-path handler on invoke virtual method path in which
405 * base method is unresolved at compile-time. Doesn't need to
406 * return anything - just either ensure that
407 * method->dex_cache_resolved_methods_(method_idx) != NULL or
408 * throw and unwind. The caller will restart call sequence
409 * from the beginning.
410 */
411}
412
Ian Rogersce9eca62011-10-07 17:11:03 -0700413Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static) {
414 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
415 Field* f = class_linker->ResolveField(field_idx, referrer, is_static);
416 if (f != NULL) {
417 Class* c = f->GetDeclaringClass();
418 // If the class is already initializing, we must be inside <clinit>, or
419 // we'd still be waiting for the lock.
420 if (c->GetStatus() == Class::kStatusInitializing || class_linker->EnsureInitialized(c, true)) {
421 return f;
422 }
423 }
424 DCHECK(Thread::Current()->IsExceptionPending()); // Throw exception and unwind
425 return NULL;
426}
427
428extern "C" Field* artFindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer,
429 Thread* self, Method** sp) {
430 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
431 return FindFieldFromCode(field_idx, referrer, false);
432}
433
434extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
435 Thread* self, Method** sp) {
436 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
437 Field* field = FindFieldFromCode(field_idx, referrer, true);
438 if (field != NULL) {
439 Class* type = field->GetType();
440 if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int64_t)) {
441 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
442 "Attempted read of 32-bit primitive on field '%s'",
443 PrettyField(field, true).c_str());
444 } else {
445 return field->Get32(NULL);
446 }
447 }
448 return 0; // Will throw exception by checking with Thread::Current
449}
450
451extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
452 Thread* self, Method** sp) {
453 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
454 Field* field = FindFieldFromCode(field_idx, referrer, true);
455 if (field != NULL) {
456 Class* type = field->GetType();
457 if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int64_t)) {
458 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
459 "Attempted read of 64-bit primitive on field '%s'",
460 PrettyField(field, true).c_str());
461 } else {
462 return field->Get64(NULL);
463 }
464 }
465 return 0; // Will throw exception by checking with Thread::Current
466}
467
468extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
469 Thread* self, Method** sp) {
470 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
471 Field* field = FindFieldFromCode(field_idx, referrer, true);
472 if (field != NULL) {
473 Class* type = field->GetType();
474 if (type->IsPrimitive()) {
475 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
476 "Attempted read of reference on primitive field '%s'",
477 PrettyField(field, true).c_str());
478 } else {
479 return field->GetObj(NULL);
480 }
481 }
482 return NULL; // Will throw exception by checking with Thread::Current
483}
484
485extern "C" int artSet32StaticFromCode(uint32_t field_idx, const Method* referrer,
486 uint32_t new_value, Thread* self, Method** sp) {
487 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
488 Field* field = FindFieldFromCode(field_idx, referrer, true);
489 if (field != NULL) {
490 Class* type = field->GetType();
491 if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int32_t)) {
492 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
493 "Attempted write of 32-bit primitive to field '%s'",
494 PrettyField(field, true).c_str());
495 } else {
496 field->Set32(NULL, new_value);
497 return 0; // success
498 }
499 }
500 return -1; // failure
501}
502
503extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
504 uint64_t new_value, Thread* self, Method** sp) {
505 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
506 Field* field = FindFieldFromCode(field_idx, referrer, true);
507 if (field != NULL) {
508 Class* type = field->GetType();
509 if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int64_t)) {
510 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
511 "Attempted write of 64-bit primitive to field '%s'",
512 PrettyField(field, true).c_str());
513 } else {
514 field->Set64(NULL, new_value);
515 return 0; // success
516 }
517 }
518 return -1; // failure
519}
520
521extern "C" int artSetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
522 Object* new_value, Thread* self, Method** sp) {
523 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
524 Field* field = FindFieldFromCode(field_idx, referrer, true);
525 if (field != NULL) {
526 Class* type = field->GetType();
527 if (type->IsPrimitive()) {
528 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
529 "Attempted write of reference to primitive field '%s'",
530 PrettyField(field, true).c_str());
531 } else {
532 field->SetObj(NULL, new_value);
533 return 0; // success
534 }
535 }
536 return -1; // failure
537}
538
Shih-wei Liao2d831012011-09-28 22:06:53 -0700539// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
540// cannot be resolved, throw an error. If it can, use it to create an instance.
buzbee33a129c2011-10-06 16:53:20 -0700541extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700542 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700543 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700544 Runtime* runtime = Runtime::Current();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700545 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700546 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700547 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700548 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700549 return NULL; // Failure
550 }
551 }
buzbee33a129c2011-10-06 16:53:20 -0700552 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
553 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700554 return NULL; // Failure
555 }
556 return klass->AllocObject();
557}
558
Ian Rogersce9eca62011-10-07 17:11:03 -0700559Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
560 Thread* self) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700561 if (component_count < 0) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700562 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700563 return NULL; // Failure
564 }
565 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
566 if (klass == NULL) { // Not in dex cache so try to resolve
567 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
568 if (klass == NULL) { // Error
569 DCHECK(Thread::Current()->IsExceptionPending());
570 return NULL; // Failure
571 }
572 }
573 if (klass->IsPrimitive() && !klass->IsPrimitiveInt()) {
574 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700575 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700576 "Bad filled array request for type %s",
577 PrettyDescriptor(klass->GetDescriptor()).c_str());
578 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700579 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700580 "Found type %s; filled-new-array not implemented for anything but \'int\'",
581 PrettyDescriptor(klass->GetDescriptor()).c_str());
582 }
583 return NULL; // Failure
584 } else {
585 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
586 return Array::Alloc(klass, component_count);
587 }
588}
589
Ian Rogersce9eca62011-10-07 17:11:03 -0700590// Helper function to alloc array for OP_FILLED_NEW_ARRAY
591extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
592 int32_t component_count, Thread* self, Method** sp) {
593 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
594 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self);
595}
596
Shih-wei Liao2d831012011-09-28 22:06:53 -0700597// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
598// it cannot be resolved, throw an error. If it can, use it to create an array.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700599extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
600 Thread* self, Method** sp) {
601 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700602 if (component_count < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700603 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700604 component_count);
605 return NULL; // Failure
606 }
607 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
608 if (klass == NULL) { // Not in dex cache so try to resolve
609 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
610 if (klass == NULL) { // Error
611 DCHECK(Thread::Current()->IsExceptionPending());
612 return NULL; // Failure
613 }
614 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
615 }
616 return Array::Alloc(klass, component_count);
617}
618
619// 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 -0700620extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
621 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700622 DCHECK(a->IsClass()) << PrettyClass(a);
623 DCHECK(b->IsClass()) << PrettyClass(b);
624 if (b->IsAssignableFrom(a)) {
625 return 0; // Success
626 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700627 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700628 "%s cannot be cast to %s",
629 PrettyDescriptor(a->GetDescriptor()).c_str(),
630 PrettyDescriptor(b->GetDescriptor()).c_str());
631 return -1; // Failure
632 }
633}
634
635// Tests whether 'element' can be assigned into an array of type 'array_class'.
636// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700637extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
638 Thread* self, Method** sp) {
639 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700640 DCHECK(array_class != NULL);
641 // element can't be NULL as we catch this is screened in runtime_support
642 Class* element_class = element->GetClass();
643 Class* component_type = array_class->GetComponentType();
644 if (component_type->IsAssignableFrom(element_class)) {
645 return 0; // Success
646 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700647 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700648 "Cannot store an object of type %s in to an array of type %s",
649 PrettyDescriptor(element_class->GetDescriptor()).c_str(),
650 PrettyDescriptor(array_class->GetDescriptor()).c_str());
651 return -1; // Failure
652 }
653}
654
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700655Class* InitializeStaticStorage(uint32_t type_idx, const Method* referrer, Thread* self) {
656 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
657 Class* klass = class_linker->ResolveType(type_idx, referrer);
658 if (klass == NULL) {
659 CHECK(self->IsExceptionPending());
660 return NULL; // Failure - Indicate to caller to deliver exception
661 }
662 // If we are the <clinit> of this class, just return our storage.
663 //
664 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
665 // running.
666 if (klass == referrer->GetDeclaringClass() && referrer->IsClassInitializer()) {
667 return klass;
668 }
669 if (!class_linker->EnsureInitialized(klass, true)) {
670 CHECK(self->IsExceptionPending());
671 return NULL; // Failure - Indicate to caller to deliver exception
672 }
673 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
674 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700675}
676
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700677extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
678 Thread* self, Method** sp) {
679 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
680 return InitializeStaticStorage(type_idx, referrer, self);
681}
682
Brian Carlstromaded5f72011-10-07 17:15:04 -0700683String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
684 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
685 return class_linker->ResolveString(string_idx, referrer);
686}
687
688extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
689 Thread* self, Method** sp) {
690 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
691 return ResolveStringFromCode(referrer, string_idx);
692}
693
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700694extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
695 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700696 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700697 // MonitorExit may throw exception
698 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
699}
700
701extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
702 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
703 DCHECK(obj != NULL); // Assumed to have been checked before entry
704 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700705 DCHECK(thread->HoldsLock(obj));
706 // Only possible exception is NPE and is handled before entry
707 DCHECK(!thread->IsExceptionPending());
708}
709
Ian Rogers4a510d82011-10-09 14:30:24 -0700710void CheckSuspendFromCode(Thread* thread) {
711 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700712 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
713}
714
Ian Rogers4a510d82011-10-09 14:30:24 -0700715extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
716 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700717 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700718 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
719}
720
721/*
722 * Fill the array with predefined constant values, throwing exceptions if the array is null or
723 * not of sufficient length.
724 *
725 * NOTE: When dealing with a raw dex file, the data to be copied uses
726 * little-endian ordering. Require that oat2dex do any required swapping
727 * so this routine can get by with a memcpy().
728 *
729 * Format of the data:
730 * ushort ident = 0x0300 magic value
731 * ushort width width of each element in the table
732 * uint size number of elements in the table
733 * ubyte data[size*width] table of data values (may contain a single-byte
734 * padding at the end)
735 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700736extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
737 Thread* self, Method** sp) {
738 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700739 DCHECK_EQ(table[0], 0x0300);
740 if (array == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700741 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
742 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700743 return -1; // Error
744 }
745 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
746 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
747 if (static_cast<int32_t>(size) > array->GetLength()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700748 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
749 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700750 return -1; // Error
751 }
752 uint16_t width = table[1];
753 uint32_t size_in_bytes = size * width;
754 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
755 return 0; // Success
756}
757
758// See comments in runtime_support_asm.S
759extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx,
760 Object* this_object ,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700761 Thread* thread, Method** sp) {
762 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700763 if (this_object == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700764 thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
765 "null receiver during interface dispatch");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700766 return 0;
767 }
768 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700769 Frame frame = thread->GetTopOfStack(); // Compute calling method
770 frame.Next();
771 Method* caller_method = frame.GetMethod();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700772 Method* interface_method = class_linker->ResolveMethod(method_idx, caller_method, false);
773 if (interface_method == NULL) {
774 // Could not resolve interface method. Throw error and unwind
775 CHECK(thread->IsExceptionPending());
776 return 0;
777 }
778 Method* method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
779 if (method == NULL) {
780 CHECK(thread->IsExceptionPending());
781 return 0;
782 }
783 const void* code = method->GetCode();
784
785 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
786 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
787 uint64_t result = ((code_uint << 32) | method_uint);
788 return result;
789}
790
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700791// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
792// which is responsible for recording callee save registers. We explicitly handlerize incoming
793// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
794// the invocation handler which is a field within the proxy object receiver.
795extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
796 byte* stack_args, Thread* self) {
797 // Register the top of the managed stack
798 self->SetTopOfStack(reinterpret_cast<Method**>(stack_args + 8), 0);
799 // TODO: ARM specific
800 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
801 // Start new JNI local reference state
802 JNIEnvExt* env = self->GetJniEnv();
803 ScopedJniEnvLocalRefState env_state(env);
804 // Create local ref. copies of proxy method and the receiver
805 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
806 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
807
808 // Place incoming objects in local reference table, replacing original Object* with jobject
809 size_t num_args = proxy_method->NumArgs();
810 if (num_args > 1) {
811 if (proxy_method->IsParamAReference(1)) {
812 Object* obj = *reinterpret_cast<Object**>(stack_args); // reference from r1
813 jobject jobj = AddLocalReference<jobject>(env, obj);
814 *reinterpret_cast<jobject*>(stack_args) = jobj;
815 }
816 if (num_args > 2) {
817 if (proxy_method->IsParamAReference(2)) {
818 Object* obj = *reinterpret_cast<Object**>(stack_args + kPointerSize); // reference from r2
819 jobject jobj = AddLocalReference<jobject>(env, obj);
820 *reinterpret_cast<jobject*>(stack_args + kPointerSize) = jobj;
821 }
822 // Possible out arguments are 7 words above the stack args:
823 // r2, r3, LR, Method*, r1 (spill), r2 (spill), r3 (spill)
824 size_t offset = 7 * kPointerSize;
825 for (size_t i = 3; i < num_args; i++) {
826 if (proxy_method->IsParamAReference(i)) {
827 // reference from caller's stack arguments
828 Object* obj = *reinterpret_cast<Object**>(stack_args + offset);
829 jobject jobj = AddLocalReference<jobject>(env, obj);
830 *reinterpret_cast<jobject*>(stack_args + offset) = jobj;
831 } else if (proxy_method->IsParamALongOrDouble(i)) {
832 offset += kPointerSize;
833 }
834 offset += kPointerSize;
835 }
836 }
837 }
838 // Create args array
839 ObjectArray<Object>* args =
840 Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_args - 1);
841 if(args == NULL) {
842 CHECK(self->IsExceptionPending());
843 return;
844 }
845 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
846 jvalue args_jobj[3];
847 args_jobj[0].l = rcvr_jobj;
848 args_jobj[1].l = proxy_method_jobj;
849 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
850 // Box arguments
851 ObjectArray<Class>* param_types = proxy_method->GetJavaParameterTypes();
852 if (num_args > 1) {
853 CHECK(param_types != NULL);
854 Object* obj;
855 // Argument from r2
856 Class* param_type = param_types->Get(0);
857 if (!param_type->IsPrimitive()) {
858 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args));
859 } else {
860 JValue val = *reinterpret_cast<JValue*>(stack_args);
861 BoxPrimitive(env, param_type, val);
862 if (self->IsExceptionPending()) {
863 return;
864 }
865 obj = val.l;
866 }
867 args->Set(0, obj);
868 if (num_args > 2) {
869 // Argument from r3
870 param_type = param_types->Get(1);
871 if (!param_type->IsPrimitive()) {
872 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + kPointerSize));
873 } else {
874 JValue val = *reinterpret_cast<JValue*>(stack_args + kPointerSize);
875 BoxPrimitive(env, param_type, val);
876 if (self->IsExceptionPending()) {
877 return;
878 }
879 obj = val.l;
880 }
881 args->Set(1, obj);
882 // Arguments are on the stack, again offset to out arguments is 7 words
883 size_t offset = 7 * kPointerSize;
884 for (size_t i = 3; i < num_args && !self->IsExceptionPending(); i++) {
885 param_type = param_types->Get(i - 1);
886 if (proxy_method->IsParamAReference(i)) {
887 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + offset));
888 } else {
889 JValue val = *reinterpret_cast<JValue*>(stack_args + offset);
890 BoxPrimitive(env, param_type, val);
891 if (self->IsExceptionPending()) {
892 return;
893 }
894 obj = val.l;
895 if (proxy_method->IsParamALongOrDouble(i)) {
896 offset += kPointerSize;
897 }
898 }
899 args->Set(i - 1, obj);
900 offset += kPointerSize;
901 }
902 }
903 }
904 // Get the InvocationHandler method and the field that holds it within the Proxy object
905 static jmethodID inv_hand_invoke_mid = NULL;
906 static jfieldID proxy_inv_hand_fid = NULL;
907 if (proxy_inv_hand_fid == NULL) {
908 ScopedLocalRef<jclass> proxy(env, env->FindClass("java.lang.reflect.Proxy"));
909 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
910 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java.lang.reflect.InvocationHandler"));
911 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
912 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
913 }
914 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java.lang.reflect.Proxy")));
915 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
916 // Call InvocationHandler.invoke
917 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
918 // Place result in stack args
919 if (!self->IsExceptionPending()) {
920 Object* result_ref = self->DecodeJObject(result);
921 if (result_ref != NULL) {
922 JValue result_unboxed;
923 UnboxPrimitive(env, result_ref, proxy_method->GetReturnType(), result_unboxed);
924 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
925 } else {
926 *reinterpret_cast<jobject*>(stack_args) = NULL;
927 }
928 }
929}
930
Shih-wei Liao2d831012011-09-28 22:06:53 -0700931/*
932 * Float/double conversion requires clamping to min and max of integer form. If
933 * target doesn't support this normally, use these.
934 */
935int64_t D2L(double d) {
936 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
937 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
938 if (d >= kMaxLong)
939 return (int64_t)0x7fffffffffffffffULL;
940 else if (d <= kMinLong)
941 return (int64_t)0x8000000000000000ULL;
942 else if (d != d) // NaN case
943 return 0;
944 else
945 return (int64_t)d;
946}
947
948int64_t F2L(float f) {
949 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
950 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
951 if (f >= kMaxLong)
952 return (int64_t)0x7fffffffffffffffULL;
953 else if (f <= kMinLong)
954 return (int64_t)0x8000000000000000ULL;
955 else if (f != f) // NaN case
956 return 0;
957 else
958 return (int64_t)f;
959}
960
961} // namespace art