[memprof] Use a new constructor of IndexedAllocationInfo (NFC) (#116920)
[llvm-project.git] / openmp / libompd / src / TargetValue.h
blobd7429f7705dfdf932eb04989192c6a40fb842d8f
1 /*
2 * TargetValue.h -- Access to target values using OMPD callbacks
3 */
5 //===----------------------------------------------------------------------===//
6 //
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8 // See https://llvm.org/LICENSE.txt for license information.
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11 //===----------------------------------------------------------------------===//
13 #include "omp-tools.h"
14 #include "ompd-private.h"
15 #include <stdlib.h>
17 #ifndef SRC_TARGET_VALUE_H_
18 #define SRC_TARGET_VALUE_H_
20 #ifdef __cplusplus
22 #include <cassert>
23 #include <map>
24 #include <string>
26 class TType;
27 class TValue;
28 class TBaseValue;
30 class TTypeFactory {
31 protected:
32 std::map<ompd_address_space_context_t *, std::map<const char *, TType>>
33 ttypes;
35 public:
36 TTypeFactory() : ttypes() {}
37 TType &getType(ompd_address_space_context_t *context, const char *typName,
38 ompd_addr_t segment = OMPD_SEGMENT_UNSPECIFIED);
41 static thread_local TTypeFactory tf = TTypeFactory();
43 class TType {
44 protected:
45 ompd_size_t typeSize;
46 std::map<const char *, ompd_size_t> fieldOffsets;
47 std::map<const char *, ompd_size_t> fieldSizes;
48 std::map<const char *, uint64_t> bitfieldMasks;
49 ompd_addr_t descSegment;
50 const char *typeName;
51 ompd_address_space_context_t *context;
52 bool isvoid;
53 TType(ompd_address_space_context_t *context, const char *typeName,
54 ompd_addr_t _segment = OMPD_SEGMENT_UNSPECIFIED);
56 public:
57 TType(bool, ompd_addr_t _segment = OMPD_SEGMENT_UNSPECIFIED)
58 : descSegment(_segment), isvoid(true) {}
59 bool isVoid() const { return isvoid; }
60 ompd_rc_t getElementOffset(const char *fieldName, ompd_size_t *offset);
61 ompd_rc_t getElementSize(const char *fieldName, ompd_size_t *size);
62 ompd_rc_t getBitfieldMask(const char *fieldName, uint64_t *bitfieldmask);
63 ompd_rc_t getSize(ompd_size_t *size);
64 friend TValue;
65 friend TTypeFactory;
68 static TType nullType(true);
70 /**
71 * class TError
72 * As TValue is designed to concatenate operations, we use TError
73 * to catch errors that might happen on each operation and provide
74 * the according error code and which operation raised the error.
77 class TError {
78 protected:
79 ompd_rc_t errorCode;
80 TError() : errorCode(ompd_rc_ok) {}
81 TError(const ompd_rc_t &error) : errorCode(error) {}
83 public:
84 std::string toString() {
85 return std::string("TError messages not implemented yet");
87 friend TValue;
88 friend TBaseValue;
91 /**
92 * class TValue
93 * This class encapsules the access to target values by using OMPD
94 * callback functions. The member functions are designed to concatenate
95 * the operations that are needed to access values from structures
96 * e.g., _a[6]->_b._c would read like :
97 * TValue(ctx,
98 * "_a").cast("A",2).getArrayElement(6).access("_b").cast("B").access("_c")
101 class TValue {
102 protected:
103 TError errorState;
104 TType *type;
105 int pointerLevel;
106 ompd_address_space_context_t *context;
107 ompd_thread_context_t *tcontext;
108 ompd_address_t symbolAddr;
109 ompd_size_t fieldSize;
111 public:
112 static const ompd_callbacks_t *callbacks;
113 static ompd_device_type_sizes_t type_sizes;
115 TValue() : errorState(ompd_rc_error) {}
117 * Create a target value object from symbol name
119 TValue(ompd_address_space_context_t *_context, const char *_valueName,
120 ompd_addr_t segment = OMPD_SEGMENT_UNSPECIFIED)
121 : TValue(_context, NULL, _valueName, segment) {}
123 TValue(ompd_address_space_context_t *context, ompd_thread_context_t *tcontext,
124 const char *valueName, ompd_addr_t segment = OMPD_SEGMENT_UNSPECIFIED);
126 * Create a target value object from target value address
128 TValue(ompd_address_space_context_t *_context, ompd_address_t _addr)
129 : TValue(_context, NULL, _addr) {}
130 TValue(ompd_address_space_context_t *context, ompd_thread_context_t *tcontext,
131 ompd_address_t addr);
133 * Cast the target value object to some type of typeName
135 * This call modifies the object and returns a reference to the modified
136 * object
138 TValue &cast(const char *typeName);
141 * Cast the target value object to some pointer of type typename
142 * pointerlevel gives the number of *
143 * e.g., char** would be: cast("char",2)
145 * This call modifies the object and returns a reference to the modified
146 * object
148 TValue &cast(const char *typeName, int pointerLevel,
149 ompd_addr_t segment = OMPD_SEGMENT_UNSPECIFIED);
152 * Get the target address of the target value
154 ompd_rc_t getAddress(ompd_address_t *addr) const;
156 * Get the raw memory copy of the target value
158 ompd_rc_t getRawValue(void *buf, int count);
160 * Fetch a string copy from the target. "this" represents the pointer
161 * that holds the value of a null terminated character string. "buf"
162 * points to the destination string to be allocated and copied to.
163 * Returns 'ompd_rc_error' to signify a truncated string or a target
164 * read error.
166 ompd_rc_t getString(const char **buf);
168 * Get a new target value object for the dereferenced target value
169 * reduces the pointer level, uses the target value as new target address,
170 * keeps the target type
172 TValue dereference() const;
174 * Cast to a base type
175 * Only values of base type may be read from target
177 TBaseValue castBase(ompd_target_prim_types_t baseType) const;
179 * Cast to a base type
180 * Get the size by fieldsize from runtime
182 TBaseValue castBase() const;
184 * Cast to a base type
185 * Get the size by name from the rtl
187 TBaseValue castBase(const char *varName);
189 * Resolve field access for structs/unions
190 * this supports both "->" and "." operator.
192 TValue access(const char *fieldName) const;
194 * Tests for a field bit in a bitfield
196 ompd_rc_t check(const char *bitfieldName, ompd_word_t *isSet) const;
198 * Get an array element
200 TValue getArrayElement(int elemNumber) const;
202 * Get an element of a pointer array
204 TValue getPtrArrayElement(int elemNumber) const;
206 * Did we raise some error yet?
208 bool gotError() const { return errorState.errorCode != ompd_rc_ok; }
210 * Get the error code
212 ompd_rc_t getError() const { return errorState.errorCode; }
214 * Did we raise some error yet?
216 std::string getErrorMessage() { return errorState.toString(); }
219 class TBaseValue : public TValue {
220 protected:
221 ompd_size_t baseTypeSize = 0;
222 TBaseValue(const TValue &, ompd_target_prim_types_t baseType);
223 TBaseValue(const TValue &, ompd_size_t baseTypeSize);
225 public:
226 ompd_rc_t getValue(void *buf, int count);
227 template <typename T> ompd_rc_t getValue(T &buf);
229 friend TValue;
232 template <typename T> ompd_rc_t TBaseValue::getValue(T &buf) {
233 assert(sizeof(T) >= baseTypeSize);
234 if (sizeof(T) == baseTypeSize)
235 return getValue(&buf, 1);
237 char tmp[sizeof(T)];
238 ompd_rc_t ret = getValue(tmp, 1);
239 switch (baseTypeSize) {
240 case 1:
241 buf = (T) * ((int8_t *)tmp);
242 break;
243 case 2:
244 buf = (T) * ((int16_t *)tmp);
245 break;
246 case 4:
247 buf = (T) * ((int32_t *)tmp);
248 break;
249 case 8:
250 buf = (T) * ((int64_t *)tmp);
251 break;
252 default:
253 assert(0 && "Invalid baseTypeSize");
254 break;
256 return ret;
259 #define EXTERN_C extern "C"
260 #else
261 #define EXTERN_C
262 #endif
264 #endif /*SRC_TARGET_VALUE_H_*/