Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [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 | */ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 17 | #include "calling_convention_arm.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | #include "logging.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 19 | #include "managed_register_arm.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 20 | |
| 21 | namespace art { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 22 | namespace arm { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 23 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 24 | // Calling convention |
| 25 | |
| 26 | ManagedRegister ArmManagedRuntimeCallingConvention::InterproceduralScratchRegister() { |
| 27 | return ArmManagedRegister::FromCoreRegister(IP); // R12 |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 30 | ManagedRegister ArmJniCallingConvention::InterproceduralScratchRegister() { |
| 31 | return ArmManagedRegister::FromCoreRegister(IP); // R12 |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 34 | static ManagedRegister ReturnRegisterForShorty(const char* shorty) { |
| 35 | if (shorty[0] == 'F') { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 36 | return ArmManagedRegister::FromCoreRegister(R0); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 37 | } else if (shorty[0] == 'D') { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 38 | return ArmManagedRegister::FromRegisterPair(R0_R1); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 39 | } else if (shorty[0] == 'J') { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 40 | return ArmManagedRegister::FromRegisterPair(R0_R1); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 41 | } else if (shorty[0] == 'V') { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 42 | return ArmManagedRegister::NoRegister(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 43 | } else { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 44 | return ArmManagedRegister::FromCoreRegister(R0); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 48 | ManagedRegister ArmManagedRuntimeCallingConvention::ReturnRegister() { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 49 | return ReturnRegisterForShorty(GetShorty()); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | ManagedRegister ArmJniCallingConvention::ReturnRegister() { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 53 | return ReturnRegisterForShorty(GetShorty()); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 56 | // Managed runtime calling convention |
| 57 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 58 | ManagedRegister ArmManagedRuntimeCallingConvention::MethodRegister() { |
| 59 | return ArmManagedRegister::FromCoreRegister(R0); |
| 60 | } |
| 61 | |
| 62 | bool ArmManagedRuntimeCallingConvention::IsCurrentParamInRegister() { |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 63 | return itr_slots_ < 3; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 66 | bool ArmManagedRuntimeCallingConvention::IsCurrentParamOnStack() { |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 67 | if (itr_slots_ < 2) { |
| 68 | return false; |
| 69 | } else if (itr_slots_ > 2) { |
| 70 | return true; |
| 71 | } else { |
| 72 | // handle funny case of a long/double straddling registers and the stack |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 73 | return IsParamALongOrDouble(itr_args_); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 74 | } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static const Register kManagedArgumentRegisters[] = { |
| 78 | R1, R2, R3 |
| 79 | }; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 80 | ManagedRegister ArmManagedRuntimeCallingConvention::CurrentParamRegister() { |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 81 | CHECK(IsCurrentParamInRegister()); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 82 | if (IsParamALongOrDouble(itr_args_)) { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 83 | if (itr_slots_ == 0) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 84 | return ArmManagedRegister::FromRegisterPair(R1_R2); |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 85 | } else if (itr_slots_ == 1) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 86 | return ArmManagedRegister::FromRegisterPair(R2_R3); |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 87 | } else { |
| 88 | // This is a long/double split between registers and the stack |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 89 | return ArmManagedRegister::FromCoreRegister( |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 90 | kManagedArgumentRegisters[itr_slots_]); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 91 | } |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 92 | } else { |
| 93 | return |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 94 | ArmManagedRegister::FromCoreRegister(kManagedArgumentRegisters[itr_slots_]); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 95 | } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 98 | FrameOffset ArmManagedRuntimeCallingConvention::CurrentParamStackOffset() { |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 99 | CHECK(IsCurrentParamOnStack()); |
| 100 | FrameOffset result = |
| 101 | FrameOffset(displacement_.Int32Value() + // displacement |
| 102 | kPointerSize + // Method* |
| 103 | (itr_slots_ * kPointerSize)); // offset into in args |
| 104 | if (itr_slots_ == 2) { |
| 105 | // the odd spanning case, bump the offset to skip the first half of the |
| 106 | // input which is in a register |
| 107 | CHECK(IsCurrentParamInRegister()); |
| 108 | result = FrameOffset(result.Int32Value() + 4); |
| 109 | } |
| 110 | return result; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // JNI calling convention |
| 114 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 115 | ArmJniCallingConvention::ArmJniCallingConvention(bool is_static, bool is_synchronized, |
| 116 | const char* shorty) |
| 117 | : JniCallingConvention(is_static, is_synchronized, shorty) { |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 118 | // Compute padding to ensure longs and doubles are not split in AAPCS |
| 119 | // TODO: in terms of outgoing argument size this may be overly generous |
| 120 | // due to padding appearing in the registers |
| 121 | size_t padding = 0; |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 122 | size_t check = IsStatic() ? 1 : 0; |
| 123 | for (size_t i = 0; i < NumArgs(); i++) { |
| 124 | if (((i & 1) == check) && IsParamALongOrDouble(i)) { |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 125 | padding += 4; |
| 126 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 127 | } |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 128 | padding_ = padding; |
Shih-wei Liao | 19e53eb | 2011-10-05 19:11:46 -0700 | [diff] [blame] | 129 | |
| 130 | callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R5)); |
| 131 | callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R6)); |
| 132 | callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R7)); |
| 133 | callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R8)); |
| 134 | callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R10)); |
| 135 | callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R11)); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | uint32_t ArmJniCallingConvention::CoreSpillMask() const { |
| 139 | // Compute spill mask to agree with callee saves initialized in the constructor |
| 140 | uint32_t result = 0; |
Shih-wei Liao | 19e53eb | 2011-10-05 19:11:46 -0700 | [diff] [blame] | 141 | result = 1 << R5 | 1 << R6 | 1 << R7 | 1 << R8 | 1 << R10 | 1 << R11 | 1 << LR; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 142 | return result; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 145 | ManagedRegister ArmJniCallingConvention::ReturnScratchRegister() const { |
| 146 | return ArmManagedRegister::FromCoreRegister(R2); |
| 147 | } |
| 148 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 149 | size_t ArmJniCallingConvention::FrameSize() { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 150 | // Method*, LR and callee save area size, local reference segment state |
| 151 | size_t frame_data_size = (3 + CalleeSaveRegisters().size()) * kPointerSize; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 152 | // References plus 2 words for SIRT header |
| 153 | size_t sirt_size = (ReferenceCount() + 2) * kPointerSize; |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 154 | // Plus return value spill area size |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 155 | return RoundUp(frame_data_size + sirt_size + SizeOfReturnValue(), kStackAlignment); |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 158 | size_t ArmJniCallingConvention::OutArgSize() { |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 159 | return RoundUp(NumberOfOutgoingStackArgs() * kPointerSize + padding_, |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 160 | kStackAlignment); |
| 161 | } |
| 162 | |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 163 | // Will reg be crushed by an outgoing argument? |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 164 | bool ArmJniCallingConvention::IsMethodRegisterClobberedPreCall() { |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 165 | return true; // The method register R0 is always clobbered by the JNIEnv |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 168 | // JniCallingConvention ABI follows AAPCS where longs and doubles must occur |
| 169 | // in even register numbers and stack slots |
| 170 | void ArmJniCallingConvention::Next() { |
| 171 | JniCallingConvention::Next(); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 172 | size_t arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 173 | if ((itr_args_ >= 2) && |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 174 | (arg_pos < NumArgs()) && |
| 175 | IsParamALongOrDouble(arg_pos)) { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 176 | // itr_slots_ needs to be an even number, according to AAPCS. |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 177 | if ((itr_slots_ & 0x1u) != 0) { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 178 | itr_slots_++; |
| 179 | } |
| 180 | } |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 181 | } |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 182 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 183 | bool ArmJniCallingConvention::IsCurrentParamInRegister() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 184 | return itr_slots_ < 4; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 187 | bool ArmJniCallingConvention::IsCurrentParamOnStack() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 188 | return !IsCurrentParamInRegister(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static const Register kJniArgumentRegisters[] = { |
| 192 | R0, R1, R2, R3 |
| 193 | }; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 194 | ManagedRegister ArmJniCallingConvention::CurrentParamRegister() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 195 | CHECK_LT(itr_slots_, 4u); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 196 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); |
| 197 | if ((itr_args_ >= 2) && IsParamALongOrDouble(arg_pos)) { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 198 | CHECK_EQ(itr_slots_, 2u); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 199 | return ArmManagedRegister::FromRegisterPair(R2_R3); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 200 | } else { |
| 201 | return |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 202 | ArmManagedRegister::FromCoreRegister(kJniArgumentRegisters[itr_slots_]); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 203 | } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 206 | FrameOffset ArmJniCallingConvention::CurrentParamStackOffset() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 207 | CHECK_GE(itr_slots_, 4u); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 208 | return FrameOffset(displacement_.Int32Value() - OutArgSize() |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 209 | + ((itr_slots_ - 4) * kPointerSize)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 212 | size_t ArmJniCallingConvention::NumberOfOutgoingStackArgs() { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 213 | size_t static_args = IsStatic() ? 1 : 0; // count jclass |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 214 | // regular argument parameters and this |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 215 | size_t param_args = NumArgs() + NumLongOrDoubleArgs(); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 216 | // count JNIEnv* less arguments in registers |
| 217 | return static_args + param_args + 1 - 4; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 220 | } // namespace arm |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 221 | } // namespace art |