Value.h revision d387b462eecb908af265ecc7006781b4532073ad
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();
169
170    Type *
171    GetType();
172
173    Scalar &
174    ResolveValue (ExecutionContext *exe_ctx, clang::ASTContext *ast_context);
175
176    Scalar &
177    GetScalar()
178    {
179        return m_value;
180    }
181
182    Vector &
183    GetVector()
184    {
185        return m_vector;
186    }
187
188    bool
189    SetVectorBytes(const Vector& vector)
190	{
191        m_vector = vector;
192        return m_vector.IsValid();
193    }
194
195    bool
196    SetVectorBytes(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order)
197	{
198        return m_vector.SetBytes(bytes, length, byte_order);
199    }
200
201    bool
202    SetScalarFromVector()
203	{
204        if (m_vector.IsValid())
205		{
206            m_value = m_vector.GetAsScalar();
207            return true;
208        }
209        return false;
210    }
211
212    void
213    ResizeData(size_t len);
214
215    bool
216    ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context);
217
218    Variable *
219    GetVariable();
220
221    void
222    Dump (Stream* strm);
223
224    lldb::Format
225    GetValueDefaultFormat ();
226
227    uint64_t
228    GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr);
229
230    Error
231    GetValueAsData (ExecutionContext *exe_ctx,
232                    clang::ASTContext *ast_context,
233                    DataExtractor &data,
234                    uint32_t data_offset,
235                    Module *module);     // Can be NULL
236
237    static const char *
238    GetValueTypeAsCString (ValueType context_type);
239
240    static const char *
241    GetContextTypeAsCString (ContextType context_type);
242
243    bool
244    GetData (DataExtractor &data);
245
246protected:
247    Scalar          m_value;
248    Vector          m_vector;
249    ValueType       m_value_type;
250    void *          m_context;
251    ContextType     m_context_type;
252    DataBufferHeap  m_data_buffer;
253};
254
255class ValueList
256{
257public:
258    ValueList () :
259        m_values()
260    {
261    }
262
263    ValueList (const ValueList &rhs);
264
265    ~ValueList ()
266    {
267    }
268
269    const ValueList & operator= (const ValueList &rhs);
270
271    // void InsertValue (Value *value, size_t idx);
272    void PushValue (const Value &value);
273
274    size_t GetSize ();
275    Value *GetValueAtIndex(size_t idx);
276    void Clear();
277
278protected:
279
280private:
281    typedef std::vector<Value> collection;
282
283    collection m_values;
284};
285
286} // namespace lldb_private
287
288#endif  // liblldb_Value_h_
289