1 //===-- DynamicRegisterInfo.h -----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_TARGET_DYNAMICREGISTERINFO_H
10 #define LLDB_TARGET_DYNAMICREGISTERINFO_H
11 
12 #include <map>
13 #include <vector>
14 
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/StructuredData.h"
17 #include "lldb/lldb-private.h"
18 
19 namespace lldb_private {
20 
21 class DynamicRegisterInfo {
22 protected:
23   DynamicRegisterInfo(DynamicRegisterInfo &) = default;
24   DynamicRegisterInfo &operator=(DynamicRegisterInfo &) = default;
25 
26 public:
27   struct Register {
28     ConstString name;
29     ConstString alt_name;
30     ConstString set_name;
31     uint32_t byte_size = LLDB_INVALID_INDEX32;
32     uint32_t byte_offset = LLDB_INVALID_INDEX32;
33     lldb::Encoding encoding = lldb::eEncodingUint;
34     lldb::Format format = lldb::eFormatHex;
35     uint32_t regnum_dwarf = LLDB_INVALID_REGNUM;
36     uint32_t regnum_ehframe = LLDB_INVALID_REGNUM;
37     uint32_t regnum_generic = LLDB_INVALID_REGNUM;
38     uint32_t regnum_remote = LLDB_INVALID_REGNUM;
39     std::vector<uint32_t> value_regs;
40     std::vector<uint32_t> invalidate_regs;
41     uint32_t value_reg_offset = 0;
42   };
43 
44   DynamicRegisterInfo() = default;
45 
46   DynamicRegisterInfo(const lldb_private::StructuredData::Dictionary &dict,
47                       const lldb_private::ArchSpec &arch);
48 
49   virtual ~DynamicRegisterInfo() = default;
50 
51   DynamicRegisterInfo(DynamicRegisterInfo &&info);
52   DynamicRegisterInfo &operator=(DynamicRegisterInfo &&info);
53 
54   size_t SetRegisterInfo(const lldb_private::StructuredData::Dictionary &dict,
55                          const lldb_private::ArchSpec &arch);
56 
57   size_t SetRegisterInfo(std::vector<Register> &&regs,
58                          const lldb_private::ArchSpec &arch);
59 
60   size_t GetNumRegisters() const;
61 
62   size_t GetNumRegisterSets() const;
63 
64   size_t GetRegisterDataByteSize() const;
65 
66   const lldb_private::RegisterInfo *GetRegisterInfoAtIndex(uint32_t i) const;
67 
68   const lldb_private::RegisterSet *GetRegisterSet(uint32_t i) const;
69 
70   uint32_t GetRegisterSetIndexByName(const lldb_private::ConstString &set_name,
71                                      bool can_create);
72 
73   uint32_t ConvertRegisterKindToRegisterNumber(uint32_t kind,
74                                                uint32_t num) const;
75 
76   const lldb_private::RegisterInfo *GetRegisterInfo(uint32_t kind,
77                                                     uint32_t num) const;
78 
79   void Dump() const;
80 
81   void Clear();
82 
83   bool IsReconfigurable();
84 
85   const lldb_private::RegisterInfo *
86   GetRegisterInfo(llvm::StringRef reg_name) const;
87 
88   typedef std::vector<lldb_private::RegisterInfo> reg_collection;
89   llvm::iterator_range<reg_collection::const_iterator> registers() const {
90     return llvm::iterator_range<reg_collection::const_iterator>(m_regs);
91   }
92 
93 protected:
94   // Classes that inherit from DynamicRegisterInfo can see and modify these
95   typedef std::vector<lldb_private::RegisterSet> set_collection;
96   typedef std::vector<uint32_t> reg_num_collection;
97   typedef std::vector<reg_num_collection> set_reg_num_collection;
98   typedef std::vector<lldb_private::ConstString> name_collection;
99   typedef std::map<uint32_t, reg_num_collection> reg_to_regs_map;
100   typedef std::map<uint32_t, uint32_t> reg_offset_map;
101 
102   llvm::Expected<uint32_t> ByteOffsetFromSlice(uint32_t index,
103                                                llvm::StringRef slice_str,
104                                                lldb::ByteOrder byte_order);
105   llvm::Expected<uint32_t> ByteOffsetFromComposite(
106       uint32_t index, lldb_private::StructuredData::Array &composite_reg_list,
107       lldb::ByteOrder byte_order);
108   llvm::Expected<uint32_t> ByteOffsetFromRegInfoDict(
109       uint32_t index, lldb_private::StructuredData::Dictionary &reg_info_dict,
110       lldb::ByteOrder byte_order);
111 
112   void MoveFrom(DynamicRegisterInfo &&info);
113 
114   void Finalize(const lldb_private::ArchSpec &arch);
115 
116   void ConfigureOffsets();
117 
118   reg_collection m_regs;
119   set_collection m_sets;
120   set_reg_num_collection m_set_reg_nums;
121   name_collection m_set_names;
122   reg_to_regs_map m_value_regs_map;
123   reg_to_regs_map m_invalidate_regs_map;
124   reg_offset_map m_value_reg_offset_map;
125   size_t m_reg_data_byte_size = 0u; // The number of bytes required to store
126                                     // all registers
127   bool m_finalized = false;
128   bool m_is_reconfigurable = false;
129 };
130 
131 void addSupplementaryRegister(std::vector<DynamicRegisterInfo::Register> &regs,
132                               DynamicRegisterInfo::Register new_reg_info);
133 
134 } // namespace lldb_private
135 
136 #endif // LLDB_TARGET_DYNAMICREGISTERINFO_H
137