1 //===-- RegisterNumber.cpp ------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/RegisterNumber.h"
10 #include "lldb/Target/RegisterContext.h"
11 #include "lldb/Target/Thread.h"
13 using namespace lldb_private
;
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
;
29 RegisterNumber::RegisterNumber() : m_reg_ctx_sp(), m_kind_regnum_map() {}
31 void RegisterNumber::init(lldb_private::Thread
&thread
, lldb::RegisterKind kind
,
33 m_reg_ctx_sp
= thread
.GetRegisterContext();
36 if (m_reg_ctx_sp
.get()) {
37 const lldb_private::RegisterInfo
*reginfo
=
38 m_reg_ctx_sp
->GetRegisterInfoAtIndex(
39 GetAsKind(lldb::eRegisterKindLLDB
));
40 if (reginfo
&& reginfo
->name
) {
41 m_name
= reginfo
->name
;
46 const RegisterNumber
&RegisterNumber::operator=(const RegisterNumber
&rhs
) {
47 m_reg_ctx_sp
= rhs
.m_reg_ctx_sp
;
48 m_regnum
= rhs
.m_regnum
;
50 for (auto it
: rhs
.m_kind_regnum_map
)
51 m_kind_regnum_map
[it
.first
] = it
.second
;
56 bool RegisterNumber::operator==(RegisterNumber
&rhs
) {
57 if (IsValid() != rhs
.IsValid())
60 if (m_kind
== rhs
.m_kind
) {
61 return m_regnum
== rhs
.m_regnum
;
64 uint32_t rhs_regnum
= rhs
.GetAsKind(m_kind
);
65 if (rhs_regnum
!= LLDB_INVALID_REGNUM
) {
66 return m_regnum
== rhs_regnum
;
68 uint32_t lhs_regnum
= GetAsKind(rhs
.m_kind
);
69 { return lhs_regnum
== rhs
.m_regnum
; }
73 bool RegisterNumber::operator!=(RegisterNumber
&rhs
) { return !(*this == rhs
); }
75 bool RegisterNumber::IsValid() const {
76 return m_reg_ctx_sp
.get() && m_kind
!= lldb::kNumRegisterKinds
&&
77 m_regnum
!= LLDB_INVALID_REGNUM
;
80 uint32_t RegisterNumber::GetAsKind(lldb::RegisterKind kind
) {
81 if (m_regnum
== LLDB_INVALID_REGNUM
)
82 return LLDB_INVALID_REGNUM
;
87 Collection::iterator iter
= m_kind_regnum_map
.find(kind
);
88 if (iter
!= m_kind_regnum_map
.end()) {
91 uint32_t output_regnum
= LLDB_INVALID_REGNUM
;
93 m_reg_ctx_sp
->ConvertBetweenRegisterKinds(m_kind
, m_regnum
, kind
,
95 output_regnum
!= LLDB_INVALID_REGNUM
) {
96 m_kind_regnum_map
[kind
] = output_regnum
;
101 uint32_t RegisterNumber::GetRegisterNumber() const { return m_regnum
; }
103 lldb::RegisterKind
RegisterNumber::GetRegisterKind() const { return m_kind
; }
105 const char *RegisterNumber::GetName() { return m_name
; }