• Aucun résultat trouvé

Internal Representation of Packed Records

Dans le document Domain Pascal Language Reference (Page 90-99)

QUAD WORD ALIGNMENT

3.10.7 Internal Representation of Packed Records

Table 3-3 shows the space required for fields in packed records.

Domain Pascal always starts the first field of a packed record on a word boundary. After the first field, if the exact number of bits required for the next field crosses zero or one 16-bit boundary, the field starts in the next free bit. If the field would cross two or more 16-bit boundaries, it starts at the next 16-bit boundary. Pascal allocates fields left to right within bytes and then by increasing byte address.

The minimum size of a packed record is 16 bits.

In packed records, characters are byte aligned. Structured types, except for sets, are aligned on word boundaries. Sets are aligned only if they cross two or more 16-bit boundaries.

Data Types 3-35

Table 3-3. Storage of Packed Record Fields

Data Type of Field Space Allocation

Integer; Integer16 16 bits; word aligned.

Integer32, Real, Single 32 bits; word aligned.

Double 64 bits; word aligned.

Boolean 1 bit; bit aligned.

Char 8 bits; byte aligned

Enumerated Number of bits required for largest ordinal value;

bit aligned.

Subrange Subrange of char fields require eight bits; all other subrange fields take up the number of bits required for their extreme values. Subrange of char fields are byte aligned. All other subrange fields are bit aligned.

Set If fewer than 32 elements, then exactly one bit per element; if more then 32 elements, then same size as set. Bit aligned.

Array Mayor may not be packed; requires the same space as an array outside of a packed record. (See the

"Internal Representation of Arrays" section.)

Pointer 32 bits; word aligned.

The following type declaration, along with Figure 3-17, illustrates the storage of a packed record type.

TYPE

Shapes

=

(Sphere, Cube, Ovoid, Cylinder, Tetrahedron);

Uses

=

(Toy, Tool, Weapon, Food);

Characteristics

=

PACKED RECORD

end;

Mass Shape

B

Purpose Low_temp Class

Real;

Shapes;

Boolean;

SET OF Uses;

-100 .. 40;

'A' .. 'Z';

3-36 Data Types

The fields require the following number of bits:

Mass 32 bits (word aligned)

Shape 3 bits (bit aligned)

B 1 bit (bit aligned)

Purpose 4 bits (bit aligned) Low_temp 8 bits (bit aligned) Class 8 bits (word aligned)

(The variable low_temp requires eight bits because it can take a range of 140 values (-100 to +40) and seven bits can represent only 128 values.)

Domain Pascal represents fields in the same order you declared them, as shown in Figure 3-17.

15 131211 8 7

o

Mass Mass

Shape

1 sl

Purpose Low temp

Class Unused

Figure 3-17. Sample Packed Record

In this example, the order of field declaration has been chosen very carefully. The whole record takes up only eight bytes, and out of the eight bytes, only eight bits are unused. If the fields had been declared in a different order, the record might have taken up 10 or 12 bytes.

3.11 Arrays

Like standard Pascal, Domain Pascal supports array types. An array consists of a fixed number of elements of the same data type. This data type is called the component type.

The component type can be any predeclared or user-declared data type.

Use an index type to specify the number of elements the array contains. The index type must be a subrange expression. Domain Pascal permits arrays of up to eight dimensions.

Specify one subrange expression for each dimension.

Data Types 3-37

I

This fragment includes declarations for five arrays:

TYPE

elements (H, He, Li, Be, B, C, N, 0, FI, Ne);

VAR

test_data atomic_weights last_name a_thought lie_test

{elements is an enumerated type.}

{Here are the five array declarations.}

array[1 .. 100] of INTEGER16;

array[H .. Be] of REAL; {Range defined in TYPE}

array[1 .. 15] of CHAR;

STRING;

{declaration. }

array[l .. 4,1 .. 2] of BOOLEAN; {2-dimensional array.}

Notice that variable a_thought is of type string. String is a predefined Domain Pascal ar-ray type. Domain Pascal defines string as follows:

TYPE

string

=

array[1 .. 80] of CHAR;

In other words, string is a data type of 80 characters. Use string to handle lines of text conveniently.

See the" Array Operations" listing in Chapter 4 for a description of array bound checking.

3.11.1 Initializing Array Variables-Extension

3-38

If an array has been declared static or its declaration is not in a function or procedure, you can initialize array components in Domain Pascal in a variable declaration statement.

(See the "Accessing a Variable Stored in Another Pascal Module" and "Accessing a RoU-tine Stored in Another Pascal Module" sections of Chapter 7 for more information on the static attribute.) Domain Pascal initializes only those components for which it finds initiali-zation data; it does not initialize the other components. For example, if an array consists of 1 0 components and you specify six initialization constants, Domain Pascal initializes the first six components and leaves the remaining four components uninitialized.

This section describes the following three basic methods of initializing single-dimensional and multidimensional arrays:

• Initializing mUltiple components to a single value

• Initializing components individually

• Initializing using repeat counts

In addition, this section describes controlling the order of initialization and setting a default for the size of the array.

Data Types

3.11.1.1 Initializing Multiple Components with a SingBe Expression-Extension To initialize multiple components with a single expression, specify an assignment operator

(:=) followed by the value. This initialization method is especially useful for components of type char, where the value is a string of characters. You can also initialize the char com-ponents individually, but it's usually easier to do it as a string.

