Ada Programming/Representation clauses

There are several forms to specify the representation of items.

Attributes
Day_Of_Month  1 .. 31;  Day_Of_Month'       8; Day_Of_Month' 1;

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_Name Position  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:

Device_Register Ready : Status_Flag; Error : Error_Flag; Data : Unsigned_16; ;  Device_Register Ready 0   0 .. 0;   Error  0   1 .. 1;   Data   0  16 .. 31; ;

Alternatively with identical result:

Device_Register Ready 0  0 .. 0;   Error  0  1 .. 1;   Data   1  0 .. 15; ;

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: T  1000 .. 1007;

Rec A: Integer 0 .. 1;   B: Boolean; C: T; ;

Rec B 0  0 .. 1;   C  0  4 .. 6;   ;

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  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.

Status_Flag  (Ready, Wait); Status_Flag (Ready => 0, Wait => 1);

Another representation:

Status_Flag (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.