ARRAY Declaration [ SPL to HP C/XL Migration Guide ] MPE/iX 5.0 Documentation
SPL to HP C/XL Migration Guide
ARRAY Declaration
Table 4-3. ARRAY Declaration
----------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
----------------------------------------------------------------------------------------------
| | |
| array-declaration: | array-declaration: |
| | |
| [GLOBAL] [type] ARRAY | 1b, 1d with lower <> 0. |
| | |
| [global-array-defn ,] [...] | [static] [type] |
| | array-id "[" cells "]" [init] ; |
| {global-array-defn | |
| init-global-array-defn} ; | |
| | 1a, 1c; 1b, 1d with lower <> 0. |
| | |
| global-array-defn: | [static] [type] |
| | array-ref "[" cells "]" [init] ; |
| 1a. array-id ( lower : upper ) | |
| | |
| 1b. array-id ( lower : upper ) = DB | [static] [type] * array-id |
| | |
| 2a. array-id (@) = DB | = & array-ref "[" index "]" ; |
| | |
| 2b. array-id (@) = DB + offset | init: |
| | = "{" value [,...] "}" |
| 3a. array-id (*) = DB | |
| | index: |
| 3b. array-id (*) = DB + offset | is the cell number in array-ref of the |
| | cell that corresponds to cell zero in the |
| 4a. array-id (@) | SPL array. |
| | |
| 4b. array-id (@) = register sign offset | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
| | |
| 5. array-id (*) | The other SPL formats establish an |
| | equivalence relative to other declared data |
| 6. array-id (*) = register sign offset | (not just arrays). Depending on their |
| | actual use, they may be converted to HP |
| 7a. array-id (*) = ref-id | C/XL pointer or union types, or #define |
| | directives. If their relationships are |
| 7b. array-id (*) = ref-id sign offset | fairly simple, pointers can be used. Some |
| | suggestions follow: |
| 8. array-id (*) = ref-id (index) | |
| | 2. union |
| | |
| init-global-array-defn: | "{" |
| | |
| 1c. array-id ( lower : upper ) | [type] * array-id ; |
| | |
| := value-group [,...] | [type] * other-id ; |
| | |
| 1d. array-id ( lower : upper ) = DB | : |
| | |
| := value-group [,...] | "}" |
| | |
| | 4. [type] * array-id |
| value-group: | |
| | 5. [type] * array-id |
| {initial-value | |
| repeat-factor ( initial-value[,...] )} | 7. [type] * array-id = & ref-id |
| | |
| | 8. [type] * array-id |
| | |
| | = & ref-id "[" index "]" |
| | |
----------------------------------------------------------------------------------------------
ARRAY Declaration(cont.)
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| Default type: LOGICAL | Default type: int (= long int) |
| | |
---------------------------------------------------------------------------------------------
| | |
| Array declarations specify one-dimensional | Same as SPL. |
| vectors of subscripted variables. | |
| | |
---------------------------------------------------------------------------------------------
| | |
| Array subscript declarations and references | Array subscript declarations and references |
| use parentheses, "( )" | use brackets, "[ ]" |
| | |
---------------------------------------------------------------------------------------------
| | |
| An array identifier with no subscript | An array identifier with no subscript is a |
| implies a subscript of zero. It is | pointer to cell zero. It is not equivalent |
| equivalent to array-id(0). | to array-id[0]. |
| | |
---------------------------------------------------------------------------------------------
| | |
| Array declarations which allocate space may | The lower bound of HP C/XL arrays is always |
| specify a lower bound other than zero, | zero. |
| subject to certain restrictions. | |
| | Therefore, the HP C/XL zero subscript |
| Therefore, the SPL zero subscript may refer | always refers to the "first" (lower bound) |
| to some cell other than the lower bound, or | cell of the array. |
| to a location outside the array. | |
| | |
---------------------------------------------------------------------------------------------
| | |
| Indirect array-ids are equivalent to | Array-ids are identical to pointers in |
| pointers. They may be changed to point to | computations, except that the pointer value |
| a new cell zero location. | of an array-id cannot be changed. That is, |
| | it will always point to cell zero of the |
| Direct array-ids are the location of cell | declared array. |
| zero. Their locations cannot be changed. | |
| | However, HP C/XL pointers may be |
| | subscripted and used like array-ids, |
| | allowing them to act like SPL indirect |
| | array identifiers. |
| | |
---------------------------------------------------------------------------------------------
In HP C/XL, if A is an array-id and P is a pointer-id and P=&A[0], then
the following equivalences exist:
A == &A[0] == P == &P[0]
*A == A[0] == *P == P[0]
*A+1 == A[0]+1 == *P+1 == P[0]+1
*(A+1) == A[1] == *(P+1) == P[1]
This situation simplifies some of the conversion necessary for changing
SPL procedure calls to HP C/XL function calls. If an unsubscripted HP
C/XL array-id is passed to a function, it is passed by value as a pointer
to the array. This is identical to passing cell zero by reference, the
equivalent code in SPL. Therefore, conversion is minimal for full arrays
passed by reference. To pass a specific cell by reference, convert the
SPL cell reference, "id(cell)", to the HP C/XL address format,
"&id[cell]".
Summary of SPL Array Formats
1a. Indirect; bounded; variable is pointer to cell zero; pointer in
next DB primary location; pointer IS allocated; array begins in
next DB secondary location; array IS allocated.
1b. Direct; bounded; variable is cell zero; lower in next DB primary
location; array IS allocated.
1c. Same as 1a; initialized.
1d. Same as 1b; initialized.
2a. Indirect; unbounded; variable is pointer to cell zero; pointer in
next DB primary location; pointer NOT allocated; array NOT
allocated.
2b. Indirect; unbounded; variable is pointer to cell zero; pointer in
specified DB primary location; pointer NOT allocated; array NOT
allocated.
3a. Direct; unbounded; variable is cell zero; cell zero in next DB
primary location; array NOT allocated.
3b. Direct; unbounded; variable is cell zero; cell zero in specified
DB primary location; array NOT allocated.
4a. Indirect; unbounded; variable is pointer to cell zero; pointer in
next DB primary location; pointer IS allocated; array NOT
allocated.
4b. Indirect; unbounded; variable is pointer to cell zero; pointer in
specified Q- or S-relative location; pointer NOT allocated; array
NOT allocated.
5. Indirect; unbounded; variable is pointer to cell zero; pointer in
next DB primary location; pointer IS allocated; array NOT
allocated.
6. Direct; unbounded; variable is cell zero; cell zero in specified
Q- or S-relative location; array NOT allocated.
7a. Direct (if ref-id is direct array or simple variable); unbounded;
variable is cell zero; cell zero in specified location; array NOT
allocated.
Indirect (if ref-id is pointer or indirect array); unbounded;
variable is pointer to cell zero; cell zero in ref-id location;
pointer in next DB primary location IF one id type is BYTE and
other is not; ELSE pointer location shared with ref-id; pointer IS
allocated; array NOT allocated.
7b. Direct; unbounded; variable is cell zero; cell zero in specified
location; array NOT allocated.
8. Direct (if ref-id is direct array); unbounded; variable is cell
zero; cell zero in specified location; array NOT allocated.
Indirect (if ref-id is pointer or indirect array); unbounded;
variable is pointer to cell zero; cell zero in specified location;
pointer in next DB primary location IF specified location is not
ref-id cell zero OR IF one array is BYTE and other is not; ELSE
pointer location shared with ref-id; pointer IS allocated; array
NOT allocated.
Array formats 2, 3, 4, 5, 6, 7, and 8 imply methods of data equivalencing
or "overlays".
Array formats 4, 5, 6, 7, and 8 cannot have the GLOBAL attribute.
Only array formats 1c and 1d may be initialized.
Comparison of Specific Array Declarations
Array Formats 1a and 1c: Bounded Indirect Arrays.
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| | |
| 1a. INTEGER ARRAY ABC(0:4) ; | short int ABC_REF[5]; |
| | short int *ABC = &ABC_REF[0]; |
| | |
| 1a. INTEGER ARRAY ABC(-3:4) ; | short int ABC_REF[5]; |
| | short int *ABC = &ABC_REF[3]; |
| | |
| 1c. INTEGER ARRAY ABC(0:4) := 0,1,2,3; | short int ABC_REF[5]={0,1,2,3}; |
| | short int *ABC = &ABC_REF[0]; |
| | |
| 1c. INTEGER ARRAY ABC(-3:4) := 6,2,5; | short int ABC_REF[8]={6,2,5}; |
| | short int *ABC = &ABC_REF[3]; |
| | |
---------------------------------------------------------------------------------------------
These are SPL "indirect" arrays. In SPL, the location labeled ABC is a
pointer that contains the address (initially) of the zero cell of the
array's data.
By converting the SPL indirect array identifier ABC to HP C/XL pointer
ABC, all the operations (such as assigning a new address) that may be
performed on the SPL array identifier may be performed on the HP C/XL
pointer identifier. The HP C/XL pointer may be subscripted to reference
array cells.
If the SPL lower bound is zero and the array identifier is not modified,
you may use the direct format, as shown below.
The examples above with the nonzero lower bounds show the solution to the
SPL capability to specify non-zero lower bounds. Subscripting ABC from
-3 through 4 will access the eight cells of ABC_REF from 0 through 7.
Array Formats 1b and 1d: Bounded Direct Arrays.
----------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
----------------------------------------------------------------------------------------------
| | |
| | |
| 1b. INTEGER ARRAY ABC(0:4)=DB; | short int ABC[5]; |
| | |
| 1b. INTEGER ARRAY ABC(-3:4)=DB; | short int ABC_REF[8]; |
| | short int *ABC = &ABC_REF[3]; |
| | |
| 1d. INTEGER ARRAY ABC(0:4)=DB:=0,1,2,3; | short int ABC[5]={0,1,2,3}; |
| | |
| 1d. INTEGER ARRAY ABC(-3:4)=DB:=6,2,5; | short int ABC_REF[8]={6,2,5}; |
| | short int *ABC = &ABC_REF[3]; |
| | |
----------------------------------------------------------------------------------------------
These are SPL "direct" arrays: the location labeled ABC refers directly
to cell zero of the array allocation.
Note that the examples above having a nonzero lower bound still require
an indirect solution, identical to the one used for indirect arrays.
Array Formats 2a, 4a, and 5: Unbounded Indirect Arrays.
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| | |
| 2a. INTEGER ARRAY A1(@)=DB; | short int *A1; |
| | |
| 4a. INTEGER ARRAY A2(@); | short int *A2; |
| | |
| 5. INTEGER ARRAY A3(*); | short int *A3; |
| | |
---------------------------------------------------------------------------------------------
These declarations are equivalent in SPL, and each defines an identifier.
However none of them allocates space for the array data; only one 16-bit
word is allocated to be used as a data label referring to an indirect
array, that is, as a pointer to space allocated elsewhere. The address
contained in this pointer must be initialized by the program at run time.
Simple pointers in HP C/XL are equivalent to this type of declaration.
Array Formats 7a and 8: Unbounded Equivalenced Arrays.
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| | |
| DOUBLE ARRAY EFG(0:25); | long int EFG[26]; |
| | |
| ... | ... |
| | |
| 7a. REAL ARRAY ABC(*) = EFG; | float *ABC = &EFG[0]; |
| | |
| 8. DOUBLE ARRAY ABC(*) = EFG(0); | #define ABC EFG |
| | |
| 8. REAL ARRAY ABC(*) = EFG(10); | float *ABC = &EFG[10]; |
| | |
---------------------------------------------------------------------------------------------
SPL assigns the same pointer location to ABC and EFG: if EFG is indirect,
if the index of EFG is zero, and if the type of both arrays or neither is
BYTE. HP C/XL allows you to simulate this with a #define only if both
arrays are of the identical type. Otherwise, you must use a union data
type.
Array Formats 2b and 3b: Unbounded Equivalenced Arrays.
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| | |
| 2b. INTEGER ARRAY DEF(@) = DB + 10; | short int DB[256]; |
| | ... |
| | short int *DEF = &DB[10]; |
| | |
| 3b. REAL ARRAY ABC(*) = DB + 10; | union |
| | { |
| | short int DB[256]; |
| | struct |
| | { |
| | short int dummy[10]; |
| | float ABC_REF[1]; /*cell zero*/ |
| | } |
| | } |
| | float *ABC = &ABC_REF[1]; |
| | |
---------------------------------------------------------------------------------------------
In SPL, two types of arrays may be equated to the DB-relative area:
indirect arrays, in which one word of the DB area is allocated to be used
as a pointer to an array; and direct arrays, in which the name of the
array refers to the next element of the DB area, which is assumed to be
cell zero of an array actually contained within this (DB-relative) area.
If DB-relative addressing is required for an SPL application and cannot
be rewritten in a straightforward manner, a DB area may be simulated in
HP C/XL.
In the first example, the DB area is simulated as a short int array. The
value of the pointer DEF is set to the value in DB[10]. DEF is an
indirect array.
In the second example, the DB area is equivalenced in a union with a
structure that places cell zero of reference array ABC_DEF at location
DB[10]. The pointer ABC is used to reference the array cells of ABC_REF,
thus overcoming the undefined subscript range problem presented by the
unbounded direct array in SPL. ABC_REF is a direct array.
MPE/iX 5.0 Documentation