For example, consider the following single-dimensional initializations:

CONST

msg1

=

'This is message 1';

VAR

sl array [1 .. 40] of CHAR

.-

'Quoted strings are ok';

s2 array [1 .. 30] of CHAR := msg1;

blank_line STRING

.-

chr(lO) ; {Newline}

These initializations assign elements 1 through 21 of array sl with the string "Quoted strings are ok", elements 1 through 17 of array s2 with "This is message 1", and element

1 of blank_line with the newline character.

You can also use this method to initialize multidimensional arrays. To do this, you must assign the value to each row. For example the following initialization statement initializes columns 1 through 6 in rows 1 and 2 to 0:

VAR

I2 array[1 .. 2, 1 .. 6] of INTEGER16 .- [

[1 .. 6 .- 0], [1 .. 6 .- 0], ] ;

3.11.1.2 Initializing Components to Individual Values-Extension

To initialize array components individually, specify an assignment operator (:=) followed by the values to which the components should be initialized. You must enclose the values in-side a pair of brackets and separate each value with a comma.

For example, consider the following single-dimensional array initializations:

VAR

I array[1 .. 6] of INTEGER16 := [1, 2, 4, 8, 16, 32];

R array[1 .. 3] of SINGLE := [-5.2, -7.3, -2E-3];

B array[l .. S] of BOOLEAN := [true, false, true, true, true];

Data Types 3-39

I

You can also use this method . (and the method described in Section 3.11.1. 1) to initialize multidimensional arrays. For example, the following fragment initializes two multidimen-sional arrays (12 and B2). 12 has two subrange index types (1..2 and 1..6). The first index type consists of two values, so you must supply two rows of brackets. The second index is 1 .. 6, so you must specify six values for each row, initializing a total of 12 components.

VAR

I2 array[I .. 2, 1 .. 6] of INTEGER16

.-B2 array[I .. 4, 1 .. 3] of BOOLEAN

.-[ 1 , 2 , 3 , 4 , 5 , 6 ] , [ 7 , 5 , 9 , 1 , 2 , 8 ] , ] ;

[true, true, true], [true, false, false], [false, true, false], [false, false, false], ] ;

3.11.1.3 Initializing Arrays Using Repeat Counts-Extension

Repeat counts let you initialize groups of elements in an array. There are two forms of repeat counts.

The first form takes the following syntax:

n of constant

where of is a keyword, n is an integer, and constant is any valid constant that corresponds to the data type specified in the declaration of the array.

This form tells the compiler to initialize n components of the array to the value of con-stant. n can be an integer or an expression that evaluates to an integer. The following initializations demonstrate this form of the repeat count:

CONST VAR

x = 50;

a array[I .. 1024] of INTEGER16 := [512 OF 0, 512 OF -1];

b array[I .. 400] of REAL := [x of 3.14, 400-x OF 2.7];

In the preceding example, Domain Pascal initializes the first 512 values of array a to 0 and the second 512 values to -1. Domain Pascal also initializes the first 50 components of ar-ray b to 3.14 and the remaining 350 components to 2.7.

The second form of the repeat count takes the following syntax:

• of constant

3-40 Data Types

The asterisk (.) tells Domain Pascal to initialize the remainder of the components in the

3.11.1.4 Initializing Components in Any Order-Extension

{wrong!}

{wrong!}

{Right!}

{Right!}

You can initialize array components in any order. Consider the following initialization statements:

These statements produce the following initializations:

Component 1 = 3

3.11.1.5 Defaulting the Size of an Array-Extension

When you initialize an array in the var declaration part of a program, you can let Domain Pascal determine the size of the array for you. To do this, put an asterisk (*) in place of the upper bound of the array declaration.

For example, in the following fragment, the upper bound of init4 is 18; the upper bound of initS is 22; and the upper bound of init6 is 4. The compiler defines the upper bound once it has counted the number of initializers.

CONST VAR

msg5 = 'And this is message 5.';

init4 initS init6

ARRAY[l .. *] of CHAR := 'This is message 4.';

ARRAY[l .. *l of CHAR := msg5;

ARRAY[l .. *] of INTEGER16 := [1, -17, 35, 46];

NOTE: You can use an asterisk in the index type only if you supply an initialization value for the array. For example, the following frag-ment causes a "Size of TABLEt is zero" warning:

VAR

table1 array[l .. *] of integer16;

3-42 Data Types

3.11.1. 6 Mixing Methods of Array Initialization-Extension

You can combine all methods of array initialization described in this section. The follow-ing example illustrates the use of the methods to initialize an array named array_example:

CONST TYPE

VAR

x = 100;

subr one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve );

array_example; ARRAY [subr] OF integer :=

[

2 of -1,

four .. five := 47, 2 of x,

42,

ten ..

* .-

814

] ;

{repeat count method}

{initializing multiple components to a single value}

{repeat count method}

{initializing components individually}

{initializing multiple components to a single value}

The results of the preceding declarations are:

Component 1 = -1 Component 2 = -1

Component 3 = undefined Component 4

=

47

Component 5 = 47 Component 6 = 100 Component 7 = 100 Component 8 = 42

Component 9 = undefined Component 10 = 814 Component 11

=

814 Component 12

=

814

Data Types 3-43

\

Dans le document Domain Pascal Language Reference (Page 90-99)