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