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_QUICK_METHOD_FRAME_INFO_ARM64_H_
18 #define ART_RUNTIME_ARCH_ARM64_QUICK_METHOD_FRAME_INFO_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 "globals.h"
25 #include "quick/quick_method_frame_info.h"
26 #include "registers_arm64.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::X18) | (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 
Arm64CalleeSaveCoreSpills(CalleeSaveType type)82 constexpr uint32_t Arm64CalleeSaveCoreSpills(CalleeSaveType type) {
83   type = GetCanonicalCalleeSaveType(type);
84   return kArm64CalleeSaveAlwaysSpills | kArm64CalleeSaveRefSpills |
85       (type == CalleeSaveType::kSaveRefsAndArgs ? kArm64CalleeSaveArgSpills : 0) |
86       (type == CalleeSaveType::kSaveAllCalleeSaves ? kArm64CalleeSaveAllSpills : 0) |
87       (type == CalleeSaveType::kSaveEverything ? kArm64CalleeSaveEverythingSpills : 0);
88 }
89 
Arm64CalleeSaveFpSpills(CalleeSaveType type)90 constexpr uint32_t Arm64CalleeSaveFpSpills(CalleeSaveType type) {
91   type = GetCanonicalCalleeSaveType(type);
92   return kArm64CalleeSaveFpAlwaysSpills | kArm64CalleeSaveFpRefSpills |
93       (type == CalleeSaveType::kSaveRefsAndArgs ? kArm64CalleeSaveFpArgSpills : 0) |
94       (type == CalleeSaveType::kSaveAllCalleeSaves ? kArm64CalleeSaveFpAllSpills : 0) |
95       (type == CalleeSaveType::kSaveEverything ? kArm64CalleeSaveFpEverythingSpills : 0);
96 }
97 
Arm64CalleeSaveFrameSize(CalleeSaveType type)98 constexpr uint32_t Arm64CalleeSaveFrameSize(CalleeSaveType type) {
99   type = GetCanonicalCalleeSaveType(type);
100   return RoundUp((POPCOUNT(Arm64CalleeSaveCoreSpills(type)) /* gprs */ +
101                   POPCOUNT(Arm64CalleeSaveFpSpills(type)) /* fprs */ +
102                   1 /* Method* */) * static_cast<size_t>(kArm64PointerSize), kStackAlignment);
103 }
104 
Arm64CalleeSaveMethodFrameInfo(CalleeSaveType type)105 constexpr QuickMethodFrameInfo Arm64CalleeSaveMethodFrameInfo(CalleeSaveType type) {
106   type = GetCanonicalCalleeSaveType(type);
107   return QuickMethodFrameInfo(Arm64CalleeSaveFrameSize(type),
108                               Arm64CalleeSaveCoreSpills(type),
109                               Arm64CalleeSaveFpSpills(type));
110 }
111 
Arm64CalleeSaveFpr1Offset(CalleeSaveType type)112 constexpr size_t Arm64CalleeSaveFpr1Offset(CalleeSaveType type) {
113   type = GetCanonicalCalleeSaveType(type);
114   return Arm64CalleeSaveFrameSize(type) -
115          (POPCOUNT(Arm64CalleeSaveCoreSpills(type)) +
116           POPCOUNT(Arm64CalleeSaveFpSpills(type))) * static_cast<size_t>(kArm64PointerSize);
117 }
118 
Arm64CalleeSaveGpr1Offset(CalleeSaveType type)119 constexpr size_t Arm64CalleeSaveGpr1Offset(CalleeSaveType type) {
120   type = GetCanonicalCalleeSaveType(type);
121   return Arm64CalleeSaveFrameSize(type) -
122          POPCOUNT(Arm64CalleeSaveCoreSpills(type)) * static_cast<size_t>(kArm64PointerSize);
123 }
124 
Arm64CalleeSaveLrOffset(CalleeSaveType type)125 constexpr size_t Arm64CalleeSaveLrOffset(CalleeSaveType type) {
126   type = GetCanonicalCalleeSaveType(type);
127   return Arm64CalleeSaveFrameSize(type) -
128       POPCOUNT(Arm64CalleeSaveCoreSpills(type) & (-(1 << LR))) *
129       static_cast<size_t>(kArm64PointerSize);
130 }
131 
132 }  // namespace arm64
133 }  // namespace art
134 
135 #endif  // ART_RUNTIME_ARCH_ARM64_QUICK_METHOD_FRAME_INFO_ARM64_H_
136