Make UEFI boot-platform build again
[haiku.git] / headers / private / debugger / types / ValueLocation.h
blob045e7db91e166f36fecaebbdd7ef3270f2773d8f
1 /*
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.
5 */
6 #ifndef VALUE_LOCATION_H
7 #define VALUE_LOCATION_H
9 #include <vector>
11 #include <stdlib.h>
12 #include <string.h>
14 #include <Referenceable.h>
16 #include "Types.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 {
29 union {
30 target_addr_t address; // memory address
31 uint32 reg; // register number
33 target_size_t size; // size in bytes (including
34 // incomplete ones)
35 uint64 bitSize; // total size in bits
36 uint64 bitOffset; // bit offset (to the most
37 // significant bit)
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
45 ValuePieceLocation()
47 type(VALUE_PIECE_LOCATION_INVALID),
48 value(NULL),
49 writable(false)
53 ValuePieceLocation(const ValuePieceLocation& other)
55 if (!Copy(other))
56 throw std::bad_alloc();
59 ~ValuePieceLocation()
61 if (value != NULL)
62 free(value);
65 ValuePieceLocation& operator=(const ValuePieceLocation& other)
67 if (!Copy(other))
68 throw std::bad_alloc();
70 return *this;
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;
80 return false;
83 memcpy(tempValue, value, other.size);
84 value = tempValue;
87 return true;
90 bool IsValid() const
92 return type != VALUE_PIECE_LOCATION_INVALID;
95 void SetToUnknown()
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;
110 this->reg = reg;
111 this->writable = true;
114 void SetSize(target_size_t size)
116 this->size = size;
117 this->bitSize = size * 8;
118 this->bitOffset = 0;
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)
132 return false;
133 memcpy(valueData, data, size);
134 SetSize(size);
135 type = VALUE_PIECE_LOCATION_IMPLICIT;
136 value = valueData;
137 writable = false;
138 return true;
141 ValuePieceLocation& Normalize(bool bigEndian);
145 class ValueLocation : public BReferenceable {
146 public:
147 ValueLocation();
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);
160 void Clear();
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);
173 void Dump() const;
175 private:
176 typedef std::vector<ValuePieceLocation> PieceVector;
178 private:
179 PieceVector fPieces;
180 bool fBigEndian;
181 bool fWritable;
185 #endif // VALUE_LOCATION_H