Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | #include "calling_convention.h" |
| 18 | |
| 19 | #include "base/logging.h" |
| 20 | #include "jni/quick/arm/calling_convention_arm.h" |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 21 | #include "jni/quick/arm64/calling_convention_arm64.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | #include "jni/quick/mips/calling_convention_mips.h" |
| 23 | #include "jni/quick/x86/calling_convention_x86.h" |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame^] | 24 | #include "jni/quick/x86_64/calling_convention_x86_64.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 25 | #include "utils.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | // Offset of Method within the frame |
| 30 | FrameOffset CallingConvention::MethodStackOffset() { |
| 31 | return displacement_; |
| 32 | } |
| 33 | |
| 34 | // Managed runtime calling convention |
| 35 | |
| 36 | ManagedRuntimeCallingConvention* ManagedRuntimeCallingConvention::Create( |
| 37 | bool is_static, bool is_synchronized, const char* shorty, InstructionSet instruction_set) { |
| 38 | switch (instruction_set) { |
| 39 | case kArm: |
| 40 | case kThumb2: |
| 41 | return new arm::ArmManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 42 | case kArm64: |
| 43 | return new arm64::Arm64ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 44 | case kMips: |
| 45 | return new mips::MipsManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
| 46 | case kX86: |
| 47 | return new x86::X86ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame^] | 48 | case kX86_64: |
| 49 | return new x86_64::X86_64ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 50 | default: |
| 51 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
| 52 | return NULL; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | bool ManagedRuntimeCallingConvention::HasNext() { |
| 57 | return itr_args_ < NumArgs(); |
| 58 | } |
| 59 | |
| 60 | void ManagedRuntimeCallingConvention::Next() { |
| 61 | CHECK(HasNext()); |
| 62 | if (IsCurrentArgExplicit() && // don't query parameter type of implicit args |
| 63 | IsParamALongOrDouble(itr_args_)) { |
| 64 | itr_longs_and_doubles_++; |
| 65 | itr_slots_++; |
| 66 | } |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame^] | 67 | if (IsParamAFloatOrDouble(itr_args_)) { |
| 68 | itr_float_and_doubles_++; |
| 69 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 70 | if (IsCurrentParamAReference()) { |
| 71 | itr_refs_++; |
| 72 | } |
| 73 | itr_args_++; |
| 74 | itr_slots_++; |
| 75 | } |
| 76 | |
| 77 | bool ManagedRuntimeCallingConvention::IsCurrentArgExplicit() { |
| 78 | // Static methods have no implicit arguments, others implicitly pass this |
| 79 | return IsStatic() || (itr_args_ != 0); |
| 80 | } |
| 81 | |
| 82 | bool ManagedRuntimeCallingConvention::IsCurrentArgPossiblyNull() { |
| 83 | return IsCurrentArgExplicit(); // any user parameter may be null |
| 84 | } |
| 85 | |
| 86 | size_t ManagedRuntimeCallingConvention::CurrentParamSize() { |
| 87 | return ParamSize(itr_args_); |
| 88 | } |
| 89 | |
| 90 | bool ManagedRuntimeCallingConvention::IsCurrentParamAReference() { |
| 91 | return IsParamAReference(itr_args_); |
| 92 | } |
| 93 | |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame^] | 94 | bool ManagedRuntimeCallingConvention::IsCurrentParamAFloatOrDouble() { |
| 95 | return IsParamAFloatOrDouble(itr_args_); |
| 96 | } |
| 97 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 98 | // JNI calling convention |
| 99 | |
| 100 | JniCallingConvention* JniCallingConvention::Create(bool is_static, bool is_synchronized, |
| 101 | const char* shorty, |
| 102 | InstructionSet instruction_set) { |
| 103 | switch (instruction_set) { |
| 104 | case kArm: |
| 105 | case kThumb2: |
| 106 | return new arm::ArmJniCallingConvention(is_static, is_synchronized, shorty); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 107 | case kArm64: |
| 108 | return new arm64::Arm64JniCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 109 | case kMips: |
| 110 | return new mips::MipsJniCallingConvention(is_static, is_synchronized, shorty); |
| 111 | case kX86: |
| 112 | return new x86::X86JniCallingConvention(is_static, is_synchronized, shorty); |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame^] | 113 | case kX86_64: |
| 114 | return new x86_64::X86_64JniCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 115 | default: |
| 116 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
| 117 | return NULL; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | size_t JniCallingConvention::ReferenceCount() const { |
| 122 | return NumReferenceArgs() + (IsStatic() ? 1 : 0); |
| 123 | } |
| 124 | |
| 125 | FrameOffset JniCallingConvention::SavedLocalReferenceCookieOffset() const { |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame^] | 126 | size_t references_size = kSirtPointerSize * ReferenceCount(); // size excluding header |
| 127 | return FrameOffset(SirtReferencesOffset().Int32Value() + references_size); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | FrameOffset JniCallingConvention::ReturnValueSaveLocation() const { |
| 131 | // Segment state is 4 bytes long |
| 132 | return FrameOffset(SavedLocalReferenceCookieOffset().Int32Value() + 4); |
| 133 | } |
| 134 | |
| 135 | bool JniCallingConvention::HasNext() { |
| 136 | if (itr_args_ <= kObjectOrClass) { |
| 137 | return true; |
| 138 | } else { |
| 139 | unsigned int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 140 | return arg_pos < NumArgs(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void JniCallingConvention::Next() { |
| 145 | CHECK(HasNext()); |
| 146 | if (itr_args_ > kObjectOrClass) { |
| 147 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 148 | if (IsParamALongOrDouble(arg_pos)) { |
| 149 | itr_longs_and_doubles_++; |
| 150 | itr_slots_++; |
| 151 | } |
| 152 | } |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame^] | 153 | if (IsCurrentParamAFloatOrDouble()) { |
| 154 | itr_float_and_doubles_++; |
| 155 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 156 | if (IsCurrentParamAReference()) { |
| 157 | itr_refs_++; |
| 158 | } |
| 159 | itr_args_++; |
| 160 | itr_slots_++; |
| 161 | } |
| 162 | |
| 163 | bool JniCallingConvention::IsCurrentParamAReference() { |
| 164 | switch (itr_args_) { |
| 165 | case kJniEnv: |
| 166 | return false; // JNIEnv* |
| 167 | case kObjectOrClass: |
| 168 | return true; // jobject or jclass |
| 169 | default: { |
| 170 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 171 | return IsParamAReference(arg_pos); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame^] | 176 | bool JniCallingConvention::IsCurrentParamAFloatOrDouble() { |
| 177 | switch (itr_args_) { |
| 178 | case kJniEnv: |
| 179 | return false; // JNIEnv* |
| 180 | case kObjectOrClass: |
| 181 | return false; // jobject or jclass |
| 182 | default: { |
| 183 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 184 | return IsParamAFloatOrDouble(arg_pos); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 189 | // Return position of SIRT entry holding reference at the current iterator |
| 190 | // position |
| 191 | FrameOffset JniCallingConvention::CurrentParamSirtEntryOffset() { |
| 192 | CHECK(IsCurrentParamAReference()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 193 | CHECK_LT(SirtLinkOffset(), SirtNumRefsOffset()); |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame^] | 194 | int result = SirtReferencesOffset().Int32Value() + itr_refs_ * kSirtPointerSize; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 195 | CHECK_GT(result, SirtNumRefsOffset().Int32Value()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 196 | return FrameOffset(result); |
| 197 | } |
| 198 | |
| 199 | size_t JniCallingConvention::CurrentParamSize() { |
| 200 | if (itr_args_ <= kObjectOrClass) { |
| 201 | return kPointerSize; // JNIEnv or jobject/jclass |
| 202 | } else { |
| 203 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 204 | return ParamSize(arg_pos); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | size_t JniCallingConvention::NumberOfExtraArgumentsForJni() { |
| 209 | // The first argument is the JNIEnv*. |
| 210 | // Static methods have an extra argument which is the jclass. |
| 211 | return IsStatic() ? 2 : 1; |
| 212 | } |
| 213 | |
| 214 | } // namespace art |