/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_RUNTIME_ARCH_ARM_JNI_FRAME_ARM_H_
#define ART_RUNTIME_ARCH_ARM_JNI_FRAME_ARM_H_

#include <string.h>

#include "arch/instruction_set.h"
#include "base/bit_utils.h"
#include "base/globals.h"
#include "base/logging.h"
#include "base/macros.h"

namespace art HIDDEN {
namespace arm {

constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k32);
static_assert(kArmPointerSize == PointerSize::k32, "Unexpected ARM pointer size");

// The AAPCS requires 8-byte alignment. This is not as strict as the Managed ABI stack alignment.
static constexpr size_t kAapcsStackAlignment = 8u;
static_assert(kAapcsStackAlignment < kStackAlignment);

// How many registers can be used for passing arguments.
// Note: AAPCS is soft-float, so these are all core registers.
constexpr size_t kJniArgumentRegisterCount = 4u;

// Get stack args size for @CriticalNative method calls.
inline size_t GetCriticalNativeCallArgsSize(const char* shorty, uint32_t shorty_len) {
  DCHECK_EQ(shorty_len, strlen(shorty));

  size_t reg = 0;  // Register for the current argument; if reg >= 4, we shall use stack.
  for (size_t i = 1; i != shorty_len; ++i) {
    if (shorty[i] == 'J' || shorty[i] == 'D') {
      // 8-byte args need to start in even-numbered register or at aligned stack position.
      reg += (reg & 1);
      // Count first word and let the common path count the second.
      reg += 1u;
    }
    reg += 1u;
  }
  size_t stack_args = std::max(reg, kJniArgumentRegisterCount) - kJniArgumentRegisterCount;
  return kFramePointerSize * stack_args;
}

// Get the frame size for @CriticalNative method stub.
// This must match the size of the frame emitted by the JNI compiler at the native call site.
inline size_t GetCriticalNativeStubFrameSize(const char* shorty, uint32_t shorty_len) {
  // The size of outgoing arguments.
  size_t size = GetCriticalNativeCallArgsSize(shorty, shorty_len);

  // Check if this is a tail call, i.e. there are no stack args and the return type
  // is not  an FP type (otherwise we need to move the result to FP register).
  // No need to sign/zero extend small return types thanks to AAPCS.
  if (size != 0u || shorty[0] == 'F' || shorty[0] == 'D') {
    size += kFramePointerSize;  // We need to spill LR with the args.
  }
  return RoundUp(size, kAapcsStackAlignment);
}

// Get the frame size for direct call to a @CriticalNative method.
// This must match the size of the extra frame emitted by the compiler at the native call site.
inline size_t GetCriticalNativeDirectCallFrameSize(const char* shorty, uint32_t shorty_len) {
  // The size of outgoing arguments.
  size_t size = GetCriticalNativeCallArgsSize(shorty, shorty_len);

  // No return PC to save, zero- and sign-extension and FP value moves are handled by the caller.
  return RoundUp(size, kAapcsStackAlignment);
}

}  // namespace arm
}  // namespace art

#endif  // ART_RUNTIME_ARCH_ARM_JNI_FRAME_ARM_H_
