1 //===-- RegisterNumber.cpp ------------------------------------------------===//
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 #include "lldb/Target/RegisterNumber.h"
10 #include "lldb/Target/RegisterContext.h"
11 #include "lldb/Target/Thread.h"
12
13 using namespace lldb_private;
14
RegisterNumber(lldb_private::Thread & thread,lldb::RegisterKind kind,uint32_t num)15 RegisterNumber::RegisterNumber(lldb_private::Thread &thread,
16 lldb::RegisterKind kind, uint32_t num)
17 : m_reg_ctx_sp(thread.GetRegisterContext()), m_regnum(num), m_kind(kind),
18 m_kind_regnum_map(), m_name("") {
19 if (m_reg_ctx_sp.get()) {
20 const lldb_private::RegisterInfo *reginfo =
21 m_reg_ctx_sp->GetRegisterInfoAtIndex(
22 GetAsKind(lldb::eRegisterKindLLDB));
23 if (reginfo && reginfo->name) {
24 m_name = reginfo->name;
25 }
26 }
27 }
28
RegisterNumber()29 RegisterNumber::RegisterNumber()
30 : m_reg_ctx_sp(), m_regnum(LLDB_INVALID_REGNUM),
31 m_kind(lldb::kNumRegisterKinds), m_kind_regnum_map(), m_name(nullptr) {}
32
init(lldb_private::Thread & thread,lldb::RegisterKind kind,uint32_t num)33 void RegisterNumber::init(lldb_private::Thread &thread, lldb::RegisterKind kind,
34 uint32_t num) {
35 m_reg_ctx_sp = thread.GetRegisterContext();
36 m_regnum = num;
37 m_kind = kind;
38 if (m_reg_ctx_sp.get()) {
39 const lldb_private::RegisterInfo *reginfo =
40 m_reg_ctx_sp->GetRegisterInfoAtIndex(
41 GetAsKind(lldb::eRegisterKindLLDB));
42 if (reginfo && reginfo->name) {
43 m_name = reginfo->name;
44 }
45 }
46 }
47
operator =(const RegisterNumber & rhs)48 const RegisterNumber &RegisterNumber::operator=(const RegisterNumber &rhs) {
49 m_reg_ctx_sp = rhs.m_reg_ctx_sp;
50 m_regnum = rhs.m_regnum;
51 m_kind = rhs.m_kind;
52 for (auto it : rhs.m_kind_regnum_map)
53 m_kind_regnum_map[it.first] = it.second;
54 m_name = rhs.m_name;
55 return *this;
56 }
57
operator ==(RegisterNumber & rhs)58 bool RegisterNumber::operator==(RegisterNumber &rhs) {
59 if (IsValid() != rhs.IsValid())
60 return false;
61
62 if (m_kind == rhs.m_kind) {
63 return m_regnum == rhs.m_regnum;
64 }
65
66 uint32_t rhs_regnum = rhs.GetAsKind(m_kind);
67 if (rhs_regnum != LLDB_INVALID_REGNUM) {
68 return m_regnum == rhs_regnum;
69 }
70 uint32_t lhs_regnum = GetAsKind(rhs.m_kind);
71 { return lhs_regnum == rhs.m_regnum; }
72 return false;
73 }
74
operator !=(RegisterNumber & rhs)75 bool RegisterNumber::operator!=(RegisterNumber &rhs) { return !(*this == rhs); }
76
IsValid() const77 bool RegisterNumber::IsValid() const {
78 return m_reg_ctx_sp.get() && m_kind != lldb::kNumRegisterKinds &&
79 m_regnum != LLDB_INVALID_REGNUM;
80 }
81
GetAsKind(lldb::RegisterKind kind)82 uint32_t RegisterNumber::GetAsKind(lldb::RegisterKind kind) {
83 if (m_regnum == LLDB_INVALID_REGNUM)
84 return LLDB_INVALID_REGNUM;
85
86 if (kind == m_kind)
87 return m_regnum;
88
89 Collection::iterator iter = m_kind_regnum_map.find(kind);
90 if (iter != m_kind_regnum_map.end()) {
91 return iter->second;
92 }
93 uint32_t output_regnum = LLDB_INVALID_REGNUM;
94 if (m_reg_ctx_sp &&
95 m_reg_ctx_sp->ConvertBetweenRegisterKinds(m_kind, m_regnum, kind,
96 output_regnum) &&
97 output_regnum != LLDB_INVALID_REGNUM) {
98 m_kind_regnum_map[kind] = output_regnum;
99 }
100 return output_regnum;
101 }
102
GetRegisterNumber() const103 uint32_t RegisterNumber::GetRegisterNumber() const { return m_regnum; }
104
GetRegisterKind() const105 lldb::RegisterKind RegisterNumber::GetRegisterKind() const { return m_kind; }
106
GetName()107 const char *RegisterNumber::GetName() { return m_name; }
108