MPE/iX Extensions [ HP Pascal/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP Pascal/iX Programmer's Guide
MPE/iX Extensions
MPE/iX extensions are available only to programs that are run on the
MPE/iX operating system or contain the compiler option OS 'MPE/XL'. They
are:
* Predefined function ccode
* Predefined function fnum
* Predefined function get_alignment
* Predefined function statement_number
* Predefined procedure setconvert
* Predefined procedure strconvert
* Pascal/V packing algorithm
ccode Function
The predefined function ccode returns an integer in the range 0..2, which
represents the condition code set by the most recently executed intrinsic
or external SPL routine.
The correspondence between possible return values and condition codes is:
Value Condition Code
0 CCG
1 CCL
2 CCE
For the meanings of the condition codes, refer to the MPE/iX Intrinsics
Reference Manual.
The value that ccode returns is valid between the time that the intrinsic
or external SPL routine returns and any subsequent calls that can change
the value of ccode, which are:
* Another intrinsic or external SPL routine.
* Any predefined routine.
* An HP Pascal error condition.
NOTE The scope rules for ccode are different in MPE/iX and MPE V.
Example
PROGRAM prog (output);
PROCEDURE intrin; INTRINSIC;
PROCEDURE extspl; EXTERNAL SPL;
PROCEDURE p;
BEGIN
writeln(ccode); {Garbage -- no intrinsic or external SPL
intrin;
writeln(ccode); {Returns condition code that intrin set}
extspl;
writeln(ccode); {Returns condition code that extspl set}
END;
BEGIN
p;
END.
Fnum Function
The predefined function fnum returns the MPE/iX file number of the
physical file currently associated with a given logical file. You can
use this file number in calls to MPE/iX file system intrinsics.
Syntax
fnum (filename)
Parameter
filename The name of the logical file. This parameter is required,
even if the logical file is the standard file input or
output. The logical file must be associated with a
physical file.
Example
PROGRAM aaa (output,f);
VAR
f : text;
file_number : integer;
file_name : PACKED ARRAY [1..86] OF char;
PROCEDURE fgetinfo; INTRINSIC;
BEGIN
reset(f);
file_number := fnum(f);
file_name := ' ';
fgetinfo(file_number,file_name);
writeln('File name of f is', file_name);
END.
Get_alignment Function
The predefined function get_alignment returns the alignment requirement
of a given type or variable. For a type, get_alignment returns the
minimum possible alignment. For a variable, it returns the actual
alignment.
Syntax
get_alignment ({variable})
{type }
Parameters
variable Any variable. The function get_alignment returns its
alignment requirement.
type Any type identifier (the name of any type). The function
get_alignment returns its alignment requirement.
Example
$OS 'MPE XL'$
PROGRAM prog;
TYPE
Rec = $ALIGNMENT 8$
RECORD
f1 : integer;
f2 : shortint;
f3 : real;
END;
integer_ = $ALIGNMENT 2$ integer;
VAR
ptr : ^integer_;
BEGIN
i := get_alignment(rec);
IF get_alignment(ptr^) <> 2 THEN ...
END.
Statement_number Function
The predefined function statement_number returns the statement number of
the statement that calls it, as shown on the compiled listing. It is a
useful debugging aid, especially when used with the predefined procedure
assert.
Syntax
statement_number
Example
PROGRAM prog (output);
VAR
i : integer;
BEGIN
i := statement_number;
writeln('Current Statement Number is ', i);
assert(a > b, statement_number);
END.
Setconvert Procedure
The predefined procedure setconvert converts a set from HP Pascal packing
algorithm (HP3000_32) format to Pascal/V packing algorithm (HP3000_16)
format, or vice versa. It is enabled by the HP3000_16 compiler option.
Syntax
setconvert(set1,set2)
Parameters
set1 The name of the set variable to be converted.
set2 The name of the set variable into which the converted set
is to be stored.
The sets set1 and set2 can vary only in packing algorithm format. Their
packing (unpacked, packed, or crunched) and base types must be the same.
Their packing algorithm formats cannot be the same.
Example
PROGRAM prog;
$HP3000_16$ {Enables setconvert procedure}
TYPE
hp3000_16_set1 = SET OF char;
hp3000_32_set1 = $HP3000_32$ SET OF char;
hp3000_32_set2 = $HP3000_32$ PACKED SET OF char;
hp3000_32_set3 = $HP3000_32$ SET OF integer;
VAR
set16_1,
set16_2 : hp3000_16_set1;
set32_1 : hp3000_32_set1;
set32_2 : hp3000_32_set2;
set32_3 : hp3000_32_set3;
BEGIN
setconvert(set16_1,set32_1); {convert from Pascal/V to HP Pascal}
setconvert(set32_1,set16_1); {convert from HP Pascal to Pascal/V}
setconvert(set16_1,set32_2); {Illegal -- different packings}
setconvert(set16_1,set32_3); {Illegal -- different base types}
setconvert(set16_1,set16_2); {Illegal -- same packing algorithm format}
END.
Strconvert Procedure
The predefined procedure strconvert converts a string from Pascal/V
packing algorithm (HP3000_16) format to HP Pascal packing algorithm
(HP3000_32) format. It is enabled by the HP3000_16 compiler option.
Syntax
strconvert(string1,string2)
Parameters
string1 The name of the string variable to be converted. The
string variable must be in Pascal/V packing algorithm
(HP3000_16) format.
string2 The name of the string variable into which the converted
string is to be stored. The string variable must be in HP
Pascal packing algorithm (HP3000_32) format.
Example
PROGRAM prog;
$HP3000_16$ {Enables strconvert procedure}
TYPE
str16_20=string[20]; {Pascal/V packing algorithm (HP3000_16)}
str32_40=$HP3000_32$ string[40]; {HP Pascal packing algorithm (HP3000_32)}
VAR
sv32_1,
sv32_2 : str32_40;
sv16_1,
sv16_2 : str16_20;
BEGIN
strconvert(sv16_1,sv32_1);
strconvert(sv32_2,sv32_1); {Illegal}
strconvert(sv16_1,sv16_2); {Illegal}
END.
Pascal/V Packing Algorithm
The Pascal/V packing algorithm is an alternative to the default HP Pascal
packing algorithm that Chapter 5 explains. If you want the compiler to
use the Pascal/V packing algorithm, include the compiler option HP3000_16
in your program (see the HP Pascal/iX Reference Manual for more
information on the compiler option HP3000_16). HP3000_16 causes the
compiler to use the Pascal/V packing algorithm, with these exceptions:
* Pointers are allocated four bytes each and are 4-byte-aligned.
* Files are aligned according to the HP Pascal packing algorithm.
File control blocks are determined by the HP Pascal packing
algorithm. Buffer size is determined by the Pascal/V packing
algorithm.
* Variables of types that specify the HP3000_32 compiler option are
allocated and aligned according to the HP Pascal packing
algorithm.
Unpacked Variables.
An unpacked variable is either not part of an array or record, or it is
part of an unpacked array or record. In either case, it is allocated and
aligned the same way.
Table A-2 shows how the Pascal/V packing algorithm allocates and
aligns the elements of an unpacked array or the fields of an unpacked
record. The element or field types are in alphabetical order.
Subsections that Table A-2 references are in this section, "Pascal/V
Packing Algorithm" .
Table A-2. Allocation and Alignment of Unpacked Variables
(Pascal/V Packing Algorithm)
-----------------------------------------------------------------------------------------------
| | | |
| Variable Type | Allocation | Alignment |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Array | Use formula in "Arrays" | Byte or 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Bit16 | 2 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Bit32 | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Bit52 | 8 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Boolean | 1 byte | Byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Char | 1 byte | |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Enumeration | 1-256 elements |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Enumeration | 1 byte | Byte |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Enumeration | 257 or more elements |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Enumeration | 2 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| File | See "Files" | 8-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Longint | 8 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Longreal | 8 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Pointer | HP3000_16 does not affect pointers. See Table 5-1 . |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Real | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Record | Each field is allocated by | 2-byte |
| | type and record is padded to | |
| | nearest 2-byte boundary | |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Set | See "Sets" |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| String | See "Strings" | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Subrange of enumeration | Same as base type | Byte or 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Subrange of integer | Inside range -32768..32767 |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Subrange of integer | 2 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Subrange of integer | Outside range -32768..32767 |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Subrange of integer | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
Packed Variables.
A packed variable is the element of a packed array or the field of a
packed record. Packed elements and packed fields are allocated and
aligned differently.
Table A-3 shows how the Pascal/V packing algorithm allocates and
aligns the elements of a packed array. The element types are in
alphabetical order. Subsections that Table A-3 references are in
this section, "Pascal/V Packing Algorithm" .
Table A-3. Allocation and Alignment of Packed Array Elements
(Pascal/V Packing Algorithm)
-----------------------------------------------------------------------------------------------
| | | |
| Element Type | Allocation | Alignment |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Array | Use formula in "Arrays" | Byte if element is allocated |
| | | 8 bits; 2-byte otherwise |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Bit16 | 2 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Bit32 | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Bit52 | 8 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Boolean | 1 bit | Bit |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Char | 1 byte | Byte |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Enumeration | See "Packed Enumerations" |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| File | See "Files" | 8-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Longint | 8 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Longreal | 8 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Pointer | HP3000_16 does not affect pointers. |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Real | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Record | Each field is allocated by | 2-byte |
| | type and record is padded to | |
| | nearest 2-byte boundary | |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Set | See "Sets" |
| | |
-----------------------------------------------------------------------------------------------
| | |
| String | See "Strings" |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Subrange of enumeration | See "Packed Subranges of Enumerations" |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Subrange of integer | See "Packed Subranges of Integers" |
| | |
-----------------------------------------------------------------------------------------------
Table A-4 shows how the Pascal/V packing algorithm allocates and
aligns the fields of a packed record. The field types are in
alphabetical order. Subsections that Table A-4 references are in this
section, "Pascal/V Packing Algorithm" .
Table A-4. Allocation and Alignment of Packed Record Fields
(Pascal/V Packing Algorithm)
-----------------------------------------------------------------------------------------------
| | | |
| Variable Type | Allocation | Alignment |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Array | Use formula in "Arrays" | Byte if element is allocated |
| | | 8 bits; 2-byte otherwise |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Bit16 | 2 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Bit32 | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Bit52 | 8 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Boolean | 1 bit | Bit |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Char | 8 bits | Bit, but does not cross |
| | | 2-byte boundary |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Enumeration | See "Packed Enumerations" |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| File | See "Files" | 8-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Longint | 8 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Longreal | 8 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Pointer | HP3000_16 does not affect pointers. See Table 5-1 |
| | |
-----------------------------------------------------------------------------------------------
| | | |
| Real | 4 bytes | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Record | Each field is allocated by | 2-byte |
| | type and record is padded to | |
| | nearest 2-byte boundary | |
| | | |
-----------------------------------------------------------------------------------------------
| | |
| Set | See "Sets" |
| | |
-----------------------------------------------------------------------------------------------
| | |
| String | See "Strings" |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Subrange of enumeration | See "Packed Subranges of Enumerations" |
| | |
-----------------------------------------------------------------------------------------------
| | |
| Subrange of integer | See "Packed Subranges of Integers" |
| | |
-----------------------------------------------------------------------------------------------
Arrays.
This section applies to the allocation of unpacked and packed arrays.
For alignment, see Table A-2 and Table A-3 .
The Pascal/V packing algorithm stores arrays in row-major order (for a
definition of row-major order, see Chapter 5 ).
The Pascal/V packing algorithm uses this formula to allocate an array:
(number_of_elements * space_for_one_element)
+
number_of_internal_unused_bits
+
number_of_trailing_pad bits
The space_for_one_element depends on the element type and whether the
array is unpacked or packed. If the array is unpacked, find its type in
Table A-2 . If the array is packed, find its type in Table A-3 .
If space_for_one_element is less than 16 bits, the
number_of_internal_unused_bits is
16-((16 DIV space_for_one_element) * space_for_one_element)
otherwise, it is zero.
The number_of_trailing_pad_bits is the number of leftover bits in the
last byte or word (whichever each element is allocated).
Example
TYPE
day = (sun,mon,tues,wed,thurs,fri,sat);
VAR
ua : ARRAY [1..8] OF day;
pa : PACKED ARRAY [1..8] OF day;
Each element of ua takes one byte. The entire array takes eight bytes,
with no internal unused bits and no trailing pad bits. The array ua is
allocated and aligned like this:
Each element of pa takes three bits. No element can cross a 2-byte
boundary, so the bit following pa[5] is unused. The entire array takes
four bytes, with one internal unused bit and seven trailing pad bits. It
is allocated and aligned like this:
Files.
The HP Pascal compiler allocates space for an HP3000_16 file this way:
* The file control block is allocated according to the HP Pascal
packing algorithm.
* The file buffer variable size is allocated according to the
Pascal/V packing algorithm.
* The file is 8-byte-aligned.
Records.
This section applies to unpacked and packed records unless otherwise
noted.
The Pascal/V packing algorithm does not always align variant parts of
fields on the same boundary. Each variant part's boundary depends on its
type.
Example
TYPE
Rec = PACKED RECORD
i : integer;
CASE b : boolean OF
TRUE : (f1 : char);
FALSE : (f2 : ARRAY[1..2] OF -32768..32767;
END;
A variable of type Rec is allocated 10 bytes. The TRUE and FALSE
variants are aligned like this:
TRUE Variant
FALSE Variant
The variants f1 and f2 do not start on the same boundary; therefore, f1
cannot be overlaid with f2.
Sometimes you can reduce the space that a record takes by declaring its
fields in different order.
Example
VAR
upr1 : RECORD
b : boolean;
p : 0..32767;
c : char;
END;
upr2 : RECORD
b : boolean;
c : char;
p : 0..32767;
END;
The only difference between the variables upr1 and upr2 above is the
order of their fields.
The variable upr1 takes six bytes:
Because p must be 2-byte-aligned, it cannot start in the second byte.
The sixth byte is allocated to upr1 also, because records are
2-byte-aligned.
The variable upr2 takes four bytes:
Sets.
The Pascal/V packing algorithm allocates sets in byte pairs. The number
of byte pairs allocated to a set depends on its type. For the types
Boolean, char, enumeration, and integer, the formula for the number of
byte pairs is:
number_of_byte_pairs = ceil(bits_required_for_set/16)
(where ceil(x) means the integer closest to x that is greater than or
equal to x).
Table A-5 gives the values for bits_required_for_set and
number_of_byte_pairs for Boolean, char, and integer types.
Table A-5. Bit and Byte Pair Requirements for Boolean, Char, and Integer Base Types
(Pascal/V Packing Algorithm)
-----------------------------------------------------------------------------------------------
| | | |
| Base Type | bits_required_for_set | number_of_byte_pairs |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Boolean | 2 | 1 |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Char | 256 | 16 |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| Integer * | 256 (by default) * | 16 |
| | | |
-----------------------------------------------------------------------------------------------
* Same as bit16, bit32, bit52, shortint, and longint.
* Integers outside the range 0..255 cannot belong to the set.
For enumerated sets, bits_required_for_set is the number of elements in
the set, and you must use the formula to determine number_of_byte_pairs.
Example
VAR
days = SET OF (sun,mon,tues,wed,thurs,fri,sat);
months = SET OF (ja,f,mr,ap,ma,jn,jl,au,s,o,n,d);
set_33 = SET OF (e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,
e12,e13,e14,e15,e16,e17,e18,e19,e20,e21,e22,
e23,e24,e25,e26,e27,e28,e29,e30,e31,e32,e33);
The set days has seven elements and requires seven bits. It is allocated
one byte pair (ceil(7/16) = 1).
Each element is represented by one bit, like this:
The set months has 12 elements and requires 12 bits. It is allocated one
byte pair (ceil(12/16) = 1). Each element is represented by one bit.
The set set_33 has 33 elements and requires 33 bits. It is allocated
three byte pairs (ceil(33/16) = 3). Each element is represented by one
bit.
For integer subrange sets, the formula for the number of byte pairs is:
number_of_byte_pairs = (upper_bound_byte_pair_number -
lower_bound_byte_pair_number) + 1
The upper bound of the integer subrange determines
upper_bound_byte_pair_number, and the lower bound determines
lower_bound_byte_pair_number. The formula is:
byte_pair_number = floor(bound / 16)
(where floor(x) means the integer closest to x that is less than or equal
to x).
Example
VAR
s : SET OF -7..18;
The upper bound of the subrange is 18, so upper_bound_byte_pair_number is
1 (floor(18/16)=1). The lower bound of the subrange is -7, so
lower_bound_byte_pair_number is -1 (floor(-7/16)=-1). The set s is
allocated three byte pairs ((1-(-1))+1=3).
Each set element is represented by one bit, like this:
[REV BEG]
[REV END]
To minimize storage space, avoid base types that are small subranges that
overlap byte pair boundaries.
Example
VAR
s : SET OF 31..32;
The set s takes two byte pairs, using 32 bits to represent a set that
requires only two bits. The arithmetic is:
floor(32/16) - floor31/16) + 1 = (2-1)+1 = 2.
Strings.
The Pascal/V packing algorithm aligns strings on 2-byte boundaries.
Because the current length (0..32767) is allocated two bytes, four bytes
is the smallest possible string allocation.
The formula for the number of bytes allocated to a string is:
Example
VAR
s1 : string[10];
s2 : string[7];
The string s1 takes 14 bytes:
2+10+{2-ORD[ODD(10)]} =
12+[2-ORD(FALSE)] =
12+(2-0) = 14
The allocation is:
The string s2 takes 10 bytes:
2+7+{2-ORD[ODD(7)]} =
9+[2-ORD(TRUE)] =
9+(2-1) = 10
The allocation is:
Packed Enumerations.
This subsection explains how the Pascal/V packing algorithm allocates and
aligns packed enumeration variables. A packed enumeration variable is
either the element of a packed array or the field of a packed record.
The algorithm treats the two cases differently.
Table A-6 shows the relationship between the number of bits that an
enumeration element of a packed array requires, the number of bits that
the Pascal/V packing algorithm allocates it, and its alignment. A
bit-aligned element never crosses a 2-byte boundary.
Table A-6. Allocation and Alignment of Enumeration Elements of Packed Arrays
(Pascal/V Packing Algorithm)
-------------------------------------------------------------------------------------
| | | |
| Required Number of Bits | Number of Bits Allocated | Element Alignment |
| Per Element | Per Element | |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 1 | 1 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 2 | 2 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 3 | 3 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 4 | 4 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 5 | 5 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 6 to 8 | 8 (1 byte) | Byte |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 9 to 16 | 16 (2 bytes) | 2-byte |
| | | |
-------------------------------------------------------------------------------------
Table A-7 shows the relationship between the number of bits that an
enumeration field of a packed record requires, the number of bits that
the Pascal/V mapping algorithm allocates it, and its alignment. A
bit-aligned field never crosses a 2-byte boundary.
Table A-7. Allocation and Alignment of Enumeration Fields of Packed Records
(Pascal/V Packing Algorithm)
-------------------------------------------------------------------------------------
| | | |
| Required Number of Bits | Number of Bits Allocated | Field Alignment |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 1 | 1 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 2 | 2 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 3 | 3 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 4 | 4 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 5 | 5 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 6 | 6 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 7 | 7 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 8 | 8 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 9 | 9 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 10 | 10 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 11 | 11 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 12 | 12 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 13 | 13 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 14 | 14 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 15 | 15 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 16 (2 bytes) | 2 bytes | 2-byte |
| | | |
-------------------------------------------------------------------------------------
Example
TYPE
day = (sun,mon,tues,wed,thurs,fri,sat);
enum_32 = (e1,e2,e3,e4,e5,e6,e7,e8,
e9,e10,e11,e12,e13,e14,e15,e16,
e17,e18,e19,e20,e21,e22,e23,e24,
e25,e26,e27,e28,e29,e30,e31,e32);
VAR
a : PACKED ARRAY [1..11] OF day;
r : PACKED RECORD
f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11 : day;
END;
aa : PACKED ARRAY [1..4] OF enum_32;
rr : PACKED RECORD
f1,f2,f3,f4 : enum_32;
END;
Each element of the array a requires three bits, and no element can cross
a 2-byte boundary. The entire array occupies 35 bits, and is allocated
six bytes.
Each element of the record r requires three bits, and no element can
cross a 2-byte boundary. The entire record occupies 35 bits, and is
allocated six bytes.
Each element of the array aa requires six bits, but is allocated eight
bits (one byte) and is byte-aligned. The entire array takes four bytes:
Each field of the record rr requires and is allocated six bits, and no
field can cross a 2-byte boundary. The entire record occupies 26 bits,
and is allocated four bytes:
Packed Subranges of Enumerations.
This subsection explains how the Pascal/V packing algorithm allocates and
aligns packed variables whose types are subranges of enumerations. These
packed variables are either the elements of packed arrays or the fields
of packed records. The algorithm treats the two cases differently.
The number of bits that an enumeration of a subrange type requires is
determined by ord(upper_bound_of_enumerated_subrange).
Table A-8 shows the relationship between the number of bits that an
enumeration-of-subrange element of a packed array requires, the number of
bits that the Pascal/V packing algorithm allocates it, and its alignment.
No element crosses a 2-byte boundary.
Table A-8. Allocation and Alignment of Enumeration-of-Subrange Elements of Packed Arrays
(Pascal/V Packing Algorithm)
-------------------------------------------------------------------------------------
| | | |
| Required Number of Bits | Number of Bits Allocated | Alignment |
| Per Element | Per Element | |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 1 | 1 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 2 | 2 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 3 | 3 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 4 | 4 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 5 | 5 | Bit |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 6 to 8 | 8 (1 byte) | Byte |
| | | |
-------------------------------------------------------------------------------------
| | | |
| 9 to 16 | 16 (2 bytes) | 2-byte |
| | | |
-------------------------------------------------------------------------------------
Example
TYPE
enum_32 = (e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,
e11,e12,e13,e14,e15,e16,e17,e18,e19,e20,
e21,e22,e23,e24,e25,e26,e27,e28,e29,e30,
e31,e32);
VAR
a : PACKED ARRAY [1..4] OF e7..e15;
b : PACKED ARRAY [1..4] OF e24..e31;
Each element of array a requires and is allocated four bits (see Table
A-6 ). The elements are bit-aligned, and the entire array occupies 16
bits. It is allocated two bytes:
Each element of array b requires and is allocated five bits (see Table
A-6 ). The elements are bit-aligned, and the entire array occupies 21
bits. It is allocated four bytes.
To the enumeration-of-subrange field of a packed record, the Pascal/V
packing algorithm allocates the required number of bits. Any allocation
from one bit to two bytes is possible. The field is bit-aligned, but
never crosses a 2-byte boundary.
Example
TYPE
enum_32 = (e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,
e11,e12,e13,e14,e15,e16,e17,e18,e19,e20,
e21,e22,e23,e24,e25,e26,e27,e28,e29,e30,
e31,e32);
VAR
a : PACKED RECORD
f1,f2,f3,f4 : e7..e15;
END;
b : PACKED RECORD
f1,f2,f3,f4 : e24..e31;
END;
Each field of record a requires and is allocated four bits. The fields
are bit-aligned, but cannot cross 2-byte boundaries. The entire record
is allocated two bytes:
Each field of record b requires and is allocated five bits. The fields
are bit-aligned, but cannot cross 2-byte boundaries. The entire record
occupies 21 bits. It is allocated four bytes:
Packed Subranges of Integers.
This subsection explains how the Pascal/V packing algorithm allocates and
aligns packed variables whose types are subranges of integers. These
packed variables are either the elements of packed arrays or the fields
of packed records.
To the integer subrange variable of a packed array or packed record, the
Pascal/V packing algorithm allocates the required number of bits (if the
subrange is, or is included in, -32768..32767) or four bytes (if the
subrange is outside that range).
Table A-9 shows the relationship between the number of bits that an
element of a PACKED array of subrange type requires, the number of bits
that the Pascal/V mapping algorithm allocates it, and its alignment.
Table A-9. Allocation and Alignment of Elements of Packed Arrays of Subrange Type
(Pascal/V Packing Algorithm)
-----------------------------------------------------------------------------------------------
| | | |
| Required Number of Bits | Number of Bits Allocated | Alignment |
| Per Element * | Per Element | |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| 1 | 1 | Bit |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| 2 | 2 | Bit |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| 3 | 3 | Bit |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| 4 | 4 | Bit |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| 5 | 5 | Bit |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| 6 to 8 | 8 (1 byte) | Byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| 9 to 16 | 16 (2 bytes) | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| 32 | 32 (4 bytes) | 2-byte |
| | | |
-----------------------------------------------------------------------------------------------
* Only if the subrange is, or is included in, -32768..32767; four bytes
otherwise.
Example
VAR
a : PACKED ARRAY [1..4] OF 0..16;
b : PACKED ARRAY [1..4] OF 0..32;
Each element of the array a requires and is allocated five bits, and is
bit-aligned (see Table A-8 ). The entire array occupies 20 bits. It
is allocated four bytes:
Each element of the array b requires six bits, is allocated one byte, and
is byte-aligned (see Table A-8 ). The entire array occupies four
bytes.
For the integer subrange type of a packed record, any bit allocation from
one bit to 15 bits is possible, as are allocations of two and four bytes.
Bit allocations are bit-aligned, but never cross 2-byte boundaries. Two-
and 4-byte allocations are 2-byte aligned. See "Records" for more
information.
Example
VAR
r : PACKED RECORD
a : 0..1; {Requires 1 bit}
b : 0..255; {Requires 8 bits}
c : 0..16; {Requires 5 bits}
d : 0..4; {Requires 3 bits}
e : 10..40000; {Requires 4 bytes}
f : 0..MAXINT; {Requires 4 bytes}
END;
The fields of the record r are allocated the bits that they require.
Fields a, b, c, and d are bit-aligned, but cannot cross 2-byte boundaries
(notice where d and e start). Fields e and f are 2-byte-aligned.
MPE/iX 5.0 Documentation