Home | History | Annotate | Download | only in Core
      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 uint8_t *bytes, int len);
    128     Value(const Value &rhs);
    129 
    130     Value &
    131     operator=(const Value &rhs);
    132 
    133     const ClangASTType &
    134     GetClangType();
    135 
    136     void
    137     SetClangType (const ClangASTType &clang_type);
    138 
    139     ValueType
    140     GetValueType() const;
    141 
    142     AddressType
    143     GetValueAddressType () const;
    144 
    145     ContextType
    146     GetContextType() const
    147     {
    148         return m_context_type;
    149     }
    150 
    151     void
    152     SetValueType (ValueType value_type)
    153     {
    154         m_value_type = value_type;
    155     }
    156 
    157     void
    158     ClearContext ()
    159     {
    160         m_context = NULL;
    161         m_context_type = eContextTypeInvalid;
    162     }
    163 
    164     void
    165     SetContext (ContextType context_type, void *p)
    166     {
    167         m_context_type = context_type;
    168         m_context = p;
    169         if (m_context_type == eContextTypeRegisterInfo) {
    170             RegisterInfo *reg_info = GetRegisterInfo();
    171             if (reg_info->encoding == lldb::eEncodingVector)
    172                 SetValueType(eValueTypeVector);
    173             else
    174                 SetValueType(eValueTypeScalar);
    175         }
    176     }
    177 
    178     RegisterInfo *
    179     GetRegisterInfo() const;
    180 
    181     Type *
    182     GetType();
    183 
    184     Scalar &
    185     ResolveValue (ExecutionContext *exe_ctx);
    186 
    187     const Scalar &
    188     GetScalar() const
    189     {
    190         return m_value;
    191     }
    192 
    193     const Vector &
    194     GetVector() const
    195     {
    196         return m_vector;
    197     }
    198 
    199     Scalar &
    200     GetScalar()
    201     {
    202         return m_value;
    203     }
    204 
    205     Vector &
    206     GetVector()
    207     {
    208         return m_vector;
    209     }
    210 
    211     bool
    212     SetVectorBytes(const Vector& vector)
    213 	{
    214         m_vector = vector;
    215         return m_vector.IsValid();
    216     }
    217 
    218     bool
    219     SetVectorBytes(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order)
    220 	{
    221         return m_vector.SetBytes(bytes, length, byte_order);
    222     }
    223 
    224     bool
    225     SetScalarFromVector()
    226 	{
    227         if (m_vector.IsValid())
    228 		{
    229             m_value = m_vector.GetAsScalar();
    230             return true;
    231         }
    232         return false;
    233     }
    234 
    235     void
    236     ResizeData(size_t len);
    237 
    238     bool
    239     ValueOf(ExecutionContext *exe_ctx);
    240 
    241     Variable *
    242     GetVariable();
    243 
    244     void
    245     Dump (Stream* strm);
    246 
    247     lldb::Format
    248     GetValueDefaultFormat ();
    249 
    250     uint64_t
    251     GetValueByteSize (Error *error_ptr);
    252 
    253     Error
    254     GetValueAsData (ExecutionContext *exe_ctx,
    255                     DataExtractor &data,
    256                     uint32_t data_offset,
    257                     Module *module);     // Can be NULL
    258 
    259     static const char *
    260     GetValueTypeAsCString (ValueType context_type);
    261 
    262     static const char *
    263     GetContextTypeAsCString (ContextType context_type);
    264 
    265     bool
    266     GetData (DataExtractor &data);
    267 
    268     void
    269     Clear();
    270 
    271 protected:
    272     Scalar          m_value;
    273     Vector          m_vector;
    274     ClangASTType    m_clang_type;
    275     void *          m_context;
    276     ValueType       m_value_type;
    277     ContextType     m_context_type;
    278     DataBufferHeap  m_data_buffer;
    279 };
    280 
    281 class ValueList
    282 {
    283 public:
    284     ValueList () :
    285         m_values()
    286     {
    287     }
    288 
    289     ValueList (const ValueList &rhs);
    290 
    291     ~ValueList ()
    292     {
    293     }
    294 
    295     const ValueList & operator= (const ValueList &rhs);
    296 
    297     // void InsertValue (Value *value, size_t idx);
    298     void PushValue (const Value &value);
    299 
    300     size_t GetSize ();
    301     Value *GetValueAtIndex(size_t idx);
    302     void Clear();
    303 
    304 protected:
    305 
    306 private:
    307     typedef std::vector<Value> collection;
    308 
    309     collection m_values;
    310 };
    311 
    312 } // namespace lldb_private
    313 
    314 #endif  // liblldb_Value_h_
    315