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