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);
87 m_set_reg_nums.resize(m_sets.size());
89 PythonList regs (dict.GetItemForKey("registers"));
92 const uint32_t num_regs = regs.GetSize();
93 PythonString name_pystr("name");
94 PythonString altname_pystr("alt-name");
95 PythonString bitsize_pystr("bitsize");
96 PythonString offset_pystr("offset");
97 PythonString encoding_pystr("encoding");
98 PythonString format_pystr("format");
99 PythonString set_pystr("set");
100 PythonString gcc_pystr("gcc");
101 PythonString dwarf_pystr("dwarf");
102 PythonString generic_pystr("generic");
103 PythonString slice_pystr("slice");
104 PythonString composite_pystr("composite");
105 PythonString invalidate_regs_pystr("invalidate-regs");
107 // typedef std::map<std::string, std::vector<std::string> > InvalidateNameMap;
108 // InvalidateNameMap invalidate_map;
109 for (uint32_t i=0; i<num_regs; ++i)
111 PythonDictionary reg_info_dict(regs.GetItemAtIndex(i));
114 // { 'name':'rcx' , 'bitsize' : 64, 'offset' : 16, 'encoding':'uint' , 'format':'hex' , 'set': 0, 'gcc' : 2, 'dwarf' : 2, 'generic':'arg4', 'alt-name':'arg4', },
115 RegisterInfo reg_info;
116 std::vector<uint32_t> value_regs;
117 std::vector<uint32_t> invalidate_regs;
118 bzero (®_info, sizeof(reg_info));
120 reg_info.name = ConstString (reg_info_dict.GetItemForKeyAsString(name_pystr)).GetCString();
121 if (reg_info.name == NULL)
127 reg_info.alt_name = ConstString (reg_info_dict.GetItemForKeyAsString(altname_pystr)).GetCString();
129 reg_info.byte_offset = reg_info_dict.GetItemForKeyAsInteger(offset_pystr, UINT32_MAX);
131 if (reg_info.byte_offset == UINT32_MAX)
133 // No offset for this register, see if the register has a value expression
134 // which indicates this register is part of another register. Value expressions
135 // are things like "rax[31:0]" which state that the current register's value
136 // is in a concrete register "rax" in bits 31:0. If there is a value expression
137 // we can calculate the offset
138 bool success = false;
139 const char *slice_cstr = reg_info_dict.GetItemForKeyAsString(slice_pystr);
142 // Slices use the following format:
143 // REGNAME[MSBIT:LSBIT]
144 // REGNAME - name of the register to grab a slice of
145 // MSBIT - the most significant bit at which the current register value starts at
146 // LSBIT - the least significant bit at which the current register value ends at
147 static RegularExpression g_bitfield_regex("([A-Za-z_][A-Za-z0-9_]*)\\[([0-9]+):([0-9]+)\\]");
148 RegularExpression::Match regex_match(3);
149 if (g_bitfield_regex.Execute(slice_cstr, ®ex_match))
151 llvm::StringRef reg_name_str;
152 std::string msbit_str;
153 std::string lsbit_str;
154 if (regex_match.GetMatchAtIndex(slice_cstr, 1, reg_name_str) &&
155 regex_match.GetMatchAtIndex(slice_cstr, 2, msbit_str) &&
156 regex_match.GetMatchAtIndex(slice_cstr, 3, lsbit_str))
158 const uint32_t msbit = Args::StringToUInt32(msbit_str.c_str(), UINT32_MAX);
159 const uint32_t lsbit = Args::StringToUInt32(lsbit_str.c_str(), UINT32_MAX);
160 if (msbit != UINT32_MAX && lsbit != UINT32_MAX)
164 const uint32_t msbyte = msbit / 8;
165 const uint32_t lsbyte = lsbit / 8;
167 ConstString containing_reg_name(reg_name_str);
169 RegisterInfo *containing_reg_info = GetRegisterInfo (containing_reg_name);
170 if (containing_reg_info)
172 const uint32_t max_bit = containing_reg_info->byte_size * 8;
173 if (msbit < max_bit && lsbit < max_bit)
175 m_invalidate_regs_map[containing_reg_info->kinds[eRegisterKindLLDB]].push_back(i);
176 m_value_regs_map[i].push_back(containing_reg_info->kinds[eRegisterKindLLDB]);
177 m_invalidate_regs_map[i].push_back(containing_reg_info->kinds[eRegisterKindLLDB]);
179 if (byte_order == eByteOrderLittle)
182 reg_info.byte_offset = containing_reg_info->byte_offset + lsbyte;
184 else if (byte_order == eByteOrderBig)
187 reg_info.byte_offset = containing_reg_info->byte_offset + msbyte;
191 assert(!"Invalid byte order");
197 printf("error: msbit (%u) must be less than the bitsize of the register (%u)\n", msbit, max_bit);
199 printf("error: lsbit (%u) must be less than the bitsize of the register (%u)\n", lsbit, max_bit);
204 printf("error: invalid concrete register \"%s\"\n", containing_reg_name.GetCString());
209 printf("error: msbit (%u) must be greater than lsbit (%u)\n", msbit, lsbit);
214 printf("error: msbit (%u) and lsbit (%u) must be valid\n", msbit, lsbit);
219 // TODO: print error invalid slice string that doesn't follow the format
220 printf("error: failed to extract regex matches for parsing the register bitfield regex\n");
226 // TODO: print error invalid slice string that doesn't follow the format
227 printf("error: failed to match against register bitfield regex\n");
232 PythonList composite_reg_list (reg_info_dict.GetItemForKey(composite_pystr));
233 if (composite_reg_list)
235 const size_t num_composite_regs = composite_reg_list.GetSize();
236 if (num_composite_regs > 0)
238 uint32_t composite_offset = UINT32_MAX;
239 for (uint32_t composite_idx=0; composite_idx<num_composite_regs; ++composite_idx)
241 PythonString composite_reg_name_pystr(composite_reg_list.GetItemAtIndex(composite_idx));
242 if (composite_reg_name_pystr)
244 ConstString composite_reg_name(composite_reg_name_pystr.GetString());
245 if (composite_reg_name)
247 RegisterInfo *composite_reg_info = GetRegisterInfo (composite_reg_name);
248 if (composite_reg_info)
250 if (composite_offset > composite_reg_info->byte_offset)
251 composite_offset = composite_reg_info->byte_offset;
252 m_value_regs_map[i].push_back(composite_reg_info->kinds[eRegisterKindLLDB]);
253 m_invalidate_regs_map[composite_reg_info->kinds[eRegisterKindLLDB]].push_back(i);
254 m_invalidate_regs_map[i].push_back(composite_reg_info->kinds[eRegisterKindLLDB]);
258 // TODO: print error invalid slice string that doesn't follow the format
259 printf("error: failed to find composite register by name: \"%s\"\n", composite_reg_name.GetCString());
264 printf("error: 'composite' key contained an empty string\n");
269 printf("error: 'composite' list value wasn't a python string\n");
272 if (composite_offset != UINT32_MAX)
274 reg_info.byte_offset = composite_offset;
275 success = m_value_regs_map.find(i) != m_value_regs_map.end();
279 printf("error: 'composite' registers must specify at least one real register\n");
284 printf("error: 'composite' list was empty\n");
296 const int64_t bitsize = reg_info_dict.GetItemForKeyAsInteger(bitsize_pystr, 0);
303 reg_info.byte_size = bitsize / 8;
305 const char *format_cstr = reg_info_dict.GetItemForKeyAsString(format_pystr);
308 if (Args::StringToFormat(format_cstr, reg_info.format, NULL).Fail())
316 reg_info.format = (Format)reg_info_dict.GetItemForKeyAsInteger (format_pystr, eFormatHex);
319 const char *encoding_cstr = reg_info_dict.GetItemForKeyAsString(encoding_pystr);
321 reg_info.encoding = Args::StringToEncoding (encoding_cstr, eEncodingUint);
323 reg_info.encoding = (Encoding)reg_info_dict.GetItemForKeyAsInteger (encoding_pystr, eEncodingUint);
325 const int64_t set = reg_info_dict.GetItemForKeyAsInteger(set_pystr, -1);
326 if (set >= m_sets.size())
332 // Fill in the register numbers
333 reg_info.kinds[lldb::eRegisterKindLLDB] = i;
334 reg_info.kinds[lldb::eRegisterKindGDB] = i;
335 reg_info.kinds[lldb::eRegisterKindGCC] = reg_info_dict.GetItemForKeyAsInteger(gcc_pystr, LLDB_INVALID_REGNUM);
336 reg_info.kinds[lldb::eRegisterKindDWARF] = reg_info_dict.GetItemForKeyAsInteger(dwarf_pystr, LLDB_INVALID_REGNUM);
337 const char *generic_cstr = reg_info_dict.GetItemForKeyAsString(generic_pystr);
339 reg_info.kinds[lldb::eRegisterKindGeneric] = Args::StringToGenericRegister (generic_cstr);
341 reg_info.kinds[lldb::eRegisterKindGeneric] = reg_info_dict.GetItemForKeyAsInteger(generic_pystr, LLDB_INVALID_REGNUM);
343 // Check if this register invalidates any other register values when it is modified
344 PythonList invalidate_reg_list (reg_info_dict.GetItemForKey(invalidate_regs_pystr));
345 if (invalidate_reg_list)
347 const size_t num_regs = invalidate_reg_list.GetSize();
350 for (uint32_t idx=0; idx<num_regs; ++idx)
352 PythonObject invalidate_reg_object (invalidate_reg_list.GetItemAtIndex(idx));
353 PythonString invalidate_reg_name_pystr(invalidate_reg_object);
354 if (invalidate_reg_name_pystr)
356 ConstString invalidate_reg_name(invalidate_reg_name_pystr.GetString());
357 if (invalidate_reg_name)
359 RegisterInfo *invalidate_reg_info = GetRegisterInfo (invalidate_reg_name);
360 if (invalidate_reg_info)
362 m_invalidate_regs_map[i].push_back(invalidate_reg_info->kinds[eRegisterKindLLDB]);
366 // TODO: print error invalid slice string that doesn't follow the format
367 printf("error: failed to find a 'invalidate-regs' register for \"%s\" while parsing register \"%s\"\n", invalidate_reg_name.GetCString(), reg_info.name);
372 printf("error: 'invalidate-regs' list value was an empty string\n");
377 PythonInteger invalidate_reg_num(invalidate_reg_object);
379 if (invalidate_reg_num)
381 const int64_t r = invalidate_reg_num.GetInteger();
383 m_invalidate_regs_map[i].push_back(r);
385 printf("error: 'invalidate-regs' list value wasn't a valid integer\n");
389 printf("error: 'invalidate-regs' list value wasn't a python string or integer\n");
396 printf("error: 'invalidate-regs' contained an empty list\n");
400 // Calculate the register offset
401 const size_t end_reg_offset = reg_info.byte_offset + reg_info.byte_size;
402 if (m_reg_data_byte_size < end_reg_offset)
403 m_reg_data_byte_size = end_reg_offset;
405 m_regs.push_back (reg_info);
406 m_set_reg_nums[set].push_back(i);
418 return m_regs.size();
423 DynamicRegisterInfo::AddRegister (RegisterInfo ®_info,
424 ConstString ®_name,
425 ConstString ®_alt_name,
426 ConstString &set_name)
428 assert(!m_finalized);
429 const uint32_t reg_num = m_regs.size();
430 reg_info.name = reg_name.AsCString();
431 assert (reg_info.name);
432 reg_info.alt_name = reg_alt_name.AsCString(NULL);
434 if (reg_info.value_regs)
436 for (i=0; reg_info.value_regs[i] != LLDB_INVALID_REGNUM; ++i)
437 m_value_regs_map[reg_num].push_back(reg_info.value_regs[i]);
439 if (reg_info.invalidate_regs)
441 for (i=0; reg_info.invalidate_regs[i] != LLDB_INVALID_REGNUM; ++i)
442 m_invalidate_regs_map[reg_num].push_back(reg_info.invalidate_regs[i]);
444 m_regs.push_back (reg_info);
445 uint32_t set = GetRegisterSetIndexByName (set_name, true);
446 assert (set < m_sets.size());
447 assert (set < m_set_reg_nums.size());
448 assert (set < m_set_names.size());
449 m_set_reg_nums[set].push_back(reg_num);
450 size_t end_reg_offset = reg_info.byte_offset + reg_info.byte_size;
451 if (m_reg_data_byte_size < end_reg_offset)
452 m_reg_data_byte_size = end_reg_offset;
456 DynamicRegisterInfo::Finalize ()
462 const size_t num_sets = m_sets.size();
463 for (size_t set = 0; set < num_sets; ++set)
465 assert (m_sets.size() == m_set_reg_nums.size());
466 m_sets[set].num_registers = m_set_reg_nums[set].size();
467 m_sets[set].registers = &m_set_reg_nums[set][0];
470 // sort and unique all value registers and make sure each is terminated with
471 // LLDB_INVALID_REGNUM
473 for (reg_to_regs_map::iterator pos = m_value_regs_map.begin(), end = m_value_regs_map.end();
477 if (pos->second.size() > 1)
479 std::sort (pos->second.begin(), pos->second.end());
480 reg_num_collection::iterator unique_end = std::unique (pos->second.begin(), pos->second.end());
481 if (unique_end != pos->second.end())
482 pos->second.erase(unique_end, pos->second.end());
484 assert (!pos->second.empty());
485 if (pos->second.back() != LLDB_INVALID_REGNUM)
486 pos->second.push_back(LLDB_INVALID_REGNUM);
489 // Now update all value_regs with each register info as needed
490 const size_t num_regs = m_regs.size();
491 for (size_t i=0; i<num_regs; ++i)
493 if (m_value_regs_map.find(i) != m_value_regs_map.end())
494 m_regs[i].value_regs = m_value_regs_map[i].data();
496 m_regs[i].value_regs = NULL;
499 // Expand all invalidation dependencies
500 for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(), end = m_invalidate_regs_map.end();
504 const uint32_t reg_num = pos->first;
506 if (m_regs[reg_num].value_regs)
508 reg_num_collection extra_invalid_regs;
509 for (const uint32_t invalidate_reg_num : pos->second)
511 reg_to_regs_map::iterator invalidate_pos = m_invalidate_regs_map.find(invalidate_reg_num);
512 if (invalidate_pos != m_invalidate_regs_map.end())
514 for (const uint32_t concrete_invalidate_reg_num : invalidate_pos->second)
516 if (concrete_invalidate_reg_num != reg_num)
517 extra_invalid_regs.push_back(concrete_invalidate_reg_num);
521 pos->second.insert(pos->second.end(), extra_invalid_regs.begin(), extra_invalid_regs.end());
525 // sort and unique all invalidate registers and make sure each is terminated with
526 // LLDB_INVALID_REGNUM
527 for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(), end = m_invalidate_regs_map.end();
531 if (pos->second.size() > 1)
533 std::sort (pos->second.begin(), pos->second.end());
534 reg_num_collection::iterator unique_end = std::unique (pos->second.begin(), pos->second.end());
535 if (unique_end != pos->second.end())
536 pos->second.erase(unique_end, pos->second.end());
538 assert (!pos->second.empty());
539 if (pos->second.back() != LLDB_INVALID_REGNUM)
540 pos->second.push_back(LLDB_INVALID_REGNUM);
543 // Now update all invalidate_regs with each register info as needed
544 for (size_t i=0; i<num_regs; ++i)
546 if (m_invalidate_regs_map.find(i) != m_invalidate_regs_map.end())
547 m_regs[i].invalidate_regs = m_invalidate_regs_map[i].data();
549 m_regs[i].invalidate_regs = NULL;
554 DynamicRegisterInfo::GetNumRegisters() const
556 return m_regs.size();
560 DynamicRegisterInfo::GetNumRegisterSets() const
562 return m_sets.size();
566 DynamicRegisterInfo::GetRegisterDataByteSize() const
568 return m_reg_data_byte_size;
572 DynamicRegisterInfo::GetRegisterInfoAtIndex (uint32_t i) const
574 if (i < m_regs.size())
580 DynamicRegisterInfo::GetRegisterSet (uint32_t i) const
582 if (i < m_sets.size())
588 DynamicRegisterInfo::GetRegisterSetIndexByName (ConstString &set_name, bool can_create)
590 name_collection::iterator pos, end = m_set_names.end();
591 for (pos = m_set_names.begin(); pos != end; ++pos)
593 if (*pos == set_name)
594 return std::distance (m_set_names.begin(), pos);
597 m_set_names.push_back(set_name);
598 m_set_reg_nums.resize(m_set_reg_nums.size()+1);
599 RegisterSet new_set = { set_name.AsCString(), NULL, 0, NULL };
600 m_sets.push_back (new_set);
601 return m_sets.size() - 1;
605 DynamicRegisterInfo::ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num) const
607 reg_collection::const_iterator pos, end = m_regs.end();
608 for (pos = m_regs.begin(); pos != end; ++pos)
610 if (pos->kinds[kind] == num)
611 return std::distance (m_regs.begin(), pos);
614 return LLDB_INVALID_REGNUM;
618 DynamicRegisterInfo::Clear()
622 m_set_reg_nums.clear();
624 m_value_regs_map.clear();
625 m_invalidate_regs_map.clear();
626 m_reg_data_byte_size = 0;
631 DynamicRegisterInfo::Dump () const
633 StreamFile s(stdout, false);
634 const size_t num_regs = m_regs.size();
635 s.Printf("%p: DynamicRegisterInfo contains %zu registers:\n", this, num_regs);
636 for (size_t i=0; i<num_regs; ++i)
638 s.Printf("[%3zu] name = %-10s", i, m_regs[i].name);
639 s.Printf(", size = %2u, offset = %4u, encoding = %u, format = %-10s",
641 m_regs[i].byte_offset,
643 FormatManager::GetFormatAsCString (m_regs[i].format));
644 if (m_regs[i].kinds[eRegisterKindGDB] != LLDB_INVALID_REGNUM)
645 s.Printf(", gdb = %3u", m_regs[i].kinds[eRegisterKindGDB]);
646 if (m_regs[i].kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM)
647 s.Printf(", dwarf = %3u", m_regs[i].kinds[eRegisterKindDWARF]);
648 if (m_regs[i].kinds[eRegisterKindGCC] != LLDB_INVALID_REGNUM)
649 s.Printf(", gcc = %3u", m_regs[i].kinds[eRegisterKindGCC]);
650 if (m_regs[i].kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM)
651 s.Printf(", generic = %3u", m_regs[i].kinds[eRegisterKindGeneric]);
652 if (m_regs[i].alt_name)
653 s.Printf(", alt-name = %s", m_regs[i].alt_name);
654 if (m_regs[i].value_regs)
656 s.Printf(", value_regs = [ ");
657 for (size_t j=0; m_regs[i].value_regs[j] != LLDB_INVALID_REGNUM; ++j)
659 s.Printf("%s ", m_regs[m_regs[i].value_regs[j]].name);
663 if (m_regs[i].invalidate_regs)
665 s.Printf(", invalidate_regs = [ ");
666 for (size_t j=0; m_regs[i].invalidate_regs[j] != LLDB_INVALID_REGNUM; ++j)
668 s.Printf("%s ", m_regs[m_regs[i].invalidate_regs[j]].name);
675 const size_t num_sets = m_sets.size();
676 s.Printf("%p: DynamicRegisterInfo contains %zu register sets:\n", this, num_sets);
677 for (size_t i=0; i<num_sets; ++i)
679 s.Printf("set[%zu] name = %s, regs = [", i, m_sets[i].name);
680 for (size_t idx=0; idx<m_sets[i].num_registers; ++idx)
682 s.Printf("%s ", m_regs[m_sets[i].registers[idx]].name);
690 lldb_private::RegisterInfo *
691 DynamicRegisterInfo::GetRegisterInfo (const lldb_private::ConstString ®_name)
693 for (auto ®_info : m_regs)
695 // We can use pointer comparison since we used a ConstString to set
696 // the "name" member in AddRegister()
697 if (reg_info.name == reg_name.GetCString())