Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 36 Β· Systems and Tools

Representation Clauses and Bit Layout

Control data layout in Ada: Size and Alignment, enumeration representation, record representation clauses, bit-order and modular types for hardware registers, with C stand-ins.

Ada is a high-level language that can still describe data down to the individual bit. Hardware registers, network packets and file formats all have a fixed layout that your program must match exactly. Ada's representation clauses let you state that layout in the language, instead of juggling shifts and masks everywhere, and the compiler checks that it fits.

The Ada code here is read-only on this site (no compiler is available). Runnable C stand-ins use bit fields and masks to show the same layouts.

Size and alignment

You can ask for the number of bits a type needs, and the compiler rejects impossible requests:

ADA
type Level is range 0 .. 15;
for Level'Size use 4;              --  4 bits

type Byte is mod 2 ** 8;
for Byte'Size use 8;

'Size is in bits; 'Alignment is in storage units. pragma Pack (T); (or with Pack) asks the compiler to squeeze components of an array or record as tightly as possible. Use these when memory or an external format matters; otherwise let the compiler choose, because an aligned layout is faster.

Enumeration representation

By default an enumeration literal's internal code is its position (0, 1, 2...). You can assign your own, for example to match a protocol or a hardware status register:

ADA
type Command is (Idle, Start, Stop, Reset);
for Command use (Idle => 0, Start => 16#10#, Stop => 16#20#, Reset => 16#FF#);

Command'Pos (Stop) is still 2 (its position); the representation 16#20# appears only when the value is stored or sent, which you can inspect with Unchecked_Conversion. The language keeps logical order and physical code separate.

Record representation clauses

A record can be laid out down to the bit:

ADA
type Control_Register is record
   Enable : Boolean;
   Mode   : Natural range 0 .. 7;
   Speed  : Natural range 0 .. 15;
   Ready  : Boolean;
end record;

for Control_Register use record
   Enable at 0 range 0 .. 0;
   Mode   at 0 range 1 .. 3;
   Speed  at 0 range 4 .. 7;
   Ready  at 1 range 0 .. 0;
end record;
for Control_Register'Size use 16;

Each component is placed at a storage-unit offset with a bit range inside it. The compiler checks that all components fit, that none overlap, and that the sizes agree with the types, so a typo in a bit position is a compile-time error rather than a late-night debugging session. Access to the register then reads naturally: Reg.Mode := 5; instead of reg = (reg & ~0x0E) | (5 << 1).

Layout clauses

'Size bits per object
'Alignment address multiples
Enum rep chosen codes
Record rep bit-exact fields

Checked at compile time.

1 / 2

Endianness and bit order

Some processors store the least significant byte first (little endian), others the most significant. Ada exposes this through System.Default_Bit_Order and the Bit_Order and Scalar_Storage_Order attributes. If your record layout is declared for a big-endian network format, for Packet'Scalar_Storage_Order use System.High_Order_First; tells the compiler to byte-swap on little-endian machines, so you do not write conversion code by hand. When writing portable code, state the order explicitly rather than assuming the host's.

Modular types for masks

Modular types (type Word is mod 2 ** 16;) have wrap-around arithmetic and the operators and, or, xor, not, plus the shifts available from Interfaces:

ADA
with Interfaces; use Interfaces;

Reg : Unsigned_8 := 2#0000_0100#;
Reg := Reg or  Shift_Left (1, 3);        --  set bit 3
Reg := Reg and not Shift_Left (1, 2);    --  clear bit 2
Test : constant Boolean := (Reg and 8) /= 0;   --  test bit 3

Use modular types when you want masks and wrap-around; use ordinary ranged types when you want overflow to be an error.

Stand-in: the register layout with masks (C, runnable)

This program models Control_Register as a 16-bit word with the same layout: Enable at bit 0, Mode in bits 1-3, Speed in bits 4-7, Ready at bit 8 (byte 1, bit 0). Setters and getters hide the shifts, the same job the Ada record does automatically.

C C stand-in: the record as shifts and masks

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The raw value is 0x019B: bit 0 (Enable) is 1, Mode 5 gives bits 1-3 = 101, Speed 9 gives bits 4-7 = 1001, and bit 8 is Ready. This is the arithmetic the Ada compiler does for you.

Stand-in: endianness (C, runnable)

Printing a 32-bit value byte by byte shows the host's byte order, which is what Default_Bit_Order and Scalar_Storage_Order are about.

C C stand-in: look at byte order

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

On the usual x86 and ARM machines the host is little endian: 04 03 02 01 first, then network order 01 02 03 04.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. What unit does the 'Size attribute use?

    'Size is measured in bits; 'Alignment is measured in storage units.
  2. 2. What does a record representation clause specify?

    Each component is placed at an offset with a bit range and the compiler checks for overlap and fit.
  3. 3. When is a modular type a good choice?

    Modular types wrap and support and/or/xor/not; ranged types raise Constraint_Error on overflow.
  4. 4. How do you make an Ada record use big-endian byte order regardless of the host?

    The compiler then converts on little-endian hosts.

Summary

  • 'Size, 'Alignment and Pack control storage; use them only when a layout matters.
  • Enumeration representation clauses assign specific codes without changing logical order.
  • Record representation clauses place components bit-exactly and are checked by the compiler.
  • Endianness is handled with bit-order and scalar storage order attributes.
  • Modular types give masks and wrap-around arithmetic.

Next: Interfacing with C