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 | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 102 | ArmJniCallingConvention::ArmJniCallingConvention(Method* method) : |
| 103 | JniCallingConvention(method) { |
| 104 | // A synchronized method will call monitor enter clobbering R1, R2 and R3 |
| 105 | // unless they are spilled. |
| 106 | if (method->IsSynchronized()) { |
| 107 | spill_regs_.push_back(ArmManagedRegister::FromCoreRegister(R1)); |
| 108 | spill_regs_.push_back(ArmManagedRegister::FromCoreRegister(R2)); |
| 109 | spill_regs_.push_back(ArmManagedRegister::FromCoreRegister(R3)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | size_t ArmJniCallingConvention::FrameSize() { |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 114 | // Method* and spill area size |
| 115 | size_t frame_data_size = kPointerSize + SpillAreaSize(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 116 | // References plus 2 words for SIRT header |
| 117 | size_t sirt_size = (ReferenceCount() + 2) * kPointerSize; |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 118 | // Plus return value spill area size |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 119 | return RoundUp(frame_data_size + sirt_size + SizeOfReturnValue(), |
| 120 | kStackAlignment); |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 123 | size_t ArmJniCallingConvention::OutArgSize() { |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 124 | const Method* method = GetMethod(); |
| 125 | size_t padding; // padding to ensure longs and doubles are not split in AAPCS |
| 126 | if (method->IsStatic()) { |
| 127 | padding = (method->NumArgs() > 1) && !method->IsParamALongOrDouble(0) && |
| 128 | method->IsParamALongOrDouble(1) ? 4 : 0; |
| 129 | } else { |
| 130 | padding = (method->NumArgs() > 2) && !method->IsParamALongOrDouble(1) && |
| 131 | method->IsParamALongOrDouble(2) ? 4 : 0; |
| 132 | } |
| 133 | return RoundUp(NumberOfOutgoingStackArgs() * kPointerSize + padding, |
| 134 | kStackAlignment); |
| 135 | } |
| 136 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 137 | size_t ArmJniCallingConvention::ReturnPcOffset() { |
Ian Rogers | 762400c | 2011-08-23 12:14:16 -0700 | [diff] [blame] | 138 | // Link register is always the last value spilled, skip forward one word for |
| 139 | // the Method* then skip back one word to get the link register (ie +0) |
| 140 | return SpillAreaSize(); |
| 141 | } |
| 142 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 143 | size_t ArmJniCallingConvention::SpillAreaSize() { |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 144 | // Space for link register. For synchronized methods we need enough space to |
| 145 | // save R1, R2 and R3 (R0 is the method register and always preserved) |
| 146 | return GetMethod()->IsSynchronized() ? (4 * kPointerSize) : kPointerSize; |
| 147 | } |
| 148 | |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 149 | // Will reg be crushed by an outgoing argument? |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 150 | bool ArmJniCallingConvention::IsOutArgRegister(ManagedRegister mreg) { |
| 151 | Register reg = mreg.AsArm().AsCoreRegister(); |
| 152 | return reg >= R0 && reg <= R3; |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 155 | // JniCallingConvention ABI follows AAPCS |
| 156 | // |
| 157 | // In processing each parameter, we know that IsCurrentParamInRegister() |
| 158 | // or IsCurrentParamOnStack() will be called first. |
| 159 | // Both functions will ensure that we conform to AAPCS. |
| 160 | // |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 161 | bool ArmJniCallingConvention::IsCurrentParamInRegister() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 162 | // AAPCS processing |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 163 | Method* method = GetMethod(); |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 164 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(method); |
| 165 | if ((itr_args_ >= 2) && method->IsParamALongOrDouble(arg_pos)) { |
| 166 | // itr_slots_ needs to be an even number, according to AAPCS. |
| 167 | if (itr_slots_ & 0x1u) { |
| 168 | itr_slots_++; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return itr_slots_ < 4; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 175 | bool ArmJniCallingConvention::IsCurrentParamOnStack() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 176 | return !IsCurrentParamInRegister(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static const Register kJniArgumentRegisters[] = { |
| 180 | R0, R1, R2, R3 |
| 181 | }; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 182 | ManagedRegister ArmJniCallingConvention::CurrentParamRegister() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 183 | CHECK_LT(itr_slots_, 4u); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 184 | Method* method = GetMethod(); |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 185 | int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(method); |
| 186 | if ((itr_args_ >= 2) && method->IsParamALongOrDouble(arg_pos)) { |
| 187 | CHECK_EQ(itr_slots_, 2u); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 188 | return ArmManagedRegister::FromRegisterPair(R2_R3); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 189 | } else { |
| 190 | return |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 191 | ArmManagedRegister::FromCoreRegister(kJniArgumentRegisters[itr_slots_]); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 192 | } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 195 | FrameOffset ArmJniCallingConvention::CurrentParamStackOffset() { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 196 | CHECK_GE(itr_slots_, 4u); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 197 | return FrameOffset(displacement_.Int32Value() - OutArgSize() |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 198 | + ((itr_slots_ - 4) * kPointerSize)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 201 | size_t ArmJniCallingConvention::NumberOfOutgoingStackArgs() { |
| 202 | Method* method = GetMethod(); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 203 | size_t static_args = method->IsStatic() ? 1 : 0; // count jclass |
| 204 | // regular argument parameters and this |
| 205 | size_t param_args = method->NumArgs() + |
| 206 | method->NumLongOrDoubleArgs(); |
| 207 | // count JNIEnv* less arguments in registers |
| 208 | return static_args + param_args + 1 - 4; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame^] | 211 | } // namespace arm |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 212 | } // namespace art |