xref: /src/contrib/llvm-project/lldb/source/Target/RegisterNumber.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1cfca06d7SDimitry Andric //===-- RegisterNumber.cpp ------------------------------------------------===//
2205afe67SEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6205afe67SEd Maste //
7205afe67SEd Maste //===----------------------------------------------------------------------===//
8205afe67SEd Maste 
974a628f7SDimitry Andric #include "lldb/Target/RegisterNumber.h"
10205afe67SEd Maste #include "lldb/Target/RegisterContext.h"
1114f1b3e8SDimitry Andric #include "lldb/Target/Thread.h"
12205afe67SEd Maste 
13205afe67SEd Maste using namespace lldb_private;
14205afe67SEd Maste 
RegisterNumber(lldb_private::Thread & thread,lldb::RegisterKind kind,uint32_t num)1514f1b3e8SDimitry Andric RegisterNumber::RegisterNumber(lldb_private::Thread &thread,
1614f1b3e8SDimitry Andric                                lldb::RegisterKind kind, uint32_t num)
1714f1b3e8SDimitry Andric     : m_reg_ctx_sp(thread.GetRegisterContext()), m_regnum(num), m_kind(kind),
1814f1b3e8SDimitry Andric       m_kind_regnum_map(), m_name("") {
1914f1b3e8SDimitry Andric   if (m_reg_ctx_sp.get()) {
2014f1b3e8SDimitry Andric     const lldb_private::RegisterInfo *reginfo =
2114f1b3e8SDimitry Andric         m_reg_ctx_sp->GetRegisterInfoAtIndex(
2214f1b3e8SDimitry Andric             GetAsKind(lldb::eRegisterKindLLDB));
2314f1b3e8SDimitry Andric     if (reginfo && reginfo->name) {
24205afe67SEd Maste       m_name = reginfo->name;
25205afe67SEd Maste     }
26205afe67SEd Maste   }
27205afe67SEd Maste }
28205afe67SEd Maste 
RegisterNumber()29344a3780SDimitry Andric RegisterNumber::RegisterNumber() : m_reg_ctx_sp(), m_kind_regnum_map() {}
30205afe67SEd Maste 
init(lldb_private::Thread & thread,lldb::RegisterKind kind,uint32_t num)3114f1b3e8SDimitry Andric void RegisterNumber::init(lldb_private::Thread &thread, lldb::RegisterKind kind,
3214f1b3e8SDimitry Andric                           uint32_t num) {
33205afe67SEd Maste   m_reg_ctx_sp = thread.GetRegisterContext();
34205afe67SEd Maste   m_regnum = num;
35205afe67SEd Maste   m_kind = kind;
3614f1b3e8SDimitry Andric   if (m_reg_ctx_sp.get()) {
3714f1b3e8SDimitry Andric     const lldb_private::RegisterInfo *reginfo =
3814f1b3e8SDimitry Andric         m_reg_ctx_sp->GetRegisterInfoAtIndex(
3914f1b3e8SDimitry Andric             GetAsKind(lldb::eRegisterKindLLDB));
4014f1b3e8SDimitry Andric     if (reginfo && reginfo->name) {
41205afe67SEd Maste       m_name = reginfo->name;
42205afe67SEd Maste     }
43205afe67SEd Maste   }
44205afe67SEd Maste }
45205afe67SEd Maste 
operator =(const RegisterNumber & rhs)4614f1b3e8SDimitry Andric const RegisterNumber &RegisterNumber::operator=(const RegisterNumber &rhs) {
47205afe67SEd Maste   m_reg_ctx_sp = rhs.m_reg_ctx_sp;
48205afe67SEd Maste   m_regnum = rhs.m_regnum;
49205afe67SEd Maste   m_kind = rhs.m_kind;
50205afe67SEd Maste   for (auto it : rhs.m_kind_regnum_map)
51205afe67SEd Maste     m_kind_regnum_map[it.first] = it.second;
52205afe67SEd Maste   m_name = rhs.m_name;
53205afe67SEd Maste   return *this;
54205afe67SEd Maste }
55205afe67SEd Maste 
operator ==(RegisterNumber & rhs)5614f1b3e8SDimitry Andric bool RegisterNumber::operator==(RegisterNumber &rhs) {
57205afe67SEd Maste   if (IsValid() != rhs.IsValid())
58205afe67SEd Maste     return false;
59205afe67SEd Maste 
6014f1b3e8SDimitry Andric   if (m_kind == rhs.m_kind) {
6194994d37SDimitry Andric     return m_regnum == rhs.m_regnum;
62205afe67SEd Maste   }
63205afe67SEd Maste 
64205afe67SEd Maste   uint32_t rhs_regnum = rhs.GetAsKind(m_kind);
6514f1b3e8SDimitry Andric   if (rhs_regnum != LLDB_INVALID_REGNUM) {
6694994d37SDimitry Andric     return m_regnum == rhs_regnum;
67205afe67SEd Maste   }
68205afe67SEd Maste   uint32_t lhs_regnum = GetAsKind(rhs.m_kind);
6994994d37SDimitry Andric   { return lhs_regnum == rhs.m_regnum; }
70205afe67SEd Maste   return false;
71205afe67SEd Maste }
72205afe67SEd Maste 
operator !=(RegisterNumber & rhs)7314f1b3e8SDimitry Andric bool RegisterNumber::operator!=(RegisterNumber &rhs) { return !(*this == rhs); }
7414f1b3e8SDimitry Andric 
IsValid() const7514f1b3e8SDimitry Andric bool RegisterNumber::IsValid() const {
7614f1b3e8SDimitry Andric   return m_reg_ctx_sp.get() && m_kind != lldb::kNumRegisterKinds &&
7714f1b3e8SDimitry Andric          m_regnum != LLDB_INVALID_REGNUM;
78205afe67SEd Maste }
79205afe67SEd Maste 
GetAsKind(lldb::RegisterKind kind)8014f1b3e8SDimitry Andric uint32_t RegisterNumber::GetAsKind(lldb::RegisterKind kind) {
81205afe67SEd Maste   if (m_regnum == LLDB_INVALID_REGNUM)
82205afe67SEd Maste     return LLDB_INVALID_REGNUM;
83205afe67SEd Maste 
84205afe67SEd Maste   if (kind == m_kind)
85205afe67SEd Maste     return m_regnum;
86205afe67SEd Maste 
87205afe67SEd Maste   Collection::iterator iter = m_kind_regnum_map.find(kind);
8814f1b3e8SDimitry Andric   if (iter != m_kind_regnum_map.end()) {
89205afe67SEd Maste     return iter->second;
90205afe67SEd Maste   }
91205afe67SEd Maste   uint32_t output_regnum = LLDB_INVALID_REGNUM;
9214f1b3e8SDimitry Andric   if (m_reg_ctx_sp &&
9314f1b3e8SDimitry Andric       m_reg_ctx_sp->ConvertBetweenRegisterKinds(m_kind, m_regnum, kind,
9414f1b3e8SDimitry Andric                                                 output_regnum) &&
9514f1b3e8SDimitry Andric       output_regnum != LLDB_INVALID_REGNUM) {
96205afe67SEd Maste     m_kind_regnum_map[kind] = output_regnum;
97205afe67SEd Maste   }
98205afe67SEd Maste   return output_regnum;
99205afe67SEd Maste }
100205afe67SEd Maste 
GetRegisterNumber() const10114f1b3e8SDimitry Andric uint32_t RegisterNumber::GetRegisterNumber() const { return m_regnum; }
102205afe67SEd Maste 
GetRegisterKind() const10314f1b3e8SDimitry Andric lldb::RegisterKind RegisterNumber::GetRegisterKind() const { return m_kind; }
104205afe67SEd Maste 
GetName()10514f1b3e8SDimitry Andric const char *RegisterNumber::GetName() { return m_name; }
106