Home | History | Annotate | Download | only in arm
      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                   kPointerSize +                 // Method*
     84                   (itr_slots_ * kPointerSize));  // offset into in args
     85   return result;
     86 }
     87 
     88 const std::vector<ManagedRegister>& 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) {
    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 = (3 + CalleeSaveRegisters().size()) * kPointerSize;
    147   // References plus 2 words for SIRT header
    148   size_t sirt_size = (ReferenceCount() + 2) * kPointerSize;
    149   // Plus return value spill area size
    150   return RoundUp(frame_data_size + sirt_size + SizeOfReturnValue(), kStackAlignment);
    151 }
    152 
    153 size_t ArmJniCallingConvention::OutArgSize() {
    154   return RoundUp(NumberOfOutgoingStackArgs() * kPointerSize + padding_,
    155                  kStackAlignment);
    156 }
    157 
    158 // JniCallingConvention ABI follows AAPCS where longs and doubles must occur
    159 // in even register numbers and stack slots
    160 void ArmJniCallingConvention::Next() {
    161   JniCallingConvention::Next();
    162   size_t arg_pos = itr_args_ - NumberOfExtraArgumentsForJni();
    163   if ((itr_args_ >= 2) &&
    164       (arg_pos < NumArgs()) &&
    165       IsParamALongOrDouble(arg_pos)) {
    166     // itr_slots_ needs to be an even number, according to AAPCS.
    167     if ((itr_slots_ & 0x1u) != 0) {
    168       itr_slots_++;
    169     }
    170   }
    171 }
    172 
    173 bool ArmJniCallingConvention::IsCurrentParamInRegister() {
    174   return itr_slots_ < 4;
    175 }
    176 
    177 bool ArmJniCallingConvention::IsCurrentParamOnStack() {
    178   return !IsCurrentParamInRegister();
    179 }
    180 
    181 static const Register kJniArgumentRegisters[] = {
    182   R0, R1, R2, R3
    183 };
    184 ManagedRegister ArmJniCallingConvention::CurrentParamRegister() {
    185   CHECK_LT(itr_slots_, 4u);
    186   int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni();
    187   if ((itr_args_ >= 2) && IsParamALongOrDouble(arg_pos)) {
    188     CHECK_EQ(itr_slots_, 2u);
    189     return ArmManagedRegister::FromRegisterPair(R2_R3);
    190   } else {
    191     return
    192       ArmManagedRegister::FromCoreRegister(kJniArgumentRegisters[itr_slots_]);
    193   }
    194 }
    195 
    196 FrameOffset ArmJniCallingConvention::CurrentParamStackOffset() {
    197   CHECK_GE(itr_slots_, 4u);
    198   size_t offset = displacement_.Int32Value() - OutArgSize() + ((itr_slots_ - 4) * kPointerSize);
    199   CHECK_LT(offset, OutArgSize());
    200   return FrameOffset(offset);
    201 }
    202 
    203 size_t ArmJniCallingConvention::NumberOfOutgoingStackArgs() {
    204   size_t static_args = IsStatic() ? 1 : 0;  // count jclass
    205   // regular argument parameters and this
    206   size_t param_args = NumArgs() + NumLongOrDoubleArgs();
    207   // count JNIEnv* less arguments in registers
    208   return static_args + param_args + 1 - 4;
    209 }
    210 
    211 }  // namespace arm
    212 }  // namespace art
    213