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/Symbol/CompilerType.h"
14 #include "lldb/Utility/DataBufferHeap.h"
15 #include "lldb/Utility/Scalar.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/lldb-enumerations.h"
18 #include "lldb/lldb-private-enumerations.h"
19 #include "lldb/lldb-private-types.h"
21 #include "llvm/ADT/APInt.h"
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 returns
52 // 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
110 // supported size. Truncates to the beginning of the vector if required.
111 // Returns a default constructed Scalar if the Vector data is internally
113 llvm::APInt rhs = llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
114 ((type128 *)bytes)->x);
115 Scalar GetAsScalar() const {
119 scalar = *(const uint8_t *)bytes;
120 else if (length == 2)
121 scalar = *(const uint16_t *)bytes;
122 else if (length == 4)
123 scalar = *(const uint32_t *)bytes;
124 else if (length == 8)
125 scalar = *(const uint64_t *)bytes;
126 else if (length >= 16)
134 Value(const Scalar &scalar);
135 Value(const Vector &vector);
136 Value(const void *bytes, int len);
137 Value(const Value &rhs);
139 void SetBytes(const void *bytes, int len);
141 void AppendBytes(const void *bytes, int len);
143 Value &operator=(const Value &rhs);
145 const CompilerType &GetCompilerType();
147 void SetCompilerType(const CompilerType &compiler_type);
149 ValueType GetValueType() const;
151 AddressType GetValueAddressType() const;
153 ContextType GetContextType() const { return m_context_type; }
155 void SetValueType(ValueType value_type) { m_value_type = value_type; }
157 void ClearContext() {
159 m_context_type = eContextTypeInvalid;
162 void SetContext(ContextType context_type, void *p) {
163 m_context_type = context_type;
165 if (m_context_type == eContextTypeRegisterInfo) {
166 RegisterInfo *reg_info = GetRegisterInfo();
167 if (reg_info->encoding == lldb::eEncodingVector &&
168 m_vector.byte_order != lldb::eByteOrderInvalid)
169 SetValueType(eValueTypeScalar);
173 RegisterInfo *GetRegisterInfo() const;
177 Scalar &ResolveValue(ExecutionContext *exe_ctx);
179 const Scalar &GetScalar() const { return m_value; }
181 const Vector &GetVector() const { return m_vector; }
183 Scalar &GetScalar() { return m_value; }
185 Vector &GetVector() { return m_vector; }
187 bool SetVectorBytes(const Vector &vector) {
189 return m_vector.IsValid();
192 bool SetVectorBytes(uint8_t *bytes, size_t length,
193 lldb::ByteOrder byte_order) {
194 return m_vector.SetBytes(bytes, length, byte_order);
197 bool SetScalarFromVector() {
198 if (m_vector.IsValid()) {
199 m_value = m_vector.GetAsScalar();
205 size_t ResizeData(size_t len);
207 size_t AppendDataToHostBuffer(const Value &rhs);
209 DataBufferHeap &GetBuffer() { return m_data_buffer; }
211 const DataBufferHeap &GetBuffer() const { return m_data_buffer; }
213 bool ValueOf(ExecutionContext *exe_ctx);
215 Variable *GetVariable();
217 void Dump(Stream *strm);
219 lldb::Format GetValueDefaultFormat();
221 uint64_t GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx);
223 Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
224 uint32_t data_offset,
225 Module *module); // Can be nullptr
227 static const char *GetValueTypeAsCString(ValueType context_type);
229 static const char *GetContextTypeAsCString(ContextType context_type);
231 /// Convert this value's file address to a load address, if possible.
232 void ConvertToLoadAddress(Module *module, Target *target);
234 bool GetData(DataExtractor &data);
241 CompilerType m_compiler_type;
243 ValueType m_value_type;
244 ContextType m_context_type;
245 DataBufferHeap m_data_buffer;
250 ValueList() : m_values() {}
252 ValueList(const ValueList &rhs);
254 ~ValueList() = default;
256 const ValueList &operator=(const ValueList &rhs);
258 // void InsertValue (Value *value, size_t idx);
259 void PushValue(const Value &value);
262 Value *GetValueAtIndex(size_t idx);
266 typedef std::vector<Value> collection;
271 } // namespace lldb_private
273 #endif // liblldb_Value_h_