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" |
| 20 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 21 | namespace art { |
| 22 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 23 | // Place a special frame at the TOS that will save the callee saves for the given type |
| 24 | static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 25 | // Be aware the store below may well stomp on an incoming argument |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 26 | *sp = Runtime::Current()->GetCalleeSaveMethod(type); |
| 27 | self->SetTopOfStack(sp, 0); |
| 28 | } |
| 29 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 30 | // Temporary debugging hook for compiler. |
| 31 | extern void DebugMe(Method* method, uint32_t info) { |
| 32 | LOG(INFO) << "DebugMe"; |
| 33 | if (method != NULL) { |
| 34 | LOG(INFO) << PrettyMethod(method); |
| 35 | } |
| 36 | LOG(INFO) << "Info: " << info; |
| 37 | } |
| 38 | |
| 39 | // Return value helper for jobject return types |
| 40 | extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) { |
Brian Carlstrom | 6f495f2 | 2011-10-10 15:05:03 -0700 | [diff] [blame] | 41 | if (thread->IsExceptionPending()) { |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 42 | // clear any result if an exception is pending to avoid making a |
| 43 | // local reference out of garbage. |
Brian Carlstrom | 6f495f2 | 2011-10-10 15:05:03 -0700 | [diff] [blame] | 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(); |
| 318 | uint32_t saved_local_ref_cookie = env->local_ref_cookie; |
| 319 | env->local_ref_cookie = env->locals.GetSegmentState(); |
| 320 | // Discover shorty (avoid GCs) |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 321 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 322 | const char* shorty = linker->MethodShorty(method_idx, *caller_sp); |
| 323 | size_t shorty_len = strlen(shorty); |
| 324 | size_t args_in_regs = shorty_len < 3 ? shorty_len : 3; |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 325 | if (type == Runtime::kUnknownMethod) { |
| 326 | uint32_t dex_pc = (*caller_sp)->ToDexPC(caller_pc - 2); |
| 327 | UNIMPLEMENTED(WARNING) << "Missed argument handlerization in direct method trampoline. " |
| 328 | "Need to discover method invoke type at " << PrettyMethod(*caller_sp) |
| 329 | << " PC: " << (void*)dex_pc; |
| 330 | } else { |
| 331 | bool is_static = type == Runtime::kStaticMethod; |
| 332 | // Handlerize references in registers |
| 333 | int cur_arg = 1; // skip method_idx in R0, first arg is in R1 |
| 334 | if (!is_static) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 335 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 336 | cur_arg++; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 337 | AddLocalReference<jobject>(env, obj); |
| 338 | } |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 339 | for(size_t i = 0; i < args_in_regs; i++) { |
| 340 | char c = shorty[i + 1]; // offset to skip return value |
| 341 | if (c == 'L') { |
| 342 | Object* obj = reinterpret_cast<Object*>(regs[cur_arg]); |
| 343 | AddLocalReference<jobject>(env, obj); |
| 344 | } |
| 345 | cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 346 | } |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 347 | // Handlerize references in out going arguments |
| 348 | for(size_t i = 3; i < shorty_len; i++) { |
| 349 | char c = shorty[i + 1]; // offset to skip return value |
| 350 | if (c == 'L') { |
| 351 | Object* obj = reinterpret_cast<Object*>(regs[i + 3]); // skip R0, LR and Method* of caller |
| 352 | AddLocalReference<jobject>(env, obj); |
| 353 | } |
| 354 | cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1); |
| 355 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 356 | } |
| 357 | // Resolve method filling in dex cache |
| 358 | Method* called = linker->ResolveMethod(method_idx, *caller_sp, true); |
| 359 | if (!thread->IsExceptionPending()) { |
| 360 | // We got this far, ensure that the declaring class is initialized |
| 361 | linker->EnsureInitialized(called->GetDeclaringClass(), true); |
| 362 | } |
| 363 | // Restore JNI env state |
| 364 | env->locals.SetSegmentState(env->local_ref_cookie); |
| 365 | env->local_ref_cookie = saved_local_ref_cookie; |
| 366 | |
| 367 | void* code; |
| 368 | if (thread->IsExceptionPending()) { |
| 369 | // Something went wrong, go into deliver exception with the pending exception in r0 |
| 370 | code = reinterpret_cast<void*>(art_deliver_exception_from_code); |
| 371 | regs[0] = reinterpret_cast<uintptr_t>(thread->GetException()); |
| 372 | thread->ClearException(); |
| 373 | } else { |
| 374 | // Expect class to at least be initializing |
| 375 | CHECK(called->GetDeclaringClass()->IsInitializing()); |
| 376 | // Set up entry into main method |
| 377 | regs[0] = reinterpret_cast<uintptr_t>(called); |
| 378 | code = const_cast<void*>(called->GetCode()); |
| 379 | } |
| 380 | return code; |
| 381 | } |
| 382 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 383 | // TODO: placeholder. Helper function to type |
| 384 | Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) { |
| 385 | /* |
| 386 | * Should initialize & fix up method->dex_cache_resolved_types_[]. |
| 387 | * Returns initialized type. Does not return normally if an exception |
| 388 | * is thrown, but instead initiates the catch. Should be similar to |
| 389 | * ClassLinker::InitializeStaticStorageFromCode. |
| 390 | */ |
| 391 | UNIMPLEMENTED(FATAL); |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | // TODO: placeholder. Helper function to resolve virtual method |
| 396 | void ResolveMethodFromCode(Method* method, uint32_t method_idx) { |
| 397 | /* |
| 398 | * Slow-path handler on invoke virtual method path in which |
| 399 | * base method is unresolved at compile-time. Doesn't need to |
| 400 | * return anything - just either ensure that |
| 401 | * method->dex_cache_resolved_methods_(method_idx) != NULL or |
| 402 | * throw and unwind. The caller will restart call sequence |
| 403 | * from the beginning. |
| 404 | */ |
| 405 | } |
| 406 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 407 | Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static) { |
| 408 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 409 | Field* f = class_linker->ResolveField(field_idx, referrer, is_static); |
| 410 | if (f != NULL) { |
| 411 | Class* c = f->GetDeclaringClass(); |
| 412 | // If the class is already initializing, we must be inside <clinit>, or |
| 413 | // we'd still be waiting for the lock. |
| 414 | if (c->GetStatus() == Class::kStatusInitializing || class_linker->EnsureInitialized(c, true)) { |
| 415 | return f; |
| 416 | } |
| 417 | } |
| 418 | DCHECK(Thread::Current()->IsExceptionPending()); // Throw exception and unwind |
| 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | extern "C" Field* artFindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer, |
| 423 | Thread* self, Method** sp) { |
| 424 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 425 | return FindFieldFromCode(field_idx, referrer, false); |
| 426 | } |
| 427 | |
| 428 | extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 429 | Thread* self, Method** sp) { |
| 430 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 431 | Field* field = FindFieldFromCode(field_idx, referrer, true); |
| 432 | if (field != NULL) { |
| 433 | Class* type = field->GetType(); |
| 434 | if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int64_t)) { |
| 435 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 436 | "Attempted read of 32-bit primitive on field '%s'", |
| 437 | PrettyField(field, true).c_str()); |
| 438 | } else { |
| 439 | return field->Get32(NULL); |
| 440 | } |
| 441 | } |
| 442 | return 0; // Will throw exception by checking with Thread::Current |
| 443 | } |
| 444 | |
| 445 | extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 446 | Thread* self, Method** sp) { |
| 447 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 448 | Field* field = FindFieldFromCode(field_idx, referrer, true); |
| 449 | if (field != NULL) { |
| 450 | Class* type = field->GetType(); |
| 451 | if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int64_t)) { |
| 452 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 453 | "Attempted read of 64-bit primitive on field '%s'", |
| 454 | PrettyField(field, true).c_str()); |
| 455 | } else { |
| 456 | return field->Get64(NULL); |
| 457 | } |
| 458 | } |
| 459 | return 0; // Will throw exception by checking with Thread::Current |
| 460 | } |
| 461 | |
| 462 | extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer, |
| 463 | Thread* self, Method** sp) { |
| 464 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 465 | Field* field = FindFieldFromCode(field_idx, referrer, true); |
| 466 | if (field != NULL) { |
| 467 | Class* type = field->GetType(); |
| 468 | if (type->IsPrimitive()) { |
| 469 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 470 | "Attempted read of reference on primitive field '%s'", |
| 471 | PrettyField(field, true).c_str()); |
| 472 | } else { |
| 473 | return field->GetObj(NULL); |
| 474 | } |
| 475 | } |
| 476 | return NULL; // Will throw exception by checking with Thread::Current |
| 477 | } |
| 478 | |
| 479 | extern "C" int artSet32StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 480 | uint32_t new_value, Thread* self, Method** sp) { |
| 481 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 482 | Field* field = FindFieldFromCode(field_idx, referrer, true); |
| 483 | if (field != NULL) { |
| 484 | Class* type = field->GetType(); |
| 485 | if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int32_t)) { |
| 486 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 487 | "Attempted write of 32-bit primitive to field '%s'", |
| 488 | PrettyField(field, true).c_str()); |
| 489 | } else { |
| 490 | field->Set32(NULL, new_value); |
| 491 | return 0; // success |
| 492 | } |
| 493 | } |
| 494 | return -1; // failure |
| 495 | } |
| 496 | |
| 497 | extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer, |
| 498 | uint64_t new_value, Thread* self, Method** sp) { |
| 499 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 500 | Field* field = FindFieldFromCode(field_idx, referrer, true); |
| 501 | if (field != NULL) { |
| 502 | Class* type = field->GetType(); |
| 503 | if (!type->IsPrimitive() || type->PrimitiveSize() != sizeof(int64_t)) { |
| 504 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 505 | "Attempted write of 64-bit primitive to field '%s'", |
| 506 | PrettyField(field, true).c_str()); |
| 507 | } else { |
| 508 | field->Set64(NULL, new_value); |
| 509 | return 0; // success |
| 510 | } |
| 511 | } |
| 512 | return -1; // failure |
| 513 | } |
| 514 | |
| 515 | extern "C" int artSetObjStaticFromCode(uint32_t field_idx, const Method* referrer, |
| 516 | Object* new_value, Thread* self, Method** sp) { |
| 517 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 518 | Field* field = FindFieldFromCode(field_idx, referrer, true); |
| 519 | if (field != NULL) { |
| 520 | Class* type = field->GetType(); |
| 521 | if (type->IsPrimitive()) { |
| 522 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 523 | "Attempted write of reference to primitive field '%s'", |
| 524 | PrettyField(field, true).c_str()); |
| 525 | } else { |
| 526 | field->SetObj(NULL, new_value); |
| 527 | return 0; // success |
| 528 | } |
| 529 | } |
| 530 | return -1; // failure |
| 531 | } |
| 532 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 533 | // Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it |
| 534 | // 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] | 535 | 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] | 536 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 537 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 538 | Runtime* runtime = Runtime::Current(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 539 | if (klass == NULL) { |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 540 | klass = runtime->GetClassLinker()->ResolveType(type_idx, method); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 541 | if (klass == NULL) { |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 542 | DCHECK(self->IsExceptionPending()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 543 | return NULL; // Failure |
| 544 | } |
| 545 | } |
buzbee | 33a129c | 2011-10-06 16:53:20 -0700 | [diff] [blame] | 546 | if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) { |
| 547 | DCHECK(self->IsExceptionPending()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 548 | return NULL; // Failure |
| 549 | } |
| 550 | return klass->AllocObject(); |
| 551 | } |
| 552 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 553 | Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
| 554 | Thread* self) { |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 555 | if (component_count < 0) { |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 556 | self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 557 | return NULL; // Failure |
| 558 | } |
| 559 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 560 | if (klass == NULL) { // Not in dex cache so try to resolve |
| 561 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 562 | if (klass == NULL) { // Error |
| 563 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 564 | return NULL; // Failure |
| 565 | } |
| 566 | } |
| 567 | if (klass->IsPrimitive() && !klass->IsPrimitiveInt()) { |
| 568 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 569 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 570 | "Bad filled array request for type %s", |
| 571 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 572 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 573 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 574 | "Found type %s; filled-new-array not implemented for anything but \'int\'", |
| 575 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 576 | } |
| 577 | return NULL; // Failure |
| 578 | } else { |
| 579 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 580 | return Array::Alloc(klass, component_count); |
| 581 | } |
| 582 | } |
| 583 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 584 | // Helper function to alloc array for OP_FILLED_NEW_ARRAY |
| 585 | extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, |
| 586 | int32_t component_count, Thread* self, Method** sp) { |
| 587 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 588 | return CheckAndAllocArrayFromCode(type_idx, method, component_count, self); |
| 589 | } |
| 590 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 591 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 592 | // 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] | 593 | extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
| 594 | Thread* self, Method** sp) { |
| 595 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 596 | if (component_count < 0) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 597 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 598 | component_count); |
| 599 | return NULL; // Failure |
| 600 | } |
| 601 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 602 | if (klass == NULL) { // Not in dex cache so try to resolve |
| 603 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 604 | if (klass == NULL) { // Error |
| 605 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 606 | return NULL; // Failure |
| 607 | } |
| 608 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 609 | } |
| 610 | return Array::Alloc(klass, component_count); |
| 611 | } |
| 612 | |
| 613 | // 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] | 614 | extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) { |
| 615 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 616 | DCHECK(a->IsClass()) << PrettyClass(a); |
| 617 | DCHECK(b->IsClass()) << PrettyClass(b); |
| 618 | if (b->IsAssignableFrom(a)) { |
| 619 | return 0; // Success |
| 620 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 621 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 622 | "%s cannot be cast to %s", |
| 623 | PrettyDescriptor(a->GetDescriptor()).c_str(), |
| 624 | PrettyDescriptor(b->GetDescriptor()).c_str()); |
| 625 | return -1; // Failure |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | // Tests whether 'element' can be assigned into an array of type 'array_class'. |
| 630 | // Returns 0 on success and -1 if an exception is pending. |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 631 | extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class, |
| 632 | Thread* self, Method** sp) { |
| 633 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 634 | DCHECK(array_class != NULL); |
| 635 | // element can't be NULL as we catch this is screened in runtime_support |
| 636 | Class* element_class = element->GetClass(); |
| 637 | Class* component_type = array_class->GetComponentType(); |
| 638 | if (component_type->IsAssignableFrom(element_class)) { |
| 639 | return 0; // Success |
| 640 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 641 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 642 | "Cannot store an object of type %s in to an array of type %s", |
| 643 | PrettyDescriptor(element_class->GetDescriptor()).c_str(), |
| 644 | PrettyDescriptor(array_class->GetDescriptor()).c_str()); |
| 645 | return -1; // Failure |
| 646 | } |
| 647 | } |
| 648 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 649 | Class* InitializeStaticStorage(uint32_t type_idx, const Method* referrer, Thread* self) { |
| 650 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 651 | Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 652 | if (klass == NULL) { |
| 653 | CHECK(self->IsExceptionPending()); |
| 654 | return NULL; // Failure - Indicate to caller to deliver exception |
| 655 | } |
| 656 | // If we are the <clinit> of this class, just return our storage. |
| 657 | // |
| 658 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 659 | // running. |
| 660 | if (klass == referrer->GetDeclaringClass() && referrer->IsClassInitializer()) { |
| 661 | return klass; |
| 662 | } |
| 663 | if (!class_linker->EnsureInitialized(klass, true)) { |
| 664 | CHECK(self->IsExceptionPending()); |
| 665 | return NULL; // Failure - Indicate to caller to deliver exception |
| 666 | } |
| 667 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
| 668 | return klass; |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 671 | extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer, |
| 672 | Thread* self, Method** sp) { |
| 673 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 674 | return InitializeStaticStorage(type_idx, referrer, self); |
| 675 | } |
| 676 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 677 | String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) { |
| 678 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 679 | return class_linker->ResolveString(string_idx, referrer); |
| 680 | } |
| 681 | |
| 682 | extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx, |
| 683 | Thread* self, Method** sp) { |
| 684 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 685 | return ResolveStringFromCode(referrer, string_idx); |
| 686 | } |
| 687 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 688 | extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) { |
| 689 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 690 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 691 | // MonitorExit may throw exception |
| 692 | return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */; |
| 693 | } |
| 694 | |
| 695 | extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) { |
| 696 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly); |
| 697 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
| 698 | obj->MonitorEnter(thread); // May block |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 699 | DCHECK(thread->HoldsLock(obj)); |
| 700 | // Only possible exception is NPE and is handled before entry |
| 701 | DCHECK(!thread->IsExceptionPending()); |
| 702 | } |
| 703 | |
Ian Rogers | 4a510d8 | 2011-10-09 14:30:24 -0700 | [diff] [blame] | 704 | void CheckSuspendFromCode(Thread* thread) { |
| 705 | // Called when thread->suspend_count_ != 0 |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 706 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
| 707 | } |
| 708 | |
Ian Rogers | 4a510d8 | 2011-10-09 14:30:24 -0700 | [diff] [blame] | 709 | extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) { |
| 710 | // 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] | 711 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 712 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Fill the array with predefined constant values, throwing exceptions if the array is null or |
| 717 | * not of sufficient length. |
| 718 | * |
| 719 | * NOTE: When dealing with a raw dex file, the data to be copied uses |
| 720 | * little-endian ordering. Require that oat2dex do any required swapping |
| 721 | * so this routine can get by with a memcpy(). |
| 722 | * |
| 723 | * Format of the data: |
| 724 | * ushort ident = 0x0300 magic value |
| 725 | * ushort width width of each element in the table |
| 726 | * uint size number of elements in the table |
| 727 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 728 | * padding at the end) |
| 729 | */ |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 730 | extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table, |
| 731 | Thread* self, Method** sp) { |
| 732 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 733 | DCHECK_EQ(table[0], 0x0300); |
| 734 | if (array == NULL) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 735 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 736 | "null array in fill array"); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 737 | return -1; // Error |
| 738 | } |
| 739 | DCHECK(array->IsArrayInstance() && !array->IsObjectArray()); |
| 740 | uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16); |
| 741 | if (static_cast<int32_t>(size) > array->GetLength()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 742 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 743 | "failed array fill. length=%d; index=%d", array->GetLength(), size); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 744 | return -1; // Error |
| 745 | } |
| 746 | uint16_t width = table[1]; |
| 747 | uint32_t size_in_bytes = size * width; |
| 748 | memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes); |
| 749 | return 0; // Success |
| 750 | } |
| 751 | |
| 752 | // See comments in runtime_support_asm.S |
| 753 | extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx, |
| 754 | Object* this_object , |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 755 | Thread* thread, Method** sp) { |
| 756 | FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 757 | if (this_object == NULL) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 758 | thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 759 | "null receiver during interface dispatch"); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 763 | Frame frame = thread->GetTopOfStack(); // Compute calling method |
| 764 | frame.Next(); |
| 765 | Method* caller_method = frame.GetMethod(); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 766 | Method* interface_method = class_linker->ResolveMethod(method_idx, caller_method, false); |
| 767 | if (interface_method == NULL) { |
| 768 | // Could not resolve interface method. Throw error and unwind |
| 769 | CHECK(thread->IsExceptionPending()); |
| 770 | return 0; |
| 771 | } |
| 772 | Method* method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method); |
| 773 | if (method == NULL) { |
| 774 | CHECK(thread->IsExceptionPending()); |
| 775 | return 0; |
| 776 | } |
| 777 | const void* code = method->GetCode(); |
| 778 | |
| 779 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 780 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 781 | uint64_t result = ((code_uint << 32) | method_uint); |
| 782 | return result; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Float/double conversion requires clamping to min and max of integer form. If |
| 787 | * target doesn't support this normally, use these. |
| 788 | */ |
| 789 | int64_t D2L(double d) { |
| 790 | static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL; |
| 791 | static const double kMinLong = (double)(int64_t)0x8000000000000000ULL; |
| 792 | if (d >= kMaxLong) |
| 793 | return (int64_t)0x7fffffffffffffffULL; |
| 794 | else if (d <= kMinLong) |
| 795 | return (int64_t)0x8000000000000000ULL; |
| 796 | else if (d != d) // NaN case |
| 797 | return 0; |
| 798 | else |
| 799 | return (int64_t)d; |
| 800 | } |
| 801 | |
| 802 | int64_t F2L(float f) { |
| 803 | static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL; |
| 804 | static const float kMinLong = (float)(int64_t)0x8000000000000000ULL; |
| 805 | if (f >= kMaxLong) |
| 806 | return (int64_t)0x7fffffffffffffffULL; |
| 807 | else if (f <= kMinLong) |
| 808 | return (int64_t)0x8000000000000000ULL; |
| 809 | else if (f != f) // NaN case |
| 810 | return 0; |
| 811 | else |
| 812 | return (int64_t)f; |
| 813 | } |
| 814 | |
| 815 | } // namespace art |