DIGITAL Fortran 90
User Manual for
DIGITAL UNIX Systems


Previous Contents Index

11.1.4 Passing Arguments and Function Return Values

DIGITAL Fortran 90 uses the same argument-passing conventions as DIGITAL Fortran 77 on DIGITAL UNIX systems for non-pointer scalar variables and explicit-shape and assumed-size arrays.

When calling DIGITAL Fortran 90 subprograms, be aware that DIGITAL Fortran 90 expects to receive arguments the same way it passes them.

The main points about argument passing and function return values are as follows:

The arguments passed from a calling routine must match the dummy arguments declared in the called function or subroutine (or other procedure), as follows:

For More Information:

11.1.5 Passing Arrays as Arguments

Certain arguments or function return values require the use of an explicit interface, including assumed-shape dummy arguments, pointer dummy arguments, and function return values that are arrays. This is discussed in the DIGITAL Fortran Language Reference Manual.

When passing arrays as arguments, the rank and the extents (number of elements in a dimension) should agree, so the arrays have the same shape and are conformable. If you use an assumed-shape array, the rank is specified and extents of the dummy array argument are taken from the actual array argument.

If the rank and extent (shape) do not agree, the arrays are not conformable. The assignment of elements from the actual array to the noncomformable (assumed-size or explicit-shape) dummy array is done by using array element sequence association. Using array element sequence associations is discussed in the DIGITAL Fortran Language Reference Manual.

Certain combinations of actual and dummy array arguments are disallowed.

For More Information:

11.1.6 Passing Pointers as Arguments

Previous sections have discussed the case where the actual and dummy arguments have neither the POINTER attribute nor the TARGET attribute.

The argument passing rules of like type, kind, and rank (for conformable arrays) or array element sequence association (for noncomformable arrays) apply when:

You can specify an actual argument of type POINTER and a dummy argument of type POINTER. You must use an explicit interface that defines the dummy argument with the POINTER attribute in the code containing the actual argument. This ensures that the pointer is passed, rather than the array data itself.

However, if you specify an actual argument of type POINTER and do not specify an appropriate explicit interface (such as an interface block), it is passed as actual (target) data.

For More Information:

On using pointers and pointer arguments, see the DIGITAL Fortran Language Reference Manual.

11.1.7 DIGITAL Fortran 90 Array Descriptor Format

When using an explicit interface (by association or procedure interface block), DIGITAL Fortran 90 will generate a descriptor for the following types of dummy argument data structures:

To allow calling between DIGITAL Fortran 77 and DIGITAL Fortran 90, certain data structure arguments also supported by DIGITAL Fortran 77 do not use a descriptor, even when an appropriate explicit interface is provided. For example, since explicit-shape and assumed-size arrays are supported by both DIGITAL Fortran 77 and DIGITAL Fortran 90, a descriptor is not used.

However, for cases where the called routine needs the information in the DIGITAL Fortran 90 descriptor, declare the routine with an assumed-shape or pointer argument and an explicit interface.

The byte components of the DIGITAL Fortran 90 descriptor follow:

Within the dimension information (24 bytes) for each rank:

For example, consider the following declaration:


   integer,target :: a(10,10) 
   integer,pointer :: p(:,:) 
   p => a(9:1:-2,1:9:3) 
   call f(p) 
   . 
   . 
   . 

The descriptor for actual argument p would contain the following values:

11.1.8 Argument-Passing Mechanisms and Built-In Functions

When a DIGITAL Fortran 90 program needs to call a routine written in a different language (or in some cases a Fortran 90 subprogram), there may be a need to use a form other the DIGITAL Fortran 90 default passing mechanisms. For example, numeric arguments may need to be passed by immediate value instead of by reference.

To change the DIGITAL Fortran 90 default mechanisms with the DIGITAL Fortran 90 built-in functions, use the following functions:

The %VAL or %REF functions can only be used as unparenthesized arguments in actual argument lists.

