[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes the representation clauses accepted by GNAT, and their effect on the representation of corresponding data objects.
GNAT fully implements Annex C (Systems Programming). This means that all the implementation advice sections in chapter 13 are fully implemented. However, these sections only require a minimal level of support for representation clauses. GNAT provides much more extensive capabilities, and this section describes the additional capabilities provided.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
GNAT requires that all alignment clauses specify a power of 2, and all default alignments are always a power of 2. The default alignment values are as follows:
Storage_Unit
,
and the maximum alignment supported by the target.
(This maximum alignment is given by the GNAT-specific attribute
Standard'Maximum_Alignment
; see Maximum_Alignment.)
For example, for type Long_Float
, the object size is 8 bytes, and the
default alignment will be 8 on any target that supports alignments
this large, but on some targets, the maximum alignment may be smaller
than 8, in which case objects of type Long_Float
will be maximally
aligned.
Pack
is
used and all fields are packable (see separate section on pragma Pack
),
then the resulting alignment is 1.
A special case is when:
type Small is record A, B : Character; end record; for Small'Size use 16; |
then the default alignment of the record type Small
is 2, not 1. This
leads to more efficient code when the record is treated as a unit, and also
allows the type to specified as Atomic
on architectures requiring
strict alignment.
An alignment clause may
always specify a larger alignment than the default value, up to some
maximum value dependent on the target (obtainable by using the
attribute reference Standard'Maximum_Alignment
).
The only case where
it is permissible to specify a smaller alignment than the default value
is for a record with a record representation clause.
In this case, packable fields for which a component clause is
given still result in a default alignment corresponding to the original
type, but this may be overridden, since these components in fact only
require an alignment of one byte. For example, given
type V is record A : Integer; end record; for V use record A at 0 range 0 .. 31; end record; for V'alignment use 1; |
The default alignment for the type V
is 4, as a result of the
Integer field in the record, but since this field is placed with a
component clause, it is permissible, as shown, to override the default
alignment of the record with a smaller value.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The default size for a type T
is obtainable through the
language-defined attribute T'Size
and also through the
equivalent GNAT-defined attribute T'Value_Size
.
For objects of type T
, GNAT will generally increase the type size
so that the object size (obtainable through the GNAT-defined attribute
T'Object_Size
)
is a multiple of T'Alignment * Storage_Unit
.
For example
type Smallint is range 1 .. 6; type Rec is record Y1 : integer; Y2 : boolean; end record; |
In this example, Smallint'Size
= Smallint'Value_Size
= 3,
as specified by the RM rules,
but objects of this type will have a size of 8
(Smallint'Object_Size
= 8),
since objects by default occupy an integral number
of storage units. On some targets, notably older
versions of the Digital Alpha, the size of stand
alone objects of this type may be 32, reflecting
the inability of the hardware to do byte load/stores.
Similarly, the size of type Rec
is 40 bits
(Rec'Size
= Rec'Value_Size
= 40), but
the alignment is 4, so objects of this type will have
their size increased to 64 bits so that it is a multiple
of the alignment (in bits). This decision is
in accordance with the specific Implementation Advice in RM 13.3(43):
ASize
clause should be supported for an object if the specifiedSize
is at least as large as its subtype'sSize
, and corresponds to a size in storage elements that is a multiple of the object'sAlignment
(if theAlignment
is nonzero).
An explicit size clause may be used to override the default size by increasing it. For example, if we have:
type My_Boolean is new Boolean; for My_Boolean'Size use 32; |
then values of this type will always be 32 bits long. In the case of discrete types, the size can be increased up to 64 bits, with the effect that the entire specified field is used to hold the value, sign- or zero-extended as appropriate. If more than 64 bits is specified, then padding space is allocated after the value, and a warning is issued that there are unused bits.
Similarly the size of records and arrays may be increased, and the effect is to add padding bits after the value. This also causes a warning message to be generated.
The largest Size value permitted in GNAT is 2**31-1. Since this is a Size in bits, this corresponds to an object of size 256 megabytes (minus one). This limitation is true on all targets. The reason for this limitation is that it improves the quality of the code in many cases if it is known that a Size value can be accommodated in an object of type Integer.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
For tasks, the Storage_Size
clause specifies the amount of space
to be allocated for the task stack. This cannot be extended, and if the
stack is exhausted, then Storage_Error
will be raised (if stack
checking is enabled). Use a Storage_Size
attribute definition clause,
or a Storage_Size
pragma in the task definition to set the
appropriate required size. A useful technique is to include in every
task definition a pragma of the form:
pragma Storage_Size (Default_Stack_Size); |
Then Default_Stack_Size
can be defined in a global package, and
modified as required. Any tasks requiring stack sizes different from the
default can have an appropriate alternative reference in the pragma.
For access types, the Storage_Size
clause specifies the maximum
space available for allocation of objects of the type. If this space is
exceeded then Storage_Error
will be raised by an allocation attempt.
In the case where the access type is declared local to a subprogram, the
use of a Storage_Size
clause triggers automatic use of a special
predefined storage pool (System.Pool_Size
) that ensures that all
space for the pool is automatically reclaimed on exit from the scope in
which the type is declared.
A special case recognized by the compiler is the specification of a
Storage_Size
of zero for an access type. This means that no
items can be allocated from the pool, and this is recognized at compile
time, and all the overhead normally associated with maintaining a fixed
size storage pool is eliminated. Consider the following example:
procedure p is type R is array (Natural) of Character; type P is access all R; for P'Storage_Size use 0; -- Above access type intended only for interfacing purposes y : P; procedure g (m : P); pragma Import (C, g); -- ... begin -- ... y := new R; end; |
As indicated in this example, these dummy storage pools are often useful in connection with interfacing where no object will ever be allocated. If you compile the above example, you get the warning:
p.adb:16:09: warning: allocation from empty storage pool p.adb:16:09: warning: Storage_Error will be raised at run time |
Of course in practice, there will not be any explicit allocators in the case of such an access declaration.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In the case of variant record objects, there is a question whether Size gives information about a particular variant, or the maximum size required for any variant. Consider the following program
with Text_IO; use Text_IO; procedure q is type R1 (A : Boolean := False) is record case A is when True => X : Character; when False => null; end case; end record; V1 : R1 (False); V2 : R1; begin Put_Line (Integer'Image (V1'Size)); Put_Line (Integer'Image (V2'Size)); end q; |
Here we are dealing with a variant record, where the True variant requires 16 bits, and the False variant requires 8 bits. In the above example, both V1 and V2 contain the False variant, which is only 8 bits long. However, the result of running the program is:
8 16 |
The reason for the difference here is that the discriminant value of V1 is fixed, and will always be False. It is not possible to assign a True variant value to V1, therefore 8 bits is sufficient. On the other hand, in the case of V2, the initial discriminant value is False (from the default), but it is possible to assign a True variant value to V2, therefore 16 bits must be allocated for V2 in the general case, even fewer bits may be needed at any particular point during the program execution.
As can be seen from the output of this program, the 'Size
attribute applied to such an object in GNAT gives the actual allocated
size of the variable, which is the largest size of any of the variants.
The Ada Reference Manual is not completely clear on what choice should
be made here, but the GNAT behavior seems most consistent with the
language in the RM.
In some cases, it may be desirable to obtain the size of the current variant, rather than the size of the largest variant. This can be achieved in GNAT by making use of the fact that in the case of a subprogram parameter, GNAT does indeed return the size of the current variant (because a subprogram has no way of knowing how much space is actually allocated for the actual).
Consider the following modified version of the above program:
with Text_IO; use Text_IO; procedure q is type R1 (A : Boolean := False) is record case A is when True => X : Character; when False => null; end case; end record; V2 : R1; function Size (V : R1) return Integer is begin return V'Size; end Size; begin Put_Line (Integer'Image (V2'Size)); Put_Line (Integer'IMage (Size (V2))); V2 := (True, 'x'); Put_Line (Integer'Image (V2'Size)); Put_Line (Integer'IMage (Size (V2))); end q; |
The output from this program is
16 8 16 16 |
Here we see that while the 'Size
attribute always returns
the maximum size, regardless of the current variant value, the
Size
function does indeed return the size of the current
variant value.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In the case of scalars with a range starting at other than zero, it is possible in some cases to specify a size smaller than the default minimum value, and in such cases, GNAT uses an unsigned biased representation, in which zero is used to represent the lower bound, and successive values represent successive values of the type.
For example, suppose we have the declaration:
type Small is range -7 .. -4; for Small'Size use 2; |
Although the default size of type Small
is 4, the Size
clause is accepted by GNAT and results in the following representation
scheme:
-7 is represented as 2#00# -6 is represented as 2#01# -5 is represented as 2#10# -4 is represented as 2#11# |
Biased representation is only used if the specified Size
clause
cannot be accepted in any other manner. These reduced sizes that force
biased representation can be used for all discrete types except for
enumeration types for which a representation clause is given.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In Ada 95, T'Size
for a type T
is the minimum number of bits
required to hold values of type T
. Although this interpretation was
allowed in Ada 83, it was not required, and this requirement in practice
can cause some significant difficulties. For example, in most Ada 83
compilers, Natural'Size
was 32. However, in Ada 95,
Natural'Size
is
typically 31. This means that code may change in behavior when moving
from Ada 83 to Ada 95. For example, consider:
type Rec is record; A : Natural; B : Natural; end record; for Rec use record at 0 range 0 .. Natural'Size - 1; at 0 range Natural'Size .. 2 * Natural'Size - 1; end record; |
In the above code, since the typical size of Natural
objects
is 32 bits and Natural'Size
is 31, the above code can cause
unexpected inefficient packing in Ada 95, and in general there are
cases where the fact that the object size can exceed the
size of the type causes surprises.
To help get around this problem GNAT provides two implementation
defined attributes, Value_Size
and Object_Size
. When
applied to a type, these attributes yield the size of the type
(corresponding to the RM defined size attribute), and the size of
objects of the type respectively.
The Object_Size
is used for determining the default size of
objects and components. This size value can be referred to using the
Object_Size
attribute. The phrase "is used" here means that it is
the basis of the determination of the size. The backend is free to
pad this up if necessary for efficiency, e.g. an 8-bit stand-alone
character might be stored in 32 bits on a machine with no efficient
byte access instructions such as the Alpha.
The default rules for the value of Object_Size
for
discrete types are as follows:
Object_Size
for base subtypes reflect the natural hardware
size in bits (run the compiler with `-gnatS' to find those values
for numeric types). Enumeration types and fixed-point base subtypes have
8, 16, 32 or 64 bits for this size, depending on the range of values
to be stored.
Object_Size
of a subtype is the same as the
Object_Size
of
the type from which it is obtained.
Object_Size
of a derived base type is copied from the parent
base type, and the Object_Size
of a derived first subtype is copied
from the parent first subtype.
The Value_Size
attribute
is the (minimum) number of bits required to store a value
of the type.
This value is used to determine how tightly to pack
records or arrays with components of this type, and also affects
the semantics of unchecked conversion (unchecked conversions where
the Value_Size
values differ generate a warning, and are potentially
target dependent).
The default rules for the value of Value_Size
are as follows:
Value_Size
for a base subtype is the minimum number of bits
required to store all values of the type (including the sign bit
only if negative values are possible).
Value_Size
as the first subtype. This is a
consequence of RM 13.1(14) ("if two subtypes statically match,
then their subtype-specific aspects are the same".)
Value_Size
corresponding to the minimum
number of bits required to store all values of the subtype. For
dynamic bounds, it is assumed that the value can range down or up
to the corresponding bound of the ancestor
The RM defined attribute Size
corresponds to the
Value_Size
attribute.
The Size
attribute may be defined for a first-named subtype. This sets
the Value_Size
of
the first-named subtype to the given value, and the
Object_Size
of this first-named subtype to the given value padded up
to an appropriate boundary. It is a consequence of the default rules
above that this Object_Size
will apply to all further subtypes. On the
other hand, Value_Size
is affected only for the first subtype, any
dynamic subtypes obtained from it directly, and any statically matching
subtypes. The Value_Size
of any other static subtypes is not affected.
Value_Size
and
Object_Size
may be explicitly set for any subtype using
an attribute definition clause. Note that the use of these attributes
can cause the RM 13.1(14) rule to be violated. If two access types
reference aliased objects whose subtypes have differing Object_Size
values as a result of explicit attribute definition clauses, then it
is erroneous to convert from one access subtype to the other.
At the implementation level, Esize stores the Object_Size and the
RM_Size field stores the Value_Size
(and hence the value of the
Size
attribute,
which, as noted above, is equivalent to Value_Size
).
To get a feel for the difference, consider the following examples (note
that in each case the base is Short_Short_Integer
with a size of 8):
Object_Size Value_Size type x1 is range 0 .. 5; 8 3 type x2 is range 0 .. 5; for x2'size use 12; 16 12 subtype x3 is x2 range 0 .. 3; 16 2 subtype x4 is x2'base range 0 .. 10; 8 4 subtype x5 is x2 range 0 .. dynamic; 16 3* subtype x6 is x2'base range 0 .. dynamic; 8 3* |
Note: the entries marked "3*" are not actually specified by the Ada 95 RM,
but it seems in the spirit of the RM rules to allocate the minimum number
of bits (here 3, given the range for x2
)
known to be large enough to hold the given range of values.
So far, so good, but GNAT has to obey the RM rules, so the question is
under what conditions must the RM Size
be used.
The following is a list
of the occasions on which the RM Size
must be used:
Size
for a type
For record types, the Object_Size
is always a multiple of the
alignment of the type (this is true for all types). In some cases the
Value_Size
can be smaller. Consider:
type R is record X : Integer; Y : Character; end record; |
On a typical 32-bit architecture, the X component will be four bytes, and
require four-byte alignment, and the Y component will be one byte. In this
case R'Value_Size
will be 40 (bits) since this is the minimum size
required to store a value of this type, and for example, it is permissible
to have a component of type R in an outer record whose component size is
specified to be 48 bits. However, R'Object_Size
will be 64 (bits),
since it must be rounded up so that this value is a multiple of the
alignment (4 bytes = 32 bits).
For all other types, the Object_Size
and Value_Size are the same (and equivalent to the RM attribute Size
).
Only Size
may be specified for such types.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Normally, the value specified in a component clause must be consistent with the subtype of the array component with regard to size and alignment. In other words, the value specified must be at least equal to the size of this subtype, and must be a multiple of the alignment value.
In addition, component size clauses are allowed which cause the array to be packed, by specifying a smaller value. The cases in which this is allowed are for component size values in the range 1 through 63. The value specified must not be smaller than the Size of the subtype. GNAT will accurately honor all packing requests in this range. For example, if we have:
type r is array (1 .. 8) of Natural; for r'Component_Size use 31; |
then the resulting array has a length of 31 bytes (248 bits = 8 * 31). Of course access to the components of such an array is considerably less efficient than if the natural component size of 32 is used.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
For record subtypes, GNAT permits the specification of the Bit_Order
attribute. The specification may either correspond to the default bit
order for the target, in which case the specification has no effect and
places no additional restrictions, or it may be for the non-standard
setting (that is the opposite of the default).
In the case where the non-standard value is specified, the effect is to renumber bits within each byte, but the ordering of bytes is not affected. There are certain restrictions placed on component clauses as follows:
Low_Order_First
being the default, then the following two declarations have exactly
the same effect:
type R1 is record A : Boolean; B : Integer range 1 .. 120; end record; for R1 use record A at 0 range 0 .. 0; B at 0 range 1 .. 7; end record; type R2 is record A : Boolean; B : Integer range 1 .. 120; end record; for R2'Bit_Order use High_Order_First; for R2 use record A at 0 range 7 .. 7; B at 0 range 0 .. 6; end record; |
The useful application here is to write the second declaration with the
Bit_Order
attribute definition clause, and know that it will be treated
the same, regardless of whether the target is little-endian or big-endian.
Bit_Order
specification does not affect the ordering of bytes.
In particular, the following attempt at getting an endian-independent integer
does not work:
type R2 is record A : Integer; end record; for R2'Bit_Order use High_Order_First; for R2 use record A at 0 range 0 .. 31; end record; |
This declaration will result in a little-endian integer on a
little-endian machine, and a big-endian integer on a big-endian machine.
If byte flipping is required for interoperability between big- and
little-endian machines, this must be explicitly programmed. This capability
is not provided by Bit_Order
.
Since the misconception that Bit_Order automatically deals with all
endian-related incompatibilities is a common one, the specification of
a component field that is an integral number of bytes will always
generate a warning. This warning may be suppressed using
pragma Suppress
if desired. The following section contains additional
details regarding the issue of byte ordering.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In this section we will review the effect of the Bit_Order
attribute
definition clause on byte ordering. Briefly, it has no effect at all, but
a detailed example will be helpful. Before giving this
example, let us review the precise
definition of the effect of defining Bit_Order
. The effect of a
non-standard bit order is described in section 15.5.3 of the Ada
Reference Manual:
2 A bit ordering is a method of interpreting the meaning of the storage place attributes.
To understand the precise definition of storage place attributes in this context, we visit section 13.5.1 of the manual:
13 A record_representation_clause (without the mod_clause) specifies the layout. The storage place attributes (see 13.5.2) are taken from the values of the position, first_bit, and last_bit expressions after normalizing those values so that first_bit is less than Storage_Unit.
The critical point here is that storage places are taken from
the values after normalization, not before. So the Bit_Order
interpretation applies to normalized values. The interpretation
is described in the later part of the 15.5.3 paragraph:
2 A bit ordering is a method of interpreting the meaning of the storage place attributes. High_Order_First (known in the vernacular as "big endian") means that the first bit of a storage element (bit 0) is the most significant bit (interpreting the sequence of bits that represent a component as an unsigned integer value). Low_Order_First (known in the vernacular as "little endian") means the opposite: the first bit is the least significant.
Note that the numbering is with respect to the bits of a storage unit. In other words, the specification affects only the numbering of bits within a single storage unit.
We can make the effect clearer by giving an example.
Suppose that we have an external device which presents two bytes, the first byte presented, which is the first (low addressed byte) of the two byte record is called Master, and the second byte is called Slave.
The left most (most significant bit is called Control for each byte, and the remaining 7 bits are called V1, V2, ... V7, where V7 is the rightmost (least significant) bit.
On a big-endian machine, we can write the following representation clause
type Data is record Master_Control : Bit; Master_V1 : Bit; Master_V2 : Bit; Master_V3 : Bit; Master_V4 : Bit; Master_V5 : Bit; Master_V6 : Bit; Master_V7 : Bit; Slave_Control : Bit; Slave_V1 : Bit; Slave_V2 : Bit; Slave_V3 : Bit; Slave_V4 : Bit; Slave_V5 : Bit; Slave_V6 : Bit; Slave_V7 : Bit; end record; for Data use record Master_Control at 0 range 0 .. 0; Master_V1 at 0 range 1 .. 1; Master_V2 at 0 range 2 .. 2; Master_V3 at 0 range 3 .. 3; Master_V4 at 0 range 4 .. 4; Master_V5 at 0 range 5 .. 5; Master_V6 at 0 range 6 .. 6; Master_V7 at 0 range 7 .. 7; Slave_Control at 1 range 0 .. 0; Slave_V1 at 1 range 1 .. 1; Slave_V2 at 1 range 2 .. 2; Slave_V3 at 1 range 3 .. 3; Slave_V4 at 1 range 4 .. 4; Slave_V5 at 1 range 5 .. 5; Slave_V6 at 1 range 6 .. 6; Slave_V7 at 1 range 7 .. 7; end record; |
Now if we move this to a little endian machine, then the bit ordering within the byte is backwards, so we have to rewrite the record rep clause as:
for Data use record Master_Control at 0 range 7 .. 7; Master_V1 at 0 range 6 .. 6; Master_V2 at 0 range 5 .. 5; Master_V3 at 0 range 4 .. 4; Master_V4 at 0 range 3 .. 3; Master_V5 at 0 range 2 .. 2; Master_V6 at 0 range 1 .. 1; Master_V7 at 0 range 0 .. 0; Slave_Control at 1 range 7 .. 7; Slave_V1 at 1 range 6 .. 6; Slave_V2 at 1 range 5 .. 5; Slave_V3 at 1 range 4 .. 4; Slave_V4 at 1 range 3 .. 3; Slave_V5 at 1 range 2 .. 2; Slave_V6 at 1 range 1 .. 1; Slave_V7 at 1 range 0 .. 0; end record; |
It is a nuisance to have to rewrite the clause, especially if
the code has to be maintained on both machines. However,
this is a case that we can handle with the
Bit_Order
attribute if it is implemented.
Note that the implementation is not required on byte addressed
machines, but it is indeed implemented in GNAT.
This means that we can simply use the
first record clause, together with the declaration
for Data'Bit_Order use High_Order_First; |
and the effect is what is desired, namely the layout is exactly the same, independent of whether the code is compiled on a big-endian or little-endian machine.
The important point to understand is that byte ordering is not affected.
A Bit_Order
attribute definition never affects which byte a field
ends up in, only where it ends up in that byte.
To make this clear, let us rewrite the record rep clause of the previous
example as:
for Data'Bit_Order use High_Order_First; for Data use record Master_Control at 0 range 0 .. 0; Master_V1 at 0 range 1 .. 1; Master_V2 at 0 range 2 .. 2; Master_V3 at 0 range 3 .. 3; Master_V4 at 0 range 4 .. 4; Master_V5 at 0 range 5 .. 5; Master_V6 at 0 range 6 .. 6; Master_V7 at 0 range 7 .. 7; Slave_Control at 0 range 8 .. 8; Slave_V1 at 0 range 9 .. 9; Slave_V2 at 0 range 10 .. 10; Slave_V3 at 0 range 11 .. 11; Slave_V4 at 0 range 12 .. 12; Slave_V5 at 0 range 13 .. 13; Slave_V6 at 0 range 14 .. 14; Slave_V7 at 0 range 15 .. 15; end record; |
This is exactly equivalent to saying (a repeat of the first example):
for Data'Bit_Order use High_Order_First; for Data use record Master_Control at 0 range 0 .. 0; Master_V1 at 0 range 1 .. 1; Master_V2 at 0 range 2 .. 2; Master_V3 at 0 range 3 .. 3; Master_V4 at 0 range 4 .. 4; Master_V5 at 0 range 5 .. 5; Master_V6 at 0 range 6 .. 6; Master_V7 at 0 range 7 .. 7; Slave_Control at 1 range 0 .. 0; Slave_V1 at 1 range 1 .. 1; Slave_V2 at 1 range 2 .. 2; Slave_V3 at 1 range 3 .. 3; Slave_V4 at 1 range 4 .. 4; Slave_V5 at 1 range 5 .. 5; Slave_V6 at 1 range 6 .. 6; Slave_V7 at 1 range 7 .. 7; end record; |
Why are they equivalent? Well take a specific field, the Slave_V2
field. The storage place attributes are obtained by normalizing the
values given so that the First_Bit
value is less than 8. After
normalizing the values (0,10,10) we get (1,2,2) which is exactly what
we specified in the other case.
Now one might expect that the Bit_Order
attribute might affect
bit numbering within the entire record component (two bytes in this
case, thus affecting which byte fields end up in), but that is not
the way this feature is defined, it only affects numbering of bits,
not which byte they end up in.
Consequently it never makes sense to specify a starting bit number
greater than 7 (for a byte addressable field) if an attribute
definition for Bit_Order
has been given, and indeed it
may be actively confusing to specify such a value, so the compiler
generates a warning for such usage.
If you do need to control byte ordering then appropriate conditional values must be used. If in our example, the slave byte came first on some machines we might write:
Master_Byte_First constant Boolean := ...; Master_Byte : constant Natural := 1 - Boolean'Pos (Master_Byte_First); Slave_Byte : constant Natural := Boolean'Pos (Master_Byte_First); for Data'Bit_Order use High_Order_First; for Data use record Master_Control at Master_Byte range 0 .. 0; Master_V1 at Master_Byte range 1 .. 1; Master_V2 at Master_Byte range 2 .. 2; Master_V3 at Master_Byte range 3 .. 3; Master_V4 at Master_Byte range 4 .. 4; Master_V5 at Master_Byte range 5 .. 5; Master_V6 at Master_Byte range 6 .. 6; Master_V7 at Master_Byte range 7 .. 7; Slave_Control at Slave_Byte range 0 .. 0; Slave_V1 at Slave_Byte range 1 .. 1; Slave_V2 at Slave_Byte range 2 .. 2; Slave_V3 at Slave_Byte range 3 .. 3; Slave_V4 at Slave_Byte range 4 .. 4; Slave_V5 at Slave_Byte range 5 .. 5; Slave_V6 at Slave_Byte range 6 .. 6; Slave_V7 at Slave_Byte range 7 .. 7; end record; |
Now to switch between machines, all that is necessary is
to set the boolean constant Master_Byte_First
in
an appropriate manner.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Pragma Pack
applied to an array has no effect unless the component type
is packable. For a component type to be packable, it must be one of the
following cases:
For all these cases, if the component subtype size is in the range
1 through 63, then the effect of the pragma Pack
is exactly as though a
component size were specified giving the component subtype size.
For example if we have:
type r is range 0 .. 17; type ar is array (1 .. 8) of r; pragma Pack (ar); |
Then the component size of ar
will be set to 5 (i.e. to r'size
,
and the size of the array ar
will be exactly 40 bits.
Note that in some cases this rather fierce approach to packing can produce
unexpected effects. For example, in Ada 95, type Natural typically has a
size of 31, meaning that if you pack an array of Natural, you get 31-bit
close packing, which saves a few bits, but results in far less efficient
access. Since many other Ada compilers will ignore such a packing request,
GNAT will generate a warning on some uses of pragma Pack
that it guesses
might not be what is intended. You can easily remove this warning by
using an explicit Component_Size
setting instead, which never generates
a warning, since the intention of the programmer is clear in this case.
GNAT treats packed arrays in one of two ways. If the size of the array is known at compile time and is less than 64 bits, then internally the array is represented as a single modular type, of exactly the appropriate number of bits. If the length is greater than 63 bits, or is not known at compile time, then the packed array is represented as an array of bytes, and the length is always a multiple of 8 bits.
Note that to represent a packed array as a modular type, the alignment must be suitable for the modular type involved. For example, on typical machines a 32-bit packed array will be represented by a 32-bit modular integer with an alignment of four bytes. If you explicitly override the default alignment with an alignment clause that is too small, the modular representation cannot be used. For example, consider the following set of declarations:
type R is range 1 .. 3; type S is array (1 .. 31) of R; for S'Component_Size use 2; for S'Size use 62; for S'Alignment use 1; |
If the alignment clause were not present, then a 62-bit modular representation would be chosen (typically with an alignment of 4 or 8 bytes depending on the target). But the default alignment is overridden with the explicit alignment clause. This means that the modular representation cannot be used, and instead the array of bytes representation must be used, meaning that the length must be a multiple of 8. Thus the above set of declarations will result in a diagnostic rejecting the size clause and noting that the minimum size allowed is 64.
One special case that is worth noting occurs when the base type of the
component size is 8/16/32 and the subtype is one bit less. Notably this
occurs with subtype Natural
. Consider:
type Arr is array (1 .. 32) of Natural; pragma Pack (Arr); |
In all commonly used Ada 83 compilers, this pragma Pack would be ignored,
since typically Natural'Size
is 32 in Ada 83, and in any case most
Ada 83 compilers did not attempt 31 bit packing.
In Ada 95, Natural'Size
is required to be 31. Furthermore, GNAT really
does pack 31-bit subtype to 31 bits. This may result in a substantial
unintended performance penalty when porting legacy Ada 83 code. To help
prevent this, GNAT generates a warning in such cases. If you really want 31
bit packing in a case like this, you can set the component size explicitly:
type Arr is array (1 .. 32) of Natural; for Arr'Component_Size use 31; |
Here 31-bit packing is achieved as required, and no warning is generated, since in this case the programmer intention is clear.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Pragma Pack
applied to a record will pack the components to reduce
wasted space from alignment gaps and by reducing the amount of space
taken by components. We distinguish between packable components and
non-packable components.
Components of the following types are considered packable:
All packable components occupy the exact number of bits corresponding to
their Size
value, and are packed with no padding bits, i.e. they
can start on an arbitrary bit boundary.
All other types are non-packable, they occupy an integral number of storage units, and are placed at a boundary corresponding to their alignment requirements.
For example, consider the record
type Rb1 is array (1 .. 13) of Boolean; pragma Pack (rb1); type Rb2 is array (1 .. 65) of Boolean; pragma Pack (rb2); type x2 is record l1 : Boolean; l2 : Duration; l3 : Float; l4 : Boolean; l5 : Rb1; l6 : Rb2; end record; pragma Pack (x2); |
The representation for the record x2 is as follows:
for x2'Size use 224; for x2 use record l1 at 0 range 0 .. 0; l2 at 0 range 1 .. 64; l3 at 12 range 0 .. 31; l4 at 16 range 0 .. 0; l5 at 16 range 1 .. 13; l6 at 18 range 0 .. 71; end record; |
Studying this example, we see that the packable fields l1
and l2
are
of length equal to their sizes, and placed at specific bit boundaries (and
not byte boundaries) to
eliminate padding. But l3
is of a non-packable float type, so
it is on the next appropriate alignment boundary.
The next two fields are fully packable, so l4
and l5
are
minimally packed with no gaps. However, type Rb2
is a packed
array that is longer than 64 bits, so it is itself non-packable. Thus
the l6
field is aligned to the next byte boundary, and takes an
integral number of bytes, i.e. 72 bits.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Record representation clauses may be given for all record types, including types obtained by record extension. Component clauses are allowed for any static component. The restrictions on component clauses depend on the type of the component.
For all components of an elementary type, the only restriction on component clauses is that the size must be at least the 'Size value of the type (actually the Value_Size). There are no restrictions due to alignment, and such components may freely cross storage boundaries.
Packed arrays with a size up to and including 64 bits are represented internally using a modular type with the appropriate number of bits, and thus the same lack of restriction applies. For example, if you declare:
type R is array (1 .. 49) of Boolean; pragma Pack (R); for R'Size use 49; |
then a component clause for a component of type R may start on any specified bit boundary, and may specify a value of 49 bits or greater.
For packed bit arrays that are longer than 64 bits, there are two cases. If the component size is a power of 2 (1,2,4,8,16,32 bits), including the important case of single bits or boolean values, then there are no limitations on placement of such components, and they may start and end at arbitrary bit boundaries.
If the component size is not a power of 2 (e.g. 3 or 5), then an array of this type longer than 64 bits must always be placed on on a storage unit (byte) boundary and occupy an integral number of storage units (bytes). Any component clause that does not meet this requirement will be rejected.
Any aliased component, or component of an aliased type, must have its normal alignment and size. A component clause that does not meet this requirement will be rejected.
The tag field of a tagged type always occupies an address sized field at the start of the record. No component clause may attempt to overlay this tag. When a tagged type appears as a component, the tag field must have proper alignment
In the case of a record extension T1, of a type T, no component clause applied to the type T1 can specify a storage location that would overlap the first T'Size bytes of the record.
For all other component types, including non-bit-packed arrays, the component can be placed at an arbitrary bit boundary, so for example, the following is permitted:
type R is array (1 .. 10) of Boolean; for R'Size use 80; type Q is record G, H : Boolean; L, M : R; end record; for Q use record G at 0 range 0 .. 0; H at 0 range 1 .. 1; L at 0 range 2 .. 81; R at 0 range 82 .. 161; end record; |
Note: the above rules apply to recent releases of GNAT 5. In GNAT 3, there are more severe restrictions on larger components. For non-primitive types, including packed arrays with a size greater than 64 bits, component clauses must respect the alignment requirement of the type, in particular, always starting on a byte boundary, and the length must be a multiple of the storage unit.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The only restriction on enumeration clauses is that the range of values must be representable. For the signed case, if one or more of the representation values are negative, all values must be in the range:
System.Min_Int .. System.Max_Int |
For the unsigned case, where all values are non negative, the values must be in the range:
0 .. System.Max_Binary_Modulus; |
A confirming representation clause is one in which the values range from 0 in sequence, i.e. a clause that confirms the default representation for an enumeration type. Such a confirming representation is permitted by these rules, and is specially recognized by the compiler so that no extra overhead results from the use of such a clause.
If an array has an index type which is an enumeration type to which an enumeration clause has been applied, then the array is stored in a compact manner. Consider the declarations:
type r is (A, B, C); for r use (A => 1, B => 5, C => 10); type t is array (r) of Character; |
The array type t corresponds to a vector with exactly three elements and
has a default size equal to 3*Character'Size
. This ensures efficient
use of space, but means that accesses to elements of the array will incur
the overhead of converting representation values to the corresponding
positional values, (i.e. the value delivered by the Pos
attribute).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The reference manual allows a general restriction on representation clauses, as found in RM 13.1(22):
An implementation need not support representation items containing nonstatic expressions, except that an implementation should support a representation item for a given entity if each nonstatic expression in the representation item is a name that statically denotes a constant declared before the entity.
In practice this is applicable only to address clauses, since this is the only case in which a non-static expression is permitted by the syntax. As the AARM notes in sections 13.1 (22.a-22.h):
22.a Reason: This is to avoid the following sort of thing: 22.b X : Integer := F(...); Y : Address := G(...); for X'Address use Y; 22.c In the above, we have to evaluate the initialization expression for X before we know where to put the result. This seems like an unreasonable implementation burden. 22.d The above code should instead be written like this: 22.e Y : constant Address := G(...); X : Integer := F(...); for X'Address use Y; 22.f This allows the expression ``Y'' to be safely evaluated before X is created. 22.g The constant could be a formal parameter of mode in. 22.h An implementation can support other nonstatic expressions if it wants to. Expressions of type Address are hardly ever static, but their value might be known at compile time anyway in many cases. |
GNAT does indeed permit many additional cases of non-static expressions. In particular, if the type involved is elementary there are no restrictions (since in this case, holding a temporary copy of the initialization value, if one is present, is inexpensive). In addition, if there is no implicit or explicit initialization, then there are no restrictions. GNAT will reject only the case where all three of these conditions hold:
Anchor : Some_Initialized_Type; Overlay : Some_Initialized_Type; for Overlay'Address use Anchor'Address; |
However, the prefix of the address clause cannot be an array component, or a component of a discriminated record.
As noted above in section 22.h, address values are typically non-static. In particular the To_Address function, even if applied to a literal value, is a non-static function call. To avoid this minor annoyance, GNAT provides the implementation defined attribute 'To_Address. The following two expressions have identical values:
To_Address (16#1234_0000#) System'To_Address (16#1234_0000#); |
except that the second form is considered to be a static expression, and thus when used as an address clause value is always permitted.
Additionally, GNAT treats as static an address clause that is an
unchecked_conversion of a static integer value. This simplifies the porting
of legacy code, and provides a portable equivalent to the GNAT attribute
To_Address
.
Another issue with address clauses is the interaction with alignment requirements. When an address clause is given for an object, the address value must be consistent with the alignment of the object (which is usually the same as the alignment of the type of the object). If an address clause is given that specifies an inappropriately aligned address value, then the program execution is erroneous.
Since this source of erroneous behavior can have unfortunate effects, GNAT
checks (at compile time if possible, generating a warning, or at execution
time with a run-time check) that the alignment is appropriate. If the
run-time check fails, then Program_Error
is raised. This run-time
check is suppressed if range checks are suppressed, or if
pragma Restrictions (No_Elaboration_Code)
is in effect.
An address clause cannot be given for an exported object. More understandably the real restriction is that objects with an address clause cannot be exported. This is because such variables are not defined by the Ada program, so there is no external object to export.
It is permissible to give an address clause and a pragma Import for the same object. In this case, the variable is not really defined by the Ada program, so there is no external symbol to be linked. The link name and the external name are ignored in this case. The reason that we allow this combination is that it provides a useful idiom to avoid unwanted initializations on objects with address clauses.
When an address clause is given for an object that has implicit or explicit initialization, then by default initialization takes place. This means that the effect of the object declaration is to overwrite the memory at the specified address. This is almost always not what the programmer wants, so GNAT will output a warning:
with System; package G is type R is record M : Integer := 0; end record; Ext : R; for Ext'Address use System'To_Address (16#1234_1234#); | >>> warning: implicit initialization of "Ext" may modify overlaid storage >>> warning: use pragma Import for "Ext" to suppress initialization (RM B(24)) end G; |
As indicated by the warning message, the solution is to use a (dummy) pragma Import to suppress this initialization. The pragma tell the compiler that the object is declared and initialized elsewhere. The following package compiles without warnings (and the initialization is suppressed):
with System; package G is type R is record M : Integer := 0; end record; Ext : R; for Ext'Address use System'To_Address (16#1234_1234#); pragma Import (Ada, Ext); end G; |
A final issue with address clauses involves their use for overlaying variables, as in the following example:
A : Integer; B : Integer; for B'Address use A'Address; |
or alternatively, using the form recommended by the RM:
A : Integer; Addr : constant Address := A'Address; B : Integer; for B'Address use Addr; |
In both of these cases, A
and B
become aliased to one another via the
address clause. This use of address clauses to overlay
variables, achieving an effect similar to unchecked
conversion was erroneous in Ada 83, but in Ada 95
the effect is implementation defined. Furthermore, the
Ada 95 RM specifically recommends that in a situation
like this, B
should be subject to the following
implementation advice (RM 13.3(19)):
19 If the Address of an object is specified, or it is imported or exported, then the implementation should not perform optimizations based on assumptions of no aliases.
GNAT follows this recommendation, and goes further by also applying
this recommendation to the overlaid variable (A
in the above example) in this case. This means that the overlay
works "as expected", in that a modification to one of the variables
will affect the value of the other.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Normally the specification of a foreign language convention for a type or an object has no effect on the chosen representation. In particular, the representation chosen for data in GNAT generally meets the standard system conventions, and for example records are laid out in a manner that is consistent with C. This means that specifying convention C (for example) has no effect.
There are three exceptions to this general rule:
type Color is (Red, Green, Blue); |
8 bits is sufficient to store all values of the type, so by default, objects
of type Color
will be represented using 8 bits. However, normal C
convention is to use 32 bits for all enum values in C, since enum values
are essentially of type int. If pragma Convention C
is specified for an
Ada enumeration type, then the size is modified as necessary (usually to
32 bits) to be consistent with the C convention for enum values.
Fortran has a similar convention for LOGICAL
values (any nonzero
value represents true).
To accommodate the Fortran and C conventions, if a pragma Convention specifies C or Fortran convention for a derived Boolean, as in the following example:
type C_Switch is new Boolean; pragma Convention (C, C_Switch); |
then the GNAT generated code will treat any nonzero value as true. For truth values generated by GNAT, the conventional value 1 will be used for True, but when one of these values is read, any nonzero value is treated as True.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Although the descriptions in this section are intended to be complete, it is often easier to simply experiment to see what GNAT accepts and what the effect is on the layout of types and objects.
As required by the Ada RM, if a representation clause is not accepted, then
it must be rejected as illegal by the compiler. However, when a
representation clause or pragma is accepted, there can still be questions
of what the compiler actually does. For example, if a partial record
representation clause specifies the location of some components and not
others, then where are the non-specified components placed? Or if pragma
Pack
is used on a record, then exactly where are the resulting
fields placed? The section on pragma Pack
in this chapter can be
used to answer the second question, but it is often easier to just see
what the compiler does.
For this purpose, GNAT provides the option -gnatR
. If you compile
with this option, then the compiler will output information on the actual
representations chosen, in a format similar to source representation
clauses. For example, if we compile the package:
package q is type r (x : boolean) is tagged record case x is when True => S : String (1 .. 100); when False => null; end case; end record; type r2 is new r (false) with record y2 : integer; end record; for r2 use record y2 at 16 range 0 .. 31; end record; type x is record y : character; end record; type x1 is array (1 .. 10) of x; for x1'component_size use 11; type ia is access integer; type Rb1 is array (1 .. 13) of Boolean; pragma Pack (rb1); type Rb2 is array (1 .. 65) of Boolean; pragma Pack (rb2); type x2 is record l1 : Boolean; l2 : Duration; l3 : Float; l4 : Boolean; l5 : Rb1; l6 : Rb2; end record; pragma Pack (x2); end q; |
using the switch -gnatR
we obtain the following output:
Representation information for unit q ------------------------------------- for r'Size use ??; for r'Alignment use 4; for r use record x at 4 range 0 .. 7; _tag at 0 range 0 .. 31; s at 5 range 0 .. 799; end record; for r2'Size use 160; for r2'Alignment use 4; for r2 use record x at 4 range 0 .. 7; _tag at 0 range 0 .. 31; _parent at 0 range 0 .. 63; y2 at 16 range 0 .. 31; end record; for x'Size use 8; for x'Alignment use 1; for x use record y at 0 range 0 .. 7; end record; for x1'Size use 112; for x1'Alignment use 1; for x1'Component_Size use 11; for rb1'Size use 13; for rb1'Alignment use 2; for rb1'Component_Size use 1; for rb2'Size use 72; for rb2'Alignment use 1; for rb2'Component_Size use 1; for x2'Size use 224; for x2'Alignment use 4; for x2 use record l1 at 0 range 0 .. 0; l2 at 0 range 1 .. 64; l3 at 12 range 0 .. 31; l4 at 16 range 0 .. 0; l5 at 16 range 1 .. 13; l6 at 18 range 0 .. 71; end record; |
The Size values are actually the Object_Size, i.e. the default size that will be allocated for objects of the type. The ?? size for type r indicates that we have a variant record, and the actual size of objects will depend on the discriminant value.
The Alignment values show the actual alignment chosen by the compiler for each record or array type.
The record representation clause for type r shows where all fields are placed, including the compiler generated tag field (whose location cannot be controlled by the programmer).
The record representation clause for the type extension r2 shows all the fields present, including the parent field, which is a copy of the fields of the parent type of r2, i.e. r1.
The component size and size clauses for types rb1 and rb2 show
the exact effect of pragma Pack
on these arrays, and the record
representation clause for type x2 shows how pragma Pack
affects
this record type.
In some cases, it may be useful to cut and paste the representation clauses generated by the compiler into the original source to fix and guarantee the actual representation to be used.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |