Debian Codegraph @

visa/iga/GEDLibrary/GED_external/Source/ged/xcoder/ged_ins.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_INS_H
10 #define GED_INS_H
11
12 #include <set>
13 #include <vector>
14 #include "common/ged_base.h"
15 #include "common/ged_ins_decoding_table.h"
16 #include "common/ged_compact_mapping_table.h"
17 #include "common/ged_validation_utils.h"
18 #include "xcoder/ged_internal_api.h"
19 #include "xcoder/ged_restrictions_handler.h"
20
21 using std::set;
22 using std::vector;
23
24
25 enum GED_INS_STATUS
26 {
27 GED_INS_STATUS_CLEAR = 0,
28 GED_INS_STATUS_NATIVE_VALID = 1,
29 GED_INS_STATUS_COMPACT_VALID = 1 << 1,
30 GED_INS_STATUS_NATIVE_ENCODED = 1 << 2,
31 GED_INS_STATUS_COMPACT_ENCODED = 1 << 3,
32 # if GED_EXPERIMENTAL
33 GED_INS_STATUS_RAW_BIT_SET = 1 << 4,
34 # endif // GED_EXPERIMENTAL
35
36 // Utility enum entries - useful combinations
37 GED_INS_STATUS_VALID = GED_INS_STATUS_NATIVE_VALID | GED_INS_STATUS_COMPACT_VALID,
38 GED_INS_STATUS_ENCODED = GED_INS_STATUS_NATIVE_ENCODED | GED_INS_STATUS_COMPACT_ENCODED
39 };
40
41
42 class GEDIns
43 {
44 public:
45 // API FUNCTIONS
46
47 /*!
48 * Initialize an empty instruction with a new opcode.
49 *
50 * @param[in] modelId GED model version as specified by @ref GED_MODEL.
51 * @param[in] opcode The enumerator representing the new opcode.
52 *
53 * @return GED_RETURN_VALUE indicating success or invalid opcode.
54 */
55 GED_RETURN_VALUE Init(const /* GED_MODEL */ uint8_t modelId, /* GED_OPCODE */ uint32_t opcode);
56
57
58 /*!
59 * Assign raw (undecoded) bytes to the GEDIns object. If the instruction is compact, it is mapped to the native format.
60 *
61 * @param[in] modelId GED model version as specified by @ref GED_MODEL.
62 * @param[in] rawBytes An array of size "size" containing the raw bytes.
63 * @param[in] size The size of the array.
64 *
65 * @return GED_RETURN_VALUE indicating success or decoding error.
66 *
67 * @note If the array is larger than GED_NATIVE_INS_SIZE, the remaining bytes are ignored.
68 */
69 GED_RETURN_VALUE Decode(const /* GED_MODEL */ uint8_t modelId, const unsigned char* rawBytes, const unsigned int size);
70
71
72 /*!
73 * Copy the raw bytes to the given rawBytes buffer. If the instruction cannot be encoded as requested, the given buffer will
74 * remain unchanged. The buffer is expected to be preallocated and large enough to hold the requested instruction bytes. One
75 * may pass a NULL pointer instead of a preallocated buffer, in which case the function will apply all encoding restrictions and
76 * return.
77 *
78 * @param[in] insType Determine if instruction should be compacted or not. If the instruction does not have a valid
79 * compact form, the rawBytes buffer will remain unchanged and error will be returned.
80 * @param[out] rawBytes A preallocated buffer in which to store the instruction bytes. The buffer is expected to be large
81 * enough to hold the requested instruction bytes.
82 *
83 * @return GED_RETURN_VALUE indicating success or encoding error.
84 */
85 GED_RETURN_VALUE Encode(const GED_INS_TYPE insType, unsigned char* rawBytes);
86
87
88 # if GED_VALIDATION_API
89 /*!
90 * Count the amount of valid compact encodings the instruction has.
91 *
92 * @param[out] count The number of valid compact encodings the instruction has. If an instruction does not have a valid
93 * compact encoding, result is undefined.
94 *
95 * @return GED_RETURN_VALUE indicating success or encoding error.
96 *
97 * @note If the given instruction is encoded in its compacted format, the function would first uncompact the instruction.
98 */
99 GED_RETURN_VALUE CountCompacted(unsigned int& count);
100
101
102 /*!
103 * Generate all valid compact encodings of the instruction. The buffer is expected to be preallocated and large enough to hold the
104 * requested instructions' bytes. Use @ref CountCompacted get the amount of expected compact encodings.
105 *
106 * @param[in] size The size of the preallocated buffer @ref rawBytes, in bytes.
107 * @param[out] compactBytesArray A preallocated buffer in which to store the instruction bytes. The buffer is expected to be
108 * large enough to hold the requested instructions' bytes.
109 *
110 * @return GED_RETURN_VALUE indicating success or encoding error.
111 *
112 * @note If the given instruction is encoded in its compacted format, the function would first uncompact the instruction.
113 */
114 GED_RETURN_VALUE RetrieveAllCompactedFormats(const unsigned int size, unsigned char* compactBytesArray);
115
116
117 /*!
118 * Print all bit positions and their mapping of given field in the instruction. If a field is invalid, print accordingly.
119 * If a fragment of the field has a fixed value ("padded"), print its fixed value.
120 *
121 * @param[in] field Id of the requested field.
122 *
123 * @return GED_RETURN_VALUE indicating success or encoding error.
124 *
125 */
126 GED_RETURN_VALUE PrintFieldBitLocation(const /* GED_INS_FIELD */ uint32_t field) const;
127
128 # endif // GED_VALIDATION_API
129
130 /*!
131 * Return description of how a field is encoded within instructions. Top 16bits of
132 * each fragment specifies the length, whereas bottom 16bits specifies LSB of field.
133 * Pass NULL as fragments to have a function return the number of elements in 'length'.
134 *
135 * @param[in] field Id of the requested field.
136 * @param[in] fragments Array of fragments (*length in size)
137 * @param[in] length Number of entries in fragments
138 *
139 * @return GED_RETURN_VALUE indicating success or encoding error.
140 *
141 */
142 GED_RETURN_VALUE QueryFieldBitLocation(const /* GED_INS_FIELD */ uint32_t field, uint32_t *fragments, uint32_t *length) const;
143
144 /*!
145 * Get the GED_MODEL Id for the GED model version by which this instruction is decoded.
146 *
147 * @return The requested GED_MODEL Id.
148 */
149 inline /* GED_MODEL */ uint8_t GetCurrentModel() const { return _modelId; }
150
151
152 /*!
153 * Get the instruction's opcode in the form of a GED_OPCODE enumerator. Use @ref GetRawOpcode for obtaining the raw encoding of
154 * the opcode in the GED model instruction.
155 *
156 * @return The enumerator representing the instruction's opcode.
157 *
158 * @note This function is predefined and does not depend on the active model.
159 */
160 inline /* GED_OPCODE */ uint32_t GetOpcode() const { return *((uint32_t*)(GetCurrentModelData().Opcodes)[_opcode]); }
161
162
163 /*!
164 * Get the instruction raw opcode as it is encoded in the GED model instruction. Use @ref GetOpcode for obtaining the GED_OPCODE for
165 * this opcode.
166 *
167 * @return The opcode encoding.
168 *
169 * @note This function is predefined and does not depend on the active model.
170 */
171 inline uint8_t GetRawOpcode() const { return _opcode; }
172
173
174 /*!
175 * Ascertain whether this instruction was modified (i.e. on of the Set***Field APIs was called) since it was last decoded/encoded
176 * (i.e since the last call to @ref Decode or @ref Encode).
177 *
178 * @return TRUE if the instruction was modified since the last decode/encode operation.
179 */
180 inline bool IsModified() const { return !IsEncoded(); }
181
182
183 /*!
184 * Indicates whether the instruction is encoded as compact or not. For decoded instructions it reflects the state of the CmptCtrl
185 * bit. For Encoded instructions, it reflects the encoded status. If @ref Encode was successfully called with compact=TRUE then
186 * the function will return TRUE. If @ref Encode was successfully called with GED_INS_TYPE_COMPACT, then the function will return
187 * FALSE regardless of the validity of the instruction's compact form. This means that even if the instruction can be encoded as
188 * compact the function will still return FALSE. If @ref IsModified returns TRUE, this function will return FALSE regardless of the
189 * validity of the instruction's compact form.
190 *
191 * @return TRUE if the instruction is in compact format.
192 *
193 * @note This function is predefined and does not depend on the active model.
194 */
195 inline bool IsCompact() const { return IsCompactEncoded(); }
196
197
198 /*!
199 * Return the size (in bytes) of the instruction as it is encoded. See @ref IsCompact for details.
200 *
201 * @return The size of the instruction.
202 */
203 inline uint32_t GetInstructionSize() const { return (IsCompact()) ? GED_COMPACT_INS_SIZE : GED_NATIVE_INS_SIZE; }
204
205
206 /*!
207 * Get the size (in bits) of the given field. If the field is invalid for this instruction, the function returns 0.
208 *
209 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
210 * For custom models, these are Ids assigned by the model parser.
211 *
212 * @return The field's size (in bits) if it is valid, 0 otherwise.
213 */
214 uint32_t GetFieldSize(const /* GED_INS_FIELD */ uint32_t field) const;
215
216 # if GED_VALIDATION_API
217 /*!
218 * Get the name of the given field.
219 *
220 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
221 * For custom models, these are Ids assigned by the model parser.
222 *
223 * @return The field's name.
224 */
225 inline const char* GetFieldName(const /* GED_INS_FIELD */ uint32_t field) const { return fieldNameByField[opcodeFieldEnumId]; }
226 # endif // GED_VALIDATION_API
227
228
229 /*!
230 * Generic function for retrieving the value of a signed instruction field of size up to 32 bits. If the field size is less than
231 * the size of int32_t, the returned value is sign extended. If the field is not valid for this instruction, -1 is returned.
232 *
233 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
234 * For custom models, these are Ids assigned by the model parser.
235 * @param[out] ret The function stores a GED_RETURN_VALUE indicating success or decoding error.
236 *
237 * @return On success (the field is valid), the function returns the requested field's value, -1 otherwise.
238 *
239 * @note -1 may be a valid value for this field, so it is important to check the GED_RETURN_VALUE result.
240 */
241 inline SignedDNum GetSignedField(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret)
242 { SignedDNum num = { GetField<int32_t>(field, ret) }; return num; }
243
244
245 /*!
246 * Generic function for retrieving the value of an unsigned instruction field of size up to 32 bits. If the field is not valid for
247 * this instruction, the uint32_t equivalent of -1 is returned (0xFFFFFFFF).
248 *
249 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
250 * For custom models, these are Ids assigned by the model parser.
251 * @param[out] ret The function stores a GED_RETURN_VALUE indicating success or decoding error.
252 *
253 * @return On success (the field is valid), the function returns the requested field's value, 0xFFFFFFFF otherwise.
254 *
255 * @note Although unlikely, 0xFFFFFFFF may be a valid value for this field, so it is important to check the
256 * GED_RETURN_VALUE result.
257 */
258 inline uint32_t GetUnsignedField(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret)
259 { uint32_t num = GetField<uint32_t>(field, ret); return num; }
260
261
262 /*!
263 * Generic function for retrieving the value of a signed instruction field of size up to 64 bits. If the field size is less than
264 * the size of int64_t, the returned value is sign extended. If the field is not valid for this instruction, -1 is returned.
265 *
266 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
267 * For custom models, these are Ids assigned by the model parser.
268 * @param[out] ret The function stores a GED_RETURN_VALUE indicating success or decoding error.
269 *
270 * @return On success (the field is valid), the function returns the requested field's value, -1 otherwise.
271 *
272 * @note -1 may be a valid value for this field, so it is important to check the GED_RETURN_VALUE result.
273 */
274 inline SignedQNum GetSigned64Field(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret)
275 { SignedQNum num = { GetField<int64_t>(field, ret) }; return num; }
276
277
278 /*!
279 * Generic function for retrieving the value of an unsigned instruction field of size up to 64 bits. If the field is not valid for
280 * this instruction, the uint64_t equivalent of -1 is returned (0xFFFFFFFFFFFFFFFF).
281 *
282 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
283 * For custom models, these are Ids assigned by the model parser.
284 * @param[out] ret The function stores a GED_RETURN_VALUE indicating success or decoding error.
285 *
286 * @return On success (the field is valid), the function returns the requested field's value, 0xFFFFFFFFFFFFFFFF otherwise.
287 *
288 * @note Although unlikely, 0xFFFFFFFFFFFFFFFF may be a valid value for this field, so it is important to check the
289 * GED_RETURN_VALUE result.
290 */
291 inline uint64_t GetUnsigned64Field(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret)
292 { uint64_t num = GetField<uint64_t>(field, ret); return num; }
293
294
295 /*!
296 * In some cases, it may be useful to get the raw encoded value of the given field in the instruction bytes. This value is always
297 * an unsigned value. It is not padded or sign extended, and is not translated to an enumeration.
298 *
299 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
300 * For custom models, these are Ids assigned by the model parser.
301 * @param[out] ret The function stores a GED_RETURN_VALUE indicating success or decoding error.
302 *
303 * @return On success (the field is valid), the function returns the requested field's value, 0xFFFFFFFF otherwise.
304 *
305 * @note Although unlikely, 0xFFFFFFFF may be a valid value for this field, so it is important to check the
306 * GED_RETURN_VALUE result.
307 */
308 inline uint32_t GetRawField(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret) const;
309
310
311 /*!
312 * In some cases, it may be useful to get the raw encoded value of the given field in the instruction bytes. This value is always
313 * an unsigned value. It is not padded or sign extended, and is not translated to an enumeration.
314 *
315 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
316 * For custom models, these are Ids assigned by the model parser.
317 * @param[out] ret The function stores a GED_RETURN_VALUE indicating success or decoding error.
318 *
319 * @return On success (the field is valid), the function returns the requested field's value, 0xFFFFFFFF otherwise.
320 *
321 * @note Although unlikely, 0xFFFFFFFFFFFFFFFF may be a valid value for this field, so it is important to check the
322 * GED_RETURN_VALUE result.
323 */
324 inline uint64_t GetRawQuadField(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret) const;
325
326
327 /*!
328 * Set the instruction opcode.
329 *
330 * @param[in] opcode The enumerator representing the new opcode.
331 *
332 * @return GED_RETURN_VALUE indicating success or encoding error.
333 *
334 * @note This function is predefined and does not depend on the active model.
335 */
336 GED_RETURN_VALUE SetOpcode(/* GED_OPCODE */ uint32_t opcode);
337
338
339 /*!
340 * Generic function for modifying the value of a signed instruction field of size up to 32 bits.
341 *
342 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
343 * For custom models, these are Ids assigned by the model parser.
344 * @param[in] val The value to store in the given field.
345 *
346 * @return GED_RETURN_VALUE indicating success or encoding error.
347 */
348 inline GED_RETURN_VALUE SetSignedField(const /* GED_INS_FIELD */ uint32_t field, const int32_t val)
349 { GED_RETURN_VALUE ret = GED_RETURN_VALUE_SUCCESS; ret = SetField(field, val); return ret; }
350
351
352 /*!
353 * Generic function for modifying the value of an unsigned instruction field of size up to 32 bits.
354 *
355 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
356 * For custom models, these are Ids assigned by the model parser.
357 * @param[in] val The value to store in the given field.
358 *
359 * @return GED_RETURN_VALUE indicating success or encoding error.
360 */
361 inline GED_RETURN_VALUE SetUnsignedField(const /* GED_INS_FIELD */ uint32_t field, const uint32_t val)
362 { GED_RETURN_VALUE ret = GED_RETURN_VALUE_SUCCESS; ret = SetField(field, val); return ret; }
363
364
365 /*!
366 * Generic function for modifying the value of a signed instruction field of size up to 64 bits.
367 *
368 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
369 * For custom models, these are Ids assigned by the model parser.
370 * @param[in] val The value to store in the given field.
371 *
372 * @return GED_RETURN_VALUE indicating success or encoding error.
373 */
374 inline GED_RETURN_VALUE SetSigned64Field(const /* GED_INS_FIELD */ uint32_t field, const int64_t val)
375 { GED_RETURN_VALUE ret = GED_RETURN_VALUE_SUCCESS; ret = SetField(field, val); return ret; }
376
377
378 /*!
379 * Generic function for modifying the value of an unsigned instruction field of size up to 64 bits.
380 *
381 * @param[in] field Id of the requested field. For precompiled models, these are the possible values of GED_INS_FIELD.
382 * For custom models, these are Ids assigned by the model parser.
383 * @param[in] val The value to store in the given field.
384 *
385 * @return GED_RETURN_VALUE indicating success or encoding error.
386 */
387 inline GED_RETURN_VALUE SetUnsigned64Field(const /* GED_INS_FIELD */ uint32_t field, const uint64_t val)
388 { GED_RETURN_VALUE ret = GED_RETURN_VALUE_SUCCESS; ret = SetField(field, val); return ret; }
389
390
391 # if GED_EXPERIMENTAL
392 /*!
393 * Set specific raw bits in native instruction, ignoring restrictions and encoding masks.
394 *
395 * @param[in] low First bit of the area in the instruction asked to modify
396 * @param[in] high Last bit of the area in the instruction asked to modify
397 * @param[in] val The value to store in the given range.
398 *
399 * @return GED_RETURN_VALUE indicating success or encoding error.
400 *
401 * @note Calling this function prevents the application of encoding masks when calling Encode().
402 */
403 GED_RETURN_VALUE SetRawBits(const uint8_t low, const uint8_t high, const uint64_t val);
404 # endif // GED_EXPERIMENTAL
405
406 public:
407 // BLOCKED API FUNCTIONS
408
409 // We expect the GEDIns class to be supplied by the user. However, the user allocates a POD struct and we reinterpret_cast it
410 // to GEDIns. To enforce this usage model, the constructors, destructor and assignments operator are deleted.
411 GEDIns() = delete; // default constructor
412 GEDIns(const GEDIns&) = delete; // copy constructor
413 GEDIns& operator=(const GEDIns&) = delete; // copy-assign
414 ~GEDIns() = delete; // destructor
415 GEDIns(GEDIns&&) = delete; // move constructor
416 GEDIns& operator=(GEDIns&&) = delete; // move-assign
417
418 protected:
419 // PROTECTED MEMBER FUNCTIONS
420
421 inline const ModelData& GetCurrentModelData() const { GEDASSERT(_modelId < numOfSupportedModels); return ModelsArray[_modelId]; }
422
423
424 /*!
425 * Get the field's size in bits. If the field depends on other fields, and may have different sizes, the maximal size is
426 * returned.
427 *
428 * @return The field's size in bits.
429 */
430 uint8_t GetFieldWidth(const uint16_t field, const bool interpField = false) const;
431
432
433 /*!
434 * Get a string representation of the instruction's bytes.
435 *
436 * @return The requested string.
437 */
438 string GetInstructionBytes() const;
439
440 private:
441 // PRIVATE MEMBER FUNCTIONS
442
443 // Functions for handling predefined fields
444 // Any change to the following functions should be complemented by a corresponding change to the relevant decoding entries defined
445 // in ged_predefined_fields.cpp. These entries are hardcoded and the data therein must match the data in the functions below.
446 inline void ExtractOpcode(const unsigned char* bytes) { _opcode = uint32_t(bytes[0] & uint8_t(0x7f)); } // extract bits 0-6
447 inline void ExtractOpcode() { ExtractOpcode(_nativeBytes); }
448 inline void ExtractCmptCtrl(const unsigned char* bytes) // extract bit 29
449 {
450 const uint32_t* rawBytes = reinterpret_cast<const uint32_t*>(bytes);
451 if (0 != (rawBytes[0] & (uint32_t)0x20000000)) _status |= GED_INS_STATUS_COMPACT_VALID ;
452 }
453 inline void ExtractCmptCtrl() { ExtractCmptCtrl(_nativeBytes); }
454 inline void SetNativeOpcode()
455 {
456 uint32_t* nativeBytes = reinterpret_cast<uint32_t*>(_nativeBytes);
457 (nativeBytes[0] &= 0xffffff80) |= _opcode;
458 }
459 inline void SetCompactOpcode()
460 {
461 uint32_t* compactBytes = reinterpret_cast<uint32_t*>(_compactBytes);
462 (compactBytes[0] &= 0xffffff80) |= _opcode;
463 }
464 inline void SetNonCompact() // TODO: Remove this when implementing Mantis 3732
465 {
466 uint32_t* nativeBytes = reinterpret_cast<uint32_t*>(_nativeBytes);
467 nativeBytes[0] &= (uint32_t)0xdfffffff;
468 }
469 inline void SetCompact(unsigned char* compactBytes) // TODO: Remove this when implementing Mantis 3732
470 {
471 *(reinterpret_cast<uint32_t*>(compactBytes)) |= (uint32_t)compactInsInitializer;
472 }
473
474 // Status utility functions
475 inline bool IsNativeValid() const { return (0 != (_status & (uint8_t)GED_INS_STATUS_NATIVE_VALID)); }
476 inline bool IsCompactValid() const { return (0 != (_status & (uint8_t)GED_INS_STATUS_COMPACT_VALID)); }
477 inline bool IsValid() const { return (0 != (_status & (uint8_t)GED_INS_STATUS_VALID)); }
478 inline bool IsNativeEncoded() const { return(0 != (_status & (uint8_t)GED_INS_STATUS_NATIVE_ENCODED)); }
479 inline bool IsCompactEncoded() const { return(0 != (_status & (uint8_t)GED_INS_STATUS_COMPACT_ENCODED)); }
480 inline bool IsEncoded() const { return (0 != (_status & (uint8_t)GED_INS_STATUS_ENCODED)); }
481 inline void ClearStatus() { _status = (uint8_t)GED_INS_STATUS_CLEAR; }
482 inline void SetNativeValid() { _status |= (uint8_t)GED_INS_STATUS_NATIVE_VALID; }
483 inline void SetCompactValid() { _status |= (uint8_t)GED_INS_STATUS_COMPACT_VALID; }
484 inline void SetNativeEncoded() { _status |= (uint8_t)GED_INS_STATUS_NATIVE_ENCODED; }
485 inline void SetCompactEncoded() { _status |= (uint8_t)GED_INS_STATUS_COMPACT_ENCODED; }
486 inline void SetNativeNotValid() { _status &= (uint8_t)(~GED_INS_STATUS_NATIVE_VALID); }
487 inline void SetCompactNotValid() { _status &= (uint8_t)(~GED_INS_STATUS_COMPACT_VALID); }
488 inline void SetNativeNotEncoded() { _status &= (uint8_t)(~GED_INS_STATUS_NATIVE_ENCODED); }
489 inline void SetCompactNotEncoded() { _status &= (uint8_t)(~GED_INS_STATUS_COMPACT_ENCODED); }
490 inline void SetNotEncoded() { _status &= (uint8_t)(~GED_INS_STATUS_ENCODED); }
491 # if GED_EXPERIMENTAL
492 inline void SetEncodingMasksDisabled() { _status |= (uint8_t)(GED_INS_STATUS_RAW_BIT_SET); }
493 inline void SetEncodingMasksEnabled() { _status &= (uint8_t)(~GED_INS_STATUS_RAW_BIT_SET); }
494 inline bool ShouldApplyEncodingMasks() const { return (0 == (_status & (uint8_t)GED_INS_STATUS_RAW_BIT_SET)); }
495 # endif // GED_EXPERIMENTAL
496
497
498 // General utility functions
499 inline void SignExtend(int32_t& val, const ged_ins_field_entry_t* dataEntry) const;
500 inline void SignExtend(int64_t& val, const ged_ins_field_entry_t* dataEntry) const;
501 inline void SignExtend(uint32_t& val, const ged_ins_field_entry_t* dataEntry) const;
502 inline void SignExtend(uint64_t& val, const ged_ins_field_entry_t* dataEntry) const;
503 void SetInstructionBytes(unsigned char* dst, const unsigned char* src, unsigned int size, const unsigned int maxSize) const;
504
505 // Internal decoding functions
506 template <typename NumType>
507 NumType GetField(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret);
508
509 template <typename NumType>
510 NumType GetField(const unsigned char* bytes, const ged_ins_decoding_table_t table, /* GED_INS_FIELD */ const uint32_t field,
511 const GED_VALUE_TYPE valueType, GED_RETURN_VALUE& ret) const;
512
513 uint32_t GetMappedField(const /* GED_INS_FIELD */ uint32_t field, const unsigned char* validBits, bool& extracted) const;
514
515 // Decoding utility functions
516 inline const ged_ins_field_entry_t* GetInstructionDataEntry(ged_ins_decoding_table_t table,
517 /* GED_INS_FIELD */ uint32_t tableIndex) const;
518 const ged_ins_field_entry_t* GetDependentInstructionDataEntry(ged_ins_decoding_table_t table,
519 /* GED_INS_FIELD */ uint32_t tableIndex) const;
520 inline const ged_ins_field_entry_t* GetMappedInstructionDataEntry(ged_ins_decoding_table_t table,
521 /* GED_INS_FIELD */ uint32_t tableIndex,
522 const unsigned char* validBits, bool& extracted) const;
523 const ged_ins_field_entry_t* GetDependentMappedInstructionDataEntry(ged_ins_decoding_table_t table,
524 /* GED_INS_FIELD */ uint32_t tableIndex,
525 const unsigned char* validBits, bool& extracted) const;
526
527 inline uint32_t ExtractConsecutiveEntryValue(const unsigned char* bytes, const ged_ins_field_position_fragment_t& pos) const;
528
529 template<typename NumType>
530 NumType ExtractFragmentedEntryValue(const unsigned char* bytes, const ged_ins_field_entry_t* dataEntry) const;
531
532 template<typename NumType>
533 inline NumType ExtractFragmentValue(const unsigned char* bytes, const ged_ins_field_position_fragment_t& pos) const;
534
535 inline bool IsVariableField(const ged_ins_field_entry_t* dataEntry) const;
536 inline bool IsSignedVariableField(const ged_ins_field_entry_t* dataEntry) const;
537
538 // Mapping utility functions
539 GED_RETURN_VALUE BuildNativeInsFromCompact();
540 bool MapCurrentField(const ged_ins_decoding_table_t compactTable, const ged_compact_mapping_table_t mappingTable,
541 const /* GED_INS_FIELD */ uint32_t field, unsigned char* validBits);
542 inline const ged_compact_mapping_entry_t* GetCompactionMappingEntry(ged_compact_mapping_table_t table,
543 /* GED_INS_FIELD */ uint32_t tableIndex,
544 const unsigned char* validBits) const;
545 const ged_compact_mapping_entry_t* GetDependentCompactionMappingEntry(ged_compact_mapping_table_t table,
546 /* GED_INS_FIELD */ uint32_t tableIndex,
547 const unsigned char* validBits) const;
548 void MapRawBytes(const uint32_t src, const ged_ins_field_position_fragment_t* to, const uint32_t fromMask,
549 unsigned char* validBits);
550 void MapRawBytes(const uint32_t src, const uint32_t numOfFragments, const ged_compact_mapping_fragment_t* fragments,
551 unsigned char* validBits);
552 void MapRawBytes(const uint64_t src, const uint32_t numOfFragments, const ged_compact_mapping_fragment_t* fragments,
553 unsigned char* validBits);
554 void MapReppedValue(const uint32_t src, const ged_ins_field_position_fragment_t* to,
555 const ged_ins_field_position_fragment_t* from, unsigned char* validBits);
556 void MapOneToOneValue(const uint32_t src, const ged_ins_field_position_fragment_t* to,
557 const ged_ins_field_position_fragment_t* from, unsigned char* validBits);
558 void MapFixedValue(const uint32_t value, const ged_ins_field_position_fragment_t* to, unsigned char* validBits);
559 inline void SetMappedBits(const uint8_t dwordIndex, const uint32_t mask, const uint32_t value, unsigned char* validBits);
560
561 void EmitMappingCyclicDependencyError(const set<uint32_t>& unMapped, const unsigned char* validBits) const;
562
563
564 // Internal encoding functions
565 template<typename NumType>
566 GED_RETURN_VALUE SetField(const /* GED_INS_FIELD */ uint32_t field, const NumType val);
567
568 template<typename NumType>
569 GED_RETURN_VALUE SetField(unsigned char* bytes, const ged_ins_decoding_table_t table, const /* GED_INS_FIELD */ uint32_t field,
570 const GED_VALUE_TYPE valueType, NumType val) const;
571
572 // Encoding utility functions
573 bool BuildCompactInsFromNative();
574 # if GED_VALIDATION_API
575 bool CountCompactFormats(unsigned int& count);
576 bool CountCurrentField(const ged_ins_decoding_table_t compactTable, const ged_compact_mapping_table_t mappingTable,
577 const unsigned char* const orMask, const /* GED_INS_FIELD */ uint32_t field, unsigned int& count);
578 bool CountCompactionTableEntry(uint64_t& val, const uint64_t& valMask, const uint32_t tableSize,
579 ged_compaction_table_t table, unsigned int& count) const;
580 bool BuildAllCompactedFormats(unsigned char* compactBytesArray, const unsigned size);
581 bool CollectCurrentValueField(const ged_ins_decoding_table_t compactTable, const ged_compact_mapping_table_t mappingTable,
582 const /* GED_INS_FIELD */ uint32_t field, unsigned char* buf);
583 vector<vector<unsigned char> > CollectCurrentMappedFields(const ged_ins_decoding_table_t compactTable,
584 const ged_compact_mapping_table_t mappingTable,
585 const unsigned char* const orMask,
586 /* GED_INS_FIELD */ uint32_t field, bool& succeeded);
587 # endif // GED_VALIDATION_API
588 bool CollectCurrentField(const ged_ins_decoding_table_t compactTable, const ged_compact_mapping_table_t mappingTable,
589 const unsigned char* const orMask, const /* GED_INS_FIELD */ uint32_t field);
590
591 bool CollectFragmentedEntryDWValue(uint32_t& val, const unsigned char* bytes, const ged_compact_mapping_entry_t* mappingEntry) const;
592 bool CollectFragmentedEntryQWValue(uint64_t& val, const unsigned char* bytes, const ged_compact_mapping_entry_t* mappingEntry) const;
593 bool CollectFragmentValue(uint32_t& val, const unsigned char* bytes, const ged_compact_mapping_fragment_t& mapping) const;
594 bool FindCompactionTableEntry(uint64_t& val, const uint64_t& valMask, const uint32_t tableSize,
595 ged_compaction_table_t table) const;
596 void ApplyNativeEncodingMasks();
597 void ApplyCompactEncodingMasks(unsigned char* compactBytes);
598 void BuildNativeOrMask(unsigned char* orMask) const;
599
600 template<typename NumType>
601 void SetFragment(unsigned char* bytes, const ged_ins_field_position_fragment_t& fragment, NumType fullVal) const;
602
603 // String utilities
604 string GetInstructionBytes(const unsigned char* instructionBytes, int dwords) const;
605 # if GED_VALIDATION_API
606 bool RecordPadding(vector<ged_ins_field_mapping_fragment_t> &mappingFragments, const ged_ins_field_entry_t* dataEntry) const;
607 # endif // GED_VALIDATION_API
608 bool RecordPosition(vector<ged_ins_field_mapping_fragment_t> &mappingFragments, const ged_ins_field_entry_t* dataEntry) const;
609 void RecordSingleFragment(vector<ged_ins_field_mapping_fragment_t> &mappingFragments,
610 const ged_ins_field_position_fragment_t &position) const;
611 void MergeFragments(vector<ged_ins_field_mapping_fragment_t> &mappingFragments) const;
612
613 private:
614 // PRIVATE STATIC DATA MEMBERS
615
616 // The table below is used for sign-extending signed fields. Since instruction fields are currently limited to 64 bits, we only
617 // need 64 entries (for the 64 possible locations of the field's MSB). The field is expected to be extracted from the instruction
618 // bytes prior to performing the sign-extension check and apply operations.
619 static const int64_t signExtendTable[GED_QWORD_BITS];
620
621 // This is the predefined enumerator (index) of the opcode field in the GED_INS_FIELD autogenerated enumeration.
622 static const unsigned int opcodeFieldEnumId{ 0 };
623
624 // Initialization constants.
625 static const uint64_t compactInsInitializer{ 0x20000000 }; // TODO: Remove this when implementing Mantis 3732
626 static const uint8_t invalidOpcode{ MAX_UINT8_T };
627
628 private:
629 // PRIVATE DATA MEMBERS
630
631 unsigned char _nativeBytes[GED_NATIVE_INS_SIZE];
632 unsigned char _compactBytes[GED_COMPACT_INS_SIZE];
633
634 uint8_t _opcode;
635
636 // Indicates the instruction's status:
637 // When a new instruction is assigned, it is initially stored in the _nativeBytes array. If the compact bit is set, the instruction
638 // is immediately expanded into its native format, in which case _status will show that both formats are valid.
639 /* GED_INS_STATUS */ uint8_t _status;
640
641 /* GED_MODEL */ uint8_t _modelId;
642
643 ged_ins_decoding_table_t _decodingTable;
644 };
645
646
647 /*************************************************************************************************
648 * class GEDIns API functions
649 *************************************************************************************************/
650
651 uint32_t GEDIns::GetRawField(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret) const
652 {
653 GEDASSERT(!MAY_BE_QWORD(fieldTypesByField[field]));
654 uint32_t num = GetField<uint32_t>(_nativeBytes, _decodingTable, field, GED_VALUE_TYPE_ENCODED, ret);
655 return num;
656 }
657
658 uint64_t GEDIns::GetRawQuadField(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret) const
659 {
660 uint64_t num = GetField<uint64_t>(_nativeBytes, _decodingTable, field, GED_VALUE_TYPE_ENCODED, ret);
661 return num;
662 }
663
664
665 /*************************************************************************************************
666 * class GEDIns private member functions
667 *************************************************************************************************/
668
669 void GEDIns::SignExtend(int32_t& val, const ged_ins_field_entry_t* dataEntry) const
670 {
671 #if defined(GED_VALIDATE)
672 if (NULL != dataEntry->_restrictions)
673 {
674 // Make sure this isn't a variable field since variable fields must use unsigned getters. The variable-field modifier must be
675 // first in the restrictions table if it exists.
676 GEDASSERT(GED_FIELD_RESTRICTIONS_TYPE_FIELD_TYPE != dataEntry->_restrictions[0]->_restrictionType);
677 }
678 #endif // GED_VALIDATE
679 const uint8_t highBit = dataEntry->_bitSize - 1;
680 if (GEDRestrictionsHandler::IsNegative(val, highBit))
681 {
682 val |= (int32_t)signExtendTable[highBit]; // sign extend
683 }
684 }
685
686
687 void GEDIns::SignExtend(int64_t& val, const ged_ins_field_entry_t* dataEntry) const
688 {
689 #if defined(GED_VALIDATE)
690 if (NULL != dataEntry->_restrictions)
691 {
692 // Make sure this isn't a variable field since variable fields must use unsigned getters. The variable-field modifier must be
693 // first in the restrictions table if it exists.
694 GEDASSERT(GED_FIELD_RESTRICTIONS_TYPE_FIELD_TYPE != dataEntry->_restrictions[0]->_restrictionType);
695 }
696 #endif // GED_VALIDATE
697 const uint8_t highBit = dataEntry->_bitSize - 1;
698 if (GEDRestrictionsHandler::IsNegative(val, highBit))
699 {
700 val |= signExtendTable[highBit]; // sign extend
701 }
702 }
703
704
705 void GEDIns::SignExtend(uint32_t& val, const ged_ins_field_entry_t* dataEntry) const
706 {
707 if (IsVariableField(dataEntry) && IsSignedVariableField(dataEntry))
708 {
709 // Apply sign extension if the field's value is negative.
710 const uint8_t highBit = dataEntry->_restrictions[0]->_fieldType._attr._bits - 1;
711 if (GEDRestrictionsHandler::IsNegative(val, highBit))
712 {
713 val |= (uint32_t)signExtendTable[highBit]; // sign extend
714 }
715 }
716 }
717
718
719 void GEDIns::SignExtend(uint64_t& val, const ged_ins_field_entry_t* dataEntry) const
720 {
721 if (IsVariableField(dataEntry) && IsSignedVariableField(dataEntry))
722 {
723 // Apply sign extension if the field's value is negative.
724 const uint8_t highBit = dataEntry->_restrictions[0]->_fieldType._attr._bits - 1;
725 if (GEDRestrictionsHandler::IsNegative(val, highBit))
726 {
727 val |= (uint64_t)signExtendTable[highBit]; // sign extend
728 }
729 }
730 }
731
732
733 template <typename NumType>
734 NumType GEDIns::GetField(const /* GED_INS_FIELD */ uint32_t field, GED_RETURN_VALUE& ret)
735 {
736 ret = GED_RETURN_VALUE_INVALID_FIELD;
737 if (GetCurrentModelData().numberOfInstructionFields <= field)
738 {
739 return (NumType) - 1;
740 }
741
742 // Try to use the native format first. It may not be valid if we are in the middle of encoding a compact instruction, in which
743 // case expand the instruction first.
744 if (!IsNativeValid())
745 {
746 GEDASSERT(IsCompactValid());
747 BuildNativeInsFromCompact(); // Should always succeed, however this is not true, see Mantis 3755
748 }
749 GEDASSERT(IsNativeValid());
750 NumType fieldValue = GetField<NumType>(_nativeBytes, _decodingTable, field, GED_VALUE_TYPE_PROCESSED, ret);
751
752 // If the field was not found and this is a compact instruction, it is possible that this field is only valid in the compact
753 // instruction format, so try it now.
754 if (GED_RETURN_VALUE_INVALID_FIELD == ret && IsCompactValid())
755 {
756 GEDASSERT(NULL != GetCurrentModelData().opcodeTables[_opcode].compactDecoding);
757 fieldValue = GetField<NumType>(_compactBytes, GetCurrentModelData().opcodeTables[_opcode].compactDecoding, field,
758 GED_VALUE_TYPE_PROCESSED, ret);
759 }
760 return fieldValue;
761 }
762
763
764 template <typename NumType>
765 NumType GEDIns::GetField(const unsigned char* bytes, const ged_ins_decoding_table_t table, const /* GED_INS_FIELD */ uint32_t field,
766 const GED_VALUE_TYPE valueType, GED_RETURN_VALUE& ret) const
767 {
768 GEDASSERT(NULL != bytes);
769 GEDASSERT(NULL != table);
770 GEDASSERT(field < GetCurrentModelData().numberOfInstructionFields);
771 GEDASSERT(field == table[field]._field);
772 ret = GED_RETURN_VALUE_INVALID_FIELD; // initialize the status return value
773
774 // Traverse the intermediate tables (if necessary).
775 const ged_ins_field_entry_t* dataEntry = GetInstructionDataEntry(table, field);
776 if (NULL == dataEntry)
777 {
778 return (NumType) - 1;
779 }
780
781 // Now that we are at the bottommost table, extract the actual data.
782 NumType val = (NumType) - 1;
783 switch (dataEntry->_entryType)
784 {
785 case GED_TABLE_ENTRY_TYPE_CONSECUTIVE:
786 // Consecutive entries are limited to one dword, but NumType may be a 64-bit type so verify the bit size of the field.
787 GEDASSERT(dataEntry->_bitSize <= GED_DWORD_BITS);
788 val = (NumType)ExtractConsecutiveEntryValue(bytes, dataEntry->_consecutive._position);
789 break;
790 case GED_TABLE_ENTRY_TYPE_FRAGMENTED:
791 val = ExtractFragmentedEntryValue<NumType>(bytes, dataEntry);
792 break;
793 case GED_TABLE_ENTRY_TYPE_FIXED_VALUE:
794 GEDASSERT(dataEntry->_bitSize <= GED_DWORD_BITS);
795 val = dataEntry->_fixed._value;
796 break;
797 default:
798 GEDASSERT(0);
799 }
800 ret = GED_RETURN_VALUE_SUCCESS;
801
802 // When using the field's encoded value as an index, we need the raw encoded value without any modifications. This includes
803 // paddings, sign extension and enumeration.
804 if (GED_VALUE_TYPE_ENCODED == valueType) return val;
805
806 // Sign extend if necessary.
807 SignExtend(val, dataEntry);
808
809 return GEDRestrictionsHandler::HandleDecodingRestrictions(dataEntry, val, ret);
810 }
811
812
813 const ged_ins_field_entry_t* GEDIns::GetInstructionDataEntry(ged_ins_decoding_table_t table,
814 /* GED_INS_FIELD */ uint32_t tableIndex) const
815 {
816 GEDASSERT(NULL != table);
817 if (table[tableIndex]._entryType <= GED_TABLE_ENTRY_TYPE_LAST_EXPLICIT) return &table[tableIndex];
818 return GetDependentInstructionDataEntry(table, tableIndex);
819 }
820
821
822 const ged_ins_field_entry_t* GEDIns::GetMappedInstructionDataEntry(ged_ins_decoding_table_t table,
823 /* GED_INS_FIELD */ uint32_t tableIndex,
824 const unsigned char* validBits, bool& extracted) const
825 {
826 GEDASSERT(NULL != table);
827 if (table[tableIndex]._entryType <= GED_TABLE_ENTRY_TYPE_LAST_EXPLICIT)
828 {
829 extracted = true;
830 return &table[tableIndex];
831 }
832 return GetDependentMappedInstructionDataEntry(table, tableIndex, validBits, extracted);
833 }
834
835
836 uint32_t GEDIns::ExtractConsecutiveEntryValue(const unsigned char* bytes, const ged_ins_field_position_fragment_t& pos) const
837 {
838 GEDASSERT(NULL != bytes);
839 uint32_t val = ((uint32_t*)bytes)[pos._dwordIndex];
840 val &= pos._bitMask;
841 val >>= pos._shift;
842 return val;
843 }
844
845
846 template<typename NumType>
847 NumType GEDIns::ExtractFragmentedEntryValue(const unsigned char* bytes, const ged_ins_field_entry_t* dataEntry) const
848 {
849 GEDASSERT(NULL != bytes);
850 GEDASSERT(NULL != dataEntry);
851 GEDASSERT(GED_TABLE_ENTRY_TYPE_FRAGMENTED == dataEntry->_entryType);
852 GEDASSERT(dataEntry->_fragmented._numOfPositionFragments > 1);
853
854 // The field is fragmented, so all fragments will eventually be gathered in fullVal.
855 NumType fullVal = 0;
856 for (unsigned int i = 0; i < dataEntry->_fragmented._numOfPositionFragments; ++i)
857 {
858 NumType fragmentVal = ExtractFragmentValue<NumType>(bytes, dataEntry->_fragmented._fragments[i]);
859 fullVal |= fragmentVal;
860 }
861 return fullVal;
862 }
863
864
865 template<typename NumType>
866 NumType GEDIns::ExtractFragmentValue(const unsigned char* bytes, const ged_ins_field_position_fragment_t& pos) const
867 {
868 NumType val = ((uint32_t*)bytes)[pos._dwordIndex];
869 val &= pos._bitMask;
870 if (0 == val) return val; // early out - nothing to do
871 if (pos._shift > 0)
872 {
873 val >>= pos._shift;
874 }
875 else if (pos._shift < 0)
876 {
877 val <<= abs(pos._shift);
878 }
879 return val;
880 }
881
882
883 bool GEDIns::IsVariableField(const ged_ins_field_entry_t* dataEntry) const
884 {
885 return ((NULL != dataEntry->_restrictions) && // the field has modifiers/restrictions
886 (GED_FIELD_RESTRICTIONS_TYPE_FIELD_TYPE == dataEntry->_restrictions[0]->_restrictionType)); // this is a variable field
887 }
888
889
890 bool GEDIns::IsSignedVariableField(const ged_ins_field_entry_t* dataEntry) const
891 {
892 GEDASSERT(IsVariableField(dataEntry));
893 return (dataEntry->_restrictions[0]->_fieldType._attr._signed); // the field is signed
894 }
895
896
897 const ged_compact_mapping_entry_t* GEDIns::GetCompactionMappingEntry(ged_compact_mapping_table_t table,
898 /* GED_INS_FIELD */ uint32_t tableIndex,
899 const unsigned char* validBits) const
900 {
901 GEDASSERT(NULL != table);
902 if (table[tableIndex]._entryType <= GED_MAPPING_TABLE_ENTRY_TYPE_LAST_EXPLICIT) return &table[tableIndex];
903 return GetDependentCompactionMappingEntry(table, tableIndex, validBits);
904 }
905
906
907 void GEDIns::SetMappedBits(const uint8_t dwordIndex, const uint32_t mask, const uint32_t value, unsigned char* validBits)
908 {
909 // GEDASSERT(0 == (((uint32_t*)_nativeBytes)[dwordIndex] & mask)); // prevents overwriting bits when uncompacting, see Mantis 4342
910 GEDASSERT((value == ((((uint32_t*)_nativeBytes)[dwordIndex] & mask) | value)));
911 ((uint32_t*)_nativeBytes)[dwordIndex] |= value;
912 ((uint32_t*)validBits)[dwordIndex] &= ~mask;
913 }
914
915
916 template<typename NumType>
917 GED_RETURN_VALUE GEDIns::SetField(const /* GED_INS_FIELD */ uint32_t field, const NumType val)
918 {
919 if (GetCurrentModelData().numberOfInstructionFields <= field) return GED_RETURN_VALUE_INVALID_FIELD;
920
921 // We try to keep both the native and compact formats valid as long as we can. The following scenarios are possible:
922 // 1. If both formats are valid:
923 // a. If the field was set in both formats, don't change the instruction's status.
924 // b. If the field was set in only one format, mark the other format invalid in the instruction's status.
925 // c. If the field was not set in both formats, the field is invalid, don't change the instruction's status.
926 // 2. If only one format is valid, only attempt to set the field in that format.
927 // a. If the field was set, it is valid, don't change the instruction's status.
928 // b. If the field was not set, it is invalid, don't change the instruction's status.
929 // 3. No format is valid - this is an internal error.
930
931 GEDASSERT(IsValid()); // at least one format must be valid
932
933 // Try to encode the field in the native format first.
934 GED_RETURN_VALUE nativeRet = GED_RETURN_VALUE_INVALID_FIELD;
935 if (IsNativeValid())
936 {
937 nativeRet = SetField(_nativeBytes, _decodingTable, field, GED_VALUE_TYPE_PROCESSED, val);
938 }
939
940 // Now try the compact format.
941 GED_RETURN_VALUE compactRet = GED_RETURN_VALUE_INVALID_FIELD;
942 if (IsCompactValid())
943 {
944 compactRet = SetField(_compactBytes, GetCurrentModelData().opcodeTables[_opcode].compactDecoding, field,
945 GED_VALUE_TYPE_PROCESSED, val);
946 if (GED_RETURN_VALUE_SUCCESS == compactRet)
947 {
948 // The compact format was updated. Check if the native format was updated as well.
949 if (GED_RETURN_VALUE_SUCCESS != nativeRet)
950 {
951 // The field was only updated in the the compact format, mark that the native format is no longer valid.
952 SetNativeNotValid();
953 }
954 }
955 else
956 {
957 if (GED_RETURN_VALUE_SUCCESS == nativeRet)
958 {
959 // The field was only updated in the native format, mark that the compact format is no longer valid.
960 SetCompactNotValid();
961 }
962 }
963 }
964
965 GEDASSERT(IsValid()); // make sure that the instruction is still valid
966 if (GED_RETURN_VALUE_SUCCESS == nativeRet || GED_RETURN_VALUE_SUCCESS == compactRet)
967 {
968 SetNotEncoded(); // the field was encoded, mark that the instruction needs to be re-encoded
969 return GED_RETURN_VALUE_SUCCESS;
970 }
971
972 // The field was not encoded. Check why.
973 if (GED_RETURN_VALUE_INVALID_VALUE == nativeRet || GED_RETURN_VALUE_INVALID_VALUE == compactRet)
974 {
975 return GED_RETURN_VALUE_INVALID_VALUE; // the field is valid, but the value is not
976 }
977
978 GEDASSERT(GED_RETURN_VALUE_INVALID_FIELD == nativeRet && GED_RETURN_VALUE_INVALID_FIELD == compactRet);
979 return GED_RETURN_VALUE_INVALID_FIELD;
980 }
981
982
983 template<typename NumType>
984 GED_RETURN_VALUE GEDIns::SetField(unsigned char* bytes, const ged_ins_decoding_table_t table, const /* GED_INS_FIELD */ uint32_t field,
985 const GED_VALUE_TYPE valueType, NumType val) const
986 {
987 GEDASSERT(NULL != bytes);
988 GEDASSERT(NULL != table);
989 GEDASSERT(field < GetCurrentModelData().numberOfInstructionFields);
990 GEDASSERT(field == table[field]._field);
991
992 // Traverse the intermediate tables (if necessary).
993 const ged_ins_field_entry_t* dataEntry = GetInstructionDataEntry(table, field);
994 if (NULL == dataEntry) return GED_RETURN_VALUE_INVALID_FIELD; // the field is not valid for this instruction
995
996 // Now that we are at the bottommost table, validate the new value and store it. Validate the field's value and handle any field
997 // modifiers e.g. if this is an enumerated field, this function convert the enumerated value to the raw value that should be
998 // encoded.
999 if (!GEDRestrictionsHandler::HandleEncodingRestrictions(dataEntry, valueType, val)) return GED_RETURN_VALUE_INVALID_VALUE;
1000
1001 // Now that we know the value is legal, we can encode it.
1002 if (GED_TABLE_ENTRY_TYPE_CONSECUTIVE == dataEntry->_entryType)
1003 {
1004 // Consecutive entries are limited to one dword, but NumType may be a 64-bit type so verify the bit size of the field.
1005 GEDASSERT(dataEntry->_bitSize <= GED_DWORD_BITS);
1006
1007 // Shift the value into position.
1008 val <<= dataEntry->_consecutive._position._shift;
1009
1010 // Apply the mask.
1011 val &= dataEntry->_consecutive._position._bitMask;
1012
1013 // Clear the field in the destination.
1014 ((uint32_t*)bytes)[dataEntry->_consecutive._position._dwordIndex] &= ~dataEntry->_consecutive._position._bitMask;
1015
1016 // Now set the field with the value itself.
1017 ((uint32_t*)bytes)[dataEntry->_consecutive._position._dwordIndex] |= val;
1018 }
1019 else if (GED_TABLE_ENTRY_TYPE_FRAGMENTED == dataEntry->_entryType)
1020 {
1021 for (unsigned int i = 0; i < dataEntry->_fragmented._numOfPositionFragments; ++i)
1022 {
1023 SetFragment(bytes, dataEntry->_fragmented._fragments[i], val);
1024 }
1025 }
1026 else if (GED_TABLE_ENTRY_TYPE_FIXED_VALUE == dataEntry->_entryType)
1027 {
1028 if (dataEntry->_fixed._value != static_cast<uint32_t>(val))
1029 {
1030 return GED_RETURN_VALUE_INVALID_VALUE;
1031 }
1032 }
1033 else
1034 {
1035 GEDASSERT(0);
1036 }
1037
1038 return GED_RETURN_VALUE_SUCCESS;
1039 }
1040
1041
1042 template<typename NumType>
1043 void GEDIns::SetFragment(unsigned char* bytes, const ged_ins_field_position_fragment_t& fragment, NumType val) const
1044 {
1045 GEDASSERT(NULL != bytes);
1046
1047 // Shift the value into position.
1048 if (fragment._shift > 0)
1049 {
1050 val <<= fragment._shift;
1051 }
1052 else if (fragment._shift < 0)
1053 {
1054 val >>= abs(fragment._shift);
1055 }
1056
1057 // Apply the mask.
1058 val &= fragment._bitMask;
1059
1060 // Clear the fragment in the destination.
1061 ((uint32_t*)bytes)[fragment._dwordIndex] &= ~fragment._bitMask;
1062
1063 // Set the fragment value in the encoded bytes.
1064 ((uint32_t*)bytes)[fragment._dwordIndex] |= (uint32_t)val;
1065 }
1066
1067 #endif // GED_INS_H