1 /*
2  * Copyright (C) 2014 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_CALLEE_SAVE_FRAME_ARM64_H_
18 #define ART_RUNTIME_ARCH_ARM64_CALLEE_SAVE_FRAME_ARM64_H_
19 
20 #include "arch/instruction_set.h"
21 #include "base/bit_utils.h"
22 #include "base/callee_save_type.h"
23 #include "base/enums.h"
24 #include "quick/quick_method_frame_info.h"
25 #include "registers_arm64.h"
26 #include "runtime_globals.h"
27 
28 namespace art {
29 namespace arm64 {
30 
31 // Registers need to be restored but not preserved by aapcs64.
32 static constexpr uint32_t kArm64CalleeSaveAlwaysSpills =
33     // Note: ArtMethod::GetReturnPcOffsetInBytes() rely on the assumption that
34     // LR is always saved on the top of the frame for all targets.
35     // That is, lr = *(sp + framesize - pointer_size).
36     (1 << art::arm64::LR);
37 // Callee saved registers
38 static constexpr uint32_t kArm64CalleeSaveRefSpills =
39     (1 << art::arm64::X20) | (1 << art::arm64::X21) | (1 << art::arm64::X22) |
40     (1 << art::arm64::X23) | (1 << art::arm64::X24) | (1 << art::arm64::X25) |
41     (1 << art::arm64::X26) | (1 << art::arm64::X27) | (1 << art::arm64::X28) |
42     (1 << art::arm64::X29);
43 // X0 is the method pointer. Not saved.
44 static constexpr uint32_t kArm64CalleeSaveArgSpills =
45     (1 << art::arm64::X1) | (1 << art::arm64::X2) | (1 << art::arm64::X3) |
46     (1 << art::arm64::X4) | (1 << art::arm64::X5) | (1 << art::arm64::X6) |
47     (1 << art::arm64::X7);
48 static constexpr uint32_t kArm64CalleeSaveAllSpills =
49     (1 << art::arm64::X19);
50 static constexpr uint32_t kArm64CalleeSaveEverythingSpills =
51     (1 << art::arm64::X0) | (1 << art::arm64::X1) | (1 << art::arm64::X2) |
52     (1 << art::arm64::X3) | (1 << art::arm64::X4) | (1 << art::arm64::X5) |
53     (1 << art::arm64::X6) | (1 << art::arm64::X7) | (1 << art::arm64::X8) |
54     (1 << art::arm64::X9) | (1 << art::arm64::X10) | (1 << art::arm64::X11) |
55     (1 << art::arm64::X12) | (1 << art::arm64::X13) | (1 << art::arm64::X14) |
56     (1 << art::arm64::X15) | (1 << art::arm64::X16) | (1 << art::arm64::X17) |
57     (1 << art::arm64::X19);
58 
59 static constexpr uint32_t kArm64CalleeSaveFpAlwaysSpills = 0;
60 static constexpr uint32_t kArm64CalleeSaveFpRefSpills = 0;
61 static constexpr uint32_t kArm64CalleeSaveFpArgSpills =
62     (1 << art::arm64::D0) | (1 << art::arm64::D1) | (1 << art::arm64::D2) |
63     (1 << art::arm64::D3) | (1 << art::arm64::D4) | (1 << art::arm64::D5) |
64     (1 << art::arm64::D6) | (1 << art::arm64::D7);
65 static constexpr uint32_t kArm64CalleeSaveFpAllSpills =
66     (1 << art::arm64::D8)  | (1 << art::arm64::D9)  | (1 << art::arm64::D10) |
67     (1 << art::arm64::D11)  | (1 << art::arm64::D12)  | (1 << art::arm64::D13) |
68     (1 << art::arm64::D14)  | (1 << art::arm64::D15);
69 static constexpr uint32_t kArm64CalleeSaveFpEverythingSpills =
70     (1 << art::arm64::D0) | (1 << art::arm64::D1) | (1 << art::arm64::D2) |
71     (1 << art::arm64::D3) | (1 << art::arm64::D4) | (1 << art::arm64::D5) |
72     (1 << art::arm64::D6) | (1 << art::arm64::D7) | (1 << art::arm64::D8) |
73     (1 << art::arm64::D9) | (1 << art::arm64::D10) | (1 << art::arm64::D11) |
74     (1 << art::arm64::D12) | (1 << art::arm64::D13) | (1 << art::arm64::D14) |
75     (1 << art::arm64::D15) | (1 << art::arm64::D16) | (1 << art::arm64::D17) |
76     (1 << art::arm64::D18) | (1 << art::arm64::D19) | (1 << art::arm64::D20) |
77     (1 << art::arm64::D21) | (1 << art::arm64::D22) | (1 << art::arm64::D23) |
78     (1 << art::arm64::D24) | (1 << art::arm64::D25) | (1 << art::arm64::D26) |
79     (1 << art::arm64::D27) | (1 << art::arm64::D28) | (1 << art::arm64::D29) |
80     (1 << art::arm64::D30) | (1 << art::arm64::D31);
81 
82 class Arm64CalleeSaveFrame {
83  public:
GetCoreSpills(CalleeSaveType type)84   static constexpr uint32_t GetCoreSpills(CalleeSaveType type) {
85     type = GetCanonicalCalleeSaveType(type);
86     return kArm64CalleeSaveAlwaysSpills | kArm64CalleeSaveRefSpills |
87         (type == CalleeSaveType::kSaveRefsAndArgs ? kArm64CalleeSaveArgSpills : 0) |
88         (type == CalleeSaveType::kSaveAllCalleeSaves ? kArm64CalleeSaveAllSpills : 0) |
89         (type == CalleeSaveType::kSaveEverything ? kArm64CalleeSaveEverythingSpills : 0);
90   }
91 
GetFpSpills(CalleeSaveType type)92   static constexpr uint32_t GetFpSpills(CalleeSaveType type) {
93     type = GetCanonicalCalleeSaveType(type);
94     return kArm64CalleeSaveFpAlwaysSpills | kArm64CalleeSaveFpRefSpills |
95         (type == CalleeSaveType::kSaveRefsAndArgs ? kArm64CalleeSaveFpArgSpills : 0) |
96         (type == CalleeSaveType::kSaveAllCalleeSaves ? kArm64CalleeSaveFpAllSpills : 0) |
97         (type == CalleeSaveType::kSaveEverything ? kArm64CalleeSaveFpEverythingSpills : 0);
98   }
99 
GetFrameSize(CalleeSaveType type)100   static constexpr uint32_t GetFrameSize(CalleeSaveType type) {
101     type = GetCanonicalCalleeSaveType(type);
102     return RoundUp((POPCOUNT(GetCoreSpills(type)) /* gprs */ +
103                     POPCOUNT(GetFpSpills(type)) /* fprs */ +
104                     1 /* Method* */) * static_cast<size_t>(kArm64PointerSize), kStackAlignment);
105   }
106 
GetMethodFrameInfo(CalleeSaveType type)107   static constexpr QuickMethodFrameInfo GetMethodFrameInfo(CalleeSaveType type) {
108     type = GetCanonicalCalleeSaveType(type);
109     return QuickMethodFrameInfo(GetFrameSize(type), GetCoreSpills(type), GetFpSpills(type));
110   }
111 
GetFpr1Offset(CalleeSaveType type)112   static constexpr size_t GetFpr1Offset(CalleeSaveType type) {
113     type = GetCanonicalCalleeSaveType(type);
114     return GetFrameSize(type) -
115            (POPCOUNT(GetCoreSpills(type)) +
116             POPCOUNT(GetFpSpills(type))) * static_cast<size_t>(kArm64PointerSize);
117   }
118 
GetGpr1Offset(CalleeSaveType type)119   static constexpr size_t GetGpr1Offset(CalleeSaveType type) {
120     type = GetCanonicalCalleeSaveType(type);
121     return GetFrameSize(type) -
122            POPCOUNT(GetCoreSpills(type)) * static_cast<size_t>(kArm64PointerSize);
123   }
124 
GetReturnPcOffset(CalleeSaveType type)125   static constexpr size_t GetReturnPcOffset(CalleeSaveType type) {
126     type = GetCanonicalCalleeSaveType(type);
127     return GetFrameSize(type) - static_cast<size_t>(kArm64PointerSize);
128   }
129 };
130 
131 }  // namespace arm64
132 }  // namespace art
133 
134 #endif  // ART_RUNTIME_ARCH_ARM64_CALLEE_SAVE_FRAME_ARM64_H_
135