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