1 //===-- Value.h -------------------------------------------------*- C++ -*-===//
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 #ifndef liblldb_Value_h_
10 #define liblldb_Value_h_
12 #include "lldb/Symbol/CompilerType.h"
13 #include "lldb/Utility/DataBufferHeap.h"
14 #include "lldb/Utility/Scalar.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/lldb-enumerations.h"
17 #include "lldb/lldb-private-enumerations.h"
18 #include "lldb/lldb-private-types.h"
20 #include "llvm/ADT/APInt.h"
27 namespace lldb_private {
29 class ExecutionContext;
36 namespace lldb_private {
40 // Values Less than zero are an error, greater than or equal to zero returns
41 // what the Scalar result is.
43 // m_value contains...
44 // ============================
45 eValueTypeScalar, // raw scalar value
46 eValueTypeVector, // byte array of m_vector.length with endianness of
47 // m_vector.byte_order
48 eValueTypeFileAddress, // file address value
49 eValueTypeLoadAddress, // load address value
50 eValueTypeHostAddress // host address value (for memory in the process that
54 enum ContextType // Type that describes Value::m_context
56 // m_context contains...
57 // ====================
58 eContextTypeInvalid, // undefined
59 eContextTypeRegisterInfo, // RegisterInfo * (can be a scalar or a vector
61 eContextTypeLLDBType, // lldb_private::Type *
62 eContextTypeVariable // lldb_private::Variable *
65 const static size_t kMaxByteSize = 32u;
68 // The byte array must be big enough to hold vector registers for any
70 uint8_t bytes[kMaxByteSize];
72 lldb::ByteOrder byte_order;
74 Vector() : length(0), byte_order(lldb::eByteOrderInvalid) {}
76 Vector(const Vector &vector) { *this = vector; }
77 const Vector &operator=(const Vector &vector) {
78 SetBytes(vector.bytes, vector.length, vector.byte_order);
82 void Clear() { length = 0; }
84 bool SetBytes(const void *bytes, size_t length,
85 lldb::ByteOrder byte_order) {
86 this->length = length;
87 this->byte_order = byte_order;
89 ::memcpy(this->bytes, bytes,
90 length < kMaxByteSize ? length : kMaxByteSize);
94 bool IsValid() const {
95 return (length > 0 && length < kMaxByteSize &&
96 byte_order != lldb::eByteOrderInvalid);
98 // Casts a vector, if valid, to an unsigned int of matching or largest
99 // supported size. Truncates to the beginning of the vector if required.
100 // Returns a default constructed Scalar if the Vector data is internally
102 llvm::APInt rhs = llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
103 ((type128 *)bytes)->x);
104 Scalar GetAsScalar() const {
108 scalar = *(const uint8_t *)bytes;
109 else if (length == 2)
110 scalar = *(const uint16_t *)bytes;
111 else if (length == 4)
112 scalar = *(const uint32_t *)bytes;
113 else if (length == 8)
114 scalar = *(const uint64_t *)bytes;
115 else if (length >= 16)
123 Value(const Scalar &scalar);
124 Value(const Vector &vector);
125 Value(const void *bytes, int len);
126 Value(const Value &rhs);
128 void SetBytes(const void *bytes, int len);
130 void AppendBytes(const void *bytes, int len);
132 Value &operator=(const Value &rhs);
134 const CompilerType &GetCompilerType();
136 void SetCompilerType(const CompilerType &compiler_type);
138 ValueType GetValueType() const;
140 AddressType GetValueAddressType() const;
142 ContextType GetContextType() const { return m_context_type; }
144 void SetValueType(ValueType value_type) { m_value_type = value_type; }
146 void ClearContext() {
148 m_context_type = eContextTypeInvalid;
151 void SetContext(ContextType context_type, void *p) {
152 m_context_type = context_type;
154 if (m_context_type == eContextTypeRegisterInfo) {
155 RegisterInfo *reg_info = GetRegisterInfo();
156 if (reg_info->encoding == lldb::eEncodingVector &&
157 m_vector.byte_order != lldb::eByteOrderInvalid)
158 SetValueType(eValueTypeScalar);
162 RegisterInfo *GetRegisterInfo() const;
166 Scalar &ResolveValue(ExecutionContext *exe_ctx);
168 const Scalar &GetScalar() const { return m_value; }
170 const Vector &GetVector() const { return m_vector; }
172 Scalar &GetScalar() { return m_value; }
174 Vector &GetVector() { return m_vector; }
176 bool SetVectorBytes(const Vector &vector) {
178 return m_vector.IsValid();
181 bool SetVectorBytes(uint8_t *bytes, size_t length,
182 lldb::ByteOrder byte_order) {
183 return m_vector.SetBytes(bytes, length, byte_order);
186 bool SetScalarFromVector() {
187 if (m_vector.IsValid()) {
188 m_value = m_vector.GetAsScalar();
194 size_t ResizeData(size_t len);
196 size_t AppendDataToHostBuffer(const Value &rhs);
198 DataBufferHeap &GetBuffer() { return m_data_buffer; }
200 const DataBufferHeap &GetBuffer() const { return m_data_buffer; }
202 bool ValueOf(ExecutionContext *exe_ctx);
204 Variable *GetVariable();
206 void Dump(Stream *strm);
208 lldb::Format GetValueDefaultFormat();
210 uint64_t GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx);
212 Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
213 Module *module); // Can be nullptr
215 static const char *GetValueTypeAsCString(ValueType context_type);
217 static const char *GetContextTypeAsCString(ContextType context_type);
219 /// Convert this value's file address to a load address, if possible.
220 void ConvertToLoadAddress(Module *module, Target *target);
222 bool GetData(DataExtractor &data);
229 CompilerType m_compiler_type;
231 ValueType m_value_type;
232 ContextType m_context_type;
233 DataBufferHeap m_data_buffer;
238 ValueList() : m_values() {}
240 ValueList(const ValueList &rhs);
242 ~ValueList() = default;
244 const ValueList &operator=(const ValueList &rhs);
246 // void InsertValue (Value *value, size_t idx);
247 void PushValue(const Value &value);
250 Value *GetValueAtIndex(size_t idx);
254 typedef std::vector<Value> collection;
259 } // namespace lldb_private
261 #endif // liblldb_Value_h_