1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_ARCH_ARM64_JNI_FRAME_ARM64_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_ARCH_ARM64_JNI_FRAME_ARM64_H_
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include <string.h>
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
24*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
27*795d594fSAndroid Build Coastguard Worker
28*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
29*795d594fSAndroid Build Coastguard Worker namespace arm64 {
30*795d594fSAndroid Build Coastguard Worker
31*795d594fSAndroid Build Coastguard Worker constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k64);
32*795d594fSAndroid Build Coastguard Worker static_assert(kArm64PointerSize == PointerSize::k64, "Unexpected ARM64 pointer size");
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker // The AAPCS64 requires 16-byte alignment. This is the same as the Managed ABI stack alignment.
35*795d594fSAndroid Build Coastguard Worker static constexpr size_t kAapcs64StackAlignment = 16u;
36*795d594fSAndroid Build Coastguard Worker static_assert(kAapcs64StackAlignment == kStackAlignment);
37*795d594fSAndroid Build Coastguard Worker
38*795d594fSAndroid Build Coastguard Worker // Up to how many float-like (float, double) args can be in registers.
39*795d594fSAndroid Build Coastguard Worker // The rest of the args must go on the stack.
40*795d594fSAndroid Build Coastguard Worker constexpr size_t kMaxFloatOrDoubleRegisterArguments = 8u;
41*795d594fSAndroid Build Coastguard Worker // Up to how many integer-like (pointers, objects, longs, int, short, bool, etc) args can be
42*795d594fSAndroid Build Coastguard Worker // in registers. The rest of the args must go on the stack.
43*795d594fSAndroid Build Coastguard Worker constexpr size_t kMaxIntLikeRegisterArguments = 8u;
44*795d594fSAndroid Build Coastguard Worker
45*795d594fSAndroid Build Coastguard Worker // Get the size of the arguments for a native call.
GetNativeOutArgsSize(size_t num_fp_args,size_t num_non_fp_args)46*795d594fSAndroid Build Coastguard Worker inline size_t GetNativeOutArgsSize(size_t num_fp_args, size_t num_non_fp_args) {
47*795d594fSAndroid Build Coastguard Worker // Account for FP arguments passed through v0-v7.
48*795d594fSAndroid Build Coastguard Worker size_t num_stack_fp_args =
49*795d594fSAndroid Build Coastguard Worker num_fp_args - std::min(kMaxFloatOrDoubleRegisterArguments, num_fp_args);
50*795d594fSAndroid Build Coastguard Worker // Account for other (integer and pointer) arguments passed through GPR (x0-x7).
51*795d594fSAndroid Build Coastguard Worker size_t num_stack_non_fp_args =
52*795d594fSAndroid Build Coastguard Worker num_non_fp_args - std::min(kMaxIntLikeRegisterArguments, num_non_fp_args);
53*795d594fSAndroid Build Coastguard Worker // Each stack argument takes 8 bytes.
54*795d594fSAndroid Build Coastguard Worker return (num_stack_fp_args + num_stack_non_fp_args) * static_cast<size_t>(kArm64PointerSize);
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker
57*795d594fSAndroid Build Coastguard Worker // Get stack args size for @CriticalNative method calls.
GetCriticalNativeCallArgsSize(std::string_view shorty)58*795d594fSAndroid Build Coastguard Worker inline size_t GetCriticalNativeCallArgsSize(std::string_view shorty) {
59*795d594fSAndroid Build Coastguard Worker size_t num_fp_args =
60*795d594fSAndroid Build Coastguard Worker std::count_if(shorty.begin() + 1, shorty.end(), [](char c) { return c == 'F' || c == 'D'; });
61*795d594fSAndroid Build Coastguard Worker size_t num_non_fp_args = shorty.length() - 1u - num_fp_args;
62*795d594fSAndroid Build Coastguard Worker
63*795d594fSAndroid Build Coastguard Worker return GetNativeOutArgsSize(num_fp_args, num_non_fp_args);
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker // Get the frame size for @CriticalNative method stub.
67*795d594fSAndroid Build Coastguard Worker // This must match the size of the extra frame emitted by the compiler at the native call site.
GetCriticalNativeStubFrameSize(std::string_view shorty)68*795d594fSAndroid Build Coastguard Worker inline size_t GetCriticalNativeStubFrameSize(std::string_view shorty) {
69*795d594fSAndroid Build Coastguard Worker // The size of outgoing arguments.
70*795d594fSAndroid Build Coastguard Worker size_t size = GetCriticalNativeCallArgsSize(shorty);
71*795d594fSAndroid Build Coastguard Worker
72*795d594fSAndroid Build Coastguard Worker // We can make a tail call if there are no stack args and we do not need
73*795d594fSAndroid Build Coastguard Worker // to extend the result. Otherwise, add space for return PC.
74*795d594fSAndroid Build Coastguard Worker if (size != 0u || shorty[0] == 'B' || shorty[0] == 'C' || shorty[0] == 'S' || shorty[0] == 'Z') {
75*795d594fSAndroid Build Coastguard Worker size += kFramePointerSize; // We need to spill LR with the args.
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker return RoundUp(size, kAapcs64StackAlignment);
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker
80*795d594fSAndroid Build Coastguard Worker // Get the frame size for direct call to a @CriticalNative method.
81*795d594fSAndroid Build Coastguard Worker // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeDirectCallFrameSize(std::string_view shorty)82*795d594fSAndroid Build Coastguard Worker inline size_t GetCriticalNativeDirectCallFrameSize(std::string_view shorty) {
83*795d594fSAndroid Build Coastguard Worker // The size of outgoing arguments.
84*795d594fSAndroid Build Coastguard Worker size_t size = GetCriticalNativeCallArgsSize(shorty);
85*795d594fSAndroid Build Coastguard Worker
86*795d594fSAndroid Build Coastguard Worker // No return PC to save, zero- and sign-extension are handled by the caller.
87*795d594fSAndroid Build Coastguard Worker return RoundUp(size, kAapcs64StackAlignment);
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker
90*795d594fSAndroid Build Coastguard Worker } // namespace arm64
91*795d594fSAndroid Build Coastguard Worker } // namespace art
92*795d594fSAndroid Build Coastguard Worker
93*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_ARCH_ARM64_JNI_FRAME_ARM64_H_
94