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