LLDB mainline
RegisterType.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10
11#include "lldb/Utility/Log.h"
12#include "lldb/Utility/Stream.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/Support/Casting.h"
15#include "llvm/Support/raw_ostream.h"
16
17#include <atomic>
18#include <cassert>
19#include <cinttypes>
20#include <limits>
21
22using namespace lldb_private;
23
24static std::atomic<uint64_t> g_next_register_type_uid{1};
25
27 : m_kind(kind), m_id(std::move(id)),
28 m_uid(g_next_register_type_uid.fetch_add(1, std::memory_order_relaxed)) {}
29
31 std::unordered_set<std::string> &previously_emitted,
32 const RegisterType *user) const {
33 // XML type references use IDs, so definitions must also be unique by ID.
34 if (!previously_emitted.insert(GetID()).second)
35 return;
36
37 // Emit this type's dependencies first.
38 for (auto dep : m_dependencies)
39 dep->ToXML(strm, previously_emitted, this);
40
41 // Finally emit this type.
42 ToXMLElement(strm, user);
43}
44
45void RegisterType::PrintXMLAttributeValue(Stream &strm, llvm::StringRef value) {
46 std::string escaped;
47 llvm::raw_string_ostream escape_strm(escaped);
48 llvm::printHTMLEscaped(value, escape_strm);
49 strm << escaped;
50}
51
53 lldb::Encoding encoding,
54 lldb::Format format,
55 std::optional<uint64_t> byte_size)
56 : RegisterType(eRegisterTypeKindBuiltin, std::move(id)),
57 m_encoding(encoding), m_format(format), m_byte_size(byte_size) {}
58
59void RegisterTypeBuiltin::ToXML(Stream &, std::unordered_set<std::string> &,
60 const RegisterType *) const {}
61
63
65 const RegisterType *element_type,
66 uint32_t count)
67 : RegisterType(eRegisterTypeKindVector, std::move(id)),
68 m_element_type(element_type), m_count(count) {
69 assert(m_element_type && "Vector element type cannot be null");
70 assert(m_count && "Vector element count cannot be zero");
72}
73
74std::optional<uint64_t> RegisterTypeVector::GetByteSize() const {
75 std::optional<uint64_t> element_size = m_element_type->GetByteSize();
76 if (!element_size ||
77 *element_size > std::numeric_limits<uint64_t>::max() / m_count)
78 return std::nullopt;
79 return *element_size * m_count;
80}
81
82bool RegisterTypeVector::IsByteSizeCompatible(uint64_t byte_size) const {
83 if (!byte_size || byte_size % m_count)
84 return false;
85
86 uint64_t element_byte_size = byte_size / m_count;
87 if (std::optional<uint64_t> fixed_size = m_element_type->GetByteSize())
88 return *fixed_size == element_byte_size;
89 if (const auto *vector = llvm::dyn_cast<RegisterTypeVector>(m_element_type))
90 return vector->IsByteSizeCompatible(element_byte_size);
91 return llvm::isa<RegisterTypeBuiltin>(m_element_type);
92}
93
95 LLDB_LOG(log, "ID: \"{0}\" Element type: \"{1}\" Count: {2}", GetID().c_str(),
96 m_element_type->GetID().c_str(), m_count);
97}
98
100 const RegisterType *) const {
101 strm.Indent();
102 strm << "<vector id=\"";
104 strm << "\" type=\"";
106 strm.Printf("\" count=\"%" PRIu32 "\"/>\n", m_count);
107}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
static std::atomic< uint64_t > g_next_register_type_uid
void ToXMLElement(Stream &strm, const RegisterType *user=nullptr) const override
Output the register type as an XML element.
RegisterTypeBuiltin(std::string id, lldb::Encoding encoding, lldb::Format format, std::optional< uint64_t > byte_size)
A missing byte size means the size depends on the target.
const lldb::Encoding m_encoding
const std::optional< uint64_t > m_byte_size
void ToXML(Stream &strm, std::unordered_set< std::string > &previously_emitted, const RegisterType *user=nullptr) const override
Output XML that describes this type, to be inserted into a target XML file.
RegisterTypeVector(std::string id, const RegisterType *element_type, uint32_t count)
bool IsByteSizeCompatible(uint64_t byte_size) const
void ToXMLElement(Stream &strm, const RegisterType *user=nullptr) const override
Output the register type as an XML element.
std::optional< uint64_t > GetByteSize() const override
Return this type's fixed size in bytes, if it has one.
const RegisterType * m_element_type
RegisterType(RegisterTypeKind kind, std::string id)
virtual void ToXMLElement(Stream &strm, const RegisterType *user=nullptr) const =0
Output the register type as an XML element.
const std::string & GetID() const
const RegisterTypeKind m_kind
std::vector< const RegisterType * > m_dependencies
void SetDependencies(std::vector< const RegisterType * > dependencies)
const std::string m_id
static void PrintXMLAttributeValue(Stream &strm, llvm::StringRef value)
Print a string escaped for use as an XML attribute value.
virtual void ToXML(Stream &strm, std::unordered_set< std::string > &previously_emitted, const RegisterType *user=nullptr) const
Output XML that describes this type, to be inserted into a target XML file.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition Stream.cpp:157
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
A class that represents a running process on the host machine.
Format
Display format definitions.
Encoding
Register encoding definitions.