xref: /freebsd-src/contrib/llvm-project/lldb/source/Target/DynamicRegisterInfo.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1349cc55cSDimitry Andric //===-- DynamicRegisterInfo.cpp -------------------------------------------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric 
9349cc55cSDimitry Andric #include "lldb/Target/DynamicRegisterInfo.h"
10349cc55cSDimitry Andric #include "lldb/Core/StreamFile.h"
11349cc55cSDimitry Andric #include "lldb/DataFormatters/FormatManager.h"
12349cc55cSDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
13349cc55cSDimitry Andric #include "lldb/Utility/ArchSpec.h"
14*81ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
15349cc55cSDimitry Andric #include "lldb/Utility/Log.h"
16349cc55cSDimitry Andric #include "lldb/Utility/RegularExpression.h"
17349cc55cSDimitry Andric #include "lldb/Utility/StringExtractor.h"
18349cc55cSDimitry Andric #include "lldb/Utility/StructuredData.h"
19349cc55cSDimitry Andric 
20349cc55cSDimitry Andric using namespace lldb;
21349cc55cSDimitry Andric using namespace lldb_private;
22349cc55cSDimitry Andric 
23349cc55cSDimitry Andric DynamicRegisterInfo::DynamicRegisterInfo(
24349cc55cSDimitry Andric     const lldb_private::StructuredData::Dictionary &dict,
25349cc55cSDimitry Andric     const lldb_private::ArchSpec &arch) {
26349cc55cSDimitry Andric   SetRegisterInfo(dict, arch);
27349cc55cSDimitry Andric }
28349cc55cSDimitry Andric 
29349cc55cSDimitry Andric DynamicRegisterInfo::DynamicRegisterInfo(DynamicRegisterInfo &&info) {
30349cc55cSDimitry Andric   MoveFrom(std::move(info));
31349cc55cSDimitry Andric }
32349cc55cSDimitry Andric 
33349cc55cSDimitry Andric DynamicRegisterInfo &
34349cc55cSDimitry Andric DynamicRegisterInfo::operator=(DynamicRegisterInfo &&info) {
35349cc55cSDimitry Andric   MoveFrom(std::move(info));
36349cc55cSDimitry Andric   return *this;
37349cc55cSDimitry Andric }
38349cc55cSDimitry Andric 
39349cc55cSDimitry Andric void DynamicRegisterInfo::MoveFrom(DynamicRegisterInfo &&info) {
40349cc55cSDimitry Andric   m_regs = std::move(info.m_regs);
41349cc55cSDimitry Andric   m_sets = std::move(info.m_sets);
42349cc55cSDimitry Andric   m_set_reg_nums = std::move(info.m_set_reg_nums);
43349cc55cSDimitry Andric   m_set_names = std::move(info.m_set_names);
44349cc55cSDimitry Andric   m_value_regs_map = std::move(info.m_value_regs_map);
45349cc55cSDimitry Andric   m_invalidate_regs_map = std::move(info.m_invalidate_regs_map);
46349cc55cSDimitry Andric 
47349cc55cSDimitry Andric   m_reg_data_byte_size = info.m_reg_data_byte_size;
48349cc55cSDimitry Andric   m_finalized = info.m_finalized;
49349cc55cSDimitry Andric 
50349cc55cSDimitry Andric   if (m_finalized) {
51349cc55cSDimitry Andric     const size_t num_sets = m_sets.size();
52349cc55cSDimitry Andric     for (size_t set = 0; set < num_sets; ++set)
53349cc55cSDimitry Andric       m_sets[set].registers = m_set_reg_nums[set].data();
54349cc55cSDimitry Andric   }
55349cc55cSDimitry Andric 
56349cc55cSDimitry Andric   info.Clear();
57349cc55cSDimitry Andric }
58349cc55cSDimitry Andric 
59349cc55cSDimitry Andric llvm::Expected<uint32_t> DynamicRegisterInfo::ByteOffsetFromSlice(
60349cc55cSDimitry Andric     uint32_t index, llvm::StringRef slice_str, lldb::ByteOrder byte_order) {
61349cc55cSDimitry Andric   // Slices use the following format:
62349cc55cSDimitry Andric   //  REGNAME[MSBIT:LSBIT]
63349cc55cSDimitry Andric   // REGNAME - name of the register to grab a slice of
64349cc55cSDimitry Andric   // MSBIT - the most significant bit at which the current register value
65349cc55cSDimitry Andric   // starts at
66349cc55cSDimitry Andric   // LSBIT - the least significant bit at which the current register value
67349cc55cSDimitry Andric   // ends at
68349cc55cSDimitry Andric   static llvm::Regex g_bitfield_regex(
69349cc55cSDimitry Andric       "([A-Za-z_][A-Za-z0-9_]*)\\[([0-9]+):([0-9]+)\\]");
70349cc55cSDimitry Andric   llvm::SmallVector<llvm::StringRef, 4> matches;
71349cc55cSDimitry Andric   if (!g_bitfield_regex.match(slice_str, &matches))
72349cc55cSDimitry Andric     return llvm::createStringError(
73349cc55cSDimitry Andric         llvm::inconvertibleErrorCode(),
74349cc55cSDimitry Andric         "failed to match against register bitfield regex (slice: %s)",
75349cc55cSDimitry Andric         slice_str.str().c_str());
76349cc55cSDimitry Andric 
77349cc55cSDimitry Andric   llvm::StringRef reg_name_str = matches[1];
78349cc55cSDimitry Andric   llvm::StringRef msbit_str = matches[2];
79349cc55cSDimitry Andric   llvm::StringRef lsbit_str = matches[3];
80349cc55cSDimitry Andric   uint32_t msbit;
81349cc55cSDimitry Andric   uint32_t lsbit;
82349cc55cSDimitry Andric   if (!llvm::to_integer(msbit_str, msbit) ||
83349cc55cSDimitry Andric       !llvm::to_integer(lsbit_str, lsbit))
84349cc55cSDimitry Andric     return llvm::createStringError(
85349cc55cSDimitry Andric         llvm::inconvertibleErrorCode(), "msbit (%s) or lsbit (%s) are invalid",
86349cc55cSDimitry Andric         msbit_str.str().c_str(), lsbit_str.str().c_str());
87349cc55cSDimitry Andric 
88349cc55cSDimitry Andric   if (msbit <= lsbit)
89349cc55cSDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
90349cc55cSDimitry Andric                                    "msbit (%u) must be greater than lsbit (%u)",
91349cc55cSDimitry Andric                                    msbit, lsbit);
92349cc55cSDimitry Andric 
93349cc55cSDimitry Andric   const uint32_t msbyte = msbit / 8;
94349cc55cSDimitry Andric   const uint32_t lsbyte = lsbit / 8;
95349cc55cSDimitry Andric 
96349cc55cSDimitry Andric   const RegisterInfo *containing_reg_info = GetRegisterInfo(reg_name_str);
97349cc55cSDimitry Andric   if (!containing_reg_info)
98349cc55cSDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
99349cc55cSDimitry Andric                                    "invalid concrete register \"%s\"",
100349cc55cSDimitry Andric                                    reg_name_str.str().c_str());
101349cc55cSDimitry Andric 
102349cc55cSDimitry Andric   const uint32_t max_bit = containing_reg_info->byte_size * 8;
103349cc55cSDimitry Andric 
104349cc55cSDimitry Andric   if (msbit > max_bit)
105349cc55cSDimitry Andric     return llvm::createStringError(
106349cc55cSDimitry Andric         llvm::inconvertibleErrorCode(),
107349cc55cSDimitry Andric         "msbit (%u) must be less than the bitsize of the register \"%s\" (%u)",
108349cc55cSDimitry Andric         msbit, reg_name_str.str().c_str(), max_bit);
109349cc55cSDimitry Andric   if (lsbit > max_bit)
110349cc55cSDimitry Andric     return llvm::createStringError(
111349cc55cSDimitry Andric         llvm::inconvertibleErrorCode(),
112349cc55cSDimitry Andric         "lsbit (%u) must be less than the bitsize of the register \"%s\" (%u)",
113349cc55cSDimitry Andric         lsbit, reg_name_str.str().c_str(), max_bit);
114349cc55cSDimitry Andric 
115349cc55cSDimitry Andric   m_invalidate_regs_map[containing_reg_info->kinds[eRegisterKindLLDB]]
116349cc55cSDimitry Andric       .push_back(index);
117349cc55cSDimitry Andric   m_value_regs_map[index].push_back(
118349cc55cSDimitry Andric       containing_reg_info->kinds[eRegisterKindLLDB]);
119349cc55cSDimitry Andric   m_invalidate_regs_map[index].push_back(
120349cc55cSDimitry Andric       containing_reg_info->kinds[eRegisterKindLLDB]);
121349cc55cSDimitry Andric 
122349cc55cSDimitry Andric   if (byte_order == eByteOrderLittle)
123349cc55cSDimitry Andric     return containing_reg_info->byte_offset + lsbyte;
124349cc55cSDimitry Andric   if (byte_order == eByteOrderBig)
125349cc55cSDimitry Andric     return containing_reg_info->byte_offset + msbyte;
126349cc55cSDimitry Andric   llvm_unreachable("Invalid byte order");
127349cc55cSDimitry Andric }
128349cc55cSDimitry Andric 
129349cc55cSDimitry Andric llvm::Expected<uint32_t> DynamicRegisterInfo::ByteOffsetFromComposite(
130349cc55cSDimitry Andric     uint32_t index, StructuredData::Array &composite_reg_list,
131349cc55cSDimitry Andric     lldb::ByteOrder byte_order) {
132349cc55cSDimitry Andric   const size_t num_composite_regs = composite_reg_list.GetSize();
133349cc55cSDimitry Andric   if (num_composite_regs == 0)
134349cc55cSDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
135349cc55cSDimitry Andric                                    "\"composite\" list is empty");
136349cc55cSDimitry Andric 
137349cc55cSDimitry Andric   uint32_t composite_offset = UINT32_MAX;
138349cc55cSDimitry Andric   for (uint32_t composite_idx = 0; composite_idx < num_composite_regs;
139349cc55cSDimitry Andric        ++composite_idx) {
140349cc55cSDimitry Andric     ConstString composite_reg_name;
141349cc55cSDimitry Andric     if (!composite_reg_list.GetItemAtIndexAsString(composite_idx,
142349cc55cSDimitry Andric                                                    composite_reg_name, nullptr))
143349cc55cSDimitry Andric       return llvm::createStringError(
144349cc55cSDimitry Andric           llvm::inconvertibleErrorCode(),
145349cc55cSDimitry Andric           "\"composite\" list value is not a Python string at index %d",
146349cc55cSDimitry Andric           composite_idx);
147349cc55cSDimitry Andric 
148349cc55cSDimitry Andric     const RegisterInfo *composite_reg_info =
149349cc55cSDimitry Andric         GetRegisterInfo(composite_reg_name.GetStringRef());
150349cc55cSDimitry Andric     if (!composite_reg_info)
151349cc55cSDimitry Andric       return llvm::createStringError(
152349cc55cSDimitry Andric           llvm::inconvertibleErrorCode(),
153349cc55cSDimitry Andric           "failed to find composite register by name: \"%s\"",
154349cc55cSDimitry Andric           composite_reg_name.GetCString());
155349cc55cSDimitry Andric 
156349cc55cSDimitry Andric     composite_offset =
157349cc55cSDimitry Andric         std::min(composite_offset, composite_reg_info->byte_offset);
158349cc55cSDimitry Andric     m_value_regs_map[index].push_back(
159349cc55cSDimitry Andric         composite_reg_info->kinds[eRegisterKindLLDB]);
160349cc55cSDimitry Andric     m_invalidate_regs_map[composite_reg_info->kinds[eRegisterKindLLDB]]
161349cc55cSDimitry Andric         .push_back(index);
162349cc55cSDimitry Andric     m_invalidate_regs_map[index].push_back(
163349cc55cSDimitry Andric         composite_reg_info->kinds[eRegisterKindLLDB]);
164349cc55cSDimitry Andric   }
165349cc55cSDimitry Andric 
166349cc55cSDimitry Andric   return composite_offset;
167349cc55cSDimitry Andric }
168349cc55cSDimitry Andric 
169349cc55cSDimitry Andric llvm::Expected<uint32_t> DynamicRegisterInfo::ByteOffsetFromRegInfoDict(
170349cc55cSDimitry Andric     uint32_t index, StructuredData::Dictionary &reg_info_dict,
171349cc55cSDimitry Andric     lldb::ByteOrder byte_order) {
172349cc55cSDimitry Andric   uint32_t byte_offset;
173349cc55cSDimitry Andric   if (reg_info_dict.GetValueForKeyAsInteger("offset", byte_offset))
174349cc55cSDimitry Andric     return byte_offset;
175349cc55cSDimitry Andric 
176349cc55cSDimitry Andric   // No offset for this register, see if the register has a value
177349cc55cSDimitry Andric   // expression which indicates this register is part of another register.
178349cc55cSDimitry Andric   // Value expressions are things like "rax[31:0]" which state that the
179349cc55cSDimitry Andric   // current register's value is in a concrete register "rax" in bits 31:0.
180349cc55cSDimitry Andric   // If there is a value expression we can calculate the offset
181349cc55cSDimitry Andric   llvm::StringRef slice_str;
182349cc55cSDimitry Andric   if (reg_info_dict.GetValueForKeyAsString("slice", slice_str, nullptr))
183349cc55cSDimitry Andric     return ByteOffsetFromSlice(index, slice_str, byte_order);
184349cc55cSDimitry Andric 
185349cc55cSDimitry Andric   StructuredData::Array *composite_reg_list;
186349cc55cSDimitry Andric   if (reg_info_dict.GetValueForKeyAsArray("composite", composite_reg_list))
187349cc55cSDimitry Andric     return ByteOffsetFromComposite(index, *composite_reg_list, byte_order);
188349cc55cSDimitry Andric 
189349cc55cSDimitry Andric   return llvm::createStringError(llvm::inconvertibleErrorCode(),
190349cc55cSDimitry Andric                                  "insufficient data to calculate byte offset");
191349cc55cSDimitry Andric }
192349cc55cSDimitry Andric 
193349cc55cSDimitry Andric size_t
194349cc55cSDimitry Andric DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
195349cc55cSDimitry Andric                                      const ArchSpec &arch) {
196*81ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
197349cc55cSDimitry Andric   assert(!m_finalized);
198349cc55cSDimitry Andric   StructuredData::Array *sets = nullptr;
199349cc55cSDimitry Andric   if (dict.GetValueForKeyAsArray("sets", sets)) {
200349cc55cSDimitry Andric     const uint32_t num_sets = sets->GetSize();
201349cc55cSDimitry Andric     for (uint32_t i = 0; i < num_sets; ++i) {
202349cc55cSDimitry Andric       ConstString set_name;
203349cc55cSDimitry Andric       if (sets->GetItemAtIndexAsString(i, set_name) && !set_name.IsEmpty()) {
204349cc55cSDimitry Andric         m_sets.push_back({set_name.AsCString(), nullptr, 0, nullptr});
205349cc55cSDimitry Andric       } else {
206349cc55cSDimitry Andric         Clear();
207349cc55cSDimitry Andric         printf("error: register sets must have valid names\n");
208349cc55cSDimitry Andric         return 0;
209349cc55cSDimitry Andric       }
210349cc55cSDimitry Andric     }
211349cc55cSDimitry Andric     m_set_reg_nums.resize(m_sets.size());
212349cc55cSDimitry Andric   }
213349cc55cSDimitry Andric 
214349cc55cSDimitry Andric   StructuredData::Array *regs = nullptr;
215349cc55cSDimitry Andric   if (!dict.GetValueForKeyAsArray("registers", regs))
216349cc55cSDimitry Andric     return 0;
217349cc55cSDimitry Andric 
218349cc55cSDimitry Andric   const ByteOrder byte_order = arch.GetByteOrder();
219349cc55cSDimitry Andric 
220349cc55cSDimitry Andric   const uint32_t num_regs = regs->GetSize();
221349cc55cSDimitry Andric   //        typedef std::map<std::string, std::vector<std::string> >
222349cc55cSDimitry Andric   //        InvalidateNameMap;
223349cc55cSDimitry Andric   //        InvalidateNameMap invalidate_map;
224349cc55cSDimitry Andric   for (uint32_t i = 0; i < num_regs; ++i) {
225349cc55cSDimitry Andric     StructuredData::Dictionary *reg_info_dict = nullptr;
226349cc55cSDimitry Andric     if (!regs->GetItemAtIndexAsDictionary(i, reg_info_dict)) {
227349cc55cSDimitry Andric       Clear();
228349cc55cSDimitry Andric       printf("error: items in the 'registers' array must be dictionaries\n");
229349cc55cSDimitry Andric       regs->DumpToStdout();
230349cc55cSDimitry Andric       return 0;
231349cc55cSDimitry Andric     }
232349cc55cSDimitry Andric 
233349cc55cSDimitry Andric     // { 'name':'rcx'       , 'bitsize' :  64, 'offset' :  16,
234349cc55cSDimitry Andric     // 'encoding':'uint' , 'format':'hex'         , 'set': 0, 'ehframe' : 2,
235349cc55cSDimitry Andric     // 'dwarf' : 2, 'generic':'arg4', 'alt-name':'arg4', },
236349cc55cSDimitry Andric     RegisterInfo reg_info;
237349cc55cSDimitry Andric     std::vector<uint32_t> value_regs;
238349cc55cSDimitry Andric     std::vector<uint32_t> invalidate_regs;
239349cc55cSDimitry Andric     memset(&reg_info, 0, sizeof(reg_info));
240349cc55cSDimitry Andric 
241349cc55cSDimitry Andric     ConstString name_val;
242349cc55cSDimitry Andric     ConstString alt_name_val;
243349cc55cSDimitry Andric     if (!reg_info_dict->GetValueForKeyAsString("name", name_val, nullptr)) {
244349cc55cSDimitry Andric       Clear();
245349cc55cSDimitry Andric       printf("error: registers must have valid names and offsets\n");
246349cc55cSDimitry Andric       reg_info_dict->DumpToStdout();
247349cc55cSDimitry Andric       return 0;
248349cc55cSDimitry Andric     }
249349cc55cSDimitry Andric     reg_info.name = name_val.GetCString();
250349cc55cSDimitry Andric     reg_info_dict->GetValueForKeyAsString("alt-name", alt_name_val, nullptr);
251349cc55cSDimitry Andric     reg_info.alt_name = alt_name_val.GetCString();
252349cc55cSDimitry Andric 
253349cc55cSDimitry Andric     llvm::Expected<uint32_t> byte_offset =
254349cc55cSDimitry Andric         ByteOffsetFromRegInfoDict(i, *reg_info_dict, byte_order);
255349cc55cSDimitry Andric     if (byte_offset)
256349cc55cSDimitry Andric       reg_info.byte_offset = byte_offset.get();
257349cc55cSDimitry Andric     else {
258349cc55cSDimitry Andric       LLDB_LOG_ERROR(log, byte_offset.takeError(),
259349cc55cSDimitry Andric                      "error while parsing register {1}: {0}", reg_info.name);
260349cc55cSDimitry Andric       Clear();
261349cc55cSDimitry Andric       reg_info_dict->DumpToStdout();
262349cc55cSDimitry Andric       return 0;
263349cc55cSDimitry Andric     }
264349cc55cSDimitry Andric 
265349cc55cSDimitry Andric     int64_t bitsize = 0;
266349cc55cSDimitry Andric     if (!reg_info_dict->GetValueForKeyAsInteger("bitsize", bitsize)) {
267349cc55cSDimitry Andric       Clear();
268349cc55cSDimitry Andric       printf("error: invalid or missing 'bitsize' key/value pair in register "
269349cc55cSDimitry Andric              "dictionary\n");
270349cc55cSDimitry Andric       reg_info_dict->DumpToStdout();
271349cc55cSDimitry Andric       return 0;
272349cc55cSDimitry Andric     }
273349cc55cSDimitry Andric 
274349cc55cSDimitry Andric     reg_info.byte_size = bitsize / 8;
275349cc55cSDimitry Andric 
276349cc55cSDimitry Andric     llvm::StringRef format_str;
277349cc55cSDimitry Andric     if (reg_info_dict->GetValueForKeyAsString("format", format_str, nullptr)) {
278349cc55cSDimitry Andric       if (OptionArgParser::ToFormat(format_str.str().c_str(), reg_info.format,
279349cc55cSDimitry Andric                                     nullptr)
280349cc55cSDimitry Andric               .Fail()) {
281349cc55cSDimitry Andric         Clear();
282349cc55cSDimitry Andric         printf("error: invalid 'format' value in register dictionary\n");
283349cc55cSDimitry Andric         reg_info_dict->DumpToStdout();
284349cc55cSDimitry Andric         return 0;
285349cc55cSDimitry Andric       }
286349cc55cSDimitry Andric     } else {
287349cc55cSDimitry Andric       reg_info_dict->GetValueForKeyAsInteger("format", reg_info.format,
288349cc55cSDimitry Andric                                              eFormatHex);
289349cc55cSDimitry Andric     }
290349cc55cSDimitry Andric 
291349cc55cSDimitry Andric     llvm::StringRef encoding_str;
292349cc55cSDimitry Andric     if (reg_info_dict->GetValueForKeyAsString("encoding", encoding_str))
293349cc55cSDimitry Andric       reg_info.encoding = Args::StringToEncoding(encoding_str, eEncodingUint);
294349cc55cSDimitry Andric     else
295349cc55cSDimitry Andric       reg_info_dict->GetValueForKeyAsInteger("encoding", reg_info.encoding,
296349cc55cSDimitry Andric                                              eEncodingUint);
297349cc55cSDimitry Andric 
298349cc55cSDimitry Andric     size_t set = 0;
299349cc55cSDimitry Andric     if (!reg_info_dict->GetValueForKeyAsInteger<size_t>("set", set, -1) ||
300349cc55cSDimitry Andric         set >= m_sets.size()) {
301349cc55cSDimitry Andric       Clear();
302349cc55cSDimitry Andric       printf("error: invalid 'set' value in register dictionary, valid values "
303349cc55cSDimitry Andric              "are 0 - %i\n",
304349cc55cSDimitry Andric              (int)set);
305349cc55cSDimitry Andric       reg_info_dict->DumpToStdout();
306349cc55cSDimitry Andric       return 0;
307349cc55cSDimitry Andric     }
308349cc55cSDimitry Andric 
309349cc55cSDimitry Andric     // Fill in the register numbers
310349cc55cSDimitry Andric     reg_info.kinds[lldb::eRegisterKindLLDB] = i;
311349cc55cSDimitry Andric     reg_info.kinds[lldb::eRegisterKindProcessPlugin] = i;
312349cc55cSDimitry Andric     uint32_t eh_frame_regno = LLDB_INVALID_REGNUM;
313349cc55cSDimitry Andric     reg_info_dict->GetValueForKeyAsInteger("gcc", eh_frame_regno,
314349cc55cSDimitry Andric                                            LLDB_INVALID_REGNUM);
315349cc55cSDimitry Andric     if (eh_frame_regno == LLDB_INVALID_REGNUM)
316349cc55cSDimitry Andric       reg_info_dict->GetValueForKeyAsInteger("ehframe", eh_frame_regno,
317349cc55cSDimitry Andric                                              LLDB_INVALID_REGNUM);
318349cc55cSDimitry Andric     reg_info.kinds[lldb::eRegisterKindEHFrame] = eh_frame_regno;
319349cc55cSDimitry Andric     reg_info_dict->GetValueForKeyAsInteger(
320349cc55cSDimitry Andric         "dwarf", reg_info.kinds[lldb::eRegisterKindDWARF], LLDB_INVALID_REGNUM);
321349cc55cSDimitry Andric     llvm::StringRef generic_str;
322349cc55cSDimitry Andric     if (reg_info_dict->GetValueForKeyAsString("generic", generic_str))
323349cc55cSDimitry Andric       reg_info.kinds[lldb::eRegisterKindGeneric] =
324349cc55cSDimitry Andric           Args::StringToGenericRegister(generic_str);
325349cc55cSDimitry Andric     else
326349cc55cSDimitry Andric       reg_info_dict->GetValueForKeyAsInteger(
327349cc55cSDimitry Andric           "generic", reg_info.kinds[lldb::eRegisterKindGeneric],
328349cc55cSDimitry Andric           LLDB_INVALID_REGNUM);
329349cc55cSDimitry Andric 
330349cc55cSDimitry Andric     // Check if this register invalidates any other register values when it is
331349cc55cSDimitry Andric     // modified
332349cc55cSDimitry Andric     StructuredData::Array *invalidate_reg_list = nullptr;
333349cc55cSDimitry Andric     if (reg_info_dict->GetValueForKeyAsArray("invalidate-regs",
334349cc55cSDimitry Andric                                              invalidate_reg_list)) {
335349cc55cSDimitry Andric       const size_t num_regs = invalidate_reg_list->GetSize();
336349cc55cSDimitry Andric       if (num_regs > 0) {
337349cc55cSDimitry Andric         for (uint32_t idx = 0; idx < num_regs; ++idx) {
338349cc55cSDimitry Andric           ConstString invalidate_reg_name;
339349cc55cSDimitry Andric           uint64_t invalidate_reg_num;
340349cc55cSDimitry Andric           if (invalidate_reg_list->GetItemAtIndexAsString(
341349cc55cSDimitry Andric                   idx, invalidate_reg_name)) {
342349cc55cSDimitry Andric             const RegisterInfo *invalidate_reg_info =
343349cc55cSDimitry Andric                 GetRegisterInfo(invalidate_reg_name.GetStringRef());
344349cc55cSDimitry Andric             if (invalidate_reg_info) {
345349cc55cSDimitry Andric               m_invalidate_regs_map[i].push_back(
346349cc55cSDimitry Andric                   invalidate_reg_info->kinds[eRegisterKindLLDB]);
347349cc55cSDimitry Andric             } else {
348349cc55cSDimitry Andric               // TODO: print error invalid slice string that doesn't follow the
349349cc55cSDimitry Andric               // format
350349cc55cSDimitry Andric               printf("error: failed to find a 'invalidate-regs' register for "
351349cc55cSDimitry Andric                      "\"%s\" while parsing register \"%s\"\n",
352349cc55cSDimitry Andric                      invalidate_reg_name.GetCString(), reg_info.name);
353349cc55cSDimitry Andric             }
354349cc55cSDimitry Andric           } else if (invalidate_reg_list->GetItemAtIndexAsInteger(
355349cc55cSDimitry Andric                          idx, invalidate_reg_num)) {
356349cc55cSDimitry Andric             if (invalidate_reg_num != UINT64_MAX)
357349cc55cSDimitry Andric               m_invalidate_regs_map[i].push_back(invalidate_reg_num);
358349cc55cSDimitry Andric             else
359349cc55cSDimitry Andric               printf("error: 'invalidate-regs' list value wasn't a valid "
360349cc55cSDimitry Andric                      "integer\n");
361349cc55cSDimitry Andric           } else {
362349cc55cSDimitry Andric             printf("error: 'invalidate-regs' list value wasn't a python string "
363349cc55cSDimitry Andric                    "or integer\n");
364349cc55cSDimitry Andric           }
365349cc55cSDimitry Andric         }
366349cc55cSDimitry Andric       } else {
367349cc55cSDimitry Andric         printf("error: 'invalidate-regs' contained an empty list\n");
368349cc55cSDimitry Andric       }
369349cc55cSDimitry Andric     }
370349cc55cSDimitry Andric 
371349cc55cSDimitry Andric     // Calculate the register offset
372349cc55cSDimitry Andric     const size_t end_reg_offset = reg_info.byte_offset + reg_info.byte_size;
373349cc55cSDimitry Andric     if (m_reg_data_byte_size < end_reg_offset)
374349cc55cSDimitry Andric       m_reg_data_byte_size = end_reg_offset;
375349cc55cSDimitry Andric 
376349cc55cSDimitry Andric     m_regs.push_back(reg_info);
377349cc55cSDimitry Andric     m_set_reg_nums[set].push_back(i);
378349cc55cSDimitry Andric   }
379349cc55cSDimitry Andric   Finalize(arch);
380349cc55cSDimitry Andric   return m_regs.size();
381349cc55cSDimitry Andric }
382349cc55cSDimitry Andric 
383349cc55cSDimitry Andric size_t DynamicRegisterInfo::SetRegisterInfo(
384349cc55cSDimitry Andric     std::vector<DynamicRegisterInfo::Register> &&regs,
385349cc55cSDimitry Andric     const ArchSpec &arch) {
386349cc55cSDimitry Andric   assert(!m_finalized);
387349cc55cSDimitry Andric 
388349cc55cSDimitry Andric   for (auto it : llvm::enumerate(regs)) {
389349cc55cSDimitry Andric     uint32_t local_regnum = it.index();
390349cc55cSDimitry Andric     const DynamicRegisterInfo::Register &reg = it.value();
391349cc55cSDimitry Andric 
392349cc55cSDimitry Andric     assert(reg.name);
393349cc55cSDimitry Andric     assert(reg.set_name);
394349cc55cSDimitry Andric 
395349cc55cSDimitry Andric     if (!reg.value_regs.empty())
396349cc55cSDimitry Andric       m_value_regs_map[local_regnum] = std::move(reg.value_regs);
397349cc55cSDimitry Andric     if (!reg.invalidate_regs.empty())
398349cc55cSDimitry Andric       m_invalidate_regs_map[local_regnum] = std::move(reg.invalidate_regs);
399349cc55cSDimitry Andric     if (reg.value_reg_offset != 0) {
400349cc55cSDimitry Andric       assert(reg.value_regs.size() == 1);
401349cc55cSDimitry Andric       m_value_reg_offset_map[local_regnum] = reg.value_reg_offset;
402349cc55cSDimitry Andric     }
403349cc55cSDimitry Andric 
404349cc55cSDimitry Andric     struct RegisterInfo reg_info {
405349cc55cSDimitry Andric       reg.name.AsCString(), reg.alt_name.AsCString(), reg.byte_size,
406349cc55cSDimitry Andric           reg.byte_offset, reg.encoding, reg.format,
407349cc55cSDimitry Andric           {reg.regnum_ehframe, reg.regnum_dwarf, reg.regnum_generic,
408349cc55cSDimitry Andric            reg.regnum_remote, local_regnum},
409349cc55cSDimitry Andric           // value_regs and invalidate_regs are filled by Finalize()
410349cc55cSDimitry Andric           nullptr, nullptr
411349cc55cSDimitry Andric     };
412349cc55cSDimitry Andric 
413349cc55cSDimitry Andric     m_regs.push_back(reg_info);
414349cc55cSDimitry Andric 
415349cc55cSDimitry Andric     uint32_t set = GetRegisterSetIndexByName(reg.set_name, true);
416349cc55cSDimitry Andric     assert(set < m_sets.size());
417349cc55cSDimitry Andric     assert(set < m_set_reg_nums.size());
418349cc55cSDimitry Andric     assert(set < m_set_names.size());
419349cc55cSDimitry Andric     m_set_reg_nums[set].push_back(local_regnum);
420349cc55cSDimitry Andric   };
421349cc55cSDimitry Andric 
422349cc55cSDimitry Andric   Finalize(arch);
423349cc55cSDimitry Andric   return m_regs.size();
424349cc55cSDimitry Andric }
425349cc55cSDimitry Andric 
426349cc55cSDimitry Andric void DynamicRegisterInfo::Finalize(const ArchSpec &arch) {
427349cc55cSDimitry Andric   if (m_finalized)
428349cc55cSDimitry Andric     return;
429349cc55cSDimitry Andric 
430349cc55cSDimitry Andric   m_finalized = true;
431349cc55cSDimitry Andric   const size_t num_sets = m_sets.size();
432349cc55cSDimitry Andric   for (size_t set = 0; set < num_sets; ++set) {
433349cc55cSDimitry Andric     assert(m_sets.size() == m_set_reg_nums.size());
434349cc55cSDimitry Andric     m_sets[set].num_registers = m_set_reg_nums[set].size();
435349cc55cSDimitry Andric     m_sets[set].registers = m_set_reg_nums[set].data();
436349cc55cSDimitry Andric   }
437349cc55cSDimitry Andric 
438349cc55cSDimitry Andric   // make sure value_regs are terminated with LLDB_INVALID_REGNUM
439349cc55cSDimitry Andric 
440349cc55cSDimitry Andric   for (reg_to_regs_map::iterator pos = m_value_regs_map.begin(),
441349cc55cSDimitry Andric                                  end = m_value_regs_map.end();
442349cc55cSDimitry Andric        pos != end; ++pos) {
443349cc55cSDimitry Andric     if (pos->second.back() != LLDB_INVALID_REGNUM)
444349cc55cSDimitry Andric       pos->second.push_back(LLDB_INVALID_REGNUM);
445349cc55cSDimitry Andric   }
446349cc55cSDimitry Andric 
447349cc55cSDimitry Andric   // Now update all value_regs with each register info as needed
448349cc55cSDimitry Andric   const size_t num_regs = m_regs.size();
449349cc55cSDimitry Andric   for (size_t i = 0; i < num_regs; ++i) {
450349cc55cSDimitry Andric     if (m_value_regs_map.find(i) != m_value_regs_map.end())
451349cc55cSDimitry Andric       m_regs[i].value_regs = m_value_regs_map[i].data();
452349cc55cSDimitry Andric     else
453349cc55cSDimitry Andric       m_regs[i].value_regs = nullptr;
454349cc55cSDimitry Andric   }
455349cc55cSDimitry Andric 
456349cc55cSDimitry Andric   // Expand all invalidation dependencies
457349cc55cSDimitry Andric   for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(),
458349cc55cSDimitry Andric                                  end = m_invalidate_regs_map.end();
459349cc55cSDimitry Andric        pos != end; ++pos) {
460349cc55cSDimitry Andric     const uint32_t reg_num = pos->first;
461349cc55cSDimitry Andric 
462349cc55cSDimitry Andric     if (m_regs[reg_num].value_regs) {
463349cc55cSDimitry Andric       reg_num_collection extra_invalid_regs;
464349cc55cSDimitry Andric       for (const uint32_t invalidate_reg_num : pos->second) {
465349cc55cSDimitry Andric         reg_to_regs_map::iterator invalidate_pos =
466349cc55cSDimitry Andric             m_invalidate_regs_map.find(invalidate_reg_num);
467349cc55cSDimitry Andric         if (invalidate_pos != m_invalidate_regs_map.end()) {
468349cc55cSDimitry Andric           for (const uint32_t concrete_invalidate_reg_num :
469349cc55cSDimitry Andric                invalidate_pos->second) {
470349cc55cSDimitry Andric             if (concrete_invalidate_reg_num != reg_num)
471349cc55cSDimitry Andric               extra_invalid_regs.push_back(concrete_invalidate_reg_num);
472349cc55cSDimitry Andric           }
473349cc55cSDimitry Andric         }
474349cc55cSDimitry Andric       }
475349cc55cSDimitry Andric       pos->second.insert(pos->second.end(), extra_invalid_regs.begin(),
476349cc55cSDimitry Andric                          extra_invalid_regs.end());
477349cc55cSDimitry Andric     }
478349cc55cSDimitry Andric   }
479349cc55cSDimitry Andric 
480349cc55cSDimitry Andric   // sort and unique all invalidate registers and make sure each is terminated
481349cc55cSDimitry Andric   // with LLDB_INVALID_REGNUM
482349cc55cSDimitry Andric   for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(),
483349cc55cSDimitry Andric                                  end = m_invalidate_regs_map.end();
484349cc55cSDimitry Andric        pos != end; ++pos) {
485349cc55cSDimitry Andric     if (pos->second.size() > 1) {
486349cc55cSDimitry Andric       llvm::sort(pos->second.begin(), pos->second.end());
487349cc55cSDimitry Andric       reg_num_collection::iterator unique_end =
488349cc55cSDimitry Andric           std::unique(pos->second.begin(), pos->second.end());
489349cc55cSDimitry Andric       if (unique_end != pos->second.end())
490349cc55cSDimitry Andric         pos->second.erase(unique_end, pos->second.end());
491349cc55cSDimitry Andric     }
492349cc55cSDimitry Andric     assert(!pos->second.empty());
493349cc55cSDimitry Andric     if (pos->second.back() != LLDB_INVALID_REGNUM)
494349cc55cSDimitry Andric       pos->second.push_back(LLDB_INVALID_REGNUM);
495349cc55cSDimitry Andric   }
496349cc55cSDimitry Andric 
497349cc55cSDimitry Andric   // Now update all invalidate_regs with each register info as needed
498349cc55cSDimitry Andric   for (size_t i = 0; i < num_regs; ++i) {
499349cc55cSDimitry Andric     if (m_invalidate_regs_map.find(i) != m_invalidate_regs_map.end())
500349cc55cSDimitry Andric       m_regs[i].invalidate_regs = m_invalidate_regs_map[i].data();
501349cc55cSDimitry Andric     else
502349cc55cSDimitry Andric       m_regs[i].invalidate_regs = nullptr;
503349cc55cSDimitry Andric   }
504349cc55cSDimitry Andric 
505349cc55cSDimitry Andric   // Check if we need to automatically set the generic registers in case they
506349cc55cSDimitry Andric   // weren't set
507349cc55cSDimitry Andric   bool generic_regs_specified = false;
508349cc55cSDimitry Andric   for (const auto &reg : m_regs) {
509349cc55cSDimitry Andric     if (reg.kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM) {
510349cc55cSDimitry Andric       generic_regs_specified = true;
511349cc55cSDimitry Andric       break;
512349cc55cSDimitry Andric     }
513349cc55cSDimitry Andric   }
514349cc55cSDimitry Andric 
515349cc55cSDimitry Andric   if (!generic_regs_specified) {
516349cc55cSDimitry Andric     switch (arch.GetMachine()) {
517349cc55cSDimitry Andric     case llvm::Triple::aarch64:
518349cc55cSDimitry Andric     case llvm::Triple::aarch64_32:
519349cc55cSDimitry Andric     case llvm::Triple::aarch64_be:
520349cc55cSDimitry Andric       for (auto &reg : m_regs) {
521349cc55cSDimitry Andric         if (strcmp(reg.name, "pc") == 0)
522349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
523349cc55cSDimitry Andric         else if ((strcmp(reg.name, "fp") == 0) ||
524349cc55cSDimitry Andric                  (strcmp(reg.name, "x29") == 0))
525349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
526349cc55cSDimitry Andric         else if ((strcmp(reg.name, "lr") == 0) ||
527349cc55cSDimitry Andric                  (strcmp(reg.name, "x30") == 0))
528349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA;
529349cc55cSDimitry Andric         else if ((strcmp(reg.name, "sp") == 0) ||
530349cc55cSDimitry Andric                  (strcmp(reg.name, "x31") == 0))
531349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
532349cc55cSDimitry Andric         else if (strcmp(reg.name, "cpsr") == 0)
533349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
534349cc55cSDimitry Andric       }
535349cc55cSDimitry Andric       break;
536349cc55cSDimitry Andric 
537349cc55cSDimitry Andric     case llvm::Triple::arm:
538349cc55cSDimitry Andric     case llvm::Triple::armeb:
539349cc55cSDimitry Andric     case llvm::Triple::thumb:
540349cc55cSDimitry Andric     case llvm::Triple::thumbeb:
541349cc55cSDimitry Andric       for (auto &reg : m_regs) {
542349cc55cSDimitry Andric         if ((strcmp(reg.name, "pc") == 0) || (strcmp(reg.name, "r15") == 0))
543349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
544349cc55cSDimitry Andric         else if ((strcmp(reg.name, "sp") == 0) ||
545349cc55cSDimitry Andric                  (strcmp(reg.name, "r13") == 0))
546349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
547349cc55cSDimitry Andric         else if ((strcmp(reg.name, "lr") == 0) ||
548349cc55cSDimitry Andric                  (strcmp(reg.name, "r14") == 0))
549349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA;
550349cc55cSDimitry Andric         else if ((strcmp(reg.name, "r7") == 0) &&
551349cc55cSDimitry Andric                  arch.GetTriple().getVendor() == llvm::Triple::Apple)
552349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
553349cc55cSDimitry Andric         else if ((strcmp(reg.name, "r11") == 0) &&
554349cc55cSDimitry Andric                  arch.GetTriple().getVendor() != llvm::Triple::Apple)
555349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
556349cc55cSDimitry Andric         else if (strcmp(reg.name, "fp") == 0)
557349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
558349cc55cSDimitry Andric         else if (strcmp(reg.name, "cpsr") == 0)
559349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
560349cc55cSDimitry Andric       }
561349cc55cSDimitry Andric       break;
562349cc55cSDimitry Andric 
563349cc55cSDimitry Andric     case llvm::Triple::x86:
564349cc55cSDimitry Andric       for (auto &reg : m_regs) {
565349cc55cSDimitry Andric         if ((strcmp(reg.name, "eip") == 0) || (strcmp(reg.name, "pc") == 0))
566349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
567349cc55cSDimitry Andric         else if ((strcmp(reg.name, "esp") == 0) ||
568349cc55cSDimitry Andric                  (strcmp(reg.name, "sp") == 0))
569349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
570349cc55cSDimitry Andric         else if ((strcmp(reg.name, "ebp") == 0) ||
571349cc55cSDimitry Andric                  (strcmp(reg.name, "fp") == 0))
572349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
573349cc55cSDimitry Andric         else if ((strcmp(reg.name, "eflags") == 0) ||
574349cc55cSDimitry Andric                  (strcmp(reg.name, "flags") == 0))
575349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
576349cc55cSDimitry Andric       }
577349cc55cSDimitry Andric       break;
578349cc55cSDimitry Andric 
579349cc55cSDimitry Andric     case llvm::Triple::x86_64:
580349cc55cSDimitry Andric       for (auto &reg : m_regs) {
581349cc55cSDimitry Andric         if ((strcmp(reg.name, "rip") == 0) || (strcmp(reg.name, "pc") == 0))
582349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
583349cc55cSDimitry Andric         else if ((strcmp(reg.name, "rsp") == 0) ||
584349cc55cSDimitry Andric                  (strcmp(reg.name, "sp") == 0))
585349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
586349cc55cSDimitry Andric         else if ((strcmp(reg.name, "rbp") == 0) ||
587349cc55cSDimitry Andric                  (strcmp(reg.name, "fp") == 0))
588349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
589349cc55cSDimitry Andric         else if ((strcmp(reg.name, "rflags") == 0) ||
590349cc55cSDimitry Andric                  (strcmp(reg.name, "eflags") == 0) ||
591349cc55cSDimitry Andric                  (strcmp(reg.name, "flags") == 0))
592349cc55cSDimitry Andric           reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
593349cc55cSDimitry Andric       }
594349cc55cSDimitry Andric       break;
595349cc55cSDimitry Andric 
596349cc55cSDimitry Andric     default:
597349cc55cSDimitry Andric       break;
598349cc55cSDimitry Andric     }
599349cc55cSDimitry Andric   }
600349cc55cSDimitry Andric 
601349cc55cSDimitry Andric   // At this stage call ConfigureOffsets to calculate register offsets for
602349cc55cSDimitry Andric   // targets supporting dynamic offset calculation. It also calculates
603349cc55cSDimitry Andric   // total byte size of register data.
604349cc55cSDimitry Andric   ConfigureOffsets();
605349cc55cSDimitry Andric 
606349cc55cSDimitry Andric   // Check if register info is reconfigurable
607349cc55cSDimitry Andric   // AArch64 SVE register set has configurable register sizes
608349cc55cSDimitry Andric   if (arch.GetTriple().isAArch64()) {
609349cc55cSDimitry Andric     for (const auto &reg : m_regs) {
610349cc55cSDimitry Andric       if (strcmp(reg.name, "vg") == 0) {
611349cc55cSDimitry Andric         m_is_reconfigurable = true;
612349cc55cSDimitry Andric         break;
613349cc55cSDimitry Andric       }
614349cc55cSDimitry Andric     }
615349cc55cSDimitry Andric   }
616349cc55cSDimitry Andric }
617349cc55cSDimitry Andric 
618349cc55cSDimitry Andric void DynamicRegisterInfo::ConfigureOffsets() {
619349cc55cSDimitry Andric   // We are going to create a map between remote (eRegisterKindProcessPlugin)
620349cc55cSDimitry Andric   // and local (eRegisterKindLLDB) register numbers. This map will give us
621349cc55cSDimitry Andric   // remote register numbers in increasing order for offset calculation.
622349cc55cSDimitry Andric   std::map<uint32_t, uint32_t> remote_to_local_regnum_map;
623349cc55cSDimitry Andric   for (const auto &reg : m_regs)
624349cc55cSDimitry Andric     remote_to_local_regnum_map[reg.kinds[eRegisterKindProcessPlugin]] =
625349cc55cSDimitry Andric         reg.kinds[eRegisterKindLLDB];
626349cc55cSDimitry Andric 
627349cc55cSDimitry Andric   // At this stage we manually calculate g/G packet offsets of all primary
628349cc55cSDimitry Andric   // registers, only if target XML or qRegisterInfo packet did not send
629349cc55cSDimitry Andric   // an offset explicitly.
630349cc55cSDimitry Andric   uint32_t reg_offset = 0;
631349cc55cSDimitry Andric   for (auto const &regnum_pair : remote_to_local_regnum_map) {
632349cc55cSDimitry Andric     if (m_regs[regnum_pair.second].byte_offset == LLDB_INVALID_INDEX32 &&
633349cc55cSDimitry Andric         m_regs[regnum_pair.second].value_regs == nullptr) {
634349cc55cSDimitry Andric       m_regs[regnum_pair.second].byte_offset = reg_offset;
635349cc55cSDimitry Andric 
636349cc55cSDimitry Andric       reg_offset = m_regs[regnum_pair.second].byte_offset +
637349cc55cSDimitry Andric                    m_regs[regnum_pair.second].byte_size;
638349cc55cSDimitry Andric     }
639349cc55cSDimitry Andric   }
640349cc55cSDimitry Andric 
641349cc55cSDimitry Andric   // Now update all value_regs with each register info as needed
642349cc55cSDimitry Andric   for (auto &reg : m_regs) {
643349cc55cSDimitry Andric     if (reg.value_regs != nullptr) {
644349cc55cSDimitry Andric       // Assign a valid offset to all pseudo registers that have only a single
645349cc55cSDimitry Andric       // parent register in value_regs list, if not assigned by stub.  Pseudo
646349cc55cSDimitry Andric       // registers with value_regs list populated will share same offset as
647349cc55cSDimitry Andric       // that of their corresponding parent register.
648349cc55cSDimitry Andric       if (reg.byte_offset == LLDB_INVALID_INDEX32) {
649349cc55cSDimitry Andric         uint32_t value_regnum = reg.value_regs[0];
650349cc55cSDimitry Andric         if (value_regnum != LLDB_INVALID_INDEX32 &&
651349cc55cSDimitry Andric             reg.value_regs[1] == LLDB_INVALID_INDEX32) {
652349cc55cSDimitry Andric           reg.byte_offset =
653349cc55cSDimitry Andric               GetRegisterInfoAtIndex(value_regnum)->byte_offset;
654349cc55cSDimitry Andric           auto it = m_value_reg_offset_map.find(reg.kinds[eRegisterKindLLDB]);
655349cc55cSDimitry Andric           if (it != m_value_reg_offset_map.end())
656349cc55cSDimitry Andric             reg.byte_offset += it->second;
657349cc55cSDimitry Andric         }
658349cc55cSDimitry Andric       }
659349cc55cSDimitry Andric     }
660349cc55cSDimitry Andric 
661349cc55cSDimitry Andric     reg_offset = reg.byte_offset + reg.byte_size;
662349cc55cSDimitry Andric     if (m_reg_data_byte_size < reg_offset)
663349cc55cSDimitry Andric       m_reg_data_byte_size = reg_offset;
664349cc55cSDimitry Andric   }
665349cc55cSDimitry Andric }
666349cc55cSDimitry Andric 
667349cc55cSDimitry Andric bool DynamicRegisterInfo::IsReconfigurable() { return m_is_reconfigurable; }
668349cc55cSDimitry Andric 
669349cc55cSDimitry Andric size_t DynamicRegisterInfo::GetNumRegisters() const { return m_regs.size(); }
670349cc55cSDimitry Andric 
671349cc55cSDimitry Andric size_t DynamicRegisterInfo::GetNumRegisterSets() const { return m_sets.size(); }
672349cc55cSDimitry Andric 
673349cc55cSDimitry Andric size_t DynamicRegisterInfo::GetRegisterDataByteSize() const {
674349cc55cSDimitry Andric   return m_reg_data_byte_size;
675349cc55cSDimitry Andric }
676349cc55cSDimitry Andric 
677349cc55cSDimitry Andric const RegisterInfo *
678349cc55cSDimitry Andric DynamicRegisterInfo::GetRegisterInfoAtIndex(uint32_t i) const {
679349cc55cSDimitry Andric   if (i < m_regs.size())
680349cc55cSDimitry Andric     return &m_regs[i];
681349cc55cSDimitry Andric   return nullptr;
682349cc55cSDimitry Andric }
683349cc55cSDimitry Andric 
684349cc55cSDimitry Andric const RegisterInfo *DynamicRegisterInfo::GetRegisterInfo(uint32_t kind,
685349cc55cSDimitry Andric                                                          uint32_t num) const {
686349cc55cSDimitry Andric   uint32_t reg_index = ConvertRegisterKindToRegisterNumber(kind, num);
687349cc55cSDimitry Andric   if (reg_index != LLDB_INVALID_REGNUM)
688349cc55cSDimitry Andric     return &m_regs[reg_index];
689349cc55cSDimitry Andric   return nullptr;
690349cc55cSDimitry Andric }
691349cc55cSDimitry Andric 
692349cc55cSDimitry Andric const RegisterSet *DynamicRegisterInfo::GetRegisterSet(uint32_t i) const {
693349cc55cSDimitry Andric   if (i < m_sets.size())
694349cc55cSDimitry Andric     return &m_sets[i];
695349cc55cSDimitry Andric   return nullptr;
696349cc55cSDimitry Andric }
697349cc55cSDimitry Andric 
698349cc55cSDimitry Andric uint32_t
699349cc55cSDimitry Andric DynamicRegisterInfo::GetRegisterSetIndexByName(const ConstString &set_name,
700349cc55cSDimitry Andric                                                bool can_create) {
701349cc55cSDimitry Andric   name_collection::iterator pos, end = m_set_names.end();
702349cc55cSDimitry Andric   for (pos = m_set_names.begin(); pos != end; ++pos) {
703349cc55cSDimitry Andric     if (*pos == set_name)
704349cc55cSDimitry Andric       return std::distance(m_set_names.begin(), pos);
705349cc55cSDimitry Andric   }
706349cc55cSDimitry Andric 
707349cc55cSDimitry Andric   m_set_names.push_back(set_name);
708349cc55cSDimitry Andric   m_set_reg_nums.resize(m_set_reg_nums.size() + 1);
709349cc55cSDimitry Andric   RegisterSet new_set = {set_name.AsCString(), nullptr, 0, nullptr};
710349cc55cSDimitry Andric   m_sets.push_back(new_set);
711349cc55cSDimitry Andric   return m_sets.size() - 1;
712349cc55cSDimitry Andric }
713349cc55cSDimitry Andric 
714349cc55cSDimitry Andric uint32_t
715349cc55cSDimitry Andric DynamicRegisterInfo::ConvertRegisterKindToRegisterNumber(uint32_t kind,
716349cc55cSDimitry Andric                                                          uint32_t num) const {
717349cc55cSDimitry Andric   reg_collection::const_iterator pos, end = m_regs.end();
718349cc55cSDimitry Andric   for (pos = m_regs.begin(); pos != end; ++pos) {
719349cc55cSDimitry Andric     if (pos->kinds[kind] == num)
720349cc55cSDimitry Andric       return std::distance(m_regs.begin(), pos);
721349cc55cSDimitry Andric   }
722349cc55cSDimitry Andric 
723349cc55cSDimitry Andric   return LLDB_INVALID_REGNUM;
724349cc55cSDimitry Andric }
725349cc55cSDimitry Andric 
726349cc55cSDimitry Andric void DynamicRegisterInfo::Clear() {
727349cc55cSDimitry Andric   m_regs.clear();
728349cc55cSDimitry Andric   m_sets.clear();
729349cc55cSDimitry Andric   m_set_reg_nums.clear();
730349cc55cSDimitry Andric   m_set_names.clear();
731349cc55cSDimitry Andric   m_value_regs_map.clear();
732349cc55cSDimitry Andric   m_invalidate_regs_map.clear();
733349cc55cSDimitry Andric   m_reg_data_byte_size = 0;
734349cc55cSDimitry Andric   m_finalized = false;
735349cc55cSDimitry Andric }
736349cc55cSDimitry Andric 
737349cc55cSDimitry Andric void DynamicRegisterInfo::Dump() const {
738349cc55cSDimitry Andric   StreamFile s(stdout, false);
739349cc55cSDimitry Andric   const size_t num_regs = m_regs.size();
740349cc55cSDimitry Andric   s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " registers:\n",
741349cc55cSDimitry Andric            static_cast<const void *>(this), static_cast<uint64_t>(num_regs));
742349cc55cSDimitry Andric   for (size_t i = 0; i < num_regs; ++i) {
743349cc55cSDimitry Andric     s.Printf("[%3" PRIu64 "] name = %-10s", (uint64_t)i, m_regs[i].name);
744349cc55cSDimitry Andric     s.Printf(", size = %2u, offset = %4u, encoding = %u, format = %-10s",
745349cc55cSDimitry Andric              m_regs[i].byte_size, m_regs[i].byte_offset, m_regs[i].encoding,
746349cc55cSDimitry Andric              FormatManager::GetFormatAsCString(m_regs[i].format));
747349cc55cSDimitry Andric     if (m_regs[i].kinds[eRegisterKindProcessPlugin] != LLDB_INVALID_REGNUM)
748349cc55cSDimitry Andric       s.Printf(", process plugin = %3u",
749349cc55cSDimitry Andric                m_regs[i].kinds[eRegisterKindProcessPlugin]);
750349cc55cSDimitry Andric     if (m_regs[i].kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM)
751349cc55cSDimitry Andric       s.Printf(", dwarf = %3u", m_regs[i].kinds[eRegisterKindDWARF]);
752349cc55cSDimitry Andric     if (m_regs[i].kinds[eRegisterKindEHFrame] != LLDB_INVALID_REGNUM)
753349cc55cSDimitry Andric       s.Printf(", ehframe = %3u", m_regs[i].kinds[eRegisterKindEHFrame]);
754349cc55cSDimitry Andric     if (m_regs[i].kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM)
755349cc55cSDimitry Andric       s.Printf(", generic = %3u", m_regs[i].kinds[eRegisterKindGeneric]);
756349cc55cSDimitry Andric     if (m_regs[i].alt_name)
757349cc55cSDimitry Andric       s.Printf(", alt-name = %s", m_regs[i].alt_name);
758349cc55cSDimitry Andric     if (m_regs[i].value_regs) {
759349cc55cSDimitry Andric       s.Printf(", value_regs = [ ");
760349cc55cSDimitry Andric       for (size_t j = 0; m_regs[i].value_regs[j] != LLDB_INVALID_REGNUM; ++j) {
761349cc55cSDimitry Andric         s.Printf("%s ", m_regs[m_regs[i].value_regs[j]].name);
762349cc55cSDimitry Andric       }
763349cc55cSDimitry Andric       s.Printf("]");
764349cc55cSDimitry Andric     }
765349cc55cSDimitry Andric     if (m_regs[i].invalidate_regs) {
766349cc55cSDimitry Andric       s.Printf(", invalidate_regs = [ ");
767349cc55cSDimitry Andric       for (size_t j = 0; m_regs[i].invalidate_regs[j] != LLDB_INVALID_REGNUM;
768349cc55cSDimitry Andric            ++j) {
769349cc55cSDimitry Andric         s.Printf("%s ", m_regs[m_regs[i].invalidate_regs[j]].name);
770349cc55cSDimitry Andric       }
771349cc55cSDimitry Andric       s.Printf("]");
772349cc55cSDimitry Andric     }
773349cc55cSDimitry Andric     s.EOL();
774349cc55cSDimitry Andric   }
775349cc55cSDimitry Andric 
776349cc55cSDimitry Andric   const size_t num_sets = m_sets.size();
777349cc55cSDimitry Andric   s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " register sets:\n",
778349cc55cSDimitry Andric            static_cast<const void *>(this), static_cast<uint64_t>(num_sets));
779349cc55cSDimitry Andric   for (size_t i = 0; i < num_sets; ++i) {
780349cc55cSDimitry Andric     s.Printf("set[%" PRIu64 "] name = %s, regs = [", (uint64_t)i,
781349cc55cSDimitry Andric              m_sets[i].name);
782349cc55cSDimitry Andric     for (size_t idx = 0; idx < m_sets[i].num_registers; ++idx) {
783349cc55cSDimitry Andric       s.Printf("%s ", m_regs[m_sets[i].registers[idx]].name);
784349cc55cSDimitry Andric     }
785349cc55cSDimitry Andric     s.Printf("]\n");
786349cc55cSDimitry Andric   }
787349cc55cSDimitry Andric }
788349cc55cSDimitry Andric 
789349cc55cSDimitry Andric const lldb_private::RegisterInfo *
790349cc55cSDimitry Andric DynamicRegisterInfo::GetRegisterInfo(llvm::StringRef reg_name) const {
791349cc55cSDimitry Andric   for (auto &reg_info : m_regs)
792349cc55cSDimitry Andric     if (reg_info.name == reg_name)
793349cc55cSDimitry Andric       return &reg_info;
794349cc55cSDimitry Andric   return nullptr;
795349cc55cSDimitry Andric }
796349cc55cSDimitry Andric 
797349cc55cSDimitry Andric void lldb_private::addSupplementaryRegister(
798349cc55cSDimitry Andric     std::vector<DynamicRegisterInfo::Register> &regs,
799349cc55cSDimitry Andric     DynamicRegisterInfo::Register new_reg_info) {
800349cc55cSDimitry Andric   assert(!new_reg_info.value_regs.empty());
801349cc55cSDimitry Andric   const uint32_t reg_num = regs.size();
802349cc55cSDimitry Andric   regs.push_back(new_reg_info);
803349cc55cSDimitry Andric 
804349cc55cSDimitry Andric   std::map<uint32_t, std::vector<uint32_t>> new_invalidates;
805349cc55cSDimitry Andric   for (uint32_t value_reg : new_reg_info.value_regs) {
806349cc55cSDimitry Andric     // copy value_regs to invalidate_regs
807349cc55cSDimitry Andric     new_invalidates[reg_num].push_back(value_reg);
808349cc55cSDimitry Andric 
809349cc55cSDimitry Andric     // copy invalidate_regs from the parent register
810349cc55cSDimitry Andric     llvm::append_range(new_invalidates[reg_num],
811349cc55cSDimitry Andric                        regs[value_reg].invalidate_regs);
812349cc55cSDimitry Andric 
813349cc55cSDimitry Andric     // add reverse invalidate entries
814349cc55cSDimitry Andric     for (uint32_t x : new_invalidates[reg_num])
815349cc55cSDimitry Andric       new_invalidates[x].push_back(reg_num);
816349cc55cSDimitry Andric   }
817349cc55cSDimitry Andric 
818349cc55cSDimitry Andric   for (const auto &x : new_invalidates)
819349cc55cSDimitry Andric     llvm::append_range(regs[x.first].invalidate_regs, x.second);
820349cc55cSDimitry Andric }
821