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