Lady Ada

Ada '83 Language Reference Manual

Copyright 1980, 1982, 1983 owned by the United States Government. Direct reproduction and usage requests to the Ada Information Clearinghouse.


13.4. Record Representation Clauses

[PREVIOUS][UP][NEXT]

A record representation clause specifies the storage representation of records, that is, the order, position, and size of record components (including discriminants, if any).

      record_representation_clause ::=
        for type_simple_name use
          record [alignment_clause]
            {component clause}
          end record;

      alignment_clause ::= at mod static_simple_expression;

      component_clause ::=
        component_name at static_simple_expression range static_range;

The simple expression given after the reserved words at mod in an alignment clause, or after the reserved word at in a component clause, must be a static expression of some integer type. If the bounds of the range of a component clause are defined by simple expressions, then each bound of the range must be defined by a static expression of some integer type, but the two bounds need not have the same integer type.

An alignment clause forces each record of the given type to be allocated at a starting address that is a multiple of the value of the given expression (that is, the address modulo the expression must be zero). An implementation may place restrictions on the allowable alignments.

A component clause specifies the storage place of a component, relative to the start of the record. The integer defined by the static expression of a component clause is a relative address expressed in storage units. The range defines the bit positions of the storage place, relative to the storage unit. The first storage unit of a record is numbered zero.The first bit of a storage unit is numbered zero. The ordering of bits in a storage unit is machine_dependent and may extend to adjacent storage units (For a specific machine, the size in bits of a storage unit is given by the configuration-dependent named number SYSTEM.STORAGE_UNIT.) Whether a component is allowed to overlap a storage boundary, and if so, how, is implementation-defined.

At most one component clause is allowed for each component of the record type, including for each discriminant (component clauses may be given for some, all, or none of the components). If no component clause is given for a component, then the choice of the storage place for the component is left to the compiler. If component clauses are given for all components, the record representation clause completely specifies the representation of the record type and must be obeyed exactly by the compiler.

Storage places within a record variant must not overlap, but overlap of the storage for distinct variants is allowed. Each component clause must allow for enough storage space to accommodate every allowable value of the component. A component clause is only allowed for a component if any constraint on this component or on any of its subcomponents is static.

An implementation may generate names that denote implementation-dependent components (for example, one containing the offset of another component). Such implementation-dependent names can be used in record representation clauses (these names need not be simple names; for example, they could be implementation-dependent attributes).

Example:

   WORD : constant := 4;  --  storage unit is byte, 4 bytes per word

   type STATE         is (A,M,W,P);
   type MODE          is (FIX, DEC, EXP, SIGNIF);

   type BYTE_MASK     is array (0.. 7) of BOOLEAN;
   type STATE_MASK    is array (STATE) of BOOLEAN;
   type MODE_MASK     is array (MODE)  of BOOLEAN;

   type PROGRAM_STATUS_WORD is

     record
       SYSTEM_MASK        : BYTE_MASK;
       PROTECTION_KEY     : INTEGER range 0 .. 3;
       MACHINE_STATE      : STATE_MASK;
       INTERRUPT_CAUSE    : INTERRUPTION_CODE;
       ILC                : INTEGER range 0 .. 3;
       CC                 : INTEGER range 0 .. 3;
       PROGRAM_MASK       : MODE_MASK;
      INST_ADDRESS       : ADDRESS;
    end record;

  for PROGRAM_STATUS_WORD use
    record at mod 8;
        SYSTEM_MASK      at 0*WORD range 0  .. 7;  
        PROTECTION_KEY   at 0*WORD range 10 .. 11; -- bits 8,9 unused
        MACHINE_STATE    at 0*WORD range 12 .. 15;
        INTERRUPT_CAUSE  at 0*WORD range 16 .. 31;
        ILC              at 1*WORD range 0  .. 1;  -- second word
        CC               at 1*WORD range 2  .. 3;
        PROGRAM_MASK     at 1*WORD range 4  .. 7;
        INST_ADDRESS     at 1*WORD range 8  .. 31;
    end record;

  for PROGRAM_STATUS_WORD'SIZE use 8*SYSTEM.STORAGE_UNIT;

Note on the example:

The record representation clause defines the record layout. The length clause guarantees that exactly eight storage units are used.

References: allow, attribute, constant, constraint, discriminant, integer type, must, named number, range, record component, record type, simple expression, simple name, static constraint, static expression, storage unit, subcomponent, system package, variant.

Rationale references: 15.4.3 Record Representation Clauses

Style Guide references: 2.1.2 Indentation, 7.6.1 Representation Clauses

[INDEX][CONTENTS]


[Ada Information Clearinghouse]

Address any questions or comments to adainfo@sw-eng.falls-church.va.us.