Instead of the DIGITAL Fortran 90 built-in functions, you can use the cDEC$ ATTRIBUTES directive to change the DIGITAL Fortran 90 default passing mechanisms for either an entire call or for individual arguments. (see Section 11.2.2).

11.1.8.1 Passing Addresses --- %LOC Function

The %LOC built-in function computes the address of a storage element as an INTEGER*8 (Alpha UNIX systems) value. You can then use this value in an arithmetic expression. It has the following form:

%LOC(arg)

The %LOC function is particularly useful for non-Fortran procedures that may require argument data structures containing the addresses of storage elements. In such cases, the data structures should be declared volatile to protect them from possible optimizations.

For More Information:

11.1.8.2 Passing Arguments by Immediate Value --- %VAL Function

The %VAL function passes the argument list entry as a 64-bit immediate value on DIGITAL UNIX systems. It has the following form:

%VAL(arg)

The argument-list entry generated by the compiler is the value of the argument (arg). Because argument-list entries are eight bytes long, the argument value must be an INTEGER (including INTEGER*8), LOGICAL (including LOGICAL*8), or REAL (REAL*4 and REAL*8) constant, variable, array element, or expression.

If a COMPLEX (KIND=4) or COMPLEX (KIND=8) argument is passed by value, two REAL arguments (one contains the real part; the other the imaginary part) are passed by immediate value. If a COMPLEX parameter to a routine is specified as received by value (or given the C attribute), two REAL parameters are received and stored in the real and imaginary parts of the COMPLEX parameter specified.

If the value is a byte, word, or longword, it is sign-extended to eight bytes.

To produce a zero-extended value rather than a sign-extended value, use the ZEXT intrinsic function (see the DIGITAL Fortran Language Reference Manual).

11.1.8.3 Passing Arguments by Reference --- %REF Function

The %REF function passes the argument by reference. It has the following form:

%REF(arg)

The argument-list entry the compiler generates will contain the address of the argument, (arg). The argument value can be a record name, a procedure name, or a numeric or character expression, array, character array section, or array element. In DIGITAL Fortran 90, passing by reference is the default mechanism for numeric values, so the %REF call is usually not needed. Passing character arrays with %REF caused the hidden length to be omitted.

11.1.8.4 Examples of Argument Passing Built-in Functions

The following examples show the use of the argument list built-in functions:

For an example of passing integer data by value (using %VAL) and by reference (default) to a C function, see Section 11.4.7.

11.2 Using the cDEC$ ALIAS and cDEC$ ATTRIBUTES Directives

This section provides reference information about the following directives:

11.2.1 The cDEC$ ALIAS directive

DIGITAL Fortran 90 supports the cDEC$ ALIAS directive in the same manner as DIGITAL Fortran 77. Use this directive to specify that the external name of an external subprogram is different from the name by which the calling procedure references it.

The syntax is:

cDEC$ ALIAS internal-name, external-name

The internal-name is the name of the subprogram as used in the current program unit.

The external-name is either a quoted character constant (delimited by single quotation marks) or a symbolic name.

If external-name is a quoted character constant, the value of that constant is used as the external name for the specified internal name. The character constant is used as it appears, with no modifications for case or addition or removal of punctuation characters. The default for the DIGITAL Fortran 90 compiler is to force the name into lowercase and append an underscore unless directed otherwise.

If external-name is a symbolic name, the symbolic name (in lowercase) is used as the external name for the specified internal name and an underscore is appended. Any other declaration of the specified symbolic name is ignored for the purposes of the ALIAS directive.

The DIGITAL UNIX linker may have restrictions on what appears in an external name and whether external names are case sensitive.

For example, in the following program (free source form):


  PROGRAM ALIAS_EXAMPLE 
  !DEC$ ALIAS ROUT1, 'ROUT1A' 
  !DEC$ ALIAS ROUT2, 'routine2_' 
  !DEC$ ALIAS ROUT3, rout3A 
        CALL ROUT1 
        CALL ROUT2 
        CALL ROUT3 
  END PROGRAM ALIAS_EXAMPLE 

