2 * Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2013, Rene Gollent, rene@gollent.com.
4 * Distributed under the terms of the MIT License.
6 #ifndef VALUE_LOCATION_H
7 #define VALUE_LOCATION_H
14 #include <Referenceable.h>
19 enum value_piece_location_type
{
20 VALUE_PIECE_LOCATION_INVALID
, // structure is invalid
21 VALUE_PIECE_LOCATION_UNKNOWN
, // location unknown, but size is valid
22 VALUE_PIECE_LOCATION_MEMORY
, // piece is in memory
23 VALUE_PIECE_LOCATION_REGISTER
, // piece is in a register
24 VALUE_PIECE_LOCATION_IMPLICIT
// value isn't stored anywhere in memory but is known
28 struct ValuePieceLocation
{
30 target_addr_t address
; // memory address
31 uint32 reg
; // register number
33 target_size_t size
; // size in bytes (including
35 uint64 bitSize
; // total size in bits
36 uint64 bitOffset
; // bit offset (to the most
38 value_piece_location_type type
;
39 void* value
; // used for storing implicit values
40 bool writable
; // indicates if the piece is in a
41 // location in the target team
42 // where it can be modified
47 type(VALUE_PIECE_LOCATION_INVALID
),
53 ValuePieceLocation(const ValuePieceLocation
& other
)
56 throw std::bad_alloc();
65 ValuePieceLocation
& operator=(const ValuePieceLocation
& other
)
68 throw std::bad_alloc();
73 bool Copy(const ValuePieceLocation
& other
)
75 memcpy(this, &other
, sizeof(ValuePieceLocation
));
76 if (type
== VALUE_PIECE_LOCATION_IMPLICIT
) {
77 void* tempValue
= malloc(size
);
78 if (tempValue
== NULL
) {
79 type
= VALUE_PIECE_LOCATION_INVALID
;
83 memcpy(tempValue
, value
, other
.size
);
92 return type
!= VALUE_PIECE_LOCATION_INVALID
;
97 type
= VALUE_PIECE_LOCATION_UNKNOWN
;
100 void SetToMemory(target_addr_t address
)
102 type
= VALUE_PIECE_LOCATION_MEMORY
;
103 this->address
= address
;
104 this->writable
= true;
107 void SetToRegister(uint32 reg
)
109 type
= VALUE_PIECE_LOCATION_REGISTER
;
111 this->writable
= true;
114 void SetSize(target_size_t size
)
117 this->bitSize
= size
* 8;
121 void SetSize(uint64 bitSize
, uint64 bitOffset
)
123 this->size
= (bitOffset
+ bitSize
+ 7) / 8;
124 this->bitSize
= bitSize
;
125 this->bitOffset
= bitOffset
;
128 bool SetToValue(const void* data
, target_size_t size
)
130 char* valueData
= (char*)malloc(size
);
131 if (valueData
== NULL
)
133 memcpy(valueData
, data
, size
);
135 type
= VALUE_PIECE_LOCATION_IMPLICIT
;
141 ValuePieceLocation
& Normalize(bool bigEndian
);
145 class ValueLocation
: public BReferenceable
{
148 ValueLocation(bool bigEndian
);
149 ValueLocation(bool bigEndian
,
150 const ValuePieceLocation
& piece
);
152 ValueLocation(const ValueLocation
& other
);
154 bool SetToByteOffset(const ValueLocation
& other
,
155 uint64 byteffset
, uint64 Size
);
157 bool SetTo(const ValueLocation
& other
,
158 uint64 bitOffset
, uint64 bitSize
);
162 bool IsBigEndian() const { return fBigEndian
; }
163 bool IsWritable() const { return fWritable
; }
165 bool AddPiece(const ValuePieceLocation
& piece
);
167 int32
CountPieces() const;
168 ValuePieceLocation
PieceAt(int32 index
) const;
169 bool SetPieceAt(int32 index
,
170 const ValuePieceLocation
& piece
);
171 ValueLocation
& operator=(const ValueLocation
& other
);
176 typedef std::vector
<ValuePieceLocation
> PieceVector
;
185 #endif // VALUE_LOCATION_H