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" |
Maja Gagic | 6ea651f | 2015-02-24 16:55:04 +0100 | [diff] [blame] | 23 | #include "jni/quick/mips64/calling_convention_mips64.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | #include "jni/quick/x86/calling_convention_x86.h" |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 25 | #include "jni/quick/x86_64/calling_convention_x86_64.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | #include "utils.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 30 | // Managed runtime calling convention |
| 31 | |
| 32 | ManagedRuntimeCallingConvention* ManagedRuntimeCallingConvention::Create( |
| 33 | bool is_static, bool is_synchronized, const char* shorty, InstructionSet instruction_set) { |
| 34 | switch (instruction_set) { |
| 35 | case kArm: |
| 36 | case kThumb2: |
| 37 | return new arm::ArmManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 38 | case kArm64: |
| 39 | return new arm64::Arm64ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 40 | case kMips: |
| 41 | return new mips::MipsManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Maja Gagic | 6ea651f | 2015-02-24 16:55:04 +0100 | [diff] [blame] | 42 | case kMips64: |
| 43 | return new mips64::Mips64ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 44 | case kX86: |
| 45 | return new x86::X86ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 46 | case kX86_64: |
| 47 | return new x86_64::X86_64ManagedRuntimeCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 48 | default: |
| 49 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 50 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | bool ManagedRuntimeCallingConvention::HasNext() { |
| 55 | return itr_args_ < NumArgs(); |
| 56 | } |
| 57 | |
| 58 | void ManagedRuntimeCallingConvention::Next() { |
| 59 | CHECK(HasNext()); |
| 60 | if (IsCurrentArgExplicit() && // don't query parameter type of implicit args |
| 61 | IsParamALongOrDouble(itr_args_)) { |
| 62 | itr_longs_and_doubles_++; |
| 63 | itr_slots_++; |
| 64 | } |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 65 | if (IsParamAFloatOrDouble(itr_args_)) { |
| 66 | itr_float_and_doubles_++; |
| 67 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 68 | if (IsCurrentParamAReference()) { |
| 69 | itr_refs_++; |
| 70 | } |
| 71 | itr_args_++; |
| 72 | itr_slots_++; |
| 73 | } |
| 74 | |
| 75 | bool ManagedRuntimeCallingConvention::IsCurrentArgExplicit() { |
| 76 | // Static methods have no implicit arguments, others implicitly pass this |
| 77 | return IsStatic() || (itr_args_ != 0); |
| 78 | } |
| 79 | |
| 80 | bool ManagedRuntimeCallingConvention::IsCurrentArgPossiblyNull() { |
| 81 | return IsCurrentArgExplicit(); // any user parameter may be null |
| 82 | } |
| 83 | |
| 84 | size_t ManagedRuntimeCallingConvention::CurrentParamSize() { |
| 85 | return ParamSize(itr_args_); |
| 86 | } |
| 87 | |
| 88 | bool ManagedRuntimeCallingConvention::IsCurrentParamAReference() { |
| 89 | return IsParamAReference(itr_args_); |
| 90 | } |
| 91 | |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 92 | bool ManagedRuntimeCallingConvention::IsCurrentParamAFloatOrDouble() { |
| 93 | return IsParamAFloatOrDouble(itr_args_); |
| 94 | } |
| 95 | |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 96 | bool ManagedRuntimeCallingConvention::IsCurrentParamADouble() { |
| 97 | return IsParamADouble(itr_args_); |
| 98 | } |
| 99 | |
| 100 | bool ManagedRuntimeCallingConvention::IsCurrentParamALong() { |
| 101 | return IsParamALong(itr_args_); |
| 102 | } |
| 103 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 104 | // JNI calling convention |
| 105 | |
| 106 | JniCallingConvention* JniCallingConvention::Create(bool is_static, bool is_synchronized, |
| 107 | const char* shorty, |
| 108 | InstructionSet instruction_set) { |
| 109 | switch (instruction_set) { |
| 110 | case kArm: |
| 111 | case kThumb2: |
| 112 | return new arm::ArmJniCallingConvention(is_static, is_synchronized, shorty); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 113 | case kArm64: |
| 114 | return new arm64::Arm64JniCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 115 | case kMips: |
| 116 | return new mips::MipsJniCallingConvention(is_static, is_synchronized, shorty); |
Maja Gagic | 6ea651f | 2015-02-24 16:55:04 +0100 | [diff] [blame] | 117 | case kMips64: |
| 118 | return new mips64::Mips64JniCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 119 | case kX86: |
| 120 | return new x86::X86JniCallingConvention(is_static, is_synchronized, shorty); |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 121 | case kX86_64: |
| 122 | return new x86_64::X86_64JniCallingConvention(is_static, is_synchronized, shorty); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 123 | default: |
| 124 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 125 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | size_t JniCallingConvention::ReferenceCount() const { |
| 130 | return NumReferenceArgs() + (IsStatic() ? 1 : 0); |
| 131 | } |
| 132 | |
| 133 | FrameOffset JniCallingConvention::SavedLocalReferenceCookieOffset() const { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 134 | size_t references_size = handle_scope_pointer_size_ * ReferenceCount(); // size excluding header |
| 135 | return FrameOffset(HandleerencesOffset().Int32Value() + references_size); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | FrameOffset JniCallingConvention::ReturnValueSaveLocation() const { |
| 139 | // Segment state is 4 bytes long |
| 140 | return FrameOffset(SavedLocalReferenceCookieOffset().Int32Value() + 4); |
| 141 | } |
| 142 | |
| 143 | bool JniCallingConvention::HasNext() { |
| 144 | if (itr_args_ <= kObjectOrClass) { |
| 145 | return true; |
| 146 | } else { |
| 147 | unsigned int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 148 | return arg_pos < NumArgs(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void JniCallingConvention::Next() { |
| 153 | CHECK(HasNext()); |
| 154 | if (itr_args_ > kObjectOrClass) { |
| 155 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 156 | if (IsParamALongOrDouble(arg_pos)) { |
| 157 | itr_longs_and_doubles_++; |
| 158 | itr_slots_++; |
| 159 | } |
| 160 | } |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 161 | if (IsCurrentParamAFloatOrDouble()) { |
| 162 | itr_float_and_doubles_++; |
| 163 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 164 | if (IsCurrentParamAReference()) { |
| 165 | itr_refs_++; |
| 166 | } |
| 167 | itr_args_++; |
| 168 | itr_slots_++; |
| 169 | } |
| 170 | |
| 171 | bool JniCallingConvention::IsCurrentParamAReference() { |
| 172 | switch (itr_args_) { |
| 173 | case kJniEnv: |
| 174 | return false; // JNIEnv* |
| 175 | case kObjectOrClass: |
| 176 | return true; // jobject or jclass |
| 177 | default: { |
| 178 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 179 | return IsParamAReference(arg_pos); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 184 | bool JniCallingConvention::IsCurrentParamJniEnv() { |
| 185 | return (itr_args_ == kJniEnv); |
| 186 | } |
| 187 | |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 188 | bool JniCallingConvention::IsCurrentParamAFloatOrDouble() { |
| 189 | switch (itr_args_) { |
| 190 | case kJniEnv: |
| 191 | return false; // JNIEnv* |
| 192 | case kObjectOrClass: |
| 193 | return false; // jobject or jclass |
| 194 | default: { |
| 195 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 196 | return IsParamAFloatOrDouble(arg_pos); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 201 | bool JniCallingConvention::IsCurrentParamADouble() { |
| 202 | switch (itr_args_) { |
| 203 | case kJniEnv: |
| 204 | return false; // JNIEnv* |
| 205 | case kObjectOrClass: |
| 206 | return false; // jobject or jclass |
| 207 | default: { |
| 208 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 209 | return IsParamADouble(arg_pos); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | bool JniCallingConvention::IsCurrentParamALong() { |
| 215 | switch (itr_args_) { |
| 216 | case kJniEnv: |
| 217 | return false; // JNIEnv* |
| 218 | case kObjectOrClass: |
| 219 | return false; // jobject or jclass |
| 220 | default: { |
| 221 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 222 | return IsParamALong(arg_pos); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 227 | // Return position of handle scope entry holding reference at the current iterator |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 228 | // position |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 229 | FrameOffset JniCallingConvention::CurrentParamHandleScopeEntryOffset() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 230 | CHECK(IsCurrentParamAReference()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 231 | CHECK_LT(HandleScopeLinkOffset(), HandleScopeNumRefsOffset()); |
| 232 | int result = HandleerencesOffset().Int32Value() + itr_refs_ * handle_scope_pointer_size_; |
| 233 | CHECK_GT(result, HandleScopeNumRefsOffset().Int32Value()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 234 | return FrameOffset(result); |
| 235 | } |
| 236 | |
| 237 | size_t JniCallingConvention::CurrentParamSize() { |
| 238 | if (itr_args_ <= kObjectOrClass) { |
Ian Rogers | 790a6b7 | 2014-04-01 10:36:00 -0700 | [diff] [blame] | 239 | return frame_pointer_size_; // JNIEnv or jobject/jclass |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 240 | } else { |
| 241 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 242 | return ParamSize(arg_pos); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | size_t JniCallingConvention::NumberOfExtraArgumentsForJni() { |
| 247 | // The first argument is the JNIEnv*. |
| 248 | // Static methods have an extra argument which is the jclass. |
| 249 | return IsStatic() ? 2 : 1; |
| 250 | } |
| 251 | |
| 252 | } // namespace art |