LLDB mainline
VectorType.cpp
Go to the documentation of this file.
1//===-- VectorType.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
10
14#include "lldb/Target/Target.h"
17
19#include "lldb/Utility/Log.h"
20#include <optional>
21
22using namespace lldb;
23using namespace lldb_private;
24using namespace lldb_private::formatters;
25
27 CompilerType element_type,
28 TypeSystemSP type_system) {
29 lldbassert(type_system && "type_system needs to be not NULL");
30 if (!type_system)
31 return {};
32
33 switch (format) {
36 return type_system->GetBuiltinTypeForEncodingAndBitSize(
37 eEncodingUint, 8 * type_system->GetPointerByteSize());
38
40 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeBool);
41
47 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
48
49 case lldb::eFormatComplex /* lldb::eFormatComplexFloat */:
50 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloatComplex);
51
53 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar)
54 .GetPointerType();
55
57 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
58
62 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeInt);
63
65 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
66
69
71 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeUnsignedInt);
72
74 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
75
77 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754,
78 32);
79
81 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754,
82 64);
83
85 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 16);
86
88 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 32);
89
91 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 64);
92
94 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 8);
95
97 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 128);
98
100 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
101
103 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
104
106 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 64);
107
109 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
110
112 return element_type;
113
121 default:
122 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
123 }
124}
125
127 CompilerType element_type) {
128 switch (format) {
130 return lldb::eFormatChar;
131
134 return lldb::eFormatFloat;
135
141
148
156 return eFormatHex;
157
159 // special case the (default, char) combination to actually display as an
160 // integer value most often, you won't want to see the ASCII characters...
161 // (and if you do, eFormatChar is a keystroke away)
162 bool is_char = element_type.IsCharType();
163 bool is_signed = false;
164 element_type.IsIntegerType(is_signed);
165 return is_char ? (is_signed ? lldb::eFormatDecimal : eFormatHex) : format;
166 } break;
167
168 default:
169 return format;
170 }
171}
172
173/// Calculates the number of elements stored in a container (with
174/// element type 'container_elem_type') as if it had elements of type
175/// 'element_type'.
176///
177/// For example, a container of type
178/// `uint8_t __attribute__((vector_size(16)))` has 16 elements.
179/// But calling `CalculateNumChildren` with an 'element_type'
180/// of `float` (4-bytes) will return `4` because we are interpreting
181/// the byte-array as a `float32[]`.
182///
183/// \param[in] container_elem_type The type of the elements stored
184/// in the container we are calculating the children of.
185///
186/// \param[in] num_elements Number of 'container_elem_type's our
187/// container stores.
188///
189/// \param[in] element_type The type of elements we interpret
190/// container_type to contain for the purposes of calculating
191/// the number of children.
192///
193/// \returns The number of elements stored in a container of
194/// type 'element_type'. Returns a std::nullopt if the
195/// size of the container is not a multiple of 'element_type'
196/// or if an error occurs.
197static std::optional<size_t>
198CalculateNumChildren(CompilerType container_elem_type, uint64_t num_elements,
199 CompilerType element_type) {
200 std::optional<uint64_t> container_elem_size =
201 container_elem_type.GetByteSize(/* exe_scope */ nullptr);
202 if (!container_elem_size)
203 return {};
204
205 auto container_size = *container_elem_size * num_elements;
206
207 std::optional<uint64_t> element_size =
208 element_type.GetByteSize(/* exe_scope */ nullptr);
209 if (!element_size || !*element_size)
210 return {};
211
212 if (container_size % *element_size)
213 return {};
214
215 return container_size / *element_size;
216}
217
218namespace lldb_private {
219namespace formatters {
220
222public:
224 : SyntheticChildrenFrontEnd(*valobj_sp), m_child_type() {}
225
226 ~VectorTypeSyntheticFrontEnd() override = default;
227
228 llvm::Expected<uint32_t> CalculateNumChildren() override {
229 return m_num_children;
230 }
231
232 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
233 auto num_children_or_err = CalculateNumChildren();
234 if (!num_children_or_err)
236 nullptr, Status::FromError(num_children_or_err.takeError()));
237 if (idx >= *num_children_or_err)
238 return {};
239 std::optional<uint64_t> size = m_child_type.GetByteSize(nullptr);
240 if (!size)
241 return {};
242 auto offset = idx * *size;
243 StreamString idx_name;
244 idx_name.Printf("[%" PRIu64 "]", (uint64_t)idx);
246 offset, m_child_type, true, ConstString(idx_name.GetString())));
247 if (!child_sp)
248 return child_sp;
249
250 child_sp->SetFormat(m_item_format);
251
252 return child_sp;
253 }
254
258 CompilerType element_type;
259 uint64_t num_elements;
260 parent_type.IsVectorType(&element_type, &num_elements);
262 m_parent_format, element_type,
263 parent_type.GetTypeSystem().GetSharedPointer());
265 ::CalculateNumChildren(element_type, num_elements, m_child_type)
266 .value_or(0);
269 }
270
271 bool MightHaveChildren() override { return true; }
272
273 size_t GetIndexOfChildWithName(ConstString name) override {
274 const char *item_name = name.GetCString();
275 uint32_t idx = ExtractIndexFromString(item_name);
276 if (idx < UINT32_MAX && idx >= CalculateNumChildrenIgnoringErrors())
277 return UINT32_MAX;
278 return idx;
279 }
280
281private:
285 size_t m_num_children = 0;
286};
287
288} // namespace formatters
289} // namespace lldb_private
290
292 ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
293 auto synthetic_children =
294 VectorTypeSyntheticFrontEndCreator(nullptr, valobj.GetSP());
295 if (!synthetic_children)
296 return false;
297
298 synthetic_children->Update();
299
300 s.PutChar('(');
301 bool first = true;
302
303 size_t idx = 0,
304 len = synthetic_children->CalculateNumChildrenIgnoringErrors();
305
306 for (; idx < len; idx++) {
307 auto child_sp = synthetic_children->GetChildAtIndex(idx);
308 if (!child_sp)
309 continue;
310 child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
312
313 const char *child_value = child_sp->GetValueAsCString();
314 if (child_value && *child_value) {
315 if (first) {
316 s.Printf("%s", child_value);
317 first = false;
318 } else {
319 s.Printf(", %s", child_value);
320 }
321 }
322 }
323
324 s.PutChar(')');
325
326 return true;
327}
328
332 if (!valobj_sp)
333 return nullptr;
334 return new VectorTypeSyntheticFrontEnd(valobj_sp);
335}
#define lldbassert(x)
Definition: LLDBAssert.h:15
static lldb::Format GetItemFormatForFormat(lldb::Format format, CompilerType element_type)
Definition: VectorType.cpp:126
static CompilerType GetCompilerTypeForFormat(lldb::Format format, CompilerType element_type, TypeSystemSP type_system)
Definition: VectorType.cpp:26
static std::optional< size_t > CalculateNumChildren(CompilerType container_elem_type, uint64_t num_elements, CompilerType element_type)
Calculates the number of elements stored in a container (with element type 'container_elem_type') as ...
Definition: VectorType.cpp:198
lldb::TypeSystemSP GetSharedPointer() const
Definition: CompilerType.h:85
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
bool IsIntegerType(bool &is_signed) const
bool IsVectorType(CompilerType *element_type=nullptr, uint64_t *size=nullptr) const
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition: Status.cpp:137
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutChar(char ch)
Definition: Stream.cpp:131
uint32_t CalculateNumChildrenIgnoringErrors(uint32_t max=UINT32_MAX)
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address=LLDB_INVALID_ADDRESS)
CompilerType GetCompilerType()
Definition: ValueObject.h:352
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:569
lldb::Format GetFormat() const
virtual lldb::ValueObjectSP GetSyntheticChildAtOffset(uint32_t offset, const CompilerType &type, bool can_create, ConstString name_const_str=ConstString())
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
Definition: VectorType.cpp:255
llvm::Expected< uint32_t > CalculateNumChildren() override
Definition: VectorType.cpp:228
VectorTypeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Definition: VectorType.cpp:223
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
Definition: VectorType.cpp:232
size_t GetIndexOfChildWithName(ConstString name) override
Definition: VectorType.cpp:273
#define UINT32_MAX
Definition: lldb-defines.h:19
bool VectorTypeSummaryProvider(ValueObject &, Stream &, const TypeSummaryOptions &)
Definition: VectorType.cpp:291
SyntheticChildrenFrontEnd * VectorTypeSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
Definition: VectorType.cpp:330
size_t ExtractIndexFromString(const char *item_name)
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::TypeSystem > TypeSystemSP
Definition: lldb-forward.h:469
@ eBasicTypeFloatComplex
@ eBasicTypeUnsignedInt
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
@ eRefetch
Children need to be recomputed dynamically.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:484
Format
Display format definitions.
@ eFormatCString
NULL terminated C strings.
@ eFormatCharArray
Print characters with no single quotes, used for character arrays that can contain non printable char...
@ eFormatInstruction
Disassemble an opcode.
@ eFormatVectorOfChar
@ eFormatVectorOfUInt64
@ eFormatVoid
Do not print this.
@ eFormatVectorOfSInt64
@ eFormatComplex
Floating point complex type.
@ eFormatHexFloat
ISO C99 hex float string.
@ eFormatBytesWithASCII
@ eFormatOSType
OS character codes encoded into an integer 'PICT' 'text' etc...
@ eFormatUnicode16
@ eFormatAddressInfo
Describe what an address points to (func + offset.
@ eFormatVectorOfUInt128
@ eFormatVectorOfUInt8
@ eFormatVectorOfFloat32
@ eFormatVectorOfSInt32
@ eFormatUnicode32
@ eFormatVectorOfSInt8
@ eFormatVectorOfUInt16
@ eFormatHexUppercase
@ eFormatVectorOfFloat64
@ eFormatCharPrintable
Only printable characters, '.' if not printable.
@ eFormatComplexInteger
Integer complex type.
@ eFormatVectorOfSInt16
@ eFormatVectorOfUInt32
@ eEncodingIEEE754
float
@ eEncodingUint
unsigned integer
@ eEncodingSint
signed integer
@ eDynamicDontRunTarget