| United States-English |
|
|
|
![]() |
Fortran 90 Compiler for HP-UX: Fortran 90 Programmer's Guide > Chapter 9 Using Fortran 90 directivesUsing HP Fortran 90 directives |
|
HP Fortran 90 provides a number of compiler directives that are useful for controlling certain functions (for example, optimization) within the source file. Table 9-1 “HP Fortran 90 directives” lists and briefly describes these directives; they are listed in the order in which they appear in the sections below. Table 9-1 HP Fortran 90 directives
In files that use free format, directives must start with the comment character !. In fixed format, they must start with the comment character C, *, or ! in column 1. Keywords and any arguments must be delimited by at least one space character, as in the following: !$HP$ OPTIMIZE ON Using the comment character as the directive prefix ensures that, unless the compiler is specifically looking for the directive, it is otherwise treated as a comment and ignored. The following sections describe each of the HP Fortran 90 directives. The ALIAS directive associates the name of a subroutine, function, entry, or common block with an external name and specifies the parameter-passing conventions of routines written in other languages. Syntax!$HP$ ALIAS name [= external-name] [(arg-pass-mode-list)]
Description and restrictionsThe $HP$ ALIAS directive serves two purposes:
external-name should never conflict with the name of an HP-UX system routine (described in sections 2 and 3 of the HP-UX Reference) or with a Fortran 90 library routine (for example, OPEN, READ, or CLOSE). The $HP$ ALIAS directive applies to subroutines, entries, and functions that are used externally. It does not apply to the main program unit. %VAL is a built-in function that specifies that the value of the actual argument is to be passed to the called procedure. You can use this parameter with all types of arguments. However, when used with a procedure name, it has no effect; a pointer to the procedure is still passed. %REF specifies that the address of the actual argument is to be passed to the called procedure. For non-character arguments, this is the default. For character arguments, %REF disables the passing of the hidden length parameter. When %VAL and %REF are used with the CALL statement, they override the specification in the $HP$ ALIAS directive. For detailed information about these built-in functions and their use in the CALL statement, see the HP Fortran 90 Programmer's Reference.. Note the following restrictions:
Local and global usageThe $HP$ ALIAS directive can be used either locally or globally, as follows:
ExamplesThe $HP$ ALIAS directive is especially useful when calling a routine in a language that uses different conventions than Fortran. The following examples illustrate how to use the $HP$ ALIAS directive to resolve differences with:
Case sensitivity Names in HP Fortran 90 are not case sensitive; that is, the compiler converts all names to lowercase. This means that if you reference a routine in a language that is case sensitive and the routine name contains uppercase letters, a call to that routine in HP Fortran 90 will result in an unresolved reference—unless you use the $HP$ ALIAS directive to redefine the name in all lowercase letters, as in the following example:
Argument-passing conventions By default, HP Fortran 90 assumes that all parameters in a subroutine or function call are passed by reference; that is, the call passes the addresses of the parameters, not their values. On the other hand, C code assumes that parameters are passed by value; that is, the current value of the actual parameter is passed to the called routine. Without the $HP$ ALIAS directive, it would be difficult to call a C routine from a Fortran program. For example, suppose you want to call the system routine calloc (see the malloc(3C) man page) to obtain dynamic memory. The man page describes the calling sequence as:
It would be difficult, using standard Fortran 90 constructs, to provide actual parameters corresponding to nelem and elsize because HP Fortran 90 always passes addresses. The $HP$ ALIAS directive can solve this problem by directing the compiler to generate call-by-value actual parameters:
Strings Programs written in C expect strings to be terminated with the null character ('\0'). But HP Fortran 90 programs pass a hidden length parameter to indicate the end of a string argument. Thus, if you want to pass a string from HP Fortran 90 to a C language function, you must explicitly append the null to the string and suppress the hidden length parameter. The $HP$ ALIAS directive enables you to pass the string from Fortran to C. For example, consider the following routine: The ALIAS directive in the following program enables the string to be passed to c_rout: Example 9-2 pass_str.f90
Here are the command lines to compile and link both files, and to execute the program, along with the output from a sample run:
For more informationFor detailed information about the %REF and %VAL built-in functions, see the HP Fortran 90 Programmer's Reference. The $HP$ CHECK_OVERFLOW directive generates code to trap when an overflow occurs in integer arithmetic. By default, integer overflow is ignored. Syntax!$HP$ CHECK_OVERFLOW INTEGER [ON | OFF]
Description and restrictionsIf you use $HP$ CHECK_OVERFLOW with the ON statement, you can cause your program to ignore the overflow, abort on the overflow, or branch to a trap subroutine. If this directive is not used, the ON statement has no effect on integer overflow errors. This directive can appear anywhere in your program. It stays in effect until a subsequent $HP$ CHECK_OVERFLOW directive changes the status. For more informationFor more information about the ON statement $HP$ CHECK_OVERFLOW directive, see Chapter 10 and the HP Fortran 90 Programmer's Guide. The $HP$ LIST directive turns on or off the inclusion of subsequent source lines in the listing output. Syntax!$HP$ LIST [ON | OFF]
Description and restrictionsThe $HP$ LIST directive controls which source lines are output to the listing file. This directive is effective only when the source files are compiled with the +list option. It may appear anywhere in the source file. If the $HP$ LIST OFF directive occurs in a file that is compiled with the +list option, the listing will contain everything in the source file up through the directive. The $HP$ LIST OFF directive applies to the rest of the file, or until a $HP$ LIST ON directive is encountered. ExampleThe $HP$ LIST directive is especially useful for disabling the listing of include files, as in the following example:
For more informationFor information about the +list option, see Chapter 13. The $HP$ OPTIMIZE directive enables or disables the level of optimization that was specified on the compile line. Syntax!$HP$ OPTIMIZE [ON | OFF]
This directive is effective for all program units that follow it in your program. It should therefore be placed outside and before the program units it is to affect. If you insert this directive inside a program unit, it will have no effect on that program unit, only on those that follow. Description and restrictionsThe $HP$ OPTIMIZE directive allows you to determine which areas of your program that the optimizer will process. Specifying $HP$ OPTIMIZE OFF causes the following source lines not to be optimized. $HP$ OPTIMIZE ON re-enables optimization for the following source lines. This directive is effective only if you have used either the -On or +On option when you compiled the program. If you have not specified either option, both $HP$ OPTIMIZE ON and $HP$ OPTIMIZE OFF will give you level 0 optimization. For more informationFor information about the -On and +On options $HP$ OPTIMIZE directive is also discussed in the HP Fortran 90 Programmer's Guide. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||