Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 19 | #include "dex_verifier.h" |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 20 | #include "reflection.h" |
| 21 | #include "ScopedLocalRef.h" |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 22 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 23 | namespace art { |
| 24 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 25 | // Place a special frame at the TOS that will save the callee saves for the given type |
| 26 | static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 27 | // Be aware the store below may well stomp on an incoming argument |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 28 | *sp = Runtime::Current()->GetCalleeSaveMethod(type); |
| 29 | self->SetTopOfStack(sp, 0); |
| 30 | } |
| 31 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 32 | // Temporary debugging hook for compiler. |
| 33 | extern 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 |
| 42 | extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) { |
Brian Carlstrom | 6f495f2 | 2011-10-10 15:05:03 -0700 | [diff] [blame] | 43 | if (thread->IsExceptionPending()) { |
| 44 | return NULL; |
| 45 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 46 | return thread->DecodeJObject(obj); |
| 47 | } |
| 48 | |
| 49 | extern 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 |
| 69 | extern "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 Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 77 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 78 | 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 |
| 87 | extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 88 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 89 | thread->DeliverException(); |
| 90 | } |
| 91 | |
| 92 | // Called by generated call to throw a NPE exception |
| 93 | extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 94 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 95 | thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 96 | thread->DeliverException(); |
| 97 | } |
| 98 | |
| 99 | // Called by generated call to throw an arithmetic divide by zero exception |
| 100 | extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 101 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 102 | 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 |
| 107 | extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 108 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
| 109 | thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 110 | "length=%d; index=%d", limit, index); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 111 | thread->DeliverException(); |
| 112 | } |
| 113 | |
| 114 | // Called by the AbstractMethodError stub (not runtime support) |
| 115 | extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 116 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
| 117 | thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;", |
| 118 | "abstract method \"%s\"", PrettyMethod(method).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 119 | thread->DeliverException(); |
| 120 | } |
| 121 | |
| 122 | extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 123 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 124 | thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 125 | thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;", |
| 126 | "stack size %zdkb; default stack size: %zdkb", |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 127 | thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 128 | thread->ResetDefaultStackEnd(); // Return to default stack size |
| 129 | thread->DeliverException(); |
| 130 | } |
| 131 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 132 | static std::string ClassNameFromIndex(Method* method, uint32_t ref, |
| 133 | DexVerifier::VerifyErrorRefType ref_type, bool access) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 134 | 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 Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 163 | static std::string FieldNameFromIndex(const Method* method, uint32_t ref, |
| 164 | DexVerifier::VerifyErrorRefType ref_type, bool access) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 165 | 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 Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 185 | static std::string MethodNameFromIndex(const Method* method, uint32_t ref, |
| 186 | DexVerifier::VerifyErrorRefType ref_type, bool access) { |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 187 | 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 Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 201 | result += class_name + "." + method_name + ":" + |
| 202 | dex_file.CreateMethodDescriptor(id.proto_idx_, NULL); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 203 | result += " from class "; |
| 204 | result += PrettyDescriptor(method->GetDeclaringClass()->GetDescriptor()); |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 209 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll); |
| 210 | Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref' |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 211 | frame.Next(); |
| 212 | Method* method = frame.GetMethod(); |
| 213 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 214 | DexVerifier::VerifyErrorRefType ref_type = |
| 215 | static_cast<DexVerifier::VerifyErrorRefType>(kind >> kVerifyErrorRefTypeShift); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 216 | |
| 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 Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 260 | self->ThrowNewException(exception_class, msg.c_str()); |
| 261 | self->DeliverException(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 265 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 266 | LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 267 | thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 268 | thread->DeliverException(); |
| 269 | } |
| 270 | |
| 271 | extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 272 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 273 | LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 274 | thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 275 | thread->DeliverException(); |
| 276 | } |
| 277 | |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 278 | extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 279 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll); |
| 280 | Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 281 | frame.Next(); |
| 282 | Method* method = frame.GetMethod(); |
Elliott Hughes | e1410a2 | 2011-10-04 12:10:24 -0700 | [diff] [blame] | 283 | self->ThrowNewException("Ljava/lang/NoSuchMethodError;", |
| 284 | MethodNameFromIndex(method, method_idx, DexVerifier::VERIFY_ERROR_REF_METHOD, false).c_str()); |
| 285 | self->DeliverException(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 289 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 290 | LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode"; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 291 | thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 292 | thread->DeliverException(); |
| 293 | } |
| 294 | |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 295 | void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, void* sp, Thread* thread, |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 296 | Runtime::TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 297 | // 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**>(®s[5]); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 313 | uintptr_t caller_pc = regs[4]; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 314 | // Record the last top of the managed stack |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 315 | thread->SetTopOfStack(caller_sp, caller_pc); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 316 | // Start new JNI local reference state |
| 317 | JNIEnvExt* env = thread->GetJniEnv(); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 318 | ScopedJniEnvLocalRefState env_state(env); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 319 | // Discover shorty (avoid GCs) |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 320 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 321 | const char* shorty = linker->MethodShorty(method_idx, *caller_sp); |
| 322 | size_t shorty_len = strlen(shorty); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 323 | size_t args_in_regs = 0; |
| 324 | for (size_t i = 1; i < shorty_len; i++) { |
| 325 | char c = shorty[i]; |
| 326 | args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1); |
| 327 | if (args_in_regs > 3) { |
| 328 | args_in_regs = 3; |
| 329 | break; |
| 330 | } |
| 331 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 332 | bool is_static; |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 333 | if (type == Runtime::kUnknownMethod) { |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 334 | Method* caller = *caller_sp; |
Ian Rogers | df9a782 | 2011-10-11 16:53:22 -0700 | [diff] [blame] | 335 | // less two as return address may span into next dex instruction |
| 336 | uint32_t dex_pc = caller->ToDexPC(caller_pc - 2); |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 337 | const DexFile& dex_file = Runtime::Current()->GetClassLinker() |
| 338 | ->FindDexFile(caller->GetDeclaringClass()->GetDexCache()); |
| 339 | const DexFile::CodeItem* code = dex_file.GetCodeItem(caller->GetCodeItemOffset()); |
| 340 | CHECK_LT(dex_pc, code->insns_size_); |
| 341 | const Instruction* instr = Instruction::At(reinterpret_cast<const byte*>(&code->insns_[dex_pc])); |
| 342 | Instruction::Code instr_code = instr->Opcode(); |
| 343 | is_static = (instr_code == Instruction::INVOKE_STATIC) || |
| 344 | (instr_code == Instruction::INVOKE_STATIC_RANGE); |
| 345 | DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) || |
| 346 | (instr_code == Instruction::INVOKE_DIRECT_RANGE)); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 347 | } else { |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 348 | is_static = type == Runtime::kStaticMethod; |
| 349 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 350 | // Placing into local references incoming arguments from the caller's register arguments |
| 351 | size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1 |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 352 | if (!is_static) { |
| 353 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
| 354 | cur_arg++; |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 355 | if (args_in_regs < 3) { |
| 356 | // If we thought we had fewer than 3 arguments in registers, account for the receiver |
| 357 | args_in_regs++; |
| 358 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 359 | AddLocalReference<jobject>(env, obj); |
| 360 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 361 | size_t shorty_index = 1; // skip return value |
| 362 | // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip |
| 363 | // R0) |
| 364 | while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) { |
| 365 | char c = shorty[shorty_index]; |
| 366 | shorty_index++; |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 367 | if (c == 'L') { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 368 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
| 369 | AddLocalReference<jobject>(env, obj); |
| 370 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 371 | cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1); |
| 372 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 373 | // Placing into local references incoming arguments from the caller's stack arguments |
| 374 | cur_arg += 5; // skip LR, Method* and spills for R1 to R3 |
| 375 | while (shorty_index < shorty_len) { |
| 376 | char c = shorty[shorty_index]; |
| 377 | shorty_index++; |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 378 | if (c == 'L') { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 379 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 380 | AddLocalReference<jobject>(env, obj); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 381 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 382 | cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 383 | } |
| 384 | // Resolve method filling in dex cache |
| 385 | Method* called = linker->ResolveMethod(method_idx, *caller_sp, true); |
| 386 | if (!thread->IsExceptionPending()) { |
| 387 | // We got this far, ensure that the declaring class is initialized |
| 388 | linker->EnsureInitialized(called->GetDeclaringClass(), true); |
| 389 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 390 | void* code; |
| 391 | if (thread->IsExceptionPending()) { |
| 392 | // Something went wrong, go into deliver exception with the pending exception in r0 |
| 393 | code = reinterpret_cast<void*>(art_deliver_exception_from_code); |
| 394 | regs[0] = reinterpret_cast<uintptr_t>(thread->GetException()); |
| 395 | thread->ClearException(); |
| 396 | } else { |
| 397 | // Expect class to at least be initializing |
| 398 | CHECK(called->GetDeclaringClass()->IsInitializing()); |
| 399 | // Set up entry into main method |
| 400 | regs[0] = reinterpret_cast<uintptr_t>(called); |
| 401 | code = const_cast<void*>(called->GetCode()); |
| 402 | } |
| 403 | return code; |
| 404 | } |
| 405 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 406 | // TODO: placeholder. Helper function to type |
| 407 | Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) { |
| 408 | /* |
| 409 | * Should initialize & fix up method->dex_cache_resolved_types_[]. |
| 410 | * Returns initialized type. Does not return normally if an exception |
| 411 | * is thrown, but instead initiates the catch. Should be similar to |
| 412 | * ClassLinker::InitializeStaticStorageFromCode. |
| 413 | */ |
| 414 | UNIMPLEMENTED(FATAL); |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | // TODO: placeholder. Helper function to resolve virtual method |
| 419 | void ResolveMethodFromCode(Method* method, uint32_t method_idx) { |
| 420 | /* |
| 421 | * Slow-path handler on invoke virtual method path in which |
| 422 | * base method is unresolved at compile-time. Doesn't need to |
| 423 | * return anything - just either ensure that |
| 424 | * method->dex_cache_resolved_methods_(method_idx) != NULL or |
| 425 | * throw and unwind. The caller will restart call sequence |
| 426 | * from the beginning. |
| 427 | */ |
| 428 | } |
| 429 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 430 | Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static) { |
| 431 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 432 | Field* f = class_linker->ResolveField(field_idx, referrer, is_static); |
| 433 | if (f != NULL) { |
| 434 | Class* c = f->GetDeclaringClass(); |
| 435 | // If the class is already initializing, we must be inside <clinit>, or |
| 436 | // we'd still be waiting for the lock. |
| 437 | if (c->GetStatus() == Class::kStatusInitializing || class_linker->EnsureInitialized(c, true)) { |
| 438 | return f; |
| 439 | } |
| 440 | } |
| 441 | DCHECK(Thread::Current()->IsExceptionPending()); // Throw exception and unwind |
| 442 | return NULL; |
| 443 | } |
| 444 | |
| 445 | extern "C" Field* artFindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer, |
| 446 | Thread* self, Method** sp) { |
| 447 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 448 | return FindFieldFromCode(field_idx, referrer, false); |
| 449 | } |
| 450 | |
| 451 | extern "C" uint32_t artGet32StaticFromCode(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 32-bit primitive on field '%s'", |
| 460 | PrettyField(field, true).c_str()); |
| 461 | } else { |
| 462 | return field->Get32(NULL); |
| 463 | } |
| 464 | } |
| 465 | return 0; // Will throw exception by checking with Thread::Current |
| 466 | } |
| 467 | |
| 468 | extern "C" uint64_t artGet64StaticFromCode(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() || type->PrimitiveSize() != sizeof(int64_t)) { |
| 475 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 476 | "Attempted read of 64-bit primitive on field '%s'", |
| 477 | PrettyField(field, true).c_str()); |
| 478 | } else { |
| 479 | return field->Get64(NULL); |
| 480 | } |
| 481 | } |
| 482 | return 0; // Will throw exception by checking with Thread::Current |
| 483 | } |
| 484 | |
| 485 | extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer, |
| 486 | 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()) { |
| 492 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 493 | "Attempted read of reference on primitive field '%s'", |
| 494 | PrettyField(field, true).c_str()); |
| 495 | } else { |
| 496 | return field->GetObj(NULL); |
| 497 | } |
| 498 | } |
| 499 | return NULL; // Will throw exception by checking with Thread::Current |
| 500 | } |
| 501 | |
| 502 | extern "C" int artSet32StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 503 | uint32_t new_value, Thread* self, Method** sp) { |
| 504 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 505 | Field* field = FindFieldFromCode(field_idx, referrer, true); |
| 506 | if (field != NULL) { |
| 507 | Class* type = field->GetType(); |
| 508 | if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int32_t)) { |
| 509 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 510 | "Attempted write of 32-bit primitive to field '%s'", |
| 511 | PrettyField(field, true).c_str()); |
| 512 | } else { |
| 513 | field->Set32(NULL, new_value); |
| 514 | return 0; // success |
| 515 | } |
| 516 | } |
| 517 | return -1; // failure |
| 518 | } |
| 519 | |
| 520 | extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 521 | uint64_t new_value, Thread* self, Method** sp) { |
| 522 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 523 | Field* field = FindFieldFromCode(field_idx, referrer, true); |
| 524 | if (field != NULL) { |
| 525 | Class* type = field->GetType(); |
| 526 | if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int64_t)) { |
| 527 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 528 | "Attempted write of 64-bit primitive to field '%s'", |
| 529 | PrettyField(field, true).c_str()); |
| 530 | } else { |
| 531 | field->Set64(NULL, new_value); |
| 532 | return 0; // success |
| 533 | } |
| 534 | } |
| 535 | return -1; // failure |
| 536 | } |
| 537 | |
| 538 | extern "C" int artSetObjStaticFromCode(uint32_t field_idx, const Method* referrer, |
| 539 | Object* new_value, Thread* self, Method** sp) { |
| 540 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 541 | Field* field = FindFieldFromCode(field_idx, referrer, true); |
| 542 | if (field != NULL) { |
| 543 | Class* type = field->GetType(); |
| 544 | if (type->IsPrimitive()) { |
| 545 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 546 | "Attempted write of reference to primitive field '%s'", |
| 547 | PrettyField(field, true).c_str()); |
| 548 | } else { |
| 549 | field->SetObj(NULL, new_value); |
| 550 | return 0; // success |
| 551 | } |
| 552 | } |
| 553 | return -1; // failure |
| 554 | } |
| 555 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 556 | // Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it |
| 557 | // cannot be resolved, throw an error. If it can, use it to create an instance. |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 558 | extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self, Method** sp) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 559 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 560 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 561 | Runtime* runtime = Runtime::Current(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 562 | if (klass == NULL) { |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 563 | klass = runtime->GetClassLinker()->ResolveType(type_idx, method); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 564 | if (klass == NULL) { |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 565 | DCHECK(self->IsExceptionPending()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 566 | return NULL; // Failure |
| 567 | } |
| 568 | } |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 569 | if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) { |
| 570 | DCHECK(self->IsExceptionPending()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 571 | return NULL; // Failure |
| 572 | } |
| 573 | return klass->AllocObject(); |
| 574 | } |
| 575 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 576 | Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
| 577 | Thread* self) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 578 | if (component_count < 0) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 579 | self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 580 | return NULL; // Failure |
| 581 | } |
| 582 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 583 | if (klass == NULL) { // Not in dex cache so try to resolve |
| 584 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 585 | if (klass == NULL) { // Error |
| 586 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 587 | return NULL; // Failure |
| 588 | } |
| 589 | } |
| 590 | if (klass->IsPrimitive() && !klass->IsPrimitiveInt()) { |
| 591 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 592 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 593 | "Bad filled array request for type %s", |
| 594 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 595 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 596 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 597 | "Found type %s; filled-new-array not implemented for anything but \'int\'", |
| 598 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 599 | } |
| 600 | return NULL; // Failure |
| 601 | } else { |
| 602 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 603 | return Array::Alloc(klass, component_count); |
| 604 | } |
| 605 | } |
| 606 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 607 | // Helper function to alloc array for OP_FILLED_NEW_ARRAY |
| 608 | extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, |
| 609 | int32_t component_count, Thread* self, Method** sp) { |
| 610 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 611 | return CheckAndAllocArrayFromCode(type_idx, method, component_count, self); |
| 612 | } |
| 613 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 614 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 615 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 616 | extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
| 617 | Thread* self, Method** sp) { |
| 618 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 619 | if (component_count < 0) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 620 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 621 | component_count); |
| 622 | return NULL; // Failure |
| 623 | } |
| 624 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 625 | if (klass == NULL) { // Not in dex cache so try to resolve |
| 626 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 627 | if (klass == NULL) { // Error |
| 628 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 629 | return NULL; // Failure |
| 630 | } |
| 631 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 632 | } |
| 633 | return Array::Alloc(klass, component_count); |
| 634 | } |
| 635 | |
| 636 | // Check whether it is safe to cast one class to the other, throw exception and return -1 on failure |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 637 | extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) { |
| 638 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 639 | DCHECK(a->IsClass()) << PrettyClass(a); |
| 640 | DCHECK(b->IsClass()) << PrettyClass(b); |
| 641 | if (b->IsAssignableFrom(a)) { |
| 642 | return 0; // Success |
| 643 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 644 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 645 | "%s cannot be cast to %s", |
| 646 | PrettyDescriptor(a->GetDescriptor()).c_str(), |
| 647 | PrettyDescriptor(b->GetDescriptor()).c_str()); |
| 648 | return -1; // Failure |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // Tests whether 'element' can be assigned into an array of type 'array_class'. |
| 653 | // Returns 0 on success and -1 if an exception is pending. |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 654 | extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class, |
| 655 | Thread* self, Method** sp) { |
| 656 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 657 | DCHECK(array_class != NULL); |
| 658 | // element can't be NULL as we catch this is screened in runtime_support |
| 659 | Class* element_class = element->GetClass(); |
| 660 | Class* component_type = array_class->GetComponentType(); |
| 661 | if (component_type->IsAssignableFrom(element_class)) { |
| 662 | return 0; // Success |
| 663 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 664 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 665 | "Cannot store an object of type %s in to an array of type %s", |
| 666 | PrettyDescriptor(element_class->GetDescriptor()).c_str(), |
| 667 | PrettyDescriptor(array_class->GetDescriptor()).c_str()); |
| 668 | return -1; // Failure |
| 669 | } |
| 670 | } |
| 671 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 672 | Class* InitializeStaticStorage(uint32_t type_idx, const Method* referrer, Thread* self) { |
| 673 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 674 | Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 675 | if (klass == NULL) { |
| 676 | CHECK(self->IsExceptionPending()); |
| 677 | return NULL; // Failure - Indicate to caller to deliver exception |
| 678 | } |
| 679 | // If we are the <clinit> of this class, just return our storage. |
| 680 | // |
| 681 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 682 | // running. |
| 683 | if (klass == referrer->GetDeclaringClass() && referrer->IsClassInitializer()) { |
| 684 | return klass; |
| 685 | } |
| 686 | if (!class_linker->EnsureInitialized(klass, true)) { |
| 687 | CHECK(self->IsExceptionPending()); |
| 688 | return NULL; // Failure - Indicate to caller to deliver exception |
| 689 | } |
| 690 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
| 691 | return klass; |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 694 | extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer, |
| 695 | Thread* self, Method** sp) { |
| 696 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 697 | return InitializeStaticStorage(type_idx, referrer, self); |
| 698 | } |
| 699 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 700 | String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) { |
| 701 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 702 | return class_linker->ResolveString(string_idx, referrer); |
| 703 | } |
| 704 | |
| 705 | extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx, |
| 706 | Thread* self, Method** sp) { |
| 707 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 708 | return ResolveStringFromCode(referrer, string_idx); |
| 709 | } |
| 710 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 711 | extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) { |
| 712 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 713 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 714 | // MonitorExit may throw exception |
| 715 | return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */; |
| 716 | } |
| 717 | |
| 718 | extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) { |
| 719 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly); |
| 720 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
| 721 | obj->MonitorEnter(thread); // May block |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 722 | DCHECK(thread->HoldsLock(obj)); |
| 723 | // Only possible exception is NPE and is handled before entry |
| 724 | DCHECK(!thread->IsExceptionPending()); |
| 725 | } |
| 726 | |
Ian Rogers | 4a510d8 | 2011-10-09 14:30:24 -0700 | [diff] [blame] | 727 | void CheckSuspendFromCode(Thread* thread) { |
| 728 | // Called when thread->suspend_count_ != 0 |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 729 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
| 730 | } |
| 731 | |
Ian Rogers | 4a510d8 | 2011-10-09 14:30:24 -0700 | [diff] [blame] | 732 | extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) { |
| 733 | // Called when suspend count check value is 0 and thread->suspend_count_ != 0 |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 734 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 735 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Fill the array with predefined constant values, throwing exceptions if the array is null or |
| 740 | * not of sufficient length. |
| 741 | * |
| 742 | * NOTE: When dealing with a raw dex file, the data to be copied uses |
| 743 | * little-endian ordering. Require that oat2dex do any required swapping |
| 744 | * so this routine can get by with a memcpy(). |
| 745 | * |
| 746 | * Format of the data: |
| 747 | * ushort ident = 0x0300 magic value |
| 748 | * ushort width width of each element in the table |
| 749 | * uint size number of elements in the table |
| 750 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 751 | * padding at the end) |
| 752 | */ |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 753 | extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table, |
| 754 | Thread* self, Method** sp) { |
| 755 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 756 | DCHECK_EQ(table[0], 0x0300); |
| 757 | if (array == NULL) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 758 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 759 | "null array in fill array"); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 760 | return -1; // Error |
| 761 | } |
| 762 | DCHECK(array->IsArrayInstance() && !array->IsObjectArray()); |
| 763 | uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16); |
| 764 | if (static_cast<int32_t>(size) > array->GetLength()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 765 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 766 | "failed array fill. length=%d; index=%d", array->GetLength(), size); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 767 | return -1; // Error |
| 768 | } |
| 769 | uint16_t width = table[1]; |
| 770 | uint32_t size_in_bytes = size * width; |
| 771 | memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes); |
| 772 | return 0; // Success |
| 773 | } |
| 774 | |
| 775 | // See comments in runtime_support_asm.S |
| 776 | extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx, |
| 777 | Object* this_object , |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 778 | Thread* thread, Method** sp) { |
| 779 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 780 | if (this_object == NULL) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 781 | thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 782 | "null receiver during interface dispatch"); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 783 | return 0; |
| 784 | } |
| 785 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 786 | Frame frame = thread->GetTopOfStack(); // Compute calling method |
| 787 | frame.Next(); |
| 788 | Method* caller_method = frame.GetMethod(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 789 | Method* interface_method = class_linker->ResolveMethod(method_idx, caller_method, false); |
| 790 | if (interface_method == NULL) { |
| 791 | // Could not resolve interface method. Throw error and unwind |
| 792 | CHECK(thread->IsExceptionPending()); |
| 793 | return 0; |
| 794 | } |
| 795 | Method* method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method); |
| 796 | if (method == NULL) { |
| 797 | CHECK(thread->IsExceptionPending()); |
| 798 | return 0; |
| 799 | } |
| 800 | const void* code = method->GetCode(); |
| 801 | |
| 802 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 803 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 804 | uint64_t result = ((code_uint << 32) | method_uint); |
| 805 | return result; |
| 806 | } |
| 807 | |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 808 | // Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method |
| 809 | // which is responsible for recording callee save registers. We explicitly handlerize incoming |
| 810 | // reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke |
| 811 | // the invocation handler which is a field within the proxy object receiver. |
| 812 | extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver, |
| 813 | byte* stack_args, Thread* self) { |
| 814 | // Register the top of the managed stack |
| 815 | self->SetTopOfStack(reinterpret_cast<Method**>(stack_args + 8), 0); |
| 816 | // TODO: ARM specific |
| 817 | DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u); |
| 818 | // Start new JNI local reference state |
| 819 | JNIEnvExt* env = self->GetJniEnv(); |
| 820 | ScopedJniEnvLocalRefState env_state(env); |
| 821 | // Create local ref. copies of proxy method and the receiver |
| 822 | jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver); |
| 823 | jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method); |
| 824 | |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 825 | // Placing into local references incoming arguments from the caller's register arguments, |
| 826 | // replacing original Object* with jobject |
| 827 | const size_t num_params = proxy_method->NumArgs(); |
| 828 | size_t args_in_regs = 0; |
| 829 | for (size_t i = 1; i < num_params; i++) { // skip receiver |
| 830 | args_in_regs = args_in_regs + (proxy_method->IsParamALongOrDouble(i) ? 2 : 1); |
| 831 | if (args_in_regs > 2) { |
| 832 | args_in_regs = 2; |
| 833 | break; |
| 834 | } |
| 835 | } |
| 836 | size_t cur_arg = 0; // current stack location to read |
| 837 | size_t param_index = 1; // skip receiver |
| 838 | while (cur_arg < args_in_regs && param_index < num_params) { |
| 839 | if (proxy_method->IsParamAReference(param_index)) { |
| 840 | Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize)); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 841 | jobject jobj = AddLocalReference<jobject>(env, obj); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 842 | *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 843 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 844 | cur_arg = cur_arg + (proxy_method->IsParamALongOrDouble(param_index) ? 2 : 1); |
| 845 | param_index++; |
| 846 | } |
| 847 | // Placing into local references incoming arguments from the caller's stack arguments |
| 848 | cur_arg += 5; // skip LR, Method* and spills for R1 to R3 |
| 849 | while (param_index < num_params) { |
| 850 | if (proxy_method->IsParamAReference(param_index)) { |
| 851 | Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize)); |
| 852 | jobject jobj = AddLocalReference<jobject>(env, obj); |
| 853 | *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 854 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 855 | cur_arg = cur_arg + (proxy_method->IsParamALongOrDouble(param_index) ? 2 : 1); |
| 856 | param_index++; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 857 | } |
| 858 | // Create args array |
| 859 | ObjectArray<Object>* args = |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 860 | Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 861 | if(args == NULL) { |
| 862 | CHECK(self->IsExceptionPending()); |
| 863 | return; |
| 864 | } |
| 865 | // Set up arguments array and place in local IRT during boxing (which may allocate/GC) |
| 866 | jvalue args_jobj[3]; |
| 867 | args_jobj[0].l = rcvr_jobj; |
| 868 | args_jobj[1].l = proxy_method_jobj; |
| 869 | args_jobj[2].l = AddLocalReference<jobjectArray>(env, args); |
| 870 | // Box arguments |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 871 | cur_arg = 0; // reset stack location to read to start |
| 872 | // reset index, will index into param type array which doesn't include the receiver |
| 873 | param_index = 0; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 874 | ObjectArray<Class>* param_types = proxy_method->GetJavaParameterTypes(); |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 875 | CHECK(param_types != NULL); |
| 876 | // Check number of parameter types agrees with number from the Method - less 1 for the receiver. |
| 877 | CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1); |
| 878 | while (cur_arg < args_in_regs && param_index < (num_params - 1)) { |
| 879 | Class* param_type = param_types->Get(param_index); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 880 | Object* obj; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 881 | if (!param_type->IsPrimitive()) { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 882 | obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize))); |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 883 | } else { |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 884 | JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize)); |
| 885 | if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) { |
| 886 | // long/double split over regs and stack, mask in high half from stack arguments |
| 887 | // (7 = 2 reg args + LR + Method* + 3 arg reg spill slots) |
| 888 | uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (7 * kPointerSize)); |
| 889 | val.j = (val.j & 0xFFFFFFFFull) | (high_half << 32); |
| 890 | } |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 891 | BoxPrimitive(env, param_type, val); |
| 892 | if (self->IsExceptionPending()) { |
| 893 | return; |
| 894 | } |
| 895 | obj = val.l; |
| 896 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 897 | args->Set(param_index, obj); |
| 898 | cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1); |
| 899 | param_index++; |
| 900 | } |
| 901 | // Placing into local references incoming arguments from the caller's stack arguments |
| 902 | cur_arg += 5; // skip LR, Method* and spills for R1 to R3 |
| 903 | while (param_index < num_params) { |
| 904 | Class* param_type = param_types->Get(param_index); |
| 905 | Object* obj; |
| 906 | if (!param_type->IsPrimitive()) { |
| 907 | obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize))); |
| 908 | } else { |
| 909 | JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize)); |
| 910 | BoxPrimitive(env, param_type, val); |
| 911 | if (self->IsExceptionPending()) { |
| 912 | return; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 913 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 914 | obj = val.l; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 915 | } |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame^] | 916 | args->Set(param_index, obj); |
| 917 | cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1); |
| 918 | param_index++; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 919 | } |
| 920 | // Get the InvocationHandler method and the field that holds it within the Proxy object |
| 921 | static jmethodID inv_hand_invoke_mid = NULL; |
| 922 | static jfieldID proxy_inv_hand_fid = NULL; |
| 923 | if (proxy_inv_hand_fid == NULL) { |
| 924 | ScopedLocalRef<jclass> proxy(env, env->FindClass("java.lang.reflect.Proxy")); |
| 925 | proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;"); |
| 926 | ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java.lang.reflect.InvocationHandler")); |
| 927 | inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke", |
| 928 | "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;"); |
| 929 | } |
| 930 | DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java.lang.reflect.Proxy"))); |
| 931 | jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid); |
| 932 | // Call InvocationHandler.invoke |
| 933 | jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj); |
| 934 | // Place result in stack args |
| 935 | if (!self->IsExceptionPending()) { |
| 936 | Object* result_ref = self->DecodeJObject(result); |
| 937 | if (result_ref != NULL) { |
| 938 | JValue result_unboxed; |
| 939 | UnboxPrimitive(env, result_ref, proxy_method->GetReturnType(), result_unboxed); |
| 940 | *reinterpret_cast<JValue*>(stack_args) = result_unboxed; |
| 941 | } else { |
| 942 | *reinterpret_cast<jobject*>(stack_args) = NULL; |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 947 | /* |
| 948 | * Float/double conversion requires clamping to min and max of integer form. If |
| 949 | * target doesn't support this normally, use these. |
| 950 | */ |
| 951 | int64_t D2L(double d) { |
| 952 | static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL; |
| 953 | static const double kMinLong = (double)(int64_t)0x8000000000000000ULL; |
| 954 | if (d >= kMaxLong) |
| 955 | return (int64_t)0x7fffffffffffffffULL; |
| 956 | else if (d <= kMinLong) |
| 957 | return (int64_t)0x8000000000000000ULL; |
| 958 | else if (d != d) // NaN case |
| 959 | return 0; |
| 960 | else |
| 961 | return (int64_t)d; |
| 962 | } |
| 963 | |
| 964 | int64_t F2L(float f) { |
| 965 | static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL; |
| 966 | static const float kMinLong = (float)(int64_t)0x8000000000000000ULL; |
| 967 | if (f >= kMaxLong) |
| 968 | return (int64_t)0x7fffffffffffffffULL; |
| 969 | else if (f <= kMinLong) |
| 970 | return (int64_t)0x8000000000000000ULL; |
| 971 | else if (f != f) // NaN case |
| 972 | return 0; |
| 973 | else |
| 974 | return (int64_t)f; |
| 975 | } |
| 976 | |
| 977 | } // namespace art |