1 //===-- Value.h -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Value_h_
11 #define liblldb_Value_h_
13 #include "lldb/Core/Scalar.h"
14 #include "lldb/Symbol/CompilerType.h"
15 #include "lldb/Utility/DataBufferHeap.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/lldb-enumerations.h" // for ByteOrder, ByteOrder::eB...
18 #include "lldb/lldb-private-enumerations.h" // for AddressType
19 #include "lldb/lldb-private-types.h" // for type128, RegisterInfo
21 #include "llvm/ADT/APInt.h" // for APInt
25 #include <stdint.h> // for uint8_t, uint32_t, uint64_t
26 #include <string.h> // for size_t, memcpy
28 namespace lldb_private {
31 namespace lldb_private {
32 class ExecutionContext;
34 namespace lldb_private {
37 namespace lldb_private {
40 namespace lldb_private {
43 namespace lldb_private {
47 namespace lldb_private {
51 // Values Less than zero are an error, greater than or equal to zero
52 // returns what the Scalar result is.
54 // m_value contains...
55 // ============================
56 eValueTypeScalar, // raw scalar value
57 eValueTypeVector, // byte array of m_vector.length with endianness of
58 // m_vector.byte_order
59 eValueTypeFileAddress, // file address value
60 eValueTypeLoadAddress, // load address value
61 eValueTypeHostAddress // host address value (for memory in the process that
65 enum ContextType // Type that describes Value::m_context
67 // m_context contains...
68 // ====================
69 eContextTypeInvalid, // undefined
70 eContextTypeRegisterInfo, // RegisterInfo * (can be a scalar or a vector
72 eContextTypeLLDBType, // lldb_private::Type *
73 eContextTypeVariable // lldb_private::Variable *
76 const static size_t kMaxByteSize = 32u;
79 // The byte array must be big enough to hold vector registers for any
81 uint8_t bytes[kMaxByteSize];
83 lldb::ByteOrder byte_order;
85 Vector() : length(0), byte_order(lldb::eByteOrderInvalid) {}
87 Vector(const Vector &vector) { *this = vector; }
88 const Vector &operator=(const Vector &vector) {
89 SetBytes(vector.bytes, vector.length, vector.byte_order);
93 void Clear() { length = 0; }
95 bool SetBytes(const void *bytes, size_t length,
96 lldb::ByteOrder byte_order) {
97 this->length = length;
98 this->byte_order = byte_order;
100 ::memcpy(this->bytes, bytes,
101 length < kMaxByteSize ? length : kMaxByteSize);
105 bool IsValid() const {
106 return (length > 0 && length < kMaxByteSize &&
107 byte_order != lldb::eByteOrderInvalid);
109 // Casts a vector, if valid, to an unsigned int of matching or largest
111 // Truncates to the beginning of the vector if required.
112 // Returns a default constructed Scalar if the Vector data is internally
114 llvm::APInt rhs = llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
115 ((type128 *)bytes)->x);
116 Scalar GetAsScalar() const {
120 scalar = *(const uint8_t *)bytes;
121 else if (length == 2)
122 scalar = *(const uint16_t *)bytes;
123 else if (length == 4)
124 scalar = *(const uint32_t *)bytes;
125 else if (length == 8)
126 scalar = *(const uint64_t *)bytes;
127 else if (length >= 16)
135 Value(const Scalar &scalar);
136 Value(const Vector &vector);
137 Value(const void *bytes, int len);
138 Value(const Value &rhs);
140 void SetBytes(const void *bytes, int len);
142 void AppendBytes(const void *bytes, int len);
144 Value &operator=(const Value &rhs);
146 const CompilerType &GetCompilerType();
148 void SetCompilerType(const CompilerType &compiler_type);
150 ValueType GetValueType() const;
152 AddressType GetValueAddressType() const;
154 ContextType GetContextType() const { return m_context_type; }
156 void SetValueType(ValueType value_type) { m_value_type = value_type; }
158 void ClearContext() {
160 m_context_type = eContextTypeInvalid;
163 void SetContext(ContextType context_type, void *p) {
164 m_context_type = context_type;
166 if (m_context_type == eContextTypeRegisterInfo) {
167 RegisterInfo *reg_info = GetRegisterInfo();
168 if (reg_info->encoding == lldb::eEncodingVector &&
169 m_vector.byte_order != lldb::eByteOrderInvalid)
170 SetValueType(eValueTypeScalar);
174 RegisterInfo *GetRegisterInfo() const;
178 Scalar &ResolveValue(ExecutionContext *exe_ctx);
180 const Scalar &GetScalar() const { return m_value; }
182 const Vector &GetVector() const { return m_vector; }
184 Scalar &GetScalar() { return m_value; }
186 Vector &GetVector() { return m_vector; }
188 bool SetVectorBytes(const Vector &vector) {
190 return m_vector.IsValid();
193 bool SetVectorBytes(uint8_t *bytes, size_t length,
194 lldb::ByteOrder byte_order) {
195 return m_vector.SetBytes(bytes, length, byte_order);
198 bool SetScalarFromVector() {
199 if (m_vector.IsValid()) {
200 m_value = m_vector.GetAsScalar();
206 size_t ResizeData(size_t len);
208 size_t AppendDataToHostBuffer(const Value &rhs);
210 DataBufferHeap &GetBuffer() { return m_data_buffer; }
212 const DataBufferHeap &GetBuffer() const { return m_data_buffer; }
214 bool ValueOf(ExecutionContext *exe_ctx);
216 Variable *GetVariable();
218 void Dump(Stream *strm);
220 lldb::Format GetValueDefaultFormat();
222 uint64_t GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx);
224 Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
225 uint32_t data_offset,
226 Module *module); // Can be nullptr
228 static const char *GetValueTypeAsCString(ValueType context_type);
230 static const char *GetContextTypeAsCString(ContextType context_type);
232 bool GetData(DataExtractor &data);
239 CompilerType m_compiler_type;
241 ValueType m_value_type;
242 ContextType m_context_type;
243 DataBufferHeap m_data_buffer;
248 ValueList() : m_values() {}
250 ValueList(const ValueList &rhs);
252 ~ValueList() = default;
254 const ValueList &operator=(const ValueList &rhs);
256 // void InsertValue (Value *value, size_t idx);
257 void PushValue(const Value &value);
260 Value *GetValueAtIndex(size_t idx);
264 typedef std::vector<Value> collection;
269 } // namespace lldb_private
271 #endif // liblldb_Value_h_