Chapter 12
Global Symbols
In standard PL/I, a variable that is to be shared by external procedures must be declared with the EXTERNAL attribute in each procedure that references it. Endless PL/I provides an alternative method for defining external variables. Using the GLOBALDEF attribute, one module can completely declare an external variable; all other modules that reference the variable declare it with the GLOBALREF attribute. The VALUE and READONLY attributes provide additional control over the storage of these variables.
Even if a PL/I program does not itself define external variables in this way, the GLOBALREF attribute permits a PL/I program to access variables defined in modules written in other languages.
This chapter discusses the following topics:
Using global symbols within PL/I procedures
The READONLY and VALUE attributes
Declaring and using system-defined global symbols
12.1. Using Global Symbols in PL/I Procedures
Within your PL/I programs, you can define variables as global external symbols when you are coding calls to system procedures. You can also use global symbols instead of external variables in PL/I procedures and functions.
Table 12.1, “Comparison of Global Symbols and External Variables” summarizes the differences between global symbols and external variables. Note that a primary difference between these variables is the manner in which the linker allocates storage for them. Linker storage allocation is described in Chapter 15, Storage Allocation.
| Global Symbols | External Variables |
|---|---|
| Declared with the GLOBALDEF and GLOBALREF attributes. | Declared with the EXTERNAL attribute. |
| Can be initialized only in the module that defines it with the GLOBALDEF attribute. All other modules must specify GLOBALREF. | Must be declared with the EXTERNAL attribute in all modules that declare it. If initialized, must be initialized with the same value in all modules that declare it. |
| Correspond to global symbols declared in assembly language. | Correspond to FORTRAN common blocks. |
| Fixed-point binary or bit string (less than 33 bits) global symbols can have the VALUE attribute. | Cannot have the VALUE attribute. |
| No practical limit on the number of global symbols that can be defined and referenced in an object module. | Limited to 254 external variables in an object module (minus the number of external file constants). |
| Allocation of storage can be controlled by explicit specification of a program section name. | No control over storage allocation. Each variable is placed in a separate program section. |
12.1.1. The GLOBALDEF Attribute
The GLOBALDEF attribute declares an external variable or an external file constant. You can optionally control the program section in which the data is allocated.
The format of the GLOBALDEF attribute is as follows:
GLOBALDEF [ (psect-name) ]psect-nameSpecifies the name of a program section. A program section name can contain up to 31 alphanumeric characters, including dollar signs ($) and underscores (_). The first character cannot be a numeric character (0 through 9).
If you do not specify a program section name, PL/I places the definition for the name in the default program section associated with the variable. For information on program sections created by PL/I, see Chapter 15, Storage Allocation.
The GLOBALDEF attribute implies the EXTERNAL and STATIC attributes.
The following restrictions apply to the use of the GLOBALDEF attribute:
The GLOBALDEF attribute conflicts with the GLOBALREF and INTERNAL attributes.
It cannot be used with ENTRY constants.
Only one procedure in a program can declare a particular external variable with the GLOBALDEF attribute.
12.1.2. The GLOBALREF Attribute
The GLOBALREF attribute indicates that the declared name is a global symbol defined in an external procedure.
The GLOBALREF attribute implies the EXTERNAL and STATIC attributes. The corresponding name must be declared in another procedure with the GLOBALDEF attribute or, if the external procedure is written in another programming language, its equivalent in that language.
The following restrictions apply to the use of the GLOBALREF attribute:
The GLOBALREF attribute conflicts with the INITIAL, GLOBALDEF, and INTERNAL attributes.
If GLOBALREF is specified with the FILE attribute, no other file description attributes can be specified.
12.1.3. Defining Global Symbols in PL/I
To create a global symbol definition in a PL/I program, you must declare it with the GLOBALDEF attribute in one, and only one, PL/I external procedure. The GLOBALDEF attribute implies the EXTERNAL attribute.
An external variable defined with the GLOBALDEF attribute can be accessed by external procedures that declare the name with the GLOBALREF attribute. For example, the procedure ABC contains the following lines:
ABC: PROCEDURE; DECLARE UNIQUE_VALUE GLOBALDEF FIXED BINARY INITIAL (60); DECLARE XYZ EXTERNAL ENTRY (CHARACTER (*)); ⋮ CALL XYZ ('STRING');
The procedure XYZ contains the following lines:
XYZ: PROCEDURE (STRING_VAL); DECLARE UNIQUE_VALUE GLOBALREF FIXED BINARY; ⋮
In these examples, the external variable UNIQUE_VALUE is declared with the GLOBALDEF attribute and initialized in the procedure ABC. The called external procedure XYZ declares this variable with the attribute GLOBALREF and the appropriate data type attributes.
12.1.4. Using MACRO Global Symbols with Multiple Definitions
Using the VAX MACRO programming language, it is possible to give a global external variable more than one name. However, in a PL/I procedure, you can use only one global symbol name for a particular variable. PL/I assumes that distinct global symbol names denote distinct storage locations; the storage associated with different names must not overlap. This rule applies only to global symbols that are declared without the VALUE attribute.
12.2. The READONLY and VALUE Attributes
Endless PL/I defines two storage class attributes that are not in the standard PL/I language: READONLY and VALUE. The READONLY attribute can be specified for any static variable. The VALUE attribute can be specified only for variables that are declared with the GLOBALREF or GLOBALDEF attributes. You cannot declare a variable with both the READONLY and VALUE attributes.
12.2.1. The READONLY Attribute
The READONLY attribute can be applied to any static variable whose value will not change during the program execution. For example, you can initialize fixed values with the PL/I attributes STATIC and INITIAL, and use the READONLY attribute as in this example:
DECLARE MSG_TEXT CHARACTER(80) STATIC READONLY INITIAL ('Good morning');
This use of the READONLY attribute provides storage optimization and protects variables from inadvertent modification.
12.2.2. The VALUE Attribute
A variable declared with the VALUE attribute does not require an address reference in storage; instead, the compiler can refer to it by value during execution.
When you give a variable the VALUE attribute, you must specify either GLOBALDEF or GLOBALREF. If you specify GLOBALDEF, you must use the INITIAL attribute to define a value for the variable.
For example, the VALUE attribute can be specified in the declaration of an external global symbol, as follows:
DECLARE REQUEST_CODE GLOBALDEF VALUE FIXED BINARY STATIC INITIAL (10);
The variable REQUEST_CODE in this example can be accessed in any external procedure that declares it with the attribute GLOBALREF.
When the VALUE attribute is used with the GLOBALDEF or GLOBALREF attributes, the following rules apply:
The variable can have only one of the following data types:
FIXED BINARY
BIT (n) ALIGNED where n is less than 33
The variable must be scalar.
The value of the variable cannot be modified. Thus, it cannot be used as the target of an assignment statement or an input operation, nor can it be passed by reference in a procedure call. PL/I always creates a dummy argument for a variable with the VALUE attribute that is specified in an argument list.
All declarations of the variable must specify the VALUE attribute.
The variable is not addressable; thus, it cannot be used as the argument of the ADDR built-in function.
A variable declared with the VALUE attribute can be specified as a value to initialize another variable; it must have the same data type as the variable that is being initialized. For example:
DECLARE TEMP GLOBALDEF FIXED VALUE INITIAL(10), ABC FIXED STATIC INIT(TEMP);
The declaration of ABC in this example gives ABC the value 10.
12.3. Obtaining Definitions for System Global Symbols
Within the OpenVMS system, many global symbol definitions are used and accessed by programs and procedures in many ways. The most common uses are to define symbolic names for the following:
Return status values from system procedures
Function codes for system programs
Symbolic names for system mailbox message senders
Bit field definitions in system data structures
From a PL/I program, you can declare the symbolic names for system global symbols with the GLOBALREF and VALUE attributes. The format of these declarations is as follows:
DECLARE symbol-name GLOBALREF FIXED BINARY(31) VALUE;The GLOBALREF attribute indicates to PL/I that the variable is a reference to a global symbol defined in another module. The VALUE attribute indicates that the value of the variable is to be treated as if it were a constant.
The definitions for system global symbols are declared in the default system object module libraries. These libraries are automatically searched when you link a PL/I program. Of particular interest are the global symbols that define symbolic names for system service and file system return status values. Their use is described in Chapter 11, Using PL/I in the Common Language Environment.