Elements of FORTRAN

1


This chapter introduces the basic elements of Sun FORTRAN 77.


Standards

The Sun FORTRAN 77 compiler f77 is an enhanced FORTRAN development system. It conforms to the ANSI X3.9-1978 FORTRAN standard and the corresponding International Standards Organization number is ISO 1539-1980. NIST (formerly GSA and NBS) validates it at appropriate intervals.

This compiler also conforms to the standards FIPS 69-1, BS 6832, and MIL-STD-1753. It provides an IEEE standard 754-1985 floating-point package.

On SPARC systems, it provides support for optimization exploiting features of SPARC V8, including the SuperSPARCTM implementation. These features are defined in the SPARC Architecture Manual: Version 8.


Extensions

Extensions to the standard FORTRAN 77 language include recursion, pointers, double-precision complex, quadruple-precision real, quadruple-precision complex, and many VAX® and VMS® FORTRAN 5.0 extensions, including NAMELIST, DO WHILE, structures, records, unions, maps, and variable formats. Multiprocessor FORTRAN includes automatic and explicit loop parallelization.

Sun FORTRAN 77 accepts many VMS extensions, so that programs originally written for VAX systems will port easily to Solaris.


Basic Terms

Some of the FORTRAN basic terms and concepts are:


Character Set

The character set consists of the following:

Note the following usage and restrictions:

The -U option of f77 makes case significant in symbolic names.
For the backslash (\) character, you may need to use an escape sequence or use the -xl compiler option. The backslash (\) is also called a reverse solidus, and the slash (/), a solidus. For the newline (\n) character, you must use an escape sequence. See also Table 2-3.

Symbolic Names

The items in the following table can have symbolic names:

Table  1-2 Items with Symbolic Names

Symbolic constants

Variables

Arrays

Structures

Records

Record fields

Labeled commons

Namelist groups

Main programs

Subroutines

Functions

Entry points

The following restrictions apply:

Example: These names are equivalent with the default in effect:

	ATAD = 1.0E-6 
	Atad = 1.0e-6 
Example: These names are equivalent:

	IF ( X .LT. ATAD ) GO TO 9 
	IF ( X .LT. A TAD ) GO TO 9 
	IF(X.LT.ATAD)GOTO9 
Here are some sample symbolic names:

Table  1-3 Sample Symbolic Names

Valid
Invalid
Reason
X2 2X Starts with a digit.
DELTA_TEMP _DELTA_TEMP Starts with an _ (reserved for the compiler).
Y$Dot Y|Dot There is an invalid character |.

Program

A program unit is a sequence of statements, terminated by an END statement. Every program unit is either a main program or a subprogram. If a program is to be executable, it must have a main program.

There are three types of subprograms: subroutines, functions, and block data subprograms. The subroutines and functions are called procedures, which are invoked from other procedures or from the main program. The block data subprograms are handled by the loader.


Statements

A statement consists of one or more key words, symbolic names, literal constants, and operators, with appropriate punctuation. In FORTRAN, no keywords are reserved in all contexts. Most statements begin with a keyword; the exceptions are the statement function and assignment statements.

Executable or Nonexecutable Statements

Every statement is either executable or nonexecutable. In general, if a statement specifies an action to be taken at runtime, it is executable. Otherwise, it is nonexecutable.

The nonexecutable statements specify attributes, such as type and size; determine arrangement or order; define initial data values; specify editing instructions; define statement functions; classify program units; and define entry points. In general, nonexecutable statements are completed before execution of the first executable statement.

FORTRAN Statements

Table  1-4 FORTRAN Statements

ACCEPT*

ASSIGN*

Assignment*

AUTOMATIC

BACKSPACE*

BLOCK DATA

BYTE

CALL*

CHARACTER

CLOSE*

COMMON

COMPLEX

CONTINUE*

DATA

DECODE*

DIMENSION

DO*

DO WHILE*

DOUBLE COMPLEX

DOUBLE PRECISION

ELSE*

ELSE IF*

ENCODE*

END*

END DO*

END FILE*

END IF*

END MAP

END STRUCTURE

END UNION

ENTRY

EQUIVALENCE

EXTERNAL

FORMAT

FUNCTION

GOTO*

GOTO (Assigned)*

GOTO (Unconditional)*

IF (Arithmetic)*

IF (Block)*

IF (Logical)*

IMPLICIT

INCLUDE

INQUIRE*

INTEGER

INTRINSIC

LOGICAL

MAP

NAMELIST

OPEN*

OPTIONS

PARAMETER

PAUSE*

POINTER

PRINT*

