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_X86_64_CALLEE_SAVE_FRAME_X86_64_H_
18 #define ART_RUNTIME_ARCH_X86_64_CALLEE_SAVE_FRAME_X86_64_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_x86_64.h"
26 #include "runtime_globals.h"
27 
28 namespace art {
29 namespace x86_64 {
30 
31 static constexpr uint32_t kX86_64CalleeSaveAlwaysSpills =
32     (1 << art::x86_64::kNumberOfCpuRegisters);  // Fake return address callee save.
33 static constexpr uint32_t kX86_64CalleeSaveRefSpills =
34     (1 << art::x86_64::RBX) | (1 << art::x86_64::RBP) | (1 << art::x86_64::R12) |
35     (1 << art::x86_64::R13) | (1 << art::x86_64::R14) | (1 << art::x86_64::R15);
36 static constexpr uint32_t kX86_64CalleeSaveArgSpills =
37     (1 << art::x86_64::RSI) | (1 << art::x86_64::RDX) | (1 << art::x86_64::RCX) |
38     (1 << art::x86_64::R8) | (1 << art::x86_64::R9);
39 static constexpr uint32_t kX86_64CalleeSaveEverythingSpills =
40     (1 << art::x86_64::RAX) | (1 << art::x86_64::RCX) | (1 << art::x86_64::RDX) |
41     (1 << art::x86_64::RSI) | (1 << art::x86_64::RDI) | (1 << art::x86_64::R8) |
42     (1 << art::x86_64::R9) | (1 << art::x86_64::R10) | (1 << art::x86_64::R11);
43 
44 static constexpr uint32_t kX86_64CalleeSaveFpArgSpills =
45     (1 << art::x86_64::XMM0) | (1 << art::x86_64::XMM1) | (1 << art::x86_64::XMM2) |
46     (1 << art::x86_64::XMM3) | (1 << art::x86_64::XMM4) | (1 << art::x86_64::XMM5) |
47     (1 << art::x86_64::XMM6) | (1 << art::x86_64::XMM7);
48 static constexpr uint32_t kX86_64CalleeSaveFpSpills =
49     (1 << art::x86_64::XMM12) | (1 << art::x86_64::XMM13) |
50     (1 << art::x86_64::XMM14) | (1 << art::x86_64::XMM15);
51 static constexpr uint32_t kX86_64CalleeSaveFpEverythingSpills =
52     (1 << art::x86_64::XMM0) | (1 << art::x86_64::XMM1) |
53     (1 << art::x86_64::XMM2) | (1 << art::x86_64::XMM3) |
54     (1 << art::x86_64::XMM4) | (1 << art::x86_64::XMM5) |
55     (1 << art::x86_64::XMM6) | (1 << art::x86_64::XMM7) |
56     (1 << art::x86_64::XMM8) | (1 << art::x86_64::XMM9) |
57     (1 << art::x86_64::XMM10) | (1 << art::x86_64::XMM11);
58 
59 class X86_64CalleeSaveFrame {
60  public:
GetCoreSpills(CalleeSaveType type)61   static constexpr uint32_t GetCoreSpills(CalleeSaveType type) {
62     type = GetCanonicalCalleeSaveType(type);
63     return kX86_64CalleeSaveAlwaysSpills | kX86_64CalleeSaveRefSpills |
64         (type == CalleeSaveType::kSaveRefsAndArgs ? kX86_64CalleeSaveArgSpills : 0) |
65         (type == CalleeSaveType::kSaveEverything ? kX86_64CalleeSaveEverythingSpills : 0);
66   }
67 
GetFpSpills(CalleeSaveType type)68   static constexpr uint32_t GetFpSpills(CalleeSaveType type) {
69     type = GetCanonicalCalleeSaveType(type);
70     return kX86_64CalleeSaveFpSpills |
71         (type == CalleeSaveType::kSaveRefsAndArgs ? kX86_64CalleeSaveFpArgSpills : 0) |
72         (type == CalleeSaveType::kSaveEverything ? kX86_64CalleeSaveFpEverythingSpills : 0);
73   }
74 
GetFrameSize(CalleeSaveType type)75   static constexpr uint32_t GetFrameSize(CalleeSaveType type) {
76     type = GetCanonicalCalleeSaveType(type);
77     return RoundUp((POPCOUNT(GetCoreSpills(type)) /* gprs */ +
78                     POPCOUNT(GetFpSpills(type)) /* fprs */ +
79                     1 /* Method* */) * static_cast<size_t>(kX86_64PointerSize), kStackAlignment);
80   }
81 
GetMethodFrameInfo(CalleeSaveType type)82   static constexpr QuickMethodFrameInfo GetMethodFrameInfo(CalleeSaveType type) {
83     type = GetCanonicalCalleeSaveType(type);
84     return QuickMethodFrameInfo(GetFrameSize(type), GetCoreSpills(type), GetFpSpills(type));
85   }
86 
GetFpr1Offset(CalleeSaveType type)87   static constexpr size_t GetFpr1Offset(CalleeSaveType type) {
88     type = GetCanonicalCalleeSaveType(type);
89     return GetFrameSize(type) -
90            (POPCOUNT(GetCoreSpills(type)) +
91             POPCOUNT(GetFpSpills(type))) * static_cast<size_t>(kX86_64PointerSize);
92   }
93 
GetGpr1Offset(CalleeSaveType type)94   static constexpr size_t GetGpr1Offset(CalleeSaveType type) {
95     type = GetCanonicalCalleeSaveType(type);
96     return GetFrameSize(type) -
97            POPCOUNT(GetCoreSpills(type)) * static_cast<size_t>(kX86_64PointerSize);
98   }
99 
GetReturnPcOffset(CalleeSaveType type)100   static constexpr size_t GetReturnPcOffset(CalleeSaveType type) {
101     type = GetCanonicalCalleeSaveType(type);
102     return GetFrameSize(type) - static_cast<size_t>(kX86_64PointerSize);
103   }
104 };
105 
106 }  // namespace x86_64
107 }  // namespace art
108 
109 #endif  // ART_RUNTIME_ARCH_X86_64_CALLEE_SAVE_FRAME_X86_64_H_
110