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_CALLEE_SAVE_FRAME_X86_H_
18 #define ART_RUNTIME_ARCH_X86_CALLEE_SAVE_FRAME_X86_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_x86.h"
27 #include "runtime_globals.h"
28 
29 namespace art HIDDEN {
30 namespace x86 {
31 
32 static constexpr uint32_t kX86CalleeSaveAlwaysSpills =
33     (1 << art::x86::kNumberOfCpuRegisters);  // Fake return address callee save.
34 static constexpr uint32_t kX86CalleeSaveRefSpills =
35     (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
36 static constexpr uint32_t kX86CalleeSaveArgSpills =
37     (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
38 static constexpr uint32_t kX86CalleeSaveEverythingSpills =
39     (1 << art::x86::EAX) | (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
40 
41 static constexpr uint32_t kX86CalleeSaveFpArgSpills =
42     (1 << art::x86::XMM0) | (1 << art::x86::XMM1) |
43     (1 << art::x86::XMM2) | (1 << art::x86::XMM3);
44 static constexpr uint32_t kX86CalleeSaveFpEverythingSpills =
45     (1 << art::x86::XMM0) | (1 << art::x86::XMM1) |
46     (1 << art::x86::XMM2) | (1 << art::x86::XMM3) |
47     (1 << art::x86::XMM4) | (1 << art::x86::XMM5) |
48     (1 << art::x86::XMM6) | (1 << art::x86::XMM7);
49 
50 class X86CalleeSaveFrame {
51  public:
GetCoreSpills(CalleeSaveType type)52   static constexpr uint32_t GetCoreSpills(CalleeSaveType type) {
53     type = GetCanonicalCalleeSaveType(type);
54     return kX86CalleeSaveAlwaysSpills | kX86CalleeSaveRefSpills |
55         (type == CalleeSaveType::kSaveRefsAndArgs ? kX86CalleeSaveArgSpills : 0) |
56         (type == CalleeSaveType::kSaveEverything ? kX86CalleeSaveEverythingSpills : 0);
57   }
58 
GetFpSpills(CalleeSaveType type)59   static constexpr uint32_t GetFpSpills(CalleeSaveType type) {
60     type = GetCanonicalCalleeSaveType(type);
61     return (type == CalleeSaveType::kSaveRefsAndArgs ? kX86CalleeSaveFpArgSpills : 0) |
62            (type == CalleeSaveType::kSaveEverything ? kX86CalleeSaveFpEverythingSpills : 0);
63   }
64 
GetFrameSize(CalleeSaveType type)65   static constexpr uint32_t GetFrameSize(CalleeSaveType type) {
66     type = GetCanonicalCalleeSaveType(type);
67     return RoundUp((POPCOUNT(GetCoreSpills(type)) /* gprs */ +
68                     2 * POPCOUNT(GetFpSpills(type)) /* fprs */ +
69                     1 /* Method* */) * static_cast<size_t>(kX86PointerSize), kStackAlignment);
70   }
71 
GetMethodFrameInfo(CalleeSaveType type)72   static constexpr QuickMethodFrameInfo GetMethodFrameInfo(CalleeSaveType type) {
73     type = GetCanonicalCalleeSaveType(type);
74     return QuickMethodFrameInfo(GetFrameSize(type), GetCoreSpills(type), GetFpSpills(type));
75   }
76 
GetFpr1Offset(CalleeSaveType type)77   static constexpr size_t GetFpr1Offset(CalleeSaveType type) {
78     type = GetCanonicalCalleeSaveType(type);
79     return GetFrameSize(type) -
80            (POPCOUNT(GetCoreSpills(type)) +
81             2 * POPCOUNT(GetFpSpills(type))) * static_cast<size_t>(kX86PointerSize);
82   }
83 
GetGpr1Offset(CalleeSaveType type)84   static constexpr size_t GetGpr1Offset(CalleeSaveType type) {
85     type = GetCanonicalCalleeSaveType(type);
86     return GetFrameSize(type) -
87            POPCOUNT(GetCoreSpills(type)) * static_cast<size_t>(kX86PointerSize);
88   }
89 
GetReturnPcOffset(CalleeSaveType type)90   static constexpr size_t GetReturnPcOffset(CalleeSaveType type) {
91     type = GetCanonicalCalleeSaveType(type);
92     return GetFrameSize(type) - static_cast<size_t>(kX86PointerSize);
93   }
94 };
95 
96 }  // namespace x86
97 }  // namespace art
98 
99 #endif  // ART_RUNTIME_ARCH_X86_CALLEE_SAVE_FRAME_X86_H_
100