The three calls are to external routines named ROUT1A, routine2_, and rout3a_. Because rout3A is not in single quotation marks (character constant), a trailing underscore is added (DIGITAL Fortran adds a trailing underscore to external names on UNIX systems unless directed otherwise) and the letter A becomes a lowercase a.

This feature can be useful when porting code systems where different routine naming conventions are in use. By adding or removing the cDEC$ ALIAS directive, you can specify an alternate routine name without recoding the application.

The cDEC$ ALIAS and cDEC$ ATTRIBUTES ALIAS directives are synonymous.

11.2.2 The cDEC$ ATTRIBUTES Directive

Use the cDEC$ ATTRIBUTES directive to specify properties for data objects and procedures. These properties let you specify how data is passed and the rules for invoking procedures. The cDEC$ ATTRIBUTES directive is intended to simplify mixed-language calls with DIGITAL Fortran 90 routines written in C or Assembler. The STDCALL keyword is synonymous with the C keyword on DIGITAL UNIX systems.

The cDEC$ ATTRIBUTES directive takes the following form:


 cDEC$ ATTRIBUTES att [,att]... :: object [,object]... 

In this form:

c

Is the letter or character (c, C, *, !) that introduces the directive (see the DIGITAL Fortran Language Reference Manual).

att

Is one of the keywords listed in the DIGITAL Fortran Language Reference Manual. For example, C, ALIAS, REFERENCE, or VALUE.

object

Is the name of a data object used as an argument or procedure. Only one object is allowed when using the C and ALIAS properties.

The DIGITAL Fortran Language Reference Manual) shows valid combinations of properties with the various types of objects.

The ATTRIBUTES options C, STDCALL, REFERENCE, VALUE, and VARYING all affect the calling convention of routines.

By default, Fortran passes all data by reference (except the hidden length argument of strings, which is a special case). If the C or STDCALL option is used, the default changes to passing almost all data by value except arrays.

In addition to the calling-convention options C and STDCALL, you can also specify argument options, VALUE and REFERENCE, to pass arguments by value or by reference, regardless of the calling convention option. Arrays can only be passed by reference.

Table 11-1 summarizes the effect of the most common Fortran calling-convention directives.

Table 11-1 Calling Conventions for ATTRIBUTES Options
Arguments Default C or STDCALL C or STDCALL with REFERENCE
Scalar Reference Value Reference
Scalar [value] Value Value Value
Scalar [reference] Reference Reference Reference
String Reference, Len: End String (1:1) Reference, Len: End
String [value] Error String(1:1) String(1:1)
String [reference] Reference, No Len Reference: No Len Reference: No Len
Array Reference Reference Reference
Array [value] Error Error Error
Array [reference] Reference Reference Reference
Derived type Reference Value, size dependent Reference
Derived type [value] Value, size dependent Value, size dependent Value, size dependent
Derived type [reference] Reference Reference Reference
F90 Pointer Descriptor Descriptor Descriptor
F90 Pointer [value] Error Error Error
F90 Pointer [reference] Descriptor Descriptor Descriptor

The terms used in Table 11-1 mean the following:
Term Description
[value] Assigned to the VALUE property
[reference] Assigned to the REFERENCE property
Value The argument value is pushed on the stack. All values are padded to the next 8-byte boundary.
Reference The 8-byte argument address is pushed on the stack.
Len: End The length of the string is pushed (by value) on the stack after all of the other arguments.
No Len The length of the string is not available to the called procedure.
String(1:1) For string arguments, the first character is converted to INTEGER(KIND=8) as in ICHAR(string(1:1)) and pushed onto the stack by value.
Error Produces a compiler error.
Descriptor 8-byte address of the array descriptor.
Size dependent Derive-type arguments specified by value are passed as follows:
  • Arguments of 1 to 4 bytes are passed by value
  • Arguments of 5 to 8 bytes are passed in two registers
  • Arguments of more than 8 bytes provide value semantics by passing a temporary storage address by reference.


Previous Next Contents Index