]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/Value.h
Merge ^/head r294599 through r294776.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / Value.h
1 //===-- Value.h -------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_Value_h_
11 #define liblldb_Value_h_
12
13 // C Includes
14 // C++ Includes
15 #include <vector>
16
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/lldb-private.h"
20 #include "lldb/Core/DataBufferHeap.h"
21 #include "lldb/Core/Error.h"
22 #include "lldb/Core/Scalar.h"
23 #include "lldb/Symbol/CompilerType.h"
24
25 namespace lldb_private {
26
27 class Value
28 {
29 public:
30     // Values Less than zero are an error, greater than or equal to zero
31     // returns what the Scalar result is.
32     enum ValueType
33     {
34                                         // m_value contains...
35                                         // ============================
36         eValueTypeScalar,               // raw scalar value
37         eValueTypeVector,               // byte array of m_vector.length with endianness of m_vector.byte_order
38         eValueTypeFileAddress,          // file address value
39         eValueTypeLoadAddress,          // load address value
40         eValueTypeHostAddress           // host address value (for memory in the process that is using liblldb)
41     };
42
43     enum ContextType                    // Type that describes Value::m_context
44     {
45                                         // m_context contains...
46                                         // ====================
47         eContextTypeInvalid,            // undefined
48         eContextTypeRegisterInfo,       // RegisterInfo * (can be a scalar or a vector register)
49         eContextTypeLLDBType,           // lldb_private::Type *
50         eContextTypeVariable            // lldb_private::Variable *
51     };
52
53     const static size_t kMaxByteSize = 32u;
54
55     struct Vector
56     {
57         // The byte array must be big enough to hold vector registers for any supported target.
58         uint8_t bytes[kMaxByteSize];
59         size_t length;
60         lldb::ByteOrder byte_order;
61
62         Vector() : 
63                         length(0), 
64                         byte_order(lldb::eByteOrderInvalid) 
65         {
66                 }
67
68         Vector(const Vector& vector) 
69                 { *this = vector; 
70         }
71         const Vector& 
72                 operator=(const Vector& vector) 
73                 {
74             SetBytes(vector.bytes, vector.length, vector.byte_order);
75             return *this;
76         }
77
78         void
79         Clear ()
80         {
81                         length = 0;
82         }
83
84         bool
85                 SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
86                 {
87             this->length = length;
88             this->byte_order = byte_order;
89             if (length)
90                 ::memcpy(this->bytes, bytes, length < kMaxByteSize ? length : kMaxByteSize);
91             return IsValid();
92         }
93
94         bool
95                 IsValid() const 
96                 {
97             return (length > 0 && length < kMaxByteSize && byte_order != lldb::eByteOrderInvalid);
98         }
99         // Casts a vector, if valid, to an unsigned int of matching or largest supported size.
100         // Truncates to the beginning of the vector if required.
101         // Returns a default constructed Scalar if the Vector data is internally inconsistent.
102         llvm::APInt rhs = llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, ((type128 *)bytes)->x);
103         Scalar 
104                 GetAsScalar() const 
105                 {
106             Scalar scalar;
107             if (IsValid())
108             {
109                 if (length == 1) scalar = *(const uint8_t *)bytes;
110                 else if (length == 2) scalar = *(const uint16_t *)bytes;
111                 else if (length == 4) scalar = *(const uint32_t *)bytes;
112                 else if (length == 8) scalar = *(const uint64_t *)bytes;
113                 else if (length >= 16) scalar = rhs;
114             }
115             return scalar;
116         }
117     };
118
119     Value();
120     Value(const Scalar& scalar);
121     Value(const Vector& vector);
122     Value(const void *bytes, int len);
123     Value(const Value &rhs);
124     
125     void
126     SetBytes (const void *bytes, int len);
127     
128     void
129     AppendBytes (const void *bytes, int len);
130
131     Value &
132     operator=(const Value &rhs);
133
134     const CompilerType &
135     GetCompilerType();
136     
137     void
138     SetCompilerType (const CompilerType &compiler_type);
139
140     ValueType
141     GetValueType() const;
142
143     AddressType
144     GetValueAddressType () const;
145
146     ContextType
147     GetContextType() const
148     {
149         return m_context_type;
150     }
151
152     void
153     SetValueType (ValueType value_type)
154     {
155         m_value_type = value_type;
156     }
157
158     void
159     ClearContext ()
160     {
161         m_context = nullptr;
162         m_context_type = eContextTypeInvalid;
163     }
164
165     void
166     SetContext (ContextType context_type, void *p)
167     {
168         m_context_type = context_type;
169         m_context = p;
170         if (m_context_type == eContextTypeRegisterInfo) {
171             RegisterInfo *reg_info = GetRegisterInfo();
172             if (reg_info->encoding == lldb::eEncodingVector)
173                 SetValueType(eValueTypeVector);
174             else
175                 SetValueType(eValueTypeScalar);
176         }
177     }
178
179     RegisterInfo *
180     GetRegisterInfo() const;
181
182     Type *
183     GetType();
184
185     Scalar &
186     ResolveValue (ExecutionContext *exe_ctx);
187
188     const Scalar &
189     GetScalar() const
190     {
191         return m_value;
192     }
193     
194     const Vector &
195     GetVector() const
196     {
197         return m_vector;
198     }
199     
200     Scalar &
201     GetScalar()
202     {
203         return m_value;
204     }
205     
206     Vector &
207     GetVector()
208     {
209         return m_vector;
210     }
211
212     bool
213     SetVectorBytes(const Vector& vector) 
214         {
215         m_vector = vector;
216         return m_vector.IsValid();
217     }
218     
219     bool
220     SetVectorBytes(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order) 
221         {
222         return m_vector.SetBytes(bytes, length, byte_order);
223     }
224
225     bool
226     SetScalarFromVector() 
227         {
228         if (m_vector.IsValid()) 
229                 {
230             m_value = m_vector.GetAsScalar();
231             return true;
232         }
233         return false;
234     }
235
236     size_t
237     ResizeData(size_t len);
238     
239     size_t
240     AppendDataToHostBuffer (const Value &rhs);
241
242     DataBufferHeap &
243     GetBuffer ()
244     {
245         return m_data_buffer;
246     }
247
248     const DataBufferHeap &
249     GetBuffer () const
250     {
251         return m_data_buffer;
252     }
253
254     bool
255     ValueOf(ExecutionContext *exe_ctx);
256
257     Variable *
258     GetVariable();
259
260     void
261     Dump (Stream* strm);
262
263     lldb::Format
264     GetValueDefaultFormat ();
265
266     uint64_t
267     GetValueByteSize (Error *error_ptr, ExecutionContext *exe_ctx);
268
269     Error
270     GetValueAsData(ExecutionContext *exe_ctx, 
271                    DataExtractor &data, 
272                    uint32_t data_offset,
273                    Module *module);     // Can be nullptr
274
275     static const char *
276     GetValueTypeAsCString (ValueType context_type);
277
278     static const char *
279     GetContextTypeAsCString (ContextType context_type);
280
281     bool
282     GetData (DataExtractor &data);
283
284     void
285     Clear();
286
287 protected:
288     Scalar          m_value;
289     Vector          m_vector;
290     CompilerType    m_compiler_type;
291     void *          m_context;
292     ValueType       m_value_type;
293     ContextType     m_context_type;
294     DataBufferHeap  m_data_buffer;
295 };
296
297 class ValueList
298 {
299 public:
300     ValueList () :
301         m_values()
302     {
303     }
304
305     ValueList (const ValueList &rhs);
306
307     ~ValueList() = default;
308
309     const ValueList & operator= (const ValueList &rhs);
310
311     // void InsertValue (Value *value, size_t idx);
312     void PushValue (const Value &value);
313
314     size_t GetSize ();
315     Value *GetValueAtIndex(size_t idx);
316     void Clear();
317
318 private:
319     typedef std::vector<Value> collection;
320
321     collection m_values;
322 };
323
324 } // namespace lldb_private
325
326 #endif // liblldb_Value_h_