Ada Programming/Representation clauses


There are several forms to specify the representation of items.

Ada. Time-tested, safe and secure.
Ada. Time-tested, safe and secure.

Attributes

edit
type Day_Of_Month is range 1 .. 31;
for  Day_Of_Month'Size      use 8;  -- 8 bits
for  Day_Of_Month'Alignment use 1;  -- 1 byte

Records

edit

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 at Position range 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_Register is record
   Ready : Status_Flag;
   Error : Error_Flag;
   Data  : Unsigned_16;
end record;

for  Device_Register use record
   Ready at 0 range  0 ..  0;
   Error at 0 range  1 ..  1;
   -- Reserved bits
   Data  at 0 range 16 .. 31;
end record;

Alternatively with identical result:

for  Device_Register use record
   Ready at 0 range 0 ..  0;
   Error at 0 range 1 ..  1;
   -- Reserved bits
   Data  at 1 range 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

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 T is range 1000 .. 1007;
type Rec is record
   A: Integer range 0 .. 1;
   B: Boolean;
   C: T;
end record;
for Rec use record
   B at 0 range 0 .. 1;
   C at 0 range 4 .. 6;  -- biased representation
end 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

An enumeration representation clause specifies the Coding aspect of an enumeration type with a named aggregate.

type Status_Flag is  (Ready, Wait);
for  Status_Flag use (Ready => 0, Wait => 1);  -- confirming clause

Another representation:

for  Status_Flag use (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

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.