1 //===-- DynamicRegisterInfo.cpp ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "DynamicRegisterInfo.h"
16 // Other libraries and framework includes
18 #include "lldb/Interpreter/Args.h"
19 #include "lldb/Core/RegularExpression.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/DataFormatters/FormatManager.h"
23 #ifndef LLDB_DISABLE_PYTHON
24 #include "lldb/Interpreter/PythonDataObjects.h"
28 using namespace lldb_private;
30 DynamicRegisterInfo::DynamicRegisterInfo () :
36 m_invalidate_regs_map (),
37 m_reg_data_byte_size (0),
42 DynamicRegisterInfo::DynamicRegisterInfo (const lldb_private::PythonDictionary &dict, ByteOrder byte_order) :
48 m_invalidate_regs_map (),
49 m_reg_data_byte_size (0),
52 SetRegisterInfo (dict, byte_order);
55 DynamicRegisterInfo::~DynamicRegisterInfo ()
61 DynamicRegisterInfo::SetRegisterInfo (const lldb_private::PythonDictionary &dict,
65 #ifndef LLDB_DISABLE_PYTHON
66 PythonList sets (dict.GetItemForKey("sets"));
69 const uint32_t num_sets = sets.GetSize();
70 for (uint32_t i=0; i<num_sets; ++i)
72 PythonString py_set_name(sets.GetItemAtIndex(i));
75 set_name.SetCString(py_set_name.GetString());
78 RegisterSet new_set = { set_name.AsCString(), NULL, 0, NULL };
79 m_sets.push_back (new_set);
84 printf("error: register sets must have valid names\n");
88 m_set_reg_nums.resize(m_sets.size());
90 PythonList regs (dict.GetItemForKey("registers"));
93 const uint32_t num_regs = regs.GetSize();
94 PythonString name_pystr("name");
95 PythonString altname_pystr("alt-name");
96 PythonString bitsize_pystr("bitsize");
97 PythonString offset_pystr("offset");
98 PythonString encoding_pystr("encoding");
99 PythonString format_pystr("format");
100 PythonString set_pystr("set");
101 PythonString gcc_pystr("gcc");
102 PythonString dwarf_pystr("dwarf");
103 PythonString generic_pystr("generic");
104 PythonString slice_pystr("slice");
105 PythonString composite_pystr("composite");
106 PythonString invalidate_regs_pystr("invalidate-regs");
108 // typedef std::map<std::string, std::vector<std::string> > InvalidateNameMap;
109 // InvalidateNameMap invalidate_map;
110 for (uint32_t i=0; i<num_regs; ++i)
112 PythonDictionary reg_info_dict(regs.GetItemAtIndex(i));
115 // { 'name':'rcx' , 'bitsize' : 64, 'offset' : 16, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 2, 'dwarf' : 2, 'generic':'arg4', 'alt-name':'arg4', },
116 RegisterInfo reg_info;
117 std::vector<uint32_t> value_regs;
118 std::vector<uint32_t> invalidate_regs;
119 memset(®_info, 0, sizeof(reg_info));
121 reg_info.name = ConstString (reg_info_dict.GetItemForKeyAsString(name_pystr)).GetCString();
122 if (reg_info.name == NULL)
125 printf("error: registers must have valid names\n");
126 reg_info_dict.Dump();
130 reg_info.alt_name = ConstString (reg_info_dict.GetItemForKeyAsString(altname_pystr)).GetCString();
132 reg_info.byte_offset = reg_info_dict.GetItemForKeyAsInteger(offset_pystr, UINT32_MAX);
134 if (reg_info.byte_offset == UINT32_MAX)
136 // No offset for this register, see if the register has a value expression
137 // which indicates this register is part of another register. Value expressions
138 // are things like "rax[31:0]" which state that the current register's value
139 // is in a concrete register "rax" in bits 31:0. If there is a value expression
140 // we can calculate the offset
141 bool success = false;
142 const char *slice_cstr = reg_info_dict.GetItemForKeyAsString(slice_pystr);
145 // Slices use the following format:
146 // REGNAME[MSBIT:LSBIT]
147 // REGNAME - name of the register to grab a slice of
148 // MSBIT - the most significant bit at which the current register value starts at
149 // LSBIT - the least significant bit at which the current register value ends at
150 static RegularExpression g_bitfield_regex("([A-Za-z_][A-Za-z0-9_]*)\\[([0-9]+):([0-9]+)\\]");
151 RegularExpression::Match regex_match(3);
152 if (g_bitfield_regex.Execute(slice_cstr, ®ex_match))
154 llvm::StringRef reg_name_str;
155 std::string msbit_str;
156 std::string lsbit_str;
157 if (regex_match.GetMatchAtIndex(slice_cstr, 1, reg_name_str) &&
158 regex_match.GetMatchAtIndex(slice_cstr, 2, msbit_str) &&
159 regex_match.GetMatchAtIndex(slice_cstr, 3, lsbit_str))
161 const uint32_t msbit = Args::StringToUInt32(msbit_str.c_str(), UINT32_MAX);
162 const uint32_t lsbit = Args::StringToUInt32(lsbit_str.c_str(), UINT32_MAX);
163 if (msbit != UINT32_MAX && lsbit != UINT32_MAX)
167 const uint32_t msbyte = msbit / 8;
168 const uint32_t lsbyte = lsbit / 8;
170 ConstString containing_reg_name(reg_name_str);
172 RegisterInfo *containing_reg_info = GetRegisterInfo (containing_reg_name);
173 if (containing_reg_info)
175 const uint32_t max_bit = containing_reg_info->byte_size * 8;
176 if (msbit < max_bit && lsbit < max_bit)
178 m_invalidate_regs_map[containing_reg_info->kinds[eRegisterKindLLDB]].push_back(i);
179 m_value_regs_map[i].push_back(containing_reg_info->kinds[eRegisterKindLLDB]);
180 m_invalidate_regs_map[i].push_back(containing_reg_info->kinds[eRegisterKindLLDB]);
182 if (byte_order == eByteOrderLittle)
185 reg_info.byte_offset = containing_reg_info->byte_offset + lsbyte;
187 else if (byte_order == eByteOrderBig)
190 reg_info.byte_offset = containing_reg_info->byte_offset + msbyte;
194 assert(!"Invalid byte order");
200 printf("error: msbit (%u) must be less than the bitsize of the register (%u)\n", msbit, max_bit);
202 printf("error: lsbit (%u) must be less than the bitsize of the register (%u)\n", lsbit, max_bit);
207 printf("error: invalid concrete register \"%s\"\n", containing_reg_name.GetCString());
212 printf("error: msbit (%u) must be greater than lsbit (%u)\n", msbit, lsbit);
217 printf("error: msbit (%u) and lsbit (%u) must be valid\n", msbit, lsbit);
222 // TODO: print error invalid slice string that doesn't follow the format
223 printf("error: failed to extract regex matches for parsing the register bitfield regex\n");
229 // TODO: print error invalid slice string that doesn't follow the format
230 printf("error: failed to match against register bitfield regex\n");
235 PythonList composite_reg_list (reg_info_dict.GetItemForKey(composite_pystr));
236 if (composite_reg_list)
238 const size_t num_composite_regs = composite_reg_list.GetSize();
239 if (num_composite_regs > 0)
241 uint32_t composite_offset = UINT32_MAX;
242 for (uint32_t composite_idx=0; composite_idx<num_composite_regs; ++composite_idx)
244 PythonString composite_reg_name_pystr(composite_reg_list.GetItemAtIndex(composite_idx));
245 if (composite_reg_name_pystr)
247 ConstString composite_reg_name(composite_reg_name_pystr.GetString());
248 if (composite_reg_name)
250 RegisterInfo *composite_reg_info = GetRegisterInfo (composite_reg_name);
251 if (composite_reg_info)
253 if (composite_offset > composite_reg_info->byte_offset)
254 composite_offset = composite_reg_info->byte_offset;
255 m_value_regs_map[i].push_back(composite_reg_info->kinds[eRegisterKindLLDB]);
256 m_invalidate_regs_map[composite_reg_info->kinds[eRegisterKindLLDB]].push_back(i);
257 m_invalidate_regs_map[i].push_back(composite_reg_info->kinds[eRegisterKindLLDB]);
261 // TODO: print error invalid slice string that doesn't follow the format
262 printf("error: failed to find composite register by name: \"%s\"\n", composite_reg_name.GetCString());
267 printf("error: 'composite' key contained an empty string\n");
272 printf("error: 'composite' list value wasn't a python string\n");
275 if (composite_offset != UINT32_MAX)
277 reg_info.byte_offset = composite_offset;
278 success = m_value_regs_map.find(i) != m_value_regs_map.end();
282 printf("error: 'composite' registers must specify at least one real register\n");
287 printf("error: 'composite' list was empty\n");
296 reg_info_dict.Dump();
300 const int64_t bitsize = reg_info_dict.GetItemForKeyAsInteger(bitsize_pystr, 0);
304 printf("error: invalid or missing 'bitsize' key/value pair in register dictionary\n");
305 reg_info_dict.Dump();
309 reg_info.byte_size = bitsize / 8;
311 const char *format_cstr = reg_info_dict.GetItemForKeyAsString(format_pystr);
314 if (Args::StringToFormat(format_cstr, reg_info.format, NULL).Fail())
317 printf("error: invalid 'format' value in register dictionary\n");
318 reg_info_dict.Dump();
324 reg_info.format = (Format)reg_info_dict.GetItemForKeyAsInteger (format_pystr, eFormatHex);
327 const char *encoding_cstr = reg_info_dict.GetItemForKeyAsString(encoding_pystr);
329 reg_info.encoding = Args::StringToEncoding (encoding_cstr, eEncodingUint);
331 reg_info.encoding = (Encoding)reg_info_dict.GetItemForKeyAsInteger (encoding_pystr, eEncodingUint);
333 const int64_t set = reg_info_dict.GetItemForKeyAsInteger(set_pystr, -1);
334 if (static_cast<size_t>(set) >= m_sets.size())
337 printf("error: invalid 'set' value in register dictionary, valid values are 0 - %i\n", (int)set);
338 reg_info_dict.Dump();
342 // Fill in the register numbers
343 reg_info.kinds[lldb::eRegisterKindLLDB] = i;
344 reg_info.kinds[lldb::eRegisterKindGDB] = i;
345 reg_info.kinds[lldb::eRegisterKindGCC] = reg_info_dict.GetItemForKeyAsInteger(gcc_pystr, LLDB_INVALID_REGNUM);
346 reg_info.kinds[lldb::eRegisterKindDWARF] = reg_info_dict.GetItemForKeyAsInteger(dwarf_pystr, LLDB_INVALID_REGNUM);
347 const char *generic_cstr = reg_info_dict.GetItemForKeyAsString(generic_pystr);
349 reg_info.kinds[lldb::eRegisterKindGeneric] = Args::StringToGenericRegister (generic_cstr);
351 reg_info.kinds[lldb::eRegisterKindGeneric] = reg_info_dict.GetItemForKeyAsInteger(generic_pystr, LLDB_INVALID_REGNUM);
353 // Check if this register invalidates any other register values when it is modified
354 PythonList invalidate_reg_list (reg_info_dict.GetItemForKey(invalidate_regs_pystr));
355 if (invalidate_reg_list)
357 const size_t num_regs = invalidate_reg_list.GetSize();
360 for (uint32_t idx=0; idx<num_regs; ++idx)
362 PythonObject invalidate_reg_object (invalidate_reg_list.GetItemAtIndex(idx));
363 PythonString invalidate_reg_name_pystr(invalidate_reg_object);
364 if (invalidate_reg_name_pystr)
366 ConstString invalidate_reg_name(invalidate_reg_name_pystr.GetString());
367 if (invalidate_reg_name)
369 RegisterInfo *invalidate_reg_info = GetRegisterInfo (invalidate_reg_name);
370 if (invalidate_reg_info)
372 m_invalidate_regs_map[i].push_back(invalidate_reg_info->kinds[eRegisterKindLLDB]);
376 // TODO: print error invalid slice string that doesn't follow the format
377 printf("error: failed to find a 'invalidate-regs' register for \"%s\" while parsing register \"%s\"\n", invalidate_reg_name.GetCString(), reg_info.name);
382 printf("error: 'invalidate-regs' list value was an empty string\n");
387 PythonInteger invalidate_reg_num(invalidate_reg_object);
389 if (invalidate_reg_num)
391 const int64_t r = invalidate_reg_num.GetInteger();
392 if (r != static_cast<int64_t>(UINT64_MAX))
393 m_invalidate_regs_map[i].push_back(r);
395 printf("error: 'invalidate-regs' list value wasn't a valid integer\n");
399 printf("error: 'invalidate-regs' list value wasn't a python string or integer\n");
406 printf("error: 'invalidate-regs' contained an empty list\n");
410 // Calculate the register offset
411 const size_t end_reg_offset = reg_info.byte_offset + reg_info.byte_size;
412 if (m_reg_data_byte_size < end_reg_offset)
413 m_reg_data_byte_size = end_reg_offset;
415 m_regs.push_back (reg_info);
416 m_set_reg_nums[set].push_back(i);
422 printf("error: items in the 'registers' array must be dictionaries\n");
430 return m_regs.size();
435 DynamicRegisterInfo::AddRegister (RegisterInfo ®_info,
436 ConstString ®_name,
437 ConstString ®_alt_name,
438 ConstString &set_name)
440 assert(!m_finalized);
441 const uint32_t reg_num = m_regs.size();
442 reg_info.name = reg_name.AsCString();
443 assert (reg_info.name);
444 reg_info.alt_name = reg_alt_name.AsCString(NULL);
446 if (reg_info.value_regs)
448 for (i=0; reg_info.value_regs[i] != LLDB_INVALID_REGNUM; ++i)
449 m_value_regs_map[reg_num].push_back(reg_info.value_regs[i]);
451 if (reg_info.invalidate_regs)
453 for (i=0; reg_info.invalidate_regs[i] != LLDB_INVALID_REGNUM; ++i)
454 m_invalidate_regs_map[reg_num].push_back(reg_info.invalidate_regs[i]);
456 m_regs.push_back (reg_info);
457 uint32_t set = GetRegisterSetIndexByName (set_name, true);
458 assert (set < m_sets.size());
459 assert (set < m_set_reg_nums.size());
460 assert (set < m_set_names.size());
461 m_set_reg_nums[set].push_back(reg_num);
462 size_t end_reg_offset = reg_info.byte_offset + reg_info.byte_size;
463 if (m_reg_data_byte_size < end_reg_offset)
464 m_reg_data_byte_size = end_reg_offset;
468 DynamicRegisterInfo::Finalize ()
474 const size_t num_sets = m_sets.size();
475 for (size_t set = 0; set < num_sets; ++set)
477 assert (m_sets.size() == m_set_reg_nums.size());
478 m_sets[set].num_registers = m_set_reg_nums[set].size();
479 m_sets[set].registers = &m_set_reg_nums[set][0];
482 // sort and unique all value registers and make sure each is terminated with
483 // LLDB_INVALID_REGNUM
485 for (reg_to_regs_map::iterator pos = m_value_regs_map.begin(), end = m_value_regs_map.end();
489 if (pos->second.size() > 1)
491 std::sort (pos->second.begin(), pos->second.end());
492 reg_num_collection::iterator unique_end = std::unique (pos->second.begin(), pos->second.end());
493 if (unique_end != pos->second.end())
494 pos->second.erase(unique_end, pos->second.end());
496 assert (!pos->second.empty());
497 if (pos->second.back() != LLDB_INVALID_REGNUM)
498 pos->second.push_back(LLDB_INVALID_REGNUM);
501 // Now update all value_regs with each register info as needed
502 const size_t num_regs = m_regs.size();
503 for (size_t i=0; i<num_regs; ++i)
505 if (m_value_regs_map.find(i) != m_value_regs_map.end())
506 m_regs[i].value_regs = m_value_regs_map[i].data();
508 m_regs[i].value_regs = NULL;
511 // Expand all invalidation dependencies
512 for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(), end = m_invalidate_regs_map.end();
516 const uint32_t reg_num = pos->first;
518 if (m_regs[reg_num].value_regs)
520 reg_num_collection extra_invalid_regs;
521 for (const uint32_t invalidate_reg_num : pos->second)
523 reg_to_regs_map::iterator invalidate_pos = m_invalidate_regs_map.find(invalidate_reg_num);
524 if (invalidate_pos != m_invalidate_regs_map.end())
526 for (const uint32_t concrete_invalidate_reg_num : invalidate_pos->second)
528 if (concrete_invalidate_reg_num != reg_num)
529 extra_invalid_regs.push_back(concrete_invalidate_reg_num);
533 pos->second.insert(pos->second.end(), extra_invalid_regs.begin(), extra_invalid_regs.end());
537 // sort and unique all invalidate registers and make sure each is terminated with
538 // LLDB_INVALID_REGNUM
539 for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(), end = m_invalidate_regs_map.end();
543 if (pos->second.size() > 1)
545 std::sort (pos->second.begin(), pos->second.end());
546 reg_num_collection::iterator unique_end = std::unique (pos->second.begin(), pos->second.end());
547 if (unique_end != pos->second.end())
548 pos->second.erase(unique_end, pos->second.end());
550 assert (!pos->second.empty());
551 if (pos->second.back() != LLDB_INVALID_REGNUM)
552 pos->second.push_back(LLDB_INVALID_REGNUM);
555 // Now update all invalidate_regs with each register info as needed
556 for (size_t i=0; i<num_regs; ++i)
558 if (m_invalidate_regs_map.find(i) != m_invalidate_regs_map.end())
559 m_regs[i].invalidate_regs = m_invalidate_regs_map[i].data();
561 m_regs[i].invalidate_regs = NULL;
566 DynamicRegisterInfo::GetNumRegisters() const
568 return m_regs.size();
572 DynamicRegisterInfo::GetNumRegisterSets() const
574 return m_sets.size();
578 DynamicRegisterInfo::GetRegisterDataByteSize() const
580 return m_reg_data_byte_size;
584 DynamicRegisterInfo::GetRegisterInfoAtIndex (uint32_t i) const
586 if (i < m_regs.size())
592 DynamicRegisterInfo::GetRegisterSet (uint32_t i) const
594 if (i < m_sets.size())
600 DynamicRegisterInfo::GetRegisterSetIndexByName (ConstString &set_name, bool can_create)
602 name_collection::iterator pos, end = m_set_names.end();
603 for (pos = m_set_names.begin(); pos != end; ++pos)
605 if (*pos == set_name)
606 return std::distance (m_set_names.begin(), pos);
609 m_set_names.push_back(set_name);
610 m_set_reg_nums.resize(m_set_reg_nums.size()+1);
611 RegisterSet new_set = { set_name.AsCString(), NULL, 0, NULL };
612 m_sets.push_back (new_set);
613 return m_sets.size() - 1;
617 DynamicRegisterInfo::ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num) const
619 reg_collection::const_iterator pos, end = m_regs.end();
620 for (pos = m_regs.begin(); pos != end; ++pos)
622 if (pos->kinds[kind] == num)
623 return std::distance (m_regs.begin(), pos);
626 return LLDB_INVALID_REGNUM;
630 DynamicRegisterInfo::Clear()
634 m_set_reg_nums.clear();
636 m_value_regs_map.clear();
637 m_invalidate_regs_map.clear();
638 m_reg_data_byte_size = 0;
643 DynamicRegisterInfo::Dump () const
645 StreamFile s(stdout, false);
646 const size_t num_regs = m_regs.size();
647 s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " registers:\n",
648 static_cast<const void*>(this), static_cast<uint64_t>(num_regs));
649 for (size_t i=0; i<num_regs; ++i)
651 s.Printf("[%3" PRIu64 "] name = %-10s", (uint64_t)i, m_regs[i].name);
652 s.Printf(", size = %2u, offset = %4u, encoding = %u, format = %-10s",
654 m_regs[i].byte_offset,
656 FormatManager::GetFormatAsCString (m_regs[i].format));
657 if (m_regs[i].kinds[eRegisterKindGDB] != LLDB_INVALID_REGNUM)
658 s.Printf(", gdb = %3u", m_regs[i].kinds[eRegisterKindGDB]);
659 if (m_regs[i].kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM)
660 s.Printf(", dwarf = %3u", m_regs[i].kinds[eRegisterKindDWARF]);
661 if (m_regs[i].kinds[eRegisterKindGCC] != LLDB_INVALID_REGNUM)
662 s.Printf(", gcc = %3u", m_regs[i].kinds[eRegisterKindGCC]);
663 if (m_regs[i].kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM)
664 s.Printf(", generic = %3u", m_regs[i].kinds[eRegisterKindGeneric]);
665 if (m_regs[i].alt_name)
666 s.Printf(", alt-name = %s", m_regs[i].alt_name);
667 if (m_regs[i].value_regs)
669 s.Printf(", value_regs = [ ");
670 for (size_t j=0; m_regs[i].value_regs[j] != LLDB_INVALID_REGNUM; ++j)
672 s.Printf("%s ", m_regs[m_regs[i].value_regs[j]].name);
676 if (m_regs[i].invalidate_regs)
678 s.Printf(", invalidate_regs = [ ");
679 for (size_t j=0; m_regs[i].invalidate_regs[j] != LLDB_INVALID_REGNUM; ++j)
681 s.Printf("%s ", m_regs[m_regs[i].invalidate_regs[j]].name);
688 const size_t num_sets = m_sets.size();
689 s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " register sets:\n",
690 static_cast<const void*>(this), static_cast<uint64_t>(num_sets));
691 for (size_t i=0; i<num_sets; ++i)
693 s.Printf("set[%" PRIu64 "] name = %s, regs = [", (uint64_t)i, m_sets[i].name);
694 for (size_t idx=0; idx<m_sets[i].num_registers; ++idx)
696 s.Printf("%s ", m_regs[m_sets[i].registers[idx]].name);
704 lldb_private::RegisterInfo *
705 DynamicRegisterInfo::GetRegisterInfo (const lldb_private::ConstString ®_name)
707 for (auto ®_info : m_regs)
709 // We can use pointer comparison since we used a ConstString to set
710 // the "name" member in AddRegister()
711 if (reg_info.name == reg_name.GetCString())