Value.h revision fe6dc6e241c52822710380cec0931351a1d7b2d3
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    enum
56    {
57        kMaxByteSize = 32u
58    };
59
60    struct Vector
61    {
62        // The byte array must be big enough to hold vector registers for any supported target.
63        uint8_t bytes[kMaxByteSize];
64        size_t length;
65        lldb::ByteOrder byte_order;
66
67        Vector() :
68			length(0),
69			byte_order(lldb::eByteOrderInvalid)
70        {
71		}
72
73        Vector(const Vector& vector)
74		{ *this = vector;
75        }
76        const Vector&
77		operator=(const Vector& vector)
78		{
79            SetBytes((uint8_t *)vector.bytes, vector.length, vector.byte_order);
80            return *this;
81        }
82
83        bool
84		SetBytes(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order)
85		{
86            this->length = length;
87            this->byte_order = byte_order;
88            if (length)
89                ::memcpy(this->bytes, bytes, length < kMaxByteSize ? length : kMaxByteSize);
90            return IsValid();
91        }
92
93        bool
94		IsValid() const
95		{
96            return (length > 0 && length < kMaxByteSize && byte_order != lldb::eByteOrderInvalid);
97        }
98        // Casts a vector, if valid, to an unsigned int of matching or largest supported size.
99        // Truncates to the beginning of the vector if required.
100        // Returns a default constructed Scalar if the Vector data is internally inconsistent.
101        Scalar
102		GetAsScalar() const
103		{
104            Scalar scalar;
105            if (IsValid())
106                if (length == 1) scalar = *(uint8_t *)bytes;
107                if (length == 2) scalar = *(uint16_t *)bytes;
108                if (length == 4) scalar = *(uint32_t *)bytes;
109                if (length == 8) scalar = *(uint64_t *)bytes;
110#if defined (ENABLE_128_BIT_SUPPORT)
111                if (length >= 16) scalar = *(__uint128_t *)bytes;
112#else
113                if (length >= 16) scalar = *(__uint64_t *)bytes;
114#endif
115            return scalar;
116        }
117    };
118
119    Value();
120    Value(const Scalar& scalar);
121    Value(const Vector& vector);
122    Value(const uint8_t *bytes, int len);
123    Value(const Value &rhs);
124
125    Value &
126    operator=(const Value &rhs);
127
128    lldb::clang_type_t
129    GetClangType();
130
131    ValueType
132    GetValueType() const;
133
134    AddressType
135    GetValueAddressType () const;
136
137    ContextType
138    GetContextType() const
139    {
140        return m_context_type;
141    }
142
143    void
144    SetValueType (ValueType value_type)
145    {
146        m_value_type = value_type;
147    }
148
149    void
150    ClearContext ()
151    {
152        m_context = NULL;
153        m_context_type = eContextTypeInvalid;
154    }
155
156    void
157    SetContext (ContextType context_type, void *p)
158    {
159        m_context_type = context_type;
160        m_context = p;
161        if (m_context_type == eContextTypeRegisterInfo) {
162            RegisterInfo *reg_info = GetRegisterInfo();
163            if (reg_info->encoding == lldb::eEncodingVector)
164                SetValueType(eValueTypeVector);
165            else
166                SetValueType(eValueTypeScalar);
167        }
168    }
169
170    RegisterInfo *
171    GetRegisterInfo();
172
173    Type *
174    GetType();
175
176    Scalar &
177    ResolveValue (ExecutionContext *exe_ctx, clang::ASTContext *ast_context);
178
179    Scalar &
180    GetScalar()
181    {
182        return m_value;
183    }
184
185    Vector &
186    GetVector()
187    {
188        return m_vector;
189    }
190
191    bool
192    SetVectorBytes(const Vector& vector)
193	{
194        m_vector = vector;
195        return m_vector.IsValid();
196    }
197
198    bool
199    SetVectorBytes(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order)
200	{
201        return m_vector.SetBytes(bytes, length, byte_order);
202    }
203
204    bool
205    SetScalarFromVector()
206	{
207        if (m_vector.IsValid())
208		{
209            m_value = m_vector.GetAsScalar();
210            return true;
211        }
212        return false;
213    }
214
215    void
216    ResizeData(size_t len);
217
218    bool
219    ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context);
220
221    Variable *
222    GetVariable();
223
224    void
225    Dump (Stream* strm);
226
227    lldb::Format
228    GetValueDefaultFormat ();
229
230    uint64_t
231    GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr);
232
233    Error
234    GetValueAsData (ExecutionContext *exe_ctx,
235                    clang::ASTContext *ast_context,
236                    DataExtractor &data,
237                    uint32_t data_offset,
238                    Module *module);     // Can be NULL
239
240    static const char *
241    GetValueTypeAsCString (ValueType context_type);
242
243    static const char *
244    GetContextTypeAsCString (ContextType context_type);
245
246    bool
247    GetData (DataExtractor &data);
248
249protected:
250    Scalar          m_value;
251    Vector          m_vector;
252    ValueType       m_value_type;
253    void *          m_context;
254    ContextType     m_context_type;
255    DataBufferHeap  m_data_buffer;
256};
257
258class ValueList
259{
260public:
261    ValueList () :
262        m_values()
263    {
264    }
265
266    ValueList (const ValueList &rhs);
267
268    ~ValueList ()
269    {
270    }
271
272    const ValueList & operator= (const ValueList &rhs);
273
274    // void InsertValue (Value *value, size_t idx);
275    void PushValue (const Value &value);
276
277    size_t GetSize ();
278    Value *GetValueAtIndex(size_t idx);
279    void Clear();
280
281protected:
282
283private:
284    typedef std::vector<Value> collection;
285
286    collection m_values;
287};
288
289} // namespace lldb_private
290
291#endif  // liblldb_Value_h_
292