xref: /llvm-project/lldb/source/Plugins/ABI/X86/ABIX86.cpp (revision 2fe8327406050d2585d2ced910a678e28caefcf5)
167231650SMichał Górny //===-- ABIX86.cpp --------------------------------------------------------===//
2f10e2df7SJonas Devlieghere //
3f10e2df7SJonas Devlieghere // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4f10e2df7SJonas Devlieghere // See https://llvm.org/LICENSE.txt for license information.
5f10e2df7SJonas Devlieghere // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f10e2df7SJonas Devlieghere //
7f10e2df7SJonas Devlieghere //===----------------------------------------------------------------------===//
8f10e2df7SJonas Devlieghere 
9f10e2df7SJonas Devlieghere #include "ABIMacOSX_i386.h"
10f10e2df7SJonas Devlieghere #include "ABISysV_i386.h"
11f10e2df7SJonas Devlieghere #include "ABISysV_x86_64.h"
12f10e2df7SJonas Devlieghere #include "ABIWindows_x86_64.h"
132712d181SMichał Górny #include "ABIX86.h"
14f10e2df7SJonas Devlieghere #include "lldb/Core/PluginManager.h"
152712d181SMichał Górny #include "lldb/Target/Process.h"
16d5c6dc8fSKazu Hirata #include <optional>
172712d181SMichał Górny 
182712d181SMichał Górny using namespace lldb;
192712d181SMichał Górny using namespace lldb_private;
20f10e2df7SJonas Devlieghere 
LLDB_PLUGIN_DEFINE(ABIX86)21bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(ABIX86)
22f10e2df7SJonas Devlieghere 
23f10e2df7SJonas Devlieghere void ABIX86::Initialize() {
24f10e2df7SJonas Devlieghere   ABIMacOSX_i386::Initialize();
25f10e2df7SJonas Devlieghere   ABISysV_i386::Initialize();
26f10e2df7SJonas Devlieghere   ABISysV_x86_64::Initialize();
27f10e2df7SJonas Devlieghere   ABIWindows_x86_64::Initialize();
28f10e2df7SJonas Devlieghere }
29f10e2df7SJonas Devlieghere 
Terminate()30f10e2df7SJonas Devlieghere void ABIX86::Terminate() {
31f10e2df7SJonas Devlieghere   ABIMacOSX_i386::Terminate();
32f10e2df7SJonas Devlieghere   ABISysV_i386::Terminate();
33f10e2df7SJonas Devlieghere   ABISysV_x86_64::Terminate();
34f10e2df7SJonas Devlieghere   ABIWindows_x86_64::Terminate();
35f10e2df7SJonas Devlieghere }
362712d181SMichał Górny 
37c6d7f248SMichał Górny namespace {
38c6d7f248SMichał Górny enum RegKind {
39c6d7f248SMichał Górny   GPR32,
402712d181SMichał Górny   GPR16,
412712d181SMichał Górny   GPR8h,
422712d181SMichał Górny   GPR8,
43c6d7f248SMichał Górny   MM,
44c6d7f248SMichał Górny   YMM_YMMh,
45c6d7f248SMichał Górny   YMM_XMM,
462712d181SMichał Górny 
47c6d7f248SMichał Górny   RegKindCount
48c6d7f248SMichał Górny };
49c6d7f248SMichał Górny }
50c6d7f248SMichał Górny 
51c6d7f248SMichał Górny struct RegData {
52c6d7f248SMichał Górny   RegKind subreg_kind;
53c6d7f248SMichał Górny   llvm::StringRef subreg_name;
54*2fe83274SKazu Hirata   std::optional<uint32_t> base_index;
552712d181SMichał Górny };
562712d181SMichał Górny 
57c6d7f248SMichał Górny static void
addPartialRegisters(std::vector<DynamicRegisterInfo::Register> & regs,llvm::ArrayRef<RegData * > subregs,uint32_t base_size,lldb::Encoding encoding,lldb::Format format,uint32_t subreg_size,uint32_t subreg_offset=0)58c6d7f248SMichał Górny addPartialRegisters(std::vector<DynamicRegisterInfo::Register> &regs,
59c6d7f248SMichał Górny                     llvm::ArrayRef<RegData *> subregs, uint32_t base_size,
60c6d7f248SMichał Górny                     lldb::Encoding encoding, lldb::Format format,
61c6d7f248SMichał Górny                     uint32_t subreg_size, uint32_t subreg_offset = 0) {
62c6d7f248SMichał Górny   for (const RegData *subreg : subregs) {
63c6d7f248SMichał Górny     assert(subreg);
64ed8fceaaSKazu Hirata     uint32_t base_index = *subreg->base_index;
652712d181SMichał Górny     DynamicRegisterInfo::Register &full_reg = regs[base_index];
66c6d7f248SMichał Górny     if (full_reg.byte_size != base_size)
672712d181SMichał Górny       continue;
682712d181SMichał Górny 
69c6d7f248SMichał Górny     lldb_private::DynamicRegisterInfo::Register new_reg{
70c6d7f248SMichał Górny         lldb_private::ConstString(subreg->subreg_name),
712712d181SMichał Górny         lldb_private::ConstString(),
722712d181SMichał Górny         lldb_private::ConstString("supplementary registers"),
732712d181SMichał Górny         subreg_size,
742712d181SMichał Górny         LLDB_INVALID_INDEX32,
752712d181SMichał Górny         encoding,
762712d181SMichał Górny         format,
772712d181SMichał Górny         LLDB_INVALID_REGNUM,
782712d181SMichał Górny         LLDB_INVALID_REGNUM,
792712d181SMichał Górny         LLDB_INVALID_REGNUM,
802712d181SMichał Górny         LLDB_INVALID_REGNUM,
812712d181SMichał Górny         {base_index},
822712d181SMichał Górny         {},
832712d181SMichał Górny         subreg_offset};
842712d181SMichał Górny 
85c6d7f248SMichał Górny     addSupplementaryRegister(regs, new_reg);
862712d181SMichał Górny   }
872712d181SMichał Górny }
882712d181SMichał Górny 
89f290efc3SMichał Górny static void
addCombinedRegisters(std::vector<DynamicRegisterInfo::Register> & regs,llvm::ArrayRef<RegData * > subregs1,llvm::ArrayRef<RegData * > subregs2,uint32_t base_size,lldb::Encoding encoding,lldb::Format format)90f290efc3SMichał Górny addCombinedRegisters(std::vector<DynamicRegisterInfo::Register> &regs,
91f290efc3SMichał Górny                      llvm::ArrayRef<RegData *> subregs1,
92f290efc3SMichał Górny                      llvm::ArrayRef<RegData *> subregs2, uint32_t base_size,
93f290efc3SMichał Górny                      lldb::Encoding encoding, lldb::Format format) {
94f290efc3SMichał Górny   for (auto it : llvm::zip(subregs1, subregs2)) {
95f290efc3SMichał Górny     RegData *regdata1, *regdata2;
96f290efc3SMichał Górny     std::tie(regdata1, regdata2) = it;
97f290efc3SMichał Górny     assert(regdata1);
98f290efc3SMichał Górny     assert(regdata2);
99f290efc3SMichał Górny 
100f290efc3SMichał Górny     // verify that we've got matching target registers
101f290efc3SMichał Górny     if (regdata1->subreg_name != regdata2->subreg_name)
102f290efc3SMichał Górny       continue;
103f290efc3SMichał Górny 
1049464bd8cSFangrui Song     uint32_t base_index1 = *regdata1->base_index;
1059464bd8cSFangrui Song     uint32_t base_index2 = *regdata2->base_index;
106f290efc3SMichał Górny     if (regs[base_index1].byte_size != base_size ||
107f290efc3SMichał Górny         regs[base_index2].byte_size != base_size)
108f290efc3SMichał Górny       continue;
109f290efc3SMichał Górny 
110f290efc3SMichał Górny     lldb_private::DynamicRegisterInfo::Register new_reg{
111f290efc3SMichał Górny         lldb_private::ConstString(regdata1->subreg_name),
112f290efc3SMichał Górny         lldb_private::ConstString(),
113f290efc3SMichał Górny         lldb_private::ConstString("supplementary registers"),
114f290efc3SMichał Górny         base_size * 2,
115f290efc3SMichał Górny         LLDB_INVALID_INDEX32,
116f290efc3SMichał Górny         encoding,
117f290efc3SMichał Górny         format,
118f290efc3SMichał Górny         LLDB_INVALID_REGNUM,
119f290efc3SMichał Górny         LLDB_INVALID_REGNUM,
120f290efc3SMichał Górny         LLDB_INVALID_REGNUM,
121f290efc3SMichał Górny         LLDB_INVALID_REGNUM,
122f290efc3SMichał Górny         {base_index1, base_index2},
123f290efc3SMichał Górny         {}};
124f290efc3SMichał Górny 
125f290efc3SMichał Górny     addSupplementaryRegister(regs, new_reg);
126f290efc3SMichał Górny   }
127f290efc3SMichał Górny }
128f290efc3SMichał Górny 
129c6d7f248SMichał Górny typedef llvm::SmallDenseMap<llvm::StringRef, llvm::SmallVector<RegData, 4>, 64>
130c6d7f248SMichał Górny     BaseRegToRegsMap;
131c6d7f248SMichał Górny 
132c6d7f248SMichał Górny #define GPRh(l)                                                                \
133c6d7f248SMichał Górny   {                                                                            \
134d5c6dc8fSKazu Hirata     is64bit ? BaseRegToRegsMap::value_type("r" l "x",                          \
135d5c6dc8fSKazu Hirata                                            {{GPR32, "e" l "x", std::nullopt},  \
136d5c6dc8fSKazu Hirata                                             {GPR16, l "x", std::nullopt},      \
137d5c6dc8fSKazu Hirata                                             {GPR8h, l "h", std::nullopt},      \
138d5c6dc8fSKazu Hirata                                             {GPR8, l "l", std::nullopt}})      \
139d5c6dc8fSKazu Hirata             : BaseRegToRegsMap::value_type("e" l "x",                          \
140d5c6dc8fSKazu Hirata                                            {{GPR16, l "x", std::nullopt},      \
141d5c6dc8fSKazu Hirata                                             {GPR8h, l "h", std::nullopt},      \
142d5c6dc8fSKazu Hirata                                             {GPR8, l "l", std::nullopt}})      \
143c6d7f248SMichał Górny   }
144c6d7f248SMichał Górny 
145c6d7f248SMichał Górny #define GPR(r16)                                                               \
146c6d7f248SMichał Górny   {                                                                            \
147d5c6dc8fSKazu Hirata     is64bit ? BaseRegToRegsMap::value_type("r" r16,                            \
148d5c6dc8fSKazu Hirata                                            {{GPR32, "e" r16, std::nullopt},    \
149d5c6dc8fSKazu Hirata                                             {GPR16, r16, std::nullopt},        \
150d5c6dc8fSKazu Hirata                                             {GPR8, r16 "l", std::nullopt}})    \
151d5c6dc8fSKazu Hirata             : BaseRegToRegsMap::value_type(                                    \
152d5c6dc8fSKazu Hirata                   "e" r16,                                                     \
153d5c6dc8fSKazu Hirata                   {{GPR16, r16, std::nullopt}, {GPR8, r16 "l", std::nullopt}}) \
154c6d7f248SMichał Górny   }
155c6d7f248SMichał Górny 
156c6d7f248SMichał Górny #define GPR64(n)                                                               \
157c6d7f248SMichał Górny   {                                                                            \
158d5c6dc8fSKazu Hirata     BaseRegToRegsMap::value_type("r" #n, {{GPR32, "r" #n "d", std::nullopt},   \
159d5c6dc8fSKazu Hirata                                           {GPR16, "r" #n "w", std::nullopt},   \
160d5c6dc8fSKazu Hirata                                           {GPR8, "r" #n "l", std::nullopt}})   \
161c6d7f248SMichał Górny   }
162c6d7f248SMichał Górny 
163c6d7f248SMichał Górny #define STMM(n)                                                                \
164d5c6dc8fSKazu Hirata   { BaseRegToRegsMap::value_type("st" #n, {{MM, "mm" #n, std::nullopt}}) }
165c6d7f248SMichał Górny 
166f290efc3SMichał Górny #define YMM(n)                                                                 \
167f290efc3SMichał Górny   {BaseRegToRegsMap::value_type("ymm" #n "h",                                  \
168d5c6dc8fSKazu Hirata                                 {{YMM_YMMh, "ymm" #n, std::nullopt}})},        \
169f290efc3SMichał Górny   {                                                                            \
170d5c6dc8fSKazu Hirata     BaseRegToRegsMap::value_type("xmm" #n,                                     \
171d5c6dc8fSKazu Hirata                                  {{YMM_XMM, "ymm" #n, std::nullopt}})          \
172f290efc3SMichał Górny   }
173f290efc3SMichał Górny 
makeBaseRegMap(bool is64bit)174c6d7f248SMichał Górny BaseRegToRegsMap makeBaseRegMap(bool is64bit) {
175f290efc3SMichał Górny   BaseRegToRegsMap out{
176f290efc3SMichał Górny       {// GPRs common to amd64 & i386
177f290efc3SMichał Górny        GPRh("a"), GPRh("b"), GPRh("c"), GPRh("d"), GPR("si"), GPR("di"),
178f290efc3SMichał Górny        GPR("bp"), GPR("sp"),
179c6d7f248SMichał Górny 
180c6d7f248SMichał Górny        // ST/MM registers
181f290efc3SMichał Górny        STMM(0), STMM(1), STMM(2), STMM(3), STMM(4), STMM(5), STMM(6), STMM(7),
182f290efc3SMichał Górny 
183f290efc3SMichał Górny        // lower YMM registers (common to amd64 & i386)
184f290efc3SMichał Górny        YMM(0), YMM(1), YMM(2), YMM(3), YMM(4), YMM(5), YMM(6), YMM(7)}};
185c6d7f248SMichał Górny 
186c6d7f248SMichał Górny   if (is64bit) {
187c6d7f248SMichał Górny     BaseRegToRegsMap amd64_regs{{// GPRs specific to amd64
188c6d7f248SMichał Górny                                  GPR64(8), GPR64(9), GPR64(10), GPR64(11),
189f290efc3SMichał Górny                                  GPR64(12), GPR64(13), GPR64(14), GPR64(15),
190f290efc3SMichał Górny 
191f290efc3SMichał Górny                                  // higher YMM registers (specific to amd64)
192f290efc3SMichał Górny                                  YMM(8), YMM(9), YMM(10), YMM(11), YMM(12),
193f290efc3SMichał Górny                                  YMM(13), YMM(14), YMM(15)}};
194c6d7f248SMichał Górny     out.insert(amd64_regs.begin(), amd64_regs.end());
195c6d7f248SMichał Górny   }
196c6d7f248SMichał Górny 
197c6d7f248SMichał Górny   return out;
198c6d7f248SMichał Górny }
199c6d7f248SMichał Górny 
AugmentRegisterInfo(std::vector<DynamicRegisterInfo::Register> & regs)2002712d181SMichał Górny void ABIX86::AugmentRegisterInfo(
2012712d181SMichał Górny     std::vector<DynamicRegisterInfo::Register> &regs) {
2022712d181SMichał Górny   MCBasedABI::AugmentRegisterInfo(regs);
2032712d181SMichał Górny 
2042712d181SMichał Górny   ProcessSP process_sp = GetProcessSP();
2052712d181SMichał Górny   if (!process_sp)
2062712d181SMichał Górny     return;
2072712d181SMichał Górny 
2082712d181SMichał Górny   uint32_t gpr_base_size =
2092712d181SMichał Górny       process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
2102712d181SMichał Górny 
211c6d7f248SMichał Górny   // primary map from a base register to its subregisters
212c6d7f248SMichał Górny   BaseRegToRegsMap base_reg_map = makeBaseRegMap(gpr_base_size == 8);
213c6d7f248SMichał Górny   // set used for fast matching of register names to subregisters
214ee11612eSMichał Górny   llvm::SmallDenseSet<llvm::StringRef, 64> subreg_name_set;
215c6d7f248SMichał Górny   // convenience array providing access to all subregisters of given kind,
216c6d7f248SMichał Górny   // sorted by base register index
217c6d7f248SMichał Górny   std::array<llvm::SmallVector<RegData *, 16>, RegKindCount> subreg_by_kind;
218ee11612eSMichał Górny 
219c6d7f248SMichał Górny   // prepare the set of all known subregisters
220c6d7f248SMichał Górny   for (const auto &x : base_reg_map) {
221c6d7f248SMichał Górny     for (const auto &subreg : x.second)
222c6d7f248SMichał Górny       subreg_name_set.insert(subreg.subreg_name);
223ee11612eSMichał Górny   }
224ee11612eSMichał Górny 
225c6d7f248SMichał Górny   // iterate over all registers
2262712d181SMichał Górny   for (const auto &x : llvm::enumerate(regs)) {
2272712d181SMichał Górny     llvm::StringRef reg_name = x.value().name.GetStringRef();
2282712d181SMichał Górny     // abort if at least one sub-register is already present
229c6d7f248SMichał Górny     if (llvm::is_contained(subreg_name_set, reg_name))
2302712d181SMichał Górny       return;
231c6d7f248SMichał Górny 
232c6d7f248SMichał Górny     auto found = base_reg_map.find(reg_name);
233c6d7f248SMichał Górny     if (found == base_reg_map.end())
234c6d7f248SMichał Górny       continue;
235c6d7f248SMichał Górny 
236c6d7f248SMichał Górny     for (auto &subreg : found->second) {
237c6d7f248SMichał Górny       // fill in base register indices
238c6d7f248SMichał Górny       subreg.base_index = x.index();
239c6d7f248SMichał Górny       // fill subreg_by_kind map-array
240c6d7f248SMichał Górny       subreg_by_kind[static_cast<size_t>(subreg.subreg_kind)].push_back(
241c6d7f248SMichał Górny           &subreg);
242c6d7f248SMichał Górny     }
2432712d181SMichał Górny   }
2442712d181SMichał Górny 
245c6d7f248SMichał Górny   // now add registers by kind
246c6d7f248SMichał Górny   addPartialRegisters(regs, subreg_by_kind[GPR32], gpr_base_size, eEncodingUint,
247c6d7f248SMichał Górny                       eFormatHex, 4);
248c6d7f248SMichał Górny   addPartialRegisters(regs, subreg_by_kind[GPR16], gpr_base_size, eEncodingUint,
249c6d7f248SMichał Górny                       eFormatHex, 2);
250c6d7f248SMichał Górny   addPartialRegisters(regs, subreg_by_kind[GPR8h], gpr_base_size, eEncodingUint,
251c6d7f248SMichał Górny                       eFormatHex, 1, 1);
252c6d7f248SMichał Górny   addPartialRegisters(regs, subreg_by_kind[GPR8], gpr_base_size, eEncodingUint,
253c6d7f248SMichał Górny                       eFormatHex, 1);
2542712d181SMichał Górny 
255c6d7f248SMichał Górny   addPartialRegisters(regs, subreg_by_kind[MM], 10, eEncodingUint, eFormatHex,
256c6d7f248SMichał Górny                       8);
257f290efc3SMichał Górny 
258f290efc3SMichał Górny   addCombinedRegisters(regs, subreg_by_kind[YMM_XMM], subreg_by_kind[YMM_YMMh],
259f290efc3SMichał Górny                        16, eEncodingVector, eFormatVectorOfUInt8);
2602712d181SMichał Górny }
261