Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 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 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 17 | #include "interpreter_common.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 18 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 19 | #include <limits> |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 21 | #include "mirror/string-inl.h" |
| 22 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 23 | namespace art { |
| 24 | namespace interpreter { |
| 25 | |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 26 | // Hand select a number of methods to be run in a not yet started runtime without using JNI. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 27 | static void UnstartedRuntimeJni(Thread* self, ArtMethod* method, |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 28 | Object* receiver, uint32_t* args, JValue* result) |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 29 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 30 | std::string name(PrettyMethod(method)); |
Ian Rogers | a4e7413 | 2014-05-06 01:14:54 -0700 | [diff] [blame] | 31 | if (name == "java.lang.Object dalvik.system.VMRuntime.newUnpaddedArray(java.lang.Class, int)") { |
| 32 | int32_t length = args[1]; |
| 33 | DCHECK_GE(length, 0); |
| 34 | mirror::Class* element_class = reinterpret_cast<Object*>(args[0])->AsClass(); |
| 35 | Runtime* runtime = Runtime::Current(); |
| 36 | mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, element_class); |
| 37 | DCHECK(array_class != nullptr); |
| 38 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 39 | result->SetL(mirror::Array::Alloc<true>(self, array_class, length, |
| 40 | array_class->GetComponentSize(), allocator, true)); |
| 41 | } else if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") { |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 42 | result->SetL(NULL); |
| 43 | } else if (name == "java.lang.Class dalvik.system.VMStack.getStackClass2()") { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 44 | NthCallerVisitor visitor(self, 3); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 45 | visitor.WalkStack(); |
| 46 | result->SetL(visitor.caller->GetDeclaringClass()); |
| 47 | } else if (name == "double java.lang.Math.log(double)") { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 48 | JValue value; |
| 49 | value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); |
| 50 | result->SetD(log(value.GetD())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 51 | } else if (name == "java.lang.String java.lang.Class.getNameNative()") { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 52 | StackHandleScope<1> hs(self); |
| 53 | result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass()))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 54 | } else if (name == "int java.lang.Float.floatToRawIntBits(float)") { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 55 | result->SetI(args[0]); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 56 | } else if (name == "float java.lang.Float.intBitsToFloat(int)") { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 57 | result->SetI(args[0]); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 58 | } else if (name == "double java.lang.Math.exp(double)") { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 59 | JValue value; |
| 60 | value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); |
| 61 | result->SetD(exp(value.GetD())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 62 | } else if (name == "java.lang.Object java.lang.Object.internalClone()") { |
| 63 | result->SetL(receiver->Clone(self)); |
| 64 | } else if (name == "void java.lang.Object.notifyAll()") { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 65 | receiver->NotifyAll(self); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 66 | } else if (name == "int java.lang.String.compareTo(java.lang.String)") { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 67 | String* rhs = reinterpret_cast<Object*>(args[0])->AsString(); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 68 | CHECK(rhs != NULL); |
| 69 | result->SetI(receiver->AsString()->CompareTo(rhs)); |
| 70 | } else if (name == "java.lang.String java.lang.String.intern()") { |
| 71 | result->SetL(receiver->AsString()->Intern()); |
| 72 | } else if (name == "int java.lang.String.fastIndexOf(int, int)") { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 73 | result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 74 | } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 75 | StackHandleScope<2> hs(self); |
| 76 | auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass())); |
| 77 | auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray())); |
| 78 | result->SetL(Array::CreateMultiArray(self, h_class, h_dimensions)); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 79 | } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") { |
| 80 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 81 | if (Runtime::Current()->IsActiveTransaction()) { |
| 82 | result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace<true>(soa))); |
| 83 | } else { |
| 84 | result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace<false>(soa))); |
| 85 | } |
Sebastien Hertz | f48644b | 2014-02-17 15:16:03 +0100 | [diff] [blame] | 86 | } else if (name == "int java.lang.System.identityHashCode(java.lang.Object)") { |
| 87 | mirror::Object* obj = reinterpret_cast<Object*>(args[0]); |
| 88 | result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 89 | } else if (name == "boolean java.nio.ByteOrder.isLittleEndian()") { |
Sebastien Hertz | f48644b | 2014-02-17 15:16:03 +0100 | [diff] [blame] | 90 | result->SetZ(JNI_TRUE); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 91 | } else if (name == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 92 | Object* obj = reinterpret_cast<Object*>(args[0]); |
| 93 | jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; |
| 94 | jint expectedValue = args[3]; |
| 95 | jint newValue = args[4]; |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 96 | bool success; |
| 97 | if (Runtime::Current()->IsActiveTransaction()) { |
| 98 | success = obj->CasField32<true>(MemberOffset(offset), expectedValue, newValue); |
| 99 | } else { |
| 100 | success = obj->CasField32<false>(MemberOffset(offset), expectedValue, newValue); |
| 101 | } |
Sebastien Hertz | f48644b | 2014-02-17 15:16:03 +0100 | [diff] [blame] | 102 | result->SetZ(success ? JNI_TRUE : JNI_FALSE); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 103 | } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 104 | Object* obj = reinterpret_cast<Object*>(args[0]); |
Sebastien Hertz | f48644b | 2014-02-17 15:16:03 +0100 | [diff] [blame] | 105 | jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 106 | Object* newValue = reinterpret_cast<Object*>(args[3]); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 107 | if (Runtime::Current()->IsActiveTransaction()) { |
| 108 | obj->SetFieldObject<true>(MemberOffset(offset), newValue); |
| 109 | } else { |
| 110 | obj->SetFieldObject<false>(MemberOffset(offset), newValue); |
| 111 | } |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 112 | } else if (name == "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)") { |
| 113 | mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass(); |
| 114 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 115 | result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value()); |
| 116 | } else if (name == "int sun.misc.Unsafe.getArrayIndexScaleForComponentType(java.lang.Class)") { |
| 117 | mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass(); |
| 118 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 119 | result->SetI(Primitive::ComponentSize(primitive_type)); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 120 | } else if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 121 | AbortTransaction(self, "Attempt to invoke native method in non-started runtime: %s", |
| 122 | name.c_str()); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 123 | |
| 124 | } else { |
| 125 | LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted " |
| 126 | "non-transactional runtime"; |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 130 | static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty, |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 131 | Object* receiver, uint32_t* args, JValue* result) |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 132 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 133 | // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler, |
| 134 | // it should be removed and JNI compiled stubs used instead. |
| 135 | ScopedObjectAccessUnchecked soa(self); |
| 136 | if (method->IsStatic()) { |
| 137 | if (shorty == "L") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 138 | typedef jobject (fntype)(JNIEnv*, jclass); |
| 139 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 140 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 141 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
Ian Rogers | 556d637 | 2012-11-20 12:19:36 -0800 | [diff] [blame] | 142 | jobject jresult; |
| 143 | { |
| 144 | ScopedThreadStateChange tsc(self, kNative); |
| 145 | jresult = fn(soa.Env(), klass.get()); |
| 146 | } |
| 147 | result->SetL(soa.Decode<Object*>(jresult)); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 148 | } else if (shorty == "V") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 149 | typedef void (fntype)(JNIEnv*, jclass); |
| 150 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 151 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 152 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 153 | ScopedThreadStateChange tsc(self, kNative); |
| 154 | fn(soa.Env(), klass.get()); |
| 155 | } else if (shorty == "Z") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 156 | typedef jboolean (fntype)(JNIEnv*, jclass); |
| 157 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 158 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 159 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 160 | ScopedThreadStateChange tsc(self, kNative); |
| 161 | result->SetZ(fn(soa.Env(), klass.get())); |
| 162 | } else if (shorty == "BI") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 163 | typedef jbyte (fntype)(JNIEnv*, jclass, jint); |
| 164 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 165 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 166 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 167 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 168 | result->SetB(fn(soa.Env(), klass.get(), args[0])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 169 | } else if (shorty == "II") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 170 | typedef jint (fntype)(JNIEnv*, jclass, jint); |
| 171 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 172 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 173 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 174 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 175 | result->SetI(fn(soa.Env(), klass.get(), args[0])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 176 | } else if (shorty == "LL") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 177 | typedef jobject (fntype)(JNIEnv*, jclass, jobject); |
| 178 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 179 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 180 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 181 | ScopedLocalRef<jobject> arg0(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 182 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0]))); |
Ian Rogers | 556d637 | 2012-11-20 12:19:36 -0800 | [diff] [blame] | 183 | jobject jresult; |
| 184 | { |
| 185 | ScopedThreadStateChange tsc(self, kNative); |
| 186 | jresult = fn(soa.Env(), klass.get(), arg0.get()); |
| 187 | } |
| 188 | result->SetL(soa.Decode<Object*>(jresult)); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 189 | } else if (shorty == "IIZ") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 190 | typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean); |
| 191 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 192 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 193 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 194 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 195 | result->SetI(fn(soa.Env(), klass.get(), args[0], args[1])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 196 | } else if (shorty == "ILI") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 197 | typedef jint (fntype)(JNIEnv*, jclass, jobject, jint); |
| 198 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 199 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 200 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 201 | ScopedLocalRef<jobject> arg0(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 202 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 203 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 204 | result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 205 | } else if (shorty == "SIZ") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 206 | typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean); |
| 207 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 208 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 209 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 210 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 211 | result->SetS(fn(soa.Env(), klass.get(), args[0], args[1])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 212 | } else if (shorty == "VIZ") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 213 | typedef void (fntype)(JNIEnv*, jclass, jint, jboolean); |
| 214 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 215 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 216 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 217 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 218 | fn(soa.Env(), klass.get(), args[0], args[1]); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 219 | } else if (shorty == "ZLL") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 220 | typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject); |
| 221 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 222 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 223 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 224 | ScopedLocalRef<jobject> arg0(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 225 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 226 | ScopedLocalRef<jobject> arg1(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 227 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 228 | ScopedThreadStateChange tsc(self, kNative); |
| 229 | result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get())); |
| 230 | } else if (shorty == "ZILL") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 231 | typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject); |
| 232 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 233 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 234 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 235 | ScopedLocalRef<jobject> arg1(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 236 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 237 | ScopedLocalRef<jobject> arg2(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 238 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 239 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 240 | result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 241 | } else if (shorty == "VILII") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 242 | typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint); |
| 243 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 244 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 245 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 246 | ScopedLocalRef<jobject> arg1(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 247 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 248 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 249 | fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 250 | } else if (shorty == "VLILII") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 251 | typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint); |
| 252 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 253 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 254 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 255 | ScopedLocalRef<jobject> arg0(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 256 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 257 | ScopedLocalRef<jobject> arg2(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 258 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 259 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 260 | fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 261 | } else { |
| 262 | LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method) |
| 263 | << " shorty: " << shorty; |
| 264 | } |
| 265 | } else { |
| 266 | if (shorty == "L") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 267 | typedef jobject (fntype)(JNIEnv*, jobject); |
| 268 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 269 | ScopedLocalRef<jobject> rcvr(soa.Env(), |
| 270 | soa.AddLocalReference<jobject>(receiver)); |
Ian Rogers | 556d637 | 2012-11-20 12:19:36 -0800 | [diff] [blame] | 271 | jobject jresult; |
| 272 | { |
| 273 | ScopedThreadStateChange tsc(self, kNative); |
| 274 | jresult = fn(soa.Env(), rcvr.get()); |
| 275 | } |
| 276 | result->SetL(soa.Decode<Object*>(jresult)); |
Jeff Hao | 3dd9f76 | 2013-07-08 13:09:25 -0700 | [diff] [blame] | 277 | } else if (shorty == "V") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 278 | typedef void (fntype)(JNIEnv*, jobject); |
| 279 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Jeff Hao | 3dd9f76 | 2013-07-08 13:09:25 -0700 | [diff] [blame] | 280 | ScopedLocalRef<jobject> rcvr(soa.Env(), |
| 281 | soa.AddLocalReference<jobject>(receiver)); |
| 282 | ScopedThreadStateChange tsc(self, kNative); |
| 283 | fn(soa.Env(), rcvr.get()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 284 | } else if (shorty == "LL") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 285 | typedef jobject (fntype)(JNIEnv*, jobject, jobject); |
| 286 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 287 | ScopedLocalRef<jobject> rcvr(soa.Env(), |
| 288 | soa.AddLocalReference<jobject>(receiver)); |
| 289 | ScopedLocalRef<jobject> arg0(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 290 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0]))); |
Ian Rogers | 556d637 | 2012-11-20 12:19:36 -0800 | [diff] [blame] | 291 | jobject jresult; |
| 292 | { |
| 293 | ScopedThreadStateChange tsc(self, kNative); |
| 294 | jresult = fn(soa.Env(), rcvr.get(), arg0.get()); |
Ian Rogers | 556d637 | 2012-11-20 12:19:36 -0800 | [diff] [blame] | 295 | } |
| 296 | result->SetL(soa.Decode<Object*>(jresult)); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 297 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 298 | } else if (shorty == "III") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 299 | typedef jint (fntype)(JNIEnv*, jobject, jint, jint); |
| 300 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 301 | ScopedLocalRef<jobject> rcvr(soa.Env(), |
| 302 | soa.AddLocalReference<jobject>(receiver)); |
| 303 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 304 | result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 305 | } else { |
| 306 | LOG(FATAL) << "Do something with native method: " << PrettyMethod(method) |
| 307 | << " shorty: " << shorty; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 312 | enum InterpreterImplKind { |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 313 | kSwitchImpl, // Switch-based interpreter implementation. |
| 314 | kComputedGotoImplKind // Computed-goto-based interpreter implementation. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 315 | }; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 316 | |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 317 | #if !defined(__clang__) |
| 318 | static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind; |
| 319 | #else |
| 320 | // Clang 3.4 fails to build the goto interpreter implementation. |
| 321 | static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl; |
| 322 | template<bool do_access_check, bool transaction_active> |
| 323 | JValue ExecuteGotoImpl(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item, |
| 324 | ShadowFrame& shadow_frame, JValue result_register) { |
| 325 | LOG(FATAL) << "UNREACHABLE"; |
| 326 | exit(0); |
| 327 | } |
| 328 | // Explicit definitions of ExecuteGotoImpl. |
Stephen Hines | 861ea56 | 2014-04-23 16:03:57 -0700 | [diff] [blame] | 329 | template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 330 | JValue ExecuteGotoImpl<true, false>(Thread* self, MethodHelper& mh, |
| 331 | const DexFile::CodeItem* code_item, |
| 332 | ShadowFrame& shadow_frame, JValue result_register); |
Stephen Hines | 861ea56 | 2014-04-23 16:03:57 -0700 | [diff] [blame] | 333 | template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 334 | JValue ExecuteGotoImpl<false, false>(Thread* self, MethodHelper& mh, |
| 335 | const DexFile::CodeItem* code_item, |
| 336 | ShadowFrame& shadow_frame, JValue result_register); |
Stephen Hines | 861ea56 | 2014-04-23 16:03:57 -0700 | [diff] [blame] | 337 | template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 338 | JValue ExecuteGotoImpl<true, true>(Thread* self, MethodHelper& mh, |
| 339 | const DexFile::CodeItem* code_item, |
| 340 | ShadowFrame& shadow_frame, JValue result_register); |
Stephen Hines | 861ea56 | 2014-04-23 16:03:57 -0700 | [diff] [blame] | 341 | template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 342 | JValue ExecuteGotoImpl<false, true>(Thread* self, MethodHelper& mh, |
| 343 | const DexFile::CodeItem* code_item, |
| 344 | ShadowFrame& shadow_frame, JValue result_register); |
| 345 | #endif |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 346 | |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 347 | static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item, |
| 348 | ShadowFrame& shadow_frame, JValue result_register) |
| 349 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 350 | |
| 351 | static inline JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item, |
| 352 | ShadowFrame& shadow_frame, JValue result_register) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 353 | DCHECK(shadow_frame.GetMethod() == mh.GetMethod() || |
| 354 | shadow_frame.GetMethod()->GetDeclaringClass()->IsProxyClass()); |
| 355 | DCHECK(!shadow_frame.GetMethod()->IsAbstract()); |
| 356 | DCHECK(!shadow_frame.GetMethod()->IsNative()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 357 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 358 | bool transaction_active = Runtime::Current()->IsActiveTransaction(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 359 | if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) { |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 360 | // Enter the "without access check" interpreter. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 361 | if (kInterpreterImplKind == kSwitchImpl) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 362 | if (transaction_active) { |
| 363 | return ExecuteSwitchImpl<false, true>(self, mh, code_item, shadow_frame, result_register); |
| 364 | } else { |
| 365 | return ExecuteSwitchImpl<false, false>(self, mh, code_item, shadow_frame, result_register); |
| 366 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 367 | } else { |
| 368 | DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 369 | if (transaction_active) { |
| 370 | return ExecuteGotoImpl<false, true>(self, mh, code_item, shadow_frame, result_register); |
| 371 | } else { |
| 372 | return ExecuteGotoImpl<false, false>(self, mh, code_item, shadow_frame, result_register); |
| 373 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 374 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 375 | } else { |
| 376 | // Enter the "with access check" interpreter. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 377 | if (kInterpreterImplKind == kSwitchImpl) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 378 | if (transaction_active) { |
| 379 | return ExecuteSwitchImpl<true, true>(self, mh, code_item, shadow_frame, result_register); |
| 380 | } else { |
| 381 | return ExecuteSwitchImpl<true, false>(self, mh, code_item, shadow_frame, result_register); |
| 382 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 383 | } else { |
| 384 | DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 385 | if (transaction_active) { |
| 386 | return ExecuteGotoImpl<true, true>(self, mh, code_item, shadow_frame, result_register); |
| 387 | } else { |
| 388 | return ExecuteGotoImpl<true, false>(self, mh, code_item, shadow_frame, result_register); |
| 389 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 390 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 394 | void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver, |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 395 | uint32_t* args, JValue* result) { |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 396 | DCHECK_EQ(self, Thread::Current()); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 397 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) { |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 398 | ThrowStackOverflowError(self); |
| 399 | return; |
| 400 | } |
| 401 | |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 402 | const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke"); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 403 | MethodHelper mh(method); |
| 404 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
| 405 | uint16_t num_regs; |
| 406 | uint16_t num_ins; |
| 407 | if (code_item != NULL) { |
| 408 | num_regs = code_item->registers_size_; |
| 409 | num_ins = code_item->ins_size_; |
jeffhao | 0a9bb73 | 2012-11-26 12:28:49 -0800 | [diff] [blame] | 410 | } else if (method->IsAbstract()) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 411 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 56adf60 | 2013-07-09 17:27:07 +0200 | [diff] [blame] | 412 | ThrowAbstractMethodError(method); |
jeffhao | 0a9bb73 | 2012-11-26 12:28:49 -0800 | [diff] [blame] | 413 | return; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 414 | } else { |
| 415 | DCHECK(method->IsNative()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 416 | num_regs = num_ins = ArtMethod::NumArgRegisters(mh.GetShorty()); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 417 | if (!method->IsStatic()) { |
| 418 | num_regs++; |
| 419 | num_ins++; |
| 420 | } |
| 421 | } |
| 422 | // Set up shadow frame with matching number of reference slots to vregs. |
| 423 | ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame(); |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 424 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
| 425 | ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory)); |
| 426 | self->PushShadowFrame(shadow_frame); |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 427 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 428 | size_t cur_reg = num_regs - num_ins; |
| 429 | if (!method->IsStatic()) { |
| 430 | CHECK(receiver != NULL); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 431 | shadow_frame->SetVRegReference(cur_reg, receiver); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 432 | ++cur_reg; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 433 | } |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 434 | const char* shorty = mh.GetShorty(); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 435 | for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) { |
| 436 | DCHECK_LT(shorty_pos + 1, mh.GetShortyLength()); |
| 437 | switch (shorty[shorty_pos + 1]) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 438 | case 'L': { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 439 | Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr(); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 440 | shadow_frame->SetVRegReference(cur_reg, o); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 441 | break; |
| 442 | } |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 443 | case 'J': case 'D': { |
| 444 | uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos]; |
| 445 | shadow_frame->SetVRegLong(cur_reg, wide_value); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 446 | cur_reg++; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 447 | arg_pos++; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 448 | break; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 449 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 450 | default: |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 451 | shadow_frame->SetVReg(cur_reg, args[arg_pos]); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 452 | break; |
| 453 | } |
| 454 | } |
Mathieu Chartier | 92246bb | 2014-02-25 18:22:39 -0800 | [diff] [blame] | 455 | self->EndAssertNoThreadSuspension(old_cause); |
| 456 | // Do this after populating the shadow frame in case EnsureInitialized causes a GC. |
| 457 | if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitializing())) { |
| 458 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 459 | StackHandleScope<1> hs(self); |
| 460 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
| 461 | if (UNLIKELY(!class_linker->EnsureInitialized(h_class, true, true))) { |
Mathieu Chartier | 92246bb | 2014-02-25 18:22:39 -0800 | [diff] [blame] | 462 | CHECK(self->IsExceptionPending()); |
| 463 | self->PopShadowFrame(); |
| 464 | return; |
| 465 | } |
| 466 | } |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 467 | if (LIKELY(!method->IsNative())) { |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 468 | JValue r = Execute(self, mh, code_item, *shadow_frame, JValue()); |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 469 | if (result != NULL) { |
| 470 | *result = r; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 471 | } |
| 472 | } else { |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 473 | // We don't expect to be asked to interpret native code (which is entered via a JNI compiler |
| 474 | // generated stub) except during testing and image writing. |
Mathieu Chartier | 92246bb | 2014-02-25 18:22:39 -0800 | [diff] [blame] | 475 | // Update args to be the args in the shadow frame since the input ones could hold stale |
| 476 | // references pointers due to moving GC. |
| 477 | args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 478 | if (!Runtime::Current()->IsStarted()) { |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 479 | UnstartedRuntimeJni(self, method, receiver, args, result); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 480 | } else { |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 481 | InterpreterJni(self, method, shorty, receiver, args, result); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | self->PopShadowFrame(); |
| 485 | } |
| 486 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 487 | void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val) |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 488 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 489 | JValue value; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 490 | value.SetJ(ret_val->GetJ()); // Set value to last known result in case the shadow frame chain is empty. |
| 491 | MethodHelper mh; |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 492 | while (shadow_frame != NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 493 | self->SetTopOfShadowStack(shadow_frame); |
| 494 | mh.ChangeMethod(shadow_frame->GetMethod()); |
| 495 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 496 | value = Execute(self, mh, code_item, *shadow_frame, value); |
| 497 | ShadowFrame* old_frame = shadow_frame; |
| 498 | shadow_frame = shadow_frame->GetLink(); |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 499 | delete old_frame; |
| 500 | } |
| 501 | ret_val->SetJ(value.GetJ()); |
| 502 | } |
| 503 | |
Ian Rogers | 7db619b | 2013-01-16 18:35:48 -0800 | [diff] [blame] | 504 | JValue EnterInterpreterFromStub(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 505 | ShadowFrame& shadow_frame) { |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 506 | DCHECK_EQ(self, Thread::Current()); |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 507 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) { |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 508 | ThrowStackOverflowError(self); |
| 509 | return JValue(); |
| 510 | } |
| 511 | |
Ian Rogers | 7db619b | 2013-01-16 18:35:48 -0800 | [diff] [blame] | 512 | return Execute(self, mh, code_item, shadow_frame, JValue()); |
| 513 | } |
| 514 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 515 | extern "C" void artInterpreterToInterpreterBridge(Thread* self, MethodHelper& mh, |
| 516 | const DexFile::CodeItem* code_item, |
| 517 | ShadowFrame* shadow_frame, JValue* result) { |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 518 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) { |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 519 | ThrowStackOverflowError(self); |
Jeff Hao | 6951067 | 2013-05-21 17:34:55 -0700 | [diff] [blame] | 520 | return; |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 523 | self->PushShadowFrame(shadow_frame); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 524 | ArtMethod* method = shadow_frame->GetMethod(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 525 | // Ensure static methods are initialized. |
| 526 | if (method->IsStatic()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 527 | StackHandleScope<1> hs(self); |
| 528 | Handle<Class> declaringClass(hs.NewHandle(method->GetDeclaringClass())); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 529 | if (UNLIKELY(!declaringClass->IsInitializing())) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 530 | if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaringClass, true, |
| 531 | true))) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 532 | DCHECK(Thread::Current()->IsExceptionPending()); |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 533 | self->PopShadowFrame(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 534 | return; |
| 535 | } |
| 536 | CHECK(declaringClass->IsInitializing()); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 537 | } |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 538 | } |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 539 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 540 | if (LIKELY(!method->IsNative())) { |
Jeff Hao | 6951067 | 2013-05-21 17:34:55 -0700 | [diff] [blame] | 541 | result->SetJ(Execute(self, mh, code_item, *shadow_frame, JValue()).GetJ()); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 542 | } else { |
| 543 | // We don't expect to be asked to interpret native code (which is entered via a JNI compiler |
| 544 | // generated stub) except during testing and image writing. |
| 545 | CHECK(!Runtime::Current()->IsStarted()); |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 546 | Object* receiver = method->IsStatic() ? nullptr : shadow_frame->GetVRegReference(0); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 547 | uint32_t* args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1); |
Jeff Hao | 6951067 | 2013-05-21 17:34:55 -0700 | [diff] [blame] | 548 | UnstartedRuntimeJni(self, method, receiver, args, result); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | self->PopShadowFrame(); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 554 | } // namespace interpreter |
| 555 | } // namespace art |