Enumerations and Subranges [ HP Pascal/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP Pascal/iX Programmer's Guide
Enumerations and Subranges
HP Pascal allocates and aligns variables of enumeration and subrange
types the same way. An enumeration of n elements and the subrange 0..n-1
are equivalent. The allocation and alignment are based on the values of
the subrange or the ordinal value of the enumeration.
Example
TYPE
enum_type = (red,blue,yellow); {enumeration of 3 elements}
subr_type = 0..2; {subrange 0..(3-1)}
VAR
enum_var : enum_type;
subr_var : subr_type;
The compiler allocates and aligns the variables enum_var and subr_var the
same way.
The allocation and alignment of an enumeration or subrange variable
depends on whether it is:
* Unpacked.
* An element of a packed array.
* A field of a packed record.
* In a crunched structure.
Unpacked Enumeration or Unsigned Subranges
Table 5-7 shows how the HP Pascal packing algorithm allocates and
aligns unpacked enumeration or unsigned subrange variables.
Table 5-7. Allocation and Alignment of Unpacked Enumeration or Unsigned Subrange Variables
(HP Pascal Packing Algorithm)
----------------------------------------------------------------------------------------------
| | | |
| Values in Enumeration or Subrange | | |
| | Allocation | Alignment |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| 0..255 | 1 byte | byte |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| 256..65535 | 2 bytes | 2-byte |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| 65536..maxint | 4 bytes | 4-byte |
| | | |
----------------------------------------------------------------------------------------------
An unpacked, signed subrange is always allocated four bytes.
Example
The value zero is always included in the subrange when the minimum number
of bits is calculated.
TYPE
enum_type = (red,blue,yellow); {3 elements}
subr_type1 = 1..300; {Including zero, 2 bytes}
subr_type2 = 1..66000; {Including zero, 4 bytes}
subr_type3 = 100000..100010; {Including zero, 4 bytes}
subr_type4 = -1..200; {4 bytes}
VAR
enum_var : enum_type; {Allocated 1 byte, byte-aligned}
subr_var1 : subr_type1; {Allocated 2 bytes, 2-byte-aligned}
subr_var2 : subr_type2; {Allocated 4 bytes, 4-byte-aligned}
subr_var4 : subr_type4; {Allocated 4 bytes, 4-byte-aligned}
unpacked_array : ARRAY [1..3] OF enum_type; {Each element is
allocated one byte
and is byte-aligned}
unpacked_record : RECORD
f1 : subr_type1; {Allocated 2 bytes,
2-byte-aligned}
f2 : subr_type2; {Allocated 4 bytes,
4-byte-aligned}
END;
Packed Array Elements of Enumeration or Subrange Types
A packed enumeration or subrange variable requires the minimum number of
bits needed to represent its values in a record. It is bit-aligned.
If the enumeration or subrange variable belongs to a packed array, the HP
Pascal packing algorithm allocates it the smallest power of two bits that
is greater than or equal to the number of bits it requires, and aligns it
on that boundary.
Table 5-8 shows the relationship between the number of bits that a
packed array element of an enumeration- or subrange-type array requires,
the number of bits that the HP Pascal packing algorithm allocates to it,
and its alignment.
Table 5-8. Allocation and Alignment of Packed Array Elements of Enumeration or Subrange Type
(HP Pascal Packing Algorithm)
-------------------------------------------------------------------------------------------
| | | |
| Required Number of | Number of Bits | Alignment |
| Bits Per Element | Allocated Per Element | |
| | | |
-------------------------------------------------------------------------------------------
| | | |
| 1 | 1 | Bit |
| | | |
-------------------------------------------------------------------------------------------
| | | |
| 2 | 2 | 2-bit |
| | | |
-------------------------------------------------------------------------------------------
| | | |
| 3 or 4 | 4 | 4-bit |
| | | |
-------------------------------------------------------------------------------------------
| | | |
| 5 to 8 | 8 (1 byte) | Byte |
| | | |
-------------------------------------------------------------------------------------------
| | | |
| 9 to 16 | 16 (2 bytes) | 2-byte |
| | | |
-------------------------------------------------------------------------------------------
| | | |
| 17 to 32 | 32 (4 bytes) | 4-byte |
| | | |
-------------------------------------------------------------------------------------------
Example
TYPE
direction = (north,south,east,west);
day = (sun,mon,tues,wed,thurs,fri,sat);
VAR
pa1 = PACKED ARRAY [1..5] OF direction;
pa2 = PACKED ARRAY [1..5] OF day;
Each element of the array pa1 requires two bits. Two is a power of two,
so each element is allocated two bits. The entire array occupies 10
bits. It is allocated two bytes:
Each element of the array pa2 requires three bits. The smallest power of
two that is greater than or equal to three is four, so each element is
allocated four bits. The entire array occupies 20 bits. It is allocated
three bytes:
Packed Record Elements of Enumeration or Subrange Types
If the variable belongs to a packed record, the HP Pascal packing
algorithm allocates it as many bits as it requires, and bit-aligns it.
Example
TYPE
day = (sun,mon,tues,wed,thurs,fri,sat);
VAR
r : PACKED RECORD
f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11 : day;
END;
Each field of the record r requires three bits. The entire record
occupies 33 bits. It is allocated five bytes:
NOTE Subranges can cross 4-byte boundaries, but code is less efficient
when they do.
Packed records (such as those above) are byte-aligned. Code is
more efficient when their alignment is specified with the ALIGNMENT
compiler option.
MPE/iX 5.0 Documentation