xref: /aosp_15_r20/art/runtime/arch/arm64/jni_frame_arm64.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2020 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 #ifndef ART_RUNTIME_ARCH_ARM64_JNI_FRAME_ARM64_H_
18 #define ART_RUNTIME_ARCH_ARM64_JNI_FRAME_ARM64_H_
19 
20 #include <string.h>
21 
22 #include "arch/instruction_set.h"
23 #include "base/bit_utils.h"
24 #include "base/globals.h"
25 #include "base/logging.h"
26 #include "base/macros.h"
27 
28 namespace art HIDDEN {
29 namespace arm64 {
30 
31 constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k64);
32 static_assert(kArm64PointerSize == PointerSize::k64, "Unexpected ARM64 pointer size");
33 
34 // The AAPCS64 requires 16-byte alignment. This is the same as the Managed ABI stack alignment.
35 static constexpr size_t kAapcs64StackAlignment = 16u;
36 static_assert(kAapcs64StackAlignment == kStackAlignment);
37 
38 // Up to how many float-like (float, double) args can be in registers.
39 // The rest of the args must go on the stack.
40 constexpr size_t kMaxFloatOrDoubleRegisterArguments = 8u;
41 // Up to how many integer-like (pointers, objects, longs, int, short, bool, etc) args can be
42 // in registers. The rest of the args must go on the stack.
43 constexpr size_t kMaxIntLikeRegisterArguments = 8u;
44 
45 // Get the size of the arguments for a native call.
GetNativeOutArgsSize(size_t num_fp_args,size_t num_non_fp_args)46 inline size_t GetNativeOutArgsSize(size_t num_fp_args, size_t num_non_fp_args) {
47   // Account for FP arguments passed through v0-v7.
48   size_t num_stack_fp_args =
49       num_fp_args - std::min(kMaxFloatOrDoubleRegisterArguments, num_fp_args);
50   // Account for other (integer and pointer) arguments passed through GPR (x0-x7).
51   size_t num_stack_non_fp_args =
52       num_non_fp_args - std::min(kMaxIntLikeRegisterArguments, num_non_fp_args);
53   // Each stack argument takes 8 bytes.
54   return (num_stack_fp_args + num_stack_non_fp_args) * static_cast<size_t>(kArm64PointerSize);
55 }
56 
57 // Get stack args size for @CriticalNative method calls.
GetCriticalNativeCallArgsSize(std::string_view shorty)58 inline size_t GetCriticalNativeCallArgsSize(std::string_view shorty) {
59   size_t num_fp_args =
60       std::count_if(shorty.begin() + 1, shorty.end(), [](char c) { return c == 'F' || c == 'D'; });
61   size_t num_non_fp_args = shorty.length() - 1u - num_fp_args;
62 
63   return GetNativeOutArgsSize(num_fp_args, num_non_fp_args);
64 }
65 
66 // Get the frame size for @CriticalNative method stub.
67 // This must match the size of the extra frame emitted by the compiler at the native call site.
GetCriticalNativeStubFrameSize(std::string_view shorty)68 inline size_t GetCriticalNativeStubFrameSize(std::string_view shorty) {
69   // The size of outgoing arguments.
70   size_t size = GetCriticalNativeCallArgsSize(shorty);
71 
72   // We can make a tail call if there are no stack args and we do not need
73   // to extend the result. Otherwise, add space for return PC.
74   if (size != 0u || shorty[0] == 'B' || shorty[0] == 'C' || shorty[0] == 'S' || shorty[0] == 'Z') {
75     size += kFramePointerSize;  // We need to spill LR with the args.
76   }
77   return RoundUp(size, kAapcs64StackAlignment);
78 }
79 
80 // Get the frame size for direct call to a @CriticalNative method.
81 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeDirectCallFrameSize(std::string_view shorty)82 inline size_t GetCriticalNativeDirectCallFrameSize(std::string_view shorty) {
83   // The size of outgoing arguments.
84   size_t size = GetCriticalNativeCallArgsSize(shorty);
85 
86   // No return PC to save, zero- and sign-extension are handled by the caller.
87   return RoundUp(size, kAapcs64StackAlignment);
88 }
89 
90 }  // namespace arm64
91 }  // namespace art
92 
93 #endif  // ART_RUNTIME_ARCH_ARM64_JNI_FRAME_ARM64_H_
94