Ada Programming/Representation clauses
There are several forms to specify the representation of items.
Attributes
typeDay_Of_Monthisrange1 .. 31;forDay_Of_Month'Sizeuse8; -- 8 bitsforDay_Of_Month'Alignmentuse1; -- 1 byte
Records
A record representation clause specifies the Layout aspect of a record. For any component, a component clause may be given specifying the location within the record object by its storage unit offset with respect to the address (called Position) and the bits occupied at this position (called First_Bit and Last_Bit):
Component_NameatPositionrangeFirst_Bit .. Last_Bit;
These three expressions must be static, not negative and of any integer type; they have corresponding attributes 'Position, 'First_Bit and 'Last_Bit. Note that the bit range may extend well over storage unit boundaries. Components without a component clause are located at the compiler's choice.
Example:
typeDevice_RegisterisrecordReady : Status_Flag; Error : Error_Flag; Data : Unsigned_16;endrecord;forDevice_RegisteruserecordReadyat0range0 .. 0; Errorat0range1 .. 1; -- Reserved bits Dataat0range16 .. 31;endrecord;
Alternatively with identical result:
forDevice_RegisteruserecordReadyat0range0 .. 0; Errorat0range1 .. 1; -- Reserved bits Dataat1range0 .. 15;endrecord;
The layout depends of course on whether the machine architecture is little endian or big endian. The corresponding attribute is called 'Bit_Order.
For the Data component in the native bit order, 'Position, 'First_Bit and 'Last_Bit will in both cases return 1, 0 and 15.
Biased Representation
For certain components, a so-called biased representation may be possible. For the type T, the attribute 'Size will return 10, but since it has only eight values, three bits would suffice. This can be forced with the following specification:
typeTisrange1000 .. 1007;
typeRecisrecordA: Integerrange0 .. 1; B: Boolean; C: T;endrecord;
forRecuserecordBat0range0 .. 1; Cat0range4 .. 6; -- biased representationendrecord;
Note that no size clause is needed for type T. Thus, a change of representation is performed in either way when record components and stand-alone objects of type T are assigned to one another.
Also note that for component A the compiler is free to choose any of the remaining bits of position 0, but may also use at position 1 as many bits as an integer requires in the given implementation.
Enumerations
An enumeration representation clause specifies the Coding aspect of an enumeration type with a named aggregate.
typeStatus_Flagis(Ready, Wait);forStatus_Flaguse(Ready => 0, Wait => 1); -- confirming clause
Another representation:
forStatus_Flaguse(Ready => 0, Wait => 2#100#);
The expressions for the literals must be static and of any integer type. The values must not conflict with the order.
The Aspect Pack
The aspect Pack may be used for arrays and records. It specifies that for each component as many bits are used as the attribute 'Size applied to its subtype returns.