Debian Codegraph @

visa/iga/GEDLibrary/GED_external/Source/ged/xcoder/ged_restrictions_handler.h

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2017-2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 #ifndef GED_RESTRICTIONS_HANDLER_H
10 #define GED_RESTRICTIONS_HANDLER_H
11
12 #include "common/ged_base.h"
13 #include "common/ged_ins_decoding_table.h"
14
15
16 class GEDRestrictionsHandler
17 {
18 public:
19 // STATIC FUNCTIONS
20
21 template<typename NumType>
22 static NumType HandleDecodingRestrictions(const ged_ins_field_entry_t* dataEntry, NumType val, GED_RETURN_VALUE& ret);
23
24 template<typename NumType>
25 inline static bool HandleEncodingRestrictions(const ged_ins_field_entry_t* dataEntry, const GED_VALUE_TYPE valueType,
26 NumType& val);
27
28 template<typename NumType>
29 static bool ConvertEnumeratedValueToRawEncodedValue(NumType& val, const uint32_t maxValue,
30 const ged_field_enum_table_t enumTable);
31
32 template<typename NumType>
33 static bool ConvertRawEncodedValueToEnumeratedValue(NumType& val, const ged_field_enum_table_t enumTable);
34
35 template<typename NumType>
36 static inline bool IsNegative(const NumType val, const uint8_t highBit) { return (0 != (val & checkNegativeTable[highBit])); }
37
38 private:
39 inline static bool CheckMaxValue(const int32_t val, const uint8_t bits);
40 inline static bool CheckMaxValue(const int64_t val, const uint8_t bits);
41 inline static bool CheckMaxValue(const uint32_t val, const uint8_t bits);
42 inline static bool CheckMaxValue(const uint64_t val, const uint8_t bits);
43 inline static bool CheckMaxValue(const int32_t val, const ged_field_type_modifier_t& fieldType);
44 inline static bool CheckMaxValue(const int64_t val, const ged_field_type_modifier_t& fieldType);
45 inline static bool CheckMaxValue(const uint32_t val, const ged_field_type_modifier_t& fieldType);
46 inline static bool CheckMaxValue(const uint64_t val, const ged_field_type_modifier_t& fieldType);
47
48 template<typename NumType>
49 inline static bool VerifyValueRestriction(const NumType val, const ged_field_restriction_value_t resVal);
50
51 template<typename NumType>
52 inline static bool VerifyRangeRestriction(const NumType val, const ged_field_restriction_range_t& range);
53
54 template<typename NumType>
55 inline static bool VerifyMaskRestriction(const NumType val, const ged_field_restriction_mask_t mask);
56
57 template<typename NumType>
58 inline static void HandleDuplication(GEDFORASSERT(const ged_ins_field_entry_t* dataEntry) COMMA NumType& val,
59 const ged_field_type_modifier_t::ged_field_type_attributes& fieldAttributes);
60
61 template<typename NumType>
62 inline static bool HandleNonEnumEncodingRestrictions(const ged_ins_field_entry_t* dataEntry, NumType& val);
63
64 template<typename NumType>
65 static bool HandleNonEnumEncodingRestriction(const ged_ins_field_entry_t* dataEntry,
66 const ged_field_restriction_t* restriction, NumType& val);
67
68 private:
69 // PRIVATE STATIC DATA MEMBERS
70
71 // The table below is used for checking if a value is negative. Since instruction fields are currently limited to 64 bits, we only
72 // need 64 entries (for the 64 possible locations of the field's MSB).
73 static const int64_t checkNegativeTable[GED_QWORD_BITS];
74 };
75
76
77 /*************************************************************************************************
78 * class GEDRestrictionsHandler static functions
79 *************************************************************************************************/
80
81 template <typename NumType>
82 NumType GEDRestrictionsHandler::HandleDecodingRestrictions(const ged_ins_field_entry_t* dataEntry, NumType val, GED_RETURN_VALUE& ret)
83 {
84 GEDASSERT(NULL != dataEntry);
85 GEDASSERT(GED_RETURN_VALUE_SUCCESS == ret);
86 if (NULL == dataEntry->_restrictions) return val; // no restrictions (value modifiers)
87
88 GEDFORASSERT(bool isVariableField = false);
89 GEDFORASSERT(static const unsigned int fieldTypeLocation = 0);
90 GEDFORASSERT(unsigned int paddingLocation = 0);
91 GEDFORASSERT(bool foundEnumeration = false);
92 unsigned int restrictionIndex = 0;
93 const ged_field_restriction_t* restriction = dataEntry->_restrictions[restrictionIndex];
94 while (NULL != restriction)
95 {
96 GEDASSERT(!foundEnumeration); // enumerations are only allowed as the final restriction (followed by the NULL terminator)
97
98 // When decoding a field, only padding and enums should be handled since they convert the raw encoded value into the
99 // actual meaningful value we return to the user. This implicitly validates enumerated values, however we currently do not
100 // validate other restrictions.
101 switch (restriction->_restrictionType)
102 {
103 case GED_FIELD_RESTRICTIONS_TYPE_VALUE:
104 case GED_FIELD_RESTRICTIONS_TYPE_RANGE:
105 case GED_FIELD_RESTRICTIONS_TYPE_MASK:
106 break;
107 case GED_FIELD_RESTRICTIONS_TYPE_PADDING:
108 // Only one padding is allowed and it is either first, or immediately follows the field-type modifier.
109 GEDASSERT(paddingLocation == restrictionIndex);
110 val |= restriction->_padding._value;
111 break;
112 case GED_FIELD_RESTRICTIONS_TYPE_ENUM:
113 if (!ConvertRawEncodedValueToEnumeratedValue(val, restriction->_enumTable))
114 {
115 ret = GED_RETURN_VALUE_INVALID_VALUE;
116 return val;
117 }
118 GEDFORASSERT(foundEnumeration = true);
119 break;
120 case GED_FIELD_RESTRICTIONS_TYPE_FIELD_TYPE:
121 GEDASSERT(fieldTypeLocation == restrictionIndex);
122 GEDASSERT(!isVariableField); // there may only be one field-type modifier
123 GEDFORASSERT(isVariableField = true);
124 GEDFORASSERT(paddingLocation = 1);
125 // For non-negative values, it may be necessary to trim excess bits since the field's upper-limit bit-size may be larger
126 // than the actual bits. For negative numbers, the sign-extension has already taken care of this.
127 if (restriction->_fieldType._attr._signed && (0 != (val & checkNegativeTable[restriction->_fieldType._attr._bits - 1])))
128 {
129 break;
130 }
131 val &= BitsToMaxValue(restriction->_fieldType._attr._bits);
132 break;
133 case GED_FIELD_RESTRICTIONS_TYPE_NONE: // if there is a non-null restriction pointer, it should be a valid restriction
134 default:
135 GEDASSERT(0);
136 }
137 restriction = dataEntry->_restrictions[++restrictionIndex];
138 }
139 return val;
140 }
141
142
143 template<typename NumType>
144 bool GEDRestrictionsHandler::HandleEncodingRestrictions(const ged_ins_field_entry_t* dataEntry, const GED_VALUE_TYPE valueType,
145 NumType& val)
146 {
147 GEDASSERT(NULL != dataEntry);
148 if (NULL == dataEntry->_restrictions || GED_VALUE_TYPE_ENCODED == valueType)
149 {
150 // No restrictions or value modifiers, just check the max value.
151 return CheckMaxValue(val, dataEntry->_bitSize);
152 }
153 GEDASSERT(GED_VALUE_TYPE_PROCESSED == valueType);
154 GEDASSERT(NULL != dataEntry->_restrictions[0]);
155 if (GED_FIELD_RESTRICTIONS_TYPE_ENUM == dataEntry->_restrictions[0]->_restrictionType)
156 {
157 GEDASSERT(NULL == dataEntry->_restrictions[1]);
158 return ConvertEnumeratedValueToRawEncodedValue(val, (uint32_t)MaxValue(dataEntry), dataEntry->_restrictions[0]->_enumTable);
159 }
160 return HandleNonEnumEncodingRestrictions(dataEntry, val);
161 }
162
163
164 template<typename NumType>
165 bool GEDRestrictionsHandler::ConvertEnumeratedValueToRawEncodedValue(NumType& val, const uint32_t maxValue,
166 const ged_field_enum_table_t enumTable)
167 {
168 // Traverse the enumeration table and find the index for the given value.
169 for (uint32_t i = 0; i <= maxValue; ++i)
170 {
171 if (NULL != enumTable[i])
172 {
173 const NumType tabVal = *(((const uint32_t* const*)(enumTable))[i]);
174 if (tabVal == val)
175 {
176 val = (NumType)i; // the actual value that needs to be encoded is the index
177 return true;
178 }
179 }
180 }
181 return false; // the value was not found in the table meaning that it is not a valid value for this field.
182 }
183
184
185 template<typename NumType>
186 bool GEDRestrictionsHandler::ConvertRawEncodedValueToEnumeratedValue(NumType& val, const ged_field_enum_table_t enumTable)
187 {
188 // TODO: Need to check that the given val does not exceed the maximum value for the entry containing this enum table
189
190 if (NULL == enumTable[val]) return false; // there is no valid enumeration value for the encoded value
191 val = *(((const NumType * const*)(enumTable))[val]); // the actual decoded value should be the enumeration value
192 return true;
193 }
194
195
196 /*************************************************************************************************
197 * class GEDRestrictionsHandler private member functions
198 *************************************************************************************************/
199
200 bool GEDRestrictionsHandler::CheckMaxValue(const int32_t val, const uint8_t bits)
201 {
202 GEDASSERT(GED_DWORD_BITS >= bits);
203 if (GED_DWORD_BITS == bits) return true;
204 const int32_t shifted = (val >> (bits - 1));
205 return (0 == shifted || (int32_t)-1 == shifted);
206 }
207
208
209 bool GEDRestrictionsHandler::CheckMaxValue(const int64_t val, const uint8_t bits)
210 {
211 GEDASSERT(GED_QWORD_BITS >= bits);
212 if (GED_QWORD_BITS == bits) return true;
213 const int64_t shifted = (val >> (bits - 1));
214 return (0 == shifted || (int64_t)-1 == shifted);
215 }
216
217
218 bool GEDRestrictionsHandler::CheckMaxValue(const uint32_t val, const uint8_t bits)
219 {
220 GEDASSERT(GED_DWORD_BITS >= bits);
221 if (GED_DWORD_BITS == bits) return true;
222 return (0 == (val >> bits));
223 }
224
225
226 bool GEDRestrictionsHandler::CheckMaxValue(const uint64_t val, const uint8_t bits)
227 {
228 GEDASSERT(GED_QWORD_BITS >= bits);
229 if (GED_QWORD_BITS == bits) return true;
230 return (0 == (val >> bits));
231 }
232
233
234 bool GEDRestrictionsHandler::CheckMaxValue(const int32_t val, const ged_field_type_modifier_t& fieldType)
235 {
236 // variable fields are not allowed to be signed
237 GEDASSERT(0);
238 return false;
239 }
240
241
242 bool GEDRestrictionsHandler::CheckMaxValue(const int64_t val, const ged_field_type_modifier_t& fieldType)
243 {
244 // variable fields are not allowed to be signed
245 GEDASSERT(0);
246 return false;
247 }
248
249
250 bool GEDRestrictionsHandler::CheckMaxValue(const uint32_t val, const ged_field_type_modifier_t& fieldType)
251 {
252 return (fieldType._attr._signed) ? CheckMaxValue((int32_t)val, fieldType._attr._bits) : CheckMaxValue(val, fieldType._attr._bits);
253 }
254
255
256 bool GEDRestrictionsHandler::CheckMaxValue(const uint64_t val, const ged_field_type_modifier_t& fieldType)
257 {
258 return (fieldType._attr._signed) ? CheckMaxValue((int64_t)val, fieldType._attr._bits) : CheckMaxValue(val, fieldType._attr._bits);
259 }
260
261
262 template<typename NumType>
263 bool GEDRestrictionsHandler::VerifyValueRestriction(const NumType val, const ged_field_restriction_value_t resVal)
264 {
265 return ((NumType)(resVal) == val);
266 }
267
268
269 template<typename NumType>
270 bool GEDRestrictionsHandler::VerifyRangeRestriction(const NumType val, const ged_field_restriction_range_t& range)
271 {
272 return ((NumType)(range._min) <= val && (NumType)(range._max) >= val);
273 }
274
275
276 template<typename NumType>
277 bool GEDRestrictionsHandler::VerifyMaskRestriction(const NumType val, const ged_field_restriction_mask_t mask)
278 {
279 return (0 == ((NumType)mask & val));
280 }
281
282
283 template<typename NumType>
284 void GEDRestrictionsHandler::HandleDuplication(GEDFORASSERT(const ged_ins_field_entry_t* dataEntry) COMMA NumType& val,
285 const ged_field_type_modifier_t::ged_field_type_attributes& fieldAttributes)
286 {
287 if (!fieldAttributes._duplicate) return;
288
289 // Apply value duplication.
290 GEDASSERT(fieldAttributes._bits * 2 <= dataEntry->_bitSize);
291 GEDASSERT(fieldAttributes._bits * 2 <= sizeof(NumType) * GED_BYTE_BITS);
292 val &= BitsToMaxValue(fieldAttributes._bits);
293 val |= (val << fieldAttributes._bits);
294 }
295
296
297 template<typename NumType>
298 bool GEDRestrictionsHandler::HandleNonEnumEncodingRestrictions(const ged_ins_field_entry_t* dataEntry, NumType& val)
299 {
300 // TODO: Currently this is hardcoded to two, need to extend this to a given number.
301 for (unsigned int i = 0; i < 2; ++i)
302 {
303 if (NULL == dataEntry->_restrictions[i]) return true;
304 if (!HandleNonEnumEncodingRestriction(dataEntry, dataEntry->_restrictions[i], val)) return false;
305 }
306 return true;
307 }
308
309
310 template<typename NumType>
311 bool GEDRestrictionsHandler::HandleNonEnumEncodingRestriction(const ged_ins_field_entry_t* dataEntry,
312 const ged_field_restriction_t* restriction, NumType& val)
313 {
314 GEDASSERT(NULL != restriction);
315
316 switch (restriction->_restrictionType)
317 {
318 case GED_FIELD_RESTRICTIONS_TYPE_VALUE:
319 if (!CheckMaxValue(val, dataEntry->_bitSize)) return false; // TODO: This was an assert, should switch back for optimization?
320 return VerifyValueRestriction(val, restriction->_value);
321 case GED_FIELD_RESTRICTIONS_TYPE_RANGE:
322 if (!CheckMaxValue(val, dataEntry->_bitSize)) return false; // TODO: This was an assert, should switch back for optimization?
323 return VerifyRangeRestriction(val, restriction->_range);
324 case GED_FIELD_RESTRICTIONS_TYPE_MASK:
325 if (!CheckMaxValue(val, dataEntry->_bitSize)) return false; // TODO: This was an assert, should switch back for optimization?
326 return VerifyMaskRestriction(val, restriction->_mask);
327 case GED_FIELD_RESTRICTIONS_TYPE_PADDING:
328 if (!CheckMaxValue(val, dataEntry->_bitSize)) return false;
329 return ((NumType)(restriction->_padding._value) == ((NumType)(restriction->_padding._mask) & val));
330 case GED_FIELD_RESTRICTIONS_TYPE_FIELD_TYPE:
331 GEDASSERT(GED_QWORD_BITS >= restriction->_fieldType._attr._bits);
332 GEDASSERT(0 < restriction->_fieldType._attr._bits);
333 if (!CheckMaxValue(val, restriction->_fieldType)) return false;
334 HandleDuplication(GEDFORASSERT(dataEntry) COMMA val, restriction->_fieldType._attr);
335 return true;
336 case GED_FIELD_RESTRICTIONS_TYPE_ENUM:
337 GEDASSERT(0);
338 case GED_FIELD_RESTRICTIONS_TYPE_NONE: // if there is a non-null restriction pointer, it should be a valid restriction
339 GEDASSERT(0);
340 default:
341 GEDASSERT(0);
342 }
343 return false;
344 }
345
346 #endif // GED_RESTRICTIONS_HANDLER_H