1 /*
2  * Copyright (C) 2023 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_RISCV64_JNI_FRAME_RISCV64_H_
18 #define ART_RUNTIME_ARCH_RISCV64_JNI_FRAME_RISCV64_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 riscv64 {
30 
31 constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k64);
32 static_assert(kRiscv64PointerSize == PointerSize::k64, "Unexpected RISCV64 pointer size");
33 
34 // The RISCV64 requires 16-byte alignment. This is the same as the Managed ABI stack alignment.
35 static constexpr size_t kNativeStackAlignment = 16u;
36 static_assert(kNativeStackAlignment == kStackAlignment);
37 
38 // Up to how many float-like (float, double) args can be in FP registers.
39 // The rest of the args must go to general purpose registers (native ABI only) or on the stack.
40 constexpr size_t kMaxFloatOrDoubleArgumentRegisters = 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. Note that even FP args can use these
43 // registers in native ABI after using all FP arg registers. We do not pass FP args in registers in
44 // managed ABI to avoid some complexity in the compiler - more than 8 FP args are quite rare anyway.
45 constexpr size_t kMaxIntLikeArgumentRegisters = 8u;
46 
47 // Get the size of the arguments for a native call.
GetNativeOutArgsSize(size_t num_fp_args,size_t num_non_fp_args)48 inline size_t GetNativeOutArgsSize(size_t num_fp_args, size_t num_non_fp_args) {
49   // Account for FP arguments passed through FA0-FA7.
50   size_t num_fp_args_without_fprs =
51       num_fp_args - std::min(kMaxFloatOrDoubleArgumentRegisters, num_fp_args);
52   // All other args are passed through A0-A7 (even FP args) and the stack.
53   size_t num_gpr_and_stack_args = num_non_fp_args + num_fp_args_without_fprs;
54   size_t num_stack_args =
55       num_gpr_and_stack_args - std::min(kMaxIntLikeArgumentRegisters, num_gpr_and_stack_args);
56   // Each stack argument takes 8 bytes.
57   return num_stack_args * static_cast<size_t>(kRiscv64PointerSize);
58 }
59 
60 // Get stack args size for @CriticalNative method calls.
GetCriticalNativeCallArgsSize(std::string_view shorty)61 inline size_t GetCriticalNativeCallArgsSize(std::string_view shorty) {
62   size_t num_fp_args =
63       std::count_if(shorty.begin() + 1, shorty.end(), [](char c) { return c == 'F' || c == 'D'; });
64   size_t num_non_fp_args = shorty.length() - 1u - num_fp_args;
65 
66   return GetNativeOutArgsSize(num_fp_args, num_non_fp_args);
67 }
68 
69 // Get the frame size for @CriticalNative method stub.
70 // This must match the size of the extra frame emitted by the compiler at the native call site.
GetCriticalNativeStubFrameSize(std::string_view shorty)71 inline size_t GetCriticalNativeStubFrameSize(std::string_view shorty) {
72   // The size of outgoing arguments.
73   size_t size = GetCriticalNativeCallArgsSize(shorty);
74 
75   // We can make a tail call if there are no stack args. Otherwise, add space for return PC.
76   // Note: Result does not neeed to be zero- or sign-extended.
77   if (size != 0u) {
78     size += kFramePointerSize;  // We need to spill RA with the args.
79   }
80   return RoundUp(size, kNativeStackAlignment);
81 }
82 
83 // Get the frame size for direct call to a @CriticalNative method.
84 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeDirectCallFrameSize(std::string_view shorty)85 inline size_t GetCriticalNativeDirectCallFrameSize(std::string_view shorty) {
86   // The size of outgoing arguments.
87   size_t size = GetCriticalNativeCallArgsSize(shorty);
88 
89   // No return PC to save.
90   return RoundUp(size, kNativeStackAlignment);
91 }
92 
93 }  // namespace riscv64
94 }  // namespace art
95 
96 #endif  // ART_RUNTIME_ARCH_RISCV64_JNI_FRAME_RISCV64_H_
97