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_CALLEE_SAVE_FRAME_RISCV64_H_ 18 #define ART_RUNTIME_ARCH_RISCV64_CALLEE_SAVE_FRAME_RISCV64_H_ 19 20 #include "arch/instruction_set.h" 21 #include "base/bit_utils.h" 22 #include "base/callee_save_type.h" 23 #include "base/macros.h" 24 #include "base/pointer_size.h" 25 #include "quick/quick_method_frame_info.h" 26 #include "registers_riscv64.h" 27 #include "runtime_globals.h" 28 29 namespace art HIDDEN { 30 namespace riscv64 { 31 32 static constexpr uint32_t kRiscv64CalleeSaveAlwaysSpills = 33 (1 << art::riscv64::RA); // Return address 34 // Callee-saved registers except for SP and S1 (SP is callee-saved according to RISC-V spec, but 35 // it cannot contain object reference, and S1(TR) is excluded as the ART thread register). 36 static constexpr uint32_t kRiscv64CalleeSaveRefSpills = 37 (1 << art::riscv64::S0) | (1 << art::riscv64::S2) | (1 << art::riscv64::S3) | 38 (1 << art::riscv64::S4) | (1 << art::riscv64::S5) | (1 << art::riscv64::S6) | 39 (1 << art::riscv64::S7) | (1 << art::riscv64::S8) | (1 << art::riscv64::S9) | 40 (1 << art::riscv64::S10) | (1 << art::riscv64::S11); 41 // Stack pointer SP is excluded (although it is callee-saved by calling convention) because it is 42 // restored by the code logic and not from a stack frame. 43 static constexpr uint32_t kRiscv64CalleeSaveAllSpills = 0; 44 // Argument registers except X10/A0 (which contains method pointer). 45 static constexpr uint32_t kRiscv64CalleeSaveArgSpills = 46 (1 << art::riscv64::A1) | (1 << art::riscv64::A2) | (1 << art::riscv64::A3) | 47 (1 << art::riscv64::A4) | (1 << art::riscv64::A5) | (1 << art::riscv64::A6) | 48 (1 << art::riscv64::A7); 49 // All registers except SP, immutable Zero, unallocatable TP and GP, and the ART thread register TR. 50 static constexpr uint32_t kRiscv64CalleeSaveEverythingSpills = 51 (1 << art::riscv64::T0) | (1 << art::riscv64::T1) | (1 << art::riscv64::T2) | 52 (1 << art::riscv64::T3) | (1 << art::riscv64::T4) | (1 << art::riscv64::T5) | 53 (1 << art::riscv64::T6) | (1 << art::riscv64::A0) | (1 << art::riscv64::A1) | 54 (1 << art::riscv64::A2) | (1 << art::riscv64::A3) | (1 << art::riscv64::A4) | 55 (1 << art::riscv64::A5) | (1 << art::riscv64::A6) | (1 << art::riscv64::A7); 56 57 // No references in floating-point registers. 58 static constexpr uint32_t kRiscv64CalleeSaveFpSpills = 0; 59 // Floating-point argument registers FA0 - FA7. 60 static constexpr uint32_t kRiscv64CalleeSaveFpArgSpills = 61 (1 << art::riscv64::FA0) | (1 << art::riscv64::FA1) | (1 << art::riscv64::FA2) | 62 (1 << art::riscv64::FA3) | (1 << art::riscv64::FA4) | (1 << art::riscv64::FA5) | 63 (1 << art::riscv64::FA6) | (1 << art::riscv64::FA7); 64 // Floating-point callee-saved registers FS0 - FS11. 65 static constexpr uint32_t kRiscv64CalleeSaveFpAllSpills = 66 (1 << art::riscv64::FS0) | (1 << art::riscv64::FS1) | (1 << art::riscv64::FS2) | 67 (1 << art::riscv64::FS3) | (1 << art::riscv64::FS4) | (1 << art::riscv64::FS5) | 68 (1 << art::riscv64::FS6) | (1 << art::riscv64::FS7) | (1 << art::riscv64::FS8) | 69 (1 << art::riscv64::FS9) | (1 << art::riscv64::FS10) | (1 << art::riscv64::FS11); 70 // All floating-point registers. 71 static constexpr uint32_t kRiscv64CalleeSaveFpEverythingSpills = 72 (1 << art::riscv64::FT0) | (1 << art::riscv64::FT1) | (1 << art::riscv64::FT2) | 73 (1 << art::riscv64::FT3) | (1 << art::riscv64::FT4) | (1 << art::riscv64::FT5) | 74 (1 << art::riscv64::FT6) | (1 << art::riscv64::FT7) | (1 << art::riscv64::FT8) | 75 (1 << art::riscv64::FT9) | (1 << art::riscv64::FT10) | (1 << art::riscv64::FT11) | 76 (1 << art::riscv64::FS0) | (1 << art::riscv64::FS1) | (1 << art::riscv64::FS2) | 77 (1 << art::riscv64::FS3) | (1 << art::riscv64::FS4) | (1 << art::riscv64::FS5) | 78 (1 << art::riscv64::FS6) | (1 << art::riscv64::FS7) | (1 << art::riscv64::FS8) | 79 (1 << art::riscv64::FS9) | (1 << art::riscv64::FS10) | (1 << art::riscv64::FS11) | 80 (1 << art::riscv64::FA0) | (1 << art::riscv64::FA1) | (1 << art::riscv64::FA2) | 81 (1 << art::riscv64::FA3) | (1 << art::riscv64::FA4) | (1 << art::riscv64::FA5) | 82 (1 << art::riscv64::FA6) | (1 << art::riscv64::FA7); 83 84 class Riscv64CalleeSaveFrame { 85 public: GetCoreSpills(CalleeSaveType type)86 static constexpr uint32_t GetCoreSpills(CalleeSaveType type) { 87 type = GetCanonicalCalleeSaveType(type); 88 return kRiscv64CalleeSaveAlwaysSpills | kRiscv64CalleeSaveRefSpills | 89 (type == CalleeSaveType::kSaveRefsAndArgs ? kRiscv64CalleeSaveArgSpills : 0) | 90 (type == CalleeSaveType::kSaveAllCalleeSaves ? kRiscv64CalleeSaveAllSpills : 0) | 91 (type == CalleeSaveType::kSaveEverything ? kRiscv64CalleeSaveEverythingSpills : 0); 92 } 93 GetFpSpills(CalleeSaveType type)94 static constexpr uint32_t GetFpSpills(CalleeSaveType type) { 95 type = GetCanonicalCalleeSaveType(type); 96 return kRiscv64CalleeSaveFpSpills | 97 (type == CalleeSaveType::kSaveRefsAndArgs ? kRiscv64CalleeSaveFpArgSpills : 0) | 98 (type == CalleeSaveType::kSaveAllCalleeSaves ? kRiscv64CalleeSaveFpAllSpills : 0) | 99 (type == CalleeSaveType::kSaveEverything ? kRiscv64CalleeSaveFpEverythingSpills : 0); 100 } 101 GetFrameSize(CalleeSaveType type)102 static constexpr uint32_t GetFrameSize(CalleeSaveType type) { 103 type = GetCanonicalCalleeSaveType(type); 104 return RoundUp((POPCOUNT(GetCoreSpills(type)) /* gprs */ + 105 POPCOUNT(GetFpSpills(type)) /* fprs */ + 1 /* Method* */) * 106 static_cast<size_t>(kRiscv64PointerSize), 107 kStackAlignment); 108 } 109 GetMethodFrameInfo(CalleeSaveType type)110 static constexpr QuickMethodFrameInfo GetMethodFrameInfo(CalleeSaveType type) { 111 type = GetCanonicalCalleeSaveType(type); 112 return QuickMethodFrameInfo(GetFrameSize(type), GetCoreSpills(type), GetFpSpills(type)); 113 } 114 GetFpr1Offset(CalleeSaveType type)115 static constexpr size_t GetFpr1Offset(CalleeSaveType type) { 116 type = GetCanonicalCalleeSaveType(type); 117 return GetFrameSize(type) - (POPCOUNT(GetCoreSpills(type)) + POPCOUNT(GetFpSpills(type))) * 118 static_cast<size_t>(kRiscv64PointerSize); 119 } 120 GetGpr1Offset(CalleeSaveType type)121 static constexpr size_t GetGpr1Offset(CalleeSaveType type) { 122 type = GetCanonicalCalleeSaveType(type); 123 return GetFrameSize(type) - 124 POPCOUNT(GetCoreSpills(type)) * static_cast<size_t>(kRiscv64PointerSize); 125 } 126 GetReturnPcOffset(CalleeSaveType type)127 static constexpr size_t GetReturnPcOffset(CalleeSaveType type) { 128 type = GetCanonicalCalleeSaveType(type); 129 return GetFrameSize(type) - static_cast<size_t>(kRiscv64PointerSize); 130 } 131 }; 132 133 // Assembly entrypoints rely on these constants. 134 static_assert(Riscv64CalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs) == 224); 135 static_assert(Riscv64CalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveAllCalleeSaves) == 208); 136 static_assert(Riscv64CalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveEverything) == 480); 137 138 } // namespace riscv64 139 } // namespace art 140 141 #endif // ART_RUNTIME_ARCH_RISCV64_CALLEE_SAVE_FRAME_RISCV64_H_ 142