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/DataFormatters/FormatManager.h"
115f757f3fSDimitry Andric #include "lldb/Host/StreamFile.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
2306c3fb27SDimitry Andric std::unique_ptr<DynamicRegisterInfo>
Create(const StructuredData::Dictionary & dict,const ArchSpec & arch)2406c3fb27SDimitry Andric DynamicRegisterInfo::Create(const StructuredData::Dictionary &dict,
2506c3fb27SDimitry Andric const ArchSpec &arch) {
2606c3fb27SDimitry Andric auto dyn_reg_info = std::make_unique<DynamicRegisterInfo>();
2706c3fb27SDimitry Andric if (!dyn_reg_info)
2806c3fb27SDimitry Andric return nullptr;
2906c3fb27SDimitry Andric
3006c3fb27SDimitry Andric if (dyn_reg_info->SetRegisterInfo(dict, arch) == 0)
3106c3fb27SDimitry Andric return nullptr;
3206c3fb27SDimitry Andric
3306c3fb27SDimitry Andric return dyn_reg_info;
34349cc55cSDimitry Andric }
35349cc55cSDimitry Andric
DynamicRegisterInfo(DynamicRegisterInfo && info)36349cc55cSDimitry Andric DynamicRegisterInfo::DynamicRegisterInfo(DynamicRegisterInfo &&info) {
37349cc55cSDimitry Andric MoveFrom(std::move(info));
38349cc55cSDimitry Andric }
39349cc55cSDimitry Andric
40349cc55cSDimitry Andric DynamicRegisterInfo &
operator =(DynamicRegisterInfo && info)41349cc55cSDimitry Andric DynamicRegisterInfo::operator=(DynamicRegisterInfo &&info) {
42349cc55cSDimitry Andric MoveFrom(std::move(info));
43349cc55cSDimitry Andric return *this;
44349cc55cSDimitry Andric }
45349cc55cSDimitry Andric
MoveFrom(DynamicRegisterInfo && info)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
ByteOffsetFromSlice(uint32_t index,llvm::StringRef slice_str,lldb::ByteOrder byte_order)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
ByteOffsetFromComposite(uint32_t index,StructuredData::Array & composite_reg_list,lldb::ByteOrder byte_order)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) {
1475f757f3fSDimitry Andric std::optional<llvm::StringRef> maybe_composite_reg_name =
1485f757f3fSDimitry Andric composite_reg_list.GetItemAtIndexAsString(composite_idx);
1495f757f3fSDimitry Andric if (!maybe_composite_reg_name)
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 =
1565f757f3fSDimitry Andric GetRegisterInfo(*maybe_composite_reg_name);
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\"",
1615f757f3fSDimitry Andric maybe_composite_reg_name->str().c_str());
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
ByteOffsetFromRegInfoDict(uint32_t index,StructuredData::Dictionary & reg_info_dict,lldb::ByteOrder byte_order)176349cc55cSDimitry Andric llvm::Expected<uint32_t> DynamicRegisterInfo::ByteOffsetFromRegInfoDict(
177349cc55cSDimitry Andric uint32_t index, StructuredData::Dictionary ®_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
SetRegisterInfo(const StructuredData::Dictionary & dict,const ArchSpec & arch)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) {
2095f757f3fSDimitry Andric std::optional<llvm::StringRef> maybe_set_name =
2105f757f3fSDimitry Andric sets->GetItemAtIndexAsString(i);
2115f757f3fSDimitry Andric if (maybe_set_name && !maybe_set_name->empty()) {
2125f757f3fSDimitry Andric m_sets.push_back(
2135f757f3fSDimitry Andric {ConstString(*maybe_set_name).AsCString(), nullptr, 0, nullptr});
214349cc55cSDimitry Andric } else {
215349cc55cSDimitry Andric Clear();
216349cc55cSDimitry Andric printf("error: register sets must have valid names\n");
217349cc55cSDimitry Andric return 0;
218349cc55cSDimitry Andric }
219349cc55cSDimitry Andric }
220349cc55cSDimitry Andric m_set_reg_nums.resize(m_sets.size());
221349cc55cSDimitry Andric }
222349cc55cSDimitry Andric
223349cc55cSDimitry Andric StructuredData::Array *regs = nullptr;
224349cc55cSDimitry Andric if (!dict.GetValueForKeyAsArray("registers", regs))
225349cc55cSDimitry Andric return 0;
226349cc55cSDimitry Andric
227349cc55cSDimitry Andric const ByteOrder byte_order = arch.GetByteOrder();
228349cc55cSDimitry Andric
229349cc55cSDimitry Andric const uint32_t num_regs = regs->GetSize();
230349cc55cSDimitry Andric // typedef std::map<std::string, std::vector<std::string> >
231349cc55cSDimitry Andric // InvalidateNameMap;
232349cc55cSDimitry Andric // InvalidateNameMap invalidate_map;
233349cc55cSDimitry Andric for (uint32_t i = 0; i < num_regs; ++i) {
2345f757f3fSDimitry Andric std::optional<StructuredData::Dictionary *> maybe_reg_info_dict =
2355f757f3fSDimitry Andric regs->GetItemAtIndexAsDictionary(i);
2365f757f3fSDimitry Andric if (!maybe_reg_info_dict) {
237349cc55cSDimitry Andric Clear();
238349cc55cSDimitry Andric printf("error: items in the 'registers' array must be dictionaries\n");
239349cc55cSDimitry Andric regs->DumpToStdout();
240349cc55cSDimitry Andric return 0;
241349cc55cSDimitry Andric }
2425f757f3fSDimitry Andric StructuredData::Dictionary *reg_info_dict = *maybe_reg_info_dict;
243349cc55cSDimitry Andric
244349cc55cSDimitry Andric // { 'name':'rcx' , 'bitsize' : 64, 'offset' : 16,
245349cc55cSDimitry Andric // 'encoding':'uint' , 'format':'hex' , 'set': 0, 'ehframe' : 2,
246349cc55cSDimitry Andric // 'dwarf' : 2, 'generic':'arg4', 'alt-name':'arg4', },
247349cc55cSDimitry Andric RegisterInfo reg_info;
248349cc55cSDimitry Andric std::vector<uint32_t> value_regs;
249349cc55cSDimitry Andric std::vector<uint32_t> invalidate_regs;
250349cc55cSDimitry Andric memset(®_info, 0, sizeof(reg_info));
251349cc55cSDimitry Andric
25206c3fb27SDimitry Andric llvm::StringRef name_val;
25306c3fb27SDimitry Andric if (!reg_info_dict->GetValueForKeyAsString("name", name_val)) {
254349cc55cSDimitry Andric Clear();
255349cc55cSDimitry Andric printf("error: registers must have valid names and offsets\n");
256349cc55cSDimitry Andric reg_info_dict->DumpToStdout();
257349cc55cSDimitry Andric return 0;
258349cc55cSDimitry Andric }
25906c3fb27SDimitry Andric reg_info.name = ConstString(name_val).GetCString();
26006c3fb27SDimitry Andric
26106c3fb27SDimitry Andric llvm::StringRef alt_name_val;
26206c3fb27SDimitry Andric if (reg_info_dict->GetValueForKeyAsString("alt-name", alt_name_val))
26306c3fb27SDimitry Andric reg_info.alt_name = ConstString(alt_name_val).GetCString();
26406c3fb27SDimitry Andric else
26506c3fb27SDimitry Andric reg_info.alt_name = nullptr;
266349cc55cSDimitry Andric
267349cc55cSDimitry Andric llvm::Expected<uint32_t> byte_offset =
268349cc55cSDimitry Andric ByteOffsetFromRegInfoDict(i, *reg_info_dict, byte_order);
269349cc55cSDimitry Andric if (byte_offset)
270349cc55cSDimitry Andric reg_info.byte_offset = byte_offset.get();
271349cc55cSDimitry Andric else {
272349cc55cSDimitry Andric LLDB_LOG_ERROR(log, byte_offset.takeError(),
273349cc55cSDimitry Andric "error while parsing register {1}: {0}", reg_info.name);
274349cc55cSDimitry Andric Clear();
275349cc55cSDimitry Andric reg_info_dict->DumpToStdout();
276349cc55cSDimitry Andric return 0;
277349cc55cSDimitry Andric }
278349cc55cSDimitry Andric
27906c3fb27SDimitry Andric uint64_t bitsize = 0;
280349cc55cSDimitry Andric if (!reg_info_dict->GetValueForKeyAsInteger("bitsize", bitsize)) {
281349cc55cSDimitry Andric Clear();
282349cc55cSDimitry Andric printf("error: invalid or missing 'bitsize' key/value pair in register "
283349cc55cSDimitry Andric "dictionary\n");
284349cc55cSDimitry Andric reg_info_dict->DumpToStdout();
285349cc55cSDimitry Andric return 0;
286349cc55cSDimitry Andric }
287349cc55cSDimitry Andric
288349cc55cSDimitry Andric reg_info.byte_size = bitsize / 8;
289349cc55cSDimitry Andric
290349cc55cSDimitry Andric llvm::StringRef format_str;
291349cc55cSDimitry Andric if (reg_info_dict->GetValueForKeyAsString("format", format_str, nullptr)) {
292349cc55cSDimitry Andric if (OptionArgParser::ToFormat(format_str.str().c_str(), reg_info.format,
293349cc55cSDimitry Andric nullptr)
294349cc55cSDimitry Andric .Fail()) {
295349cc55cSDimitry Andric Clear();
296349cc55cSDimitry Andric printf("error: invalid 'format' value in register dictionary\n");
297349cc55cSDimitry Andric reg_info_dict->DumpToStdout();
298349cc55cSDimitry Andric return 0;
299349cc55cSDimitry Andric }
300349cc55cSDimitry Andric } else {
301349cc55cSDimitry Andric reg_info_dict->GetValueForKeyAsInteger("format", reg_info.format,
302349cc55cSDimitry Andric eFormatHex);
303349cc55cSDimitry Andric }
304349cc55cSDimitry Andric
305349cc55cSDimitry Andric llvm::StringRef encoding_str;
306349cc55cSDimitry Andric if (reg_info_dict->GetValueForKeyAsString("encoding", encoding_str))
307349cc55cSDimitry Andric reg_info.encoding = Args::StringToEncoding(encoding_str, eEncodingUint);
308349cc55cSDimitry Andric else
309349cc55cSDimitry Andric reg_info_dict->GetValueForKeyAsInteger("encoding", reg_info.encoding,
310349cc55cSDimitry Andric eEncodingUint);
311349cc55cSDimitry Andric
312349cc55cSDimitry Andric size_t set = 0;
31306c3fb27SDimitry Andric if (!reg_info_dict->GetValueForKeyAsInteger("set", set) ||
314349cc55cSDimitry Andric set >= m_sets.size()) {
315349cc55cSDimitry Andric Clear();
316349cc55cSDimitry Andric printf("error: invalid 'set' value in register dictionary, valid values "
317349cc55cSDimitry Andric "are 0 - %i\n",
318349cc55cSDimitry Andric (int)set);
319349cc55cSDimitry Andric reg_info_dict->DumpToStdout();
320349cc55cSDimitry Andric return 0;
321349cc55cSDimitry Andric }
322349cc55cSDimitry Andric
323349cc55cSDimitry Andric // Fill in the register numbers
324349cc55cSDimitry Andric reg_info.kinds[lldb::eRegisterKindLLDB] = i;
325349cc55cSDimitry Andric reg_info.kinds[lldb::eRegisterKindProcessPlugin] = i;
326349cc55cSDimitry Andric uint32_t eh_frame_regno = LLDB_INVALID_REGNUM;
327349cc55cSDimitry Andric reg_info_dict->GetValueForKeyAsInteger("gcc", eh_frame_regno,
328349cc55cSDimitry Andric LLDB_INVALID_REGNUM);
329349cc55cSDimitry Andric if (eh_frame_regno == LLDB_INVALID_REGNUM)
330349cc55cSDimitry Andric reg_info_dict->GetValueForKeyAsInteger("ehframe", eh_frame_regno,
331349cc55cSDimitry Andric LLDB_INVALID_REGNUM);
332349cc55cSDimitry Andric reg_info.kinds[lldb::eRegisterKindEHFrame] = eh_frame_regno;
333349cc55cSDimitry Andric reg_info_dict->GetValueForKeyAsInteger(
334349cc55cSDimitry Andric "dwarf", reg_info.kinds[lldb::eRegisterKindDWARF], LLDB_INVALID_REGNUM);
335349cc55cSDimitry Andric llvm::StringRef generic_str;
336349cc55cSDimitry Andric if (reg_info_dict->GetValueForKeyAsString("generic", generic_str))
337349cc55cSDimitry Andric reg_info.kinds[lldb::eRegisterKindGeneric] =
338349cc55cSDimitry Andric Args::StringToGenericRegister(generic_str);
339349cc55cSDimitry Andric else
340349cc55cSDimitry Andric reg_info_dict->GetValueForKeyAsInteger(
341349cc55cSDimitry Andric "generic", reg_info.kinds[lldb::eRegisterKindGeneric],
342349cc55cSDimitry Andric LLDB_INVALID_REGNUM);
343349cc55cSDimitry Andric
344349cc55cSDimitry Andric // Check if this register invalidates any other register values when it is
345349cc55cSDimitry Andric // modified
346349cc55cSDimitry Andric StructuredData::Array *invalidate_reg_list = nullptr;
347349cc55cSDimitry Andric if (reg_info_dict->GetValueForKeyAsArray("invalidate-regs",
348349cc55cSDimitry Andric invalidate_reg_list)) {
349349cc55cSDimitry Andric const size_t num_regs = invalidate_reg_list->GetSize();
350349cc55cSDimitry Andric if (num_regs > 0) {
351349cc55cSDimitry Andric for (uint32_t idx = 0; idx < num_regs; ++idx) {
352*1db9f3b2SDimitry Andric if (auto maybe_invalidate_reg_name =
353*1db9f3b2SDimitry Andric invalidate_reg_list->GetItemAtIndexAsString(idx)) {
354349cc55cSDimitry Andric const RegisterInfo *invalidate_reg_info =
3555f757f3fSDimitry Andric GetRegisterInfo(*maybe_invalidate_reg_name);
356349cc55cSDimitry Andric if (invalidate_reg_info) {
357349cc55cSDimitry Andric m_invalidate_regs_map[i].push_back(
358349cc55cSDimitry Andric invalidate_reg_info->kinds[eRegisterKindLLDB]);
359349cc55cSDimitry Andric } else {
360349cc55cSDimitry Andric // TODO: print error invalid slice string that doesn't follow the
361349cc55cSDimitry Andric // format
362349cc55cSDimitry Andric printf("error: failed to find a 'invalidate-regs' register for "
363349cc55cSDimitry Andric "\"%s\" while parsing register \"%s\"\n",
3645f757f3fSDimitry Andric maybe_invalidate_reg_name->str().c_str(), reg_info.name);
365349cc55cSDimitry Andric }
366*1db9f3b2SDimitry Andric } else if (auto maybe_invalidate_reg_num =
367*1db9f3b2SDimitry Andric invalidate_reg_list->GetItemAtIndexAsInteger<uint64_t>(
368*1db9f3b2SDimitry Andric idx)) {
369*1db9f3b2SDimitry Andric if (*maybe_invalidate_reg_num != UINT64_MAX)
370*1db9f3b2SDimitry Andric m_invalidate_regs_map[i].push_back(*maybe_invalidate_reg_num);
371349cc55cSDimitry Andric else
372349cc55cSDimitry Andric printf("error: 'invalidate-regs' list value wasn't a valid "
373349cc55cSDimitry Andric "integer\n");
374349cc55cSDimitry Andric } else {
375349cc55cSDimitry Andric printf("error: 'invalidate-regs' list value wasn't a python string "
376349cc55cSDimitry Andric "or integer\n");
377349cc55cSDimitry Andric }
378349cc55cSDimitry Andric }
379349cc55cSDimitry Andric } else {
380349cc55cSDimitry Andric printf("error: 'invalidate-regs' contained an empty list\n");
381349cc55cSDimitry Andric }
382349cc55cSDimitry Andric }
383349cc55cSDimitry Andric
384349cc55cSDimitry Andric // Calculate the register offset
385349cc55cSDimitry Andric const size_t end_reg_offset = reg_info.byte_offset + reg_info.byte_size;
386349cc55cSDimitry Andric if (m_reg_data_byte_size < end_reg_offset)
387349cc55cSDimitry Andric m_reg_data_byte_size = end_reg_offset;
388349cc55cSDimitry Andric
389349cc55cSDimitry Andric m_regs.push_back(reg_info);
390349cc55cSDimitry Andric m_set_reg_nums[set].push_back(i);
391349cc55cSDimitry Andric }
392349cc55cSDimitry Andric Finalize(arch);
393349cc55cSDimitry Andric return m_regs.size();
394349cc55cSDimitry Andric }
395349cc55cSDimitry Andric
SetRegisterInfo(std::vector<DynamicRegisterInfo::Register> && regs,const ArchSpec & arch)396349cc55cSDimitry Andric size_t DynamicRegisterInfo::SetRegisterInfo(
397349cc55cSDimitry Andric std::vector<DynamicRegisterInfo::Register> &®s,
398349cc55cSDimitry Andric const ArchSpec &arch) {
399349cc55cSDimitry Andric assert(!m_finalized);
400349cc55cSDimitry Andric
401349cc55cSDimitry Andric for (auto it : llvm::enumerate(regs)) {
402349cc55cSDimitry Andric uint32_t local_regnum = it.index();
403349cc55cSDimitry Andric const DynamicRegisterInfo::Register ® = it.value();
404349cc55cSDimitry Andric
405349cc55cSDimitry Andric assert(reg.name);
406349cc55cSDimitry Andric assert(reg.set_name);
407349cc55cSDimitry Andric
408349cc55cSDimitry Andric if (!reg.value_regs.empty())
409349cc55cSDimitry Andric m_value_regs_map[local_regnum] = std::move(reg.value_regs);
410349cc55cSDimitry Andric if (!reg.invalidate_regs.empty())
411349cc55cSDimitry Andric m_invalidate_regs_map[local_regnum] = std::move(reg.invalidate_regs);
412349cc55cSDimitry Andric if (reg.value_reg_offset != 0) {
413349cc55cSDimitry Andric assert(reg.value_regs.size() == 1);
414349cc55cSDimitry Andric m_value_reg_offset_map[local_regnum] = reg.value_reg_offset;
415349cc55cSDimitry Andric }
416349cc55cSDimitry Andric
417349cc55cSDimitry Andric struct RegisterInfo reg_info {
418349cc55cSDimitry Andric reg.name.AsCString(), reg.alt_name.AsCString(), reg.byte_size,
419349cc55cSDimitry Andric reg.byte_offset, reg.encoding, reg.format,
420349cc55cSDimitry Andric {reg.regnum_ehframe, reg.regnum_dwarf, reg.regnum_generic,
421349cc55cSDimitry Andric reg.regnum_remote, local_regnum},
422349cc55cSDimitry Andric // value_regs and invalidate_regs are filled by Finalize()
42306c3fb27SDimitry Andric nullptr, nullptr, reg.flags_type
424349cc55cSDimitry Andric };
425349cc55cSDimitry Andric
426349cc55cSDimitry Andric m_regs.push_back(reg_info);
427349cc55cSDimitry Andric
428349cc55cSDimitry Andric uint32_t set = GetRegisterSetIndexByName(reg.set_name, true);
429349cc55cSDimitry Andric assert(set < m_sets.size());
430349cc55cSDimitry Andric assert(set < m_set_reg_nums.size());
431349cc55cSDimitry Andric assert(set < m_set_names.size());
432349cc55cSDimitry Andric m_set_reg_nums[set].push_back(local_regnum);
433349cc55cSDimitry Andric };
434349cc55cSDimitry Andric
435349cc55cSDimitry Andric Finalize(arch);
436349cc55cSDimitry Andric return m_regs.size();
437349cc55cSDimitry Andric }
438349cc55cSDimitry Andric
Finalize(const ArchSpec & arch)439349cc55cSDimitry Andric void DynamicRegisterInfo::Finalize(const ArchSpec &arch) {
440349cc55cSDimitry Andric if (m_finalized)
441349cc55cSDimitry Andric return;
442349cc55cSDimitry Andric
443349cc55cSDimitry Andric m_finalized = true;
444349cc55cSDimitry Andric const size_t num_sets = m_sets.size();
445349cc55cSDimitry Andric for (size_t set = 0; set < num_sets; ++set) {
446349cc55cSDimitry Andric assert(m_sets.size() == m_set_reg_nums.size());
447349cc55cSDimitry Andric m_sets[set].num_registers = m_set_reg_nums[set].size();
448349cc55cSDimitry Andric m_sets[set].registers = m_set_reg_nums[set].data();
449349cc55cSDimitry Andric }
450349cc55cSDimitry Andric
451349cc55cSDimitry Andric // make sure value_regs are terminated with LLDB_INVALID_REGNUM
452349cc55cSDimitry Andric
453349cc55cSDimitry Andric for (reg_to_regs_map::iterator pos = m_value_regs_map.begin(),
454349cc55cSDimitry Andric end = m_value_regs_map.end();
455349cc55cSDimitry Andric pos != end; ++pos) {
456349cc55cSDimitry Andric if (pos->second.back() != LLDB_INVALID_REGNUM)
457349cc55cSDimitry Andric pos->second.push_back(LLDB_INVALID_REGNUM);
458349cc55cSDimitry Andric }
459349cc55cSDimitry Andric
460349cc55cSDimitry Andric // Now update all value_regs with each register info as needed
461349cc55cSDimitry Andric const size_t num_regs = m_regs.size();
462349cc55cSDimitry Andric for (size_t i = 0; i < num_regs; ++i) {
463349cc55cSDimitry Andric if (m_value_regs_map.find(i) != m_value_regs_map.end())
464349cc55cSDimitry Andric m_regs[i].value_regs = m_value_regs_map[i].data();
465349cc55cSDimitry Andric else
466349cc55cSDimitry Andric m_regs[i].value_regs = nullptr;
467349cc55cSDimitry Andric }
468349cc55cSDimitry Andric
469349cc55cSDimitry Andric // Expand all invalidation dependencies
470349cc55cSDimitry Andric for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(),
471349cc55cSDimitry Andric end = m_invalidate_regs_map.end();
472349cc55cSDimitry Andric pos != end; ++pos) {
473349cc55cSDimitry Andric const uint32_t reg_num = pos->first;
474349cc55cSDimitry Andric
475349cc55cSDimitry Andric if (m_regs[reg_num].value_regs) {
476349cc55cSDimitry Andric reg_num_collection extra_invalid_regs;
477349cc55cSDimitry Andric for (const uint32_t invalidate_reg_num : pos->second) {
478349cc55cSDimitry Andric reg_to_regs_map::iterator invalidate_pos =
479349cc55cSDimitry Andric m_invalidate_regs_map.find(invalidate_reg_num);
480349cc55cSDimitry Andric if (invalidate_pos != m_invalidate_regs_map.end()) {
481349cc55cSDimitry Andric for (const uint32_t concrete_invalidate_reg_num :
482349cc55cSDimitry Andric invalidate_pos->second) {
483349cc55cSDimitry Andric if (concrete_invalidate_reg_num != reg_num)
484349cc55cSDimitry Andric extra_invalid_regs.push_back(concrete_invalidate_reg_num);
485349cc55cSDimitry Andric }
486349cc55cSDimitry Andric }
487349cc55cSDimitry Andric }
488349cc55cSDimitry Andric pos->second.insert(pos->second.end(), extra_invalid_regs.begin(),
489349cc55cSDimitry Andric extra_invalid_regs.end());
490349cc55cSDimitry Andric }
491349cc55cSDimitry Andric }
492349cc55cSDimitry Andric
493349cc55cSDimitry Andric // sort and unique all invalidate registers and make sure each is terminated
494349cc55cSDimitry Andric // with LLDB_INVALID_REGNUM
495349cc55cSDimitry Andric for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(),
496349cc55cSDimitry Andric end = m_invalidate_regs_map.end();
497349cc55cSDimitry Andric pos != end; ++pos) {
498349cc55cSDimitry Andric if (pos->second.size() > 1) {
499fcaf7f86SDimitry Andric llvm::sort(pos->second);
500349cc55cSDimitry Andric reg_num_collection::iterator unique_end =
501349cc55cSDimitry Andric std::unique(pos->second.begin(), pos->second.end());
502349cc55cSDimitry Andric if (unique_end != pos->second.end())
503349cc55cSDimitry Andric pos->second.erase(unique_end, pos->second.end());
504349cc55cSDimitry Andric }
505349cc55cSDimitry Andric assert(!pos->second.empty());
506349cc55cSDimitry Andric if (pos->second.back() != LLDB_INVALID_REGNUM)
507349cc55cSDimitry Andric pos->second.push_back(LLDB_INVALID_REGNUM);
508349cc55cSDimitry Andric }
509349cc55cSDimitry Andric
510349cc55cSDimitry Andric // Now update all invalidate_regs with each register info as needed
511349cc55cSDimitry Andric for (size_t i = 0; i < num_regs; ++i) {
512349cc55cSDimitry Andric if (m_invalidate_regs_map.find(i) != m_invalidate_regs_map.end())
513349cc55cSDimitry Andric m_regs[i].invalidate_regs = m_invalidate_regs_map[i].data();
514349cc55cSDimitry Andric else
515349cc55cSDimitry Andric m_regs[i].invalidate_regs = nullptr;
516349cc55cSDimitry Andric }
517349cc55cSDimitry Andric
518349cc55cSDimitry Andric // Check if we need to automatically set the generic registers in case they
519349cc55cSDimitry Andric // weren't set
520349cc55cSDimitry Andric bool generic_regs_specified = false;
521349cc55cSDimitry Andric for (const auto ® : m_regs) {
522349cc55cSDimitry Andric if (reg.kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM) {
523349cc55cSDimitry Andric generic_regs_specified = true;
524349cc55cSDimitry Andric break;
525349cc55cSDimitry Andric }
526349cc55cSDimitry Andric }
527349cc55cSDimitry Andric
528349cc55cSDimitry Andric if (!generic_regs_specified) {
529349cc55cSDimitry Andric switch (arch.GetMachine()) {
530349cc55cSDimitry Andric case llvm::Triple::aarch64:
531349cc55cSDimitry Andric case llvm::Triple::aarch64_32:
532349cc55cSDimitry Andric case llvm::Triple::aarch64_be:
533349cc55cSDimitry Andric for (auto ® : m_regs) {
534349cc55cSDimitry Andric if (strcmp(reg.name, "pc") == 0)
535349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
536349cc55cSDimitry Andric else if ((strcmp(reg.name, "fp") == 0) ||
537349cc55cSDimitry Andric (strcmp(reg.name, "x29") == 0))
538349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
539349cc55cSDimitry Andric else if ((strcmp(reg.name, "lr") == 0) ||
540349cc55cSDimitry Andric (strcmp(reg.name, "x30") == 0))
541349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA;
542349cc55cSDimitry Andric else if ((strcmp(reg.name, "sp") == 0) ||
543349cc55cSDimitry Andric (strcmp(reg.name, "x31") == 0))
544349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
545349cc55cSDimitry Andric else if (strcmp(reg.name, "cpsr") == 0)
546349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
547349cc55cSDimitry Andric }
548349cc55cSDimitry Andric break;
549349cc55cSDimitry Andric
550349cc55cSDimitry Andric case llvm::Triple::arm:
551349cc55cSDimitry Andric case llvm::Triple::armeb:
552349cc55cSDimitry Andric case llvm::Triple::thumb:
553349cc55cSDimitry Andric case llvm::Triple::thumbeb:
554349cc55cSDimitry Andric for (auto ® : m_regs) {
555349cc55cSDimitry Andric if ((strcmp(reg.name, "pc") == 0) || (strcmp(reg.name, "r15") == 0))
556349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
557349cc55cSDimitry Andric else if ((strcmp(reg.name, "sp") == 0) ||
558349cc55cSDimitry Andric (strcmp(reg.name, "r13") == 0))
559349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
560349cc55cSDimitry Andric else if ((strcmp(reg.name, "lr") == 0) ||
561349cc55cSDimitry Andric (strcmp(reg.name, "r14") == 0))
562349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA;
563349cc55cSDimitry Andric else if ((strcmp(reg.name, "r7") == 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, "r11") == 0) &&
567349cc55cSDimitry Andric arch.GetTriple().getVendor() != llvm::Triple::Apple)
568349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
569349cc55cSDimitry Andric else if (strcmp(reg.name, "fp") == 0)
570349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
571349cc55cSDimitry Andric else if (strcmp(reg.name, "cpsr") == 0)
572349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
573349cc55cSDimitry Andric }
574349cc55cSDimitry Andric break;
575349cc55cSDimitry Andric
576349cc55cSDimitry Andric case llvm::Triple::x86:
577349cc55cSDimitry Andric for (auto ® : m_regs) {
578349cc55cSDimitry Andric if ((strcmp(reg.name, "eip") == 0) || (strcmp(reg.name, "pc") == 0))
579349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
580349cc55cSDimitry Andric else if ((strcmp(reg.name, "esp") == 0) ||
581349cc55cSDimitry Andric (strcmp(reg.name, "sp") == 0))
582349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
583349cc55cSDimitry Andric else if ((strcmp(reg.name, "ebp") == 0) ||
584349cc55cSDimitry Andric (strcmp(reg.name, "fp") == 0))
585349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
586349cc55cSDimitry Andric else if ((strcmp(reg.name, "eflags") == 0) ||
587349cc55cSDimitry Andric (strcmp(reg.name, "flags") == 0))
588349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
589349cc55cSDimitry Andric }
590349cc55cSDimitry Andric break;
591349cc55cSDimitry Andric
592349cc55cSDimitry Andric case llvm::Triple::x86_64:
593349cc55cSDimitry Andric for (auto ® : m_regs) {
594349cc55cSDimitry Andric if ((strcmp(reg.name, "rip") == 0) || (strcmp(reg.name, "pc") == 0))
595349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
596349cc55cSDimitry Andric else if ((strcmp(reg.name, "rsp") == 0) ||
597349cc55cSDimitry Andric (strcmp(reg.name, "sp") == 0))
598349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
599349cc55cSDimitry Andric else if ((strcmp(reg.name, "rbp") == 0) ||
600349cc55cSDimitry Andric (strcmp(reg.name, "fp") == 0))
601349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
602349cc55cSDimitry Andric else if ((strcmp(reg.name, "rflags") == 0) ||
603349cc55cSDimitry Andric (strcmp(reg.name, "eflags") == 0) ||
604349cc55cSDimitry Andric (strcmp(reg.name, "flags") == 0))
605349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
606349cc55cSDimitry Andric }
607349cc55cSDimitry Andric break;
608349cc55cSDimitry Andric
609349cc55cSDimitry Andric default:
610349cc55cSDimitry Andric break;
611349cc55cSDimitry Andric }
612349cc55cSDimitry Andric }
613349cc55cSDimitry Andric
614349cc55cSDimitry Andric // At this stage call ConfigureOffsets to calculate register offsets for
615349cc55cSDimitry Andric // targets supporting dynamic offset calculation. It also calculates
616349cc55cSDimitry Andric // total byte size of register data.
617349cc55cSDimitry Andric ConfigureOffsets();
618349cc55cSDimitry Andric
619349cc55cSDimitry Andric // Check if register info is reconfigurable
6205f757f3fSDimitry Andric // AArch64 SVE register set has configurable register sizes, as does the ZA
6215f757f3fSDimitry Andric // register that SME added (the streaming state of SME reuses the SVE state).
622349cc55cSDimitry Andric if (arch.GetTriple().isAArch64()) {
623349cc55cSDimitry Andric for (const auto ® : m_regs) {
6245f757f3fSDimitry Andric if ((strcmp(reg.name, "vg") == 0) || (strcmp(reg.name, "svg") == 0)) {
625349cc55cSDimitry Andric m_is_reconfigurable = true;
626349cc55cSDimitry Andric break;
627349cc55cSDimitry Andric }
628349cc55cSDimitry Andric }
629349cc55cSDimitry Andric }
630349cc55cSDimitry Andric }
631349cc55cSDimitry Andric
ConfigureOffsets()632349cc55cSDimitry Andric void DynamicRegisterInfo::ConfigureOffsets() {
633349cc55cSDimitry Andric // We are going to create a map between remote (eRegisterKindProcessPlugin)
634349cc55cSDimitry Andric // and local (eRegisterKindLLDB) register numbers. This map will give us
635349cc55cSDimitry Andric // remote register numbers in increasing order for offset calculation.
636349cc55cSDimitry Andric std::map<uint32_t, uint32_t> remote_to_local_regnum_map;
637349cc55cSDimitry Andric for (const auto ® : m_regs)
638349cc55cSDimitry Andric remote_to_local_regnum_map[reg.kinds[eRegisterKindProcessPlugin]] =
639349cc55cSDimitry Andric reg.kinds[eRegisterKindLLDB];
640349cc55cSDimitry Andric
641349cc55cSDimitry Andric // At this stage we manually calculate g/G packet offsets of all primary
642349cc55cSDimitry Andric // registers, only if target XML or qRegisterInfo packet did not send
643349cc55cSDimitry Andric // an offset explicitly.
644349cc55cSDimitry Andric uint32_t reg_offset = 0;
645349cc55cSDimitry Andric for (auto const ®num_pair : remote_to_local_regnum_map) {
646349cc55cSDimitry Andric if (m_regs[regnum_pair.second].byte_offset == LLDB_INVALID_INDEX32 &&
647349cc55cSDimitry Andric m_regs[regnum_pair.second].value_regs == nullptr) {
648349cc55cSDimitry Andric m_regs[regnum_pair.second].byte_offset = reg_offset;
649349cc55cSDimitry Andric
650349cc55cSDimitry Andric reg_offset = m_regs[regnum_pair.second].byte_offset +
651349cc55cSDimitry Andric m_regs[regnum_pair.second].byte_size;
652349cc55cSDimitry Andric }
653349cc55cSDimitry Andric }
654349cc55cSDimitry Andric
655349cc55cSDimitry Andric // Now update all value_regs with each register info as needed
656349cc55cSDimitry Andric for (auto ® : m_regs) {
657349cc55cSDimitry Andric if (reg.value_regs != nullptr) {
658349cc55cSDimitry Andric // Assign a valid offset to all pseudo registers that have only a single
659349cc55cSDimitry Andric // parent register in value_regs list, if not assigned by stub. Pseudo
660349cc55cSDimitry Andric // registers with value_regs list populated will share same offset as
661349cc55cSDimitry Andric // that of their corresponding parent register.
662349cc55cSDimitry Andric if (reg.byte_offset == LLDB_INVALID_INDEX32) {
663349cc55cSDimitry Andric uint32_t value_regnum = reg.value_regs[0];
664349cc55cSDimitry Andric if (value_regnum != LLDB_INVALID_INDEX32 &&
665349cc55cSDimitry Andric reg.value_regs[1] == LLDB_INVALID_INDEX32) {
666349cc55cSDimitry Andric reg.byte_offset =
667349cc55cSDimitry Andric GetRegisterInfoAtIndex(value_regnum)->byte_offset;
668349cc55cSDimitry Andric auto it = m_value_reg_offset_map.find(reg.kinds[eRegisterKindLLDB]);
669349cc55cSDimitry Andric if (it != m_value_reg_offset_map.end())
670349cc55cSDimitry Andric reg.byte_offset += it->second;
671349cc55cSDimitry Andric }
672349cc55cSDimitry Andric }
673349cc55cSDimitry Andric }
674349cc55cSDimitry Andric
675349cc55cSDimitry Andric reg_offset = reg.byte_offset + reg.byte_size;
676349cc55cSDimitry Andric if (m_reg_data_byte_size < reg_offset)
677349cc55cSDimitry Andric m_reg_data_byte_size = reg_offset;
678349cc55cSDimitry Andric }
679349cc55cSDimitry Andric }
680349cc55cSDimitry Andric
IsReconfigurable()681349cc55cSDimitry Andric bool DynamicRegisterInfo::IsReconfigurable() { return m_is_reconfigurable; }
682349cc55cSDimitry Andric
GetNumRegisters() const683349cc55cSDimitry Andric size_t DynamicRegisterInfo::GetNumRegisters() const { return m_regs.size(); }
684349cc55cSDimitry Andric
GetNumRegisterSets() const685349cc55cSDimitry Andric size_t DynamicRegisterInfo::GetNumRegisterSets() const { return m_sets.size(); }
686349cc55cSDimitry Andric
GetRegisterDataByteSize() const687349cc55cSDimitry Andric size_t DynamicRegisterInfo::GetRegisterDataByteSize() const {
688349cc55cSDimitry Andric return m_reg_data_byte_size;
689349cc55cSDimitry Andric }
690349cc55cSDimitry Andric
691349cc55cSDimitry Andric const RegisterInfo *
GetRegisterInfoAtIndex(uint32_t i) const692349cc55cSDimitry Andric DynamicRegisterInfo::GetRegisterInfoAtIndex(uint32_t i) const {
693349cc55cSDimitry Andric if (i < m_regs.size())
694349cc55cSDimitry Andric return &m_regs[i];
695349cc55cSDimitry Andric return nullptr;
696349cc55cSDimitry Andric }
697349cc55cSDimitry Andric
GetRegisterInfo(uint32_t kind,uint32_t num) const698349cc55cSDimitry Andric const RegisterInfo *DynamicRegisterInfo::GetRegisterInfo(uint32_t kind,
699349cc55cSDimitry Andric uint32_t num) const {
700349cc55cSDimitry Andric uint32_t reg_index = ConvertRegisterKindToRegisterNumber(kind, num);
701349cc55cSDimitry Andric if (reg_index != LLDB_INVALID_REGNUM)
702349cc55cSDimitry Andric return &m_regs[reg_index];
703349cc55cSDimitry Andric return nullptr;
704349cc55cSDimitry Andric }
705349cc55cSDimitry Andric
GetRegisterSet(uint32_t i) const706349cc55cSDimitry Andric const RegisterSet *DynamicRegisterInfo::GetRegisterSet(uint32_t i) const {
707349cc55cSDimitry Andric if (i < m_sets.size())
708349cc55cSDimitry Andric return &m_sets[i];
709349cc55cSDimitry Andric return nullptr;
710349cc55cSDimitry Andric }
711349cc55cSDimitry Andric
712349cc55cSDimitry Andric uint32_t
GetRegisterSetIndexByName(const ConstString & set_name,bool can_create)713349cc55cSDimitry Andric DynamicRegisterInfo::GetRegisterSetIndexByName(const ConstString &set_name,
714349cc55cSDimitry Andric bool can_create) {
715349cc55cSDimitry Andric name_collection::iterator pos, end = m_set_names.end();
716349cc55cSDimitry Andric for (pos = m_set_names.begin(); pos != end; ++pos) {
717349cc55cSDimitry Andric if (*pos == set_name)
718349cc55cSDimitry Andric return std::distance(m_set_names.begin(), pos);
719349cc55cSDimitry Andric }
720349cc55cSDimitry Andric
721349cc55cSDimitry Andric m_set_names.push_back(set_name);
722349cc55cSDimitry Andric m_set_reg_nums.resize(m_set_reg_nums.size() + 1);
723349cc55cSDimitry Andric RegisterSet new_set = {set_name.AsCString(), nullptr, 0, nullptr};
724349cc55cSDimitry Andric m_sets.push_back(new_set);
725349cc55cSDimitry Andric return m_sets.size() - 1;
726349cc55cSDimitry Andric }
727349cc55cSDimitry Andric
728349cc55cSDimitry Andric uint32_t
ConvertRegisterKindToRegisterNumber(uint32_t kind,uint32_t num) const729349cc55cSDimitry Andric DynamicRegisterInfo::ConvertRegisterKindToRegisterNumber(uint32_t kind,
730349cc55cSDimitry Andric uint32_t num) const {
731349cc55cSDimitry Andric reg_collection::const_iterator pos, end = m_regs.end();
732349cc55cSDimitry Andric for (pos = m_regs.begin(); pos != end; ++pos) {
733349cc55cSDimitry Andric if (pos->kinds[kind] == num)
734349cc55cSDimitry Andric return std::distance(m_regs.begin(), pos);
735349cc55cSDimitry Andric }
736349cc55cSDimitry Andric
737349cc55cSDimitry Andric return LLDB_INVALID_REGNUM;
738349cc55cSDimitry Andric }
739349cc55cSDimitry Andric
Clear()740349cc55cSDimitry Andric void DynamicRegisterInfo::Clear() {
741349cc55cSDimitry Andric m_regs.clear();
742349cc55cSDimitry Andric m_sets.clear();
743349cc55cSDimitry Andric m_set_reg_nums.clear();
744349cc55cSDimitry Andric m_set_names.clear();
745349cc55cSDimitry Andric m_value_regs_map.clear();
746349cc55cSDimitry Andric m_invalidate_regs_map.clear();
747349cc55cSDimitry Andric m_reg_data_byte_size = 0;
748349cc55cSDimitry Andric m_finalized = false;
749349cc55cSDimitry Andric }
750349cc55cSDimitry Andric
Dump() const751349cc55cSDimitry Andric void DynamicRegisterInfo::Dump() const {
752349cc55cSDimitry Andric StreamFile s(stdout, false);
753349cc55cSDimitry Andric const size_t num_regs = m_regs.size();
754349cc55cSDimitry Andric s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " registers:\n",
755349cc55cSDimitry Andric static_cast<const void *>(this), static_cast<uint64_t>(num_regs));
756349cc55cSDimitry Andric for (size_t i = 0; i < num_regs; ++i) {
757349cc55cSDimitry Andric s.Printf("[%3" PRIu64 "] name = %-10s", (uint64_t)i, m_regs[i].name);
758349cc55cSDimitry Andric s.Printf(", size = %2u, offset = %4u, encoding = %u, format = %-10s",
759349cc55cSDimitry Andric m_regs[i].byte_size, m_regs[i].byte_offset, m_regs[i].encoding,
760349cc55cSDimitry Andric FormatManager::GetFormatAsCString(m_regs[i].format));
761349cc55cSDimitry Andric if (m_regs[i].kinds[eRegisterKindProcessPlugin] != LLDB_INVALID_REGNUM)
762349cc55cSDimitry Andric s.Printf(", process plugin = %3u",
763349cc55cSDimitry Andric m_regs[i].kinds[eRegisterKindProcessPlugin]);
764349cc55cSDimitry Andric if (m_regs[i].kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM)
765349cc55cSDimitry Andric s.Printf(", dwarf = %3u", m_regs[i].kinds[eRegisterKindDWARF]);
766349cc55cSDimitry Andric if (m_regs[i].kinds[eRegisterKindEHFrame] != LLDB_INVALID_REGNUM)
767349cc55cSDimitry Andric s.Printf(", ehframe = %3u", m_regs[i].kinds[eRegisterKindEHFrame]);
768349cc55cSDimitry Andric if (m_regs[i].kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM)
769349cc55cSDimitry Andric s.Printf(", generic = %3u", m_regs[i].kinds[eRegisterKindGeneric]);
770349cc55cSDimitry Andric if (m_regs[i].alt_name)
771349cc55cSDimitry Andric s.Printf(", alt-name = %s", m_regs[i].alt_name);
772349cc55cSDimitry Andric if (m_regs[i].value_regs) {
773349cc55cSDimitry Andric s.Printf(", value_regs = [ ");
774349cc55cSDimitry Andric for (size_t j = 0; m_regs[i].value_regs[j] != LLDB_INVALID_REGNUM; ++j) {
775349cc55cSDimitry Andric s.Printf("%s ", m_regs[m_regs[i].value_regs[j]].name);
776349cc55cSDimitry Andric }
777349cc55cSDimitry Andric s.Printf("]");
778349cc55cSDimitry Andric }
779349cc55cSDimitry Andric if (m_regs[i].invalidate_regs) {
780349cc55cSDimitry Andric s.Printf(", invalidate_regs = [ ");
781349cc55cSDimitry Andric for (size_t j = 0; m_regs[i].invalidate_regs[j] != LLDB_INVALID_REGNUM;
782349cc55cSDimitry Andric ++j) {
783349cc55cSDimitry Andric s.Printf("%s ", m_regs[m_regs[i].invalidate_regs[j]].name);
784349cc55cSDimitry Andric }
785349cc55cSDimitry Andric s.Printf("]");
786349cc55cSDimitry Andric }
787349cc55cSDimitry Andric s.EOL();
788349cc55cSDimitry Andric }
789349cc55cSDimitry Andric
790349cc55cSDimitry Andric const size_t num_sets = m_sets.size();
791349cc55cSDimitry Andric s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " register sets:\n",
792349cc55cSDimitry Andric static_cast<const void *>(this), static_cast<uint64_t>(num_sets));
793349cc55cSDimitry Andric for (size_t i = 0; i < num_sets; ++i) {
794349cc55cSDimitry Andric s.Printf("set[%" PRIu64 "] name = %s, regs = [", (uint64_t)i,
795349cc55cSDimitry Andric m_sets[i].name);
796349cc55cSDimitry Andric for (size_t idx = 0; idx < m_sets[i].num_registers; ++idx) {
797349cc55cSDimitry Andric s.Printf("%s ", m_regs[m_sets[i].registers[idx]].name);
798349cc55cSDimitry Andric }
799349cc55cSDimitry Andric s.Printf("]\n");
800349cc55cSDimitry Andric }
801349cc55cSDimitry Andric }
802349cc55cSDimitry Andric
803349cc55cSDimitry Andric const lldb_private::RegisterInfo *
GetRegisterInfo(llvm::StringRef reg_name) const804349cc55cSDimitry Andric DynamicRegisterInfo::GetRegisterInfo(llvm::StringRef reg_name) const {
805349cc55cSDimitry Andric for (auto ®_info : m_regs)
806349cc55cSDimitry Andric if (reg_info.name == reg_name)
807349cc55cSDimitry Andric return ®_info;
808349cc55cSDimitry Andric return nullptr;
809349cc55cSDimitry Andric }
810349cc55cSDimitry Andric
addSupplementaryRegister(std::vector<DynamicRegisterInfo::Register> & regs,DynamicRegisterInfo::Register new_reg_info)811349cc55cSDimitry Andric void lldb_private::addSupplementaryRegister(
812349cc55cSDimitry Andric std::vector<DynamicRegisterInfo::Register> ®s,
813349cc55cSDimitry Andric DynamicRegisterInfo::Register new_reg_info) {
814349cc55cSDimitry Andric assert(!new_reg_info.value_regs.empty());
815349cc55cSDimitry Andric const uint32_t reg_num = regs.size();
816349cc55cSDimitry Andric regs.push_back(new_reg_info);
817349cc55cSDimitry Andric
818349cc55cSDimitry Andric std::map<uint32_t, std::vector<uint32_t>> new_invalidates;
819349cc55cSDimitry Andric for (uint32_t value_reg : new_reg_info.value_regs) {
820349cc55cSDimitry Andric // copy value_regs to invalidate_regs
821349cc55cSDimitry Andric new_invalidates[reg_num].push_back(value_reg);
822349cc55cSDimitry Andric
823349cc55cSDimitry Andric // copy invalidate_regs from the parent register
824349cc55cSDimitry Andric llvm::append_range(new_invalidates[reg_num],
825349cc55cSDimitry Andric regs[value_reg].invalidate_regs);
826349cc55cSDimitry Andric
827349cc55cSDimitry Andric // add reverse invalidate entries
828349cc55cSDimitry Andric for (uint32_t x : new_invalidates[reg_num])
829349cc55cSDimitry Andric new_invalidates[x].push_back(reg_num);
830349cc55cSDimitry Andric }
831349cc55cSDimitry Andric
832349cc55cSDimitry Andric for (const auto &x : new_invalidates)
833349cc55cSDimitry Andric llvm::append_range(regs[x.first].invalidate_regs, x.second);
834349cc55cSDimitry Andric }
835