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