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" 11*5f757f3fSDimitry 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> 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 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) { 147*5f757f3fSDimitry Andric std::optional<llvm::StringRef> maybe_composite_reg_name = 148*5f757f3fSDimitry Andric composite_reg_list.GetItemAtIndexAsString(composite_idx); 149*5f757f3fSDimitry 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 = 156*5f757f3fSDimitry 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\"", 161*5f757f3fSDimitry 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 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 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) { 209*5f757f3fSDimitry Andric std::optional<llvm::StringRef> maybe_set_name = 210*5f757f3fSDimitry Andric sets->GetItemAtIndexAsString(i); 211*5f757f3fSDimitry Andric if (maybe_set_name && !maybe_set_name->empty()) { 212*5f757f3fSDimitry Andric m_sets.push_back( 213*5f757f3fSDimitry 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) { 234*5f757f3fSDimitry Andric std::optional<StructuredData::Dictionary *> maybe_reg_info_dict = 235*5f757f3fSDimitry Andric regs->GetItemAtIndexAsDictionary(i); 236*5f757f3fSDimitry 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 } 242*5f757f3fSDimitry 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) { 352349cc55cSDimitry Andric uint64_t invalidate_reg_num; 353*5f757f3fSDimitry Andric std::optional<llvm::StringRef> maybe_invalidate_reg_name = 354*5f757f3fSDimitry Andric invalidate_reg_list->GetItemAtIndexAsString(idx); 355*5f757f3fSDimitry Andric if (maybe_invalidate_reg_name) { 356349cc55cSDimitry Andric const RegisterInfo *invalidate_reg_info = 357*5f757f3fSDimitry Andric GetRegisterInfo(*maybe_invalidate_reg_name); 358349cc55cSDimitry Andric if (invalidate_reg_info) { 359349cc55cSDimitry Andric m_invalidate_regs_map[i].push_back( 360349cc55cSDimitry Andric invalidate_reg_info->kinds[eRegisterKindLLDB]); 361349cc55cSDimitry Andric } else { 362349cc55cSDimitry Andric // TODO: print error invalid slice string that doesn't follow the 363349cc55cSDimitry Andric // format 364349cc55cSDimitry Andric printf("error: failed to find a 'invalidate-regs' register for " 365349cc55cSDimitry Andric "\"%s\" while parsing register \"%s\"\n", 366*5f757f3fSDimitry Andric maybe_invalidate_reg_name->str().c_str(), reg_info.name); 367349cc55cSDimitry Andric } 368349cc55cSDimitry Andric } else if (invalidate_reg_list->GetItemAtIndexAsInteger( 369349cc55cSDimitry Andric idx, invalidate_reg_num)) { 370349cc55cSDimitry Andric if (invalidate_reg_num != UINT64_MAX) 371349cc55cSDimitry Andric m_invalidate_regs_map[i].push_back(invalidate_reg_num); 372349cc55cSDimitry Andric else 373349cc55cSDimitry Andric printf("error: 'invalidate-regs' list value wasn't a valid " 374349cc55cSDimitry Andric "integer\n"); 375349cc55cSDimitry Andric } else { 376349cc55cSDimitry Andric printf("error: 'invalidate-regs' list value wasn't a python string " 377349cc55cSDimitry Andric "or integer\n"); 378349cc55cSDimitry Andric } 379349cc55cSDimitry Andric } 380349cc55cSDimitry Andric } else { 381349cc55cSDimitry Andric printf("error: 'invalidate-regs' contained an empty list\n"); 382349cc55cSDimitry Andric } 383349cc55cSDimitry Andric } 384349cc55cSDimitry Andric 385349cc55cSDimitry Andric // Calculate the register offset 386349cc55cSDimitry Andric const size_t end_reg_offset = reg_info.byte_offset + reg_info.byte_size; 387349cc55cSDimitry Andric if (m_reg_data_byte_size < end_reg_offset) 388349cc55cSDimitry Andric m_reg_data_byte_size = end_reg_offset; 389349cc55cSDimitry Andric 390349cc55cSDimitry Andric m_regs.push_back(reg_info); 391349cc55cSDimitry Andric m_set_reg_nums[set].push_back(i); 392349cc55cSDimitry Andric } 393349cc55cSDimitry Andric Finalize(arch); 394349cc55cSDimitry Andric return m_regs.size(); 395349cc55cSDimitry Andric } 396349cc55cSDimitry Andric 397349cc55cSDimitry Andric size_t DynamicRegisterInfo::SetRegisterInfo( 398349cc55cSDimitry Andric std::vector<DynamicRegisterInfo::Register> &®s, 399349cc55cSDimitry Andric const ArchSpec &arch) { 400349cc55cSDimitry Andric assert(!m_finalized); 401349cc55cSDimitry Andric 402349cc55cSDimitry Andric for (auto it : llvm::enumerate(regs)) { 403349cc55cSDimitry Andric uint32_t local_regnum = it.index(); 404349cc55cSDimitry Andric const DynamicRegisterInfo::Register ® = it.value(); 405349cc55cSDimitry Andric 406349cc55cSDimitry Andric assert(reg.name); 407349cc55cSDimitry Andric assert(reg.set_name); 408349cc55cSDimitry Andric 409349cc55cSDimitry Andric if (!reg.value_regs.empty()) 410349cc55cSDimitry Andric m_value_regs_map[local_regnum] = std::move(reg.value_regs); 411349cc55cSDimitry Andric if (!reg.invalidate_regs.empty()) 412349cc55cSDimitry Andric m_invalidate_regs_map[local_regnum] = std::move(reg.invalidate_regs); 413349cc55cSDimitry Andric if (reg.value_reg_offset != 0) { 414349cc55cSDimitry Andric assert(reg.value_regs.size() == 1); 415349cc55cSDimitry Andric m_value_reg_offset_map[local_regnum] = reg.value_reg_offset; 416349cc55cSDimitry Andric } 417349cc55cSDimitry Andric 418349cc55cSDimitry Andric struct RegisterInfo reg_info { 419349cc55cSDimitry Andric reg.name.AsCString(), reg.alt_name.AsCString(), reg.byte_size, 420349cc55cSDimitry Andric reg.byte_offset, reg.encoding, reg.format, 421349cc55cSDimitry Andric {reg.regnum_ehframe, reg.regnum_dwarf, reg.regnum_generic, 422349cc55cSDimitry Andric reg.regnum_remote, local_regnum}, 423349cc55cSDimitry Andric // value_regs and invalidate_regs are filled by Finalize() 42406c3fb27SDimitry Andric nullptr, nullptr, reg.flags_type 425349cc55cSDimitry Andric }; 426349cc55cSDimitry Andric 427349cc55cSDimitry Andric m_regs.push_back(reg_info); 428349cc55cSDimitry Andric 429349cc55cSDimitry Andric uint32_t set = GetRegisterSetIndexByName(reg.set_name, true); 430349cc55cSDimitry Andric assert(set < m_sets.size()); 431349cc55cSDimitry Andric assert(set < m_set_reg_nums.size()); 432349cc55cSDimitry Andric assert(set < m_set_names.size()); 433349cc55cSDimitry Andric m_set_reg_nums[set].push_back(local_regnum); 434349cc55cSDimitry Andric }; 435349cc55cSDimitry Andric 436349cc55cSDimitry Andric Finalize(arch); 437349cc55cSDimitry Andric return m_regs.size(); 438349cc55cSDimitry Andric } 439349cc55cSDimitry Andric 440349cc55cSDimitry Andric void DynamicRegisterInfo::Finalize(const ArchSpec &arch) { 441349cc55cSDimitry Andric if (m_finalized) 442349cc55cSDimitry Andric return; 443349cc55cSDimitry Andric 444349cc55cSDimitry Andric m_finalized = true; 445349cc55cSDimitry Andric const size_t num_sets = m_sets.size(); 446349cc55cSDimitry Andric for (size_t set = 0; set < num_sets; ++set) { 447349cc55cSDimitry Andric assert(m_sets.size() == m_set_reg_nums.size()); 448349cc55cSDimitry Andric m_sets[set].num_registers = m_set_reg_nums[set].size(); 449349cc55cSDimitry Andric m_sets[set].registers = m_set_reg_nums[set].data(); 450349cc55cSDimitry Andric } 451349cc55cSDimitry Andric 452349cc55cSDimitry Andric // make sure value_regs are terminated with LLDB_INVALID_REGNUM 453349cc55cSDimitry Andric 454349cc55cSDimitry Andric for (reg_to_regs_map::iterator pos = m_value_regs_map.begin(), 455349cc55cSDimitry Andric end = m_value_regs_map.end(); 456349cc55cSDimitry Andric pos != end; ++pos) { 457349cc55cSDimitry Andric if (pos->second.back() != LLDB_INVALID_REGNUM) 458349cc55cSDimitry Andric pos->second.push_back(LLDB_INVALID_REGNUM); 459349cc55cSDimitry Andric } 460349cc55cSDimitry Andric 461349cc55cSDimitry Andric // Now update all value_regs with each register info as needed 462349cc55cSDimitry Andric const size_t num_regs = m_regs.size(); 463349cc55cSDimitry Andric for (size_t i = 0; i < num_regs; ++i) { 464349cc55cSDimitry Andric if (m_value_regs_map.find(i) != m_value_regs_map.end()) 465349cc55cSDimitry Andric m_regs[i].value_regs = m_value_regs_map[i].data(); 466349cc55cSDimitry Andric else 467349cc55cSDimitry Andric m_regs[i].value_regs = nullptr; 468349cc55cSDimitry Andric } 469349cc55cSDimitry Andric 470349cc55cSDimitry Andric // Expand all invalidation dependencies 471349cc55cSDimitry Andric for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(), 472349cc55cSDimitry Andric end = m_invalidate_regs_map.end(); 473349cc55cSDimitry Andric pos != end; ++pos) { 474349cc55cSDimitry Andric const uint32_t reg_num = pos->first; 475349cc55cSDimitry Andric 476349cc55cSDimitry Andric if (m_regs[reg_num].value_regs) { 477349cc55cSDimitry Andric reg_num_collection extra_invalid_regs; 478349cc55cSDimitry Andric for (const uint32_t invalidate_reg_num : pos->second) { 479349cc55cSDimitry Andric reg_to_regs_map::iterator invalidate_pos = 480349cc55cSDimitry Andric m_invalidate_regs_map.find(invalidate_reg_num); 481349cc55cSDimitry Andric if (invalidate_pos != m_invalidate_regs_map.end()) { 482349cc55cSDimitry Andric for (const uint32_t concrete_invalidate_reg_num : 483349cc55cSDimitry Andric invalidate_pos->second) { 484349cc55cSDimitry Andric if (concrete_invalidate_reg_num != reg_num) 485349cc55cSDimitry Andric extra_invalid_regs.push_back(concrete_invalidate_reg_num); 486349cc55cSDimitry Andric } 487349cc55cSDimitry Andric } 488349cc55cSDimitry Andric } 489349cc55cSDimitry Andric pos->second.insert(pos->second.end(), extra_invalid_regs.begin(), 490349cc55cSDimitry Andric extra_invalid_regs.end()); 491349cc55cSDimitry Andric } 492349cc55cSDimitry Andric } 493349cc55cSDimitry Andric 494349cc55cSDimitry Andric // sort and unique all invalidate registers and make sure each is terminated 495349cc55cSDimitry Andric // with LLDB_INVALID_REGNUM 496349cc55cSDimitry Andric for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(), 497349cc55cSDimitry Andric end = m_invalidate_regs_map.end(); 498349cc55cSDimitry Andric pos != end; ++pos) { 499349cc55cSDimitry Andric if (pos->second.size() > 1) { 500fcaf7f86SDimitry Andric llvm::sort(pos->second); 501349cc55cSDimitry Andric reg_num_collection::iterator unique_end = 502349cc55cSDimitry Andric std::unique(pos->second.begin(), pos->second.end()); 503349cc55cSDimitry Andric if (unique_end != pos->second.end()) 504349cc55cSDimitry Andric pos->second.erase(unique_end, pos->second.end()); 505349cc55cSDimitry Andric } 506349cc55cSDimitry Andric assert(!pos->second.empty()); 507349cc55cSDimitry Andric if (pos->second.back() != LLDB_INVALID_REGNUM) 508349cc55cSDimitry Andric pos->second.push_back(LLDB_INVALID_REGNUM); 509349cc55cSDimitry Andric } 510349cc55cSDimitry Andric 511349cc55cSDimitry Andric // Now update all invalidate_regs with each register info as needed 512349cc55cSDimitry Andric for (size_t i = 0; i < num_regs; ++i) { 513349cc55cSDimitry Andric if (m_invalidate_regs_map.find(i) != m_invalidate_regs_map.end()) 514349cc55cSDimitry Andric m_regs[i].invalidate_regs = m_invalidate_regs_map[i].data(); 515349cc55cSDimitry Andric else 516349cc55cSDimitry Andric m_regs[i].invalidate_regs = nullptr; 517349cc55cSDimitry Andric } 518349cc55cSDimitry Andric 519349cc55cSDimitry Andric // Check if we need to automatically set the generic registers in case they 520349cc55cSDimitry Andric // weren't set 521349cc55cSDimitry Andric bool generic_regs_specified = false; 522349cc55cSDimitry Andric for (const auto ® : m_regs) { 523349cc55cSDimitry Andric if (reg.kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM) { 524349cc55cSDimitry Andric generic_regs_specified = true; 525349cc55cSDimitry Andric break; 526349cc55cSDimitry Andric } 527349cc55cSDimitry Andric } 528349cc55cSDimitry Andric 529349cc55cSDimitry Andric if (!generic_regs_specified) { 530349cc55cSDimitry Andric switch (arch.GetMachine()) { 531349cc55cSDimitry Andric case llvm::Triple::aarch64: 532349cc55cSDimitry Andric case llvm::Triple::aarch64_32: 533349cc55cSDimitry Andric case llvm::Triple::aarch64_be: 534349cc55cSDimitry Andric for (auto ® : m_regs) { 535349cc55cSDimitry Andric if (strcmp(reg.name, "pc") == 0) 536349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC; 537349cc55cSDimitry Andric else if ((strcmp(reg.name, "fp") == 0) || 538349cc55cSDimitry Andric (strcmp(reg.name, "x29") == 0)) 539349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP; 540349cc55cSDimitry Andric else if ((strcmp(reg.name, "lr") == 0) || 541349cc55cSDimitry Andric (strcmp(reg.name, "x30") == 0)) 542349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA; 543349cc55cSDimitry Andric else if ((strcmp(reg.name, "sp") == 0) || 544349cc55cSDimitry Andric (strcmp(reg.name, "x31") == 0)) 545349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP; 546349cc55cSDimitry Andric else if (strcmp(reg.name, "cpsr") == 0) 547349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS; 548349cc55cSDimitry Andric } 549349cc55cSDimitry Andric break; 550349cc55cSDimitry Andric 551349cc55cSDimitry Andric case llvm::Triple::arm: 552349cc55cSDimitry Andric case llvm::Triple::armeb: 553349cc55cSDimitry Andric case llvm::Triple::thumb: 554349cc55cSDimitry Andric case llvm::Triple::thumbeb: 555349cc55cSDimitry Andric for (auto ® : m_regs) { 556349cc55cSDimitry Andric if ((strcmp(reg.name, "pc") == 0) || (strcmp(reg.name, "r15") == 0)) 557349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC; 558349cc55cSDimitry Andric else if ((strcmp(reg.name, "sp") == 0) || 559349cc55cSDimitry Andric (strcmp(reg.name, "r13") == 0)) 560349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP; 561349cc55cSDimitry Andric else if ((strcmp(reg.name, "lr") == 0) || 562349cc55cSDimitry Andric (strcmp(reg.name, "r14") == 0)) 563349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA; 564349cc55cSDimitry Andric else if ((strcmp(reg.name, "r7") == 0) && 565349cc55cSDimitry Andric arch.GetTriple().getVendor() == llvm::Triple::Apple) 566349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP; 567349cc55cSDimitry Andric else if ((strcmp(reg.name, "r11") == 0) && 568349cc55cSDimitry Andric arch.GetTriple().getVendor() != llvm::Triple::Apple) 569349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP; 570349cc55cSDimitry Andric else if (strcmp(reg.name, "fp") == 0) 571349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP; 572349cc55cSDimitry Andric else if (strcmp(reg.name, "cpsr") == 0) 573349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS; 574349cc55cSDimitry Andric } 575349cc55cSDimitry Andric break; 576349cc55cSDimitry Andric 577349cc55cSDimitry Andric case llvm::Triple::x86: 578349cc55cSDimitry Andric for (auto ® : m_regs) { 579349cc55cSDimitry Andric if ((strcmp(reg.name, "eip") == 0) || (strcmp(reg.name, "pc") == 0)) 580349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC; 581349cc55cSDimitry Andric else if ((strcmp(reg.name, "esp") == 0) || 582349cc55cSDimitry Andric (strcmp(reg.name, "sp") == 0)) 583349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP; 584349cc55cSDimitry Andric else if ((strcmp(reg.name, "ebp") == 0) || 585349cc55cSDimitry Andric (strcmp(reg.name, "fp") == 0)) 586349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP; 587349cc55cSDimitry Andric else if ((strcmp(reg.name, "eflags") == 0) || 588349cc55cSDimitry Andric (strcmp(reg.name, "flags") == 0)) 589349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS; 590349cc55cSDimitry Andric } 591349cc55cSDimitry Andric break; 592349cc55cSDimitry Andric 593349cc55cSDimitry Andric case llvm::Triple::x86_64: 594349cc55cSDimitry Andric for (auto ® : m_regs) { 595349cc55cSDimitry Andric if ((strcmp(reg.name, "rip") == 0) || (strcmp(reg.name, "pc") == 0)) 596349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC; 597349cc55cSDimitry Andric else if ((strcmp(reg.name, "rsp") == 0) || 598349cc55cSDimitry Andric (strcmp(reg.name, "sp") == 0)) 599349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP; 600349cc55cSDimitry Andric else if ((strcmp(reg.name, "rbp") == 0) || 601349cc55cSDimitry Andric (strcmp(reg.name, "fp") == 0)) 602349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP; 603349cc55cSDimitry Andric else if ((strcmp(reg.name, "rflags") == 0) || 604349cc55cSDimitry Andric (strcmp(reg.name, "eflags") == 0) || 605349cc55cSDimitry Andric (strcmp(reg.name, "flags") == 0)) 606349cc55cSDimitry Andric reg.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS; 607349cc55cSDimitry Andric } 608349cc55cSDimitry Andric break; 609349cc55cSDimitry Andric 610349cc55cSDimitry Andric default: 611349cc55cSDimitry Andric break; 612349cc55cSDimitry Andric } 613349cc55cSDimitry Andric } 614349cc55cSDimitry Andric 615349cc55cSDimitry Andric // At this stage call ConfigureOffsets to calculate register offsets for 616349cc55cSDimitry Andric // targets supporting dynamic offset calculation. It also calculates 617349cc55cSDimitry Andric // total byte size of register data. 618349cc55cSDimitry Andric ConfigureOffsets(); 619349cc55cSDimitry Andric 620349cc55cSDimitry Andric // Check if register info is reconfigurable 621*5f757f3fSDimitry Andric // AArch64 SVE register set has configurable register sizes, as does the ZA 622*5f757f3fSDimitry Andric // register that SME added (the streaming state of SME reuses the SVE state). 623349cc55cSDimitry Andric if (arch.GetTriple().isAArch64()) { 624349cc55cSDimitry Andric for (const auto ® : m_regs) { 625*5f757f3fSDimitry Andric if ((strcmp(reg.name, "vg") == 0) || (strcmp(reg.name, "svg") == 0)) { 626349cc55cSDimitry Andric m_is_reconfigurable = true; 627349cc55cSDimitry Andric break; 628349cc55cSDimitry Andric } 629349cc55cSDimitry Andric } 630349cc55cSDimitry Andric } 631349cc55cSDimitry Andric } 632349cc55cSDimitry Andric 633349cc55cSDimitry Andric void DynamicRegisterInfo::ConfigureOffsets() { 634349cc55cSDimitry Andric // We are going to create a map between remote (eRegisterKindProcessPlugin) 635349cc55cSDimitry Andric // and local (eRegisterKindLLDB) register numbers. This map will give us 636349cc55cSDimitry Andric // remote register numbers in increasing order for offset calculation. 637349cc55cSDimitry Andric std::map<uint32_t, uint32_t> remote_to_local_regnum_map; 638349cc55cSDimitry Andric for (const auto ® : m_regs) 639349cc55cSDimitry Andric remote_to_local_regnum_map[reg.kinds[eRegisterKindProcessPlugin]] = 640349cc55cSDimitry Andric reg.kinds[eRegisterKindLLDB]; 641349cc55cSDimitry Andric 642349cc55cSDimitry Andric // At this stage we manually calculate g/G packet offsets of all primary 643349cc55cSDimitry Andric // registers, only if target XML or qRegisterInfo packet did not send 644349cc55cSDimitry Andric // an offset explicitly. 645349cc55cSDimitry Andric uint32_t reg_offset = 0; 646349cc55cSDimitry Andric for (auto const ®num_pair : remote_to_local_regnum_map) { 647349cc55cSDimitry Andric if (m_regs[regnum_pair.second].byte_offset == LLDB_INVALID_INDEX32 && 648349cc55cSDimitry Andric m_regs[regnum_pair.second].value_regs == nullptr) { 649349cc55cSDimitry Andric m_regs[regnum_pair.second].byte_offset = reg_offset; 650349cc55cSDimitry Andric 651349cc55cSDimitry Andric reg_offset = m_regs[regnum_pair.second].byte_offset + 652349cc55cSDimitry Andric m_regs[regnum_pair.second].byte_size; 653349cc55cSDimitry Andric } 654349cc55cSDimitry Andric } 655349cc55cSDimitry Andric 656349cc55cSDimitry Andric // Now update all value_regs with each register info as needed 657349cc55cSDimitry Andric for (auto ® : m_regs) { 658349cc55cSDimitry Andric if (reg.value_regs != nullptr) { 659349cc55cSDimitry Andric // Assign a valid offset to all pseudo registers that have only a single 660349cc55cSDimitry Andric // parent register in value_regs list, if not assigned by stub. Pseudo 661349cc55cSDimitry Andric // registers with value_regs list populated will share same offset as 662349cc55cSDimitry Andric // that of their corresponding parent register. 663349cc55cSDimitry Andric if (reg.byte_offset == LLDB_INVALID_INDEX32) { 664349cc55cSDimitry Andric uint32_t value_regnum = reg.value_regs[0]; 665349cc55cSDimitry Andric if (value_regnum != LLDB_INVALID_INDEX32 && 666349cc55cSDimitry Andric reg.value_regs[1] == LLDB_INVALID_INDEX32) { 667349cc55cSDimitry Andric reg.byte_offset = 668349cc55cSDimitry Andric GetRegisterInfoAtIndex(value_regnum)->byte_offset; 669349cc55cSDimitry Andric auto it = m_value_reg_offset_map.find(reg.kinds[eRegisterKindLLDB]); 670349cc55cSDimitry Andric if (it != m_value_reg_offset_map.end()) 671349cc55cSDimitry Andric reg.byte_offset += it->second; 672349cc55cSDimitry Andric } 673349cc55cSDimitry Andric } 674349cc55cSDimitry Andric } 675349cc55cSDimitry Andric 676349cc55cSDimitry Andric reg_offset = reg.byte_offset + reg.byte_size; 677349cc55cSDimitry Andric if (m_reg_data_byte_size < reg_offset) 678349cc55cSDimitry Andric m_reg_data_byte_size = reg_offset; 679349cc55cSDimitry Andric } 680349cc55cSDimitry Andric } 681349cc55cSDimitry Andric 682349cc55cSDimitry Andric bool DynamicRegisterInfo::IsReconfigurable() { return m_is_reconfigurable; } 683349cc55cSDimitry Andric 684349cc55cSDimitry Andric size_t DynamicRegisterInfo::GetNumRegisters() const { return m_regs.size(); } 685349cc55cSDimitry Andric 686349cc55cSDimitry Andric size_t DynamicRegisterInfo::GetNumRegisterSets() const { return m_sets.size(); } 687349cc55cSDimitry Andric 688349cc55cSDimitry Andric size_t DynamicRegisterInfo::GetRegisterDataByteSize() const { 689349cc55cSDimitry Andric return m_reg_data_byte_size; 690349cc55cSDimitry Andric } 691349cc55cSDimitry Andric 692349cc55cSDimitry Andric const RegisterInfo * 693349cc55cSDimitry Andric DynamicRegisterInfo::GetRegisterInfoAtIndex(uint32_t i) const { 694349cc55cSDimitry Andric if (i < m_regs.size()) 695349cc55cSDimitry Andric return &m_regs[i]; 696349cc55cSDimitry Andric return nullptr; 697349cc55cSDimitry Andric } 698349cc55cSDimitry Andric 699349cc55cSDimitry Andric const RegisterInfo *DynamicRegisterInfo::GetRegisterInfo(uint32_t kind, 700349cc55cSDimitry Andric uint32_t num) const { 701349cc55cSDimitry Andric uint32_t reg_index = ConvertRegisterKindToRegisterNumber(kind, num); 702349cc55cSDimitry Andric if (reg_index != LLDB_INVALID_REGNUM) 703349cc55cSDimitry Andric return &m_regs[reg_index]; 704349cc55cSDimitry Andric return nullptr; 705349cc55cSDimitry Andric } 706349cc55cSDimitry Andric 707349cc55cSDimitry Andric const RegisterSet *DynamicRegisterInfo::GetRegisterSet(uint32_t i) const { 708349cc55cSDimitry Andric if (i < m_sets.size()) 709349cc55cSDimitry Andric return &m_sets[i]; 710349cc55cSDimitry Andric return nullptr; 711349cc55cSDimitry Andric } 712349cc55cSDimitry Andric 713349cc55cSDimitry Andric uint32_t 714349cc55cSDimitry Andric DynamicRegisterInfo::GetRegisterSetIndexByName(const ConstString &set_name, 715349cc55cSDimitry Andric bool can_create) { 716349cc55cSDimitry Andric name_collection::iterator pos, end = m_set_names.end(); 717349cc55cSDimitry Andric for (pos = m_set_names.begin(); pos != end; ++pos) { 718349cc55cSDimitry Andric if (*pos == set_name) 719349cc55cSDimitry Andric return std::distance(m_set_names.begin(), pos); 720349cc55cSDimitry Andric } 721349cc55cSDimitry Andric 722349cc55cSDimitry Andric m_set_names.push_back(set_name); 723349cc55cSDimitry Andric m_set_reg_nums.resize(m_set_reg_nums.size() + 1); 724349cc55cSDimitry Andric RegisterSet new_set = {set_name.AsCString(), nullptr, 0, nullptr}; 725349cc55cSDimitry Andric m_sets.push_back(new_set); 726349cc55cSDimitry Andric return m_sets.size() - 1; 727349cc55cSDimitry Andric } 728349cc55cSDimitry Andric 729349cc55cSDimitry Andric uint32_t 730349cc55cSDimitry Andric DynamicRegisterInfo::ConvertRegisterKindToRegisterNumber(uint32_t kind, 731349cc55cSDimitry Andric uint32_t num) const { 732349cc55cSDimitry Andric reg_collection::const_iterator pos, end = m_regs.end(); 733349cc55cSDimitry Andric for (pos = m_regs.begin(); pos != end; ++pos) { 734349cc55cSDimitry Andric if (pos->kinds[kind] == num) 735349cc55cSDimitry Andric return std::distance(m_regs.begin(), pos); 736349cc55cSDimitry Andric } 737349cc55cSDimitry Andric 738349cc55cSDimitry Andric return LLDB_INVALID_REGNUM; 739349cc55cSDimitry Andric } 740349cc55cSDimitry Andric 741349cc55cSDimitry Andric void DynamicRegisterInfo::Clear() { 742349cc55cSDimitry Andric m_regs.clear(); 743349cc55cSDimitry Andric m_sets.clear(); 744349cc55cSDimitry Andric m_set_reg_nums.clear(); 745349cc55cSDimitry Andric m_set_names.clear(); 746349cc55cSDimitry Andric m_value_regs_map.clear(); 747349cc55cSDimitry Andric m_invalidate_regs_map.clear(); 748349cc55cSDimitry Andric m_reg_data_byte_size = 0; 749349cc55cSDimitry Andric m_finalized = false; 750349cc55cSDimitry Andric } 751349cc55cSDimitry Andric 752349cc55cSDimitry Andric void DynamicRegisterInfo::Dump() const { 753349cc55cSDimitry Andric StreamFile s(stdout, false); 754349cc55cSDimitry Andric const size_t num_regs = m_regs.size(); 755349cc55cSDimitry Andric s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " registers:\n", 756349cc55cSDimitry Andric static_cast<const void *>(this), static_cast<uint64_t>(num_regs)); 757349cc55cSDimitry Andric for (size_t i = 0; i < num_regs; ++i) { 758349cc55cSDimitry Andric s.Printf("[%3" PRIu64 "] name = %-10s", (uint64_t)i, m_regs[i].name); 759349cc55cSDimitry Andric s.Printf(", size = %2u, offset = %4u, encoding = %u, format = %-10s", 760349cc55cSDimitry Andric m_regs[i].byte_size, m_regs[i].byte_offset, m_regs[i].encoding, 761349cc55cSDimitry Andric FormatManager::GetFormatAsCString(m_regs[i].format)); 762349cc55cSDimitry Andric if (m_regs[i].kinds[eRegisterKindProcessPlugin] != LLDB_INVALID_REGNUM) 763349cc55cSDimitry Andric s.Printf(", process plugin = %3u", 764349cc55cSDimitry Andric m_regs[i].kinds[eRegisterKindProcessPlugin]); 765349cc55cSDimitry Andric if (m_regs[i].kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM) 766349cc55cSDimitry Andric s.Printf(", dwarf = %3u", m_regs[i].kinds[eRegisterKindDWARF]); 767349cc55cSDimitry Andric if (m_regs[i].kinds[eRegisterKindEHFrame] != LLDB_INVALID_REGNUM) 768349cc55cSDimitry Andric s.Printf(", ehframe = %3u", m_regs[i].kinds[eRegisterKindEHFrame]); 769349cc55cSDimitry Andric if (m_regs[i].kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM) 770349cc55cSDimitry Andric s.Printf(", generic = %3u", m_regs[i].kinds[eRegisterKindGeneric]); 771349cc55cSDimitry Andric if (m_regs[i].alt_name) 772349cc55cSDimitry Andric s.Printf(", alt-name = %s", m_regs[i].alt_name); 773349cc55cSDimitry Andric if (m_regs[i].value_regs) { 774349cc55cSDimitry Andric s.Printf(", value_regs = [ "); 775349cc55cSDimitry Andric for (size_t j = 0; m_regs[i].value_regs[j] != LLDB_INVALID_REGNUM; ++j) { 776349cc55cSDimitry Andric s.Printf("%s ", m_regs[m_regs[i].value_regs[j]].name); 777349cc55cSDimitry Andric } 778349cc55cSDimitry Andric s.Printf("]"); 779349cc55cSDimitry Andric } 780349cc55cSDimitry Andric if (m_regs[i].invalidate_regs) { 781349cc55cSDimitry Andric s.Printf(", invalidate_regs = [ "); 782349cc55cSDimitry Andric for (size_t j = 0; m_regs[i].invalidate_regs[j] != LLDB_INVALID_REGNUM; 783349cc55cSDimitry Andric ++j) { 784349cc55cSDimitry Andric s.Printf("%s ", m_regs[m_regs[i].invalidate_regs[j]].name); 785349cc55cSDimitry Andric } 786349cc55cSDimitry Andric s.Printf("]"); 787349cc55cSDimitry Andric } 788349cc55cSDimitry Andric s.EOL(); 789349cc55cSDimitry Andric } 790349cc55cSDimitry Andric 791349cc55cSDimitry Andric const size_t num_sets = m_sets.size(); 792349cc55cSDimitry Andric s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " register sets:\n", 793349cc55cSDimitry Andric static_cast<const void *>(this), static_cast<uint64_t>(num_sets)); 794349cc55cSDimitry Andric for (size_t i = 0; i < num_sets; ++i) { 795349cc55cSDimitry Andric s.Printf("set[%" PRIu64 "] name = %s, regs = [", (uint64_t)i, 796349cc55cSDimitry Andric m_sets[i].name); 797349cc55cSDimitry Andric for (size_t idx = 0; idx < m_sets[i].num_registers; ++idx) { 798349cc55cSDimitry Andric s.Printf("%s ", m_regs[m_sets[i].registers[idx]].name); 799349cc55cSDimitry Andric } 800349cc55cSDimitry Andric s.Printf("]\n"); 801349cc55cSDimitry Andric } 802349cc55cSDimitry Andric } 803349cc55cSDimitry Andric 804349cc55cSDimitry Andric const lldb_private::RegisterInfo * 805349cc55cSDimitry Andric DynamicRegisterInfo::GetRegisterInfo(llvm::StringRef reg_name) const { 806349cc55cSDimitry Andric for (auto ®_info : m_regs) 807349cc55cSDimitry Andric if (reg_info.name == reg_name) 808349cc55cSDimitry Andric return ®_info; 809349cc55cSDimitry Andric return nullptr; 810349cc55cSDimitry Andric } 811349cc55cSDimitry Andric 812349cc55cSDimitry Andric void lldb_private::addSupplementaryRegister( 813349cc55cSDimitry Andric std::vector<DynamicRegisterInfo::Register> ®s, 814349cc55cSDimitry Andric DynamicRegisterInfo::Register new_reg_info) { 815349cc55cSDimitry Andric assert(!new_reg_info.value_regs.empty()); 816349cc55cSDimitry Andric const uint32_t reg_num = regs.size(); 817349cc55cSDimitry Andric regs.push_back(new_reg_info); 818349cc55cSDimitry Andric 819349cc55cSDimitry Andric std::map<uint32_t, std::vector<uint32_t>> new_invalidates; 820349cc55cSDimitry Andric for (uint32_t value_reg : new_reg_info.value_regs) { 821349cc55cSDimitry Andric // copy value_regs to invalidate_regs 822349cc55cSDimitry Andric new_invalidates[reg_num].push_back(value_reg); 823349cc55cSDimitry Andric 824349cc55cSDimitry Andric // copy invalidate_regs from the parent register 825349cc55cSDimitry Andric llvm::append_range(new_invalidates[reg_num], 826349cc55cSDimitry Andric regs[value_reg].invalidate_regs); 827349cc55cSDimitry Andric 828349cc55cSDimitry Andric // add reverse invalidate entries 829349cc55cSDimitry Andric for (uint32_t x : new_invalidates[reg_num]) 830349cc55cSDimitry Andric new_invalidates[x].push_back(reg_num); 831349cc55cSDimitry Andric } 832349cc55cSDimitry Andric 833349cc55cSDimitry Andric for (const auto &x : new_invalidates) 834349cc55cSDimitry Andric llvm::append_range(regs[x.first].invalidate_regs, x.second); 835349cc55cSDimitry Andric } 836