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(); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 39 | result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length, |
| 40 | array_class->GetComponentSizeShift(), allocator)); |
Ian Rogers | a4e7413 | 2014-05-06 01:14:54 -0700 | [diff] [blame] | 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); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 141 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
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); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 152 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
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); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 159 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
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); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 166 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
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); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 173 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
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); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 180 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
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); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 193 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
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); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 200 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>( |
| 201 | method->GetEntryPointFromJni())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 202 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 203 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 204 | ScopedLocalRef<jobject> arg0(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 205 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 206 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 207 | result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 208 | } else if (shorty == "SIZ") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 209 | typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 210 | fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 211 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 212 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 213 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 214 | result->SetS(fn(soa.Env(), klass.get(), args[0], args[1])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 215 | } else if (shorty == "VIZ") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 216 | typedef void (fntype)(JNIEnv*, jclass, jint, jboolean); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 217 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 218 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 219 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 220 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 221 | fn(soa.Env(), klass.get(), args[0], args[1]); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 222 | } else if (shorty == "ZLL") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 223 | typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 224 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 225 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 226 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 227 | ScopedLocalRef<jobject> arg0(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 228 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 229 | ScopedLocalRef<jobject> arg1(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 230 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 231 | ScopedThreadStateChange tsc(self, kNative); |
| 232 | result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get())); |
| 233 | } else if (shorty == "ZILL") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 234 | typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 235 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 236 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 237 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 238 | ScopedLocalRef<jobject> arg1(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 239 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 240 | ScopedLocalRef<jobject> arg2(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 241 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 242 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 243 | 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] | 244 | } else if (shorty == "VILII") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 245 | typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 246 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 247 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 248 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 249 | ScopedLocalRef<jobject> arg1(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 250 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 251 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 252 | 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] | 253 | } else if (shorty == "VLILII") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 254 | typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 255 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 256 | ScopedLocalRef<jclass> klass(soa.Env(), |
| 257 | soa.AddLocalReference<jclass>(method->GetDeclaringClass())); |
| 258 | ScopedLocalRef<jobject> arg0(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 259 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 260 | ScopedLocalRef<jobject> arg2(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 261 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2]))); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 262 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 263 | 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] | 264 | } else { |
| 265 | LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method) |
| 266 | << " shorty: " << shorty; |
| 267 | } |
| 268 | } else { |
| 269 | if (shorty == "L") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 270 | typedef jobject (fntype)(JNIEnv*, jobject); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 271 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 272 | ScopedLocalRef<jobject> rcvr(soa.Env(), |
| 273 | soa.AddLocalReference<jobject>(receiver)); |
Ian Rogers | 556d637 | 2012-11-20 12:19:36 -0800 | [diff] [blame] | 274 | jobject jresult; |
| 275 | { |
| 276 | ScopedThreadStateChange tsc(self, kNative); |
| 277 | jresult = fn(soa.Env(), rcvr.get()); |
| 278 | } |
| 279 | result->SetL(soa.Decode<Object*>(jresult)); |
Jeff Hao | 3dd9f76 | 2013-07-08 13:09:25 -0700 | [diff] [blame] | 280 | } else if (shorty == "V") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 281 | typedef void (fntype)(JNIEnv*, jobject); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 282 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
Jeff Hao | 3dd9f76 | 2013-07-08 13:09:25 -0700 | [diff] [blame] | 283 | ScopedLocalRef<jobject> rcvr(soa.Env(), |
| 284 | soa.AddLocalReference<jobject>(receiver)); |
| 285 | ScopedThreadStateChange tsc(self, kNative); |
| 286 | fn(soa.Env(), rcvr.get()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 287 | } else if (shorty == "LL") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 288 | typedef jobject (fntype)(JNIEnv*, jobject, jobject); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 289 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 290 | ScopedLocalRef<jobject> rcvr(soa.Env(), |
| 291 | soa.AddLocalReference<jobject>(receiver)); |
| 292 | ScopedLocalRef<jobject> arg0(soa.Env(), |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 293 | soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0]))); |
Ian Rogers | 556d637 | 2012-11-20 12:19:36 -0800 | [diff] [blame] | 294 | jobject jresult; |
| 295 | { |
| 296 | ScopedThreadStateChange tsc(self, kNative); |
| 297 | jresult = fn(soa.Env(), rcvr.get(), arg0.get()); |
Ian Rogers | 556d637 | 2012-11-20 12:19:36 -0800 | [diff] [blame] | 298 | } |
| 299 | result->SetL(soa.Decode<Object*>(jresult)); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 300 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 301 | } else if (shorty == "III") { |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 302 | typedef jint (fntype)(JNIEnv*, jobject, jint, jint); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 303 | fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 304 | ScopedLocalRef<jobject> rcvr(soa.Env(), |
| 305 | soa.AddLocalReference<jobject>(receiver)); |
| 306 | ScopedThreadStateChange tsc(self, kNative); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 307 | result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1])); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 308 | } else { |
| 309 | LOG(FATAL) << "Do something with native method: " << PrettyMethod(method) |
| 310 | << " shorty: " << shorty; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 315 | enum InterpreterImplKind { |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 316 | kSwitchImpl, // Switch-based interpreter implementation. |
| 317 | kComputedGotoImplKind // Computed-goto-based interpreter implementation. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 318 | }; |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 319 | static std::ostream& operator<<(std::ostream& os, const InterpreterImplKind& rhs) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 320 | os << ((rhs == kSwitchImpl) ? "Switch-based interpreter" : "Computed-goto-based interpreter"); |
| 321 | return os; |
| 322 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 323 | |
Sebastien Hertz | fa888d0 | 2014-09-30 12:00:11 +0200 | [diff] [blame] | 324 | #if !defined(__clang__) |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 325 | static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind; |
Sebastien Hertz | fa888d0 | 2014-09-30 12:00:11 +0200 | [diff] [blame] | 326 | #else |
| 327 | // Clang 3.4 fails to build the goto interpreter implementation. |
| 328 | static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl; |
| 329 | template<bool do_access_check, bool transaction_active> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 330 | JValue ExecuteGotoImpl(Thread*, const DexFile::CodeItem*, ShadowFrame&, JValue) { |
Sebastien Hertz | fa888d0 | 2014-09-30 12:00:11 +0200 | [diff] [blame] | 331 | LOG(FATAL) << "UNREACHABLE"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 332 | UNREACHABLE(); |
Sebastien Hertz | fa888d0 | 2014-09-30 12:00:11 +0200 | [diff] [blame] | 333 | } |
| 334 | // Explicit definitions of ExecuteGotoImpl. |
| 335 | template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 336 | JValue ExecuteGotoImpl<true, false>(Thread* self, const DexFile::CodeItem* code_item, |
Sebastien Hertz | fa888d0 | 2014-09-30 12:00:11 +0200 | [diff] [blame] | 337 | ShadowFrame& shadow_frame, JValue result_register); |
| 338 | template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 339 | JValue ExecuteGotoImpl<false, false>(Thread* self, const DexFile::CodeItem* code_item, |
Sebastien Hertz | fa888d0 | 2014-09-30 12:00:11 +0200 | [diff] [blame] | 340 | ShadowFrame& shadow_frame, JValue result_register); |
| 341 | template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 342 | JValue ExecuteGotoImpl<true, true>(Thread* self, const DexFile::CodeItem* code_item, |
| 343 | ShadowFrame& shadow_frame, JValue result_register); |
| 344 | template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 345 | JValue ExecuteGotoImpl<false, true>(Thread* self, const DexFile::CodeItem* code_item, |
Sebastien Hertz | fa888d0 | 2014-09-30 12:00:11 +0200 | [diff] [blame] | 346 | ShadowFrame& shadow_frame, JValue result_register); |
Sebastien Hertz | fa888d0 | 2014-09-30 12:00:11 +0200 | [diff] [blame] | 347 | #endif |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 348 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 349 | static JValue Execute(Thread* self, const DexFile::CodeItem* code_item, ShadowFrame& shadow_frame, |
| 350 | JValue result_register) |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 351 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 352 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 353 | static inline JValue Execute(Thread* self, const DexFile::CodeItem* code_item, |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 354 | ShadowFrame& shadow_frame, JValue result_register) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 355 | DCHECK(!shadow_frame.GetMethod()->IsAbstract()); |
| 356 | DCHECK(!shadow_frame.GetMethod()->IsNative()); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 357 | shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 358 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 359 | bool transaction_active = Runtime::Current()->IsActiveTransaction(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 360 | if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) { |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 361 | // Enter the "without access check" interpreter. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 362 | if (kInterpreterImplKind == kSwitchImpl) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 363 | if (transaction_active) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 364 | return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 365 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 366 | return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 367 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 368 | } else { |
| 369 | DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 370 | if (transaction_active) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 371 | return ExecuteGotoImpl<false, true>(self, code_item, shadow_frame, result_register); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 372 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 373 | return ExecuteGotoImpl<false, false>(self, code_item, shadow_frame, result_register); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 374 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 375 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 376 | } else { |
| 377 | // Enter the "with access check" interpreter. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 378 | if (kInterpreterImplKind == kSwitchImpl) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 379 | if (transaction_active) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 380 | return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 381 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 382 | return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 383 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 384 | } else { |
| 385 | DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 386 | if (transaction_active) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 387 | return ExecuteGotoImpl<true, true>(self, code_item, shadow_frame, result_register); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 388 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 389 | return ExecuteGotoImpl<true, false>(self, code_item, shadow_frame, result_register); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 390 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 391 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 395 | void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver, |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 396 | uint32_t* args, JValue* result) { |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 397 | DCHECK_EQ(self, Thread::Current()); |
Nicolas Geoffray | 535a3fb | 2014-07-22 15:17:38 +0100 | [diff] [blame] | 398 | bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks(); |
| 399 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) { |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 400 | ThrowStackOverflowError(self); |
| 401 | return; |
| 402 | } |
| 403 | |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 404 | const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke"); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 405 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 406 | uint16_t num_regs; |
| 407 | uint16_t num_ins; |
| 408 | if (code_item != NULL) { |
| 409 | num_regs = code_item->registers_size_; |
| 410 | num_ins = code_item->ins_size_; |
jeffhao | 0a9bb73 | 2012-11-26 12:28:49 -0800 | [diff] [blame] | 411 | } else if (method->IsAbstract()) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 412 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 56adf60 | 2013-07-09 17:27:07 +0200 | [diff] [blame] | 413 | ThrowAbstractMethodError(method); |
jeffhao | 0a9bb73 | 2012-11-26 12:28:49 -0800 | [diff] [blame] | 414 | return; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 415 | } else { |
| 416 | DCHECK(method->IsNative()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 417 | num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty()); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 418 | if (!method->IsStatic()) { |
| 419 | num_regs++; |
| 420 | num_ins++; |
| 421 | } |
| 422 | } |
| 423 | // Set up shadow frame with matching number of reference slots to vregs. |
| 424 | ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame(); |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 425 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
| 426 | ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory)); |
| 427 | self->PushShadowFrame(shadow_frame); |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 428 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 429 | size_t cur_reg = num_regs - num_ins; |
| 430 | if (!method->IsStatic()) { |
| 431 | CHECK(receiver != NULL); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 432 | shadow_frame->SetVRegReference(cur_reg, receiver); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 433 | ++cur_reg; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 434 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 435 | uint32_t shorty_len = 0; |
| 436 | const char* shorty = method->GetShorty(&shorty_len); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 437 | 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] | 438 | DCHECK_LT(shorty_pos + 1, shorty_len); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 439 | switch (shorty[shorty_pos + 1]) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 440 | case 'L': { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 441 | Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr(); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 442 | shadow_frame->SetVRegReference(cur_reg, o); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 443 | break; |
| 444 | } |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 445 | case 'J': case 'D': { |
| 446 | uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos]; |
| 447 | shadow_frame->SetVRegLong(cur_reg, wide_value); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 448 | cur_reg++; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 449 | arg_pos++; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 450 | break; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 451 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 452 | default: |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 453 | shadow_frame->SetVReg(cur_reg, args[arg_pos]); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 454 | break; |
| 455 | } |
| 456 | } |
Mathieu Chartier | 92246bb | 2014-02-25 18:22:39 -0800 | [diff] [blame] | 457 | self->EndAssertNoThreadSuspension(old_cause); |
| 458 | // 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] | 459 | if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) { |
Mathieu Chartier | 92246bb | 2014-02-25 18:22:39 -0800 | [diff] [blame] | 460 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 461 | StackHandleScope<1> hs(self); |
| 462 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 463 | if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) { |
Mathieu Chartier | 92246bb | 2014-02-25 18:22:39 -0800 | [diff] [blame] | 464 | CHECK(self->IsExceptionPending()); |
| 465 | self->PopShadowFrame(); |
| 466 | return; |
| 467 | } |
| 468 | } |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 469 | if (LIKELY(!method->IsNative())) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 470 | JValue r = Execute(self, code_item, *shadow_frame, JValue()); |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 471 | if (result != NULL) { |
| 472 | *result = r; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 473 | } |
| 474 | } else { |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 475 | // We don't expect to be asked to interpret native code (which is entered via a JNI compiler |
| 476 | // generated stub) except during testing and image writing. |
Mathieu Chartier | 92246bb | 2014-02-25 18:22:39 -0800 | [diff] [blame] | 477 | // Update args to be the args in the shadow frame since the input ones could hold stale |
| 478 | // references pointers due to moving GC. |
| 479 | args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 480 | if (!Runtime::Current()->IsStarted()) { |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 481 | UnstartedRuntimeJni(self, method, receiver, args, result); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 482 | } else { |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 483 | InterpreterJni(self, method, shorty, receiver, args, result); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | self->PopShadowFrame(); |
| 487 | } |
| 488 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 489 | void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val) |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 490 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 491 | JValue value; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 492 | 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] | 493 | while (shadow_frame != NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 494 | self->SetTopOfShadowStack(shadow_frame); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 495 | const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem(); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 496 | const uint32_t dex_pc = shadow_frame->GetDexPC(); |
| 497 | uint32_t new_dex_pc; |
| 498 | if (UNLIKELY(self->IsExceptionPending())) { |
| 499 | const instrumentation::Instrumentation* const instrumentation = |
| 500 | Runtime::Current()->GetInstrumentation(); |
| 501 | uint32_t found_dex_pc = FindNextInstructionFollowingException(self, *shadow_frame, dex_pc, |
| 502 | instrumentation); |
| 503 | new_dex_pc = found_dex_pc; // the dex pc of a matching catch handler |
| 504 | // or DexFile::kDexNoIndex if there is none. |
| 505 | } else { |
| 506 | const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]); |
| 507 | new_dex_pc = dex_pc + instr->SizeInCodeUnits(); // the dex pc of the next instruction. |
| 508 | } |
| 509 | if (new_dex_pc != DexFile::kDexNoIndex) { |
| 510 | shadow_frame->SetDexPC(new_dex_pc); |
| 511 | value = Execute(self, code_item, *shadow_frame, value); |
| 512 | } |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 513 | ShadowFrame* old_frame = shadow_frame; |
| 514 | shadow_frame = shadow_frame->GetLink(); |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 515 | delete old_frame; |
| 516 | } |
| 517 | ret_val->SetJ(value.GetJ()); |
| 518 | } |
| 519 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 520 | JValue EnterInterpreterFromEntryPoint(Thread* self, const DexFile::CodeItem* code_item, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 521 | ShadowFrame* shadow_frame) { |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 522 | DCHECK_EQ(self, Thread::Current()); |
Nicolas Geoffray | 535a3fb | 2014-07-22 15:17:38 +0100 | [diff] [blame] | 523 | bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks(); |
| 524 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) { |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 525 | ThrowStackOverflowError(self); |
| 526 | return JValue(); |
| 527 | } |
| 528 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 529 | return Execute(self, code_item, *shadow_frame, JValue()); |
Ian Rogers | 7db619b | 2013-01-16 18:35:48 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 532 | extern "C" void artInterpreterToInterpreterBridge(Thread* self, const DexFile::CodeItem* code_item, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 533 | ShadowFrame* shadow_frame, JValue* result) { |
Nicolas Geoffray | 535a3fb | 2014-07-22 15:17:38 +0100 | [diff] [blame] | 534 | bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks(); |
| 535 | if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) { |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 536 | ThrowStackOverflowError(self); |
Jeff Hao | 6951067 | 2013-05-21 17:34:55 -0700 | [diff] [blame] | 537 | return; |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 540 | self->PushShadowFrame(shadow_frame); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 541 | // Ensure static methods are initialized. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 542 | const bool is_static = shadow_frame->GetMethod()->IsStatic(); |
| 543 | if (is_static) { |
| 544 | mirror::Class* declaring_class = shadow_frame->GetMethod()->GetDeclaringClass(); |
Ian Rogers | 6c5cb21 | 2014-06-18 16:07:20 -0700 | [diff] [blame] | 545 | if (UNLIKELY(!declaring_class->IsInitialized())) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 546 | StackHandleScope<1> hs(self); |
| 547 | HandleWrapper<Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class)); |
| 548 | if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized( |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 549 | self, h_declaring_class, true, true))) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 550 | DCHECK(self->IsExceptionPending()); |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 551 | self->PopShadowFrame(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 552 | return; |
| 553 | } |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 554 | CHECK(h_declaring_class->IsInitializing()); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 555 | } |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 556 | } |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 557 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 558 | if (LIKELY(!shadow_frame->GetMethod()->IsNative())) { |
| 559 | result->SetJ(Execute(self, code_item, *shadow_frame, JValue()).GetJ()); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 560 | } else { |
| 561 | // We don't expect to be asked to interpret native code (which is entered via a JNI compiler |
| 562 | // generated stub) except during testing and image writing. |
| 563 | CHECK(!Runtime::Current()->IsStarted()); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 564 | Object* receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0); |
| 565 | uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1); |
| 566 | UnstartedRuntimeJni(self, shadow_frame->GetMethod(), receiver, args, result); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | self->PopShadowFrame(); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 572 | } // namespace interpreter |
| 573 | } // namespace art |