Ada Programming/Representation clauses
There are several forms to specify the representation of items.
Attributes
[edit | edit source]type
Day_Of_Monthis
range
1 .. 31;for
Day_Of_Month'Sizeuse
8; -- 8 bitsfor
Day_Of_Month'Alignmentuse
1; -- 1 byte
Records
[edit | edit source]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_Nameat
Positionrange
First_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:
type
Device_Registeris
record
Ready : Status_Flag; Error : Error_Flag; Data : Unsigned_16;end
record
;for
Device_Registeruse
record
Readyat
0range
0 .. 0; Errorat
0range
1 .. 1; -- Reserved bits Dataat
0range
16 .. 31;end
record
;
Alternatively with identical result:
for
Device_Registeruse
record
Readyat
0range
0 .. 0; Errorat
0range
1 .. 1; -- Reserved bits Dataat
1range
0 .. 15;end
record
;
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
[edit | edit source]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:
type
Tis
range
1000 .. 1007;
type
Recis
record
A: Integerrange
0 .. 1; B: Boolean; C: T;end
record
;
for
Recuse
record
Bat
0range
0 .. 1; Cat
0range
4 .. 6; -- biased representationend
record
;
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
[edit | edit source]An enumeration representation clause specifies the Coding aspect of an enumeration type with a named aggregate.
type
Status_Flagis
(Ready, Wait);for
Status_Flaguse
(Ready => 0, Wait => 1); -- confirming clause
Another representation:
for
Status_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
[edit | edit source]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.