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 "base/logging.h" 18 #include "calling_convention_arm.h" 19 #include "utils/arm/managed_register_arm.h" 20 21 namespace art { 22 namespace arm { 23 24 // Calling convention 25 26 ManagedRegister ArmManagedRuntimeCallingConvention::InterproceduralScratchRegister() { 27 return ArmManagedRegister::FromCoreRegister(IP); // R12 28 } 29 30 ManagedRegister ArmJniCallingConvention::InterproceduralScratchRegister() { 31 return ArmManagedRegister::FromCoreRegister(IP); // R12 32 } 33 34 static ManagedRegister ReturnRegisterForShorty(const char* shorty) { 35 if (shorty[0] == 'F') { 36 return ArmManagedRegister::FromCoreRegister(R0); 37 } else if (shorty[0] == 'D') { 38 return ArmManagedRegister::FromRegisterPair(R0_R1); 39 } else if (shorty[0] == 'J') { 40 return ArmManagedRegister::FromRegisterPair(R0_R1); 41 } else if (shorty[0] == 'V') { 42 return ArmManagedRegister::NoRegister(); 43 } else { 44 return ArmManagedRegister::FromCoreRegister(R0); 45 } 46 } 47 48 ManagedRegister ArmManagedRuntimeCallingConvention::ReturnRegister() { 49 return ReturnRegisterForShorty(GetShorty()); 50 } 51 52 ManagedRegister ArmJniCallingConvention::ReturnRegister() { 53 return ReturnRegisterForShorty(GetShorty()); 54 } 55 56 ManagedRegister ArmJniCallingConvention::IntReturnRegister() { 57 return ArmManagedRegister::FromCoreRegister(R0); 58 } 59 60 // Managed runtime calling convention 61 62 ManagedRegister ArmManagedRuntimeCallingConvention::MethodRegister() { 63 return ArmManagedRegister::FromCoreRegister(R0); 64 } 65 66 bool ArmManagedRuntimeCallingConvention::IsCurrentParamInRegister() { 67 return false; // Everything moved to stack on entry. 68 } 69 70 bool ArmManagedRuntimeCallingConvention::IsCurrentParamOnStack() { 71 return true; 72 } 73 74 ManagedRegister ArmManagedRuntimeCallingConvention::CurrentParamRegister() { 75 LOG(FATAL) << "Should not reach here"; 76 return ManagedRegister::NoRegister(); 77 } 78 79 FrameOffset ArmManagedRuntimeCallingConvention::CurrentParamStackOffset() { 80 CHECK(IsCurrentParamOnStack()); 81 FrameOffset result = 82 FrameOffset(displacement_.Int32Value() + // displacement 83 kFramePointerSize + // Method* 84 (itr_slots_ * kFramePointerSize)); // offset into in args 85 return result; 86 } 87 88 const ManagedRegisterEntrySpills& ArmManagedRuntimeCallingConvention::EntrySpills() { 89 // We spill the argument registers on ARM to free them up for scratch use, we then assume 90 // all arguments are on the stack. 91 if (entry_spills_.size() == 0) { 92 size_t num_spills = NumArgs() + NumLongOrDoubleArgs(); 93 if (num_spills > 0) { 94 entry_spills_.push_back(ArmManagedRegister::FromCoreRegister(R1)); 95 if (num_spills > 1) { 96 entry_spills_.push_back(ArmManagedRegister::FromCoreRegister(R2)); 97 if (num_spills > 2) { 98 entry_spills_.push_back(ArmManagedRegister::FromCoreRegister(R3)); 99 } 100 } 101 } 102 } 103 return entry_spills_; 104 } 105 // JNI calling convention 106 107 ArmJniCallingConvention::ArmJniCallingConvention(bool is_static, bool is_synchronized, 108 const char* shorty) 109 : JniCallingConvention(is_static, is_synchronized, shorty, kFramePointerSize) { 110 // Compute padding to ensure longs and doubles are not split in AAPCS. Ignore the 'this' jobject 111 // or jclass for static methods and the JNIEnv. We start at the aligned register r2. 112 size_t padding = 0; 113 for (size_t cur_arg = IsStatic() ? 0 : 1, cur_reg = 2; cur_arg < NumArgs(); cur_arg++) { 114 if (IsParamALongOrDouble(cur_arg)) { 115 if ((cur_reg & 1) != 0) { 116 padding += 4; 117 cur_reg++; // additional bump to ensure alignment 118 } 119 cur_reg++; // additional bump to skip extra long word 120 } 121 cur_reg++; // bump the iterator for every argument 122 } 123 padding_ = padding; 124 125 callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R5)); 126 callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R6)); 127 callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R7)); 128 callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R8)); 129 callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R10)); 130 callee_save_regs_.push_back(ArmManagedRegister::FromCoreRegister(R11)); 131 } 132 133 uint32_t ArmJniCallingConvention::CoreSpillMask() const { 134 // Compute spill mask to agree with callee saves initialized in the constructor 135 uint32_t result = 0; 136 result = 1 << R5 | 1 << R6 | 1 << R7 | 1 << R8 | 1 << R10 | 1 << R11 | 1 << LR; 137 return result; 138 } 139 140 ManagedRegister ArmJniCallingConvention::ReturnScratchRegister() const { 141 return ArmManagedRegister::FromCoreRegister(R2); 142 } 143 144 size_t ArmJniCallingConvention::FrameSize() { 145 // Method*, LR and callee save area size, local reference segment state 146 size_t frame_data_size = sizeof(StackReference<mirror::ArtMethod>) + 147 (2 + CalleeSaveRegisters().size()) * kFramePointerSize; 148 // References plus 2 words for HandleScope header 149 size_t handle_scope_size = HandleScope::SizeOf(kFramePointerSize, ReferenceCount()); 150 // Plus return value spill area size 151 return RoundUp(frame_data_size + handle_scope_size + SizeOfReturnValue(), kStackAlignment); 152 } 153 154 size_t ArmJniCallingConvention::OutArgSize() { 155 return RoundUp(NumberOfOutgoingStackArgs() * kFramePointerSize + padding_, 156 kStackAlignment); 157 } 158 159 // JniCallingConvention ABI follows AAPCS where longs and doubles must occur 160 // in even register numbers and stack slots 161 void ArmJniCallingConvention::Next() { 162 JniCallingConvention::Next(); 163 size_t arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); 164 if ((itr_args_ >= 2) && 165 (arg_pos < NumArgs()) && 166 IsParamALongOrDouble(arg_pos)) { 167 // itr_slots_ needs to be an even number, according to AAPCS. 168 if ((itr_slots_ & 0x1u) != 0) { 169 itr_slots_++; 170 } 171 } 172 } 173 174 bool ArmJniCallingConvention::IsCurrentParamInRegister() { 175 return itr_slots_ < 4; 176 } 177 178 bool ArmJniCallingConvention::IsCurrentParamOnStack() { 179 return !IsCurrentParamInRegister(); 180 } 181 182 static const Register kJniArgumentRegisters[] = { 183 R0, R1, R2, R3 184 }; 185 ManagedRegister ArmJniCallingConvention::CurrentParamRegister() { 186 CHECK_LT(itr_slots_, 4u); 187 int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(); 188 if ((itr_args_ >= 2) && IsParamALongOrDouble(arg_pos)) { 189 CHECK_EQ(itr_slots_, 2u); 190 return ArmManagedRegister::FromRegisterPair(R2_R3); 191 } else { 192 return 193 ArmManagedRegister::FromCoreRegister(kJniArgumentRegisters[itr_slots_]); 194 } 195 } 196 197 FrameOffset ArmJniCallingConvention::CurrentParamStackOffset() { 198 CHECK_GE(itr_slots_, 4u); 199 size_t offset = displacement_.Int32Value() - OutArgSize() + ((itr_slots_ - 4) * kFramePointerSize); 200 CHECK_LT(offset, OutArgSize()); 201 return FrameOffset(offset); 202 } 203 204 size_t ArmJniCallingConvention::NumberOfOutgoingStackArgs() { 205 size_t static_args = IsStatic() ? 1 : 0; // count jclass 206 // regular argument parameters and this 207 size_t param_args = NumArgs() + NumLongOrDoubleArgs(); 208 // count JNIEnv* less arguments in registers 209 return static_args + param_args + 1 - 4; 210 } 211 212 } // namespace arm 213 } // namespace art 214