1254721Semaste//===-- Value.h -------------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_Value_h_
11254721Semaste#define liblldb_Value_h_
12254721Semaste
13254721Semaste// C Includes
14254721Semaste// C++ Includes
15254721Semaste#include <string>
16254721Semaste#include <vector>
17254721Semaste// Other libraries and framework includes
18254721Semaste// Project includes
19254721Semaste#include "lldb/lldb-private.h"
20254721Semaste#include "lldb/Core/ClangForward.h"
21254721Semaste#include "lldb/Core/DataBufferHeap.h"
22254721Semaste#include "lldb/Core/Error.h"
23254721Semaste#include "lldb/Core/Scalar.h"
24254721Semaste#include "lldb/Symbol/ClangASTType.h"
25254721Semaste
26254721Semastenamespace lldb_private {
27254721Semaste
28254721Semasteclass Value
29254721Semaste{
30254721Semastepublic:
31254721Semaste
32254721Semaste    // Values Less than zero are an error, greater than or equal to zero
33254721Semaste    // returns what the Scalar result is.
34254721Semaste    enum ValueType
35254721Semaste    {
36254721Semaste                                        // m_value contains...
37254721Semaste                                        // ============================
38254721Semaste        eValueTypeScalar,               // raw scalar value
39254721Semaste        eValueTypeVector,               // byte array of m_vector.length with endianness of m_vector.byte_order
40254721Semaste        eValueTypeFileAddress,          // file address value
41254721Semaste        eValueTypeLoadAddress,          // load address value
42254721Semaste        eValueTypeHostAddress           // host address value (for memory in the process that is using liblldb)
43254721Semaste    };
44254721Semaste
45254721Semaste    enum ContextType                    // Type that describes Value::m_context
46254721Semaste    {
47254721Semaste                                        // m_context contains...
48254721Semaste                                        // ====================
49254721Semaste        eContextTypeInvalid,            // undefined
50254721Semaste        eContextTypeRegisterInfo,       // RegisterInfo * (can be a scalar or a vector register)
51254721Semaste        eContextTypeLLDBType,           // lldb_private::Type *
52254721Semaste        eContextTypeVariable            // lldb_private::Variable *
53254721Semaste    };
54254721Semaste
55254721Semaste    const static size_t kMaxByteSize = 32u;
56254721Semaste
57254721Semaste    struct Vector
58254721Semaste    {
59254721Semaste        // The byte array must be big enough to hold vector registers for any supported target.
60254721Semaste        uint8_t bytes[kMaxByteSize];
61254721Semaste        size_t length;
62254721Semaste        lldb::ByteOrder byte_order;
63254721Semaste
64254721Semaste        Vector() :
65254721Semaste			length(0),
66254721Semaste			byte_order(lldb::eByteOrderInvalid)
67254721Semaste        {
68254721Semaste		}
69254721Semaste
70254721Semaste        Vector(const Vector& vector)
71254721Semaste		{ *this = vector;
72254721Semaste        }
73254721Semaste        const Vector&
74254721Semaste		operator=(const Vector& vector)
75254721Semaste		{
76254721Semaste            SetBytes(vector.bytes, vector.length, vector.byte_order);
77254721Semaste            return *this;
78254721Semaste        }
79254721Semaste
80254721Semaste        void
81254721Semaste        Clear ()
82254721Semaste        {
83254721Semaste			length = 0;
84254721Semaste        }
85254721Semaste
86254721Semaste        bool
87254721Semaste		SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
88254721Semaste		{
89254721Semaste            this->length = length;
90254721Semaste            this->byte_order = byte_order;
91254721Semaste            if (length)
92254721Semaste                ::memcpy(this->bytes, bytes, length < kMaxByteSize ? length : kMaxByteSize);
93254721Semaste            return IsValid();
94254721Semaste        }
95254721Semaste
96254721Semaste        bool
97254721Semaste		IsValid() const
98254721Semaste		{
99254721Semaste            return (length > 0 && length < kMaxByteSize && byte_order != lldb::eByteOrderInvalid);
100254721Semaste        }
101254721Semaste        // Casts a vector, if valid, to an unsigned int of matching or largest supported size.
102254721Semaste        // Truncates to the beginning of the vector if required.
103254721Semaste        // Returns a default constructed Scalar if the Vector data is internally inconsistent.
104254721Semaste        Scalar
105254721Semaste		GetAsScalar() const
106254721Semaste		{
107254721Semaste            Scalar scalar;
108254721Semaste            if (IsValid())
109254721Semaste            {
110254721Semaste                if (length == 1) scalar = *(const uint8_t *)bytes;
111254721Semaste                else if (length == 2) scalar = *(const uint16_t *)bytes;
112254721Semaste                else if (length == 4) scalar = *(const uint32_t *)bytes;
113254721Semaste                else if (length == 8) scalar = *(const uint64_t *)bytes;
114254721Semaste#if defined (ENABLE_128_BIT_SUPPORT)
115254721Semaste                else if (length >= 16) scalar = *(const __uint128_t *)bytes;
116254721Semaste#else
117263363Semaste                else if (length >= 16) scalar = *(const uint64_t *)bytes;
118254721Semaste#endif
119254721Semaste            }
120254721Semaste            return scalar;
121254721Semaste        }
122254721Semaste    };
123254721Semaste
124254721Semaste    Value();
125254721Semaste    Value(const Scalar& scalar);
126254721Semaste    Value(const Vector& vector);
127254721Semaste    Value(const uint8_t *bytes, int len);
128254721Semaste    Value(const Value &rhs);
129254721Semaste
130254721Semaste    Value &
131254721Semaste    operator=(const Value &rhs);
132254721Semaste
133254721Semaste    const ClangASTType &
134254721Semaste    GetClangType();
135254721Semaste
136254721Semaste    void
137254721Semaste    SetClangType (const ClangASTType &clang_type);
138254721Semaste
139254721Semaste    ValueType
140254721Semaste    GetValueType() const;
141254721Semaste
142254721Semaste    AddressType
143254721Semaste    GetValueAddressType () const;
144254721Semaste
145254721Semaste    ContextType
146254721Semaste    GetContextType() const
147254721Semaste    {
148254721Semaste        return m_context_type;
149254721Semaste    }
150254721Semaste
151254721Semaste    void
152254721Semaste    SetValueType (ValueType value_type)
153254721Semaste    {
154254721Semaste        m_value_type = value_type;
155254721Semaste    }
156254721Semaste
157254721Semaste    void
158254721Semaste    ClearContext ()
159254721Semaste    {
160254721Semaste        m_context = NULL;
161254721Semaste        m_context_type = eContextTypeInvalid;
162254721Semaste    }
163254721Semaste
164254721Semaste    void
165254721Semaste    SetContext (ContextType context_type, void *p)
166254721Semaste    {
167254721Semaste        m_context_type = context_type;
168254721Semaste        m_context = p;
169254721Semaste        if (m_context_type == eContextTypeRegisterInfo) {
170254721Semaste            RegisterInfo *reg_info = GetRegisterInfo();
171254721Semaste            if (reg_info->encoding == lldb::eEncodingVector)
172254721Semaste                SetValueType(eValueTypeVector);
173254721Semaste            else
174254721Semaste                SetValueType(eValueTypeScalar);
175254721Semaste        }
176254721Semaste    }
177254721Semaste
178254721Semaste    RegisterInfo *
179254721Semaste    GetRegisterInfo() const;
180254721Semaste
181254721Semaste    Type *
182254721Semaste    GetType();
183254721Semaste
184254721Semaste    Scalar &
185254721Semaste    ResolveValue (ExecutionContext *exe_ctx);
186254721Semaste
187254721Semaste    const Scalar &
188254721Semaste    GetScalar() const
189254721Semaste    {
190254721Semaste        return m_value;
191254721Semaste    }
192254721Semaste
193254721Semaste    const Vector &
194254721Semaste    GetVector() const
195254721Semaste    {
196254721Semaste        return m_vector;
197254721Semaste    }
198254721Semaste
199254721Semaste    Scalar &
200254721Semaste    GetScalar()
201254721Semaste    {
202254721Semaste        return m_value;
203254721Semaste    }
204254721Semaste
205254721Semaste    Vector &
206254721Semaste    GetVector()
207254721Semaste    {
208254721Semaste        return m_vector;
209254721Semaste    }
210254721Semaste
211254721Semaste    bool
212254721Semaste    SetVectorBytes(const Vector& vector)
213254721Semaste	{
214254721Semaste        m_vector = vector;
215254721Semaste        return m_vector.IsValid();
216254721Semaste    }
217254721Semaste
218254721Semaste    bool
219254721Semaste    SetVectorBytes(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order)
220254721Semaste	{
221254721Semaste        return m_vector.SetBytes(bytes, length, byte_order);
222254721Semaste    }
223254721Semaste
224254721Semaste    bool
225254721Semaste    SetScalarFromVector()
226254721Semaste	{
227254721Semaste        if (m_vector.IsValid())
228254721Semaste		{
229254721Semaste            m_value = m_vector.GetAsScalar();
230254721Semaste            return true;
231254721Semaste        }
232254721Semaste        return false;
233254721Semaste    }
234254721Semaste
235254721Semaste    void
236254721Semaste    ResizeData(size_t len);
237254721Semaste
238254721Semaste    bool
239254721Semaste    ValueOf(ExecutionContext *exe_ctx);
240254721Semaste
241254721Semaste    Variable *
242254721Semaste    GetVariable();
243254721Semaste
244254721Semaste    void
245254721Semaste    Dump (Stream* strm);
246254721Semaste
247254721Semaste    lldb::Format
248254721Semaste    GetValueDefaultFormat ();
249254721Semaste
250254721Semaste    uint64_t
251254721Semaste    GetValueByteSize (Error *error_ptr);
252254721Semaste
253254721Semaste    Error
254254721Semaste    GetValueAsData (ExecutionContext *exe_ctx,
255254721Semaste                    DataExtractor &data,
256254721Semaste                    uint32_t data_offset,
257254721Semaste                    Module *module);     // Can be NULL
258254721Semaste
259254721Semaste    static const char *
260254721Semaste    GetValueTypeAsCString (ValueType context_type);
261254721Semaste
262254721Semaste    static const char *
263254721Semaste    GetContextTypeAsCString (ContextType context_type);
264254721Semaste
265254721Semaste    bool
266254721Semaste    GetData (DataExtractor &data);
267254721Semaste
268254721Semaste    void
269254721Semaste    Clear();
270254721Semaste
271254721Semasteprotected:
272254721Semaste    Scalar          m_value;
273254721Semaste    Vector          m_vector;
274254721Semaste    ClangASTType    m_clang_type;
275254721Semaste    void *          m_context;
276254721Semaste    ValueType       m_value_type;
277254721Semaste    ContextType     m_context_type;
278254721Semaste    DataBufferHeap  m_data_buffer;
279254721Semaste};
280254721Semaste
281254721Semasteclass ValueList
282254721Semaste{
283254721Semastepublic:
284254721Semaste    ValueList () :
285254721Semaste        m_values()
286254721Semaste    {
287254721Semaste    }
288254721Semaste
289254721Semaste    ValueList (const ValueList &rhs);
290254721Semaste
291254721Semaste    ~ValueList ()
292254721Semaste    {
293254721Semaste    }
294254721Semaste
295254721Semaste    const ValueList & operator= (const ValueList &rhs);
296254721Semaste
297254721Semaste    // void InsertValue (Value *value, size_t idx);
298254721Semaste    void PushValue (const Value &value);
299254721Semaste
300254721Semaste    size_t GetSize ();
301254721Semaste    Value *GetValueAtIndex(size_t idx);
302254721Semaste    void Clear();
303254721Semaste
304254721Semasteprotected:
305254721Semaste
306254721Semasteprivate:
307254721Semaste    typedef std::vector<Value> collection;
308254721Semaste
309254721Semaste    collection m_values;
310254721Semaste};
311254721Semaste
312254721Semaste} // namespace lldb_private
313254721Semaste
314254721Semaste#endif  // liblldb_Value_h_
315