Value.h revision 10dc2a161c5a3c939a54ba0f4b98e797c5a29b56
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
25namespace lldb_private {
26
27class Value
28{
29public:
30
31    // Values Less than zero are an error, greater than or equal to zero
32    // returns what the Scalar result is.
33    enum ValueType
34    {
35                                        // m_value contains...
36                                        // ============================
37        eValueTypeScalar,               // raw scalar value
38        eValueTypeVector,               // byte array of m_vector.length with endianness of m_vector.byte_order
39        eValueTypeFileAddress,          // file address value
40        eValueTypeLoadAddress,          // load address value
41        eValueTypeHostAddress           // host address value (for memory in the process that is using liblldb)
42    };
43
44    enum ContextType                    // Type that describes Value::m_context
45    {
46                                        // m_context contains...
47                                        // ====================
48        eContextTypeInvalid,            // undefined
49        eContextTypeClangType,          // void * (an opaque clang::QualType * that can be fed to "static QualType QualType::getFromOpaquePtr(void *)")
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        bool
81		SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
82		{
83            this->length = length;
84            this->byte_order = byte_order;
85            if (length)
86                ::memcpy(this->bytes, bytes, length < kMaxByteSize ? length : kMaxByteSize);
87            return IsValid();
88        }
89
90        bool
91		IsValid() const
92		{
93            return (length > 0 && length < kMaxByteSize && byte_order != lldb::eByteOrderInvalid);
94        }
95        // Casts a vector, if valid, to an unsigned int of matching or largest supported size.
96        // Truncates to the beginning of the vector if required.
97        // Returns a default constructed Scalar if the Vector data is internally inconsistent.
98        Scalar
99		GetAsScalar() const
100		{
101            Scalar scalar;
102            if (IsValid())
103                if (length == 1) scalar = *(uint8_t *)bytes;
104                if (length == 2) scalar = *(uint16_t *)bytes;
105                if (length == 4) scalar = *(uint32_t *)bytes;
106                if (length == 8) scalar = *(uint64_t *)bytes;
107#if defined (ENABLE_128_BIT_SUPPORT)
108                if (length >= 16) scalar = *(__uint128_t *)bytes;
109#else
110                if (length >= 16) scalar = *(__uint64_t *)bytes;
111#endif
112            return scalar;
113        }
114    };
115
116    Value();
117    Value(const Scalar& scalar);
118    Value(const Vector& vector);
119    Value(const uint8_t *bytes, int len);
120    Value(const Value &rhs);
121
122    Value &
123    operator=(const Value &rhs);
124
125    lldb::clang_type_t
126    GetClangType();
127
128    ValueType
129    GetValueType() const;
130
131    AddressType
132    GetValueAddressType () const;
133
134    ContextType
135    GetContextType() const
136    {
137        return m_context_type;
138    }
139
140    void
141    SetValueType (ValueType value_type)
142    {
143        m_value_type = value_type;
144    }
145
146    void
147    ClearContext ()
148    {
149        m_context = NULL;
150        m_context_type = eContextTypeInvalid;
151    }
152
153    void
154    SetContext (ContextType context_type, void *p)
155    {
156        m_context_type = context_type;
157        m_context = p;
158        if (m_context_type == eContextTypeRegisterInfo) {
159            RegisterInfo *reg_info = GetRegisterInfo();
160            if (reg_info->encoding == lldb::eEncodingVector)
161                SetValueType(eValueTypeVector);
162            else
163                SetValueType(eValueTypeScalar);
164        }
165    }
166
167    RegisterInfo *
168    GetRegisterInfo() const;
169
170    Type *
171    GetType();
172
173    Scalar &
174    ResolveValue (ExecutionContext *exe_ctx, clang::ASTContext *ast_context);
175
176    const Scalar &
177    GetScalar() const
178    {
179        return m_value;
180    }
181
182    const Vector &
183    GetVector() const
184    {
185        return m_vector;
186    }
187
188    Scalar &
189    GetScalar()
190    {
191        return m_value;
192    }
193
194    Vector &
195    GetVector()
196    {
197        return m_vector;
198    }
199
200    bool
201    SetVectorBytes(const Vector& vector)
202	{
203        m_vector = vector;
204        return m_vector.IsValid();
205    }
206
207    bool
208    SetVectorBytes(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order)
209	{
210        return m_vector.SetBytes(bytes, length, byte_order);
211    }
212
213    bool
214    SetScalarFromVector()
215	{
216        if (m_vector.IsValid())
217		{
218            m_value = m_vector.GetAsScalar();
219            return true;
220        }
221        return false;
222    }
223
224    void
225    ResizeData(size_t len);
226
227    bool
228    ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context);
229
230    Variable *
231    GetVariable();
232
233    void
234    Dump (Stream* strm);
235
236    lldb::Format
237    GetValueDefaultFormat ();
238
239    uint64_t
240    GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr);
241
242    Error
243    GetValueAsData (ExecutionContext *exe_ctx,
244                    clang::ASTContext *ast_context,
245                    DataExtractor &data,
246                    uint32_t data_offset,
247                    Module *module);     // Can be NULL
248
249    static const char *
250    GetValueTypeAsCString (ValueType context_type);
251
252    static const char *
253    GetContextTypeAsCString (ContextType context_type);
254
255    bool
256    GetData (DataExtractor &data);
257
258protected:
259    Scalar          m_value;
260    Vector          m_vector;
261    ValueType       m_value_type;
262    void *          m_context;
263    ContextType     m_context_type;
264    DataBufferHeap  m_data_buffer;
265};
266
267class ValueList
268{
269public:
270    ValueList () :
271        m_values()
272    {
273    }
274
275    ValueList (const ValueList &rhs);
276
277    ~ValueList ()
278    {
279    }
280
281    const ValueList & operator= (const ValueList &rhs);
282
283    // void InsertValue (Value *value, size_t idx);
284    void PushValue (const Value &value);
285
286    size_t GetSize ();
287    Value *GetValueAtIndex(size_t idx);
288    void Clear();
289
290protected:
291
292private:
293    typedef std::vector<Value> collection;
294
295    collection m_values;
296};
297
298} // namespace lldb_private
299
300#endif  // liblldb_Value_h_
301