Merge from the pain train
[official-gcc.git] / gcc / fortran / gfortran.texi
blobc3242f7b5a3d8bdfb117a63fa17cc89a81c980e4
1 \input texinfo  @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename gfortran.info
4 @set copyrights-gfortran 1999-2005
6 @include gcc-common.texi
8 @settitle The GNU Fortran 95 Compiler
10 @c Create a separate index for command line options
11 @defcodeindex op
12 @c Merge the standard indexes into a single one.
13 @syncodeindex fn cp
14 @syncodeindex vr cp
15 @syncodeindex ky cp
16 @syncodeindex pg cp
17 @syncodeindex tp cp
19 @c %**end of header
21 @c Use with @@smallbook.
23 @c %** start of document
25 @c Cause even numbered pages to be printed on the left hand side of
26 @c the page and odd numbered pages to be printed on the right hand
27 @c side of the page.  Using this, you can print on both sides of a
28 @c sheet of paper and have the text on the same part of the sheet.
30 @c The text on right hand pages is pushed towards the right hand
31 @c margin and the text on left hand pages is pushed toward the left
32 @c hand margin.
33 @c (To provide the reverse effect, set bindingoffset to -0.75in.)
35 @c @tex
36 @c \global\bindingoffset=0.75in
37 @c \global\normaloffset =0.75in
38 @c @end tex
40 @copying
41 Copyright @copyright{} @value{copyrights-gfortran} Free Software Foundation, Inc.
43 Permission is granted to copy, distribute and/or modify this document
44 under the terms of the GNU Free Documentation License, Version 1.1 or
45 any later version published by the Free Software Foundation; with the
46 Invariant Sections being ``GNU General Public License'' and ``Funding
47 Free Software'', the Front-Cover
48 texts being (a) (see below), and with the Back-Cover Texts being (b)
49 (see below).  A copy of the license is included in the section entitled
50 ``GNU Free Documentation License''.
52 (a) The FSF's Front-Cover Text is:
54      A GNU Manual
56 (b) The FSF's Back-Cover Text is:
58      You have freedom to copy and modify this GNU Manual, like GNU
59      software.  Copies published by the Free Software Foundation raise
60      funds for GNU development.
61 @end copying
63 @ifinfo
64 @dircategory Programming
65 @direntry
66 * gfortran: (gfortran).                  The GNU Fortran 95 Compiler.
67 @end direntry
68 This file documents the use and the internals of
69 the GNU Fortran 95 compiler, (@command{gfortran}).
71 Published by the Free Software Foundation
72 59 Temple Place - Suite 330
73 Boston, MA 02111-1307 USA
75 @insertcopying
76 @end ifinfo
78 Contributed by Steven Bosscher (@email{s.bosscher@@gcc.gnu.org}).
80 @setchapternewpage odd
81 @titlepage
82 @title Using GNU Fortran 95
83 @sp 2
84 @center Steven Bosscher
85 @page
86 @vskip 0pt plus 1filll
87 For the @value{version-GCC} Version*
88 @sp 1
89 Published by the Free Software Foundation @*
90 59 Temple Place - Suite 330@*
91 Boston, MA 02111-1307, USA@*
92 @c Last printed ??ber, 19??.@*
93 @c Printed copies are available for $? each.@*
94 @c ISBN ???
95 @sp 1
96 @insertcopying
97 @end titlepage
98 @summarycontents
99 @contents
100 @page
102 @node Top, Copying,, (DIR)
103 @top Introduction
104 @cindex Introduction
106 This manual documents the use of @command{gfortran}, 
107 the GNU Fortran 95 compiler. You can find in this manual how to invoke
108 @command{gfortran}, as well as its features and incompatibilities.
110 @ifset DEVELOPMENT
111 @emph{Warning:} This document, and the compiler it describes, are still
112 under development.  While efforts are made too keep it up-to-date it might
113 not accurately reflect the status of the most recent @command{gfortran}.
114 @end ifset
116 @menu
117 * Copying::         GNU General Public License says
118                     how you can copy and share GNU Fortran.
119 * GNU Free Documentation License::
120                     How you can copy and share this manual.
121 * Funding::         How to help assure continued work for free software.
122 * Getting Started:: What you should know about @command{gfortran}.
123 * GFORTRAN and GCC::        You can compile Fortran, C, or other programs.
124 * GFORTRAN and G77::     Why we choose to start from scratch.
125 * Invoking GFORTRAN::    Command options supported by @command{gfortran}.
126 * Project Status::  Status of @command{gfortran}, Roadmap, proposed extensions.
127 * Contributing::    Helping you can help.
128 * Standards::       Standards supported by @command{gfortran}
129 * Extensions::      Language extensions implemented by @command{gfortran}
130 * Index::           Index of this documentation.
131 @end menu
135 @c ---------------------------------------------------------------------
136 @c GNU General Public License
137 @c ---------------------------------------------------------------------
139 @include gpl.texi
143 @c ---------------------------------------------------------------------
144 @c GNU Free Documentation License
145 @c ---------------------------------------------------------------------
147 @include fdl.texi
151 @c ---------------------------------------------------------------------
152 @c Funding Free Software
153 @c ---------------------------------------------------------------------
155 @include funding.texi
159 @c ---------------------------------------------------------------------
160 @c Getting Started
161 @c ---------------------------------------------------------------------
163 @node Getting Started
164 @chapter Getting Started
166 Gfortran is the GNU Fortran 95 compiler front end,
167 designed initially as a free replacement for,
168 or alternative to, the unix @command{f95} command;
169 @command{gfortran} is command you'll use to invoke the compiler.
171 Gfortran is still in an early state of development.
172 @command{gfortran} can generate code for most constructs and expressions,
173 but much work remains to be done.
175 When @command{gfortran} is finished,
176 it will do everything you expect from any decent compiler: 
178 @itemize @bullet
179 @item
180 Read a user's program,
181 stored in a file and containing instructions written
182 in Fortran 77, Fortran 90 or Fortran 95.
183 This file contains @dfn{source code}.
185 @item
186 Translate the user's program into instructions a computer
187 can carry out more quickly than it takes to translate the
188 instructions in the first
189 place.  The result after compilation of a program is
190 @dfn{machine code},
191 code designed to be efficiently translated and processed
192 by a machine such as your computer.
193 Humans usually aren't as good writing machine code
194 as they are at writing Fortran (or C++, Ada, or Java),
195 because is easy to make tiny mistakes writing machine code.
197 @item
198 Provide the user with information about the reasons why
199 the compiler is unable to create a binary from the source code.
200 Usually this will be the case if the source code is flawed.
201 When writing Fortran, it is easy to make big mistakes.
202 The Fortran 90 requires that the compiler can point out
203 mistakes to the user.
204 An incorrect usage of the language causes an @dfn{error message}.
206 The compiler will also attempt to diagnose cases where the
207 user's program contains a correct usage of the language,
208 but instructs the computer to do something questionable.
209 This kind of diagnostics message is called a @dfn{warning message}.
211 @item
212 Provide optional information about the translation passes
213 from the source code to machine code.
214 This can help a user of the compiler to find the cause of
215 certain bugs which may not be obvious in the source code,
216 but may be more easily found at a lower level compiler output.
217 It also helps developers to find bugs in the compiler itself.
219 @item
220 Provide information in the generated machine code that can
221 make it easier to find bugs in the program (using a debugging tool,
222 called a @dfn{debugger}, such as the GNU Debugger @command{gdb}). 
224 @item
225 Locate and gather machine code already generated to
226 perform actions requested by statements in the user's program.
227 This machine code is organized into @dfn{modules} and is located
228 and @dfn{linked} to the user program. 
229 @end itemize
231 Gfortran consists of several components:
233 @itemize @bullet
234 @item
235 A version of the @command{gcc} command
236 (which also might be installed as the system's @command{cc} command)
237 that also understands and accepts Fortran source code.
238 The @command{gcc} command is the @dfn{driver} program for
239 all the languages in the GNU Compiler Collection (GCC);
240 With @command{gcc},
241 you can compiler the source code of any language for
242 which a front end is available in GCC.
244 @item
245 The @command{gfortran} command itself,
246 which also might be installed as the
247 system's @command{f95} command.
248 @command{gfortran} is just another driver program,
249 but specifically for the Fortran 95 compiler only.
250 The difference with @command{gcc} is that @command{gfortran}
251 will automatically link the correct libraries to your program.
253 @item
254 A collection of run-time libraries.
255 These libraries contains the machine code needed to support
256 capabilities of the Fortran language that are not directly
257 provided by the machine code generated by the
258 @command{gfortran} compilation phase,
259 such as intrinsic functions and subroutines,
260 and routines for interaction with files and the operating system.
261 @c and mechanisms to spawn,
262 @c unleash and pause threads in parallelized code.
264 @item
265 The Fortran compiler itself, (@command{f951}).
266 This is the gfortran parser and code generator,
267 linked to and interfaced with the GCC backend library.
268 @command{f951} ``translates'' the source code to
269 assembler code.  You would typically not use this
270 program directly;
271 instead, the @command{gcc} or @command{gfortran} driver
272 programs will call it for you.
273 @end itemize
277 @c ---------------------------------------------------------------------
278 @c GFORTRAN and GCC
279 @c ---------------------------------------------------------------------
281 @node GFORTRAN and GCC
282 @chapter GFORTRAN and GCC
283 @cindex GNU Compiler Collection
285 GCC used to be the GNU ``C'' Compiler,
286 but is now known as the @dfn{GNU Compiler Collection}.
287 GCC provides the GNU system with a very versatile
288 compiler middle end (shared optimization passes),
289 and with back ends (code generators) for many different
290 computer architectures and operating systems.
291 The code of the middle end and back end are shared by all
292 compiler front ends that are in the GNU Compiler Collection.
294 A GCC front end is essentially a source code parser
295 and a pass to generate a representation of the semantics
296 of the program in the source code in the GCC language
297 independent intermediate language,
298 called @dfn{GENERIC}.
300 The parser takes a source file written in a
301 particular computer language, reads and parses it,
302 and tries to make sure that the source code conforms to
303 the language rules.
304 Once the correctness of a program has been established,
305 the compiler will build a data structure known as the
306 @dfn{Abstract Syntax tree},
307 or just @dfn{AST} or ``tree'' for short.
308 This data structure represents the whole program
309 or a subroutine or a function.
310 The ``tree'' is passed to the GCC middle end,
311 which will perform optimization passes on it,
312 pass the optimized AST and generate assembly
313 for the program unit.
315 Different phases in this translation process can be,
316 and in fact @emph{are} merged in many compiler front ends.
317 GNU Fortran 95 has a strict separation between the
318 parser and code generator.
320 The goal of the gfortran project is to build a new front end for GCC:
321 A Fortran 95 front end.
322 In a non-gfortran installation,
323 @command{gcc} will not be able to compile Fortran 95 source code
324 (only the ``C'' front end has to be compiled if you want to build GCC,
325 all other languages are optional).
326 If you build GCC with gfortran, @command{gcc} will recognize
327 @file{.f/.f90/.f95} source files and accepts Fortran 95 specific
328 command line options.
332 @c ---------------------------------------------------------------------
333 @c GFORTRAN and G77
334 @c ---------------------------------------------------------------------
336 @node GFORTRAN and G77
337 @chapter GFORTRAN and G77
338 @cindex Fortran 77
339 @cindex G77
341 Why do we write a compiler front end from scratch? 
342 There's a fine Fortran 77 compiler in the
343 GNU Compiler Collection that accepts some features
344 of the Fortran 90 standard as extensions.
345 Why not start from there and revamp it?
347 One of the reasons is that Craig Burley, the author of G77,
348 has decided to stop working on the G77 front end.
349 On @uref{http://world.std.com/~burley/g77-why.html,
350 Craig explains the reasons for his decision to stop working on G77}
351 in one of the pages in his homepage.
352 Among the reasons is a lack of interest in improvements to
353 @command{g77}.
354 Users appear to be quite satisfied with @command{g77} as it is.
355 While @command{g77} is still being maintained (by Toon Moene),
356 it is unlikely that sufficient people will be willing
357 to completely rewrite the existing code. 
359 But there are other reasons to start from scratch.
360 Many people, including Craig Burley,
361 no longer agreed with certain design decisions in the G77 front end.
362 Also, the interface of @command{g77} to the back end is written in
363 a style which is confusing and not up to date on recommended practice.
364 In fact, a full rewrite had already been planned for GCC 3.0.
366 When Craig decided to stop,
367 it just seemed to be a better idea to start a new project from scratch,
368 because it was expected to be easier to maintain code we
369 develop ourselves than to do a major overhaul of @command{g77} first,
370 and then build a Fortran 95 compiler out of it.
373 @include invoke.texi
375 @c ---------------------------------------------------------------------
376 @c Project Status
377 @c ---------------------------------------------------------------------
379 @node Project Status
380 @chapter Project Status
382 @quotation
383 As soon as gfortran can parse all of the statements correctly,
384 it will be in the ``larva'' state.
385 When we generate code, the ``puppa'' state.
386 When gfortran is done,
387 we'll see if it will be a beautiful butterfly,
388 or just a big bug....
390 --Andy Vaught, April 2000
391 @end quotation
393 The start of the GNU Fortran 95 project was announced on
394 the GCC homepage in March 18, 2000
395 (even though Andy had already been working on it for a while,
396 or course).
398 Gfortran is currently reaching the stage where is is able to compile real
399 world programs.  However it is still under development and has many rough
400 edges.
402 @menu
403 * Compiler Status::
404 * Library Status::
405 * Proposed Extensions::
406 @end menu
408 @node Compiler Status
409 @section Compiler Status
411 @table @emph
412 @item Front end
413 This is the part of gfortran which parses a source file, verifies that it
414 is valid Fortran 95, performs compile time replacement of constants
415 (PARAMETER variables) and reads and generate module files. This is
416 almost complete. Every Fortran 95 source should be accepted, and most
417 none-Fortran 95 source should be rejected. If you find a source file where
418 this is not true, please tell us. You can use the -fsyntax-only switch to
419 make gfortran quit after running the front end, effectively reducing it to
420 a syntax checker.
422 @item Middle end interface
423 These are the parts of gfortran that take the parse tree generated by the
424 front end and translate it to the GENERIC form required by the GCC back
425 end. Work is ongoing in these parts of gfortran, but a large part has
426 already been completed.
427 @end table
429 @node Library Status
430 @section Library Status
432 Some intrinsic functions map directly to library functions, and in most
433 cases the name of the library function used depends on the type of the
434 arguments.  For some intrinsics we generate inline code, and for others,
435 such as sin, cos and sqrt, we rely on the backend to use special
436 instructions in the floating point unit of the CPU if available, or to
437 fall back to a call to libm if these are not available.
439 Implementation of some non-elemental intrinsic functions (eg. DOT_PRODUCT,
440 AVERAGE) is not yet optimal. This is hard because we have to make decisions
441 whether to use inline code (good for small arrays as no function call
442 overhead occurs) or generate function calls (good for large arrays as it
443 allows use of hand-optimized assembly routines, SIMD instructions, etc.)
445 The IO library is still under development.  The following features should be
446 usable for real programs:
448 @itemize @minus
449 @item List directed
450 @item Unformatted sequential
451 @end itemize
453 Usable with bugs:
455 @itemize @minus
456 @item Formatted sequential ('T' edit descriptor, and others)
457 @item Namelist (can read a namelist that it writes, but not free-form)
458 @end itemize
460 Not recommended:
462 @itemize @minus
463 @item Unformatted direct access
464 @item Formatted direct access
465 @end itemize
467 Many Fortran programs only use a small subset of the available IO
468 capabilities, so your mileage may vary.
470 @node Proposed Extensions
471 @section Proposed Extensions
473 Here's a list of proposed extensions for @command{gfortran}, in no particular
474 order.  Most of these are necessary to be fully compatible with
475 existing Fortran compilers, but they are not part of the official
476 J3 Fortran 95 standard.
478 @subsection Compiler extensions: 
479 @itemize @bullet
480 @item
481 Flag for defining the kind number for default logicals.
483 @item
484 User-specified alignment rules for structures.
485 @item
486 Flag to generate a @code{Makefile} info.
488 @item
489 Automatically extend single precision constants to double.
491 @item
492 Cray pointers (this was high on the @command{g77} wishlist).
494 @item
495 Compile code that conserves memory by dynamically allocating common and
496 module storage either on stack or heap.
498 @item
499 Flag to cause the compiler to distinguish between upper and lower case
500 names.  The Fortran 95 standard does not distinguish them.
502 @item
503 Compile switch for changing the interpretation of a backslash from a
504 character to ``C''-style escape characters.
506 @item
507 Compile flag to generate code for array conformance checking (suggest -CC).
509 @item
510 User control of symbol names (underscores, etc).
512 @item
513 Compile setting for maximum size of stack frame size before spilling
514 parts to static or heap.
516 @item
517 Flag to force local variables into static space.
519 @item
520 Flag to force local variables onto stack.
522 @item
523 Flag to compile lines beginning with ``D''.
525 @item
526 Flag to ignore lines beginning with ``D''.
528 @item
529 Flag for maximum errors before ending compile.
531 @item
532 Generate code to check for null pointer dereferences -- prints locus of
533 dereference instead of segfaulting.  There was some discussion about this
534 option in the g95 development mailing list.
536 @item
537 Allow setting default unit number.
539 @item
540 Option to initialize of otherwise uninitialized integer and floating
541 point variables.
543 @item
544 Support for OpenMP directives.  This also requires support from the runtime
545 library and the rest of the compiler.
547 @item
548 Support for Fortran 200x. This includes several new features including
549 floating point exceptions, extended use of allocatable arrays, C
550 interoperability, Parameterizer data types and function pointers.
551 @end itemize
554 @subsection Environment Options
555 @itemize @bullet
556 @item
557 Pluggable library modules for random numbers, linear algebra.
558 LA should use BLAS calling conventions.
560 @item
561 Environment variables controlling actions on arithmetic exceptions like
562 overflow, underflow, precision loss -- Generate NaN, abort, default.
563 action.
565 @item
566 Set precision for fp units that support it (i387).
568 @item
569 Variables for setting fp rounding mode.
571 @item
572 Support old style namelists ending in $end or &end.
574 @item
575 Variable to fill uninitialized variables with a user-defined bit
576 pattern.
578 @item
579 Environment variable controlling filename that is opened for that unit
580 number.
582 @item
583 Environment variable to clear/trash memory being freed.
585 @item
586 Environment variable to control tracing of allocations and frees.
588 @item
589 Environment variable to display allocated memory at normal program end.
591 @item
592 Environment variable for filename for * IO-unit.
594 @item
595 Environment variable for temporary file directory.
597 @item
598 Environment variable forcing standard output to be line buffered (unix).
600 @item
601 Variable for swapping endianness during unformatted read.
603 @item
604 Variable for swapping Endianness during unformatted write.
605 @end itemize
607 @c ---------------------------------------------------------------------
608 @c Extensions
609 @c ---------------------------------------------------------------------
611 @c Maybe this chapter should be merged with the 'Standards' section,
612 @c whenever that is written :-)
614 @node Extensions
615 @chapter Extensions
616 @cindex Extension
618 @command{gfortran} implements a number of extensions over standard
619 Fortran. This chapter contains information on their syntax and
620 meaning.
622 @menu
623 * Old-style kind specifications::
624 * Old-style variable initialization::
625 @end menu
627 @node Old-style kind specifications
628 @section Old-style kind specifications
629 @cindex Kind specifications
631 @command{gfortran} allows old-style kind specifications in
632 declarations. These look like:
633 @smallexample
634       TYPESPEC*k x,y,z
635 @end smallexample
636 where @code{TYPESPEC} is a basic type, and where @code{k} is a valid kind
637 number for that type. The statement then declares @code{x}, @code{y}
638 and @code{z} to be of type @code{TYPESPEC} with kind @code{k}. In
639 other words, it is equivalent to the standard conforming declaration
640 @smallexample
641       TYPESPEC(k) x,y,z
642 @end smallexample
644 @node Old-style variable initialization
645 @section Old-style variable initialization
646 @cindex Initialization
648 @command{gfortran} allows old-style initialization of variables of the
649 form:
650 @smallexample
651       INTEGER*4 i/1/,j/2/
652       REAL*8 x(2,2) /3*0.,1./
653 @end smallexample
654 These are only allowed in declarations without double colons
655 (@code{::}), as these were introduced in Fortran 90 which also
656 introduced a new syntax for variable initializations. The syntax for
657 the individual initializers is as for the @code{DATA} statement, but
658 unlike in a @code{DATA} statement, an initializer only applies to the
659 variable immediately preceding. In other words, something like
660 @code{INTEGER I,J/2,3/} is not valid.
662 Examples of standard conforming code equivalent to the above example, are:
663 @smallexample
664 ! Fortran 90
665       INTEGER(4) :: i = 1, j = 2
666       REAL(8) :: x(2,2) = RESHAPE((/0.,0.,0.,1./),SHAPE(x))
667 ! Fortran 77
668       INTEGER  i, j
669       DOUBLE PRECISION x(2,2)
670       DATA i,j,x /1,2,3*0.,1./
671 @end smallexample
673 @c ---------------------------------------------------------------------
674 @c Contributing
675 @c ---------------------------------------------------------------------
677 @node Contributing
678 @chapter Contributing
679 @cindex Contributing
681 Free software is only possible if people contribute to efforts
682 to create it.
683 We're always in need of more people helping out with ideas
684 and comments, writing documentation and contributing code.
686 If you want to contribute to GNU Fortran 95,
687 have a look at the long lists of projects you can take on.
688 Some of these projects are small,
689 some of them are large;
690 some are completely orthogonal to the rest of what is
691 happening on @command{gfortran},
692 but others are ``mainstream'' projects in need of enthusiastic hackers.
693 All of these projects are important!
694 We'll eventually get around to the things here,
695 but they are also things doable by someone who is willing and able.
697 @menu
698 * Contributors::
699 * Projects::
700 @end menu
703 @node Contributors
704 @section Contributors to GNU Fortran 95
705 @cindex Contributors
706 @cindex Credits
707 @cindex Authors
709 Most of the parser was hand-crafted by @emph{Andy Vaught}, who is
710 also the initiator of the whole project.  Thanks Andy!
711 Most of the interface with GCC was written by @emph{Paul Brook}.
713 The following individuals have contributed code and/or
714 ideas and significant help to the gfortran project
715 (in no particular order): 
717 @itemize @minus
718 @item Andy Vaught
719 @item Katherine Holcomb
720 @item Tobias Schlüter
721 @item Steven Bosscher
722 @item Toon Moene
723 @item Tim Prince
724 @item Niels Kristian Bech Jensen
725 @item Steven Johnson
726 @item Paul Brook
727 @item Feng Wang
728 @item Bud Davis
729 @end itemize
731 The following people have contributed bug reports,
732 smaller or larger patches,
733 and much needed feedback and encouragement for the
734 @command{gfortran} project: 
736 @itemize @minus
737 @item Erik Schnetter
738 @item Bill Clodius
739 @item Kate Hedstrom
740 @end itemize
742 Many other individuals have helped debug,
743 test and improve @command{gfortran} over the past two years,
744 and we welcome you to do the same!
745 If you already have done so,
746 and you would like to see your name listed in the
747 list above, please contact us.
750 @node Projects
751 @section Projects
753 @table @emph
755 @item Help build the test suite
756 Solicit more code for donation to the test suite.
757 We can keep code private on request.
759 @item Bug hunting/squishing
760 Find bugs and write more test cases!
761 Test cases are especially very welcome,
762 because it allows us to concentrate on fixing bugs
763 instead of isolating them.
765 @item Smaller projects (``bug'' fixes):
766   @itemize @minus
767   @item Allow init exprs to be numbers raised to integer powers.
768   @item Implement correct rounding.
769   @item Implement F restrictions on Fortran 95 syntax.
770   @item See about making Emacs-parsable error messages.
771   @end itemize
772 @end table
774 If you wish to work on the runtime libraries,
775 please contact a project maintainer.
776 @c TODO: email!
779 @c ---------------------------------------------------------------------
780 @c Standards
781 @c ---------------------------------------------------------------------
783 @node Standards
784 @chapter Standards
785 @cindex Standards
787 The GNU Fortran 95 Compiler aims to be a conforming implementation of
788 ISO/IEC 1539:1997 (Fortran 95).
790 In the future it may also support other variants and extensions to the Fortran
791 language.  This includes ANSI Fortran 77, Fortran 90, Fortran 2000 (not yet
792 finalized), and OpenMP.
794 @node Index
795 @unnumbered Index
797 @printindex cp
799 @bye