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