PRAGMA

PROGRAM

REAL

RECORD

RETURN*

REWIND*

SAVE

Statement Function

STATIC*

STOP*

STRUCTURE

SUBROUTINE*

TYPE

UNION

VIRTUAL

VOLATILE

WRITE*

The asterisk (*) in the table indicates an executable statement.


Source Line Formats

A statement takes one or more lines; the first line is called the initial line; the subsequent lines are called the continuation lines.

You can format a source line in either of two ways:

Standard Fixed Format

The standard fixed format source lines are defined as follows:

Tab-Format

The tab-format source lines are defined as follows:

Mixing Formats

You can format lines both ways in one program unit, but not in the same line.

Continuation Lines

The default maximum number of continuation lines is 99 (1 initial and 99 continuation). To change this number of lines, use the -Nln option.

Extended Lines

To extend the source line length to 132 characters, use the -e option. Otherwise, by default, f77 ignores any characters after column 72.

Example: Compile to allow extended lines:

demo% f77 -e prog.f 

Padding

Padding is significant in lines such as the two in the following DATA statement:

C        1         2         3         4         5         6         7 
C23456789012345678901234567890123456789012345678901234567890123456789012
      DATA SIXTYH/60H
     1                              /

Comments and Blank Lines

A line with a c, C, *, d, D, or! in column one is a comment line, except that if the -xld option is set, then the lines starting with D or d are compiled as debug lines. The d, D, and! are nonstandard.

If you put an exclamation mark (!) in any column of the statement field, except within character literals, then everything after the ! on that line is a comment.

A totally blank line is a comment line.

Example: c, C, d, D, *,!, and blank comments:

c	Start expression analyzer 
	CHARACTER S, STACK*80 
	COMMON /PRMS/ N, S, STACK 
	... 
*	Crack the expression:
	IF ( S .GE. '0' .AND. S .LE. '9' ) THEN ! EoL comment 
		CALL PUSH 	! Save on stack. EoL comment 
d		PRINT *, S	! Debug comment & EoL comment 
	ELSE 
		CALL TOLOWER ! To lowercase EoL comment 
	END IF 
D	PRINT *, N		! Debug comment & EoL comment 
	... 
C	Finished 
!	expression analyzer

Directives

A directive passes information to a compiler in a special form of comment. Directives are also called compiler pragmas. There are two kinds of directives:

General Directives

The form of a general directive is one of the following:

The variable id identifies the kind of directive; a is an argument.

Syntax

A directive has the following syntax:

Rules and Restrictions

After the first eight characters, blanks are ignored, and uppercase and lowercase are equivalent, as in FORTRAN text.

Because it is a comment, a directive cannot be continued, but you can have many C$PRAGMA lines, one after the other, as needed.

If a comment satisfies the above syntax, it is expected to contain one or more directives recognized by the compiler; if it does not, a warning is issued.

The C() Directive

The C() directive specifies that its arguments are external functions written in the C language. It is equivalent to an EXTERNAL declaration with the addition that the f77 compiler does not append an underscore to such names, as it ordinarily does with external names.

The C() directive for a particular function must appear before the first reference to that function in each subprogram that contains such a reference. The recommended usage is:

	EXTERNAL ABC, XYZ  !$PRAGMA C(ABC, XYZ) 

The unroll Directive

The unroll directive requires that you specify sun after C$PRAGMA.

The C$PRAGMA sun unroll=n directive allows the optimizer to unroll loops n times, where n is a positive integer. The choices are:

Loops are only unrolled if deemed profitable by the optimizer. If any loops are actually unrolled, then the executable file becomes larger. For a discussion of loop unrolling, see the Sun Fortran: Programmer's Guide chapter Performance and Optimization.

Example: To unroll loops two times:

C$PRAGMA SUN UNROLL=2

The WEAK Directive

The WEAK directive defines a symbol to have less precedence than an earlier definition of the same symbol. This pragma is used mainly in sources files for building libraries. The linker does not produe an error message if it is unable to resolve a weak symbol.

C$PRAGMA WEAK (name1 [=name2])

WEAK (name1) defines name1 to be a weak symbol. The linker does not produce an error message if it does not find a definition for name1.

WEAK (name1=name2) defines name1 to be a weak symbol and an alias for name2.

See the Sun Fortran User's Guide and the Solaris Linker and Libraries Guide for more information.

Parallel Directives

A parallel directive is a special comment that directs the compiler to do something about parallelization. The following are the parallel directives:

For syntax and other information on parallel directives, see the chapter on parallelization in the Fortran Programmer's Guide.