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